Skip to content
Snippets Groups Projects
check-integrity.c 103 KiB
Newer Older
  • Learn to ignore specific revisions
  • 					    mapped_datav[0])->generation),
    
    			       (unsigned long long)
    			       state->max_superblock_generation);
    			btrfsic_dump_tree(state);
    		}
    
    		if (!block->is_iodone && !block->never_written) {
    			printk(KERN_INFO "btrfs: attempt to overwrite %c-block"
    			       " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
    			       " which is not yet iodone!\n",
    			       btrfsic_get_block_type(state, block),
    			       (unsigned long long)bytenr,
    			       dev_state->name,
    			       (unsigned long long)dev_bytenr,
    			       block->mirror_num,
    			       (unsigned long long)block->generation,
    			       (unsigned long long)
    			       le64_to_cpu(((struct btrfs_header *)
    
    					    mapped_datav[0])->generation));
    
    			/* it would not be safe to go on */
    			btrfsic_dump_tree(state);
    
    		}
    
    		/*
    		 * Clear all references of this block. Do not free
    		 * the block itself even if is not referenced anymore
    		 * because it still carries valueable information
    		 * like whether it was ever written and IO completed.
    		 */
    		list_for_each_safe(elem_ref_to, tmp_ref_to,
    				   &block->ref_to_list) {
    			struct btrfsic_block_link *const l =
    			    list_entry(elem_ref_to,
    				       struct btrfsic_block_link,
    				       node_ref_to);
    
    			if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    				btrfsic_print_rem_link(state, l);
    			l->ref_cnt--;
    			if (0 == l->ref_cnt) {
    				list_del(&l->node_ref_to);
    				list_del(&l->node_ref_from);
    				btrfsic_block_link_hashtable_remove(l);
    				btrfsic_block_link_free(l);
    			}
    		}
    
    		if (block->is_superblock)
    
    			ret = btrfsic_map_superblock(state, bytenr,
    						     processed_len,
    
    						     bdev, &block_ctx);
    		else
    
    			ret = btrfsic_map_block(state, bytenr, processed_len,
    
    						&block_ctx, 0);
    		if (ret) {
    			printk(KERN_INFO
    			       "btrfsic: btrfsic_map_block(root @%llu)"
    			       " failed!\n", (unsigned long long)bytenr);
    
    		block_ctx.datav = mapped_datav;
    
    		/* the following is required in case of writes to mirrors,
    		 * use the same that was used for the lookup */
    		block_ctx.dev = dev_state;
    		block_ctx.dev_bytenr = dev_bytenr;
    
    		if (is_metadata || state->include_extent_data) {
    			block->never_written = 0;
    			block->iodone_w_error = 0;
    			if (NULL != bio) {
    				block->is_iodone = 0;
    				BUG_ON(NULL == bio_is_patched);
    				if (!*bio_is_patched) {
    					block->orig_bio_bh_private =
    					    bio->bi_private;
    					block->orig_bio_bh_end_io.bio =
    					    bio->bi_end_io;
    					block->next_in_same_bio = NULL;
    					bio->bi_private = block;
    					bio->bi_end_io = btrfsic_bio_end_io;
    					*bio_is_patched = 1;
    				} else {
    					struct btrfsic_block *chained_block =
    					    (struct btrfsic_block *)
    					    bio->bi_private;
    
    					BUG_ON(NULL == chained_block);
    					block->orig_bio_bh_private =
    					    chained_block->orig_bio_bh_private;
    					block->orig_bio_bh_end_io.bio =
    					    chained_block->orig_bio_bh_end_io.
    					    bio;
    					block->next_in_same_bio = chained_block;
    					bio->bi_private = block;
    				}
    			} else if (NULL != bh) {
    				block->is_iodone = 0;
    				block->orig_bio_bh_private = bh->b_private;
    				block->orig_bio_bh_end_io.bh = bh->b_end_io;
    				block->next_in_same_bio = NULL;
    				bh->b_private = block;
    				bh->b_end_io = btrfsic_bh_end_io;
    			} else {
    				block->is_iodone = 1;
    				block->orig_bio_bh_private = NULL;
    				block->orig_bio_bh_end_io.bio = NULL;
    				block->next_in_same_bio = NULL;
    			}
    		}
    
    		block->flush_gen = dev_state->last_flush_gen + 1;
    		block->submit_bio_bh_rw = submit_bio_bh_rw;
    		if (is_metadata) {
    			block->logical_bytenr = bytenr;
    			block->is_metadata = 1;
    			if (block->is_superblock) {
    
    				BUG_ON(PAGE_CACHE_SIZE !=
    				       BTRFS_SUPER_INFO_SIZE);
    
    				ret = btrfsic_process_written_superblock(
    						state,
    						block,
    						(struct btrfs_super_block *)
    
    				if (state->print_mask &
    				    BTRFSIC_PRINT_MASK_TREE_AFTER_SB_WRITE) {
    					printk(KERN_INFO
    					"[after new superblock is written]:\n");
    					btrfsic_dump_tree_sub(state, block, 0);
    				}
    			} else {
    				block->mirror_num = 0;	/* unknown */
    				ret = btrfsic_process_metablock(
    						state,
    						block,
    						&block_ctx,
    						0, 0);
    			}
    			if (ret)
    				printk(KERN_INFO
    				       "btrfsic: btrfsic_process_metablock"
    				       "(root @%llu) failed!\n",
    				       (unsigned long long)dev_bytenr);
    		} else {
    			block->is_metadata = 0;
    			block->mirror_num = 0;	/* unknown */
    			block->generation = BTRFSIC_GENERATION_UNKNOWN;
    			if (!state->include_extent_data
    			    && list_empty(&block->ref_from_list)) {
    				/*
    				 * disk block is overwritten with extent
    				 * data (not meta data) and we are configured
    				 * to not include extent data: take the
    				 * chance and free the block's memory
    				 */
    				btrfsic_block_hashtable_remove(block);
    				list_del(&block->all_blocks_node);
    				btrfsic_block_free(block);
    			}
    		}
    		btrfsic_release_block_ctx(&block_ctx);
    	} else {
    		/* block has not been found in hash table */
    		u64 bytenr;
    
    		if (!is_metadata) {
    
    			processed_len = state->datablock_size;
    
    			if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    				printk(KERN_INFO "Written block (%s/%llu/?)"
    				       " !found in hash table, D.\n",
    				       dev_state->name,
    				       (unsigned long long)dev_bytenr);
    
    			if (!state->include_extent_data) {
    				/* ignore that written D block */
    				goto continue_loop;
    			}
    
    
    			/* this is getting ugly for the
    			 * include_extent_data case... */
    			bytenr = 0;	/* unknown */
    			block_ctx.start = bytenr;
    
    			block_ctx.len = processed_len;
    			block_ctx.mem_to_free = NULL;
    			block_ctx.pagev = NULL;
    
    			processed_len = state->metablock_size;
    
    			bytenr = le64_to_cpu(((struct btrfs_header *)
    
    					      mapped_datav[0])->bytenr);
    
    			btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
    
    			if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    				printk(KERN_INFO
    				       "Written block @%llu (%s/%llu/?)"
    				       " !found in hash table, M.\n",
    				       (unsigned long long)bytenr,
    				       dev_state->name,
    				       (unsigned long long)dev_bytenr);
    
    
    			ret = btrfsic_map_block(state, bytenr, processed_len,
    						&block_ctx, 0);
    
    			if (ret) {
    				printk(KERN_INFO
    				       "btrfsic: btrfsic_map_block(root @%llu)"
    				       " failed!\n",
    				       (unsigned long long)dev_bytenr);
    
    		block_ctx.datav = mapped_datav;
    
    		/* the following is required in case of writes to mirrors,
    		 * use the same that was used for the lookup */
    		block_ctx.dev = dev_state;
    		block_ctx.dev_bytenr = dev_bytenr;
    
    		block = btrfsic_block_alloc();
    		if (NULL == block) {
    			printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
    			btrfsic_release_block_ctx(&block_ctx);
    
    		}
    		block->dev_state = dev_state;
    		block->dev_bytenr = dev_bytenr;
    		block->logical_bytenr = bytenr;
    		block->is_metadata = is_metadata;
    		block->never_written = 0;
    		block->iodone_w_error = 0;
    		block->mirror_num = 0;	/* unknown */
    		block->flush_gen = dev_state->last_flush_gen + 1;
    		block->submit_bio_bh_rw = submit_bio_bh_rw;
    		if (NULL != bio) {
    			block->is_iodone = 0;
    			BUG_ON(NULL == bio_is_patched);
    			if (!*bio_is_patched) {
    				block->orig_bio_bh_private = bio->bi_private;
    				block->orig_bio_bh_end_io.bio = bio->bi_end_io;
    				block->next_in_same_bio = NULL;
    				bio->bi_private = block;
    				bio->bi_end_io = btrfsic_bio_end_io;
    				*bio_is_patched = 1;
    			} else {
    				struct btrfsic_block *chained_block =
    				    (struct btrfsic_block *)
    				    bio->bi_private;
    
    				BUG_ON(NULL == chained_block);
    				block->orig_bio_bh_private =
    				    chained_block->orig_bio_bh_private;
    				block->orig_bio_bh_end_io.bio =
    				    chained_block->orig_bio_bh_end_io.bio;
    				block->next_in_same_bio = chained_block;
    				bio->bi_private = block;
    			}
    		} else if (NULL != bh) {
    			block->is_iodone = 0;
    			block->orig_bio_bh_private = bh->b_private;
    			block->orig_bio_bh_end_io.bh = bh->b_end_io;
    			block->next_in_same_bio = NULL;
    			bh->b_private = block;
    			bh->b_end_io = btrfsic_bh_end_io;
    		} else {
    			block->is_iodone = 1;
    			block->orig_bio_bh_private = NULL;
    			block->orig_bio_bh_end_io.bio = NULL;
    			block->next_in_same_bio = NULL;
    		}
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			printk(KERN_INFO
    			       "New written %c-block @%llu (%s/%llu/%d)\n",
    			       is_metadata ? 'M' : 'D',
    			       (unsigned long long)block->logical_bytenr,
    			       block->dev_state->name,
    			       (unsigned long long)block->dev_bytenr,
    			       block->mirror_num);
    		list_add(&block->all_blocks_node, &state->all_blocks_list);
    		btrfsic_block_hashtable_add(block, &state->block_hashtable);
    
    		if (is_metadata) {
    			ret = btrfsic_process_metablock(state, block,
    
    			if (ret)
    				printk(KERN_INFO
    				       "btrfsic: process_metablock(root @%llu)"
    				       " failed!\n",
    				       (unsigned long long)dev_bytenr);
    		}
    		btrfsic_release_block_ctx(&block_ctx);
    	}
    
    
    continue_loop:
    	BUG_ON(!processed_len);
    	dev_bytenr += processed_len;
    	mapped_datav += processed_len >> PAGE_CACHE_SHIFT;
    	num_pages -= processed_len >> PAGE_CACHE_SHIFT;
    	goto again;
    
    }
    
    static void btrfsic_bio_end_io(struct bio *bp, int bio_error_status)
    {
    	struct btrfsic_block *block = (struct btrfsic_block *)bp->bi_private;
    	int iodone_w_error;
    
    	/* mutex is not held! This is not save if IO is not yet completed
    	 * on umount */
    	iodone_w_error = 0;
    	if (bio_error_status)
    		iodone_w_error = 1;
    
    	BUG_ON(NULL == block);
    	bp->bi_private = block->orig_bio_bh_private;
    	bp->bi_end_io = block->orig_bio_bh_end_io.bio;
    
    	do {
    		struct btrfsic_block *next_block;
    		struct btrfsic_dev_state *const dev_state = block->dev_state;
    
    		if ((dev_state->state->print_mask &
    		     BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
    			printk(KERN_INFO
    			       "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
    			       bio_error_status,
    			       btrfsic_get_block_type(dev_state->state, block),
    			       (unsigned long long)block->logical_bytenr,
    			       dev_state->name,
    			       (unsigned long long)block->dev_bytenr,
    			       block->mirror_num);
    		next_block = block->next_in_same_bio;
    		block->iodone_w_error = iodone_w_error;
    		if (block->submit_bio_bh_rw & REQ_FLUSH) {
    			dev_state->last_flush_gen++;
    			if ((dev_state->state->print_mask &
    			     BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
    				printk(KERN_INFO
    				       "bio_end_io() new %s flush_gen=%llu\n",
    				       dev_state->name,
    				       (unsigned long long)
    				       dev_state->last_flush_gen);
    		}
    		if (block->submit_bio_bh_rw & REQ_FUA)
    			block->flush_gen = 0; /* FUA completed means block is
    					       * on disk */
    		block->is_iodone = 1; /* for FLUSH, this releases the block */
    		block = next_block;
    	} while (NULL != block);
    
    	bp->bi_end_io(bp, bio_error_status);
    }
    
    static void btrfsic_bh_end_io(struct buffer_head *bh, int uptodate)
    {
    	struct btrfsic_block *block = (struct btrfsic_block *)bh->b_private;
    	int iodone_w_error = !uptodate;
    	struct btrfsic_dev_state *dev_state;
    
    	BUG_ON(NULL == block);
    	dev_state = block->dev_state;
    	if ((dev_state->state->print_mask & BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
    		printk(KERN_INFO
    		       "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
    		       iodone_w_error,
    		       btrfsic_get_block_type(dev_state->state, block),
    		       (unsigned long long)block->logical_bytenr,
    		       block->dev_state->name,
    		       (unsigned long long)block->dev_bytenr,
    		       block->mirror_num);
    
    	block->iodone_w_error = iodone_w_error;
    	if (block->submit_bio_bh_rw & REQ_FLUSH) {
    		dev_state->last_flush_gen++;
    		if ((dev_state->state->print_mask &
    		     BTRFSIC_PRINT_MASK_END_IO_BIO_BH))
    			printk(KERN_INFO
    			       "bh_end_io() new %s flush_gen=%llu\n",
    			       dev_state->name,
    			       (unsigned long long)dev_state->last_flush_gen);
    	}
    	if (block->submit_bio_bh_rw & REQ_FUA)
    		block->flush_gen = 0; /* FUA completed means block is on disk */
    
    	bh->b_private = block->orig_bio_bh_private;
    	bh->b_end_io = block->orig_bio_bh_end_io.bh;
    	block->is_iodone = 1; /* for FLUSH, this releases the block */
    	bh->b_end_io(bh, uptodate);
    }
    
    static int btrfsic_process_written_superblock(
    		struct btrfsic_state *state,
    		struct btrfsic_block *const superblock,
    		struct btrfs_super_block *const super_hdr)
    {
    	int pass;
    
    	superblock->generation = btrfs_super_generation(super_hdr);
    	if (!(superblock->generation > state->max_superblock_generation ||
    	      0 == state->max_superblock_generation)) {
    		if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
    			printk(KERN_INFO
    			       "btrfsic: superblock @%llu (%s/%llu/%d)"
    			       " with old gen %llu <= %llu\n",
    			       (unsigned long long)superblock->logical_bytenr,
    			       superblock->dev_state->name,
    			       (unsigned long long)superblock->dev_bytenr,
    			       superblock->mirror_num,
    			       (unsigned long long)
    			       btrfs_super_generation(super_hdr),
    			       (unsigned long long)
    			       state->max_superblock_generation);
    	} else {
    		if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
    			printk(KERN_INFO
    			       "btrfsic: got new superblock @%llu (%s/%llu/%d)"
    			       " with new gen %llu > %llu\n",
    			       (unsigned long long)superblock->logical_bytenr,
    			       superblock->dev_state->name,
    			       (unsigned long long)superblock->dev_bytenr,
    			       superblock->mirror_num,
    			       (unsigned long long)
    			       btrfs_super_generation(super_hdr),
    			       (unsigned long long)
    			       state->max_superblock_generation);
    
    		state->max_superblock_generation =
    		    btrfs_super_generation(super_hdr);
    		state->latest_superblock = superblock;
    	}
    
    	for (pass = 0; pass < 3; pass++) {
    		int ret;
    		u64 next_bytenr;
    		struct btrfsic_block *next_block;
    		struct btrfsic_block_data_ctx tmp_next_block_ctx;
    		struct btrfsic_block_link *l;
    		int num_copies;
    		int mirror_num;
    		const char *additional_string = NULL;
    		struct btrfs_disk_key tmp_disk_key;
    
    		tmp_disk_key.type = BTRFS_ROOT_ITEM_KEY;
    		tmp_disk_key.offset = 0;
    
    		switch (pass) {
    		case 0:
    			tmp_disk_key.objectid =
    			    cpu_to_le64(BTRFS_ROOT_TREE_OBJECTID);
    			additional_string = "root ";
    			next_bytenr = btrfs_super_root(super_hdr);
    			if (state->print_mask &
    			    BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
    				printk(KERN_INFO "root@%llu\n",
    				       (unsigned long long)next_bytenr);
    			break;
    		case 1:
    			tmp_disk_key.objectid =
    			    cpu_to_le64(BTRFS_CHUNK_TREE_OBJECTID);
    			additional_string = "chunk ";
    			next_bytenr = btrfs_super_chunk_root(super_hdr);
    			if (state->print_mask &
    			    BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
    				printk(KERN_INFO "chunk@%llu\n",
    				       (unsigned long long)next_bytenr);
    			break;
    		case 2:
    			tmp_disk_key.objectid =
    			    cpu_to_le64(BTRFS_TREE_LOG_OBJECTID);
    			additional_string = "log ";
    			next_bytenr = btrfs_super_log_root(super_hdr);
    			if (0 == next_bytenr)
    				continue;
    			if (state->print_mask &
    			    BTRFSIC_PRINT_MASK_ROOT_CHUNK_LOG_TREE_LOCATION)
    				printk(KERN_INFO "log@%llu\n",
    				       (unsigned long long)next_bytenr);
    			break;
    		}
    
    		num_copies =
    
    		    btrfs_num_copies(state->root->fs_info,
    
    				     next_bytenr, BTRFS_SUPER_INFO_SIZE);
    
    		if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
    			printk(KERN_INFO "num_copies(log_bytenr=%llu) = %d\n",
    			       (unsigned long long)next_bytenr, num_copies);
    		for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
    			int was_created;
    
    			if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    				printk(KERN_INFO
    				       "btrfsic_process_written_superblock("
    				       "mirror_num=%d)\n", mirror_num);
    
    			ret = btrfsic_map_block(state, next_bytenr,
    						BTRFS_SUPER_INFO_SIZE,
    
    						&tmp_next_block_ctx,
    						mirror_num);
    			if (ret) {
    				printk(KERN_INFO
    				       "btrfsic: btrfsic_map_block(@%llu,"
    				       " mirror=%d) failed!\n",
    				       (unsigned long long)next_bytenr,
    				       mirror_num);
    				return -1;
    			}
    
    			next_block = btrfsic_block_lookup_or_add(
    					state,
    					&tmp_next_block_ctx,
    					additional_string,
    					1, 0, 1,
    					mirror_num,
    					&was_created);
    			if (NULL == next_block) {
    				printk(KERN_INFO
    				       "btrfsic: error, kmalloc failed!\n");
    				btrfsic_release_block_ctx(&tmp_next_block_ctx);
    				return -1;
    			}
    
    			next_block->disk_key = tmp_disk_key;
    			if (was_created)
    				next_block->generation =
    				    BTRFSIC_GENERATION_UNKNOWN;
    			l = btrfsic_block_link_lookup_or_add(
    					state,
    					&tmp_next_block_ctx,
    					next_block,
    					superblock,
    					BTRFSIC_GENERATION_UNKNOWN);
    			btrfsic_release_block_ctx(&tmp_next_block_ctx);
    			if (NULL == l)
    				return -1;
    		}
    	}
    
    	if (-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)) {
    		WARN_ON(1);
    		btrfsic_dump_tree(state);
    	}
    
    	return 0;
    }
    
    static int btrfsic_check_all_ref_blocks(struct btrfsic_state *state,
    					struct btrfsic_block *const block,
    					int recursion_level)
    {
    	struct list_head *elem_ref_to;
    	int ret = 0;
    
    	if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
    		/*
    		 * Note that this situation can happen and does not
    		 * indicate an error in regular cases. It happens
    		 * when disk blocks are freed and later reused.
    		 * The check-integrity module is not aware of any
    		 * block free operations, it just recognizes block
    		 * write operations. Therefore it keeps the linkage
    		 * information for a block until a block is
    		 * rewritten. This can temporarily cause incorrect
    		 * and even circular linkage informations. This
    		 * causes no harm unless such blocks are referenced
    		 * by the most recent super block.
    		 */
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			printk(KERN_INFO
    			       "btrfsic: abort cyclic linkage (case 1).\n");
    
    		return ret;
    	}
    
    	/*
    	 * This algorithm is recursive because the amount of used stack
    	 * space is very small and the max recursion depth is limited.
    	 */
    	list_for_each(elem_ref_to, &block->ref_to_list) {
    		const struct btrfsic_block_link *const l =
    		    list_entry(elem_ref_to, struct btrfsic_block_link,
    			       node_ref_to);
    
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			printk(KERN_INFO
    			       "rl=%d, %c @%llu (%s/%llu/%d)"
    			       " %u* refers to %c @%llu (%s/%llu/%d)\n",
    			       recursion_level,
    			       btrfsic_get_block_type(state, block),
    			       (unsigned long long)block->logical_bytenr,
    			       block->dev_state->name,
    			       (unsigned long long)block->dev_bytenr,
    			       block->mirror_num,
    			       l->ref_cnt,
    			       btrfsic_get_block_type(state, l->block_ref_to),
    			       (unsigned long long)
    			       l->block_ref_to->logical_bytenr,
    			       l->block_ref_to->dev_state->name,
    			       (unsigned long long)l->block_ref_to->dev_bytenr,
    			       l->block_ref_to->mirror_num);
    		if (l->block_ref_to->never_written) {
    			printk(KERN_INFO "btrfs: attempt to write superblock"
    			       " which references block %c @%llu (%s/%llu/%d)"
    			       " which is never written!\n",
    			       btrfsic_get_block_type(state, l->block_ref_to),
    			       (unsigned long long)
    			       l->block_ref_to->logical_bytenr,
    			       l->block_ref_to->dev_state->name,
    			       (unsigned long long)l->block_ref_to->dev_bytenr,
    			       l->block_ref_to->mirror_num);
    			ret = -1;
    		} else if (!l->block_ref_to->is_iodone) {
    			printk(KERN_INFO "btrfs: attempt to write superblock"
    			       " which references block %c @%llu (%s/%llu/%d)"
    			       " which is not yet iodone!\n",
    			       btrfsic_get_block_type(state, l->block_ref_to),
    			       (unsigned long long)
    			       l->block_ref_to->logical_bytenr,
    			       l->block_ref_to->dev_state->name,
    			       (unsigned long long)l->block_ref_to->dev_bytenr,
    			       l->block_ref_to->mirror_num);
    			ret = -1;
    
    		} else if (l->block_ref_to->iodone_w_error) {
    			printk(KERN_INFO "btrfs: attempt to write superblock"
    			       " which references block %c @%llu (%s/%llu/%d)"
    			       " which has write error!\n",
    			       btrfsic_get_block_type(state, l->block_ref_to),
    			       (unsigned long long)
    			       l->block_ref_to->logical_bytenr,
    			       l->block_ref_to->dev_state->name,
    			       (unsigned long long)l->block_ref_to->dev_bytenr,
    			       l->block_ref_to->mirror_num);
    			ret = -1;
    
    2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
    		} else if (l->parent_generation !=
    			   l->block_ref_to->generation &&
    			   BTRFSIC_GENERATION_UNKNOWN !=
    			   l->parent_generation &&
    			   BTRFSIC_GENERATION_UNKNOWN !=
    			   l->block_ref_to->generation) {
    			printk(KERN_INFO "btrfs: attempt to write superblock"
    			       " which references block %c @%llu (%s/%llu/%d)"
    			       " with generation %llu !="
    			       " parent generation %llu!\n",
    			       btrfsic_get_block_type(state, l->block_ref_to),
    			       (unsigned long long)
    			       l->block_ref_to->logical_bytenr,
    			       l->block_ref_to->dev_state->name,
    			       (unsigned long long)l->block_ref_to->dev_bytenr,
    			       l->block_ref_to->mirror_num,
    			       (unsigned long long)l->block_ref_to->generation,
    			       (unsigned long long)l->parent_generation);
    			ret = -1;
    		} else if (l->block_ref_to->flush_gen >
    			   l->block_ref_to->dev_state->last_flush_gen) {
    			printk(KERN_INFO "btrfs: attempt to write superblock"
    			       " which references block %c @%llu (%s/%llu/%d)"
    			       " which is not flushed out of disk's write cache"
    			       " (block flush_gen=%llu,"
    			       " dev->flush_gen=%llu)!\n",
    			       btrfsic_get_block_type(state, l->block_ref_to),
    			       (unsigned long long)
    			       l->block_ref_to->logical_bytenr,
    			       l->block_ref_to->dev_state->name,
    			       (unsigned long long)l->block_ref_to->dev_bytenr,
    			       l->block_ref_to->mirror_num,
    			       (unsigned long long)block->flush_gen,
    			       (unsigned long long)
    			       l->block_ref_to->dev_state->last_flush_gen);
    			ret = -1;
    		} else if (-1 == btrfsic_check_all_ref_blocks(state,
    							      l->block_ref_to,
    							      recursion_level +
    							      1)) {
    			ret = -1;
    		}
    	}
    
    	return ret;
    }
    
    static int btrfsic_is_block_ref_by_superblock(
    		const struct btrfsic_state *state,
    		const struct btrfsic_block *block,
    		int recursion_level)
    {
    	struct list_head *elem_ref_from;
    
    	if (recursion_level >= 3 + BTRFS_MAX_LEVEL) {
    		/* refer to comment at "abort cyclic linkage (case 1)" */
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			printk(KERN_INFO
    			       "btrfsic: abort cyclic linkage (case 2).\n");
    
    		return 0;
    	}
    
    	/*
    	 * This algorithm is recursive because the amount of used stack space
    	 * is very small and the max recursion depth is limited.
    	 */
    	list_for_each(elem_ref_from, &block->ref_from_list) {
    		const struct btrfsic_block_link *const l =
    		    list_entry(elem_ref_from, struct btrfsic_block_link,
    			       node_ref_from);
    
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			printk(KERN_INFO
    			       "rl=%d, %c @%llu (%s/%llu/%d)"
    			       " is ref %u* from %c @%llu (%s/%llu/%d)\n",
    			       recursion_level,
    			       btrfsic_get_block_type(state, block),
    			       (unsigned long long)block->logical_bytenr,
    			       block->dev_state->name,
    			       (unsigned long long)block->dev_bytenr,
    			       block->mirror_num,
    			       l->ref_cnt,
    			       btrfsic_get_block_type(state, l->block_ref_from),
    			       (unsigned long long)
    			       l->block_ref_from->logical_bytenr,
    			       l->block_ref_from->dev_state->name,
    			       (unsigned long long)
    			       l->block_ref_from->dev_bytenr,
    			       l->block_ref_from->mirror_num);
    		if (l->block_ref_from->is_superblock &&
    		    state->latest_superblock->dev_bytenr ==
    		    l->block_ref_from->dev_bytenr &&
    		    state->latest_superblock->dev_state->bdev ==
    		    l->block_ref_from->dev_state->bdev)
    			return 1;
    		else if (btrfsic_is_block_ref_by_superblock(state,
    							    l->block_ref_from,
    							    recursion_level +
    							    1))
    			return 1;
    	}
    
    	return 0;
    }
    
    static void btrfsic_print_add_link(const struct btrfsic_state *state,
    				   const struct btrfsic_block_link *l)
    {
    	printk(KERN_INFO
    	       "Add %u* link from %c @%llu (%s/%llu/%d)"
    	       " to %c @%llu (%s/%llu/%d).\n",
    	       l->ref_cnt,
    	       btrfsic_get_block_type(state, l->block_ref_from),
    	       (unsigned long long)l->block_ref_from->logical_bytenr,
    	       l->block_ref_from->dev_state->name,
    	       (unsigned long long)l->block_ref_from->dev_bytenr,
    	       l->block_ref_from->mirror_num,
    	       btrfsic_get_block_type(state, l->block_ref_to),
    	       (unsigned long long)l->block_ref_to->logical_bytenr,
    	       l->block_ref_to->dev_state->name,
    	       (unsigned long long)l->block_ref_to->dev_bytenr,
    	       l->block_ref_to->mirror_num);
    }
    
    static void btrfsic_print_rem_link(const struct btrfsic_state *state,
    				   const struct btrfsic_block_link *l)
    {
    	printk(KERN_INFO
    	       "Rem %u* link from %c @%llu (%s/%llu/%d)"
    	       " to %c @%llu (%s/%llu/%d).\n",
    	       l->ref_cnt,
    	       btrfsic_get_block_type(state, l->block_ref_from),
    	       (unsigned long long)l->block_ref_from->logical_bytenr,
    	       l->block_ref_from->dev_state->name,
    	       (unsigned long long)l->block_ref_from->dev_bytenr,
    	       l->block_ref_from->mirror_num,
    	       btrfsic_get_block_type(state, l->block_ref_to),
    	       (unsigned long long)l->block_ref_to->logical_bytenr,
    	       l->block_ref_to->dev_state->name,
    	       (unsigned long long)l->block_ref_to->dev_bytenr,
    	       l->block_ref_to->mirror_num);
    }
    
    static char btrfsic_get_block_type(const struct btrfsic_state *state,
    				   const struct btrfsic_block *block)
    {
    	if (block->is_superblock &&
    	    state->latest_superblock->dev_bytenr == block->dev_bytenr &&
    	    state->latest_superblock->dev_state->bdev == block->dev_state->bdev)
    		return 'S';
    	else if (block->is_superblock)
    		return 's';
    	else if (block->is_metadata)
    		return 'M';
    	else
    		return 'D';
    }
    
    static void btrfsic_dump_tree(const struct btrfsic_state *state)
    {
    	btrfsic_dump_tree_sub(state, state->latest_superblock, 0);
    }
    
    static void btrfsic_dump_tree_sub(const struct btrfsic_state *state,
    				  const struct btrfsic_block *block,
    				  int indent_level)
    {
    	struct list_head *elem_ref_to;
    	int indent_add;
    	static char buf[80];
    	int cursor_position;
    
    	/*
    	 * Should better fill an on-stack buffer with a complete line and
    	 * dump it at once when it is time to print a newline character.
    	 */
    
    	/*
    	 * This algorithm is recursive because the amount of used stack space
    	 * is very small and the max recursion depth is limited.
    	 */
    	indent_add = sprintf(buf, "%c-%llu(%s/%llu/%d)",
    			     btrfsic_get_block_type(state, block),
    			     (unsigned long long)block->logical_bytenr,
    			     block->dev_state->name,
    			     (unsigned long long)block->dev_bytenr,
    			     block->mirror_num);
    	if (indent_level + indent_add > BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
    		printk("[...]\n");
    		return;
    	}
    	printk(buf);
    	indent_level += indent_add;
    	if (list_empty(&block->ref_to_list)) {
    		printk("\n");
    		return;
    	}
    	if (block->mirror_num > 1 &&
    	    !(state->print_mask & BTRFSIC_PRINT_MASK_TREE_WITH_ALL_MIRRORS)) {
    		printk(" [...]\n");
    		return;
    	}
    
    	cursor_position = indent_level;
    	list_for_each(elem_ref_to, &block->ref_to_list) {
    		const struct btrfsic_block_link *const l =
    		    list_entry(elem_ref_to, struct btrfsic_block_link,
    			       node_ref_to);
    
    		while (cursor_position < indent_level) {
    			printk(" ");
    			cursor_position++;
    		}
    		if (l->ref_cnt > 1)
    			indent_add = sprintf(buf, " %d*--> ", l->ref_cnt);
    		else
    			indent_add = sprintf(buf, " --> ");
    		if (indent_level + indent_add >
    		    BTRFSIC_TREE_DUMP_MAX_INDENT_LEVEL) {
    			printk("[...]\n");
    			cursor_position = 0;
    			continue;
    		}
    
    		printk(buf);
    
    		btrfsic_dump_tree_sub(state, l->block_ref_to,
    				      indent_level + indent_add);
    		cursor_position = 0;
    	}
    }
    
    static struct btrfsic_block_link *btrfsic_block_link_lookup_or_add(
    		struct btrfsic_state *state,
    		struct btrfsic_block_data_ctx *next_block_ctx,
    		struct btrfsic_block *next_block,
    		struct btrfsic_block *from_block,
    		u64 parent_generation)
    {
    	struct btrfsic_block_link *l;
    
    	l = btrfsic_block_link_hashtable_lookup(next_block_ctx->dev->bdev,
    						next_block_ctx->dev_bytenr,
    						from_block->dev_state->bdev,
    						from_block->dev_bytenr,
    						&state->block_link_hashtable);
    	if (NULL == l) {
    		l = btrfsic_block_link_alloc();
    		if (NULL == l) {
    			printk(KERN_INFO
    			       "btrfsic: error, kmalloc" " failed!\n");
    			return NULL;
    		}
    
    		l->block_ref_to = next_block;
    		l->block_ref_from = from_block;
    		l->ref_cnt = 1;
    		l->parent_generation = parent_generation;
    
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			btrfsic_print_add_link(state, l);
    
    		list_add(&l->node_ref_to, &from_block->ref_to_list);
    		list_add(&l->node_ref_from, &next_block->ref_from_list);
    
    		btrfsic_block_link_hashtable_add(l,
    						 &state->block_link_hashtable);
    	} else {
    		l->ref_cnt++;
    		l->parent_generation = parent_generation;
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			btrfsic_print_add_link(state, l);
    	}
    
    	return l;
    }
    
    static struct btrfsic_block *btrfsic_block_lookup_or_add(
    		struct btrfsic_state *state,
    		struct btrfsic_block_data_ctx *block_ctx,
    		const char *additional_string,
    		int is_metadata,
    		int is_iodone,
    		int never_written,
    		int mirror_num,
    		int *was_created)
    {
    	struct btrfsic_block *block;
    
    	block = btrfsic_block_hashtable_lookup(block_ctx->dev->bdev,
    					       block_ctx->dev_bytenr,
    					       &state->block_hashtable);
    	if (NULL == block) {
    		struct btrfsic_dev_state *dev_state;
    
    		block = btrfsic_block_alloc();
    		if (NULL == block) {
    			printk(KERN_INFO "btrfsic: error, kmalloc failed!\n");
    			return NULL;
    		}
    		dev_state = btrfsic_dev_state_lookup(block_ctx->dev->bdev);
    		if (NULL == dev_state) {
    			printk(KERN_INFO
    			       "btrfsic: error, lookup dev_state failed!\n");
    			btrfsic_block_free(block);
    			return NULL;
    		}
    		block->dev_state = dev_state;
    		block->dev_bytenr = block_ctx->dev_bytenr;
    		block->logical_bytenr = block_ctx->start;
    		block->is_metadata = is_metadata;
    		block->is_iodone = is_iodone;
    		block->never_written = never_written;
    		block->mirror_num = mirror_num;
    		if (state->print_mask & BTRFSIC_PRINT_MASK_VERBOSE)
    			printk(KERN_INFO
    			       "New %s%c-block @%llu (%s/%llu/%d)\n",
    			       additional_string,
    			       btrfsic_get_block_type(state, block),
    			       (unsigned long long)block->logical_bytenr,
    			       dev_state->name,
    			       (unsigned long long)block->dev_bytenr,
    			       mirror_num);
    		list_add(&block->all_blocks_node, &state->all_blocks_list);
    		btrfsic_block_hashtable_add(block, &state->block_hashtable);
    		if (NULL != was_created)
    			*was_created = 1;
    	} else {
    		if (NULL != was_created)
    			*was_created = 0;
    	}
    
    	return block;
    }
    
    static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
    					   u64 bytenr,
    					   struct btrfsic_dev_state *dev_state,
    
    {
    	int num_copies;
    	int mirror_num;
    	int ret;
    	struct btrfsic_block_data_ctx block_ctx;
    	int match = 0;
    
    
    	num_copies = btrfs_num_copies(state->root->fs_info,
    
    				      bytenr, state->metablock_size);
    
    
    	for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
    
    		ret = btrfsic_map_block(state, bytenr, state->metablock_size,
    
    					&block_ctx, mirror_num);
    		if (ret) {
    			printk(KERN_INFO "btrfsic:"
    			       " btrfsic_map_block(logical @%llu,"
    			       " mirror %d) failed!\n",
    			       (unsigned long long)bytenr, mirror_num);
    			continue;
    		}
    
    		if (dev_state->bdev == block_ctx.dev->bdev &&
    		    dev_bytenr == block_ctx.dev_bytenr) {
    			match++;
    			btrfsic_release_block_ctx(&block_ctx);
    			break;
    		}
    		btrfsic_release_block_ctx(&block_ctx);
    	}
    
    	if (!match) {
    		printk(KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
    		       " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
    		       " phys_bytenr=%llu)!\n",
    		       (unsigned long long)bytenr, dev_state->name,