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; /* 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 int btrfsic_test_for_metadata(struct btrfsic_state *state,
327 char **datav, unsigned int num_pages);
328 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
329 u64 dev_bytenr, char **mapped_datav,
330 unsigned int num_pages,
331 struct bio *bio, int *bio_is_patched,
332 struct buffer_head *bh,
333 int submit_bio_bh_rw);
334 static int btrfsic_process_written_superblock(
335 struct btrfsic_state *state,
336 struct btrfsic_block *const block,
337 struct btrfs_super_block *const super_hdr);
338 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status);
339 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
340 static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
341 const struct btrfsic_block *block,
342 int recursion_level);
343 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
344 struct btrfsic_block *const block,
345 int recursion_level);
346 static void btrfsic_print_add_link(const struct btrfsic_state *state,
347 const struct btrfsic_block_link *l);
348 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
349 const struct btrfsic_block_link *l);
350 static char btrfsic_get_block_type(const struct btrfsic_state *state,
351 const struct btrfsic_block *block);
352 static void btrfsic_dump_tree(const struct btrfsic_state *state);
353 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
354 const struct btrfsic_block *block,
356 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
357 struct btrfsic_state *state,
358 struct btrfsic_block_data_ctx *next_block_ctx,
359 struct btrfsic_block *next_block,
360 struct btrfsic_block *from_block,
361 u64 parent_generation);
362 static struct btrfsic_block *btrfsic_block_lookup_or_add(
363 struct btrfsic_state *state,
364 struct btrfsic_block_data_ctx *block_ctx,
365 const char *additional_string,
371 static int btrfsic_process_superblock_dev_mirror(
372 struct btrfsic_state *state,
373 struct btrfsic_dev_state *dev_state,
374 struct btrfs_device *device,
375 int superblock_mirror_num,
376 struct btrfsic_dev_state **selected_dev_state,
377 struct btrfs_super_block *selected_super);
378 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
379 struct block_device *bdev);
380 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
382 struct btrfsic_dev_state *dev_state,
385 static struct mutex btrfsic_mutex;
386 static int btrfsic_is_initialized;
387 static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
390 static void btrfsic_block_init(struct btrfsic_block *b)
392 b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
395 b->logical_bytenr = 0;
396 b->generation = BTRFSIC_GENERATION_UNKNOWN;
397 b->disk_key.objectid = 0;
398 b->disk_key.type = 0;
399 b->disk_key.offset = 0;
401 b->is_superblock = 0;
403 b->iodone_w_error = 0;
404 b->never_written = 0;
406 b->next_in_same_bio = NULL;
407 b->orig_bio_bh_private = NULL;
408 b->orig_bio_bh_end_io.bio = NULL;
409 INIT_LIST_HEAD(&b->collision_resolving_node);
410 INIT_LIST_HEAD(&b->all_blocks_node);
411 INIT_LIST_HEAD(&b->ref_to_list);
412 INIT_LIST_HEAD(&b->ref_from_list);
413 b->submit_bio_bh_rw = 0;
417 static struct btrfsic_block *btrfsic_block_alloc(void)
419 struct btrfsic_block *b;
421 b = kzalloc(sizeof(*b), GFP_NOFS);
423 btrfsic_block_init(b);
428 static void btrfsic_block_free(struct btrfsic_block *b)
430 BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
434 static void btrfsic_block_link_init(struct btrfsic_block_link *l)
436 l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
438 INIT_LIST_HEAD(&l->node_ref_to);
439 INIT_LIST_HEAD(&l->node_ref_from);
440 INIT_LIST_HEAD(&l->collision_resolving_node);
441 l->block_ref_to = NULL;
442 l->block_ref_from = NULL;
445 static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
447 struct btrfsic_block_link *l;
449 l = kzalloc(sizeof(*l), GFP_NOFS);
451 btrfsic_block_link_init(l);
456 static void btrfsic_block_link_free(struct btrfsic_block_link *l)
458 BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
462 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
464 ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
468 INIT_LIST_HEAD(&ds->collision_resolving_node);
469 ds->last_flush_gen = 0;
470 btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
471 ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
472 ds->dummy_block_for_bio_bh_flush.dev_state = ds;
475 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
477 struct btrfsic_dev_state *ds;
479 ds = kzalloc(sizeof(*ds), GFP_NOFS);
481 btrfsic_dev_state_init(ds);
486 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
488 BUG_ON(!(NULL == ds ||
489 BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
493 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
497 for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
498 INIT_LIST_HEAD(h->table + i);
501 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
502 struct btrfsic_block_hashtable *h)
504 const unsigned int hashval =
505 (((unsigned int)(b->dev_bytenr >> 16)) ^
506 ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
507 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
509 list_add(&b->collision_resolving_node, h->table + hashval);
512 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
514 list_del(&b->collision_resolving_node);
517 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
518 struct block_device *bdev,
520 struct btrfsic_block_hashtable *h)
522 const unsigned int hashval =
523 (((unsigned int)(dev_bytenr >> 16)) ^
524 ((unsigned int)((uintptr_t)bdev))) &
525 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
526 struct list_head *elem;
528 list_for_each(elem, h->table + hashval) {
529 struct btrfsic_block *const b =
530 list_entry(elem, struct btrfsic_block,
531 collision_resolving_node);
533 if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
540 static void btrfsic_block_link_hashtable_init(
541 struct btrfsic_block_link_hashtable *h)
545 for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
546 INIT_LIST_HEAD(h->table + i);
549 static void btrfsic_block_link_hashtable_add(
550 struct btrfsic_block_link *l,
551 struct btrfsic_block_link_hashtable *h)
553 const unsigned int hashval =
554 (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
555 ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
556 ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
557 ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
558 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
560 BUG_ON(NULL == l->block_ref_to);
561 BUG_ON(NULL == l->block_ref_from);
562 list_add(&l->collision_resolving_node, h->table + hashval);
565 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
567 list_del(&l->collision_resolving_node);
570 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
571 struct block_device *bdev_ref_to,
572 u64 dev_bytenr_ref_to,
573 struct block_device *bdev_ref_from,
574 u64 dev_bytenr_ref_from,
575 struct btrfsic_block_link_hashtable *h)
577 const unsigned int hashval =
578 (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
579 ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
580 ((unsigned int)((uintptr_t)bdev_ref_to)) ^
581 ((unsigned int)((uintptr_t)bdev_ref_from))) &
582 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
583 struct list_head *elem;
585 list_for_each(elem, h->table + hashval) {
586 struct btrfsic_block_link *const l =
587 list_entry(elem, struct btrfsic_block_link,
588 collision_resolving_node);
590 BUG_ON(NULL == l->block_ref_to);
591 BUG_ON(NULL == l->block_ref_from);
592 if (l->block_ref_to->dev_state->bdev == bdev_ref_to &&
593 l->block_ref_to->dev_bytenr == dev_bytenr_ref_to &&
594 l->block_ref_from->dev_state->bdev == bdev_ref_from &&
595 l->block_ref_from->dev_bytenr == dev_bytenr_ref_from)
602 static void btrfsic_dev_state_hashtable_init(
603 struct btrfsic_dev_state_hashtable *h)
607 for (i = 0; i < BTRFSIC_DEV2STATE_HASHTABLE_SIZE; i++)
608 INIT_LIST_HEAD(h->table + i);
611 static void btrfsic_dev_state_hashtable_add(
612 struct btrfsic_dev_state *ds,
613 struct btrfsic_dev_state_hashtable *h)
615 const unsigned int hashval =
616 (((unsigned int)((uintptr_t)ds->bdev)) &
617 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
619 list_add(&ds->collision_resolving_node, h->table + hashval);
622 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds)
624 list_del(&ds->collision_resolving_node);
627 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
628 struct block_device *bdev,
629 struct btrfsic_dev_state_hashtable *h)
631 const unsigned int hashval =
632 (((unsigned int)((uintptr_t)bdev)) &
633 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
634 struct list_head *elem;
636 list_for_each(elem, h->table + hashval) {
637 struct btrfsic_dev_state *const ds =
638 list_entry(elem, struct btrfsic_dev_state,
639 collision_resolving_node);
641 if (ds->bdev == bdev)
648 static int btrfsic_process_superblock(struct btrfsic_state *state,
649 struct btrfs_fs_devices *fs_devices)
652 struct btrfs_super_block *selected_super;
653 struct list_head *dev_head = &fs_devices->devices;
654 struct btrfs_device *device;
655 struct btrfsic_dev_state *selected_dev_state = NULL;
658 BUG_ON(NULL == state);
659 selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
660 if (NULL == selected_super) {
661 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
665 list_for_each_entry(device, dev_head, dev_list) {
667 struct btrfsic_dev_state *dev_state;
669 if (!device->bdev || !device->name)
672 dev_state = btrfsic_dev_state_lookup(device->bdev);
673 BUG_ON(NULL == dev_state);
674 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
675 ret = btrfsic_process_superblock_dev_mirror(
676 state, dev_state, device, i,
677 &selected_dev_state, selected_super);
678 if (0 != ret && 0 == i) {
679 kfree(selected_super);
685 if (NULL == state->latest_superblock) {
686 printk(KERN_INFO "btrfsic: no superblock found!\n");
687 kfree(selected_super);
691 state->csum_size = btrfs_super_csum_size(selected_super);
693 for (pass = 0; pass < 3; pass++) {
700 next_bytenr = btrfs_super_root(selected_super);
701 if (state->print_mask &
702 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
703 printk(KERN_INFO "root@%llu\n", next_bytenr);
706 next_bytenr = btrfs_super_chunk_root(selected_super);
707 if (state->print_mask &
708 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
709 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
712 next_bytenr = btrfs_super_log_root(selected_super);
713 if (0 == next_bytenr)
715 if (state->print_mask &
716 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
717 printk(KERN_INFO "log@%llu\n", next_bytenr);
722 btrfs_num_copies(state->root->fs_info,
723 next_bytenr, state->metablock_size);
724 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
725 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
726 next_bytenr, num_copies);
728 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
729 struct btrfsic_block *next_block;
730 struct btrfsic_block_data_ctx tmp_next_block_ctx;
731 struct btrfsic_block_link *l;
733 ret = btrfsic_map_block(state, next_bytenr,
734 state->metablock_size,
738 printk(KERN_INFO "btrfsic:"
739 " btrfsic_map_block(root @%llu,"
740 " mirror %d) failed!\n",
741 next_bytenr, mirror_num);
742 kfree(selected_super);
746 next_block = btrfsic_block_hashtable_lookup(
747 tmp_next_block_ctx.dev->bdev,
748 tmp_next_block_ctx.dev_bytenr,
749 &state->block_hashtable);
750 BUG_ON(NULL == next_block);
752 l = btrfsic_block_link_hashtable_lookup(
753 tmp_next_block_ctx.dev->bdev,
754 tmp_next_block_ctx.dev_bytenr,
755 state->latest_superblock->dev_state->
757 state->latest_superblock->dev_bytenr,
758 &state->block_link_hashtable);
761 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
762 if (ret < (int)PAGE_CACHE_SIZE) {
764 "btrfsic: read @logical %llu failed!\n",
765 tmp_next_block_ctx.start);
766 btrfsic_release_block_ctx(&tmp_next_block_ctx);
767 kfree(selected_super);
771 ret = btrfsic_process_metablock(state,
774 BTRFS_MAX_LEVEL + 3, 1);
775 btrfsic_release_block_ctx(&tmp_next_block_ctx);
779 kfree(selected_super);
783 static int btrfsic_process_superblock_dev_mirror(
784 struct btrfsic_state *state,
785 struct btrfsic_dev_state *dev_state,
786 struct btrfs_device *device,
787 int superblock_mirror_num,
788 struct btrfsic_dev_state **selected_dev_state,
789 struct btrfs_super_block *selected_super)
791 struct btrfs_super_block *super_tmp;
793 struct buffer_head *bh;
794 struct btrfsic_block *superblock_tmp;
796 struct block_device *const superblock_bdev = device->bdev;
798 /* super block bytenr is always the unmapped device bytenr */
799 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
800 if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
802 bh = __bread(superblock_bdev, dev_bytenr / 4096,
803 BTRFS_SUPER_INFO_SIZE);
806 super_tmp = (struct btrfs_super_block *)
807 (bh->b_data + (dev_bytenr & 4095));
809 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
810 btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
811 memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
812 btrfs_super_nodesize(super_tmp) != state->metablock_size ||
813 btrfs_super_leafsize(super_tmp) != state->metablock_size ||
814 btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
820 btrfsic_block_hashtable_lookup(superblock_bdev,
822 &state->block_hashtable);
823 if (NULL == superblock_tmp) {
824 superblock_tmp = btrfsic_block_alloc();
825 if (NULL == superblock_tmp) {
826 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
830 /* for superblock, only the dev_bytenr makes sense */
831 superblock_tmp->dev_bytenr = dev_bytenr;
832 superblock_tmp->dev_state = dev_state;
833 superblock_tmp->logical_bytenr = dev_bytenr;
834 superblock_tmp->generation = btrfs_super_generation(super_tmp);
835 superblock_tmp->is_metadata = 1;
836 superblock_tmp->is_superblock = 1;
837 superblock_tmp->is_iodone = 1;
838 superblock_tmp->never_written = 0;
839 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
840 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
841 printk_in_rcu(KERN_INFO "New initial S-block (bdev %p, %s)"
842 " @%llu (%s/%llu/%d)\n",
844 rcu_str_deref(device->name), dev_bytenr,
845 dev_state->name, dev_bytenr,
846 superblock_mirror_num);
847 list_add(&superblock_tmp->all_blocks_node,
848 &state->all_blocks_list);
849 btrfsic_block_hashtable_add(superblock_tmp,
850 &state->block_hashtable);
853 /* select the one with the highest generation field */
854 if (btrfs_super_generation(super_tmp) >
855 state->max_superblock_generation ||
856 0 == state->max_superblock_generation) {
857 memcpy(selected_super, super_tmp, sizeof(*selected_super));
858 *selected_dev_state = dev_state;
859 state->max_superblock_generation =
860 btrfs_super_generation(super_tmp);
861 state->latest_superblock = superblock_tmp;
864 for (pass = 0; pass < 3; pass++) {
868 const char *additional_string = NULL;
869 struct btrfs_disk_key tmp_disk_key;
871 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
872 tmp_disk_key.offset = 0;
875 btrfs_set_disk_key_objectid(&tmp_disk_key,
876 BTRFS_ROOT_TREE_OBJECTID);
877 additional_string = "initial root ";
878 next_bytenr = btrfs_super_root(super_tmp);
881 btrfs_set_disk_key_objectid(&tmp_disk_key,
882 BTRFS_CHUNK_TREE_OBJECTID);
883 additional_string = "initial chunk ";
884 next_bytenr = btrfs_super_chunk_root(super_tmp);
887 btrfs_set_disk_key_objectid(&tmp_disk_key,
888 BTRFS_TREE_LOG_OBJECTID);
889 additional_string = "initial log ";
890 next_bytenr = btrfs_super_log_root(super_tmp);
891 if (0 == next_bytenr)
897 btrfs_num_copies(state->root->fs_info,
898 next_bytenr, state->metablock_size);
899 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
900 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
901 next_bytenr, num_copies);
902 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
903 struct btrfsic_block *next_block;
904 struct btrfsic_block_data_ctx tmp_next_block_ctx;
905 struct btrfsic_block_link *l;
907 if (btrfsic_map_block(state, next_bytenr,
908 state->metablock_size,
911 printk(KERN_INFO "btrfsic: btrfsic_map_block("
912 "bytenr @%llu, mirror %d) failed!\n",
913 next_bytenr, mirror_num);
918 next_block = btrfsic_block_lookup_or_add(
919 state, &tmp_next_block_ctx,
920 additional_string, 1, 1, 0,
922 if (NULL == next_block) {
923 btrfsic_release_block_ctx(&tmp_next_block_ctx);
928 next_block->disk_key = tmp_disk_key;
929 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
930 l = btrfsic_block_link_lookup_or_add(
931 state, &tmp_next_block_ctx,
932 next_block, superblock_tmp,
933 BTRFSIC_GENERATION_UNKNOWN);
934 btrfsic_release_block_ctx(&tmp_next_block_ctx);
941 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
942 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
948 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
950 struct btrfsic_stack_frame *sf;
952 sf = kzalloc(sizeof(*sf), GFP_NOFS);
954 printk(KERN_INFO "btrfsic: alloc memory failed!\n");
956 sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
960 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
962 BUG_ON(!(NULL == sf ||
963 BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
967 static int btrfsic_process_metablock(
968 struct btrfsic_state *state,
969 struct btrfsic_block *const first_block,
970 struct btrfsic_block_data_ctx *const first_block_ctx,
971 int first_limit_nesting, int force_iodone_flag)
973 struct btrfsic_stack_frame initial_stack_frame = { 0 };
974 struct btrfsic_stack_frame *sf;
975 struct btrfsic_stack_frame *next_stack;
976 struct btrfs_header *const first_hdr =
977 (struct btrfs_header *)first_block_ctx->datav[0];
980 sf = &initial_stack_frame;
983 sf->limit_nesting = first_limit_nesting;
984 sf->block = first_block;
985 sf->block_ctx = first_block_ctx;
986 sf->next_block = NULL;
990 continue_with_new_stack_frame:
991 sf->block->generation = le64_to_cpu(sf->hdr->generation);
992 if (0 == sf->hdr->level) {
993 struct btrfs_leaf *const leafhdr =
994 (struct btrfs_leaf *)sf->hdr;
997 sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
999 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1001 "leaf %llu items %d generation %llu"
1003 sf->block_ctx->start, sf->nr,
1004 btrfs_stack_header_generation(
1006 btrfs_stack_header_owner(
1010 continue_with_current_leaf_stack_frame:
1011 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1016 if (sf->i < sf->nr) {
1017 struct btrfs_item disk_item;
1018 u32 disk_item_offset =
1019 (uintptr_t)(leafhdr->items + sf->i) -
1021 struct btrfs_disk_key *disk_key;
1026 if (disk_item_offset + sizeof(struct btrfs_item) >
1027 sf->block_ctx->len) {
1028 leaf_item_out_of_bounce_error:
1030 "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
1031 sf->block_ctx->start,
1032 sf->block_ctx->dev->name);
1033 goto one_stack_frame_backwards;
1035 btrfsic_read_from_block_data(sf->block_ctx,
1038 sizeof(struct btrfs_item));
1039 item_offset = btrfs_stack_item_offset(&disk_item);
1040 item_size = btrfs_stack_item_size(&disk_item);
1041 disk_key = &disk_item.key;
1042 type = btrfs_disk_key_type(disk_key);
1044 if (BTRFS_ROOT_ITEM_KEY == type) {
1045 struct btrfs_root_item root_item;
1046 u32 root_item_offset;
1049 root_item_offset = item_offset +
1050 offsetof(struct btrfs_leaf, items);
1051 if (root_item_offset + item_size >
1053 goto leaf_item_out_of_bounce_error;
1054 btrfsic_read_from_block_data(
1055 sf->block_ctx, &root_item,
1058 next_bytenr = btrfs_root_bytenr(&root_item);
1061 btrfsic_create_link_to_next_block(
1067 &sf->next_block_ctx,
1073 btrfs_root_generation(
1076 goto one_stack_frame_backwards;
1078 if (NULL != sf->next_block) {
1079 struct btrfs_header *const next_hdr =
1080 (struct btrfs_header *)
1081 sf->next_block_ctx.datav[0];
1084 btrfsic_stack_frame_alloc();
1085 if (NULL == next_stack) {
1086 btrfsic_release_block_ctx(
1089 goto one_stack_frame_backwards;
1093 next_stack->block = sf->next_block;
1094 next_stack->block_ctx =
1095 &sf->next_block_ctx;
1096 next_stack->next_block = NULL;
1097 next_stack->hdr = next_hdr;
1098 next_stack->limit_nesting =
1099 sf->limit_nesting - 1;
1100 next_stack->prev = sf;
1102 goto continue_with_new_stack_frame;
1104 } else if (BTRFS_EXTENT_DATA_KEY == type &&
1105 state->include_extent_data) {
1106 sf->error = btrfsic_handle_extent_data(
1113 goto one_stack_frame_backwards;
1116 goto continue_with_current_leaf_stack_frame;
1119 struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1122 sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
1124 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1125 printk(KERN_INFO "node %llu level %d items %d"
1126 " generation %llu owner %llu\n",
1127 sf->block_ctx->start,
1128 nodehdr->header.level, sf->nr,
1129 btrfs_stack_header_generation(
1131 btrfs_stack_header_owner(
1135 continue_with_current_node_stack_frame:
1136 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1141 if (sf->i < sf->nr) {
1142 struct btrfs_key_ptr key_ptr;
1146 key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1148 if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1149 sf->block_ctx->len) {
1151 "btrfsic: node item out of bounce at logical %llu, dev %s\n",
1152 sf->block_ctx->start,
1153 sf->block_ctx->dev->name);
1154 goto one_stack_frame_backwards;
1156 btrfsic_read_from_block_data(
1157 sf->block_ctx, &key_ptr, key_ptr_offset,
1158 sizeof(struct btrfs_key_ptr));
1159 next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
1161 sf->error = btrfsic_create_link_to_next_block(
1167 &sf->next_block_ctx,
1173 btrfs_stack_key_generation(&key_ptr));
1175 goto one_stack_frame_backwards;
1177 if (NULL != sf->next_block) {
1178 struct btrfs_header *const next_hdr =
1179 (struct btrfs_header *)
1180 sf->next_block_ctx.datav[0];
1182 next_stack = btrfsic_stack_frame_alloc();
1183 if (NULL == next_stack)
1184 goto one_stack_frame_backwards;
1187 next_stack->block = sf->next_block;
1188 next_stack->block_ctx = &sf->next_block_ctx;
1189 next_stack->next_block = NULL;
1190 next_stack->hdr = next_hdr;
1191 next_stack->limit_nesting =
1192 sf->limit_nesting - 1;
1193 next_stack->prev = sf;
1195 goto continue_with_new_stack_frame;
1198 goto continue_with_current_node_stack_frame;
1202 one_stack_frame_backwards:
1203 if (NULL != sf->prev) {
1204 struct btrfsic_stack_frame *const prev = sf->prev;
1206 /* the one for the initial block is freed in the caller */
1207 btrfsic_release_block_ctx(sf->block_ctx);
1210 prev->error = sf->error;
1211 btrfsic_stack_frame_free(sf);
1213 goto one_stack_frame_backwards;
1216 btrfsic_stack_frame_free(sf);
1218 goto continue_with_new_stack_frame;
1220 BUG_ON(&initial_stack_frame != sf);
1226 static void btrfsic_read_from_block_data(
1227 struct btrfsic_block_data_ctx *block_ctx,
1228 void *dstv, u32 offset, size_t len)
1231 size_t offset_in_page;
1233 char *dst = (char *)dstv;
1234 size_t start_offset = block_ctx->start & ((u64)PAGE_CACHE_SIZE - 1);
1235 unsigned long i = (start_offset + offset) >> PAGE_CACHE_SHIFT;
1237 WARN_ON(offset + len > block_ctx->len);
1238 offset_in_page = (start_offset + offset) & (PAGE_CACHE_SIZE - 1);
1241 cur = min(len, ((size_t)PAGE_CACHE_SIZE - offset_in_page));
1242 BUG_ON(i >= (block_ctx->len + PAGE_CACHE_SIZE - 1) >>
1244 kaddr = block_ctx->datav[i];
1245 memcpy(dst, kaddr + offset_in_page, cur);
1254 static int btrfsic_create_link_to_next_block(
1255 struct btrfsic_state *state,
1256 struct btrfsic_block *block,
1257 struct btrfsic_block_data_ctx *block_ctx,
1260 struct btrfsic_block_data_ctx *next_block_ctx,
1261 struct btrfsic_block **next_blockp,
1262 int force_iodone_flag,
1263 int *num_copiesp, int *mirror_nump,
1264 struct btrfs_disk_key *disk_key,
1265 u64 parent_generation)
1267 struct btrfsic_block *next_block = NULL;
1269 struct btrfsic_block_link *l;
1270 int did_alloc_block_link;
1271 int block_was_created;
1273 *next_blockp = NULL;
1274 if (0 == *num_copiesp) {
1276 btrfs_num_copies(state->root->fs_info,
1277 next_bytenr, state->metablock_size);
1278 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1279 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1280 next_bytenr, *num_copiesp);
1284 if (*mirror_nump > *num_copiesp)
1287 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1289 "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1291 ret = btrfsic_map_block(state, next_bytenr,
1292 state->metablock_size,
1293 next_block_ctx, *mirror_nump);
1296 "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1297 next_bytenr, *mirror_nump);
1298 btrfsic_release_block_ctx(next_block_ctx);
1299 *next_blockp = NULL;
1303 next_block = btrfsic_block_lookup_or_add(state,
1304 next_block_ctx, "referenced ",
1305 1, force_iodone_flag,
1308 &block_was_created);
1309 if (NULL == next_block) {
1310 btrfsic_release_block_ctx(next_block_ctx);
1311 *next_blockp = NULL;
1314 if (block_was_created) {
1316 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1318 if (next_block->logical_bytenr != next_bytenr &&
1319 !(!next_block->is_metadata &&
1320 0 == next_block->logical_bytenr)) {
1322 "Referenced block @%llu (%s/%llu/%d)"
1323 " found in hash table, %c,"
1324 " bytenr mismatch (!= stored %llu).\n",
1325 next_bytenr, next_block_ctx->dev->name,
1326 next_block_ctx->dev_bytenr, *mirror_nump,
1327 btrfsic_get_block_type(state, next_block),
1328 next_block->logical_bytenr);
1329 } else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1331 "Referenced block @%llu (%s/%llu/%d)"
1332 " found in hash table, %c.\n",
1333 next_bytenr, next_block_ctx->dev->name,
1334 next_block_ctx->dev_bytenr, *mirror_nump,
1335 btrfsic_get_block_type(state, next_block));
1336 next_block->logical_bytenr = next_bytenr;
1338 next_block->mirror_num = *mirror_nump;
1339 l = btrfsic_block_link_hashtable_lookup(
1340 next_block_ctx->dev->bdev,
1341 next_block_ctx->dev_bytenr,
1342 block_ctx->dev->bdev,
1343 block_ctx->dev_bytenr,
1344 &state->block_link_hashtable);
1347 next_block->disk_key = *disk_key;
1349 l = btrfsic_block_link_alloc();
1351 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
1352 btrfsic_release_block_ctx(next_block_ctx);
1353 *next_blockp = NULL;
1357 did_alloc_block_link = 1;
1358 l->block_ref_to = next_block;
1359 l->block_ref_from = block;
1361 l->parent_generation = parent_generation;
1363 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1364 btrfsic_print_add_link(state, l);
1366 list_add(&l->node_ref_to, &block->ref_to_list);
1367 list_add(&l->node_ref_from, &next_block->ref_from_list);
1369 btrfsic_block_link_hashtable_add(l,
1370 &state->block_link_hashtable);
1372 did_alloc_block_link = 0;
1373 if (0 == limit_nesting) {
1375 l->parent_generation = parent_generation;
1376 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1377 btrfsic_print_add_link(state, l);
1381 if (limit_nesting > 0 && did_alloc_block_link) {
1382 ret = btrfsic_read_block(state, next_block_ctx);
1383 if (ret < (int)next_block_ctx->len) {
1385 "btrfsic: read block @logical %llu failed!\n",
1387 btrfsic_release_block_ctx(next_block_ctx);
1388 *next_blockp = NULL;
1392 *next_blockp = next_block;
1394 *next_blockp = NULL;
1401 static int btrfsic_handle_extent_data(
1402 struct btrfsic_state *state,
1403 struct btrfsic_block *block,
1404 struct btrfsic_block_data_ctx *block_ctx,
1405 u32 item_offset, int force_iodone_flag)
1408 struct btrfs_file_extent_item file_extent_item;
1409 u64 file_extent_item_offset;
1413 struct btrfsic_block_link *l;
1415 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1417 if (file_extent_item_offset +
1418 offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1421 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1422 block_ctx->start, block_ctx->dev->name);
1426 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1427 file_extent_item_offset,
1428 offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1429 if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
1430 btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
1431 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1432 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu\n",
1433 file_extent_item.type,
1434 btrfs_stack_file_extent_disk_bytenr(
1435 &file_extent_item));
1439 if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1442 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1443 block_ctx->start, block_ctx->dev->name);
1446 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1447 file_extent_item_offset,
1448 sizeof(struct btrfs_file_extent_item));
1449 next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item) +
1450 btrfs_stack_file_extent_offset(&file_extent_item);
1451 generation = btrfs_stack_file_extent_generation(&file_extent_item);
1452 num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
1453 generation = btrfs_stack_file_extent_generation(&file_extent_item);
1455 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1456 printk(KERN_INFO "extent_data: type %u, disk_bytenr = %llu,"
1457 " offset = %llu, num_bytes = %llu\n",
1458 file_extent_item.type,
1459 btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
1460 btrfs_stack_file_extent_offset(&file_extent_item),
1462 while (num_bytes > 0) {
1467 if (num_bytes > state->datablock_size)
1468 chunk_len = state->datablock_size;
1470 chunk_len = num_bytes;
1473 btrfs_num_copies(state->root->fs_info,
1474 next_bytenr, state->datablock_size);
1475 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1476 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
1477 next_bytenr, num_copies);
1478 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1479 struct btrfsic_block_data_ctx next_block_ctx;
1480 struct btrfsic_block *next_block;
1481 int block_was_created;
1483 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1484 printk(KERN_INFO "btrfsic_handle_extent_data("
1485 "mirror_num=%d)\n", mirror_num);
1486 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1488 "\tdisk_bytenr = %llu, num_bytes %u\n",
1489 next_bytenr, chunk_len);
1490 ret = btrfsic_map_block(state, next_bytenr,
1491 chunk_len, &next_block_ctx,
1495 "btrfsic: btrfsic_map_block(@%llu,"
1496 " mirror=%d) failed!\n",
1497 next_bytenr, mirror_num);
1501 next_block = btrfsic_block_lookup_or_add(
1509 &block_was_created);
1510 if (NULL == next_block) {
1512 "btrfsic: error, kmalloc failed!\n");
1513 btrfsic_release_block_ctx(&next_block_ctx);
1516 if (!block_was_created) {
1517 if (next_block->logical_bytenr != next_bytenr &&
1518 !(!next_block->is_metadata &&
1519 0 == next_block->logical_bytenr)) {
1522 " @%llu (%s/%llu/%d)"
1523 " found in hash table, D,"
1525 " (!= stored %llu).\n",
1527 next_block_ctx.dev->name,
1528 next_block_ctx.dev_bytenr,
1530 next_block->logical_bytenr);
1532 next_block->logical_bytenr = next_bytenr;
1533 next_block->mirror_num = mirror_num;
1536 l = btrfsic_block_link_lookup_or_add(state,
1540 btrfsic_release_block_ctx(&next_block_ctx);
1545 next_bytenr += chunk_len;
1546 num_bytes -= chunk_len;
1552 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1553 struct btrfsic_block_data_ctx *block_ctx_out,
1558 struct btrfs_bio *multi = NULL;
1559 struct btrfs_device *device;
1562 ret = btrfs_map_block(state->root->fs_info, READ,
1563 bytenr, &length, &multi, mirror_num);
1566 block_ctx_out->start = 0;
1567 block_ctx_out->dev_bytenr = 0;
1568 block_ctx_out->len = 0;
1569 block_ctx_out->dev = NULL;
1570 block_ctx_out->datav = NULL;
1571 block_ctx_out->pagev = NULL;
1572 block_ctx_out->mem_to_free = NULL;
1577 device = multi->stripes[0].dev;
1578 block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev);
1579 block_ctx_out->dev_bytenr = multi->stripes[0].physical;
1580 block_ctx_out->start = bytenr;
1581 block_ctx_out->len = len;
1582 block_ctx_out->datav = NULL;
1583 block_ctx_out->pagev = NULL;
1584 block_ctx_out->mem_to_free = NULL;
1587 if (NULL == block_ctx_out->dev) {
1589 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#1)!\n");
1595 static int btrfsic_map_superblock(struct btrfsic_state *state, u64 bytenr,
1596 u32 len, struct block_device *bdev,
1597 struct btrfsic_block_data_ctx *block_ctx_out)
1599 block_ctx_out->dev = btrfsic_dev_state_lookup(bdev);
1600 block_ctx_out->dev_bytenr = bytenr;
1601 block_ctx_out->start = bytenr;
1602 block_ctx_out->len = len;
1603 block_ctx_out->datav = NULL;
1604 block_ctx_out->pagev = NULL;
1605 block_ctx_out->mem_to_free = NULL;
1606 if (NULL != block_ctx_out->dev) {
1609 printk(KERN_INFO "btrfsic: error, cannot lookup dev (#2)!\n");
1614 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1616 if (block_ctx->mem_to_free) {
1617 unsigned int num_pages;
1619 BUG_ON(!block_ctx->datav);
1620 BUG_ON(!block_ctx->pagev);
1621 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1623 while (num_pages > 0) {
1625 if (block_ctx->datav[num_pages]) {
1626 kunmap(block_ctx->pagev[num_pages]);
1627 block_ctx->datav[num_pages] = NULL;
1629 if (block_ctx->pagev[num_pages]) {
1630 __free_page(block_ctx->pagev[num_pages]);
1631 block_ctx->pagev[num_pages] = NULL;
1635 kfree(block_ctx->mem_to_free);
1636 block_ctx->mem_to_free = NULL;
1637 block_ctx->pagev = NULL;
1638 block_ctx->datav = NULL;
1642 static int btrfsic_read_block(struct btrfsic_state *state,
1643 struct btrfsic_block_data_ctx *block_ctx)
1645 unsigned int num_pages;
1650 BUG_ON(block_ctx->datav);
1651 BUG_ON(block_ctx->pagev);
1652 BUG_ON(block_ctx->mem_to_free);
1653 if (block_ctx->dev_bytenr & ((u64)PAGE_CACHE_SIZE - 1)) {
1655 "btrfsic: read_block() with unaligned bytenr %llu\n",
1656 block_ctx->dev_bytenr);
1660 num_pages = (block_ctx->len + (u64)PAGE_CACHE_SIZE - 1) >>
1662 block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
1663 sizeof(*block_ctx->pagev)) *
1664 num_pages, GFP_NOFS);
1665 if (!block_ctx->mem_to_free)
1667 block_ctx->datav = block_ctx->mem_to_free;
1668 block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1669 for (i = 0; i < num_pages; i++) {
1670 block_ctx->pagev[i] = alloc_page(GFP_NOFS);
1671 if (!block_ctx->pagev[i])
1675 dev_bytenr = block_ctx->dev_bytenr;
1676 for (i = 0; i < num_pages;) {
1680 bio = btrfs_io_bio_alloc(GFP_NOFS, num_pages - i);
1683 "btrfsic: bio_alloc() for %u pages failed!\n",
1687 bio->bi_bdev = block_ctx->dev->bdev;
1688 bio->bi_sector = dev_bytenr >> 9;
1690 for (j = i; j < num_pages; j++) {
1691 ret = bio_add_page(bio, block_ctx->pagev[j],
1692 PAGE_CACHE_SIZE, 0);
1693 if (PAGE_CACHE_SIZE != ret)
1698 "btrfsic: error, failed to add a single page!\n");
1701 if (submit_bio_wait(READ, bio)) {
1703 "btrfsic: read error at logical %llu dev %s!\n",
1704 block_ctx->start, block_ctx->dev->name);
1709 dev_bytenr += (j - i) * PAGE_CACHE_SIZE;
1712 for (i = 0; i < num_pages; i++) {
1713 block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
1714 if (!block_ctx->datav[i]) {
1715 printk(KERN_INFO "btrfsic: kmap() failed (dev %s)!\n",
1716 block_ctx->dev->name);
1721 return block_ctx->len;
1724 static void btrfsic_dump_database(struct btrfsic_state *state)
1726 struct list_head *elem_all;
1728 BUG_ON(NULL == state);
1730 printk(KERN_INFO "all_blocks_list:\n");
1731 list_for_each(elem_all, &state->all_blocks_list) {
1732 const struct btrfsic_block *const b_all =
1733 list_entry(elem_all, struct btrfsic_block,
1735 struct list_head *elem_ref_to;
1736 struct list_head *elem_ref_from;
1738 printk(KERN_INFO "%c-block @%llu (%s/%llu/%d)\n",
1739 btrfsic_get_block_type(state, b_all),
1740 b_all->logical_bytenr, b_all->dev_state->name,
1741 b_all->dev_bytenr, b_all->mirror_num);
1743 list_for_each(elem_ref_to, &b_all->ref_to_list) {
1744 const struct btrfsic_block_link *const l =
1745 list_entry(elem_ref_to,
1746 struct btrfsic_block_link,
1749 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1751 " %c @%llu (%s/%llu/%d)\n",
1752 btrfsic_get_block_type(state, b_all),
1753 b_all->logical_bytenr, b_all->dev_state->name,
1754 b_all->dev_bytenr, b_all->mirror_num,
1756 btrfsic_get_block_type(state, l->block_ref_to),
1757 l->block_ref_to->logical_bytenr,
1758 l->block_ref_to->dev_state->name,
1759 l->block_ref_to->dev_bytenr,
1760 l->block_ref_to->mirror_num);
1763 list_for_each(elem_ref_from, &b_all->ref_from_list) {
1764 const struct btrfsic_block_link *const l =
1765 list_entry(elem_ref_from,
1766 struct btrfsic_block_link,
1769 printk(KERN_INFO " %c @%llu (%s/%llu/%d)"
1771 " %c @%llu (%s/%llu/%d)\n",
1772 btrfsic_get_block_type(state, b_all),
1773 b_all->logical_bytenr, b_all->dev_state->name,
1774 b_all->dev_bytenr, b_all->mirror_num,
1776 btrfsic_get_block_type(state, l->block_ref_from),
1777 l->block_ref_from->logical_bytenr,
1778 l->block_ref_from->dev_state->name,
1779 l->block_ref_from->dev_bytenr,
1780 l->block_ref_from->mirror_num);
1783 printk(KERN_INFO "\n");
1788 * Test whether the disk block contains a tree block (leaf or node)
1789 * (note that this test fails for the super block)
1791 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
1792 char **datav, unsigned int num_pages)
1794 struct btrfs_header *h;
1795 u8 csum[BTRFS_CSUM_SIZE];
1799 if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
1800 return 1; /* not metadata */
1801 num_pages = state->metablock_size >> PAGE_CACHE_SHIFT;
1802 h = (struct btrfs_header *)datav[0];
1804 if (memcmp(h->fsid, state->root->fs_info->fsid, BTRFS_UUID_SIZE))
1807 for (i = 0; i < num_pages; i++) {
1808 u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
1809 size_t sublen = i ? PAGE_CACHE_SIZE :
1810 (PAGE_CACHE_SIZE - BTRFS_CSUM_SIZE);
1812 crc = crc32c(crc, data, sublen);
1814 btrfs_csum_final(crc, csum);
1815 if (memcmp(csum, h->csum, state->csum_size))
1818 return 0; /* is metadata */
1821 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
1822 u64 dev_bytenr, char **mapped_datav,
1823 unsigned int num_pages,
1824 struct bio *bio, int *bio_is_patched,
1825 struct buffer_head *bh,
1826 int submit_bio_bh_rw)
1829 struct btrfsic_block *block;
1830 struct btrfsic_block_data_ctx block_ctx;
1832 struct btrfsic_state *state = dev_state->state;
1833 struct block_device *bdev = dev_state->bdev;
1834 unsigned int processed_len;
1836 if (NULL != bio_is_patched)
1837 *bio_is_patched = 0;
1844 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1847 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1848 &state->block_hashtable);
1849 if (NULL != block) {
1851 struct list_head *elem_ref_to;
1852 struct list_head *tmp_ref_to;
1854 if (block->is_superblock) {
1855 bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
1857 if (num_pages * PAGE_CACHE_SIZE <
1858 BTRFS_SUPER_INFO_SIZE) {
1860 "btrfsic: cannot work with too short bios!\n");
1864 BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_CACHE_SIZE - 1));
1865 processed_len = BTRFS_SUPER_INFO_SIZE;
1866 if (state->print_mask &
1867 BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
1869 "[before new superblock is written]:\n");
1870 btrfsic_dump_tree_sub(state, block, 0);
1874 if (!block->is_superblock) {
1875 if (num_pages * PAGE_CACHE_SIZE <
1876 state->metablock_size) {
1878 "btrfsic: cannot work with too short bios!\n");
1881 processed_len = state->metablock_size;
1882 bytenr = btrfs_stack_header_bytenr(
1883 (struct btrfs_header *)
1885 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1889 if (block->logical_bytenr != bytenr &&
1890 !(!block->is_metadata &&
1891 block->logical_bytenr == 0))
1893 "Written block @%llu (%s/%llu/%d)"
1894 " found in hash table, %c,"
1896 " (!= stored %llu).\n",
1897 bytenr, dev_state->name, dev_bytenr,
1899 btrfsic_get_block_type(state, block),
1900 block->logical_bytenr);
1901 else if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1903 "Written block @%llu (%s/%llu/%d)"
1904 " found in hash table, %c.\n",
1905 bytenr, dev_state->name, dev_bytenr,
1907 btrfsic_get_block_type(state, block));
1908 block->logical_bytenr = bytenr;
1910 if (num_pages * PAGE_CACHE_SIZE <
1911 state->datablock_size) {
1913 "btrfsic: cannot work with too short bios!\n");
1916 processed_len = state->datablock_size;
1917 bytenr = block->logical_bytenr;
1918 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1920 "Written block @%llu (%s/%llu/%d)"
1921 " found in hash table, %c.\n",
1922 bytenr, dev_state->name, dev_bytenr,
1924 btrfsic_get_block_type(state, block));
1927 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1929 "ref_to_list: %cE, ref_from_list: %cE\n",
1930 list_empty(&block->ref_to_list) ? ' ' : '!',
1931 list_empty(&block->ref_from_list) ? ' ' : '!');
1932 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1933 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1934 " @%llu (%s/%llu/%d), old(gen=%llu,"
1935 " objectid=%llu, type=%d, offset=%llu),"
1937 " which is referenced by most recent superblock"
1938 " (superblockgen=%llu)!\n",
1939 btrfsic_get_block_type(state, block), bytenr,
1940 dev_state->name, dev_bytenr, block->mirror_num,
1942 btrfs_disk_key_objectid(&block->disk_key),
1943 block->disk_key.type,
1944 btrfs_disk_key_offset(&block->disk_key),
1945 btrfs_stack_header_generation(
1946 (struct btrfs_header *) mapped_datav[0]),
1947 state->max_superblock_generation);
1948 btrfsic_dump_tree(state);
1951 if (!block->is_iodone && !block->never_written) {
1952 printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
1953 " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
1954 " which is not yet iodone!\n",
1955 btrfsic_get_block_type(state, block), bytenr,
1956 dev_state->name, dev_bytenr, block->mirror_num,
1958 btrfs_stack_header_generation(
1959 (struct btrfs_header *)
1961 /* it would not be safe to go on */
1962 btrfsic_dump_tree(state);
1967 * Clear all references of this block. Do not free
1968 * the block itself even if is not referenced anymore
1969 * because it still carries valueable information
1970 * like whether it was ever written and IO completed.
1972 list_for_each_safe(elem_ref_to, tmp_ref_to,
1973 &block->ref_to_list) {
1974 struct btrfsic_block_link *const l =
1975 list_entry(elem_ref_to,
1976 struct btrfsic_block_link,
1979 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1980 btrfsic_print_rem_link(state, l);
1982 if (0 == l->ref_cnt) {
1983 list_del(&l->node_ref_to);
1984 list_del(&l->node_ref_from);
1985 btrfsic_block_link_hashtable_remove(l);
1986 btrfsic_block_link_free(l);
1990 if (block->is_superblock)
1991 ret = btrfsic_map_superblock(state, bytenr,
1995 ret = btrfsic_map_block(state, bytenr, processed_len,
1999 "btrfsic: btrfsic_map_block(root @%llu)"
2000 " failed!\n", bytenr);
2003 block_ctx.datav = mapped_datav;
2004 /* the following is required in case of writes to mirrors,
2005 * use the same that was used for the lookup */
2006 block_ctx.dev = dev_state;
2007 block_ctx.dev_bytenr = dev_bytenr;
2009 if (is_metadata || state->include_extent_data) {
2010 block->never_written = 0;
2011 block->iodone_w_error = 0;
2013 block->is_iodone = 0;
2014 BUG_ON(NULL == bio_is_patched);
2015 if (!*bio_is_patched) {
2016 block->orig_bio_bh_private =
2018 block->orig_bio_bh_end_io.bio =
2020 block->next_in_same_bio = NULL;
2021 bio->bi_private = block;
2022 bio->bi_end_io = btrfsic_bio_end_io;
2023 *bio_is_patched = 1;
2025 struct btrfsic_block *chained_block =
2026 (struct btrfsic_block *)
2029 BUG_ON(NULL == chained_block);
2030 block->orig_bio_bh_private =
2031 chained_block->orig_bio_bh_private;
2032 block->orig_bio_bh_end_io.bio =
2033 chained_block->orig_bio_bh_end_io.
2035 block->next_in_same_bio = chained_block;
2036 bio->bi_private = block;
2038 } else if (NULL != bh) {
2039 block->is_iodone = 0;
2040 block->orig_bio_bh_private = bh->b_private;
2041 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2042 block->next_in_same_bio = NULL;
2043 bh->b_private = block;
2044 bh->b_end_io = btrfsic_bh_end_io;
2046 block->is_iodone = 1;
2047 block->orig_bio_bh_private = NULL;
2048 block->orig_bio_bh_end_io.bio = NULL;
2049 block->next_in_same_bio = NULL;
2053 block->flush_gen = dev_state->last_flush_gen + 1;
2054 block->submit_bio_bh_rw = submit_bio_bh_rw;
2056 block->logical_bytenr = bytenr;
2057 block->is_metadata = 1;
2058 if (block->is_superblock) {
2059 BUG_ON(PAGE_CACHE_SIZE !=
2060 BTRFS_SUPER_INFO_SIZE);
2061 ret = btrfsic_process_written_superblock(
2064 (struct btrfs_super_block *)
2066 if (state->print_mask &
2067 BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
2069 "[after new superblock is written]:\n");
2070 btrfsic_dump_tree_sub(state, block, 0);
2073 block->mirror_num = 0; /* unknown */
2074 ret = btrfsic_process_metablock(
2082 "btrfsic: btrfsic_process_metablock"
2083 "(root @%llu) failed!\n",
2086 block->is_metadata = 0;
2087 block->mirror_num = 0; /* unknown */
2088 block->generation = BTRFSIC_GENERATION_UNKNOWN;
2089 if (!state->include_extent_data
2090 && list_empty(&block->ref_from_list)) {
2092 * disk block is overwritten with extent
2093 * data (not meta data) and we are configured
2094 * to not include extent data: take the
2095 * chance and free the block's memory
2097 btrfsic_block_hashtable_remove(block);
2098 list_del(&block->all_blocks_node);
2099 btrfsic_block_free(block);
2102 btrfsic_release_block_ctx(&block_ctx);
2104 /* block has not been found in hash table */
2108 processed_len = state->datablock_size;
2109 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2110 printk(KERN_INFO "Written block (%s/%llu/?)"
2111 " !found in hash table, D.\n",
2112 dev_state->name, dev_bytenr);
2113 if (!state->include_extent_data) {
2114 /* ignore that written D block */
2118 /* this is getting ugly for the
2119 * include_extent_data case... */
2120 bytenr = 0; /* unknown */
2121 block_ctx.start = bytenr;
2122 block_ctx.len = processed_len;
2123 block_ctx.mem_to_free = NULL;
2124 block_ctx.pagev = NULL;
2126 processed_len = state->metablock_size;
2127 bytenr = btrfs_stack_header_bytenr(
2128 (struct btrfs_header *)
2130 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
2132 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2134 "Written block @%llu (%s/%llu/?)"
2135 " !found in hash table, M.\n",
2136 bytenr, dev_state->name, dev_bytenr);
2138 ret = btrfsic_map_block(state, bytenr, processed_len,
2142 "btrfsic: btrfsic_map_block(root @%llu)"
2148 block_ctx.datav = mapped_datav;
2149 /* the following is required in case of writes to mirrors,
2150 * use the same that was used for the lookup */
2151 block_ctx.dev = dev_state;
2152 block_ctx.dev_bytenr = dev_bytenr;
2154 block = btrfsic_block_alloc();
2155 if (NULL == block) {
2156 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2157 btrfsic_release_block_ctx(&block_ctx);
2160 block->dev_state = dev_state;
2161 block->dev_bytenr = dev_bytenr;
2162 block->logical_bytenr = bytenr;
2163 block->is_metadata = is_metadata;
2164 block->never_written = 0;
2165 block->iodone_w_error = 0;
2166 block->mirror_num = 0; /* unknown */
2167 block->flush_gen = dev_state->last_flush_gen + 1;
2168 block->submit_bio_bh_rw = submit_bio_bh_rw;
2170 block->is_iodone = 0;
2171 BUG_ON(NULL == bio_is_patched);
2172 if (!*bio_is_patched) {
2173 block->orig_bio_bh_private = bio->bi_private;
2174 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2175 block->next_in_same_bio = NULL;
2176 bio->bi_private = block;
2177 bio->bi_end_io = btrfsic_bio_end_io;
2178 *bio_is_patched = 1;
2180 struct btrfsic_block *chained_block =
2181 (struct btrfsic_block *)
2184 BUG_ON(NULL == chained_block);
2185 block->orig_bio_bh_private =
2186 chained_block->orig_bio_bh_private;
2187 block->orig_bio_bh_end_io.bio =
2188 chained_block->orig_bio_bh_end_io.bio;
2189 block->next_in_same_bio = chained_block;
2190 bio->bi_private = block;
2192 } else if (NULL != bh) {
2193 block->is_iodone = 0;
2194 block->orig_bio_bh_private = bh->b_private;
2195 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2196 block->next_in_same_bio = NULL;
2197 bh->b_private = block;
2198 bh->b_end_io = btrfsic_bh_end_io;
2200 block->is_iodone = 1;
2201 block->orig_bio_bh_private = NULL;
2202 block->orig_bio_bh_end_io.bio = NULL;
2203 block->next_in_same_bio = NULL;
2205 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2207 "New written %c-block @%llu (%s/%llu/%d)\n",
2208 is_metadata ? 'M' : 'D',
2209 block->logical_bytenr, block->dev_state->name,
2210 block->dev_bytenr, block->mirror_num);
2211 list_add(&block->all_blocks_node, &state->all_blocks_list);
2212 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2215 ret = btrfsic_process_metablock(state, block,
2219 "btrfsic: process_metablock(root @%llu)"
2223 btrfsic_release_block_ctx(&block_ctx);
2227 BUG_ON(!processed_len);
2228 dev_bytenr += processed_len;
2229 mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
2230 num_pages -= processed_len >> PAGE_CACHE_SHIFT;
2234 static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status)
2236 struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
2239 /* mutex is not held! This is not save if IO is not yet completed
2242 if (bio_error_status)
2245 BUG_ON(NULL == block);
2246 bp->bi_private = block->orig_bio_bh_private;
2247 bp->bi_end_io = block->orig_bio_bh_end_io.bio;
2250 struct btrfsic_block *next_block;
2251 struct btrfsic_dev_state *const dev_state = block->dev_state;
2253 if ((dev_state->state->print_mask &
2254 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2256 "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
2258 btrfsic_get_block_type(dev_state->state, block),
2259 block->logical_bytenr, dev_state->name,
2260 block->dev_bytenr, block->mirror_num);
2261 next_block = block->next_in_same_bio;
2262 block->iodone_w_error = iodone_w_error;
2263 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2264 dev_state->last_flush_gen++;
2265 if ((dev_state->state->print_mask &
2266 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2268 "bio_end_io() new %s flush_gen=%llu\n",
2270 dev_state->last_flush_gen);
2272 if (block->submit_bio_bh_rw & REQ_FUA)
2273 block->flush_gen = 0; /* FUA completed means block is
2275 block->is_iodone = 1; /* for FLUSH, this releases the block */
2277 } while (NULL != block);
2279 bp->bi_end_io(bp, bio_error_status);
2282 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
2284 struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
2285 int iodone_w_error = !uptodate;
2286 struct btrfsic_dev_state *dev_state;
2288 BUG_ON(NULL == block);
2289 dev_state = block->dev_state;
2290 if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2292 "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
2294 btrfsic_get_block_type(dev_state->state, block),
2295 block->logical_bytenr, block->dev_state->name,
2296 block->dev_bytenr, block->mirror_num);
2298 block->iodone_w_error = iodone_w_error;
2299 if (block->submit_bio_bh_rw & REQ_FLUSH) {
2300 dev_state->last_flush_gen++;
2301 if ((dev_state->state->print_mask &
2302 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2304 "bh_end_io() new %s flush_gen=%llu\n",
2305 dev_state->name, dev_state->last_flush_gen);
2307 if (block->submit_bio_bh_rw & REQ_FUA)
2308 block->flush_gen = 0; /* FUA completed means block is on disk */
2310 bh->b_private = block->orig_bio_bh_private;
2311 bh->b_end_io = block->orig_bio_bh_end_io.bh;
2312 block->is_iodone = 1; /* for FLUSH, this releases the block */
2313 bh->b_end_io(bh, uptodate);
2316 static int btrfsic_process_written_superblock(
2317 struct btrfsic_state *state,
2318 struct btrfsic_block *const superblock,
2319 struct btrfs_super_block *const super_hdr)
2323 superblock->generation = btrfs_super_generation(super_hdr);
2324 if (!(superblock->generation > state->max_superblock_generation ||
2325 0 == state->max_superblock_generation)) {
2326 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2328 "btrfsic: superblock @%llu (%s/%llu/%d)"
2329 " with old gen %llu <= %llu\n",
2330 superblock->logical_bytenr,
2331 superblock->dev_state->name,
2332 superblock->dev_bytenr, superblock->mirror_num,
2333 btrfs_super_generation(super_hdr),
2334 state->max_superblock_generation);
2336 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2338 "btrfsic: got new superblock @%llu (%s/%llu/%d)"
2339 " with new gen %llu > %llu\n",
2340 superblock->logical_bytenr,
2341 superblock->dev_state->name,
2342 superblock->dev_bytenr, superblock->mirror_num,
2343 btrfs_super_generation(super_hdr),
2344 state->max_superblock_generation);
2346 state->max_superblock_generation =
2347 btrfs_super_generation(super_hdr);
2348 state->latest_superblock = superblock;
2351 for (pass = 0; pass < 3; pass++) {
2354 struct btrfsic_block *next_block;
2355 struct btrfsic_block_data_ctx tmp_next_block_ctx;
2356 struct btrfsic_block_link *l;
2359 const char *additional_string = NULL;
2360 struct btrfs_disk_key tmp_disk_key = {0};
2362 btrfs_set_disk_key_objectid(&tmp_disk_key,
2363 BTRFS_ROOT_ITEM_KEY);
2364 btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
2368 btrfs_set_disk_key_objectid(&tmp_disk_key,
2369 BTRFS_ROOT_TREE_OBJECTID);
2370 additional_string = "root ";
2371 next_bytenr = btrfs_super_root(super_hdr);
2372 if (state->print_mask &
2373 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2374 printk(KERN_INFO "root@%llu\n", next_bytenr);
2377 btrfs_set_disk_key_objectid(&tmp_disk_key,
2378 BTRFS_CHUNK_TREE_OBJECTID);
2379 additional_string = "chunk ";
2380 next_bytenr = btrfs_super_chunk_root(super_hdr);
2381 if (state->print_mask &
2382 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2383 printk(KERN_INFO "chunk@%llu\n", next_bytenr);
2386 btrfs_set_disk_key_objectid(&tmp_disk_key,
2387 BTRFS_TREE_LOG_OBJECTID);
2388 additional_string = "log ";
2389 next_bytenr = btrfs_super_log_root(super_hdr);
2390 if (0 == next_bytenr)
2392 if (state->print_mask &
2393 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2394 printk(KERN_INFO "log@%llu\n", next_bytenr);
2399 btrfs_num_copies(state->root->fs_info,
2400 next_bytenr, BTRFS_SUPER_INFO_SIZE);
2401 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2402 printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
2403 next_bytenr, num_copies);
2404 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2407 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2409 "btrfsic_process_written_superblock("
2410 "mirror_num=%d)\n", mirror_num);
2411 ret = btrfsic_map_block(state, next_bytenr,
2412 BTRFS_SUPER_INFO_SIZE,
2413 &tmp_next_block_ctx,
2417 "btrfsic: btrfsic_map_block(@%llu,"
2418 " mirror=%d) failed!\n",
2419 next_bytenr, mirror_num);
2423 next_block = btrfsic_block_lookup_or_add(
2425 &tmp_next_block_ctx,
2430 if (NULL == next_block) {
2432 "btrfsic: error, kmalloc failed!\n");
2433 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2437 next_block->disk_key = tmp_disk_key;
2439 next_block->generation =
2440 BTRFSIC_GENERATION_UNKNOWN;
2441 l = btrfsic_block_link_lookup_or_add(
2443 &tmp_next_block_ctx,
2446 BTRFSIC_GENERATION_UNKNOWN);
2447 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2453 if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
2454 btrfsic_dump_tree(state);
2459 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2460 struct btrfsic_block *const block,
2461 int recursion_level)
2463 struct list_head *elem_ref_to;
2466 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2468 * Note that this situation can happen and does not
2469 * indicate an error in regular cases. It happens
2470 * when disk blocks are freed and later reused.
2471 * The check-integrity module is not aware of any
2472 * block free operations, it just recognizes block
2473 * write operations. Therefore it keeps the linkage
2474 * information for a block until a block is
2475 * rewritten. This can temporarily cause incorrect
2476 * and even circular linkage informations. This
2477 * causes no harm unless such blocks are referenced
2478 * by the most recent super block.
2480 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2482 "btrfsic: abort cyclic linkage (case 1).\n");
2488 * This algorithm is recursive because the amount of used stack
2489 * space is very small and the max recursion depth is limited.
2491 list_for_each(elem_ref_to, &block->ref_to_list) {
2492 const struct btrfsic_block_link *const l =
2493 list_entry(elem_ref_to, struct btrfsic_block_link,
2496 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2498 "rl=%d, %c @%llu (%s/%llu/%d)"
2499 " %u* refers to %c @%llu (%s/%llu/%d)\n",
2501 btrfsic_get_block_type(state, block),
2502 block->logical_bytenr, block->dev_state->name,
2503 block->dev_bytenr, block->mirror_num,
2505 btrfsic_get_block_type(state, l->block_ref_to),
2506 l->block_ref_to->logical_bytenr,
2507 l->block_ref_to->dev_state->name,
2508 l->block_ref_to->dev_bytenr,
2509 l->block_ref_to->mirror_num);
2510 if (l->block_ref_to->never_written) {
2511 printk(KERN_INFO "btrfs: attempt to write superblock"
2512 " which references block %c @%llu (%s/%llu/%d)"
2513 " which is never written!\n",
2514 btrfsic_get_block_type(state, l->block_ref_to),
2515 l->block_ref_to->logical_bytenr,
2516 l->block_ref_to->dev_state->name,
2517 l->block_ref_to->dev_bytenr,
2518 l->block_ref_to->mirror_num);
2520 } else if (!l->block_ref_to->is_iodone) {
2521 printk(KERN_INFO "btrfs: attempt to write superblock"
2522 " which references block %c @%llu (%s/%llu/%d)"
2523 " which is not yet iodone!\n",
2524 btrfsic_get_block_type(state, l->block_ref_to),
2525 l->block_ref_to->logical_bytenr,
2526 l->block_ref_to->dev_state->name,
2527 l->block_ref_to->dev_bytenr,
2528 l->block_ref_to->mirror_num);
2530 } else if (l->block_ref_to->iodone_w_error) {
2531 printk(KERN_INFO "btrfs: attempt to write superblock"
2532 " which references block %c @%llu (%s/%llu/%d)"
2533 " which has write error!\n",
2534 btrfsic_get_block_type(state, l->block_ref_to),
2535 l->block_ref_to->logical_bytenr,
2536 l->block_ref_to->dev_state->name,
2537 l->block_ref_to->dev_bytenr,
2538 l->block_ref_to->mirror_num);
2540 } else if (l->parent_generation !=
2541 l->block_ref_to->generation &&
2542 BTRFSIC_GENERATION_UNKNOWN !=
2543 l->parent_generation &&
2544 BTRFSIC_GENERATION_UNKNOWN !=
2545 l->block_ref_to->generation) {
2546 printk(KERN_INFO "btrfs: attempt to write superblock"
2547 " which references block %c @%llu (%s/%llu/%d)"
2548 " with generation %llu !="
2549 " parent generation %llu!\n",
2550 btrfsic_get_block_type(state, l->block_ref_to),
2551 l->block_ref_to->logical_bytenr,
2552 l->block_ref_to->dev_state->name,
2553 l->block_ref_to->dev_bytenr,
2554 l->block_ref_to->mirror_num,
2555 l->block_ref_to->generation,
2556 l->parent_generation);
2558 } else if (l->block_ref_to->flush_gen >
2559 l->block_ref_to->dev_state->last_flush_gen) {
2560 printk(KERN_INFO "btrfs: attempt to write superblock"
2561 " which references block %c @%llu (%s/%llu/%d)"
2562 " which is not flushed out of disk's write cache"
2563 " (block flush_gen=%llu,"
2564 " dev->flush_gen=%llu)!\n",
2565 btrfsic_get_block_type(state, l->block_ref_to),
2566 l->block_ref_to->logical_bytenr,
2567 l->block_ref_to->dev_state->name,
2568 l->block_ref_to->dev_bytenr,
2569 l->block_ref_to->mirror_num, block->flush_gen,
2570 l->block_ref_to->dev_state->last_flush_gen);
2572 } else if (-1 == btrfsic_check_all_ref_blocks(state,
2583 static int btrfsic_is_block_ref_by_superblock(
2584 const struct btrfsic_state *state,
2585 const struct btrfsic_block *block,
2586 int recursion_level)
2588 struct list_head *elem_ref_from;
2590 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2591 /* refer to comment at "abort cyclic linkage (case 1)" */
2592 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2594 "btrfsic: abort cyclic linkage (case 2).\n");
2600 * This algorithm is recursive because the amount of used stack space
2601 * is very small and the max recursion depth is limited.
2603 list_for_each(elem_ref_from, &block->ref_from_list) {
2604 const struct btrfsic_block_link *const l =
2605 list_entry(elem_ref_from, struct btrfsic_block_link,
2608 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2610 "rl=%d, %c @%llu (%s/%llu/%d)"
2611 " is ref %u* from %c @%llu (%s/%llu/%d)\n",
2613 btrfsic_get_block_type(state, block),
2614 block->logical_bytenr, block->dev_state->name,
2615 block->dev_bytenr, block->mirror_num,
2617 btrfsic_get_block_type(state, l->block_ref_from),
2618 l->block_ref_from->logical_bytenr,
2619 l->block_ref_from->dev_state->name,
2620 l->block_ref_from->dev_bytenr,
2621 l->block_ref_from->mirror_num);
2622 if (l->block_ref_from->is_superblock &&
2623 state->latest_superblock->dev_bytenr ==
2624 l->block_ref_from->dev_bytenr &&
2625 state->latest_superblock->dev_state->bdev ==
2626 l->block_ref_from->dev_state->bdev)
2628 else if (btrfsic_is_block_ref_by_superblock(state,
2638 static void btrfsic_print_add_link(const struct btrfsic_state *state,
2639 const struct btrfsic_block_link *l)
2642 "Add %u* link from %c @%llu (%s/%llu/%d)"
2643 " to %c @%llu (%s/%llu/%d).\n",
2645 btrfsic_get_block_type(state, l->block_ref_from),
2646 l->block_ref_from->logical_bytenr,
2647 l->block_ref_from->dev_state->name,
2648 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2649 btrfsic_get_block_type(state, l->block_ref_to),
2650 l->block_ref_to->logical_bytenr,
2651 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2652 l->block_ref_to->mirror_num);
2655 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2656 const struct btrfsic_block_link *l)
2659 "Rem %u* link from %c @%llu (%s/%llu/%d)"
2660 " to %c @%llu (%s/%llu/%d).\n",
2662 btrfsic_get_block_type(state, l->block_ref_from),
2663 l->block_ref_from->logical_bytenr,
2664 l->block_ref_from->dev_state->name,
2665 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2666 btrfsic_get_block_type(state, l->block_ref_to),
2667 l->block_ref_to->logical_bytenr,
2668 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2669 l->block_ref_to->mirror_num);
2672 static char btrfsic_get_block_type(const struct btrfsic_state *state,
2673 const struct btrfsic_block *block)
2675 if (block->is_superblock &&
2676 state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2677 state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2679 else if (block->is_superblock)
2681 else if (block->is_metadata)
2687 static void btrfsic_dump_tree(const struct btrfsic_state *state)
2689 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2692 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2693 const struct btrfsic_block *block,
2696 struct list_head *elem_ref_to;
2698 static char buf[80];
2699 int cursor_position;
2702 * Should better fill an on-stack buffer with a complete line and
2703 * dump it at once when it is time to print a newline character.
2707 * This algorithm is recursive because the amount of used stack space
2708 * is very small and the max recursion depth is limited.
2710 indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
2711 btrfsic_get_block_type(state, block),
2712 block->logical_bytenr, block->dev_state->name,
2713 block->dev_bytenr, block->mirror_num);
2714 if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2719 indent_level += indent_add;
2720 if (list_empty(&block->ref_to_list)) {
2724 if (block->mirror_num > 1 &&
2725 !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2730 cursor_position = indent_level;
2731 list_for_each(elem_ref_to, &block->ref_to_list) {
2732 const struct btrfsic_block_link *const l =
2733 list_entry(elem_ref_to, struct btrfsic_block_link,
2736 while (cursor_position < indent_level) {
2741 indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2743 indent_add = sprintf(buf, " --> ");
2744 if (indent_level + indent_add >
2745 BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2747 cursor_position = 0;
2753 btrfsic_dump_tree_sub(state, l->block_ref_to,
2754 indent_level + indent_add);
2755 cursor_position = 0;
2759 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2760 struct btrfsic_state *state,
2761 struct btrfsic_block_data_ctx *next_block_ctx,
2762 struct btrfsic_block *next_block,
2763 struct btrfsic_block *from_block,
2764 u64 parent_generation)
2766 struct btrfsic_block_link *l;
2768 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2769 next_block_ctx->dev_bytenr,
2770 from_block->dev_state->bdev,
2771 from_block->dev_bytenr,
2772 &state->block_link_hashtable);
2774 l = btrfsic_block_link_alloc();
2777 "btrfsic: error, kmalloc" " failed!\n");
2781 l->block_ref_to = next_block;
2782 l->block_ref_from = from_block;
2784 l->parent_generation = parent_generation;
2786 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2787 btrfsic_print_add_link(state, l);
2789 list_add(&l->node_ref_to, &from_block->ref_to_list);
2790 list_add(&l->node_ref_from, &next_block->ref_from_list);
2792 btrfsic_block_link_hashtable_add(l,
2793 &state->block_link_hashtable);
2796 l->parent_generation = parent_generation;
2797 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2798 btrfsic_print_add_link(state, l);
2804 static struct btrfsic_block *btrfsic_block_lookup_or_add(
2805 struct btrfsic_state *state,
2806 struct btrfsic_block_data_ctx *block_ctx,
2807 const char *additional_string,
2814 struct btrfsic_block *block;
2816 block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2817 block_ctx->dev_bytenr,
2818 &state->block_hashtable);
2819 if (NULL == block) {
2820 struct btrfsic_dev_state *dev_state;
2822 block = btrfsic_block_alloc();
2823 if (NULL == block) {
2824 printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
2827 dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
2828 if (NULL == dev_state) {
2830 "btrfsic: error, lookup dev_state failed!\n");
2831 btrfsic_block_free(block);
2834 block->dev_state = dev_state;
2835 block->dev_bytenr = block_ctx->dev_bytenr;
2836 block->logical_bytenr = block_ctx->start;
2837 block->is_metadata = is_metadata;
2838 block->is_iodone = is_iodone;
2839 block->never_written = never_written;
2840 block->mirror_num = mirror_num;
2841 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2843 "New %s%c-block @%llu (%s/%llu/%d)\n",
2845 btrfsic_get_block_type(state, block),
2846 block->logical_bytenr, dev_state->name,
2847 block->dev_bytenr, mirror_num);
2848 list_add(&block->all_blocks_node, &state->all_blocks_list);
2849 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2850 if (NULL != was_created)
2853 if (NULL != was_created)
2860 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2862 struct btrfsic_dev_state *dev_state,
2868 struct btrfsic_block_data_ctx block_ctx;
2871 num_copies = btrfs_num_copies(state->root->fs_info,
2872 bytenr, state->metablock_size);
2874 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2875 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
2876 &block_ctx, mirror_num);
2878 printk(KERN_INFO "btrfsic:"
2879 " btrfsic_map_block(logical @%llu,"
2880 " mirror %d) failed!\n",
2881 bytenr, mirror_num);
2885 if (dev_state->bdev == block_ctx.dev->bdev &&
2886 dev_bytenr == block_ctx.dev_bytenr) {
2888 btrfsic_release_block_ctx(&block_ctx);
2891 btrfsic_release_block_ctx(&block_ctx);
2894 if (WARN_ON(!match)) {
2895 printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
2896 " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
2897 " phys_bytenr=%llu)!\n",
2898 bytenr, dev_state->name, dev_bytenr);
2899 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2900 ret = btrfsic_map_block(state, bytenr,
2901 state->metablock_size,
2902 &block_ctx, mirror_num);
2906 printk(KERN_INFO "Read logical bytenr @%llu maps to"
2908 bytenr, block_ctx.dev->name,
2909 block_ctx.dev_bytenr, mirror_num);
2914 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
2915 struct block_device *bdev)
2917 struct btrfsic_dev_state *ds;
2919 ds = btrfsic_dev_state_hashtable_lookup(bdev,
2920 &btrfsic_dev_state_hashtable);
2924 int btrfsic_submit_bh(int rw, struct buffer_head *bh)
2926 struct btrfsic_dev_state *dev_state;
2928 if (!btrfsic_is_initialized)
2929 return submit_bh(rw, bh);
2931 mutex_lock(&btrfsic_mutex);
2932 /* since btrfsic_submit_bh() might also be called before
2933 * btrfsic_mount(), this might return NULL */
2934 dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
2936 /* Only called to write the superblock (incl. FLUSH/FUA) */
2937 if (NULL != dev_state &&
2938 (rw & WRITE) && bh->b_size > 0) {
2941 dev_bytenr = 4096 * bh->b_blocknr;
2942 if (dev_state->state->print_mask &
2943 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2945 "submit_bh(rw=0x%x, blocknr=%llu (bytenr %llu),"
2946 " size=%zu, data=%p, bdev=%p)\n",
2947 rw, (unsigned long long)bh->b_blocknr,
2948 dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
2949 btrfsic_process_written_block(dev_state, dev_bytenr,
2950 &bh->b_data, 1, NULL,
2952 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
2953 if (dev_state->state->print_mask &
2954 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2956 "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
2958 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2959 if ((dev_state->state->print_mask &
2960 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2961 BTRFSIC_PRINT_MASK_VERBOSE)))
2963 "btrfsic_submit_bh(%s) with FLUSH"
2964 " but dummy block already in use"
2968 struct btrfsic_block *const block =
2969 &dev_state->dummy_block_for_bio_bh_flush;
2971 block->is_iodone = 0;
2972 block->never_written = 0;
2973 block->iodone_w_error = 0;
2974 block->flush_gen = dev_state->last_flush_gen + 1;
2975 block->submit_bio_bh_rw = rw;
2976 block->orig_bio_bh_private = bh->b_private;
2977 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2978 block->next_in_same_bio = NULL;
2979 bh->b_private = block;
2980 bh->b_end_io = btrfsic_bh_end_io;
2983 mutex_unlock(&btrfsic_mutex);
2984 return submit_bh(rw, bh);
2987 static void __btrfsic_submit_bio(int rw, struct bio *bio)
2989 struct btrfsic_dev_state *dev_state;
2991 if (!btrfsic_is_initialized)
2994 mutex_lock(&btrfsic_mutex);
2995 /* since btrfsic_submit_bio() is also called before
2996 * btrfsic_mount(), this might return NULL */
2997 dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
2998 if (NULL != dev_state &&
2999 (rw & WRITE) && NULL != bio->bi_io_vec) {
3003 char **mapped_datav;
3005 dev_bytenr = 512 * bio->bi_sector;
3007 if (dev_state->state->print_mask &
3008 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3010 "submit_bio(rw=0x%x, bi_vcnt=%u,"
3011 " bi_sector=%llu (bytenr %llu), bi_bdev=%p)\n",
3013 (unsigned long long)bio->bi_sector, dev_bytenr,
3016 mapped_datav = kmalloc(sizeof(*mapped_datav) * bio->bi_vcnt,
3020 for (i = 0; i < bio->bi_vcnt; i++) {
3021 BUG_ON(bio->bi_io_vec[i].bv_len != PAGE_CACHE_SIZE);
3022 mapped_datav[i] = kmap(bio->bi_io_vec[i].bv_page);
3023 if (!mapped_datav[i]) {
3026 kunmap(bio->bi_io_vec[i].bv_page);
3028 kfree(mapped_datav);
3031 if ((BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3032 BTRFSIC_PRINT_MASK_VERBOSE) ==
3033 (dev_state->state->print_mask &
3034 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3035 BTRFSIC_PRINT_MASK_VERBOSE)))
3037 "#%u: page=%p, len=%u, offset=%u\n",
3038 i, bio->bi_io_vec[i].bv_page,
3039 bio->bi_io_vec[i].bv_len,
3040 bio->bi_io_vec[i].bv_offset);
3042 btrfsic_process_written_block(dev_state, dev_bytenr,
3043 mapped_datav, bio->bi_vcnt,
3044 bio, &bio_is_patched,
3048 kunmap(bio->bi_io_vec[i].bv_page);
3050 kfree(mapped_datav);
3051 } else if (NULL != dev_state && (rw & REQ_FLUSH)) {
3052 if (dev_state->state->print_mask &
3053 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
3055 "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
3057 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
3058 if ((dev_state->state->print_mask &
3059 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
3060 BTRFSIC_PRINT_MASK_VERBOSE)))
3062 "btrfsic_submit_bio(%s) with FLUSH"
3063 " but dummy block already in use"
3067 struct btrfsic_block *const block =
3068 &dev_state->dummy_block_for_bio_bh_flush;
3070 block->is_iodone = 0;
3071 block->never_written = 0;
3072 block->iodone_w_error = 0;
3073 block->flush_gen = dev_state->last_flush_gen + 1;
3074 block->submit_bio_bh_rw = rw;
3075 block->orig_bio_bh_private = bio->bi_private;
3076 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
3077 block->next_in_same_bio = NULL;
3078 bio->bi_private = block;
3079 bio->bi_end_io = btrfsic_bio_end_io;
3083 mutex_unlock(&btrfsic_mutex);
3086 void btrfsic_submit_bio(int rw, struct bio *bio)
3088 __btrfsic_submit_bio(rw, bio);
3089 submit_bio(rw, bio);
3092 int btrfsic_submit_bio_wait(int rw, struct bio *bio)
3094 __btrfsic_submit_bio(rw, bio);
3095 return submit_bio_wait(rw, bio);
3098 int btrfsic_mount(struct btrfs_root *root,
3099 struct btrfs_fs_devices *fs_devices,
3100 int including_extent_data, u32 print_mask)
3103 struct btrfsic_state *state;
3104 struct list_head *dev_head = &fs_devices->devices;
3105 struct btrfs_device *device;
3107 if (root->nodesize != root->leafsize) {
3109 "btrfsic: cannot handle nodesize %d != leafsize %d!\n",
3110 root->nodesize, root->leafsize);
3113 if (root->nodesize & ((u64)PAGE_CACHE_SIZE - 1)) {
3115 "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3116 root->nodesize, PAGE_CACHE_SIZE);
3119 if (root->leafsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3121 "btrfsic: cannot handle leafsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3122 root->leafsize, PAGE_CACHE_SIZE);
3125 if (root->sectorsize & ((u64)PAGE_CACHE_SIZE - 1)) {
3127 "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3128 root->sectorsize, PAGE_CACHE_SIZE);
3131 state = kzalloc(sizeof(*state), GFP_NOFS);
3132 if (NULL == state) {
3133 printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
3137 if (!btrfsic_is_initialized) {
3138 mutex_init(&btrfsic_mutex);
3139 btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
3140 btrfsic_is_initialized = 1;
3142 mutex_lock(&btrfsic_mutex);
3144 state->print_mask = print_mask;
3145 state->include_extent_data = including_extent_data;
3146 state->csum_size = 0;
3147 state->metablock_size = root->nodesize;
3148 state->datablock_size = root->sectorsize;
3149 INIT_LIST_HEAD(&state->all_blocks_list);
3150 btrfsic_block_hashtable_init(&state->block_hashtable);
3151 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
3152 state->max_superblock_generation = 0;
3153 state->latest_superblock = NULL;
3155 list_for_each_entry(device, dev_head, dev_list) {
3156 struct btrfsic_dev_state *ds;
3159 if (!device->bdev || !device->name)
3162 ds = btrfsic_dev_state_alloc();
3165 "btrfs check-integrity: kmalloc() failed!\n");
3166 mutex_unlock(&btrfsic_mutex);
3169 ds->bdev = device->bdev;
3171 bdevname(ds->bdev, ds->name);
3172 ds->name[BDEVNAME_SIZE - 1] = '\0';
3173 for (p = ds->name; *p != '\0'; p++);
3174 while (p > ds->name && *p != '/')
3178 strlcpy(ds->name, p, sizeof(ds->name));
3179 btrfsic_dev_state_hashtable_add(ds,
3180 &btrfsic_dev_state_hashtable);
3183 ret = btrfsic_process_superblock(state, fs_devices);
3185 mutex_unlock(&btrfsic_mutex);
3186 btrfsic_unmount(root, fs_devices);
3190 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
3191 btrfsic_dump_database(state);
3192 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
3193 btrfsic_dump_tree(state);
3195 mutex_unlock(&btrfsic_mutex);
3199 void btrfsic_unmount(struct btrfs_root *root,
3200 struct btrfs_fs_devices *fs_devices)
3202 struct list_head *elem_all;
3203 struct list_head *tmp_all;
3204 struct btrfsic_state *state;
3205 struct list_head *dev_head = &fs_devices->devices;
3206 struct btrfs_device *device;
3208 if (!btrfsic_is_initialized)
3211 mutex_lock(&btrfsic_mutex);
3214 list_for_each_entry(device, dev_head, dev_list) {
3215 struct btrfsic_dev_state *ds;
3217 if (!device->bdev || !device->name)
3220 ds = btrfsic_dev_state_hashtable_lookup(
3222 &btrfsic_dev_state_hashtable);
3225 btrfsic_dev_state_hashtable_remove(ds);
3226 btrfsic_dev_state_free(ds);
3230 if (NULL == state) {
3232 "btrfsic: error, cannot find state information"
3234 mutex_unlock(&btrfsic_mutex);
3239 * Don't care about keeping the lists' state up to date,
3240 * just free all memory that was allocated dynamically.
3241 * Free the blocks and the block_links.
3243 list_for_each_safe(elem_all, tmp_all, &state->all_blocks_list) {
3244 struct btrfsic_block *const b_all =
3245 list_entry(elem_all, struct btrfsic_block,
3247 struct list_head *elem_ref_to;
3248 struct list_head *tmp_ref_to;
3250 list_for_each_safe(elem_ref_to, tmp_ref_to,
3251 &b_all->ref_to_list) {
3252 struct btrfsic_block_link *const l =
3253 list_entry(elem_ref_to,
3254 struct btrfsic_block_link,
3257 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
3258 btrfsic_print_rem_link(state, l);
3261 if (0 == l->ref_cnt)
3262 btrfsic_block_link_free(l);
3265 if (b_all->is_iodone || b_all->never_written)
3266 btrfsic_block_free(b_all);
3268 printk(KERN_INFO "btrfs: attempt to free %c-block"
3269 " @%llu (%s/%llu/%d) on umount which is"
3270 " not yet iodone!\n",
3271 btrfsic_get_block_type(state, b_all),
3272 b_all->logical_bytenr, b_all->dev_state->name,
3273 b_all->dev_bytenr, b_all->mirror_num);
3276 mutex_unlock(&btrfsic_mutex);