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