82 #include <linux/sched.h>
83 #include <linux/slab.h>
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)
108 #define BTRFSIC_GENERATION_UNKNOWN ((u64)-1)
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
197 struct list_head table[BTRFSIC_BLOCK_HASHTABLE_SIZE];
201 struct list_head table[BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE];
205 struct list_head table[BTRFSIC_DEV2STATE_HASHTABLE_SIZE];
263 static void btrfsic_block_hashtable_remove(
struct btrfsic_block *
b);
268 static void btrfsic_block_link_hashtable_init(
270 static void btrfsic_block_link_hashtable_add(
276 u64 dev_bytenr_ref_to,
278 u64 dev_bytenr_ref_from,
280 static void btrfsic_dev_state_hashtable_init(
282 static void btrfsic_dev_state_hashtable_add(
297 static void btrfsic_read_from_block_data(
300 static int btrfsic_create_link_to_next_block(
308 int force_iodone_flag,
309 int *num_copiesp,
int *mirror_nump,
311 u64 parent_generation);
315 u32 item_offset,
int force_iodone_flag);
326 static void btrfsic_complete_bio_end_io(
struct bio *bio,
int err);
330 u64 dev_bytenr,
char **mapped_datav,
332 struct bio *bio,
int *bio_is_patched,
333 struct buffer_head *bh,
334 int submit_bio_bh_rw);
335 static int btrfsic_process_written_superblock(
339 static void btrfsic_bio_end_io(
struct bio *bp,
int bio_error_status);
340 static void btrfsic_bh_end_io(
struct buffer_head *bh,
int uptodate);
343 int recursion_level);
346 int recursion_level);
366 const char *additional_string,
372 static int btrfsic_process_superblock_dev_mirror(
376 int superblock_mirror_num,
386 static struct mutex btrfsic_mutex;
387 static int btrfsic_is_initialized;
424 btrfsic_block_init(b);
452 btrfsic_block_link_init(l);
480 ds = kzalloc(
sizeof(*ds),
GFP_NOFS);
482 btrfsic_dev_state_init(ds);
499 INIT_LIST_HEAD(h->
table + i);
502 static void btrfsic_block_hashtable_add(
struct btrfsic_block *b,
505 const unsigned int hashval =
508 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
513 static void btrfsic_block_hashtable_remove(
struct btrfsic_block *b)
523 const unsigned int hashval =
524 (((
unsigned int)(dev_bytenr >> 16)) ^
526 (BTRFSIC_BLOCK_HASHTABLE_SIZE - 1);
541 static void btrfsic_block_link_hashtable_init(
547 INIT_LIST_HEAD(h->
table + i);
550 static void btrfsic_block_link_hashtable_add(
554 const unsigned int hashval =
559 & (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
573 u64 dev_bytenr_ref_to,
575 u64 dev_bytenr_ref_from,
578 const unsigned int hashval =
579 (((
unsigned int)(dev_bytenr_ref_to >> 16)) ^
580 ((
unsigned int)(dev_bytenr_ref_from >> 16)) ^
583 (BTRFSIC_BLOCK_LINK_HASHTABLE_SIZE - 1);
603 static void btrfsic_dev_state_hashtable_init(
609 INIT_LIST_HEAD(h->
table + i);
612 static void btrfsic_dev_state_hashtable_add(
616 const unsigned int hashval =
618 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
632 const unsigned int hashval =
634 (BTRFSIC_DEV2STATE_HASHTABLE_SIZE - 1));
642 if (ds->
bdev == bdev)
660 selected_super = kzalloc(
sizeof(*selected_super),
GFP_NOFS);
661 if (
NULL == selected_super) {
673 dev_state = btrfsic_dev_state_lookup(device->
bdev);
676 ret = btrfsic_process_superblock_dev_mirror(
677 state, dev_state, device, i,
678 &selected_dev_state, selected_super);
679 if (0 != ret && 0 == i) {
680 kfree(selected_super);
688 kfree(selected_super);
692 state->
csum_size = btrfs_super_csum_size(selected_super);
694 for (pass = 0; pass < 3; pass++) {
701 next_bytenr = btrfs_super_root(selected_super);
705 (
unsigned long long)next_bytenr);
708 next_bytenr = btrfs_super_chunk_root(selected_super);
712 (
unsigned long long)next_bytenr);
715 next_bytenr = btrfs_super_log_root(selected_super);
716 if (0 == next_bytenr)
721 (
unsigned long long)next_bytenr);
730 (
unsigned long long)next_bytenr, num_copies);
732 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
737 ret = btrfsic_map_block(state, next_bytenr,
743 " btrfsic_map_block(root @%llu,"
744 " mirror %d) failed!\n",
745 (
unsigned long long)next_bytenr,
747 kfree(selected_super);
751 next_block = btrfsic_block_hashtable_lookup(
752 tmp_next_block_ctx.dev->bdev,
753 tmp_next_block_ctx.dev_bytenr,
757 l = btrfsic_block_link_hashtable_lookup(
758 tmp_next_block_ctx.dev->bdev,
759 tmp_next_block_ctx.dev_bytenr,
766 ret = btrfsic_read_block(state, &tmp_next_block_ctx);
769 "btrfsic: read @logical %llu failed!\n",
771 tmp_next_block_ctx.start);
772 btrfsic_release_block_ctx(&tmp_next_block_ctx);
773 kfree(selected_super);
777 ret = btrfsic_process_metablock(state,
781 btrfsic_release_block_ctx(&tmp_next_block_ctx);
785 kfree(selected_super);
789 static int btrfsic_process_superblock_dev_mirror(
793 int superblock_mirror_num,
799 struct buffer_head *bh;
805 dev_bytenr = btrfs_sb_offset(superblock_mirror_num);
808 bh =
__bread(superblock_bdev, dev_bytenr / 4096,
813 (bh->b_data + (dev_bytenr & 4095));
815 if (btrfs_super_bytenr(super_tmp) != dev_bytenr ||
817 sizeof(super_tmp->
magic)) ||
827 btrfsic_block_hashtable_lookup(superblock_bdev,
830 if (
NULL == superblock_tmp) {
831 superblock_tmp = btrfsic_block_alloc();
832 if (
NULL == superblock_tmp) {
841 superblock_tmp->
generation = btrfs_super_generation(super_tmp);
846 superblock_tmp->
mirror_num = 1 + superblock_mirror_num;
849 " @%llu (%s/%llu/%d)\n",
852 (
unsigned long long)dev_bytenr,
854 (
unsigned long long)dev_bytenr,
855 superblock_mirror_num);
858 btrfsic_block_hashtable_add(superblock_tmp,
863 if (btrfs_super_generation(super_tmp) >
866 memcpy(selected_super, super_tmp,
sizeof(*selected_super));
869 btrfs_super_generation(super_tmp);
873 for (pass = 0; pass < 3; pass++) {
877 const char *additional_string =
NULL;
881 tmp_disk_key.offset = 0;
884 tmp_disk_key.objectid =
886 additional_string =
"initial root ";
887 next_bytenr = btrfs_super_root(super_tmp);
890 tmp_disk_key.objectid =
892 additional_string =
"initial chunk ";
893 next_bytenr = btrfs_super_chunk_root(super_tmp);
896 tmp_disk_key.objectid =
898 additional_string =
"initial log ";
899 next_bytenr = btrfs_super_log_root(super_tmp);
900 if (0 == next_bytenr)
910 (
unsigned long long)next_bytenr, num_copies);
911 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
916 if (btrfsic_map_block(state, next_bytenr,
921 "bytenr @%llu, mirror %d) failed!\n",
922 (
unsigned long long)next_bytenr,
928 next_block = btrfsic_block_lookup_or_add(
929 state, &tmp_next_block_ctx,
930 additional_string, 1, 1, 0,
932 if (
NULL == next_block) {
933 btrfsic_release_block_ctx(&tmp_next_block_ctx);
938 next_block->
disk_key = tmp_disk_key;
940 l = btrfsic_block_link_lookup_or_add(
941 state, &tmp_next_block_ctx,
942 next_block, superblock_tmp,
944 btrfsic_release_block_ctx(&tmp_next_block_ctx);
952 btrfsic_dump_tree_sub(state, superblock_tmp, 0);
962 sf = kzalloc(
sizeof(*sf),
GFP_NOFS);
977 static int btrfsic_process_metablock(
981 int first_limit_nesting,
int force_iodone_flag)
990 sf = &initial_stack_frame;
994 sf->
block = first_block;
1000 continue_with_new_stack_frame:
1002 if (0 == sf->
hdr->level) {
1011 "leaf %llu items %d generation %llu"
1013 (
unsigned long long)
1016 (
unsigned long long)
1018 (
unsigned long long)
1022 continue_with_current_leaf_stack_frame:
1028 if (sf->
i < sf->
nr) {
1030 u32 disk_item_offset =
1038 if (disk_item_offset +
sizeof(
struct btrfs_item) >
1040 leaf_item_out_of_bounce_error:
1042 "btrfsic: leaf item out of bounce at logical %llu, dev %s\n",
1045 goto one_stack_frame_backwards;
1047 btrfsic_read_from_block_data(sf->
block_ctx,
1053 disk_key = &disk_item.key;
1054 type = disk_key->
type;
1058 u32 root_item_offset;
1061 root_item_offset = item_offset +
1063 if (root_item_offset + item_size >
1065 goto leaf_item_out_of_bounce_error;
1066 btrfsic_read_from_block_data(
1073 btrfsic_create_link_to_next_block(
1088 goto one_stack_frame_backwards;
1096 btrfsic_stack_frame_alloc();
1097 if (
NULL == next_stack) {
1098 btrfsic_release_block_ctx(
1101 goto one_stack_frame_backwards;
1114 goto continue_with_new_stack_frame;
1118 sf->
error = btrfsic_handle_extent_data(
1125 goto one_stack_frame_backwards;
1128 goto continue_with_current_leaf_stack_frame;
1138 " generation %llu owner %llu\n",
1139 (
unsigned long long)
1142 (
unsigned long long)
1144 (
unsigned long long)
1148 continue_with_current_node_stack_frame:
1154 if (sf->
i < sf->
nr) {
1164 "btrfsic: node item out of bounce at logical %llu, dev %s\n",
1167 goto one_stack_frame_backwards;
1169 btrfsic_read_from_block_data(
1170 sf->
block_ctx, &key_ptr, key_ptr_offset,
1174 sf->
error = btrfsic_create_link_to_next_block(
1188 goto one_stack_frame_backwards;
1195 next_stack = btrfsic_stack_frame_alloc();
1196 if (
NULL == next_stack)
1197 goto one_stack_frame_backwards;
1208 goto continue_with_new_stack_frame;
1211 goto continue_with_current_node_stack_frame;
1215 one_stack_frame_backwards:
1220 btrfsic_release_block_ctx(sf->
block_ctx);
1224 btrfsic_stack_frame_free(sf);
1226 goto one_stack_frame_backwards;
1229 btrfsic_stack_frame_free(sf);
1231 goto continue_with_new_stack_frame;
1233 BUG_ON(&initial_stack_frame != sf);
1239 static void btrfsic_read_from_block_data(
1246 char *
dst = (
char *)dstv;
1251 offset_in_page = (start_offset +
offset) &
1258 kaddr = block_ctx->
datav[
i];
1259 memcpy(dst, kaddr + offset_in_page, cur);
1268 static int btrfsic_create_link_to_next_block(
1276 int force_iodone_flag,
1277 int *num_copiesp,
int *mirror_nump,
1279 u64 parent_generation)
1284 int did_alloc_block_link;
1285 int block_was_created;
1287 *next_blockp =
NULL;
1288 if (0 == *num_copiesp) {
1294 (
unsigned long long)next_bytenr, *num_copiesp);
1298 if (*mirror_nump > *num_copiesp)
1303 "btrfsic_create_link_to_next_block(mirror_num=%d)\n",
1305 ret = btrfsic_map_block(state, next_bytenr,
1307 next_block_ctx, *mirror_nump);
1310 "btrfsic: btrfsic_map_block(@%llu, mirror=%d) failed!\n",
1311 (
unsigned long long)next_bytenr, *mirror_nump);
1312 btrfsic_release_block_ctx(next_block_ctx);
1313 *next_blockp =
NULL;
1317 next_block = btrfsic_block_lookup_or_add(state,
1318 next_block_ctx,
"referenced ",
1319 1, force_iodone_flag,
1322 &block_was_created);
1323 if (
NULL == next_block) {
1324 btrfsic_release_block_ctx(next_block_ctx);
1325 *next_blockp =
NULL;
1328 if (block_was_created) {
1336 "Referenced block @%llu (%s/%llu/%d)"
1337 " found in hash table, %c,"
1338 " bytenr mismatch (!= stored %llu).\n",
1339 (
unsigned long long)next_bytenr,
1340 next_block_ctx->
dev->name,
1341 (
unsigned long long)next_block_ctx->
dev_bytenr,
1343 btrfsic_get_block_type(state, next_block),
1347 "Referenced block @%llu (%s/%llu/%d)"
1348 " found in hash table, %c.\n",
1349 (
unsigned long long)next_bytenr,
1350 next_block_ctx->
dev->name,
1351 (
unsigned long long)next_block_ctx->
dev_bytenr,
1353 btrfsic_get_block_type(state, next_block));
1357 l = btrfsic_block_link_hashtable_lookup(
1358 next_block_ctx->
dev->bdev,
1360 block_ctx->
dev->bdev,
1367 l = btrfsic_block_link_alloc();
1370 btrfsic_release_block_ctx(next_block_ctx);
1371 *next_blockp =
NULL;
1375 did_alloc_block_link = 1;
1382 btrfsic_print_add_link(state, l);
1387 btrfsic_block_link_hashtable_add(l,
1390 did_alloc_block_link = 0;
1391 if (0 == limit_nesting) {
1395 btrfsic_print_add_link(state, l);
1399 if (limit_nesting > 0 && did_alloc_block_link) {
1400 ret = btrfsic_read_block(state, next_block_ctx);
1401 if (ret < (
int)next_block_ctx->
len) {
1403 "btrfsic: read block @logical %llu failed!\n",
1404 (
unsigned long long)next_bytenr);
1405 btrfsic_release_block_ctx(next_block_ctx);
1406 *next_blockp =
NULL;
1410 *next_blockp = next_block;
1412 *next_blockp =
NULL;
1419 static int btrfsic_handle_extent_data(
1423 u32 item_offset,
int force_iodone_flag)
1427 u64 file_extent_item_offset;
1435 if (file_extent_item_offset +
1439 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1440 block_ctx->
start, block_ctx->
dev->name);
1444 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1445 file_extent_item_offset,
1451 file_extent_item.type,
1452 (
unsigned long long)
1460 "btrfsic: file item out of bounce at logical %llu, dev %s\n",
1461 block_ctx->
start, block_ctx->
dev->name);
1464 btrfsic_read_from_block_data(block_ctx, &file_extent_item,
1465 file_extent_item_offset,
1467 next_bytenr =
le64_to_cpu(file_extent_item.disk_bytenr) +
1469 generation =
le64_to_cpu(file_extent_item.generation);
1470 num_bytes =
le64_to_cpu(file_extent_item.num_bytes);
1471 generation =
le64_to_cpu(file_extent_item.generation);
1475 " offset = %llu, num_bytes = %llu\n",
1476 file_extent_item.type,
1477 (
unsigned long long)
1479 (
unsigned long long)
le64_to_cpu(file_extent_item.offset),
1480 (
unsigned long long)num_bytes);
1481 while (num_bytes > 0) {
1496 (
unsigned long long)next_bytenr, num_copies);
1497 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
1500 int block_was_created;
1504 "mirror_num=%d)\n", mirror_num);
1507 "\tdisk_bytenr = %llu, num_bytes %u\n",
1508 (
unsigned long long)next_bytenr,
1510 ret = btrfsic_map_block(state, next_bytenr,
1511 chunk_len, &next_block_ctx,
1515 "btrfsic: btrfsic_map_block(@%llu,"
1516 " mirror=%d) failed!\n",
1517 (
unsigned long long)next_bytenr,
1522 next_block = btrfsic_block_lookup_or_add(
1530 &block_was_created);
1531 if (
NULL == next_block) {
1533 "btrfsic: error, kmalloc failed!\n");
1534 btrfsic_release_block_ctx(&next_block_ctx);
1537 if (!block_was_created) {
1543 " @%llu (%s/%llu/%d)"
1544 " found in hash table, D,"
1546 " (!= stored %llu).\n",
1547 (
unsigned long long)next_bytenr,
1548 next_block_ctx.
dev->name,
1549 (
unsigned long long)
1552 (
unsigned long long)
1559 l = btrfsic_block_link_lookup_or_add(state,
1563 btrfsic_release_block_ctx(&next_block_ctx);
1568 next_bytenr += chunk_len;
1569 num_bytes -= chunk_len;
1586 bytenr, &length, &multi, mirror_num);
1588 device = multi->
stripes[0].dev;
1589 block_ctx_out->
dev = btrfsic_dev_state_lookup(device->
bdev);
1592 block_ctx_out->
len = len;
1599 if (
NULL == block_ctx_out->
dev) {
1611 block_ctx_out->
dev = btrfsic_dev_state_lookup(bdev);
1614 block_ctx_out->
len = len;
1618 if (
NULL != block_ctx_out->
dev) {
1635 while (num_pages > 0) {
1637 if (block_ctx->
datav[num_pages]) {
1641 if (block_ctx->
pagev[num_pages]) {
1667 "btrfsic: read_block() with unaligned bytenr %llu\n",
1675 sizeof(*block_ctx->
pagev)) *
1680 block_ctx->
pagev = (
struct page **)(block_ctx->
datav + num_pages);
1683 if (!block_ctx->
pagev[i])
1693 bio = bio_alloc(
GFP_NOFS, num_pages - i);
1696 "btrfsic: bio_alloc() for %u pages failed!\n",
1700 bio->bi_bdev = block_ctx->
dev->bdev;
1701 bio->bi_sector = dev_bytenr >> 9;
1702 bio->bi_end_io = btrfsic_complete_bio_end_io;
1713 "btrfsic: error, failed to add a single page!\n");
1721 if (!
test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1723 "btrfsic: read error at logical %llu dev %s!\n",
1724 block_ctx->
start, block_ctx->
dev->name);
1734 if (!block_ctx->
datav[i]) {
1736 block_ctx->
dev->name);
1741 return block_ctx->
len;
1744 static void btrfsic_complete_bio_end_io(
struct bio *bio,
int err)
1749 static void btrfsic_dump_database(
struct btrfsic_state *state)
1764 btrfsic_get_block_type(state, b_all),
1778 " %c @%llu (%s/%llu/%d)\n",
1779 btrfsic_get_block_type(state, b_all),
1786 (
unsigned long long)
1801 " %c @%llu (%s/%llu/%d)\n",
1802 btrfsic_get_block_type(state, b_all),
1809 (
unsigned long long)
1812 (
unsigned long long)
1825 static int btrfsic_test_for_metadata(
struct btrfsic_state *state,
1826 char **datav,
unsigned int num_pages)
1833 if (num_pages * PAGE_CACHE_SIZE < state->metablock_size)
1846 crc =
crc32c(crc, data, sublen);
1856 u64 dev_bytenr,
char **mapped_datav,
1857 unsigned int num_pages,
1858 struct bio *bio,
int *bio_is_patched,
1859 struct buffer_head *bh,
1860 int submit_bio_bh_rw)
1868 unsigned int processed_len;
1870 if (
NULL != bio_is_patched)
1871 *bio_is_patched = 0;
1878 is_metadata = (0 == btrfsic_test_for_metadata(state, mapped_datav,
1881 block = btrfsic_block_hashtable_lookup(bdev, dev_bytenr,
1883 if (
NULL != block) {
1890 mapped_datav[0])->bytenr);
1894 "btrfsic: cannot work with too short bios!\n");
1903 "[before new superblock is written]:\n");
1904 btrfsic_dump_tree_sub(state, block, 0);
1912 "btrfsic: cannot work with too short bios!\n");
1917 mapped_datav[0])->bytenr);
1918 btrfsic_cmp_log_and_dev_bytenr(state, bytenr,
1924 "Written block @%llu (%s/%llu/%d)"
1925 " found in hash table, %c,"
1927 " (!= stored %llu).\n",
1928 (
unsigned long long)bytenr,
1930 (
unsigned long long)dev_bytenr,
1932 btrfsic_get_block_type(state, block),
1933 (
unsigned long long)
1939 "Written block @%llu (%s/%llu/%d)"
1940 " found in hash table, %c.\n",
1941 (
unsigned long long)bytenr,
1943 (
unsigned long long)dev_bytenr,
1945 btrfsic_get_block_type(state, block));
1950 "btrfsic: cannot work with too short bios!\n");
1957 "Written block @%llu (%s/%llu/%d)"
1958 " found in hash table, %c.\n",
1959 (
unsigned long long)bytenr,
1961 (
unsigned long long)dev_bytenr,
1963 btrfsic_get_block_type(state, block));
1968 "ref_to_list: %cE, ref_from_list: %cE\n",
1971 if (btrfsic_is_block_ref_by_superblock(state, block, 0)) {
1973 " @%llu (%s/%llu/%d), old(gen=%llu,"
1974 " objectid=%llu, type=%d, offset=%llu),"
1976 " which is referenced by most recent superblock"
1977 " (superblockgen=%llu)!\n",
1978 btrfsic_get_block_type(state, block),
1979 (
unsigned long long)bytenr,
1981 (
unsigned long long)dev_bytenr,
1984 (
unsigned long long)
1987 (
unsigned long long)
1989 (
unsigned long long)
1991 mapped_datav[0])->generation),
1992 (
unsigned long long)
1994 btrfsic_dump_tree(state);
1999 " @%llu (%s/%llu/%d), oldgen=%llu, newgen=%llu,"
2000 " which is not yet iodone!\n",
2001 btrfsic_get_block_type(state, block),
2002 (
unsigned long long)bytenr,
2004 (
unsigned long long)dev_bytenr,
2007 (
unsigned long long)
2009 mapped_datav[0])->generation));
2011 btrfsic_dump_tree(state);
2029 btrfsic_print_rem_link(state, l);
2034 btrfsic_block_link_hashtable_remove(l);
2035 btrfsic_block_link_free(l);
2040 ret = btrfsic_map_superblock(state, bytenr,
2044 ret = btrfsic_map_block(state, bytenr, processed_len,
2048 "btrfsic: btrfsic_map_block(root @%llu)"
2049 " failed!\n", (
unsigned long long)bytenr);
2052 block_ctx.
datav = mapped_datav;
2064 if (!*bio_is_patched) {
2070 bio->bi_private =
block;
2071 bio->bi_end_io = btrfsic_bio_end_io;
2072 *bio_is_patched = 1;
2085 bio->bi_private =
block;
2087 }
else if (
NULL != bh) {
2092 bh->b_private =
block;
2093 bh->b_end_io = btrfsic_bh_end_io;
2110 ret = btrfsic_process_written_superblock(
2118 "[after new superblock is written]:\n");
2119 btrfsic_dump_tree_sub(state, block, 0);
2123 ret = btrfsic_process_metablock(
2131 "btrfsic: btrfsic_process_metablock"
2132 "(root @%llu) failed!\n",
2133 (
unsigned long long)dev_bytenr);
2146 btrfsic_block_hashtable_remove(block);
2148 btrfsic_block_free(block);
2151 btrfsic_release_block_ctx(&block_ctx);
2160 " !found in hash table, D.\n",
2162 (
unsigned long long)dev_bytenr);
2172 block_ctx.
len = processed_len;
2178 mapped_datav[0])->bytenr);
2179 btrfsic_cmp_log_and_dev_bytenr(state, bytenr, dev_state,
2183 "Written block @%llu (%s/%llu/?)"
2184 " !found in hash table, M.\n",
2185 (
unsigned long long)bytenr,
2187 (
unsigned long long)dev_bytenr);
2189 ret = btrfsic_map_block(state, bytenr, processed_len,
2193 "btrfsic: btrfsic_map_block(root @%llu)"
2195 (
unsigned long long)dev_bytenr);
2199 block_ctx.
datav = mapped_datav;
2205 block = btrfsic_block_alloc();
2206 if (
NULL == block) {
2208 btrfsic_release_block_ctx(&block_ctx);
2223 if (!*bio_is_patched) {
2227 bio->bi_private =
block;
2228 bio->bi_end_io = btrfsic_bio_end_io;
2229 *bio_is_patched = 1;
2241 bio->bi_private =
block;
2243 }
else if (
NULL != bh) {
2248 bh->b_private =
block;
2249 bh->b_end_io = btrfsic_bh_end_io;
2258 "New written %c-block @%llu (%s/%llu/%d)\n",
2259 is_metadata ?
'M' :
'D',
2268 ret = btrfsic_process_metablock(state, block,
2272 "btrfsic: process_metablock(root @%llu)"
2274 (
unsigned long long)dev_bytenr);
2276 btrfsic_release_block_ctx(&block_ctx);
2281 dev_bytenr += processed_len;
2287 static void btrfsic_bio_end_io(
struct bio *bp,
int bio_error_status)
2295 if (bio_error_status)
2306 if ((dev_state->
state->print_mask &
2309 "bio_end_io(err=%d) for %c @%llu (%s/%llu/%d)\n",
2311 btrfsic_get_block_type(dev_state->
state, block),
2320 if ((dev_state->
state->print_mask &
2323 "bio_end_io() new %s flush_gen=%llu\n",
2325 (
unsigned long long)
2333 }
while (
NULL != block);
2335 bp->bi_end_io(bp, bio_error_status);
2338 static void btrfsic_bh_end_io(
struct buffer_head *bh,
int uptodate)
2348 "bh_end_io(error=%d) for %c @%llu (%s/%llu/%d)\n",
2350 btrfsic_get_block_type(dev_state->
state, block),
2359 if ((dev_state->
state->print_mask &
2362 "bh_end_io() new %s flush_gen=%llu\n",
2372 bh->b_end_io(bh, uptodate);
2375 static int btrfsic_process_written_superblock(
2382 superblock->
generation = btrfs_super_generation(super_hdr);
2387 "btrfsic: superblock @%llu (%s/%llu/%d)"
2388 " with old gen %llu <= %llu\n",
2393 (
unsigned long long)
2394 btrfs_super_generation(super_hdr),
2395 (
unsigned long long)
2400 "btrfsic: got new superblock @%llu (%s/%llu/%d)"
2401 " with new gen %llu > %llu\n",
2406 (
unsigned long long)
2407 btrfs_super_generation(super_hdr),
2408 (
unsigned long long)
2412 btrfs_super_generation(super_hdr);
2416 for (pass = 0; pass < 3; pass++) {
2424 const char *additional_string =
NULL;
2428 tmp_disk_key.offset = 0;
2432 tmp_disk_key.objectid =
2434 additional_string =
"root ";
2435 next_bytenr = btrfs_super_root(super_hdr);
2439 (
unsigned long long)next_bytenr);
2442 tmp_disk_key.objectid =
2444 additional_string =
"chunk ";
2445 next_bytenr = btrfs_super_chunk_root(super_hdr);
2449 (
unsigned long long)next_bytenr);
2452 tmp_disk_key.objectid =
2454 additional_string =
"log ";
2455 next_bytenr = btrfs_super_log_root(super_hdr);
2456 if (0 == next_bytenr)
2461 (
unsigned long long)next_bytenr);
2470 (
unsigned long long)next_bytenr, num_copies);
2471 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2476 "btrfsic_process_written_superblock("
2477 "mirror_num=%d)\n", mirror_num);
2478 ret = btrfsic_map_block(state, next_bytenr,
2480 &tmp_next_block_ctx,
2484 "btrfsic: btrfsic_map_block(@%llu,"
2485 " mirror=%d) failed!\n",
2486 (
unsigned long long)next_bytenr,
2491 next_block = btrfsic_block_lookup_or_add(
2493 &tmp_next_block_ctx,
2498 if (
NULL == next_block) {
2500 "btrfsic: error, kmalloc failed!\n");
2501 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2505 next_block->
disk_key = tmp_disk_key;
2509 l = btrfsic_block_link_lookup_or_add(
2511 &tmp_next_block_ctx,
2515 btrfsic_release_block_ctx(&tmp_next_block_ctx);
2521 if (-1 == btrfsic_check_all_ref_blocks(state, superblock, 0)) {
2523 btrfsic_dump_tree(state);
2529 static int btrfsic_check_all_ref_blocks(
struct btrfsic_state *state,
2531 int recursion_level)
2552 "btrfsic: abort cyclic linkage (case 1).\n");
2568 "rl=%d, %c @%llu (%s/%llu/%d)"
2569 " %u* refers to %c @%llu (%s/%llu/%d)\n",
2571 btrfsic_get_block_type(state, block),
2578 (
unsigned long long)
2585 " which references block %c @%llu (%s/%llu/%d)"
2586 " which is never written!\n",
2588 (
unsigned long long)
2596 " which references block %c @%llu (%s/%llu/%d)"
2597 " which is not yet iodone!\n",
2599 (
unsigned long long)
2607 " which references block %c @%llu (%s/%llu/%d)"
2608 " which has write error!\n",
2610 (
unsigned long long)
2623 " which references block %c @%llu (%s/%llu/%d)"
2624 " with generation %llu !="
2625 " parent generation %llu!\n",
2627 (
unsigned long long)
2638 " which references block %c @%llu (%s/%llu/%d)"
2639 " which is not flushed out of disk's write cache"
2640 " (block flush_gen=%llu,"
2641 " dev->flush_gen=%llu)!\n",
2643 (
unsigned long long)
2649 (
unsigned long long)
2652 }
else if (-1 == btrfsic_check_all_ref_blocks(state,
2663 static int btrfsic_is_block_ref_by_superblock(
2666 int recursion_level)
2674 "btrfsic: abort cyclic linkage (case 2).\n");
2690 "rl=%d, %c @%llu (%s/%llu/%d)"
2691 " is ref %u* from %c @%llu (%s/%llu/%d)\n",
2693 btrfsic_get_block_type(state, block),
2700 (
unsigned long long)
2703 (
unsigned long long)
2712 else if (btrfsic_is_block_ref_by_superblock(state,
2722 static void btrfsic_print_add_link(
const struct btrfsic_state *state,
2726 "Add %u* link from %c @%llu (%s/%llu/%d)"
2727 " to %c @%llu (%s/%llu/%d).\n",
2741 static void btrfsic_print_rem_link(
const struct btrfsic_state *state,
2745 "Rem %u* link from %c @%llu (%s/%llu/%d)"
2746 " to %c @%llu (%s/%llu/%d).\n",
2760 static char btrfsic_get_block_type(
const struct btrfsic_state *state,
2775 static void btrfsic_dump_tree(
const struct btrfsic_state *state)
2780 static void btrfsic_dump_tree_sub(
const struct btrfsic_state *state,
2786 static char buf[80];
2787 int cursor_position;
2798 indent_add =
sprintf(buf,
"%c-%llu(%s/%llu/%d)",
2799 btrfsic_get_block_type(state, block),
2809 indent_level += indent_add;
2820 cursor_position = indent_level;
2826 while (cursor_position < indent_level) {
2833 indent_add =
sprintf(buf,
" --> ");
2834 if (indent_level + indent_add >
2837 cursor_position = 0;
2844 indent_level + indent_add);
2845 cursor_position = 0;
2854 u64 parent_generation)
2858 l = btrfsic_block_link_hashtable_lookup(next_block_ctx->
dev->bdev,
2864 l = btrfsic_block_link_alloc();
2867 "btrfsic: error, kmalloc" " failed!\n");
2877 btrfsic_print_add_link(state, l);
2882 btrfsic_block_link_hashtable_add(l,
2888 btrfsic_print_add_link(state, l);
2897 const char *additional_string,
2906 block = btrfsic_block_hashtable_lookup(block_ctx->
dev->bdev,
2909 if (
NULL == block) {
2912 block = btrfsic_block_alloc();
2913 if (
NULL == block) {
2917 dev_state = btrfsic_dev_state_lookup(block_ctx->
dev->bdev);
2918 if (
NULL == dev_state) {
2920 "btrfsic: error, lookup dev_state failed!\n");
2921 btrfsic_block_free(block);
2933 "New %s%c-block @%llu (%s/%llu/%d)\n",
2935 btrfsic_get_block_type(state, block),
2942 if (
NULL != was_created)
2945 if (
NULL != was_created)
2952 static void btrfsic_cmp_log_and_dev_bytenr(
struct btrfsic_state *state,
2966 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2968 &block_ctx, mirror_num);
2971 " btrfsic_map_block(logical @%llu,"
2972 " mirror %d) failed!\n",
2973 (
unsigned long long)bytenr, mirror_num);
2977 if (dev_state->
bdev == block_ctx.
dev->bdev &&
2980 btrfsic_release_block_ctx(&block_ctx);
2983 btrfsic_release_block_ctx(&block_ctx);
2987 printk(
KERN_INFO "btrfs: attempt to write M-block which contains logical bytenr that doesn't map to dev+physical bytenr of submit_bio,"
2988 " buffer->log_bytenr=%llu, submit_bio(bdev=%s,"
2989 " phys_bytenr=%llu)!\n",
2990 (
unsigned long long)bytenr, dev_state->
name,
2991 (
unsigned long long)dev_bytenr);
2992 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2993 ret = btrfsic_map_block(state, bytenr,
2995 &block_ctx, mirror_num);
3001 (
unsigned long long)bytenr,
3002 block_ctx.
dev->name,
3015 ds = btrfsic_dev_state_hashtable_lookup(bdev,
3024 if (!btrfsic_is_initialized)
3030 dev_state = btrfsic_dev_state_lookup(bh->b_bdev);
3033 if (
NULL != dev_state &&
3034 (rw &
WRITE) && bh->b_size > 0) {
3037 dev_bytenr = 4096 * bh->b_blocknr;
3038 if (dev_state->
state->print_mask &
3041 "submit_bh(rw=0x%x, blocknr=%lu (bytenr %llu),"
3042 " size=%lu, data=%p, bdev=%p)\n",
3043 rw, (
unsigned long)bh->b_blocknr,
3044 (
unsigned long long)dev_bytenr,
3045 (
unsigned long)bh->b_size, bh->b_data,
3047 btrfsic_process_written_block(dev_state, dev_bytenr,
3048 &bh->b_data, 1,
NULL,
3051 if (dev_state->
state->print_mask &
3054 "submit_bh(rw=0x%x FLUSH, bdev=%p)\n",
3057 if ((dev_state->
state->print_mask &
3061 "btrfsic_submit_bh(%s) with FLUSH"
3062 " but dummy block already in use"
3077 bh->b_private =
block;
3078 bh->b_end_io = btrfsic_bh_end_io;
3089 if (!btrfsic_is_initialized) {
3097 dev_state = btrfsic_dev_state_lookup(bio->bi_bdev);
3098 if (
NULL != dev_state &&
3099 (rw &
WRITE) &&
NULL != bio->bi_io_vec) {
3103 char **mapped_datav;
3105 dev_bytenr = 512 * bio->bi_sector;
3107 if (dev_state->
state->print_mask &
3110 "submit_bio(rw=0x%x, bi_vcnt=%u,"
3111 " bi_sector=%lu (bytenr %llu), bi_bdev=%p)\n",
3112 rw, bio->bi_vcnt, (
unsigned long)bio->bi_sector,
3113 (
unsigned long long)dev_bytenr,
3116 mapped_datav =
kmalloc(
sizeof(*mapped_datav) * bio->bi_vcnt,
3120 for (i = 0; i < bio->bi_vcnt; i++) {
3122 mapped_datav[
i] =
kmap(bio->bi_io_vec[i].bv_page);
3123 if (!mapped_datav[i]) {
3126 kunmap(bio->bi_io_vec[i].bv_page);
3128 kfree(mapped_datav);
3133 (dev_state->
state->print_mask &
3137 "#%u: page=%p, len=%u, offset=%u\n",
3138 i, bio->bi_io_vec[i].bv_page,
3139 bio->bi_io_vec[i].bv_len,
3140 bio->bi_io_vec[i].bv_offset);
3142 btrfsic_process_written_block(dev_state, dev_bytenr,
3143 mapped_datav, bio->bi_vcnt,
3144 bio, &bio_is_patched,
3148 kunmap(bio->bi_io_vec[i].bv_page);
3150 kfree(mapped_datav);
3152 if (dev_state->
state->print_mask &
3155 "submit_bio(rw=0x%x FLUSH, bdev=%p)\n",
3158 if ((dev_state->
state->print_mask &
3162 "btrfsic_submit_bio(%s) with FLUSH"
3163 " but dummy block already in use"
3178 bio->bi_private =
block;
3179 bio->bi_end_io = btrfsic_bio_end_io;
3190 int including_extent_data,
u32 print_mask)
3199 "btrfsic: cannot handle nodesize %d != leafsize %d!\n",
3205 "btrfsic: cannot handle nodesize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3211 "btrfsic: cannot handle leafsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3217 "btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_CACHE_SIZE %ld!\n",
3221 state = kzalloc(
sizeof(*state),
GFP_NOFS);
3222 if (
NULL == state) {
3227 if (!btrfsic_is_initialized) {
3230 btrfsic_is_initialized = 1;
3249 if (!device->
bdev || !device->
name)
3252 ds = btrfsic_dev_state_alloc();
3255 "btrfs check-integrity: kmalloc() failed!\n");
3263 for (p = ds->
name; *p !=
'\0'; p++);
3264 while (p > ds->
name && *p !=
'/')
3269 btrfsic_dev_state_hashtable_add(ds,
3273 ret = btrfsic_process_superblock(state, fs_devices);
3281 btrfsic_dump_database(state);
3283 btrfsic_dump_tree(state);
3298 if (!btrfsic_is_initialized)
3307 if (!device->
bdev || !device->
name)
3310 ds = btrfsic_dev_state_hashtable_lookup(
3315 btrfsic_dev_state_hashtable_remove(ds);
3316 btrfsic_dev_state_free(ds);
3320 if (
NULL == state) {
3322 "btrfsic: error, cannot find state information"
3348 btrfsic_print_rem_link(state, l);
3352 btrfsic_block_link_free(l);
3356 btrfsic_block_free(b_all);
3359 " @%llu (%s/%llu/%d) on umount which is"
3360 " not yet iodone!\n",
3361 btrfsic_get_block_type(state, b_all),