Btrfs image tool
[platform/upstream/btrfs-progs.git] / print-tree.c
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
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"
26
27 static int print_dir_item(struct extent_buffer *eb, struct btrfs_item *item,
28                           struct btrfs_dir_item *di)
29 {
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;
37
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;
53         }
54         return 0;
55 }
56
57 static int print_inode_ref_item(struct extent_buffer *eb, struct btrfs_item *item,
58                                 struct btrfs_inode_ref *ref)
59 {
60         u32 total;
61         u32 cur = 0;
62         u32 len;
63         u32 name_len;
64         u64 index;
65         char namebuf[BTRFS_NAME_LEN];
66         total = btrfs_item_size(eb, item);
67         while(cur < total) {
68                 name_len = btrfs_inode_ref_name_len(eb, ref);
69                 index = btrfs_inode_ref_index(eb, ref);
70                 len = (name_len <= sizeof(namebuf))? name_len: sizeof(namebuf);
71                 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
72                 printf("\t\tinode ref index %llu namelen %u name: %.*s\n",
73                        index, name_len, len, namebuf);
74                 len = sizeof(*ref) + name_len;
75                 ref = (struct btrfs_inode_ref *)((char *)ref + len);
76                 cur += len;
77         }
78         return 0;
79 }
80
81 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
82 {
83         int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
84         int i;
85         printf("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
86                (unsigned long long)btrfs_chunk_length(eb, chunk),
87                (unsigned long long)btrfs_chunk_owner(eb, chunk),
88                (unsigned long long)btrfs_chunk_type(eb, chunk),
89                num_stripes);
90         for (i = 0 ; i < num_stripes ; i++) {
91                 printf("\t\t\tstripe %d devid %llu offset %llu\n", i,
92                       (unsigned long long)btrfs_stripe_devid_nr(eb, chunk, i),
93                       (unsigned long long)btrfs_stripe_offset_nr(eb, chunk, i));
94         }
95 }
96 static void print_dev_item(struct extent_buffer *eb,
97                            struct btrfs_dev_item *dev_item)
98 {
99         printf("\t\tdev item devid %llu "
100                "total_bytes %llu bytes used %Lu\n",
101                (unsigned long long)btrfs_device_id(eb, dev_item),
102                (unsigned long long)btrfs_device_total_bytes(eb, dev_item),
103                (unsigned long long)btrfs_device_bytes_used(eb, dev_item));
104 }
105
106 static void print_uuids(struct extent_buffer *eb)
107 {
108         char fs_uuid[37];
109         char chunk_uuid[37];
110         u8 disk_uuid[BTRFS_UUID_SIZE];
111
112         read_extent_buffer(eb, disk_uuid, (unsigned long)btrfs_header_fsid(eb),
113                            BTRFS_FSID_SIZE);
114
115         fs_uuid[36] = '\0';
116         uuid_unparse(disk_uuid, fs_uuid);
117
118         read_extent_buffer(eb, disk_uuid,
119                            (unsigned long)btrfs_header_chunk_tree_uuid(eb),
120                            BTRFS_UUID_SIZE);
121
122         chunk_uuid[36] = '\0';
123         uuid_unparse(disk_uuid, chunk_uuid);
124         printf("fs uuid %s\nchunk uuid %s\n", fs_uuid, chunk_uuid);
125 }
126
127 static void print_file_extent_item(struct extent_buffer *eb,
128                                    struct btrfs_item *item,
129                                    struct btrfs_file_extent_item *fi)
130 {
131         int extent_type = btrfs_file_extent_type(eb, fi);
132
133         if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
134                 printf("\t\tinline extent data size %u "
135                        "ram %u compress %d\n",
136                   btrfs_file_extent_inline_item_len(eb, item),
137                   btrfs_file_extent_inline_len(eb, fi),
138                   btrfs_file_extent_compression(eb, fi));
139                 return;
140         }
141         if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
142                 printf("\t\tprealloc data disk byte %llu nr %llu\n",
143                   (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
144                   (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
145                 printf("\t\tprealloc data offset %llu nr %llu\n",
146                   (unsigned long long)btrfs_file_extent_offset(eb, fi),
147                   (unsigned long long)btrfs_file_extent_num_bytes(eb, fi));
148                 return;
149         }
150         printf("\t\textent data disk byte %llu nr %llu\n",
151                 (unsigned long long)btrfs_file_extent_disk_bytenr(eb, fi),
152                 (unsigned long long)btrfs_file_extent_disk_num_bytes(eb, fi));
153         printf("\t\textent data offset %llu nr %llu ram %llu\n",
154                 (unsigned long long)btrfs_file_extent_offset(eb, fi),
155                 (unsigned long long)btrfs_file_extent_num_bytes(eb, fi),
156                 (unsigned long long)btrfs_file_extent_ram_bytes(eb, fi));
157         printf("\t\textent compression %d\n",
158                btrfs_file_extent_compression(eb, fi));
159 }
160
161
162 static void print_root_ref(struct extent_buffer *leaf, int slot, char *tag)
163 {
164         struct btrfs_root_ref *ref;
165         char namebuf[BTRFS_NAME_LEN];
166         int namelen;
167
168         ref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref);
169         namelen = btrfs_root_ref_name_len(leaf, ref);
170         read_extent_buffer(leaf, namebuf, (unsigned long)(ref + 1), namelen);
171         printf("\t\troot %s key dirid %llu sequence %llu name %.*s\n", tag,
172                (unsigned long long)btrfs_root_ref_dirid(leaf, ref),
173                (unsigned long long)btrfs_root_ref_sequence(leaf, ref),
174                namelen, namebuf);
175 }
176
177 void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
178 {
179         int i;
180         char *str;
181         struct btrfs_item *item;
182         struct btrfs_extent_item *ei;
183         struct btrfs_root_item *ri;
184         struct btrfs_dir_item *di;
185         struct btrfs_inode_item *ii;
186         struct btrfs_file_extent_item *fi;
187         struct btrfs_csum_item *ci;
188         struct btrfs_block_group_item *bi;
189         struct btrfs_extent_ref *ref;
190         struct btrfs_inode_ref *iref;
191         struct btrfs_dev_extent *dev_extent;
192         struct btrfs_disk_key disk_key;
193         struct btrfs_root_item root_item;
194         struct btrfs_block_group_item bg_item;
195         u32 nr = btrfs_header_nritems(l);
196         u32 type;
197
198         printf("leaf %llu ptrs %d free space %d generation %llu owner %llu\n",
199                 (unsigned long long)btrfs_header_bytenr(l), nr,
200                 btrfs_leaf_free_space(root, l),
201                 (unsigned long long)btrfs_header_generation(l),
202                 (unsigned long long)btrfs_header_owner(l));
203         print_uuids(l);
204         fflush(stdout);
205         for (i = 0 ; i < nr ; i++) {
206                 item = btrfs_item_nr(l, i);
207                 btrfs_item_key(l, &disk_key, i);
208                 type = btrfs_disk_key_type(&disk_key);
209                 printf("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
210                         i,
211                         (unsigned long long)btrfs_disk_key_objectid(&disk_key),
212                         btrfs_disk_key_type(&disk_key),
213                         (unsigned long long)btrfs_disk_key_offset(&disk_key),
214                         btrfs_item_offset(l, item),
215                         btrfs_item_size(l, item));
216                 switch (type) {
217                 case BTRFS_INODE_ITEM_KEY:
218                         ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
219                         printf("\t\tinode generation %llu size %llu block group %llu mode %o links %u\n",
220                                (unsigned long long)btrfs_inode_generation(l, ii),
221                                (unsigned long long)btrfs_inode_size(l, ii),
222                                (unsigned long long)btrfs_inode_block_group(l,ii),
223                                btrfs_inode_mode(l, ii),
224                                btrfs_inode_nlink(l, ii));
225                         break;
226                 case BTRFS_INODE_REF_KEY:
227                         iref = btrfs_item_ptr(l, i, struct btrfs_inode_ref);
228                         print_inode_ref_item(l, item, iref);
229                         break;
230                 case BTRFS_DIR_ITEM_KEY:
231                 case BTRFS_DIR_INDEX_KEY:
232                 case BTRFS_XATTR_ITEM_KEY:
233                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
234                         print_dir_item(l, item, di);
235                         break;
236                 case BTRFS_ORPHAN_ITEM_KEY:
237                         printf("\t\torphan item\n");
238                         break;
239                 case BTRFS_ROOT_ITEM_KEY:
240                         ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
241                         read_extent_buffer(l, &root_item, (unsigned long)ri, sizeof(root_item));
242                         printf("\t\troot data bytenr %llu level %d dirid %llu refs %u\n",
243                                 (unsigned long long)btrfs_root_bytenr(&root_item),
244                                 btrfs_root_level(&root_item),
245                                 (unsigned long long)btrfs_root_dirid(&root_item),
246                                 btrfs_root_refs(&root_item));
247                         if (1 || btrfs_root_refs(&root_item) == 0) {
248                                 struct btrfs_key drop_key;
249                                 btrfs_disk_key_to_cpu(&drop_key,
250                                                       &root_item.drop_progress);
251                                 printf("\t\tdrop key %Lu %x %Lu level %d\n",
252                                        (unsigned long long)drop_key.objectid,
253                                        drop_key.type,
254                                        (unsigned long long)drop_key.offset,
255                                        root_item.drop_level);
256                         }
257                         break;
258                 case BTRFS_ROOT_REF_KEY:
259                         print_root_ref(l, i, "ref");
260                         break;
261                 case BTRFS_ROOT_BACKREF_KEY:
262                         print_root_ref(l, i, "backref");
263                         break;
264                 case BTRFS_EXTENT_ITEM_KEY:
265                         ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
266                         printf("\t\textent data refs %u\n",
267                                 btrfs_extent_refs(l, ei));
268                         break;
269                 case BTRFS_EXTENT_REF_KEY:
270                         ref = btrfs_item_ptr(l, i, struct btrfs_extent_ref);
271                         printf("\t\textent back ref root %llu gen %llu "
272                                "owner %llu num_refs %lu\n",
273                                (unsigned long long)btrfs_ref_root(l, ref),
274                                (unsigned long long)btrfs_ref_generation(l, ref),
275                                (unsigned long long)btrfs_ref_objectid(l, ref),
276                                (unsigned long)btrfs_ref_num_refs(l, ref));
277                         break;
278                 case BTRFS_CSUM_ITEM_KEY:
279                         ci = btrfs_item_ptr(l, i, struct btrfs_csum_item);
280                         printf("\t\tcsum item\n");
281                         break;
282                 case BTRFS_EXTENT_DATA_KEY:
283                         fi = btrfs_item_ptr(l, i,
284                                             struct btrfs_file_extent_item);
285                         print_file_extent_item(l, item, fi);
286                         break;
287                 case BTRFS_BLOCK_GROUP_ITEM_KEY:
288                         bi = btrfs_item_ptr(l, i,
289                                             struct btrfs_block_group_item);
290                         read_extent_buffer(l, &bg_item, (unsigned long)bi,
291                                            sizeof(bg_item));
292                         printf("\t\tblock group used %llu flags %llu\n",
293                                (unsigned long long)btrfs_block_group_used(&bg_item),
294                                (unsigned long long)btrfs_block_group_flags(&bg_item));
295                         break;
296                 case BTRFS_CHUNK_ITEM_KEY:
297                         print_chunk(l, btrfs_item_ptr(l, i, struct btrfs_chunk));
298                         break;
299                 case BTRFS_DEV_ITEM_KEY:
300                         print_dev_item(l, btrfs_item_ptr(l, i,
301                                         struct btrfs_dev_item));
302                         break;
303                 case BTRFS_DEV_EXTENT_KEY:
304                         dev_extent = btrfs_item_ptr(l, i,
305                                                     struct btrfs_dev_extent);
306                         printf("\t\tdev extent chunk_tree %llu\n"
307                                "\t\tchunk objectid %llu chunk offset %llu "
308                                "length %llu\n",
309                                (unsigned long long)
310                                btrfs_dev_extent_chunk_tree(l, dev_extent),
311                                (unsigned long long)
312                                btrfs_dev_extent_chunk_objectid(l, dev_extent),
313                                (unsigned long long)
314                                btrfs_dev_extent_chunk_offset(l, dev_extent),
315                                (unsigned long long)
316                                btrfs_dev_extent_length(l, dev_extent));
317                         break;
318                 case BTRFS_STRING_ITEM_KEY:
319                         /* dirty, but it's simple */
320                         str = l->data + btrfs_item_ptr_offset(l, i);
321                         printf("\t\titem data %.*s\n", btrfs_item_size(l, item), str);
322                         break;
323                 };
324                 fflush(stdout);
325         }
326 }
327
328 void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb)
329 {
330         int i;
331         u32 nr;
332         u32 size;
333         struct btrfs_key key;
334
335         if (!eb)
336                 return;
337         nr = btrfs_header_nritems(eb);
338         if (btrfs_is_leaf(eb)) {
339                 btrfs_print_leaf(root, eb);
340                 return;
341         }
342         printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n",
343                (unsigned long long)eb->start,
344                 btrfs_header_level(eb), nr,
345                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
346                 (unsigned long long)btrfs_header_generation(eb),
347                 (unsigned long long)btrfs_header_owner(eb));
348         print_uuids(eb);
349         fflush(stdout);
350         size = btrfs_level_size(root, btrfs_header_level(eb) - 1);
351         for (i = 0; i < nr; i++) {
352                 u64 blocknr = btrfs_node_blockptr(eb, i);
353                 btrfs_node_key_to_cpu(eb, &key, i);
354                 printf("\tkey %d (%llu %x %llu) block %llu (%llu) gen %llu\n",
355                        i,
356                        (unsigned long long)key.objectid,
357                        key.type,
358                        (unsigned long long)key.offset,
359                        (unsigned long long)blocknr,
360                        (unsigned long long)blocknr / size,
361                        (unsigned long long)btrfs_node_ptr_generation(eb, i));
362                 fflush(stdout);
363         }
364         for (i = 0; i < nr; i++) {
365                 struct extent_buffer *next = read_tree_block(root,
366                                              btrfs_node_blockptr(eb, i),
367                                              size,
368                                              btrfs_node_ptr_generation(eb, i));
369                 if (btrfs_is_leaf(next) &&
370                     btrfs_header_level(eb) != 1)
371                         BUG();
372                 if (btrfs_header_level(next) !=
373                         btrfs_header_level(eb) - 1)
374                         BUG();
375                 btrfs_print_tree(root, next);
376                 free_extent_buffer(next);
377         }
378 }
379