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.
81 * Expect millions of lines of information in the kernel log with an
82 * enabled check_int_print_mask. Therefore set LOG_BUF_SHIFT in the
83 * kernel config to at least 26 (which is 64MB). Usually the value is
84 * limited to 21 (which is 2MB) in init/Kconfig. The file needs to be
85 * changed like this before LOG_BUF_SHIFT can be set to a high value:
86 * config LOG_BUF_SHIFT
87 * int "Kernel log buffer size (16 => 64KB, 17 => 128KB)"
91 #include <linux/sched.h>
92 #include <linux/slab.h>
93 #include <linux/buffer_head.h>
94 #include <linux/mutex.h>
95 #include <linux/genhd.h>
96 #include <linux/blkdev.h>
98 #include <linux/string.h>
102 #include "transaction.h"
103 #include "extent_io.h"
105 #include "print-tree.h"
107 #include "check-integrity.h"
108 #include "rcu-string.h"
109 #include "compression.h"
111 #define BTRFSIC_BLOCK_HASHTABLE_SIZE 0x10000
112 #define BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE 0x10000
113 #define BTRFSIC_DEV2STATE_HASHTABLE_SIZE 0x100
114 #define BTRFSIC_BLOCK_MAGIC_NUMBER 0x14491051
115 #define BTRFSIC_BLOCK_LINK_MAGIC_NUMBER 0x11070807
116 #define BTRFSIC_DEV2STATE_MAGIC_NUMBER 0x20111530
117 #define BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER 20111300
118 #define BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL (200 - 6) /* in characters,
119 * excluding " [...]" */
120 #define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
123 * The definition of the bitmask fields for the print_mask.
124 * They are specified with the mount option check_integrity_print_mask.
126 #define BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE 0x00000001
127 #define BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION 0x00000002
128 #define BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE 0x00000004
129 #define BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE 0x00000008
130 #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH 0x00000010
131 #define BTRFSIC_PRINT_MASK_END_IO_BIO_BH 0x00000020
132 #define BTRFSIC_PRINT_MASK_VERBOSE 0x00000040
133 #define BTRFSIC_PRINT_MASK_VERY_VERBOSE 0x00000080
134 #define BTRFSIC_PRINT_MASK_INITIAL_TREE 0x00000100
135 #define BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES 0x00000200
136 #define BTRFSIC_PRINT_MASK_INITIAL_DATABASE 0x00000400
137 #define BTRFSIC_PRINT_MASK_NUM_COPIES 0x00000800
138 #define BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS 0x00001000
139 #define BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE 0x00002000
141 struct btrfsic_dev_state;
142 struct btrfsic_state;
144 struct btrfsic_block {
145 u32 magic_num; /* only used for debug purposes */
146 unsigned int is_metadata:1; /* if it is meta-data, not data-data */
147 unsigned int is_superblock:1; /* if it is one of the superblocks */
148 unsigned int is_iodone:1; /* if is done by lower subsystem */
149 unsigned int iodone_w_error:1; /* error was indicated to endio */
150 unsigned int never_written:1; /* block was added because it was
151 * referenced, not because it was
153 unsigned int mirror_num; /* large enough to hold
154 * BTRFS_SUPER_MIRROR_MAX */
155 struct btrfsic_dev_state *dev_state;
156 u64 dev_bytenr; /* key, physical byte num on disk */
157 u64 logical_bytenr; /* logical byte num on disk */
159 struct btrfs_disk_key disk_key; /* extra info to print in case of
160 * issues, will not always be correct */
161 struct list_head collision_resolving_node; /* list node */
162 struct list_head all_blocks_node; /* list node */
164 /* the following two lists contain block_link items */
165 struct list_head ref_to_list; /* list */
166 struct list_head ref_from_list; /* list */
167 struct btrfsic_block *next_in_same_bio;
168 void *orig_bio_bh_private;
172 } orig_bio_bh_end_io;
173 int submit_bio_bh_rw;
174 u64 flush_gen; /* only valid if !never_written */
178 * Elements of this type are allocated dynamically and required because
179 * each block object can refer to and can be ref from multiple blocks.
180 * The key to lookup them in the hashtable is the dev_bytenr of
181 * the block ref to plus the one from the block referred from.
182 * The fact that they are searchable via a hashtable and that a
183 * ref_cnt is maintained is not required for the btrfs integrity
184 * check algorithm itself, it is only used to make the output more
185 * beautiful in case that an error is detected (an error is defined
186 * as a write operation to a block while that block is still referenced).
188 struct btrfsic_block_link {
189 u32 magic_num; /* only used for debug purposes */
191 struct list_head node_ref_to; /* list node */
192 struct list_head node_ref_from; /* list node */
193 struct list_head collision_resolving_node; /* list node */
194 struct btrfsic_block *block_ref_to;
195 struct btrfsic_block *block_ref_from;
196 u64 parent_generation;
199 struct btrfsic_dev_state {
200 u32 magic_num; /* only used for debug purposes */
201 struct block_device *bdev;
202 struct btrfsic_state *state;
203 struct list_head collision_resolving_node; /* list node */
204 struct btrfsic_block dummy_block_for_bio_bh_flush;
206 char name[BDEVNAME_SIZE];
209 struct btrfsic_block_hashtable {
210 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
213 struct btrfsic_block_link_hashtable {
214 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
217 struct btrfsic_dev_state_hashtable {
218 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
221 struct btrfsic_block_data_ctx {
222 u64 start; /* virtual bytenr */
223 u64 dev_bytenr; /* physical bytenr on device */
225 struct btrfsic_dev_state *dev;
231 /* This structure is used to implement recursion without occupying
232 * any stack space, refer to btrfsic_process_metablock() */
233 struct btrfsic_stack_frame {
241 struct btrfsic_block *block;
242 struct btrfsic_block_data_ctx *block_ctx;
243 struct btrfsic_block *next_block;
244 struct btrfsic_block_data_ctx next_block_ctx;
245 struct btrfs_header *hdr;
246 struct btrfsic_stack_frame *prev;
249 /* Some state per mounted filesystem */
250 struct btrfsic_state {
252 int include_extent_data;
254 struct list_head all_blocks_list;
255 struct btrfsic_block_hashtable block_hashtable;
256 struct btrfsic_block_link_hashtable block_link_hashtable;
257 struct btrfs_fs_info *fs_info;
258 u64 max_superblock_generation;
259 struct btrfsic_block *latest_superblock;
264 static void btrfsic_block_init(struct btrfsic_block *b);
265 static struct btrfsic_block *btrfsic_block_alloc(void);
266 static void btrfsic_block_free(struct btrfsic_block *b);
267 static void btrfsic_block_link_init(struct btrfsic_block_link *n);
268 static struct btrfsic_block_link *btrfsic_block_link_alloc(void);
269 static void btrfsic_block_link_free(struct btrfsic_block_link *n);
270 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds);
271 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void);
272 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds);
273 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h);
274 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
275 struct btrfsic_block_hashtable *h);
276 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b);
277 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
278 struct block_device *bdev,
280 struct btrfsic_block_hashtable *h);
281 static void btrfsic_block_link_hashtable_init(
282 struct btrfsic_block_link_hashtable *h);
283 static void btrfsic_block_link_hashtable_add(
284 struct btrfsic_block_link *l,
285 struct btrfsic_block_link_hashtable *h);
286 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l);
287 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
288 struct block_device *bdev_ref_to,
289 u64 dev_bytenr_ref_to,
290 struct block_device *bdev_ref_from,
291 u64 dev_bytenr_ref_from,
292 struct btrfsic_block_link_hashtable *h);
293 static void btrfsic_dev_state_hashtable_init(
294 struct btrfsic_dev_state_hashtable *h);
295 static void btrfsic_dev_state_hashtable_add(
296 struct btrfsic_dev_state *ds,
297 struct btrfsic_dev_state_hashtable *h);
298 static void btrfsic_dev_state_hashtable_remove(struct btrfsic_dev_state *ds);
299 static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(dev_t dev,
300 struct btrfsic_dev_state_hashtable *h);
301 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void);
302 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf);
303 static int btrfsic_process_superblock(struct btrfsic_state *state,
304 struct btrfs_fs_devices *fs_devices);
305 static int btrfsic_process_metablock(struct btrfsic_state *state,
306 struct btrfsic_block *block,
307 struct btrfsic_block_data_ctx *block_ctx,
308 int limit_nesting, int force_iodone_flag);
309 static void btrfsic_read_from_block_data(
310 struct btrfsic_block_data_ctx *block_ctx,
311 void *dst, u32 offset, size_t len);
312 static int btrfsic_create_link_to_next_block(
313 struct btrfsic_state *state,
314 struct btrfsic_block *block,
315 struct btrfsic_block_data_ctx
316 *block_ctx, u64 next_bytenr,
318 struct btrfsic_block_data_ctx *next_block_ctx,
319 struct btrfsic_block **next_blockp,
320 int force_iodone_flag,
321 int *num_copiesp, int *mirror_nump,
322 struct btrfs_disk_key *disk_key,
323 u64 parent_generation);
324 static int btrfsic_handle_extent_data(struct btrfsic_state *state,
325 struct btrfsic_block *block,
326 struct btrfsic_block_data_ctx *block_ctx,
327 u32 item_offset, int force_iodone_flag);
328 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
329 struct btrfsic_block_data_ctx *block_ctx_out,
331 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx);
332 static int btrfsic_read_block(struct btrfsic_state *state,
333 struct btrfsic_block_data_ctx *block_ctx);
334 static void btrfsic_dump_database(struct btrfsic_state *state);
335 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
336 char **datav, unsigned int num_pages);
337 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
338 u64 dev_bytenr, char **mapped_datav,
339 unsigned int num_pages,
340 struct bio *bio, int *bio_is_patched,
341 struct buffer_head *bh,
342 int submit_bio_bh_rw);
343 static int btrfsic_process_written_superblock(
344 struct btrfsic_state *state,
345 struct btrfsic_block *const block,
346 struct btrfs_super_block *const super_hdr);
347 static void btrfsic_bio_end_io(struct bio *bp);
348 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate);
349 static int btrfsic_is_block_ref_by_superblock(const struct btrfsic_state *state,
350 const struct btrfsic_block *block,
351 int recursion_level);
352 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
353 struct btrfsic_block *const block,
354 int recursion_level);
355 static void btrfsic_print_add_link(const struct btrfsic_state *state,
356 const struct btrfsic_block_link *l);
357 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
358 const struct btrfsic_block_link *l);
359 static char btrfsic_get_block_type(const struct btrfsic_state *state,
360 const struct btrfsic_block *block);
361 static void btrfsic_dump_tree(const struct btrfsic_state *state);
362 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
363 const struct btrfsic_block *block,
365 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
366 struct btrfsic_state *state,
367 struct btrfsic_block_data_ctx *next_block_ctx,
368 struct btrfsic_block *next_block,
369 struct btrfsic_block *from_block,
370 u64 parent_generation);
371 static struct btrfsic_block *btrfsic_block_lookup_or_add(
372 struct btrfsic_state *state,
373 struct btrfsic_block_data_ctx *block_ctx,
374 const char *additional_string,
380 static int btrfsic_process_superblock_dev_mirror(
381 struct btrfsic_state *state,
382 struct btrfsic_dev_state *dev_state,
383 struct btrfs_device *device,
384 int superblock_mirror_num,
385 struct btrfsic_dev_state **selected_dev_state,
386 struct btrfs_super_block *selected_super);
387 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev);
388 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
390 struct btrfsic_dev_state *dev_state,
393 static struct mutex btrfsic_mutex;
394 static int btrfsic_is_initialized;
395 static struct btrfsic_dev_state_hashtable btrfsic_dev_state_hashtable;
398 static void btrfsic_block_init(struct btrfsic_block *b)
400 b->magic_num = BTRFSIC_BLOCK_MAGIC_NUMBER;
403 b->logical_bytenr = 0;
404 b->generation = BTRFSIC_GENERATION_UNKNOWN;
405 b->disk_key.objectid = 0;
406 b->disk_key.type = 0;
407 b->disk_key.offset = 0;
409 b->is_superblock = 0;
411 b->iodone_w_error = 0;
412 b->never_written = 0;
414 b->next_in_same_bio = NULL;
415 b->orig_bio_bh_private = NULL;
416 b->orig_bio_bh_end_io.bio = NULL;
417 INIT_LIST_HEAD(&b->collision_resolving_node);
418 INIT_LIST_HEAD(&b->all_blocks_node);
419 INIT_LIST_HEAD(&b->ref_to_list);
420 INIT_LIST_HEAD(&b->ref_from_list);
421 b->submit_bio_bh_rw = 0;
425 static struct btrfsic_block *btrfsic_block_alloc(void)
427 struct btrfsic_block *b;
429 b = kzalloc(sizeof(*b), GFP_NOFS);
431 btrfsic_block_init(b);
436 static void btrfsic_block_free(struct btrfsic_block *b)
438 BUG_ON(!(NULL == b || BTRFSIC_BLOCK_MAGIC_NUMBER == b->magic_num));
442 static void btrfsic_block_link_init(struct btrfsic_block_link *l)
444 l->magic_num = BTRFSIC_BLOCK_LINK_MAGIC_NUMBER;
446 INIT_LIST_HEAD(&l->node_ref_to);
447 INIT_LIST_HEAD(&l->node_ref_from);
448 INIT_LIST_HEAD(&l->collision_resolving_node);
449 l->block_ref_to = NULL;
450 l->block_ref_from = NULL;
453 static struct btrfsic_block_link *btrfsic_block_link_alloc(void)
455 struct btrfsic_block_link *l;
457 l = kzalloc(sizeof(*l), GFP_NOFS);
459 btrfsic_block_link_init(l);
464 static void btrfsic_block_link_free(struct btrfsic_block_link *l)
466 BUG_ON(!(NULL == l || BTRFSIC_BLOCK_LINK_MAGIC_NUMBER == l->magic_num));
470 static void btrfsic_dev_state_init(struct btrfsic_dev_state *ds)
472 ds->magic_num = BTRFSIC_DEV2STATE_MAGIC_NUMBER;
476 INIT_LIST_HEAD(&ds->collision_resolving_node);
477 ds->last_flush_gen = 0;
478 btrfsic_block_init(&ds->dummy_block_for_bio_bh_flush);
479 ds->dummy_block_for_bio_bh_flush.is_iodone = 1;
480 ds->dummy_block_for_bio_bh_flush.dev_state = ds;
483 static struct btrfsic_dev_state *btrfsic_dev_state_alloc(void)
485 struct btrfsic_dev_state *ds;
487 ds = kzalloc(sizeof(*ds), GFP_NOFS);
489 btrfsic_dev_state_init(ds);
494 static void btrfsic_dev_state_free(struct btrfsic_dev_state *ds)
496 BUG_ON(!(NULL == ds ||
497 BTRFSIC_DEV2STATE_MAGIC_NUMBER == ds->magic_num));
501 static void btrfsic_block_hashtable_init(struct btrfsic_block_hashtable *h)
505 for (i = 0; i < BTRFSIC_BLOCK_HASHTABLE_SIZE; i++)
506 INIT_LIST_HEAD(h->table + i);
509 static void btrfsic_block_hashtable_add(struct btrfsic_block *b,
510 struct btrfsic_block_hashtable *h)
512 const unsigned int hashval =
513 (((unsigned int)(b->dev_bytenr >> 16)) ^
514 ((unsigned int)((uintptr_t)b->dev_state->bdev))) &
515 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
517 list_add(&b->collision_resolving_node, h->table + hashval);
520 static void btrfsic_block_hashtable_remove(struct btrfsic_block *b)
522 list_del(&b->collision_resolving_node);
525 static struct btrfsic_block *btrfsic_block_hashtable_lookup(
526 struct block_device *bdev,
528 struct btrfsic_block_hashtable *h)
530 const unsigned int hashval =
531 (((unsigned int)(dev_bytenr >> 16)) ^
532 ((unsigned int)((uintptr_t)bdev))) &
533 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
534 struct btrfsic_block *b;
536 list_for_each_entry(b, h->table + hashval, collision_resolving_node) {
537 if (b->dev_state->bdev == bdev && b->dev_bytenr == dev_bytenr)
544 static void btrfsic_block_link_hashtable_init(
545 struct btrfsic_block_link_hashtable *h)
549 for (i = 0; i < BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE; i++)
550 INIT_LIST_HEAD(h->table + i);
553 static void btrfsic_block_link_hashtable_add(
554 struct btrfsic_block_link *l,
555 struct btrfsic_block_link_hashtable *h)
557 const unsigned int hashval =
558 (((unsigned int)(l->block_ref_to->dev_bytenr >> 16)) ^
559 ((unsigned int)(l->block_ref_from->dev_bytenr >> 16)) ^
560 ((unsigned int)((uintptr_t)l->block_ref_to->dev_state->bdev)) ^
561 ((unsigned int)((uintptr_t)l->block_ref_from->dev_state->bdev)))
562 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
564 BUG_ON(NULL == l->block_ref_to);
565 BUG_ON(NULL == l->block_ref_from);
566 list_add(&l->collision_resolving_node, h->table + hashval);
569 static void btrfsic_block_link_hashtable_remove(struct btrfsic_block_link *l)
571 list_del(&l->collision_resolving_node);
574 static struct btrfsic_block_link *btrfsic_block_link_hashtable_lookup(
575 struct block_device *bdev_ref_to,
576 u64 dev_bytenr_ref_to,
577 struct block_device *bdev_ref_from,
578 u64 dev_bytenr_ref_from,
579 struct btrfsic_block_link_hashtable *h)
581 const unsigned int hashval =
582 (((unsigned int)(dev_bytenr_ref_to >> 16)) ^
583 ((unsigned int)(dev_bytenr_ref_from >> 16)) ^
584 ((unsigned int)((uintptr_t)bdev_ref_to)) ^
585 ((unsigned int)((uintptr_t)bdev_ref_from))) &
586 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
587 struct btrfsic_block_link *l;
589 list_for_each_entry(l, h->table + hashval, 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(dev_t dev,
628 struct btrfsic_dev_state_hashtable *h)
630 const unsigned int hashval =
631 dev & (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1);
632 struct btrfsic_dev_state *ds;
634 list_for_each_entry(ds, h->table + hashval, collision_resolving_node) {
635 if (ds->bdev->bd_dev == dev)
642 static int btrfsic_process_superblock(struct btrfsic_state *state,
643 struct btrfs_fs_devices *fs_devices)
645 struct btrfs_super_block *selected_super;
646 struct list_head *dev_head = &fs_devices->devices;
647 struct btrfs_device *device;
648 struct btrfsic_dev_state *selected_dev_state = NULL;
652 BUG_ON(NULL == state);
653 selected_super = kzalloc(sizeof(*selected_super), GFP_NOFS);
654 if (NULL == selected_super) {
655 pr_info("btrfsic: error, kmalloc failed!\n");
659 list_for_each_entry(device, dev_head, dev_list) {
661 struct btrfsic_dev_state *dev_state;
663 if (!device->bdev || !device->name)
666 dev_state = btrfsic_dev_state_lookup(device->bdev->bd_dev);
667 BUG_ON(NULL == dev_state);
668 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
669 ret = btrfsic_process_superblock_dev_mirror(
670 state, dev_state, device, i,
671 &selected_dev_state, selected_super);
672 if (0 != ret && 0 == i) {
673 kfree(selected_super);
679 if (NULL == state->latest_superblock) {
680 pr_info("btrfsic: no superblock found!\n");
681 kfree(selected_super);
685 state->csum_size = btrfs_super_csum_size(selected_super);
687 for (pass = 0; pass < 3; pass++) {
694 next_bytenr = btrfs_super_root(selected_super);
695 if (state->print_mask &
696 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
697 pr_info("root@%llu\n", next_bytenr);
700 next_bytenr = btrfs_super_chunk_root(selected_super);
701 if (state->print_mask &
702 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
703 pr_info("chunk@%llu\n", next_bytenr);
706 next_bytenr = btrfs_super_log_root(selected_super);
707 if (0 == next_bytenr)
709 if (state->print_mask &
710 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
711 pr_info("log@%llu\n", next_bytenr);
715 num_copies = btrfs_num_copies(state->fs_info, next_bytenr,
716 state->metablock_size);
717 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
718 pr_info("num_copies(log_bytenr=%llu) = %d\n",
719 next_bytenr, num_copies);
721 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
722 struct btrfsic_block *next_block;
723 struct btrfsic_block_data_ctx tmp_next_block_ctx;
724 struct btrfsic_block_link *l;
726 ret = btrfsic_map_block(state, next_bytenr,
727 state->metablock_size,
731 pr_info("btrfsic: btrfsic_map_block(root @%llu, mirror %d) failed!\n",
732 next_bytenr, mirror_num);
733 kfree(selected_super);
737 next_block = btrfsic_block_hashtable_lookup(
738 tmp_next_block_ctx.dev->bdev,
739 tmp_next_block_ctx.dev_bytenr,
740 &state->block_hashtable);
741 BUG_ON(NULL == next_block);
743 l = btrfsic_block_link_hashtable_lookup(
744 tmp_next_block_ctx.dev->bdev,
745 tmp_next_block_ctx.dev_bytenr,
746 state->latest_superblock->dev_state->
748 state->latest_superblock->dev_bytenr,
749 &state->block_link_hashtable);
752 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
753 if (ret < (int)PAGE_SIZE) {
754 pr_info("btrfsic: read @logical %llu failed!\n",
755 tmp_next_block_ctx.start);
756 btrfsic_release_block_ctx(&tmp_next_block_ctx);
757 kfree(selected_super);
761 ret = btrfsic_process_metablock(state,
764 BTRFS_MAX_LEVEL + 3, 1);
765 btrfsic_release_block_ctx(&tmp_next_block_ctx);
769 kfree(selected_super);
773 static int btrfsic_process_superblock_dev_mirror(
774 struct btrfsic_state *state,
775 struct btrfsic_dev_state *dev_state,
776 struct btrfs_device *device,
777 int superblock_mirror_num,
778 struct btrfsic_dev_state **selected_dev_state,
779 struct btrfs_super_block *selected_super)
781 struct btrfs_fs_info *fs_info = state->fs_info;
782 struct btrfs_super_block *super_tmp;
784 struct buffer_head *bh;
785 struct btrfsic_block *superblock_tmp;
787 struct block_device *const superblock_bdev = device->bdev;
789 /* super block bytenr is always the unmapped device bytenr */
790 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
791 if (dev_bytenr + BTRFS_SUPER_INFO_SIZE > device->commit_total_bytes)
793 bh = __bread(superblock_bdev, dev_bytenr / BTRFS_BDEV_BLOCKSIZE,
794 BTRFS_SUPER_INFO_SIZE);
797 super_tmp = (struct btrfs_super_block *)
798 (bh->b_data + (dev_bytenr & (BTRFS_BDEV_BLOCKSIZE - 1)));
800 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
801 btrfs_super_magic(super_tmp) != BTRFS_MAGIC ||
802 memcmp(device->uuid, super_tmp->dev_item.uuid, BTRFS_UUID_SIZE) ||
803 btrfs_super_nodesize(super_tmp) != state->metablock_size ||
804 btrfs_super_sectorsize(super_tmp) != state->datablock_size) {
810 btrfsic_block_hashtable_lookup(superblock_bdev,
812 &state->block_hashtable);
813 if (NULL == superblock_tmp) {
814 superblock_tmp = btrfsic_block_alloc();
815 if (NULL == superblock_tmp) {
816 pr_info("btrfsic: error, kmalloc failed!\n");
820 /* for superblock, only the dev_bytenr makes sense */
821 superblock_tmp->dev_bytenr = dev_bytenr;
822 superblock_tmp->dev_state = dev_state;
823 superblock_tmp->logical_bytenr = dev_bytenr;
824 superblock_tmp->generation = btrfs_super_generation(super_tmp);
825 superblock_tmp->is_metadata = 1;
826 superblock_tmp->is_superblock = 1;
827 superblock_tmp->is_iodone = 1;
828 superblock_tmp->never_written = 0;
829 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
830 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
831 btrfs_info_in_rcu(fs_info,
832 "new initial S-block (bdev %p, %s) @%llu (%s/%llu/%d)",
834 rcu_str_deref(device->name), dev_bytenr,
835 dev_state->name, dev_bytenr,
836 superblock_mirror_num);
837 list_add(&superblock_tmp->all_blocks_node,
838 &state->all_blocks_list);
839 btrfsic_block_hashtable_add(superblock_tmp,
840 &state->block_hashtable);
843 /* select the one with the highest generation field */
844 if (btrfs_super_generation(super_tmp) >
845 state->max_superblock_generation ||
846 0 == state->max_superblock_generation) {
847 memcpy(selected_super, super_tmp, sizeof(*selected_super));
848 *selected_dev_state = dev_state;
849 state->max_superblock_generation =
850 btrfs_super_generation(super_tmp);
851 state->latest_superblock = superblock_tmp;
854 for (pass = 0; pass < 3; pass++) {
858 const char *additional_string = NULL;
859 struct btrfs_disk_key tmp_disk_key;
861 tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
862 tmp_disk_key.offset = 0;
865 btrfs_set_disk_key_objectid(&tmp_disk_key,
866 BTRFS_ROOT_TREE_OBJECTID);
867 additional_string = "initial root ";
868 next_bytenr = btrfs_super_root(super_tmp);
871 btrfs_set_disk_key_objectid(&tmp_disk_key,
872 BTRFS_CHUNK_TREE_OBJECTID);
873 additional_string = "initial chunk ";
874 next_bytenr = btrfs_super_chunk_root(super_tmp);
877 btrfs_set_disk_key_objectid(&tmp_disk_key,
878 BTRFS_TREE_LOG_OBJECTID);
879 additional_string = "initial log ";
880 next_bytenr = btrfs_super_log_root(super_tmp);
881 if (0 == next_bytenr)
886 num_copies = btrfs_num_copies(fs_info, next_bytenr,
887 state->metablock_size);
888 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
889 pr_info("num_copies(log_bytenr=%llu) = %d\n",
890 next_bytenr, num_copies);
891 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
892 struct btrfsic_block *next_block;
893 struct btrfsic_block_data_ctx tmp_next_block_ctx;
894 struct btrfsic_block_link *l;
896 if (btrfsic_map_block(state, next_bytenr,
897 state->metablock_size,
900 pr_info("btrfsic: btrfsic_map_block(bytenr @%llu, mirror %d) failed!\n",
901 next_bytenr, mirror_num);
906 next_block = btrfsic_block_lookup_or_add(
907 state, &tmp_next_block_ctx,
908 additional_string, 1, 1, 0,
910 if (NULL == next_block) {
911 btrfsic_release_block_ctx(&tmp_next_block_ctx);
916 next_block->disk_key = tmp_disk_key;
917 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
918 l = btrfsic_block_link_lookup_or_add(
919 state, &tmp_next_block_ctx,
920 next_block, superblock_tmp,
921 BTRFSIC_GENERATION_UNKNOWN);
922 btrfsic_release_block_ctx(&tmp_next_block_ctx);
929 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_ALL_TREES)
930 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
936 static struct btrfsic_stack_frame *btrfsic_stack_frame_alloc(void)
938 struct btrfsic_stack_frame *sf;
940 sf = kzalloc(sizeof(*sf), GFP_NOFS);
942 pr_info("btrfsic: alloc memory failed!\n");
944 sf->magic = BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER;
948 static void btrfsic_stack_frame_free(struct btrfsic_stack_frame *sf)
950 BUG_ON(!(NULL == sf ||
951 BTRFSIC_BLOCK_STACK_FRAME_MAGIC_NUMBER == sf->magic));
955 static int btrfsic_process_metablock(
956 struct btrfsic_state *state,
957 struct btrfsic_block *const first_block,
958 struct btrfsic_block_data_ctx *const first_block_ctx,
959 int first_limit_nesting, int force_iodone_flag)
961 struct btrfsic_stack_frame initial_stack_frame = { 0 };
962 struct btrfsic_stack_frame *sf;
963 struct btrfsic_stack_frame *next_stack;
964 struct btrfs_header *const first_hdr =
965 (struct btrfs_header *)first_block_ctx->datav[0];
968 sf = &initial_stack_frame;
971 sf->limit_nesting = first_limit_nesting;
972 sf->block = first_block;
973 sf->block_ctx = first_block_ctx;
974 sf->next_block = NULL;
978 continue_with_new_stack_frame:
979 sf->block->generation = le64_to_cpu(sf->hdr->generation);
980 if (0 == sf->hdr->level) {
981 struct btrfs_leaf *const leafhdr =
982 (struct btrfs_leaf *)sf->hdr;
985 sf->nr = btrfs_stack_header_nritems(&leafhdr->header);
987 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
988 pr_info("leaf %llu items %d generation %llu owner %llu\n",
989 sf->block_ctx->start, sf->nr,
990 btrfs_stack_header_generation(
992 btrfs_stack_header_owner(
996 continue_with_current_leaf_stack_frame:
997 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1002 if (sf->i < sf->nr) {
1003 struct btrfs_item disk_item;
1004 u32 disk_item_offset =
1005 (uintptr_t)(leafhdr->items + sf->i) -
1007 struct btrfs_disk_key *disk_key;
1012 if (disk_item_offset + sizeof(struct btrfs_item) >
1013 sf->block_ctx->len) {
1014 leaf_item_out_of_bounce_error:
1015 pr_info("btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
1016 sf->block_ctx->start,
1017 sf->block_ctx->dev->name);
1018 goto one_stack_frame_backwards;
1020 btrfsic_read_from_block_data(sf->block_ctx,
1023 sizeof(struct btrfs_item));
1024 item_offset = btrfs_stack_item_offset(&disk_item);
1025 item_size = btrfs_stack_item_size(&disk_item);
1026 disk_key = &disk_item.key;
1027 type = btrfs_disk_key_type(disk_key);
1029 if (BTRFS_ROOT_ITEM_KEY == type) {
1030 struct btrfs_root_item root_item;
1031 u32 root_item_offset;
1034 root_item_offset = item_offset +
1035 offsetof(struct btrfs_leaf, items);
1036 if (root_item_offset + item_size >
1038 goto leaf_item_out_of_bounce_error;
1039 btrfsic_read_from_block_data(
1040 sf->block_ctx, &root_item,
1043 next_bytenr = btrfs_root_bytenr(&root_item);
1046 btrfsic_create_link_to_next_block(
1052 &sf->next_block_ctx,
1058 btrfs_root_generation(
1061 goto one_stack_frame_backwards;
1063 if (NULL != sf->next_block) {
1064 struct btrfs_header *const next_hdr =
1065 (struct btrfs_header *)
1066 sf->next_block_ctx.datav[0];
1069 btrfsic_stack_frame_alloc();
1070 if (NULL == next_stack) {
1072 btrfsic_release_block_ctx(
1075 goto one_stack_frame_backwards;
1079 next_stack->block = sf->next_block;
1080 next_stack->block_ctx =
1081 &sf->next_block_ctx;
1082 next_stack->next_block = NULL;
1083 next_stack->hdr = next_hdr;
1084 next_stack->limit_nesting =
1085 sf->limit_nesting - 1;
1086 next_stack->prev = sf;
1088 goto continue_with_new_stack_frame;
1090 } else if (BTRFS_EXTENT_DATA_KEY == type &&
1091 state->include_extent_data) {
1092 sf->error = btrfsic_handle_extent_data(
1099 goto one_stack_frame_backwards;
1102 goto continue_with_current_leaf_stack_frame;
1105 struct btrfs_node *const nodehdr = (struct btrfs_node *)sf->hdr;
1108 sf->nr = btrfs_stack_header_nritems(&nodehdr->header);
1110 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1111 pr_info("node %llu level %d items %d generation %llu owner %llu\n",
1112 sf->block_ctx->start,
1113 nodehdr->header.level, sf->nr,
1114 btrfs_stack_header_generation(
1116 btrfs_stack_header_owner(
1120 continue_with_current_node_stack_frame:
1121 if (0 == sf->num_copies || sf->mirror_num > sf->num_copies) {
1126 if (sf->i < sf->nr) {
1127 struct btrfs_key_ptr key_ptr;
1131 key_ptr_offset = (uintptr_t)(nodehdr->ptrs + sf->i) -
1133 if (key_ptr_offset + sizeof(struct btrfs_key_ptr) >
1134 sf->block_ctx->len) {
1135 pr_info("btrfsic: node item out of bounce at logical %llu, dev %s\n",
1136 sf->block_ctx->start,
1137 sf->block_ctx->dev->name);
1138 goto one_stack_frame_backwards;
1140 btrfsic_read_from_block_data(
1141 sf->block_ctx, &key_ptr, key_ptr_offset,
1142 sizeof(struct btrfs_key_ptr));
1143 next_bytenr = btrfs_stack_key_blockptr(&key_ptr);
1145 sf->error = btrfsic_create_link_to_next_block(
1151 &sf->next_block_ctx,
1157 btrfs_stack_key_generation(&key_ptr));
1159 goto one_stack_frame_backwards;
1161 if (NULL != sf->next_block) {
1162 struct btrfs_header *const next_hdr =
1163 (struct btrfs_header *)
1164 sf->next_block_ctx.datav[0];
1166 next_stack = btrfsic_stack_frame_alloc();
1167 if (NULL == next_stack) {
1169 goto one_stack_frame_backwards;
1173 next_stack->block = sf->next_block;
1174 next_stack->block_ctx = &sf->next_block_ctx;
1175 next_stack->next_block = NULL;
1176 next_stack->hdr = next_hdr;
1177 next_stack->limit_nesting =
1178 sf->limit_nesting - 1;
1179 next_stack->prev = sf;
1181 goto continue_with_new_stack_frame;
1184 goto continue_with_current_node_stack_frame;
1188 one_stack_frame_backwards:
1189 if (NULL != sf->prev) {
1190 struct btrfsic_stack_frame *const prev = sf->prev;
1192 /* the one for the initial block is freed in the caller */
1193 btrfsic_release_block_ctx(sf->block_ctx);
1196 prev->error = sf->error;
1197 btrfsic_stack_frame_free(sf);
1199 goto one_stack_frame_backwards;
1202 btrfsic_stack_frame_free(sf);
1204 goto continue_with_new_stack_frame;
1206 BUG_ON(&initial_stack_frame != sf);
1212 static void btrfsic_read_from_block_data(
1213 struct btrfsic_block_data_ctx *block_ctx,
1214 void *dstv, u32 offset, size_t len)
1217 size_t offset_in_page;
1219 char *dst = (char *)dstv;
1220 size_t start_offset = block_ctx->start & ((u64)PAGE_SIZE - 1);
1221 unsigned long i = (start_offset + offset) >> PAGE_SHIFT;
1223 WARN_ON(offset + len > block_ctx->len);
1224 offset_in_page = (start_offset + offset) & (PAGE_SIZE - 1);
1227 cur = min(len, ((size_t)PAGE_SIZE - offset_in_page));
1228 BUG_ON(i >= DIV_ROUND_UP(block_ctx->len, PAGE_SIZE));
1229 kaddr = block_ctx->datav[i];
1230 memcpy(dst, kaddr + offset_in_page, cur);
1239 static int btrfsic_create_link_to_next_block(
1240 struct btrfsic_state *state,
1241 struct btrfsic_block *block,
1242 struct btrfsic_block_data_ctx *block_ctx,
1245 struct btrfsic_block_data_ctx *next_block_ctx,
1246 struct btrfsic_block **next_blockp,
1247 int force_iodone_flag,
1248 int *num_copiesp, int *mirror_nump,
1249 struct btrfs_disk_key *disk_key,
1250 u64 parent_generation)
1252 struct btrfs_fs_info *fs_info = state->fs_info;
1253 struct btrfsic_block *next_block = NULL;
1255 struct btrfsic_block_link *l;
1256 int did_alloc_block_link;
1257 int block_was_created;
1259 *next_blockp = NULL;
1260 if (0 == *num_copiesp) {
1261 *num_copiesp = btrfs_num_copies(fs_info, next_bytenr,
1262 state->metablock_size);
1263 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1264 pr_info("num_copies(log_bytenr=%llu) = %d\n",
1265 next_bytenr, *num_copiesp);
1269 if (*mirror_nump > *num_copiesp)
1272 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1273 pr_info("btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1275 ret = btrfsic_map_block(state, next_bytenr,
1276 state->metablock_size,
1277 next_block_ctx, *mirror_nump);
1279 pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1280 next_bytenr, *mirror_nump);
1281 btrfsic_release_block_ctx(next_block_ctx);
1282 *next_blockp = NULL;
1286 next_block = btrfsic_block_lookup_or_add(state,
1287 next_block_ctx, "referenced ",
1288 1, force_iodone_flag,
1291 &block_was_created);
1292 if (NULL == next_block) {
1293 btrfsic_release_block_ctx(next_block_ctx);
1294 *next_blockp = NULL;
1297 if (block_was_created) {
1299 next_block->generation = BTRFSIC_GENERATION_UNKNOWN;
1301 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1302 if (next_block->logical_bytenr != next_bytenr &&
1303 !(!next_block->is_metadata &&
1304 0 == next_block->logical_bytenr))
1305 pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
1306 next_bytenr, next_block_ctx->dev->name,
1307 next_block_ctx->dev_bytenr, *mirror_nump,
1308 btrfsic_get_block_type(state,
1310 next_block->logical_bytenr);
1312 pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, %c.\n",
1313 next_bytenr, next_block_ctx->dev->name,
1314 next_block_ctx->dev_bytenr, *mirror_nump,
1315 btrfsic_get_block_type(state,
1318 next_block->logical_bytenr = next_bytenr;
1320 next_block->mirror_num = *mirror_nump;
1321 l = btrfsic_block_link_hashtable_lookup(
1322 next_block_ctx->dev->bdev,
1323 next_block_ctx->dev_bytenr,
1324 block_ctx->dev->bdev,
1325 block_ctx->dev_bytenr,
1326 &state->block_link_hashtable);
1329 next_block->disk_key = *disk_key;
1331 l = btrfsic_block_link_alloc();
1333 pr_info("btrfsic: error, kmalloc failed!\n");
1334 btrfsic_release_block_ctx(next_block_ctx);
1335 *next_blockp = NULL;
1339 did_alloc_block_link = 1;
1340 l->block_ref_to = next_block;
1341 l->block_ref_from = block;
1343 l->parent_generation = parent_generation;
1345 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1346 btrfsic_print_add_link(state, l);
1348 list_add(&l->node_ref_to, &block->ref_to_list);
1349 list_add(&l->node_ref_from, &next_block->ref_from_list);
1351 btrfsic_block_link_hashtable_add(l,
1352 &state->block_link_hashtable);
1354 did_alloc_block_link = 0;
1355 if (0 == limit_nesting) {
1357 l->parent_generation = parent_generation;
1358 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1359 btrfsic_print_add_link(state, l);
1363 if (limit_nesting > 0 && did_alloc_block_link) {
1364 ret = btrfsic_read_block(state, next_block_ctx);
1365 if (ret < (int)next_block_ctx->len) {
1366 pr_info("btrfsic: read block @logical %llu failed!\n",
1368 btrfsic_release_block_ctx(next_block_ctx);
1369 *next_blockp = NULL;
1373 *next_blockp = next_block;
1375 *next_blockp = NULL;
1382 static int btrfsic_handle_extent_data(
1383 struct btrfsic_state *state,
1384 struct btrfsic_block *block,
1385 struct btrfsic_block_data_ctx *block_ctx,
1386 u32 item_offset, int force_iodone_flag)
1388 struct btrfs_fs_info *fs_info = state->fs_info;
1389 struct btrfs_file_extent_item file_extent_item;
1390 u64 file_extent_item_offset;
1394 struct btrfsic_block_link *l;
1397 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1399 if (file_extent_item_offset +
1400 offsetof(struct btrfs_file_extent_item, disk_num_bytes) >
1402 pr_info("btrfsic: file item out of bounce at logical %llu, dev %s\n",
1403 block_ctx->start, block_ctx->dev->name);
1407 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1408 file_extent_item_offset,
1409 offsetof(struct btrfs_file_extent_item, disk_num_bytes));
1410 if (BTRFS_FILE_EXTENT_REG != file_extent_item.type ||
1411 btrfs_stack_file_extent_disk_bytenr(&file_extent_item) == 0) {
1412 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1413 pr_info("extent_data: type %u, disk_bytenr = %llu\n",
1414 file_extent_item.type,
1415 btrfs_stack_file_extent_disk_bytenr(
1416 &file_extent_item));
1420 if (file_extent_item_offset + sizeof(struct btrfs_file_extent_item) >
1422 pr_info("btrfsic: file item out of bounce at logical %llu, dev %s\n",
1423 block_ctx->start, block_ctx->dev->name);
1426 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1427 file_extent_item_offset,
1428 sizeof(struct btrfs_file_extent_item));
1429 next_bytenr = btrfs_stack_file_extent_disk_bytenr(&file_extent_item);
1430 if (btrfs_stack_file_extent_compression(&file_extent_item) ==
1431 BTRFS_COMPRESS_NONE) {
1432 next_bytenr += btrfs_stack_file_extent_offset(&file_extent_item);
1433 num_bytes = btrfs_stack_file_extent_num_bytes(&file_extent_item);
1435 num_bytes = btrfs_stack_file_extent_disk_num_bytes(&file_extent_item);
1437 generation = btrfs_stack_file_extent_generation(&file_extent_item);
1439 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1440 pr_info("extent_data: type %u, disk_bytenr = %llu, offset = %llu, num_bytes = %llu\n",
1441 file_extent_item.type,
1442 btrfs_stack_file_extent_disk_bytenr(&file_extent_item),
1443 btrfs_stack_file_extent_offset(&file_extent_item),
1445 while (num_bytes > 0) {
1450 if (num_bytes > state->datablock_size)
1451 chunk_len = state->datablock_size;
1453 chunk_len = num_bytes;
1455 num_copies = btrfs_num_copies(fs_info, next_bytenr,
1456 state->datablock_size);
1457 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1458 pr_info("num_copies(log_bytenr=%llu) = %d\n",
1459 next_bytenr, num_copies);
1460 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1461 struct btrfsic_block_data_ctx next_block_ctx;
1462 struct btrfsic_block *next_block;
1463 int block_was_created;
1465 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1466 pr_info("btrfsic_handle_extent_data(mirror_num=%d)\n",
1468 if (state->print_mask & BTRFSIC_PRINT_MASK_VERY_VERBOSE)
1469 pr_info("\tdisk_bytenr = %llu, num_bytes %u\n",
1470 next_bytenr, chunk_len);
1471 ret = btrfsic_map_block(state, next_bytenr,
1472 chunk_len, &next_block_ctx,
1475 pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1476 next_bytenr, mirror_num);
1480 next_block = btrfsic_block_lookup_or_add(
1488 &block_was_created);
1489 if (NULL == next_block) {
1490 pr_info("btrfsic: error, kmalloc failed!\n");
1491 btrfsic_release_block_ctx(&next_block_ctx);
1494 if (!block_was_created) {
1495 if ((state->print_mask &
1496 BTRFSIC_PRINT_MASK_VERBOSE) &&
1497 next_block->logical_bytenr != next_bytenr &&
1498 !(!next_block->is_metadata &&
1499 0 == next_block->logical_bytenr)) {
1500 pr_info("Referenced block @%llu (%s/%llu/%d) found in hash table, D, bytenr mismatch (!= stored %llu).\n",
1502 next_block_ctx.dev->name,
1503 next_block_ctx.dev_bytenr,
1505 next_block->logical_bytenr);
1507 next_block->logical_bytenr = next_bytenr;
1508 next_block->mirror_num = mirror_num;
1511 l = btrfsic_block_link_lookup_or_add(state,
1515 btrfsic_release_block_ctx(&next_block_ctx);
1520 next_bytenr += chunk_len;
1521 num_bytes -= chunk_len;
1527 static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1528 struct btrfsic_block_data_ctx *block_ctx_out,
1531 struct btrfs_fs_info *fs_info = state->fs_info;
1534 struct btrfs_bio *multi = NULL;
1535 struct btrfs_device *device;
1538 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
1539 bytenr, &length, &multi, mirror_num);
1542 block_ctx_out->start = 0;
1543 block_ctx_out->dev_bytenr = 0;
1544 block_ctx_out->len = 0;
1545 block_ctx_out->dev = NULL;
1546 block_ctx_out->datav = NULL;
1547 block_ctx_out->pagev = NULL;
1548 block_ctx_out->mem_to_free = NULL;
1553 device = multi->stripes[0].dev;
1554 block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev->bd_dev);
1555 block_ctx_out->dev_bytenr = multi->stripes[0].physical;
1556 block_ctx_out->start = bytenr;
1557 block_ctx_out->len = len;
1558 block_ctx_out->datav = NULL;
1559 block_ctx_out->pagev = NULL;
1560 block_ctx_out->mem_to_free = NULL;
1563 if (NULL == block_ctx_out->dev) {
1565 pr_info("btrfsic: error, cannot lookup dev (#1)!\n");
1571 static void btrfsic_release_block_ctx(struct btrfsic_block_data_ctx *block_ctx)
1573 if (block_ctx->mem_to_free) {
1574 unsigned int num_pages;
1576 BUG_ON(!block_ctx->datav);
1577 BUG_ON(!block_ctx->pagev);
1578 num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
1580 while (num_pages > 0) {
1582 if (block_ctx->datav[num_pages]) {
1583 kunmap(block_ctx->pagev[num_pages]);
1584 block_ctx->datav[num_pages] = NULL;
1586 if (block_ctx->pagev[num_pages]) {
1587 __free_page(block_ctx->pagev[num_pages]);
1588 block_ctx->pagev[num_pages] = NULL;
1592 kfree(block_ctx->mem_to_free);
1593 block_ctx->mem_to_free = NULL;
1594 block_ctx->pagev = NULL;
1595 block_ctx->datav = NULL;
1599 static int btrfsic_read_block(struct btrfsic_state *state,
1600 struct btrfsic_block_data_ctx *block_ctx)
1602 unsigned int num_pages;
1607 BUG_ON(block_ctx->datav);
1608 BUG_ON(block_ctx->pagev);
1609 BUG_ON(block_ctx->mem_to_free);
1610 if (block_ctx->dev_bytenr & ((u64)PAGE_SIZE - 1)) {
1611 pr_info("btrfsic: read_block() with unaligned bytenr %llu\n",
1612 block_ctx->dev_bytenr);
1616 num_pages = (block_ctx->len + (u64)PAGE_SIZE - 1) >>
1618 block_ctx->mem_to_free = kzalloc((sizeof(*block_ctx->datav) +
1619 sizeof(*block_ctx->pagev)) *
1620 num_pages, GFP_NOFS);
1621 if (!block_ctx->mem_to_free)
1623 block_ctx->datav = block_ctx->mem_to_free;
1624 block_ctx->pagev = (struct page **)(block_ctx->datav + num_pages);
1625 for (i = 0; i < num_pages; i++) {
1626 block_ctx->pagev[i] = alloc_page(GFP_NOFS);
1627 if (!block_ctx->pagev[i])
1631 dev_bytenr = block_ctx->dev_bytenr;
1632 for (i = 0; i < num_pages;) {
1636 bio = btrfs_io_bio_alloc(num_pages - i);
1637 bio_set_dev(bio, block_ctx->dev->bdev);
1638 bio->bi_iter.bi_sector = dev_bytenr >> 9;
1639 bio_set_op_attrs(bio, REQ_OP_READ, 0);
1641 for (j = i; j < num_pages; j++) {
1642 ret = bio_add_page(bio, block_ctx->pagev[j],
1644 if (PAGE_SIZE != ret)
1648 pr_info("btrfsic: error, failed to add a single page!\n");
1651 if (submit_bio_wait(bio)) {
1652 pr_info("btrfsic: read error at logical %llu dev %s!\n",
1653 block_ctx->start, block_ctx->dev->name);
1658 dev_bytenr += (j - i) * PAGE_SIZE;
1661 for (i = 0; i < num_pages; i++)
1662 block_ctx->datav[i] = kmap(block_ctx->pagev[i]);
1664 return block_ctx->len;
1667 static void btrfsic_dump_database(struct btrfsic_state *state)
1669 const struct btrfsic_block *b_all;
1671 BUG_ON(NULL == state);
1673 pr_info("all_blocks_list:\n");
1674 list_for_each_entry(b_all, &state->all_blocks_list, all_blocks_node) {
1675 const struct btrfsic_block_link *l;
1677 pr_info("%c-block @%llu (%s/%llu/%d)\n",
1678 btrfsic_get_block_type(state, b_all),
1679 b_all->logical_bytenr, b_all->dev_state->name,
1680 b_all->dev_bytenr, b_all->mirror_num);
1682 list_for_each_entry(l, &b_all->ref_to_list, node_ref_to) {
1683 pr_info(" %c @%llu (%s/%llu/%d) refers %u* to %c @%llu (%s/%llu/%d)\n",
1684 btrfsic_get_block_type(state, b_all),
1685 b_all->logical_bytenr, b_all->dev_state->name,
1686 b_all->dev_bytenr, b_all->mirror_num,
1688 btrfsic_get_block_type(state, l->block_ref_to),
1689 l->block_ref_to->logical_bytenr,
1690 l->block_ref_to->dev_state->name,
1691 l->block_ref_to->dev_bytenr,
1692 l->block_ref_to->mirror_num);
1695 list_for_each_entry(l, &b_all->ref_from_list, node_ref_from) {
1696 pr_info(" %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
1697 btrfsic_get_block_type(state, b_all),
1698 b_all->logical_bytenr, b_all->dev_state->name,
1699 b_all->dev_bytenr, b_all->mirror_num,
1701 btrfsic_get_block_type(state, l->block_ref_from),
1702 l->block_ref_from->logical_bytenr,
1703 l->block_ref_from->dev_state->name,
1704 l->block_ref_from->dev_bytenr,
1705 l->block_ref_from->mirror_num);
1713 * Test whether the disk block contains a tree block (leaf or node)
1714 * (note that this test fails for the super block)
1716 static int btrfsic_test_for_metadata(struct btrfsic_state *state,
1717 char **datav, unsigned int num_pages)
1719 struct btrfs_fs_info *fs_info = state->fs_info;
1720 struct btrfs_header *h;
1721 u8 csum[BTRFS_CSUM_SIZE];
1725 if (num_pages * PAGE_SIZE < state->metablock_size)
1726 return 1; /* not metadata */
1727 num_pages = state->metablock_size >> PAGE_SHIFT;
1728 h = (struct btrfs_header *)datav[0];
1730 if (memcmp(h->fsid, fs_info->fsid, BTRFS_FSID_SIZE))
1733 for (i = 0; i < num_pages; i++) {
1734 u8 *data = i ? datav[i] : (datav[i] + BTRFS_CSUM_SIZE);
1735 size_t sublen = i ? PAGE_SIZE :
1736 (PAGE_SIZE - BTRFS_CSUM_SIZE);
1738 crc = btrfs_crc32c(crc, data, sublen);
1740 btrfs_csum_final(crc, csum);
1741 if (memcmp(csum, h->csum, state->csum_size))
1744 return 0; /* is metadata */
1747 static void btrfsic_process_written_block(struct btrfsic_dev_state *dev_state,
1748 u64 dev_bytenr, char **mapped_datav,
1749 unsigned int num_pages,
1750 struct bio *bio, int *bio_is_patched,
1751 struct buffer_head *bh,
1752 int submit_bio_bh_rw)
1755 struct btrfsic_block *block;
1756 struct btrfsic_block_data_ctx block_ctx;
1758 struct btrfsic_state *state = dev_state->state;
1759 struct block_device *bdev = dev_state->bdev;
1760 unsigned int processed_len;
1762 if (NULL != bio_is_patched)
1763 *bio_is_patched = 0;
1770 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1773 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1774 &state->block_hashtable);
1775 if (NULL != block) {
1777 struct btrfsic_block_link *l, *tmp;
1779 if (block->is_superblock) {
1780 bytenr = btrfs_super_bytenr((struct btrfs_super_block *)
1782 if (num_pages * PAGE_SIZE <
1783 BTRFS_SUPER_INFO_SIZE) {
1784 pr_info("btrfsic: cannot work with too short bios!\n");
1788 BUG_ON(BTRFS_SUPER_INFO_SIZE & (PAGE_SIZE - 1));
1789 processed_len = BTRFS_SUPER_INFO_SIZE;
1790 if (state->print_mask &
1791 BTRFSIC_PRINT_MASK_TREE_BEFORE_SB_WRITE) {
1792 pr_info("[before new superblock is written]:\n");
1793 btrfsic_dump_tree_sub(state, block, 0);
1797 if (!block->is_superblock) {
1798 if (num_pages * PAGE_SIZE <
1799 state->metablock_size) {
1800 pr_info("btrfsic: cannot work with too short bios!\n");
1803 processed_len = state->metablock_size;
1804 bytenr = btrfs_stack_header_bytenr(
1805 (struct btrfs_header *)
1807 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1811 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE) {
1812 if (block->logical_bytenr != bytenr &&
1813 !(!block->is_metadata &&
1814 block->logical_bytenr == 0))
1815 pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c, bytenr mismatch (!= stored %llu).\n",
1816 bytenr, dev_state->name,
1819 btrfsic_get_block_type(state,
1821 block->logical_bytenr);
1823 pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
1824 bytenr, dev_state->name,
1825 dev_bytenr, block->mirror_num,
1826 btrfsic_get_block_type(state,
1829 block->logical_bytenr = bytenr;
1831 if (num_pages * PAGE_SIZE <
1832 state->datablock_size) {
1833 pr_info("btrfsic: cannot work with too short bios!\n");
1836 processed_len = state->datablock_size;
1837 bytenr = block->logical_bytenr;
1838 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1839 pr_info("Written block @%llu (%s/%llu/%d) found in hash table, %c.\n",
1840 bytenr, dev_state->name, dev_bytenr,
1842 btrfsic_get_block_type(state, block));
1845 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1846 pr_info("ref_to_list: %cE, ref_from_list: %cE\n",
1847 list_empty(&block->ref_to_list) ? ' ' : '!',
1848 list_empty(&block->ref_from_list) ? ' ' : '!');
1849 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1850 pr_info("btrfs: attempt to overwrite %c-block @%llu (%s/%llu/%d), old(gen=%llu, objectid=%llu, type=%d, offset=%llu), new(gen=%llu), which is referenced by most recent superblock (superblockgen=%llu)!\n",
1851 btrfsic_get_block_type(state, block), bytenr,
1852 dev_state->name, dev_bytenr, block->mirror_num,
1854 btrfs_disk_key_objectid(&block->disk_key),
1855 block->disk_key.type,
1856 btrfs_disk_key_offset(&block->disk_key),
1857 btrfs_stack_header_generation(
1858 (struct btrfs_header *) mapped_datav[0]),
1859 state->max_superblock_generation);
1860 btrfsic_dump_tree(state);
1863 if (!block->is_iodone && !block->never_written) {
1864 pr_info("btrfs: attempt to overwrite %c-block @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu, which is not yet iodone!\n",
1865 btrfsic_get_block_type(state, block), bytenr,
1866 dev_state->name, dev_bytenr, block->mirror_num,
1868 btrfs_stack_header_generation(
1869 (struct btrfs_header *)
1871 /* it would not be safe to go on */
1872 btrfsic_dump_tree(state);
1877 * Clear all references of this block. Do not free
1878 * the block itself even if is not referenced anymore
1879 * because it still carries valuable information
1880 * like whether it was ever written and IO completed.
1882 list_for_each_entry_safe(l, tmp, &block->ref_to_list,
1884 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
1885 btrfsic_print_rem_link(state, l);
1887 if (0 == l->ref_cnt) {
1888 list_del(&l->node_ref_to);
1889 list_del(&l->node_ref_from);
1890 btrfsic_block_link_hashtable_remove(l);
1891 btrfsic_block_link_free(l);
1895 block_ctx.dev = dev_state;
1896 block_ctx.dev_bytenr = dev_bytenr;
1897 block_ctx.start = bytenr;
1898 block_ctx.len = processed_len;
1899 block_ctx.pagev = NULL;
1900 block_ctx.mem_to_free = NULL;
1901 block_ctx.datav = mapped_datav;
1903 if (is_metadata || state->include_extent_data) {
1904 block->never_written = 0;
1905 block->iodone_w_error = 0;
1907 block->is_iodone = 0;
1908 BUG_ON(NULL == bio_is_patched);
1909 if (!*bio_is_patched) {
1910 block->orig_bio_bh_private =
1912 block->orig_bio_bh_end_io.bio =
1914 block->next_in_same_bio = NULL;
1915 bio->bi_private = block;
1916 bio->bi_end_io = btrfsic_bio_end_io;
1917 *bio_is_patched = 1;
1919 struct btrfsic_block *chained_block =
1920 (struct btrfsic_block *)
1923 BUG_ON(NULL == chained_block);
1924 block->orig_bio_bh_private =
1925 chained_block->orig_bio_bh_private;
1926 block->orig_bio_bh_end_io.bio =
1927 chained_block->orig_bio_bh_end_io.
1929 block->next_in_same_bio = chained_block;
1930 bio->bi_private = block;
1932 } else if (NULL != bh) {
1933 block->is_iodone = 0;
1934 block->orig_bio_bh_private = bh->b_private;
1935 block->orig_bio_bh_end_io.bh = bh->b_end_io;
1936 block->next_in_same_bio = NULL;
1937 bh->b_private = block;
1938 bh->b_end_io = btrfsic_bh_end_io;
1940 block->is_iodone = 1;
1941 block->orig_bio_bh_private = NULL;
1942 block->orig_bio_bh_end_io.bio = NULL;
1943 block->next_in_same_bio = NULL;
1947 block->flush_gen = dev_state->last_flush_gen + 1;
1948 block->submit_bio_bh_rw = submit_bio_bh_rw;
1950 block->logical_bytenr = bytenr;
1951 block->is_metadata = 1;
1952 if (block->is_superblock) {
1954 BTRFS_SUPER_INFO_SIZE);
1955 ret = btrfsic_process_written_superblock(
1958 (struct btrfs_super_block *)
1960 if (state->print_mask &
1961 BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
1962 pr_info("[after new superblock is written]:\n");
1963 btrfsic_dump_tree_sub(state, block, 0);
1966 block->mirror_num = 0; /* unknown */
1967 ret = btrfsic_process_metablock(
1974 pr_info("btrfsic: btrfsic_process_metablock(root @%llu) failed!\n",
1977 block->is_metadata = 0;
1978 block->mirror_num = 0; /* unknown */
1979 block->generation = BTRFSIC_GENERATION_UNKNOWN;
1980 if (!state->include_extent_data
1981 && list_empty(&block->ref_from_list)) {
1983 * disk block is overwritten with extent
1984 * data (not meta data) and we are configured
1985 * to not include extent data: take the
1986 * chance and free the block's memory
1988 btrfsic_block_hashtable_remove(block);
1989 list_del(&block->all_blocks_node);
1990 btrfsic_block_free(block);
1993 btrfsic_release_block_ctx(&block_ctx);
1995 /* block has not been found in hash table */
1999 processed_len = state->datablock_size;
2000 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2001 pr_info("Written block (%s/%llu/?) !found in hash table, D.\n",
2002 dev_state->name, dev_bytenr);
2003 if (!state->include_extent_data) {
2004 /* ignore that written D block */
2008 /* this is getting ugly for the
2009 * include_extent_data case... */
2010 bytenr = 0; /* unknown */
2012 processed_len = state->metablock_size;
2013 bytenr = btrfs_stack_header_bytenr(
2014 (struct btrfs_header *)
2016 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
2018 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2019 pr_info("Written block @%llu (%s/%llu/?) !found in hash table, M.\n",
2020 bytenr, dev_state->name, dev_bytenr);
2023 block_ctx.dev = dev_state;
2024 block_ctx.dev_bytenr = dev_bytenr;
2025 block_ctx.start = bytenr;
2026 block_ctx.len = processed_len;
2027 block_ctx.pagev = NULL;
2028 block_ctx.mem_to_free = NULL;
2029 block_ctx.datav = mapped_datav;
2031 block = btrfsic_block_alloc();
2032 if (NULL == block) {
2033 pr_info("btrfsic: error, kmalloc failed!\n");
2034 btrfsic_release_block_ctx(&block_ctx);
2037 block->dev_state = dev_state;
2038 block->dev_bytenr = dev_bytenr;
2039 block->logical_bytenr = bytenr;
2040 block->is_metadata = is_metadata;
2041 block->never_written = 0;
2042 block->iodone_w_error = 0;
2043 block->mirror_num = 0; /* unknown */
2044 block->flush_gen = dev_state->last_flush_gen + 1;
2045 block->submit_bio_bh_rw = submit_bio_bh_rw;
2047 block->is_iodone = 0;
2048 BUG_ON(NULL == bio_is_patched);
2049 if (!*bio_is_patched) {
2050 block->orig_bio_bh_private = bio->bi_private;
2051 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2052 block->next_in_same_bio = NULL;
2053 bio->bi_private = block;
2054 bio->bi_end_io = btrfsic_bio_end_io;
2055 *bio_is_patched = 1;
2057 struct btrfsic_block *chained_block =
2058 (struct btrfsic_block *)
2061 BUG_ON(NULL == chained_block);
2062 block->orig_bio_bh_private =
2063 chained_block->orig_bio_bh_private;
2064 block->orig_bio_bh_end_io.bio =
2065 chained_block->orig_bio_bh_end_io.bio;
2066 block->next_in_same_bio = chained_block;
2067 bio->bi_private = block;
2069 } else if (NULL != bh) {
2070 block->is_iodone = 0;
2071 block->orig_bio_bh_private = bh->b_private;
2072 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2073 block->next_in_same_bio = NULL;
2074 bh->b_private = block;
2075 bh->b_end_io = btrfsic_bh_end_io;
2077 block->is_iodone = 1;
2078 block->orig_bio_bh_private = NULL;
2079 block->orig_bio_bh_end_io.bio = NULL;
2080 block->next_in_same_bio = NULL;
2082 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2083 pr_info("New written %c-block @%llu (%s/%llu/%d)\n",
2084 is_metadata ? 'M' : 'D',
2085 block->logical_bytenr, block->dev_state->name,
2086 block->dev_bytenr, block->mirror_num);
2087 list_add(&block->all_blocks_node, &state->all_blocks_list);
2088 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2091 ret = btrfsic_process_metablock(state, block,
2094 pr_info("btrfsic: process_metablock(root @%llu) failed!\n",
2097 btrfsic_release_block_ctx(&block_ctx);
2101 BUG_ON(!processed_len);
2102 dev_bytenr += processed_len;
2103 mapped_datav += processed_len >> PAGE_SHIFT;
2104 num_pages -= processed_len >> PAGE_SHIFT;
2108 static void btrfsic_bio_end_io(struct bio *bp)
2110 struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
2113 /* mutex is not held! This is not save if IO is not yet completed
2119 BUG_ON(NULL == block);
2120 bp->bi_private = block->orig_bio_bh_private;
2121 bp->bi_end_io = block->orig_bio_bh_end_io.bio;
2124 struct btrfsic_block *next_block;
2125 struct btrfsic_dev_state *const dev_state = block->dev_state;
2127 if ((dev_state->state->print_mask &
2128 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2129 pr_info("bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
2131 btrfsic_get_block_type(dev_state->state, block),
2132 block->logical_bytenr, dev_state->name,
2133 block->dev_bytenr, block->mirror_num);
2134 next_block = block->next_in_same_bio;
2135 block->iodone_w_error = iodone_w_error;
2136 if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
2137 dev_state->last_flush_gen++;
2138 if ((dev_state->state->print_mask &
2139 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2140 pr_info("bio_end_io() new %s flush_gen=%llu\n",
2142 dev_state->last_flush_gen);
2144 if (block->submit_bio_bh_rw & REQ_FUA)
2145 block->flush_gen = 0; /* FUA completed means block is
2147 block->is_iodone = 1; /* for FLUSH, this releases the block */
2149 } while (NULL != block);
2154 static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
2156 struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
2157 int iodone_w_error = !uptodate;
2158 struct btrfsic_dev_state *dev_state;
2160 BUG_ON(NULL == block);
2161 dev_state = block->dev_state;
2162 if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2163 pr_info("bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
2165 btrfsic_get_block_type(dev_state->state, block),
2166 block->logical_bytenr, block->dev_state->name,
2167 block->dev_bytenr, block->mirror_num);
2169 block->iodone_w_error = iodone_w_error;
2170 if (block->submit_bio_bh_rw & REQ_PREFLUSH) {
2171 dev_state->last_flush_gen++;
2172 if ((dev_state->state->print_mask &
2173 BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
2174 pr_info("bh_end_io() new %s flush_gen=%llu\n",
2175 dev_state->name, dev_state->last_flush_gen);
2177 if (block->submit_bio_bh_rw & REQ_FUA)
2178 block->flush_gen = 0; /* FUA completed means block is on disk */
2180 bh->b_private = block->orig_bio_bh_private;
2181 bh->b_end_io = block->orig_bio_bh_end_io.bh;
2182 block->is_iodone = 1; /* for FLUSH, this releases the block */
2183 bh->b_end_io(bh, uptodate);
2186 static int btrfsic_process_written_superblock(
2187 struct btrfsic_state *state,
2188 struct btrfsic_block *const superblock,
2189 struct btrfs_super_block *const super_hdr)
2191 struct btrfs_fs_info *fs_info = state->fs_info;
2194 superblock->generation = btrfs_super_generation(super_hdr);
2195 if (!(superblock->generation > state->max_superblock_generation ||
2196 0 == state->max_superblock_generation)) {
2197 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2198 pr_info("btrfsic: superblock @%llu (%s/%llu/%d) with old gen %llu <= %llu\n",
2199 superblock->logical_bytenr,
2200 superblock->dev_state->name,
2201 superblock->dev_bytenr, superblock->mirror_num,
2202 btrfs_super_generation(super_hdr),
2203 state->max_superblock_generation);
2205 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
2206 pr_info("btrfsic: got new superblock @%llu (%s/%llu/%d) with new gen %llu > %llu\n",
2207 superblock->logical_bytenr,
2208 superblock->dev_state->name,
2209 superblock->dev_bytenr, superblock->mirror_num,
2210 btrfs_super_generation(super_hdr),
2211 state->max_superblock_generation);
2213 state->max_superblock_generation =
2214 btrfs_super_generation(super_hdr);
2215 state->latest_superblock = superblock;
2218 for (pass = 0; pass < 3; pass++) {
2221 struct btrfsic_block *next_block;
2222 struct btrfsic_block_data_ctx tmp_next_block_ctx;
2223 struct btrfsic_block_link *l;
2226 const char *additional_string = NULL;
2227 struct btrfs_disk_key tmp_disk_key = {0};
2229 btrfs_set_disk_key_objectid(&tmp_disk_key,
2230 BTRFS_ROOT_ITEM_KEY);
2231 btrfs_set_disk_key_objectid(&tmp_disk_key, 0);
2235 btrfs_set_disk_key_objectid(&tmp_disk_key,
2236 BTRFS_ROOT_TREE_OBJECTID);
2237 additional_string = "root ";
2238 next_bytenr = btrfs_super_root(super_hdr);
2239 if (state->print_mask &
2240 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2241 pr_info("root@%llu\n", next_bytenr);
2244 btrfs_set_disk_key_objectid(&tmp_disk_key,
2245 BTRFS_CHUNK_TREE_OBJECTID);
2246 additional_string = "chunk ";
2247 next_bytenr = btrfs_super_chunk_root(super_hdr);
2248 if (state->print_mask &
2249 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2250 pr_info("chunk@%llu\n", next_bytenr);
2253 btrfs_set_disk_key_objectid(&tmp_disk_key,
2254 BTRFS_TREE_LOG_OBJECTID);
2255 additional_string = "log ";
2256 next_bytenr = btrfs_super_log_root(super_hdr);
2257 if (0 == next_bytenr)
2259 if (state->print_mask &
2260 BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
2261 pr_info("log@%llu\n", next_bytenr);
2265 num_copies = btrfs_num_copies(fs_info, next_bytenr,
2266 BTRFS_SUPER_INFO_SIZE);
2267 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2268 pr_info("num_copies(log_bytenr=%llu) = %d\n",
2269 next_bytenr, num_copies);
2270 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2273 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2274 pr_info("btrfsic_process_written_superblock(mirror_num=%d)\n", mirror_num);
2275 ret = btrfsic_map_block(state, next_bytenr,
2276 BTRFS_SUPER_INFO_SIZE,
2277 &tmp_next_block_ctx,
2280 pr_info("btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
2281 next_bytenr, mirror_num);
2285 next_block = btrfsic_block_lookup_or_add(
2287 &tmp_next_block_ctx,
2292 if (NULL == next_block) {
2293 pr_info("btrfsic: error, kmalloc failed!\n");
2294 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2298 next_block->disk_key = tmp_disk_key;
2300 next_block->generation =
2301 BTRFSIC_GENERATION_UNKNOWN;
2302 l = btrfsic_block_link_lookup_or_add(
2304 &tmp_next_block_ctx,
2307 BTRFSIC_GENERATION_UNKNOWN);
2308 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2314 if (WARN_ON(-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)))
2315 btrfsic_dump_tree(state);
2320 static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
2321 struct btrfsic_block *const block,
2322 int recursion_level)
2324 const struct btrfsic_block_link *l;
2327 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2329 * Note that this situation can happen and does not
2330 * indicate an error in regular cases. It happens
2331 * when disk blocks are freed and later reused.
2332 * The check-integrity module is not aware of any
2333 * block free operations, it just recognizes block
2334 * write operations. Therefore it keeps the linkage
2335 * information for a block until a block is
2336 * rewritten. This can temporarily cause incorrect
2337 * and even circular linkage informations. This
2338 * causes no harm unless such blocks are referenced
2339 * by the most recent super block.
2341 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2342 pr_info("btrfsic: abort cyclic linkage (case 1).\n");
2348 * This algorithm is recursive because the amount of used stack
2349 * space is very small and the max recursion depth is limited.
2351 list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
2352 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2353 pr_info("rl=%d, %c @%llu (%s/%llu/%d) %u* refers to %c @%llu (%s/%llu/%d)\n",
2355 btrfsic_get_block_type(state, block),
2356 block->logical_bytenr, block->dev_state->name,
2357 block->dev_bytenr, block->mirror_num,
2359 btrfsic_get_block_type(state, l->block_ref_to),
2360 l->block_ref_to->logical_bytenr,
2361 l->block_ref_to->dev_state->name,
2362 l->block_ref_to->dev_bytenr,
2363 l->block_ref_to->mirror_num);
2364 if (l->block_ref_to->never_written) {
2365 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is never written!\n",
2366 btrfsic_get_block_type(state, l->block_ref_to),
2367 l->block_ref_to->logical_bytenr,
2368 l->block_ref_to->dev_state->name,
2369 l->block_ref_to->dev_bytenr,
2370 l->block_ref_to->mirror_num);
2372 } else if (!l->block_ref_to->is_iodone) {
2373 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is not yet iodone!\n",
2374 btrfsic_get_block_type(state, l->block_ref_to),
2375 l->block_ref_to->logical_bytenr,
2376 l->block_ref_to->dev_state->name,
2377 l->block_ref_to->dev_bytenr,
2378 l->block_ref_to->mirror_num);
2380 } else if (l->block_ref_to->iodone_w_error) {
2381 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which has write error!\n",
2382 btrfsic_get_block_type(state, l->block_ref_to),
2383 l->block_ref_to->logical_bytenr,
2384 l->block_ref_to->dev_state->name,
2385 l->block_ref_to->dev_bytenr,
2386 l->block_ref_to->mirror_num);
2388 } else if (l->parent_generation !=
2389 l->block_ref_to->generation &&
2390 BTRFSIC_GENERATION_UNKNOWN !=
2391 l->parent_generation &&
2392 BTRFSIC_GENERATION_UNKNOWN !=
2393 l->block_ref_to->generation) {
2394 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) with generation %llu != parent generation %llu!\n",
2395 btrfsic_get_block_type(state, l->block_ref_to),
2396 l->block_ref_to->logical_bytenr,
2397 l->block_ref_to->dev_state->name,
2398 l->block_ref_to->dev_bytenr,
2399 l->block_ref_to->mirror_num,
2400 l->block_ref_to->generation,
2401 l->parent_generation);
2403 } else if (l->block_ref_to->flush_gen >
2404 l->block_ref_to->dev_state->last_flush_gen) {
2405 pr_info("btrfs: attempt to write superblock which references block %c @%llu (%s/%llu/%d) which is not flushed out of disk's write cache (block flush_gen=%llu, dev->flush_gen=%llu)!\n",
2406 btrfsic_get_block_type(state, l->block_ref_to),
2407 l->block_ref_to->logical_bytenr,
2408 l->block_ref_to->dev_state->name,
2409 l->block_ref_to->dev_bytenr,
2410 l->block_ref_to->mirror_num, block->flush_gen,
2411 l->block_ref_to->dev_state->last_flush_gen);
2413 } else if (-1 == btrfsic_check_all_ref_blocks(state,
2424 static int btrfsic_is_block_ref_by_superblock(
2425 const struct btrfsic_state *state,
2426 const struct btrfsic_block *block,
2427 int recursion_level)
2429 const struct btrfsic_block_link *l;
2431 if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
2432 /* refer to comment at "abort cyclic linkage (case 1)" */
2433 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2434 pr_info("btrfsic: abort cyclic linkage (case 2).\n");
2440 * This algorithm is recursive because the amount of used stack space
2441 * is very small and the max recursion depth is limited.
2443 list_for_each_entry(l, &block->ref_from_list, node_ref_from) {
2444 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2445 pr_info("rl=%d, %c @%llu (%s/%llu/%d) is ref %u* from %c @%llu (%s/%llu/%d)\n",
2447 btrfsic_get_block_type(state, block),
2448 block->logical_bytenr, block->dev_state->name,
2449 block->dev_bytenr, block->mirror_num,
2451 btrfsic_get_block_type(state, l->block_ref_from),
2452 l->block_ref_from->logical_bytenr,
2453 l->block_ref_from->dev_state->name,
2454 l->block_ref_from->dev_bytenr,
2455 l->block_ref_from->mirror_num);
2456 if (l->block_ref_from->is_superblock &&
2457 state->latest_superblock->dev_bytenr ==
2458 l->block_ref_from->dev_bytenr &&
2459 state->latest_superblock->dev_state->bdev ==
2460 l->block_ref_from->dev_state->bdev)
2462 else if (btrfsic_is_block_ref_by_superblock(state,
2472 static void btrfsic_print_add_link(const struct btrfsic_state *state,
2473 const struct btrfsic_block_link *l)
2475 pr_info("Add %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
2477 btrfsic_get_block_type(state, l->block_ref_from),
2478 l->block_ref_from->logical_bytenr,
2479 l->block_ref_from->dev_state->name,
2480 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2481 btrfsic_get_block_type(state, l->block_ref_to),
2482 l->block_ref_to->logical_bytenr,
2483 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2484 l->block_ref_to->mirror_num);
2487 static void btrfsic_print_rem_link(const struct btrfsic_state *state,
2488 const struct btrfsic_block_link *l)
2490 pr_info("Rem %u* link from %c @%llu (%s/%llu/%d) to %c @%llu (%s/%llu/%d).\n",
2492 btrfsic_get_block_type(state, l->block_ref_from),
2493 l->block_ref_from->logical_bytenr,
2494 l->block_ref_from->dev_state->name,
2495 l->block_ref_from->dev_bytenr, l->block_ref_from->mirror_num,
2496 btrfsic_get_block_type(state, l->block_ref_to),
2497 l->block_ref_to->logical_bytenr,
2498 l->block_ref_to->dev_state->name, l->block_ref_to->dev_bytenr,
2499 l->block_ref_to->mirror_num);
2502 static char btrfsic_get_block_type(const struct btrfsic_state *state,
2503 const struct btrfsic_block *block)
2505 if (block->is_superblock &&
2506 state->latest_superblock->dev_bytenr == block->dev_bytenr &&
2507 state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
2509 else if (block->is_superblock)
2511 else if (block->is_metadata)
2517 static void btrfsic_dump_tree(const struct btrfsic_state *state)
2519 btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
2522 static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
2523 const struct btrfsic_block *block,
2526 const struct btrfsic_block_link *l;
2528 static char buf[80];
2529 int cursor_position;
2532 * Should better fill an on-stack buffer with a complete line and
2533 * dump it at once when it is time to print a newline character.
2537 * This algorithm is recursive because the amount of used stack space
2538 * is very small and the max recursion depth is limited.
2540 indent_add = sprintf(buf, "%c-%llu(%s/%llu/%u)",
2541 btrfsic_get_block_type(state, block),
2542 block->logical_bytenr, block->dev_state->name,
2543 block->dev_bytenr, block->mirror_num);
2544 if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2549 indent_level += indent_add;
2550 if (list_empty(&block->ref_to_list)) {
2554 if (block->mirror_num > 1 &&
2555 !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
2560 cursor_position = indent_level;
2561 list_for_each_entry(l, &block->ref_to_list, node_ref_to) {
2562 while (cursor_position < indent_level) {
2567 indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
2569 indent_add = sprintf(buf, " --> ");
2570 if (indent_level + indent_add >
2571 BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
2573 cursor_position = 0;
2579 btrfsic_dump_tree_sub(state, l->block_ref_to,
2580 indent_level + indent_add);
2581 cursor_position = 0;
2585 static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
2586 struct btrfsic_state *state,
2587 struct btrfsic_block_data_ctx *next_block_ctx,
2588 struct btrfsic_block *next_block,
2589 struct btrfsic_block *from_block,
2590 u64 parent_generation)
2592 struct btrfsic_block_link *l;
2594 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
2595 next_block_ctx->dev_bytenr,
2596 from_block->dev_state->bdev,
2597 from_block->dev_bytenr,
2598 &state->block_link_hashtable);
2600 l = btrfsic_block_link_alloc();
2602 pr_info("btrfsic: error, kmalloc failed!\n");
2606 l->block_ref_to = next_block;
2607 l->block_ref_from = from_block;
2609 l->parent_generation = parent_generation;
2611 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2612 btrfsic_print_add_link(state, l);
2614 list_add(&l->node_ref_to, &from_block->ref_to_list);
2615 list_add(&l->node_ref_from, &next_block->ref_from_list);
2617 btrfsic_block_link_hashtable_add(l,
2618 &state->block_link_hashtable);
2621 l->parent_generation = parent_generation;
2622 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2623 btrfsic_print_add_link(state, l);
2629 static struct btrfsic_block *btrfsic_block_lookup_or_add(
2630 struct btrfsic_state *state,
2631 struct btrfsic_block_data_ctx *block_ctx,
2632 const char *additional_string,
2639 struct btrfsic_block *block;
2641 block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
2642 block_ctx->dev_bytenr,
2643 &state->block_hashtable);
2644 if (NULL == block) {
2645 struct btrfsic_dev_state *dev_state;
2647 block = btrfsic_block_alloc();
2648 if (NULL == block) {
2649 pr_info("btrfsic: error, kmalloc failed!\n");
2652 dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev->bd_dev);
2653 if (NULL == dev_state) {
2654 pr_info("btrfsic: error, lookup dev_state failed!\n");
2655 btrfsic_block_free(block);
2658 block->dev_state = dev_state;
2659 block->dev_bytenr = block_ctx->dev_bytenr;
2660 block->logical_bytenr = block_ctx->start;
2661 block->is_metadata = is_metadata;
2662 block->is_iodone = is_iodone;
2663 block->never_written = never_written;
2664 block->mirror_num = mirror_num;
2665 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
2666 pr_info("New %s%c-block @%llu (%s/%llu/%d)\n",
2668 btrfsic_get_block_type(state, block),
2669 block->logical_bytenr, dev_state->name,
2670 block->dev_bytenr, mirror_num);
2671 list_add(&block->all_blocks_node, &state->all_blocks_list);
2672 btrfsic_block_hashtable_add(block, &state->block_hashtable);
2673 if (NULL != was_created)
2676 if (NULL != was_created)
2683 static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2685 struct btrfsic_dev_state *dev_state,
2688 struct btrfs_fs_info *fs_info = state->fs_info;
2689 struct btrfsic_block_data_ctx block_ctx;
2695 num_copies = btrfs_num_copies(fs_info, bytenr, state->metablock_size);
2697 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2698 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
2699 &block_ctx, mirror_num);
2701 pr_info("btrfsic: btrfsic_map_block(logical @%llu, mirror %d) failed!\n",
2702 bytenr, mirror_num);
2706 if (dev_state->bdev == block_ctx.dev->bdev &&
2707 dev_bytenr == block_ctx.dev_bytenr) {
2709 btrfsic_release_block_ctx(&block_ctx);
2712 btrfsic_release_block_ctx(&block_ctx);
2715 if (WARN_ON(!match)) {
2716 pr_info("btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio, buffer->log_bytenr=%llu, submit_bio(bdev=%s, phys_bytenr=%llu)!\n",
2717 bytenr, dev_state->name, dev_bytenr);
2718 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2719 ret = btrfsic_map_block(state, bytenr,
2720 state->metablock_size,
2721 &block_ctx, mirror_num);
2725 pr_info("Read logical bytenr @%llu maps to (%s/%llu/%d)\n",
2726 bytenr, block_ctx.dev->name,
2727 block_ctx.dev_bytenr, mirror_num);
2732 static struct btrfsic_dev_state *btrfsic_dev_state_lookup(dev_t dev)
2734 return btrfsic_dev_state_hashtable_lookup(dev,
2735 &btrfsic_dev_state_hashtable);
2738 int btrfsic_submit_bh(int op, int op_flags, struct buffer_head *bh)
2740 struct btrfsic_dev_state *dev_state;
2742 if (!btrfsic_is_initialized)
2743 return submit_bh(op, op_flags, bh);
2745 mutex_lock(&btrfsic_mutex);
2746 /* since btrfsic_submit_bh() might also be called before
2747 * btrfsic_mount(), this might return NULL */
2748 dev_state = btrfsic_dev_state_lookup(bh->b_bdev->bd_dev);
2750 /* Only called to write the superblock (incl. FLUSH/FUA) */
2751 if (NULL != dev_state &&
2752 (op == REQ_OP_WRITE) && bh->b_size > 0) {
2755 dev_bytenr = BTRFS_BDEV_BLOCKSIZE * bh->b_blocknr;
2756 if (dev_state->state->print_mask &
2757 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2758 pr_info("submit_bh(op=0x%x,0x%x, blocknr=%llu (bytenr %llu), size=%zu, data=%p, bdev=%p)\n",
2759 op, op_flags, (unsigned long long)bh->b_blocknr,
2760 dev_bytenr, bh->b_size, bh->b_data, bh->b_bdev);
2761 btrfsic_process_written_block(dev_state, dev_bytenr,
2762 &bh->b_data, 1, NULL,
2763 NULL, bh, op_flags);
2764 } else if (NULL != dev_state && (op_flags & REQ_PREFLUSH)) {
2765 if (dev_state->state->print_mask &
2766 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2767 pr_info("submit_bh(op=0x%x,0x%x FLUSH, bdev=%p)\n",
2768 op, op_flags, bh->b_bdev);
2769 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2770 if ((dev_state->state->print_mask &
2771 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2772 BTRFSIC_PRINT_MASK_VERBOSE)))
2773 pr_info("btrfsic_submit_bh(%s) with FLUSH but dummy block already in use (ignored)!\n",
2776 struct btrfsic_block *const block =
2777 &dev_state->dummy_block_for_bio_bh_flush;
2779 block->is_iodone = 0;
2780 block->never_written = 0;
2781 block->iodone_w_error = 0;
2782 block->flush_gen = dev_state->last_flush_gen + 1;
2783 block->submit_bio_bh_rw = op_flags;
2784 block->orig_bio_bh_private = bh->b_private;
2785 block->orig_bio_bh_end_io.bh = bh->b_end_io;
2786 block->next_in_same_bio = NULL;
2787 bh->b_private = block;
2788 bh->b_end_io = btrfsic_bh_end_io;
2791 mutex_unlock(&btrfsic_mutex);
2792 return submit_bh(op, op_flags, bh);
2795 static void __btrfsic_submit_bio(struct bio *bio)
2797 struct btrfsic_dev_state *dev_state;
2799 if (!btrfsic_is_initialized)
2802 mutex_lock(&btrfsic_mutex);
2803 /* since btrfsic_submit_bio() is also called before
2804 * btrfsic_mount(), this might return NULL */
2805 dev_state = btrfsic_dev_state_lookup(bio_dev(bio));
2806 if (NULL != dev_state &&
2807 (bio_op(bio) == REQ_OP_WRITE) && bio_has_data(bio)) {
2811 struct bio_vec bvec;
2812 struct bvec_iter iter;
2814 char **mapped_datav;
2815 unsigned int segs = bio_segments(bio);
2817 dev_bytenr = 512 * bio->bi_iter.bi_sector;
2819 if (dev_state->state->print_mask &
2820 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2821 pr_info("submit_bio(rw=%d,0x%x, bi_vcnt=%u, bi_sector=%llu (bytenr %llu), bi_disk=%p)\n",
2822 bio_op(bio), bio->bi_opf, segs,
2823 (unsigned long long)bio->bi_iter.bi_sector,
2824 dev_bytenr, bio->bi_disk);
2826 mapped_datav = kmalloc_array(segs,
2827 sizeof(*mapped_datav), GFP_NOFS);
2830 cur_bytenr = dev_bytenr;
2832 bio_for_each_segment(bvec, bio, iter) {
2833 BUG_ON(bvec.bv_len != PAGE_SIZE);
2834 mapped_datav[i] = kmap(bvec.bv_page);
2837 if (dev_state->state->print_mask &
2838 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH_VERBOSE)
2839 pr_info("#%u: bytenr=%llu, len=%u, offset=%u\n",
2840 i, cur_bytenr, bvec.bv_len, bvec.bv_offset);
2841 cur_bytenr += bvec.bv_len;
2843 btrfsic_process_written_block(dev_state, dev_bytenr,
2845 bio, &bio_is_patched,
2847 bio_for_each_segment(bvec, bio, iter)
2848 kunmap(bvec.bv_page);
2849 kfree(mapped_datav);
2850 } else if (NULL != dev_state && (bio->bi_opf & REQ_PREFLUSH)) {
2851 if (dev_state->state->print_mask &
2852 BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
2853 pr_info("submit_bio(rw=%d,0x%x FLUSH, disk=%p)\n",
2854 bio_op(bio), bio->bi_opf, bio->bi_disk);
2855 if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
2856 if ((dev_state->state->print_mask &
2857 (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
2858 BTRFSIC_PRINT_MASK_VERBOSE)))
2859 pr_info("btrfsic_submit_bio(%s) with FLUSH but dummy block already in use (ignored)!\n",
2862 struct btrfsic_block *const block =
2863 &dev_state->dummy_block_for_bio_bh_flush;
2865 block->is_iodone = 0;
2866 block->never_written = 0;
2867 block->iodone_w_error = 0;
2868 block->flush_gen = dev_state->last_flush_gen + 1;
2869 block->submit_bio_bh_rw = bio->bi_opf;
2870 block->orig_bio_bh_private = bio->bi_private;
2871 block->orig_bio_bh_end_io.bio = bio->bi_end_io;
2872 block->next_in_same_bio = NULL;
2873 bio->bi_private = block;
2874 bio->bi_end_io = btrfsic_bio_end_io;
2878 mutex_unlock(&btrfsic_mutex);
2881 void btrfsic_submit_bio(struct bio *bio)
2883 __btrfsic_submit_bio(bio);
2887 int btrfsic_submit_bio_wait(struct bio *bio)
2889 __btrfsic_submit_bio(bio);
2890 return submit_bio_wait(bio);
2893 int btrfsic_mount(struct btrfs_fs_info *fs_info,
2894 struct btrfs_fs_devices *fs_devices,
2895 int including_extent_data, u32 print_mask)
2898 struct btrfsic_state *state;
2899 struct list_head *dev_head = &fs_devices->devices;
2900 struct btrfs_device *device;
2902 if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) {
2903 pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n",
2904 fs_info->nodesize, PAGE_SIZE);
2907 if (fs_info->sectorsize & ((u64)PAGE_SIZE - 1)) {
2908 pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n",
2909 fs_info->sectorsize, PAGE_SIZE);
2912 state = kvzalloc(sizeof(*state), GFP_KERNEL);
2914 pr_info("btrfs check-integrity: allocation failed!\n");
2918 if (!btrfsic_is_initialized) {
2919 mutex_init(&btrfsic_mutex);
2920 btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
2921 btrfsic_is_initialized = 1;
2923 mutex_lock(&btrfsic_mutex);
2924 state->fs_info = fs_info;
2925 state->print_mask = print_mask;
2926 state->include_extent_data = including_extent_data;
2927 state->csum_size = 0;
2928 state->metablock_size = fs_info->nodesize;
2929 state->datablock_size = fs_info->sectorsize;
2930 INIT_LIST_HEAD(&state->all_blocks_list);
2931 btrfsic_block_hashtable_init(&state->block_hashtable);
2932 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
2933 state->max_superblock_generation = 0;
2934 state->latest_superblock = NULL;
2936 list_for_each_entry(device, dev_head, dev_list) {
2937 struct btrfsic_dev_state *ds;
2940 if (!device->bdev || !device->name)
2943 ds = btrfsic_dev_state_alloc();
2945 pr_info("btrfs check-integrity: kmalloc() failed!\n");
2946 mutex_unlock(&btrfsic_mutex);
2949 ds->bdev = device->bdev;
2951 bdevname(ds->bdev, ds->name);
2952 ds->name[BDEVNAME_SIZE - 1] = '\0';
2953 p = kbasename(ds->name);
2954 strlcpy(ds->name, p, sizeof(ds->name));
2955 btrfsic_dev_state_hashtable_add(ds,
2956 &btrfsic_dev_state_hashtable);
2959 ret = btrfsic_process_superblock(state, fs_devices);
2961 mutex_unlock(&btrfsic_mutex);
2962 btrfsic_unmount(fs_devices);
2966 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
2967 btrfsic_dump_database(state);
2968 if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
2969 btrfsic_dump_tree(state);
2971 mutex_unlock(&btrfsic_mutex);
2975 void btrfsic_unmount(struct btrfs_fs_devices *fs_devices)
2977 struct btrfsic_block *b_all, *tmp_all;
2978 struct btrfsic_state *state;
2979 struct list_head *dev_head = &fs_devices->devices;
2980 struct btrfs_device *device;
2982 if (!btrfsic_is_initialized)
2985 mutex_lock(&btrfsic_mutex);
2988 list_for_each_entry(device, dev_head, dev_list) {
2989 struct btrfsic_dev_state *ds;
2991 if (!device->bdev || !device->name)
2994 ds = btrfsic_dev_state_hashtable_lookup(
2995 device->bdev->bd_dev,
2996 &btrfsic_dev_state_hashtable);
2999 btrfsic_dev_state_hashtable_remove(ds);
3000 btrfsic_dev_state_free(ds);
3004 if (NULL == state) {
3005 pr_info("btrfsic: error, cannot find state information on umount!\n");
3006 mutex_unlock(&btrfsic_mutex);
3011 * Don't care about keeping the lists' state up to date,
3012 * just free all memory that was allocated dynamically.
3013 * Free the blocks and the block_links.
3015 list_for_each_entry_safe(b_all, tmp_all, &state->all_blocks_list,
3017 struct btrfsic_block_link *l, *tmp;
3019 list_for_each_entry_safe(l, tmp, &b_all->ref_to_list,
3021 if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
3022 btrfsic_print_rem_link(state, l);
3025 if (0 == l->ref_cnt)
3026 btrfsic_block_link_free(l);
3029 if (b_all->is_iodone || b_all->never_written)
3030 btrfsic_block_free(b_all);
3032 pr_info("btrfs: attempt to free %c-block @%llu (%s/%llu/%d) on umount which is not yet iodone!\n",
3033 btrfsic_get_block_type(state, b_all),
3034 b_all->logical_bytenr, b_all->dev_state->name,
3035 b_all->dev_bytenr, b_all->mirror_num);
3038 mutex_unlock(&btrfsic_mutex);