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