GNU Linux-libre 4.14.332-gnu1
[releases.git] / fs / btrfs / 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 "ctree.h"
20 #include "disk-io.h"
21 #include "print-tree.h"
22
23 static void print_chunk(struct extent_buffer *eb, struct btrfs_chunk *chunk)
24 {
25         int num_stripes = btrfs_chunk_num_stripes(eb, chunk);
26         int i;
27         pr_info("\t\tchunk length %llu owner %llu type %llu num_stripes %d\n",
28                btrfs_chunk_length(eb, chunk), btrfs_chunk_owner(eb, chunk),
29                btrfs_chunk_type(eb, chunk), num_stripes);
30         for (i = 0 ; i < num_stripes ; i++) {
31                 pr_info("\t\t\tstripe %d devid %llu offset %llu\n", i,
32                       btrfs_stripe_devid_nr(eb, chunk, i),
33                       btrfs_stripe_offset_nr(eb, chunk, i));
34         }
35 }
36 static void print_dev_item(struct extent_buffer *eb,
37                            struct btrfs_dev_item *dev_item)
38 {
39         pr_info("\t\tdev item devid %llu total_bytes %llu bytes used %llu\n",
40                btrfs_device_id(eb, dev_item),
41                btrfs_device_total_bytes(eb, dev_item),
42                btrfs_device_bytes_used(eb, dev_item));
43 }
44 static void print_extent_data_ref(struct extent_buffer *eb,
45                                   struct btrfs_extent_data_ref *ref)
46 {
47         pr_cont("extent data backref root %llu objectid %llu offset %llu count %u\n",
48                btrfs_extent_data_ref_root(eb, ref),
49                btrfs_extent_data_ref_objectid(eb, ref),
50                btrfs_extent_data_ref_offset(eb, ref),
51                btrfs_extent_data_ref_count(eb, ref));
52 }
53
54 static void print_extent_item(struct extent_buffer *eb, int slot, int type)
55 {
56         struct btrfs_extent_item *ei;
57         struct btrfs_extent_inline_ref *iref;
58         struct btrfs_extent_data_ref *dref;
59         struct btrfs_shared_data_ref *sref;
60         struct btrfs_disk_key key;
61         unsigned long end;
62         unsigned long ptr;
63         u32 item_size = btrfs_item_size_nr(eb, slot);
64         u64 flags;
65         u64 offset;
66         int ref_index = 0;
67
68         if (item_size < sizeof(*ei)) {
69 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
70                 struct btrfs_extent_item_v0 *ei0;
71                 BUG_ON(item_size != sizeof(*ei0));
72                 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
73                 pr_info("\t\textent refs %u\n",
74                        btrfs_extent_refs_v0(eb, ei0));
75                 return;
76 #else
77                 BUG();
78 #endif
79         }
80
81         ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
82         flags = btrfs_extent_flags(eb, ei);
83
84         pr_info("\t\textent refs %llu gen %llu flags %llu\n",
85                btrfs_extent_refs(eb, ei), btrfs_extent_generation(eb, ei),
86                flags);
87
88         if ((type == BTRFS_EXTENT_ITEM_KEY) &&
89             flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
90                 struct btrfs_tree_block_info *info;
91                 info = (struct btrfs_tree_block_info *)(ei + 1);
92                 btrfs_tree_block_key(eb, info, &key);
93                 pr_info("\t\ttree block key (%llu %u %llu) level %d\n",
94                        btrfs_disk_key_objectid(&key), key.type,
95                        btrfs_disk_key_offset(&key),
96                        btrfs_tree_block_level(eb, info));
97                 iref = (struct btrfs_extent_inline_ref *)(info + 1);
98         } else {
99                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
100         }
101
102         ptr = (unsigned long)iref;
103         end = (unsigned long)ei + item_size;
104         while (ptr < end) {
105                 iref = (struct btrfs_extent_inline_ref *)ptr;
106                 type = btrfs_extent_inline_ref_type(eb, iref);
107                 offset = btrfs_extent_inline_ref_offset(eb, iref);
108                 pr_info("\t\tref#%d: ", ref_index++);
109                 switch (type) {
110                 case BTRFS_TREE_BLOCK_REF_KEY:
111                         pr_cont("tree block backref root %llu\n", offset);
112                         break;
113                 case BTRFS_SHARED_BLOCK_REF_KEY:
114                         pr_cont("shared block backref parent %llu\n", offset);
115                         /*
116                          * offset is supposed to be a tree block which
117                          * must be aligned to nodesize.
118                          */
119                         if (!IS_ALIGNED(offset, eb->fs_info->sectorsize))
120                                 pr_info(
121                         "\t\t\t(parent %llu not aligned to sectorsize %u)\n",
122                                         offset, eb->fs_info->sectorsize);
123                         break;
124                 case BTRFS_EXTENT_DATA_REF_KEY:
125                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
126                         print_extent_data_ref(eb, dref);
127                         break;
128                 case BTRFS_SHARED_DATA_REF_KEY:
129                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
130                         pr_cont("shared data backref parent %llu count %u\n",
131                                offset, btrfs_shared_data_ref_count(eb, sref));
132                         /*
133                          * Offset is supposed to be a tree block which must be
134                          * aligned to sectorsize.
135                          */
136                         if (!IS_ALIGNED(offset, eb->fs_info->sectorsize))
137                                 pr_info(
138                         "\t\t\t(parent %llu not aligned to sectorsize %u)\n",
139                                      offset, eb->fs_info->sectorsize);
140                         break;
141                 default:
142                         pr_cont("(extent %llu has INVALID ref type %d)\n",
143                                   eb->start, type);
144                         return;
145                 }
146                 ptr += btrfs_extent_inline_ref_size(type);
147         }
148         WARN_ON(ptr > end);
149 }
150
151 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
152 static void print_extent_ref_v0(struct extent_buffer *eb, int slot)
153 {
154         struct btrfs_extent_ref_v0 *ref0;
155
156         ref0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_ref_v0);
157         printk("\t\textent back ref root %llu gen %llu owner %llu num_refs %lu\n",
158                 btrfs_ref_root_v0(eb, ref0),
159                 btrfs_ref_generation_v0(eb, ref0),
160                 btrfs_ref_objectid_v0(eb, ref0),
161                 (unsigned long)btrfs_ref_count_v0(eb, ref0));
162 }
163 #endif
164
165 static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
166                             u32 item_size)
167 {
168         if (!IS_ALIGNED(item_size, sizeof(u64))) {
169                 pr_warn("BTRFS: uuid item with illegal size %lu!\n",
170                         (unsigned long)item_size);
171                 return;
172         }
173         while (item_size) {
174                 __le64 subvol_id;
175
176                 read_extent_buffer(l, &subvol_id, offset, sizeof(subvol_id));
177                 pr_info("\t\tsubvol_id %llu\n",
178                        (unsigned long long)le64_to_cpu(subvol_id));
179                 item_size -= sizeof(u64);
180                 offset += sizeof(u64);
181         }
182 }
183
184 void btrfs_print_leaf(struct extent_buffer *l)
185 {
186         struct btrfs_fs_info *fs_info;
187         int i;
188         u32 type, nr;
189         struct btrfs_item *item;
190         struct btrfs_root_item *ri;
191         struct btrfs_dir_item *di;
192         struct btrfs_inode_item *ii;
193         struct btrfs_block_group_item *bi;
194         struct btrfs_file_extent_item *fi;
195         struct btrfs_extent_data_ref *dref;
196         struct btrfs_shared_data_ref *sref;
197         struct btrfs_dev_extent *dev_extent;
198         struct btrfs_key key;
199         struct btrfs_key found_key;
200
201         if (!l)
202                 return;
203
204         fs_info = l->fs_info;
205         nr = btrfs_header_nritems(l);
206
207         btrfs_info(fs_info, "leaf %llu total ptrs %d free space %d",
208                    btrfs_header_bytenr(l), nr,
209                    btrfs_leaf_free_space(fs_info, l));
210         for (i = 0 ; i < nr ; i++) {
211                 item = btrfs_item_nr(i);
212                 btrfs_item_key_to_cpu(l, &key, i);
213                 type = key.type;
214                 pr_info("\titem %d key (%llu %u %llu) itemoff %d itemsize %d\n",
215                         i, key.objectid, type, key.offset,
216                         btrfs_item_offset(l, item), btrfs_item_size(l, item));
217                 switch (type) {
218                 case BTRFS_INODE_ITEM_KEY:
219                         ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
220                         pr_info("\t\tinode generation %llu size %llu mode %o\n",
221                                btrfs_inode_generation(l, ii),
222                                btrfs_inode_size(l, ii),
223                                btrfs_inode_mode(l, ii));
224                         break;
225                 case BTRFS_DIR_ITEM_KEY:
226                         di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
227                         btrfs_dir_item_key_to_cpu(l, di, &found_key);
228                         pr_info("\t\tdir oid %llu type %u\n",
229                                 found_key.objectid,
230                                 btrfs_dir_type(l, di));
231                         break;
232                 case BTRFS_ROOT_ITEM_KEY:
233                         ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
234                         pr_info("\t\troot data bytenr %llu refs %u\n",
235                                 btrfs_disk_root_bytenr(l, ri),
236                                 btrfs_disk_root_refs(l, ri));
237                         break;
238                 case BTRFS_EXTENT_ITEM_KEY:
239                 case BTRFS_METADATA_ITEM_KEY:
240                         print_extent_item(l, i, type);
241                         break;
242                 case BTRFS_TREE_BLOCK_REF_KEY:
243                         pr_info("\t\ttree block backref\n");
244                         break;
245                 case BTRFS_SHARED_BLOCK_REF_KEY:
246                         pr_info("\t\tshared block backref\n");
247                         break;
248                 case BTRFS_EXTENT_DATA_REF_KEY:
249                         dref = btrfs_item_ptr(l, i,
250                                               struct btrfs_extent_data_ref);
251                         print_extent_data_ref(l, dref);
252                         break;
253                 case BTRFS_SHARED_DATA_REF_KEY:
254                         sref = btrfs_item_ptr(l, i,
255                                               struct btrfs_shared_data_ref);
256                         pr_info("\t\tshared data backref count %u\n",
257                                btrfs_shared_data_ref_count(l, sref));
258                         break;
259                 case BTRFS_EXTENT_DATA_KEY:
260                         fi = btrfs_item_ptr(l, i,
261                                             struct btrfs_file_extent_item);
262                         if (btrfs_file_extent_type(l, fi) ==
263                             BTRFS_FILE_EXTENT_INLINE) {
264                                 pr_info("\t\tinline extent data size %llu\n",
265                                        btrfs_file_extent_ram_bytes(l, fi));
266                                 break;
267                         }
268                         pr_info("\t\textent data disk bytenr %llu nr %llu\n",
269                                btrfs_file_extent_disk_bytenr(l, fi),
270                                btrfs_file_extent_disk_num_bytes(l, fi));
271                         pr_info("\t\textent data offset %llu nr %llu ram %llu\n",
272                                btrfs_file_extent_offset(l, fi),
273                                btrfs_file_extent_num_bytes(l, fi),
274                                btrfs_file_extent_ram_bytes(l, fi));
275                         break;
276                 case BTRFS_EXTENT_REF_V0_KEY:
277 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
278                         print_extent_ref_v0(l, i);
279 #else
280                         BUG();
281 #endif
282                         break;
283                 case BTRFS_BLOCK_GROUP_ITEM_KEY:
284                         bi = btrfs_item_ptr(l, i,
285                                             struct btrfs_block_group_item);
286                         pr_info(
287                    "\t\tblock group used %llu chunk_objectid %llu flags %llu\n",
288                                 btrfs_disk_block_group_used(l, bi),
289                                 btrfs_disk_block_group_chunk_objectid(l, bi),
290                                 btrfs_disk_block_group_flags(l, bi));
291                         break;
292                 case BTRFS_CHUNK_ITEM_KEY:
293                         print_chunk(l, btrfs_item_ptr(l, i,
294                                                       struct btrfs_chunk));
295                         break;
296                 case BTRFS_DEV_ITEM_KEY:
297                         print_dev_item(l, btrfs_item_ptr(l, i,
298                                         struct btrfs_dev_item));
299                         break;
300                 case BTRFS_DEV_EXTENT_KEY:
301                         dev_extent = btrfs_item_ptr(l, i,
302                                                     struct btrfs_dev_extent);
303                         pr_info("\t\tdev extent chunk_tree %llu\n\t\tchunk objectid %llu chunk offset %llu length %llu\n",
304                                btrfs_dev_extent_chunk_tree(l, dev_extent),
305                                btrfs_dev_extent_chunk_objectid(l, dev_extent),
306                                btrfs_dev_extent_chunk_offset(l, dev_extent),
307                                btrfs_dev_extent_length(l, dev_extent));
308                         break;
309                 case BTRFS_PERSISTENT_ITEM_KEY:
310                         pr_info("\t\tpersistent item objectid %llu offset %llu\n",
311                                         key.objectid, key.offset);
312                         switch (key.objectid) {
313                         case BTRFS_DEV_STATS_OBJECTID:
314                                 pr_info("\t\tdevice stats\n");
315                                 break;
316                         default:
317                                 pr_info("\t\tunknown persistent item\n");
318                         }
319                         break;
320                 case BTRFS_TEMPORARY_ITEM_KEY:
321                         pr_info("\t\ttemporary item objectid %llu offset %llu\n",
322                                         key.objectid, key.offset);
323                         switch (key.objectid) {
324                         case BTRFS_BALANCE_OBJECTID:
325                                 pr_info("\t\tbalance status\n");
326                                 break;
327                         default:
328                                 pr_info("\t\tunknown temporary item\n");
329                         }
330                         break;
331                 case BTRFS_DEV_REPLACE_KEY:
332                         pr_info("\t\tdev replace\n");
333                         break;
334                 case BTRFS_UUID_KEY_SUBVOL:
335                 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
336                         print_uuid_item(l, btrfs_item_ptr_offset(l, i),
337                                         btrfs_item_size_nr(l, i));
338                         break;
339                 };
340         }
341 }
342
343 void btrfs_print_tree(struct extent_buffer *c)
344 {
345         struct btrfs_fs_info *fs_info;
346         int i; u32 nr;
347         struct btrfs_key key;
348         int level;
349
350         if (!c)
351                 return;
352         fs_info = c->fs_info;
353         nr = btrfs_header_nritems(c);
354         level = btrfs_header_level(c);
355         if (level == 0) {
356                 btrfs_print_leaf(c);
357                 return;
358         }
359         btrfs_info(fs_info,
360                    "node %llu level %d total ptrs %d free spc %u",
361                    btrfs_header_bytenr(c), level, nr,
362                    (u32)BTRFS_NODEPTRS_PER_BLOCK(fs_info) - nr);
363         for (i = 0; i < nr; i++) {
364                 btrfs_node_key_to_cpu(c, &key, i);
365                 pr_info("\tkey %d (%llu %u %llu) block %llu\n",
366                        i, key.objectid, key.type, key.offset,
367                        btrfs_node_blockptr(c, i));
368         }
369         for (i = 0; i < nr; i++) {
370                 struct extent_buffer *next = read_tree_block(fs_info,
371                                         btrfs_node_blockptr(c, i),
372                                         btrfs_node_ptr_generation(c, i));
373                 if (IS_ERR(next)) {
374                         continue;
375                 } else if (!extent_buffer_uptodate(next)) {
376                         free_extent_buffer(next);
377                         continue;
378                 }
379
380                 if (btrfs_is_leaf(next) &&
381                    level != 1)
382                         BUG();
383                 if (btrfs_header_level(next) !=
384                        level - 1)
385                         BUG();
386                 btrfs_print_tree(next);
387                 free_extent_buffer(next);
388         }
389 }