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