print-tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. #include "print-tree.h"
  8. static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
  9. {
  10. int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
  11. int i;
  12. pr_info("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
  13. btrfs_chunk_length(eb, chunk), btrfs_chunk_owner(eb, chunk),
  14. btrfs_chunk_type(eb, chunk), num_stripes);
  15. for (i = 0 ; i < num_stripes ; i++) {
  16. pr_info("\t\t\tstripe %d devid %llu offset %llu\n", i,
  17. btrfs_stripe_devid_nr(eb, chunk, i),
  18. btrfs_stripe_offset_nr(eb, chunk, i));
  19. }
  20. }
  21. static void print_dev_item(struct extent_buffer *eb,
  22. struct btrfs_dev_item *dev_item)
  23. {
  24. pr_info("\t\tdev item devid %llu total_bytes %llu bytes used %llu\n",
  25. btrfs_device_id(eb, dev_item),
  26. btrfs_device_total_bytes(eb, dev_item),
  27. btrfs_device_bytes_used(eb, dev_item));
  28. }
  29. static void print_extent_data_ref(struct extent_buffer *eb,
  30. struct btrfs_extent_data_ref *ref)
  31. {
  32. pr_cont("extent data backref root %llu objectid %llu offset %llu count %u\n",
  33. btrfs_extent_data_ref_root(eb, ref),
  34. btrfs_extent_data_ref_objectid(eb, ref),
  35. btrfs_extent_data_ref_offset(eb, ref),
  36. btrfs_extent_data_ref_count(eb, ref));
  37. }
  38. static void print_extent_item(struct extent_buffer *eb, int slot, int type)
  39. {
  40. struct btrfs_extent_item *ei;
  41. struct btrfs_extent_inline_ref *iref;
  42. struct btrfs_extent_data_ref *dref;
  43. struct btrfs_shared_data_ref *sref;
  44. struct btrfs_disk_key key;
  45. unsigned long end;
  46. unsigned long ptr;
  47. u32 item_size = btrfs_item_size_nr(eb, slot);
  48. u64 flags;
  49. u64 offset;
  50. int ref_index = 0;
  51. if (unlikely(item_size < sizeof(*ei))) {
  52. btrfs_print_v0_err(eb->fs_info);
  53. btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
  54. }
  55. ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
  56. flags = btrfs_extent_flags(eb, ei);
  57. pr_info("\t\textent refs %llu gen %llu flags %llu\n",
  58. btrfs_extent_refs(eb, ei), btrfs_extent_generation(eb, ei),
  59. flags);
  60. if ((type == BTRFS_EXTENT_ITEM_KEY) &&
  61. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  62. struct btrfs_tree_block_info *info;
  63. info = (struct btrfs_tree_block_info *)(ei + 1);
  64. btrfs_tree_block_key(eb, info, &key);
  65. pr_info("\t\ttree block key (%llu %u %llu) level %d\n",
  66. btrfs_disk_key_objectid(&key), key.type,
  67. btrfs_disk_key_offset(&key),
  68. btrfs_tree_block_level(eb, info));
  69. iref = (struct btrfs_extent_inline_ref *)(info + 1);
  70. } else {
  71. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  72. }
  73. ptr = (unsigned long)iref;
  74. end = (unsigned long)ei + item_size;
  75. while (ptr < end) {
  76. iref = (struct btrfs_extent_inline_ref *)ptr;
  77. type = btrfs_extent_inline_ref_type(eb, iref);
  78. offset = btrfs_extent_inline_ref_offset(eb, iref);
  79. pr_info("\t\tref#%d: ", ref_index++);
  80. switch (type) {
  81. case BTRFS_TREE_BLOCK_REF_KEY:
  82. pr_cont("tree block backref root %llu\n", offset);
  83. break;
  84. case BTRFS_SHARED_BLOCK_REF_KEY:
  85. pr_cont("shared block backref parent %llu\n", offset);
  86. /*
  87. * offset is supposed to be a tree block which
  88. * must be aligned to nodesize.
  89. */
  90. if (!IS_ALIGNED(offset, eb->fs_info->nodesize))
  91. pr_info("\t\t\t(parent %llu is NOT ALIGNED to nodesize %llu)\n",
  92. offset, (unsigned long long)eb->fs_info->nodesize);
  93. break;
  94. case BTRFS_EXTENT_DATA_REF_KEY:
  95. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  96. print_extent_data_ref(eb, dref);
  97. break;
  98. case BTRFS_SHARED_DATA_REF_KEY:
  99. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  100. pr_cont("shared data backref parent %llu count %u\n",
  101. offset, btrfs_shared_data_ref_count(eb, sref));
  102. /*
  103. * offset is supposed to be a tree block which
  104. * must be aligned to nodesize.
  105. */
  106. if (!IS_ALIGNED(offset, eb->fs_info->nodesize))
  107. pr_info("\t\t\t(parent %llu is NOT ALIGNED to nodesize %llu)\n",
  108. offset, (unsigned long long)eb->fs_info->nodesize);
  109. break;
  110. default:
  111. pr_cont("(extent %llu has INVALID ref type %d)\n",
  112. eb->start, type);
  113. return;
  114. }
  115. ptr += btrfs_extent_inline_ref_size(type);
  116. }
  117. WARN_ON(ptr > end);
  118. }
  119. static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
  120. u32 item_size)
  121. {
  122. if (!IS_ALIGNED(item_size, sizeof(u64))) {
  123. pr_warn("BTRFS: uuid item with illegal size %lu!\n",
  124. (unsigned long)item_size);
  125. return;
  126. }
  127. while (item_size) {
  128. __le64 subvol_id;
  129. read_extent_buffer(l, &subvol_id, offset, sizeof(subvol_id));
  130. pr_info("\t\tsubvol_id %llu\n",
  131. (unsigned long long)le64_to_cpu(subvol_id));
  132. item_size -= sizeof(u64);
  133. offset += sizeof(u64);
  134. }
  135. }
  136. /*
  137. * Helper to output refs and locking status of extent buffer. Useful to debug
  138. * race condition related problems.
  139. */
  140. static void print_eb_refs_lock(struct extent_buffer *eb)
  141. {
  142. #ifdef CONFIG_BTRFS_DEBUG
  143. btrfs_info(eb->fs_info,
  144. "refs %u lock (w:%d r:%d bw:%d br:%d sw:%d sr:%d) lock_owner %u current %u",
  145. atomic_read(&eb->refs), atomic_read(&eb->write_locks),
  146. atomic_read(&eb->read_locks),
  147. atomic_read(&eb->blocking_writers),
  148. atomic_read(&eb->blocking_readers),
  149. atomic_read(&eb->spinning_writers),
  150. atomic_read(&eb->spinning_readers),
  151. eb->lock_owner, current->pid);
  152. #endif
  153. }
  154. void btrfs_print_leaf(struct extent_buffer *l)
  155. {
  156. struct btrfs_fs_info *fs_info;
  157. int i;
  158. u32 type, nr;
  159. struct btrfs_item *item;
  160. struct btrfs_root_item *ri;
  161. struct btrfs_dir_item *di;
  162. struct btrfs_inode_item *ii;
  163. struct btrfs_block_group_item *bi;
  164. struct btrfs_file_extent_item *fi;
  165. struct btrfs_extent_data_ref *dref;
  166. struct btrfs_shared_data_ref *sref;
  167. struct btrfs_dev_extent *dev_extent;
  168. struct btrfs_key key;
  169. struct btrfs_key found_key;
  170. if (!l)
  171. return;
  172. fs_info = l->fs_info;
  173. nr = btrfs_header_nritems(l);
  174. btrfs_info(fs_info,
  175. "leaf %llu gen %llu total ptrs %d free space %d owner %llu",
  176. btrfs_header_bytenr(l), btrfs_header_generation(l), nr,
  177. btrfs_leaf_free_space(fs_info, l), btrfs_header_owner(l));
  178. print_eb_refs_lock(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. pr_info("\titem %d key (%llu %u %llu) itemoff %d itemsize %d\n",
  184. i, key.objectid, type, key.offset,
  185. btrfs_item_offset(l, item), btrfs_item_size(l, item));
  186. switch (type) {
  187. case BTRFS_INODE_ITEM_KEY:
  188. ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
  189. pr_info("\t\tinode generation %llu size %llu mode %o\n",
  190. btrfs_inode_generation(l, ii),
  191. btrfs_inode_size(l, ii),
  192. btrfs_inode_mode(l, ii));
  193. break;
  194. case BTRFS_DIR_ITEM_KEY:
  195. di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
  196. btrfs_dir_item_key_to_cpu(l, di, &found_key);
  197. pr_info("\t\tdir oid %llu type %u\n",
  198. found_key.objectid,
  199. btrfs_dir_type(l, di));
  200. break;
  201. case BTRFS_ROOT_ITEM_KEY:
  202. ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
  203. pr_info("\t\troot data bytenr %llu refs %u\n",
  204. btrfs_disk_root_bytenr(l, ri),
  205. btrfs_disk_root_refs(l, ri));
  206. break;
  207. case BTRFS_EXTENT_ITEM_KEY:
  208. case BTRFS_METADATA_ITEM_KEY:
  209. print_extent_item(l, i, type);
  210. break;
  211. case BTRFS_TREE_BLOCK_REF_KEY:
  212. pr_info("\t\ttree block backref\n");
  213. break;
  214. case BTRFS_SHARED_BLOCK_REF_KEY:
  215. pr_info("\t\tshared block backref\n");
  216. break;
  217. case BTRFS_EXTENT_DATA_REF_KEY:
  218. dref = btrfs_item_ptr(l, i,
  219. struct btrfs_extent_data_ref);
  220. print_extent_data_ref(l, dref);
  221. break;
  222. case BTRFS_SHARED_DATA_REF_KEY:
  223. sref = btrfs_item_ptr(l, i,
  224. struct btrfs_shared_data_ref);
  225. pr_info("\t\tshared data backref count %u\n",
  226. btrfs_shared_data_ref_count(l, sref));
  227. break;
  228. case BTRFS_EXTENT_DATA_KEY:
  229. fi = btrfs_item_ptr(l, i,
  230. struct btrfs_file_extent_item);
  231. if (btrfs_file_extent_type(l, fi) ==
  232. BTRFS_FILE_EXTENT_INLINE) {
  233. pr_info("\t\tinline extent data size %llu\n",
  234. btrfs_file_extent_ram_bytes(l, fi));
  235. break;
  236. }
  237. pr_info("\t\textent data disk bytenr %llu nr %llu\n",
  238. btrfs_file_extent_disk_bytenr(l, fi),
  239. btrfs_file_extent_disk_num_bytes(l, fi));
  240. pr_info("\t\textent data offset %llu nr %llu ram %llu\n",
  241. btrfs_file_extent_offset(l, fi),
  242. btrfs_file_extent_num_bytes(l, fi),
  243. btrfs_file_extent_ram_bytes(l, fi));
  244. break;
  245. case BTRFS_EXTENT_REF_V0_KEY:
  246. btrfs_print_v0_err(fs_info);
  247. btrfs_handle_fs_error(fs_info, -EINVAL, NULL);
  248. break;
  249. case BTRFS_BLOCK_GROUP_ITEM_KEY:
  250. bi = btrfs_item_ptr(l, i,
  251. struct btrfs_block_group_item);
  252. pr_info(
  253. "\t\tblock group used %llu chunk_objectid %llu flags %llu\n",
  254. btrfs_disk_block_group_used(l, bi),
  255. btrfs_disk_block_group_chunk_objectid(l, bi),
  256. btrfs_disk_block_group_flags(l, bi));
  257. break;
  258. case BTRFS_CHUNK_ITEM_KEY:
  259. print_chunk(l, btrfs_item_ptr(l, i,
  260. struct btrfs_chunk));
  261. break;
  262. case BTRFS_DEV_ITEM_KEY:
  263. print_dev_item(l, btrfs_item_ptr(l, i,
  264. struct btrfs_dev_item));
  265. break;
  266. case BTRFS_DEV_EXTENT_KEY:
  267. dev_extent = btrfs_item_ptr(l, i,
  268. struct btrfs_dev_extent);
  269. pr_info("\t\tdev extent chunk_tree %llu\n\t\tchunk objectid %llu chunk offset %llu length %llu\n",
  270. btrfs_dev_extent_chunk_tree(l, dev_extent),
  271. btrfs_dev_extent_chunk_objectid(l, dev_extent),
  272. btrfs_dev_extent_chunk_offset(l, dev_extent),
  273. btrfs_dev_extent_length(l, dev_extent));
  274. break;
  275. case BTRFS_PERSISTENT_ITEM_KEY:
  276. pr_info("\t\tpersistent item objectid %llu offset %llu\n",
  277. key.objectid, key.offset);
  278. switch (key.objectid) {
  279. case BTRFS_DEV_STATS_OBJECTID:
  280. pr_info("\t\tdevice stats\n");
  281. break;
  282. default:
  283. pr_info("\t\tunknown persistent item\n");
  284. }
  285. break;
  286. case BTRFS_TEMPORARY_ITEM_KEY:
  287. pr_info("\t\ttemporary item objectid %llu offset %llu\n",
  288. key.objectid, key.offset);
  289. switch (key.objectid) {
  290. case BTRFS_BALANCE_OBJECTID:
  291. pr_info("\t\tbalance status\n");
  292. break;
  293. default:
  294. pr_info("\t\tunknown temporary item\n");
  295. }
  296. break;
  297. case BTRFS_DEV_REPLACE_KEY:
  298. pr_info("\t\tdev replace\n");
  299. break;
  300. case BTRFS_UUID_KEY_SUBVOL:
  301. case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
  302. print_uuid_item(l, btrfs_item_ptr_offset(l, i),
  303. btrfs_item_size_nr(l, i));
  304. break;
  305. };
  306. }
  307. }
  308. void btrfs_print_tree(struct extent_buffer *c, bool follow)
  309. {
  310. struct btrfs_fs_info *fs_info;
  311. int i; u32 nr;
  312. struct btrfs_key key;
  313. int level;
  314. if (!c)
  315. return;
  316. fs_info = c->fs_info;
  317. nr = btrfs_header_nritems(c);
  318. level = btrfs_header_level(c);
  319. if (level == 0) {
  320. btrfs_print_leaf(c);
  321. return;
  322. }
  323. btrfs_info(fs_info,
  324. "node %llu level %d gen %llu total ptrs %d free spc %u owner %llu",
  325. btrfs_header_bytenr(c), level, btrfs_header_generation(c),
  326. nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(fs_info) - nr,
  327. btrfs_header_owner(c));
  328. print_eb_refs_lock(c);
  329. for (i = 0; i < nr; i++) {
  330. btrfs_node_key_to_cpu(c, &key, i);
  331. pr_info("\tkey %d (%llu %u %llu) block %llu gen %llu\n",
  332. i, key.objectid, key.type, key.offset,
  333. btrfs_node_blockptr(c, i),
  334. btrfs_node_ptr_generation(c, i));
  335. }
  336. if (!follow)
  337. return;
  338. for (i = 0; i < nr; i++) {
  339. struct btrfs_key first_key;
  340. struct extent_buffer *next;
  341. btrfs_node_key_to_cpu(c, &first_key, i);
  342. next = read_tree_block(fs_info, btrfs_node_blockptr(c, i),
  343. btrfs_node_ptr_generation(c, i),
  344. level - 1, &first_key);
  345. if (IS_ERR(next)) {
  346. continue;
  347. } else if (!extent_buffer_uptodate(next)) {
  348. free_extent_buffer(next);
  349. continue;
  350. }
  351. if (btrfs_is_leaf(next) &&
  352. level != 1)
  353. BUG();
  354. if (btrfs_header_level(next) !=
  355. level - 1)
  356. BUG();
  357. btrfs_print_tree(next, follow);
  358. free_extent_buffer(next);
  359. }
  360. }