blob: e66acd4e2745936a7a6e3149726c3974ab6812fe [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.h>
Chris Mason5de08d72007-02-24 06:24:44 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason35b7e472007-05-02 15:53:43 -040022#include "print-tree.h"
Chris Mason5de08d72007-02-24 06:24:44 -050023
Chris Mason123abc82007-03-14 14:14:43 -040024void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
Chris Mason5de08d72007-02-24 06:24:44 -050025{
26 int i;
Chris Mason7518a232007-03-12 12:01:18 -040027 u32 nr = btrfs_header_nritems(&l->header);
Chris Mason0783fcf2007-03-12 20:12:07 -040028 struct btrfs_item *item;
Chris Mason234b63a2007-03-13 10:46:10 -040029 struct btrfs_extent_item *ei;
Chris Mason3768f362007-03-13 16:47:54 -040030 struct btrfs_root_item *ri;
Chris Mason1d4f6402007-03-15 15:18:43 -040031 struct btrfs_dir_item *di;
Chris Mason293ffd52007-03-20 15:57:25 -040032 struct btrfs_inode_item *ii;
Chris Mason9078a3e2007-04-26 16:46:15 -040033 struct btrfs_block_group_item *bi;
Chris Mason8c2383c2007-06-18 09:57:58 -040034 struct btrfs_file_extent_item *fi;
Chris Mason62e27492007-03-15 12:56:47 -040035 u32 type;
Chris Mason1d4f6402007-03-15 15:18:43 -040036
Chris Mason5af39812007-06-12 07:50:13 -040037 printk("leaf %llu total ptrs %d free space %d\n",
38 (unsigned long long)btrfs_header_blocknr(&l->header), nr,
Chris Mason123abc82007-03-14 14:14:43 -040039 btrfs_leaf_free_space(root, l));
Chris Mason5de08d72007-02-24 06:24:44 -050040 for (i = 0 ; i < nr ; i++) {
41 item = l->items + i;
Chris Mason62e27492007-03-15 12:56:47 -040042 type = btrfs_disk_key_type(&item->key);
Chris Mason5af39812007-06-12 07:50:13 -040043 printk("\titem %d key (%llu %x %llu) itemoff %d itemsize %d\n",
Chris Mason5de08d72007-02-24 06:24:44 -050044 i,
Chris Mason5af39812007-06-12 07:50:13 -040045 (unsigned long long)btrfs_disk_key_objectid(&item->key),
Chris Mason62e27492007-03-15 12:56:47 -040046 btrfs_disk_key_flags(&item->key),
Chris Mason5af39812007-06-12 07:50:13 -040047 (unsigned long long)btrfs_disk_key_offset(&item->key),
Chris Mason0783fcf2007-03-12 20:12:07 -040048 btrfs_item_offset(item),
49 btrfs_item_size(item));
Chris Mason62e27492007-03-15 12:56:47 -040050 switch (type) {
51 case BTRFS_INODE_ITEM_KEY:
Chris Mason293ffd52007-03-20 15:57:25 -040052 ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
Chris Mason5af39812007-06-12 07:50:13 -040053 printk("\t\tinode generation %llu size %llu mode %o\n",
54 (unsigned long long)btrfs_inode_generation(ii),
55 (unsigned long long)btrfs_inode_size(ii),
Chris Mason2e635a22007-03-21 11:12:56 -040056 btrfs_inode_mode(ii));
Chris Mason62e27492007-03-15 12:56:47 -040057 break;
58 case BTRFS_DIR_ITEM_KEY:
Chris Mason1d4f6402007-03-15 15:18:43 -040059 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
Chris Mason5af39812007-06-12 07:50:13 -040060 printk("\t\tdir oid %llu flags %u type %u\n",
61 (unsigned long long)btrfs_disk_key_objectid(
62 &di->location),
Chris Mason1d4f6402007-03-15 15:18:43 -040063 btrfs_dir_flags(di),
64 btrfs_dir_type(di));
Chris Mason2e635a22007-03-21 11:12:56 -040065 printk("\t\tname %.*s\n",
Chris Mason9f5fae22007-03-20 14:38:32 -040066 btrfs_dir_name_len(di),(char *)(di + 1));
Chris Mason62e27492007-03-15 12:56:47 -040067 break;
68 case BTRFS_ROOT_ITEM_KEY:
69 ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
Chris Mason5af39812007-06-12 07:50:13 -040070 printk("\t\troot data blocknr %llu refs %u\n",
71 (unsigned long long)btrfs_root_blocknr(ri),
72 btrfs_root_refs(ri));
Chris Mason62e27492007-03-15 12:56:47 -040073 break;
74 case BTRFS_EXTENT_ITEM_KEY:
75 ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
Chris Masond0dbc622007-04-10 12:36:36 -040076 printk("\t\textent data refs %u\n",
77 btrfs_extent_refs(ei));
Chris Mason62e27492007-03-15 12:56:47 -040078 break;
Chris Mason8c2383c2007-06-18 09:57:58 -040079
80 case BTRFS_EXTENT_DATA_KEY:
81 fi = btrfs_item_ptr(l, i,
82 struct btrfs_file_extent_item);
83 if (btrfs_file_extent_type(fi) ==
84 BTRFS_FILE_EXTENT_INLINE) {
85 printk("\t\tinline extent data size %u\n",
86 btrfs_file_extent_inline_len(l->items + i));
87 break;
88 }
89 printk("\t\textent data disk block %llu nr %llu\n",
90 (unsigned long long)btrfs_file_extent_disk_blocknr(fi),
91 (unsigned long long)btrfs_file_extent_disk_num_blocks(fi));
92 printk("\t\textent data offset %llu nr %llu\n",
93 (unsigned long long)btrfs_file_extent_offset(fi),
94 (unsigned long long)btrfs_file_extent_num_blocks(fi));
95 break;
Chris Mason9078a3e2007-04-26 16:46:15 -040096 case BTRFS_BLOCK_GROUP_ITEM_KEY:
97 bi = btrfs_item_ptr(l, i,
98 struct btrfs_block_group_item);
Chris Mason5af39812007-06-12 07:50:13 -040099 printk("\t\tblock group used %llu\n",
100 (unsigned long long)btrfs_block_group_used(bi));
Chris Mason9078a3e2007-04-26 16:46:15 -0400101 break;
Chris Mason62e27492007-03-15 12:56:47 -0400102 case BTRFS_STRING_ITEM_KEY:
Chris Mason2e635a22007-03-21 11:12:56 -0400103 printk("\t\titem data %.*s\n", btrfs_item_size(item),
Chris Mason62e27492007-03-15 12:56:47 -0400104 btrfs_leaf_data(l) + btrfs_item_offset(item));
105 break;
106 };
Chris Mason5de08d72007-02-24 06:24:44 -0500107 }
108}
Chris Masone20d96d2007-03-22 12:13:20 -0400109
110void btrfs_print_tree(struct btrfs_root *root, struct buffer_head *t)
Chris Mason5de08d72007-02-24 06:24:44 -0500111{
112 int i;
Chris Mason7518a232007-03-12 12:01:18 -0400113 u32 nr;
Chris Mason234b63a2007-03-13 10:46:10 -0400114 struct btrfs_node *c;
Chris Mason5de08d72007-02-24 06:24:44 -0500115
116 if (!t)
117 return;
Chris Masone20d96d2007-03-22 12:13:20 -0400118 c = btrfs_buffer_node(t);
Chris Mason7518a232007-03-12 12:01:18 -0400119 nr = btrfs_header_nritems(&c->header);
120 if (btrfs_is_leaf(c)) {
Chris Mason123abc82007-03-14 14:14:43 -0400121 btrfs_print_leaf(root, (struct btrfs_leaf *)c);
Chris Mason5de08d72007-02-24 06:24:44 -0500122 return;
123 }
Chris Mason5af39812007-06-12 07:50:13 -0400124 printk("node %llu level %d total ptrs %d free spc %u\n",
125 (unsigned long long)btrfs_header_blocknr(&c->header),
Chris Masone20d96d2007-03-22 12:13:20 -0400126 btrfs_header_level(&c->header), nr,
127 (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
Chris Mason5de08d72007-02-24 06:24:44 -0500128 for (i = 0; i < nr; i++) {
Chris Mason5af39812007-06-12 07:50:13 -0400129 printk("\tkey %d (%llu %u %llu) block %llu\n",
Chris Mason5de08d72007-02-24 06:24:44 -0500130 i,
Chris Mason5af39812007-06-12 07:50:13 -0400131 (unsigned long long)c->ptrs[i].key.objectid,
Chris Masona1516c82007-03-14 14:26:53 -0400132 c->ptrs[i].key.flags,
Chris Mason5af39812007-06-12 07:50:13 -0400133 (unsigned long long)c->ptrs[i].key.offset,
134 (unsigned long long)btrfs_node_blockptr(c, i));
Chris Mason5de08d72007-02-24 06:24:44 -0500135 }
136 for (i = 0; i < nr; i++) {
Chris Masone20d96d2007-03-22 12:13:20 -0400137 struct buffer_head *next_buf = read_tree_block(root,
Chris Mason1d4f8a02007-03-13 09:28:32 -0400138 btrfs_node_blockptr(c, i));
Chris Masone20d96d2007-03-22 12:13:20 -0400139 struct btrfs_node *next = btrfs_buffer_node(next_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400140 if (btrfs_is_leaf(next) &&
141 btrfs_header_level(&c->header) != 1)
Chris Mason5de08d72007-02-24 06:24:44 -0500142 BUG();
Chris Mason7518a232007-03-12 12:01:18 -0400143 if (btrfs_header_level(&next->header) !=
144 btrfs_header_level(&c->header) - 1)
Chris Mason5de08d72007-02-24 06:24:44 -0500145 BUG();
Chris Mason234b63a2007-03-13 10:46:10 -0400146 btrfs_print_tree(root, next_buf);
147 btrfs_block_release(root, next_buf);
Chris Mason5de08d72007-02-24 06:24:44 -0500148 }
Chris Mason5de08d72007-02-24 06:24:44 -0500149}
150