add GPLv2
[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 "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25
26 static int print_dir_item(struct btrfs_item *item,
27                           struct btrfs_dir_item *di)
28 {
29         u32 total;
30         u32 cur = 0;
31         u32 len;
32         total = btrfs_item_size(item);
33         while(cur < total) {
34                 printf("\t\tdir index %llu flags %u type %u\n",
35                      (unsigned long long)btrfs_disk_key_objectid(&di->location),
36                      btrfs_dir_flags(di),
37                      btrfs_dir_type(di));
38                 printf("\t\tname %.*s\n",
39                        btrfs_dir_name_len(di),(char *)(di + 1));
40                 len = sizeof(*di) + btrfs_dir_name_len(di);
41                 di = (struct btrfs_dir_item *)((char *)di + len);
42                 cur += len;
43         }
44         return 0;
45 }
46 void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
47 {
48         int i;
49         u32 nr = btrfs_header_nritems(&l->header);
50         struct btrfs_item *item;
51         struct btrfs_extent_item *ei;
52         struct btrfs_root_item *ri;
53         struct btrfs_dir_item *di;
54         struct btrfs_inode_item *ii;
55         struct btrfs_file_extent_item *fi;
56         struct btrfs_csum_item *ci;
57         struct btrfs_block_group_item *bi;
58         u32 type;
59
60         printf("leaf %llu ptrs %d free space %d generation %llu owner %llu\n",
61                 (unsigned long long)btrfs_header_blocknr(&l->header), nr,
62                 btrfs_leaf_free_space(root, l),
63                 (unsigned long long)btrfs_header_generation(&l->header),
64                 (unsigned long long)btrfs_header_owner(&l->header));
65         fflush(stdout);
66         for (i = 0 ; i < nr ; i++) {
67                 item = l->items + i;
68                 type = btrfs_disk_key_type(&item->key);
69                 printf("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
70                         i,
71                         (unsigned long long)btrfs_disk_key_objectid(&item->key),
72                         btrfs_disk_key_flags(&item->key),
73                         (unsigned long long)btrfs_disk_key_offset(&item->key),
74                         btrfs_item_offset(item),
75                         btrfs_item_size(item));
76                 switch (type) {
77                 case BTRFS_INODE_ITEM_KEY:
78                         ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
79                         printf("\t\tinode generation %llu size %llu block group %llu mode %o\n",
80                                (unsigned long long)btrfs_inode_generation(ii),
81                                (unsigned long long)btrfs_inode_size(ii),
82                                (unsigned long long)btrfs_inode_block_group(ii),
83                                btrfs_inode_mode(ii));
84                         break;
85                 case BTRFS_DIR_ITEM_KEY:
86                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
87                         print_dir_item(l->items + i, di);
88                         break;
89                 case BTRFS_DIR_INDEX_KEY:
90                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
91                         print_dir_item(l->items + i, di);
92                         break;
93                 case BTRFS_ROOT_ITEM_KEY:
94                         ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
95                         printf("\t\troot data blocknr %llu dirid %llu refs %u\n",
96                                 (unsigned long long)btrfs_root_blocknr(ri),
97                                 (unsigned long long)btrfs_root_dirid(ri),
98                                 btrfs_root_refs(ri));
99                         break;
100                 case BTRFS_EXTENT_ITEM_KEY:
101                         ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
102                         printf("\t\textent data refs %u owner %llu\n",
103                                 btrfs_extent_refs(ei),
104                                 (unsigned long long)btrfs_extent_owner(ei));
105                         break;
106                 case BTRFS_CSUM_ITEM_KEY:
107                         ci = btrfs_item_ptr(l, i,
108                                             struct btrfs_csum_item);
109                         printf("\t\tcsum item\n");
110                         break;
111                 case BTRFS_EXTENT_DATA_KEY:
112                         fi = btrfs_item_ptr(l, i,
113                                             struct btrfs_file_extent_item);
114                         if (btrfs_file_extent_type(fi) ==
115                             BTRFS_FILE_EXTENT_INLINE) {
116                                 printf("\t\tinline extent data size %u\n",
117                                    btrfs_file_extent_inline_len(l->items + i));
118                                 break;
119                         }
120                         printf("\t\textent data disk block %llu nr %llu\n",
121                                (unsigned long long)btrfs_file_extent_disk_blocknr(fi),
122                                (unsigned long long)btrfs_file_extent_disk_num_blocks(fi));
123                         printf("\t\textent data offset %llu nr %llu\n",
124                           (unsigned long long)btrfs_file_extent_offset(fi),
125                           (unsigned long long)btrfs_file_extent_num_blocks(fi));
126                         break;
127                 case BTRFS_BLOCK_GROUP_ITEM_KEY:
128                         bi = btrfs_item_ptr(l, i,
129                                             struct btrfs_block_group_item);
130                         printf("\t\tblock group used %llu flags %x\n",
131                                (unsigned long long)btrfs_block_group_used(bi),
132                                bi->flags);
133                         break;
134                 case BTRFS_STRING_ITEM_KEY:
135                         printf("\t\titem data %.*s\n", btrfs_item_size(item),
136                                 btrfs_leaf_data(l) + btrfs_item_offset(item));
137                         break;
138                 };
139                 fflush(stdout);
140         }
141 }
142 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
143 {
144         int i;
145         u32 nr;
146         struct btrfs_node *c;
147
148         if (!t)
149                 return;
150         c = &t->node;
151         nr = btrfs_header_nritems(&c->header);
152         if (btrfs_is_leaf(c)) {
153                 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
154                 return;
155         }
156         printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n",
157                (unsigned long long)t->blocknr,
158                 btrfs_header_level(&c->header), nr,
159                 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr,
160                 (unsigned long long)btrfs_header_generation(&c->header),
161                 (unsigned long long)btrfs_header_owner(&c->header));
162         fflush(stdout);
163         for (i = 0; i < nr; i++) {
164                 printf("\tkey %d (%llu %x %llu) block %llu\n",
165                        i,
166                        (unsigned long long)c->ptrs[i].key.objectid,
167                        c->ptrs[i].key.flags,
168                        (unsigned long long)c->ptrs[i].key.offset,
169                        (unsigned long long)btrfs_node_blockptr(c, i));
170                 fflush(stdout);
171         }
172         for (i = 0; i < nr; i++) {
173                 struct btrfs_buffer *next_buf = read_tree_block(root,
174                                                 btrfs_node_blockptr(c, i));
175                 struct btrfs_node *next = &next_buf->node;
176                 if (btrfs_is_leaf(next) &&
177                     btrfs_header_level(&c->header) != 1)
178                         BUG();
179                 if (btrfs_header_level(&next->header) !=
180                         btrfs_header_level(&c->header) - 1)
181                         BUG();
182                 btrfs_print_tree(root, next_buf);
183                 btrfs_block_release(root, next_buf);
184         }
185 }
186