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