Whamcloud - gitweb
debugfs: fix gcc -Wall complaints
[tools/e2fsprogs.git] / misc / e2freefrag.c
1 /*
2  * e2freefrag - report filesystem free-space fragmentation
3  *
4  * Copyright (C) 2009 Sun Microsystems, Inc.
5  *
6  * Author: Rupesh Thakare <rupesh@sun.com>
7  *         Andreas Dilger <adilger@sun.com>
8  *
9  * %Begin-Header%
10  * This file may be redistributed under the terms of the GNU Public
11  * License version 2.
12  * %End-Header%
13  */
14 #include "config.h"
15 #include <stdio.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #ifdef HAVE_STDLIB_H
20 #include <stdlib.h>
21 #endif
22 #ifdef HAVE_GETOPT_H
23 #include <getopt.h>
24 #else
25 extern char *optarg;
26 extern int optind;
27 #endif
28
29 #include "ext2fs/ext2_fs.h"
30 #include "ext2fs/ext2fs.h"
31 #include "e2freefrag.h"
32
33 static void usage(const char *prog)
34 {
35         fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
36                 "device_name\n", prog);
37 #ifndef DEBUGFS
38         exit(1);
39 #endif
40 }
41
42 static int ul_log2(unsigned long arg)
43 {
44         int     l = 0;
45
46         arg >>= 1;
47         while (arg) {
48                 l++;
49                 arg >>= 1;
50         }
51         return l;
52 }
53
54 static void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
55 {
56         int i;
57
58         info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
59         if (info->chunkbytes) {
60                 info->chunkbits = ul_log2(info->chunkbytes);
61                 info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
62         } else {
63                 info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
64                 info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
65         }
66
67         info->min = ~0UL;
68         info->max = info->avg = 0;
69         info->real_free_chunks = 0;
70
71         for (i = 0; i < MAX_HIST; i++) {
72                 info->histogram.fc_chunks[i] = 0;
73                 info->histogram.fc_blocks[i] = 0;
74         }
75 }
76
77 static void update_chunk_stats(struct chunk_info *info,
78                                unsigned long chunk_size)
79 {
80         unsigned long idx;
81
82         idx = ul_log2(chunk_size) + 1;
83         if (idx >= MAX_HIST)
84                 idx = MAX_HIST-1;
85         info->histogram.fc_chunks[idx]++;
86         info->histogram.fc_blocks[idx] += chunk_size;
87
88         if (chunk_size > info->max)
89                 info->max = chunk_size;
90         if (chunk_size < info->min)
91                 info->min = chunk_size;
92         info->avg += chunk_size;
93         info->real_free_chunks++;
94 }
95
96 static void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
97 {
98         unsigned long long blocks_count = ext2fs_blocks_count(fs->super);
99         unsigned long long chunks = (blocks_count + info->blks_in_chunk) >>
100                                 (info->chunkbits - info->blocksize_bits);
101         unsigned long long chunk_num;
102         unsigned long last_chunk_size = 0;
103         unsigned long long chunk_start_blk = 0;
104         int used;
105
106         for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
107                 unsigned long long blk, num_blks;
108                 int chunk_free;
109
110                 /* Last chunk may be smaller */
111                 if (chunk_start_blk + info->blks_in_chunk > blocks_count)
112                         num_blks = blocks_count - chunk_start_blk;
113                 else
114                         num_blks = info->blks_in_chunk;
115
116                 chunk_free = 0;
117
118                 /* Initialize starting block for first chunk correctly else
119                  * there is a segfault when blocksize = 1024 in which case
120                  * block_map->start = 1 */
121                 for (blk = 0; blk < num_blks; blk++, chunk_start_blk++) {
122                         if (chunk_num == 0 && blk == 0) {
123                                 blk = fs->super->s_first_data_block;
124                                 chunk_start_blk = blk;
125                         }
126                         used = ext2fs_fast_test_block_bitmap2(fs->block_map,
127                                 chunk_start_blk >> fs->cluster_ratio_bits);
128                         if (!used) {
129                                 last_chunk_size++;
130                                 chunk_free++;
131                         }
132
133                         if (used && last_chunk_size != 0) {
134                                 update_chunk_stats(info, last_chunk_size);
135                                 last_chunk_size = 0;
136                         }
137                 }
138
139                 if (chunk_free == info->blks_in_chunk)
140                         info->free_chunks++;
141         }
142         if (last_chunk_size != 0)
143                 update_chunk_stats(info, last_chunk_size);
144 }
145
146 static errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info,
147                                 FILE *f)
148 {
149         unsigned long total_chunks;
150         const char *unitp = "KMGTPEZY";
151         int units = 10;
152         unsigned long start = 0, end;
153         int i, retval = 0;
154
155         scan_block_bitmap(fs, info);
156
157         fprintf(f, "Total blocks: %llu\nFree blocks: %u (%0.1f%%)\n",
158                 ext2fs_blocks_count(fs->super), fs->super->s_free_blocks_count,
159                 (double)fs->super->s_free_blocks_count * 100 /
160                 ext2fs_blocks_count(fs->super));
161
162         if (info->chunkbytes) {
163                 fprintf(f, "\nChunksize: %lu bytes (%u blocks)\n",
164                         info->chunkbytes, info->blks_in_chunk);
165                 total_chunks = (ext2fs_blocks_count(fs->super) +
166                                 info->blks_in_chunk) >>
167                         (info->chunkbits - info->blocksize_bits);
168                 fprintf(f, "Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n",
169                         total_chunks, info->free_chunks,
170                         (double)info->free_chunks * 100 / total_chunks);
171         }
172
173         /* Display chunk information in KB */
174         if (info->real_free_chunks) {
175                 info->min = (info->min * fs->blocksize) >> 10;
176                 info->max = (info->max * fs->blocksize) >> 10;
177                 info->avg = (info->avg / info->real_free_chunks *
178                              fs->blocksize) >> 10;
179         } else {
180                 info->min = 0;
181         }
182
183         fprintf(f, "\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n"
184                 "Avg. free extent: %lu KB\n", info->min, info->max, info->avg);
185         fprintf(f, "Num. free extent: %lu\n", info->real_free_chunks);
186
187         fprintf(f, "\nHISTOGRAM OF FREE EXTENT SIZES:\n");
188         fprintf(f, "%s :  %12s  %12s  %7s\n", "Extent Size Range",
189                 "Free extents", "Free Blocks", "Percent");
190         for (i = 0; i < MAX_HIST; i++) {
191                 end = 1 << (i + info->blocksize_bits - units);
192                 if (info->histogram.fc_chunks[i] != 0) {
193                         char end_str[32];
194
195                         sprintf(end_str, "%5lu%c-", end, *unitp);
196                         if (i == MAX_HIST-1)
197                                 strcpy(end_str, "max ");
198                         fprintf(f, "%5lu%c...%7s  :  %12lu  %12lu  %6.2f%%\n",
199                                 start, *unitp, end_str,
200                                 info->histogram.fc_chunks[i],
201                                 info->histogram.fc_blocks[i],
202                                 (double)info->histogram.fc_blocks[i] * 100 /
203                                 fs->super->s_free_blocks_count);
204                 }
205                 start = end;
206                 if (start == 1<<10) {
207                         start = 1;
208                         units += 10;
209                         unitp++;
210                 }
211         }
212
213         return retval;
214 }
215
216 static void close_device(char *device_name, ext2_filsys fs)
217 {
218         int retval = ext2fs_close(fs);
219
220         if (retval)
221                 com_err(device_name, retval, "while closing the filesystem.\n");
222 }
223
224 static void collect_info(ext2_filsys fs, struct chunk_info *chunk_info, FILE *f)
225 {
226         unsigned int retval = 0;
227
228         fprintf(f, "Device: %s\n", fs->device_name);
229         fprintf(f, "Blocksize: %u bytes\n", fs->blocksize);
230
231         retval = ext2fs_read_block_bitmap(fs);
232         if (retval) {
233                 com_err(fs->device_name, retval, "while reading block bitmap");
234                 close_device(fs->device_name, fs);
235                 exit(1);
236         }
237
238         init_chunk_info(fs, chunk_info);
239
240         retval = get_chunk_info(fs, chunk_info, f);
241         if (retval) {
242                 com_err(fs->device_name, retval, "while collecting chunk info");
243                 close_device(fs->device_name, fs);
244                 exit(1);
245         }
246 }
247
248 #ifndef DEBUGFS
249 static void open_device(char *device_name, ext2_filsys *fs)
250 {
251         int retval;
252         int flag = EXT2_FLAG_FORCE;
253
254         retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
255         if (retval) {
256                 com_err(device_name, retval, "while opening filesystem");
257                 exit(1);
258         }
259 }
260 #endif
261
262 #ifdef DEBUGFS
263 #include "debugfs.h"
264
265 void do_freefrag(int argc, char **argv)
266 #else
267 int main(int argc, char *argv[])
268 #endif
269 {
270         struct chunk_info chunk_info;
271         ext2_filsys fs = NULL;
272         char *progname;
273         char *end;
274         int c;
275
276 #ifdef DEBUGFS
277         if (check_fs_open(argv[0]))
278                 return;
279 #else
280         char *device_name;
281
282         add_error_table(&et_ext2_error_table);
283 #endif
284         progname = argv[0];
285         memset(&chunk_info, 0, sizeof(chunk_info));
286
287         while ((c = getopt(argc, argv, "c:h")) != EOF) {
288                 switch (c) {
289                 case 'c':
290                         chunk_info.chunkbytes = strtoull(optarg, &end, 0);
291                         if (*end != '\0') {
292                                 fprintf(stderr, "%s: bad chunk size '%s'\n",
293                                         progname, optarg);
294                                 usage(progname);
295                         }
296                         if (chunk_info.chunkbytes &
297                             (chunk_info.chunkbytes - 1)) {
298                                 fprintf(stderr, "%s: chunk size must be a "
299                                         "power of 2.\n", argv[0]);
300                                 usage(progname);
301                         }
302                         chunk_info.chunkbytes *= 1024;
303                         break;
304                 case 'h':
305                 default:
306                         usage(progname);
307                         break;
308                 }
309         }
310
311 #ifndef DEBUGFS
312         if (optind == argc) {
313                 fprintf(stderr, "%s: missing device name.\n", progname);
314                 usage(progname);
315         }
316
317         device_name = argv[optind];
318
319         open_device(device_name, &fs);
320 #else
321         fs = current_fs;
322 #endif
323
324         if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) {
325                 fprintf(stderr, "%s: chunksize must be greater than or equal "
326                         "to filesystem blocksize.\n", progname);
327                 exit(1);
328         }
329         collect_info(fs, &chunk_info, stdout);
330 #ifndef DEBUGFS
331         close_device(device_name, fs);
332
333         return 0;
334 #endif
335 }