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