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