blob: 5a240f5e6cebb4929a592dbbd2cea8985b5d749f [file] [log] [blame]
Arne Jansena2de7332011-03-08 14:14:00 +01001/*
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002 * Copyright (C) 2011, 2012 STRATO. All rights reserved.
Arne Jansena2de7332011-03-08 14:14:00 +01003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Arne Jansena2de7332011-03-08 14:14:00 +010019#include <linux/blkdev.h>
Jan Schmidt558540c2011-06-13 19:59:12 +020020#include <linux/ratelimit.h>
Arne Jansena2de7332011-03-08 14:14:00 +010021#include "ctree.h"
22#include "volumes.h"
23#include "disk-io.h"
24#include "ordered-data.h"
Jan Schmidt0ef8e452011-06-13 20:04:15 +020025#include "transaction.h"
Jan Schmidt558540c2011-06-13 19:59:12 +020026#include "backref.h"
Jan Schmidt5da6fcb2011-08-04 18:11:04 +020027#include "extent_io.h"
Stefan Behrensff023aa2012-11-06 11:43:11 +010028#include "dev-replace.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010029#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040030#include "rcu-string.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050031#include "raid56.h"
Arne Jansena2de7332011-03-08 14:14:00 +010032
33/*
34 * This is only the first step towards a full-features scrub. It reads all
35 * extent and super block and verifies the checksums. In case a bad checksum
36 * is found or the extent cannot be read, good data will be written back if
37 * any can be found.
38 *
39 * Future enhancements:
Arne Jansena2de7332011-03-08 14:14:00 +010040 * - In case an unrepairable extent is encountered, track which files are
41 * affected and report them
Arne Jansena2de7332011-03-08 14:14:00 +010042 * - track and record media errors, throw out bad devices
Arne Jansena2de7332011-03-08 14:14:00 +010043 * - add a mode to also read unallocated space
Arne Jansena2de7332011-03-08 14:14:00 +010044 */
45
Stefan Behrensb5d67f62012-03-27 14:21:27 -040046struct scrub_block;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010047struct scrub_ctx;
Arne Jansena2de7332011-03-08 14:14:00 +010048
Stefan Behrensff023aa2012-11-06 11:43:11 +010049/*
50 * the following three values only influence the performance.
51 * The last one configures the number of parallel and outstanding I/O
52 * operations. The first two values configure an upper limit for the number
53 * of (dynamically allocated) pages that are added to a bio.
54 */
55#define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */
56#define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */
57#define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */
Stefan Behrens7a9e9982012-11-02 14:58:04 +010058
59/*
60 * the following value times PAGE_SIZE needs to be large enough to match the
61 * largest node/leaf/sector size that shall be supported.
62 * Values larger than BTRFS_STRIPE_LEN are not supported.
63 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -040064#define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */
Arne Jansena2de7332011-03-08 14:14:00 +010065
66struct scrub_page {
Stefan Behrensb5d67f62012-03-27 14:21:27 -040067 struct scrub_block *sblock;
68 struct page *page;
Stefan Behrens442a4f62012-05-25 16:06:08 +020069 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010070 u64 flags; /* extent flags */
71 u64 generation;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040072 u64 logical;
73 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010074 u64 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +010075 atomic_t ref_count;
Stefan Behrensb5d67f62012-03-27 14:21:27 -040076 struct {
77 unsigned int mirror_num:8;
78 unsigned int have_csum:1;
79 unsigned int io_error:1;
80 };
Arne Jansena2de7332011-03-08 14:14:00 +010081 u8 csum[BTRFS_CSUM_SIZE];
82};
83
84struct scrub_bio {
85 int index;
Stefan Behrensd9d181c2012-11-02 09:58:09 +010086 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +010087 struct btrfs_device *dev;
Arne Jansena2de7332011-03-08 14:14:00 +010088 struct bio *bio;
89 int err;
90 u64 logical;
91 u64 physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +010092#if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO
93 struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO];
94#else
95 struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO];
96#endif
Stefan Behrensb5d67f62012-03-27 14:21:27 -040097 int page_count;
Arne Jansena2de7332011-03-08 14:14:00 +010098 int next_free;
Qu Wenruo0339ef22014-02-28 10:46:17 +080099 struct btrfs_work_struct
100 work;
Arne Jansena2de7332011-03-08 14:14:00 +0100101};
102
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400103struct scrub_block {
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100104 struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400105 int page_count;
106 atomic_t outstanding_pages;
107 atomic_t ref_count; /* free mem on transition to zero */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100108 struct scrub_ctx *sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400109 struct {
110 unsigned int header_error:1;
111 unsigned int checksum_error:1;
112 unsigned int no_io_error_seen:1;
Stefan Behrens442a4f62012-05-25 16:06:08 +0200113 unsigned int generation_error:1; /* also sets header_error */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400114 };
115};
116
Stefan Behrensff023aa2012-11-06 11:43:11 +0100117struct scrub_wr_ctx {
118 struct scrub_bio *wr_curr_bio;
119 struct btrfs_device *tgtdev;
120 int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */
121 atomic_t flush_all_writes;
122 struct mutex wr_lock;
123};
124
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100125struct scrub_ctx {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100126 struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX];
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100127 struct btrfs_root *dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +0100128 int first_free;
129 int curr;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100130 atomic_t bios_in_flight;
131 atomic_t workers_pending;
Arne Jansena2de7332011-03-08 14:14:00 +0100132 spinlock_t list_lock;
133 wait_queue_head_t list_wait;
134 u16 csum_size;
135 struct list_head csum_list;
136 atomic_t cancel_req;
Arne Jansen86287642011-03-23 16:34:19 +0100137 int readonly;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100138 int pages_per_rd_bio;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400139 u32 sectorsize;
140 u32 nodesize;
141 u32 leafsize;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100142
143 int is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100144 struct scrub_wr_ctx wr_ctx;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100145
Arne Jansena2de7332011-03-08 14:14:00 +0100146 /*
147 * statistics
148 */
149 struct btrfs_scrub_progress stat;
150 spinlock_t stat_lock;
151};
152
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200153struct scrub_fixup_nodatasum {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100154 struct scrub_ctx *sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100155 struct btrfs_device *dev;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200156 u64 logical;
157 struct btrfs_root *root;
Qu Wenruo0339ef22014-02-28 10:46:17 +0800158 struct btrfs_work_struct
159 work;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200160 int mirror_num;
161};
162
Josef Bacik652f25a2013-09-12 16:58:28 -0400163struct scrub_nocow_inode {
164 u64 inum;
165 u64 offset;
166 u64 root;
167 struct list_head list;
168};
169
Stefan Behrensff023aa2012-11-06 11:43:11 +0100170struct scrub_copy_nocow_ctx {
171 struct scrub_ctx *sctx;
172 u64 logical;
173 u64 len;
174 int mirror_num;
175 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -0400176 struct list_head inodes;
Qu Wenruo0339ef22014-02-28 10:46:17 +0800177 struct btrfs_work_struct
178 work;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100179};
180
Jan Schmidt558540c2011-06-13 19:59:12 +0200181struct scrub_warning {
182 struct btrfs_path *path;
183 u64 extent_item_size;
184 char *scratch_buf;
185 char *msg_buf;
186 const char *errstr;
187 sector_t sector;
188 u64 logical;
189 struct btrfs_device *dev;
190 int msg_bufsize;
191 int scratch_bufsize;
192};
193
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400194
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100195static void scrub_pending_bio_inc(struct scrub_ctx *sctx);
196static void scrub_pending_bio_dec(struct scrub_ctx *sctx);
197static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx);
198static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400199static int scrub_handle_errored_block(struct scrub_block *sblock_to_check);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100200static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100201 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100202 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400203 u64 length, u64 logical,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100204 struct scrub_block *sblocks_for_recheck);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100205static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
206 struct scrub_block *sblock, int is_metadata,
207 int have_csum, u8 *csum, u64 generation,
208 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400209static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
210 struct scrub_block *sblock,
211 int is_metadata, int have_csum,
212 const u8 *csum, u64 generation,
213 u16 csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400214static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
215 struct scrub_block *sblock_good,
216 int force_write);
217static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
218 struct scrub_block *sblock_good,
219 int page_num, int force_write);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100220static void scrub_write_block_to_dev_replace(struct scrub_block *sblock);
221static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
222 int page_num);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400223static int scrub_checksum_data(struct scrub_block *sblock);
224static int scrub_checksum_tree_block(struct scrub_block *sblock);
225static int scrub_checksum_super(struct scrub_block *sblock);
226static void scrub_block_get(struct scrub_block *sblock);
227static void scrub_block_put(struct scrub_block *sblock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100228static void scrub_page_get(struct scrub_page *spage);
229static void scrub_page_put(struct scrub_page *spage);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100230static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
231 struct scrub_page *spage);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100232static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100233 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +0100234 u64 gen, int mirror_num, u8 *csum, int force,
235 u64 physical_for_dev_replace);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400236static void scrub_bio_end_io(struct bio *bio, int err);
Qu Wenruo0339ef22014-02-28 10:46:17 +0800237static void scrub_bio_end_io_worker(struct btrfs_work_struct *work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400238static void scrub_block_complete(struct scrub_block *sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100239static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
240 u64 extent_logical, u64 extent_len,
241 u64 *extent_physical,
242 struct btrfs_device **extent_dev,
243 int *extent_mirror_num);
244static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
245 struct scrub_wr_ctx *wr_ctx,
246 struct btrfs_fs_info *fs_info,
247 struct btrfs_device *dev,
248 int is_dev_replace);
249static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx);
250static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
251 struct scrub_page *spage);
252static void scrub_wr_submit(struct scrub_ctx *sctx);
253static void scrub_wr_bio_end_io(struct bio *bio, int err);
Qu Wenruo0339ef22014-02-28 10:46:17 +0800254static void scrub_wr_bio_end_io_worker(struct btrfs_work_struct *work);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100255static int write_page_nocow(struct scrub_ctx *sctx,
256 u64 physical_for_dev_replace, struct page *page);
257static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
Josef Bacik652f25a2013-09-12 16:58:28 -0400258 struct scrub_copy_nocow_ctx *ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100259static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
260 int mirror_num, u64 physical_for_dev_replace);
Qu Wenruo0339ef22014-02-28 10:46:17 +0800261static void copy_nocow_pages_worker(struct btrfs_work_struct *work);
Wang Shilongcb7ab022013-12-04 21:16:53 +0800262static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Wang Shilong3cb09292013-12-04 21:15:19 +0800263static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info);
Stefan Behrens1623ede2012-03-27 14:21:26 -0400264
265
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100266static void scrub_pending_bio_inc(struct scrub_ctx *sctx)
267{
268 atomic_inc(&sctx->bios_in_flight);
269}
270
271static void scrub_pending_bio_dec(struct scrub_ctx *sctx)
272{
273 atomic_dec(&sctx->bios_in_flight);
274 wake_up(&sctx->list_wait);
275}
276
Wang Shilongcb7ab022013-12-04 21:16:53 +0800277static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
Wang Shilong3cb09292013-12-04 21:15:19 +0800278{
279 while (atomic_read(&fs_info->scrub_pause_req)) {
280 mutex_unlock(&fs_info->scrub_lock);
281 wait_event(fs_info->scrub_pause_wait,
282 atomic_read(&fs_info->scrub_pause_req) == 0);
283 mutex_lock(&fs_info->scrub_lock);
284 }
285}
286
Wang Shilongcb7ab022013-12-04 21:16:53 +0800287static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info)
288{
289 atomic_inc(&fs_info->scrubs_paused);
290 wake_up(&fs_info->scrub_pause_wait);
291
292 mutex_lock(&fs_info->scrub_lock);
293 __scrub_blocked_if_needed(fs_info);
294 atomic_dec(&fs_info->scrubs_paused);
295 mutex_unlock(&fs_info->scrub_lock);
296
297 wake_up(&fs_info->scrub_pause_wait);
298}
299
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100300/*
301 * used for workers that require transaction commits (i.e., for the
302 * NOCOW case)
303 */
304static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx)
305{
306 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
307
308 /*
309 * increment scrubs_running to prevent cancel requests from
310 * completing as long as a worker is running. we must also
311 * increment scrubs_paused to prevent deadlocking on pause
312 * requests used for transactions commits (as the worker uses a
313 * transaction context). it is safe to regard the worker
314 * as paused for all matters practical. effectively, we only
315 * avoid cancellation requests from completing.
316 */
317 mutex_lock(&fs_info->scrub_lock);
318 atomic_inc(&fs_info->scrubs_running);
319 atomic_inc(&fs_info->scrubs_paused);
320 mutex_unlock(&fs_info->scrub_lock);
Wang Shilong32a44782014-02-19 19:24:19 +0800321
322 /*
323 * check if @scrubs_running=@scrubs_paused condition
324 * inside wait_event() is not an atomic operation.
325 * which means we may inc/dec @scrub_running/paused
326 * at any time. Let's wake up @scrub_pause_wait as
327 * much as we can to let commit transaction blocked less.
328 */
329 wake_up(&fs_info->scrub_pause_wait);
330
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100331 atomic_inc(&sctx->workers_pending);
332}
333
334/* used for workers that require transaction commits */
335static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx)
336{
337 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
338
339 /*
340 * see scrub_pending_trans_workers_inc() why we're pretending
341 * to be paused in the scrub counters
342 */
343 mutex_lock(&fs_info->scrub_lock);
344 atomic_dec(&fs_info->scrubs_running);
345 atomic_dec(&fs_info->scrubs_paused);
346 mutex_unlock(&fs_info->scrub_lock);
347 atomic_dec(&sctx->workers_pending);
348 wake_up(&fs_info->scrub_pause_wait);
349 wake_up(&sctx->list_wait);
350}
351
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100352static void scrub_free_csums(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100353{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100354 while (!list_empty(&sctx->csum_list)) {
Arne Jansena2de7332011-03-08 14:14:00 +0100355 struct btrfs_ordered_sum *sum;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100356 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +0100357 struct btrfs_ordered_sum, list);
358 list_del(&sum->list);
359 kfree(sum);
360 }
361}
362
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100363static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100364{
365 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100366
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100367 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100368 return;
369
Stefan Behrensff023aa2012-11-06 11:43:11 +0100370 scrub_free_wr_ctx(&sctx->wr_ctx);
371
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400372 /* this can happen when scrub is cancelled */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100373 if (sctx->curr != -1) {
374 struct scrub_bio *sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400375
376 for (i = 0; i < sbio->page_count; i++) {
Stefan Behrensff023aa2012-11-06 11:43:11 +0100377 WARN_ON(!sbio->pagev[i]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400378 scrub_block_put(sbio->pagev[i]->sblock);
379 }
380 bio_put(sbio->bio);
381 }
382
Stefan Behrensff023aa2012-11-06 11:43:11 +0100383 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100384 struct scrub_bio *sbio = sctx->bios[i];
Arne Jansena2de7332011-03-08 14:14:00 +0100385
386 if (!sbio)
387 break;
Arne Jansena2de7332011-03-08 14:14:00 +0100388 kfree(sbio);
389 }
390
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100391 scrub_free_csums(sctx);
392 kfree(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100393}
394
395static noinline_for_stack
Stefan Behrens63a212a2012-11-05 18:29:28 +0100396struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +0100397{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100398 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100399 int i;
Arne Jansena2de7332011-03-08 14:14:00 +0100400 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100401 int pages_per_rd_bio;
402 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +0100403
Stefan Behrensff023aa2012-11-06 11:43:11 +0100404 /*
405 * the setting of pages_per_rd_bio is correct for scrub but might
406 * be wrong for the dev_replace code where we might read from
407 * different devices in the initial huge bios. However, that
408 * code is able to correctly handle the case when adding a page
409 * to a bio fails.
410 */
411 if (dev->bdev)
412 pages_per_rd_bio = min_t(int, SCRUB_PAGES_PER_RD_BIO,
413 bio_get_nr_vecs(dev->bdev));
414 else
415 pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100416 sctx = kzalloc(sizeof(*sctx), GFP_NOFS);
417 if (!sctx)
Arne Jansena2de7332011-03-08 14:14:00 +0100418 goto nomem;
Stefan Behrens63a212a2012-11-05 18:29:28 +0100419 sctx->is_dev_replace = is_dev_replace;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100420 sctx->pages_per_rd_bio = pages_per_rd_bio;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100421 sctx->curr = -1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100422 sctx->dev_root = dev->dev_root;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100423 for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) {
Arne Jansena2de7332011-03-08 14:14:00 +0100424 struct scrub_bio *sbio;
425
426 sbio = kzalloc(sizeof(*sbio), GFP_NOFS);
427 if (!sbio)
428 goto nomem;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100429 sctx->bios[i] = sbio;
Arne Jansena2de7332011-03-08 14:14:00 +0100430
Arne Jansena2de7332011-03-08 14:14:00 +0100431 sbio->index = i;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100432 sbio->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400433 sbio->page_count = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +0800434 btrfs_init_work(&sbio->work, scrub_bio_end_io_worker,
435 NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +0100436
Stefan Behrensff023aa2012-11-06 11:43:11 +0100437 if (i != SCRUB_BIOS_PER_SCTX - 1)
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100438 sctx->bios[i]->next_free = i + 1;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200439 else
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100440 sctx->bios[i]->next_free = -1;
Arne Jansena2de7332011-03-08 14:14:00 +0100441 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100442 sctx->first_free = 0;
443 sctx->nodesize = dev->dev_root->nodesize;
444 sctx->leafsize = dev->dev_root->leafsize;
445 sctx->sectorsize = dev->dev_root->sectorsize;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100446 atomic_set(&sctx->bios_in_flight, 0);
447 atomic_set(&sctx->workers_pending, 0);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100448 atomic_set(&sctx->cancel_req, 0);
449 sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy);
450 INIT_LIST_HEAD(&sctx->csum_list);
Arne Jansena2de7332011-03-08 14:14:00 +0100451
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100452 spin_lock_init(&sctx->list_lock);
453 spin_lock_init(&sctx->stat_lock);
454 init_waitqueue_head(&sctx->list_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100455
456 ret = scrub_setup_wr_ctx(sctx, &sctx->wr_ctx, fs_info,
457 fs_info->dev_replace.tgtdev, is_dev_replace);
458 if (ret) {
459 scrub_free_ctx(sctx);
460 return ERR_PTR(ret);
461 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100462 return sctx;
Arne Jansena2de7332011-03-08 14:14:00 +0100463
464nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100465 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +0100466 return ERR_PTR(-ENOMEM);
467}
468
Stefan Behrensff023aa2012-11-06 11:43:11 +0100469static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
470 void *warn_ctx)
Jan Schmidt558540c2011-06-13 19:59:12 +0200471{
472 u64 isize;
473 u32 nlink;
474 int ret;
475 int i;
476 struct extent_buffer *eb;
477 struct btrfs_inode_item *inode_item;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100478 struct scrub_warning *swarn = warn_ctx;
Jan Schmidt558540c2011-06-13 19:59:12 +0200479 struct btrfs_fs_info *fs_info = swarn->dev->dev_root->fs_info;
480 struct inode_fs_paths *ipath = NULL;
481 struct btrfs_root *local_root;
482 struct btrfs_key root_key;
483
484 root_key.objectid = root;
485 root_key.type = BTRFS_ROOT_ITEM_KEY;
486 root_key.offset = (u64)-1;
487 local_root = btrfs_read_fs_root_no_name(fs_info, &root_key);
488 if (IS_ERR(local_root)) {
489 ret = PTR_ERR(local_root);
490 goto err;
491 }
492
493 ret = inode_item_info(inum, 0, local_root, swarn->path);
494 if (ret) {
495 btrfs_release_path(swarn->path);
496 goto err;
497 }
498
499 eb = swarn->path->nodes[0];
500 inode_item = btrfs_item_ptr(eb, swarn->path->slots[0],
501 struct btrfs_inode_item);
502 isize = btrfs_inode_size(eb, inode_item);
503 nlink = btrfs_inode_nlink(eb, inode_item);
504 btrfs_release_path(swarn->path);
505
506 ipath = init_ipath(4096, local_root, swarn->path);
Dan Carpenter26bdef52011-11-16 11:28:01 +0300507 if (IS_ERR(ipath)) {
508 ret = PTR_ERR(ipath);
509 ipath = NULL;
510 goto err;
511 }
Jan Schmidt558540c2011-06-13 19:59:12 +0200512 ret = paths_from_inode(inum, ipath);
513
514 if (ret < 0)
515 goto err;
516
517 /*
518 * we deliberately ignore the bit ipath might have been too small to
519 * hold all of the paths here
520 */
521 for (i = 0; i < ipath->fspath->elem_cnt; ++i)
Frank Holtonefe120a2013-12-20 11:37:06 -0500522 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200523 "%s, sector %llu, root %llu, inode %llu, offset %llu, "
524 "length %llu, links %u (path: %s)\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400525 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200526 (unsigned long long)swarn->sector, root, inum, offset,
527 min(isize - offset, (u64)PAGE_SIZE), nlink,
Jeff Mahoney745c4d82011-11-20 07:31:57 -0500528 (char *)(unsigned long)ipath->fspath->val[i]);
Jan Schmidt558540c2011-06-13 19:59:12 +0200529
530 free_ipath(ipath);
531 return 0;
532
533err:
Frank Holtonefe120a2013-12-20 11:37:06 -0500534 printk_in_rcu(KERN_WARNING "BTRFS: %s at logical %llu on dev "
Jan Schmidt558540c2011-06-13 19:59:12 +0200535 "%s, sector %llu, root %llu, inode %llu, offset %llu: path "
536 "resolving failed with ret=%d\n", swarn->errstr,
Josef Bacik606686e2012-06-04 14:03:51 -0400537 swarn->logical, rcu_str_deref(swarn->dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200538 (unsigned long long)swarn->sector, root, inum, offset, ret);
539
540 free_ipath(ipath);
541 return 0;
542}
543
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400544static void scrub_print_warning(const char *errstr, struct scrub_block *sblock)
Jan Schmidt558540c2011-06-13 19:59:12 +0200545{
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100546 struct btrfs_device *dev;
547 struct btrfs_fs_info *fs_info;
Jan Schmidt558540c2011-06-13 19:59:12 +0200548 struct btrfs_path *path;
549 struct btrfs_key found_key;
550 struct extent_buffer *eb;
551 struct btrfs_extent_item *ei;
552 struct scrub_warning swarn;
Jan Schmidt558540c2011-06-13 19:59:12 +0200553 unsigned long ptr = 0;
Jan Schmidt4692cf52011-12-02 14:56:41 +0100554 u64 extent_item_pos;
Liu Bo69917e42012-09-07 20:01:28 -0600555 u64 flags = 0;
556 u64 ref_root;
557 u32 item_size;
558 u8 ref_level;
559 const int bufsize = 4096;
560 int ret;
Jan Schmidt558540c2011-06-13 19:59:12 +0200561
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100562 WARN_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100563 dev = sblock->pagev[0]->dev;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100564 fs_info = sblock->sctx->dev_root->fs_info;
565
Jan Schmidt558540c2011-06-13 19:59:12 +0200566 path = btrfs_alloc_path();
567
568 swarn.scratch_buf = kmalloc(bufsize, GFP_NOFS);
569 swarn.msg_buf = kmalloc(bufsize, GFP_NOFS);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100570 swarn.sector = (sblock->pagev[0]->physical) >> 9;
571 swarn.logical = sblock->pagev[0]->logical;
Jan Schmidt558540c2011-06-13 19:59:12 +0200572 swarn.errstr = errstr;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100573 swarn.dev = NULL;
Jan Schmidt558540c2011-06-13 19:59:12 +0200574 swarn.msg_bufsize = bufsize;
575 swarn.scratch_bufsize = bufsize;
576
577 if (!path || !swarn.scratch_buf || !swarn.msg_buf)
578 goto out;
579
Liu Bo69917e42012-09-07 20:01:28 -0600580 ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
581 &flags);
Jan Schmidt558540c2011-06-13 19:59:12 +0200582 if (ret < 0)
583 goto out;
584
Jan Schmidt4692cf52011-12-02 14:56:41 +0100585 extent_item_pos = swarn.logical - found_key.objectid;
Jan Schmidt558540c2011-06-13 19:59:12 +0200586 swarn.extent_item_size = found_key.offset;
587
588 eb = path->nodes[0];
589 ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item);
590 item_size = btrfs_item_size_nr(eb, path->slots[0]);
591
Liu Bo69917e42012-09-07 20:01:28 -0600592 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Jan Schmidt558540c2011-06-13 19:59:12 +0200593 do {
594 ret = tree_backref_for_extent(&ptr, eb, ei, item_size,
595 &ref_root, &ref_level);
Josef Bacik606686e2012-06-04 14:03:51 -0400596 printk_in_rcu(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -0500597 "BTRFS: %s at logical %llu on dev %s, "
Jan Schmidt558540c2011-06-13 19:59:12 +0200598 "sector %llu: metadata %s (level %d) in tree "
Josef Bacik606686e2012-06-04 14:03:51 -0400599 "%llu\n", errstr, swarn.logical,
600 rcu_str_deref(dev->name),
Jan Schmidt558540c2011-06-13 19:59:12 +0200601 (unsigned long long)swarn.sector,
602 ref_level ? "node" : "leaf",
603 ret < 0 ? -1 : ref_level,
604 ret < 0 ? -1 : ref_root);
605 } while (ret != 1);
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600606 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200607 } else {
Josef Bacikd8fe29e2013-03-29 08:09:34 -0600608 btrfs_release_path(path);
Jan Schmidt558540c2011-06-13 19:59:12 +0200609 swarn.path = path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100610 swarn.dev = dev;
Jan Schmidt7a3ae2f2012-03-23 17:32:28 +0100611 iterate_extent_inodes(fs_info, found_key.objectid,
612 extent_item_pos, 1,
Jan Schmidt558540c2011-06-13 19:59:12 +0200613 scrub_print_warning_inode, &swarn);
614 }
615
616out:
617 btrfs_free_path(path);
618 kfree(swarn.scratch_buf);
619 kfree(swarn.msg_buf);
620}
621
Stefan Behrensff023aa2012-11-06 11:43:11 +0100622static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200623{
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200624 struct page *page = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200625 unsigned long index;
Stefan Behrensff023aa2012-11-06 11:43:11 +0100626 struct scrub_fixup_nodatasum *fixup = fixup_ctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200627 int ret;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200628 int corrected = 0;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200629 struct btrfs_key key;
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200630 struct inode *inode = NULL;
Liu Bo6f1c3602013-01-29 03:22:10 +0000631 struct btrfs_fs_info *fs_info;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200632 u64 end = offset + PAGE_SIZE - 1;
633 struct btrfs_root *local_root;
Liu Bo6f1c3602013-01-29 03:22:10 +0000634 int srcu_index;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200635
636 key.objectid = root;
637 key.type = BTRFS_ROOT_ITEM_KEY;
638 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +0000639
640 fs_info = fixup->root->fs_info;
641 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
642
643 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
644 if (IS_ERR(local_root)) {
645 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200646 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +0000647 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200648
649 key.type = BTRFS_INODE_ITEM_KEY;
650 key.objectid = inum;
651 key.offset = 0;
Liu Bo6f1c3602013-01-29 03:22:10 +0000652 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
653 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200654 if (IS_ERR(inode))
655 return PTR_ERR(inode);
656
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200657 index = offset >> PAGE_CACHE_SHIFT;
658
659 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200660 if (!page) {
661 ret = -ENOMEM;
662 goto out;
663 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200664
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200665 if (PageUptodate(page)) {
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200666 if (PageDirty(page)) {
667 /*
668 * we need to write the data to the defect sector. the
669 * data that was in that sector is not in memory,
670 * because the page was modified. we must not write the
671 * modified page to that sector.
672 *
673 * TODO: what could be done here: wait for the delalloc
674 * runner to write out that page (might involve
675 * COW) and see whether the sector is still
676 * referenced afterwards.
677 *
678 * For the meantime, we'll treat this error
679 * incorrectable, although there is a chance that a
680 * later scrub will find the bad sector again and that
681 * there's no dirty page in memory, then.
682 */
683 ret = -EIO;
684 goto out;
685 }
Stefan Behrens3ec706c2012-11-05 15:46:42 +0100686 fs_info = BTRFS_I(inode)->root->fs_info;
687 ret = repair_io_failure(fs_info, offset, PAGE_SIZE,
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200688 fixup->logical, page,
689 fixup->mirror_num);
690 unlock_page(page);
691 corrected = !ret;
692 } else {
693 /*
694 * we need to get good data first. the general readpage path
695 * will call repair_io_failure for us, we just have to make
696 * sure we read the bad mirror.
697 */
698 ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
699 EXTENT_DAMAGED, GFP_NOFS);
700 if (ret) {
701 /* set_extent_bits should give proper error */
702 WARN_ON(ret > 0);
703 if (ret > 0)
704 ret = -EFAULT;
705 goto out;
706 }
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200707
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200708 ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page,
709 btrfs_get_extent,
710 fixup->mirror_num);
711 wait_on_page_locked(page);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200712
Jan Schmidt5da6fcb2011-08-04 18:11:04 +0200713 corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset,
714 end, EXTENT_DAMAGED, 0, NULL);
715 if (!corrected)
716 clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end,
717 EXTENT_DAMAGED, GFP_NOFS);
718 }
719
720out:
721 if (page)
722 put_page(page);
723 if (inode)
724 iput(inode);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200725
726 if (ret < 0)
727 return ret;
728
729 if (ret == 0 && corrected) {
730 /*
731 * we only need to call readpage for one of the inodes belonging
732 * to this extent. so make iterate_extent_inodes stop
733 */
734 return 1;
735 }
736
737 return -EIO;
738}
739
Qu Wenruo0339ef22014-02-28 10:46:17 +0800740static void scrub_fixup_nodatasum(struct btrfs_work_struct *work)
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200741{
742 int ret;
743 struct scrub_fixup_nodatasum *fixup;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100744 struct scrub_ctx *sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200745 struct btrfs_trans_handle *trans = NULL;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200746 struct btrfs_path *path;
747 int uncorrectable = 0;
748
749 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100750 sctx = fixup->sctx;
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200751
752 path = btrfs_alloc_path();
753 if (!path) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100754 spin_lock(&sctx->stat_lock);
755 ++sctx->stat.malloc_errors;
756 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200757 uncorrectable = 1;
758 goto out;
759 }
760
761 trans = btrfs_join_transaction(fixup->root);
762 if (IS_ERR(trans)) {
763 uncorrectable = 1;
764 goto out;
765 }
766
767 /*
768 * the idea is to trigger a regular read through the standard path. we
769 * read a page from the (failed) logical address by specifying the
770 * corresponding copynum of the failed sector. thus, that readpage is
771 * expected to fail.
772 * that is the point where on-the-fly error correction will kick in
773 * (once it's finished) and rewrite the failed sector if a good copy
774 * can be found.
775 */
776 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info,
777 path, scrub_fixup_readpage,
778 fixup);
779 if (ret < 0) {
780 uncorrectable = 1;
781 goto out;
782 }
783 WARN_ON(ret != 1);
784
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100785 spin_lock(&sctx->stat_lock);
786 ++sctx->stat.corrected_errors;
787 spin_unlock(&sctx->stat_lock);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200788
789out:
790 if (trans && !IS_ERR(trans))
791 btrfs_end_transaction(trans, fixup->root);
792 if (uncorrectable) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100793 spin_lock(&sctx->stat_lock);
794 ++sctx->stat.uncorrectable_errors;
795 spin_unlock(&sctx->stat_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +0100796 btrfs_dev_replace_stats_inc(
797 &sctx->dev_root->fs_info->dev_replace.
798 num_uncorrectable_read_errors);
Frank Holtonefe120a2013-12-20 11:37:06 -0500799 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
800 "unable to fixup (nodatasum) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200801 fixup->logical, rcu_str_deref(fixup->dev->name));
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200802 }
803
804 btrfs_free_path(path);
805 kfree(fixup);
806
Stefan Behrensb6bfebc2012-11-02 16:44:58 +0100807 scrub_pending_trans_workers_dec(sctx);
Jan Schmidt0ef8e452011-06-13 20:04:15 +0200808}
809
Arne Jansena2de7332011-03-08 14:14:00 +0100810/*
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400811 * scrub_handle_errored_block gets called when either verification of the
812 * pages failed or the bio failed to read, e.g. with EIO. In the latter
813 * case, this function handles all pages in the bio, even though only one
814 * may be bad.
815 * The goal of this function is to repair the errored block by using the
816 * contents of one of the mirrors.
Arne Jansena2de7332011-03-08 14:14:00 +0100817 */
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400818static int scrub_handle_errored_block(struct scrub_block *sblock_to_check)
Arne Jansena2de7332011-03-08 14:14:00 +0100819{
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100820 struct scrub_ctx *sctx = sblock_to_check->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100821 struct btrfs_device *dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400822 struct btrfs_fs_info *fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +0100823 u64 length;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400824 u64 logical;
825 u64 generation;
826 unsigned int failed_mirror_index;
827 unsigned int is_metadata;
828 unsigned int have_csum;
829 u8 *csum;
830 struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */
831 struct scrub_block *sblock_bad;
Arne Jansena2de7332011-03-08 14:14:00 +0100832 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400833 int mirror_index;
834 int page_num;
835 int success;
836 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
837 DEFAULT_RATELIMIT_BURST);
Arne Jansena2de7332011-03-08 14:14:00 +0100838
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400839 BUG_ON(sblock_to_check->page_count < 1);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100840 fs_info = sctx->dev_root->fs_info;
Stefan Behrens4ded4f62012-11-14 18:57:29 +0000841 if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) {
842 /*
843 * if we find an error in a super block, we just report it.
844 * They will get written with the next transaction commit
845 * anyway
846 */
847 spin_lock(&sctx->stat_lock);
848 ++sctx->stat.super_errors;
849 spin_unlock(&sctx->stat_lock);
850 return 0;
851 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400852 length = sblock_to_check->page_count * PAGE_SIZE;
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100853 logical = sblock_to_check->pagev[0]->logical;
854 generation = sblock_to_check->pagev[0]->generation;
855 BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1);
856 failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1;
857 is_metadata = !(sblock_to_check->pagev[0]->flags &
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400858 BTRFS_EXTENT_FLAG_DATA);
Stefan Behrens7a9e9982012-11-02 14:58:04 +0100859 have_csum = sblock_to_check->pagev[0]->have_csum;
860 csum = sblock_to_check->pagev[0]->csum;
861 dev = sblock_to_check->pagev[0]->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400862
Stefan Behrensff023aa2012-11-06 11:43:11 +0100863 if (sctx->is_dev_replace && !is_metadata && !have_csum) {
864 sblocks_for_recheck = NULL;
865 goto nodatasum_case;
866 }
867
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400868 /*
869 * read all mirrors one after the other. This includes to
870 * re-read the extent or metadata block that failed (that was
871 * the cause that this fixup code is called) another time,
872 * page by page this time in order to know which pages
873 * caused I/O errors and which ones are good (for all mirrors).
874 * It is the goal to handle the situation when more than one
875 * mirror contains I/O errors, but the errors do not
876 * overlap, i.e. the data can be repaired by selecting the
877 * pages from those mirrors without I/O error on the
878 * particular pages. One example (with blocks >= 2 * PAGE_SIZE)
879 * would be that mirror #1 has an I/O error on the first page,
880 * the second page is good, and mirror #2 has an I/O error on
881 * the second page, but the first page is good.
882 * Then the first page of the first mirror can be repaired by
883 * taking the first page of the second mirror, and the
884 * second page of the second mirror can be repaired by
885 * copying the contents of the 2nd page of the 1st mirror.
886 * One more note: if the pages of one mirror contain I/O
887 * errors, the checksum cannot be verified. In order to get
888 * the best data for repairing, the first attempt is to find
889 * a mirror without I/O errors and with a validated checksum.
890 * Only if this is not possible, the pages are picked from
891 * mirrors with I/O errors without considering the checksum.
892 * If the latter is the case, at the end, the checksum of the
893 * repaired area is verified in order to correctly maintain
894 * the statistics.
895 */
896
897 sblocks_for_recheck = kzalloc(BTRFS_MAX_MIRRORS *
898 sizeof(*sblocks_for_recheck),
899 GFP_NOFS);
900 if (!sblocks_for_recheck) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100901 spin_lock(&sctx->stat_lock);
902 sctx->stat.malloc_errors++;
903 sctx->stat.read_errors++;
904 sctx->stat.uncorrectable_errors++;
905 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100906 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400907 goto out;
908 }
909
910 /* setup the context, map the logical blocks and alloc the pages */
Stefan Behrensff023aa2012-11-06 11:43:11 +0100911 ret = scrub_setup_recheck_block(sctx, fs_info, sblock_to_check, length,
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400912 logical, sblocks_for_recheck);
913 if (ret) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100914 spin_lock(&sctx->stat_lock);
915 sctx->stat.read_errors++;
916 sctx->stat.uncorrectable_errors++;
917 spin_unlock(&sctx->stat_lock);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100918 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400919 goto out;
920 }
921 BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS);
922 sblock_bad = sblocks_for_recheck + failed_mirror_index;
923
924 /* build and submit the bios for the failed mirror, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +0100925 scrub_recheck_block(fs_info, sblock_bad, is_metadata, have_csum,
926 csum, generation, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400927
928 if (!sblock_bad->header_error && !sblock_bad->checksum_error &&
929 sblock_bad->no_io_error_seen) {
930 /*
931 * the error disappeared after reading page by page, or
932 * the area was part of a huge bio and other parts of the
933 * bio caused I/O errors, or the block layer merged several
934 * read requests into one and the error is caused by a
935 * different bio (usually one of the two latter cases is
936 * the cause)
937 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100938 spin_lock(&sctx->stat_lock);
939 sctx->stat.unverified_errors++;
940 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400941
Stefan Behrensff023aa2012-11-06 11:43:11 +0100942 if (sctx->is_dev_replace)
943 scrub_write_block_to_dev_replace(sblock_bad);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400944 goto out;
945 }
946
947 if (!sblock_bad->no_io_error_seen) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100948 spin_lock(&sctx->stat_lock);
949 sctx->stat.read_errors++;
950 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400951 if (__ratelimit(&_rs))
952 scrub_print_warning("i/o error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100953 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400954 } else if (sblock_bad->checksum_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100955 spin_lock(&sctx->stat_lock);
956 sctx->stat.csum_errors++;
957 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400958 if (__ratelimit(&_rs))
959 scrub_print_warning("checksum error", sblock_to_check);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100960 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200961 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400962 } else if (sblock_bad->header_error) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100963 spin_lock(&sctx->stat_lock);
964 sctx->stat.verify_errors++;
965 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400966 if (__ratelimit(&_rs))
967 scrub_print_warning("checksum/header error",
968 sblock_to_check);
Stefan Behrens442a4f62012-05-25 16:06:08 +0200969 if (sblock_bad->generation_error)
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100970 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200971 BTRFS_DEV_STAT_GENERATION_ERRS);
972 else
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100973 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +0200974 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400975 }
976
Ilya Dryomov33ef30a2013-11-03 19:06:38 +0200977 if (sctx->readonly) {
978 ASSERT(!sctx->is_dev_replace);
979 goto out;
980 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400981
982 if (!is_metadata && !have_csum) {
983 struct scrub_fixup_nodatasum *fixup_nodatasum;
984
Stefan Behrensff023aa2012-11-06 11:43:11 +0100985nodatasum_case:
986 WARN_ON(sctx->is_dev_replace);
987
Stefan Behrensb5d67f62012-03-27 14:21:27 -0400988 /*
989 * !is_metadata and !have_csum, this means that the data
990 * might not be COW'ed, that it might be modified
991 * concurrently. The general strategy to work on the
992 * commit root does not help in the case when COW is not
993 * used.
994 */
995 fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS);
996 if (!fixup_nodatasum)
997 goto did_not_correct_error;
Stefan Behrensd9d181c2012-11-02 09:58:09 +0100998 fixup_nodatasum->sctx = sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +0100999 fixup_nodatasum->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001000 fixup_nodatasum->logical = logical;
1001 fixup_nodatasum->root = fs_info->extent_root;
1002 fixup_nodatasum->mirror_num = failed_mirror_index + 1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001003 scrub_pending_trans_workers_inc(sctx);
Qu Wenruo0339ef22014-02-28 10:46:17 +08001004 btrfs_init_work(&fixup_nodatasum->work, scrub_fixup_nodatasum,
1005 NULL, NULL);
1006 btrfs_queue_work(fs_info->scrub_workers,
1007 &fixup_nodatasum->work);
Arne Jansena2de7332011-03-08 14:14:00 +01001008 goto out;
1009 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001010
1011 /*
1012 * now build and submit the bios for the other mirrors, check
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001013 * checksums.
1014 * First try to pick the mirror which is completely without I/O
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001015 * errors and also does not have a checksum error.
1016 * If one is found, and if a checksum is present, the full block
1017 * that is known to contain an error is rewritten. Afterwards
1018 * the block is known to be corrected.
1019 * If a mirror is found which is completely correct, and no
1020 * checksum is present, only those pages are rewritten that had
1021 * an I/O error in the block to be repaired, since it cannot be
1022 * determined, which copy of the other pages is better (and it
1023 * could happen otherwise that a correct page would be
1024 * overwritten by a bad one).
1025 */
1026 for (mirror_index = 0;
1027 mirror_index < BTRFS_MAX_MIRRORS &&
1028 sblocks_for_recheck[mirror_index].page_count > 0;
1029 mirror_index++) {
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001030 struct scrub_block *sblock_other;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001031
Stefan Behrenscb2ced72012-11-02 16:14:21 +01001032 if (mirror_index == failed_mirror_index)
1033 continue;
1034 sblock_other = sblocks_for_recheck + mirror_index;
1035
1036 /* build and submit the bios, check checksums */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001037 scrub_recheck_block(fs_info, sblock_other, is_metadata,
1038 have_csum, csum, generation,
1039 sctx->csum_size);
1040
1041 if (!sblock_other->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001042 !sblock_other->checksum_error &&
1043 sblock_other->no_io_error_seen) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01001044 if (sctx->is_dev_replace) {
1045 scrub_write_block_to_dev_replace(sblock_other);
1046 } else {
1047 int force_write = is_metadata || have_csum;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001048
Stefan Behrensff023aa2012-11-06 11:43:11 +01001049 ret = scrub_repair_block_from_good_copy(
1050 sblock_bad, sblock_other,
1051 force_write);
1052 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001053 if (0 == ret)
1054 goto corrected_error;
Arne Jansena2de7332011-03-08 14:14:00 +01001055 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001056 }
1057
1058 /*
Stefan Behrensff023aa2012-11-06 11:43:11 +01001059 * for dev_replace, pick good pages and write to the target device.
1060 */
1061 if (sctx->is_dev_replace) {
1062 success = 1;
1063 for (page_num = 0; page_num < sblock_bad->page_count;
1064 page_num++) {
1065 int sub_success;
1066
1067 sub_success = 0;
1068 for (mirror_index = 0;
1069 mirror_index < BTRFS_MAX_MIRRORS &&
1070 sblocks_for_recheck[mirror_index].page_count > 0;
1071 mirror_index++) {
1072 struct scrub_block *sblock_other =
1073 sblocks_for_recheck + mirror_index;
1074 struct scrub_page *page_other =
1075 sblock_other->pagev[page_num];
1076
1077 if (!page_other->io_error) {
1078 ret = scrub_write_page_to_dev_replace(
1079 sblock_other, page_num);
1080 if (ret == 0) {
1081 /* succeeded for this page */
1082 sub_success = 1;
1083 break;
1084 } else {
1085 btrfs_dev_replace_stats_inc(
1086 &sctx->dev_root->
1087 fs_info->dev_replace.
1088 num_write_errors);
1089 }
1090 }
1091 }
1092
1093 if (!sub_success) {
1094 /*
1095 * did not find a mirror to fetch the page
1096 * from. scrub_write_page_to_dev_replace()
1097 * handles this case (page->io_error), by
1098 * filling the block with zeros before
1099 * submitting the write request
1100 */
1101 success = 0;
1102 ret = scrub_write_page_to_dev_replace(
1103 sblock_bad, page_num);
1104 if (ret)
1105 btrfs_dev_replace_stats_inc(
1106 &sctx->dev_root->fs_info->
1107 dev_replace.num_write_errors);
1108 }
1109 }
1110
1111 goto out;
1112 }
1113
1114 /*
1115 * for regular scrub, repair those pages that are errored.
1116 * In case of I/O errors in the area that is supposed to be
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001117 * repaired, continue by picking good copies of those pages.
1118 * Select the good pages from mirrors to rewrite bad pages from
1119 * the area to fix. Afterwards verify the checksum of the block
1120 * that is supposed to be repaired. This verification step is
1121 * only done for the purpose of statistic counting and for the
1122 * final scrub report, whether errors remain.
1123 * A perfect algorithm could make use of the checksum and try
1124 * all possible combinations of pages from the different mirrors
1125 * until the checksum verification succeeds. For example, when
1126 * the 2nd page of mirror #1 faces I/O errors, and the 2nd page
1127 * of mirror #2 is readable but the final checksum test fails,
1128 * then the 2nd page of mirror #3 could be tried, whether now
1129 * the final checksum succeedes. But this would be a rare
1130 * exception and is therefore not implemented. At least it is
1131 * avoided that the good copy is overwritten.
1132 * A more useful improvement would be to pick the sectors
1133 * without I/O error based on sector sizes (512 bytes on legacy
1134 * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one
1135 * mirror could be repaired by taking 512 byte of a different
1136 * mirror, even if other 512 byte sectors in the same PAGE_SIZE
1137 * area are unreadable.
1138 */
1139
1140 /* can only fix I/O errors from here on */
1141 if (sblock_bad->no_io_error_seen)
1142 goto did_not_correct_error;
1143
1144 success = 1;
1145 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001146 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001147
1148 if (!page_bad->io_error)
1149 continue;
1150
1151 for (mirror_index = 0;
1152 mirror_index < BTRFS_MAX_MIRRORS &&
1153 sblocks_for_recheck[mirror_index].page_count > 0;
1154 mirror_index++) {
1155 struct scrub_block *sblock_other = sblocks_for_recheck +
1156 mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001157 struct scrub_page *page_other = sblock_other->pagev[
1158 page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001159
1160 if (!page_other->io_error) {
1161 ret = scrub_repair_page_from_good_copy(
1162 sblock_bad, sblock_other, page_num, 0);
1163 if (0 == ret) {
1164 page_bad->io_error = 0;
1165 break; /* succeeded for this page */
1166 }
Jan Schmidt13db62b2011-06-13 19:56:13 +02001167 }
1168 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001169
1170 if (page_bad->io_error) {
1171 /* did not find a mirror to copy the page from */
1172 success = 0;
1173 }
1174 }
1175
1176 if (success) {
1177 if (is_metadata || have_csum) {
1178 /*
1179 * need to verify the checksum now that all
1180 * sectors on disk are repaired (the write
1181 * request for data to be repaired is on its way).
1182 * Just be lazy and use scrub_recheck_block()
1183 * which re-reads the data before the checksum
1184 * is verified, but most likely the data comes out
1185 * of the page cache.
1186 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001187 scrub_recheck_block(fs_info, sblock_bad,
1188 is_metadata, have_csum, csum,
1189 generation, sctx->csum_size);
1190 if (!sblock_bad->header_error &&
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001191 !sblock_bad->checksum_error &&
1192 sblock_bad->no_io_error_seen)
1193 goto corrected_error;
1194 else
1195 goto did_not_correct_error;
1196 } else {
1197corrected_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001198 spin_lock(&sctx->stat_lock);
1199 sctx->stat.corrected_errors++;
1200 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001201 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001202 "BTRFS: fixed up error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001203 logical, rcu_str_deref(dev->name));
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001204 }
1205 } else {
1206did_not_correct_error:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001207 spin_lock(&sctx->stat_lock);
1208 sctx->stat.uncorrectable_errors++;
1209 spin_unlock(&sctx->stat_lock);
Josef Bacik606686e2012-06-04 14:03:51 -04001210 printk_ratelimited_in_rcu(KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05001211 "BTRFS: unable to fixup (regular) error at logical %llu on dev %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001212 logical, rcu_str_deref(dev->name));
Arne Jansena2de7332011-03-08 14:14:00 +01001213 }
1214
1215out:
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001216 if (sblocks_for_recheck) {
1217 for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS;
1218 mirror_index++) {
1219 struct scrub_block *sblock = sblocks_for_recheck +
1220 mirror_index;
1221 int page_index;
1222
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001223 for (page_index = 0; page_index < sblock->page_count;
1224 page_index++) {
1225 sblock->pagev[page_index]->sblock = NULL;
1226 scrub_page_put(sblock->pagev[page_index]);
1227 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001228 }
1229 kfree(sblocks_for_recheck);
1230 }
1231
1232 return 0;
Arne Jansena2de7332011-03-08 14:14:00 +01001233}
1234
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001235static int scrub_setup_recheck_block(struct scrub_ctx *sctx,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001236 struct btrfs_fs_info *fs_info,
Stefan Behrensff023aa2012-11-06 11:43:11 +01001237 struct scrub_block *original_sblock,
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001238 u64 length, u64 logical,
1239 struct scrub_block *sblocks_for_recheck)
Arne Jansena2de7332011-03-08 14:14:00 +01001240{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001241 int page_index;
1242 int mirror_index;
1243 int ret;
1244
1245 /*
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001246 * note: the two members ref_count and outstanding_pages
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001247 * are not used (and not set) in the blocks that are used for
1248 * the recheck procedure
1249 */
1250
1251 page_index = 0;
1252 while (length > 0) {
1253 u64 sublen = min_t(u64, length, PAGE_SIZE);
1254 u64 mapped_length = sublen;
1255 struct btrfs_bio *bbio = NULL;
1256
1257 /*
1258 * with a length of PAGE_SIZE, each returned stripe
1259 * represents one mirror
1260 */
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01001261 ret = btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, logical,
1262 &mapped_length, &bbio, 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001263 if (ret || !bbio || mapped_length < sublen) {
1264 kfree(bbio);
1265 return -EIO;
1266 }
1267
Stefan Behrensff023aa2012-11-06 11:43:11 +01001268 BUG_ON(page_index >= SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001269 for (mirror_index = 0; mirror_index < (int)bbio->num_stripes;
1270 mirror_index++) {
1271 struct scrub_block *sblock;
1272 struct scrub_page *page;
1273
1274 if (mirror_index >= BTRFS_MAX_MIRRORS)
1275 continue;
1276
1277 sblock = sblocks_for_recheck + mirror_index;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001278 sblock->sctx = sctx;
1279 page = kzalloc(sizeof(*page), GFP_NOFS);
1280 if (!page) {
1281leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001282 spin_lock(&sctx->stat_lock);
1283 sctx->stat.malloc_errors++;
1284 spin_unlock(&sctx->stat_lock);
Wei Yongjuncf93dcc2012-09-02 07:44:51 -06001285 kfree(bbio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001286 return -ENOMEM;
1287 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001288 scrub_page_get(page);
1289 sblock->pagev[page_index] = page;
1290 page->logical = logical;
1291 page->physical = bbio->stripes[mirror_index].physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001292 BUG_ON(page_index >= original_sblock->page_count);
1293 page->physical_for_dev_replace =
1294 original_sblock->pagev[page_index]->
1295 physical_for_dev_replace;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001296 /* for missing devices, dev->bdev is NULL */
1297 page->dev = bbio->stripes[mirror_index].dev;
1298 page->mirror_num = mirror_index + 1;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001299 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001300 page->page = alloc_page(GFP_NOFS);
1301 if (!page->page)
1302 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001303 }
1304 kfree(bbio);
1305 length -= sublen;
1306 logical += sublen;
1307 page_index++;
1308 }
1309
1310 return 0;
1311}
1312
1313/*
1314 * this function will check the on disk data for checksum errors, header
1315 * errors and read I/O errors. If any I/O errors happen, the exact pages
1316 * which are errored are marked as being bad. The goal is to enable scrub
1317 * to take those pages that are not errored from all the mirrors so that
1318 * the pages that are errored in the just handled mirror can be repaired.
1319 */
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001320static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
1321 struct scrub_block *sblock, int is_metadata,
1322 int have_csum, u8 *csum, u64 generation,
1323 u16 csum_size)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001324{
1325 int page_num;
1326
1327 sblock->no_io_error_seen = 1;
1328 sblock->header_error = 0;
1329 sblock->checksum_error = 0;
1330
1331 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1332 struct bio *bio;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001333 struct scrub_page *page = sblock->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001334
Stefan Behrens442a4f62012-05-25 16:06:08 +02001335 if (page->dev->bdev == NULL) {
Stefan Behrensea9947b2012-05-04 15:16:07 -04001336 page->io_error = 1;
1337 sblock->no_io_error_seen = 0;
1338 continue;
1339 }
1340
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001341 WARN_ON(!page->page);
Chris Mason9be33952013-05-17 18:30:14 -04001342 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001343 if (!bio) {
1344 page->io_error = 1;
1345 sblock->no_io_error_seen = 0;
1346 continue;
1347 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02001348 bio->bi_bdev = page->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001349 bio->bi_sector = page->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001350
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001351 bio_add_page(bio, page->page, PAGE_SIZE, 0);
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001352 if (btrfsic_submit_bio_wait(READ, bio))
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001353 sblock->no_io_error_seen = 0;
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001354
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001355 bio_put(bio);
1356 }
1357
1358 if (sblock->no_io_error_seen)
1359 scrub_recheck_block_checksum(fs_info, sblock, is_metadata,
1360 have_csum, csum, generation,
1361 csum_size);
1362
Stefan Behrens34f5c8e2012-11-02 16:16:26 +01001363 return;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001364}
1365
1366static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
1367 struct scrub_block *sblock,
1368 int is_metadata, int have_csum,
1369 const u8 *csum, u64 generation,
1370 u16 csum_size)
1371{
1372 int page_num;
1373 u8 calculated_csum[BTRFS_CSUM_SIZE];
1374 u32 crc = ~(u32)0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001375 void *mapped_buffer;
1376
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001377 WARN_ON(!sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001378 if (is_metadata) {
1379 struct btrfs_header *h;
1380
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001381 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001382 h = (struct btrfs_header *)mapped_buffer;
1383
Qu Wenruo3cae2102013-07-16 11:19:18 +08001384 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h) ||
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001385 memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE) ||
1386 memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001387 BTRFS_UUID_SIZE)) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001388 sblock->header_error = 1;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001389 } else if (generation != btrfs_stack_header_generation(h)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001390 sblock->header_error = 1;
1391 sblock->generation_error = 1;
1392 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001393 csum = h->csum;
1394 } else {
1395 if (!have_csum)
1396 return;
1397
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001398 mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001399 }
1400
1401 for (page_num = 0;;) {
1402 if (page_num == 0 && is_metadata)
Liu Bob0496682013-03-14 14:57:45 +00001403 crc = btrfs_csum_data(
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001404 ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
1405 crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
1406 else
Liu Bob0496682013-03-14 14:57:45 +00001407 crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001408
Linus Torvalds9613beb2012-03-30 12:44:29 -07001409 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001410 page_num++;
1411 if (page_num >= sblock->page_count)
1412 break;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001413 WARN_ON(!sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001414
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001415 mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001416 }
1417
1418 btrfs_csum_final(crc, calculated_csum);
1419 if (memcmp(calculated_csum, csum, csum_size))
1420 sblock->checksum_error = 1;
1421}
1422
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001423static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad,
1424 struct scrub_block *sblock_good,
1425 int force_write)
1426{
1427 int page_num;
1428 int ret = 0;
1429
1430 for (page_num = 0; page_num < sblock_bad->page_count; page_num++) {
1431 int ret_sub;
1432
1433 ret_sub = scrub_repair_page_from_good_copy(sblock_bad,
1434 sblock_good,
1435 page_num,
1436 force_write);
1437 if (ret_sub)
1438 ret = ret_sub;
1439 }
1440
1441 return ret;
1442}
1443
1444static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1445 struct scrub_block *sblock_good,
1446 int page_num, int force_write)
1447{
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001448 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1449 struct scrub_page *page_good = sblock_good->pagev[page_num];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001450
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001451 BUG_ON(page_bad->page == NULL);
1452 BUG_ON(page_good->page == NULL);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001453 if (force_write || sblock_bad->header_error ||
1454 sblock_bad->checksum_error || page_bad->io_error) {
1455 struct bio *bio;
1456 int ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001457
Stefan Behrensff023aa2012-11-06 11:43:11 +01001458 if (!page_bad->dev->bdev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001459 printk_ratelimited(KERN_WARNING "BTRFS: "
1460 "scrub_repair_page_from_good_copy(bdev == NULL) "
1461 "is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001462 return -EIO;
1463 }
1464
Chris Mason9be33952013-05-17 18:30:14 -04001465 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04001466 if (!bio)
1467 return -EIO;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001468 bio->bi_bdev = page_bad->dev->bdev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001469 bio->bi_sector = page_bad->physical >> 9;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001470
1471 ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0);
1472 if (PAGE_SIZE != ret) {
1473 bio_put(bio);
1474 return -EIO;
1475 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001476
Kent Overstreetc170bbb2013-11-24 16:32:22 -07001477 if (btrfsic_submit_bio_wait(WRITE, bio)) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02001478 btrfs_dev_stat_inc_and_print(page_bad->dev,
1479 BTRFS_DEV_STAT_WRITE_ERRS);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001480 btrfs_dev_replace_stats_inc(
1481 &sblock_bad->sctx->dev_root->fs_info->
1482 dev_replace.num_write_errors);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001483 bio_put(bio);
1484 return -EIO;
1485 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001486 bio_put(bio);
1487 }
1488
1489 return 0;
1490}
1491
Stefan Behrensff023aa2012-11-06 11:43:11 +01001492static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1493{
1494 int page_num;
1495
1496 for (page_num = 0; page_num < sblock->page_count; page_num++) {
1497 int ret;
1498
1499 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1500 if (ret)
1501 btrfs_dev_replace_stats_inc(
1502 &sblock->sctx->dev_root->fs_info->dev_replace.
1503 num_write_errors);
1504 }
1505}
1506
1507static int scrub_write_page_to_dev_replace(struct scrub_block *sblock,
1508 int page_num)
1509{
1510 struct scrub_page *spage = sblock->pagev[page_num];
1511
1512 BUG_ON(spage->page == NULL);
1513 if (spage->io_error) {
1514 void *mapped_buffer = kmap_atomic(spage->page);
1515
1516 memset(mapped_buffer, 0, PAGE_CACHE_SIZE);
1517 flush_dcache_page(spage->page);
1518 kunmap_atomic(mapped_buffer);
1519 }
1520 return scrub_add_page_to_wr_bio(sblock->sctx, spage);
1521}
1522
1523static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
1524 struct scrub_page *spage)
1525{
1526 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1527 struct scrub_bio *sbio;
1528 int ret;
1529
1530 mutex_lock(&wr_ctx->wr_lock);
1531again:
1532 if (!wr_ctx->wr_curr_bio) {
1533 wr_ctx->wr_curr_bio = kzalloc(sizeof(*wr_ctx->wr_curr_bio),
1534 GFP_NOFS);
1535 if (!wr_ctx->wr_curr_bio) {
1536 mutex_unlock(&wr_ctx->wr_lock);
1537 return -ENOMEM;
1538 }
1539 wr_ctx->wr_curr_bio->sctx = sctx;
1540 wr_ctx->wr_curr_bio->page_count = 0;
1541 }
1542 sbio = wr_ctx->wr_curr_bio;
1543 if (sbio->page_count == 0) {
1544 struct bio *bio;
1545
1546 sbio->physical = spage->physical_for_dev_replace;
1547 sbio->logical = spage->logical;
1548 sbio->dev = wr_ctx->tgtdev;
1549 bio = sbio->bio;
1550 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001551 bio = btrfs_io_bio_alloc(GFP_NOFS, wr_ctx->pages_per_wr_bio);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001552 if (!bio) {
1553 mutex_unlock(&wr_ctx->wr_lock);
1554 return -ENOMEM;
1555 }
1556 sbio->bio = bio;
1557 }
1558
1559 bio->bi_private = sbio;
1560 bio->bi_end_io = scrub_wr_bio_end_io;
1561 bio->bi_bdev = sbio->dev->bdev;
1562 bio->bi_sector = sbio->physical >> 9;
1563 sbio->err = 0;
1564 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1565 spage->physical_for_dev_replace ||
1566 sbio->logical + sbio->page_count * PAGE_SIZE !=
1567 spage->logical) {
1568 scrub_wr_submit(sctx);
1569 goto again;
1570 }
1571
1572 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1573 if (ret != PAGE_SIZE) {
1574 if (sbio->page_count < 1) {
1575 bio_put(sbio->bio);
1576 sbio->bio = NULL;
1577 mutex_unlock(&wr_ctx->wr_lock);
1578 return -EIO;
1579 }
1580 scrub_wr_submit(sctx);
1581 goto again;
1582 }
1583
1584 sbio->pagev[sbio->page_count] = spage;
1585 scrub_page_get(spage);
1586 sbio->page_count++;
1587 if (sbio->page_count == wr_ctx->pages_per_wr_bio)
1588 scrub_wr_submit(sctx);
1589 mutex_unlock(&wr_ctx->wr_lock);
1590
1591 return 0;
1592}
1593
1594static void scrub_wr_submit(struct scrub_ctx *sctx)
1595{
1596 struct scrub_wr_ctx *wr_ctx = &sctx->wr_ctx;
1597 struct scrub_bio *sbio;
1598
1599 if (!wr_ctx->wr_curr_bio)
1600 return;
1601
1602 sbio = wr_ctx->wr_curr_bio;
1603 wr_ctx->wr_curr_bio = NULL;
1604 WARN_ON(!sbio->bio->bi_bdev);
1605 scrub_pending_bio_inc(sctx);
1606 /* process all writes in a single worker thread. Then the block layer
1607 * orders the requests before sending them to the driver which
1608 * doubled the write performance on spinning disks when measured
1609 * with Linux 3.5 */
1610 btrfsic_submit_bio(WRITE, sbio->bio);
1611}
1612
1613static void scrub_wr_bio_end_io(struct bio *bio, int err)
1614{
1615 struct scrub_bio *sbio = bio->bi_private;
1616 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
1617
1618 sbio->err = err;
1619 sbio->bio = bio;
1620
Qu Wenruo0339ef22014-02-28 10:46:17 +08001621 btrfs_init_work(&sbio->work, scrub_wr_bio_end_io_worker, NULL, NULL);
1622 btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001623}
1624
Qu Wenruo0339ef22014-02-28 10:46:17 +08001625static void scrub_wr_bio_end_io_worker(struct btrfs_work_struct *work)
Stefan Behrensff023aa2012-11-06 11:43:11 +01001626{
1627 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
1628 struct scrub_ctx *sctx = sbio->sctx;
1629 int i;
1630
1631 WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO);
1632 if (sbio->err) {
1633 struct btrfs_dev_replace *dev_replace =
1634 &sbio->sctx->dev_root->fs_info->dev_replace;
1635
1636 for (i = 0; i < sbio->page_count; i++) {
1637 struct scrub_page *spage = sbio->pagev[i];
1638
1639 spage->io_error = 1;
1640 btrfs_dev_replace_stats_inc(&dev_replace->
1641 num_write_errors);
1642 }
1643 }
1644
1645 for (i = 0; i < sbio->page_count; i++)
1646 scrub_page_put(sbio->pagev[i]);
1647
1648 bio_put(sbio->bio);
1649 kfree(sbio);
1650 scrub_pending_bio_dec(sctx);
1651}
1652
1653static int scrub_checksum(struct scrub_block *sblock)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001654{
1655 u64 flags;
1656 int ret;
1657
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001658 WARN_ON(sblock->page_count < 1);
1659 flags = sblock->pagev[0]->flags;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001660 ret = 0;
1661 if (flags & BTRFS_EXTENT_FLAG_DATA)
1662 ret = scrub_checksum_data(sblock);
1663 else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1664 ret = scrub_checksum_tree_block(sblock);
1665 else if (flags & BTRFS_EXTENT_FLAG_SUPER)
1666 (void)scrub_checksum_super(sblock);
1667 else
1668 WARN_ON(1);
1669 if (ret)
1670 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01001671
1672 return ret;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001673}
1674
1675static int scrub_checksum_data(struct scrub_block *sblock)
1676{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001677 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001678 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001679 u8 *on_disk_csum;
1680 struct page *page;
1681 void *buffer;
Arne Jansena2de7332011-03-08 14:14:00 +01001682 u32 crc = ~(u32)0;
1683 int fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001684 u64 len;
1685 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001686
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001687 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001688 if (!sblock->pagev[0]->have_csum)
Arne Jansena2de7332011-03-08 14:14:00 +01001689 return 0;
1690
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001691 on_disk_csum = sblock->pagev[0]->csum;
1692 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001693 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001694
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001695 len = sctx->sectorsize;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001696 index = 0;
1697 for (;;) {
1698 u64 l = min_t(u64, len, PAGE_SIZE);
1699
Liu Bob0496682013-03-14 14:57:45 +00001700 crc = btrfs_csum_data(buffer, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001701 kunmap_atomic(buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001702 len -= l;
1703 if (len == 0)
1704 break;
1705 index++;
1706 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001707 BUG_ON(!sblock->pagev[index]->page);
1708 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001709 buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001710 }
1711
Arne Jansena2de7332011-03-08 14:14:00 +01001712 btrfs_csum_final(crc, csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001713 if (memcmp(csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001714 fail = 1;
1715
Arne Jansena2de7332011-03-08 14:14:00 +01001716 return fail;
1717}
1718
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001719static int scrub_checksum_tree_block(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001720{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001721 struct scrub_ctx *sctx = sblock->sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01001722 struct btrfs_header *h;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001723 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001724 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001725 u8 calculated_csum[BTRFS_CSUM_SIZE];
1726 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1727 struct page *page;
1728 void *mapped_buffer;
1729 u64 mapped_size;
1730 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001731 u32 crc = ~(u32)0;
1732 int fail = 0;
1733 int crc_fail = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001734 u64 len;
1735 int index;
1736
1737 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001738 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001739 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001740 h = (struct btrfs_header *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001741 memcpy(on_disk_csum, h->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001742
1743 /*
1744 * we don't use the getter functions here, as we
1745 * a) don't have an extent buffer and
1746 * b) the page is already kmapped
1747 */
Arne Jansena2de7332011-03-08 14:14:00 +01001748
Qu Wenruo3cae2102013-07-16 11:19:18 +08001749 if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001750 ++fail;
1751
Qu Wenruo3cae2102013-07-16 11:19:18 +08001752 if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h))
Arne Jansena2de7332011-03-08 14:14:00 +01001753 ++fail;
1754
1755 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1756 ++fail;
1757
1758 if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid,
1759 BTRFS_UUID_SIZE))
1760 ++fail;
1761
Stefan Behrensff023aa2012-11-06 11:43:11 +01001762 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001763 len = sctx->nodesize - BTRFS_CSUM_SIZE;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001764 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1765 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1766 index = 0;
1767 for (;;) {
1768 u64 l = min_t(u64, len, mapped_size);
1769
Liu Bob0496682013-03-14 14:57:45 +00001770 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001771 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001772 len -= l;
1773 if (len == 0)
1774 break;
1775 index++;
1776 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001777 BUG_ON(!sblock->pagev[index]->page);
1778 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001779 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001780 mapped_size = PAGE_SIZE;
1781 p = mapped_buffer;
1782 }
1783
1784 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001785 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Arne Jansena2de7332011-03-08 14:14:00 +01001786 ++crc_fail;
1787
Arne Jansena2de7332011-03-08 14:14:00 +01001788 return fail || crc_fail;
1789}
1790
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001791static int scrub_checksum_super(struct scrub_block *sblock)
Arne Jansena2de7332011-03-08 14:14:00 +01001792{
1793 struct btrfs_super_block *s;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001794 struct scrub_ctx *sctx = sblock->sctx;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001795 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01001796 struct btrfs_fs_info *fs_info = root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001797 u8 calculated_csum[BTRFS_CSUM_SIZE];
1798 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1799 struct page *page;
1800 void *mapped_buffer;
1801 u64 mapped_size;
1802 void *p;
Arne Jansena2de7332011-03-08 14:14:00 +01001803 u32 crc = ~(u32)0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02001804 int fail_gen = 0;
1805 int fail_cor = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001806 u64 len;
1807 int index;
Arne Jansena2de7332011-03-08 14:14:00 +01001808
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001809 BUG_ON(sblock->page_count < 1);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001810 page = sblock->pagev[0]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001811 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001812 s = (struct btrfs_super_block *)mapped_buffer;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001813 memcpy(on_disk_csum, s->csum, sctx->csum_size);
Arne Jansena2de7332011-03-08 14:14:00 +01001814
Qu Wenruo3cae2102013-07-16 11:19:18 +08001815 if (sblock->pagev[0]->logical != btrfs_super_bytenr(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001816 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001817
Qu Wenruo3cae2102013-07-16 11:19:18 +08001818 if (sblock->pagev[0]->generation != btrfs_super_generation(s))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001819 ++fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001820
1821 if (memcmp(s->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001822 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001823
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001824 len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE;
1825 mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
1826 p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
1827 index = 0;
1828 for (;;) {
1829 u64 l = min_t(u64, len, mapped_size);
1830
Liu Bob0496682013-03-14 14:57:45 +00001831 crc = btrfs_csum_data(p, crc, l);
Linus Torvalds9613beb2012-03-30 12:44:29 -07001832 kunmap_atomic(mapped_buffer);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001833 len -= l;
1834 if (len == 0)
1835 break;
1836 index++;
1837 BUG_ON(index >= sblock->page_count);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001838 BUG_ON(!sblock->pagev[index]->page);
1839 page = sblock->pagev[index]->page;
Linus Torvalds9613beb2012-03-30 12:44:29 -07001840 mapped_buffer = kmap_atomic(page);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001841 mapped_size = PAGE_SIZE;
1842 p = mapped_buffer;
1843 }
1844
1845 btrfs_csum_final(crc, calculated_csum);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001846 if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
Stefan Behrens442a4f62012-05-25 16:06:08 +02001847 ++fail_cor;
Arne Jansena2de7332011-03-08 14:14:00 +01001848
Stefan Behrens442a4f62012-05-25 16:06:08 +02001849 if (fail_cor + fail_gen) {
Arne Jansena2de7332011-03-08 14:14:00 +01001850 /*
1851 * if we find an error in a super block, we just report it.
1852 * They will get written with the next transaction commit
1853 * anyway
1854 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001855 spin_lock(&sctx->stat_lock);
1856 ++sctx->stat.super_errors;
1857 spin_unlock(&sctx->stat_lock);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001858 if (fail_cor)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001859 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001860 BTRFS_DEV_STAT_CORRUPTION_ERRS);
1861 else
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001862 btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev,
Stefan Behrens442a4f62012-05-25 16:06:08 +02001863 BTRFS_DEV_STAT_GENERATION_ERRS);
Arne Jansena2de7332011-03-08 14:14:00 +01001864 }
1865
Stefan Behrens442a4f62012-05-25 16:06:08 +02001866 return fail_cor + fail_gen;
Arne Jansena2de7332011-03-08 14:14:00 +01001867}
1868
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001869static void scrub_block_get(struct scrub_block *sblock)
1870{
1871 atomic_inc(&sblock->ref_count);
1872}
1873
1874static void scrub_block_put(struct scrub_block *sblock)
1875{
1876 if (atomic_dec_and_test(&sblock->ref_count)) {
1877 int i;
1878
1879 for (i = 0; i < sblock->page_count; i++)
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001880 scrub_page_put(sblock->pagev[i]);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001881 kfree(sblock);
1882 }
1883}
1884
Stefan Behrens7a9e9982012-11-02 14:58:04 +01001885static void scrub_page_get(struct scrub_page *spage)
1886{
1887 atomic_inc(&spage->ref_count);
1888}
1889
1890static void scrub_page_put(struct scrub_page *spage)
1891{
1892 if (atomic_dec_and_test(&spage->ref_count)) {
1893 if (spage->page)
1894 __free_page(spage->page);
1895 kfree(spage);
1896 }
1897}
1898
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001899static void scrub_submit(struct scrub_ctx *sctx)
Arne Jansena2de7332011-03-08 14:14:00 +01001900{
1901 struct scrub_bio *sbio;
1902
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001903 if (sctx->curr == -1)
Stefan Behrens1623ede2012-03-27 14:21:26 -04001904 return;
Arne Jansena2de7332011-03-08 14:14:00 +01001905
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001906 sbio = sctx->bios[sctx->curr];
1907 sctx->curr = -1;
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01001908 scrub_pending_bio_inc(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001909
Stefan Behrensff023aa2012-11-06 11:43:11 +01001910 if (!sbio->bio->bi_bdev) {
1911 /*
1912 * this case should not happen. If btrfs_map_block() is
1913 * wrong, it could happen for dev-replace operations on
1914 * missing devices when no mirrors are available, but in
1915 * this case it should already fail the mount.
1916 * This case is handled correctly (but _very_ slowly).
1917 */
1918 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05001919 "BTRFS: scrub_submit(bio bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01001920 bio_endio(sbio->bio, -EIO);
1921 } else {
1922 btrfsic_submit_bio(READ, sbio->bio);
1923 }
Arne Jansena2de7332011-03-08 14:14:00 +01001924}
1925
Stefan Behrensff023aa2012-11-06 11:43:11 +01001926static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
1927 struct scrub_page *spage)
Arne Jansena2de7332011-03-08 14:14:00 +01001928{
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001929 struct scrub_block *sblock = spage->sblock;
Arne Jansena2de7332011-03-08 14:14:00 +01001930 struct scrub_bio *sbio;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001931 int ret;
Arne Jansena2de7332011-03-08 14:14:00 +01001932
1933again:
1934 /*
1935 * grab a fresh bio or wait for one to become available
1936 */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001937 while (sctx->curr == -1) {
1938 spin_lock(&sctx->list_lock);
1939 sctx->curr = sctx->first_free;
1940 if (sctx->curr != -1) {
1941 sctx->first_free = sctx->bios[sctx->curr]->next_free;
1942 sctx->bios[sctx->curr]->next_free = -1;
1943 sctx->bios[sctx->curr]->page_count = 0;
1944 spin_unlock(&sctx->list_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01001945 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001946 spin_unlock(&sctx->list_lock);
1947 wait_event(sctx->list_wait, sctx->first_free != -1);
Arne Jansena2de7332011-03-08 14:14:00 +01001948 }
1949 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001950 sbio = sctx->bios[sctx->curr];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001951 if (sbio->page_count == 0) {
Arne Jansen69f4cb52011-11-11 08:17:10 -05001952 struct bio *bio;
1953
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001954 sbio->physical = spage->physical;
1955 sbio->logical = spage->logical;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001956 sbio->dev = spage->dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001957 bio = sbio->bio;
1958 if (!bio) {
Chris Mason9be33952013-05-17 18:30:14 -04001959 bio = btrfs_io_bio_alloc(GFP_NOFS, sctx->pages_per_rd_bio);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001960 if (!bio)
1961 return -ENOMEM;
1962 sbio->bio = bio;
1963 }
Arne Jansen69f4cb52011-11-11 08:17:10 -05001964
1965 bio->bi_private = sbio;
1966 bio->bi_end_io = scrub_bio_end_io;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001967 bio->bi_bdev = sbio->dev->bdev;
1968 bio->bi_sector = sbio->physical >> 9;
Arne Jansen69f4cb52011-11-11 08:17:10 -05001969 sbio->err = 0;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001970 } else if (sbio->physical + sbio->page_count * PAGE_SIZE !=
1971 spage->physical ||
1972 sbio->logical + sbio->page_count * PAGE_SIZE !=
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01001973 spage->logical ||
1974 sbio->dev != spage->dev) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001975 scrub_submit(sctx);
Arne Jansen69f4cb52011-11-11 08:17:10 -05001976 goto again;
1977 }
1978
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001979 sbio->pagev[sbio->page_count] = spage;
1980 ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0);
1981 if (ret != PAGE_SIZE) {
1982 if (sbio->page_count < 1) {
1983 bio_put(sbio->bio);
1984 sbio->bio = NULL;
1985 return -EIO;
1986 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001987 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001988 goto again;
Arne Jansena2de7332011-03-08 14:14:00 +01001989 }
Arne Jansen1bc87792011-05-28 21:57:55 +02001990
Stefan Behrensff023aa2012-11-06 11:43:11 +01001991 scrub_block_get(sblock); /* one for the page added to the bio */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04001992 atomic_inc(&sblock->outstanding_pages);
1993 sbio->page_count++;
Stefan Behrensff023aa2012-11-06 11:43:11 +01001994 if (sbio->page_count == sctx->pages_per_rd_bio)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01001995 scrub_submit(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01001996
1997 return 0;
1998}
1999
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002000static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002001 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002002 u64 gen, int mirror_num, u8 *csum, int force,
2003 u64 physical_for_dev_replace)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002004{
2005 struct scrub_block *sblock;
2006 int index;
2007
2008 sblock = kzalloc(sizeof(*sblock), GFP_NOFS);
2009 if (!sblock) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002010 spin_lock(&sctx->stat_lock);
2011 sctx->stat.malloc_errors++;
2012 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002013 return -ENOMEM;
2014 }
2015
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002016 /* one ref inside this function, plus one for each page added to
2017 * a bio later on */
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002018 atomic_set(&sblock->ref_count, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002019 sblock->sctx = sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002020 sblock->no_io_error_seen = 1;
2021
2022 for (index = 0; len > 0; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002023 struct scrub_page *spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002024 u64 l = min_t(u64, len, PAGE_SIZE);
2025
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002026 spage = kzalloc(sizeof(*spage), GFP_NOFS);
2027 if (!spage) {
2028leave_nomem:
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002029 spin_lock(&sctx->stat_lock);
2030 sctx->stat.malloc_errors++;
2031 spin_unlock(&sctx->stat_lock);
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002032 scrub_block_put(sblock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002033 return -ENOMEM;
2034 }
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002035 BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK);
2036 scrub_page_get(spage);
2037 sblock->pagev[index] = spage;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002038 spage->sblock = sblock;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002039 spage->dev = dev;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002040 spage->flags = flags;
2041 spage->generation = gen;
2042 spage->logical = logical;
2043 spage->physical = physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002044 spage->physical_for_dev_replace = physical_for_dev_replace;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002045 spage->mirror_num = mirror_num;
2046 if (csum) {
2047 spage->have_csum = 1;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002048 memcpy(spage->csum, csum, sctx->csum_size);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002049 } else {
2050 spage->have_csum = 0;
2051 }
2052 sblock->page_count++;
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002053 spage->page = alloc_page(GFP_NOFS);
2054 if (!spage->page)
2055 goto leave_nomem;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002056 len -= l;
2057 logical += l;
2058 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002059 physical_for_dev_replace += l;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002060 }
2061
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002062 WARN_ON(sblock->page_count == 0);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002063 for (index = 0; index < sblock->page_count; index++) {
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002064 struct scrub_page *spage = sblock->pagev[index];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002065 int ret;
2066
Stefan Behrensff023aa2012-11-06 11:43:11 +01002067 ret = scrub_add_page_to_rd_bio(sctx, spage);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002068 if (ret) {
2069 scrub_block_put(sblock);
2070 return ret;
2071 }
2072 }
2073
2074 if (force)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002075 scrub_submit(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002076
2077 /* last one frees, either here or in bio completion for last page */
2078 scrub_block_put(sblock);
2079 return 0;
2080}
2081
2082static void scrub_bio_end_io(struct bio *bio, int err)
2083{
2084 struct scrub_bio *sbio = bio->bi_private;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002085 struct btrfs_fs_info *fs_info = sbio->dev->dev_root->fs_info;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002086
2087 sbio->err = err;
2088 sbio->bio = bio;
2089
Qu Wenruo0339ef22014-02-28 10:46:17 +08002090 btrfs_queue_work(fs_info->scrub_workers, &sbio->work);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002091}
2092
Qu Wenruo0339ef22014-02-28 10:46:17 +08002093static void scrub_bio_end_io_worker(struct btrfs_work_struct *work)
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002094{
2095 struct scrub_bio *sbio = container_of(work, struct scrub_bio, work);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002096 struct scrub_ctx *sctx = sbio->sctx;
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002097 int i;
2098
Stefan Behrensff023aa2012-11-06 11:43:11 +01002099 BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002100 if (sbio->err) {
2101 for (i = 0; i < sbio->page_count; i++) {
2102 struct scrub_page *spage = sbio->pagev[i];
2103
2104 spage->io_error = 1;
2105 spage->sblock->no_io_error_seen = 0;
2106 }
2107 }
2108
2109 /* now complete the scrub_block items that have all pages completed */
2110 for (i = 0; i < sbio->page_count; i++) {
2111 struct scrub_page *spage = sbio->pagev[i];
2112 struct scrub_block *sblock = spage->sblock;
2113
2114 if (atomic_dec_and_test(&sblock->outstanding_pages))
2115 scrub_block_complete(sblock);
2116 scrub_block_put(sblock);
2117 }
2118
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002119 bio_put(sbio->bio);
2120 sbio->bio = NULL;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002121 spin_lock(&sctx->list_lock);
2122 sbio->next_free = sctx->first_free;
2123 sctx->first_free = sbio->index;
2124 spin_unlock(&sctx->list_lock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002125
2126 if (sctx->is_dev_replace &&
2127 atomic_read(&sctx->wr_ctx.flush_all_writes)) {
2128 mutex_lock(&sctx->wr_ctx.wr_lock);
2129 scrub_wr_submit(sctx);
2130 mutex_unlock(&sctx->wr_ctx.wr_lock);
2131 }
2132
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002133 scrub_pending_bio_dec(sctx);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002134}
2135
2136static void scrub_block_complete(struct scrub_block *sblock)
2137{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002138 if (!sblock->no_io_error_seen) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002139 scrub_handle_errored_block(sblock);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002140 } else {
2141 /*
2142 * if has checksum error, write via repair mechanism in
2143 * dev replace case, otherwise write here in dev replace
2144 * case.
2145 */
2146 if (!scrub_checksum(sblock) && sblock->sctx->is_dev_replace)
2147 scrub_write_block_to_dev_replace(sblock);
2148 }
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002149}
2150
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002151static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u64 len,
Arne Jansena2de7332011-03-08 14:14:00 +01002152 u8 *csum)
2153{
2154 struct btrfs_ordered_sum *sum = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +08002155 unsigned long index;
Arne Jansena2de7332011-03-08 14:14:00 +01002156 unsigned long num_sectors;
Arne Jansena2de7332011-03-08 14:14:00 +01002157
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002158 while (!list_empty(&sctx->csum_list)) {
2159 sum = list_first_entry(&sctx->csum_list,
Arne Jansena2de7332011-03-08 14:14:00 +01002160 struct btrfs_ordered_sum, list);
2161 if (sum->bytenr > logical)
2162 return 0;
2163 if (sum->bytenr + sum->len > logical)
2164 break;
2165
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002166 ++sctx->stat.csum_discards;
Arne Jansena2de7332011-03-08 14:14:00 +01002167 list_del(&sum->list);
2168 kfree(sum);
2169 sum = NULL;
2170 }
2171 if (!sum)
2172 return 0;
2173
Miao Xief51a4a12013-06-19 10:36:09 +08002174 index = ((u32)(logical - sum->bytenr)) / sctx->sectorsize;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002175 num_sectors = sum->len / sctx->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +08002176 memcpy(csum, sum->sums + index, sctx->csum_size);
2177 if (index == num_sectors - 1) {
Arne Jansena2de7332011-03-08 14:14:00 +01002178 list_del(&sum->list);
2179 kfree(sum);
2180 }
Miao Xief51a4a12013-06-19 10:36:09 +08002181 return 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002182}
2183
2184/* scrub extent tries to collect up to 64 kB for each bio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002185static int scrub_extent(struct scrub_ctx *sctx, u64 logical, u64 len,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002186 u64 physical, struct btrfs_device *dev, u64 flags,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002187 u64 gen, int mirror_num, u64 physical_for_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002188{
2189 int ret;
2190 u8 csum[BTRFS_CSUM_SIZE];
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002191 u32 blocksize;
2192
2193 if (flags & BTRFS_EXTENT_FLAG_DATA) {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002194 blocksize = sctx->sectorsize;
2195 spin_lock(&sctx->stat_lock);
2196 sctx->stat.data_extents_scrubbed++;
2197 sctx->stat.data_bytes_scrubbed += len;
2198 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002199 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002200 WARN_ON(sctx->nodesize != sctx->leafsize);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002201 blocksize = sctx->nodesize;
2202 spin_lock(&sctx->stat_lock);
2203 sctx->stat.tree_extents_scrubbed++;
2204 sctx->stat.tree_bytes_scrubbed += len;
2205 spin_unlock(&sctx->stat_lock);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002206 } else {
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002207 blocksize = sctx->sectorsize;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002208 WARN_ON(1);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002209 }
Arne Jansena2de7332011-03-08 14:14:00 +01002210
2211 while (len) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002212 u64 l = min_t(u64, len, blocksize);
Arne Jansena2de7332011-03-08 14:14:00 +01002213 int have_csum = 0;
2214
2215 if (flags & BTRFS_EXTENT_FLAG_DATA) {
2216 /* push csums to sbio */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002217 have_csum = scrub_find_csum(sctx, logical, l, csum);
Arne Jansena2de7332011-03-08 14:14:00 +01002218 if (have_csum == 0)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002219 ++sctx->stat.no_csum;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002220 if (sctx->is_dev_replace && !have_csum) {
2221 ret = copy_nocow_pages(sctx, logical, l,
2222 mirror_num,
2223 physical_for_dev_replace);
2224 goto behind_scrub_pages;
2225 }
Arne Jansena2de7332011-03-08 14:14:00 +01002226 }
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002227 ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002228 mirror_num, have_csum ? csum : NULL, 0,
2229 physical_for_dev_replace);
2230behind_scrub_pages:
Arne Jansena2de7332011-03-08 14:14:00 +01002231 if (ret)
2232 return ret;
2233 len -= l;
2234 logical += l;
2235 physical += l;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002236 physical_for_dev_replace += l;
Arne Jansena2de7332011-03-08 14:14:00 +01002237 }
2238 return 0;
2239}
2240
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002241static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002242 struct map_lookup *map,
2243 struct btrfs_device *scrub_dev,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002244 int num, u64 base, u64 length,
2245 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002246{
2247 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002248 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
Arne Jansena2de7332011-03-08 14:14:00 +01002249 struct btrfs_root *root = fs_info->extent_root;
2250 struct btrfs_root *csum_root = fs_info->csum_root;
2251 struct btrfs_extent_item *extent;
Arne Jansene7786c32011-05-28 20:58:38 +00002252 struct blk_plug plug;
Arne Jansena2de7332011-03-08 14:14:00 +01002253 u64 flags;
2254 int ret;
2255 int slot;
Arne Jansena2de7332011-03-08 14:14:00 +01002256 u64 nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002257 struct extent_buffer *l;
2258 struct btrfs_key key;
2259 u64 physical;
2260 u64 logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002261 u64 logic_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002262 u64 generation;
Jan Schmidte12fa9c2011-06-17 15:55:21 +02002263 int mirror_num;
Arne Jansen7a262852011-06-10 12:39:23 +02002264 struct reada_control *reada1;
2265 struct reada_control *reada2;
2266 struct btrfs_key key_start;
2267 struct btrfs_key key_end;
Arne Jansena2de7332011-03-08 14:14:00 +01002268 u64 increment = map->stripe_len;
2269 u64 offset;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002270 u64 extent_logical;
2271 u64 extent_physical;
2272 u64 extent_len;
2273 struct btrfs_device *extent_dev;
2274 int extent_mirror_num;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002275 int stop_loop;
Arne Jansena2de7332011-03-08 14:14:00 +01002276
David Woodhouse53b381b2013-01-29 18:40:14 -05002277 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
2278 BTRFS_BLOCK_GROUP_RAID6)) {
2279 if (num >= nr_data_stripes(map)) {
2280 return 0;
2281 }
2282 }
2283
Arne Jansena2de7332011-03-08 14:14:00 +01002284 nstripes = length;
2285 offset = 0;
2286 do_div(nstripes, map->stripe_len);
2287 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2288 offset = map->stripe_len * num;
2289 increment = map->stripe_len * map->num_stripes;
Jan Schmidt193ea742011-06-13 19:56:54 +02002290 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002291 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2292 int factor = map->num_stripes / map->sub_stripes;
2293 offset = map->stripe_len * (num / map->sub_stripes);
2294 increment = map->stripe_len * factor;
Jan Schmidt193ea742011-06-13 19:56:54 +02002295 mirror_num = num % map->sub_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002296 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2297 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002298 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002299 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2300 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002301 mirror_num = num % map->num_stripes + 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002302 } else {
2303 increment = map->stripe_len;
Jan Schmidt193ea742011-06-13 19:56:54 +02002304 mirror_num = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002305 }
2306
2307 path = btrfs_alloc_path();
2308 if (!path)
2309 return -ENOMEM;
2310
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002311 /*
2312 * work on commit root. The related disk blocks are static as
2313 * long as COW is applied. This means, it is save to rewrite
2314 * them to repair disk errors without any race conditions
2315 */
Arne Jansena2de7332011-03-08 14:14:00 +01002316 path->search_commit_root = 1;
2317 path->skip_locking = 1;
2318
2319 /*
Arne Jansen7a262852011-06-10 12:39:23 +02002320 * trigger the readahead for extent tree csum tree and wait for
2321 * completion. During readahead, the scrub is officially paused
2322 * to not hold off transaction commits
Arne Jansena2de7332011-03-08 14:14:00 +01002323 */
2324 logical = base + offset;
Arne Jansena2de7332011-03-08 14:14:00 +01002325
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002326 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002327 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilongcb7ab022013-12-04 21:16:53 +08002328 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002329
Arne Jansen7a262852011-06-10 12:39:23 +02002330 /* FIXME it might be better to start readahead at commit root */
2331 key_start.objectid = logical;
2332 key_start.type = BTRFS_EXTENT_ITEM_KEY;
2333 key_start.offset = (u64)0;
2334 key_end.objectid = base + offset + nstripes * increment;
Josef Bacik3173a182013-03-07 14:22:04 -05002335 key_end.type = BTRFS_METADATA_ITEM_KEY;
2336 key_end.offset = (u64)-1;
Arne Jansen7a262852011-06-10 12:39:23 +02002337 reada1 = btrfs_reada_add(root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002338
Arne Jansen7a262852011-06-10 12:39:23 +02002339 key_start.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2340 key_start.type = BTRFS_EXTENT_CSUM_KEY;
2341 key_start.offset = logical;
2342 key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2343 key_end.type = BTRFS_EXTENT_CSUM_KEY;
2344 key_end.offset = base + offset + nstripes * increment;
2345 reada2 = btrfs_reada_add(csum_root, &key_start, &key_end);
Arne Jansena2de7332011-03-08 14:14:00 +01002346
Arne Jansen7a262852011-06-10 12:39:23 +02002347 if (!IS_ERR(reada1))
2348 btrfs_reada_wait(reada1);
2349 if (!IS_ERR(reada2))
2350 btrfs_reada_wait(reada2);
Arne Jansena2de7332011-03-08 14:14:00 +01002351
Arne Jansena2de7332011-03-08 14:14:00 +01002352
2353 /*
2354 * collect all data csums for the stripe to avoid seeking during
2355 * the scrub. This might currently (crc32) end up to be about 1MB
2356 */
Arne Jansene7786c32011-05-28 20:58:38 +00002357 blk_start_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002358
Arne Jansena2de7332011-03-08 14:14:00 +01002359 /*
2360 * now find all extents for each stripe and scrub them
2361 */
Arne Jansen7a262852011-06-10 12:39:23 +02002362 logical = base + offset;
2363 physical = map->stripes[num].physical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002364 logic_end = logical + increment * nstripes;
Arne Jansena2de7332011-03-08 14:14:00 +01002365 ret = 0;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002366 while (logical < logic_end) {
Arne Jansena2de7332011-03-08 14:14:00 +01002367 /*
2368 * canceled?
2369 */
2370 if (atomic_read(&fs_info->scrub_cancel_req) ||
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002371 atomic_read(&sctx->cancel_req)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002372 ret = -ECANCELED;
2373 goto out;
2374 }
2375 /*
2376 * check to see if we have to pause
2377 */
2378 if (atomic_read(&fs_info->scrub_pause_req)) {
2379 /* push queued extents */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002380 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002381 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002382 mutex_lock(&sctx->wr_ctx.wr_lock);
2383 scrub_wr_submit(sctx);
2384 mutex_unlock(&sctx->wr_ctx.wr_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002385 wait_event(sctx->list_wait,
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002386 atomic_read(&sctx->bios_in_flight) == 0);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002387 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
Wang Shilong3cb09292013-12-04 21:15:19 +08002388 scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002389 }
2390
Wang Shilong7c76edb2014-01-12 21:38:32 +08002391 if (btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2392 key.type = BTRFS_METADATA_ITEM_KEY;
2393 else
2394 key.type = BTRFS_EXTENT_ITEM_KEY;
Arne Jansena2de7332011-03-08 14:14:00 +01002395 key.objectid = logical;
Liu Bo625f1c8d2013-04-27 02:56:57 +00002396 key.offset = (u64)-1;
Arne Jansena2de7332011-03-08 14:14:00 +01002397
2398 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2399 if (ret < 0)
2400 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002401
Arne Jansen8c510322011-06-03 10:09:26 +02002402 if (ret > 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08002403 ret = btrfs_previous_extent_item(root, path, 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002404 if (ret < 0)
2405 goto out;
Arne Jansen8c510322011-06-03 10:09:26 +02002406 if (ret > 0) {
2407 /* there's no smaller item, so stick with the
2408 * larger one */
2409 btrfs_release_path(path);
2410 ret = btrfs_search_slot(NULL, root, &key,
2411 path, 0, 0);
2412 if (ret < 0)
2413 goto out;
2414 }
Arne Jansena2de7332011-03-08 14:14:00 +01002415 }
2416
Liu Bo625f1c8d2013-04-27 02:56:57 +00002417 stop_loop = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002418 while (1) {
Josef Bacik3173a182013-03-07 14:22:04 -05002419 u64 bytes;
2420
Arne Jansena2de7332011-03-08 14:14:00 +01002421 l = path->nodes[0];
2422 slot = path->slots[0];
2423 if (slot >= btrfs_header_nritems(l)) {
2424 ret = btrfs_next_leaf(root, path);
2425 if (ret == 0)
2426 continue;
2427 if (ret < 0)
2428 goto out;
2429
Liu Bo625f1c8d2013-04-27 02:56:57 +00002430 stop_loop = 1;
Arne Jansena2de7332011-03-08 14:14:00 +01002431 break;
2432 }
2433 btrfs_item_key_to_cpu(l, &key, slot);
2434
Josef Bacik3173a182013-03-07 14:22:04 -05002435 if (key.type == BTRFS_METADATA_ITEM_KEY)
2436 bytes = root->leafsize;
2437 else
2438 bytes = key.offset;
2439
2440 if (key.objectid + bytes <= logical)
Arne Jansena2de7332011-03-08 14:14:00 +01002441 goto next;
2442
Liu Bo625f1c8d2013-04-27 02:56:57 +00002443 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2444 key.type != BTRFS_METADATA_ITEM_KEY)
2445 goto next;
Arne Jansena2de7332011-03-08 14:14:00 +01002446
Liu Bo625f1c8d2013-04-27 02:56:57 +00002447 if (key.objectid >= logical + map->stripe_len) {
2448 /* out of this device extent */
2449 if (key.objectid >= logic_end)
2450 stop_loop = 1;
2451 break;
2452 }
Arne Jansena2de7332011-03-08 14:14:00 +01002453
2454 extent = btrfs_item_ptr(l, slot,
2455 struct btrfs_extent_item);
2456 flags = btrfs_extent_flags(l, extent);
2457 generation = btrfs_extent_generation(l, extent);
2458
2459 if (key.objectid < logical &&
2460 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002461 btrfs_err(fs_info,
2462 "scrub: tree block %llu spanning "
2463 "stripes, ignored. logical=%llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02002464 key.objectid, logical);
Arne Jansena2de7332011-03-08 14:14:00 +01002465 goto next;
2466 }
2467
Liu Bo625f1c8d2013-04-27 02:56:57 +00002468again:
2469 extent_logical = key.objectid;
2470 extent_len = bytes;
2471
Arne Jansena2de7332011-03-08 14:14:00 +01002472 /*
2473 * trim extent to this stripe
2474 */
Liu Bo625f1c8d2013-04-27 02:56:57 +00002475 if (extent_logical < logical) {
2476 extent_len -= logical - extent_logical;
2477 extent_logical = logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002478 }
Liu Bo625f1c8d2013-04-27 02:56:57 +00002479 if (extent_logical + extent_len >
Arne Jansena2de7332011-03-08 14:14:00 +01002480 logical + map->stripe_len) {
Liu Bo625f1c8d2013-04-27 02:56:57 +00002481 extent_len = logical + map->stripe_len -
2482 extent_logical;
Arne Jansena2de7332011-03-08 14:14:00 +01002483 }
2484
Liu Bo625f1c8d2013-04-27 02:56:57 +00002485 extent_physical = extent_logical - logical + physical;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002486 extent_dev = scrub_dev;
2487 extent_mirror_num = mirror_num;
2488 if (is_dev_replace)
2489 scrub_remap_extent(fs_info, extent_logical,
2490 extent_len, &extent_physical,
2491 &extent_dev,
2492 &extent_mirror_num);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002493
2494 ret = btrfs_lookup_csums_range(csum_root, logical,
2495 logical + map->stripe_len - 1,
2496 &sctx->csum_list, 1);
Arne Jansena2de7332011-03-08 14:14:00 +01002497 if (ret)
2498 goto out;
2499
Liu Bo625f1c8d2013-04-27 02:56:57 +00002500 ret = scrub_extent(sctx, extent_logical, extent_len,
2501 extent_physical, extent_dev, flags,
2502 generation, extent_mirror_num,
Stefan Behrens115930c2013-07-04 16:14:23 +02002503 extent_logical - logical + physical);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002504 if (ret)
2505 goto out;
2506
Josef Bacikd88d46c2013-06-10 12:59:04 +00002507 scrub_free_csums(sctx);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002508 if (extent_logical + extent_len <
2509 key.objectid + bytes) {
2510 logical += increment;
2511 physical += map->stripe_len;
2512
2513 if (logical < key.objectid + bytes) {
2514 cond_resched();
2515 goto again;
2516 }
2517
2518 if (logical >= logic_end) {
2519 stop_loop = 1;
2520 break;
2521 }
2522 }
Arne Jansena2de7332011-03-08 14:14:00 +01002523next:
2524 path->slots[0]++;
2525 }
Chris Mason71267332011-05-23 06:30:52 -04002526 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002527 logical += increment;
2528 physical += map->stripe_len;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002529 spin_lock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002530 if (stop_loop)
2531 sctx->stat.last_physical = map->stripes[num].physical +
2532 length;
2533 else
2534 sctx->stat.last_physical = physical;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002535 spin_unlock(&sctx->stat_lock);
Liu Bo625f1c8d2013-04-27 02:56:57 +00002536 if (stop_loop)
2537 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002538 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002539out:
Arne Jansena2de7332011-03-08 14:14:00 +01002540 /* push queued extents */
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002541 scrub_submit(sctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002542 mutex_lock(&sctx->wr_ctx.wr_lock);
2543 scrub_wr_submit(sctx);
2544 mutex_unlock(&sctx->wr_ctx.wr_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002545
Arne Jansene7786c32011-05-28 20:58:38 +00002546 blk_finish_plug(&plug);
Arne Jansena2de7332011-03-08 14:14:00 +01002547 btrfs_free_path(path);
2548 return ret < 0 ? ret : 0;
2549}
2550
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002551static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002552 struct btrfs_device *scrub_dev,
2553 u64 chunk_tree, u64 chunk_objectid,
2554 u64 chunk_offset, u64 length,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002555 u64 dev_offset, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002556{
2557 struct btrfs_mapping_tree *map_tree =
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002558 &sctx->dev_root->fs_info->mapping_tree;
Arne Jansena2de7332011-03-08 14:14:00 +01002559 struct map_lookup *map;
2560 struct extent_map *em;
2561 int i;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002562 int ret = 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002563
2564 read_lock(&map_tree->map_tree.lock);
2565 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2566 read_unlock(&map_tree->map_tree.lock);
2567
2568 if (!em)
2569 return -EINVAL;
2570
2571 map = (struct map_lookup *)em->bdev;
2572 if (em->start != chunk_offset)
2573 goto out;
2574
2575 if (em->len < length)
2576 goto out;
2577
2578 for (i = 0; i < map->num_stripes; ++i) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002579 if (map->stripes[i].dev->bdev == scrub_dev->bdev &&
Arne Jansen859acaf2012-02-09 15:09:02 +01002580 map->stripes[i].physical == dev_offset) {
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002581 ret = scrub_stripe(sctx, map, scrub_dev, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002582 chunk_offset, length,
2583 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002584 if (ret)
2585 goto out;
2586 }
2587 }
2588out:
2589 free_extent_map(em);
2590
2591 return ret;
2592}
2593
2594static noinline_for_stack
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002595int scrub_enumerate_chunks(struct scrub_ctx *sctx,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002596 struct btrfs_device *scrub_dev, u64 start, u64 end,
2597 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002598{
2599 struct btrfs_dev_extent *dev_extent = NULL;
2600 struct btrfs_path *path;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002601 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002602 struct btrfs_fs_info *fs_info = root->fs_info;
2603 u64 length;
2604 u64 chunk_tree;
2605 u64 chunk_objectid;
2606 u64 chunk_offset;
2607 int ret;
2608 int slot;
2609 struct extent_buffer *l;
2610 struct btrfs_key key;
2611 struct btrfs_key found_key;
2612 struct btrfs_block_group_cache *cache;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002613 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
Arne Jansena2de7332011-03-08 14:14:00 +01002614
2615 path = btrfs_alloc_path();
2616 if (!path)
2617 return -ENOMEM;
2618
2619 path->reada = 2;
2620 path->search_commit_root = 1;
2621 path->skip_locking = 1;
2622
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002623 key.objectid = scrub_dev->devid;
Arne Jansena2de7332011-03-08 14:14:00 +01002624 key.offset = 0ull;
2625 key.type = BTRFS_DEV_EXTENT_KEY;
2626
Arne Jansena2de7332011-03-08 14:14:00 +01002627 while (1) {
2628 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2629 if (ret < 0)
Arne Jansen8c510322011-06-03 10:09:26 +02002630 break;
2631 if (ret > 0) {
2632 if (path->slots[0] >=
2633 btrfs_header_nritems(path->nodes[0])) {
2634 ret = btrfs_next_leaf(root, path);
2635 if (ret)
2636 break;
2637 }
2638 }
Arne Jansena2de7332011-03-08 14:14:00 +01002639
2640 l = path->nodes[0];
2641 slot = path->slots[0];
2642
2643 btrfs_item_key_to_cpu(l, &found_key, slot);
2644
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002645 if (found_key.objectid != scrub_dev->devid)
Arne Jansena2de7332011-03-08 14:14:00 +01002646 break;
2647
Arne Jansen8c510322011-06-03 10:09:26 +02002648 if (btrfs_key_type(&found_key) != BTRFS_DEV_EXTENT_KEY)
Arne Jansena2de7332011-03-08 14:14:00 +01002649 break;
2650
2651 if (found_key.offset >= end)
2652 break;
2653
2654 if (found_key.offset < key.offset)
2655 break;
2656
2657 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2658 length = btrfs_dev_extent_length(l, dev_extent);
2659
2660 if (found_key.offset + length <= start) {
2661 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002662 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002663 continue;
2664 }
2665
2666 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2667 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
2668 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
2669
2670 /*
2671 * get a reference on the corresponding block group to prevent
2672 * the chunk from going away while we scrub it
2673 */
2674 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2675 if (!cache) {
2676 ret = -ENOENT;
Arne Jansen8c510322011-06-03 10:09:26 +02002677 break;
Arne Jansena2de7332011-03-08 14:14:00 +01002678 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01002679 dev_replace->cursor_right = found_key.offset + length;
2680 dev_replace->cursor_left = found_key.offset;
2681 dev_replace->item_needs_writeback = 1;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002682 ret = scrub_chunk(sctx, scrub_dev, chunk_tree, chunk_objectid,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002683 chunk_offset, length, found_key.offset,
2684 is_dev_replace);
2685
2686 /*
2687 * flush, submit all pending read and write bios, afterwards
2688 * wait for them.
2689 * Note that in the dev replace case, a read request causes
2690 * write requests that are submitted in the read completion
2691 * worker. Therefore in the current situation, it is required
2692 * that all write requests are flushed, so that all read and
2693 * write requests are really completed when bios_in_flight
2694 * changes to 0.
2695 */
2696 atomic_set(&sctx->wr_ctx.flush_all_writes, 1);
2697 scrub_submit(sctx);
2698 mutex_lock(&sctx->wr_ctx.wr_lock);
2699 scrub_wr_submit(sctx);
2700 mutex_unlock(&sctx->wr_ctx.wr_lock);
2701
2702 wait_event(sctx->list_wait,
2703 atomic_read(&sctx->bios_in_flight) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08002704 atomic_inc(&fs_info->scrubs_paused);
2705 wake_up(&fs_info->scrub_pause_wait);
2706
2707 /*
2708 * must be called before we decrease @scrub_paused.
2709 * make sure we don't block transaction commit while
2710 * we are waiting pending workers finished.
2711 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002712 wait_event(sctx->list_wait,
2713 atomic_read(&sctx->workers_pending) == 0);
Wang Shilong12cf9372014-02-19 19:24:17 +08002714 atomic_set(&sctx->wr_ctx.flush_all_writes, 0);
2715
2716 mutex_lock(&fs_info->scrub_lock);
2717 __scrub_blocked_if_needed(fs_info);
2718 atomic_dec(&fs_info->scrubs_paused);
2719 mutex_unlock(&fs_info->scrub_lock);
2720 wake_up(&fs_info->scrub_pause_wait);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002721
Arne Jansena2de7332011-03-08 14:14:00 +01002722 btrfs_put_block_group(cache);
2723 if (ret)
2724 break;
Stefan Behrensaf1be4f2012-11-27 17:39:51 +00002725 if (is_dev_replace &&
2726 atomic64_read(&dev_replace->num_write_errors) > 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002727 ret = -EIO;
2728 break;
2729 }
2730 if (sctx->stat.malloc_errors > 0) {
2731 ret = -ENOMEM;
2732 break;
2733 }
Arne Jansena2de7332011-03-08 14:14:00 +01002734
Ilya Dryomov539f3582013-10-07 13:42:57 +03002735 dev_replace->cursor_left = dev_replace->cursor_right;
2736 dev_replace->item_needs_writeback = 1;
2737
Arne Jansena2de7332011-03-08 14:14:00 +01002738 key.offset = found_key.offset + length;
Chris Mason71267332011-05-23 06:30:52 -04002739 btrfs_release_path(path);
Arne Jansena2de7332011-03-08 14:14:00 +01002740 }
2741
Arne Jansena2de7332011-03-08 14:14:00 +01002742 btrfs_free_path(path);
Arne Jansen8c510322011-06-03 10:09:26 +02002743
2744 /*
2745 * ret can still be 1 from search_slot or next_leaf,
2746 * that's not an error
2747 */
2748 return ret < 0 ? ret : 0;
Arne Jansena2de7332011-03-08 14:14:00 +01002749}
2750
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002751static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
2752 struct btrfs_device *scrub_dev)
Arne Jansena2de7332011-03-08 14:14:00 +01002753{
2754 int i;
2755 u64 bytenr;
2756 u64 gen;
2757 int ret;
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002758 struct btrfs_root *root = sctx->dev_root;
Arne Jansena2de7332011-03-08 14:14:00 +01002759
Miao Xie87533c42013-01-29 10:14:48 +00002760 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002761 return -EIO;
2762
Arne Jansena2de7332011-03-08 14:14:00 +01002763 gen = root->fs_info->last_trans_committed;
2764
2765 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2766 bytenr = btrfs_sb_offset(i);
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002767 if (bytenr + BTRFS_SUPER_INFO_SIZE > scrub_dev->total_bytes)
Arne Jansena2de7332011-03-08 14:14:00 +01002768 break;
2769
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002770 ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr,
Stefan Behrensa36cf8b2012-11-02 13:26:57 +01002771 scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i,
Stefan Behrensff023aa2012-11-06 11:43:11 +01002772 NULL, 1, bytenr);
Arne Jansena2de7332011-03-08 14:14:00 +01002773 if (ret)
2774 return ret;
2775 }
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002776 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002777
2778 return 0;
2779}
2780
2781/*
2782 * get a reference count on fs_info->scrub_workers. start worker if necessary
2783 */
Stefan Behrensff023aa2012-11-06 11:43:11 +01002784static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info,
2785 int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002786{
Josef Bacik0dc3b842011-11-18 14:37:27 -05002787 int ret = 0;
Qu Wenruo0339ef22014-02-28 10:46:17 +08002788 int flags = WQ_FREEZABLE | WQ_UNBOUND;
2789 int max_active = fs_info->thread_pool_size;
Arne Jansena2de7332011-03-08 14:14:00 +01002790
Arne Jansen632dd772011-06-10 12:07:07 +02002791 if (fs_info->scrub_workers_refcnt == 0) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01002792 if (is_dev_replace)
Qu Wenruo0339ef22014-02-28 10:46:17 +08002793 fs_info->scrub_workers =
2794 btrfs_alloc_workqueue("btrfs-scrub", flags,
2795 1, 4);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002796 else
Qu Wenruo0339ef22014-02-28 10:46:17 +08002797 fs_info->scrub_workers =
2798 btrfs_alloc_workqueue("btrfs-scrub", flags,
2799 max_active, 4);
2800 if (!fs_info->scrub_workers) {
2801 ret = -ENOMEM;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002802 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08002803 }
2804 fs_info->scrub_wr_completion_workers =
2805 btrfs_alloc_workqueue("btrfs-scrubwrc", flags,
2806 max_active, 2);
2807 if (!fs_info->scrub_wr_completion_workers) {
2808 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002809 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08002810 }
2811 fs_info->scrub_nocow_workers =
2812 btrfs_alloc_workqueue("btrfs-scrubnc", flags, 1, 0);
2813 if (!fs_info->scrub_nocow_workers) {
2814 ret = -ENOMEM;
Stefan Behrensff023aa2012-11-06 11:43:11 +01002815 goto out;
Qu Wenruo0339ef22014-02-28 10:46:17 +08002816 }
Arne Jansen632dd772011-06-10 12:07:07 +02002817 }
Arne Jansena2de7332011-03-08 14:14:00 +01002818 ++fs_info->scrub_workers_refcnt;
Josef Bacik0dc3b842011-11-18 14:37:27 -05002819out:
Josef Bacik0dc3b842011-11-18 14:37:27 -05002820 return ret;
Arne Jansena2de7332011-03-08 14:14:00 +01002821}
2822
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002823static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01002824{
Stefan Behrensff023aa2012-11-06 11:43:11 +01002825 if (--fs_info->scrub_workers_refcnt == 0) {
Qu Wenruo0339ef22014-02-28 10:46:17 +08002826 btrfs_destroy_workqueue(fs_info->scrub_workers);
2827 btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers);
2828 btrfs_destroy_workqueue(fs_info->scrub_nocow_workers);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002829 }
Arne Jansena2de7332011-03-08 14:14:00 +01002830 WARN_ON(fs_info->scrub_workers_refcnt < 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002831}
2832
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002833int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
2834 u64 end, struct btrfs_scrub_progress *progress,
Stefan Behrens63a212a2012-11-05 18:29:28 +01002835 int readonly, int is_dev_replace)
Arne Jansena2de7332011-03-08 14:14:00 +01002836{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002837 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01002838 int ret;
2839 struct btrfs_device *dev;
2840
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002841 if (btrfs_fs_closing(fs_info))
Arne Jansena2de7332011-03-08 14:14:00 +01002842 return -EINVAL;
2843
2844 /*
2845 * check some assumptions
2846 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002847 if (fs_info->chunk_root->nodesize != fs_info->chunk_root->leafsize) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002848 btrfs_err(fs_info,
2849 "scrub: size assumption nodesize == leafsize (%d == %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002850 fs_info->chunk_root->nodesize,
2851 fs_info->chunk_root->leafsize);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002852 return -EINVAL;
2853 }
2854
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002855 if (fs_info->chunk_root->nodesize > BTRFS_STRIPE_LEN) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002856 /*
2857 * in this case scrub is unable to calculate the checksum
2858 * the way scrub is implemented. Do not handle this
2859 * situation at all because it won't ever happen.
2860 */
Frank Holtonefe120a2013-12-20 11:37:06 -05002861 btrfs_err(fs_info,
2862 "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails",
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002863 fs_info->chunk_root->nodesize, BTRFS_STRIPE_LEN);
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002864 return -EINVAL;
2865 }
2866
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002867 if (fs_info->chunk_root->sectorsize != PAGE_SIZE) {
Stefan Behrensb5d67f62012-03-27 14:21:27 -04002868 /* not supported for data w/o checksums */
Frank Holtonefe120a2013-12-20 11:37:06 -05002869 btrfs_err(fs_info,
2870 "scrub: size assumption sectorsize != PAGE_SIZE "
2871 "(%d != %lu) fails",
Geert Uytterhoeven27f9f022013-08-20 13:20:09 +02002872 fs_info->chunk_root->sectorsize, PAGE_SIZE);
Arne Jansena2de7332011-03-08 14:14:00 +01002873 return -EINVAL;
2874 }
2875
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002876 if (fs_info->chunk_root->nodesize >
2877 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK ||
2878 fs_info->chunk_root->sectorsize >
2879 PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) {
2880 /*
2881 * would exhaust the array bounds of pagev member in
2882 * struct scrub_block
2883 */
Frank Holtonefe120a2013-12-20 11:37:06 -05002884 btrfs_err(fs_info, "scrub: size assumption nodesize and sectorsize "
2885 "<= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails",
Stefan Behrens7a9e9982012-11-02 14:58:04 +01002886 fs_info->chunk_root->nodesize,
2887 SCRUB_MAX_PAGES_PER_BLOCK,
2888 fs_info->chunk_root->sectorsize,
2889 SCRUB_MAX_PAGES_PER_BLOCK);
2890 return -EINVAL;
2891 }
2892
Arne Jansena2de7332011-03-08 14:14:00 +01002893
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002894 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2895 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002896 if (!dev || (dev->missing && !is_dev_replace)) {
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002897 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002898 return -ENODEV;
2899 }
Arne Jansena2de7332011-03-08 14:14:00 +01002900
Wang Shilong3b7a0162013-10-12 02:11:12 +08002901 mutex_lock(&fs_info->scrub_lock);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002902 if (!dev->in_fs_metadata || dev->is_tgtdev_for_dev_replace) {
Arne Jansena2de7332011-03-08 14:14:00 +01002903 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002904 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002905 return -EIO;
Arne Jansena2de7332011-03-08 14:14:00 +01002906 }
2907
Stefan Behrens8dabb742012-11-06 13:15:27 +01002908 btrfs_dev_replace_lock(&fs_info->dev_replace);
2909 if (dev->scrub_device ||
2910 (!is_dev_replace &&
2911 btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) {
2912 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002913 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002914 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002915 return -EINPROGRESS;
2916 }
Stefan Behrens8dabb742012-11-06 13:15:27 +01002917 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Wang Shilong3b7a0162013-10-12 02:11:12 +08002918
2919 ret = scrub_workers_get(fs_info, is_dev_replace);
2920 if (ret) {
2921 mutex_unlock(&fs_info->scrub_lock);
2922 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2923 return ret;
2924 }
2925
Stefan Behrens63a212a2012-11-05 18:29:28 +01002926 sctx = scrub_setup_ctx(dev, is_dev_replace);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002927 if (IS_ERR(sctx)) {
Arne Jansena2de7332011-03-08 14:14:00 +01002928 mutex_unlock(&fs_info->scrub_lock);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002929 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2930 scrub_workers_put(fs_info);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002931 return PTR_ERR(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002932 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002933 sctx->readonly = readonly;
2934 dev->scrub_device = sctx;
Wang Shilong3cb09292013-12-04 21:15:19 +08002935 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Arne Jansena2de7332011-03-08 14:14:00 +01002936
Wang Shilong3cb09292013-12-04 21:15:19 +08002937 /*
2938 * checking @scrub_pause_req here, we can avoid
2939 * race between committing transaction and scrubbing.
2940 */
Wang Shilongcb7ab022013-12-04 21:16:53 +08002941 __scrub_blocked_if_needed(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002942 atomic_inc(&fs_info->scrubs_running);
2943 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002944
Stefan Behrensff023aa2012-11-06 11:43:11 +01002945 if (!is_dev_replace) {
Wang Shilong9b011ad2013-10-25 19:12:02 +08002946 /*
2947 * by holding device list mutex, we can
2948 * kick off writing super in log tree sync.
2949 */
Wang Shilong3cb09292013-12-04 21:15:19 +08002950 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002951 ret = scrub_supers(sctx, dev);
Wang Shilong3cb09292013-12-04 21:15:19 +08002952 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrensff023aa2012-11-06 11:43:11 +01002953 }
Arne Jansena2de7332011-03-08 14:14:00 +01002954
2955 if (!ret)
Stefan Behrensff023aa2012-11-06 11:43:11 +01002956 ret = scrub_enumerate_chunks(sctx, dev, start, end,
2957 is_dev_replace);
Arne Jansena2de7332011-03-08 14:14:00 +01002958
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002959 wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0);
Arne Jansena2de7332011-03-08 14:14:00 +01002960 atomic_dec(&fs_info->scrubs_running);
2961 wake_up(&fs_info->scrub_pause_wait);
2962
Stefan Behrensb6bfebc2012-11-02 16:44:58 +01002963 wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0);
Jan Schmidt0ef8e452011-06-13 20:04:15 +02002964
Arne Jansena2de7332011-03-08 14:14:00 +01002965 if (progress)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002966 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01002967
2968 mutex_lock(&fs_info->scrub_lock);
2969 dev->scrub_device = NULL;
Wang Shilong3b7a0162013-10-12 02:11:12 +08002970 scrub_workers_put(fs_info);
Arne Jansena2de7332011-03-08 14:14:00 +01002971 mutex_unlock(&fs_info->scrub_lock);
2972
Stefan Behrensd9d181c2012-11-02 09:58:09 +01002973 scrub_free_ctx(sctx);
Arne Jansena2de7332011-03-08 14:14:00 +01002974
2975 return ret;
2976}
2977
Jeff Mahoney143bede2012-03-01 14:56:26 +01002978void btrfs_scrub_pause(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002979{
2980 struct btrfs_fs_info *fs_info = root->fs_info;
2981
2982 mutex_lock(&fs_info->scrub_lock);
2983 atomic_inc(&fs_info->scrub_pause_req);
2984 while (atomic_read(&fs_info->scrubs_paused) !=
2985 atomic_read(&fs_info->scrubs_running)) {
2986 mutex_unlock(&fs_info->scrub_lock);
2987 wait_event(fs_info->scrub_pause_wait,
2988 atomic_read(&fs_info->scrubs_paused) ==
2989 atomic_read(&fs_info->scrubs_running));
2990 mutex_lock(&fs_info->scrub_lock);
2991 }
2992 mutex_unlock(&fs_info->scrub_lock);
Arne Jansena2de7332011-03-08 14:14:00 +01002993}
2994
Jeff Mahoney143bede2012-03-01 14:56:26 +01002995void btrfs_scrub_continue(struct btrfs_root *root)
Arne Jansena2de7332011-03-08 14:14:00 +01002996{
2997 struct btrfs_fs_info *fs_info = root->fs_info;
2998
2999 atomic_dec(&fs_info->scrub_pause_req);
3000 wake_up(&fs_info->scrub_pause_wait);
Arne Jansena2de7332011-03-08 14:14:00 +01003001}
3002
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003003int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info)
Arne Jansena2de7332011-03-08 14:14:00 +01003004{
Arne Jansena2de7332011-03-08 14:14:00 +01003005 mutex_lock(&fs_info->scrub_lock);
3006 if (!atomic_read(&fs_info->scrubs_running)) {
3007 mutex_unlock(&fs_info->scrub_lock);
3008 return -ENOTCONN;
3009 }
3010
3011 atomic_inc(&fs_info->scrub_cancel_req);
3012 while (atomic_read(&fs_info->scrubs_running)) {
3013 mutex_unlock(&fs_info->scrub_lock);
3014 wait_event(fs_info->scrub_pause_wait,
3015 atomic_read(&fs_info->scrubs_running) == 0);
3016 mutex_lock(&fs_info->scrub_lock);
3017 }
3018 atomic_dec(&fs_info->scrub_cancel_req);
3019 mutex_unlock(&fs_info->scrub_lock);
3020
3021 return 0;
3022}
3023
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003024int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
3025 struct btrfs_device *dev)
Jeff Mahoney49b25e02012-03-01 17:24:58 +01003026{
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003027 struct scrub_ctx *sctx;
Arne Jansena2de7332011-03-08 14:14:00 +01003028
3029 mutex_lock(&fs_info->scrub_lock);
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003030 sctx = dev->scrub_device;
3031 if (!sctx) {
Arne Jansena2de7332011-03-08 14:14:00 +01003032 mutex_unlock(&fs_info->scrub_lock);
3033 return -ENOTCONN;
3034 }
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003035 atomic_inc(&sctx->cancel_req);
Arne Jansena2de7332011-03-08 14:14:00 +01003036 while (dev->scrub_device) {
3037 mutex_unlock(&fs_info->scrub_lock);
3038 wait_event(fs_info->scrub_pause_wait,
3039 dev->scrub_device == NULL);
3040 mutex_lock(&fs_info->scrub_lock);
3041 }
3042 mutex_unlock(&fs_info->scrub_lock);
3043
3044 return 0;
3045}
Stefan Behrens1623ede2012-03-27 14:21:26 -04003046
Arne Jansena2de7332011-03-08 14:14:00 +01003047int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
3048 struct btrfs_scrub_progress *progress)
3049{
3050 struct btrfs_device *dev;
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003051 struct scrub_ctx *sctx = NULL;
Arne Jansena2de7332011-03-08 14:14:00 +01003052
3053 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01003054 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL);
Arne Jansena2de7332011-03-08 14:14:00 +01003055 if (dev)
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003056 sctx = dev->scrub_device;
3057 if (sctx)
3058 memcpy(progress, &sctx->stat, sizeof(*progress));
Arne Jansena2de7332011-03-08 14:14:00 +01003059 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3060
Stefan Behrensd9d181c2012-11-02 09:58:09 +01003061 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
Arne Jansena2de7332011-03-08 14:14:00 +01003062}
Stefan Behrensff023aa2012-11-06 11:43:11 +01003063
3064static void scrub_remap_extent(struct btrfs_fs_info *fs_info,
3065 u64 extent_logical, u64 extent_len,
3066 u64 *extent_physical,
3067 struct btrfs_device **extent_dev,
3068 int *extent_mirror_num)
3069{
3070 u64 mapped_length;
3071 struct btrfs_bio *bbio = NULL;
3072 int ret;
3073
3074 mapped_length = extent_len;
3075 ret = btrfs_map_block(fs_info, READ, extent_logical,
3076 &mapped_length, &bbio, 0);
3077 if (ret || !bbio || mapped_length < extent_len ||
3078 !bbio->stripes[0].dev->bdev) {
3079 kfree(bbio);
3080 return;
3081 }
3082
3083 *extent_physical = bbio->stripes[0].physical;
3084 *extent_mirror_num = bbio->mirror_num;
3085 *extent_dev = bbio->stripes[0].dev;
3086 kfree(bbio);
3087}
3088
3089static int scrub_setup_wr_ctx(struct scrub_ctx *sctx,
3090 struct scrub_wr_ctx *wr_ctx,
3091 struct btrfs_fs_info *fs_info,
3092 struct btrfs_device *dev,
3093 int is_dev_replace)
3094{
3095 WARN_ON(wr_ctx->wr_curr_bio != NULL);
3096
3097 mutex_init(&wr_ctx->wr_lock);
3098 wr_ctx->wr_curr_bio = NULL;
3099 if (!is_dev_replace)
3100 return 0;
3101
3102 WARN_ON(!dev->bdev);
3103 wr_ctx->pages_per_wr_bio = min_t(int, SCRUB_PAGES_PER_WR_BIO,
3104 bio_get_nr_vecs(dev->bdev));
3105 wr_ctx->tgtdev = dev;
3106 atomic_set(&wr_ctx->flush_all_writes, 0);
3107 return 0;
3108}
3109
3110static void scrub_free_wr_ctx(struct scrub_wr_ctx *wr_ctx)
3111{
3112 mutex_lock(&wr_ctx->wr_lock);
3113 kfree(wr_ctx->wr_curr_bio);
3114 wr_ctx->wr_curr_bio = NULL;
3115 mutex_unlock(&wr_ctx->wr_lock);
3116}
3117
3118static int copy_nocow_pages(struct scrub_ctx *sctx, u64 logical, u64 len,
3119 int mirror_num, u64 physical_for_dev_replace)
3120{
3121 struct scrub_copy_nocow_ctx *nocow_ctx;
3122 struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
3123
3124 nocow_ctx = kzalloc(sizeof(*nocow_ctx), GFP_NOFS);
3125 if (!nocow_ctx) {
3126 spin_lock(&sctx->stat_lock);
3127 sctx->stat.malloc_errors++;
3128 spin_unlock(&sctx->stat_lock);
3129 return -ENOMEM;
3130 }
3131
3132 scrub_pending_trans_workers_inc(sctx);
3133
3134 nocow_ctx->sctx = sctx;
3135 nocow_ctx->logical = logical;
3136 nocow_ctx->len = len;
3137 nocow_ctx->mirror_num = mirror_num;
3138 nocow_ctx->physical_for_dev_replace = physical_for_dev_replace;
Qu Wenruo0339ef22014-02-28 10:46:17 +08003139 btrfs_init_work(&nocow_ctx->work, copy_nocow_pages_worker, NULL, NULL);
Josef Bacik652f25a2013-09-12 16:58:28 -04003140 INIT_LIST_HEAD(&nocow_ctx->inodes);
Qu Wenruo0339ef22014-02-28 10:46:17 +08003141 btrfs_queue_work(fs_info->scrub_nocow_workers,
3142 &nocow_ctx->work);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003143
3144 return 0;
3145}
3146
Josef Bacik652f25a2013-09-12 16:58:28 -04003147static int record_inode_for_nocow(u64 inum, u64 offset, u64 root, void *ctx)
3148{
3149 struct scrub_copy_nocow_ctx *nocow_ctx = ctx;
3150 struct scrub_nocow_inode *nocow_inode;
3151
3152 nocow_inode = kzalloc(sizeof(*nocow_inode), GFP_NOFS);
3153 if (!nocow_inode)
3154 return -ENOMEM;
3155 nocow_inode->inum = inum;
3156 nocow_inode->offset = offset;
3157 nocow_inode->root = root;
3158 list_add_tail(&nocow_inode->list, &nocow_ctx->inodes);
3159 return 0;
3160}
3161
3162#define COPY_COMPLETE 1
3163
Qu Wenruo0339ef22014-02-28 10:46:17 +08003164static void copy_nocow_pages_worker(struct btrfs_work_struct *work)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003165{
3166 struct scrub_copy_nocow_ctx *nocow_ctx =
3167 container_of(work, struct scrub_copy_nocow_ctx, work);
3168 struct scrub_ctx *sctx = nocow_ctx->sctx;
3169 u64 logical = nocow_ctx->logical;
3170 u64 len = nocow_ctx->len;
3171 int mirror_num = nocow_ctx->mirror_num;
3172 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
3173 int ret;
3174 struct btrfs_trans_handle *trans = NULL;
3175 struct btrfs_fs_info *fs_info;
3176 struct btrfs_path *path;
3177 struct btrfs_root *root;
3178 int not_written = 0;
3179
3180 fs_info = sctx->dev_root->fs_info;
3181 root = fs_info->extent_root;
3182
3183 path = btrfs_alloc_path();
3184 if (!path) {
3185 spin_lock(&sctx->stat_lock);
3186 sctx->stat.malloc_errors++;
3187 spin_unlock(&sctx->stat_lock);
3188 not_written = 1;
3189 goto out;
3190 }
3191
3192 trans = btrfs_join_transaction(root);
3193 if (IS_ERR(trans)) {
3194 not_written = 1;
3195 goto out;
3196 }
3197
3198 ret = iterate_inodes_from_logical(logical, fs_info, path,
Josef Bacik652f25a2013-09-12 16:58:28 -04003199 record_inode_for_nocow, nocow_ctx);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003200 if (ret != 0 && ret != -ENOENT) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003201 btrfs_warn(fs_info, "iterate_inodes_from_logical() failed: log %llu, "
3202 "phys %llu, len %llu, mir %u, ret %d",
Geert Uytterhoeven118a0a22013-08-20 13:20:10 +02003203 logical, physical_for_dev_replace, len, mirror_num,
3204 ret);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003205 not_written = 1;
3206 goto out;
3207 }
3208
Josef Bacik652f25a2013-09-12 16:58:28 -04003209 btrfs_end_transaction(trans, root);
3210 trans = NULL;
3211 while (!list_empty(&nocow_ctx->inodes)) {
3212 struct scrub_nocow_inode *entry;
3213 entry = list_first_entry(&nocow_ctx->inodes,
3214 struct scrub_nocow_inode,
3215 list);
3216 list_del_init(&entry->list);
3217 ret = copy_nocow_pages_for_inode(entry->inum, entry->offset,
3218 entry->root, nocow_ctx);
3219 kfree(entry);
3220 if (ret == COPY_COMPLETE) {
3221 ret = 0;
3222 break;
3223 } else if (ret) {
3224 break;
3225 }
3226 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003227out:
Josef Bacik652f25a2013-09-12 16:58:28 -04003228 while (!list_empty(&nocow_ctx->inodes)) {
3229 struct scrub_nocow_inode *entry;
3230 entry = list_first_entry(&nocow_ctx->inodes,
3231 struct scrub_nocow_inode,
3232 list);
3233 list_del_init(&entry->list);
3234 kfree(entry);
3235 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003236 if (trans && !IS_ERR(trans))
3237 btrfs_end_transaction(trans, root);
3238 if (not_written)
3239 btrfs_dev_replace_stats_inc(&fs_info->dev_replace.
3240 num_uncorrectable_read_errors);
3241
3242 btrfs_free_path(path);
3243 kfree(nocow_ctx);
3244
3245 scrub_pending_trans_workers_dec(sctx);
3246}
3247
Josef Bacik652f25a2013-09-12 16:58:28 -04003248static int copy_nocow_pages_for_inode(u64 inum, u64 offset, u64 root,
3249 struct scrub_copy_nocow_ctx *nocow_ctx)
Stefan Behrensff023aa2012-11-06 11:43:11 +01003250{
Miao Xie826aa0a2013-06-27 18:50:59 +08003251 struct btrfs_fs_info *fs_info = nocow_ctx->sctx->dev_root->fs_info;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003252 struct btrfs_key key;
Miao Xie826aa0a2013-06-27 18:50:59 +08003253 struct inode *inode;
3254 struct page *page;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003255 struct btrfs_root *local_root;
Josef Bacik652f25a2013-09-12 16:58:28 -04003256 struct btrfs_ordered_extent *ordered;
3257 struct extent_map *em;
3258 struct extent_state *cached_state = NULL;
3259 struct extent_io_tree *io_tree;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003260 u64 physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04003261 u64 len = nocow_ctx->len;
3262 u64 lockstart = offset, lockend = offset + len - 1;
Miao Xie826aa0a2013-06-27 18:50:59 +08003263 unsigned long index;
Liu Bo6f1c3602013-01-29 03:22:10 +00003264 int srcu_index;
Josef Bacik652f25a2013-09-12 16:58:28 -04003265 int ret = 0;
3266 int err = 0;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003267
3268 key.objectid = root;
3269 key.type = BTRFS_ROOT_ITEM_KEY;
3270 key.offset = (u64)-1;
Liu Bo6f1c3602013-01-29 03:22:10 +00003271
3272 srcu_index = srcu_read_lock(&fs_info->subvol_srcu);
3273
Stefan Behrensff023aa2012-11-06 11:43:11 +01003274 local_root = btrfs_read_fs_root_no_name(fs_info, &key);
Liu Bo6f1c3602013-01-29 03:22:10 +00003275 if (IS_ERR(local_root)) {
3276 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003277 return PTR_ERR(local_root);
Liu Bo6f1c3602013-01-29 03:22:10 +00003278 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003279
3280 key.type = BTRFS_INODE_ITEM_KEY;
3281 key.objectid = inum;
3282 key.offset = 0;
3283 inode = btrfs_iget(fs_info->sb, &key, local_root, NULL);
Liu Bo6f1c3602013-01-29 03:22:10 +00003284 srcu_read_unlock(&fs_info->subvol_srcu, srcu_index);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003285 if (IS_ERR(inode))
3286 return PTR_ERR(inode);
3287
Miao Xieedd14002013-06-27 18:51:00 +08003288 /* Avoid truncate/dio/punch hole.. */
3289 mutex_lock(&inode->i_mutex);
3290 inode_dio_wait(inode);
3291
Stefan Behrensff023aa2012-11-06 11:43:11 +01003292 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
Josef Bacik652f25a2013-09-12 16:58:28 -04003293 io_tree = &BTRFS_I(inode)->io_tree;
3294
3295 lock_extent_bits(io_tree, lockstart, lockend, 0, &cached_state);
3296 ordered = btrfs_lookup_ordered_range(inode, lockstart, len);
3297 if (ordered) {
3298 btrfs_put_ordered_extent(ordered);
3299 goto out_unlock;
3300 }
3301
3302 em = btrfs_get_extent(inode, NULL, 0, lockstart, len, 0);
3303 if (IS_ERR(em)) {
3304 ret = PTR_ERR(em);
3305 goto out_unlock;
3306 }
3307
3308 /*
3309 * This extent does not actually cover the logical extent anymore,
3310 * move on to the next inode.
3311 */
3312 if (em->block_start > nocow_ctx->logical ||
3313 em->block_start + em->block_len < nocow_ctx->logical + len) {
3314 free_extent_map(em);
3315 goto out_unlock;
3316 }
3317 free_extent_map(em);
3318
Stefan Behrensff023aa2012-11-06 11:43:11 +01003319 while (len >= PAGE_CACHE_SIZE) {
Stefan Behrensff023aa2012-11-06 11:43:11 +01003320 index = offset >> PAGE_CACHE_SHIFT;
Miao Xieedd14002013-06-27 18:51:00 +08003321again:
Stefan Behrensff023aa2012-11-06 11:43:11 +01003322 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3323 if (!page) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003324 btrfs_err(fs_info, "find_or_create_page() failed");
Stefan Behrensff023aa2012-11-06 11:43:11 +01003325 ret = -ENOMEM;
Miao Xie826aa0a2013-06-27 18:50:59 +08003326 goto out;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003327 }
3328
3329 if (PageUptodate(page)) {
3330 if (PageDirty(page))
3331 goto next_page;
3332 } else {
3333 ClearPageError(page);
Josef Bacik652f25a2013-09-12 16:58:28 -04003334 err = extent_read_full_page_nolock(io_tree, page,
3335 btrfs_get_extent,
3336 nocow_ctx->mirror_num);
Miao Xie826aa0a2013-06-27 18:50:59 +08003337 if (err) {
3338 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003339 goto next_page;
3340 }
Miao Xieedd14002013-06-27 18:51:00 +08003341
Miao Xie26b258912013-06-27 18:50:58 +08003342 lock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08003343 /*
3344 * If the page has been remove from the page cache,
3345 * the data on it is meaningless, because it may be
3346 * old one, the new data may be written into the new
3347 * page in the page cache.
3348 */
3349 if (page->mapping != inode->i_mapping) {
Josef Bacik652f25a2013-09-12 16:58:28 -04003350 unlock_page(page);
Miao Xieedd14002013-06-27 18:51:00 +08003351 page_cache_release(page);
3352 goto again;
3353 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003354 if (!PageUptodate(page)) {
3355 ret = -EIO;
3356 goto next_page;
3357 }
3358 }
Miao Xie826aa0a2013-06-27 18:50:59 +08003359 err = write_page_nocow(nocow_ctx->sctx,
3360 physical_for_dev_replace, page);
3361 if (err)
3362 ret = err;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003363next_page:
Miao Xie826aa0a2013-06-27 18:50:59 +08003364 unlock_page(page);
3365 page_cache_release(page);
3366
3367 if (ret)
3368 break;
3369
Stefan Behrensff023aa2012-11-06 11:43:11 +01003370 offset += PAGE_CACHE_SIZE;
3371 physical_for_dev_replace += PAGE_CACHE_SIZE;
3372 len -= PAGE_CACHE_SIZE;
3373 }
Josef Bacik652f25a2013-09-12 16:58:28 -04003374 ret = COPY_COMPLETE;
3375out_unlock:
3376 unlock_extent_cached(io_tree, lockstart, lockend, &cached_state,
3377 GFP_NOFS);
Miao Xie826aa0a2013-06-27 18:50:59 +08003378out:
Miao Xieedd14002013-06-27 18:51:00 +08003379 mutex_unlock(&inode->i_mutex);
Miao Xie826aa0a2013-06-27 18:50:59 +08003380 iput(inode);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003381 return ret;
3382}
3383
3384static int write_page_nocow(struct scrub_ctx *sctx,
3385 u64 physical_for_dev_replace, struct page *page)
3386{
3387 struct bio *bio;
3388 struct btrfs_device *dev;
3389 int ret;
Stefan Behrensff023aa2012-11-06 11:43:11 +01003390
3391 dev = sctx->wr_ctx.tgtdev;
3392 if (!dev)
3393 return -EIO;
3394 if (!dev->bdev) {
3395 printk_ratelimited(KERN_WARNING
Frank Holtonefe120a2013-12-20 11:37:06 -05003396 "BTRFS: scrub write_page_nocow(bdev == NULL) is unexpected!\n");
Stefan Behrensff023aa2012-11-06 11:43:11 +01003397 return -EIO;
3398 }
Chris Mason9be33952013-05-17 18:30:14 -04003399 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Stefan Behrensff023aa2012-11-06 11:43:11 +01003400 if (!bio) {
3401 spin_lock(&sctx->stat_lock);
3402 sctx->stat.malloc_errors++;
3403 spin_unlock(&sctx->stat_lock);
3404 return -ENOMEM;
3405 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003406 bio->bi_size = 0;
3407 bio->bi_sector = physical_for_dev_replace >> 9;
3408 bio->bi_bdev = dev->bdev;
3409 ret = bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
3410 if (ret != PAGE_CACHE_SIZE) {
3411leave_with_eio:
3412 bio_put(bio);
3413 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
3414 return -EIO;
3415 }
Stefan Behrensff023aa2012-11-06 11:43:11 +01003416
Kent Overstreetc170bbb2013-11-24 16:32:22 -07003417 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio))
Stefan Behrensff023aa2012-11-06 11:43:11 +01003418 goto leave_with_eio;
3419
3420 bio_put(bio);
3421 return 0;
3422}