Add a readonly flag open_ctree to force RO opens
[btrfs-progs-unstable.git] / print-tree.c
blobf0757764b6d03659e2ebbbf72a7a2722edf95d12
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <uuid/uuid.h>
22 #include "kerncompat.h"
23 #include "radix-tree.h"
24 #include "ctree.h"
25 #include "disk-io.h"
27 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
28 struct btrfs_dir_item *di)
30 u32 total;
31 u32 cur = 0;
32 u32 len;
33 u32 name_len;
34 u32 data_len;
35 char namebuf[BTRFS_NAME_LEN];
36 struct btrfs_disk_key location;
38 total = btrfs_item_size(eb, item);
39 while(cur < total) {
40 btrfs_dir_item_key(eb, di, &location);
41 printf("\t\tdir index %llu type %u\n",
42 (unsigned long long)btrfs_disk_key_objectid(&location),
43 btrfs_dir_type(eb, di));
44 name_len = btrfs_dir_name_len(eb, di);
45 data_len = btrfs_dir_data_len(eb, di);
46 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
47 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
48 printf("\t\tnamelen %u datalen %u name: %.*s\n",
49 name_len, data_len, len, namebuf);
50 len = sizeof(*di) + name_len + data_len;
51 di = (struct btrfs_dir_item *)((char *)di + len);
52 cur += len;
54 return 0;
57 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
58 struct btrfs_inode_ref *ref)
60 u32 total;
61 u32 cur = 0;
62 u32 len;
63 u32 name_len;
64 char namebuf[BTRFS_NAME_LEN];
65 total = btrfs_item_size(eb, item);
66 while(cur < total) {
67 name_len = btrfs_inode_ref_name_len(eb, ref);
68 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
69 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
70 printf("\t\tinode ref namelen %u name: %.*s\n",
71 name_len, len, namebuf);
72 len = sizeof(*ref) + name_len;
73 ref = (struct btrfs_inode_ref *)((char *)ref + len);
74 cur += len;
76 return 0;
79 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
81 int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
82 int i;
83 printf("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
84 (unsigned long long)btrfs_chunk_length(eb, chunk),
85 (unsigned long long)btrfs_chunk_owner(eb, chunk),
86 (unsigned long long)btrfs_chunk_type(eb, chunk),
87 num_stripes);
88 for (i = 0 ; i < num_stripes ; i++) {
89 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
90 (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
91 (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
94 static void print_dev_item(struct extent_buffer *eb,
95 struct btrfs_dev_item *dev_item)
97 printf("\t\tdev item devid %llu "
98 "total_bytes %llu bytes used %Lu\n",
99 (unsigned long long)btrfs_device_id(eb, dev_item),
100 (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
101 (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
104 static void print_uuids(struct extent_buffer *eb)
106 char fs_uuid[37];
107 char chunk_uuid[37];
108 u8 disk_uuid[BTRFS_UUID_SIZE];
110 read_extent_buffer(eb, disk_uuid, (unsigned long)btrfs_header_fsid(eb),
111 BTRFS_FSID_SIZE);
113 fs_uuid[36] = '\0';
114 uuid_unparse(disk_uuid, fs_uuid);
116 read_extent_buffer(eb, disk_uuid,
117 (unsigned long)btrfs_header_chunk_tree_uuid(eb),
118 BTRFS_UUID_SIZE);
120 chunk_uuid[36] = '\0';
121 uuid_unparse(disk_uuid, chunk_uuid);
122 printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
125 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
127 int i;
128 char *str;
129 struct btrfs_item *item;
130 struct btrfs_extent_item *ei;
131 struct btrfs_root_item *ri;
132 struct btrfs_dir_item *di;
133 struct btrfs_inode_item *ii;
134 struct btrfs_file_extent_item *fi;
135 struct btrfs_csum_item *ci;
136 struct btrfs_block_group_item *bi;
137 struct btrfs_extent_ref *ref;
138 struct btrfs_inode_ref *iref;
139 struct btrfs_dev_extent *dev_extent;
140 struct btrfs_disk_key disk_key;
141 struct btrfs_root_item root_item;
142 struct btrfs_block_group_item bg_item;
143 u32 nr = btrfs_header_nritems(l);
144 u32 type;
146 printf("leaf %llu ptrs %d free space %d generation %llu owner %llu\n",
147 (unsigned long long)btrfs_header_bytenr(l), nr,
148 btrfs_leaf_free_space(root, l),
149 (unsigned long long)btrfs_header_generation(l),
150 (unsigned long long)btrfs_header_owner(l));
151 print_uuids(l);
152 fflush(stdout);
153 for (i = 0 ; i < nr ; i++) {
154 item = btrfs_item_nr(l, i);
155 btrfs_item_key(l, &disk_key, i);
156 type = btrfs_disk_key_type(&disk_key);
157 printf("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
159 (unsigned long long)btrfs_disk_key_objectid(&disk_key),
160 btrfs_disk_key_type(&disk_key),
161 (unsigned long long)btrfs_disk_key_offset(&disk_key),
162 btrfs_item_offset(l, item),
163 btrfs_item_size(l, item));
164 switch (type) {
165 case BTRFS_INODE_ITEM_KEY:
166 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
167 printf("\t\tinode generation %llu size %llu block group %llu mode %o links %u\n",
168 (unsigned long long)btrfs_inode_generation(l, ii),
169 (unsigned long long)btrfs_inode_size(l, ii),
170 (unsigned long long)btrfs_inode_block_group(l,ii),
171 btrfs_inode_mode(l, ii),
172 btrfs_inode_nlink(l, ii));
173 break;
174 case BTRFS_INODE_REF_KEY:
175 iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
176 print_inode_ref_item(l, item, iref);
177 break;
178 case BTRFS_DIR_ITEM_KEY:
179 case BTRFS_DIR_INDEX_KEY:
180 case BTRFS_XATTR_ITEM_KEY:
181 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
182 print_dir_item(l, item, di);
183 break;
184 case BTRFS_ROOT_ITEM_KEY:
185 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
186 read_extent_buffer(l, &root_item, (unsigned long)ri, sizeof(root_item));
187 printf("\t\troot data bytenr %llu level %d dirid %llu refs %u\n",
188 (unsigned long long)btrfs_root_bytenr(&root_item),
189 btrfs_root_level(&root_item),
190 (unsigned long long)btrfs_root_dirid(&root_item),
191 btrfs_root_refs(&root_item));
192 if (1 || btrfs_root_refs(&root_item) == 0) {
193 struct btrfs_key drop_key;
194 btrfs_disk_key_to_cpu(&drop_key,
195 &root_item.drop_progress);
196 printf("\t\tdrop key %Lu %x %Lu level %d\n",
197 (unsigned long long)drop_key.objectid,
198 drop_key.type,
199 (unsigned long long)drop_key.offset,
200 root_item.drop_level);
202 break;
203 case BTRFS_EXTENT_ITEM_KEY:
204 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
205 printf("\t\textent data refs %u\n",
206 btrfs_extent_refs(l, ei));
207 break;
208 case BTRFS_EXTENT_REF_KEY:
209 ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
210 printf("\t\textent back ref root %llu gen %llu "
211 "owner %llu offset %llu\n",
212 (unsigned long long)btrfs_ref_root(l, ref),
213 (unsigned long long)btrfs_ref_generation(l, ref),
214 (unsigned long long)btrfs_ref_objectid(l, ref),
215 (unsigned long long)btrfs_ref_offset(l, ref));
216 break;
217 case BTRFS_CSUM_ITEM_KEY:
218 ci = btrfs_item_ptr(l, i, struct btrfs_csum_item);
219 printf("\t\tcsum item\n");
220 break;
221 case BTRFS_EXTENT_DATA_KEY:
222 fi = btrfs_item_ptr(l, i,
223 struct btrfs_file_extent_item);
224 if (btrfs_file_extent_type(l, fi) ==
225 BTRFS_FILE_EXTENT_INLINE) {
226 printf("\t\tinline extent data size %u\n",
227 btrfs_file_extent_inline_len(l, item));
228 break;
230 printf("\t\textent data disk byte %llu nr %llu\n",
231 (unsigned long long)btrfs_file_extent_disk_bytenr(l, fi),
232 (unsigned long long)btrfs_file_extent_disk_num_bytes(l, fi));
233 printf("\t\textent data offset %llu nr %llu\n",
234 (unsigned long long)btrfs_file_extent_offset(l, fi),
235 (unsigned long long)btrfs_file_extent_num_bytes(l, fi));
236 break;
237 case BTRFS_BLOCK_GROUP_ITEM_KEY:
238 bi = btrfs_item_ptr(l, i,
239 struct btrfs_block_group_item);
240 read_extent_buffer(l, &bg_item, (unsigned long)bi,
241 sizeof(bg_item));
242 printf("\t\tblock group used %llu flags %llu\n",
243 (unsigned long long)btrfs_block_group_used(&bg_item),
244 (unsigned long long)btrfs_block_group_flags(&bg_item));
245 break;
246 case BTRFS_CHUNK_ITEM_KEY:
247 print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
248 break;
249 case BTRFS_DEV_ITEM_KEY:
250 print_dev_item(l, btrfs_item_ptr(l, i,
251 struct btrfs_dev_item));
252 break;
253 case BTRFS_DEV_EXTENT_KEY:
254 dev_extent = btrfs_item_ptr(l, i,
255 struct btrfs_dev_extent);
256 printf("\t\tdev extent chunk_tree %llu\n"
257 "\t\tchunk objectid %llu chunk offset %llu "
258 "length %llu\n",
259 (unsigned long long)
260 btrfs_dev_extent_chunk_tree(l, dev_extent),
261 (unsigned long long)
262 btrfs_dev_extent_chunk_objectid(l, dev_extent),
263 (unsigned long long)
264 btrfs_dev_extent_chunk_offset(l, dev_extent),
265 (unsigned long long)
266 btrfs_dev_extent_length(l, dev_extent));
267 break;
268 case BTRFS_STRING_ITEM_KEY:
269 /* dirty, but it's simple */
270 str = l->data + btrfs_item_ptr_offset(l, i);
271 printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
272 break;
274 fflush(stdout);
278 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb)
280 int i;
281 u32 nr;
282 u32 size;
283 struct btrfs_key key;
285 if (!eb)
286 return;
287 nr = btrfs_header_nritems(eb);
288 if (btrfs_is_leaf(eb)) {
289 btrfs_print_leaf(root, eb);
290 return;
292 printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n",
293 (unsigned long long)eb->start,
294 btrfs_header_level(eb), nr,
295 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
296 (unsigned long long)btrfs_header_generation(eb),
297 (unsigned long long)btrfs_header_owner(eb));
298 print_uuids(eb);
299 fflush(stdout);
300 size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
301 for (i = 0; i < nr; i++) {
302 u64 blocknr = btrfs_node_blockptr(eb, i);
303 btrfs_node_key_to_cpu(eb, &key, i);
304 printf("\tkey %d (%llu %x %llu) block %llu (%llu) gen %llu\n",
306 (unsigned long long)key.objectid,
307 key.type,
308 (unsigned long long)key.offset,
309 (unsigned long long)blocknr,
310 (unsigned long long)blocknr / size,
311 (unsigned long long)btrfs_node_ptr_generation(eb, i));
312 fflush(stdout);
314 for (i = 0; i < nr; i++) {
315 struct extent_buffer *next = read_tree_block(root,
316 btrfs_node_blockptr(eb, i),
317 size);
318 if (btrfs_is_leaf(next) &&
319 btrfs_header_level(eb) != 1)
320 BUG();
321 if (btrfs_header_level(next) !=
322 btrfs_header_level(eb) - 1)
323 BUG();
324 btrfs_print_tree(root, next);
325 free_extent_buffer(next);