print-tree.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/module.h>
  19. #include "ctree.h"
  20. #include "disk-io.h"
  21. #include "print-tree.h"
  22. void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
  23. {
  24. int i;
  25. u32 nr = btrfs_header_nritems(&l->header);
  26. struct btrfs_item *item;
  27. struct btrfs_extent_item *ei;
  28. struct btrfs_root_item *ri;
  29. struct btrfs_dir_item *di;
  30. struct btrfs_inode_item *ii;
  31. struct btrfs_block_group_item *bi;
  32. u32 type;
  33. printk("leaf %llu total ptrs %d free space %d\n",
  34. (unsigned long long)btrfs_header_blocknr(&l->header), nr,
  35. btrfs_leaf_free_space(root, l));
  36. for (i = 0 ; i < nr ; i++) {
  37. item = l->items + i;
  38. type = btrfs_disk_key_type(&item->key);
  39. printk("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
  40. i,
  41. (unsigned long long)btrfs_disk_key_objectid(&item->key),
  42. btrfs_disk_key_flags(&item->key),
  43. (unsigned long long)btrfs_disk_key_offset(&item->key),
  44. btrfs_item_offset(item),
  45. btrfs_item_size(item));
  46. switch (type) {
  47. case BTRFS_INODE_ITEM_KEY:
  48. ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
  49. printk("\t\tinode generation %llu size %llu mode %o\n",
  50. (unsigned long long)btrfs_inode_generation(ii),
  51. (unsigned long long)btrfs_inode_size(ii),
  52. btrfs_inode_mode(ii));
  53. break;
  54. case BTRFS_DIR_ITEM_KEY:
  55. di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
  56. printk("\t\tdir oid %llu flags %u type %u\n",
  57. (unsigned long long)btrfs_disk_key_objectid(
  58. &di->location),
  59. btrfs_dir_flags(di),
  60. btrfs_dir_type(di));
  61. printk("\t\tname %.*s\n",
  62. btrfs_dir_name_len(di),(char *)(di + 1));
  63. break;
  64. case BTRFS_ROOT_ITEM_KEY:
  65. ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
  66. printk("\t\troot data blocknr %llu refs %u\n",
  67. (unsigned long long)btrfs_root_blocknr(ri),
  68. btrfs_root_refs(ri));
  69. break;
  70. case BTRFS_EXTENT_ITEM_KEY:
  71. ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
  72. printk("\t\textent data refs %u\n",
  73. btrfs_extent_refs(ei));
  74. break;
  75. case BTRFS_BLOCK_GROUP_ITEM_KEY:
  76. bi = btrfs_item_ptr(l, i,
  77. struct btrfs_block_group_item);
  78. printk("\t\tblock group used %llu\n",
  79. (unsigned long long)btrfs_block_group_used(bi));
  80. break;
  81. case BTRFS_STRING_ITEM_KEY:
  82. printk("\t\titem data %.*s\n", btrfs_item_size(item),
  83. btrfs_leaf_data(l) + btrfs_item_offset(item));
  84. break;
  85. };
  86. }
  87. }
  88. void btrfs_print_tree(struct btrfs_root *root, struct buffer_head *t)
  89. {
  90. int i;
  91. u32 nr;
  92. struct btrfs_node *c;
  93. if (!t)
  94. return;
  95. c = btrfs_buffer_node(t);
  96. nr = btrfs_header_nritems(&c->header);
  97. if (btrfs_is_leaf(c)) {
  98. btrfs_print_leaf(root, (struct btrfs_leaf *)c);
  99. return;
  100. }
  101. printk("node %llu level %d total ptrs %d free spc %u\n",
  102. (unsigned long long)btrfs_header_blocknr(&c->header),
  103. btrfs_header_level(&c->header), nr,
  104. (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
  105. for (i = 0; i < nr; i++) {
  106. printk("\tkey %d (%llu %u %llu) block %llu\n",
  107. i,
  108. (unsigned long long)c->ptrs[i].key.objectid,
  109. c->ptrs[i].key.flags,
  110. (unsigned long long)c->ptrs[i].key.offset,
  111. (unsigned long long)btrfs_node_blockptr(c, i));
  112. }
  113. for (i = 0; i < nr; i++) {
  114. struct buffer_head *next_buf = read_tree_block(root,
  115. btrfs_node_blockptr(c, i));
  116. struct btrfs_node *next = btrfs_buffer_node(next_buf);
  117. if (btrfs_is_leaf(next) &&
  118. btrfs_header_level(&c->header) != 1)
  119. BUG();
  120. if (btrfs_header_level(&next->header) !=
  121. btrfs_header_level(&c->header) - 1)
  122. BUG();
  123. btrfs_print_tree(root, next_buf);
  124. btrfs_block_release(root, next_buf);
  125. }
  126. }