Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) STRATO AG 2011. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * This module can be used to catch cases when the btrfs kernel |
| 21 | * code executes write requests to the disk that bring the file |
| 22 | * system in an inconsistent state. In such a state, a power-loss |
| 23 | * or kernel panic event would cause that the data on disk is |
| 24 | * lost or at least damaged. |
| 25 | * |
| 26 | * Code is added that examines all block write requests during |
| 27 | * runtime (including writes of the super block). Three rules |
| 28 | * are verified and an error is printed on violation of the |
| 29 | * rules: |
| 30 | * 1. It is not allowed to write a disk block which is |
| 31 | * currently referenced by the super block (either directly |
| 32 | * or indirectly). |
| 33 | * 2. When a super block is written, it is verified that all |
| 34 | * referenced (directly or indirectly) blocks fulfill the |
| 35 | * following requirements: |
| 36 | * 2a. All referenced blocks have either been present when |
| 37 | * the file system was mounted, (i.e., they have been |
| 38 | * referenced by the super block) or they have been |
| 39 | * written since then and the write completion callback |
Stefan Behrens | 62856a9 | 2012-07-31 11:09:44 -0600 | [diff] [blame] | 40 | * was called and no write error was indicated and a |
| 41 | * FLUSH request to the device where these blocks are |
| 42 | * located was received and completed. |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 43 | * 2b. All referenced blocks need to have a generation |
| 44 | * number which is equal to the parent's number. |
| 45 | * |
| 46 | * One issue that was found using this module was that the log |
| 47 | * tree on disk became temporarily corrupted because disk blocks |
| 48 | * that had been in use for the log tree had been freed and |
| 49 | * reused too early, while being referenced by the written super |
| 50 | * block. |
| 51 | * |
| 52 | * The search term in the kernel log that can be used to filter |
| 53 | * on the existence of detected integrity issues is |
| 54 | * "btrfs: attempt". |
| 55 | * |
| 56 | * The integrity check is enabled via mount options. These |
| 57 | * mount options are only supported if the integrity check |
| 58 | * tool is compiled by defining BTRFS_FS_CHECK_INTEGRITY. |
| 59 | * |
| 60 | * Example #1, apply integrity checks to all metadata: |
| 61 | * mount /dev/sdb1 /mnt -o check_int |
| 62 | * |
| 63 | * Example #2, apply integrity checks to all metadata and |
| 64 | * to data extents: |
| 65 | * mount /dev/sdb1 /mnt -o check_int_data |
| 66 | * |
| 67 | * Example #3, apply integrity checks to all metadata and dump |
| 68 | * the tree that the super block references to kernel messages |
| 69 | * each time after a super block was written: |
| 70 | * mount /dev/sdb1 /mnt -o check_int,check_int_print_mask=263 |
| 71 | * |
| 72 | * If the integrity check tool is included and activated in |
| 73 | * the mount options, plenty of kernel memory is used, and |
| 74 | * plenty of additional CPU cycles are spent. Enabling this |
| 75 | * functionality is not intended for normal use. In most |
| 76 | * cases, unless you are a btrfs developer who needs to verify |
| 77 | * the integrity of (super)-block write requests, do not |
| 78 | * enable the config option BTRFS_FS_CHECK_INTEGRITY to |
| 79 | * include and compile the integrity check tool. |
Stefan Behrens | 56d140f | 2013-11-13 17:19:08 +0100 | [diff] [blame] | 80 | * |
| 81 | * Expect millions of lines of information in the kernel log with an |
| 82 | * enabled check_int_print_mask. Therefore set LOG_BUF_SHIFT in the |
| 83 | * kernel config to at least 26 (which is 64MB). Usually the value is |
| 84 | * limited to 21 (which is 2MB) in init/Kconfig. The file needs to be |
| 85 | * changed like this before LOG_BUF_SHIFT can be set to a high value: |
| 86 | * config LOG_BUF_SHIFT |
| 87 | * int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" |
| 88 | * range 12 30 |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 89 | */ |
| 90 | |
| 91 | #include <linux/sched.h> |
| 92 | #include <linux/slab.h> |
| 93 | #include <linux/buffer_head.h> |
| 94 | #include <linux/mutex.h> |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 95 | #include <linux/genhd.h> |
| 96 | #include <linux/blkdev.h> |
Chris Mason | 8f608de6 | 2014-11-25 05:58:23 -0800 | [diff] [blame] | 97 | #include <linux/vmalloc.h> |
Rasmus Villemoes | 02def69 | 2015-11-27 09:42:11 +0100 | [diff] [blame^] | 98 | #include <linux/string.h> |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 99 | #include "ctree.h" |
| 100 | #include "disk-io.h" |
Filipe David Borba Manana | 0b947af | 2014-01-29 21:06:04 +0000 | [diff] [blame] | 101 | #include "hash.h" |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 102 | #include "transaction.h" |
| 103 | #include "extent_io.h" |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 104 | #include "volumes.h" |
| 105 | #include "print-tree.h" |
| 106 | #include "locking.h" |
| 107 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 108 | #include "rcu-string.h" |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 109 | |
| 110 | #define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000 |
| 111 | #define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000 |
| 112 | #define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100 |
| 113 | #define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051 |
| 114 | #define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807 |
| 115 | #define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530 |
| 116 | #define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300 |
| 117 | #define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters, |
| 118 | * excluding " [...]" */ |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 119 | #define BTRFSIC_GENERATION_UNKNOWN ((u64)-1) |
| 120 | |
| 121 | /* |
| 122 | * The definition of the bitmask fields for the print_mask. |
| 123 | * They are specified with the mount option check_integrity_print_mask. |
| 124 | */ |
| 125 | #define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001 |
| 126 | #define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002 |
| 127 | #define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004 |
| 128 | #define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008 |
| 129 | #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010 |
| 130 | #define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020 |
| 131 | #define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040 |
| 132 | #define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080 |
| 133 | #define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100 |
| 134 | #define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200 |
| 135 | #define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400 |
| 136 | #define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800 |
| 137 | #define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000 |
Stefan Behrens | 56d140f | 2013-11-13 17:19:08 +0100 | [diff] [blame] | 138 | #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000 |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 139 | |
| 140 | struct btrfsic_dev_state; |
| 141 | struct btrfsic_state; |
| 142 | |
| 143 | struct btrfsic_block { |
| 144 | u32 magic_num; /* only used for debug purposes */ |
| 145 | unsigned int is_metadata:1; /* if it is meta-data, not data-data */ |
| 146 | unsigned int is_superblock:1; /* if it is one of the superblocks */ |
| 147 | unsigned int is_iodone:1; /* if is done by lower subsystem */ |
| 148 | unsigned int iodone_w_error:1; /* error was indicated to endio */ |
| 149 | unsigned int never_written:1; /* block was added because it was |
| 150 | * referenced, not because it was |
| 151 | * written */ |
Stefan Behrens | cb3806e | 2012-11-27 16:10:21 +0000 | [diff] [blame] | 152 | unsigned int mirror_num; /* large enough to hold |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 153 | * BTRFS_SUPER_MIRROR_MAX */ |
| 154 | struct btrfsic_dev_state *dev_state; |
| 155 | u64 dev_bytenr; /* key, physical byte num on disk */ |
| 156 | u64 logical_bytenr; /* logical byte num on disk */ |
| 157 | u64 generation; |
| 158 | struct btrfs_disk_key disk_key; /* extra info to print in case of |
| 159 | * issues, will not always be correct */ |
| 160 | struct list_head collision_resolving_node; /* list node */ |
| 161 | struct list_head all_blocks_node; /* list node */ |
| 162 | |
| 163 | /* the following two lists contain block_link items */ |
| 164 | struct list_head ref_to_list; /* list */ |
| 165 | struct list_head ref_from_list; /* list */ |
| 166 | struct btrfsic_block *next_in_same_bio; |
| 167 | void *orig_bio_bh_private; |
| 168 | union { |
| 169 | bio_end_io_t *bio; |
| 170 | bh_end_io_t *bh; |
| 171 | } orig_bio_bh_end_io; |
| 172 | int submit_bio_bh_rw; |
| 173 | u64 flush_gen; /* only valid if !never_written */ |
| 174 | }; |
| 175 | |
| 176 | /* |
| 177 | * Elements of this type are allocated dynamically and required because |
| 178 | * each block object can refer to and can be ref from multiple blocks. |
| 179 | * The key to lookup them in the hashtable is the dev_bytenr of |
| 180 | * the block ref to plus the one from the block refered from. |
| 181 | * The fact that they are searchable via a hashtable and that a |
| 182 | * ref_cnt is maintained is not required for the btrfs integrity |
| 183 | * check algorithm itself, it is only used to make the output more |
| 184 | * beautiful in case that an error is detected (an error is defined |
| 185 | * as a write operation to a block while that block is still referenced). |
| 186 | */ |
| 187 | struct btrfsic_block_link { |
| 188 | u32 magic_num; /* only used for debug purposes */ |
| 189 | u32 ref_cnt; |
| 190 | struct list_head node_ref_to; /* list node */ |
| 191 | struct list_head node_ref_from; /* list node */ |
| 192 | struct list_head collision_resolving_node; /* list node */ |
| 193 | struct btrfsic_block *block_ref_to; |
| 194 | struct btrfsic_block *block_ref_from; |
| 195 | u64 parent_generation; |
| 196 | }; |
| 197 | |
| 198 | struct btrfsic_dev_state { |
| 199 | u32 magic_num; /* only used for debug purposes */ |
| 200 | struct block_device *bdev; |
| 201 | struct btrfsic_state *state; |
| 202 | struct list_head collision_resolving_node; /* list node */ |
| 203 | struct btrfsic_block dummy_block_for_bio_bh_flush; |
| 204 | u64 last_flush_gen; |
| 205 | char name[BDEVNAME_SIZE]; |
| 206 | }; |
| 207 | |
| 208 | struct btrfsic_block_hashtable { |
| 209 | struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE]; |
| 210 | }; |
| 211 | |
| 212 | struct btrfsic_block_link_hashtable { |
| 213 | struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE]; |
| 214 | }; |
| 215 | |
| 216 | struct btrfsic_dev_state_hashtable { |
| 217 | struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE]; |
| 218 | }; |
| 219 | |
| 220 | struct btrfsic_block_data_ctx { |
| 221 | u64 start; /* virtual bytenr */ |
| 222 | u64 dev_bytenr; /* physical bytenr on device */ |
| 223 | u32 len; |
| 224 | struct btrfsic_dev_state *dev; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 225 | char **datav; |
| 226 | struct page **pagev; |
| 227 | void *mem_to_free; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | /* This structure is used to implement recursion without occupying |
| 231 | * any stack space, refer to btrfsic_process_metablock() */ |
| 232 | struct btrfsic_stack_frame { |
| 233 | u32 magic; |
| 234 | u32 nr; |
| 235 | int error; |
| 236 | int i; |
| 237 | int limit_nesting; |
| 238 | int num_copies; |
| 239 | int mirror_num; |
| 240 | struct btrfsic_block *block; |
| 241 | struct btrfsic_block_data_ctx *block_ctx; |
| 242 | struct btrfsic_block *next_block; |
| 243 | struct btrfsic_block_data_ctx next_block_ctx; |
| 244 | struct btrfs_header *hdr; |
| 245 | struct btrfsic_stack_frame *prev; |
| 246 | }; |
| 247 | |
| 248 | /* Some state per mounted filesystem */ |
| 249 | struct btrfsic_state { |
| 250 | u32 print_mask; |
| 251 | int include_extent_data; |
| 252 | int csum_size; |
| 253 | struct list_head all_blocks_list; |
| 254 | struct btrfsic_block_hashtable block_hashtable; |
| 255 | struct btrfsic_block_link_hashtable block_link_hashtable; |
| 256 | struct btrfs_root *root; |
| 257 | u64 max_superblock_generation; |
| 258 | struct btrfsic_block *latest_superblock; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 259 | u32 metablock_size; |
| 260 | u32 datablock_size; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | static void btrfsic_block_init(struct btrfsic_block *b); |
| 264 | static struct btrfsic_block *btrfsic_block_alloc(void); |
| 265 | static void btrfsic_block_free(struct btrfsic_block *b); |
| 266 | static void btrfsic_block_link_init(struct btrfsic_block_link *n); |
| 267 | static struct btrfsic_block_link *btrfsic_block_link_alloc(void); |
| 268 | static void btrfsic_block_link_free(struct btrfsic_block_link *n); |
| 269 | static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds); |
| 270 | static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void); |
| 271 | static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds); |
| 272 | static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h); |
| 273 | static void btrfsic_block_hashtable_add(struct btrfsic_block *b, |
| 274 | struct btrfsic_block_hashtable *h); |
| 275 | static void btrfsic_block_hashtable_remove(struct btrfsic_block *b); |
| 276 | static struct btrfsic_block *btrfsic_block_hashtable_lookup( |
| 277 | struct block_device *bdev, |
| 278 | u64 dev_bytenr, |
| 279 | struct btrfsic_block_hashtable *h); |
| 280 | static void btrfsic_block_link_hashtable_init( |
| 281 | struct btrfsic_block_link_hashtable *h); |
| 282 | static void btrfsic_block_link_hashtable_add( |
| 283 | struct btrfsic_block_link *l, |
| 284 | struct btrfsic_block_link_hashtable *h); |
| 285 | static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l); |
| 286 | static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup( |
| 287 | struct block_device *bdev_ref_to, |
| 288 | u64 dev_bytenr_ref_to, |
| 289 | struct block_device *bdev_ref_from, |
| 290 | u64 dev_bytenr_ref_from, |
| 291 | struct btrfsic_block_link_hashtable *h); |
| 292 | static void btrfsic_dev_state_hashtable_init( |
| 293 | struct btrfsic_dev_state_hashtable *h); |
| 294 | static void btrfsic_dev_state_hashtable_add( |
| 295 | struct btrfsic_dev_state *ds, |
| 296 | struct btrfsic_dev_state_hashtable *h); |
| 297 | static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds); |
| 298 | static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup( |
| 299 | struct block_device *bdev, |
| 300 | struct btrfsic_dev_state_hashtable *h); |
| 301 | static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void); |
| 302 | static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf); |
| 303 | static int btrfsic_process_superblock(struct btrfsic_state *state, |
| 304 | struct btrfs_fs_devices *fs_devices); |
| 305 | static int btrfsic_process_metablock(struct btrfsic_state *state, |
| 306 | struct btrfsic_block *block, |
| 307 | struct btrfsic_block_data_ctx *block_ctx, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 308 | int limit_nesting, int force_iodone_flag); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 309 | static void btrfsic_read_from_block_data( |
| 310 | struct btrfsic_block_data_ctx *block_ctx, |
| 311 | void *dst, u32 offset, size_t len); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 312 | static int btrfsic_create_link_to_next_block( |
| 313 | struct btrfsic_state *state, |
| 314 | struct btrfsic_block *block, |
| 315 | struct btrfsic_block_data_ctx |
| 316 | *block_ctx, u64 next_bytenr, |
| 317 | int limit_nesting, |
| 318 | struct btrfsic_block_data_ctx *next_block_ctx, |
| 319 | struct btrfsic_block **next_blockp, |
| 320 | int force_iodone_flag, |
| 321 | int *num_copiesp, int *mirror_nump, |
| 322 | struct btrfs_disk_key *disk_key, |
| 323 | u64 parent_generation); |
| 324 | static int btrfsic_handle_extent_data(struct btrfsic_state *state, |
| 325 | struct btrfsic_block *block, |
| 326 | struct btrfsic_block_data_ctx *block_ctx, |
| 327 | u32 item_offset, int force_iodone_flag); |
| 328 | static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len, |
| 329 | struct btrfsic_block_data_ctx *block_ctx_out, |
| 330 | int mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 331 | static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx); |
| 332 | static int btrfsic_read_block(struct btrfsic_state *state, |
| 333 | struct btrfsic_block_data_ctx *block_ctx); |
| 334 | static void btrfsic_dump_database(struct btrfsic_state *state); |
| 335 | static int btrfsic_test_for_metadata(struct btrfsic_state *state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 336 | char **datav, unsigned int num_pages); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 337 | static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 338 | u64 dev_bytenr, char **mapped_datav, |
| 339 | unsigned int num_pages, |
| 340 | struct bio *bio, int *bio_is_patched, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 341 | struct buffer_head *bh, |
| 342 | int submit_bio_bh_rw); |
| 343 | static int btrfsic_process_written_superblock( |
| 344 | struct btrfsic_state *state, |
| 345 | struct btrfsic_block *const block, |
| 346 | struct btrfs_super_block *const super_hdr); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 347 | static void btrfsic_bio_end_io(struct bio *bp); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 348 | static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate); |
| 349 | static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state, |
| 350 | const struct btrfsic_block *block, |
| 351 | int recursion_level); |
| 352 | static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state, |
| 353 | struct btrfsic_block *const block, |
| 354 | int recursion_level); |
| 355 | static void btrfsic_print_add_link(const struct btrfsic_state *state, |
| 356 | const struct btrfsic_block_link *l); |
| 357 | static void btrfsic_print_rem_link(const struct btrfsic_state *state, |
| 358 | const struct btrfsic_block_link *l); |
| 359 | static char btrfsic_get_block_type(const struct btrfsic_state *state, |
| 360 | const struct btrfsic_block *block); |
| 361 | static void btrfsic_dump_tree(const struct btrfsic_state *state); |
| 362 | static void btrfsic_dump_tree_sub(const struct btrfsic_state *state, |
| 363 | const struct btrfsic_block *block, |
| 364 | int indent_level); |
| 365 | static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add( |
| 366 | struct btrfsic_state *state, |
| 367 | struct btrfsic_block_data_ctx *next_block_ctx, |
| 368 | struct btrfsic_block *next_block, |
| 369 | struct btrfsic_block *from_block, |
| 370 | u64 parent_generation); |
| 371 | static struct btrfsic_block *btrfsic_block_lookup_or_add( |
| 372 | struct btrfsic_state *state, |
| 373 | struct btrfsic_block_data_ctx *block_ctx, |
| 374 | const char *additional_string, |
| 375 | int is_metadata, |
| 376 | int is_iodone, |
| 377 | int never_written, |
| 378 | int mirror_num, |
| 379 | int *was_created); |
| 380 | static int btrfsic_process_superblock_dev_mirror( |
| 381 | struct btrfsic_state *state, |
| 382 | struct btrfsic_dev_state *dev_state, |
| 383 | struct btrfs_device *device, |
| 384 | int superblock_mirror_num, |
| 385 | struct btrfsic_dev_state **selected_dev_state, |
| 386 | struct btrfs_super_block *selected_super); |
| 387 | static struct btrfsic_dev_state *btrfsic_dev_state_lookup( |
| 388 | struct block_device *bdev); |
| 389 | static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state, |
| 390 | u64 bytenr, |
| 391 | struct btrfsic_dev_state *dev_state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 392 | u64 dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 393 | |
| 394 | static struct mutex btrfsic_mutex; |
| 395 | static int btrfsic_is_initialized; |
| 396 | static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable; |
| 397 | |
| 398 | |
| 399 | static void btrfsic_block_init(struct btrfsic_block *b) |
| 400 | { |
| 401 | b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER; |
| 402 | b->dev_state = NULL; |
| 403 | b->dev_bytenr = 0; |
| 404 | b->logical_bytenr = 0; |
| 405 | b->generation = BTRFSIC_GENERATION_UNKNOWN; |
| 406 | b->disk_key.objectid = 0; |
| 407 | b->disk_key.type = 0; |
| 408 | b->disk_key.offset = 0; |
| 409 | b->is_metadata = 0; |
| 410 | b->is_superblock = 0; |
| 411 | b->is_iodone = 0; |
| 412 | b->iodone_w_error = 0; |
| 413 | b->never_written = 0; |
| 414 | b->mirror_num = 0; |
| 415 | b->next_in_same_bio = NULL; |
| 416 | b->orig_bio_bh_private = NULL; |
| 417 | b->orig_bio_bh_end_io.bio = NULL; |
| 418 | INIT_LIST_HEAD(&b->collision_resolving_node); |
| 419 | INIT_LIST_HEAD(&b->all_blocks_node); |
| 420 | INIT_LIST_HEAD(&b->ref_to_list); |
| 421 | INIT_LIST_HEAD(&b->ref_from_list); |
| 422 | b->submit_bio_bh_rw = 0; |
| 423 | b->flush_gen = 0; |
| 424 | } |
| 425 | |
| 426 | static struct btrfsic_block *btrfsic_block_alloc(void) |
| 427 | { |
| 428 | struct btrfsic_block *b; |
| 429 | |
| 430 | b = kzalloc(sizeof(*b), GFP_NOFS); |
| 431 | if (NULL != b) |
| 432 | btrfsic_block_init(b); |
| 433 | |
| 434 | return b; |
| 435 | } |
| 436 | |
| 437 | static void btrfsic_block_free(struct btrfsic_block *b) |
| 438 | { |
| 439 | BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num)); |
| 440 | kfree(b); |
| 441 | } |
| 442 | |
| 443 | static void btrfsic_block_link_init(struct btrfsic_block_link *l) |
| 444 | { |
| 445 | l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER; |
| 446 | l->ref_cnt = 1; |
| 447 | INIT_LIST_HEAD(&l->node_ref_to); |
| 448 | INIT_LIST_HEAD(&l->node_ref_from); |
| 449 | INIT_LIST_HEAD(&l->collision_resolving_node); |
| 450 | l->block_ref_to = NULL; |
| 451 | l->block_ref_from = NULL; |
| 452 | } |
| 453 | |
| 454 | static struct btrfsic_block_link *btrfsic_block_link_alloc(void) |
| 455 | { |
| 456 | struct btrfsic_block_link *l; |
| 457 | |
| 458 | l = kzalloc(sizeof(*l), GFP_NOFS); |
| 459 | if (NULL != l) |
| 460 | btrfsic_block_link_init(l); |
| 461 | |
| 462 | return l; |
| 463 | } |
| 464 | |
| 465 | static void btrfsic_block_link_free(struct btrfsic_block_link *l) |
| 466 | { |
| 467 | BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num)); |
| 468 | kfree(l); |
| 469 | } |
| 470 | |
| 471 | static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds) |
| 472 | { |
| 473 | ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER; |
| 474 | ds->bdev = NULL; |
| 475 | ds->state = NULL; |
| 476 | ds->name[0] = '\0'; |
| 477 | INIT_LIST_HEAD(&ds->collision_resolving_node); |
| 478 | ds->last_flush_gen = 0; |
| 479 | btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush); |
| 480 | ds->dummy_block_for_bio_bh_flush.is_iodone = 1; |
| 481 | ds->dummy_block_for_bio_bh_flush.dev_state = ds; |
| 482 | } |
| 483 | |
| 484 | static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void) |
| 485 | { |
| 486 | struct btrfsic_dev_state *ds; |
| 487 | |
| 488 | ds = kzalloc(sizeof(*ds), GFP_NOFS); |
| 489 | if (NULL != ds) |
| 490 | btrfsic_dev_state_init(ds); |
| 491 | |
| 492 | return ds; |
| 493 | } |
| 494 | |
| 495 | static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds) |
| 496 | { |
| 497 | BUG_ON(!(NULL == ds || |
| 498 | BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num)); |
| 499 | kfree(ds); |
| 500 | } |
| 501 | |
| 502 | static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h) |
| 503 | { |
| 504 | int i; |
| 505 | |
| 506 | for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++) |
| 507 | INIT_LIST_HEAD(h->table + i); |
| 508 | } |
| 509 | |
| 510 | static void btrfsic_block_hashtable_add(struct btrfsic_block *b, |
| 511 | struct btrfsic_block_hashtable *h) |
| 512 | { |
| 513 | const unsigned int hashval = |
| 514 | (((unsigned int)(b->dev_bytenr >> 16)) ^ |
| 515 | ((unsigned int)((uintptr_t)b->dev_state->bdev))) & |
| 516 | (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1); |
| 517 | |
| 518 | list_add(&b->collision_resolving_node, h->table + hashval); |
| 519 | } |
| 520 | |
| 521 | static void btrfsic_block_hashtable_remove(struct btrfsic_block *b) |
| 522 | { |
| 523 | list_del(&b->collision_resolving_node); |
| 524 | } |
| 525 | |
| 526 | static struct btrfsic_block *btrfsic_block_hashtable_lookup( |
| 527 | struct block_device *bdev, |
| 528 | u64 dev_bytenr, |
| 529 | struct btrfsic_block_hashtable *h) |
| 530 | { |
| 531 | const unsigned int hashval = |
| 532 | (((unsigned int)(dev_bytenr >> 16)) ^ |
| 533 | ((unsigned int)((uintptr_t)bdev))) & |
| 534 | (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1); |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 535 | struct btrfsic_block *b; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 536 | |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 537 | list_for_each_entry(b, h->table + hashval, collision_resolving_node) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 538 | if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr) |
| 539 | return b; |
| 540 | } |
| 541 | |
| 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | static void btrfsic_block_link_hashtable_init( |
| 546 | struct btrfsic_block_link_hashtable *h) |
| 547 | { |
| 548 | int i; |
| 549 | |
| 550 | for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++) |
| 551 | INIT_LIST_HEAD(h->table + i); |
| 552 | } |
| 553 | |
| 554 | static void btrfsic_block_link_hashtable_add( |
| 555 | struct btrfsic_block_link *l, |
| 556 | struct btrfsic_block_link_hashtable *h) |
| 557 | { |
| 558 | const unsigned int hashval = |
| 559 | (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^ |
| 560 | ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^ |
| 561 | ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^ |
| 562 | ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev))) |
| 563 | & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1); |
| 564 | |
| 565 | BUG_ON(NULL == l->block_ref_to); |
| 566 | BUG_ON(NULL == l->block_ref_from); |
| 567 | list_add(&l->collision_resolving_node, h->table + hashval); |
| 568 | } |
| 569 | |
| 570 | static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l) |
| 571 | { |
| 572 | list_del(&l->collision_resolving_node); |
| 573 | } |
| 574 | |
| 575 | static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup( |
| 576 | struct block_device *bdev_ref_to, |
| 577 | u64 dev_bytenr_ref_to, |
| 578 | struct block_device *bdev_ref_from, |
| 579 | u64 dev_bytenr_ref_from, |
| 580 | struct btrfsic_block_link_hashtable *h) |
| 581 | { |
| 582 | const unsigned int hashval = |
| 583 | (((unsigned int)(dev_bytenr_ref_to >> 16)) ^ |
| 584 | ((unsigned int)(dev_bytenr_ref_from >> 16)) ^ |
| 585 | ((unsigned int)((uintptr_t)bdev_ref_to)) ^ |
| 586 | ((unsigned int)((uintptr_t)bdev_ref_from))) & |
| 587 | (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1); |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 588 | struct btrfsic_block_link *l; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 589 | |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 590 | list_for_each_entry(l, h->table + hashval, collision_resolving_node) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 591 | BUG_ON(NULL == l->block_ref_to); |
| 592 | BUG_ON(NULL == l->block_ref_from); |
| 593 | if (l->block_ref_to->dev_state->bdev == bdev_ref_to && |
| 594 | l->block_ref_to->dev_bytenr == dev_bytenr_ref_to && |
| 595 | l->block_ref_from->dev_state->bdev == bdev_ref_from && |
| 596 | l->block_ref_from->dev_bytenr == dev_bytenr_ref_from) |
| 597 | return l; |
| 598 | } |
| 599 | |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | static void btrfsic_dev_state_hashtable_init( |
| 604 | struct btrfsic_dev_state_hashtable *h) |
| 605 | { |
| 606 | int i; |
| 607 | |
| 608 | for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++) |
| 609 | INIT_LIST_HEAD(h->table + i); |
| 610 | } |
| 611 | |
| 612 | static void btrfsic_dev_state_hashtable_add( |
| 613 | struct btrfsic_dev_state *ds, |
| 614 | struct btrfsic_dev_state_hashtable *h) |
| 615 | { |
| 616 | const unsigned int hashval = |
| 617 | (((unsigned int)((uintptr_t)ds->bdev)) & |
| 618 | (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1)); |
| 619 | |
| 620 | list_add(&ds->collision_resolving_node, h->table + hashval); |
| 621 | } |
| 622 | |
| 623 | static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds) |
| 624 | { |
| 625 | list_del(&ds->collision_resolving_node); |
| 626 | } |
| 627 | |
| 628 | static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup( |
| 629 | struct block_device *bdev, |
| 630 | struct btrfsic_dev_state_hashtable *h) |
| 631 | { |
| 632 | const unsigned int hashval = |
| 633 | (((unsigned int)((uintptr_t)bdev)) & |
| 634 | (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1)); |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 635 | struct btrfsic_dev_state *ds; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 636 | |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 637 | list_for_each_entry(ds, h->table + hashval, collision_resolving_node) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 638 | if (ds->bdev == bdev) |
| 639 | return ds; |
| 640 | } |
| 641 | |
| 642 | return NULL; |
| 643 | } |
| 644 | |
| 645 | static int btrfsic_process_superblock(struct btrfsic_state *state, |
| 646 | struct btrfs_fs_devices *fs_devices) |
| 647 | { |
Chris Mason | e77266e | 2012-02-24 10:39:05 -0500 | [diff] [blame] | 648 | int ret = 0; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 649 | struct btrfs_super_block *selected_super; |
| 650 | struct list_head *dev_head = &fs_devices->devices; |
| 651 | struct btrfs_device *device; |
| 652 | struct btrfsic_dev_state *selected_dev_state = NULL; |
| 653 | int pass; |
| 654 | |
| 655 | BUG_ON(NULL == state); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 656 | selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 657 | if (NULL == selected_super) { |
| 658 | printk(KERN_INFO "btrfsic: error, kmalloc failed!\n"); |
Luis de Bethencourt | 0b8d8ce | 2015-10-20 14:56:22 +0100 | [diff] [blame] | 659 | return -ENOMEM; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | list_for_each_entry(device, dev_head, dev_list) { |
| 663 | int i; |
| 664 | struct btrfsic_dev_state *dev_state; |
| 665 | |
| 666 | if (!device->bdev || !device->name) |
| 667 | continue; |
| 668 | |
| 669 | dev_state = btrfsic_dev_state_lookup(device->bdev); |
| 670 | BUG_ON(NULL == dev_state); |
| 671 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 672 | ret = btrfsic_process_superblock_dev_mirror( |
| 673 | state, dev_state, device, i, |
| 674 | &selected_dev_state, selected_super); |
| 675 | if (0 != ret && 0 == i) { |
| 676 | kfree(selected_super); |
| 677 | return ret; |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | if (NULL == state->latest_superblock) { |
| 683 | printk(KERN_INFO "btrfsic: no superblock found!\n"); |
| 684 | kfree(selected_super); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | state->csum_size = btrfs_super_csum_size(selected_super); |
| 689 | |
| 690 | for (pass = 0; pass < 3; pass++) { |
| 691 | int num_copies; |
| 692 | int mirror_num; |
| 693 | u64 next_bytenr; |
| 694 | |
| 695 | switch (pass) { |
| 696 | case 0: |
| 697 | next_bytenr = btrfs_super_root(selected_super); |
| 698 | if (state->print_mask & |
| 699 | BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION) |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 700 | printk(KERN_INFO "root@%llu\n", next_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 701 | break; |
| 702 | case 1: |
| 703 | next_bytenr = btrfs_super_chunk_root(selected_super); |
| 704 | if (state->print_mask & |
| 705 | BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION) |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 706 | printk(KERN_INFO "chunk@%llu\n", next_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 707 | break; |
| 708 | case 2: |
| 709 | next_bytenr = btrfs_super_log_root(selected_super); |
| 710 | if (0 == next_bytenr) |
| 711 | continue; |
| 712 | if (state->print_mask & |
| 713 | BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION) |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 714 | printk(KERN_INFO "log@%llu\n", next_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 715 | break; |
| 716 | } |
| 717 | |
| 718 | num_copies = |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 719 | btrfs_num_copies(state->root->fs_info, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 720 | next_bytenr, state->metablock_size); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 721 | if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) |
| 722 | printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 723 | next_bytenr, num_copies); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 724 | |
| 725 | for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { |
| 726 | struct btrfsic_block *next_block; |
| 727 | struct btrfsic_block_data_ctx tmp_next_block_ctx; |
| 728 | struct btrfsic_block_link *l; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 729 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 730 | ret = btrfsic_map_block(state, next_bytenr, |
| 731 | state->metablock_size, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 732 | &tmp_next_block_ctx, |
| 733 | mirror_num); |
| 734 | if (ret) { |
| 735 | printk(KERN_INFO "btrfsic:" |
| 736 | " btrfsic_map_block(root @%llu," |
| 737 | " mirror %d) failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 738 | next_bytenr, mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 739 | kfree(selected_super); |
| 740 | return -1; |
| 741 | } |
| 742 | |
| 743 | next_block = btrfsic_block_hashtable_lookup( |
| 744 | tmp_next_block_ctx.dev->bdev, |
| 745 | tmp_next_block_ctx.dev_bytenr, |
| 746 | &state->block_hashtable); |
| 747 | BUG_ON(NULL == next_block); |
| 748 | |
| 749 | l = btrfsic_block_link_hashtable_lookup( |
| 750 | tmp_next_block_ctx.dev->bdev, |
| 751 | tmp_next_block_ctx.dev_bytenr, |
| 752 | state->latest_superblock->dev_state-> |
| 753 | bdev, |
| 754 | state->latest_superblock->dev_bytenr, |
| 755 | &state->block_link_hashtable); |
| 756 | BUG_ON(NULL == l); |
| 757 | |
| 758 | ret = btrfsic_read_block(state, &tmp_next_block_ctx); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 759 | if (ret < (int)PAGE_CACHE_SIZE) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 760 | printk(KERN_INFO |
| 761 | "btrfsic: read @logical %llu failed!\n", |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 762 | tmp_next_block_ctx.start); |
| 763 | btrfsic_release_block_ctx(&tmp_next_block_ctx); |
| 764 | kfree(selected_super); |
| 765 | return -1; |
| 766 | } |
| 767 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 768 | ret = btrfsic_process_metablock(state, |
| 769 | next_block, |
| 770 | &tmp_next_block_ctx, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 771 | BTRFS_MAX_LEVEL + 3, 1); |
| 772 | btrfsic_release_block_ctx(&tmp_next_block_ctx); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | kfree(selected_super); |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | static int btrfsic_process_superblock_dev_mirror( |
| 781 | struct btrfsic_state *state, |
| 782 | struct btrfsic_dev_state *dev_state, |
| 783 | struct btrfs_device *device, |
| 784 | int superblock_mirror_num, |
| 785 | struct btrfsic_dev_state **selected_dev_state, |
| 786 | struct btrfs_super_block *selected_super) |
| 787 | { |
| 788 | struct btrfs_super_block *super_tmp; |
| 789 | u64 dev_bytenr; |
| 790 | struct buffer_head *bh; |
| 791 | struct btrfsic_block *superblock_tmp; |
| 792 | int pass; |
| 793 | struct block_device *const superblock_bdev = device->bdev; |
| 794 | |
| 795 | /* super block bytenr is always the unmapped device bytenr */ |
| 796 | dev_bytenr = btrfs_sb_offset(superblock_mirror_num); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 797 | if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes) |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 798 | return -1; |
| 799 | bh = __bread(superblock_bdev, dev_bytenr / 4096, |
| 800 | BTRFS_SUPER_INFO_SIZE); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 801 | if (NULL == bh) |
| 802 | return -1; |
| 803 | super_tmp = (struct btrfs_super_block *) |
| 804 | (bh->b_data + (dev_bytenr & 4095)); |
| 805 | |
| 806 | if (btrfs_super_bytenr(super_tmp) != dev_bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 807 | btrfs_super_magic(super_tmp) != BTRFS_MAGIC || |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 808 | memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) || |
| 809 | btrfs_super_nodesize(super_tmp) != state->metablock_size || |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 810 | btrfs_super_sectorsize(super_tmp) != state->datablock_size) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 811 | brelse(bh); |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | superblock_tmp = |
| 816 | btrfsic_block_hashtable_lookup(superblock_bdev, |
| 817 | dev_bytenr, |
| 818 | &state->block_hashtable); |
| 819 | if (NULL == superblock_tmp) { |
| 820 | superblock_tmp = btrfsic_block_alloc(); |
| 821 | if (NULL == superblock_tmp) { |
| 822 | printk(KERN_INFO "btrfsic: error, kmalloc failed!\n"); |
| 823 | brelse(bh); |
| 824 | return -1; |
| 825 | } |
| 826 | /* for superblock, only the dev_bytenr makes sense */ |
| 827 | superblock_tmp->dev_bytenr = dev_bytenr; |
| 828 | superblock_tmp->dev_state = dev_state; |
| 829 | superblock_tmp->logical_bytenr = dev_bytenr; |
| 830 | superblock_tmp->generation = btrfs_super_generation(super_tmp); |
| 831 | superblock_tmp->is_metadata = 1; |
| 832 | superblock_tmp->is_superblock = 1; |
| 833 | superblock_tmp->is_iodone = 1; |
| 834 | superblock_tmp->never_written = 0; |
| 835 | superblock_tmp->mirror_num = 1 + superblock_mirror_num; |
| 836 | if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE) |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 837 | btrfs_info_in_rcu(device->dev_root->fs_info, |
| 838 | "new initial S-block (bdev %p, %s) @%llu (%s/%llu/%d)", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 839 | superblock_bdev, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 840 | rcu_str_deref(device->name), dev_bytenr, |
| 841 | dev_state->name, dev_bytenr, |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 842 | superblock_mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 843 | list_add(&superblock_tmp->all_blocks_node, |
| 844 | &state->all_blocks_list); |
| 845 | btrfsic_block_hashtable_add(superblock_tmp, |
| 846 | &state->block_hashtable); |
| 847 | } |
| 848 | |
| 849 | /* select the one with the highest generation field */ |
| 850 | if (btrfs_super_generation(super_tmp) > |
| 851 | state->max_superblock_generation || |
| 852 | 0 == state->max_superblock_generation) { |
| 853 | memcpy(selected_super, super_tmp, sizeof(*selected_super)); |
| 854 | *selected_dev_state = dev_state; |
| 855 | state->max_superblock_generation = |
| 856 | btrfs_super_generation(super_tmp); |
| 857 | state->latest_superblock = superblock_tmp; |
| 858 | } |
| 859 | |
| 860 | for (pass = 0; pass < 3; pass++) { |
| 861 | u64 next_bytenr; |
| 862 | int num_copies; |
| 863 | int mirror_num; |
| 864 | const char *additional_string = NULL; |
| 865 | struct btrfs_disk_key tmp_disk_key; |
| 866 | |
| 867 | tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY; |
| 868 | tmp_disk_key.offset = 0; |
| 869 | switch (pass) { |
| 870 | case 0: |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 871 | btrfs_set_disk_key_objectid(&tmp_disk_key, |
| 872 | BTRFS_ROOT_TREE_OBJECTID); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 873 | additional_string = "initial root "; |
| 874 | next_bytenr = btrfs_super_root(super_tmp); |
| 875 | break; |
| 876 | case 1: |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 877 | btrfs_set_disk_key_objectid(&tmp_disk_key, |
| 878 | BTRFS_CHUNK_TREE_OBJECTID); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 879 | additional_string = "initial chunk "; |
| 880 | next_bytenr = btrfs_super_chunk_root(super_tmp); |
| 881 | break; |
| 882 | case 2: |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 883 | btrfs_set_disk_key_objectid(&tmp_disk_key, |
| 884 | BTRFS_TREE_LOG_OBJECTID); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 885 | additional_string = "initial log "; |
| 886 | next_bytenr = btrfs_super_log_root(super_tmp); |
| 887 | if (0 == next_bytenr) |
| 888 | continue; |
| 889 | break; |
| 890 | } |
| 891 | |
| 892 | num_copies = |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 893 | btrfs_num_copies(state->root->fs_info, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 894 | next_bytenr, state->metablock_size); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 895 | if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) |
| 896 | printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 897 | next_bytenr, num_copies); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 898 | for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { |
| 899 | struct btrfsic_block *next_block; |
| 900 | struct btrfsic_block_data_ctx tmp_next_block_ctx; |
| 901 | struct btrfsic_block_link *l; |
| 902 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 903 | if (btrfsic_map_block(state, next_bytenr, |
| 904 | state->metablock_size, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 905 | &tmp_next_block_ctx, |
| 906 | mirror_num)) { |
| 907 | printk(KERN_INFO "btrfsic: btrfsic_map_block(" |
| 908 | "bytenr @%llu, mirror %d) failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 909 | next_bytenr, mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 910 | brelse(bh); |
| 911 | return -1; |
| 912 | } |
| 913 | |
| 914 | next_block = btrfsic_block_lookup_or_add( |
| 915 | state, &tmp_next_block_ctx, |
| 916 | additional_string, 1, 1, 0, |
| 917 | mirror_num, NULL); |
| 918 | if (NULL == next_block) { |
| 919 | btrfsic_release_block_ctx(&tmp_next_block_ctx); |
| 920 | brelse(bh); |
| 921 | return -1; |
| 922 | } |
| 923 | |
| 924 | next_block->disk_key = tmp_disk_key; |
| 925 | next_block->generation = BTRFSIC_GENERATION_UNKNOWN; |
| 926 | l = btrfsic_block_link_lookup_or_add( |
| 927 | state, &tmp_next_block_ctx, |
| 928 | next_block, superblock_tmp, |
| 929 | BTRFSIC_GENERATION_UNKNOWN); |
| 930 | btrfsic_release_block_ctx(&tmp_next_block_ctx); |
| 931 | if (NULL == l) { |
| 932 | brelse(bh); |
| 933 | return -1; |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES) |
| 938 | btrfsic_dump_tree_sub(state, superblock_tmp, 0); |
| 939 | |
| 940 | brelse(bh); |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void) |
| 945 | { |
| 946 | struct btrfsic_stack_frame *sf; |
| 947 | |
| 948 | sf = kzalloc(sizeof(*sf), GFP_NOFS); |
| 949 | if (NULL == sf) |
| 950 | printk(KERN_INFO "btrfsic: alloc memory failed!\n"); |
| 951 | else |
| 952 | sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER; |
| 953 | return sf; |
| 954 | } |
| 955 | |
| 956 | static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf) |
| 957 | { |
| 958 | BUG_ON(!(NULL == sf || |
| 959 | BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic)); |
| 960 | kfree(sf); |
| 961 | } |
| 962 | |
| 963 | static int btrfsic_process_metablock( |
| 964 | struct btrfsic_state *state, |
| 965 | struct btrfsic_block *const first_block, |
| 966 | struct btrfsic_block_data_ctx *const first_block_ctx, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 967 | int first_limit_nesting, int force_iodone_flag) |
| 968 | { |
| 969 | struct btrfsic_stack_frame initial_stack_frame = { 0 }; |
| 970 | struct btrfsic_stack_frame *sf; |
| 971 | struct btrfsic_stack_frame *next_stack; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 972 | struct btrfs_header *const first_hdr = |
| 973 | (struct btrfs_header *)first_block_ctx->datav[0]; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 974 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 975 | BUG_ON(!first_hdr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 976 | sf = &initial_stack_frame; |
| 977 | sf->error = 0; |
| 978 | sf->i = -1; |
| 979 | sf->limit_nesting = first_limit_nesting; |
| 980 | sf->block = first_block; |
| 981 | sf->block_ctx = first_block_ctx; |
| 982 | sf->next_block = NULL; |
| 983 | sf->hdr = first_hdr; |
| 984 | sf->prev = NULL; |
| 985 | |
| 986 | continue_with_new_stack_frame: |
| 987 | sf->block->generation = le64_to_cpu(sf->hdr->generation); |
| 988 | if (0 == sf->hdr->level) { |
| 989 | struct btrfs_leaf *const leafhdr = |
| 990 | (struct btrfs_leaf *)sf->hdr; |
| 991 | |
| 992 | if (-1 == sf->i) { |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 993 | sf->nr = btrfs_stack_header_nritems(&leafhdr->header); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 994 | |
| 995 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 996 | printk(KERN_INFO |
| 997 | "leaf %llu items %d generation %llu" |
| 998 | " owner %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 999 | sf->block_ctx->start, sf->nr, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1000 | btrfs_stack_header_generation( |
| 1001 | &leafhdr->header), |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1002 | btrfs_stack_header_owner( |
| 1003 | &leafhdr->header)); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | continue_with_current_leaf_stack_frame: |
| 1007 | if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) { |
| 1008 | sf->i++; |
| 1009 | sf->num_copies = 0; |
| 1010 | } |
| 1011 | |
| 1012 | if (sf->i < sf->nr) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1013 | struct btrfs_item disk_item; |
| 1014 | u32 disk_item_offset = |
| 1015 | (uintptr_t)(leafhdr->items + sf->i) - |
| 1016 | (uintptr_t)leafhdr; |
| 1017 | struct btrfs_disk_key *disk_key; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1018 | u8 type; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1019 | u32 item_offset; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 1020 | u32 item_size; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1021 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1022 | if (disk_item_offset + sizeof(struct btrfs_item) > |
| 1023 | sf->block_ctx->len) { |
| 1024 | leaf_item_out_of_bounce_error: |
| 1025 | printk(KERN_INFO |
| 1026 | "btrfsic: leaf item out of bounce at logical %llu, dev %s\n", |
| 1027 | sf->block_ctx->start, |
| 1028 | sf->block_ctx->dev->name); |
| 1029 | goto one_stack_frame_backwards; |
| 1030 | } |
| 1031 | btrfsic_read_from_block_data(sf->block_ctx, |
| 1032 | &disk_item, |
| 1033 | disk_item_offset, |
| 1034 | sizeof(struct btrfs_item)); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1035 | item_offset = btrfs_stack_item_offset(&disk_item); |
Stefan Behrens | a5f519c | 2013-10-21 14:18:17 +0200 | [diff] [blame] | 1036 | item_size = btrfs_stack_item_size(&disk_item); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1037 | disk_key = &disk_item.key; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1038 | type = btrfs_disk_key_type(disk_key); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1039 | |
| 1040 | if (BTRFS_ROOT_ITEM_KEY == type) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1041 | struct btrfs_root_item root_item; |
| 1042 | u32 root_item_offset; |
| 1043 | u64 next_bytenr; |
| 1044 | |
| 1045 | root_item_offset = item_offset + |
| 1046 | offsetof(struct btrfs_leaf, items); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 1047 | if (root_item_offset + item_size > |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1048 | sf->block_ctx->len) |
| 1049 | goto leaf_item_out_of_bounce_error; |
| 1050 | btrfsic_read_from_block_data( |
| 1051 | sf->block_ctx, &root_item, |
| 1052 | root_item_offset, |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 1053 | item_size); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1054 | next_bytenr = btrfs_root_bytenr(&root_item); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1055 | |
| 1056 | sf->error = |
| 1057 | btrfsic_create_link_to_next_block( |
| 1058 | state, |
| 1059 | sf->block, |
| 1060 | sf->block_ctx, |
| 1061 | next_bytenr, |
| 1062 | sf->limit_nesting, |
| 1063 | &sf->next_block_ctx, |
| 1064 | &sf->next_block, |
| 1065 | force_iodone_flag, |
| 1066 | &sf->num_copies, |
| 1067 | &sf->mirror_num, |
| 1068 | disk_key, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1069 | btrfs_root_generation( |
| 1070 | &root_item)); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1071 | if (sf->error) |
| 1072 | goto one_stack_frame_backwards; |
| 1073 | |
| 1074 | if (NULL != sf->next_block) { |
| 1075 | struct btrfs_header *const next_hdr = |
| 1076 | (struct btrfs_header *) |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1077 | sf->next_block_ctx.datav[0]; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1078 | |
| 1079 | next_stack = |
| 1080 | btrfsic_stack_frame_alloc(); |
| 1081 | if (NULL == next_stack) { |
Stefan Behrens | 98806b4 | 2014-05-09 15:28:07 +0200 | [diff] [blame] | 1082 | sf->error = -1; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1083 | btrfsic_release_block_ctx( |
| 1084 | &sf-> |
| 1085 | next_block_ctx); |
| 1086 | goto one_stack_frame_backwards; |
| 1087 | } |
| 1088 | |
| 1089 | next_stack->i = -1; |
| 1090 | next_stack->block = sf->next_block; |
| 1091 | next_stack->block_ctx = |
| 1092 | &sf->next_block_ctx; |
| 1093 | next_stack->next_block = NULL; |
| 1094 | next_stack->hdr = next_hdr; |
| 1095 | next_stack->limit_nesting = |
| 1096 | sf->limit_nesting - 1; |
| 1097 | next_stack->prev = sf; |
| 1098 | sf = next_stack; |
| 1099 | goto continue_with_new_stack_frame; |
| 1100 | } |
| 1101 | } else if (BTRFS_EXTENT_DATA_KEY == type && |
| 1102 | state->include_extent_data) { |
| 1103 | sf->error = btrfsic_handle_extent_data( |
| 1104 | state, |
| 1105 | sf->block, |
| 1106 | sf->block_ctx, |
| 1107 | item_offset, |
| 1108 | force_iodone_flag); |
| 1109 | if (sf->error) |
| 1110 | goto one_stack_frame_backwards; |
| 1111 | } |
| 1112 | |
| 1113 | goto continue_with_current_leaf_stack_frame; |
| 1114 | } |
| 1115 | } else { |
| 1116 | struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr; |
| 1117 | |
| 1118 | if (-1 == sf->i) { |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1119 | sf->nr = btrfs_stack_header_nritems(&nodehdr->header); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1120 | |
| 1121 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1122 | printk(KERN_INFO "node %llu level %d items %d" |
| 1123 | " generation %llu owner %llu\n", |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1124 | sf->block_ctx->start, |
| 1125 | nodehdr->header.level, sf->nr, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1126 | btrfs_stack_header_generation( |
| 1127 | &nodehdr->header), |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1128 | btrfs_stack_header_owner( |
| 1129 | &nodehdr->header)); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | continue_with_current_node_stack_frame: |
| 1133 | if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) { |
| 1134 | sf->i++; |
| 1135 | sf->num_copies = 0; |
| 1136 | } |
| 1137 | |
| 1138 | if (sf->i < sf->nr) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1139 | struct btrfs_key_ptr key_ptr; |
| 1140 | u32 key_ptr_offset; |
| 1141 | u64 next_bytenr; |
| 1142 | |
| 1143 | key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) - |
| 1144 | (uintptr_t)nodehdr; |
| 1145 | if (key_ptr_offset + sizeof(struct btrfs_key_ptr) > |
| 1146 | sf->block_ctx->len) { |
| 1147 | printk(KERN_INFO |
| 1148 | "btrfsic: node item out of bounce at logical %llu, dev %s\n", |
| 1149 | sf->block_ctx->start, |
| 1150 | sf->block_ctx->dev->name); |
| 1151 | goto one_stack_frame_backwards; |
| 1152 | } |
| 1153 | btrfsic_read_from_block_data( |
| 1154 | sf->block_ctx, &key_ptr, key_ptr_offset, |
| 1155 | sizeof(struct btrfs_key_ptr)); |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1156 | next_bytenr = btrfs_stack_key_blockptr(&key_ptr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1157 | |
| 1158 | sf->error = btrfsic_create_link_to_next_block( |
| 1159 | state, |
| 1160 | sf->block, |
| 1161 | sf->block_ctx, |
| 1162 | next_bytenr, |
| 1163 | sf->limit_nesting, |
| 1164 | &sf->next_block_ctx, |
| 1165 | &sf->next_block, |
| 1166 | force_iodone_flag, |
| 1167 | &sf->num_copies, |
| 1168 | &sf->mirror_num, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1169 | &key_ptr.key, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1170 | btrfs_stack_key_generation(&key_ptr)); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1171 | if (sf->error) |
| 1172 | goto one_stack_frame_backwards; |
| 1173 | |
| 1174 | if (NULL != sf->next_block) { |
| 1175 | struct btrfs_header *const next_hdr = |
| 1176 | (struct btrfs_header *) |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1177 | sf->next_block_ctx.datav[0]; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1178 | |
| 1179 | next_stack = btrfsic_stack_frame_alloc(); |
Stefan Behrens | 98806b4 | 2014-05-09 15:28:07 +0200 | [diff] [blame] | 1180 | if (NULL == next_stack) { |
| 1181 | sf->error = -1; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1182 | goto one_stack_frame_backwards; |
Stefan Behrens | 98806b4 | 2014-05-09 15:28:07 +0200 | [diff] [blame] | 1183 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1184 | |
| 1185 | next_stack->i = -1; |
| 1186 | next_stack->block = sf->next_block; |
| 1187 | next_stack->block_ctx = &sf->next_block_ctx; |
| 1188 | next_stack->next_block = NULL; |
| 1189 | next_stack->hdr = next_hdr; |
| 1190 | next_stack->limit_nesting = |
| 1191 | sf->limit_nesting - 1; |
| 1192 | next_stack->prev = sf; |
| 1193 | sf = next_stack; |
| 1194 | goto continue_with_new_stack_frame; |
| 1195 | } |
| 1196 | |
| 1197 | goto continue_with_current_node_stack_frame; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | one_stack_frame_backwards: |
| 1202 | if (NULL != sf->prev) { |
| 1203 | struct btrfsic_stack_frame *const prev = sf->prev; |
| 1204 | |
| 1205 | /* the one for the initial block is freed in the caller */ |
| 1206 | btrfsic_release_block_ctx(sf->block_ctx); |
| 1207 | |
| 1208 | if (sf->error) { |
| 1209 | prev->error = sf->error; |
| 1210 | btrfsic_stack_frame_free(sf); |
| 1211 | sf = prev; |
| 1212 | goto one_stack_frame_backwards; |
| 1213 | } |
| 1214 | |
| 1215 | btrfsic_stack_frame_free(sf); |
| 1216 | sf = prev; |
| 1217 | goto continue_with_new_stack_frame; |
| 1218 | } else { |
| 1219 | BUG_ON(&initial_stack_frame != sf); |
| 1220 | } |
| 1221 | |
| 1222 | return sf->error; |
| 1223 | } |
| 1224 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1225 | static void btrfsic_read_from_block_data( |
| 1226 | struct btrfsic_block_data_ctx *block_ctx, |
| 1227 | void *dstv, u32 offset, size_t len) |
| 1228 | { |
| 1229 | size_t cur; |
| 1230 | size_t offset_in_page; |
| 1231 | char *kaddr; |
| 1232 | char *dst = (char *)dstv; |
| 1233 | size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1); |
| 1234 | unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT; |
| 1235 | |
| 1236 | WARN_ON(offset + len > block_ctx->len); |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 1237 | offset_in_page = (start_offset + offset) & (PAGE_CACHE_SIZE - 1); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1238 | |
| 1239 | while (len > 0) { |
| 1240 | cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page)); |
David Sterba | ed6078f | 2014-06-05 01:59:57 +0200 | [diff] [blame] | 1241 | BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_CACHE_SIZE)); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1242 | kaddr = block_ctx->datav[i]; |
| 1243 | memcpy(dst, kaddr + offset_in_page, cur); |
| 1244 | |
| 1245 | dst += cur; |
| 1246 | len -= cur; |
| 1247 | offset_in_page = 0; |
| 1248 | i++; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1252 | static int btrfsic_create_link_to_next_block( |
| 1253 | struct btrfsic_state *state, |
| 1254 | struct btrfsic_block *block, |
| 1255 | struct btrfsic_block_data_ctx *block_ctx, |
| 1256 | u64 next_bytenr, |
| 1257 | int limit_nesting, |
| 1258 | struct btrfsic_block_data_ctx *next_block_ctx, |
| 1259 | struct btrfsic_block **next_blockp, |
| 1260 | int force_iodone_flag, |
| 1261 | int *num_copiesp, int *mirror_nump, |
| 1262 | struct btrfs_disk_key *disk_key, |
| 1263 | u64 parent_generation) |
| 1264 | { |
| 1265 | struct btrfsic_block *next_block = NULL; |
| 1266 | int ret; |
| 1267 | struct btrfsic_block_link *l; |
| 1268 | int did_alloc_block_link; |
| 1269 | int block_was_created; |
| 1270 | |
| 1271 | *next_blockp = NULL; |
| 1272 | if (0 == *num_copiesp) { |
| 1273 | *num_copiesp = |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 1274 | btrfs_num_copies(state->root->fs_info, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1275 | next_bytenr, state->metablock_size); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1276 | if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) |
| 1277 | printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1278 | next_bytenr, *num_copiesp); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1279 | *mirror_nump = 1; |
| 1280 | } |
| 1281 | |
| 1282 | if (*mirror_nump > *num_copiesp) |
| 1283 | return 0; |
| 1284 | |
| 1285 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1286 | printk(KERN_INFO |
| 1287 | "btrfsic_create_link_to_next_block(mirror_num=%d)\n", |
| 1288 | *mirror_nump); |
| 1289 | ret = btrfsic_map_block(state, next_bytenr, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1290 | state->metablock_size, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1291 | next_block_ctx, *mirror_nump); |
| 1292 | if (ret) { |
| 1293 | printk(KERN_INFO |
| 1294 | "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1295 | next_bytenr, *mirror_nump); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1296 | btrfsic_release_block_ctx(next_block_ctx); |
| 1297 | *next_blockp = NULL; |
| 1298 | return -1; |
| 1299 | } |
| 1300 | |
| 1301 | next_block = btrfsic_block_lookup_or_add(state, |
| 1302 | next_block_ctx, "referenced ", |
| 1303 | 1, force_iodone_flag, |
| 1304 | !force_iodone_flag, |
| 1305 | *mirror_nump, |
| 1306 | &block_was_created); |
| 1307 | if (NULL == next_block) { |
| 1308 | btrfsic_release_block_ctx(next_block_ctx); |
| 1309 | *next_blockp = NULL; |
| 1310 | return -1; |
| 1311 | } |
| 1312 | if (block_was_created) { |
| 1313 | l = NULL; |
| 1314 | next_block->generation = BTRFSIC_GENERATION_UNKNOWN; |
| 1315 | } else { |
Stefan Behrens | cf90c59 | 2014-10-17 14:10:10 +0200 | [diff] [blame] | 1316 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) { |
| 1317 | if (next_block->logical_bytenr != next_bytenr && |
| 1318 | !(!next_block->is_metadata && |
| 1319 | 0 == next_block->logical_bytenr)) |
| 1320 | printk(KERN_INFO |
| 1321 | "Referenced block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n", |
| 1322 | next_bytenr, next_block_ctx->dev->name, |
| 1323 | next_block_ctx->dev_bytenr, *mirror_nump, |
| 1324 | btrfsic_get_block_type(state, |
| 1325 | next_block), |
| 1326 | next_block->logical_bytenr); |
| 1327 | else |
| 1328 | printk(KERN_INFO |
| 1329 | "Referenced block @%llu (%s/%llu/%d) found in hash table, %c.\n", |
| 1330 | next_bytenr, next_block_ctx->dev->name, |
| 1331 | next_block_ctx->dev_bytenr, *mirror_nump, |
| 1332 | btrfsic_get_block_type(state, |
| 1333 | next_block)); |
| 1334 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1335 | next_block->logical_bytenr = next_bytenr; |
| 1336 | |
| 1337 | next_block->mirror_num = *mirror_nump; |
| 1338 | l = btrfsic_block_link_hashtable_lookup( |
| 1339 | next_block_ctx->dev->bdev, |
| 1340 | next_block_ctx->dev_bytenr, |
| 1341 | block_ctx->dev->bdev, |
| 1342 | block_ctx->dev_bytenr, |
| 1343 | &state->block_link_hashtable); |
| 1344 | } |
| 1345 | |
| 1346 | next_block->disk_key = *disk_key; |
| 1347 | if (NULL == l) { |
| 1348 | l = btrfsic_block_link_alloc(); |
| 1349 | if (NULL == l) { |
| 1350 | printk(KERN_INFO "btrfsic: error, kmalloc failed!\n"); |
| 1351 | btrfsic_release_block_ctx(next_block_ctx); |
| 1352 | *next_blockp = NULL; |
| 1353 | return -1; |
| 1354 | } |
| 1355 | |
| 1356 | did_alloc_block_link = 1; |
| 1357 | l->block_ref_to = next_block; |
| 1358 | l->block_ref_from = block; |
| 1359 | l->ref_cnt = 1; |
| 1360 | l->parent_generation = parent_generation; |
| 1361 | |
| 1362 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1363 | btrfsic_print_add_link(state, l); |
| 1364 | |
| 1365 | list_add(&l->node_ref_to, &block->ref_to_list); |
| 1366 | list_add(&l->node_ref_from, &next_block->ref_from_list); |
| 1367 | |
| 1368 | btrfsic_block_link_hashtable_add(l, |
| 1369 | &state->block_link_hashtable); |
| 1370 | } else { |
| 1371 | did_alloc_block_link = 0; |
| 1372 | if (0 == limit_nesting) { |
| 1373 | l->ref_cnt++; |
| 1374 | l->parent_generation = parent_generation; |
| 1375 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1376 | btrfsic_print_add_link(state, l); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | if (limit_nesting > 0 && did_alloc_block_link) { |
| 1381 | ret = btrfsic_read_block(state, next_block_ctx); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1382 | if (ret < (int)next_block_ctx->len) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1383 | printk(KERN_INFO |
| 1384 | "btrfsic: read block @logical %llu failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1385 | next_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1386 | btrfsic_release_block_ctx(next_block_ctx); |
| 1387 | *next_blockp = NULL; |
| 1388 | return -1; |
| 1389 | } |
| 1390 | |
| 1391 | *next_blockp = next_block; |
| 1392 | } else { |
| 1393 | *next_blockp = NULL; |
| 1394 | } |
| 1395 | (*mirror_nump)++; |
| 1396 | |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | static int btrfsic_handle_extent_data( |
| 1401 | struct btrfsic_state *state, |
| 1402 | struct btrfsic_block *block, |
| 1403 | struct btrfsic_block_data_ctx *block_ctx, |
| 1404 | u32 item_offset, int force_iodone_flag) |
| 1405 | { |
| 1406 | int ret; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1407 | struct btrfs_file_extent_item file_extent_item; |
| 1408 | u64 file_extent_item_offset; |
| 1409 | u64 next_bytenr; |
| 1410 | u64 num_bytes; |
| 1411 | u64 generation; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1412 | struct btrfsic_block_link *l; |
| 1413 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1414 | file_extent_item_offset = offsetof(struct btrfs_leaf, items) + |
| 1415 | item_offset; |
Stefan Behrens | 86ff7ff | 2012-04-24 18:10:16 +0200 | [diff] [blame] | 1416 | if (file_extent_item_offset + |
| 1417 | offsetof(struct btrfs_file_extent_item, disk_num_bytes) > |
| 1418 | block_ctx->len) { |
| 1419 | printk(KERN_INFO |
| 1420 | "btrfsic: file item out of bounce at logical %llu, dev %s\n", |
| 1421 | block_ctx->start, block_ctx->dev->name); |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
| 1425 | btrfsic_read_from_block_data(block_ctx, &file_extent_item, |
| 1426 | file_extent_item_offset, |
| 1427 | offsetof(struct btrfs_file_extent_item, disk_num_bytes)); |
| 1428 | if (BTRFS_FILE_EXTENT_REG != file_extent_item.type || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1429 | btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) { |
Stefan Behrens | 86ff7ff | 2012-04-24 18:10:16 +0200 | [diff] [blame] | 1430 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE) |
| 1431 | printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n", |
| 1432 | file_extent_item.type, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1433 | btrfs_stack_file_extent_disk_bytenr( |
| 1434 | &file_extent_item)); |
Stefan Behrens | 86ff7ff | 2012-04-24 18:10:16 +0200 | [diff] [blame] | 1435 | return 0; |
| 1436 | } |
| 1437 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1438 | if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) > |
| 1439 | block_ctx->len) { |
| 1440 | printk(KERN_INFO |
| 1441 | "btrfsic: file item out of bounce at logical %llu, dev %s\n", |
| 1442 | block_ctx->start, block_ctx->dev->name); |
| 1443 | return -1; |
| 1444 | } |
| 1445 | btrfsic_read_from_block_data(block_ctx, &file_extent_item, |
| 1446 | file_extent_item_offset, |
| 1447 | sizeof(struct btrfs_file_extent_item)); |
Josef Bacik | e20d6c5 | 2013-11-13 21:11:49 -0500 | [diff] [blame] | 1448 | next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item); |
| 1449 | if (btrfs_stack_file_extent_compression(&file_extent_item) == |
| 1450 | BTRFS_COMPRESS_NONE) { |
| 1451 | next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item); |
| 1452 | num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item); |
| 1453 | } else { |
| 1454 | num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item); |
| 1455 | } |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1456 | generation = btrfs_stack_file_extent_generation(&file_extent_item); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1457 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1458 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE) |
| 1459 | printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu," |
| 1460 | " offset = %llu, num_bytes = %llu\n", |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1461 | file_extent_item.type, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1462 | btrfs_stack_file_extent_disk_bytenr(&file_extent_item), |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1463 | btrfs_stack_file_extent_offset(&file_extent_item), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1464 | num_bytes); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1465 | while (num_bytes > 0) { |
| 1466 | u32 chunk_len; |
| 1467 | int num_copies; |
| 1468 | int mirror_num; |
| 1469 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1470 | if (num_bytes > state->datablock_size) |
| 1471 | chunk_len = state->datablock_size; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1472 | else |
| 1473 | chunk_len = num_bytes; |
| 1474 | |
| 1475 | num_copies = |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 1476 | btrfs_num_copies(state->root->fs_info, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1477 | next_bytenr, state->datablock_size); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1478 | if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) |
| 1479 | printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1480 | next_bytenr, num_copies); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1481 | for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { |
| 1482 | struct btrfsic_block_data_ctx next_block_ctx; |
| 1483 | struct btrfsic_block *next_block; |
| 1484 | int block_was_created; |
| 1485 | |
| 1486 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1487 | printk(KERN_INFO "btrfsic_handle_extent_data(" |
| 1488 | "mirror_num=%d)\n", mirror_num); |
| 1489 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE) |
| 1490 | printk(KERN_INFO |
| 1491 | "\tdisk_bytenr = %llu, num_bytes %u\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1492 | next_bytenr, chunk_len); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1493 | ret = btrfsic_map_block(state, next_bytenr, |
| 1494 | chunk_len, &next_block_ctx, |
| 1495 | mirror_num); |
| 1496 | if (ret) { |
| 1497 | printk(KERN_INFO |
| 1498 | "btrfsic: btrfsic_map_block(@%llu," |
| 1499 | " mirror=%d) failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1500 | next_bytenr, mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1501 | return -1; |
| 1502 | } |
| 1503 | |
| 1504 | next_block = btrfsic_block_lookup_or_add( |
| 1505 | state, |
| 1506 | &next_block_ctx, |
| 1507 | "referenced ", |
| 1508 | 0, |
| 1509 | force_iodone_flag, |
| 1510 | !force_iodone_flag, |
| 1511 | mirror_num, |
| 1512 | &block_was_created); |
| 1513 | if (NULL == next_block) { |
| 1514 | printk(KERN_INFO |
| 1515 | "btrfsic: error, kmalloc failed!\n"); |
| 1516 | btrfsic_release_block_ctx(&next_block_ctx); |
| 1517 | return -1; |
| 1518 | } |
| 1519 | if (!block_was_created) { |
Stefan Behrens | cf90c59 | 2014-10-17 14:10:10 +0200 | [diff] [blame] | 1520 | if ((state->print_mask & |
| 1521 | BTRFSIC_PRINT_MASK_VERBOSE) && |
| 1522 | next_block->logical_bytenr != next_bytenr && |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1523 | !(!next_block->is_metadata && |
| 1524 | 0 == next_block->logical_bytenr)) { |
| 1525 | printk(KERN_INFO |
| 1526 | "Referenced block" |
| 1527 | " @%llu (%s/%llu/%d)" |
| 1528 | " found in hash table, D," |
| 1529 | " bytenr mismatch" |
| 1530 | " (!= stored %llu).\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1531 | next_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1532 | next_block_ctx.dev->name, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1533 | next_block_ctx.dev_bytenr, |
| 1534 | mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1535 | next_block->logical_bytenr); |
| 1536 | } |
| 1537 | next_block->logical_bytenr = next_bytenr; |
| 1538 | next_block->mirror_num = mirror_num; |
| 1539 | } |
| 1540 | |
| 1541 | l = btrfsic_block_link_lookup_or_add(state, |
| 1542 | &next_block_ctx, |
| 1543 | next_block, block, |
| 1544 | generation); |
| 1545 | btrfsic_release_block_ctx(&next_block_ctx); |
| 1546 | if (NULL == l) |
| 1547 | return -1; |
| 1548 | } |
| 1549 | |
| 1550 | next_bytenr += chunk_len; |
| 1551 | num_bytes -= chunk_len; |
| 1552 | } |
| 1553 | |
| 1554 | return 0; |
| 1555 | } |
| 1556 | |
| 1557 | static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len, |
| 1558 | struct btrfsic_block_data_ctx *block_ctx_out, |
| 1559 | int mirror_num) |
| 1560 | { |
| 1561 | int ret; |
| 1562 | u64 length; |
| 1563 | struct btrfs_bio *multi = NULL; |
| 1564 | struct btrfs_device *device; |
| 1565 | |
| 1566 | length = len; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1567 | ret = btrfs_map_block(state->root->fs_info, READ, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1568 | bytenr, &length, &multi, mirror_num); |
| 1569 | |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1570 | if (ret) { |
| 1571 | block_ctx_out->start = 0; |
| 1572 | block_ctx_out->dev_bytenr = 0; |
| 1573 | block_ctx_out->len = 0; |
| 1574 | block_ctx_out->dev = NULL; |
| 1575 | block_ctx_out->datav = NULL; |
| 1576 | block_ctx_out->pagev = NULL; |
| 1577 | block_ctx_out->mem_to_free = NULL; |
| 1578 | |
| 1579 | return ret; |
| 1580 | } |
| 1581 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1582 | device = multi->stripes[0].dev; |
| 1583 | block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev); |
| 1584 | block_ctx_out->dev_bytenr = multi->stripes[0].physical; |
| 1585 | block_ctx_out->start = bytenr; |
| 1586 | block_ctx_out->len = len; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1587 | block_ctx_out->datav = NULL; |
| 1588 | block_ctx_out->pagev = NULL; |
| 1589 | block_ctx_out->mem_to_free = NULL; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1590 | |
Stefan Behrens | 6189192 | 2012-11-05 18:51:52 +0100 | [diff] [blame] | 1591 | kfree(multi); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1592 | if (NULL == block_ctx_out->dev) { |
| 1593 | ret = -ENXIO; |
| 1594 | printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n"); |
| 1595 | } |
| 1596 | |
| 1597 | return ret; |
| 1598 | } |
| 1599 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1600 | static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx) |
| 1601 | { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1602 | if (block_ctx->mem_to_free) { |
| 1603 | unsigned int num_pages; |
| 1604 | |
| 1605 | BUG_ON(!block_ctx->datav); |
| 1606 | BUG_ON(!block_ctx->pagev); |
| 1607 | num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >> |
| 1608 | PAGE_CACHE_SHIFT; |
| 1609 | while (num_pages > 0) { |
| 1610 | num_pages--; |
| 1611 | if (block_ctx->datav[num_pages]) { |
| 1612 | kunmap(block_ctx->pagev[num_pages]); |
| 1613 | block_ctx->datav[num_pages] = NULL; |
| 1614 | } |
| 1615 | if (block_ctx->pagev[num_pages]) { |
| 1616 | __free_page(block_ctx->pagev[num_pages]); |
| 1617 | block_ctx->pagev[num_pages] = NULL; |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | kfree(block_ctx->mem_to_free); |
| 1622 | block_ctx->mem_to_free = NULL; |
| 1623 | block_ctx->pagev = NULL; |
| 1624 | block_ctx->datav = NULL; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | static int btrfsic_read_block(struct btrfsic_state *state, |
| 1629 | struct btrfsic_block_data_ctx *block_ctx) |
| 1630 | { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1631 | unsigned int num_pages; |
| 1632 | unsigned int i; |
| 1633 | u64 dev_bytenr; |
| 1634 | int ret; |
| 1635 | |
| 1636 | BUG_ON(block_ctx->datav); |
| 1637 | BUG_ON(block_ctx->pagev); |
| 1638 | BUG_ON(block_ctx->mem_to_free); |
| 1639 | if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1640 | printk(KERN_INFO |
| 1641 | "btrfsic: read_block() with unaligned bytenr %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1642 | block_ctx->dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1643 | return -1; |
| 1644 | } |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1645 | |
| 1646 | num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >> |
| 1647 | PAGE_CACHE_SHIFT; |
| 1648 | block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) + |
| 1649 | sizeof(*block_ctx->pagev)) * |
| 1650 | num_pages, GFP_NOFS); |
| 1651 | if (!block_ctx->mem_to_free) |
Luis de Bethencourt | 0b8d8ce | 2015-10-20 14:56:22 +0100 | [diff] [blame] | 1652 | return -ENOMEM; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1653 | block_ctx->datav = block_ctx->mem_to_free; |
| 1654 | block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages); |
| 1655 | for (i = 0; i < num_pages; i++) { |
| 1656 | block_ctx->pagev[i] = alloc_page(GFP_NOFS); |
| 1657 | if (!block_ctx->pagev[i]) |
| 1658 | return -1; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1659 | } |
| 1660 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1661 | dev_bytenr = block_ctx->dev_bytenr; |
| 1662 | for (i = 0; i < num_pages;) { |
| 1663 | struct bio *bio; |
| 1664 | unsigned int j; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1665 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 1666 | bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1667 | if (!bio) { |
| 1668 | printk(KERN_INFO |
| 1669 | "btrfsic: bio_alloc() for %u pages failed!\n", |
| 1670 | num_pages - i); |
| 1671 | return -1; |
| 1672 | } |
| 1673 | bio->bi_bdev = block_ctx->dev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1674 | bio->bi_iter.bi_sector = dev_bytenr >> 9; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1675 | |
| 1676 | for (j = i; j < num_pages; j++) { |
| 1677 | ret = bio_add_page(bio, block_ctx->pagev[j], |
| 1678 | PAGE_CACHE_SIZE, 0); |
| 1679 | if (PAGE_CACHE_SIZE != ret) |
| 1680 | break; |
| 1681 | } |
| 1682 | if (j == i) { |
| 1683 | printk(KERN_INFO |
| 1684 | "btrfsic: error, failed to add a single page!\n"); |
| 1685 | return -1; |
| 1686 | } |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 1687 | if (submit_bio_wait(READ, bio)) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1688 | printk(KERN_INFO |
| 1689 | "btrfsic: read error at logical %llu dev %s!\n", |
| 1690 | block_ctx->start, block_ctx->dev->name); |
| 1691 | bio_put(bio); |
| 1692 | return -1; |
| 1693 | } |
| 1694 | bio_put(bio); |
| 1695 | dev_bytenr += (j - i) * PAGE_CACHE_SIZE; |
| 1696 | i = j; |
| 1697 | } |
| 1698 | for (i = 0; i < num_pages; i++) { |
| 1699 | block_ctx->datav[i] = kmap(block_ctx->pagev[i]); |
| 1700 | if (!block_ctx->datav[i]) { |
| 1701 | printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n", |
| 1702 | block_ctx->dev->name); |
| 1703 | return -1; |
| 1704 | } |
| 1705 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1706 | |
| 1707 | return block_ctx->len; |
| 1708 | } |
| 1709 | |
| 1710 | static void btrfsic_dump_database(struct btrfsic_state *state) |
| 1711 | { |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 1712 | const struct btrfsic_block *b_all; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1713 | |
| 1714 | BUG_ON(NULL == state); |
| 1715 | |
| 1716 | printk(KERN_INFO "all_blocks_list:\n"); |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 1717 | list_for_each_entry(b_all, &state->all_blocks_list, all_blocks_node) { |
| 1718 | const struct btrfsic_block_link *l; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1719 | |
| 1720 | printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n", |
| 1721 | btrfsic_get_block_type(state, b_all), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1722 | b_all->logical_bytenr, b_all->dev_state->name, |
| 1723 | b_all->dev_bytenr, b_all->mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1724 | |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 1725 | list_for_each_entry(l, &b_all->ref_to_list, node_ref_to) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1726 | printk(KERN_INFO " %c @%llu (%s/%llu/%d)" |
| 1727 | " refers %u* to" |
| 1728 | " %c @%llu (%s/%llu/%d)\n", |
| 1729 | btrfsic_get_block_type(state, b_all), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1730 | b_all->logical_bytenr, b_all->dev_state->name, |
| 1731 | b_all->dev_bytenr, b_all->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1732 | l->ref_cnt, |
| 1733 | btrfsic_get_block_type(state, l->block_ref_to), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1734 | l->block_ref_to->logical_bytenr, |
| 1735 | l->block_ref_to->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1736 | l->block_ref_to->dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1737 | l->block_ref_to->mirror_num); |
| 1738 | } |
| 1739 | |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 1740 | list_for_each_entry(l, &b_all->ref_from_list, node_ref_from) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1741 | printk(KERN_INFO " %c @%llu (%s/%llu/%d)" |
| 1742 | " is ref %u* from" |
| 1743 | " %c @%llu (%s/%llu/%d)\n", |
| 1744 | btrfsic_get_block_type(state, b_all), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1745 | b_all->logical_bytenr, b_all->dev_state->name, |
| 1746 | b_all->dev_bytenr, b_all->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1747 | l->ref_cnt, |
| 1748 | btrfsic_get_block_type(state, l->block_ref_from), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1749 | l->block_ref_from->logical_bytenr, |
| 1750 | l->block_ref_from->dev_state->name, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1751 | l->block_ref_from->dev_bytenr, |
| 1752 | l->block_ref_from->mirror_num); |
| 1753 | } |
| 1754 | |
| 1755 | printk(KERN_INFO "\n"); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /* |
| 1760 | * Test whether the disk block contains a tree block (leaf or node) |
| 1761 | * (note that this test fails for the super block) |
| 1762 | */ |
| 1763 | static int btrfsic_test_for_metadata(struct btrfsic_state *state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1764 | char **datav, unsigned int num_pages) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1765 | { |
| 1766 | struct btrfs_header *h; |
| 1767 | u8 csum[BTRFS_CSUM_SIZE]; |
| 1768 | u32 crc = ~(u32)0; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1769 | unsigned int i; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1770 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1771 | if (num_pages * PAGE_CACHE_SIZE < state->metablock_size) |
| 1772 | return 1; /* not metadata */ |
| 1773 | num_pages = state->metablock_size >> PAGE_CACHE_SHIFT; |
| 1774 | h = (struct btrfs_header *)datav[0]; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1775 | |
| 1776 | if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE)) |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1777 | return 1; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1778 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1779 | for (i = 0; i < num_pages; i++) { |
| 1780 | u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE); |
| 1781 | size_t sublen = i ? PAGE_CACHE_SIZE : |
| 1782 | (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE); |
| 1783 | |
Filipe David Borba Manana | 0b947af | 2014-01-29 21:06:04 +0000 | [diff] [blame] | 1784 | crc = btrfs_crc32c(crc, data, sublen); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1785 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1786 | btrfs_csum_final(crc, csum); |
| 1787 | if (memcmp(csum, h->csum, state->csum_size)) |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1788 | return 1; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1789 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1790 | return 0; /* is metadata */ |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1794 | u64 dev_bytenr, char **mapped_datav, |
| 1795 | unsigned int num_pages, |
| 1796 | struct bio *bio, int *bio_is_patched, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1797 | struct buffer_head *bh, |
| 1798 | int submit_bio_bh_rw) |
| 1799 | { |
| 1800 | int is_metadata; |
| 1801 | struct btrfsic_block *block; |
| 1802 | struct btrfsic_block_data_ctx block_ctx; |
| 1803 | int ret; |
| 1804 | struct btrfsic_state *state = dev_state->state; |
| 1805 | struct block_device *bdev = dev_state->bdev; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1806 | unsigned int processed_len; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1807 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1808 | if (NULL != bio_is_patched) |
| 1809 | *bio_is_patched = 0; |
| 1810 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1811 | again: |
| 1812 | if (num_pages == 0) |
| 1813 | return; |
| 1814 | |
| 1815 | processed_len = 0; |
| 1816 | is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav, |
| 1817 | num_pages)); |
| 1818 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1819 | block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr, |
| 1820 | &state->block_hashtable); |
| 1821 | if (NULL != block) { |
Stefan Behrens | 0b48514 | 2012-01-26 15:01:11 -0500 | [diff] [blame] | 1822 | u64 bytenr = 0; |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 1823 | struct btrfsic_block_link *l, *tmp; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1824 | |
| 1825 | if (block->is_superblock) { |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1826 | bytenr = btrfs_super_bytenr((struct btrfs_super_block *) |
| 1827 | mapped_datav[0]); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1828 | if (num_pages * PAGE_CACHE_SIZE < |
| 1829 | BTRFS_SUPER_INFO_SIZE) { |
| 1830 | printk(KERN_INFO |
| 1831 | "btrfsic: cannot work with too short bios!\n"); |
| 1832 | return; |
| 1833 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1834 | is_metadata = 1; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1835 | BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1)); |
| 1836 | processed_len = BTRFS_SUPER_INFO_SIZE; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1837 | if (state->print_mask & |
| 1838 | BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) { |
| 1839 | printk(KERN_INFO |
| 1840 | "[before new superblock is written]:\n"); |
| 1841 | btrfsic_dump_tree_sub(state, block, 0); |
| 1842 | } |
| 1843 | } |
| 1844 | if (is_metadata) { |
| 1845 | if (!block->is_superblock) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1846 | if (num_pages * PAGE_CACHE_SIZE < |
| 1847 | state->metablock_size) { |
| 1848 | printk(KERN_INFO |
| 1849 | "btrfsic: cannot work with too short bios!\n"); |
| 1850 | return; |
| 1851 | } |
| 1852 | processed_len = state->metablock_size; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1853 | bytenr = btrfs_stack_header_bytenr( |
| 1854 | (struct btrfs_header *) |
| 1855 | mapped_datav[0]); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1856 | btrfsic_cmp_log_and_dev_bytenr(state, bytenr, |
| 1857 | dev_state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1858 | dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1859 | } |
Stefan Behrens | cf90c59 | 2014-10-17 14:10:10 +0200 | [diff] [blame] | 1860 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) { |
| 1861 | if (block->logical_bytenr != bytenr && |
| 1862 | !(!block->is_metadata && |
| 1863 | block->logical_bytenr == 0)) |
| 1864 | printk(KERN_INFO |
| 1865 | "Written block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n", |
| 1866 | bytenr, dev_state->name, |
| 1867 | dev_bytenr, |
| 1868 | block->mirror_num, |
| 1869 | btrfsic_get_block_type(state, |
| 1870 | block), |
| 1871 | block->logical_bytenr); |
| 1872 | else |
| 1873 | printk(KERN_INFO |
| 1874 | "Written block @%llu (%s/%llu/%d) found in hash table, %c.\n", |
| 1875 | bytenr, dev_state->name, |
| 1876 | dev_bytenr, block->mirror_num, |
| 1877 | btrfsic_get_block_type(state, |
| 1878 | block)); |
| 1879 | } |
Stefan Behrens | 301993a | 2013-10-21 18:46:58 +0200 | [diff] [blame] | 1880 | block->logical_bytenr = bytenr; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1881 | } else { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1882 | if (num_pages * PAGE_CACHE_SIZE < |
| 1883 | state->datablock_size) { |
| 1884 | printk(KERN_INFO |
| 1885 | "btrfsic: cannot work with too short bios!\n"); |
| 1886 | return; |
| 1887 | } |
| 1888 | processed_len = state->datablock_size; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1889 | bytenr = block->logical_bytenr; |
| 1890 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1891 | printk(KERN_INFO |
| 1892 | "Written block @%llu (%s/%llu/%d)" |
| 1893 | " found in hash table, %c.\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1894 | bytenr, dev_state->name, dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1895 | block->mirror_num, |
| 1896 | btrfsic_get_block_type(state, block)); |
| 1897 | } |
| 1898 | |
| 1899 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1900 | printk(KERN_INFO |
| 1901 | "ref_to_list: %cE, ref_from_list: %cE\n", |
| 1902 | list_empty(&block->ref_to_list) ? ' ' : '!', |
| 1903 | list_empty(&block->ref_from_list) ? ' ' : '!'); |
| 1904 | if (btrfsic_is_block_ref_by_superblock(state, block, 0)) { |
| 1905 | printk(KERN_INFO "btrfs: attempt to overwrite %c-block" |
| 1906 | " @%llu (%s/%llu/%d), old(gen=%llu," |
| 1907 | " objectid=%llu, type=%d, offset=%llu)," |
| 1908 | " new(gen=%llu)," |
| 1909 | " which is referenced by most recent superblock" |
| 1910 | " (superblockgen=%llu)!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1911 | btrfsic_get_block_type(state, block), bytenr, |
| 1912 | dev_state->name, dev_bytenr, block->mirror_num, |
| 1913 | block->generation, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1914 | btrfs_disk_key_objectid(&block->disk_key), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1915 | block->disk_key.type, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1916 | btrfs_disk_key_offset(&block->disk_key), |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1917 | btrfs_stack_header_generation( |
| 1918 | (struct btrfs_header *) mapped_datav[0]), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1919 | state->max_superblock_generation); |
| 1920 | btrfsic_dump_tree(state); |
| 1921 | } |
| 1922 | |
| 1923 | if (!block->is_iodone && !block->never_written) { |
| 1924 | printk(KERN_INFO "btrfs: attempt to overwrite %c-block" |
| 1925 | " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu," |
| 1926 | " which is not yet iodone!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1927 | btrfsic_get_block_type(state, block), bytenr, |
| 1928 | dev_state->name, dev_bytenr, block->mirror_num, |
| 1929 | block->generation, |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1930 | btrfs_stack_header_generation( |
| 1931 | (struct btrfs_header *) |
| 1932 | mapped_datav[0])); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1933 | /* it would not be safe to go on */ |
| 1934 | btrfsic_dump_tree(state); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 1935 | goto continue_loop; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | * Clear all references of this block. Do not free |
| 1940 | * the block itself even if is not referenced anymore |
| 1941 | * because it still carries valueable information |
| 1942 | * like whether it was ever written and IO completed. |
| 1943 | */ |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 1944 | list_for_each_entry_safe(l, tmp, &block->ref_to_list, |
| 1945 | node_ref_to) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1946 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 1947 | btrfsic_print_rem_link(state, l); |
| 1948 | l->ref_cnt--; |
| 1949 | if (0 == l->ref_cnt) { |
| 1950 | list_del(&l->node_ref_to); |
| 1951 | list_del(&l->node_ref_from); |
| 1952 | btrfsic_block_link_hashtable_remove(l); |
| 1953 | btrfsic_block_link_free(l); |
| 1954 | } |
| 1955 | } |
| 1956 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1957 | block_ctx.dev = dev_state; |
| 1958 | block_ctx.dev_bytenr = dev_bytenr; |
Stefan Behrens | f382e46 | 2014-10-16 17:48:48 +0200 | [diff] [blame] | 1959 | block_ctx.start = bytenr; |
| 1960 | block_ctx.len = processed_len; |
| 1961 | block_ctx.pagev = NULL; |
| 1962 | block_ctx.mem_to_free = NULL; |
| 1963 | block_ctx.datav = mapped_datav; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 1964 | |
| 1965 | if (is_metadata || state->include_extent_data) { |
| 1966 | block->never_written = 0; |
| 1967 | block->iodone_w_error = 0; |
| 1968 | if (NULL != bio) { |
| 1969 | block->is_iodone = 0; |
| 1970 | BUG_ON(NULL == bio_is_patched); |
| 1971 | if (!*bio_is_patched) { |
| 1972 | block->orig_bio_bh_private = |
| 1973 | bio->bi_private; |
| 1974 | block->orig_bio_bh_end_io.bio = |
| 1975 | bio->bi_end_io; |
| 1976 | block->next_in_same_bio = NULL; |
| 1977 | bio->bi_private = block; |
| 1978 | bio->bi_end_io = btrfsic_bio_end_io; |
| 1979 | *bio_is_patched = 1; |
| 1980 | } else { |
| 1981 | struct btrfsic_block *chained_block = |
| 1982 | (struct btrfsic_block *) |
| 1983 | bio->bi_private; |
| 1984 | |
| 1985 | BUG_ON(NULL == chained_block); |
| 1986 | block->orig_bio_bh_private = |
| 1987 | chained_block->orig_bio_bh_private; |
| 1988 | block->orig_bio_bh_end_io.bio = |
| 1989 | chained_block->orig_bio_bh_end_io. |
| 1990 | bio; |
| 1991 | block->next_in_same_bio = chained_block; |
| 1992 | bio->bi_private = block; |
| 1993 | } |
| 1994 | } else if (NULL != bh) { |
| 1995 | block->is_iodone = 0; |
| 1996 | block->orig_bio_bh_private = bh->b_private; |
| 1997 | block->orig_bio_bh_end_io.bh = bh->b_end_io; |
| 1998 | block->next_in_same_bio = NULL; |
| 1999 | bh->b_private = block; |
| 2000 | bh->b_end_io = btrfsic_bh_end_io; |
| 2001 | } else { |
| 2002 | block->is_iodone = 1; |
| 2003 | block->orig_bio_bh_private = NULL; |
| 2004 | block->orig_bio_bh_end_io.bio = NULL; |
| 2005 | block->next_in_same_bio = NULL; |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | block->flush_gen = dev_state->last_flush_gen + 1; |
| 2010 | block->submit_bio_bh_rw = submit_bio_bh_rw; |
| 2011 | if (is_metadata) { |
| 2012 | block->logical_bytenr = bytenr; |
| 2013 | block->is_metadata = 1; |
| 2014 | if (block->is_superblock) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2015 | BUG_ON(PAGE_CACHE_SIZE != |
| 2016 | BTRFS_SUPER_INFO_SIZE); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2017 | ret = btrfsic_process_written_superblock( |
| 2018 | state, |
| 2019 | block, |
| 2020 | (struct btrfs_super_block *) |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2021 | mapped_datav[0]); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2022 | if (state->print_mask & |
| 2023 | BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) { |
| 2024 | printk(KERN_INFO |
| 2025 | "[after new superblock is written]:\n"); |
| 2026 | btrfsic_dump_tree_sub(state, block, 0); |
| 2027 | } |
| 2028 | } else { |
| 2029 | block->mirror_num = 0; /* unknown */ |
| 2030 | ret = btrfsic_process_metablock( |
| 2031 | state, |
| 2032 | block, |
| 2033 | &block_ctx, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2034 | 0, 0); |
| 2035 | } |
| 2036 | if (ret) |
| 2037 | printk(KERN_INFO |
| 2038 | "btrfsic: btrfsic_process_metablock" |
| 2039 | "(root @%llu) failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2040 | dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2041 | } else { |
| 2042 | block->is_metadata = 0; |
| 2043 | block->mirror_num = 0; /* unknown */ |
| 2044 | block->generation = BTRFSIC_GENERATION_UNKNOWN; |
| 2045 | if (!state->include_extent_data |
| 2046 | && list_empty(&block->ref_from_list)) { |
| 2047 | /* |
| 2048 | * disk block is overwritten with extent |
| 2049 | * data (not meta data) and we are configured |
| 2050 | * to not include extent data: take the |
| 2051 | * chance and free the block's memory |
| 2052 | */ |
| 2053 | btrfsic_block_hashtable_remove(block); |
| 2054 | list_del(&block->all_blocks_node); |
| 2055 | btrfsic_block_free(block); |
| 2056 | } |
| 2057 | } |
| 2058 | btrfsic_release_block_ctx(&block_ctx); |
| 2059 | } else { |
| 2060 | /* block has not been found in hash table */ |
| 2061 | u64 bytenr; |
| 2062 | |
| 2063 | if (!is_metadata) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2064 | processed_len = state->datablock_size; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2065 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2066 | printk(KERN_INFO "Written block (%s/%llu/?)" |
| 2067 | " !found in hash table, D.\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2068 | dev_state->name, dev_bytenr); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2069 | if (!state->include_extent_data) { |
| 2070 | /* ignore that written D block */ |
| 2071 | goto continue_loop; |
| 2072 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* this is getting ugly for the |
| 2075 | * include_extent_data case... */ |
| 2076 | bytenr = 0; /* unknown */ |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2077 | } else { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2078 | processed_len = state->metablock_size; |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2079 | bytenr = btrfs_stack_header_bytenr( |
| 2080 | (struct btrfs_header *) |
| 2081 | mapped_datav[0]); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2082 | btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2083 | dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2084 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2085 | printk(KERN_INFO |
| 2086 | "Written block @%llu (%s/%llu/?)" |
| 2087 | " !found in hash table, M.\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2088 | bytenr, dev_state->name, dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2089 | } |
Stefan Behrens | f382e46 | 2014-10-16 17:48:48 +0200 | [diff] [blame] | 2090 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2091 | block_ctx.dev = dev_state; |
| 2092 | block_ctx.dev_bytenr = dev_bytenr; |
Stefan Behrens | f382e46 | 2014-10-16 17:48:48 +0200 | [diff] [blame] | 2093 | block_ctx.start = bytenr; |
| 2094 | block_ctx.len = processed_len; |
| 2095 | block_ctx.pagev = NULL; |
| 2096 | block_ctx.mem_to_free = NULL; |
| 2097 | block_ctx.datav = mapped_datav; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2098 | |
| 2099 | block = btrfsic_block_alloc(); |
| 2100 | if (NULL == block) { |
| 2101 | printk(KERN_INFO "btrfsic: error, kmalloc failed!\n"); |
| 2102 | btrfsic_release_block_ctx(&block_ctx); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2103 | goto continue_loop; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2104 | } |
| 2105 | block->dev_state = dev_state; |
| 2106 | block->dev_bytenr = dev_bytenr; |
| 2107 | block->logical_bytenr = bytenr; |
| 2108 | block->is_metadata = is_metadata; |
| 2109 | block->never_written = 0; |
| 2110 | block->iodone_w_error = 0; |
| 2111 | block->mirror_num = 0; /* unknown */ |
| 2112 | block->flush_gen = dev_state->last_flush_gen + 1; |
| 2113 | block->submit_bio_bh_rw = submit_bio_bh_rw; |
| 2114 | if (NULL != bio) { |
| 2115 | block->is_iodone = 0; |
| 2116 | BUG_ON(NULL == bio_is_patched); |
| 2117 | if (!*bio_is_patched) { |
| 2118 | block->orig_bio_bh_private = bio->bi_private; |
| 2119 | block->orig_bio_bh_end_io.bio = bio->bi_end_io; |
| 2120 | block->next_in_same_bio = NULL; |
| 2121 | bio->bi_private = block; |
| 2122 | bio->bi_end_io = btrfsic_bio_end_io; |
| 2123 | *bio_is_patched = 1; |
| 2124 | } else { |
| 2125 | struct btrfsic_block *chained_block = |
| 2126 | (struct btrfsic_block *) |
| 2127 | bio->bi_private; |
| 2128 | |
| 2129 | BUG_ON(NULL == chained_block); |
| 2130 | block->orig_bio_bh_private = |
| 2131 | chained_block->orig_bio_bh_private; |
| 2132 | block->orig_bio_bh_end_io.bio = |
| 2133 | chained_block->orig_bio_bh_end_io.bio; |
| 2134 | block->next_in_same_bio = chained_block; |
| 2135 | bio->bi_private = block; |
| 2136 | } |
| 2137 | } else if (NULL != bh) { |
| 2138 | block->is_iodone = 0; |
| 2139 | block->orig_bio_bh_private = bh->b_private; |
| 2140 | block->orig_bio_bh_end_io.bh = bh->b_end_io; |
| 2141 | block->next_in_same_bio = NULL; |
| 2142 | bh->b_private = block; |
| 2143 | bh->b_end_io = btrfsic_bh_end_io; |
| 2144 | } else { |
| 2145 | block->is_iodone = 1; |
| 2146 | block->orig_bio_bh_private = NULL; |
| 2147 | block->orig_bio_bh_end_io.bio = NULL; |
| 2148 | block->next_in_same_bio = NULL; |
| 2149 | } |
| 2150 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2151 | printk(KERN_INFO |
| 2152 | "New written %c-block @%llu (%s/%llu/%d)\n", |
| 2153 | is_metadata ? 'M' : 'D', |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2154 | block->logical_bytenr, block->dev_state->name, |
| 2155 | block->dev_bytenr, block->mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2156 | list_add(&block->all_blocks_node, &state->all_blocks_list); |
| 2157 | btrfsic_block_hashtable_add(block, &state->block_hashtable); |
| 2158 | |
| 2159 | if (is_metadata) { |
| 2160 | ret = btrfsic_process_metablock(state, block, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2161 | &block_ctx, 0, 0); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2162 | if (ret) |
| 2163 | printk(KERN_INFO |
| 2164 | "btrfsic: process_metablock(root @%llu)" |
| 2165 | " failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2166 | dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2167 | } |
| 2168 | btrfsic_release_block_ctx(&block_ctx); |
| 2169 | } |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2170 | |
| 2171 | continue_loop: |
| 2172 | BUG_ON(!processed_len); |
| 2173 | dev_bytenr += processed_len; |
| 2174 | mapped_datav += processed_len >> PAGE_CACHE_SHIFT; |
| 2175 | num_pages -= processed_len >> PAGE_CACHE_SHIFT; |
| 2176 | goto again; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2179 | static void btrfsic_bio_end_io(struct bio *bp) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2180 | { |
| 2181 | struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private; |
| 2182 | int iodone_w_error; |
| 2183 | |
| 2184 | /* mutex is not held! This is not save if IO is not yet completed |
| 2185 | * on umount */ |
| 2186 | iodone_w_error = 0; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2187 | if (bp->bi_error) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2188 | iodone_w_error = 1; |
| 2189 | |
| 2190 | BUG_ON(NULL == block); |
| 2191 | bp->bi_private = block->orig_bio_bh_private; |
| 2192 | bp->bi_end_io = block->orig_bio_bh_end_io.bio; |
| 2193 | |
| 2194 | do { |
| 2195 | struct btrfsic_block *next_block; |
| 2196 | struct btrfsic_dev_state *const dev_state = block->dev_state; |
| 2197 | |
| 2198 | if ((dev_state->state->print_mask & |
| 2199 | BTRFSIC_PRINT_MASK_END_IO_BIO_BH)) |
| 2200 | printk(KERN_INFO |
| 2201 | "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n", |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2202 | bp->bi_error, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2203 | btrfsic_get_block_type(dev_state->state, block), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2204 | block->logical_bytenr, dev_state->name, |
| 2205 | block->dev_bytenr, block->mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2206 | next_block = block->next_in_same_bio; |
| 2207 | block->iodone_w_error = iodone_w_error; |
| 2208 | if (block->submit_bio_bh_rw & REQ_FLUSH) { |
| 2209 | dev_state->last_flush_gen++; |
| 2210 | if ((dev_state->state->print_mask & |
| 2211 | BTRFSIC_PRINT_MASK_END_IO_BIO_BH)) |
| 2212 | printk(KERN_INFO |
| 2213 | "bio_end_io() new %s flush_gen=%llu\n", |
| 2214 | dev_state->name, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2215 | dev_state->last_flush_gen); |
| 2216 | } |
| 2217 | if (block->submit_bio_bh_rw & REQ_FUA) |
| 2218 | block->flush_gen = 0; /* FUA completed means block is |
| 2219 | * on disk */ |
| 2220 | block->is_iodone = 1; /* for FLUSH, this releases the block */ |
| 2221 | block = next_block; |
| 2222 | } while (NULL != block); |
| 2223 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2224 | bp->bi_end_io(bp); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate) |
| 2228 | { |
| 2229 | struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private; |
| 2230 | int iodone_w_error = !uptodate; |
| 2231 | struct btrfsic_dev_state *dev_state; |
| 2232 | |
| 2233 | BUG_ON(NULL == block); |
| 2234 | dev_state = block->dev_state; |
| 2235 | if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH)) |
| 2236 | printk(KERN_INFO |
| 2237 | "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n", |
| 2238 | iodone_w_error, |
| 2239 | btrfsic_get_block_type(dev_state->state, block), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2240 | block->logical_bytenr, block->dev_state->name, |
| 2241 | block->dev_bytenr, block->mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2242 | |
| 2243 | block->iodone_w_error = iodone_w_error; |
| 2244 | if (block->submit_bio_bh_rw & REQ_FLUSH) { |
| 2245 | dev_state->last_flush_gen++; |
| 2246 | if ((dev_state->state->print_mask & |
| 2247 | BTRFSIC_PRINT_MASK_END_IO_BIO_BH)) |
| 2248 | printk(KERN_INFO |
| 2249 | "bh_end_io() new %s flush_gen=%llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2250 | dev_state->name, dev_state->last_flush_gen); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2251 | } |
| 2252 | if (block->submit_bio_bh_rw & REQ_FUA) |
| 2253 | block->flush_gen = 0; /* FUA completed means block is on disk */ |
| 2254 | |
| 2255 | bh->b_private = block->orig_bio_bh_private; |
| 2256 | bh->b_end_io = block->orig_bio_bh_end_io.bh; |
| 2257 | block->is_iodone = 1; /* for FLUSH, this releases the block */ |
| 2258 | bh->b_end_io(bh, uptodate); |
| 2259 | } |
| 2260 | |
| 2261 | static int btrfsic_process_written_superblock( |
| 2262 | struct btrfsic_state *state, |
| 2263 | struct btrfsic_block *const superblock, |
| 2264 | struct btrfs_super_block *const super_hdr) |
| 2265 | { |
| 2266 | int pass; |
| 2267 | |
| 2268 | superblock->generation = btrfs_super_generation(super_hdr); |
| 2269 | if (!(superblock->generation > state->max_superblock_generation || |
| 2270 | 0 == state->max_superblock_generation)) { |
| 2271 | if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE) |
| 2272 | printk(KERN_INFO |
| 2273 | "btrfsic: superblock @%llu (%s/%llu/%d)" |
| 2274 | " with old gen %llu <= %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2275 | superblock->logical_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2276 | superblock->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2277 | superblock->dev_bytenr, superblock->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2278 | btrfs_super_generation(super_hdr), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2279 | state->max_superblock_generation); |
| 2280 | } else { |
| 2281 | if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE) |
| 2282 | printk(KERN_INFO |
| 2283 | "btrfsic: got new superblock @%llu (%s/%llu/%d)" |
| 2284 | " with new gen %llu > %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2285 | superblock->logical_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2286 | superblock->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2287 | superblock->dev_bytenr, superblock->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2288 | btrfs_super_generation(super_hdr), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2289 | state->max_superblock_generation); |
| 2290 | |
| 2291 | state->max_superblock_generation = |
| 2292 | btrfs_super_generation(super_hdr); |
| 2293 | state->latest_superblock = superblock; |
| 2294 | } |
| 2295 | |
| 2296 | for (pass = 0; pass < 3; pass++) { |
| 2297 | int ret; |
| 2298 | u64 next_bytenr; |
| 2299 | struct btrfsic_block *next_block; |
| 2300 | struct btrfsic_block_data_ctx tmp_next_block_ctx; |
| 2301 | struct btrfsic_block_link *l; |
| 2302 | int num_copies; |
| 2303 | int mirror_num; |
| 2304 | const char *additional_string = NULL; |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 2305 | struct btrfs_disk_key tmp_disk_key = {0}; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2306 | |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2307 | btrfs_set_disk_key_objectid(&tmp_disk_key, |
| 2308 | BTRFS_ROOT_ITEM_KEY); |
| 2309 | btrfs_set_disk_key_objectid(&tmp_disk_key, 0); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2310 | |
| 2311 | switch (pass) { |
| 2312 | case 0: |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2313 | btrfs_set_disk_key_objectid(&tmp_disk_key, |
| 2314 | BTRFS_ROOT_TREE_OBJECTID); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2315 | additional_string = "root "; |
| 2316 | next_bytenr = btrfs_super_root(super_hdr); |
| 2317 | if (state->print_mask & |
| 2318 | BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION) |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2319 | printk(KERN_INFO "root@%llu\n", next_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2320 | break; |
| 2321 | case 1: |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2322 | btrfs_set_disk_key_objectid(&tmp_disk_key, |
| 2323 | BTRFS_CHUNK_TREE_OBJECTID); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2324 | additional_string = "chunk "; |
| 2325 | next_bytenr = btrfs_super_chunk_root(super_hdr); |
| 2326 | if (state->print_mask & |
| 2327 | BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION) |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2328 | printk(KERN_INFO "chunk@%llu\n", next_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2329 | break; |
| 2330 | case 2: |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 2331 | btrfs_set_disk_key_objectid(&tmp_disk_key, |
| 2332 | BTRFS_TREE_LOG_OBJECTID); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2333 | additional_string = "log "; |
| 2334 | next_bytenr = btrfs_super_log_root(super_hdr); |
| 2335 | if (0 == next_bytenr) |
| 2336 | continue; |
| 2337 | if (state->print_mask & |
| 2338 | BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION) |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2339 | printk(KERN_INFO "log@%llu\n", next_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2340 | break; |
| 2341 | } |
| 2342 | |
| 2343 | num_copies = |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 2344 | btrfs_num_copies(state->root->fs_info, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2345 | next_bytenr, BTRFS_SUPER_INFO_SIZE); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2346 | if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) |
| 2347 | printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2348 | next_bytenr, num_copies); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2349 | for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { |
| 2350 | int was_created; |
| 2351 | |
| 2352 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2353 | printk(KERN_INFO |
| 2354 | "btrfsic_process_written_superblock(" |
| 2355 | "mirror_num=%d)\n", mirror_num); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2356 | ret = btrfsic_map_block(state, next_bytenr, |
| 2357 | BTRFS_SUPER_INFO_SIZE, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2358 | &tmp_next_block_ctx, |
| 2359 | mirror_num); |
| 2360 | if (ret) { |
| 2361 | printk(KERN_INFO |
| 2362 | "btrfsic: btrfsic_map_block(@%llu," |
| 2363 | " mirror=%d) failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2364 | next_bytenr, mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2365 | return -1; |
| 2366 | } |
| 2367 | |
| 2368 | next_block = btrfsic_block_lookup_or_add( |
| 2369 | state, |
| 2370 | &tmp_next_block_ctx, |
| 2371 | additional_string, |
| 2372 | 1, 0, 1, |
| 2373 | mirror_num, |
| 2374 | &was_created); |
| 2375 | if (NULL == next_block) { |
| 2376 | printk(KERN_INFO |
| 2377 | "btrfsic: error, kmalloc failed!\n"); |
| 2378 | btrfsic_release_block_ctx(&tmp_next_block_ctx); |
| 2379 | return -1; |
| 2380 | } |
| 2381 | |
| 2382 | next_block->disk_key = tmp_disk_key; |
| 2383 | if (was_created) |
| 2384 | next_block->generation = |
| 2385 | BTRFSIC_GENERATION_UNKNOWN; |
| 2386 | l = btrfsic_block_link_lookup_or_add( |
| 2387 | state, |
| 2388 | &tmp_next_block_ctx, |
| 2389 | next_block, |
| 2390 | superblock, |
| 2391 | BTRFSIC_GENERATION_UNKNOWN); |
| 2392 | btrfsic_release_block_ctx(&tmp_next_block_ctx); |
| 2393 | if (NULL == l) |
| 2394 | return -1; |
| 2395 | } |
| 2396 | } |
| 2397 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2398 | if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0))) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2399 | btrfsic_dump_tree(state); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2400 | |
| 2401 | return 0; |
| 2402 | } |
| 2403 | |
| 2404 | static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state, |
| 2405 | struct btrfsic_block *const block, |
| 2406 | int recursion_level) |
| 2407 | { |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 2408 | const struct btrfsic_block_link *l; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2409 | int ret = 0; |
| 2410 | |
| 2411 | if (recursion_level >= 3 + BTRFS_MAX_LEVEL) { |
| 2412 | /* |
| 2413 | * Note that this situation can happen and does not |
| 2414 | * indicate an error in regular cases. It happens |
| 2415 | * when disk blocks are freed and later reused. |
| 2416 | * The check-integrity module is not aware of any |
| 2417 | * block free operations, it just recognizes block |
| 2418 | * write operations. Therefore it keeps the linkage |
| 2419 | * information for a block until a block is |
| 2420 | * rewritten. This can temporarily cause incorrect |
| 2421 | * and even circular linkage informations. This |
| 2422 | * causes no harm unless such blocks are referenced |
| 2423 | * by the most recent super block. |
| 2424 | */ |
| 2425 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2426 | printk(KERN_INFO |
| 2427 | "btrfsic: abort cyclic linkage (case 1).\n"); |
| 2428 | |
| 2429 | return ret; |
| 2430 | } |
| 2431 | |
| 2432 | /* |
| 2433 | * This algorithm is recursive because the amount of used stack |
| 2434 | * space is very small and the max recursion depth is limited. |
| 2435 | */ |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 2436 | list_for_each_entry(l, &block->ref_to_list, node_ref_to) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2437 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2438 | printk(KERN_INFO |
| 2439 | "rl=%d, %c @%llu (%s/%llu/%d)" |
| 2440 | " %u* refers to %c @%llu (%s/%llu/%d)\n", |
| 2441 | recursion_level, |
| 2442 | btrfsic_get_block_type(state, block), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2443 | block->logical_bytenr, block->dev_state->name, |
| 2444 | block->dev_bytenr, block->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2445 | l->ref_cnt, |
| 2446 | btrfsic_get_block_type(state, l->block_ref_to), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2447 | l->block_ref_to->logical_bytenr, |
| 2448 | l->block_ref_to->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2449 | l->block_ref_to->dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2450 | l->block_ref_to->mirror_num); |
| 2451 | if (l->block_ref_to->never_written) { |
| 2452 | printk(KERN_INFO "btrfs: attempt to write superblock" |
| 2453 | " which references block %c @%llu (%s/%llu/%d)" |
| 2454 | " which is never written!\n", |
| 2455 | btrfsic_get_block_type(state, l->block_ref_to), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2456 | l->block_ref_to->logical_bytenr, |
| 2457 | l->block_ref_to->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2458 | l->block_ref_to->dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2459 | l->block_ref_to->mirror_num); |
| 2460 | ret = -1; |
| 2461 | } else if (!l->block_ref_to->is_iodone) { |
| 2462 | printk(KERN_INFO "btrfs: attempt to write superblock" |
| 2463 | " which references block %c @%llu (%s/%llu/%d)" |
| 2464 | " which is not yet iodone!\n", |
| 2465 | btrfsic_get_block_type(state, l->block_ref_to), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2466 | l->block_ref_to->logical_bytenr, |
| 2467 | l->block_ref_to->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2468 | l->block_ref_to->dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2469 | l->block_ref_to->mirror_num); |
| 2470 | ret = -1; |
Stefan Behrens | 62856a9 | 2012-07-31 11:09:44 -0600 | [diff] [blame] | 2471 | } else if (l->block_ref_to->iodone_w_error) { |
| 2472 | printk(KERN_INFO "btrfs: attempt to write superblock" |
| 2473 | " which references block %c @%llu (%s/%llu/%d)" |
| 2474 | " which has write error!\n", |
| 2475 | btrfsic_get_block_type(state, l->block_ref_to), |
Stefan Behrens | 62856a9 | 2012-07-31 11:09:44 -0600 | [diff] [blame] | 2476 | l->block_ref_to->logical_bytenr, |
| 2477 | l->block_ref_to->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2478 | l->block_ref_to->dev_bytenr, |
Stefan Behrens | 62856a9 | 2012-07-31 11:09:44 -0600 | [diff] [blame] | 2479 | l->block_ref_to->mirror_num); |
| 2480 | ret = -1; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2481 | } else if (l->parent_generation != |
| 2482 | l->block_ref_to->generation && |
| 2483 | BTRFSIC_GENERATION_UNKNOWN != |
| 2484 | l->parent_generation && |
| 2485 | BTRFSIC_GENERATION_UNKNOWN != |
| 2486 | l->block_ref_to->generation) { |
| 2487 | printk(KERN_INFO "btrfs: attempt to write superblock" |
| 2488 | " which references block %c @%llu (%s/%llu/%d)" |
| 2489 | " with generation %llu !=" |
| 2490 | " parent generation %llu!\n", |
| 2491 | btrfsic_get_block_type(state, l->block_ref_to), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2492 | l->block_ref_to->logical_bytenr, |
| 2493 | l->block_ref_to->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2494 | l->block_ref_to->dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2495 | l->block_ref_to->mirror_num, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2496 | l->block_ref_to->generation, |
| 2497 | l->parent_generation); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2498 | ret = -1; |
| 2499 | } else if (l->block_ref_to->flush_gen > |
| 2500 | l->block_ref_to->dev_state->last_flush_gen) { |
| 2501 | printk(KERN_INFO "btrfs: attempt to write superblock" |
| 2502 | " which references block %c @%llu (%s/%llu/%d)" |
| 2503 | " which is not flushed out of disk's write cache" |
| 2504 | " (block flush_gen=%llu," |
| 2505 | " dev->flush_gen=%llu)!\n", |
| 2506 | btrfsic_get_block_type(state, l->block_ref_to), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2507 | l->block_ref_to->logical_bytenr, |
| 2508 | l->block_ref_to->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2509 | l->block_ref_to->dev_bytenr, |
| 2510 | l->block_ref_to->mirror_num, block->flush_gen, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2511 | l->block_ref_to->dev_state->last_flush_gen); |
| 2512 | ret = -1; |
| 2513 | } else if (-1 == btrfsic_check_all_ref_blocks(state, |
| 2514 | l->block_ref_to, |
| 2515 | recursion_level + |
| 2516 | 1)) { |
| 2517 | ret = -1; |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | return ret; |
| 2522 | } |
| 2523 | |
| 2524 | static int btrfsic_is_block_ref_by_superblock( |
| 2525 | const struct btrfsic_state *state, |
| 2526 | const struct btrfsic_block *block, |
| 2527 | int recursion_level) |
| 2528 | { |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 2529 | const struct btrfsic_block_link *l; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2530 | |
| 2531 | if (recursion_level >= 3 + BTRFS_MAX_LEVEL) { |
| 2532 | /* refer to comment at "abort cyclic linkage (case 1)" */ |
| 2533 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2534 | printk(KERN_INFO |
| 2535 | "btrfsic: abort cyclic linkage (case 2).\n"); |
| 2536 | |
| 2537 | return 0; |
| 2538 | } |
| 2539 | |
| 2540 | /* |
| 2541 | * This algorithm is recursive because the amount of used stack space |
| 2542 | * is very small and the max recursion depth is limited. |
| 2543 | */ |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 2544 | list_for_each_entry(l, &block->ref_from_list, node_ref_from) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2545 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2546 | printk(KERN_INFO |
| 2547 | "rl=%d, %c @%llu (%s/%llu/%d)" |
| 2548 | " is ref %u* from %c @%llu (%s/%llu/%d)\n", |
| 2549 | recursion_level, |
| 2550 | btrfsic_get_block_type(state, block), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2551 | block->logical_bytenr, block->dev_state->name, |
| 2552 | block->dev_bytenr, block->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2553 | l->ref_cnt, |
| 2554 | btrfsic_get_block_type(state, l->block_ref_from), |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2555 | l->block_ref_from->logical_bytenr, |
| 2556 | l->block_ref_from->dev_state->name, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2557 | l->block_ref_from->dev_bytenr, |
| 2558 | l->block_ref_from->mirror_num); |
| 2559 | if (l->block_ref_from->is_superblock && |
| 2560 | state->latest_superblock->dev_bytenr == |
| 2561 | l->block_ref_from->dev_bytenr && |
| 2562 | state->latest_superblock->dev_state->bdev == |
| 2563 | l->block_ref_from->dev_state->bdev) |
| 2564 | return 1; |
| 2565 | else if (btrfsic_is_block_ref_by_superblock(state, |
| 2566 | l->block_ref_from, |
| 2567 | recursion_level + |
| 2568 | 1)) |
| 2569 | return 1; |
| 2570 | } |
| 2571 | |
| 2572 | return 0; |
| 2573 | } |
| 2574 | |
| 2575 | static void btrfsic_print_add_link(const struct btrfsic_state *state, |
| 2576 | const struct btrfsic_block_link *l) |
| 2577 | { |
| 2578 | printk(KERN_INFO |
| 2579 | "Add %u* link from %c @%llu (%s/%llu/%d)" |
| 2580 | " to %c @%llu (%s/%llu/%d).\n", |
| 2581 | l->ref_cnt, |
| 2582 | btrfsic_get_block_type(state, l->block_ref_from), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2583 | l->block_ref_from->logical_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2584 | l->block_ref_from->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2585 | l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2586 | btrfsic_get_block_type(state, l->block_ref_to), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2587 | l->block_ref_to->logical_bytenr, |
| 2588 | l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2589 | l->block_ref_to->mirror_num); |
| 2590 | } |
| 2591 | |
| 2592 | static void btrfsic_print_rem_link(const struct btrfsic_state *state, |
| 2593 | const struct btrfsic_block_link *l) |
| 2594 | { |
| 2595 | printk(KERN_INFO |
| 2596 | "Rem %u* link from %c @%llu (%s/%llu/%d)" |
| 2597 | " to %c @%llu (%s/%llu/%d).\n", |
| 2598 | l->ref_cnt, |
| 2599 | btrfsic_get_block_type(state, l->block_ref_from), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2600 | l->block_ref_from->logical_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2601 | l->block_ref_from->dev_state->name, |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2602 | l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2603 | btrfsic_get_block_type(state, l->block_ref_to), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2604 | l->block_ref_to->logical_bytenr, |
| 2605 | l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2606 | l->block_ref_to->mirror_num); |
| 2607 | } |
| 2608 | |
| 2609 | static char btrfsic_get_block_type(const struct btrfsic_state *state, |
| 2610 | const struct btrfsic_block *block) |
| 2611 | { |
| 2612 | if (block->is_superblock && |
| 2613 | state->latest_superblock->dev_bytenr == block->dev_bytenr && |
| 2614 | state->latest_superblock->dev_state->bdev == block->dev_state->bdev) |
| 2615 | return 'S'; |
| 2616 | else if (block->is_superblock) |
| 2617 | return 's'; |
| 2618 | else if (block->is_metadata) |
| 2619 | return 'M'; |
| 2620 | else |
| 2621 | return 'D'; |
| 2622 | } |
| 2623 | |
| 2624 | static void btrfsic_dump_tree(const struct btrfsic_state *state) |
| 2625 | { |
| 2626 | btrfsic_dump_tree_sub(state, state->latest_superblock, 0); |
| 2627 | } |
| 2628 | |
| 2629 | static void btrfsic_dump_tree_sub(const struct btrfsic_state *state, |
| 2630 | const struct btrfsic_block *block, |
| 2631 | int indent_level) |
| 2632 | { |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 2633 | const struct btrfsic_block_link *l; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2634 | int indent_add; |
| 2635 | static char buf[80]; |
| 2636 | int cursor_position; |
| 2637 | |
| 2638 | /* |
| 2639 | * Should better fill an on-stack buffer with a complete line and |
| 2640 | * dump it at once when it is time to print a newline character. |
| 2641 | */ |
| 2642 | |
| 2643 | /* |
| 2644 | * This algorithm is recursive because the amount of used stack space |
| 2645 | * is very small and the max recursion depth is limited. |
| 2646 | */ |
| 2647 | indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)", |
| 2648 | btrfsic_get_block_type(state, block), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2649 | block->logical_bytenr, block->dev_state->name, |
| 2650 | block->dev_bytenr, block->mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2651 | if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) { |
| 2652 | printk("[...]\n"); |
| 2653 | return; |
| 2654 | } |
| 2655 | printk(buf); |
| 2656 | indent_level += indent_add; |
| 2657 | if (list_empty(&block->ref_to_list)) { |
| 2658 | printk("\n"); |
| 2659 | return; |
| 2660 | } |
| 2661 | if (block->mirror_num > 1 && |
| 2662 | !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) { |
| 2663 | printk(" [...]\n"); |
| 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | cursor_position = indent_level; |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 2668 | list_for_each_entry(l, &block->ref_to_list, node_ref_to) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2669 | while (cursor_position < indent_level) { |
| 2670 | printk(" "); |
| 2671 | cursor_position++; |
| 2672 | } |
| 2673 | if (l->ref_cnt > 1) |
| 2674 | indent_add = sprintf(buf, " %d*--> ", l->ref_cnt); |
| 2675 | else |
| 2676 | indent_add = sprintf(buf, " --> "); |
| 2677 | if (indent_level + indent_add > |
| 2678 | BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) { |
| 2679 | printk("[...]\n"); |
| 2680 | cursor_position = 0; |
| 2681 | continue; |
| 2682 | } |
| 2683 | |
| 2684 | printk(buf); |
| 2685 | |
| 2686 | btrfsic_dump_tree_sub(state, l->block_ref_to, |
| 2687 | indent_level + indent_add); |
| 2688 | cursor_position = 0; |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add( |
| 2693 | struct btrfsic_state *state, |
| 2694 | struct btrfsic_block_data_ctx *next_block_ctx, |
| 2695 | struct btrfsic_block *next_block, |
| 2696 | struct btrfsic_block *from_block, |
| 2697 | u64 parent_generation) |
| 2698 | { |
| 2699 | struct btrfsic_block_link *l; |
| 2700 | |
| 2701 | l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev, |
| 2702 | next_block_ctx->dev_bytenr, |
| 2703 | from_block->dev_state->bdev, |
| 2704 | from_block->dev_bytenr, |
| 2705 | &state->block_link_hashtable); |
| 2706 | if (NULL == l) { |
| 2707 | l = btrfsic_block_link_alloc(); |
| 2708 | if (NULL == l) { |
| 2709 | printk(KERN_INFO |
| 2710 | "btrfsic: error, kmalloc" " failed!\n"); |
| 2711 | return NULL; |
| 2712 | } |
| 2713 | |
| 2714 | l->block_ref_to = next_block; |
| 2715 | l->block_ref_from = from_block; |
| 2716 | l->ref_cnt = 1; |
| 2717 | l->parent_generation = parent_generation; |
| 2718 | |
| 2719 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2720 | btrfsic_print_add_link(state, l); |
| 2721 | |
| 2722 | list_add(&l->node_ref_to, &from_block->ref_to_list); |
| 2723 | list_add(&l->node_ref_from, &next_block->ref_from_list); |
| 2724 | |
| 2725 | btrfsic_block_link_hashtable_add(l, |
| 2726 | &state->block_link_hashtable); |
| 2727 | } else { |
| 2728 | l->ref_cnt++; |
| 2729 | l->parent_generation = parent_generation; |
| 2730 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2731 | btrfsic_print_add_link(state, l); |
| 2732 | } |
| 2733 | |
| 2734 | return l; |
| 2735 | } |
| 2736 | |
| 2737 | static struct btrfsic_block *btrfsic_block_lookup_or_add( |
| 2738 | struct btrfsic_state *state, |
| 2739 | struct btrfsic_block_data_ctx *block_ctx, |
| 2740 | const char *additional_string, |
| 2741 | int is_metadata, |
| 2742 | int is_iodone, |
| 2743 | int never_written, |
| 2744 | int mirror_num, |
| 2745 | int *was_created) |
| 2746 | { |
| 2747 | struct btrfsic_block *block; |
| 2748 | |
| 2749 | block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev, |
| 2750 | block_ctx->dev_bytenr, |
| 2751 | &state->block_hashtable); |
| 2752 | if (NULL == block) { |
| 2753 | struct btrfsic_dev_state *dev_state; |
| 2754 | |
| 2755 | block = btrfsic_block_alloc(); |
| 2756 | if (NULL == block) { |
| 2757 | printk(KERN_INFO "btrfsic: error, kmalloc failed!\n"); |
| 2758 | return NULL; |
| 2759 | } |
| 2760 | dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev); |
| 2761 | if (NULL == dev_state) { |
| 2762 | printk(KERN_INFO |
| 2763 | "btrfsic: error, lookup dev_state failed!\n"); |
| 2764 | btrfsic_block_free(block); |
| 2765 | return NULL; |
| 2766 | } |
| 2767 | block->dev_state = dev_state; |
| 2768 | block->dev_bytenr = block_ctx->dev_bytenr; |
| 2769 | block->logical_bytenr = block_ctx->start; |
| 2770 | block->is_metadata = is_metadata; |
| 2771 | block->is_iodone = is_iodone; |
| 2772 | block->never_written = never_written; |
| 2773 | block->mirror_num = mirror_num; |
| 2774 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 2775 | printk(KERN_INFO |
| 2776 | "New %s%c-block @%llu (%s/%llu/%d)\n", |
| 2777 | additional_string, |
| 2778 | btrfsic_get_block_type(state, block), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2779 | block->logical_bytenr, dev_state->name, |
| 2780 | block->dev_bytenr, mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2781 | list_add(&block->all_blocks_node, &state->all_blocks_list); |
| 2782 | btrfsic_block_hashtable_add(block, &state->block_hashtable); |
| 2783 | if (NULL != was_created) |
| 2784 | *was_created = 1; |
| 2785 | } else { |
| 2786 | if (NULL != was_created) |
| 2787 | *was_created = 0; |
| 2788 | } |
| 2789 | |
| 2790 | return block; |
| 2791 | } |
| 2792 | |
| 2793 | static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state, |
| 2794 | u64 bytenr, |
| 2795 | struct btrfsic_dev_state *dev_state, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2796 | u64 dev_bytenr) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2797 | { |
| 2798 | int num_copies; |
| 2799 | int mirror_num; |
| 2800 | int ret; |
| 2801 | struct btrfsic_block_data_ctx block_ctx; |
| 2802 | int match = 0; |
| 2803 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 2804 | num_copies = btrfs_num_copies(state->root->fs_info, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2805 | bytenr, state->metablock_size); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2806 | |
| 2807 | for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2808 | ret = btrfsic_map_block(state, bytenr, state->metablock_size, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2809 | &block_ctx, mirror_num); |
| 2810 | if (ret) { |
| 2811 | printk(KERN_INFO "btrfsic:" |
| 2812 | " btrfsic_map_block(logical @%llu," |
| 2813 | " mirror %d) failed!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2814 | bytenr, mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2815 | continue; |
| 2816 | } |
| 2817 | |
| 2818 | if (dev_state->bdev == block_ctx.dev->bdev && |
| 2819 | dev_bytenr == block_ctx.dev_bytenr) { |
| 2820 | match++; |
| 2821 | btrfsic_release_block_ctx(&block_ctx); |
| 2822 | break; |
| 2823 | } |
| 2824 | btrfsic_release_block_ctx(&block_ctx); |
| 2825 | } |
| 2826 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2827 | if (WARN_ON(!match)) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2828 | printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio," |
| 2829 | " buffer->log_bytenr=%llu, submit_bio(bdev=%s," |
| 2830 | " phys_bytenr=%llu)!\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2831 | bytenr, dev_state->name, dev_bytenr); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2832 | for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2833 | ret = btrfsic_map_block(state, bytenr, |
| 2834 | state->metablock_size, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2835 | &block_ctx, mirror_num); |
| 2836 | if (ret) |
| 2837 | continue; |
| 2838 | |
| 2839 | printk(KERN_INFO "Read logical bytenr @%llu maps to" |
| 2840 | " (%s/%llu/%d)\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 2841 | bytenr, block_ctx.dev->name, |
| 2842 | block_ctx.dev_bytenr, mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2843 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | static struct btrfsic_dev_state *btrfsic_dev_state_lookup( |
| 2848 | struct block_device *bdev) |
| 2849 | { |
| 2850 | struct btrfsic_dev_state *ds; |
| 2851 | |
| 2852 | ds = btrfsic_dev_state_hashtable_lookup(bdev, |
| 2853 | &btrfsic_dev_state_hashtable); |
| 2854 | return ds; |
| 2855 | } |
| 2856 | |
| 2857 | int btrfsic_submit_bh(int rw, struct buffer_head *bh) |
| 2858 | { |
| 2859 | struct btrfsic_dev_state *dev_state; |
| 2860 | |
| 2861 | if (!btrfsic_is_initialized) |
| 2862 | return submit_bh(rw, bh); |
| 2863 | |
| 2864 | mutex_lock(&btrfsic_mutex); |
| 2865 | /* since btrfsic_submit_bh() might also be called before |
| 2866 | * btrfsic_mount(), this might return NULL */ |
| 2867 | dev_state = btrfsic_dev_state_lookup(bh->b_bdev); |
| 2868 | |
| 2869 | /* Only called to write the superblock (incl. FLUSH/FUA) */ |
| 2870 | if (NULL != dev_state && |
| 2871 | (rw & WRITE) && bh->b_size > 0) { |
| 2872 | u64 dev_bytenr; |
| 2873 | |
| 2874 | dev_bytenr = 4096 * bh->b_blocknr; |
| 2875 | if (dev_state->state->print_mask & |
| 2876 | BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH) |
| 2877 | printk(KERN_INFO |
Geert Uytterhoeven | fce2936 | 2013-08-20 13:20:17 +0200 | [diff] [blame] | 2878 | "submit_bh(rw=0x%x, blocknr=%llu (bytenr %llu)," |
Geert Uytterhoeven | 8d78eb1 | 2013-08-20 13:20:18 +0200 | [diff] [blame] | 2879 | " size=%zu, data=%p, bdev=%p)\n", |
Geert Uytterhoeven | fce2936 | 2013-08-20 13:20:17 +0200 | [diff] [blame] | 2880 | rw, (unsigned long long)bh->b_blocknr, |
Geert Uytterhoeven | 8d78eb1 | 2013-08-20 13:20:18 +0200 | [diff] [blame] | 2881 | dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2882 | btrfsic_process_written_block(dev_state, dev_bytenr, |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2883 | &bh->b_data, 1, NULL, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2884 | NULL, bh, rw); |
| 2885 | } else if (NULL != dev_state && (rw & REQ_FLUSH)) { |
| 2886 | if (dev_state->state->print_mask & |
| 2887 | BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH) |
| 2888 | printk(KERN_INFO |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2889 | "submit_bh(rw=0x%x FLUSH, bdev=%p)\n", |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2890 | rw, bh->b_bdev); |
| 2891 | if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) { |
| 2892 | if ((dev_state->state->print_mask & |
| 2893 | (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH | |
| 2894 | BTRFSIC_PRINT_MASK_VERBOSE))) |
| 2895 | printk(KERN_INFO |
| 2896 | "btrfsic_submit_bh(%s) with FLUSH" |
| 2897 | " but dummy block already in use" |
| 2898 | " (ignored)!\n", |
| 2899 | dev_state->name); |
| 2900 | } else { |
| 2901 | struct btrfsic_block *const block = |
| 2902 | &dev_state->dummy_block_for_bio_bh_flush; |
| 2903 | |
| 2904 | block->is_iodone = 0; |
| 2905 | block->never_written = 0; |
| 2906 | block->iodone_w_error = 0; |
| 2907 | block->flush_gen = dev_state->last_flush_gen + 1; |
| 2908 | block->submit_bio_bh_rw = rw; |
| 2909 | block->orig_bio_bh_private = bh->b_private; |
| 2910 | block->orig_bio_bh_end_io.bh = bh->b_end_io; |
| 2911 | block->next_in_same_bio = NULL; |
| 2912 | bh->b_private = block; |
| 2913 | bh->b_end_io = btrfsic_bh_end_io; |
| 2914 | } |
| 2915 | } |
| 2916 | mutex_unlock(&btrfsic_mutex); |
| 2917 | return submit_bh(rw, bh); |
| 2918 | } |
| 2919 | |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 2920 | static void __btrfsic_submit_bio(int rw, struct bio *bio) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2921 | { |
| 2922 | struct btrfsic_dev_state *dev_state; |
| 2923 | |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 2924 | if (!btrfsic_is_initialized) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2925 | return; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2926 | |
| 2927 | mutex_lock(&btrfsic_mutex); |
| 2928 | /* since btrfsic_submit_bio() is also called before |
| 2929 | * btrfsic_mount(), this might return NULL */ |
| 2930 | dev_state = btrfsic_dev_state_lookup(bio->bi_bdev); |
| 2931 | if (NULL != dev_state && |
| 2932 | (rw & WRITE) && NULL != bio->bi_io_vec) { |
| 2933 | unsigned int i; |
| 2934 | u64 dev_bytenr; |
Stefan Behrens | 56d140f | 2013-11-13 17:19:08 +0100 | [diff] [blame] | 2935 | u64 cur_bytenr; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2936 | int bio_is_patched; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2937 | char **mapped_datav; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2938 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2939 | dev_bytenr = 512 * bio->bi_iter.bi_sector; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2940 | bio_is_patched = 0; |
| 2941 | if (dev_state->state->print_mask & |
| 2942 | BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH) |
| 2943 | printk(KERN_INFO |
| 2944 | "submit_bio(rw=0x%x, bi_vcnt=%u," |
Geert Uytterhoeven | fce2936 | 2013-08-20 13:20:17 +0200 | [diff] [blame] | 2945 | " bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n", |
| 2946 | rw, bio->bi_vcnt, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2947 | (unsigned long long)bio->bi_iter.bi_sector, |
| 2948 | dev_bytenr, bio->bi_bdev); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2949 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 2950 | mapped_datav = kmalloc_array(bio->bi_vcnt, |
| 2951 | sizeof(*mapped_datav), GFP_NOFS); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2952 | if (!mapped_datav) |
| 2953 | goto leave; |
Stefan Behrens | 56d140f | 2013-11-13 17:19:08 +0100 | [diff] [blame] | 2954 | cur_bytenr = dev_bytenr; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2955 | for (i = 0; i < bio->bi_vcnt; i++) { |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2956 | BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE); |
| 2957 | mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page); |
| 2958 | if (!mapped_datav[i]) { |
| 2959 | while (i > 0) { |
| 2960 | i--; |
| 2961 | kunmap(bio->bi_io_vec[i].bv_page); |
| 2962 | } |
| 2963 | kfree(mapped_datav); |
| 2964 | goto leave; |
| 2965 | } |
Stefan Behrens | 56d140f | 2013-11-13 17:19:08 +0100 | [diff] [blame] | 2966 | if (dev_state->state->print_mask & |
| 2967 | BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2968 | printk(KERN_INFO |
Stefan Behrens | 56d140f | 2013-11-13 17:19:08 +0100 | [diff] [blame] | 2969 | "#%u: bytenr=%llu, len=%u, offset=%u\n", |
| 2970 | i, cur_bytenr, bio->bi_io_vec[i].bv_len, |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2971 | bio->bi_io_vec[i].bv_offset); |
Stefan Behrens | 56d140f | 2013-11-13 17:19:08 +0100 | [diff] [blame] | 2972 | cur_bytenr += bio->bi_io_vec[i].bv_len; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2973 | } |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2974 | btrfsic_process_written_block(dev_state, dev_bytenr, |
| 2975 | mapped_datav, bio->bi_vcnt, |
| 2976 | bio, &bio_is_patched, |
| 2977 | NULL, rw); |
| 2978 | while (i > 0) { |
| 2979 | i--; |
| 2980 | kunmap(bio->bi_io_vec[i].bv_page); |
| 2981 | } |
| 2982 | kfree(mapped_datav); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2983 | } else if (NULL != dev_state && (rw & REQ_FLUSH)) { |
| 2984 | if (dev_state->state->print_mask & |
| 2985 | BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH) |
| 2986 | printk(KERN_INFO |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 2987 | "submit_bio(rw=0x%x FLUSH, bdev=%p)\n", |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 2988 | rw, bio->bi_bdev); |
| 2989 | if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) { |
| 2990 | if ((dev_state->state->print_mask & |
| 2991 | (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH | |
| 2992 | BTRFSIC_PRINT_MASK_VERBOSE))) |
| 2993 | printk(KERN_INFO |
| 2994 | "btrfsic_submit_bio(%s) with FLUSH" |
| 2995 | " but dummy block already in use" |
| 2996 | " (ignored)!\n", |
| 2997 | dev_state->name); |
| 2998 | } else { |
| 2999 | struct btrfsic_block *const block = |
| 3000 | &dev_state->dummy_block_for_bio_bh_flush; |
| 3001 | |
| 3002 | block->is_iodone = 0; |
| 3003 | block->never_written = 0; |
| 3004 | block->iodone_w_error = 0; |
| 3005 | block->flush_gen = dev_state->last_flush_gen + 1; |
| 3006 | block->submit_bio_bh_rw = rw; |
| 3007 | block->orig_bio_bh_private = bio->bi_private; |
| 3008 | block->orig_bio_bh_end_io.bio = bio->bi_end_io; |
| 3009 | block->next_in_same_bio = NULL; |
| 3010 | bio->bi_private = block; |
| 3011 | bio->bi_end_io = btrfsic_bio_end_io; |
| 3012 | } |
| 3013 | } |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 3014 | leave: |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3015 | mutex_unlock(&btrfsic_mutex); |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 3016 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3017 | |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 3018 | void btrfsic_submit_bio(int rw, struct bio *bio) |
| 3019 | { |
| 3020 | __btrfsic_submit_bio(rw, bio); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3021 | submit_bio(rw, bio); |
| 3022 | } |
| 3023 | |
Kent Overstreet | 33879d4 | 2013-11-23 22:33:32 -0800 | [diff] [blame] | 3024 | int btrfsic_submit_bio_wait(int rw, struct bio *bio) |
| 3025 | { |
| 3026 | __btrfsic_submit_bio(rw, bio); |
| 3027 | return submit_bio_wait(rw, bio); |
| 3028 | } |
| 3029 | |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3030 | int btrfsic_mount(struct btrfs_root *root, |
| 3031 | struct btrfs_fs_devices *fs_devices, |
| 3032 | int including_extent_data, u32 print_mask) |
| 3033 | { |
| 3034 | int ret; |
| 3035 | struct btrfsic_state *state; |
| 3036 | struct list_head *dev_head = &fs_devices->devices; |
| 3037 | struct btrfs_device *device; |
| 3038 | |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 3039 | if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) { |
| 3040 | printk(KERN_INFO |
| 3041 | "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n", |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 3042 | root->nodesize, PAGE_CACHE_SIZE); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 3043 | return -1; |
| 3044 | } |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 3045 | if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) { |
| 3046 | printk(KERN_INFO |
| 3047 | "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n", |
Geert Uytterhoeven | 778746b | 2013-08-20 13:20:16 +0200 | [diff] [blame] | 3048 | root->sectorsize, PAGE_CACHE_SIZE); |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 3049 | return -1; |
| 3050 | } |
Shilong Wang | 6b3a4d6 | 2014-10-10 17:35:59 -0400 | [diff] [blame] | 3051 | state = kzalloc(sizeof(*state), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); |
| 3052 | if (!state) { |
| 3053 | state = vzalloc(sizeof(*state)); |
| 3054 | if (!state) { |
| 3055 | printk(KERN_INFO "btrfs check-integrity: vzalloc() failed!\n"); |
| 3056 | return -1; |
| 3057 | } |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | if (!btrfsic_is_initialized) { |
| 3061 | mutex_init(&btrfsic_mutex); |
| 3062 | btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable); |
| 3063 | btrfsic_is_initialized = 1; |
| 3064 | } |
| 3065 | mutex_lock(&btrfsic_mutex); |
| 3066 | state->root = root; |
| 3067 | state->print_mask = print_mask; |
| 3068 | state->include_extent_data = including_extent_data; |
| 3069 | state->csum_size = 0; |
Stefan Behrens | e06baab | 2012-04-12 12:53:40 +0200 | [diff] [blame] | 3070 | state->metablock_size = root->nodesize; |
| 3071 | state->datablock_size = root->sectorsize; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3072 | INIT_LIST_HEAD(&state->all_blocks_list); |
| 3073 | btrfsic_block_hashtable_init(&state->block_hashtable); |
| 3074 | btrfsic_block_link_hashtable_init(&state->block_link_hashtable); |
| 3075 | state->max_superblock_generation = 0; |
| 3076 | state->latest_superblock = NULL; |
| 3077 | |
| 3078 | list_for_each_entry(device, dev_head, dev_list) { |
| 3079 | struct btrfsic_dev_state *ds; |
Rasmus Villemoes | 02def69 | 2015-11-27 09:42:11 +0100 | [diff] [blame^] | 3080 | const char *p; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3081 | |
| 3082 | if (!device->bdev || !device->name) |
| 3083 | continue; |
| 3084 | |
| 3085 | ds = btrfsic_dev_state_alloc(); |
| 3086 | if (NULL == ds) { |
| 3087 | printk(KERN_INFO |
| 3088 | "btrfs check-integrity: kmalloc() failed!\n"); |
| 3089 | mutex_unlock(&btrfsic_mutex); |
| 3090 | return -1; |
| 3091 | } |
| 3092 | ds->bdev = device->bdev; |
| 3093 | ds->state = state; |
| 3094 | bdevname(ds->bdev, ds->name); |
| 3095 | ds->name[BDEVNAME_SIZE - 1] = '\0'; |
Rasmus Villemoes | 02def69 | 2015-11-27 09:42:11 +0100 | [diff] [blame^] | 3096 | p = kbasename(ds->name); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3097 | strlcpy(ds->name, p, sizeof(ds->name)); |
| 3098 | btrfsic_dev_state_hashtable_add(ds, |
| 3099 | &btrfsic_dev_state_hashtable); |
| 3100 | } |
| 3101 | |
| 3102 | ret = btrfsic_process_superblock(state, fs_devices); |
| 3103 | if (0 != ret) { |
| 3104 | mutex_unlock(&btrfsic_mutex); |
| 3105 | btrfsic_unmount(root, fs_devices); |
| 3106 | return ret; |
| 3107 | } |
| 3108 | |
| 3109 | if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE) |
| 3110 | btrfsic_dump_database(state); |
| 3111 | if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE) |
| 3112 | btrfsic_dump_tree(state); |
| 3113 | |
| 3114 | mutex_unlock(&btrfsic_mutex); |
| 3115 | return 0; |
| 3116 | } |
| 3117 | |
| 3118 | void btrfsic_unmount(struct btrfs_root *root, |
| 3119 | struct btrfs_fs_devices *fs_devices) |
| 3120 | { |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 3121 | struct btrfsic_block *b_all, *tmp_all; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3122 | struct btrfsic_state *state; |
| 3123 | struct list_head *dev_head = &fs_devices->devices; |
| 3124 | struct btrfs_device *device; |
| 3125 | |
| 3126 | if (!btrfsic_is_initialized) |
| 3127 | return; |
| 3128 | |
| 3129 | mutex_lock(&btrfsic_mutex); |
| 3130 | |
| 3131 | state = NULL; |
| 3132 | list_for_each_entry(device, dev_head, dev_list) { |
| 3133 | struct btrfsic_dev_state *ds; |
| 3134 | |
| 3135 | if (!device->bdev || !device->name) |
| 3136 | continue; |
| 3137 | |
| 3138 | ds = btrfsic_dev_state_hashtable_lookup( |
| 3139 | device->bdev, |
| 3140 | &btrfsic_dev_state_hashtable); |
| 3141 | if (NULL != ds) { |
| 3142 | state = ds->state; |
| 3143 | btrfsic_dev_state_hashtable_remove(ds); |
| 3144 | btrfsic_dev_state_free(ds); |
| 3145 | } |
| 3146 | } |
| 3147 | |
| 3148 | if (NULL == state) { |
| 3149 | printk(KERN_INFO |
| 3150 | "btrfsic: error, cannot find state information" |
| 3151 | " on umount!\n"); |
| 3152 | mutex_unlock(&btrfsic_mutex); |
| 3153 | return; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | * Don't care about keeping the lists' state up to date, |
| 3158 | * just free all memory that was allocated dynamically. |
| 3159 | * Free the blocks and the block_links. |
| 3160 | */ |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 3161 | list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list, |
| 3162 | all_blocks_node) { |
| 3163 | struct btrfsic_block_link *l, *tmp; |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3164 | |
Geliang Tang | b69f2be | 2015-12-18 22:16:59 +0800 | [diff] [blame] | 3165 | list_for_each_entry_safe(l, tmp, &b_all->ref_to_list, |
| 3166 | node_ref_to) { |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3167 | if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) |
| 3168 | btrfsic_print_rem_link(state, l); |
| 3169 | |
| 3170 | l->ref_cnt--; |
| 3171 | if (0 == l->ref_cnt) |
| 3172 | btrfsic_block_link_free(l); |
| 3173 | } |
| 3174 | |
Stefan Behrens | 48235a6 | 2012-05-23 17:57:49 +0200 | [diff] [blame] | 3175 | if (b_all->is_iodone || b_all->never_written) |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3176 | btrfsic_block_free(b_all); |
| 3177 | else |
| 3178 | printk(KERN_INFO "btrfs: attempt to free %c-block" |
| 3179 | " @%llu (%s/%llu/%d) on umount which is" |
| 3180 | " not yet iodone!\n", |
| 3181 | btrfsic_get_block_type(state, b_all), |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3182 | b_all->logical_bytenr, b_all->dev_state->name, |
| 3183 | b_all->dev_bytenr, b_all->mirror_num); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | mutex_unlock(&btrfsic_mutex); |
| 3187 | |
Wang Shilong | f749303 | 2014-11-22 21:13:10 +0800 | [diff] [blame] | 3188 | kvfree(state); |
Stefan Behrens | 5db0276 | 2011-11-01 17:04:16 +0100 | [diff] [blame] | 3189 | } |