Skip to content
Snippets Groups Projects
check-integrity.c 93.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 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
    			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,
    							&block_ctx,
    							(struct btrfs_header *)
    							block_ctx.data, 0, 0);
    			if (ret)
    				printk(KERN_INFO
    				       "btrfsic: process_metablock(root @%llu)"
    				       " failed!\n",
    				       (unsigned long long)dev_bytenr);
    		}
    		btrfsic_release_block_ctx(&block_ctx);
    	}
    }
    
    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->mapping_tree,
    				     next_bytenr, PAGE_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, PAGE_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->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,
    					   u64 dev_bytenr, char *data)
    {
    	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->mapping_tree,
    				      bytenr, PAGE_SIZE);
    
    	for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
    		ret = btrfsic_map_block(state, bytenr, PAGE_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,
    		       (unsigned long long)dev_bytenr);
    		for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
    			ret = btrfsic_map_block(state, bytenr, PAGE_SIZE,
    						&block_ctx, mirror_num);
    			if (ret)
    				continue;
    
    			printk(KERN_INFO "Read logical bytenr @%llu maps to"
    			       " (%s/%llu/%d)\n",
    			       (unsigned long long)bytenr,
    			       block_ctx.dev->name,
    			       (unsigned long long)block_ctx.dev_bytenr,
    			       mirror_num);
    		}
    		WARN_ON(1);
    	}
    }
    
    static struct btrfsic_dev_state *btrfsic_dev_state_lookup(
    		struct block_device *bdev)
    {
    	struct btrfsic_dev_state *ds;
    
    	ds = btrfsic_dev_state_hashtable_lookup(bdev,
    						&btrfsic_dev_state_hashtable);
    	return ds;
    }
    
    int btrfsic_submit_bh(int rw, struct buffer_head *bh)
    {
    	struct btrfsic_dev_state *dev_state;
    
    	if (!btrfsic_is_initialized)
    		return submit_bh(rw, bh);
    
    	mutex_lock(&btrfsic_mutex);
    	/* since btrfsic_submit_bh() might also be called before
    	 * btrfsic_mount(), this might return NULL */
    	dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
    
    	/* Only called to write the superblock (incl. FLUSH/FUA) */
    	if (NULL != dev_state &&
    	    (rw & WRITE) && bh->b_size > 0) {
    		u64 dev_bytenr;
    
    		dev_bytenr = 4096 * bh->b_blocknr;
    		if (dev_state->state->print_mask &
    		    BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
    			printk(KERN_INFO
    			       "submit_bh(rw=0x%x, blocknr=%lu (bytenr %llu),"
    			       " size=%lu, data=%p, bdev=%p)\n",
    
    			       rw, (unsigned long)bh->b_blocknr,
    			       (unsigned long long)dev_bytenr,
    			       (unsigned long)bh->b_size, bh->b_data,
    			       bh->b_bdev);
    
    		btrfsic_process_written_block(dev_state, dev_bytenr,
    					      bh->b_data, bh->b_size, NULL,
    					      NULL, bh, rw);
    	} else if (NULL != dev_state && (rw & REQ_FLUSH)) {
    		if (dev_state->state->print_mask &
    		    BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
    			printk(KERN_INFO
    			       "submit_bh(rw=0x%x) FLUSH, bdev=%p)\n",
    			       rw, bh->b_bdev);
    		if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
    			if ((dev_state->state->print_mask &
    			     (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
    			      BTRFSIC_PRINT_MASK_VERBOSE)))
    				printk(KERN_INFO
    				       "btrfsic_submit_bh(%s) with FLUSH"
    				       " but dummy block already in use"
    				       " (ignored)!\n",
    				       dev_state->name);
    		} else {
    			struct btrfsic_block *const block =
    				&dev_state->dummy_block_for_bio_bh_flush;
    
    			block->is_iodone = 0;
    			block->never_written = 0;
    			block->iodone_w_error = 0;
    			block->flush_gen = dev_state->last_flush_gen + 1;
    			block->submit_bio_bh_rw = rw;
    			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;
    		}
    	}
    	mutex_unlock(&btrfsic_mutex);
    	return submit_bh(rw, bh);
    }
    
    void btrfsic_submit_bio(int rw, struct bio *bio)
    {
    	struct btrfsic_dev_state *dev_state;
    
    	if (!btrfsic_is_initialized) {
    		submit_bio(rw, bio);
    		return;
    	}
    
    	mutex_lock(&btrfsic_mutex);
    	/* since btrfsic_submit_bio() is also called before
    	 * btrfsic_mount(), this might return NULL */
    	dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
    	if (NULL != dev_state &&
    	    (rw & WRITE) && NULL != bio->bi_io_vec) {
    		unsigned int i;
    		u64 dev_bytenr;
    		int bio_is_patched;
    
    		dev_bytenr = 512 * bio->bi_sector;
    		bio_is_patched = 0;
    		if (dev_state->state->print_mask &
    		    BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
    			printk(KERN_INFO
    			       "submit_bio(rw=0x%x, bi_vcnt=%u,"
    			       " bi_sector=%lu (bytenr %llu), bi_bdev=%p)\n",
    
    			       rw, bio->bi_vcnt, (unsigned long)bio->bi_sector,
    
    			       (unsigned long long)dev_bytenr,
    			       bio->bi_bdev);
    
    		for (i = 0; i < bio->bi_vcnt; i++) {
    			u8 *mapped_data;
    
    			mapped_data = kmap(bio->bi_io_vec[i].bv_page);
    			if ((BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
    			     BTRFSIC_PRINT_MASK_VERBOSE) ==
    			    (dev_state->state->print_mask &
    			     (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
    			      BTRFSIC_PRINT_MASK_VERBOSE)))
    				printk(KERN_INFO
    				       "#%u: page=%p, mapped=%p, len=%u,"
    				       " offset=%u\n",
    				       i, bio->bi_io_vec[i].bv_page,
    				       mapped_data,
    				       bio->bi_io_vec[i].bv_len,
    				       bio->bi_io_vec[i].bv_offset);
    			btrfsic_process_written_block(dev_state, dev_bytenr,
    						      mapped_data,
    						      bio->bi_io_vec[i].bv_len,
    						      bio, &bio_is_patched,
    						      NULL, rw);
    			kunmap(bio->bi_io_vec[i].bv_page);
    			dev_bytenr += bio->bi_io_vec[i].bv_len;
    		}
    	} else if (NULL != dev_state && (rw & REQ_FLUSH)) {
    		if (dev_state->state->print_mask &
    		    BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH)
    			printk(KERN_INFO
    			       "submit_bio(rw=0x%x) FLUSH, bdev=%p)\n",
    			       rw, bio->bi_bdev);
    		if (!dev_state->dummy_block_for_bio_bh_flush.is_iodone) {
    			if ((dev_state->state->print_mask &
    			     (BTRFSIC_PRINT_MASK_SUBMIT_BIO_BH |
    			      BTRFSIC_PRINT_MASK_VERBOSE)))
    				printk(KERN_INFO
    				       "btrfsic_submit_bio(%s) with FLUSH"
    				       " but dummy block already in use"
    				       " (ignored)!\n",
    				       dev_state->name);
    		} else {
    			struct btrfsic_block *const block =
    				&dev_state->dummy_block_for_bio_bh_flush;
    
    			block->is_iodone = 0;
    			block->never_written = 0;
    			block->iodone_w_error = 0;
    			block->flush_gen = dev_state->last_flush_gen + 1;
    			block->submit_bio_bh_rw = rw;
    			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;
    		}
    	}
    	mutex_unlock(&btrfsic_mutex);
    
    	submit_bio(rw, bio);
    }
    
    int btrfsic_mount(struct btrfs_root *root,
    		  struct btrfs_fs_devices *fs_devices,
    		  int including_extent_data, u32 print_mask)
    {
    	int ret;
    	struct btrfsic_state *state;
    	struct list_head *dev_head = &fs_devices->devices;
    	struct btrfs_device *device;
    
    	state = kzalloc(sizeof(*state), GFP_NOFS);
    	if (NULL == state) {
    		printk(KERN_INFO "btrfs check-integrity: kmalloc() failed!\n");
    		return -1;
    	}
    
    	if (!btrfsic_is_initialized) {
    		mutex_init(&btrfsic_mutex);
    		btrfsic_dev_state_hashtable_init(&btrfsic_dev_state_hashtable);
    		btrfsic_is_initialized = 1;
    	}
    	mutex_lock(&btrfsic_mutex);
    	state->root = root;
    	state->print_mask = print_mask;
    	state->include_extent_data = including_extent_data;
    	state->csum_size = 0;
    	INIT_LIST_HEAD(&state->all_blocks_list);
    	btrfsic_block_hashtable_init(&state->block_hashtable);
    	btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
    	state->max_superblock_generation = 0;
    	state->latest_superblock = NULL;
    
    	list_for_each_entry(device, dev_head, dev_list) {
    		struct btrfsic_dev_state *ds;
    		char *p;
    
    		if (!device->bdev || !device->name)
    			continue;
    
    		ds = btrfsic_dev_state_alloc();
    		if (NULL == ds) {
    			printk(KERN_INFO
    			       "btrfs check-integrity: kmalloc() failed!\n");
    			mutex_unlock(&btrfsic_mutex);
    			return -1;
    		}
    		ds->bdev = device->bdev;
    		ds->state = state;
    		bdevname(ds->bdev, ds->name);
    		ds->name[BDEVNAME_SIZE - 1] = '\0';
    		for (p = ds->name; *p != '\0'; p++);
    		while (p > ds->name && *p != '/')
    			p--;
    		if (*p == '/')
    			p++;
    		strlcpy(ds->name, p, sizeof(ds->name));
    		btrfsic_dev_state_hashtable_add(ds,
    						&btrfsic_dev_state_hashtable);
    	}
    
    	ret = btrfsic_process_superblock(state, fs_devices);
    	if (0 != ret) {
    		mutex_unlock(&btrfsic_mutex);
    		btrfsic_unmount(root, fs_devices);
    		return ret;
    	}
    
    	if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_DATABASE)
    		btrfsic_dump_database(state);
    	if (state->print_mask & BTRFSIC_PRINT_MASK_INITIAL_TREE)
    		btrfsic_dump_tree(state);
    
    	mutex_unlock(&btrfsic_mutex);
    	return 0;
    }
    
    void btrfsic_unmount(struct btrfs_root *root,
    		     struct btrfs_fs_devices *fs_devices)
    {
    	struct list_head *elem_all;
    	struct list_head *tmp_all;
    	struct btrfsic_state *state;
    	struct list_head *dev_head = &fs_devices->devices;
    	struct btrfs_device *device;
    
    	if (!btrfsic_is_initialized)
    		return;
    
    	mutex_lock(&btrfsic_mutex);