print-tree.c 11 KB

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