print-tree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "ctree.h"
  19. #include "disk-io.h"
  20. #include "print-tree.h"
  21. static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
  22. {
  23. int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
  24. int i;
  25. printk(KERN_INFO "\t\tchunk length %llu owner %llu type %llu "
  26. "num_stripes %d\n",
  27. btrfs_chunk_length(eb, chunk), btrfs_chunk_owner(eb, chunk),
  28. btrfs_chunk_type(eb, chunk), num_stripes);
  29. for (i = 0 ; i < num_stripes ; i++) {
  30. printk(KERN_INFO "\t\t\tstripe %d devid %llu offset %llu\n", i,
  31. btrfs_stripe_devid_nr(eb, chunk, i),
  32. btrfs_stripe_offset_nr(eb, chunk, i));
  33. }
  34. }
  35. static void print_dev_item(struct extent_buffer *eb,
  36. struct btrfs_dev_item *dev_item)
  37. {
  38. printk(KERN_INFO "\t\tdev item devid %llu "
  39. "total_bytes %llu bytes used %llu\n",
  40. btrfs_device_id(eb, dev_item),
  41. btrfs_device_total_bytes(eb, dev_item),
  42. btrfs_device_bytes_used(eb, dev_item));
  43. }
  44. static void print_extent_data_ref(struct extent_buffer *eb,
  45. struct btrfs_extent_data_ref *ref)
  46. {
  47. printk(KERN_INFO "\t\textent data backref root %llu "
  48. "objectid %llu offset %llu count %u\n",
  49. btrfs_extent_data_ref_root(eb, ref),
  50. btrfs_extent_data_ref_objectid(eb, ref),
  51. btrfs_extent_data_ref_offset(eb, ref),
  52. btrfs_extent_data_ref_count(eb, ref));
  53. }
  54. static void print_extent_item(struct extent_buffer *eb, int slot, int type)
  55. {
  56. struct btrfs_extent_item *ei;
  57. struct btrfs_extent_inline_ref *iref;
  58. struct btrfs_extent_data_ref *dref;
  59. struct btrfs_shared_data_ref *sref;
  60. struct btrfs_disk_key key;
  61. unsigned long end;
  62. unsigned long ptr;
  63. u32 item_size = btrfs_item_size_nr(eb, slot);
  64. u64 flags;
  65. u64 offset;
  66. if (item_size < sizeof(*ei)) {
  67. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  68. struct btrfs_extent_item_v0 *ei0;
  69. BUG_ON(item_size != sizeof(*ei0));
  70. ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
  71. printk(KERN_INFO "\t\textent refs %u\n",
  72. btrfs_extent_refs_v0(eb, ei0));
  73. return;
  74. #else
  75. BUG();
  76. #endif
  77. }
  78. ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
  79. flags = btrfs_extent_flags(eb, ei);
  80. printk(KERN_INFO "\t\textent refs %llu gen %llu flags %llu\n",
  81. btrfs_extent_refs(eb, ei), btrfs_extent_generation(eb, ei),
  82. flags);
  83. if ((type == BTRFS_EXTENT_ITEM_KEY) &&
  84. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  85. struct btrfs_tree_block_info *info;
  86. info = (struct btrfs_tree_block_info *)(ei + 1);
  87. btrfs_tree_block_key(eb, info, &key);
  88. printk(KERN_INFO "\t\ttree block key (%llu %u %llu) "
  89. "level %d\n",
  90. btrfs_disk_key_objectid(&key), key.type,
  91. btrfs_disk_key_offset(&key),
  92. btrfs_tree_block_level(eb, info));
  93. iref = (struct btrfs_extent_inline_ref *)(info + 1);
  94. } else {
  95. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  96. }
  97. ptr = (unsigned long)iref;
  98. end = (unsigned long)ei + item_size;
  99. while (ptr < end) {
  100. iref = (struct btrfs_extent_inline_ref *)ptr;
  101. type = btrfs_extent_inline_ref_type(eb, iref);
  102. offset = btrfs_extent_inline_ref_offset(eb, iref);
  103. switch (type) {
  104. case BTRFS_TREE_BLOCK_REF_KEY:
  105. printk(KERN_INFO "\t\ttree block backref "
  106. "root %llu\n", offset);
  107. break;
  108. case BTRFS_SHARED_BLOCK_REF_KEY:
  109. printk(KERN_INFO "\t\tshared block backref "
  110. "parent %llu\n", offset);
  111. break;
  112. case BTRFS_EXTENT_DATA_REF_KEY:
  113. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  114. print_extent_data_ref(eb, dref);
  115. break;
  116. case BTRFS_SHARED_DATA_REF_KEY:
  117. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  118. printk(KERN_INFO "\t\tshared data backref "
  119. "parent %llu count %u\n",
  120. offset, btrfs_shared_data_ref_count(eb, sref));
  121. break;
  122. default:
  123. BUG();
  124. }
  125. ptr += btrfs_extent_inline_ref_size(type);
  126. }
  127. WARN_ON(ptr > end);
  128. }
  129. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  130. static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
  131. {
  132. struct btrfs_extent_ref_v0 *ref0;
  133. ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
  134. printk("\t\textent back ref root %llu gen %llu "
  135. "owner %llu num_refs %lu\n",
  136. btrfs_ref_root_v0(eb, ref0),
  137. btrfs_ref_generation_v0(eb, ref0),
  138. btrfs_ref_objectid_v0(eb, ref0),
  139. (unsigned long)btrfs_ref_count_v0(eb, ref0));
  140. }
  141. #endif
  142. static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
  143. u32 item_size)
  144. {
  145. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  146. pr_warn("BTRFS: uuid item with illegal size %lu!\n",
  147. (unsigned long)item_size);
  148. return;
  149. }
  150. while (item_size) {
  151. __le64 subvol_id;
  152. read_extent_buffer(l, &subvol_id, offset, sizeof(subvol_id));
  153. printk(KERN_INFO "\t\tsubvol_id %llu\n",
  154. (unsigned long long)le64_to_cpu(subvol_id));
  155. item_size -= sizeof(u64);
  156. offset += sizeof(u64);
  157. }
  158. }
  159. void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
  160. {
  161. int i;
  162. u32 type, nr;
  163. struct btrfs_item *item;
  164. struct btrfs_root_item *ri;
  165. struct btrfs_dir_item *di;
  166. struct btrfs_inode_item *ii;
  167. struct btrfs_block_group_item *bi;
  168. struct btrfs_file_extent_item *fi;
  169. struct btrfs_extent_data_ref *dref;
  170. struct btrfs_shared_data_ref *sref;
  171. struct btrfs_dev_extent *dev_extent;
  172. struct btrfs_key key;
  173. struct btrfs_key found_key;
  174. if (!l)
  175. return;
  176. nr = btrfs_header_nritems(l);
  177. btrfs_info(root->fs_info, "leaf %llu total ptrs %d free space %d",
  178. btrfs_header_bytenr(l), nr, btrfs_leaf_free_space(root, l));
  179. for (i = 0 ; i < nr ; i++) {
  180. item = btrfs_item_nr(i);
  181. btrfs_item_key_to_cpu(l, &key, i);
  182. type = key.type;
  183. printk(KERN_INFO "\titem %d key (%llu %u %llu) itemoff %d "
  184. "itemsize %d\n",
  185. i, key.objectid, type, key.offset,
  186. btrfs_item_offset(l, item), btrfs_item_size(l, item));
  187. switch (type) {
  188. case BTRFS_INODE_ITEM_KEY:
  189. ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
  190. printk(KERN_INFO "\t\tinode generation %llu size %llu "
  191. "mode %o\n",
  192. btrfs_inode_generation(l, ii),
  193. btrfs_inode_size(l, ii),
  194. btrfs_inode_mode(l, ii));
  195. break;
  196. case BTRFS_DIR_ITEM_KEY:
  197. di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
  198. btrfs_dir_item_key_to_cpu(l, di, &found_key);
  199. printk(KERN_INFO "\t\tdir oid %llu type %u\n",
  200. found_key.objectid,
  201. btrfs_dir_type(l, di));
  202. break;
  203. case BTRFS_ROOT_ITEM_KEY:
  204. ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
  205. printk(KERN_INFO "\t\troot data bytenr %llu refs %u\n",
  206. btrfs_disk_root_bytenr(l, ri),
  207. btrfs_disk_root_refs(l, ri));
  208. break;
  209. case BTRFS_EXTENT_ITEM_KEY:
  210. case BTRFS_METADATA_ITEM_KEY:
  211. print_extent_item(l, i, type);
  212. break;
  213. case BTRFS_TREE_BLOCK_REF_KEY:
  214. printk(KERN_INFO "\t\ttree block backref\n");
  215. break;
  216. case BTRFS_SHARED_BLOCK_REF_KEY:
  217. printk(KERN_INFO "\t\tshared block backref\n");
  218. break;
  219. case BTRFS_EXTENT_DATA_REF_KEY:
  220. dref = btrfs_item_ptr(l, i,
  221. struct btrfs_extent_data_ref);
  222. print_extent_data_ref(l, dref);
  223. break;
  224. case BTRFS_SHARED_DATA_REF_KEY:
  225. sref = btrfs_item_ptr(l, i,
  226. struct btrfs_shared_data_ref);
  227. printk(KERN_INFO "\t\tshared data backref count %u\n",
  228. btrfs_shared_data_ref_count(l, sref));
  229. break;
  230. case BTRFS_EXTENT_DATA_KEY:
  231. fi = btrfs_item_ptr(l, i,
  232. struct btrfs_file_extent_item);
  233. if (btrfs_file_extent_type(l, fi) ==
  234. BTRFS_FILE_EXTENT_INLINE) {
  235. printk(KERN_INFO "\t\tinline extent data "
  236. "size %u\n",
  237. btrfs_file_extent_inline_len(l, i, fi));
  238. break;
  239. }
  240. printk(KERN_INFO "\t\textent data disk bytenr %llu "
  241. "nr %llu\n",
  242. btrfs_file_extent_disk_bytenr(l, fi),
  243. btrfs_file_extent_disk_num_bytes(l, fi));
  244. printk(KERN_INFO "\t\textent data offset %llu "
  245. "nr %llu ram %llu\n",
  246. btrfs_file_extent_offset(l, fi),
  247. btrfs_file_extent_num_bytes(l, fi),
  248. btrfs_file_extent_ram_bytes(l, fi));
  249. break;
  250. case BTRFS_EXTENT_REF_V0_KEY:
  251. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  252. print_extent_ref_v0(l, i);
  253. #else
  254. BUG();
  255. #endif
  256. break;
  257. case BTRFS_BLOCK_GROUP_ITEM_KEY:
  258. bi = btrfs_item_ptr(l, i,
  259. struct btrfs_block_group_item);
  260. printk(KERN_INFO "\t\tblock group used %llu\n",
  261. btrfs_disk_block_group_used(l, bi));
  262. break;
  263. case BTRFS_CHUNK_ITEM_KEY:
  264. print_chunk(l, btrfs_item_ptr(l, i,
  265. struct btrfs_chunk));
  266. break;
  267. case BTRFS_DEV_ITEM_KEY:
  268. print_dev_item(l, btrfs_item_ptr(l, i,
  269. struct btrfs_dev_item));
  270. break;
  271. case BTRFS_DEV_EXTENT_KEY:
  272. dev_extent = btrfs_item_ptr(l, i,
  273. struct btrfs_dev_extent);
  274. printk(KERN_INFO "\t\tdev extent chunk_tree %llu\n"
  275. "\t\tchunk objectid %llu chunk offset %llu "
  276. "length %llu\n",
  277. btrfs_dev_extent_chunk_tree(l, dev_extent),
  278. btrfs_dev_extent_chunk_objectid(l, dev_extent),
  279. btrfs_dev_extent_chunk_offset(l, dev_extent),
  280. btrfs_dev_extent_length(l, dev_extent));
  281. break;
  282. case BTRFS_DEV_STATS_KEY:
  283. printk(KERN_INFO "\t\tdevice stats\n");
  284. break;
  285. case BTRFS_DEV_REPLACE_KEY:
  286. printk(KERN_INFO "\t\tdev replace\n");
  287. break;
  288. case BTRFS_UUID_KEY_SUBVOL:
  289. case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
  290. print_uuid_item(l, btrfs_item_ptr_offset(l, i),
  291. btrfs_item_size_nr(l, i));
  292. break;
  293. };
  294. }
  295. }
  296. void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
  297. {
  298. int i; u32 nr;
  299. struct btrfs_key key;
  300. int level;
  301. if (!c)
  302. return;
  303. nr = btrfs_header_nritems(c);
  304. level = btrfs_header_level(c);
  305. if (level == 0) {
  306. btrfs_print_leaf(root, c);
  307. return;
  308. }
  309. btrfs_info(root->fs_info, "node %llu level %d total ptrs %d free spc %u",
  310. btrfs_header_bytenr(c), level, nr,
  311. (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
  312. for (i = 0; i < nr; i++) {
  313. btrfs_node_key_to_cpu(c, &key, i);
  314. printk(KERN_INFO "\tkey %d (%llu %u %llu) block %llu\n",
  315. i, key.objectid, key.type, key.offset,
  316. btrfs_node_blockptr(c, i));
  317. }
  318. for (i = 0; i < nr; i++) {
  319. struct extent_buffer *next = read_tree_block(root,
  320. btrfs_node_blockptr(c, i),
  321. btrfs_node_ptr_generation(c, i));
  322. if (btrfs_is_leaf(next) &&
  323. level != 1)
  324. BUG();
  325. if (btrfs_header_level(next) !=
  326. level - 1)
  327. BUG();
  328. btrfs_print_tree(root, next);
  329. free_extent_buffer(next);
  330. }
  331. }