print-tree.c 3.0 KB
Newer Older
1
#include <linux/module.h>
C
Chris Mason 已提交
2 3 4
#include "ctree.h"
#include "disk-io.h"

C
Chris Mason 已提交
5
void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
C
Chris Mason 已提交
6 7
{
	int i;
8
	u32 nr = btrfs_header_nritems(&l->header);
C
Chris Mason 已提交
9
	struct btrfs_item *item;
C
Chris Mason 已提交
10
	struct btrfs_extent_item *ei;
11
	struct btrfs_root_item *ri;
12
	struct btrfs_dir_item *di;
13
	struct btrfs_inode_item *ii;
14
	u32 type;
15

16
	printk("leaf %Lu total ptrs %d free space %d\n",
C
Chris Mason 已提交
17 18
		btrfs_header_blocknr(&l->header), nr,
		btrfs_leaf_free_space(root, l));
C
Chris Mason 已提交
19 20
	for (i = 0 ; i < nr ; i++) {
		item = l->items + i;
21
		type = btrfs_disk_key_type(&item->key);
22
		printk("\titem %d key (%Lu %x %Lu) itemoff %d itemsize %d\n",
C
Chris Mason 已提交
23
			i,
24 25
			btrfs_disk_key_objectid(&item->key),
			btrfs_disk_key_flags(&item->key),
26
			btrfs_disk_key_offset(&item->key),
C
Chris Mason 已提交
27 28
			btrfs_item_offset(item),
			btrfs_item_size(item));
29 30
		switch (type) {
		case BTRFS_INODE_ITEM_KEY:
31
			ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
32
			printk("\t\tinode generation %Lu size %Lu mode %o\n",
33
			       btrfs_inode_generation(ii),
34 35
			       btrfs_inode_size(ii),
			       btrfs_inode_mode(ii));
36 37
			break;
		case BTRFS_DIR_ITEM_KEY:
38
			di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
39
			printk("\t\tdir oid %Lu flags %u type %u\n",
40
				btrfs_disk_key_objectid(&di->location),
41 42
				btrfs_dir_flags(di),
				btrfs_dir_type(di));
43
			printk("\t\tname %.*s\n",
44
			       btrfs_dir_name_len(di),(char *)(di + 1));
45 46 47
			break;
		case BTRFS_ROOT_ITEM_KEY:
			ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
48
			printk("\t\troot data blocknr %Lu refs %u\n",
49 50 51 52
				btrfs_root_blocknr(ri), btrfs_root_refs(ri));
			break;
		case BTRFS_EXTENT_ITEM_KEY:
			ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
C
Chris Mason 已提交
53 54
			printk("\t\textent data refs %u\n",
				btrfs_extent_refs(ei));
55 56
			break;
		case BTRFS_STRING_ITEM_KEY:
57
			printk("\t\titem data %.*s\n", btrfs_item_size(item),
58 59 60
				btrfs_leaf_data(l) + btrfs_item_offset(item));
			break;
		};
C
Chris Mason 已提交
61 62
	}
}
C
Chris Mason 已提交
63 64

void btrfs_print_tree(struct btrfs_root *root, struct buffer_head *t)
C
Chris Mason 已提交
65 66
{
	int i;
67
	u32 nr;
C
Chris Mason 已提交
68
	struct btrfs_node *c;
C
Chris Mason 已提交
69 70 71

	if (!t)
		return;
C
Chris Mason 已提交
72
	c = btrfs_buffer_node(t);
73 74
	nr = btrfs_header_nritems(&c->header);
	if (btrfs_is_leaf(c)) {
C
Chris Mason 已提交
75
		btrfs_print_leaf(root, (struct btrfs_leaf *)c);
C
Chris Mason 已提交
76 77
		return;
	}
C
Chris Mason 已提交
78 79 80 81
	printk("node %Lu level %d total ptrs %d free spc %u\n",
	       btrfs_header_blocknr(&c->header),
	       btrfs_header_level(&c->header), nr,
	       (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
C
Chris Mason 已提交
82
	for (i = 0; i < nr; i++) {
83
		printk("\tkey %d (%Lu %u %Lu) block %Lu\n",
C
Chris Mason 已提交
84
		       i,
85 86
		       c->ptrs[i].key.objectid,
		       c->ptrs[i].key.flags,
87
		       c->ptrs[i].key.offset,
88
		       btrfs_node_blockptr(c, i));
C
Chris Mason 已提交
89 90
	}
	for (i = 0; i < nr; i++) {
C
Chris Mason 已提交
91
		struct buffer_head *next_buf = read_tree_block(root,
92
						btrfs_node_blockptr(c, i));
C
Chris Mason 已提交
93
		struct btrfs_node *next = btrfs_buffer_node(next_buf);
94 95
		if (btrfs_is_leaf(next) &&
		    btrfs_header_level(&c->header) != 1)
C
Chris Mason 已提交
96
			BUG();
97 98
		if (btrfs_header_level(&next->header) !=
			btrfs_header_level(&c->header) - 1)
C
Chris Mason 已提交
99
			BUG();
C
Chris Mason 已提交
100 101
		btrfs_print_tree(root, next_buf);
		btrfs_block_release(root, next_buf);
C
Chris Mason 已提交
102 103 104
	}
}