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