2 * Copyright (C) STRATO AG 2011. All rights reserved.
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.
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.
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.
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.
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
30 * 1. It is not allowed to write a disk block which is
31 * currently referenced by the super block (either directly
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
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.
43 * 2b. All referenced blocks need to have a generation
44 * number which is equal to the parent's number.
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
52 * The search term in the kernel log that can be used to filter
53 * on the existence of detected integrity issues is
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.
60 * Example #1, apply integrity checks to all metadata:
61 * mount /dev/sdb1 /mnt -o check_int
63 * Example #2, apply integrity checks to all metadata and
65 * mount /dev/sdb1 /mnt -o check_int_data
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
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.
82 #include <linux/sched.h>
83 #include <linux/slab.h>
84 #include <linux/buffer_head.h>
85 #include <linux/mutex.h>
86 #include <linux/crc32c.h>
87 #include <linux/genhd.h>
88 #include <linux/blkdev.h>
91 #include "transaction.h"
92 #include "extent_io.h"
94 #include "print-tree.h"
96 #include "check-integrity.h"
97 #include "rcu-string.h"
99 #define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
100 #define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
101 #define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
102 #define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
103 #define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
104 #define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
105 #define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
106 #define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
107 * excluding " [...]" */
108 #define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
111 * The definition of the bitmask fields for the print_mask.
112 * They are specified with the mount option check_integrity_print_mask.
114 #define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
115 #define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
116 #define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
117 #define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
118 #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
119 #define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
120 #define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
121 #define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
122 #define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
123 #define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
124 #define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
125 #define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
126 #define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
128 struct btrfsic_dev_state;
129 struct btrfsic_state;
131 struct btrfsic_block {
132 u32 magic_num; /* only used for debug purposes */
133 unsigned int is_metadata:1; /* if it is meta-data, not data-data */
134 unsigned int is_superblock:1; /* if it is one of the superblocks */
135 unsigned int is_iodone:1; /* if is done by lower subsystem */
136 unsigned int iodone_w_error:1; /* error was indicated to endio */
137 unsigned int never_written:1; /* block was added because it was
138 * referenced, not because it was
140 unsigned int mirror_num:2; /* large enough to hold
141 * BTRFS_SUPER_MIRROR_MAX */
142 struct btrfsic_dev_state *dev_state;
143 u64 dev_bytenr; /* key, physical byte num on disk */
144 u64 logical_bytenr; /* logical byte num on disk */
146 struct btrfs_disk_key disk_key; /* extra info to print in case of
147 * issues, will not always be correct */
148 struct list_head collision_resolving_node; /* list node */
149 struct list_head all_blocks_node; /* list node */
151 /* the following two lists contain block_link items */
152 struct list_head ref_to_list; /* list */
153 struct list_head ref_from_list; /* list */
154 struct btrfsic_block *next_in_same_bio;
155 void *orig_bio_bh_private;
159 } orig_bio_bh_end_io;
160 int submit_bio_bh_rw;
161 u64 flush_gen; /* only valid if !never_written */
165 * Elements of this type are allocated dynamically and required because
166 * each block object can refer to and can be ref from multiple blocks.
167 * The key to lookup them in the hashtable is the dev_bytenr of
168 * the block ref to plus the one from the block refered from.
169 * The fact that they are searchable via a hashtable and that a
170 * ref_cnt is maintained is not required for the btrfs integrity
171 * check algorithm itself, it is only used to make the output more
172 * beautiful in case that an error is detected (an error is defined
173 * as a write operation to a block while that block is still referenced).
175 struct btrfsic_block_link {
176 u32 magic_num; /* only used for debug purposes */
178 struct list_head node_ref_to; /* list node */
179 struct list_head node_ref_from; /* list node */
180 struct list_head collision_resolving_node; /* list node */
181 struct btrfsic_block *block_ref_to;
182 struct btrfsic_block *block_ref_from;
183 u64 parent_generation;
186 struct btrfsic_dev_state {
187 u32 magic_num; /* only used for debug purposes */
188 struct block_device *bdev;
189 struct btrfsic_state *state;
190 struct list_head collision_resolving_node; /* list node */
191 struct btrfsic_block dummy_block_for_bio_bh_flush;
193 char name[BDEVNAME_SIZE];
196 struct btrfsic_block_hashtable {
197 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
200 struct btrfsic_block_link_hashtable {
201 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
204 struct btrfsic_dev_state_hashtable {
205 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
208 struct btrfsic_block_data_ctx {
209 u64 start; /* virtual bytenr */
210 u64 dev_bytenr; /* physical bytenr on device */
212 struct btrfsic_dev_state *dev;
218 /* This structure is used to implement recursion without occupying
219 * any stack space, refer to btrfsic_process_metablock() */
220 struct btrfsic_stack_frame {
228 struct btrfsic_block *block;
229 struct btrfsic_block_data_ctx *block_ctx;
230 struct btrfsic_block *next_block;
231 struct btrfsic_block_data_ctx next_block_ctx;
232 struct btrfs_header *hdr;
233 struct btrfsic_stack_frame *prev;
236 /* Some state per mounted filesystem */
237 struct btrfsic_state {
239 int include_extent_data;
241 struct list_head all_blocks_list;
242 struct btrfsic_block_hashtable block_hashtable;
243 struct btrfsic_block_link_hashtable block_link_hashtable;
244 struct btrfs_root *root;
245 u64 max_superblock_generation;
246 struct btrfsic_block *latest_superblock;
251 static void btrfsic_block_init(struct btrfsic_block *b);
252 static struct btrfsic_block *btrfsic_block_alloc(void);
253 static void btrfsic_block_free(struct btrfsic_block *b);
254 static void btrfsic_block_link_init(struct btrfsic_block_link *n);
255 static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
256 static void btrfsic_block_link_free(struct btrfsic_block_link *n);
257 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
258 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
259 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
260 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
261 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
262 struct btrfsic_block_hashtable *h);
263 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
264 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
265 struct block_device *bdev,
267 struct btrfsic_block_hashtable *h);
268 static void btrfsic_block_link_hashtable_init(
269 struct btrfsic_block_link_hashtable *h);
270 static void btrfsic_block_link_hashtable_add(
271 struct btrfsic_block_link *l,
272 struct btrfsic_block_link_hashtable *h);
273 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
274 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
275 struct block_device *bdev_ref_to,
276 u64 dev_bytenr_ref_to,
277 struct block_device *bdev_ref_from,
278 u64 dev_bytenr_ref_from,
279 struct btrfsic_block_link_hashtable *h);
280 static void btrfsic_dev_state_hashtable_init(
281 struct btrfsic_dev_state_hashtable *h);
282 static void btrfsic_dev_state_hashtable_add(
283 struct btrfsic_dev_state *ds,
284 struct btrfsic_dev_state_hashtable *h);
285 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
286 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
287 struct block_device *bdev,
288 struct btrfsic_dev_state_hashtable *h);
289 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void);
290 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf);
291 static int btrfsic_process_superblock(struct btrfsic_state *state,
292 struct btrfs_fs_devices *fs_devices);
293 static int btrfsic_process_metablock(struct btrfsic_state *state,
294 struct btrfsic_block *block,
295 struct btrfsic_block_data_ctx *block_ctx,
296 int limit_nesting, int force_iodone_flag);
297 static void btrfsic_read_from_block_data(
298 struct btrfsic_block_data_ctx *block_ctx,
299 void *dst, u32 offset, size_t len);
300 static int btrfsic_create_link_to_next_block(
301 struct btrfsic_state *state,
302 struct btrfsic_block *block,
303 struct btrfsic_block_data_ctx
304 *block_ctx, u64 next_bytenr,
306 struct btrfsic_block_data_ctx *next_block_ctx,
307 struct btrfsic_block **next_blockp,
308 int force_iodone_flag,
309 int *num_copiesp, int *mirror_nump,
310 struct btrfs_disk_key *disk_key,
311 u64 parent_generation);
312 static int btrfsic_handle_extent_data(struct btrfsic_state *state,
313 struct btrfsic_block *block,
314 struct btrfsic_block_data_ctx *block_ctx,
315 u32 item_offset, int force_iodone_flag);
316 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
317 struct btrfsic_block_data_ctx *block_ctx_out,
319 static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
320 u32 len, struct block_device *bdev,
321 struct btrfsic_block_data_ctx *block_ctx_out);
322 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
323 static int btrfsic_read_block(struct btrfsic_state *state,
324 struct btrfsic_block_data_ctx *block_ctx);
325 static void btrfsic_dump_database(struct btrfsic_state *state);
326 static void btrfsic_complete_bio_end_io(struct bio *bio, int err);
327 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
328 char **datav, unsigned int num_pages);
329 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
330 u64 dev_bytenr, char **mapped_datav,
331 unsigned int num_pages,
332 struct bio *bio, int *bio_is_patched,
333 struct buffer_head *bh,
334 int submit_bio_bh_rw);
335 static int btrfsic_process_written_superblock(
336 struct btrfsic_state *state,
337 struct btrfsic_block *const block,
338 struct btrfs_super_block *const super_hdr);
339 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status);
340 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
341 static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
342 const struct btrfsic_block *block,
343 int recursion_level);
344 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
345 struct btrfsic_block *const block,
346 int recursion_level);
347 static void btrfsic_print_add_link(const struct btrfsic_state *state,
348 const struct btrfsic_block_link *l);
349 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
350 const struct btrfsic_block_link *l);
351 static char btrfsic_get_block_type(const struct btrfsic_state *state,
352 const struct btrfsic_block *block);
353 static void btrfsic_dump_tree(const struct btrfsic_state *state);
354 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
355 const struct btrfsic_block *block,
357 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
358 struct btrfsic_state *state,
359 struct btrfsic_block_data_ctx *next_block_ctx,
360 struct btrfsic_block *next_block,
361 struct btrfsic_block *from_block,
362 u64 parent_generation);
363 static struct btrfsic_block *btrfsic_block_lookup_or_add(
364 struct btrfsic_state *state,
365 struct btrfsic_block_data_ctx *block_ctx,
366 const char *additional_string,
372 static int btrfsic_process_superblock_dev_mirror(
373 struct btrfsic_state *state,
374 struct btrfsic_dev_state *dev_state,
375 struct btrfs_device *device,
376 int superblock_mirror_num,
377 struct btrfsic_dev_state **selected_dev_state,
378 struct btrfs_super_block *selected_super);
379 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
380 struct block_device *bdev);
381 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
383 struct btrfsic_dev_state *dev_state,
386 static struct mutex btrfsic_mutex;
387 static int btrfsic_is_initialized;
388 static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
391 static void btrfsic_block_init(struct btrfsic_block *b)
393 b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
396 b->logical_bytenr = 0;
397 b->generation = BTRFSIC_GENERATION_UNKNOWN;
398 b->disk_key.objectid = 0;
399 b->disk_key.type = 0;
400 b->disk_key.offset = 0;
402 b->is_superblock = 0;
404 b->iodone_w_error = 0;
405 b->never_written = 0;
407 b->next_in_same_bio = NULL;
408 b->orig_bio_bh_private = NULL;
409 b->orig_bio_bh_end_io.bio = NULL;
410 INIT_LIST_HEAD(&b->collision_resolving_node);
411 INIT_LIST_HEAD(&b->all_blocks_node);
412 INIT_LIST_HEAD(&b->ref_to_list);
413 INIT_LIST_HEAD(&b->ref_from_list);
414 b->submit_bio_bh_rw = 0;
418 static struct btrfsic_block *btrfsic_block_alloc(void)
420 struct btrfsic_block *b;
422 b = kzalloc(sizeof(*b), GFP_NOFS);
424 btrfsic_block_init(b);
429 static void btrfsic_block_free(struct btrfsic_block *b)
431 BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
435 static void btrfsic_block_link_init(struct btrfsic_block_link *l)
437 l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
439 INIT_LIST_HEAD(&l->node_ref_to);
440 INIT_LIST_HEAD(&l->node_ref_from);
441 INIT_LIST_HEAD(&l->collision_resolving_node);
442 l->block_ref_to = NULL;
443 l->block_ref_from = NULL;
446 static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
448 struct btrfsic_block_link *l;
450 l = kzalloc(sizeof(*l), GFP_NOFS);
452 btrfsic_block_link_init(l);
457 static void btrfsic_block_link_free(struct btrfsic_block_link *l)
459 BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
463 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
465 ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
469 INIT_LIST_HEAD(&ds->collision_resolving_node);
470 ds->last_flush_gen = 0;
471 btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
472 ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
473 ds->dummy_block_for_bio_bh_flush.dev_state = ds;
476 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
478 struct btrfsic_dev_state *ds;
480 ds = kzalloc(sizeof(*ds), GFP_NOFS);
482 btrfsic_dev_state_init(ds);
487 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
489 BUG_ON(!(NULL == ds ||
490 BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
494 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
498 for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
499 INIT_LIST_HEAD(h->table + i);
502 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
503 struct btrfsic_block_hashtable *h)
505 const unsigned int hashval =
506 (((unsigned int)(b->dev_bytenr >> 16)) ^
507 ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
508 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
510 list_add(&b->collision_resolving_node, h->table + hashval);
513 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
515 list_del(&b->collision_resolving_node);
518 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
519 struct block_device *bdev,
521 struct btrfsic_block_hashtable *h)
523 const unsigned int hashval =
524 (((unsigned int)(dev_bytenr >> 16)) ^
525 ((unsigned int)((uintptr_t)bdev))) &
526 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
527 struct list_head *elem;
529 list_for_each(elem, h->table + hashval) {
530 struct btrfsic_block *const b =
531 list_entry(elem, struct btrfsic_block,
532 collision_resolving_node);
534 if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
541 static void btrfsic_block_link_hashtable_init(
542 struct btrfsic_block_link_hashtable *h)
546 for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
547 INIT_LIST_HEAD(h->table + i);
550 static void btrfsic_block_link_hashtable_add(
551 struct btrfsic_block_link *l,
552 struct btrfsic_block_link_hashtable *h)
554 const unsigned int hashval =
555 (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
556 ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
557 ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
558 ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
559 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
561 BUG_ON(NULL == l->block_ref_to);
562 BUG_ON(NULL == l->block_ref_from);
563 list_add(&l->collision_resolving_node, h->table + hashval);
566 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
568 list_del(&l->collision_resolving_node);
571 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
572 struct block_device *bdev_ref_to,
573 u64 dev_bytenr_ref_to,
574 struct block_device *bdev_ref_from,
575 u64 dev_bytenr_ref_from,
576 struct btrfsic_block_link_hashtable *h)
578 const unsigned int hashval =
579 (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
580 ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
581 ((unsigned int)((uintptr_t)bdev_ref_to)) ^
582 ((unsigned int)((uintptr_t)bdev_ref_from))) &
583 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
584 struct list_head *elem;
586 list_for_each(elem, h->table + hashval) {
587 struct btrfsic_block_link *const l =
588 list_entry(elem, struct btrfsic_block_link,
589 collision_resolving_node);
591 BUG_ON(NULL == l->block_ref_to);
592 BUG_ON(NULL == l->block_ref_from);
593 if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
594 l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
595 l->block_ref_from->dev_state->bdev == bdev_ref_from &&
596 l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
603 static void btrfsic_dev_state_hashtable_init(
604 struct btrfsic_dev_state_hashtable *h)
608 for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
609 INIT_LIST_HEAD(h->table + i);
612 static void btrfsic_dev_state_hashtable_add(
613 struct btrfsic_dev_state *ds,
614 struct btrfsic_dev_state_hashtable *h)
616 const unsigned int hashval =
617 (((unsigned int)((uintptr_t)ds->bdev)) &
618 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
620 list_add(&ds->collision_resolving_node, h->table + hashval);
623 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
625 list_del(&ds->collision_resolving_node);
628 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
629 struct block_device *bdev,
630 struct btrfsic_dev_state_hashtable *h)
632 const unsigned int hashval =
633 (((unsigned int)((uintptr_t)bdev)) &
634 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
635 struct list_head *elem;
637 list_for_each(elem, h->table + hashval) {
638 struct btrfsic_dev_state *const ds =
639 list_entry(elem, struct btrfsic_dev_state,
640 collision_resolving_node);
642 if (ds->bdev == bdev)
649 static int btrfsic_process_superblock(struct btrfsic_state *state,
650 struct btrfs_fs_devices *fs_devices)
653 struct btrfs_super_block *selected_super;
654 struct list_head *dev_head = &fs_devices->devices;
655 struct btrfs_device *device;
656 struct btrfsic_dev_state *selected_dev_state = NULL;
659 BUG_ON(NULL == state);
660 selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
661 if (NULL == selected_super) {
662 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
666 list_for_each_entry(device, dev_head, dev_list) {
668 struct btrfsic_dev_state *dev_state;
670 if (!device->bdev || !device->name)
673 dev_state = btrfsic_dev_state_lookup(device->bdev);
674 BUG_ON(NULL == dev_state);
675 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
676 ret = btrfsic_process_superblock_dev_mirror(
677 state, dev_state, device, i,
678 &selected_dev_state, selected_super);
679 if (0 != ret && 0 == i) {
680 kfree(selected_super);
686 if (NULL == state->latest_superblock) {
687 printk(KERN_INFO "btrfsic: no superblock found!\n");
688 kfree(selected_super);
692 state->csum_size = btrfs_super_csum_size(selected_super);
694 for (pass = 0; pass < 3; pass++) {
701 next_bytenr = btrfs_super_root(selected_super);
702 if (state->print_mask &
703 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
704 printk(KERN_INFO "root@%llu\n",
705 (unsigned long long)next_bytenr);
708 next_bytenr = btrfs_super_chunk_root(selected_super);
709 if (state->print_mask &
710 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
711 printk(KERN_INFO "chunk@%llu\n",
712 (unsigned long long)next_bytenr);
715 next_bytenr = btrfs_super_log_root(selected_super);
716 if (0 == next_bytenr)
718 if (state->print_mask &
719 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
720 printk(KERN_INFO "log@%llu\n",
721 (unsigned long long)next_bytenr);
726 btrfs_num_copies(&state->root->fs_info->mapping_tree,
727 next_bytenr, state->metablock_size);
728 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
729 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
730 (unsigned long long)next_bytenr, num_copies);
732 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
733 struct btrfsic_block *next_block;
734 struct btrfsic_block_data_ctx tmp_next_block_ctx;
735 struct btrfsic_block_link *l;
737 ret = btrfsic_map_block(state, next_bytenr,
738 state->metablock_size,
742 printk(KERN_INFO "btrfsic:"
743 " btrfsic_map_block(root @%llu,"
744 " mirror %d) failed!\n",
745 (unsigned long long)next_bytenr,
747 kfree(selected_super);
751 next_block = btrfsic_block_hashtable_lookup(
752 tmp_next_block_ctx.dev->bdev,
753 tmp_next_block_ctx.dev_bytenr,
754 &state->block_hashtable);
755 BUG_ON(NULL == next_block);
757 l = btrfsic_block_link_hashtable_lookup(
758 tmp_next_block_ctx.dev->bdev,
759 tmp_next_block_ctx.dev_bytenr,
760 state->latest_superblock->dev_state->
762 state->latest_superblock->dev_bytenr,
763 &state->block_link_hashtable);
766 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
767 if (ret < (int)PAGE_CACHE_SIZE) {
769 "btrfsic: read @logical %llu failed!\n",
771 tmp_next_block_ctx.start);
772 btrfsic_release_block_ctx(&tmp_next_block_ctx);
773 kfree(selected_super);
777 ret = btrfsic_process_metablock(state,
780 BTRFS_MAX_LEVEL + 3, 1);
781 btrfsic_release_block_ctx(&tmp_next_block_ctx);
785 kfree(selected_super);
789 static int btrfsic_process_superblock_dev_mirror(
790 struct btrfsic_state *state,
791 struct btrfsic_dev_state *dev_state,
792 struct btrfs_device *device,
793 int superblock_mirror_num,
794 struct btrfsic_dev_state **selected_dev_state,
795 struct btrfs_super_block *selected_super)
797 struct btrfs_super_block *super_tmp;
799 struct buffer_head *bh;
800 struct btrfsic_block *superblock_tmp;
802 struct block_device *const superblock_bdev = device->bdev;
804 /* super block bytenr is always the unmapped device bytenr */
805 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
806 if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
808 bh = __bread(superblock_bdev, dev_bytenr / 4096,
809 BTRFS_SUPER_INFO_SIZE);
812 super_tmp = (struct btrfs_super_block *)
813 (bh->b_data + (dev_bytenr & 4095));
815 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
816 strncmp((char *)(&(super_tmp->magic)), BTRFS_MAGIC,
817 sizeof(super_tmp->magic)) ||
818 memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
819 btrfs_super_nodesize(super_tmp) != state->metablock_size ||
820 btrfs_super_leafsize(super_tmp) != state->metablock_size ||
821 btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
827 btrfsic_block_hashtable_lookup(superblock_bdev,
829 &state->block_hashtable);
830 if (NULL == superblock_tmp) {
831 superblock_tmp = btrfsic_block_alloc();
832 if (NULL == superblock_tmp) {
833 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
837 /* for superblock, only the dev_bytenr makes sense */
838 superblock_tmp->dev_bytenr = dev_bytenr;
839 superblock_tmp->dev_state = dev_state;
840 superblock_tmp->logical_bytenr = dev_bytenr;
841 superblock_tmp->generation = btrfs_super_generation(super_tmp);
842 superblock_tmp->is_metadata = 1;
843 superblock_tmp->is_superblock = 1;
844 superblock_tmp->is_iodone = 1;
845 superblock_tmp->never_written = 0;
846 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
847 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
848 printk_in_rcu(KERN_INFO "New initial S-block (bdev %p, %s)"
849 " @%llu (%s/%llu/%d)\n",
851 rcu_str_deref(device->name),
852 (unsigned long long)dev_bytenr,
854 (unsigned long long)dev_bytenr,
855 superblock_mirror_num);
856 list_add(&superblock_tmp->all_blocks_node,
857 &state->all_blocks_list);
858 btrfsic_block_hashtable_add(superblock_tmp,
859 &state->block_hashtable);
862 /* select the one with the highest generation field */
863 if (btrfs_super_generation(super_tmp) >
864 state->max_superblock_generation ||
865 0 == state->max_superblock_generation) {
866 memcpy(selected_super, super_tmp, sizeof(*selected_super));
867 *selected_dev_state = dev_state;
868 state->max_superblock_generation =
869 btrfs_super_generation(super_tmp);
870 state->latest_superblock = superblock_tmp;
873 for (pass = 0; pass < 3; pass++) {
877 const char *additional_string = NULL;
878 struct btrfs_disk_key tmp_disk_key;
880 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
881 tmp_disk_key.offset = 0;
884 tmp_disk_key.objectid =
885 cpu_to_le64(BTRFS_ROOT_TREE_OBJECTID);
886 additional_string = "initial root ";
887 next_bytenr = btrfs_super_root(super_tmp);
890 tmp_disk_key.objectid =
891 cpu_to_le64(BTRFS_CHUNK_TREE_OBJECTID);
892 additional_string = "initial chunk ";
893 next_bytenr = btrfs_super_chunk_root(super_tmp);
896 tmp_disk_key.objectid =
897 cpu_to_le64(BTRFS_TREE_LOG_OBJECTID);
898 additional_string = "initial log ";
899 next_bytenr = btrfs_super_log_root(super_tmp);
900 if (0 == next_bytenr)
906 btrfs_num_copies(&state->root->fs_info->mapping_tree,
907 next_bytenr, state->metablock_size);
908 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
909 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
910 (unsigned long long)next_bytenr, num_copies);
911 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
912 struct btrfsic_block *next_block;
913 struct btrfsic_block_data_ctx tmp_next_block_ctx;
914 struct btrfsic_block_link *l;
916 if (btrfsic_map_block(state, next_bytenr,
917 state->metablock_size,
920 printk(KERN_INFO "btrfsic: btrfsic_map_block("
921 "bytenr @%llu, mirror %d) failed!\n",
922 (unsigned long long)next_bytenr,
928 next_block = btrfsic_block_lookup_or_add(
929 state, &tmp_next_block_ctx,
930 additional_string, 1, 1, 0,
932 if (NULL == next_block) {
933 btrfsic_release_block_ctx(&tmp_next_block_ctx);
938 next_block->disk_key = tmp_disk_key;
939 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
940 l = btrfsic_block_link_lookup_or_add(
941 state, &tmp_next_block_ctx,
942 next_block, superblock_tmp,
943 BTRFSIC_GENERATION_UNKNOWN);
944 btrfsic_release_block_ctx(&tmp_next_block_ctx);
951 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
952 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
958 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
960 struct btrfsic_stack_frame *sf;
962 sf = kzalloc(sizeof(*sf), GFP_NOFS);
964 printk(KERN_INFO "btrfsic: alloc memory failed!\n");
966 sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
970 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
972 BUG_ON(!(NULL == sf ||
973 BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
977 static int btrfsic_process_metablock(
978 struct btrfsic_state *state,
979 struct btrfsic_block *const first_block,
980 struct btrfsic_block_data_ctx *const first_block_ctx,
981 int first_limit_nesting, int force_iodone_flag)
983 struct btrfsic_stack_frame initial_stack_frame = { 0 };
984 struct btrfsic_stack_frame *sf;
985 struct btrfsic_stack_frame *next_stack;
986 struct btrfs_header *const first_hdr =
987 (struct btrfs_header *)first_block_ctx->datav[0];
990 sf = &initial_stack_frame;
993 sf->limit_nesting = first_limit_nesting;
994 sf->block = first_block;
995 sf->block_ctx = first_block_ctx;
996 sf->next_block = NULL;
1000 continue_with_new_stack_frame:
1001 sf->block->generation = le64_to_cpu(sf->hdr->generation);
1002 if (0 == sf->hdr->level) {
1003 struct btrfs_leaf *const leafhdr =
1004 (struct btrfs_leaf *)sf->hdr;
1007 sf->nr = le32_to_cpu(leafhdr->header.nritems);
1009 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1011 "leaf %llu items %d generation %llu"
1013 (unsigned long long)
1014 sf->block_ctx->start,
1016 (unsigned long long)
1017 le64_to_cpu(leafhdr->header.generation),
1018 (unsigned long long)
1019 le64_to_cpu(leafhdr->header.owner));
1022 continue_with_current_leaf_stack_frame:
1023 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1028 if (sf->i < sf->nr) {
1029 struct btrfs_item disk_item;
1030 u32 disk_item_offset =
1031 (uintptr_t)(leafhdr->items + sf->i) -
1033 struct btrfs_disk_key *disk_key;
1038 if (disk_item_offset + sizeof(struct btrfs_item) >
1039 sf->block_ctx->len) {
1040 leaf_item_out_of_bounce_error:
1042 "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
1043 sf->block_ctx->start,
1044 sf->block_ctx->dev->name);
1045 goto one_stack_frame_backwards;
1047 btrfsic_read_from_block_data(sf->block_ctx,
1050 sizeof(struct btrfs_item));
1051 item_offset = le32_to_cpu(disk_item.offset);
1052 item_size = le32_to_cpu(disk_item.size);
1053 disk_key = &disk_item.key;
1054 type = disk_key->type;
1056 if (BTRFS_ROOT_ITEM_KEY == type) {
1057 struct btrfs_root_item root_item;
1058 u32 root_item_offset;
1061 root_item_offset = item_offset +
1062 offsetof(struct btrfs_leaf, items);
1063 if (root_item_offset + item_size >
1065 goto leaf_item_out_of_bounce_error;
1066 btrfsic_read_from_block_data(
1067 sf->block_ctx, &root_item,
1070 next_bytenr = le64_to_cpu(root_item.bytenr);
1073 btrfsic_create_link_to_next_block(
1079 &sf->next_block_ctx,
1085 le64_to_cpu(root_item.
1088 goto one_stack_frame_backwards;
1090 if (NULL != sf->next_block) {
1091 struct btrfs_header *const next_hdr =
1092 (struct btrfs_header *)
1093 sf->next_block_ctx.datav[0];
1096 btrfsic_stack_frame_alloc();
1097 if (NULL == next_stack) {
1098 btrfsic_release_block_ctx(
1101 goto one_stack_frame_backwards;
1105 next_stack->block = sf->next_block;
1106 next_stack->block_ctx =
1107 &sf->next_block_ctx;
1108 next_stack->next_block = NULL;
1109 next_stack->hdr = next_hdr;
1110 next_stack->limit_nesting =
1111 sf->limit_nesting - 1;
1112 next_stack->prev = sf;
1114 goto continue_with_new_stack_frame;
1116 } else if (BTRFS_EXTENT_DATA_KEY == type &&
1117 state->include_extent_data) {
1118 sf->error = btrfsic_handle_extent_data(
1125 goto one_stack_frame_backwards;
1128 goto continue_with_current_leaf_stack_frame;
1131 struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1134 sf->nr = le32_to_cpu(nodehdr->header.nritems);
1136 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1137 printk(KERN_INFO "node %llu level %d items %d"
1138 " generation %llu owner %llu\n",
1139 (unsigned long long)
1140 sf->block_ctx->start,
1141 nodehdr->header.level, sf->nr,
1142 (unsigned long long)
1143 le64_to_cpu(nodehdr->header.generation),
1144 (unsigned long long)
1145 le64_to_cpu(nodehdr->header.owner));
1148 continue_with_current_node_stack_frame:
1149 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1154 if (sf->i < sf->nr) {
1155 struct btrfs_key_ptr key_ptr;
1159 key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1161 if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1162 sf->block_ctx->len) {
1164 "btrfsic: node item out of bounce at logical %llu, dev %s\n",
1165 sf->block_ctx->start,
1166 sf->block_ctx->dev->name);
1167 goto one_stack_frame_backwards;
1169 btrfsic_read_from_block_data(
1170 sf->block_ctx, &key_ptr, key_ptr_offset,
1171 sizeof(struct btrfs_key_ptr));
1172 next_bytenr = le64_to_cpu(key_ptr.blockptr);
1174 sf->error = btrfsic_create_link_to_next_block(
1180 &sf->next_block_ctx,
1186 le64_to_cpu(key_ptr.generation));
1188 goto one_stack_frame_backwards;
1190 if (NULL != sf->next_block) {
1191 struct btrfs_header *const next_hdr =
1192 (struct btrfs_header *)
1193 sf->next_block_ctx.datav[0];
1195 next_stack = btrfsic_stack_frame_alloc();
1196 if (NULL == next_stack)
1197 goto one_stack_frame_backwards;
1200 next_stack->block = sf->next_block;
1201 next_stack->block_ctx = &sf->next_block_ctx;
1202 next_stack->next_block = NULL;
1203 next_stack->hdr = next_hdr;
1204 next_stack->limit_nesting =
1205 sf->limit_nesting - 1;
1206 next_stack->prev = sf;
1208 goto continue_with_new_stack_frame;
1211 goto continue_with_current_node_stack_frame;
1215 one_stack_frame_backwards:
1216 if (NULL != sf->prev) {
1217 struct btrfsic_stack_frame *const prev = sf->prev;
1219 /* the one for the initial block is freed in the caller */
1220 btrfsic_release_block_ctx(sf->block_ctx);
1223 prev->error = sf->error;
1224 btrfsic_stack_frame_free(sf);
1226 goto one_stack_frame_backwards;
1229 btrfsic_stack_frame_free(sf);
1231 goto continue_with_new_stack_frame;
1233 BUG_ON(&initial_stack_frame != sf);
1239 static void btrfsic_read_from_block_data(
1240 struct btrfsic_block_data_ctx *block_ctx,
1241 void *dstv, u32 offset, size_t len)
1244 size_t offset_in_page;
1246 char *dst = (char *)dstv;
1247 size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1);
1248 unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT;
1250 WARN_ON(offset + len > block_ctx->len);
1251 offset_in_page = (start_offset + offset) &
1252 ((unsigned long)PAGE_CACHE_SIZE - 1);
1255 cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
1256 BUG_ON(i >= (block_ctx->len + PAGE_CACHE_SIZE - 1) >>
1258 kaddr = block_ctx->datav[i];
1259 memcpy(dst, kaddr + offset_in_page, cur);
1268 static int btrfsic_create_link_to_next_block(
1269 struct btrfsic_state *state,
1270 struct btrfsic_block *block,
1271 struct btrfsic_block_data_ctx *block_ctx,
1274 struct btrfsic_block_data_ctx *next_block_ctx,
1275 struct btrfsic_block **next_blockp,
1276 int force_iodone_flag,
1277 int *num_copiesp, int *mirror_nump,
1278 struct btrfs_disk_key *disk_key,
1279 u64 parent_generation)
1281 struct btrfsic_block *next_block = NULL;
1283 struct btrfsic_block_link *l;
1284 int did_alloc_block_link;
1285 int block_was_created;
1287 *next_blockp = NULL;
1288 if (0 == *num_copiesp) {
1290 btrfs_num_copies(&state->root->fs_info->mapping_tree,
1291 next_bytenr, state->metablock_size);
1292 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1293 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1294 (unsigned long long)next_bytenr, *num_copiesp);
1298 if (*mirror_nump > *num_copiesp)
1301 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1303 "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1305 ret = btrfsic_map_block(state, next_bytenr,
1306 state->metablock_size,
1307 next_block_ctx, *mirror_nump);
1310 "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1311 (unsigned long long)next_bytenr, *mirror_nump);
1312 btrfsic_release_block_ctx(next_block_ctx);
1313 *next_blockp = NULL;
1317 next_block = btrfsic_block_lookup_or_add(state,
1318 next_block_ctx, "referenced ",
1319 1, force_iodone_flag,
1322 &block_was_created);
1323 if (NULL == next_block) {
1324 btrfsic_release_block_ctx(next_block_ctx);
1325 *next_blockp = NULL;
1328 if (block_was_created) {
1330 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1332 if (next_block->logical_bytenr != next_bytenr &&
1333 !(!next_block->is_metadata &&
1334 0 == next_block->logical_bytenr)) {
1336 "Referenced block @%llu (%s/%llu/%d)"
1337 " found in hash table, %c,"
1338 " bytenr mismatch (!= stored %llu).\n",
1339 (unsigned long long)next_bytenr,
1340 next_block_ctx->dev->name,
1341 (unsigned long long)next_block_ctx->dev_bytenr,
1343 btrfsic_get_block_type(state, next_block),
1344 (unsigned long long)next_block->logical_bytenr);
1345 } else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1347 "Referenced block @%llu (%s/%llu/%d)"
1348 " found in hash table, %c.\n",
1349 (unsigned long long)next_bytenr,
1350 next_block_ctx->dev->name,
1351 (unsigned long long)next_block_ctx->dev_bytenr,
1353 btrfsic_get_block_type(state, next_block));
1354 next_block->logical_bytenr = next_bytenr;
1356 next_block->mirror_num = *mirror_nump;
1357 l = btrfsic_block_link_hashtable_lookup(
1358 next_block_ctx->dev->bdev,
1359 next_block_ctx->dev_bytenr,
1360 block_ctx->dev->bdev,
1361 block_ctx->dev_bytenr,
1362 &state->block_link_hashtable);
1365 next_block->disk_key = *disk_key;
1367 l = btrfsic_block_link_alloc();
1369 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
1370 btrfsic_release_block_ctx(next_block_ctx);
1371 *next_blockp = NULL;
1375 did_alloc_block_link = 1;
1376 l->block_ref_to = next_block;
1377 l->block_ref_from = block;
1379 l->parent_generation = parent_generation;
1381 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1382 btrfsic_print_add_link(state, l);
1384 list_add(&l->node_ref_to, &block->ref_to_list);
1385 list_add(&l->node_ref_from, &next_block->ref_from_list);
1387 btrfsic_block_link_hashtable_add(l,
1388 &state->block_link_hashtable);
1390 did_alloc_block_link = 0;
1391 if (0 == limit_nesting) {
1393 l->parent_generation = parent_generation;
1394 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1395 btrfsic_print_add_link(state, l);
1399 if (limit_nesting > 0 && did_alloc_block_link) {
1400 ret = btrfsic_read_block(state, next_block_ctx);
1401 if (ret < (int)next_block_ctx->len) {
1403 "btrfsic: read block @logical %llu failed!\n",
1404 (unsigned long long)next_bytenr);
1405 btrfsic_release_block_ctx(next_block_ctx);
1406 *next_blockp = NULL;
1410 *next_blockp = next_block;
1412 *next_blockp = NULL;
1419 static int btrfsic_handle_extent_data(
1420 struct btrfsic_state *state,
1421 struct btrfsic_block *block,
1422 struct btrfsic_block_data_ctx *block_ctx,
1423 u32 item_offset, int force_iodone_flag)
1426 struct btrfs_file_extent_item file_extent_item;
1427 u64 file_extent_item_offset;
1431 struct btrfsic_block_link *l;
1433 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1435 if (file_extent_item_offset +
1436 offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1439 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1440 block_ctx->start, block_ctx->dev->name);
1444 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1445 file_extent_item_offset,
1446 offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1447 if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
1448 ((u64)0) == le64_to_cpu(file_extent_item.disk_bytenr)) {
1449 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1450 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
1451 file_extent_item.type,
1452 (unsigned long long)
1453 le64_to_cpu(file_extent_item.disk_bytenr));
1457 if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1460 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1461 block_ctx->start, block_ctx->dev->name);
1464 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1465 file_extent_item_offset,
1466 sizeof(struct btrfs_file_extent_item));
1467 next_bytenr = le64_to_cpu(file_extent_item.disk_bytenr) +
1468 le64_to_cpu(file_extent_item.offset);
1469 generation = le64_to_cpu(file_extent_item.generation);
1470 num_bytes = le64_to_cpu(file_extent_item.num_bytes);
1471 generation = le64_to_cpu(file_extent_item.generation);
1473 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1474 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
1475 " offset = %llu, num_bytes = %llu\n",
1476 file_extent_item.type,
1477 (unsigned long long)
1478 le64_to_cpu(file_extent_item.disk_bytenr),
1479 (unsigned long long)le64_to_cpu(file_extent_item.offset),
1480 (unsigned long long)num_bytes);
1481 while (num_bytes > 0) {
1486 if (num_bytes > state->datablock_size)
1487 chunk_len = state->datablock_size;
1489 chunk_len = num_bytes;
1492 btrfs_num_copies(&state->root->fs_info->mapping_tree,
1493 next_bytenr, state->datablock_size);
1494 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1495 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1496 (unsigned long long)next_bytenr, num_copies);
1497 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1498 struct btrfsic_block_data_ctx next_block_ctx;
1499 struct btrfsic_block *next_block;
1500 int block_was_created;
1502 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1503 printk(KERN_INFO "btrfsic_handle_extent_data("
1504 "mirror_num=%d)\n", mirror_num);
1505 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1507 "\tdisk_bytenr = %llu, num_bytes %u\n",
1508 (unsigned long long)next_bytenr,
1510 ret = btrfsic_map_block(state, next_bytenr,
1511 chunk_len, &next_block_ctx,
1515 "btrfsic: btrfsic_map_block(@%llu,"
1516 " mirror=%d) failed!\n",
1517 (unsigned long long)next_bytenr,
1522 next_block = btrfsic_block_lookup_or_add(
1530 &block_was_created);
1531 if (NULL == next_block) {
1533 "btrfsic: error, kmalloc failed!\n");
1534 btrfsic_release_block_ctx(&next_block_ctx);
1537 if (!block_was_created) {
1538 if (next_block->logical_bytenr != next_bytenr &&
1539 !(!next_block->is_metadata &&
1540 0 == next_block->logical_bytenr)) {
1543 " @%llu (%s/%llu/%d)"
1544 " found in hash table, D,"
1546 " (!= stored %llu).\n",
1547 (unsigned long long)next_bytenr,
1548 next_block_ctx.dev->name,
1549 (unsigned long long)
1550 next_block_ctx.dev_bytenr,
1552 (unsigned long long)
1553 next_block->logical_bytenr);
1555 next_block->logical_bytenr = next_bytenr;
1556 next_block->mirror_num = mirror_num;
1559 l = btrfsic_block_link_lookup_or_add(state,
1563 btrfsic_release_block_ctx(&next_block_ctx);
1568 next_bytenr += chunk_len;
1569 num_bytes -= chunk_len;
1575 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1576 struct btrfsic_block_data_ctx *block_ctx_out,
1581 struct btrfs_bio *multi = NULL;
1582 struct btrfs_device *device;
1585 ret = btrfs_map_block(&state->root->fs_info->mapping_tree, READ,
1586 bytenr, &length, &multi, mirror_num);
1588 device = multi->stripes[0].dev;
1589 block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
1590 block_ctx_out->dev_bytenr = multi->stripes[0].physical;
1591 block_ctx_out->start = bytenr;
1592 block_ctx_out->len = len;
1593 block_ctx_out->datav = NULL;
1594 block_ctx_out->pagev = NULL;
1595 block_ctx_out->mem_to_free = NULL;
1599 if (NULL == block_ctx_out->dev) {
1601 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
1607 static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
1608 u32 len, struct block_device *bdev,
1609 struct btrfsic_block_data_ctx *block_ctx_out)
1611 block_ctx_out->dev = btrfsic_dev_state_lookup(bdev);
1612 block_ctx_out->dev_bytenr = bytenr;
1613 block_ctx_out->start = bytenr;
1614 block_ctx_out->len = len;
1615 block_ctx_out->datav = NULL;
1616 block_ctx_out->pagev = NULL;
1617 block_ctx_out->mem_to_free = NULL;
1618 if (NULL != block_ctx_out->dev) {
1621 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#2)!\n");
1626 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1628 if (block_ctx->mem_to_free) {
1629 unsigned int num_pages;
1631 BUG_ON(!block_ctx->datav);
1632 BUG_ON(!block_ctx->pagev);
1633 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1635 while (num_pages > 0) {
1637 if (block_ctx->datav[num_pages]) {
1638 kunmap(block_ctx->pagev[num_pages]);
1639 block_ctx->datav[num_pages] = NULL;
1641 if (block_ctx->pagev[num_pages]) {
1642 __free_page(block_ctx->pagev[num_pages]);
1643 block_ctx->pagev[num_pages] = NULL;
1647 kfree(block_ctx->mem_to_free);
1648 block_ctx->mem_to_free = NULL;
1649 block_ctx->pagev = NULL;
1650 block_ctx->datav = NULL;
1654 static int btrfsic_read_block(struct btrfsic_state *state,
1655 struct btrfsic_block_data_ctx *block_ctx)
1657 unsigned int num_pages;
1662 BUG_ON(block_ctx->datav);
1663 BUG_ON(block_ctx->pagev);
1664 BUG_ON(block_ctx->mem_to_free);
1665 if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
1667 "btrfsic: read_block() with unaligned bytenr %llu\n",
1668 (unsigned long long)block_ctx->dev_bytenr);
1672 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1674 block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
1675 sizeof(*block_ctx->pagev)) *
1676 num_pages, GFP_NOFS);
1677 if (!block_ctx->mem_to_free)
1679 block_ctx->datav = block_ctx->mem_to_free;
1680 block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1681 for (i = 0; i < num_pages; i++) {
1682 block_ctx->pagev[i] = alloc_page(GFP_NOFS);
1683 if (!block_ctx->pagev[i])
1687 dev_bytenr = block_ctx->dev_bytenr;
1688 for (i = 0; i < num_pages;) {
1691 DECLARE_COMPLETION_ONSTACK(complete);
1693 bio = bio_alloc(GFP_NOFS, num_pages - i);
1696 "btrfsic: bio_alloc() for %u pages failed!\n",
1700 bio->bi_bdev = block_ctx->dev->bdev;
1701 bio->bi_sector = dev_bytenr >> 9;
1702 bio->bi_end_io = btrfsic_complete_bio_end_io;
1703 bio->bi_private = &complete;
1705 for (j = i; j < num_pages; j++) {
1706 ret = bio_add_page(bio, block_ctx->pagev[j],
1707 PAGE_CACHE_SIZE, 0);
1708 if (PAGE_CACHE_SIZE != ret)
1713 "btrfsic: error, failed to add a single page!\n");
1716 submit_bio(READ, bio);
1718 /* this will also unplug the queue */
1719 wait_for_completion(&complete);
1721 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1723 "btrfsic: read error at logical %llu dev %s!\n",
1724 block_ctx->start, block_ctx->dev->name);
1729 dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
1732 for (i = 0; i < num_pages; i++) {
1733 block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
1734 if (!block_ctx->datav[i]) {
1735 printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
1736 block_ctx->dev->name);
1741 return block_ctx->len;
1744 static void btrfsic_complete_bio_end_io(struct bio *bio, int err)
1746 complete((struct completion *)bio->bi_private);
1749 static void btrfsic_dump_database(struct btrfsic_state *state)
1751 struct list_head *elem_all;
1753 BUG_ON(NULL == state);
1755 printk(KERN_INFO "all_blocks_list:\n");
1756 list_for_each(elem_all, &state->all_blocks_list) {
1757 const struct btrfsic_block *const b_all =
1758 list_entry(elem_all, struct btrfsic_block,
1760 struct list_head *elem_ref_to;
1761 struct list_head *elem_ref_from;
1763 printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
1764 btrfsic_get_block_type(state, b_all),
1765 (unsigned long long)b_all->logical_bytenr,
1766 b_all->dev_state->name,
1767 (unsigned long long)b_all->dev_bytenr,
1770 list_for_each(elem_ref_to, &b_all->ref_to_list) {
1771 const struct btrfsic_block_link *const l =
1772 list_entry(elem_ref_to,
1773 struct btrfsic_block_link,
1776 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1778 " %c @%llu (%s/%llu/%d)\n",
1779 btrfsic_get_block_type(state, b_all),
1780 (unsigned long long)b_all->logical_bytenr,
1781 b_all->dev_state->name,
1782 (unsigned long long)b_all->dev_bytenr,
1785 btrfsic_get_block_type(state, l->block_ref_to),
1786 (unsigned long long)
1787 l->block_ref_to->logical_bytenr,
1788 l->block_ref_to->dev_state->name,
1789 (unsigned long long)l->block_ref_to->dev_bytenr,
1790 l->block_ref_to->mirror_num);
1793 list_for_each(elem_ref_from, &b_all->ref_from_list) {
1794 const struct btrfsic_block_link *const l =
1795 list_entry(elem_ref_from,
1796 struct btrfsic_block_link,
1799 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1801 " %c @%llu (%s/%llu/%d)\n",
1802 btrfsic_get_block_type(state, b_all),
1803 (unsigned long long)b_all->logical_bytenr,
1804 b_all->dev_state->name,
1805 (unsigned long long)b_all->dev_bytenr,
1808 btrfsic_get_block_type(state, l->block_ref_from),
1809 (unsigned long long)
1810 l->block_ref_from->logical_bytenr,
1811 l->block_ref_from->dev_state->name,
1812 (unsigned long long)
1813 l->block_ref_from->dev_bytenr,
1814 l->block_ref_from->mirror_num);
1817 printk(KERN_INFO "\n");
1822 * Test whether the disk block contains a tree block (leaf or node)
1823 * (note that this test fails for the super block)
1825 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
1826 char **datav, unsigned int num_pages)
1828 struct btrfs_header *h;
1829 u8 csum[BTRFS_CSUM_SIZE];
1833 if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
1834 return 1; /* not metadata */
1835 num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
1836 h = (struct btrfs_header *)datav[0];
1838 if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
1841 for (i = 0; i < num_pages; i++) {
1842 u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
1843 size_t sublen = i ? PAGE_CACHE_SIZE :
1844 (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
1846 crc = crc32c(crc, data, sublen);
1848 btrfs_csum_final(crc, csum);
1849 if (memcmp(csum, h->csum, state->csum_size))
1852 return 0; /* is metadata */
1855 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
1856 u64 dev_bytenr, char **mapped_datav,
1857 unsigned int num_pages,
1858 struct bio *bio, int *bio_is_patched,
1859 struct buffer_head *bh,
1860 int submit_bio_bh_rw)
1863 struct btrfsic_block *block;
1864 struct btrfsic_block_data_ctx block_ctx;
1866 struct btrfsic_state *state = dev_state->state;
1867 struct block_device *bdev = dev_state->bdev;
1868 unsigned int processed_len;
1870 if (NULL != bio_is_patched)
1871 *bio_is_patched = 0;
1878 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1881 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1882 &state->block_hashtable);
1883 if (NULL != block) {
1885 struct list_head *elem_ref_to;
1886 struct list_head *tmp_ref_to;
1888 if (block->is_superblock) {
1889 bytenr = le64_to_cpu(((struct btrfs_super_block *)
1890 mapped_datav[0])->bytenr);
1891 if (num_pages * PAGE_CACHE_SIZE <
1892 BTRFS_SUPER_INFO_SIZE) {
1894 "btrfsic: cannot work with too short bios!\n");
1898 BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
1899 processed_len = BTRFS_SUPER_INFO_SIZE;
1900 if (state->print_mask &
1901 BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
1903 "[before new superblock is written]:\n");
1904 btrfsic_dump_tree_sub(state, block, 0);
1908 if (!block->is_superblock) {
1909 if (num_pages * PAGE_CACHE_SIZE <
1910 state->metablock_size) {
1912 "btrfsic: cannot work with too short bios!\n");
1915 processed_len = state->metablock_size;
1916 bytenr = le64_to_cpu(((struct btrfs_header *)
1917 mapped_datav[0])->bytenr);
1918 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1922 if (block->logical_bytenr != bytenr) {
1924 "Written block @%llu (%s/%llu/%d)"
1925 " found in hash table, %c,"
1927 " (!= stored %llu).\n",
1928 (unsigned long long)bytenr,
1930 (unsigned long long)dev_bytenr,
1932 btrfsic_get_block_type(state, block),
1933 (unsigned long long)
1934 block->logical_bytenr);
1935 block->logical_bytenr = bytenr;
1936 } else if (state->print_mask &
1937 BTRFSIC_PRINT_MASK_VERBOSE)
1939 "Written block @%llu (%s/%llu/%d)"
1940 " found in hash table, %c.\n",
1941 (unsigned long long)bytenr,
1943 (unsigned long long)dev_bytenr,
1945 btrfsic_get_block_type(state, block));
1947 if (num_pages * PAGE_CACHE_SIZE <
1948 state->datablock_size) {
1950 "btrfsic: cannot work with too short bios!\n");
1953 processed_len = state->datablock_size;
1954 bytenr = block->logical_bytenr;
1955 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1957 "Written block @%llu (%s/%llu/%d)"
1958 " found in hash table, %c.\n",
1959 (unsigned long long)bytenr,
1961 (unsigned long long)dev_bytenr,
1963 btrfsic_get_block_type(state, block));
1966 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1968 "ref_to_list: %cE, ref_from_list: %cE\n",
1969 list_empty(&block->ref_to_list) ? ' ' : '!',
1970 list_empty(&block->ref_from_list) ? ' ' : '!');
1971 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1972 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1973 " @%llu (%s/%llu/%d), old(gen=%llu,"
1974 " objectid=%llu, type=%d, offset=%llu),"
1976 " which is referenced by most recent superblock"
1977 " (superblockgen=%llu)!\n",
1978 btrfsic_get_block_type(state, block),
1979 (unsigned long long)bytenr,
1981 (unsigned long long)dev_bytenr,
1983 (unsigned long long)block->generation,
1984 (unsigned long long)
1985 le64_to_cpu(block->disk_key.objectid),
1986 block->disk_key.type,
1987 (unsigned long long)
1988 le64_to_cpu(block->disk_key.offset),
1989 (unsigned long long)
1990 le64_to_cpu(((struct btrfs_header *)
1991 mapped_datav[0])->generation),
1992 (unsigned long long)
1993 state->max_superblock_generation);
1994 btrfsic_dump_tree(state);
1997 if (!block->is_iodone && !block->never_written) {
1998 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1999 " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
2000 " which is not yet iodone!\n",
2001 btrfsic_get_block_type(state, block),
2002 (unsigned long long)bytenr,
2004 (unsigned long long)dev_bytenr,
2006 (unsigned long long)block->generation,
2007 (unsigned long long)
2008 le64_to_cpu(((struct btrfs_header *)
2009 mapped_datav[0])->generation));
2010 /* it would not be safe to go on */
2011 btrfsic_dump_tree(state);
2016 * Clear all references of this block. Do not free
2017 * the block itself even if is not referenced anymore
2018 * because it still carries valueable information
2019 * like whether it was ever written and IO completed.
2021 list_for_each_safe(elem_ref_to, tmp_ref_to,
2022 &block->ref_to_list) {
2023 struct btrfsic_block_link *const l =
2024 list_entry(elem_ref_to,
2025 struct btrfsic_block_link,
2028 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2029 btrfsic_print_rem_link(state, l);
2031 if (0 == l->ref_cnt) {
2032 list_del(&l->node_ref_to);
2033 list_del(&l->node_ref_from);
2034 btrfsic_block_link_hashtable_remove(l);
2035 btrfsic_block_link_free(l);
2039 if (block->is_superblock)
2040 ret = btrfsic_map_superblock(state, bytenr,
2044 ret = btrfsic_map_block(state, bytenr, processed_len,
2048 "btrfsic: btrfsic_map_block(root @%llu)"
2049 " failed!\n", (unsigned long long)bytenr);
2052 block_ctx.datav = mapped_datav;
2053 /* the following is required in case of writes to mirrors,
2054 * use the same that was used for the lookup */
2055 block_ctx.dev = dev_state;
2056 block_ctx.dev_bytenr = dev_bytenr;
2058 if (is_metadata || state->include_extent_data) {
2059 block->never_written = 0;
2060 block->iodone_w_error = 0;
2062 block->is_iodone = 0;
2063 BUG_ON(NULL == bio_is_patched);
2064 if (!*bio_is_patched) {
2065 block->orig_bio_bh_private =
2067 block->orig_bio_bh_end_io.bio =
2069 block->next_in_same_bio = NULL;
2070 bio->bi_private = block;
2071 bio->bi_end_io = btrfsic_bio_end_io;
2072 *bio_is_patched = 1;
2074 struct btrfsic_block *chained_block =
2075 (struct btrfsic_block *)
2078 BUG_ON(NULL == chained_block);
2079 block->orig_bio_bh_private =
2080 chained_block->orig_bio_bh_private;
2081 block->orig_bio_bh_end_io.bio =
2082 chained_block->orig_bio_bh_end_io.
2084 block->next_in_same_bio = chained_block;
2085 bio->bi_private = block;
2087 } else if (NULL != bh) {
2088 block->is_iodone = 0;
2089 block->orig_bio_bh_private = bh->b_private;
2090 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2091 block->next_in_same_bio = NULL;
2092 bh->b_private = block;
2093 bh->b_end_io = btrfsic_bh_end_io;
2095 block->is_iodone = 1;
2096 block->orig_bio_bh_private = NULL;
2097 block->orig_bio_bh_end_io.bio = NULL;
2098 block->next_in_same_bio = NULL;
2102 block->flush_gen = dev_state->last_flush_gen + 1;
2103 block->submit_bio_bh_rw = submit_bio_bh_rw;
2105 block->logical_bytenr = bytenr;
2106 block->is_metadata = 1;
2107 if (block->is_superblock) {
2108 BUG_ON(PAGE_CACHE_SIZE !=
2109 BTRFS_SUPER_INFO_SIZE);
2110 ret = btrfsic_process_written_superblock(
2113 (struct btrfs_super_block *)
2115 if (state->print_mask &
2116 BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
2118 "[after new superblock is written]:\n");
2119 btrfsic_dump_tree_sub(state, block, 0);
2122 block->mirror_num = 0; /* unknown */
2123 ret = btrfsic_process_metablock(
2131 "btrfsic: btrfsic_process_metablock"
2132 "(root @%llu) failed!\n",
2133 (unsigned long long)dev_bytenr);
2135 block->is_metadata = 0;
2136 block->mirror_num = 0; /* unknown */
2137 block->generation = BTRFSIC_GENERATION_UNKNOWN;
2138 if (!state->include_extent_data
2139 && list_empty(&block->ref_from_list)) {
2141 * disk block is overwritten with extent
2142 * data (not meta data) and we are configured
2143 * to not include extent data: take the
2144 * chance and free the block's memory
2146 btrfsic_block_hashtable_remove(block);
2147 list_del(&block->all_blocks_node);
2148 btrfsic_block_free(block);
2151 btrfsic_release_block_ctx(&block_ctx);
2153 /* block has not been found in hash table */
2157 processed_len = state->datablock_size;
2158 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2159 printk(KERN_INFO "Written block (%s/%llu/?)"
2160 " !found in hash table, D.\n",
2162 (unsigned long long)dev_bytenr);
2163 if (!state->include_extent_data) {
2164 /* ignore that written D block */
2168 /* this is getting ugly for the
2169 * include_extent_data case... */
2170 bytenr = 0; /* unknown */
2171 block_ctx.start = bytenr;
2172 block_ctx.len = processed_len;
2173 block_ctx.mem_to_free = NULL;
2174 block_ctx.pagev = NULL;
2176 processed_len = state->metablock_size;
2177 bytenr = le64_to_cpu(((struct btrfs_header *)
2178 mapped_datav[0])->bytenr);
2179 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
2181 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2183 "Written block @%llu (%s/%llu/?)"
2184 " !found in hash table, M.\n",
2185 (unsigned long long)bytenr,
2187 (unsigned long long)dev_bytenr);
2189 ret = btrfsic_map_block(state, bytenr, processed_len,
2193 "btrfsic: btrfsic_map_block(root @%llu)"
2195 (unsigned long long)dev_bytenr);
2199 block_ctx.datav = mapped_datav;
2200 /* the following is required in case of writes to mirrors,
2201 * use the same that was used for the lookup */
2202 block_ctx.dev = dev_state;
2203 block_ctx.dev_bytenr = dev_bytenr;
2205 block = btrfsic_block_alloc();
2206 if (NULL == block) {
2207 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2208 btrfsic_release_block_ctx(&block_ctx);
2211 block->dev_state = dev_state;
2212 block->dev_bytenr = dev_bytenr;
2213 block->logical_bytenr = bytenr;
2214 block->is_metadata = is_metadata;
2215 block->never_written = 0;
2216 block->iodone_w_error = 0;
2217 block->mirror_num = 0; /* unknown */
2218 block->flush_gen = dev_state->last_flush_gen + 1;
2219 block->submit_bio_bh_rw = submit_bio_bh_rw;
2221 block->is_iodone = 0;
2222 BUG_ON(NULL == bio_is_patched);
2223 if (!*bio_is_patched) {
2224 block->orig_bio_bh_private = bio->bi_private;
2225 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2226 block->next_in_same_bio = NULL;
2227 bio->bi_private = block;
2228 bio->bi_end_io = btrfsic_bio_end_io;
2229 *bio_is_patched = 1;
2231 struct btrfsic_block *chained_block =
2232 (struct btrfsic_block *)
2235 BUG_ON(NULL == chained_block);
2236 block->orig_bio_bh_private =
2237 chained_block->orig_bio_bh_private;
2238 block->orig_bio_bh_end_io.bio =
2239 chained_block->orig_bio_bh_end_io.bio;
2240 block->next_in_same_bio = chained_block;
2241 bio->bi_private = block;
2243 } else if (NULL != bh) {
2244 block->is_iodone = 0;
2245 block->orig_bio_bh_private = bh->b_private;
2246 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2247 block->next_in_same_bio = NULL;
2248 bh->b_private = block;
2249 bh->b_end_io = btrfsic_bh_end_io;
2251 block->is_iodone = 1;
2252 block->orig_bio_bh_private = NULL;
2253 block->orig_bio_bh_end_io.bio = NULL;
2254 block->next_in_same_bio = NULL;
2256 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2258 "New written %c-block @%llu (%s/%llu/%d)\n",
2259 is_metadata ? 'M' : 'D',
2260 (unsigned long long)block->logical_bytenr,
2261 block->dev_state->name,
2262 (unsigned long long)block->dev_bytenr,
2264 list_add(&block->all_blocks_node, &state->all_blocks_list);
2265 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2268 ret = btrfsic_process_metablock(state, block,
2272 "btrfsic: process_metablock(root @%llu)"
2274 (unsigned long long)dev_bytenr);
2276 btrfsic_release_block_ctx(&block_ctx);
2280 BUG_ON(!processed_len);
2281 dev_bytenr += processed_len;
2282 mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
2283 num_pages -= processed_len >> PAGE_CACHE_SHIFT;
2287 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status)
2289 struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
2292 /* mutex is not held! This is not save if IO is not yet completed
2295 if (bio_error_status)
2298 BUG_ON(NULL == block);
2299 bp->bi_private = block->orig_bio_bh_private;
2300 bp->bi_end_io = block->orig_bio_bh_end_io.bio;
2303 struct btrfsic_block *next_block;
2304 struct btrfsic_dev_state *const dev_state = block->dev_state;
2306 if ((dev_state->state->print_mask &
2307 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2309 "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
2311 btrfsic_get_block_type(dev_state->state, block),
2312 (unsigned long long)block->logical_bytenr,
2314 (unsigned long long)block->dev_bytenr,
2316 next_block = block->next_in_same_bio;
2317 block->iodone_w_error = iodone_w_error;
2318 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2319 dev_state->last_flush_gen++;
2320 if ((dev_state->state->print_mask &
2321 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2323 "bio_end_io() new %s flush_gen=%llu\n",
2325 (unsigned long long)
2326 dev_state->last_flush_gen);
2328 if (block->submit_bio_bh_rw & REQ_FUA)
2329 block->flush_gen = 0; /* FUA completed means block is
2331 block->is_iodone = 1; /* for FLUSH, this releases the block */
2333 } while (NULL != block);
2335 bp->bi_end_io(bp, bio_error_status);
2338 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
2340 struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
2341 int iodone_w_error = !uptodate;
2342 struct btrfsic_dev_state *dev_state;
2344 BUG_ON(NULL == block);
2345 dev_state = block->dev_state;
2346 if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2348 "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
2350 btrfsic_get_block_type(dev_state->state, block),
2351 (unsigned long long)block->logical_bytenr,
2352 block->dev_state->name,
2353 (unsigned long long)block->dev_bytenr,
2356 block->iodone_w_error = iodone_w_error;
2357 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2358 dev_state->last_flush_gen++;
2359 if ((dev_state->state->print_mask &
2360 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2362 "bh_end_io() new %s flush_gen=%llu\n",
2364 (unsigned long long)dev_state->last_flush_gen);
2366 if (block->submit_bio_bh_rw & REQ_FUA)
2367 block->flush_gen = 0; /* FUA completed means block is on disk */
2369 bh->b_private = block->orig_bio_bh_private;
2370 bh->b_end_io = block->orig_bio_bh_end_io.bh;
2371 block->is_iodone = 1; /* for FLUSH, this releases the block */
2372 bh->b_end_io(bh, uptodate);
2375 static int btrfsic_process_written_superblock(
2376 struct btrfsic_state *state,
2377 struct btrfsic_block *const superblock,
2378 struct btrfs_super_block *const super_hdr)
2382 superblock->generation = btrfs_super_generation(super_hdr);
2383 if (!(superblock->generation > state->max_superblock_generation ||
2384 0 == state->max_superblock_generation)) {
2385 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2387 "btrfsic: superblock @%llu (%s/%llu/%d)"
2388 " with old gen %llu <= %llu\n",
2389 (unsigned long long)superblock->logical_bytenr,
2390 superblock->dev_state->name,
2391 (unsigned long long)superblock->dev_bytenr,
2392 superblock->mirror_num,
2393 (unsigned long long)
2394 btrfs_super_generation(super_hdr),
2395 (unsigned long long)
2396 state->max_superblock_generation);
2398 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2400 "btrfsic: got new superblock @%llu (%s/%llu/%d)"
2401 " with new gen %llu > %llu\n",
2402 (unsigned long long)superblock->logical_bytenr,
2403 superblock->dev_state->name,
2404 (unsigned long long)superblock->dev_bytenr,
2405 superblock->mirror_num,
2406 (unsigned long long)
2407 btrfs_super_generation(super_hdr),
2408 (unsigned long long)
2409 state->max_superblock_generation);
2411 state->max_superblock_generation =
2412 btrfs_super_generation(super_hdr);
2413 state->latest_superblock = superblock;
2416 for (pass = 0; pass < 3; pass++) {
2419 struct btrfsic_block *next_block;
2420 struct btrfsic_block_data_ctx tmp_next_block_ctx;
2421 struct btrfsic_block_link *l;
2424 const char *additional_string = NULL;
2425 struct btrfs_disk_key tmp_disk_key;
2427 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
2428 tmp_disk_key.offset = 0;
2432 tmp_disk_key.objectid =
2433 cpu_to_le64(BTRFS_ROOT_TREE_OBJECTID);
2434 additional_string = "root ";
2435 next_bytenr = btrfs_super_root(super_hdr);
2436 if (state->print_mask &
2437 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2438 printk(KERN_INFO "root@%llu\n",
2439 (unsigned long long)next_bytenr);
2442 tmp_disk_key.objectid =
2443 cpu_to_le64(BTRFS_CHUNK_TREE_OBJECTID);
2444 additional_string = "chunk ";
2445 next_bytenr = btrfs_super_chunk_root(super_hdr);
2446 if (state->print_mask &
2447 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2448 printk(KERN_INFO "chunk@%llu\n",
2449 (unsigned long long)next_bytenr);
2452 tmp_disk_key.objectid =
2453 cpu_to_le64(BTRFS_TREE_LOG_OBJECTID);
2454 additional_string = "log ";
2455 next_bytenr = btrfs_super_log_root(super_hdr);
2456 if (0 == next_bytenr)
2458 if (state->print_mask &
2459 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2460 printk(KERN_INFO "log@%llu\n",
2461 (unsigned long long)next_bytenr);
2466 btrfs_num_copies(&state->root->fs_info->mapping_tree,
2467 next_bytenr, BTRFS_SUPER_INFO_SIZE);
2468 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2469 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
2470 (unsigned long long)next_bytenr, num_copies);
2471 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2474 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2476 "btrfsic_process_written_superblock("
2477 "mirror_num=%d)\n", mirror_num);
2478 ret = btrfsic_map_block(state, next_bytenr,
2479 BTRFS_SUPER_INFO_SIZE,
2480 &tmp_next_block_ctx,
2484 "btrfsic: btrfsic_map_block(@%llu,"
2485 " mirror=%d) failed!\n",
2486 (unsigned long long)next_bytenr,
2491 next_block = btrfsic_block_lookup_or_add(
2493 &tmp_next_block_ctx,
2498 if (NULL == next_block) {
2500 "btrfsic: error, kmalloc failed!\n");
2501 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2505 next_block->disk_key = tmp_disk_key;
2507 next_block->generation =
2508 BTRFSIC_GENERATION_UNKNOWN;
2509 l = btrfsic_block_link_lookup_or_add(
2511 &tmp_next_block_ctx,
2514 BTRFSIC_GENERATION_UNKNOWN);
2515 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2521 if (-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)) {
2523 btrfsic_dump_tree(state);
2529 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2530 struct btrfsic_block *const block,
2531 int recursion_level)
2533 struct list_head *elem_ref_to;
2536 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2538 * Note that this situation can happen and does not
2539 * indicate an error in regular cases. It happens
2540 * when disk blocks are freed and later reused.
2541 * The check-integrity module is not aware of any
2542 * block free operations, it just recognizes block
2543 * write operations. Therefore it keeps the linkage
2544 * information for a block until a block is
2545 * rewritten. This can temporarily cause incorrect
2546 * and even circular linkage informations. This
2547 * causes no harm unless such blocks are referenced
2548 * by the most recent super block.
2550 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2552 "btrfsic: abort cyclic linkage (case 1).\n");
2558 * This algorithm is recursive because the amount of used stack
2559 * space is very small and the max recursion depth is limited.
2561 list_for_each(elem_ref_to, &block->ref_to_list) {
2562 const struct btrfsic_block_link *const l =
2563 list_entry(elem_ref_to, struct btrfsic_block_link,
2566 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2568 "rl=%d, %c @%llu (%s/%llu/%d)"
2569 " %u* refers to %c @%llu (%s/%llu/%d)\n",
2571 btrfsic_get_block_type(state, block),
2572 (unsigned long long)block->logical_bytenr,
2573 block->dev_state->name,
2574 (unsigned long long)block->dev_bytenr,
2577 btrfsic_get_block_type(state, l->block_ref_to),
2578 (unsigned long long)
2579 l->block_ref_to->logical_bytenr,
2580 l->block_ref_to->dev_state->name,
2581 (unsigned long long)l->block_ref_to->dev_bytenr,
2582 l->block_ref_to->mirror_num);
2583 if (l->block_ref_to->never_written) {
2584 printk(KERN_INFO "btrfs: attempt to write superblock"
2585 " which references block %c @%llu (%s/%llu/%d)"
2586 " which is never written!\n",
2587 btrfsic_get_block_type(state, l->block_ref_to),
2588 (unsigned long long)
2589 l->block_ref_to->logical_bytenr,
2590 l->block_ref_to->dev_state->name,
2591 (unsigned long long)l->block_ref_to->dev_bytenr,
2592 l->block_ref_to->mirror_num);
2594 } else if (!l->block_ref_to->is_iodone) {
2595 printk(KERN_INFO "btrfs: attempt to write superblock"
2596 " which references block %c @%llu (%s/%llu/%d)"
2597 " which is not yet iodone!\n",
2598 btrfsic_get_block_type(state, l->block_ref_to),
2599 (unsigned long long)
2600 l->block_ref_to->logical_bytenr,
2601 l->block_ref_to->dev_state->name,
2602 (unsigned long long)l->block_ref_to->dev_bytenr,
2603 l->block_ref_to->mirror_num);
2605 } else if (l->block_ref_to->iodone_w_error) {
2606 printk(KERN_INFO "btrfs: attempt to write superblock"
2607 " which references block %c @%llu (%s/%llu/%d)"
2608 " which has write error!\n",
2609 btrfsic_get_block_type(state, l->block_ref_to),
2610 (unsigned long long)
2611 l->block_ref_to->logical_bytenr,
2612 l->block_ref_to->dev_state->name,
2613 (unsigned long long)l->block_ref_to->dev_bytenr,
2614 l->block_ref_to->mirror_num);
2616 } else if (l->parent_generation !=
2617 l->block_ref_to->generation &&
2618 BTRFSIC_GENERATION_UNKNOWN !=
2619 l->parent_generation &&
2620 BTRFSIC_GENERATION_UNKNOWN !=
2621 l->block_ref_to->generation) {
2622 printk(KERN_INFO "btrfs: attempt to write superblock"
2623 " which references block %c @%llu (%s/%llu/%d)"
2624 " with generation %llu !="
2625 " parent generation %llu!\n",
2626 btrfsic_get_block_type(state, l->block_ref_to),
2627 (unsigned long long)
2628 l->block_ref_to->logical_bytenr,
2629 l->block_ref_to->dev_state->name,
2630 (unsigned long long)l->block_ref_to->dev_bytenr,
2631 l->block_ref_to->mirror_num,
2632 (unsigned long long)l->block_ref_to->generation,
2633 (unsigned long long)l->parent_generation);
2635 } else if (l->block_ref_to->flush_gen >
2636 l->block_ref_to->dev_state->last_flush_gen) {
2637 printk(KERN_INFO "btrfs: attempt to write superblock"
2638 " which references block %c @%llu (%s/%llu/%d)"
2639 " which is not flushed out of disk's write cache"
2640 " (block flush_gen=%llu,"
2641 " dev->flush_gen=%llu)!\n",
2642 btrfsic_get_block_type(state, l->block_ref_to),
2643 (unsigned long long)
2644 l->block_ref_to->logical_bytenr,
2645 l->block_ref_to->dev_state->name,
2646 (unsigned long long)l->block_ref_to->dev_bytenr,
2647 l->block_ref_to->mirror_num,
2648 (unsigned long long)block->flush_gen,
2649 (unsigned long long)
2650 l->block_ref_to->dev_state->last_flush_gen);
2652 } else if (-1 == btrfsic_check_all_ref_blocks(state,
2663 static int btrfsic_is_block_ref_by_superblock(
2664 const struct btrfsic_state *state,
2665 const struct btrfsic_block *block,
2666 int recursion_level)
2668 struct list_head *elem_ref_from;
2670 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2671 /* refer to comment at "abort cyclic linkage (case 1)" */
2672 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2674 "btrfsic: abort cyclic linkage (case 2).\n");
2680 * This algorithm is recursive because the amount of used stack space
2681 * is very small and the max recursion depth is limited.
2683 list_for_each(elem_ref_from, &block->ref_from_list) {
2684 const struct btrfsic_block_link *const l =
2685 list_entry(elem_ref_from, struct btrfsic_block_link,
2688 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2690 "rl=%d, %c @%llu (%s/%llu/%d)"
2691 " is ref %u* from %c @%llu (%s/%llu/%d)\n",
2693 btrfsic_get_block_type(state, block),
2694 (unsigned long long)block->logical_bytenr,
2695 block->dev_state->name,
2696 (unsigned long long)block->dev_bytenr,
2699 btrfsic_get_block_type(state, l->block_ref_from),
2700 (unsigned long long)
2701 l->block_ref_from->logical_bytenr,
2702 l->block_ref_from->dev_state->name,
2703 (unsigned long long)
2704 l->block_ref_from->dev_bytenr,
2705 l->block_ref_from->mirror_num);
2706 if (l->block_ref_from->is_superblock &&
2707 state->latest_superblock->dev_bytenr ==
2708 l->block_ref_from->dev_bytenr &&
2709 state->latest_superblock->dev_state->bdev ==
2710 l->block_ref_from->dev_state->bdev)
2712 else if (btrfsic_is_block_ref_by_superblock(state,
2722 static void btrfsic_print_add_link(const struct btrfsic_state *state,
2723 const struct btrfsic_block_link *l)
2726 "Add %u* link from %c @%llu (%s/%llu/%d)"
2727 " to %c @%llu (%s/%llu/%d).\n",
2729 btrfsic_get_block_type(state, l->block_ref_from),
2730 (unsigned long long)l->block_ref_from->logical_bytenr,
2731 l->block_ref_from->dev_state->name,
2732 (unsigned long long)l->block_ref_from->dev_bytenr,
2733 l->block_ref_from->mirror_num,
2734 btrfsic_get_block_type(state, l->block_ref_to),
2735 (unsigned long long)l->block_ref_to->logical_bytenr,
2736 l->block_ref_to->dev_state->name,
2737 (unsigned long long)l->block_ref_to->dev_bytenr,
2738 l->block_ref_to->mirror_num);
2741 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2742 const struct btrfsic_block_link *l)
2745 "Rem %u* link from %c @%llu (%s/%llu/%d)"
2746 " to %c @%llu (%s/%llu/%d).\n",
2748 btrfsic_get_block_type(state, l->block_ref_from),
2749 (unsigned long long)l->block_ref_from->logical_bytenr,
2750 l->block_ref_from->dev_state->name,
2751 (unsigned long long)l->block_ref_from->dev_bytenr,
2752 l->block_ref_from->mirror_num,
2753 btrfsic_get_block_type(state, l->block_ref_to),
2754 (unsigned long long)l->block_ref_to->logical_bytenr,
2755 l->block_ref_to->dev_state->name,
2756 (unsigned long long)l->block_ref_to->dev_bytenr,
2757 l->block_ref_to->mirror_num);
2760 static char btrfsic_get_block_type(const struct btrfsic_state *state,
2761 const struct btrfsic_block *block)
2763 if (block->is_superblock &&
2764 state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2765 state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2767 else if (block->is_superblock)
2769 else if (block->is_metadata)
2775 static void btrfsic_dump_tree(const struct btrfsic_state *state)
2777 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2780 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2781 const struct btrfsic_block *block,
2784 struct list_head *elem_ref_to;
2786 static char buf[80];
2787 int cursor_position;
2790 * Should better fill an on-stack buffer with a complete line and
2791 * dump it at once when it is time to print a newline character.
2795 * This algorithm is recursive because the amount of used stack space
2796 * is very small and the max recursion depth is limited.
2798 indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
2799 btrfsic_get_block_type(state, block),
2800 (unsigned long long)block->logical_bytenr,
2801 block->dev_state->name,
2802 (unsigned long long)block->dev_bytenr,
2804 if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2809 indent_level += indent_add;
2810 if (list_empty(&block->ref_to_list)) {
2814 if (block->mirror_num > 1 &&
2815 !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2820 cursor_position = indent_level;
2821 list_for_each(elem_ref_to, &block->ref_to_list) {
2822 const struct btrfsic_block_link *const l =
2823 list_entry(elem_ref_to, struct btrfsic_block_link,
2826 while (cursor_position < indent_level) {
2831 indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2833 indent_add = sprintf(buf, " --> ");
2834 if (indent_level + indent_add >
2835 BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2837 cursor_position = 0;
2843 btrfsic_dump_tree_sub(state, l->block_ref_to,
2844 indent_level + indent_add);
2845 cursor_position = 0;
2849 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2850 struct btrfsic_state *state,
2851 struct btrfsic_block_data_ctx *next_block_ctx,
2852 struct btrfsic_block *next_block,
2853 struct btrfsic_block *from_block,
2854 u64 parent_generation)
2856 struct btrfsic_block_link *l;
2858 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2859 next_block_ctx->dev_bytenr,
2860 from_block->dev_state->bdev,
2861 from_block->dev_bytenr,
2862 &state->block_link_hashtable);
2864 l = btrfsic_block_link_alloc();
2867 "btrfsic: error, kmalloc" " failed!\n");
2871 l->block_ref_to = next_block;
2872 l->block_ref_from = from_block;
2874 l->parent_generation = parent_generation;
2876 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2877 btrfsic_print_add_link(state, l);
2879 list_add(&l->node_ref_to, &from_block->ref_to_list);
2880 list_add(&l->node_ref_from, &next_block->ref_from_list);
2882 btrfsic_block_link_hashtable_add(l,
2883 &state->block_link_hashtable);
2886 l->parent_generation = parent_generation;
2887 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2888 btrfsic_print_add_link(state, l);
2894 static struct btrfsic_block *btrfsic_block_lookup_or_add(
2895 struct btrfsic_state *state,
2896 struct btrfsic_block_data_ctx *block_ctx,
2897 const char *additional_string,
2904 struct btrfsic_block *block;
2906 block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2907 block_ctx->dev_bytenr,
2908 &state->block_hashtable);
2909 if (NULL == block) {
2910 struct btrfsic_dev_state *dev_state;
2912 block = btrfsic_block_alloc();
2913 if (NULL == block) {
2914 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2917 dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
2918 if (NULL == dev_state) {
2920 "btrfsic: error, lookup dev_state failed!\n");
2921 btrfsic_block_free(block);
2924 block->dev_state = dev_state;
2925 block->dev_bytenr = block_ctx->dev_bytenr;
2926 block->logical_bytenr = block_ctx->start;
2927 block->is_metadata = is_metadata;
2928 block->is_iodone = is_iodone;
2929 block->never_written = never_written;
2930 block->mirror_num = mirror_num;
2931 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2933 "New %s%c-block @%llu (%s/%llu/%d)\n",
2935 btrfsic_get_block_type(state, block),
2936 (unsigned long long)block->logical_bytenr,
2938 (unsigned long long)block->dev_bytenr,
2940 list_add(&block->all_blocks_node, &state->all_blocks_list);
2941 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2942 if (NULL != was_created)
2945 if (NULL != was_created)
2952 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2954 struct btrfsic_dev_state *dev_state,
2960 struct btrfsic_block_data_ctx block_ctx;
2963 num_copies = btrfs_num_copies(&state->root->fs_info->mapping_tree,
2964 bytenr, state->metablock_size);
2966 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2967 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
2968 &block_ctx, mirror_num);
2970 printk(KERN_INFO "btrfsic:"
2971 " btrfsic_map_block(logical @%llu,"
2972 " mirror %d) failed!\n",
2973 (unsigned long long)bytenr, mirror_num);
2977 if (dev_state->bdev == block_ctx.dev->bdev &&
2978 dev_bytenr == block_ctx.dev_bytenr) {
2980 btrfsic_release_block_ctx(&block_ctx);
2983 btrfsic_release_block_ctx(&block_ctx);
2987 printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
2988 " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
2989 " phys_bytenr=%llu)!\n",
2990 (unsigned long long)bytenr, dev_state->name,
2991 (unsigned long long)dev_bytenr);
2992 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2993 ret = btrfsic_map_block(state, bytenr,
2994 state->metablock_size,
2995 &block_ctx, mirror_num);
2999 printk(KERN_INFO "Read logical bytenr @%llu maps to"
3001 (unsigned long long)bytenr,
3002 block_ctx.dev->name,
3003 (unsigned long long)block_ctx.dev_bytenr,
3010 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
3011 struct block_device *bdev)
3013 struct btrfsic_dev_state *ds;
3015 ds = btrfsic_dev_state_hashtable_lookup(bdev,
3016 &btrfsic_dev_state_hashtable);
3020 int btrfsic_submit_bh(int rw, struct buffer_head *bh)
3022 struct btrfsic_dev_state *dev_state;
3024 if (!btrfsic_is_initialized)
3025 return submit_bh(rw, bh);
3027 mutex_lock(&btrfsic_mutex);
3028 /* since btrfsic_submit_bh() might also be called before
3029 * btrfsic_mount(), this might return NULL */
3030 dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
3032 /* Only called to write the superblock (incl. FLUSH/FUA) */
3033 if (NULL != dev_state &&
3034 (rw & WRITE) && bh->b_size > 0) {
3037 dev_bytenr = 4096 * bh->b_blocknr;
3038 if (dev_state->state->print_mask &
3039 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3041 "submit_bh(rw=0x%x, blocknr=%lu (bytenr %llu),"
3042 " size=%lu, data=%p, bdev=%p)\n",
3043 rw, (unsigned long)bh->b_blocknr,
3044 (unsigned long long)dev_bytenr,
3045 (unsigned long)bh->b_size, bh->b_data,
3047 btrfsic_process_written_block(dev_state, dev_bytenr,
3048 &bh->b_data, 1, NULL,
3050 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
3051 if (dev_state->state->print_mask &
3052 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3054 "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
3056 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
3057 if ((dev_state->state->print_mask &
3058 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3059 BTRFSIC_PRINT_MASK_VERBOSE)))
3061 "btrfsic_submit_bh(%s) with FLUSH"
3062 " but dummy block already in use"
3066 struct btrfsic_block *const block =
3067 &dev_state->dummy_block_for_bio_bh_flush;
3069 block->is_iodone = 0;
3070 block->never_written = 0;
3071 block->iodone_w_error = 0;
3072 block->flush_gen = dev_state->last_flush_gen + 1;
3073 block->submit_bio_bh_rw = rw;
3074 block->orig_bio_bh_private = bh->b_private;
3075 block->orig_bio_bh_end_io.bh = bh->b_end_io;
3076 block->next_in_same_bio = NULL;
3077 bh->b_private = block;
3078 bh->b_end_io = btrfsic_bh_end_io;
3081 mutex_unlock(&btrfsic_mutex);
3082 return submit_bh(rw, bh);
3085 void btrfsic_submit_bio(int rw, struct bio *bio)
3087 struct btrfsic_dev_state *dev_state;
3089 if (!btrfsic_is_initialized) {
3090 submit_bio(rw, bio);
3094 mutex_lock(&btrfsic_mutex);
3095 /* since btrfsic_submit_bio() is also called before
3096 * btrfsic_mount(), this might return NULL */
3097 dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
3098 if (NULL != dev_state &&
3099 (rw & WRITE) && NULL != bio->bi_io_vec) {
3103 char **mapped_datav;
3105 dev_bytenr = 512 * bio->bi_sector;
3107 if (dev_state->state->print_mask &
3108 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3110 "submit_bio(rw=0x%x, bi_vcnt=%u,"
3111 " bi_sector=%lu (bytenr %llu), bi_bdev=%p)\n",
3112 rw, bio->bi_vcnt, (unsigned long)bio->bi_sector,
3113 (unsigned long long)dev_bytenr,
3116 mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
3120 for (i = 0; i < bio->bi_vcnt; i++) {
3121 BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
3122 mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
3123 if (!mapped_datav[i]) {
3126 kunmap(bio->bi_io_vec[i].bv_page);
3128 kfree(mapped_datav);
3131 if ((BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3132 BTRFSIC_PRINT_MASK_VERBOSE) ==
3133 (dev_state->state->print_mask &
3134 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3135 BTRFSIC_PRINT_MASK_VERBOSE)))
3137 "#%u: page=%p, len=%u, offset=%u\n",
3138 i, bio->bi_io_vec[i].bv_page,
3139 bio->bi_io_vec[i].bv_len,
3140 bio->bi_io_vec[i].bv_offset);
3142 btrfsic_process_written_block(dev_state, dev_bytenr,
3143 mapped_datav, bio->bi_vcnt,
3144 bio, &bio_is_patched,
3148 kunmap(bio->bi_io_vec[i].bv_page);
3150 kfree(mapped_datav);
3151 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
3152 if (dev_state->state->print_mask &
3153 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3155 "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
3157 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
3158 if ((dev_state->state->print_mask &
3159 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3160 BTRFSIC_PRINT_MASK_VERBOSE)))
3162 "btrfsic_submit_bio(%s) with FLUSH"
3163 " but dummy block already in use"
3167 struct btrfsic_block *const block =
3168 &dev_state->dummy_block_for_bio_bh_flush;
3170 block->is_iodone = 0;
3171 block->never_written = 0;
3172 block->iodone_w_error = 0;
3173 block->flush_gen = dev_state->last_flush_gen + 1;
3174 block->submit_bio_bh_rw = rw;
3175 block->orig_bio_bh_private = bio->bi_private;
3176 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
3177 block->next_in_same_bio = NULL;
3178 bio->bi_private = block;
3179 bio->bi_end_io = btrfsic_bio_end_io;
3183 mutex_unlock(&btrfsic_mutex);
3185 submit_bio(rw, bio);
3188 int btrfsic_mount(struct btrfs_root *root,
3189 struct btrfs_fs_devices *fs_devices,
3190 int including_extent_data, u32 print_mask)
3193 struct btrfsic_state *state;
3194 struct list_head *dev_head = &fs_devices->devices;
3195 struct btrfs_device *device;
3197 if (root->nodesize != root->leafsize) {
3199 "btrfsic: cannot handle nodesize %d != leafsize %d!\n",
3200 root->nodesize, root->leafsize);
3203 if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
3205 "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3206 root->nodesize, (unsigned long)PAGE_CACHE_SIZE);
3209 if (root->leafsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3211 "btrfsic: cannot handle leafsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3212 root->leafsize, (unsigned long)PAGE_CACHE_SIZE);
3215 if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3217 "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3218 root->sectorsize, (unsigned long)PAGE_CACHE_SIZE);
3221 state = kzalloc(sizeof(*state), GFP_NOFS);
3222 if (NULL == state) {
3223 printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
3227 if (!btrfsic_is_initialized) {
3228 mutex_init(&btrfsic_mutex);
3229 btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
3230 btrfsic_is_initialized = 1;
3232 mutex_lock(&btrfsic_mutex);
3234 state->print_mask = print_mask;
3235 state->include_extent_data = including_extent_data;
3236 state->csum_size = 0;
3237 state->metablock_size = root->nodesize;
3238 state->datablock_size = root->sectorsize;
3239 INIT_LIST_HEAD(&state->all_blocks_list);
3240 btrfsic_block_hashtable_init(&state->block_hashtable);
3241 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
3242 state->max_superblock_generation = 0;
3243 state->latest_superblock = NULL;
3245 list_for_each_entry(device, dev_head, dev_list) {
3246 struct btrfsic_dev_state *ds;
3249 if (!device->bdev || !device->name)
3252 ds = btrfsic_dev_state_alloc();
3255 "btrfs check-integrity: kmalloc() failed!\n");
3256 mutex_unlock(&btrfsic_mutex);
3259 ds->bdev = device->bdev;
3261 bdevname(ds->bdev, ds->name);
3262 ds->name[BDEVNAME_SIZE - 1] = '\0';
3263 for (p = ds->name; *p != '\0'; p++);
3264 while (p > ds->name && *p != '/')
3268 strlcpy(ds->name, p, sizeof(ds->name));
3269 btrfsic_dev_state_hashtable_add(ds,
3270 &btrfsic_dev_state_hashtable);
3273 ret = btrfsic_process_superblock(state, fs_devices);
3275 mutex_unlock(&btrfsic_mutex);
3276 btrfsic_unmount(root, fs_devices);
3280 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
3281 btrfsic_dump_database(state);
3282 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
3283 btrfsic_dump_tree(state);
3285 mutex_unlock(&btrfsic_mutex);
3289 void btrfsic_unmount(struct btrfs_root *root,
3290 struct btrfs_fs_devices *fs_devices)
3292 struct list_head *elem_all;
3293 struct list_head *tmp_all;
3294 struct btrfsic_state *state;
3295 struct list_head *dev_head = &fs_devices->devices;
3296 struct btrfs_device *device;
3298 if (!btrfsic_is_initialized)
3301 mutex_lock(&btrfsic_mutex);
3304 list_for_each_entry(device, dev_head, dev_list) {
3305 struct btrfsic_dev_state *ds;
3307 if (!device->bdev || !device->name)
3310 ds = btrfsic_dev_state_hashtable_lookup(
3312 &btrfsic_dev_state_hashtable);
3315 btrfsic_dev_state_hashtable_remove(ds);
3316 btrfsic_dev_state_free(ds);
3320 if (NULL == state) {
3322 "btrfsic: error, cannot find state information"
3324 mutex_unlock(&btrfsic_mutex);
3329 * Don't care about keeping the lists' state up to date,
3330 * just free all memory that was allocated dynamically.
3331 * Free the blocks and the block_links.
3333 list_for_each_safe(elem_all, tmp_all, &state->all_blocks_list) {
3334 struct btrfsic_block *const b_all =
3335 list_entry(elem_all, struct btrfsic_block,
3337 struct list_head *elem_ref_to;
3338 struct list_head *tmp_ref_to;
3340 list_for_each_safe(elem_ref_to, tmp_ref_to,
3341 &b_all->ref_to_list) {
3342 struct btrfsic_block_link *const l =
3343 list_entry(elem_ref_to,
3344 struct btrfsic_block_link,
3347 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
3348 btrfsic_print_rem_link(state, l);
3351 if (0 == l->ref_cnt)
3352 btrfsic_block_link_free(l);
3355 if (b_all->is_iodone || b_all->never_written)
3356 btrfsic_block_free(b_all);
3358 printk(KERN_INFO "btrfs: attempt to free %c-block"
3359 " @%llu (%s/%llu/%d) on umount which is"
3360 " not yet iodone!\n",
3361 btrfsic_get_block_type(state, b_all),
3362 (unsigned long long)b_all->logical_bytenr,
3363 b_all->dev_state->name,
3364 (unsigned long long)b_all->dev_bytenr,
3368 mutex_unlock(&btrfsic_mutex);