2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
26 static int print_dir_item(struct btrfs_item *item,
27 struct btrfs_dir_item *di)
32 total = btrfs_item_size(item);
34 printf("\t\tdir index %llu type %u\n",
35 (unsigned long long)btrfs_disk_key_objectid(&di->location),
37 printf("\t\tname: %.*s\n",
38 btrfs_dir_name_len(di),(char *)(di + 1));
39 if (btrfs_dir_data_len(di))
40 printf("\t\tdata: %.*s\n", btrfs_dir_data_len(di),
41 (char *)((char *)(di + 1) + btrfs_dir_name_len(di)));
42 len = sizeof(*di) + btrfs_dir_name_len(di) + btrfs_dir_data_len(di);
43 di = (struct btrfs_dir_item *)((char *)di + len);
48 void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
51 u32 nr = btrfs_header_nritems(&l->header);
52 struct btrfs_item *item;
53 struct btrfs_extent_item *ei;
54 struct btrfs_root_item *ri;
55 struct btrfs_dir_item *di;
56 struct btrfs_inode_item *ii;
57 struct btrfs_file_extent_item *fi;
58 struct btrfs_csum_item *ci;
59 struct btrfs_block_group_item *bi;
60 struct btrfs_extent_ref *ref;
63 printf("leaf %llu ptrs %d free space %d generation %llu owner %llu\n",
64 (unsigned long long)btrfs_header_bytenr(&l->header), nr,
65 btrfs_leaf_free_space(root, l),
66 (unsigned long long)btrfs_header_generation(&l->header),
67 (unsigned long long)btrfs_header_owner(&l->header));
69 for (i = 0 ; i < nr ; i++) {
71 type = btrfs_disk_key_type(&item->key);
72 printf("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
74 (unsigned long long)btrfs_disk_key_objectid(&item->key),
75 btrfs_disk_key_type(&item->key),
76 (unsigned long long)btrfs_disk_key_offset(&item->key),
77 btrfs_item_offset(item),
78 btrfs_item_size(item));
80 case BTRFS_INODE_ITEM_KEY:
81 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
82 printf("\t\tinode generation %llu size %llu block group %llu mode %o links %u\n",
83 (unsigned long long)btrfs_inode_generation(ii),
84 (unsigned long long)btrfs_inode_size(ii),
85 (unsigned long long)btrfs_inode_block_group(ii),
87 btrfs_inode_nlink(ii));
89 case BTRFS_DIR_ITEM_KEY:
90 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
91 print_dir_item(l->items + i, di);
93 case BTRFS_XATTR_ITEM_KEY:
94 case BTRFS_DIR_INDEX_KEY:
95 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
96 print_dir_item(l->items + i, di);
98 case BTRFS_ROOT_ITEM_KEY:
99 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
100 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u\n",
101 (unsigned long long)btrfs_root_bytenr(ri),
103 (unsigned long long)btrfs_root_dirid(ri),
104 btrfs_root_refs(ri));
105 if (1 || btrfs_root_refs(ri) == 0) {
106 struct btrfs_key drop_key;
107 btrfs_disk_key_to_cpu(&drop_key,
109 printf("\t\tdrop key %Lu %x %Lu level %d\n",
110 (unsigned long long)drop_key.objectid,
112 (unsigned long long)drop_key.offset,
116 case BTRFS_EXTENT_ITEM_KEY:
117 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
118 printf("\t\textent data refs %u\n",
119 btrfs_extent_refs(ei));
121 case BTRFS_EXTENT_REF_KEY:
122 ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
123 printf("\t\textent back ref root %llu gen %llu "
124 "owner %llu offset %llu\n",
125 (unsigned long long)btrfs_ref_root(ref),
126 (unsigned long long)btrfs_ref_generation(ref),
127 (unsigned long long)btrfs_ref_objectid(ref),
128 (unsigned long long)btrfs_ref_offset(ref));
130 case BTRFS_CSUM_ITEM_KEY:
131 ci = btrfs_item_ptr(l, i,
132 struct btrfs_csum_item);
133 printf("\t\tcsum item\n");
135 case BTRFS_EXTENT_DATA_KEY:
136 fi = btrfs_item_ptr(l, i,
137 struct btrfs_file_extent_item);
138 if (btrfs_file_extent_type(fi) ==
139 BTRFS_FILE_EXTENT_INLINE) {
140 printf("\t\tinline extent data size %u\n",
141 btrfs_file_extent_inline_len(l->items + i));
144 printf("\t\textent data disk byte %llu nr %llu\n",
145 (unsigned long long)btrfs_file_extent_disk_bytenr(fi),
146 (unsigned long long)btrfs_file_extent_disk_num_bytes(fi));
147 printf("\t\textent data offset %llu nr %llu\n",
148 (unsigned long long)btrfs_file_extent_offset(fi),
149 (unsigned long long)btrfs_file_extent_num_bytes(fi));
151 case BTRFS_BLOCK_GROUP_ITEM_KEY:
152 bi = btrfs_item_ptr(l, i,
153 struct btrfs_block_group_item);
154 printf("\t\tblock group used %llu flags %x\n",
155 (unsigned long long)btrfs_block_group_used(bi),
158 case BTRFS_STRING_ITEM_KEY:
159 printf("\t\titem data %.*s\n", btrfs_item_size(item),
160 btrfs_leaf_data(l) + btrfs_item_offset(item));
166 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
170 struct btrfs_node *c;
176 nr = btrfs_header_nritems(&c->header);
177 if (btrfs_is_leaf(c)) {
178 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
181 printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n",
182 (unsigned long long)t->bytenr,
183 btrfs_header_level(&c->header), nr,
184 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
185 (unsigned long long)btrfs_header_generation(&c->header),
186 (unsigned long long)btrfs_header_owner(&c->header));
188 size = btrfs_level_size(root, btrfs_header_level(&c->header) - 1);
189 for (i = 0; i < nr; i++) {
190 u64 blocknr = btrfs_node_blockptr(c, i);
191 printf("\tkey %d (%llu %x %llu) block %llu (%llu) gen %llu\n",
193 (unsigned long long)c->ptrs[i].key.objectid,
195 (unsigned long long)c->ptrs[i].key.offset,
196 (unsigned long long)blocknr,
197 (unsigned long long)blocknr / size,
198 (unsigned long long)btrfs_node_ptr_generation(c, i));
201 for (i = 0; i < nr; i++) {
202 struct btrfs_buffer *next_buf = read_tree_block(root,
203 btrfs_node_blockptr(c, i),
205 struct btrfs_node *next = &next_buf->node;
206 if (btrfs_is_leaf(next) &&
207 btrfs_header_level(&c->header) != 1)
209 if (btrfs_header_level(&next->header) !=
210 btrfs_header_level(&c->header) - 1)
212 btrfs_print_tree(root, next_buf);
213 btrfs_block_release(root, next_buf);