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