btrfs-progs: per-thread, per-call pretty buffer
[platform/upstream/btrfs-progs.git] / btrfs-calc-size.c
1 /*
2  * Copyright (C) 2011 Red Hat.  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 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <zlib.h>
28 #include "kerncompat.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "print-tree.h"
32 #include "transaction.h"
33 #include "list.h"
34 #include "version.h"
35 #include "volumes.h"
36 #include "utils.h"
37
38 static int verbose = 0;
39 static int no_pretty = 0;
40
41 struct root_stats {
42         u64 total_nodes;
43         u64 total_leaves;
44         u64 total_bytes;
45         u64 total_inline;
46         int total_levels;
47 };
48
49 struct fs_root {
50         struct btrfs_key key;
51         struct btrfs_key *snaps;
52 };
53
54 static int walk_leaf(struct btrfs_root *root, struct btrfs_path *path,
55                      struct root_stats *stat, int find_inline)
56 {
57         struct extent_buffer *b = path->nodes[0];
58         struct btrfs_file_extent_item *fi;
59         struct btrfs_key found_key;
60         int i;
61
62         stat->total_bytes += root->leafsize;
63         stat->total_leaves++;
64
65         if (!find_inline)
66                 return 0;
67
68         for (i = 0; i < btrfs_header_nritems(b); i++) {
69                 btrfs_item_key_to_cpu(b, &found_key, i);
70                 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
71                         continue;
72
73                 fi = btrfs_item_ptr(b, i, struct btrfs_file_extent_item);
74                 if (btrfs_file_extent_type(b, fi) == BTRFS_FILE_EXTENT_INLINE)
75                         stat->total_inline +=
76                                 btrfs_file_extent_inline_item_len(b,
77                                                         btrfs_item_nr(b, i));
78         }
79
80         return 0;
81 }
82
83 static int walk_nodes(struct btrfs_root *root, struct btrfs_path *path,
84                       struct root_stats *stat, int level, int find_inline)
85 {
86         struct extent_buffer *b = path->nodes[level];
87         int i;
88         int ret = 0;
89
90         stat->total_bytes += root->nodesize;
91         stat->total_nodes++;
92
93         for (i = 0; i < btrfs_header_nritems(b); i++) {
94                 struct extent_buffer *tmp = NULL;
95
96                 path->slots[level] = i;
97                 if ((level - 1) > 0 || find_inline) {
98                         tmp = read_tree_block(root, btrfs_node_blockptr(b, i),
99                                               btrfs_level_size(root, level - 1),
100                                               btrfs_node_ptr_generation(b, i));
101                         if (!tmp) {
102                                 fprintf(stderr, "Failed to read blocknr %Lu\n",
103                                         btrfs_node_blockptr(b, i));
104                                 continue;
105                         }
106                         path->nodes[level - 1] = tmp;
107                 }
108                 if (level - 1)
109                         ret = walk_nodes(root, path, stat, level - 1,
110                                          find_inline);
111                 else
112                         ret = walk_leaf(root, path, stat, find_inline);
113                 free_extent_buffer(tmp);
114                 if (ret) {
115                         fprintf(stderr, "Error walking down path\n");
116                         break;
117                 }
118         }
119
120         return ret;
121 }
122
123 static int calc_root_size(struct btrfs_root *tree_root, struct btrfs_key *key,
124                           int find_inline)
125 {
126         struct btrfs_root *root;
127         struct btrfs_path *path;
128         struct root_stats stat;
129         int level;
130         int ret = 0;
131         int size_fail = 0;
132
133         root = btrfs_read_fs_root(tree_root->fs_info, key);
134         if (!root) {
135                 fprintf(stderr, "Failed to read root %Lu\n", key->objectid);
136                 return 1;
137         }
138
139         path = btrfs_alloc_path();
140         if (!path) {
141                 fprintf(stderr, "Could not allocate path\n");
142                 return 1;
143         }
144
145         memset(&stat, 0, sizeof(stat));
146         level = btrfs_header_level(root->node);
147         path->nodes[level] = root->node;
148         if (!level) {
149                 ret = walk_leaf(root, path, &stat, find_inline);
150                 if (ret)
151                         goto out;
152                 goto out_print;
153         }
154
155         ret = walk_nodes(root, path, &stat, level, find_inline);
156         if (ret)
157                 goto out;
158 out_print:
159         if (no_pretty || size_fail) {
160                 printf("\t%Lu total bytes, %Lu inline data bytes, %Lu nodes, "
161                        "%Lu leaves, %d levels\n", stat.total_bytes,
162                        stat.total_inline, stat.total_nodes, stat.total_leaves,
163                        level + 1);
164         } else {
165                 printf("\t%s total size, %s inline data, %Lu nodes, "
166                        "%Lu leaves, %d levels\n",
167                        pretty_size(stat.total_bytes),
168                        pretty_size(stat.total_inline),
169                        stat.total_nodes, stat.total_leaves, level + 1);
170         }
171 out:
172         btrfs_free_path(path);
173         return ret;
174 }
175
176 static void usage()
177 {
178         fprintf(stderr, "Usage: calc-size [-v] [-b] <device>\n");
179 }
180
181 int main(int argc, char **argv)
182 {
183         struct btrfs_key key;
184         struct fs_root *roots;
185         struct btrfs_root *root;
186         size_t fs_roots_size = sizeof(struct fs_root);
187         int opt;
188         int ret = 0;
189
190         while ((opt = getopt(argc, argv, "vb")) != -1) {
191                 switch (opt) {
192                         case 'v':
193                                 verbose++;
194                                 break;
195                         case 'b':
196                                 no_pretty = 1;
197                                 break;
198                         default:
199                                 usage();
200                                 exit(1);
201                 }
202         }
203
204         if (optind >= argc) {
205                 usage();
206                 exit(1);
207         }
208
209         /*
210         if ((ret = check_mounted(argv[optind])) < 0) {
211                 fprintf(stderr, "Could not check mount status: %d\n", ret);
212                 if (ret == -EACCES)
213                         fprintf(stderr, "Maybe you need to run as root?\n");
214                 return ret;
215         } else if (ret) {
216                 fprintf(stderr, "%s is currently mounted.  Aborting.\n",
217                         argv[optind]);
218                 return -EBUSY;
219         }
220         */
221
222         root = open_ctree(argv[optind], 0, 0);
223         if (!root) {
224                 fprintf(stderr, "Couldn't open ctree\n");
225                 exit(1);
226         }
227
228         roots = malloc(fs_roots_size);
229         if (!roots) {
230                 fprintf(stderr, "No memory\n");
231                 goto out;
232         }
233
234         printf("Calculating size of root tree\n");
235         key.objectid = BTRFS_ROOT_TREE_OBJECTID;
236         ret = calc_root_size(root, &key, 0);
237         if (ret)
238                 goto out;
239
240         printf("Calculating size of extent tree\n");
241         key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
242         ret = calc_root_size(root, &key, 0);
243         if (ret)
244                 goto out;
245
246         printf("Calculating size of csum tree\n");
247         key.objectid = BTRFS_CSUM_TREE_OBJECTID;
248         ret = calc_root_size(root, &key, 0);
249         if (ret)
250                 goto out;
251
252         roots[0].key.objectid = BTRFS_FS_TREE_OBJECTID;
253         roots[0].key.offset = (u64)-1;
254         printf("Calculatin' size of fs tree\n");
255         ret = calc_root_size(root, &roots[0].key, 1);
256         if (ret)
257                 goto out;
258 out:
259         close_ctree(root);
260         return ret;
261 }