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