Add back pointers from extents to the file or btree referencing them
[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 type %u\n",
35                      (unsigned long long)btrfs_disk_key_objectid(&di->location),
36                      btrfs_dir_type(di));
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);
44                 cur += len;
45         }
46         return 0;
47 }
48 void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
49 {
50         int i;
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;
61         u32 type;
62
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));
68         fflush(stdout);
69         for (i = 0 ; i < nr ; i++) {
70                 item = l->items + i;
71                 type = btrfs_disk_key_type(&item->key);
72                 printf("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
73                         i,
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));
79                 switch (type) {
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),
86                                btrfs_inode_mode(ii),
87                                btrfs_inode_nlink(ii));
88                         break;
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);
92                         break;
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);
97                         break;
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),
102                                 ri->level,
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,
108                                                       &ri->drop_progress);
109                                 printf("\t\tdrop key %Lu %x %Lu level %d\n",
110                                        (unsigned long long)drop_key.objectid,
111                                        drop_key.type,
112                                        (unsigned long long)drop_key.offset,
113                                        ri->drop_level);
114                         }
115                         break;
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));
120                         break;
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));
129                         break;
130                 case BTRFS_CSUM_ITEM_KEY:
131                         ci = btrfs_item_ptr(l, i,
132                                             struct btrfs_csum_item);
133                         printf("\t\tcsum item\n");
134                         break;
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));
142                                 break;
143                         }
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));
150                         break;
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),
156                                bi->flags);
157                         break;
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));
161                         break;
162                 };
163                 fflush(stdout);
164         }
165 }
166 void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
167 {
168         int i;
169         u32 nr;
170         struct btrfs_node *c;
171         u32 size;
172
173         if (!t)
174                 return;
175         c = &t->node;
176         nr = btrfs_header_nritems(&c->header);
177         if (btrfs_is_leaf(c)) {
178                 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
179                 return;
180         }
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));
187         fflush(stdout);
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",
192                        i,
193                        (unsigned long long)c->ptrs[i].key.objectid,
194                        c->ptrs[i].key.type,
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));
199                 fflush(stdout);
200         }
201         for (i = 0; i < nr; i++) {
202                 struct btrfs_buffer *next_buf = read_tree_block(root,
203                                                 btrfs_node_blockptr(c, i),
204                                                 size);
205                 struct btrfs_node *next = &next_buf->node;
206                 if (btrfs_is_leaf(next) &&
207                     btrfs_header_level(&c->header) != 1)
208                         BUG();
209                 if (btrfs_header_level(&next->header) !=
210                         btrfs_header_level(&c->header) - 1)
211                         BUG();
212                 btrfs_print_tree(root, next_buf);
213                 btrfs_block_release(root, next_buf);
214         }
215 }
216