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