David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2 | /* |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 3 | * Copyright (C) 2011, 2012 STRATO. All rights reserved. |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 6 | #include <linux/blkdev.h> |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 7 | #include <linux/ratelimit.h> |
David Sterba | de2491f | 2017-05-31 19:21:38 +0200 | [diff] [blame] | 8 | #include <linux/sched/mm.h> |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 9 | #include "ctree.h" |
| 10 | #include "volumes.h" |
| 11 | #include "disk-io.h" |
| 12 | #include "ordered-data.h" |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 13 | #include "transaction.h" |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 14 | #include "backref.h" |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 15 | #include "extent_io.h" |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 16 | #include "dev-replace.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 17 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 18 | #include "rcu-string.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 19 | #include "raid56.h" |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * This is only the first step towards a full-features scrub. It reads all |
| 23 | * extent and super block and verifies the checksums. In case a bad checksum |
| 24 | * is found or the extent cannot be read, good data will be written back if |
| 25 | * any can be found. |
| 26 | * |
| 27 | * Future enhancements: |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 28 | * - In case an unrepairable extent is encountered, track which files are |
| 29 | * affected and report them |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 30 | * - track and record media errors, throw out bad devices |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 31 | * - add a mode to also read unallocated space |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 32 | */ |
| 33 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 34 | struct scrub_block; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 35 | struct scrub_ctx; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 36 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 37 | /* |
| 38 | * the following three values only influence the performance. |
| 39 | * The last one configures the number of parallel and outstanding I/O |
| 40 | * operations. The first two values configure an upper limit for the number |
| 41 | * of (dynamically allocated) pages that are added to a bio. |
| 42 | */ |
| 43 | #define SCRUB_PAGES_PER_RD_BIO 32 /* 128k per bio */ |
| 44 | #define SCRUB_PAGES_PER_WR_BIO 32 /* 128k per bio */ |
| 45 | #define SCRUB_BIOS_PER_SCTX 64 /* 8MB per device in flight */ |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * the following value times PAGE_SIZE needs to be large enough to match the |
| 49 | * largest node/leaf/sector size that shall be supported. |
| 50 | * Values larger than BTRFS_STRIPE_LEN are not supported. |
| 51 | */ |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 52 | #define SCRUB_MAX_PAGES_PER_BLOCK 16 /* 64k per node/leaf/sector */ |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 53 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 54 | struct scrub_recover { |
Elena Reshetova | 6f61501 | 2017-03-03 10:55:21 +0200 | [diff] [blame] | 55 | refcount_t refs; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 56 | struct btrfs_bio *bbio; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 57 | u64 map_length; |
| 58 | }; |
| 59 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 60 | struct scrub_page { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 61 | struct scrub_block *sblock; |
| 62 | struct page *page; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 63 | struct btrfs_device *dev; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 64 | struct list_head list; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 65 | u64 flags; /* extent flags */ |
| 66 | u64 generation; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 67 | u64 logical; |
| 68 | u64 physical; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 69 | u64 physical_for_dev_replace; |
Zhao Lei | 5701934 | 2015-01-20 15:11:45 +0800 | [diff] [blame] | 70 | atomic_t refs; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 71 | struct { |
| 72 | unsigned int mirror_num:8; |
| 73 | unsigned int have_csum:1; |
| 74 | unsigned int io_error:1; |
| 75 | }; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 76 | u8 csum[BTRFS_CSUM_SIZE]; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 77 | |
| 78 | struct scrub_recover *recover; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | struct scrub_bio { |
| 82 | int index; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 83 | struct scrub_ctx *sctx; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 84 | struct btrfs_device *dev; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 85 | struct bio *bio; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 86 | blk_status_t status; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 87 | u64 logical; |
| 88 | u64 physical; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 89 | #if SCRUB_PAGES_PER_WR_BIO >= SCRUB_PAGES_PER_RD_BIO |
| 90 | struct scrub_page *pagev[SCRUB_PAGES_PER_WR_BIO]; |
| 91 | #else |
| 92 | struct scrub_page *pagev[SCRUB_PAGES_PER_RD_BIO]; |
| 93 | #endif |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 94 | int page_count; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 95 | int next_free; |
| 96 | struct btrfs_work work; |
| 97 | }; |
| 98 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 99 | struct scrub_block { |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 100 | struct scrub_page *pagev[SCRUB_MAX_PAGES_PER_BLOCK]; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 101 | int page_count; |
| 102 | atomic_t outstanding_pages; |
Elena Reshetova | 186debd | 2017-03-03 10:55:23 +0200 | [diff] [blame] | 103 | refcount_t refs; /* free mem on transition to zero */ |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 104 | struct scrub_ctx *sctx; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 105 | struct scrub_parity *sparity; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 106 | struct { |
| 107 | unsigned int header_error:1; |
| 108 | unsigned int checksum_error:1; |
| 109 | unsigned int no_io_error_seen:1; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 110 | unsigned int generation_error:1; /* also sets header_error */ |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 111 | |
| 112 | /* The following is for the data used to check parity */ |
| 113 | /* It is for the data with checksum */ |
| 114 | unsigned int data_corrected:1; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 115 | }; |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 116 | struct btrfs_work work; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 119 | /* Used for the chunks with parity stripe such RAID5/6 */ |
| 120 | struct scrub_parity { |
| 121 | struct scrub_ctx *sctx; |
| 122 | |
| 123 | struct btrfs_device *scrub_dev; |
| 124 | |
| 125 | u64 logic_start; |
| 126 | |
| 127 | u64 logic_end; |
| 128 | |
| 129 | int nsectors; |
| 130 | |
Liu Bo | 972d721 | 2017-04-03 13:45:33 -0700 | [diff] [blame] | 131 | u64 stripe_len; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 132 | |
Elena Reshetova | 78a7645 | 2017-03-03 10:55:24 +0200 | [diff] [blame] | 133 | refcount_t refs; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 134 | |
| 135 | struct list_head spages; |
| 136 | |
| 137 | /* Work of parity check and repair */ |
| 138 | struct btrfs_work work; |
| 139 | |
| 140 | /* Mark the parity blocks which have data */ |
| 141 | unsigned long *dbitmap; |
| 142 | |
| 143 | /* |
| 144 | * Mark the parity blocks which have data, but errors happen when |
| 145 | * read data or check data |
| 146 | */ |
| 147 | unsigned long *ebitmap; |
| 148 | |
| 149 | unsigned long bitmap[0]; |
| 150 | }; |
| 151 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 152 | struct scrub_ctx { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 153 | struct scrub_bio *bios[SCRUB_BIOS_PER_SCTX]; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 154 | struct btrfs_fs_info *fs_info; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 155 | int first_free; |
| 156 | int curr; |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 157 | atomic_t bios_in_flight; |
| 158 | atomic_t workers_pending; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 159 | spinlock_t list_lock; |
| 160 | wait_queue_head_t list_wait; |
| 161 | u16 csum_size; |
| 162 | struct list_head csum_list; |
| 163 | atomic_t cancel_req; |
Arne Jansen | 8628764 | 2011-03-23 16:34:19 +0100 | [diff] [blame] | 164 | int readonly; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 165 | int pages_per_rd_bio; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 166 | |
| 167 | int is_dev_replace; |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 168 | |
| 169 | struct scrub_bio *wr_curr_bio; |
| 170 | struct mutex wr_lock; |
| 171 | int pages_per_wr_bio; /* <= SCRUB_PAGES_PER_WR_BIO */ |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 172 | struct btrfs_device *wr_tgtdev; |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 173 | bool flush_all_writes; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 174 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 175 | /* |
| 176 | * statistics |
| 177 | */ |
| 178 | struct btrfs_scrub_progress stat; |
| 179 | spinlock_t stat_lock; |
Filipe Manana | f55985f | 2015-02-09 21:14:24 +0000 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * Use a ref counter to avoid use-after-free issues. Scrub workers |
| 183 | * decrement bios_in_flight and workers_pending and then do a wakeup |
| 184 | * on the list_wait wait queue. We must ensure the main scrub task |
| 185 | * doesn't free the scrub context before or while the workers are |
| 186 | * doing the wakeup() call. |
| 187 | */ |
Elena Reshetova | 99f4cdb | 2017-03-03 10:55:25 +0200 | [diff] [blame] | 188 | refcount_t refs; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 189 | }; |
| 190 | |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 191 | struct scrub_fixup_nodatasum { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 192 | struct scrub_ctx *sctx; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 193 | struct btrfs_device *dev; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 194 | u64 logical; |
| 195 | struct btrfs_root *root; |
| 196 | struct btrfs_work work; |
| 197 | int mirror_num; |
| 198 | }; |
| 199 | |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 200 | struct scrub_warning { |
| 201 | struct btrfs_path *path; |
| 202 | u64 extent_item_size; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 203 | const char *errstr; |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 204 | u64 physical; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 205 | u64 logical; |
| 206 | struct btrfs_device *dev; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
Qu Wenruo | 0966a7b | 2017-04-14 08:35:54 +0800 | [diff] [blame] | 209 | struct full_stripe_lock { |
| 210 | struct rb_node node; |
| 211 | u64 logical; |
| 212 | u64 refs; |
| 213 | struct mutex mutex; |
| 214 | }; |
| 215 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 216 | static void scrub_pending_bio_inc(struct scrub_ctx *sctx); |
| 217 | static void scrub_pending_bio_dec(struct scrub_ctx *sctx); |
| 218 | static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx); |
| 219 | static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 220 | static int scrub_handle_errored_block(struct scrub_block *sblock_to_check); |
Zhao Lei | be50a8d | 2015-01-20 15:11:42 +0800 | [diff] [blame] | 221 | static int scrub_setup_recheck_block(struct scrub_block *original_sblock, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 222 | struct scrub_block *sblocks_for_recheck); |
Stefan Behrens | 34f5c8e | 2012-11-02 16:16:26 +0100 | [diff] [blame] | 223 | static void scrub_recheck_block(struct btrfs_fs_info *fs_info, |
Zhao Lei | affe4a5 | 2015-08-24 21:32:06 +0800 | [diff] [blame] | 224 | struct scrub_block *sblock, |
| 225 | int retry_failed_mirror); |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 226 | static void scrub_recheck_block_checksum(struct scrub_block *sblock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 227 | static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad, |
Zhao Lei | 114ab50 | 2015-01-20 15:11:36 +0800 | [diff] [blame] | 228 | struct scrub_block *sblock_good); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 229 | static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad, |
| 230 | struct scrub_block *sblock_good, |
| 231 | int page_num, int force_write); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 232 | static void scrub_write_block_to_dev_replace(struct scrub_block *sblock); |
| 233 | static int scrub_write_page_to_dev_replace(struct scrub_block *sblock, |
| 234 | int page_num); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 235 | static int scrub_checksum_data(struct scrub_block *sblock); |
| 236 | static int scrub_checksum_tree_block(struct scrub_block *sblock); |
| 237 | static int scrub_checksum_super(struct scrub_block *sblock); |
| 238 | static void scrub_block_get(struct scrub_block *sblock); |
| 239 | static void scrub_block_put(struct scrub_block *sblock); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 240 | static void scrub_page_get(struct scrub_page *spage); |
| 241 | static void scrub_page_put(struct scrub_page *spage); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 242 | static void scrub_parity_get(struct scrub_parity *sparity); |
| 243 | static void scrub_parity_put(struct scrub_parity *sparity); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 244 | static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx, |
| 245 | struct scrub_page *spage); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 246 | static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len, |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 247 | u64 physical, struct btrfs_device *dev, u64 flags, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 248 | u64 gen, int mirror_num, u8 *csum, int force, |
| 249 | u64 physical_for_dev_replace); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 250 | static void scrub_bio_end_io(struct bio *bio); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 251 | static void scrub_bio_end_io_worker(struct btrfs_work *work); |
| 252 | static void scrub_block_complete(struct scrub_block *sblock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 253 | static void scrub_remap_extent(struct btrfs_fs_info *fs_info, |
| 254 | u64 extent_logical, u64 extent_len, |
| 255 | u64 *extent_physical, |
| 256 | struct btrfs_device **extent_dev, |
| 257 | int *extent_mirror_num); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 258 | static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx, |
| 259 | struct scrub_page *spage); |
| 260 | static void scrub_wr_submit(struct scrub_ctx *sctx); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 261 | static void scrub_wr_bio_end_io(struct bio *bio); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 262 | static void scrub_wr_bio_end_io_worker(struct btrfs_work *work); |
Wang Shilong | cb7ab02 | 2013-12-04 21:16:53 +0800 | [diff] [blame] | 263 | static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info); |
Wang Shilong | 3cb0929 | 2013-12-04 21:15:19 +0800 | [diff] [blame] | 264 | static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info); |
Filipe Manana | f55985f | 2015-02-09 21:14:24 +0000 | [diff] [blame] | 265 | static void scrub_put_ctx(struct scrub_ctx *sctx); |
Stefan Behrens | 1623ede | 2012-03-27 14:21:26 -0400 | [diff] [blame] | 266 | |
Liu Bo | 762221f | 2018-01-02 13:36:42 -0700 | [diff] [blame] | 267 | static inline int scrub_is_page_on_raid56(struct scrub_page *page) |
| 268 | { |
| 269 | return page->recover && |
| 270 | (page->recover->bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK); |
| 271 | } |
Stefan Behrens | 1623ede | 2012-03-27 14:21:26 -0400 | [diff] [blame] | 272 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 273 | static void scrub_pending_bio_inc(struct scrub_ctx *sctx) |
| 274 | { |
Elena Reshetova | 99f4cdb | 2017-03-03 10:55:25 +0200 | [diff] [blame] | 275 | refcount_inc(&sctx->refs); |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 276 | atomic_inc(&sctx->bios_in_flight); |
| 277 | } |
| 278 | |
| 279 | static void scrub_pending_bio_dec(struct scrub_ctx *sctx) |
| 280 | { |
| 281 | atomic_dec(&sctx->bios_in_flight); |
| 282 | wake_up(&sctx->list_wait); |
Filipe Manana | f55985f | 2015-02-09 21:14:24 +0000 | [diff] [blame] | 283 | scrub_put_ctx(sctx); |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Wang Shilong | cb7ab02 | 2013-12-04 21:16:53 +0800 | [diff] [blame] | 286 | static void __scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
Wang Shilong | 3cb0929 | 2013-12-04 21:15:19 +0800 | [diff] [blame] | 287 | { |
| 288 | while (atomic_read(&fs_info->scrub_pause_req)) { |
| 289 | mutex_unlock(&fs_info->scrub_lock); |
| 290 | wait_event(fs_info->scrub_pause_wait, |
| 291 | atomic_read(&fs_info->scrub_pause_req) == 0); |
| 292 | mutex_lock(&fs_info->scrub_lock); |
| 293 | } |
| 294 | } |
| 295 | |
Zhaolei | 0e22be8 | 2015-08-05 16:43:28 +0800 | [diff] [blame] | 296 | static void scrub_pause_on(struct btrfs_fs_info *fs_info) |
Wang Shilong | cb7ab02 | 2013-12-04 21:16:53 +0800 | [diff] [blame] | 297 | { |
| 298 | atomic_inc(&fs_info->scrubs_paused); |
| 299 | wake_up(&fs_info->scrub_pause_wait); |
Zhaolei | 0e22be8 | 2015-08-05 16:43:28 +0800 | [diff] [blame] | 300 | } |
Wang Shilong | cb7ab02 | 2013-12-04 21:16:53 +0800 | [diff] [blame] | 301 | |
Zhaolei | 0e22be8 | 2015-08-05 16:43:28 +0800 | [diff] [blame] | 302 | static void scrub_pause_off(struct btrfs_fs_info *fs_info) |
| 303 | { |
Wang Shilong | cb7ab02 | 2013-12-04 21:16:53 +0800 | [diff] [blame] | 304 | mutex_lock(&fs_info->scrub_lock); |
| 305 | __scrub_blocked_if_needed(fs_info); |
| 306 | atomic_dec(&fs_info->scrubs_paused); |
| 307 | mutex_unlock(&fs_info->scrub_lock); |
| 308 | |
| 309 | wake_up(&fs_info->scrub_pause_wait); |
| 310 | } |
| 311 | |
Zhaolei | 0e22be8 | 2015-08-05 16:43:28 +0800 | [diff] [blame] | 312 | static void scrub_blocked_if_needed(struct btrfs_fs_info *fs_info) |
| 313 | { |
| 314 | scrub_pause_on(fs_info); |
| 315 | scrub_pause_off(fs_info); |
| 316 | } |
| 317 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 318 | /* |
Qu Wenruo | 0966a7b | 2017-04-14 08:35:54 +0800 | [diff] [blame] | 319 | * Insert new full stripe lock into full stripe locks tree |
| 320 | * |
| 321 | * Return pointer to existing or newly inserted full_stripe_lock structure if |
| 322 | * everything works well. |
| 323 | * Return ERR_PTR(-ENOMEM) if we failed to allocate memory |
| 324 | * |
| 325 | * NOTE: caller must hold full_stripe_locks_root->lock before calling this |
| 326 | * function |
| 327 | */ |
| 328 | static struct full_stripe_lock *insert_full_stripe_lock( |
| 329 | struct btrfs_full_stripe_locks_tree *locks_root, |
| 330 | u64 fstripe_logical) |
| 331 | { |
| 332 | struct rb_node **p; |
| 333 | struct rb_node *parent = NULL; |
| 334 | struct full_stripe_lock *entry; |
| 335 | struct full_stripe_lock *ret; |
| 336 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 337 | lockdep_assert_held(&locks_root->lock); |
Qu Wenruo | 0966a7b | 2017-04-14 08:35:54 +0800 | [diff] [blame] | 338 | |
| 339 | p = &locks_root->root.rb_node; |
| 340 | while (*p) { |
| 341 | parent = *p; |
| 342 | entry = rb_entry(parent, struct full_stripe_lock, node); |
| 343 | if (fstripe_logical < entry->logical) { |
| 344 | p = &(*p)->rb_left; |
| 345 | } else if (fstripe_logical > entry->logical) { |
| 346 | p = &(*p)->rb_right; |
| 347 | } else { |
| 348 | entry->refs++; |
| 349 | return entry; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* Insert new lock */ |
| 354 | ret = kmalloc(sizeof(*ret), GFP_KERNEL); |
| 355 | if (!ret) |
| 356 | return ERR_PTR(-ENOMEM); |
| 357 | ret->logical = fstripe_logical; |
| 358 | ret->refs = 1; |
| 359 | mutex_init(&ret->mutex); |
| 360 | |
| 361 | rb_link_node(&ret->node, parent, p); |
| 362 | rb_insert_color(&ret->node, &locks_root->root); |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Search for a full stripe lock of a block group |
| 368 | * |
| 369 | * Return pointer to existing full stripe lock if found |
| 370 | * Return NULL if not found |
| 371 | */ |
| 372 | static struct full_stripe_lock *search_full_stripe_lock( |
| 373 | struct btrfs_full_stripe_locks_tree *locks_root, |
| 374 | u64 fstripe_logical) |
| 375 | { |
| 376 | struct rb_node *node; |
| 377 | struct full_stripe_lock *entry; |
| 378 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 379 | lockdep_assert_held(&locks_root->lock); |
Qu Wenruo | 0966a7b | 2017-04-14 08:35:54 +0800 | [diff] [blame] | 380 | |
| 381 | node = locks_root->root.rb_node; |
| 382 | while (node) { |
| 383 | entry = rb_entry(node, struct full_stripe_lock, node); |
| 384 | if (fstripe_logical < entry->logical) |
| 385 | node = node->rb_left; |
| 386 | else if (fstripe_logical > entry->logical) |
| 387 | node = node->rb_right; |
| 388 | else |
| 389 | return entry; |
| 390 | } |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Helper to get full stripe logical from a normal bytenr. |
| 396 | * |
| 397 | * Caller must ensure @cache is a RAID56 block group. |
| 398 | */ |
| 399 | static u64 get_full_stripe_logical(struct btrfs_block_group_cache *cache, |
| 400 | u64 bytenr) |
| 401 | { |
| 402 | u64 ret; |
| 403 | |
| 404 | /* |
| 405 | * Due to chunk item size limit, full stripe length should not be |
| 406 | * larger than U32_MAX. Just a sanity check here. |
| 407 | */ |
| 408 | WARN_ON_ONCE(cache->full_stripe_len >= U32_MAX); |
| 409 | |
| 410 | /* |
| 411 | * round_down() can only handle power of 2, while RAID56 full |
| 412 | * stripe length can be 64KiB * n, so we need to manually round down. |
| 413 | */ |
| 414 | ret = div64_u64(bytenr - cache->key.objectid, cache->full_stripe_len) * |
| 415 | cache->full_stripe_len + cache->key.objectid; |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Lock a full stripe to avoid concurrency of recovery and read |
| 421 | * |
| 422 | * It's only used for profiles with parities (RAID5/6), for other profiles it |
| 423 | * does nothing. |
| 424 | * |
| 425 | * Return 0 if we locked full stripe covering @bytenr, with a mutex held. |
| 426 | * So caller must call unlock_full_stripe() at the same context. |
| 427 | * |
| 428 | * Return <0 if encounters error. |
| 429 | */ |
| 430 | static int lock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr, |
| 431 | bool *locked_ret) |
| 432 | { |
| 433 | struct btrfs_block_group_cache *bg_cache; |
| 434 | struct btrfs_full_stripe_locks_tree *locks_root; |
| 435 | struct full_stripe_lock *existing; |
| 436 | u64 fstripe_start; |
| 437 | int ret = 0; |
| 438 | |
| 439 | *locked_ret = false; |
| 440 | bg_cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 441 | if (!bg_cache) { |
| 442 | ASSERT(0); |
| 443 | return -ENOENT; |
| 444 | } |
| 445 | |
| 446 | /* Profiles not based on parity don't need full stripe lock */ |
| 447 | if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
| 448 | goto out; |
| 449 | locks_root = &bg_cache->full_stripe_locks_root; |
| 450 | |
| 451 | fstripe_start = get_full_stripe_logical(bg_cache, bytenr); |
| 452 | |
| 453 | /* Now insert the full stripe lock */ |
| 454 | mutex_lock(&locks_root->lock); |
| 455 | existing = insert_full_stripe_lock(locks_root, fstripe_start); |
| 456 | mutex_unlock(&locks_root->lock); |
| 457 | if (IS_ERR(existing)) { |
| 458 | ret = PTR_ERR(existing); |
| 459 | goto out; |
| 460 | } |
| 461 | mutex_lock(&existing->mutex); |
| 462 | *locked_ret = true; |
| 463 | out: |
| 464 | btrfs_put_block_group(bg_cache); |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Unlock a full stripe. |
| 470 | * |
| 471 | * NOTE: Caller must ensure it's the same context calling corresponding |
| 472 | * lock_full_stripe(). |
| 473 | * |
| 474 | * Return 0 if we unlock full stripe without problem. |
| 475 | * Return <0 for error |
| 476 | */ |
| 477 | static int unlock_full_stripe(struct btrfs_fs_info *fs_info, u64 bytenr, |
| 478 | bool locked) |
| 479 | { |
| 480 | struct btrfs_block_group_cache *bg_cache; |
| 481 | struct btrfs_full_stripe_locks_tree *locks_root; |
| 482 | struct full_stripe_lock *fstripe_lock; |
| 483 | u64 fstripe_start; |
| 484 | bool freeit = false; |
| 485 | int ret = 0; |
| 486 | |
| 487 | /* If we didn't acquire full stripe lock, no need to continue */ |
| 488 | if (!locked) |
| 489 | return 0; |
| 490 | |
| 491 | bg_cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 492 | if (!bg_cache) { |
| 493 | ASSERT(0); |
| 494 | return -ENOENT; |
| 495 | } |
| 496 | if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
| 497 | goto out; |
| 498 | |
| 499 | locks_root = &bg_cache->full_stripe_locks_root; |
| 500 | fstripe_start = get_full_stripe_logical(bg_cache, bytenr); |
| 501 | |
| 502 | mutex_lock(&locks_root->lock); |
| 503 | fstripe_lock = search_full_stripe_lock(locks_root, fstripe_start); |
| 504 | /* Unpaired unlock_full_stripe() detected */ |
| 505 | if (!fstripe_lock) { |
| 506 | WARN_ON(1); |
| 507 | ret = -ENOENT; |
| 508 | mutex_unlock(&locks_root->lock); |
| 509 | goto out; |
| 510 | } |
| 511 | |
| 512 | if (fstripe_lock->refs == 0) { |
| 513 | WARN_ON(1); |
| 514 | btrfs_warn(fs_info, "full stripe lock at %llu refcount underflow", |
| 515 | fstripe_lock->logical); |
| 516 | } else { |
| 517 | fstripe_lock->refs--; |
| 518 | } |
| 519 | |
| 520 | if (fstripe_lock->refs == 0) { |
| 521 | rb_erase(&fstripe_lock->node, &locks_root->root); |
| 522 | freeit = true; |
| 523 | } |
| 524 | mutex_unlock(&locks_root->lock); |
| 525 | |
| 526 | mutex_unlock(&fstripe_lock->mutex); |
| 527 | if (freeit) |
| 528 | kfree(fstripe_lock); |
| 529 | out: |
| 530 | btrfs_put_block_group(bg_cache); |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | /* |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 535 | * used for workers that require transaction commits (i.e., for the |
| 536 | * NOCOW case) |
| 537 | */ |
| 538 | static void scrub_pending_trans_workers_inc(struct scrub_ctx *sctx) |
| 539 | { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 540 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 541 | |
Elena Reshetova | 99f4cdb | 2017-03-03 10:55:25 +0200 | [diff] [blame] | 542 | refcount_inc(&sctx->refs); |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 543 | /* |
| 544 | * increment scrubs_running to prevent cancel requests from |
| 545 | * completing as long as a worker is running. we must also |
| 546 | * increment scrubs_paused to prevent deadlocking on pause |
| 547 | * requests used for transactions commits (as the worker uses a |
| 548 | * transaction context). it is safe to regard the worker |
| 549 | * as paused for all matters practical. effectively, we only |
| 550 | * avoid cancellation requests from completing. |
| 551 | */ |
| 552 | mutex_lock(&fs_info->scrub_lock); |
| 553 | atomic_inc(&fs_info->scrubs_running); |
| 554 | atomic_inc(&fs_info->scrubs_paused); |
| 555 | mutex_unlock(&fs_info->scrub_lock); |
Wang Shilong | 32a4478 | 2014-02-19 19:24:19 +0800 | [diff] [blame] | 556 | |
| 557 | /* |
| 558 | * check if @scrubs_running=@scrubs_paused condition |
| 559 | * inside wait_event() is not an atomic operation. |
| 560 | * which means we may inc/dec @scrub_running/paused |
| 561 | * at any time. Let's wake up @scrub_pause_wait as |
| 562 | * much as we can to let commit transaction blocked less. |
| 563 | */ |
| 564 | wake_up(&fs_info->scrub_pause_wait); |
| 565 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 566 | atomic_inc(&sctx->workers_pending); |
| 567 | } |
| 568 | |
| 569 | /* used for workers that require transaction commits */ |
| 570 | static void scrub_pending_trans_workers_dec(struct scrub_ctx *sctx) |
| 571 | { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 572 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * see scrub_pending_trans_workers_inc() why we're pretending |
| 576 | * to be paused in the scrub counters |
| 577 | */ |
| 578 | mutex_lock(&fs_info->scrub_lock); |
| 579 | atomic_dec(&fs_info->scrubs_running); |
| 580 | atomic_dec(&fs_info->scrubs_paused); |
| 581 | mutex_unlock(&fs_info->scrub_lock); |
| 582 | atomic_dec(&sctx->workers_pending); |
| 583 | wake_up(&fs_info->scrub_pause_wait); |
| 584 | wake_up(&sctx->list_wait); |
Filipe Manana | f55985f | 2015-02-09 21:14:24 +0000 | [diff] [blame] | 585 | scrub_put_ctx(sctx); |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 588 | static void scrub_free_csums(struct scrub_ctx *sctx) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 589 | { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 590 | while (!list_empty(&sctx->csum_list)) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 591 | struct btrfs_ordered_sum *sum; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 592 | sum = list_first_entry(&sctx->csum_list, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 593 | struct btrfs_ordered_sum, list); |
| 594 | list_del(&sum->list); |
| 595 | kfree(sum); |
| 596 | } |
| 597 | } |
| 598 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 599 | static noinline_for_stack void scrub_free_ctx(struct scrub_ctx *sctx) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 600 | { |
| 601 | int i; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 602 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 603 | if (!sctx) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 604 | return; |
| 605 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 606 | /* this can happen when scrub is cancelled */ |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 607 | if (sctx->curr != -1) { |
| 608 | struct scrub_bio *sbio = sctx->bios[sctx->curr]; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 609 | |
| 610 | for (i = 0; i < sbio->page_count; i++) { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 611 | WARN_ON(!sbio->pagev[i]->page); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 612 | scrub_block_put(sbio->pagev[i]->sblock); |
| 613 | } |
| 614 | bio_put(sbio->bio); |
| 615 | } |
| 616 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 617 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 618 | struct scrub_bio *sbio = sctx->bios[i]; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 619 | |
| 620 | if (!sbio) |
| 621 | break; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 622 | kfree(sbio); |
| 623 | } |
| 624 | |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 625 | kfree(sctx->wr_curr_bio); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 626 | scrub_free_csums(sctx); |
| 627 | kfree(sctx); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Filipe Manana | f55985f | 2015-02-09 21:14:24 +0000 | [diff] [blame] | 630 | static void scrub_put_ctx(struct scrub_ctx *sctx) |
| 631 | { |
Elena Reshetova | 99f4cdb | 2017-03-03 10:55:25 +0200 | [diff] [blame] | 632 | if (refcount_dec_and_test(&sctx->refs)) |
Filipe Manana | f55985f | 2015-02-09 21:14:24 +0000 | [diff] [blame] | 633 | scrub_free_ctx(sctx); |
| 634 | } |
| 635 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 636 | static noinline_for_stack |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 637 | struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 638 | { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 639 | struct scrub_ctx *sctx; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 640 | int i; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 641 | struct btrfs_fs_info *fs_info = dev->fs_info; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 642 | |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 643 | sctx = kzalloc(sizeof(*sctx), GFP_KERNEL); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 644 | if (!sctx) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 645 | goto nomem; |
Elena Reshetova | 99f4cdb | 2017-03-03 10:55:25 +0200 | [diff] [blame] | 646 | refcount_set(&sctx->refs, 1); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 647 | sctx->is_dev_replace = is_dev_replace; |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 648 | sctx->pages_per_rd_bio = SCRUB_PAGES_PER_RD_BIO; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 649 | sctx->curr = -1; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 650 | sctx->fs_info = dev->fs_info; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 651 | for (i = 0; i < SCRUB_BIOS_PER_SCTX; ++i) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 652 | struct scrub_bio *sbio; |
| 653 | |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 654 | sbio = kzalloc(sizeof(*sbio), GFP_KERNEL); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 655 | if (!sbio) |
| 656 | goto nomem; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 657 | sctx->bios[i] = sbio; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 658 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 659 | sbio->index = i; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 660 | sbio->sctx = sctx; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 661 | sbio->page_count = 0; |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 662 | btrfs_init_work(&sbio->work, btrfs_scrub_helper, |
| 663 | scrub_bio_end_io_worker, NULL, NULL); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 664 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 665 | if (i != SCRUB_BIOS_PER_SCTX - 1) |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 666 | sctx->bios[i]->next_free = i + 1; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 667 | else |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 668 | sctx->bios[i]->next_free = -1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 669 | } |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 670 | sctx->first_free = 0; |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 671 | atomic_set(&sctx->bios_in_flight, 0); |
| 672 | atomic_set(&sctx->workers_pending, 0); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 673 | atomic_set(&sctx->cancel_req, 0); |
| 674 | sctx->csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 675 | INIT_LIST_HEAD(&sctx->csum_list); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 676 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 677 | spin_lock_init(&sctx->list_lock); |
| 678 | spin_lock_init(&sctx->stat_lock); |
| 679 | init_waitqueue_head(&sctx->list_wait); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 680 | |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 681 | WARN_ON(sctx->wr_curr_bio != NULL); |
| 682 | mutex_init(&sctx->wr_lock); |
| 683 | sctx->wr_curr_bio = NULL; |
David Sterba | 8fcdac3 | 2017-05-16 19:10:23 +0200 | [diff] [blame] | 684 | if (is_dev_replace) { |
David Sterba | ded5618 | 2017-06-26 15:19:00 +0200 | [diff] [blame] | 685 | WARN_ON(!fs_info->dev_replace.tgtdev); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 686 | sctx->pages_per_wr_bio = SCRUB_PAGES_PER_WR_BIO; |
David Sterba | ded5618 | 2017-06-26 15:19:00 +0200 | [diff] [blame] | 687 | sctx->wr_tgtdev = fs_info->dev_replace.tgtdev; |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 688 | sctx->flush_all_writes = false; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 689 | } |
David Sterba | 8fcdac3 | 2017-05-16 19:10:23 +0200 | [diff] [blame] | 690 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 691 | return sctx; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 692 | |
| 693 | nomem: |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 694 | scrub_free_ctx(sctx); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 695 | return ERR_PTR(-ENOMEM); |
| 696 | } |
| 697 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 698 | static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root, |
| 699 | void *warn_ctx) |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 700 | { |
| 701 | u64 isize; |
| 702 | u32 nlink; |
| 703 | int ret; |
| 704 | int i; |
David Sterba | de2491f | 2017-05-31 19:21:38 +0200 | [diff] [blame] | 705 | unsigned nofs_flag; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 706 | struct extent_buffer *eb; |
| 707 | struct btrfs_inode_item *inode_item; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 708 | struct scrub_warning *swarn = warn_ctx; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 709 | struct btrfs_fs_info *fs_info = swarn->dev->fs_info; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 710 | struct inode_fs_paths *ipath = NULL; |
| 711 | struct btrfs_root *local_root; |
| 712 | struct btrfs_key root_key; |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 713 | struct btrfs_key key; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 714 | |
| 715 | root_key.objectid = root; |
| 716 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 717 | root_key.offset = (u64)-1; |
| 718 | local_root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 719 | if (IS_ERR(local_root)) { |
| 720 | ret = PTR_ERR(local_root); |
| 721 | goto err; |
| 722 | } |
| 723 | |
David Sterba | 14692cc | 2015-01-02 18:55:46 +0100 | [diff] [blame] | 724 | /* |
| 725 | * this makes the path point to (inum INODE_ITEM ioff) |
| 726 | */ |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 727 | key.objectid = inum; |
| 728 | key.type = BTRFS_INODE_ITEM_KEY; |
| 729 | key.offset = 0; |
| 730 | |
| 731 | ret = btrfs_search_slot(NULL, local_root, &key, swarn->path, 0, 0); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 732 | if (ret) { |
| 733 | btrfs_release_path(swarn->path); |
| 734 | goto err; |
| 735 | } |
| 736 | |
| 737 | eb = swarn->path->nodes[0]; |
| 738 | inode_item = btrfs_item_ptr(eb, swarn->path->slots[0], |
| 739 | struct btrfs_inode_item); |
| 740 | isize = btrfs_inode_size(eb, inode_item); |
| 741 | nlink = btrfs_inode_nlink(eb, inode_item); |
| 742 | btrfs_release_path(swarn->path); |
| 743 | |
David Sterba | de2491f | 2017-05-31 19:21:38 +0200 | [diff] [blame] | 744 | /* |
| 745 | * init_path might indirectly call vmalloc, or use GFP_KERNEL. Scrub |
| 746 | * uses GFP_NOFS in this context, so we keep it consistent but it does |
| 747 | * not seem to be strictly necessary. |
| 748 | */ |
| 749 | nofs_flag = memalloc_nofs_save(); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 750 | ipath = init_ipath(4096, local_root, swarn->path); |
David Sterba | de2491f | 2017-05-31 19:21:38 +0200 | [diff] [blame] | 751 | memalloc_nofs_restore(nofs_flag); |
Dan Carpenter | 26bdef5 | 2011-11-16 11:28:01 +0300 | [diff] [blame] | 752 | if (IS_ERR(ipath)) { |
| 753 | ret = PTR_ERR(ipath); |
| 754 | ipath = NULL; |
| 755 | goto err; |
| 756 | } |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 757 | ret = paths_from_inode(inum, ipath); |
| 758 | |
| 759 | if (ret < 0) |
| 760 | goto err; |
| 761 | |
| 762 | /* |
| 763 | * we deliberately ignore the bit ipath might have been too small to |
| 764 | * hold all of the paths here |
| 765 | */ |
| 766 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 767 | btrfs_warn_in_rcu(fs_info, |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 768 | "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu, length %llu, links %u (path: %s)", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 769 | swarn->errstr, swarn->logical, |
| 770 | rcu_str_deref(swarn->dev->name), |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 771 | swarn->physical, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 772 | root, inum, offset, |
| 773 | min(isize - offset, (u64)PAGE_SIZE), nlink, |
| 774 | (char *)(unsigned long)ipath->fspath->val[i]); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 775 | |
| 776 | free_ipath(ipath); |
| 777 | return 0; |
| 778 | |
| 779 | err: |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 780 | btrfs_warn_in_rcu(fs_info, |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 781 | "%s at logical %llu on dev %s, physical %llu, root %llu, inode %llu, offset %llu: path resolving failed with ret=%d", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 782 | swarn->errstr, swarn->logical, |
| 783 | rcu_str_deref(swarn->dev->name), |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 784 | swarn->physical, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 785 | root, inum, offset, ret); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 786 | |
| 787 | free_ipath(ipath); |
| 788 | return 0; |
| 789 | } |
| 790 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 791 | static void scrub_print_warning(const char *errstr, struct scrub_block *sblock) |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 792 | { |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 793 | struct btrfs_device *dev; |
| 794 | struct btrfs_fs_info *fs_info; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 795 | struct btrfs_path *path; |
| 796 | struct btrfs_key found_key; |
| 797 | struct extent_buffer *eb; |
| 798 | struct btrfs_extent_item *ei; |
| 799 | struct scrub_warning swarn; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 800 | unsigned long ptr = 0; |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 801 | u64 extent_item_pos; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 802 | u64 flags = 0; |
| 803 | u64 ref_root; |
| 804 | u32 item_size; |
Dan Carpenter | 07c9a8e | 2016-03-11 11:08:56 +0300 | [diff] [blame] | 805 | u8 ref_level = 0; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 806 | int ret; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 807 | |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 808 | WARN_ON(sblock->page_count < 1); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 809 | dev = sblock->pagev[0]->dev; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 810 | fs_info = sblock->sctx->fs_info; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 811 | |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 812 | path = btrfs_alloc_path(); |
David Sterba | 8b9456d | 2014-07-30 01:25:30 +0200 | [diff] [blame] | 813 | if (!path) |
| 814 | return; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 815 | |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 816 | swarn.physical = sblock->pagev[0]->physical; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 817 | swarn.logical = sblock->pagev[0]->logical; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 818 | swarn.errstr = errstr; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 819 | swarn.dev = NULL; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 820 | |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 821 | ret = extent_from_logical(fs_info, swarn.logical, path, &found_key, |
| 822 | &flags); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 823 | if (ret < 0) |
| 824 | goto out; |
| 825 | |
Jan Schmidt | 4692cf5 | 2011-12-02 14:56:41 +0100 | [diff] [blame] | 826 | extent_item_pos = swarn.logical - found_key.objectid; |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 827 | swarn.extent_item_size = found_key.offset; |
| 828 | |
| 829 | eb = path->nodes[0]; |
| 830 | ei = btrfs_item_ptr(eb, path->slots[0], struct btrfs_extent_item); |
| 831 | item_size = btrfs_item_size_nr(eb, path->slots[0]); |
| 832 | |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 833 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 834 | do { |
Liu Bo | 6eda71d | 2014-06-09 10:54:07 +0800 | [diff] [blame] | 835 | ret = tree_backref_for_extent(&ptr, eb, &found_key, ei, |
| 836 | item_size, &ref_root, |
| 837 | &ref_level); |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 838 | btrfs_warn_in_rcu(fs_info, |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 839 | "%s at logical %llu on dev %s, physical %llu: metadata %s (level %d) in tree %llu", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 840 | errstr, swarn.logical, |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 841 | rcu_str_deref(dev->name), |
David Sterba | 6aa2126 | 2017-10-04 17:07:07 +0200 | [diff] [blame] | 842 | swarn.physical, |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 843 | ref_level ? "node" : "leaf", |
| 844 | ret < 0 ? -1 : ref_level, |
| 845 | ret < 0 ? -1 : ref_root); |
| 846 | } while (ret != 1); |
Josef Bacik | d8fe29e | 2013-03-29 08:09:34 -0600 | [diff] [blame] | 847 | btrfs_release_path(path); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 848 | } else { |
Josef Bacik | d8fe29e | 2013-03-29 08:09:34 -0600 | [diff] [blame] | 849 | btrfs_release_path(path); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 850 | swarn.path = path; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 851 | swarn.dev = dev; |
Jan Schmidt | 7a3ae2f | 2012-03-23 17:32:28 +0100 | [diff] [blame] | 852 | iterate_extent_inodes(fs_info, found_key.objectid, |
| 853 | extent_item_pos, 1, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 854 | scrub_print_warning_inode, &swarn, false); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | out: |
| 858 | btrfs_free_path(path); |
Jan Schmidt | 558540c | 2011-06-13 19:59:12 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 861 | static int scrub_fixup_readpage(u64 inum, u64 offset, u64 root, void *fixup_ctx) |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 862 | { |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 863 | struct page *page = NULL; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 864 | unsigned long index; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 865 | struct scrub_fixup_nodatasum *fixup = fixup_ctx; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 866 | int ret; |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 867 | int corrected = 0; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 868 | struct btrfs_key key; |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 869 | struct inode *inode = NULL; |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 870 | struct btrfs_fs_info *fs_info; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 871 | u64 end = offset + PAGE_SIZE - 1; |
| 872 | struct btrfs_root *local_root; |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 873 | int srcu_index; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 874 | |
| 875 | key.objectid = root; |
| 876 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 877 | key.offset = (u64)-1; |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 878 | |
| 879 | fs_info = fixup->root->fs_info; |
| 880 | srcu_index = srcu_read_lock(&fs_info->subvol_srcu); |
| 881 | |
| 882 | local_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 883 | if (IS_ERR(local_root)) { |
| 884 | srcu_read_unlock(&fs_info->subvol_srcu, srcu_index); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 885 | return PTR_ERR(local_root); |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 886 | } |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 887 | |
| 888 | key.type = BTRFS_INODE_ITEM_KEY; |
| 889 | key.objectid = inum; |
| 890 | key.offset = 0; |
Liu Bo | 6f1c360 | 2013-01-29 03:22:10 +0000 | [diff] [blame] | 891 | inode = btrfs_iget(fs_info->sb, &key, local_root, NULL); |
| 892 | srcu_read_unlock(&fs_info->subvol_srcu, srcu_index); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 893 | if (IS_ERR(inode)) |
| 894 | return PTR_ERR(inode); |
| 895 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 896 | index = offset >> PAGE_SHIFT; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 897 | |
| 898 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 899 | if (!page) { |
| 900 | ret = -ENOMEM; |
| 901 | goto out; |
| 902 | } |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 903 | |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 904 | if (PageUptodate(page)) { |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 905 | if (PageDirty(page)) { |
| 906 | /* |
| 907 | * we need to write the data to the defect sector. the |
| 908 | * data that was in that sector is not in memory, |
| 909 | * because the page was modified. we must not write the |
| 910 | * modified page to that sector. |
| 911 | * |
| 912 | * TODO: what could be done here: wait for the delalloc |
| 913 | * runner to write out that page (might involve |
| 914 | * COW) and see whether the sector is still |
| 915 | * referenced afterwards. |
| 916 | * |
| 917 | * For the meantime, we'll treat this error |
| 918 | * incorrectable, although there is a chance that a |
| 919 | * later scrub will find the bad sector again and that |
| 920 | * there's no dirty page in memory, then. |
| 921 | */ |
| 922 | ret = -EIO; |
| 923 | goto out; |
| 924 | } |
Josef Bacik | 6ec656b | 2017-05-05 11:57:14 -0400 | [diff] [blame] | 925 | ret = repair_io_failure(fs_info, inum, offset, PAGE_SIZE, |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 926 | fixup->logical, page, |
Miao Xie | ffdd201 | 2014-09-12 18:44:00 +0800 | [diff] [blame] | 927 | offset - page_offset(page), |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 928 | fixup->mirror_num); |
| 929 | unlock_page(page); |
| 930 | corrected = !ret; |
| 931 | } else { |
| 932 | /* |
| 933 | * we need to get good data first. the general readpage path |
| 934 | * will call repair_io_failure for us, we just have to make |
| 935 | * sure we read the bad mirror. |
| 936 | */ |
| 937 | ret = set_extent_bits(&BTRFS_I(inode)->io_tree, offset, end, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 938 | EXTENT_DAMAGED); |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 939 | if (ret) { |
| 940 | /* set_extent_bits should give proper error */ |
| 941 | WARN_ON(ret > 0); |
| 942 | if (ret > 0) |
| 943 | ret = -EFAULT; |
| 944 | goto out; |
| 945 | } |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 946 | |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 947 | ret = extent_read_full_page(&BTRFS_I(inode)->io_tree, page, |
| 948 | btrfs_get_extent, |
| 949 | fixup->mirror_num); |
| 950 | wait_on_page_locked(page); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 951 | |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 952 | corrected = !test_range_bit(&BTRFS_I(inode)->io_tree, offset, |
| 953 | end, EXTENT_DAMAGED, 0, NULL); |
| 954 | if (!corrected) |
| 955 | clear_extent_bits(&BTRFS_I(inode)->io_tree, offset, end, |
David Sterba | 9116621 | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 956 | EXTENT_DAMAGED); |
Jan Schmidt | 5da6fcb | 2011-08-04 18:11:04 +0200 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | out: |
| 960 | if (page) |
| 961 | put_page(page); |
Tobias Klauser | 7fb18a0 | 2014-04-25 14:58:05 +0200 | [diff] [blame] | 962 | |
| 963 | iput(inode); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 964 | |
| 965 | if (ret < 0) |
| 966 | return ret; |
| 967 | |
| 968 | if (ret == 0 && corrected) { |
| 969 | /* |
| 970 | * we only need to call readpage for one of the inodes belonging |
| 971 | * to this extent. so make iterate_extent_inodes stop |
| 972 | */ |
| 973 | return 1; |
| 974 | } |
| 975 | |
| 976 | return -EIO; |
| 977 | } |
| 978 | |
| 979 | static void scrub_fixup_nodatasum(struct btrfs_work *work) |
| 980 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 981 | struct btrfs_fs_info *fs_info; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 982 | int ret; |
| 983 | struct scrub_fixup_nodatasum *fixup; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 984 | struct scrub_ctx *sctx; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 985 | struct btrfs_trans_handle *trans = NULL; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 986 | struct btrfs_path *path; |
| 987 | int uncorrectable = 0; |
| 988 | |
| 989 | fixup = container_of(work, struct scrub_fixup_nodatasum, work); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 990 | sctx = fixup->sctx; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 991 | fs_info = fixup->root->fs_info; |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 992 | |
| 993 | path = btrfs_alloc_path(); |
| 994 | if (!path) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 995 | spin_lock(&sctx->stat_lock); |
| 996 | ++sctx->stat.malloc_errors; |
| 997 | spin_unlock(&sctx->stat_lock); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 998 | uncorrectable = 1; |
| 999 | goto out; |
| 1000 | } |
| 1001 | |
| 1002 | trans = btrfs_join_transaction(fixup->root); |
| 1003 | if (IS_ERR(trans)) { |
| 1004 | uncorrectable = 1; |
| 1005 | goto out; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * the idea is to trigger a regular read through the standard path. we |
| 1010 | * read a page from the (failed) logical address by specifying the |
| 1011 | * corresponding copynum of the failed sector. thus, that readpage is |
| 1012 | * expected to fail. |
| 1013 | * that is the point where on-the-fly error correction will kick in |
| 1014 | * (once it's finished) and rewrite the failed sector if a good copy |
| 1015 | * can be found. |
| 1016 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1017 | ret = iterate_inodes_from_logical(fixup->logical, fs_info, path, |
Zygo Blaxell | c995ab3 | 2017-09-22 13:58:45 -0400 | [diff] [blame] | 1018 | scrub_fixup_readpage, fixup, false); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 1019 | if (ret < 0) { |
| 1020 | uncorrectable = 1; |
| 1021 | goto out; |
| 1022 | } |
| 1023 | WARN_ON(ret != 1); |
| 1024 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1025 | spin_lock(&sctx->stat_lock); |
| 1026 | ++sctx->stat.corrected_errors; |
| 1027 | spin_unlock(&sctx->stat_lock); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 1028 | |
| 1029 | out: |
| 1030 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 1031 | btrfs_end_transaction(trans); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 1032 | if (uncorrectable) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1033 | spin_lock(&sctx->stat_lock); |
| 1034 | ++sctx->stat.uncorrectable_errors; |
| 1035 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1036 | btrfs_dev_replace_stats_inc( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1037 | &fs_info->dev_replace.num_uncorrectable_read_errors); |
| 1038 | btrfs_err_rl_in_rcu(fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 1039 | "unable to fixup (nodatasum) error at logical %llu on dev %s", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1040 | fixup->logical, rcu_str_deref(fixup->dev->name)); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | btrfs_free_path(path); |
| 1044 | kfree(fixup); |
| 1045 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 1046 | scrub_pending_trans_workers_dec(sctx); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1049 | static inline void scrub_get_recover(struct scrub_recover *recover) |
| 1050 | { |
Elena Reshetova | 6f61501 | 2017-03-03 10:55:21 +0200 | [diff] [blame] | 1051 | refcount_inc(&recover->refs); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1054 | static inline void scrub_put_recover(struct btrfs_fs_info *fs_info, |
| 1055 | struct scrub_recover *recover) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1056 | { |
Elena Reshetova | 6f61501 | 2017-03-03 10:55:21 +0200 | [diff] [blame] | 1057 | if (refcount_dec_and_test(&recover->refs)) { |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1058 | btrfs_bio_counter_dec(fs_info); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1059 | btrfs_put_bbio(recover->bbio); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1060 | kfree(recover); |
| 1061 | } |
| 1062 | } |
| 1063 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1064 | /* |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1065 | * scrub_handle_errored_block gets called when either verification of the |
| 1066 | * pages failed or the bio failed to read, e.g. with EIO. In the latter |
| 1067 | * case, this function handles all pages in the bio, even though only one |
| 1068 | * may be bad. |
| 1069 | * The goal of this function is to repair the errored block by using the |
| 1070 | * contents of one of the mirrors. |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1071 | */ |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1072 | static int scrub_handle_errored_block(struct scrub_block *sblock_to_check) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1073 | { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1074 | struct scrub_ctx *sctx = sblock_to_check->sctx; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1075 | struct btrfs_device *dev; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1076 | struct btrfs_fs_info *fs_info; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1077 | u64 logical; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1078 | unsigned int failed_mirror_index; |
| 1079 | unsigned int is_metadata; |
| 1080 | unsigned int have_csum; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1081 | struct scrub_block *sblocks_for_recheck; /* holds one for each mirror */ |
| 1082 | struct scrub_block *sblock_bad; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1083 | int ret; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1084 | int mirror_index; |
| 1085 | int page_num; |
| 1086 | int success; |
Qu Wenruo | 28d70e2 | 2017-04-14 08:35:55 +0800 | [diff] [blame] | 1087 | bool full_stripe_locked; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1088 | static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 1089 | DEFAULT_RATELIMIT_BURST); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1090 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1091 | BUG_ON(sblock_to_check->page_count < 1); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 1092 | fs_info = sctx->fs_info; |
Stefan Behrens | 4ded4f6 | 2012-11-14 18:57:29 +0000 | [diff] [blame] | 1093 | if (sblock_to_check->pagev[0]->flags & BTRFS_EXTENT_FLAG_SUPER) { |
| 1094 | /* |
| 1095 | * if we find an error in a super block, we just report it. |
| 1096 | * They will get written with the next transaction commit |
| 1097 | * anyway |
| 1098 | */ |
| 1099 | spin_lock(&sctx->stat_lock); |
| 1100 | ++sctx->stat.super_errors; |
| 1101 | spin_unlock(&sctx->stat_lock); |
| 1102 | return 0; |
| 1103 | } |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1104 | logical = sblock_to_check->pagev[0]->logical; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1105 | BUG_ON(sblock_to_check->pagev[0]->mirror_num < 1); |
| 1106 | failed_mirror_index = sblock_to_check->pagev[0]->mirror_num - 1; |
| 1107 | is_metadata = !(sblock_to_check->pagev[0]->flags & |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1108 | BTRFS_EXTENT_FLAG_DATA); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1109 | have_csum = sblock_to_check->pagev[0]->have_csum; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1110 | dev = sblock_to_check->pagev[0]->dev; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1111 | |
Qu Wenruo | 28d70e2 | 2017-04-14 08:35:55 +0800 | [diff] [blame] | 1112 | /* |
| 1113 | * For RAID5/6, race can happen for a different device scrub thread. |
| 1114 | * For data corruption, Parity and Data threads will both try |
| 1115 | * to recovery the data. |
| 1116 | * Race can lead to doubly added csum error, or even unrecoverable |
| 1117 | * error. |
| 1118 | */ |
| 1119 | ret = lock_full_stripe(fs_info, logical, &full_stripe_locked); |
| 1120 | if (ret < 0) { |
| 1121 | spin_lock(&sctx->stat_lock); |
| 1122 | if (ret == -ENOMEM) |
| 1123 | sctx->stat.malloc_errors++; |
| 1124 | sctx->stat.read_errors++; |
| 1125 | sctx->stat.uncorrectable_errors++; |
| 1126 | spin_unlock(&sctx->stat_lock); |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1130 | /* |
| 1131 | * read all mirrors one after the other. This includes to |
| 1132 | * re-read the extent or metadata block that failed (that was |
| 1133 | * the cause that this fixup code is called) another time, |
| 1134 | * page by page this time in order to know which pages |
| 1135 | * caused I/O errors and which ones are good (for all mirrors). |
| 1136 | * It is the goal to handle the situation when more than one |
| 1137 | * mirror contains I/O errors, but the errors do not |
| 1138 | * overlap, i.e. the data can be repaired by selecting the |
| 1139 | * pages from those mirrors without I/O error on the |
| 1140 | * particular pages. One example (with blocks >= 2 * PAGE_SIZE) |
| 1141 | * would be that mirror #1 has an I/O error on the first page, |
| 1142 | * the second page is good, and mirror #2 has an I/O error on |
| 1143 | * the second page, but the first page is good. |
| 1144 | * Then the first page of the first mirror can be repaired by |
| 1145 | * taking the first page of the second mirror, and the |
| 1146 | * second page of the second mirror can be repaired by |
| 1147 | * copying the contents of the 2nd page of the 1st mirror. |
| 1148 | * One more note: if the pages of one mirror contain I/O |
| 1149 | * errors, the checksum cannot be verified. In order to get |
| 1150 | * the best data for repairing, the first attempt is to find |
| 1151 | * a mirror without I/O errors and with a validated checksum. |
| 1152 | * Only if this is not possible, the pages are picked from |
| 1153 | * mirrors with I/O errors without considering the checksum. |
| 1154 | * If the latter is the case, at the end, the checksum of the |
| 1155 | * repaired area is verified in order to correctly maintain |
| 1156 | * the statistics. |
| 1157 | */ |
| 1158 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 1159 | sblocks_for_recheck = kcalloc(BTRFS_MAX_MIRRORS, |
| 1160 | sizeof(*sblocks_for_recheck), GFP_NOFS); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1161 | if (!sblocks_for_recheck) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1162 | spin_lock(&sctx->stat_lock); |
| 1163 | sctx->stat.malloc_errors++; |
| 1164 | sctx->stat.read_errors++; |
| 1165 | sctx->stat.uncorrectable_errors++; |
| 1166 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1167 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1168 | goto out; |
| 1169 | } |
| 1170 | |
| 1171 | /* setup the context, map the logical blocks and alloc the pages */ |
Zhao Lei | be50a8d | 2015-01-20 15:11:42 +0800 | [diff] [blame] | 1172 | ret = scrub_setup_recheck_block(sblock_to_check, sblocks_for_recheck); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1173 | if (ret) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1174 | spin_lock(&sctx->stat_lock); |
| 1175 | sctx->stat.read_errors++; |
| 1176 | sctx->stat.uncorrectable_errors++; |
| 1177 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1178 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1179 | goto out; |
| 1180 | } |
| 1181 | BUG_ON(failed_mirror_index >= BTRFS_MAX_MIRRORS); |
| 1182 | sblock_bad = sblocks_for_recheck + failed_mirror_index; |
| 1183 | |
| 1184 | /* build and submit the bios for the failed mirror, check checksums */ |
Zhao Lei | affe4a5 | 2015-08-24 21:32:06 +0800 | [diff] [blame] | 1185 | scrub_recheck_block(fs_info, sblock_bad, 1); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1186 | |
| 1187 | if (!sblock_bad->header_error && !sblock_bad->checksum_error && |
| 1188 | sblock_bad->no_io_error_seen) { |
| 1189 | /* |
| 1190 | * the error disappeared after reading page by page, or |
| 1191 | * the area was part of a huge bio and other parts of the |
| 1192 | * bio caused I/O errors, or the block layer merged several |
| 1193 | * read requests into one and the error is caused by a |
| 1194 | * different bio (usually one of the two latter cases is |
| 1195 | * the cause) |
| 1196 | */ |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1197 | spin_lock(&sctx->stat_lock); |
| 1198 | sctx->stat.unverified_errors++; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 1199 | sblock_to_check->data_corrected = 1; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1200 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1201 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1202 | if (sctx->is_dev_replace) |
| 1203 | scrub_write_block_to_dev_replace(sblock_bad); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1204 | goto out; |
| 1205 | } |
| 1206 | |
| 1207 | if (!sblock_bad->no_io_error_seen) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1208 | spin_lock(&sctx->stat_lock); |
| 1209 | sctx->stat.read_errors++; |
| 1210 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1211 | if (__ratelimit(&_rs)) |
| 1212 | scrub_print_warning("i/o error", sblock_to_check); |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1213 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1214 | } else if (sblock_bad->checksum_error) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1215 | spin_lock(&sctx->stat_lock); |
| 1216 | sctx->stat.csum_errors++; |
| 1217 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1218 | if (__ratelimit(&_rs)) |
| 1219 | scrub_print_warning("checksum error", sblock_to_check); |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1220 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1221 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1222 | } else if (sblock_bad->header_error) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1223 | spin_lock(&sctx->stat_lock); |
| 1224 | sctx->stat.verify_errors++; |
| 1225 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1226 | if (__ratelimit(&_rs)) |
| 1227 | scrub_print_warning("checksum/header error", |
| 1228 | sblock_to_check); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1229 | if (sblock_bad->generation_error) |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1230 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1231 | BTRFS_DEV_STAT_GENERATION_ERRS); |
| 1232 | else |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1233 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1234 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1235 | } |
| 1236 | |
Ilya Dryomov | 33ef30a | 2013-11-03 19:06:38 +0200 | [diff] [blame] | 1237 | if (sctx->readonly) { |
| 1238 | ASSERT(!sctx->is_dev_replace); |
| 1239 | goto out; |
| 1240 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1241 | |
Qu Wenruo | 665d495 | 2018-07-11 13:41:21 +0800 | [diff] [blame] | 1242 | /* |
| 1243 | * NOTE: Even for nodatasum case, it's still possible that it's a |
| 1244 | * compressed data extent, thus scrub_fixup_nodatasum(), which write |
| 1245 | * inode page cache onto disk, could cause serious data corruption. |
| 1246 | * |
| 1247 | * So here we could only read from disk, and hope our recovery could |
| 1248 | * reach disk before the newer write. |
| 1249 | */ |
| 1250 | if (0 && !is_metadata && !have_csum) { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1251 | struct scrub_fixup_nodatasum *fixup_nodatasum; |
| 1252 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1253 | WARN_ON(sctx->is_dev_replace); |
| 1254 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1255 | /* |
| 1256 | * !is_metadata and !have_csum, this means that the data |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1257 | * might not be COWed, that it might be modified |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1258 | * concurrently. The general strategy to work on the |
| 1259 | * commit root does not help in the case when COW is not |
| 1260 | * used. |
| 1261 | */ |
| 1262 | fixup_nodatasum = kzalloc(sizeof(*fixup_nodatasum), GFP_NOFS); |
| 1263 | if (!fixup_nodatasum) |
| 1264 | goto did_not_correct_error; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1265 | fixup_nodatasum->sctx = sctx; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 1266 | fixup_nodatasum->dev = dev; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1267 | fixup_nodatasum->logical = logical; |
| 1268 | fixup_nodatasum->root = fs_info->extent_root; |
| 1269 | fixup_nodatasum->mirror_num = failed_mirror_index + 1; |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 1270 | scrub_pending_trans_workers_inc(sctx); |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1271 | btrfs_init_work(&fixup_nodatasum->work, btrfs_scrub_helper, |
| 1272 | scrub_fixup_nodatasum, NULL, NULL); |
Qu Wenruo | 0339ef2 | 2014-02-28 10:46:17 +0800 | [diff] [blame] | 1273 | btrfs_queue_work(fs_info->scrub_workers, |
| 1274 | &fixup_nodatasum->work); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1275 | goto out; |
| 1276 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1277 | |
| 1278 | /* |
| 1279 | * now build and submit the bios for the other mirrors, check |
Stefan Behrens | cb2ced7 | 2012-11-02 16:14:21 +0100 | [diff] [blame] | 1280 | * checksums. |
| 1281 | * First try to pick the mirror which is completely without I/O |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1282 | * errors and also does not have a checksum error. |
| 1283 | * If one is found, and if a checksum is present, the full block |
| 1284 | * that is known to contain an error is rewritten. Afterwards |
| 1285 | * the block is known to be corrected. |
| 1286 | * If a mirror is found which is completely correct, and no |
| 1287 | * checksum is present, only those pages are rewritten that had |
| 1288 | * an I/O error in the block to be repaired, since it cannot be |
| 1289 | * determined, which copy of the other pages is better (and it |
| 1290 | * could happen otherwise that a correct page would be |
| 1291 | * overwritten by a bad one). |
| 1292 | */ |
Liu Bo | 762221f | 2018-01-02 13:36:42 -0700 | [diff] [blame] | 1293 | for (mirror_index = 0; ;mirror_index++) { |
Stefan Behrens | cb2ced7 | 2012-11-02 16:14:21 +0100 | [diff] [blame] | 1294 | struct scrub_block *sblock_other; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1295 | |
Stefan Behrens | cb2ced7 | 2012-11-02 16:14:21 +0100 | [diff] [blame] | 1296 | if (mirror_index == failed_mirror_index) |
| 1297 | continue; |
Liu Bo | 762221f | 2018-01-02 13:36:42 -0700 | [diff] [blame] | 1298 | |
| 1299 | /* raid56's mirror can be more than BTRFS_MAX_MIRRORS */ |
| 1300 | if (!scrub_is_page_on_raid56(sblock_bad->pagev[0])) { |
| 1301 | if (mirror_index >= BTRFS_MAX_MIRRORS) |
| 1302 | break; |
| 1303 | if (!sblocks_for_recheck[mirror_index].page_count) |
| 1304 | break; |
| 1305 | |
| 1306 | sblock_other = sblocks_for_recheck + mirror_index; |
| 1307 | } else { |
| 1308 | struct scrub_recover *r = sblock_bad->pagev[0]->recover; |
| 1309 | int max_allowed = r->bbio->num_stripes - |
| 1310 | r->bbio->num_tgtdevs; |
| 1311 | |
| 1312 | if (mirror_index >= max_allowed) |
| 1313 | break; |
| 1314 | if (!sblocks_for_recheck[1].page_count) |
| 1315 | break; |
| 1316 | |
| 1317 | ASSERT(failed_mirror_index == 0); |
| 1318 | sblock_other = sblocks_for_recheck + 1; |
| 1319 | sblock_other->pagev[0]->mirror_num = 1 + mirror_index; |
| 1320 | } |
Stefan Behrens | cb2ced7 | 2012-11-02 16:14:21 +0100 | [diff] [blame] | 1321 | |
| 1322 | /* build and submit the bios, check checksums */ |
Zhao Lei | affe4a5 | 2015-08-24 21:32:06 +0800 | [diff] [blame] | 1323 | scrub_recheck_block(fs_info, sblock_other, 0); |
Stefan Behrens | 34f5c8e | 2012-11-02 16:16:26 +0100 | [diff] [blame] | 1324 | |
| 1325 | if (!sblock_other->header_error && |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1326 | !sblock_other->checksum_error && |
| 1327 | sblock_other->no_io_error_seen) { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1328 | if (sctx->is_dev_replace) { |
| 1329 | scrub_write_block_to_dev_replace(sblock_other); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1330 | goto corrected_error; |
Zhao Lei | 114ab50 | 2015-01-20 15:11:36 +0800 | [diff] [blame] | 1331 | } else { |
| 1332 | ret = scrub_repair_block_from_good_copy( |
| 1333 | sblock_bad, sblock_other); |
| 1334 | if (!ret) |
| 1335 | goto corrected_error; |
| 1336 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1337 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1338 | } |
| 1339 | |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1340 | if (sblock_bad->no_io_error_seen && !sctx->is_dev_replace) |
| 1341 | goto did_not_correct_error; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1342 | |
| 1343 | /* |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1344 | * In case of I/O errors in the area that is supposed to be |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1345 | * repaired, continue by picking good copies of those pages. |
| 1346 | * Select the good pages from mirrors to rewrite bad pages from |
| 1347 | * the area to fix. Afterwards verify the checksum of the block |
| 1348 | * that is supposed to be repaired. This verification step is |
| 1349 | * only done for the purpose of statistic counting and for the |
| 1350 | * final scrub report, whether errors remain. |
| 1351 | * A perfect algorithm could make use of the checksum and try |
| 1352 | * all possible combinations of pages from the different mirrors |
| 1353 | * until the checksum verification succeeds. For example, when |
| 1354 | * the 2nd page of mirror #1 faces I/O errors, and the 2nd page |
| 1355 | * of mirror #2 is readable but the final checksum test fails, |
| 1356 | * then the 2nd page of mirror #3 could be tried, whether now |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1357 | * the final checksum succeeds. But this would be a rare |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1358 | * exception and is therefore not implemented. At least it is |
| 1359 | * avoided that the good copy is overwritten. |
| 1360 | * A more useful improvement would be to pick the sectors |
| 1361 | * without I/O error based on sector sizes (512 bytes on legacy |
| 1362 | * disks) instead of on PAGE_SIZE. Then maybe 512 byte of one |
| 1363 | * mirror could be repaired by taking 512 byte of a different |
| 1364 | * mirror, even if other 512 byte sectors in the same PAGE_SIZE |
| 1365 | * area are unreadable. |
| 1366 | */ |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1367 | success = 1; |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1368 | for (page_num = 0; page_num < sblock_bad->page_count; |
| 1369 | page_num++) { |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1370 | struct scrub_page *page_bad = sblock_bad->pagev[page_num]; |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1371 | struct scrub_block *sblock_other = NULL; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1372 | |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1373 | /* skip no-io-error page in scrub */ |
| 1374 | if (!page_bad->io_error && !sctx->is_dev_replace) |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1375 | continue; |
| 1376 | |
Liu Bo | 4759700 | 2018-03-02 16:10:41 -0700 | [diff] [blame] | 1377 | if (scrub_is_page_on_raid56(sblock_bad->pagev[0])) { |
| 1378 | /* |
| 1379 | * In case of dev replace, if raid56 rebuild process |
| 1380 | * didn't work out correct data, then copy the content |
| 1381 | * in sblock_bad to make sure target device is identical |
| 1382 | * to source device, instead of writing garbage data in |
| 1383 | * sblock_for_recheck array to target device. |
| 1384 | */ |
| 1385 | sblock_other = NULL; |
| 1386 | } else if (page_bad->io_error) { |
| 1387 | /* try to find no-io-error page in mirrors */ |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1388 | for (mirror_index = 0; |
| 1389 | mirror_index < BTRFS_MAX_MIRRORS && |
| 1390 | sblocks_for_recheck[mirror_index].page_count > 0; |
| 1391 | mirror_index++) { |
| 1392 | if (!sblocks_for_recheck[mirror_index]. |
| 1393 | pagev[page_num]->io_error) { |
| 1394 | sblock_other = sblocks_for_recheck + |
| 1395 | mirror_index; |
| 1396 | break; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1397 | } |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 1398 | } |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1399 | if (!sblock_other) |
| 1400 | success = 0; |
Jan Schmidt | 13db62b | 2011-06-13 19:56:13 +0200 | [diff] [blame] | 1401 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1402 | |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1403 | if (sctx->is_dev_replace) { |
| 1404 | /* |
| 1405 | * did not find a mirror to fetch the page |
| 1406 | * from. scrub_write_page_to_dev_replace() |
| 1407 | * handles this case (page->io_error), by |
| 1408 | * filling the block with zeros before |
| 1409 | * submitting the write request |
| 1410 | */ |
| 1411 | if (!sblock_other) |
| 1412 | sblock_other = sblock_bad; |
| 1413 | |
| 1414 | if (scrub_write_page_to_dev_replace(sblock_other, |
| 1415 | page_num) != 0) { |
| 1416 | btrfs_dev_replace_stats_inc( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1417 | &fs_info->dev_replace.num_write_errors); |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1418 | success = 0; |
| 1419 | } |
| 1420 | } else if (sblock_other) { |
| 1421 | ret = scrub_repair_page_from_good_copy(sblock_bad, |
| 1422 | sblock_other, |
| 1423 | page_num, 0); |
| 1424 | if (0 == ret) |
| 1425 | page_bad->io_error = 0; |
| 1426 | else |
| 1427 | success = 0; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Zhao Lei | b968fed | 2015-01-20 15:11:41 +0800 | [diff] [blame] | 1431 | if (success && !sctx->is_dev_replace) { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1432 | if (is_metadata || have_csum) { |
| 1433 | /* |
| 1434 | * need to verify the checksum now that all |
| 1435 | * sectors on disk are repaired (the write |
| 1436 | * request for data to be repaired is on its way). |
| 1437 | * Just be lazy and use scrub_recheck_block() |
| 1438 | * which re-reads the data before the checksum |
| 1439 | * is verified, but most likely the data comes out |
| 1440 | * of the page cache. |
| 1441 | */ |
Zhao Lei | affe4a5 | 2015-08-24 21:32:06 +0800 | [diff] [blame] | 1442 | scrub_recheck_block(fs_info, sblock_bad, 1); |
Stefan Behrens | 34f5c8e | 2012-11-02 16:16:26 +0100 | [diff] [blame] | 1443 | if (!sblock_bad->header_error && |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1444 | !sblock_bad->checksum_error && |
| 1445 | sblock_bad->no_io_error_seen) |
| 1446 | goto corrected_error; |
| 1447 | else |
| 1448 | goto did_not_correct_error; |
| 1449 | } else { |
| 1450 | corrected_error: |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1451 | spin_lock(&sctx->stat_lock); |
| 1452 | sctx->stat.corrected_errors++; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 1453 | sblock_to_check->data_corrected = 1; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1454 | spin_unlock(&sctx->stat_lock); |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 1455 | btrfs_err_rl_in_rcu(fs_info, |
| 1456 | "fixed up error at logical %llu on dev %s", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1457 | logical, rcu_str_deref(dev->name)); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1458 | } |
| 1459 | } else { |
| 1460 | did_not_correct_error: |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1461 | spin_lock(&sctx->stat_lock); |
| 1462 | sctx->stat.uncorrectable_errors++; |
| 1463 | spin_unlock(&sctx->stat_lock); |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 1464 | btrfs_err_rl_in_rcu(fs_info, |
| 1465 | "unable to fixup (regular) error at logical %llu on dev %s", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1466 | logical, rcu_str_deref(dev->name)); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | out: |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1470 | if (sblocks_for_recheck) { |
| 1471 | for (mirror_index = 0; mirror_index < BTRFS_MAX_MIRRORS; |
| 1472 | mirror_index++) { |
| 1473 | struct scrub_block *sblock = sblocks_for_recheck + |
| 1474 | mirror_index; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1475 | struct scrub_recover *recover; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1476 | int page_index; |
| 1477 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1478 | for (page_index = 0; page_index < sblock->page_count; |
| 1479 | page_index++) { |
| 1480 | sblock->pagev[page_index]->sblock = NULL; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1481 | recover = sblock->pagev[page_index]->recover; |
| 1482 | if (recover) { |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1483 | scrub_put_recover(fs_info, recover); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1484 | sblock->pagev[page_index]->recover = |
| 1485 | NULL; |
| 1486 | } |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1487 | scrub_page_put(sblock->pagev[page_index]); |
| 1488 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1489 | } |
| 1490 | kfree(sblocks_for_recheck); |
| 1491 | } |
| 1492 | |
Qu Wenruo | 28d70e2 | 2017-04-14 08:35:55 +0800 | [diff] [blame] | 1493 | ret = unlock_full_stripe(fs_info, logical, full_stripe_locked); |
| 1494 | if (ret < 0) |
| 1495 | return ret; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1496 | return 0; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 1499 | static inline int scrub_nr_raid_mirrors(struct btrfs_bio *bbio) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1500 | { |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 1501 | if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID5) |
| 1502 | return 2; |
| 1503 | else if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID6) |
| 1504 | return 3; |
| 1505 | else |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1506 | return (int)bbio->num_stripes; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 1509 | static inline void scrub_stripe_index_and_offset(u64 logical, u64 map_type, |
| 1510 | u64 *raid_map, |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1511 | u64 mapped_length, |
| 1512 | int nstripes, int mirror, |
| 1513 | int *stripe_index, |
| 1514 | u64 *stripe_offset) |
| 1515 | { |
| 1516 | int i; |
| 1517 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 1518 | if (map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1519 | /* RAID5/6 */ |
| 1520 | for (i = 0; i < nstripes; i++) { |
| 1521 | if (raid_map[i] == RAID6_Q_STRIPE || |
| 1522 | raid_map[i] == RAID5_P_STRIPE) |
| 1523 | continue; |
| 1524 | |
| 1525 | if (logical >= raid_map[i] && |
| 1526 | logical < raid_map[i] + mapped_length) |
| 1527 | break; |
| 1528 | } |
| 1529 | |
| 1530 | *stripe_index = i; |
| 1531 | *stripe_offset = logical - raid_map[i]; |
| 1532 | } else { |
| 1533 | /* The other RAID type */ |
| 1534 | *stripe_index = mirror; |
| 1535 | *stripe_offset = 0; |
| 1536 | } |
| 1537 | } |
| 1538 | |
Zhao Lei | be50a8d | 2015-01-20 15:11:42 +0800 | [diff] [blame] | 1539 | static int scrub_setup_recheck_block(struct scrub_block *original_sblock, |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1540 | struct scrub_block *sblocks_for_recheck) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 1541 | { |
Zhao Lei | be50a8d | 2015-01-20 15:11:42 +0800 | [diff] [blame] | 1542 | struct scrub_ctx *sctx = original_sblock->sctx; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 1543 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Zhao Lei | be50a8d | 2015-01-20 15:11:42 +0800 | [diff] [blame] | 1544 | u64 length = original_sblock->page_count * PAGE_SIZE; |
| 1545 | u64 logical = original_sblock->pagev[0]->logical; |
Zhao Lei | 4734b7e | 2015-08-19 22:39:18 +0800 | [diff] [blame] | 1546 | u64 generation = original_sblock->pagev[0]->generation; |
| 1547 | u64 flags = original_sblock->pagev[0]->flags; |
| 1548 | u64 have_csum = original_sblock->pagev[0]->have_csum; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1549 | struct scrub_recover *recover; |
| 1550 | struct btrfs_bio *bbio; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1551 | u64 sublen; |
| 1552 | u64 mapped_length; |
| 1553 | u64 stripe_offset; |
| 1554 | int stripe_index; |
Zhao Lei | be50a8d | 2015-01-20 15:11:42 +0800 | [diff] [blame] | 1555 | int page_index = 0; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1556 | int mirror_index; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1557 | int nmirrors; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1558 | int ret; |
| 1559 | |
| 1560 | /* |
Zhao Lei | 5701934 | 2015-01-20 15:11:45 +0800 | [diff] [blame] | 1561 | * note: the two members refs and outstanding_pages |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1562 | * are not used (and not set) in the blocks that are used for |
| 1563 | * the recheck procedure |
| 1564 | */ |
| 1565 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1566 | while (length > 0) { |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1567 | sublen = min_t(u64, length, PAGE_SIZE); |
| 1568 | mapped_length = sublen; |
| 1569 | bbio = NULL; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1570 | |
| 1571 | /* |
| 1572 | * with a length of PAGE_SIZE, each returned stripe |
| 1573 | * represents one mirror |
| 1574 | */ |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1575 | btrfs_bio_counter_inc_blocked(fs_info); |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 1576 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 1577 | logical, &mapped_length, &bbio); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1578 | if (ret || !bbio || mapped_length < sublen) { |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1579 | btrfs_put_bbio(bbio); |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1580 | btrfs_bio_counter_dec(fs_info); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1581 | return -EIO; |
| 1582 | } |
| 1583 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1584 | recover = kzalloc(sizeof(struct scrub_recover), GFP_NOFS); |
| 1585 | if (!recover) { |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 1586 | btrfs_put_bbio(bbio); |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1587 | btrfs_bio_counter_dec(fs_info); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1588 | return -ENOMEM; |
| 1589 | } |
| 1590 | |
Elena Reshetova | 6f61501 | 2017-03-03 10:55:21 +0200 | [diff] [blame] | 1591 | refcount_set(&recover->refs, 1); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1592 | recover->bbio = bbio; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1593 | recover->map_length = mapped_length; |
| 1594 | |
Ashish Samant | 2473114 | 2016-04-29 18:33:59 -0700 | [diff] [blame] | 1595 | BUG_ON(page_index >= SCRUB_MAX_PAGES_PER_BLOCK); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1596 | |
Zhao Lei | be50a8d | 2015-01-20 15:11:42 +0800 | [diff] [blame] | 1597 | nmirrors = min(scrub_nr_raid_mirrors(bbio), BTRFS_MAX_MIRRORS); |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 1598 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1599 | for (mirror_index = 0; mirror_index < nmirrors; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1600 | mirror_index++) { |
| 1601 | struct scrub_block *sblock; |
| 1602 | struct scrub_page *page; |
| 1603 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1604 | sblock = sblocks_for_recheck + mirror_index; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1605 | sblock->sctx = sctx; |
Zhao Lei | 4734b7e | 2015-08-19 22:39:18 +0800 | [diff] [blame] | 1606 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1607 | page = kzalloc(sizeof(*page), GFP_NOFS); |
| 1608 | if (!page) { |
| 1609 | leave_nomem: |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 1610 | spin_lock(&sctx->stat_lock); |
| 1611 | sctx->stat.malloc_errors++; |
| 1612 | spin_unlock(&sctx->stat_lock); |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1613 | scrub_put_recover(fs_info, recover); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1614 | return -ENOMEM; |
| 1615 | } |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1616 | scrub_page_get(page); |
| 1617 | sblock->pagev[page_index] = page; |
Zhao Lei | 4734b7e | 2015-08-19 22:39:18 +0800 | [diff] [blame] | 1618 | page->sblock = sblock; |
| 1619 | page->flags = flags; |
| 1620 | page->generation = generation; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1621 | page->logical = logical; |
Zhao Lei | 4734b7e | 2015-08-19 22:39:18 +0800 | [diff] [blame] | 1622 | page->have_csum = have_csum; |
| 1623 | if (have_csum) |
| 1624 | memcpy(page->csum, |
| 1625 | original_sblock->pagev[0]->csum, |
| 1626 | sctx->csum_size); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1627 | |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 1628 | scrub_stripe_index_and_offset(logical, |
| 1629 | bbio->map_type, |
| 1630 | bbio->raid_map, |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1631 | mapped_length, |
Zhao Lei | e34c330 | 2015-01-20 15:11:31 +0800 | [diff] [blame] | 1632 | bbio->num_stripes - |
| 1633 | bbio->num_tgtdevs, |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1634 | mirror_index, |
| 1635 | &stripe_index, |
| 1636 | &stripe_offset); |
| 1637 | page->physical = bbio->stripes[stripe_index].physical + |
| 1638 | stripe_offset; |
| 1639 | page->dev = bbio->stripes[stripe_index].dev; |
| 1640 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1641 | BUG_ON(page_index >= original_sblock->page_count); |
| 1642 | page->physical_for_dev_replace = |
| 1643 | original_sblock->pagev[page_index]-> |
| 1644 | physical_for_dev_replace; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1645 | /* for missing devices, dev->bdev is NULL */ |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1646 | page->mirror_num = mirror_index + 1; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1647 | sblock->page_count++; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1648 | page->page = alloc_page(GFP_NOFS); |
| 1649 | if (!page->page) |
| 1650 | goto leave_nomem; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1651 | |
| 1652 | scrub_get_recover(recover); |
| 1653 | page->recover = recover; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1654 | } |
Qu Wenruo | e501bfe | 2017-03-29 09:33:22 +0800 | [diff] [blame] | 1655 | scrub_put_recover(fs_info, recover); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1656 | length -= sublen; |
| 1657 | logical += sublen; |
| 1658 | page_index++; |
| 1659 | } |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1664 | static void scrub_bio_wait_endio(struct bio *bio) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1665 | { |
Liu Bo | b4ff5ad | 2017-11-30 17:26:39 -0700 | [diff] [blame] | 1666 | complete(bio->bi_private); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1667 | } |
| 1668 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1669 | static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info, |
| 1670 | struct bio *bio, |
| 1671 | struct scrub_page *page) |
| 1672 | { |
Liu Bo | b4ff5ad | 2017-11-30 17:26:39 -0700 | [diff] [blame] | 1673 | DECLARE_COMPLETION_ONSTACK(done); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1674 | int ret; |
Liu Bo | 762221f | 2018-01-02 13:36:42 -0700 | [diff] [blame] | 1675 | int mirror_num; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1676 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1677 | bio->bi_iter.bi_sector = page->logical >> 9; |
| 1678 | bio->bi_private = &done; |
| 1679 | bio->bi_end_io = scrub_bio_wait_endio; |
| 1680 | |
Liu Bo | 762221f | 2018-01-02 13:36:42 -0700 | [diff] [blame] | 1681 | mirror_num = page->sblock->pagev[0]->mirror_num; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1682 | ret = raid56_parity_recover(fs_info, bio, page->recover->bbio, |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1683 | page->recover->map_length, |
Liu Bo | 762221f | 2018-01-02 13:36:42 -0700 | [diff] [blame] | 1684 | mirror_num, 0); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1685 | if (ret) |
| 1686 | return ret; |
| 1687 | |
Liu Bo | b4ff5ad | 2017-11-30 17:26:39 -0700 | [diff] [blame] | 1688 | wait_for_completion_io(&done); |
| 1689 | return blk_status_to_errno(bio->bi_status); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1690 | } |
| 1691 | |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 1692 | static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info, |
| 1693 | struct scrub_block *sblock) |
| 1694 | { |
| 1695 | struct scrub_page *first_page = sblock->pagev[0]; |
| 1696 | struct bio *bio; |
| 1697 | int page_num; |
| 1698 | |
| 1699 | /* All pages in sblock belong to the same stripe on the same device. */ |
| 1700 | ASSERT(first_page->dev); |
| 1701 | if (!first_page->dev->bdev) |
| 1702 | goto out; |
| 1703 | |
| 1704 | bio = btrfs_io_bio_alloc(BIO_MAX_PAGES); |
| 1705 | bio_set_dev(bio, first_page->dev->bdev); |
| 1706 | |
| 1707 | for (page_num = 0; page_num < sblock->page_count; page_num++) { |
| 1708 | struct scrub_page *page = sblock->pagev[page_num]; |
| 1709 | |
| 1710 | WARN_ON(!page->page); |
| 1711 | bio_add_page(bio, page->page, PAGE_SIZE, 0); |
| 1712 | } |
| 1713 | |
| 1714 | if (scrub_submit_raid56_bio_wait(fs_info, bio, first_page)) { |
| 1715 | bio_put(bio); |
| 1716 | goto out; |
| 1717 | } |
| 1718 | |
| 1719 | bio_put(bio); |
| 1720 | |
| 1721 | scrub_recheck_block_checksum(sblock); |
| 1722 | |
| 1723 | return; |
| 1724 | out: |
| 1725 | for (page_num = 0; page_num < sblock->page_count; page_num++) |
| 1726 | sblock->pagev[page_num]->io_error = 1; |
| 1727 | |
| 1728 | sblock->no_io_error_seen = 0; |
| 1729 | } |
| 1730 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1731 | /* |
| 1732 | * this function will check the on disk data for checksum errors, header |
| 1733 | * errors and read I/O errors. If any I/O errors happen, the exact pages |
| 1734 | * which are errored are marked as being bad. The goal is to enable scrub |
| 1735 | * to take those pages that are not errored from all the mirrors so that |
| 1736 | * the pages that are errored in the just handled mirror can be repaired. |
| 1737 | */ |
Stefan Behrens | 34f5c8e | 2012-11-02 16:16:26 +0100 | [diff] [blame] | 1738 | static void scrub_recheck_block(struct btrfs_fs_info *fs_info, |
Zhao Lei | affe4a5 | 2015-08-24 21:32:06 +0800 | [diff] [blame] | 1739 | struct scrub_block *sblock, |
| 1740 | int retry_failed_mirror) |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1741 | { |
| 1742 | int page_num; |
| 1743 | |
| 1744 | sblock->no_io_error_seen = 1; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1745 | |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 1746 | /* short cut for raid56 */ |
| 1747 | if (!retry_failed_mirror && scrub_is_page_on_raid56(sblock->pagev[0])) |
| 1748 | return scrub_recheck_block_on_raid56(fs_info, sblock); |
| 1749 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1750 | for (page_num = 0; page_num < sblock->page_count; page_num++) { |
| 1751 | struct bio *bio; |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1752 | struct scrub_page *page = sblock->pagev[page_num]; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1753 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1754 | if (page->dev->bdev == NULL) { |
Stefan Behrens | ea9947b | 2012-05-04 15:16:07 -0400 | [diff] [blame] | 1755 | page->io_error = 1; |
| 1756 | sblock->no_io_error_seen = 0; |
| 1757 | continue; |
| 1758 | } |
| 1759 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1760 | WARN_ON(!page->page); |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 1761 | bio = btrfs_io_bio_alloc(1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1762 | bio_set_dev(bio, page->dev->bdev); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1763 | |
Stefan Behrens | 34f5c8e | 2012-11-02 16:16:26 +0100 | [diff] [blame] | 1764 | bio_add_page(bio, page->page, PAGE_SIZE, 0); |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 1765 | bio->bi_iter.bi_sector = page->physical >> 9; |
| 1766 | bio->bi_opf = REQ_OP_READ; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1767 | |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 1768 | if (btrfsic_submit_bio_wait(bio)) { |
| 1769 | page->io_error = 1; |
| 1770 | sblock->no_io_error_seen = 0; |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 1771 | } |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 1772 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1773 | bio_put(bio); |
| 1774 | } |
| 1775 | |
| 1776 | if (sblock->no_io_error_seen) |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 1777 | scrub_recheck_block_checksum(sblock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1778 | } |
| 1779 | |
Miao Xie | 17a9be2 | 2014-07-24 11:37:08 +0800 | [diff] [blame] | 1780 | static inline int scrub_check_fsid(u8 fsid[], |
| 1781 | struct scrub_page *spage) |
| 1782 | { |
| 1783 | struct btrfs_fs_devices *fs_devices = spage->dev->fs_devices; |
| 1784 | int ret; |
| 1785 | |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 1786 | ret = memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Miao Xie | 17a9be2 | 2014-07-24 11:37:08 +0800 | [diff] [blame] | 1787 | return !ret; |
| 1788 | } |
| 1789 | |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 1790 | static void scrub_recheck_block_checksum(struct scrub_block *sblock) |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1791 | { |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 1792 | sblock->header_error = 0; |
| 1793 | sblock->checksum_error = 0; |
| 1794 | sblock->generation_error = 0; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1795 | |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 1796 | if (sblock->pagev[0]->flags & BTRFS_EXTENT_FLAG_DATA) |
| 1797 | scrub_checksum_data(sblock); |
| 1798 | else |
| 1799 | scrub_checksum_tree_block(sblock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1800 | } |
| 1801 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1802 | static int scrub_repair_block_from_good_copy(struct scrub_block *sblock_bad, |
Zhao Lei | 114ab50 | 2015-01-20 15:11:36 +0800 | [diff] [blame] | 1803 | struct scrub_block *sblock_good) |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1804 | { |
| 1805 | int page_num; |
| 1806 | int ret = 0; |
| 1807 | |
| 1808 | for (page_num = 0; page_num < sblock_bad->page_count; page_num++) { |
| 1809 | int ret_sub; |
| 1810 | |
| 1811 | ret_sub = scrub_repair_page_from_good_copy(sblock_bad, |
| 1812 | sblock_good, |
Zhao Lei | 114ab50 | 2015-01-20 15:11:36 +0800 | [diff] [blame] | 1813 | page_num, 1); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1814 | if (ret_sub) |
| 1815 | ret = ret_sub; |
| 1816 | } |
| 1817 | |
| 1818 | return ret; |
| 1819 | } |
| 1820 | |
| 1821 | static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad, |
| 1822 | struct scrub_block *sblock_good, |
| 1823 | int page_num, int force_write) |
| 1824 | { |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1825 | struct scrub_page *page_bad = sblock_bad->pagev[page_num]; |
| 1826 | struct scrub_page *page_good = sblock_good->pagev[page_num]; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1827 | struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1828 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 1829 | BUG_ON(page_bad->page == NULL); |
| 1830 | BUG_ON(page_good->page == NULL); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1831 | if (force_write || sblock_bad->header_error || |
| 1832 | sblock_bad->checksum_error || page_bad->io_error) { |
| 1833 | struct bio *bio; |
| 1834 | int ret; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1835 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1836 | if (!page_bad->dev->bdev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1837 | btrfs_warn_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 1838 | "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected"); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1839 | return -EIO; |
| 1840 | } |
| 1841 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 1842 | bio = btrfs_io_bio_alloc(1); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1843 | bio_set_dev(bio, page_bad->dev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1844 | bio->bi_iter.bi_sector = page_bad->physical >> 9; |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 1845 | bio->bi_opf = REQ_OP_WRITE; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1846 | |
| 1847 | ret = bio_add_page(bio, page_good->page, PAGE_SIZE, 0); |
| 1848 | if (PAGE_SIZE != ret) { |
| 1849 | bio_put(bio); |
| 1850 | return -EIO; |
| 1851 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1852 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1853 | if (btrfsic_submit_bio_wait(bio)) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1854 | btrfs_dev_stat_inc_and_print(page_bad->dev, |
| 1855 | BTRFS_DEV_STAT_WRITE_ERRS); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1856 | btrfs_dev_replace_stats_inc( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1857 | &fs_info->dev_replace.num_write_errors); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 1858 | bio_put(bio); |
| 1859 | return -EIO; |
| 1860 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 1861 | bio_put(bio); |
| 1862 | } |
| 1863 | |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1867 | static void scrub_write_block_to_dev_replace(struct scrub_block *sblock) |
| 1868 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1869 | struct btrfs_fs_info *fs_info = sblock->sctx->fs_info; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1870 | int page_num; |
| 1871 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 1872 | /* |
| 1873 | * This block is used for the check of the parity on the source device, |
| 1874 | * so the data needn't be written into the destination device. |
| 1875 | */ |
| 1876 | if (sblock->sparity) |
| 1877 | return; |
| 1878 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1879 | for (page_num = 0; page_num < sblock->page_count; page_num++) { |
| 1880 | int ret; |
| 1881 | |
| 1882 | ret = scrub_write_page_to_dev_replace(sblock, page_num); |
| 1883 | if (ret) |
| 1884 | btrfs_dev_replace_stats_inc( |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1885 | &fs_info->dev_replace.num_write_errors); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | static int scrub_write_page_to_dev_replace(struct scrub_block *sblock, |
| 1890 | int page_num) |
| 1891 | { |
| 1892 | struct scrub_page *spage = sblock->pagev[page_num]; |
| 1893 | |
| 1894 | BUG_ON(spage->page == NULL); |
| 1895 | if (spage->io_error) { |
| 1896 | void *mapped_buffer = kmap_atomic(spage->page); |
| 1897 | |
David Sterba | 619a974 | 2017-03-29 20:48:44 +0200 | [diff] [blame] | 1898 | clear_page(mapped_buffer); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1899 | flush_dcache_page(spage->page); |
| 1900 | kunmap_atomic(mapped_buffer); |
| 1901 | } |
| 1902 | return scrub_add_page_to_wr_bio(sblock->sctx, spage); |
| 1903 | } |
| 1904 | |
| 1905 | static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx, |
| 1906 | struct scrub_page *spage) |
| 1907 | { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1908 | struct scrub_bio *sbio; |
| 1909 | int ret; |
| 1910 | |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1911 | mutex_lock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1912 | again: |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1913 | if (!sctx->wr_curr_bio) { |
| 1914 | sctx->wr_curr_bio = kzalloc(sizeof(*sctx->wr_curr_bio), |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 1915 | GFP_KERNEL); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1916 | if (!sctx->wr_curr_bio) { |
| 1917 | mutex_unlock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1918 | return -ENOMEM; |
| 1919 | } |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1920 | sctx->wr_curr_bio->sctx = sctx; |
| 1921 | sctx->wr_curr_bio->page_count = 0; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1922 | } |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1923 | sbio = sctx->wr_curr_bio; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1924 | if (sbio->page_count == 0) { |
| 1925 | struct bio *bio; |
| 1926 | |
| 1927 | sbio->physical = spage->physical_for_dev_replace; |
| 1928 | sbio->logical = spage->logical; |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1929 | sbio->dev = sctx->wr_tgtdev; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1930 | bio = sbio->bio; |
| 1931 | if (!bio) { |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 1932 | bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1933 | sbio->bio = bio; |
| 1934 | } |
| 1935 | |
| 1936 | bio->bi_private = sbio; |
| 1937 | bio->bi_end_io = scrub_wr_bio_end_io; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1938 | bio_set_dev(bio, sbio->dev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1939 | bio->bi_iter.bi_sector = sbio->physical >> 9; |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 1940 | bio->bi_opf = REQ_OP_WRITE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1941 | sbio->status = 0; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1942 | } else if (sbio->physical + sbio->page_count * PAGE_SIZE != |
| 1943 | spage->physical_for_dev_replace || |
| 1944 | sbio->logical + sbio->page_count * PAGE_SIZE != |
| 1945 | spage->logical) { |
| 1946 | scrub_wr_submit(sctx); |
| 1947 | goto again; |
| 1948 | } |
| 1949 | |
| 1950 | ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0); |
| 1951 | if (ret != PAGE_SIZE) { |
| 1952 | if (sbio->page_count < 1) { |
| 1953 | bio_put(sbio->bio); |
| 1954 | sbio->bio = NULL; |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1955 | mutex_unlock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1956 | return -EIO; |
| 1957 | } |
| 1958 | scrub_wr_submit(sctx); |
| 1959 | goto again; |
| 1960 | } |
| 1961 | |
| 1962 | sbio->pagev[sbio->page_count] = spage; |
| 1963 | scrub_page_get(spage); |
| 1964 | sbio->page_count++; |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1965 | if (sbio->page_count == sctx->pages_per_wr_bio) |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1966 | scrub_wr_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1967 | mutex_unlock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1968 | |
| 1969 | return 0; |
| 1970 | } |
| 1971 | |
| 1972 | static void scrub_wr_submit(struct scrub_ctx *sctx) |
| 1973 | { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1974 | struct scrub_bio *sbio; |
| 1975 | |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1976 | if (!sctx->wr_curr_bio) |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1977 | return; |
| 1978 | |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 1979 | sbio = sctx->wr_curr_bio; |
| 1980 | sctx->wr_curr_bio = NULL; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1981 | WARN_ON(!sbio->bio->bi_disk); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1982 | scrub_pending_bio_inc(sctx); |
| 1983 | /* process all writes in a single worker thread. Then the block layer |
| 1984 | * orders the requests before sending them to the driver which |
| 1985 | * doubled the write performance on spinning disks when measured |
| 1986 | * with Linux 3.5 */ |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1987 | btrfsic_submit_bio(sbio->bio); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1990 | static void scrub_wr_bio_end_io(struct bio *bio) |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1991 | { |
| 1992 | struct scrub_bio *sbio = bio->bi_private; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 1993 | struct btrfs_fs_info *fs_info = sbio->dev->fs_info; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1994 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1995 | sbio->status = bio->bi_status; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 1996 | sbio->bio = bio; |
| 1997 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 1998 | btrfs_init_work(&sbio->work, btrfs_scrubwrc_helper, |
| 1999 | scrub_wr_bio_end_io_worker, NULL, NULL); |
Qu Wenruo | 0339ef2 | 2014-02-28 10:46:17 +0800 | [diff] [blame] | 2000 | btrfs_queue_work(fs_info->scrub_wr_completion_workers, &sbio->work); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | static void scrub_wr_bio_end_io_worker(struct btrfs_work *work) |
| 2004 | { |
| 2005 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); |
| 2006 | struct scrub_ctx *sctx = sbio->sctx; |
| 2007 | int i; |
| 2008 | |
| 2009 | WARN_ON(sbio->page_count > SCRUB_PAGES_PER_WR_BIO); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2010 | if (sbio->status) { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2011 | struct btrfs_dev_replace *dev_replace = |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2012 | &sbio->sctx->fs_info->dev_replace; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2013 | |
| 2014 | for (i = 0; i < sbio->page_count; i++) { |
| 2015 | struct scrub_page *spage = sbio->pagev[i]; |
| 2016 | |
| 2017 | spage->io_error = 1; |
| 2018 | btrfs_dev_replace_stats_inc(&dev_replace-> |
| 2019 | num_write_errors); |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | for (i = 0; i < sbio->page_count; i++) |
| 2024 | scrub_page_put(sbio->pagev[i]); |
| 2025 | |
| 2026 | bio_put(sbio->bio); |
| 2027 | kfree(sbio); |
| 2028 | scrub_pending_bio_dec(sctx); |
| 2029 | } |
| 2030 | |
| 2031 | static int scrub_checksum(struct scrub_block *sblock) |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2032 | { |
| 2033 | u64 flags; |
| 2034 | int ret; |
| 2035 | |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2036 | /* |
| 2037 | * No need to initialize these stats currently, |
| 2038 | * because this function only use return value |
| 2039 | * instead of these stats value. |
| 2040 | * |
| 2041 | * Todo: |
| 2042 | * always use stats |
| 2043 | */ |
| 2044 | sblock->header_error = 0; |
| 2045 | sblock->generation_error = 0; |
| 2046 | sblock->checksum_error = 0; |
| 2047 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2048 | WARN_ON(sblock->page_count < 1); |
| 2049 | flags = sblock->pagev[0]->flags; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2050 | ret = 0; |
| 2051 | if (flags & BTRFS_EXTENT_FLAG_DATA) |
| 2052 | ret = scrub_checksum_data(sblock); |
| 2053 | else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
| 2054 | ret = scrub_checksum_tree_block(sblock); |
| 2055 | else if (flags & BTRFS_EXTENT_FLAG_SUPER) |
| 2056 | (void)scrub_checksum_super(sblock); |
| 2057 | else |
| 2058 | WARN_ON(1); |
| 2059 | if (ret) |
| 2060 | scrub_handle_errored_block(sblock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2061 | |
| 2062 | return ret; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | static int scrub_checksum_data(struct scrub_block *sblock) |
| 2066 | { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2067 | struct scrub_ctx *sctx = sblock->sctx; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2068 | u8 csum[BTRFS_CSUM_SIZE]; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2069 | u8 *on_disk_csum; |
| 2070 | struct page *page; |
| 2071 | void *buffer; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2072 | u32 crc = ~(u32)0; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2073 | u64 len; |
| 2074 | int index; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2075 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2076 | BUG_ON(sblock->page_count < 1); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2077 | if (!sblock->pagev[0]->have_csum) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2078 | return 0; |
| 2079 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2080 | on_disk_csum = sblock->pagev[0]->csum; |
| 2081 | page = sblock->pagev[0]->page; |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2082 | buffer = kmap_atomic(page); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2083 | |
David Sterba | 25cc122 | 2017-05-16 19:10:41 +0200 | [diff] [blame] | 2084 | len = sctx->fs_info->sectorsize; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2085 | index = 0; |
| 2086 | for (;;) { |
| 2087 | u64 l = min_t(u64, len, PAGE_SIZE); |
| 2088 | |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2089 | crc = btrfs_csum_data(buffer, crc, l); |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2090 | kunmap_atomic(buffer); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2091 | len -= l; |
| 2092 | if (len == 0) |
| 2093 | break; |
| 2094 | index++; |
| 2095 | BUG_ON(index >= sblock->page_count); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2096 | BUG_ON(!sblock->pagev[index]->page); |
| 2097 | page = sblock->pagev[index]->page; |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2098 | buffer = kmap_atomic(page); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2099 | } |
| 2100 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2101 | btrfs_csum_final(crc, csum); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2102 | if (memcmp(csum, on_disk_csum, sctx->csum_size)) |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2103 | sblock->checksum_error = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2104 | |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2105 | return sblock->checksum_error; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2108 | static int scrub_checksum_tree_block(struct scrub_block *sblock) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2109 | { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2110 | struct scrub_ctx *sctx = sblock->sctx; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2111 | struct btrfs_header *h; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2112 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2113 | u8 calculated_csum[BTRFS_CSUM_SIZE]; |
| 2114 | u8 on_disk_csum[BTRFS_CSUM_SIZE]; |
| 2115 | struct page *page; |
| 2116 | void *mapped_buffer; |
| 2117 | u64 mapped_size; |
| 2118 | void *p; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2119 | u32 crc = ~(u32)0; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2120 | u64 len; |
| 2121 | int index; |
| 2122 | |
| 2123 | BUG_ON(sblock->page_count < 1); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2124 | page = sblock->pagev[0]->page; |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2125 | mapped_buffer = kmap_atomic(page); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2126 | h = (struct btrfs_header *)mapped_buffer; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2127 | memcpy(on_disk_csum, h->csum, sctx->csum_size); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2128 | |
| 2129 | /* |
| 2130 | * we don't use the getter functions here, as we |
| 2131 | * a) don't have an extent buffer and |
| 2132 | * b) the page is already kmapped |
| 2133 | */ |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2134 | if (sblock->pagev[0]->logical != btrfs_stack_header_bytenr(h)) |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2135 | sblock->header_error = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2136 | |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2137 | if (sblock->pagev[0]->generation != btrfs_stack_header_generation(h)) { |
| 2138 | sblock->header_error = 1; |
| 2139 | sblock->generation_error = 1; |
| 2140 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2141 | |
Miao Xie | 17a9be2 | 2014-07-24 11:37:08 +0800 | [diff] [blame] | 2142 | if (!scrub_check_fsid(h->fsid, sblock->pagev[0])) |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2143 | sblock->header_error = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2144 | |
| 2145 | if (memcmp(h->chunk_tree_uuid, fs_info->chunk_tree_uuid, |
| 2146 | BTRFS_UUID_SIZE)) |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2147 | sblock->header_error = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2148 | |
David Sterba | 25cc122 | 2017-05-16 19:10:41 +0200 | [diff] [blame] | 2149 | len = sctx->fs_info->nodesize - BTRFS_CSUM_SIZE; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2150 | mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE; |
| 2151 | p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE; |
| 2152 | index = 0; |
| 2153 | for (;;) { |
| 2154 | u64 l = min_t(u64, len, mapped_size); |
| 2155 | |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2156 | crc = btrfs_csum_data(p, crc, l); |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2157 | kunmap_atomic(mapped_buffer); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2158 | len -= l; |
| 2159 | if (len == 0) |
| 2160 | break; |
| 2161 | index++; |
| 2162 | BUG_ON(index >= sblock->page_count); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2163 | BUG_ON(!sblock->pagev[index]->page); |
| 2164 | page = sblock->pagev[index]->page; |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2165 | mapped_buffer = kmap_atomic(page); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2166 | mapped_size = PAGE_SIZE; |
| 2167 | p = mapped_buffer; |
| 2168 | } |
| 2169 | |
| 2170 | btrfs_csum_final(crc, calculated_csum); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2171 | if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size)) |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2172 | sblock->checksum_error = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2173 | |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2174 | return sblock->header_error || sblock->checksum_error; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2177 | static int scrub_checksum_super(struct scrub_block *sblock) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2178 | { |
| 2179 | struct btrfs_super_block *s; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2180 | struct scrub_ctx *sctx = sblock->sctx; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2181 | u8 calculated_csum[BTRFS_CSUM_SIZE]; |
| 2182 | u8 on_disk_csum[BTRFS_CSUM_SIZE]; |
| 2183 | struct page *page; |
| 2184 | void *mapped_buffer; |
| 2185 | u64 mapped_size; |
| 2186 | void *p; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2187 | u32 crc = ~(u32)0; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2188 | int fail_gen = 0; |
| 2189 | int fail_cor = 0; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2190 | u64 len; |
| 2191 | int index; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2192 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2193 | BUG_ON(sblock->page_count < 1); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2194 | page = sblock->pagev[0]->page; |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2195 | mapped_buffer = kmap_atomic(page); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2196 | s = (struct btrfs_super_block *)mapped_buffer; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2197 | memcpy(on_disk_csum, s->csum, sctx->csum_size); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2198 | |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2199 | if (sblock->pagev[0]->logical != btrfs_super_bytenr(s)) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2200 | ++fail_cor; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2201 | |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2202 | if (sblock->pagev[0]->generation != btrfs_super_generation(s)) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2203 | ++fail_gen; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2204 | |
Miao Xie | 17a9be2 | 2014-07-24 11:37:08 +0800 | [diff] [blame] | 2205 | if (!scrub_check_fsid(s->fsid, sblock->pagev[0])) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2206 | ++fail_cor; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2207 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2208 | len = BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE; |
| 2209 | mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE; |
| 2210 | p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE; |
| 2211 | index = 0; |
| 2212 | for (;;) { |
| 2213 | u64 l = min_t(u64, len, mapped_size); |
| 2214 | |
Liu Bo | b049668 | 2013-03-14 14:57:45 +0000 | [diff] [blame] | 2215 | crc = btrfs_csum_data(p, crc, l); |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2216 | kunmap_atomic(mapped_buffer); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2217 | len -= l; |
| 2218 | if (len == 0) |
| 2219 | break; |
| 2220 | index++; |
| 2221 | BUG_ON(index >= sblock->page_count); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2222 | BUG_ON(!sblock->pagev[index]->page); |
| 2223 | page = sblock->pagev[index]->page; |
Linus Torvalds | 9613beb | 2012-03-30 12:44:29 -0700 | [diff] [blame] | 2224 | mapped_buffer = kmap_atomic(page); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2225 | mapped_size = PAGE_SIZE; |
| 2226 | p = mapped_buffer; |
| 2227 | } |
| 2228 | |
| 2229 | btrfs_csum_final(crc, calculated_csum); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2230 | if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size)) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2231 | ++fail_cor; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2232 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2233 | if (fail_cor + fail_gen) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2234 | /* |
| 2235 | * if we find an error in a super block, we just report it. |
| 2236 | * They will get written with the next transaction commit |
| 2237 | * anyway |
| 2238 | */ |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2239 | spin_lock(&sctx->stat_lock); |
| 2240 | ++sctx->stat.super_errors; |
| 2241 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2242 | if (fail_cor) |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2243 | btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev, |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2244 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
| 2245 | else |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2246 | btrfs_dev_stat_inc_and_print(sblock->pagev[0]->dev, |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2247 | BTRFS_DEV_STAT_GENERATION_ERRS); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 2250 | return fail_cor + fail_gen; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2253 | static void scrub_block_get(struct scrub_block *sblock) |
| 2254 | { |
Elena Reshetova | 186debd | 2017-03-03 10:55:23 +0200 | [diff] [blame] | 2255 | refcount_inc(&sblock->refs); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | static void scrub_block_put(struct scrub_block *sblock) |
| 2259 | { |
Elena Reshetova | 186debd | 2017-03-03 10:55:23 +0200 | [diff] [blame] | 2260 | if (refcount_dec_and_test(&sblock->refs)) { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2261 | int i; |
| 2262 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2263 | if (sblock->sparity) |
| 2264 | scrub_parity_put(sblock->sparity); |
| 2265 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2266 | for (i = 0; i < sblock->page_count; i++) |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2267 | scrub_page_put(sblock->pagev[i]); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2268 | kfree(sblock); |
| 2269 | } |
| 2270 | } |
| 2271 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2272 | static void scrub_page_get(struct scrub_page *spage) |
| 2273 | { |
Zhao Lei | 5701934 | 2015-01-20 15:11:45 +0800 | [diff] [blame] | 2274 | atomic_inc(&spage->refs); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | static void scrub_page_put(struct scrub_page *spage) |
| 2278 | { |
Zhao Lei | 5701934 | 2015-01-20 15:11:45 +0800 | [diff] [blame] | 2279 | if (atomic_dec_and_test(&spage->refs)) { |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2280 | if (spage->page) |
| 2281 | __free_page(spage->page); |
| 2282 | kfree(spage); |
| 2283 | } |
| 2284 | } |
| 2285 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2286 | static void scrub_submit(struct scrub_ctx *sctx) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2287 | { |
| 2288 | struct scrub_bio *sbio; |
| 2289 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2290 | if (sctx->curr == -1) |
Stefan Behrens | 1623ede | 2012-03-27 14:21:26 -0400 | [diff] [blame] | 2291 | return; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2292 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2293 | sbio = sctx->bios[sctx->curr]; |
| 2294 | sctx->curr = -1; |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 2295 | scrub_pending_bio_inc(sctx); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2296 | btrfsic_submit_bio(sbio->bio); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2299 | static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx, |
| 2300 | struct scrub_page *spage) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2301 | { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2302 | struct scrub_block *sblock = spage->sblock; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2303 | struct scrub_bio *sbio; |
Arne Jansen | 69f4cb5 | 2011-11-11 08:17:10 -0500 | [diff] [blame] | 2304 | int ret; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2305 | |
| 2306 | again: |
| 2307 | /* |
| 2308 | * grab a fresh bio or wait for one to become available |
| 2309 | */ |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2310 | while (sctx->curr == -1) { |
| 2311 | spin_lock(&sctx->list_lock); |
| 2312 | sctx->curr = sctx->first_free; |
| 2313 | if (sctx->curr != -1) { |
| 2314 | sctx->first_free = sctx->bios[sctx->curr]->next_free; |
| 2315 | sctx->bios[sctx->curr]->next_free = -1; |
| 2316 | sctx->bios[sctx->curr]->page_count = 0; |
| 2317 | spin_unlock(&sctx->list_lock); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2318 | } else { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2319 | spin_unlock(&sctx->list_lock); |
| 2320 | wait_event(sctx->list_wait, sctx->first_free != -1); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2321 | } |
| 2322 | } |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2323 | sbio = sctx->bios[sctx->curr]; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2324 | if (sbio->page_count == 0) { |
Arne Jansen | 69f4cb5 | 2011-11-11 08:17:10 -0500 | [diff] [blame] | 2325 | struct bio *bio; |
| 2326 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2327 | sbio->physical = spage->physical; |
| 2328 | sbio->logical = spage->logical; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 2329 | sbio->dev = spage->dev; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2330 | bio = sbio->bio; |
| 2331 | if (!bio) { |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2332 | bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2333 | sbio->bio = bio; |
| 2334 | } |
Arne Jansen | 69f4cb5 | 2011-11-11 08:17:10 -0500 | [diff] [blame] | 2335 | |
| 2336 | bio->bi_private = sbio; |
| 2337 | bio->bi_end_io = scrub_bio_end_io; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2338 | bio_set_dev(bio, sbio->dev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2339 | bio->bi_iter.bi_sector = sbio->physical >> 9; |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 2340 | bio->bi_opf = REQ_OP_READ; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2341 | sbio->status = 0; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2342 | } else if (sbio->physical + sbio->page_count * PAGE_SIZE != |
| 2343 | spage->physical || |
| 2344 | sbio->logical + sbio->page_count * PAGE_SIZE != |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 2345 | spage->logical || |
| 2346 | sbio->dev != spage->dev) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2347 | scrub_submit(sctx); |
Arne Jansen | 69f4cb5 | 2011-11-11 08:17:10 -0500 | [diff] [blame] | 2348 | goto again; |
| 2349 | } |
| 2350 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2351 | sbio->pagev[sbio->page_count] = spage; |
| 2352 | ret = bio_add_page(sbio->bio, spage->page, PAGE_SIZE, 0); |
| 2353 | if (ret != PAGE_SIZE) { |
| 2354 | if (sbio->page_count < 1) { |
| 2355 | bio_put(sbio->bio); |
| 2356 | sbio->bio = NULL; |
| 2357 | return -EIO; |
| 2358 | } |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2359 | scrub_submit(sctx); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2360 | goto again; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2361 | } |
Arne Jansen | 1bc8779 | 2011-05-28 21:57:55 +0200 | [diff] [blame] | 2362 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2363 | scrub_block_get(sblock); /* one for the page added to the bio */ |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2364 | atomic_inc(&sblock->outstanding_pages); |
| 2365 | sbio->page_count++; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2366 | if (sbio->page_count == sctx->pages_per_rd_bio) |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2367 | scrub_submit(sctx); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2368 | |
| 2369 | return 0; |
| 2370 | } |
| 2371 | |
Linus Torvalds | 2236597 | 2015-09-05 15:14:43 -0700 | [diff] [blame] | 2372 | static void scrub_missing_raid56_end_io(struct bio *bio) |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2373 | { |
| 2374 | struct scrub_block *sblock = bio->bi_private; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2375 | struct btrfs_fs_info *fs_info = sblock->sctx->fs_info; |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2376 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2377 | if (bio->bi_status) |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2378 | sblock->no_io_error_seen = 0; |
| 2379 | |
Scott Talbert | 4673272 | 2016-05-09 09:14:28 -0400 | [diff] [blame] | 2380 | bio_put(bio); |
| 2381 | |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2382 | btrfs_queue_work(fs_info->scrub_workers, &sblock->work); |
| 2383 | } |
| 2384 | |
| 2385 | static void scrub_missing_raid56_worker(struct btrfs_work *work) |
| 2386 | { |
| 2387 | struct scrub_block *sblock = container_of(work, struct scrub_block, work); |
| 2388 | struct scrub_ctx *sctx = sblock->sctx; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2389 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2390 | u64 logical; |
| 2391 | struct btrfs_device *dev; |
| 2392 | |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2393 | logical = sblock->pagev[0]->logical; |
| 2394 | dev = sblock->pagev[0]->dev; |
| 2395 | |
Zhao Lei | affe4a5 | 2015-08-24 21:32:06 +0800 | [diff] [blame] | 2396 | if (sblock->no_io_error_seen) |
Zhao Lei | ba7cf98 | 2015-08-24 21:18:02 +0800 | [diff] [blame] | 2397 | scrub_recheck_block_checksum(sblock); |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2398 | |
| 2399 | if (!sblock->no_io_error_seen) { |
| 2400 | spin_lock(&sctx->stat_lock); |
| 2401 | sctx->stat.read_errors++; |
| 2402 | spin_unlock(&sctx->stat_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2403 | btrfs_err_rl_in_rcu(fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2404 | "IO error rebuilding logical %llu for dev %s", |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2405 | logical, rcu_str_deref(dev->name)); |
| 2406 | } else if (sblock->header_error || sblock->checksum_error) { |
| 2407 | spin_lock(&sctx->stat_lock); |
| 2408 | sctx->stat.uncorrectable_errors++; |
| 2409 | spin_unlock(&sctx->stat_lock); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2410 | btrfs_err_rl_in_rcu(fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 2411 | "failed to rebuild valid logical %llu for dev %s", |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2412 | logical, rcu_str_deref(dev->name)); |
| 2413 | } else { |
| 2414 | scrub_write_block_to_dev_replace(sblock); |
| 2415 | } |
| 2416 | |
| 2417 | scrub_block_put(sblock); |
| 2418 | |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 2419 | if (sctx->is_dev_replace && sctx->flush_all_writes) { |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 2420 | mutex_lock(&sctx->wr_lock); |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2421 | scrub_wr_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 2422 | mutex_unlock(&sctx->wr_lock); |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | scrub_pending_bio_dec(sctx); |
| 2426 | } |
| 2427 | |
| 2428 | static void scrub_missing_raid56_pages(struct scrub_block *sblock) |
| 2429 | { |
| 2430 | struct scrub_ctx *sctx = sblock->sctx; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2431 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2432 | u64 length = sblock->page_count * PAGE_SIZE; |
| 2433 | u64 logical = sblock->pagev[0]->logical; |
Zhao Lei | f1fee65 | 2016-05-17 17:37:38 +0800 | [diff] [blame] | 2434 | struct btrfs_bio *bbio = NULL; |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2435 | struct bio *bio; |
| 2436 | struct btrfs_raid_bio *rbio; |
| 2437 | int ret; |
| 2438 | int i; |
| 2439 | |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 2440 | btrfs_bio_counter_inc_blocked(fs_info); |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 2441 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 2442 | &length, &bbio); |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2443 | if (ret || !bbio || !bbio->raid_map) |
| 2444 | goto bbio_out; |
| 2445 | |
| 2446 | if (WARN_ON(!sctx->is_dev_replace || |
| 2447 | !(bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK))) { |
| 2448 | /* |
| 2449 | * We shouldn't be scrubbing a missing device. Even for dev |
| 2450 | * replace, we should only get here for RAID 5/6. We either |
| 2451 | * managed to mount something with no mirrors remaining or |
| 2452 | * there's a bug in scrub_remap_extent()/btrfs_map_block(). |
| 2453 | */ |
| 2454 | goto bbio_out; |
| 2455 | } |
| 2456 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 2457 | bio = btrfs_io_bio_alloc(0); |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2458 | bio->bi_iter.bi_sector = logical >> 9; |
| 2459 | bio->bi_private = sblock; |
| 2460 | bio->bi_end_io = scrub_missing_raid56_end_io; |
| 2461 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2462 | rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length); |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2463 | if (!rbio) |
| 2464 | goto rbio_out; |
| 2465 | |
| 2466 | for (i = 0; i < sblock->page_count; i++) { |
| 2467 | struct scrub_page *spage = sblock->pagev[i]; |
| 2468 | |
| 2469 | raid56_add_scrub_pages(rbio, spage->page, spage->logical); |
| 2470 | } |
| 2471 | |
| 2472 | btrfs_init_work(&sblock->work, btrfs_scrub_helper, |
| 2473 | scrub_missing_raid56_worker, NULL, NULL); |
| 2474 | scrub_block_get(sblock); |
| 2475 | scrub_pending_bio_inc(sctx); |
| 2476 | raid56_submit_missing_rbio(rbio); |
| 2477 | return; |
| 2478 | |
| 2479 | rbio_out: |
| 2480 | bio_put(bio); |
| 2481 | bbio_out: |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 2482 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2483 | btrfs_put_bbio(bbio); |
| 2484 | spin_lock(&sctx->stat_lock); |
| 2485 | sctx->stat.malloc_errors++; |
| 2486 | spin_unlock(&sctx->stat_lock); |
| 2487 | } |
| 2488 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2489 | static int scrub_pages(struct scrub_ctx *sctx, u64 logical, u64 len, |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 2490 | u64 physical, struct btrfs_device *dev, u64 flags, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2491 | u64 gen, int mirror_num, u8 *csum, int force, |
| 2492 | u64 physical_for_dev_replace) |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2493 | { |
| 2494 | struct scrub_block *sblock; |
| 2495 | int index; |
| 2496 | |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 2497 | sblock = kzalloc(sizeof(*sblock), GFP_KERNEL); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2498 | if (!sblock) { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2499 | spin_lock(&sctx->stat_lock); |
| 2500 | sctx->stat.malloc_errors++; |
| 2501 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2502 | return -ENOMEM; |
| 2503 | } |
| 2504 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2505 | /* one ref inside this function, plus one for each page added to |
| 2506 | * a bio later on */ |
Elena Reshetova | 186debd | 2017-03-03 10:55:23 +0200 | [diff] [blame] | 2507 | refcount_set(&sblock->refs, 1); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2508 | sblock->sctx = sctx; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2509 | sblock->no_io_error_seen = 1; |
| 2510 | |
| 2511 | for (index = 0; len > 0; index++) { |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2512 | struct scrub_page *spage; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2513 | u64 l = min_t(u64, len, PAGE_SIZE); |
| 2514 | |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 2515 | spage = kzalloc(sizeof(*spage), GFP_KERNEL); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2516 | if (!spage) { |
| 2517 | leave_nomem: |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2518 | spin_lock(&sctx->stat_lock); |
| 2519 | sctx->stat.malloc_errors++; |
| 2520 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2521 | scrub_block_put(sblock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2522 | return -ENOMEM; |
| 2523 | } |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2524 | BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK); |
| 2525 | scrub_page_get(spage); |
| 2526 | sblock->pagev[index] = spage; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2527 | spage->sblock = sblock; |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 2528 | spage->dev = dev; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2529 | spage->flags = flags; |
| 2530 | spage->generation = gen; |
| 2531 | spage->logical = logical; |
| 2532 | spage->physical = physical; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2533 | spage->physical_for_dev_replace = physical_for_dev_replace; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2534 | spage->mirror_num = mirror_num; |
| 2535 | if (csum) { |
| 2536 | spage->have_csum = 1; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2537 | memcpy(spage->csum, csum, sctx->csum_size); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2538 | } else { |
| 2539 | spage->have_csum = 0; |
| 2540 | } |
| 2541 | sblock->page_count++; |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 2542 | spage->page = alloc_page(GFP_KERNEL); |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2543 | if (!spage->page) |
| 2544 | goto leave_nomem; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2545 | len -= l; |
| 2546 | logical += l; |
| 2547 | physical += l; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2548 | physical_for_dev_replace += l; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2549 | } |
| 2550 | |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 2551 | WARN_ON(sblock->page_count == 0); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2552 | if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) { |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2553 | /* |
| 2554 | * This case should only be hit for RAID 5/6 device replace. See |
| 2555 | * the comment in scrub_missing_raid56_pages() for details. |
| 2556 | */ |
| 2557 | scrub_missing_raid56_pages(sblock); |
| 2558 | } else { |
| 2559 | for (index = 0; index < sblock->page_count; index++) { |
| 2560 | struct scrub_page *spage = sblock->pagev[index]; |
| 2561 | int ret; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2562 | |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2563 | ret = scrub_add_page_to_rd_bio(sctx, spage); |
| 2564 | if (ret) { |
| 2565 | scrub_block_put(sblock); |
| 2566 | return ret; |
| 2567 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2568 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2569 | |
Omar Sandoval | 73ff61d | 2015-06-19 11:52:51 -0700 | [diff] [blame] | 2570 | if (force) |
| 2571 | scrub_submit(sctx); |
| 2572 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2573 | |
| 2574 | /* last one frees, either here or in bio completion for last page */ |
| 2575 | scrub_block_put(sblock); |
| 2576 | return 0; |
| 2577 | } |
| 2578 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2579 | static void scrub_bio_end_io(struct bio *bio) |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2580 | { |
| 2581 | struct scrub_bio *sbio = bio->bi_private; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2582 | struct btrfs_fs_info *fs_info = sbio->dev->fs_info; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2583 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2584 | sbio->status = bio->bi_status; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2585 | sbio->bio = bio; |
| 2586 | |
Qu Wenruo | 0339ef2 | 2014-02-28 10:46:17 +0800 | [diff] [blame] | 2587 | btrfs_queue_work(fs_info->scrub_workers, &sbio->work); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | static void scrub_bio_end_io_worker(struct btrfs_work *work) |
| 2591 | { |
| 2592 | struct scrub_bio *sbio = container_of(work, struct scrub_bio, work); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2593 | struct scrub_ctx *sctx = sbio->sctx; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2594 | int i; |
| 2595 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2596 | BUG_ON(sbio->page_count > SCRUB_PAGES_PER_RD_BIO); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2597 | if (sbio->status) { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2598 | for (i = 0; i < sbio->page_count; i++) { |
| 2599 | struct scrub_page *spage = sbio->pagev[i]; |
| 2600 | |
| 2601 | spage->io_error = 1; |
| 2602 | spage->sblock->no_io_error_seen = 0; |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | /* now complete the scrub_block items that have all pages completed */ |
| 2607 | for (i = 0; i < sbio->page_count; i++) { |
| 2608 | struct scrub_page *spage = sbio->pagev[i]; |
| 2609 | struct scrub_block *sblock = spage->sblock; |
| 2610 | |
| 2611 | if (atomic_dec_and_test(&sblock->outstanding_pages)) |
| 2612 | scrub_block_complete(sblock); |
| 2613 | scrub_block_put(sblock); |
| 2614 | } |
| 2615 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2616 | bio_put(sbio->bio); |
| 2617 | sbio->bio = NULL; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2618 | spin_lock(&sctx->list_lock); |
| 2619 | sbio->next_free = sctx->first_free; |
| 2620 | sctx->first_free = sbio->index; |
| 2621 | spin_unlock(&sctx->list_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2622 | |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 2623 | if (sctx->is_dev_replace && sctx->flush_all_writes) { |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 2624 | mutex_lock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2625 | scrub_wr_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 2626 | mutex_unlock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2627 | } |
| 2628 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 2629 | scrub_pending_bio_dec(sctx); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2630 | } |
| 2631 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2632 | static inline void __scrub_mark_bitmap(struct scrub_parity *sparity, |
| 2633 | unsigned long *bitmap, |
| 2634 | u64 start, u64 len) |
| 2635 | { |
Liu Bo | 972d721 | 2017-04-03 13:45:33 -0700 | [diff] [blame] | 2636 | u64 offset; |
David Sterba | 7736b0a | 2017-03-31 18:02:48 +0200 | [diff] [blame] | 2637 | u64 nsectors64; |
| 2638 | u32 nsectors; |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 2639 | int sectorsize = sparity->sctx->fs_info->sectorsize; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2640 | |
| 2641 | if (len >= sparity->stripe_len) { |
| 2642 | bitmap_set(bitmap, 0, sparity->nsectors); |
| 2643 | return; |
| 2644 | } |
| 2645 | |
| 2646 | start -= sparity->logic_start; |
Liu Bo | 972d721 | 2017-04-03 13:45:33 -0700 | [diff] [blame] | 2647 | start = div64_u64_rem(start, sparity->stripe_len, &offset); |
| 2648 | offset = div_u64(offset, sectorsize); |
David Sterba | 7736b0a | 2017-03-31 18:02:48 +0200 | [diff] [blame] | 2649 | nsectors64 = div_u64(len, sectorsize); |
| 2650 | |
| 2651 | ASSERT(nsectors64 < UINT_MAX); |
| 2652 | nsectors = (u32)nsectors64; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2653 | |
| 2654 | if (offset + nsectors <= sparity->nsectors) { |
| 2655 | bitmap_set(bitmap, offset, nsectors); |
| 2656 | return; |
| 2657 | } |
| 2658 | |
| 2659 | bitmap_set(bitmap, offset, sparity->nsectors - offset); |
| 2660 | bitmap_set(bitmap, 0, nsectors - (sparity->nsectors - offset)); |
| 2661 | } |
| 2662 | |
| 2663 | static inline void scrub_parity_mark_sectors_error(struct scrub_parity *sparity, |
| 2664 | u64 start, u64 len) |
| 2665 | { |
| 2666 | __scrub_mark_bitmap(sparity, sparity->ebitmap, start, len); |
| 2667 | } |
| 2668 | |
| 2669 | static inline void scrub_parity_mark_sectors_data(struct scrub_parity *sparity, |
| 2670 | u64 start, u64 len) |
| 2671 | { |
| 2672 | __scrub_mark_bitmap(sparity, sparity->dbitmap, start, len); |
| 2673 | } |
| 2674 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2675 | static void scrub_block_complete(struct scrub_block *sblock) |
| 2676 | { |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2677 | int corrupted = 0; |
| 2678 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2679 | if (!sblock->no_io_error_seen) { |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2680 | corrupted = 1; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2681 | scrub_handle_errored_block(sblock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2682 | } else { |
| 2683 | /* |
| 2684 | * if has checksum error, write via repair mechanism in |
| 2685 | * dev replace case, otherwise write here in dev replace |
| 2686 | * case. |
| 2687 | */ |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2688 | corrupted = scrub_checksum(sblock); |
| 2689 | if (!corrupted && sblock->sctx->is_dev_replace) |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2690 | scrub_write_block_to_dev_replace(sblock); |
| 2691 | } |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2692 | |
| 2693 | if (sblock->sparity && corrupted && !sblock->data_corrected) { |
| 2694 | u64 start = sblock->pagev[0]->logical; |
| 2695 | u64 end = sblock->pagev[sblock->page_count - 1]->logical + |
| 2696 | PAGE_SIZE; |
| 2697 | |
| 2698 | scrub_parity_mark_sectors_error(sblock->sparity, |
| 2699 | start, end - start); |
| 2700 | } |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2701 | } |
| 2702 | |
Zhao Lei | 3b5753e | 2015-08-24 22:03:02 +0800 | [diff] [blame] | 2703 | static int scrub_find_csum(struct scrub_ctx *sctx, u64 logical, u8 *csum) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2704 | { |
| 2705 | struct btrfs_ordered_sum *sum = NULL; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 2706 | unsigned long index; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2707 | unsigned long num_sectors; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2708 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2709 | while (!list_empty(&sctx->csum_list)) { |
| 2710 | sum = list_first_entry(&sctx->csum_list, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2711 | struct btrfs_ordered_sum, list); |
| 2712 | if (sum->bytenr > logical) |
| 2713 | return 0; |
| 2714 | if (sum->bytenr + sum->len > logical) |
| 2715 | break; |
| 2716 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2717 | ++sctx->stat.csum_discards; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2718 | list_del(&sum->list); |
| 2719 | kfree(sum); |
| 2720 | sum = NULL; |
| 2721 | } |
| 2722 | if (!sum) |
| 2723 | return 0; |
| 2724 | |
David Sterba | 1d1bf92 | 2017-03-31 18:02:48 +0200 | [diff] [blame] | 2725 | index = div_u64(logical - sum->bytenr, sctx->fs_info->sectorsize); |
| 2726 | ASSERT(index < UINT_MAX); |
| 2727 | |
David Sterba | 25cc122 | 2017-05-16 19:10:41 +0200 | [diff] [blame] | 2728 | num_sectors = sum->len / sctx->fs_info->sectorsize; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 2729 | memcpy(csum, sum->sums + index, sctx->csum_size); |
| 2730 | if (index == num_sectors - 1) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2731 | list_del(&sum->list); |
| 2732 | kfree(sum); |
| 2733 | } |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 2734 | return 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | /* scrub extent tries to collect up to 64 kB for each bio */ |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 2738 | static int scrub_extent(struct scrub_ctx *sctx, struct map_lookup *map, |
| 2739 | u64 logical, u64 len, |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 2740 | u64 physical, struct btrfs_device *dev, u64 flags, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2741 | u64 gen, int mirror_num, u64 physical_for_dev_replace) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2742 | { |
| 2743 | int ret; |
| 2744 | u8 csum[BTRFS_CSUM_SIZE]; |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2745 | u32 blocksize; |
| 2746 | |
| 2747 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 2748 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 2749 | blocksize = map->stripe_len; |
| 2750 | else |
| 2751 | blocksize = sctx->fs_info->sectorsize; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2752 | spin_lock(&sctx->stat_lock); |
| 2753 | sctx->stat.data_extents_scrubbed++; |
| 2754 | sctx->stat.data_bytes_scrubbed += len; |
| 2755 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2756 | } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 2757 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 2758 | blocksize = map->stripe_len; |
| 2759 | else |
| 2760 | blocksize = sctx->fs_info->nodesize; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2761 | spin_lock(&sctx->stat_lock); |
| 2762 | sctx->stat.tree_extents_scrubbed++; |
| 2763 | sctx->stat.tree_bytes_scrubbed += len; |
| 2764 | spin_unlock(&sctx->stat_lock); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2765 | } else { |
David Sterba | 25cc122 | 2017-05-16 19:10:41 +0200 | [diff] [blame] | 2766 | blocksize = sctx->fs_info->sectorsize; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2767 | WARN_ON(1); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2768 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2769 | |
| 2770 | while (len) { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 2771 | u64 l = min_t(u64, len, blocksize); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2772 | int have_csum = 0; |
| 2773 | |
| 2774 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
| 2775 | /* push csums to sbio */ |
Zhao Lei | 3b5753e | 2015-08-24 22:03:02 +0800 | [diff] [blame] | 2776 | have_csum = scrub_find_csum(sctx, logical, csum); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2777 | if (have_csum == 0) |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 2778 | ++sctx->stat.no_csum; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2779 | } |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 2780 | ret = scrub_pages(sctx, logical, l, physical, dev, flags, gen, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2781 | mirror_num, have_csum ? csum : NULL, 0, |
| 2782 | physical_for_dev_replace); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2783 | if (ret) |
| 2784 | return ret; |
| 2785 | len -= l; |
| 2786 | logical += l; |
| 2787 | physical += l; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 2788 | physical_for_dev_replace += l; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 2789 | } |
| 2790 | return 0; |
| 2791 | } |
| 2792 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2793 | static int scrub_pages_for_parity(struct scrub_parity *sparity, |
| 2794 | u64 logical, u64 len, |
| 2795 | u64 physical, struct btrfs_device *dev, |
| 2796 | u64 flags, u64 gen, int mirror_num, u8 *csum) |
| 2797 | { |
| 2798 | struct scrub_ctx *sctx = sparity->sctx; |
| 2799 | struct scrub_block *sblock; |
| 2800 | int index; |
| 2801 | |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 2802 | sblock = kzalloc(sizeof(*sblock), GFP_KERNEL); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2803 | if (!sblock) { |
| 2804 | spin_lock(&sctx->stat_lock); |
| 2805 | sctx->stat.malloc_errors++; |
| 2806 | spin_unlock(&sctx->stat_lock); |
| 2807 | return -ENOMEM; |
| 2808 | } |
| 2809 | |
| 2810 | /* one ref inside this function, plus one for each page added to |
| 2811 | * a bio later on */ |
Elena Reshetova | 186debd | 2017-03-03 10:55:23 +0200 | [diff] [blame] | 2812 | refcount_set(&sblock->refs, 1); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2813 | sblock->sctx = sctx; |
| 2814 | sblock->no_io_error_seen = 1; |
| 2815 | sblock->sparity = sparity; |
| 2816 | scrub_parity_get(sparity); |
| 2817 | |
| 2818 | for (index = 0; len > 0; index++) { |
| 2819 | struct scrub_page *spage; |
| 2820 | u64 l = min_t(u64, len, PAGE_SIZE); |
| 2821 | |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 2822 | spage = kzalloc(sizeof(*spage), GFP_KERNEL); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2823 | if (!spage) { |
| 2824 | leave_nomem: |
| 2825 | spin_lock(&sctx->stat_lock); |
| 2826 | sctx->stat.malloc_errors++; |
| 2827 | spin_unlock(&sctx->stat_lock); |
| 2828 | scrub_block_put(sblock); |
| 2829 | return -ENOMEM; |
| 2830 | } |
| 2831 | BUG_ON(index >= SCRUB_MAX_PAGES_PER_BLOCK); |
| 2832 | /* For scrub block */ |
| 2833 | scrub_page_get(spage); |
| 2834 | sblock->pagev[index] = spage; |
| 2835 | /* For scrub parity */ |
| 2836 | scrub_page_get(spage); |
| 2837 | list_add_tail(&spage->list, &sparity->spages); |
| 2838 | spage->sblock = sblock; |
| 2839 | spage->dev = dev; |
| 2840 | spage->flags = flags; |
| 2841 | spage->generation = gen; |
| 2842 | spage->logical = logical; |
| 2843 | spage->physical = physical; |
| 2844 | spage->mirror_num = mirror_num; |
| 2845 | if (csum) { |
| 2846 | spage->have_csum = 1; |
| 2847 | memcpy(spage->csum, csum, sctx->csum_size); |
| 2848 | } else { |
| 2849 | spage->have_csum = 0; |
| 2850 | } |
| 2851 | sblock->page_count++; |
David Sterba | 58c4e17 | 2016-02-11 10:49:42 +0100 | [diff] [blame] | 2852 | spage->page = alloc_page(GFP_KERNEL); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2853 | if (!spage->page) |
| 2854 | goto leave_nomem; |
| 2855 | len -= l; |
| 2856 | logical += l; |
| 2857 | physical += l; |
| 2858 | } |
| 2859 | |
| 2860 | WARN_ON(sblock->page_count == 0); |
| 2861 | for (index = 0; index < sblock->page_count; index++) { |
| 2862 | struct scrub_page *spage = sblock->pagev[index]; |
| 2863 | int ret; |
| 2864 | |
| 2865 | ret = scrub_add_page_to_rd_bio(sctx, spage); |
| 2866 | if (ret) { |
| 2867 | scrub_block_put(sblock); |
| 2868 | return ret; |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | /* last one frees, either here or in bio completion for last page */ |
| 2873 | scrub_block_put(sblock); |
| 2874 | return 0; |
| 2875 | } |
| 2876 | |
| 2877 | static int scrub_extent_for_parity(struct scrub_parity *sparity, |
| 2878 | u64 logical, u64 len, |
| 2879 | u64 physical, struct btrfs_device *dev, |
| 2880 | u64 flags, u64 gen, int mirror_num) |
| 2881 | { |
| 2882 | struct scrub_ctx *sctx = sparity->sctx; |
| 2883 | int ret; |
| 2884 | u8 csum[BTRFS_CSUM_SIZE]; |
| 2885 | u32 blocksize; |
| 2886 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2887 | if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) { |
Omar Sandoval | 4a77089 | 2015-06-19 11:52:52 -0700 | [diff] [blame] | 2888 | scrub_parity_mark_sectors_error(sparity, logical, len); |
| 2889 | return 0; |
| 2890 | } |
| 2891 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2892 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 2893 | blocksize = sparity->stripe_len; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2894 | } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 2895 | blocksize = sparity->stripe_len; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2896 | } else { |
David Sterba | 25cc122 | 2017-05-16 19:10:41 +0200 | [diff] [blame] | 2897 | blocksize = sctx->fs_info->sectorsize; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2898 | WARN_ON(1); |
| 2899 | } |
| 2900 | |
| 2901 | while (len) { |
| 2902 | u64 l = min_t(u64, len, blocksize); |
| 2903 | int have_csum = 0; |
| 2904 | |
| 2905 | if (flags & BTRFS_EXTENT_FLAG_DATA) { |
| 2906 | /* push csums to sbio */ |
Zhao Lei | 3b5753e | 2015-08-24 22:03:02 +0800 | [diff] [blame] | 2907 | have_csum = scrub_find_csum(sctx, logical, csum); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2908 | if (have_csum == 0) |
| 2909 | goto skip; |
| 2910 | } |
| 2911 | ret = scrub_pages_for_parity(sparity, logical, l, physical, dev, |
| 2912 | flags, gen, mirror_num, |
| 2913 | have_csum ? csum : NULL); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2914 | if (ret) |
| 2915 | return ret; |
Dan Carpenter | 6b6d24b | 2014-12-12 22:30:00 +0300 | [diff] [blame] | 2916 | skip: |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2917 | len -= l; |
| 2918 | logical += l; |
| 2919 | physical += l; |
| 2920 | } |
| 2921 | return 0; |
| 2922 | } |
| 2923 | |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 2924 | /* |
| 2925 | * Given a physical address, this will calculate it's |
| 2926 | * logical offset. if this is a parity stripe, it will return |
| 2927 | * the most left data stripe's logical offset. |
| 2928 | * |
| 2929 | * return 0 if it is a data stripe, 1 means parity stripe. |
| 2930 | */ |
| 2931 | static int get_raid56_logic_offset(u64 physical, int num, |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2932 | struct map_lookup *map, u64 *offset, |
| 2933 | u64 *stripe_start) |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 2934 | { |
| 2935 | int i; |
| 2936 | int j = 0; |
| 2937 | u64 stripe_nr; |
| 2938 | u64 last_offset; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 2939 | u32 stripe_index; |
| 2940 | u32 rot; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 2941 | |
| 2942 | last_offset = (physical - map->stripes[num].physical) * |
| 2943 | nr_data_stripes(map); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2944 | if (stripe_start) |
| 2945 | *stripe_start = last_offset; |
| 2946 | |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 2947 | *offset = last_offset; |
| 2948 | for (i = 0; i < nr_data_stripes(map); i++) { |
| 2949 | *offset = last_offset + i * map->stripe_len; |
| 2950 | |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 2951 | stripe_nr = div64_u64(*offset, map->stripe_len); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 2952 | stripe_nr = div_u64(stripe_nr, nr_data_stripes(map)); |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 2953 | |
| 2954 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 2955 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot); |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 2956 | /* calculate which stripe this data locates */ |
| 2957 | rot += i; |
Wang Shilong | e4fbaee | 2014-04-11 18:32:25 +0800 | [diff] [blame] | 2958 | stripe_index = rot % map->num_stripes; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 2959 | if (stripe_index == num) |
| 2960 | return 0; |
| 2961 | if (stripe_index < num) |
| 2962 | j++; |
| 2963 | } |
| 2964 | *offset = last_offset + j * map->stripe_len; |
| 2965 | return 1; |
| 2966 | } |
| 2967 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 2968 | static void scrub_free_parity(struct scrub_parity *sparity) |
| 2969 | { |
| 2970 | struct scrub_ctx *sctx = sparity->sctx; |
| 2971 | struct scrub_page *curr, *next; |
| 2972 | int nbits; |
| 2973 | |
| 2974 | nbits = bitmap_weight(sparity->ebitmap, sparity->nsectors); |
| 2975 | if (nbits) { |
| 2976 | spin_lock(&sctx->stat_lock); |
| 2977 | sctx->stat.read_errors += nbits; |
| 2978 | sctx->stat.uncorrectable_errors += nbits; |
| 2979 | spin_unlock(&sctx->stat_lock); |
| 2980 | } |
| 2981 | |
| 2982 | list_for_each_entry_safe(curr, next, &sparity->spages, list) { |
| 2983 | list_del_init(&curr->list); |
| 2984 | scrub_page_put(curr); |
| 2985 | } |
| 2986 | |
| 2987 | kfree(sparity); |
| 2988 | } |
| 2989 | |
Zhao Lei | 20b2e30 | 2015-06-04 20:09:15 +0800 | [diff] [blame] | 2990 | static void scrub_parity_bio_endio_worker(struct btrfs_work *work) |
| 2991 | { |
| 2992 | struct scrub_parity *sparity = container_of(work, struct scrub_parity, |
| 2993 | work); |
| 2994 | struct scrub_ctx *sctx = sparity->sctx; |
| 2995 | |
| 2996 | scrub_free_parity(sparity); |
| 2997 | scrub_pending_bio_dec(sctx); |
| 2998 | } |
| 2999 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3000 | static void scrub_parity_bio_endio(struct bio *bio) |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3001 | { |
| 3002 | struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3003 | struct btrfs_fs_info *fs_info = sparity->sctx->fs_info; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3004 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3005 | if (bio->bi_status) |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3006 | bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap, |
| 3007 | sparity->nsectors); |
| 3008 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3009 | bio_put(bio); |
Zhao Lei | 20b2e30 | 2015-06-04 20:09:15 +0800 | [diff] [blame] | 3010 | |
| 3011 | btrfs_init_work(&sparity->work, btrfs_scrubparity_helper, |
| 3012 | scrub_parity_bio_endio_worker, NULL, NULL); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3013 | btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | static void scrub_parity_check_and_repair(struct scrub_parity *sparity) |
| 3017 | { |
| 3018 | struct scrub_ctx *sctx = sparity->sctx; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3019 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3020 | struct bio *bio; |
| 3021 | struct btrfs_raid_bio *rbio; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3022 | struct btrfs_bio *bbio = NULL; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3023 | u64 length; |
| 3024 | int ret; |
| 3025 | |
| 3026 | if (!bitmap_andnot(sparity->dbitmap, sparity->dbitmap, sparity->ebitmap, |
| 3027 | sparity->nsectors)) |
| 3028 | goto out; |
| 3029 | |
Zhao Lei | a0dd59d | 2015-07-21 15:42:26 +0800 | [diff] [blame] | 3030 | length = sparity->logic_end - sparity->logic_start; |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 3031 | |
| 3032 | btrfs_bio_counter_inc_blocked(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3033 | ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 3034 | &length, &bbio); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 3035 | if (ret || !bbio || !bbio->raid_map) |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3036 | goto bbio_out; |
| 3037 | |
David Sterba | c5e4c3d | 2017-06-12 17:29:41 +0200 | [diff] [blame] | 3038 | bio = btrfs_io_bio_alloc(0); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3039 | bio->bi_iter.bi_sector = sparity->logic_start >> 9; |
| 3040 | bio->bi_private = sparity; |
| 3041 | bio->bi_end_io = scrub_parity_bio_endio; |
| 3042 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3043 | rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 3044 | length, sparity->scrub_dev, |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3045 | sparity->dbitmap, |
| 3046 | sparity->nsectors); |
| 3047 | if (!rbio) |
| 3048 | goto rbio_out; |
| 3049 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3050 | scrub_pending_bio_inc(sctx); |
| 3051 | raid56_parity_submit_scrub_rbio(rbio); |
| 3052 | return; |
| 3053 | |
| 3054 | rbio_out: |
| 3055 | bio_put(bio); |
| 3056 | bbio_out: |
Qu Wenruo | ae6529c | 2017-03-29 09:33:21 +0800 | [diff] [blame] | 3057 | btrfs_bio_counter_dec(fs_info); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 3058 | btrfs_put_bbio(bbio); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3059 | bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap, |
| 3060 | sparity->nsectors); |
| 3061 | spin_lock(&sctx->stat_lock); |
| 3062 | sctx->stat.malloc_errors++; |
| 3063 | spin_unlock(&sctx->stat_lock); |
| 3064 | out: |
| 3065 | scrub_free_parity(sparity); |
| 3066 | } |
| 3067 | |
| 3068 | static inline int scrub_calc_parity_bitmap_len(int nsectors) |
| 3069 | { |
Zhao Lei | bfca9a6 | 2014-12-08 19:55:57 +0800 | [diff] [blame] | 3070 | return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | static void scrub_parity_get(struct scrub_parity *sparity) |
| 3074 | { |
Elena Reshetova | 78a7645 | 2017-03-03 10:55:24 +0200 | [diff] [blame] | 3075 | refcount_inc(&sparity->refs); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | static void scrub_parity_put(struct scrub_parity *sparity) |
| 3079 | { |
Elena Reshetova | 78a7645 | 2017-03-03 10:55:24 +0200 | [diff] [blame] | 3080 | if (!refcount_dec_and_test(&sparity->refs)) |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3081 | return; |
| 3082 | |
| 3083 | scrub_parity_check_and_repair(sparity); |
| 3084 | } |
| 3085 | |
| 3086 | static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx, |
| 3087 | struct map_lookup *map, |
| 3088 | struct btrfs_device *sdev, |
| 3089 | struct btrfs_path *path, |
| 3090 | u64 logic_start, |
| 3091 | u64 logic_end) |
| 3092 | { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 3093 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3094 | struct btrfs_root *root = fs_info->extent_root; |
| 3095 | struct btrfs_root *csum_root = fs_info->csum_root; |
| 3096 | struct btrfs_extent_item *extent; |
Omar Sandoval | 4a77089 | 2015-06-19 11:52:52 -0700 | [diff] [blame] | 3097 | struct btrfs_bio *bbio = NULL; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3098 | u64 flags; |
| 3099 | int ret; |
| 3100 | int slot; |
| 3101 | struct extent_buffer *l; |
| 3102 | struct btrfs_key key; |
| 3103 | u64 generation; |
| 3104 | u64 extent_logical; |
| 3105 | u64 extent_physical; |
| 3106 | u64 extent_len; |
Omar Sandoval | 4a77089 | 2015-06-19 11:52:52 -0700 | [diff] [blame] | 3107 | u64 mapped_length; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3108 | struct btrfs_device *extent_dev; |
| 3109 | struct scrub_parity *sparity; |
| 3110 | int nsectors; |
| 3111 | int bitmap_len; |
| 3112 | int extent_mirror_num; |
| 3113 | int stop_loop = 0; |
| 3114 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3115 | nsectors = div_u64(map->stripe_len, fs_info->sectorsize); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3116 | bitmap_len = scrub_calc_parity_bitmap_len(nsectors); |
| 3117 | sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len, |
| 3118 | GFP_NOFS); |
| 3119 | if (!sparity) { |
| 3120 | spin_lock(&sctx->stat_lock); |
| 3121 | sctx->stat.malloc_errors++; |
| 3122 | spin_unlock(&sctx->stat_lock); |
| 3123 | return -ENOMEM; |
| 3124 | } |
| 3125 | |
| 3126 | sparity->stripe_len = map->stripe_len; |
| 3127 | sparity->nsectors = nsectors; |
| 3128 | sparity->sctx = sctx; |
| 3129 | sparity->scrub_dev = sdev; |
| 3130 | sparity->logic_start = logic_start; |
| 3131 | sparity->logic_end = logic_end; |
Elena Reshetova | 78a7645 | 2017-03-03 10:55:24 +0200 | [diff] [blame] | 3132 | refcount_set(&sparity->refs, 1); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3133 | INIT_LIST_HEAD(&sparity->spages); |
| 3134 | sparity->dbitmap = sparity->bitmap; |
| 3135 | sparity->ebitmap = (void *)sparity->bitmap + bitmap_len; |
| 3136 | |
| 3137 | ret = 0; |
| 3138 | while (logic_start < logic_end) { |
| 3139 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
| 3140 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 3141 | else |
| 3142 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3143 | key.objectid = logic_start; |
| 3144 | key.offset = (u64)-1; |
| 3145 | |
| 3146 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3147 | if (ret < 0) |
| 3148 | goto out; |
| 3149 | |
| 3150 | if (ret > 0) { |
| 3151 | ret = btrfs_previous_extent_item(root, path, 0); |
| 3152 | if (ret < 0) |
| 3153 | goto out; |
| 3154 | if (ret > 0) { |
| 3155 | btrfs_release_path(path); |
| 3156 | ret = btrfs_search_slot(NULL, root, &key, |
| 3157 | path, 0, 0); |
| 3158 | if (ret < 0) |
| 3159 | goto out; |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | stop_loop = 0; |
| 3164 | while (1) { |
| 3165 | u64 bytes; |
| 3166 | |
| 3167 | l = path->nodes[0]; |
| 3168 | slot = path->slots[0]; |
| 3169 | if (slot >= btrfs_header_nritems(l)) { |
| 3170 | ret = btrfs_next_leaf(root, path); |
| 3171 | if (ret == 0) |
| 3172 | continue; |
| 3173 | if (ret < 0) |
| 3174 | goto out; |
| 3175 | |
| 3176 | stop_loop = 1; |
| 3177 | break; |
| 3178 | } |
| 3179 | btrfs_item_key_to_cpu(l, &key, slot); |
| 3180 | |
Zhao Lei | d7cad23 | 2015-07-22 13:14:48 +0800 | [diff] [blame] | 3181 | if (key.type != BTRFS_EXTENT_ITEM_KEY && |
| 3182 | key.type != BTRFS_METADATA_ITEM_KEY) |
| 3183 | goto next; |
| 3184 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3185 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3186 | bytes = fs_info->nodesize; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3187 | else |
| 3188 | bytes = key.offset; |
| 3189 | |
| 3190 | if (key.objectid + bytes <= logic_start) |
| 3191 | goto next; |
| 3192 | |
Zhao Lei | a0dd59d | 2015-07-21 15:42:26 +0800 | [diff] [blame] | 3193 | if (key.objectid >= logic_end) { |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3194 | stop_loop = 1; |
| 3195 | break; |
| 3196 | } |
| 3197 | |
| 3198 | while (key.objectid >= logic_start + map->stripe_len) |
| 3199 | logic_start += map->stripe_len; |
| 3200 | |
| 3201 | extent = btrfs_item_ptr(l, slot, |
| 3202 | struct btrfs_extent_item); |
| 3203 | flags = btrfs_extent_flags(l, extent); |
| 3204 | generation = btrfs_extent_generation(l, extent); |
| 3205 | |
Zhao Lei | a323e81 | 2015-07-23 12:29:49 +0800 | [diff] [blame] | 3206 | if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) && |
| 3207 | (key.objectid < logic_start || |
| 3208 | key.objectid + bytes > |
| 3209 | logic_start + map->stripe_len)) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3210 | btrfs_err(fs_info, |
| 3211 | "scrub: tree block %llu spanning stripes, ignored. logical=%llu", |
Zhao Lei | a323e81 | 2015-07-23 12:29:49 +0800 | [diff] [blame] | 3212 | key.objectid, logic_start); |
Zhao Lei | 9799d2c3 | 2015-08-25 21:31:40 +0800 | [diff] [blame] | 3213 | spin_lock(&sctx->stat_lock); |
| 3214 | sctx->stat.uncorrectable_errors++; |
| 3215 | spin_unlock(&sctx->stat_lock); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3216 | goto next; |
| 3217 | } |
| 3218 | again: |
| 3219 | extent_logical = key.objectid; |
| 3220 | extent_len = bytes; |
| 3221 | |
| 3222 | if (extent_logical < logic_start) { |
| 3223 | extent_len -= logic_start - extent_logical; |
| 3224 | extent_logical = logic_start; |
| 3225 | } |
| 3226 | |
| 3227 | if (extent_logical + extent_len > |
| 3228 | logic_start + map->stripe_len) |
| 3229 | extent_len = logic_start + map->stripe_len - |
| 3230 | extent_logical; |
| 3231 | |
| 3232 | scrub_parity_mark_sectors_data(sparity, extent_logical, |
| 3233 | extent_len); |
| 3234 | |
Omar Sandoval | 4a77089 | 2015-06-19 11:52:52 -0700 | [diff] [blame] | 3235 | mapped_length = extent_len; |
Zhao Lei | f1fee65 | 2016-05-17 17:37:38 +0800 | [diff] [blame] | 3236 | bbio = NULL; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 3237 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, |
| 3238 | extent_logical, &mapped_length, &bbio, |
| 3239 | 0); |
Omar Sandoval | 4a77089 | 2015-06-19 11:52:52 -0700 | [diff] [blame] | 3240 | if (!ret) { |
| 3241 | if (!bbio || mapped_length < extent_len) |
| 3242 | ret = -EIO; |
| 3243 | } |
| 3244 | if (ret) { |
| 3245 | btrfs_put_bbio(bbio); |
| 3246 | goto out; |
| 3247 | } |
| 3248 | extent_physical = bbio->stripes[0].physical; |
| 3249 | extent_mirror_num = bbio->mirror_num; |
| 3250 | extent_dev = bbio->stripes[0].dev; |
| 3251 | btrfs_put_bbio(bbio); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3252 | |
| 3253 | ret = btrfs_lookup_csums_range(csum_root, |
| 3254 | extent_logical, |
| 3255 | extent_logical + extent_len - 1, |
| 3256 | &sctx->csum_list, 1); |
| 3257 | if (ret) |
| 3258 | goto out; |
| 3259 | |
| 3260 | ret = scrub_extent_for_parity(sparity, extent_logical, |
| 3261 | extent_len, |
| 3262 | extent_physical, |
| 3263 | extent_dev, flags, |
| 3264 | generation, |
| 3265 | extent_mirror_num); |
Zhao Lei | 6fa96d7 | 2015-07-21 12:22:30 +0800 | [diff] [blame] | 3266 | |
| 3267 | scrub_free_csums(sctx); |
| 3268 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3269 | if (ret) |
| 3270 | goto out; |
| 3271 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3272 | if (extent_logical + extent_len < |
| 3273 | key.objectid + bytes) { |
| 3274 | logic_start += map->stripe_len; |
| 3275 | |
| 3276 | if (logic_start >= logic_end) { |
| 3277 | stop_loop = 1; |
| 3278 | break; |
| 3279 | } |
| 3280 | |
| 3281 | if (logic_start < key.objectid + bytes) { |
| 3282 | cond_resched(); |
| 3283 | goto again; |
| 3284 | } |
| 3285 | } |
| 3286 | next: |
| 3287 | path->slots[0]++; |
| 3288 | } |
| 3289 | |
| 3290 | btrfs_release_path(path); |
| 3291 | |
| 3292 | if (stop_loop) |
| 3293 | break; |
| 3294 | |
| 3295 | logic_start += map->stripe_len; |
| 3296 | } |
| 3297 | out: |
| 3298 | if (ret < 0) |
| 3299 | scrub_parity_mark_sectors_error(sparity, logic_start, |
Zhao Lei | a0dd59d | 2015-07-21 15:42:26 +0800 | [diff] [blame] | 3300 | logic_end - logic_start); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3301 | scrub_parity_put(sparity); |
| 3302 | scrub_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3303 | mutex_lock(&sctx->wr_lock); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3304 | scrub_wr_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3305 | mutex_unlock(&sctx->wr_lock); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3306 | |
| 3307 | btrfs_release_path(path); |
| 3308 | return ret < 0 ? ret : 0; |
| 3309 | } |
| 3310 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3311 | static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3312 | struct map_lookup *map, |
| 3313 | struct btrfs_device *scrub_dev, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3314 | int num, u64 base, u64 length, |
| 3315 | int is_dev_replace) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3316 | { |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3317 | struct btrfs_path *path, *ppath; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 3318 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3319 | struct btrfs_root *root = fs_info->extent_root; |
| 3320 | struct btrfs_root *csum_root = fs_info->csum_root; |
| 3321 | struct btrfs_extent_item *extent; |
Arne Jansen | e7786c3 | 2011-05-28 20:58:38 +0000 | [diff] [blame] | 3322 | struct blk_plug plug; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3323 | u64 flags; |
| 3324 | int ret; |
| 3325 | int slot; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3326 | u64 nstripes; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3327 | struct extent_buffer *l; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3328 | u64 physical; |
| 3329 | u64 logical; |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3330 | u64 logic_end; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3331 | u64 physical_end; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3332 | u64 generation; |
Jan Schmidt | e12fa9c | 2011-06-17 15:55:21 +0200 | [diff] [blame] | 3333 | int mirror_num; |
Arne Jansen | 7a26285 | 2011-06-10 12:39:23 +0200 | [diff] [blame] | 3334 | struct reada_control *reada1; |
| 3335 | struct reada_control *reada2; |
David Sterba | e6c11f9 | 2016-03-24 18:00:53 +0100 | [diff] [blame] | 3336 | struct btrfs_key key; |
Arne Jansen | 7a26285 | 2011-06-10 12:39:23 +0200 | [diff] [blame] | 3337 | struct btrfs_key key_end; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3338 | u64 increment = map->stripe_len; |
| 3339 | u64 offset; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3340 | u64 extent_logical; |
| 3341 | u64 extent_physical; |
| 3342 | u64 extent_len; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3343 | u64 stripe_logical; |
| 3344 | u64 stripe_end; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3345 | struct btrfs_device *extent_dev; |
| 3346 | int extent_mirror_num; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3347 | int stop_loop = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3348 | |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3349 | physical = map->stripes[num].physical; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3350 | offset = 0; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 3351 | nstripes = div64_u64(length, map->stripe_len); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3352 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 3353 | offset = map->stripe_len * num; |
| 3354 | increment = map->stripe_len * map->num_stripes; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 3355 | mirror_num = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3356 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 3357 | int factor = map->num_stripes / map->sub_stripes; |
| 3358 | offset = map->stripe_len * (num / map->sub_stripes); |
| 3359 | increment = map->stripe_len * factor; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 3360 | mirror_num = num % map->sub_stripes + 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3361 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
| 3362 | increment = map->stripe_len; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 3363 | mirror_num = num % map->num_stripes + 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3364 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
| 3365 | increment = map->stripe_len; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 3366 | mirror_num = num % map->num_stripes + 1; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 3367 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3368 | get_raid56_logic_offset(physical, num, map, &offset, NULL); |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3369 | increment = map->stripe_len * nr_data_stripes(map); |
| 3370 | mirror_num = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3371 | } else { |
| 3372 | increment = map->stripe_len; |
Jan Schmidt | 193ea74 | 2011-06-13 19:56:54 +0200 | [diff] [blame] | 3373 | mirror_num = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | path = btrfs_alloc_path(); |
| 3377 | if (!path) |
| 3378 | return -ENOMEM; |
| 3379 | |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3380 | ppath = btrfs_alloc_path(); |
| 3381 | if (!ppath) { |
Tsutomu Itoh | 379d685 | 2015-01-09 17:37:52 +0900 | [diff] [blame] | 3382 | btrfs_free_path(path); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3383 | return -ENOMEM; |
| 3384 | } |
| 3385 | |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 3386 | /* |
| 3387 | * work on commit root. The related disk blocks are static as |
| 3388 | * long as COW is applied. This means, it is save to rewrite |
| 3389 | * them to repair disk errors without any race conditions |
| 3390 | */ |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3391 | path->search_commit_root = 1; |
| 3392 | path->skip_locking = 1; |
| 3393 | |
Gui Hecheng | 063c54d | 2015-01-09 09:39:40 +0800 | [diff] [blame] | 3394 | ppath->search_commit_root = 1; |
| 3395 | ppath->skip_locking = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3396 | /* |
Arne Jansen | 7a26285 | 2011-06-10 12:39:23 +0200 | [diff] [blame] | 3397 | * trigger the readahead for extent tree csum tree and wait for |
| 3398 | * completion. During readahead, the scrub is officially paused |
| 3399 | * to not hold off transaction commits |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3400 | */ |
| 3401 | logical = base + offset; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3402 | physical_end = physical + nstripes * map->stripe_len; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 3403 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3404 | get_raid56_logic_offset(physical_end, num, |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3405 | map, &logic_end, NULL); |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3406 | logic_end += base; |
| 3407 | } else { |
| 3408 | logic_end = logical + increment * nstripes; |
| 3409 | } |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3410 | wait_event(sctx->list_wait, |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 3411 | atomic_read(&sctx->bios_in_flight) == 0); |
Wang Shilong | cb7ab02 | 2013-12-04 21:16:53 +0800 | [diff] [blame] | 3412 | scrub_blocked_if_needed(fs_info); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3413 | |
Arne Jansen | 7a26285 | 2011-06-10 12:39:23 +0200 | [diff] [blame] | 3414 | /* FIXME it might be better to start readahead at commit root */ |
David Sterba | e6c11f9 | 2016-03-24 18:00:53 +0100 | [diff] [blame] | 3415 | key.objectid = logical; |
| 3416 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3417 | key.offset = (u64)0; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3418 | key_end.objectid = logic_end; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3419 | key_end.type = BTRFS_METADATA_ITEM_KEY; |
| 3420 | key_end.offset = (u64)-1; |
David Sterba | e6c11f9 | 2016-03-24 18:00:53 +0100 | [diff] [blame] | 3421 | reada1 = btrfs_reada_add(root, &key, &key_end); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3422 | |
David Sterba | e6c11f9 | 2016-03-24 18:00:53 +0100 | [diff] [blame] | 3423 | key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 3424 | key.type = BTRFS_EXTENT_CSUM_KEY; |
| 3425 | key.offset = logical; |
Arne Jansen | 7a26285 | 2011-06-10 12:39:23 +0200 | [diff] [blame] | 3426 | key_end.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 3427 | key_end.type = BTRFS_EXTENT_CSUM_KEY; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3428 | key_end.offset = logic_end; |
David Sterba | e6c11f9 | 2016-03-24 18:00:53 +0100 | [diff] [blame] | 3429 | reada2 = btrfs_reada_add(csum_root, &key, &key_end); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3430 | |
Arne Jansen | 7a26285 | 2011-06-10 12:39:23 +0200 | [diff] [blame] | 3431 | if (!IS_ERR(reada1)) |
| 3432 | btrfs_reada_wait(reada1); |
| 3433 | if (!IS_ERR(reada2)) |
| 3434 | btrfs_reada_wait(reada2); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3435 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3436 | |
| 3437 | /* |
| 3438 | * collect all data csums for the stripe to avoid seeking during |
| 3439 | * the scrub. This might currently (crc32) end up to be about 1MB |
| 3440 | */ |
Arne Jansen | e7786c3 | 2011-05-28 20:58:38 +0000 | [diff] [blame] | 3441 | blk_start_plug(&plug); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3442 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3443 | /* |
| 3444 | * now find all extents for each stripe and scrub them |
| 3445 | */ |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3446 | ret = 0; |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3447 | while (physical < physical_end) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3448 | /* |
| 3449 | * canceled? |
| 3450 | */ |
| 3451 | if (atomic_read(&fs_info->scrub_cancel_req) || |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3452 | atomic_read(&sctx->cancel_req)) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3453 | ret = -ECANCELED; |
| 3454 | goto out; |
| 3455 | } |
| 3456 | /* |
| 3457 | * check to see if we have to pause |
| 3458 | */ |
| 3459 | if (atomic_read(&fs_info->scrub_pause_req)) { |
| 3460 | /* push queued extents */ |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 3461 | sctx->flush_all_writes = true; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3462 | scrub_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3463 | mutex_lock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3464 | scrub_wr_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3465 | mutex_unlock(&sctx->wr_lock); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3466 | wait_event(sctx->list_wait, |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 3467 | atomic_read(&sctx->bios_in_flight) == 0); |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 3468 | sctx->flush_all_writes = false; |
Wang Shilong | 3cb0929 | 2013-12-04 21:15:19 +0800 | [diff] [blame] | 3469 | scrub_blocked_if_needed(fs_info); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3470 | } |
| 3471 | |
Zhao Lei | f2f66a2 | 2015-07-21 12:22:29 +0800 | [diff] [blame] | 3472 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 3473 | ret = get_raid56_logic_offset(physical, num, map, |
| 3474 | &logical, |
| 3475 | &stripe_logical); |
| 3476 | logical += base; |
| 3477 | if (ret) { |
Zhao Lei | 7955323 | 2015-08-18 17:54:30 +0800 | [diff] [blame] | 3478 | /* it is parity strip */ |
Zhao Lei | f2f66a2 | 2015-07-21 12:22:29 +0800 | [diff] [blame] | 3479 | stripe_logical += base; |
Zhao Lei | a0dd59d | 2015-07-21 15:42:26 +0800 | [diff] [blame] | 3480 | stripe_end = stripe_logical + increment; |
Zhao Lei | f2f66a2 | 2015-07-21 12:22:29 +0800 | [diff] [blame] | 3481 | ret = scrub_raid56_parity(sctx, map, scrub_dev, |
| 3482 | ppath, stripe_logical, |
| 3483 | stripe_end); |
| 3484 | if (ret) |
| 3485 | goto out; |
| 3486 | goto skip; |
| 3487 | } |
| 3488 | } |
| 3489 | |
Wang Shilong | 7c76edb | 2014-01-12 21:38:32 +0800 | [diff] [blame] | 3490 | if (btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
| 3491 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 3492 | else |
| 3493 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3494 | key.objectid = logical; |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3495 | key.offset = (u64)-1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3496 | |
| 3497 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3498 | if (ret < 0) |
| 3499 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3500 | |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 3501 | if (ret > 0) { |
Wang Shilong | ade2e0b | 2014-01-12 21:38:33 +0800 | [diff] [blame] | 3502 | ret = btrfs_previous_extent_item(root, path, 0); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3503 | if (ret < 0) |
| 3504 | goto out; |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 3505 | if (ret > 0) { |
| 3506 | /* there's no smaller item, so stick with the |
| 3507 | * larger one */ |
| 3508 | btrfs_release_path(path); |
| 3509 | ret = btrfs_search_slot(NULL, root, &key, |
| 3510 | path, 0, 0); |
| 3511 | if (ret < 0) |
| 3512 | goto out; |
| 3513 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3514 | } |
| 3515 | |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3516 | stop_loop = 0; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3517 | while (1) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3518 | u64 bytes; |
| 3519 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3520 | l = path->nodes[0]; |
| 3521 | slot = path->slots[0]; |
| 3522 | if (slot >= btrfs_header_nritems(l)) { |
| 3523 | ret = btrfs_next_leaf(root, path); |
| 3524 | if (ret == 0) |
| 3525 | continue; |
| 3526 | if (ret < 0) |
| 3527 | goto out; |
| 3528 | |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3529 | stop_loop = 1; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3530 | break; |
| 3531 | } |
| 3532 | btrfs_item_key_to_cpu(l, &key, slot); |
| 3533 | |
Zhao Lei | d7cad23 | 2015-07-22 13:14:48 +0800 | [diff] [blame] | 3534 | if (key.type != BTRFS_EXTENT_ITEM_KEY && |
| 3535 | key.type != BTRFS_METADATA_ITEM_KEY) |
| 3536 | goto next; |
| 3537 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3538 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3539 | bytes = fs_info->nodesize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 3540 | else |
| 3541 | bytes = key.offset; |
| 3542 | |
| 3543 | if (key.objectid + bytes <= logical) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3544 | goto next; |
| 3545 | |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3546 | if (key.objectid >= logical + map->stripe_len) { |
| 3547 | /* out of this device extent */ |
| 3548 | if (key.objectid >= logic_end) |
| 3549 | stop_loop = 1; |
| 3550 | break; |
| 3551 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3552 | |
| 3553 | extent = btrfs_item_ptr(l, slot, |
| 3554 | struct btrfs_extent_item); |
| 3555 | flags = btrfs_extent_flags(l, extent); |
| 3556 | generation = btrfs_extent_generation(l, extent); |
| 3557 | |
Zhao Lei | a323e81 | 2015-07-23 12:29:49 +0800 | [diff] [blame] | 3558 | if ((flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) && |
| 3559 | (key.objectid < logical || |
| 3560 | key.objectid + bytes > |
| 3561 | logical + map->stripe_len)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3562 | btrfs_err(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3563 | "scrub: tree block %llu spanning stripes, ignored. logical=%llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3564 | key.objectid, logical); |
Zhao Lei | 9799d2c3 | 2015-08-25 21:31:40 +0800 | [diff] [blame] | 3565 | spin_lock(&sctx->stat_lock); |
| 3566 | sctx->stat.uncorrectable_errors++; |
| 3567 | spin_unlock(&sctx->stat_lock); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3568 | goto next; |
| 3569 | } |
| 3570 | |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3571 | again: |
| 3572 | extent_logical = key.objectid; |
| 3573 | extent_len = bytes; |
| 3574 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3575 | /* |
| 3576 | * trim extent to this stripe |
| 3577 | */ |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3578 | if (extent_logical < logical) { |
| 3579 | extent_len -= logical - extent_logical; |
| 3580 | extent_logical = logical; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3581 | } |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3582 | if (extent_logical + extent_len > |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3583 | logical + map->stripe_len) { |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3584 | extent_len = logical + map->stripe_len - |
| 3585 | extent_logical; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3586 | } |
| 3587 | |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3588 | extent_physical = extent_logical - logical + physical; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3589 | extent_dev = scrub_dev; |
| 3590 | extent_mirror_num = mirror_num; |
| 3591 | if (is_dev_replace) |
| 3592 | scrub_remap_extent(fs_info, extent_logical, |
| 3593 | extent_len, &extent_physical, |
| 3594 | &extent_dev, |
| 3595 | &extent_mirror_num); |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3596 | |
Zhao Lei | fe8cf65 | 2015-07-22 13:14:47 +0800 | [diff] [blame] | 3597 | ret = btrfs_lookup_csums_range(csum_root, |
| 3598 | extent_logical, |
| 3599 | extent_logical + |
| 3600 | extent_len - 1, |
| 3601 | &sctx->csum_list, 1); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3602 | if (ret) |
| 3603 | goto out; |
| 3604 | |
Liu Bo | 6ca1765 | 2018-03-07 12:08:09 -0700 | [diff] [blame] | 3605 | ret = scrub_extent(sctx, map, extent_logical, extent_len, |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3606 | extent_physical, extent_dev, flags, |
| 3607 | generation, extent_mirror_num, |
Stefan Behrens | 115930c | 2013-07-04 16:14:23 +0200 | [diff] [blame] | 3608 | extent_logical - logical + physical); |
Zhao Lei | 6fa96d7 | 2015-07-21 12:22:30 +0800 | [diff] [blame] | 3609 | |
| 3610 | scrub_free_csums(sctx); |
| 3611 | |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3612 | if (ret) |
| 3613 | goto out; |
| 3614 | |
| 3615 | if (extent_logical + extent_len < |
| 3616 | key.objectid + bytes) { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 3617 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3618 | /* |
| 3619 | * loop until we find next data stripe |
| 3620 | * or we have finished all stripes. |
| 3621 | */ |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3622 | loop: |
| 3623 | physical += map->stripe_len; |
| 3624 | ret = get_raid56_logic_offset(physical, |
| 3625 | num, map, &logical, |
| 3626 | &stripe_logical); |
| 3627 | logical += base; |
| 3628 | |
| 3629 | if (ret && physical < physical_end) { |
| 3630 | stripe_logical += base; |
| 3631 | stripe_end = stripe_logical + |
Zhao Lei | a0dd59d | 2015-07-21 15:42:26 +0800 | [diff] [blame] | 3632 | increment; |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3633 | ret = scrub_raid56_parity(sctx, |
| 3634 | map, scrub_dev, ppath, |
| 3635 | stripe_logical, |
| 3636 | stripe_end); |
| 3637 | if (ret) |
| 3638 | goto out; |
| 3639 | goto loop; |
| 3640 | } |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3641 | } else { |
| 3642 | physical += map->stripe_len; |
| 3643 | logical += increment; |
| 3644 | } |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3645 | if (logical < key.objectid + bytes) { |
| 3646 | cond_resched(); |
| 3647 | goto again; |
| 3648 | } |
| 3649 | |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3650 | if (physical >= physical_end) { |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3651 | stop_loop = 1; |
| 3652 | break; |
| 3653 | } |
| 3654 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3655 | next: |
| 3656 | path->slots[0]++; |
| 3657 | } |
Chris Mason | 7126733 | 2011-05-23 06:30:52 -0400 | [diff] [blame] | 3658 | btrfs_release_path(path); |
Wang Shilong | 3b080b2 | 2014-04-01 18:01:43 +0800 | [diff] [blame] | 3659 | skip: |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3660 | logical += increment; |
| 3661 | physical += map->stripe_len; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3662 | spin_lock(&sctx->stat_lock); |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3663 | if (stop_loop) |
| 3664 | sctx->stat.last_physical = map->stripes[num].physical + |
| 3665 | length; |
| 3666 | else |
| 3667 | sctx->stat.last_physical = physical; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3668 | spin_unlock(&sctx->stat_lock); |
Liu Bo | 625f1c8d | 2013-04-27 02:56:57 +0000 | [diff] [blame] | 3669 | if (stop_loop) |
| 3670 | break; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3671 | } |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3672 | out: |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3673 | /* push queued extents */ |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3674 | scrub_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3675 | mutex_lock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3676 | scrub_wr_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3677 | mutex_unlock(&sctx->wr_lock); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3678 | |
Arne Jansen | e7786c3 | 2011-05-28 20:58:38 +0000 | [diff] [blame] | 3679 | blk_finish_plug(&plug); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3680 | btrfs_free_path(path); |
Miao Xie | 5a6ac9e | 2014-11-06 17:20:58 +0800 | [diff] [blame] | 3681 | btrfs_free_path(ppath); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3682 | return ret < 0 ? ret : 0; |
| 3683 | } |
| 3684 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 3685 | static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx, |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3686 | struct btrfs_device *scrub_dev, |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3687 | u64 chunk_offset, u64 length, |
Filipe Manana | 020d5b7 | 2015-11-19 10:57:20 +0000 | [diff] [blame] | 3688 | u64 dev_offset, |
| 3689 | struct btrfs_block_group_cache *cache, |
| 3690 | int is_dev_replace) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3691 | { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 3692 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
| 3693 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3694 | struct map_lookup *map; |
| 3695 | struct extent_map *em; |
| 3696 | int i; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3697 | int ret = 0; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3698 | |
| 3699 | read_lock(&map_tree->map_tree.lock); |
| 3700 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
| 3701 | read_unlock(&map_tree->map_tree.lock); |
| 3702 | |
Filipe Manana | 020d5b7 | 2015-11-19 10:57:20 +0000 | [diff] [blame] | 3703 | if (!em) { |
| 3704 | /* |
| 3705 | * Might have been an unused block group deleted by the cleaner |
| 3706 | * kthread or relocation. |
| 3707 | */ |
| 3708 | spin_lock(&cache->lock); |
| 3709 | if (!cache->removed) |
| 3710 | ret = -EINVAL; |
| 3711 | spin_unlock(&cache->lock); |
| 3712 | |
| 3713 | return ret; |
| 3714 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3715 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 3716 | map = em->map_lookup; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3717 | if (em->start != chunk_offset) |
| 3718 | goto out; |
| 3719 | |
| 3720 | if (em->len < length) |
| 3721 | goto out; |
| 3722 | |
| 3723 | for (i = 0; i < map->num_stripes; ++i) { |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3724 | if (map->stripes[i].dev->bdev == scrub_dev->bdev && |
Arne Jansen | 859acaf | 2012-02-09 15:09:02 +0100 | [diff] [blame] | 3725 | map->stripes[i].physical == dev_offset) { |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3726 | ret = scrub_stripe(sctx, map, scrub_dev, i, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3727 | chunk_offset, length, |
| 3728 | is_dev_replace); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3729 | if (ret) |
| 3730 | goto out; |
| 3731 | } |
| 3732 | } |
| 3733 | out: |
| 3734 | free_extent_map(em); |
| 3735 | |
| 3736 | return ret; |
| 3737 | } |
| 3738 | |
| 3739 | static noinline_for_stack |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3740 | int scrub_enumerate_chunks(struct scrub_ctx *sctx, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3741 | struct btrfs_device *scrub_dev, u64 start, u64 end, |
| 3742 | int is_dev_replace) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3743 | { |
| 3744 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3745 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3746 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
| 3747 | struct btrfs_root *root = fs_info->dev_root; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3748 | u64 length; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3749 | u64 chunk_offset; |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 3750 | int ret = 0; |
Zhaolei | 76a8efa | 2015-11-17 18:46:17 +0800 | [diff] [blame] | 3751 | int ro_set; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3752 | int slot; |
| 3753 | struct extent_buffer *l; |
| 3754 | struct btrfs_key key; |
| 3755 | struct btrfs_key found_key; |
| 3756 | struct btrfs_block_group_cache *cache; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3757 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3758 | |
| 3759 | path = btrfs_alloc_path(); |
| 3760 | if (!path) |
| 3761 | return -ENOMEM; |
| 3762 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 3763 | path->reada = READA_FORWARD; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3764 | path->search_commit_root = 1; |
| 3765 | path->skip_locking = 1; |
| 3766 | |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3767 | key.objectid = scrub_dev->devid; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3768 | key.offset = 0ull; |
| 3769 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3770 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3771 | while (1) { |
| 3772 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3773 | if (ret < 0) |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 3774 | break; |
| 3775 | if (ret > 0) { |
| 3776 | if (path->slots[0] >= |
| 3777 | btrfs_header_nritems(path->nodes[0])) { |
| 3778 | ret = btrfs_next_leaf(root, path); |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 3779 | if (ret < 0) |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 3780 | break; |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 3781 | if (ret > 0) { |
| 3782 | ret = 0; |
| 3783 | break; |
| 3784 | } |
| 3785 | } else { |
| 3786 | ret = 0; |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 3787 | } |
| 3788 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3789 | |
| 3790 | l = path->nodes[0]; |
| 3791 | slot = path->slots[0]; |
| 3792 | |
| 3793 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 3794 | |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3795 | if (found_key.objectid != scrub_dev->devid) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3796 | break; |
| 3797 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 3798 | if (found_key.type != BTRFS_DEV_EXTENT_KEY) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3799 | break; |
| 3800 | |
| 3801 | if (found_key.offset >= end) |
| 3802 | break; |
| 3803 | |
| 3804 | if (found_key.offset < key.offset) |
| 3805 | break; |
| 3806 | |
| 3807 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 3808 | length = btrfs_dev_extent_length(l, dev_extent); |
| 3809 | |
Qu Wenruo | ced96ed | 2014-06-19 10:42:51 +0800 | [diff] [blame] | 3810 | if (found_key.offset + length <= start) |
| 3811 | goto skip; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3812 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3813 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
| 3814 | |
| 3815 | /* |
| 3816 | * get a reference on the corresponding block group to prevent |
| 3817 | * the chunk from going away while we scrub it |
| 3818 | */ |
| 3819 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
Qu Wenruo | ced96ed | 2014-06-19 10:42:51 +0800 | [diff] [blame] | 3820 | |
| 3821 | /* some chunks are removed but not committed to disk yet, |
| 3822 | * continue scrubbing */ |
| 3823 | if (!cache) |
| 3824 | goto skip; |
| 3825 | |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 3826 | /* |
| 3827 | * we need call btrfs_inc_block_group_ro() with scrubs_paused, |
| 3828 | * to avoid deadlock caused by: |
| 3829 | * btrfs_inc_block_group_ro() |
| 3830 | * -> btrfs_wait_for_commit() |
| 3831 | * -> btrfs_commit_transaction() |
| 3832 | * -> btrfs_scrub_pause() |
| 3833 | */ |
| 3834 | scrub_pause_on(fs_info); |
Nikolay Borisov | c83488a | 2018-06-20 15:49:14 +0300 | [diff] [blame] | 3835 | ret = btrfs_inc_block_group_ro(cache); |
Filipe Manana | f0e9b7d | 2016-05-14 09:12:53 +0100 | [diff] [blame] | 3836 | if (!ret && is_dev_replace) { |
| 3837 | /* |
| 3838 | * If we are doing a device replace wait for any tasks |
| 3839 | * that started dellaloc right before we set the block |
| 3840 | * group to RO mode, as they might have just allocated |
| 3841 | * an extent from it or decided they could do a nocow |
| 3842 | * write. And if any such tasks did that, wait for their |
| 3843 | * ordered extents to complete and then commit the |
| 3844 | * current transaction, so that we can later see the new |
| 3845 | * extent items in the extent tree - the ordered extents |
| 3846 | * create delayed data references (for cow writes) when |
| 3847 | * they complete, which will be run and insert the |
| 3848 | * corresponding extent items into the extent tree when |
| 3849 | * we commit the transaction they used when running |
| 3850 | * inode.c:btrfs_finish_ordered_io(). We later use |
| 3851 | * the commit root of the extent tree to find extents |
| 3852 | * to copy from the srcdev into the tgtdev, and we don't |
| 3853 | * want to miss any new extents. |
| 3854 | */ |
| 3855 | btrfs_wait_block_group_reservations(cache); |
| 3856 | btrfs_wait_nocow_writers(cache); |
Chris Mason | 6374e57a | 2017-06-23 09:48:21 -0700 | [diff] [blame] | 3857 | ret = btrfs_wait_ordered_roots(fs_info, U64_MAX, |
Filipe Manana | f0e9b7d | 2016-05-14 09:12:53 +0100 | [diff] [blame] | 3858 | cache->key.objectid, |
| 3859 | cache->key.offset); |
| 3860 | if (ret > 0) { |
| 3861 | struct btrfs_trans_handle *trans; |
| 3862 | |
| 3863 | trans = btrfs_join_transaction(root); |
| 3864 | if (IS_ERR(trans)) |
| 3865 | ret = PTR_ERR(trans); |
| 3866 | else |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3867 | ret = btrfs_commit_transaction(trans); |
Filipe Manana | f0e9b7d | 2016-05-14 09:12:53 +0100 | [diff] [blame] | 3868 | if (ret) { |
| 3869 | scrub_pause_off(fs_info); |
| 3870 | btrfs_put_block_group(cache); |
| 3871 | break; |
| 3872 | } |
| 3873 | } |
| 3874 | } |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 3875 | scrub_pause_off(fs_info); |
Zhaolei | 76a8efa | 2015-11-17 18:46:17 +0800 | [diff] [blame] | 3876 | |
| 3877 | if (ret == 0) { |
| 3878 | ro_set = 1; |
| 3879 | } else if (ret == -ENOSPC) { |
| 3880 | /* |
| 3881 | * btrfs_inc_block_group_ro return -ENOSPC when it |
| 3882 | * failed in creating new chunk for metadata. |
| 3883 | * It is not a problem for scrub/replace, because |
| 3884 | * metadata are always cowed, and our scrub paused |
| 3885 | * commit_transactions. |
| 3886 | */ |
| 3887 | ro_set = 0; |
| 3888 | } else { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3889 | btrfs_warn(fs_info, |
David Sterba | 913e153 | 2017-07-13 15:32:18 +0200 | [diff] [blame] | 3890 | "failed setting block group ro: %d", ret); |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 3891 | btrfs_put_block_group(cache); |
| 3892 | break; |
| 3893 | } |
| 3894 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 3895 | btrfs_dev_replace_write_lock(&fs_info->dev_replace); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3896 | dev_replace->cursor_right = found_key.offset + length; |
| 3897 | dev_replace->cursor_left = found_key.offset; |
| 3898 | dev_replace->item_needs_writeback = 1; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 3899 | btrfs_dev_replace_write_unlock(&fs_info->dev_replace); |
Zhao Lei | 8c204c9 | 2015-08-19 15:02:40 +0800 | [diff] [blame] | 3900 | ret = scrub_chunk(sctx, scrub_dev, chunk_offset, length, |
Filipe Manana | 020d5b7 | 2015-11-19 10:57:20 +0000 | [diff] [blame] | 3901 | found_key.offset, cache, is_dev_replace); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3902 | |
| 3903 | /* |
| 3904 | * flush, submit all pending read and write bios, afterwards |
| 3905 | * wait for them. |
| 3906 | * Note that in the dev replace case, a read request causes |
| 3907 | * write requests that are submitted in the read completion |
| 3908 | * worker. Therefore in the current situation, it is required |
| 3909 | * that all write requests are flushed, so that all read and |
| 3910 | * write requests are really completed when bios_in_flight |
| 3911 | * changes to 0. |
| 3912 | */ |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 3913 | sctx->flush_all_writes = true; |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3914 | scrub_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3915 | mutex_lock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3916 | scrub_wr_submit(sctx); |
David Sterba | 3fb9930 | 2017-05-16 19:10:32 +0200 | [diff] [blame] | 3917 | mutex_unlock(&sctx->wr_lock); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3918 | |
| 3919 | wait_event(sctx->list_wait, |
| 3920 | atomic_read(&sctx->bios_in_flight) == 0); |
Zhaolei | b708ce9 | 2015-08-05 16:43:29 +0800 | [diff] [blame] | 3921 | |
| 3922 | scrub_pause_on(fs_info); |
Wang Shilong | 12cf937 | 2014-02-19 19:24:17 +0800 | [diff] [blame] | 3923 | |
| 3924 | /* |
| 3925 | * must be called before we decrease @scrub_paused. |
| 3926 | * make sure we don't block transaction commit while |
| 3927 | * we are waiting pending workers finished. |
| 3928 | */ |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3929 | wait_event(sctx->list_wait, |
| 3930 | atomic_read(&sctx->workers_pending) == 0); |
David Sterba | 2073c4c | 2017-03-31 17:12:51 +0200 | [diff] [blame] | 3931 | sctx->flush_all_writes = false; |
Wang Shilong | 12cf937 | 2014-02-19 19:24:17 +0800 | [diff] [blame] | 3932 | |
Zhaolei | b708ce9 | 2015-08-05 16:43:29 +0800 | [diff] [blame] | 3933 | scrub_pause_off(fs_info); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3934 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 3935 | btrfs_dev_replace_write_lock(&fs_info->dev_replace); |
Filipe Manana | 1a1a8b7 | 2016-05-14 19:44:40 +0100 | [diff] [blame] | 3936 | dev_replace->cursor_left = dev_replace->cursor_right; |
| 3937 | dev_replace->item_needs_writeback = 1; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 3938 | btrfs_dev_replace_write_unlock(&fs_info->dev_replace); |
Filipe Manana | 1a1a8b7 | 2016-05-14 19:44:40 +0100 | [diff] [blame] | 3939 | |
Zhaolei | 76a8efa | 2015-11-17 18:46:17 +0800 | [diff] [blame] | 3940 | if (ro_set) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3941 | btrfs_dec_block_group_ro(cache); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3942 | |
Filipe Manana | 758f2df | 2015-11-19 11:45:48 +0000 | [diff] [blame] | 3943 | /* |
| 3944 | * We might have prevented the cleaner kthread from deleting |
| 3945 | * this block group if it was already unused because we raced |
| 3946 | * and set it to RO mode first. So add it back to the unused |
| 3947 | * list, otherwise it might not ever be deleted unless a manual |
| 3948 | * balance is triggered or it becomes used and unused again. |
| 3949 | */ |
| 3950 | spin_lock(&cache->lock); |
| 3951 | if (!cache->removed && !cache->ro && cache->reserved == 0 && |
| 3952 | btrfs_block_group_used(&cache->item) == 0) { |
| 3953 | spin_unlock(&cache->lock); |
Qu Wenruo | 031f24d | 2018-05-22 16:43:47 +0800 | [diff] [blame^] | 3954 | btrfs_mark_bg_unused(cache); |
Filipe Manana | 758f2df | 2015-11-19 11:45:48 +0000 | [diff] [blame] | 3955 | } else { |
| 3956 | spin_unlock(&cache->lock); |
| 3957 | } |
| 3958 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3959 | btrfs_put_block_group(cache); |
| 3960 | if (ret) |
| 3961 | break; |
Stefan Behrens | af1be4f | 2012-11-27 17:39:51 +0000 | [diff] [blame] | 3962 | if (is_dev_replace && |
| 3963 | atomic64_read(&dev_replace->num_write_errors) > 0) { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 3964 | ret = -EIO; |
| 3965 | break; |
| 3966 | } |
| 3967 | if (sctx->stat.malloc_errors > 0) { |
| 3968 | ret = -ENOMEM; |
| 3969 | break; |
| 3970 | } |
Qu Wenruo | ced96ed | 2014-06-19 10:42:51 +0800 | [diff] [blame] | 3971 | skip: |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3972 | key.offset = found_key.offset + length; |
Chris Mason | 7126733 | 2011-05-23 06:30:52 -0400 | [diff] [blame] | 3973 | btrfs_release_path(path); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3974 | } |
| 3975 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3976 | btrfs_free_path(path); |
Arne Jansen | 8c51032 | 2011-06-03 10:09:26 +0200 | [diff] [blame] | 3977 | |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 3978 | return ret; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3979 | } |
| 3980 | |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 3981 | static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx, |
| 3982 | struct btrfs_device *scrub_dev) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3983 | { |
| 3984 | int i; |
| 3985 | u64 bytenr; |
| 3986 | u64 gen; |
| 3987 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3988 | struct btrfs_fs_info *fs_info = sctx->fs_info; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3989 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3990 | if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3991 | return -EIO; |
| 3992 | |
Miao Xie | 5f54606 | 2014-07-24 11:37:09 +0800 | [diff] [blame] | 3993 | /* Seed devices of a new filesystem has their own generation. */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3994 | if (scrub_dev->fs_devices != fs_info->fs_devices) |
Miao Xie | 5f54606 | 2014-07-24 11:37:09 +0800 | [diff] [blame] | 3995 | gen = scrub_dev->generation; |
| 3996 | else |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3997 | gen = fs_info->last_trans_committed; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 3998 | |
| 3999 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 4000 | bytenr = btrfs_sb_offset(i); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4001 | if (bytenr + BTRFS_SUPER_INFO_SIZE > |
| 4002 | scrub_dev->commit_total_bytes) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4003 | break; |
| 4004 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4005 | ret = scrub_pages(sctx, bytenr, BTRFS_SUPER_INFO_SIZE, bytenr, |
Stefan Behrens | a36cf8b | 2012-11-02 13:26:57 +0100 | [diff] [blame] | 4006 | scrub_dev, BTRFS_EXTENT_FLAG_SUPER, gen, i, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4007 | NULL, 1, bytenr); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4008 | if (ret) |
| 4009 | return ret; |
| 4010 | } |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 4011 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4012 | |
| 4013 | return 0; |
| 4014 | } |
| 4015 | |
| 4016 | /* |
| 4017 | * get a reference count on fs_info->scrub_workers. start worker if necessary |
| 4018 | */ |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4019 | static noinline_for_stack int scrub_workers_get(struct btrfs_fs_info *fs_info, |
| 4020 | int is_dev_replace) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4021 | { |
David Sterba | 6f01105 | 2015-02-16 18:34:01 +0100 | [diff] [blame] | 4022 | unsigned int flags = WQ_FREEZABLE | WQ_UNBOUND; |
Qu Wenruo | 0339ef2 | 2014-02-28 10:46:17 +0800 | [diff] [blame] | 4023 | int max_active = fs_info->thread_pool_size; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4024 | |
Arne Jansen | 632dd77 | 2011-06-10 12:07:07 +0200 | [diff] [blame] | 4025 | if (fs_info->scrub_workers_refcnt == 0) { |
David Sterba | af1cbe0 | 2017-03-31 18:42:57 +0200 | [diff] [blame] | 4026 | fs_info->scrub_workers = btrfs_alloc_workqueue(fs_info, "scrub", |
| 4027 | flags, is_dev_replace ? 1 : max_active, 4); |
Zhao Lei | e82afc5 | 2015-06-12 20:36:58 +0800 | [diff] [blame] | 4028 | if (!fs_info->scrub_workers) |
| 4029 | goto fail_scrub_workers; |
| 4030 | |
Qu Wenruo | 0339ef2 | 2014-02-28 10:46:17 +0800 | [diff] [blame] | 4031 | fs_info->scrub_wr_completion_workers = |
Jeff Mahoney | cb00109 | 2016-06-09 16:22:11 -0400 | [diff] [blame] | 4032 | btrfs_alloc_workqueue(fs_info, "scrubwrc", flags, |
Qu Wenruo | 0339ef2 | 2014-02-28 10:46:17 +0800 | [diff] [blame] | 4033 | max_active, 2); |
Zhao Lei | e82afc5 | 2015-06-12 20:36:58 +0800 | [diff] [blame] | 4034 | if (!fs_info->scrub_wr_completion_workers) |
| 4035 | goto fail_scrub_wr_completion_workers; |
| 4036 | |
Zhao Lei | 20b2e30 | 2015-06-04 20:09:15 +0800 | [diff] [blame] | 4037 | fs_info->scrub_parity_workers = |
Jeff Mahoney | cb00109 | 2016-06-09 16:22:11 -0400 | [diff] [blame] | 4038 | btrfs_alloc_workqueue(fs_info, "scrubparity", flags, |
Zhao Lei | 20b2e30 | 2015-06-04 20:09:15 +0800 | [diff] [blame] | 4039 | max_active, 2); |
Zhao Lei | e82afc5 | 2015-06-12 20:36:58 +0800 | [diff] [blame] | 4040 | if (!fs_info->scrub_parity_workers) |
| 4041 | goto fail_scrub_parity_workers; |
Arne Jansen | 632dd77 | 2011-06-10 12:07:07 +0200 | [diff] [blame] | 4042 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4043 | ++fs_info->scrub_workers_refcnt; |
Zhao Lei | e82afc5 | 2015-06-12 20:36:58 +0800 | [diff] [blame] | 4044 | return 0; |
| 4045 | |
| 4046 | fail_scrub_parity_workers: |
Zhao Lei | e82afc5 | 2015-06-12 20:36:58 +0800 | [diff] [blame] | 4047 | btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers); |
| 4048 | fail_scrub_wr_completion_workers: |
| 4049 | btrfs_destroy_workqueue(fs_info->scrub_workers); |
| 4050 | fail_scrub_workers: |
| 4051 | return -ENOMEM; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4052 | } |
| 4053 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4054 | static noinline_for_stack void scrub_workers_put(struct btrfs_fs_info *fs_info) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4055 | { |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4056 | if (--fs_info->scrub_workers_refcnt == 0) { |
Qu Wenruo | 0339ef2 | 2014-02-28 10:46:17 +0800 | [diff] [blame] | 4057 | btrfs_destroy_workqueue(fs_info->scrub_workers); |
| 4058 | btrfs_destroy_workqueue(fs_info->scrub_wr_completion_workers); |
Zhao Lei | 20b2e30 | 2015-06-04 20:09:15 +0800 | [diff] [blame] | 4059 | btrfs_destroy_workqueue(fs_info->scrub_parity_workers); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4060 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4061 | WARN_ON(fs_info->scrub_workers_refcnt < 0); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4062 | } |
| 4063 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4064 | int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, |
| 4065 | u64 end, struct btrfs_scrub_progress *progress, |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4066 | int readonly, int is_dev_replace) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4067 | { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4068 | struct scrub_ctx *sctx; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4069 | int ret; |
| 4070 | struct btrfs_device *dev; |
Miao Xie | 5d68da3 | 2014-07-24 11:37:07 +0800 | [diff] [blame] | 4071 | struct rcu_string *name; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4072 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4073 | if (btrfs_fs_closing(fs_info)) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4074 | return -EINVAL; |
| 4075 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4076 | if (fs_info->nodesize > BTRFS_STRIPE_LEN) { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 4077 | /* |
| 4078 | * in this case scrub is unable to calculate the checksum |
| 4079 | * the way scrub is implemented. Do not handle this |
| 4080 | * situation at all because it won't ever happen. |
| 4081 | */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4082 | btrfs_err(fs_info, |
| 4083 | "scrub: size assumption nodesize <= BTRFS_STRIPE_LEN (%d <= %d) fails", |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4084 | fs_info->nodesize, |
| 4085 | BTRFS_STRIPE_LEN); |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 4086 | return -EINVAL; |
| 4087 | } |
| 4088 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4089 | if (fs_info->sectorsize != PAGE_SIZE) { |
Stefan Behrens | b5d67f6 | 2012-03-27 14:21:27 -0400 | [diff] [blame] | 4090 | /* not supported for data w/o checksums */ |
Chandan Rajendra | 751bebbe | 2016-07-04 10:04:39 +0530 | [diff] [blame] | 4091 | btrfs_err_rl(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4092 | "scrub: size assumption sectorsize != PAGE_SIZE (%d != %lu) fails", |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4093 | fs_info->sectorsize, PAGE_SIZE); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4094 | return -EINVAL; |
| 4095 | } |
| 4096 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4097 | if (fs_info->nodesize > |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 4098 | PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK || |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4099 | fs_info->sectorsize > PAGE_SIZE * SCRUB_MAX_PAGES_PER_BLOCK) { |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 4100 | /* |
| 4101 | * would exhaust the array bounds of pagev member in |
| 4102 | * struct scrub_block |
| 4103 | */ |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4104 | btrfs_err(fs_info, |
| 4105 | "scrub: size assumption nodesize and sectorsize <= SCRUB_MAX_PAGES_PER_BLOCK (%d <= %d && %d <= %d) fails", |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4106 | fs_info->nodesize, |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 4107 | SCRUB_MAX_PAGES_PER_BLOCK, |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 4108 | fs_info->sectorsize, |
Stefan Behrens | 7a9e998 | 2012-11-02 14:58:04 +0100 | [diff] [blame] | 4109 | SCRUB_MAX_PAGES_PER_BLOCK); |
| 4110 | return -EINVAL; |
| 4111 | } |
| 4112 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4113 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4114 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 4115 | dev = btrfs_find_device(fs_info, devid, NULL, NULL); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 4116 | if (!dev || (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) && |
| 4117 | !is_dev_replace)) { |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4118 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4119 | return -ENODEV; |
| 4120 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4121 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4122 | if (!is_dev_replace && !readonly && |
| 4123 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) { |
Miao Xie | 5d68da3 | 2014-07-24 11:37:07 +0800 | [diff] [blame] | 4124 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 4125 | rcu_read_lock(); |
| 4126 | name = rcu_dereference(dev->name); |
| 4127 | btrfs_err(fs_info, "scrub: device %s is not writable", |
| 4128 | name->str); |
| 4129 | rcu_read_unlock(); |
| 4130 | return -EROFS; |
| 4131 | } |
| 4132 | |
Wang Shilong | 3b7a016 | 2013-10-12 02:11:12 +0800 | [diff] [blame] | 4133 | mutex_lock(&fs_info->scrub_lock); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 4134 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4135 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &dev->dev_state)) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4136 | mutex_unlock(&fs_info->scrub_lock); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4137 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4138 | return -EIO; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4139 | } |
| 4140 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 4141 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
Anand Jain | cadbc0a | 2018-01-03 16:08:30 +0800 | [diff] [blame] | 4142 | if (dev->scrub_ctx || |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 4143 | (!is_dev_replace && |
| 4144 | btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))) { |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 4145 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4146 | mutex_unlock(&fs_info->scrub_lock); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4147 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4148 | return -EINPROGRESS; |
| 4149 | } |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 4150 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
Wang Shilong | 3b7a016 | 2013-10-12 02:11:12 +0800 | [diff] [blame] | 4151 | |
| 4152 | ret = scrub_workers_get(fs_info, is_dev_replace); |
| 4153 | if (ret) { |
| 4154 | mutex_unlock(&fs_info->scrub_lock); |
| 4155 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 4156 | return ret; |
| 4157 | } |
| 4158 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4159 | sctx = scrub_setup_ctx(dev, is_dev_replace); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4160 | if (IS_ERR(sctx)) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4161 | mutex_unlock(&fs_info->scrub_lock); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4162 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 4163 | scrub_workers_put(fs_info); |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4164 | return PTR_ERR(sctx); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4165 | } |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4166 | sctx->readonly = readonly; |
Anand Jain | cadbc0a | 2018-01-03 16:08:30 +0800 | [diff] [blame] | 4167 | dev->scrub_ctx = sctx; |
Wang Shilong | 3cb0929 | 2013-12-04 21:15:19 +0800 | [diff] [blame] | 4168 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4169 | |
Wang Shilong | 3cb0929 | 2013-12-04 21:15:19 +0800 | [diff] [blame] | 4170 | /* |
| 4171 | * checking @scrub_pause_req here, we can avoid |
| 4172 | * race between committing transaction and scrubbing. |
| 4173 | */ |
Wang Shilong | cb7ab02 | 2013-12-04 21:16:53 +0800 | [diff] [blame] | 4174 | __scrub_blocked_if_needed(fs_info); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4175 | atomic_inc(&fs_info->scrubs_running); |
| 4176 | mutex_unlock(&fs_info->scrub_lock); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4177 | |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4178 | if (!is_dev_replace) { |
Wang Shilong | 9b011ad | 2013-10-25 19:12:02 +0800 | [diff] [blame] | 4179 | /* |
| 4180 | * by holding device list mutex, we can |
| 4181 | * kick off writing super in log tree sync. |
| 4182 | */ |
Wang Shilong | 3cb0929 | 2013-12-04 21:15:19 +0800 | [diff] [blame] | 4183 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4184 | ret = scrub_supers(sctx, dev); |
Wang Shilong | 3cb0929 | 2013-12-04 21:15:19 +0800 | [diff] [blame] | 4185 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4186 | } |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4187 | |
| 4188 | if (!ret) |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4189 | ret = scrub_enumerate_chunks(sctx, dev, start, end, |
| 4190 | is_dev_replace); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4191 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 4192 | wait_event(sctx->list_wait, atomic_read(&sctx->bios_in_flight) == 0); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4193 | atomic_dec(&fs_info->scrubs_running); |
| 4194 | wake_up(&fs_info->scrub_pause_wait); |
| 4195 | |
Stefan Behrens | b6bfebc | 2012-11-02 16:44:58 +0100 | [diff] [blame] | 4196 | wait_event(sctx->list_wait, atomic_read(&sctx->workers_pending) == 0); |
Jan Schmidt | 0ef8e45 | 2011-06-13 20:04:15 +0200 | [diff] [blame] | 4197 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4198 | if (progress) |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4199 | memcpy(progress, &sctx->stat, sizeof(*progress)); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4200 | |
| 4201 | mutex_lock(&fs_info->scrub_lock); |
Anand Jain | cadbc0a | 2018-01-03 16:08:30 +0800 | [diff] [blame] | 4202 | dev->scrub_ctx = NULL; |
Wang Shilong | 3b7a016 | 2013-10-12 02:11:12 +0800 | [diff] [blame] | 4203 | scrub_workers_put(fs_info); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4204 | mutex_unlock(&fs_info->scrub_lock); |
| 4205 | |
Filipe Manana | f55985f | 2015-02-09 21:14:24 +0000 | [diff] [blame] | 4206 | scrub_put_ctx(sctx); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4207 | |
| 4208 | return ret; |
| 4209 | } |
| 4210 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4211 | void btrfs_scrub_pause(struct btrfs_fs_info *fs_info) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4212 | { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4213 | mutex_lock(&fs_info->scrub_lock); |
| 4214 | atomic_inc(&fs_info->scrub_pause_req); |
| 4215 | while (atomic_read(&fs_info->scrubs_paused) != |
| 4216 | atomic_read(&fs_info->scrubs_running)) { |
| 4217 | mutex_unlock(&fs_info->scrub_lock); |
| 4218 | wait_event(fs_info->scrub_pause_wait, |
| 4219 | atomic_read(&fs_info->scrubs_paused) == |
| 4220 | atomic_read(&fs_info->scrubs_running)); |
| 4221 | mutex_lock(&fs_info->scrub_lock); |
| 4222 | } |
| 4223 | mutex_unlock(&fs_info->scrub_lock); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4224 | } |
| 4225 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4226 | void btrfs_scrub_continue(struct btrfs_fs_info *fs_info) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4227 | { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4228 | atomic_dec(&fs_info->scrub_pause_req); |
| 4229 | wake_up(&fs_info->scrub_pause_wait); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4230 | } |
| 4231 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4232 | int btrfs_scrub_cancel(struct btrfs_fs_info *fs_info) |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4233 | { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4234 | mutex_lock(&fs_info->scrub_lock); |
| 4235 | if (!atomic_read(&fs_info->scrubs_running)) { |
| 4236 | mutex_unlock(&fs_info->scrub_lock); |
| 4237 | return -ENOTCONN; |
| 4238 | } |
| 4239 | |
| 4240 | atomic_inc(&fs_info->scrub_cancel_req); |
| 4241 | while (atomic_read(&fs_info->scrubs_running)) { |
| 4242 | mutex_unlock(&fs_info->scrub_lock); |
| 4243 | wait_event(fs_info->scrub_pause_wait, |
| 4244 | atomic_read(&fs_info->scrubs_running) == 0); |
| 4245 | mutex_lock(&fs_info->scrub_lock); |
| 4246 | } |
| 4247 | atomic_dec(&fs_info->scrub_cancel_req); |
| 4248 | mutex_unlock(&fs_info->scrub_lock); |
| 4249 | |
| 4250 | return 0; |
| 4251 | } |
| 4252 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 4253 | int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info, |
| 4254 | struct btrfs_device *dev) |
Jeff Mahoney | 49b25e0 | 2012-03-01 17:24:58 +0100 | [diff] [blame] | 4255 | { |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4256 | struct scrub_ctx *sctx; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4257 | |
| 4258 | mutex_lock(&fs_info->scrub_lock); |
Anand Jain | cadbc0a | 2018-01-03 16:08:30 +0800 | [diff] [blame] | 4259 | sctx = dev->scrub_ctx; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4260 | if (!sctx) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4261 | mutex_unlock(&fs_info->scrub_lock); |
| 4262 | return -ENOTCONN; |
| 4263 | } |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4264 | atomic_inc(&sctx->cancel_req); |
Anand Jain | cadbc0a | 2018-01-03 16:08:30 +0800 | [diff] [blame] | 4265 | while (dev->scrub_ctx) { |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4266 | mutex_unlock(&fs_info->scrub_lock); |
| 4267 | wait_event(fs_info->scrub_pause_wait, |
Anand Jain | cadbc0a | 2018-01-03 16:08:30 +0800 | [diff] [blame] | 4268 | dev->scrub_ctx == NULL); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4269 | mutex_lock(&fs_info->scrub_lock); |
| 4270 | } |
| 4271 | mutex_unlock(&fs_info->scrub_lock); |
| 4272 | |
| 4273 | return 0; |
| 4274 | } |
Stefan Behrens | 1623ede | 2012-03-27 14:21:26 -0400 | [diff] [blame] | 4275 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4276 | int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4277 | struct btrfs_scrub_progress *progress) |
| 4278 | { |
| 4279 | struct btrfs_device *dev; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4280 | struct scrub_ctx *sctx = NULL; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4281 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4282 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 4283 | dev = btrfs_find_device(fs_info, devid, NULL, NULL); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4284 | if (dev) |
Anand Jain | cadbc0a | 2018-01-03 16:08:30 +0800 | [diff] [blame] | 4285 | sctx = dev->scrub_ctx; |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4286 | if (sctx) |
| 4287 | memcpy(progress, &sctx->stat, sizeof(*progress)); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4288 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4289 | |
Stefan Behrens | d9d181c | 2012-11-02 09:58:09 +0100 | [diff] [blame] | 4290 | return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 4291 | } |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4292 | |
| 4293 | static void scrub_remap_extent(struct btrfs_fs_info *fs_info, |
| 4294 | u64 extent_logical, u64 extent_len, |
| 4295 | u64 *extent_physical, |
| 4296 | struct btrfs_device **extent_dev, |
| 4297 | int *extent_mirror_num) |
| 4298 | { |
| 4299 | u64 mapped_length; |
| 4300 | struct btrfs_bio *bbio = NULL; |
| 4301 | int ret; |
| 4302 | |
| 4303 | mapped_length = extent_len; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 4304 | ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, extent_logical, |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4305 | &mapped_length, &bbio, 0); |
| 4306 | if (ret || !bbio || mapped_length < extent_len || |
| 4307 | !bbio->stripes[0].dev->bdev) { |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4308 | btrfs_put_bbio(bbio); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4309 | return; |
| 4310 | } |
| 4311 | |
| 4312 | *extent_physical = bbio->stripes[0].physical; |
| 4313 | *extent_mirror_num = bbio->mirror_num; |
| 4314 | *extent_dev = bbio->stripes[0].dev; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4315 | btrfs_put_bbio(bbio); |
Stefan Behrens | ff023aa | 2012-11-06 11:43:11 +0100 | [diff] [blame] | 4316 | } |