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