2 * e2freefrag - report filesystem free-space fragmentation
4 * Copyright (C) 2009 Sun Microsystems, Inc.
6 * Author: Rupesh Thakare <rupesh@sun.com>
7 * Andreas Dilger <adilger@sun.com>
10 * This file may be redistributed under the terms of the GNU Public
29 #include "ext2fs/ext2_fs.h"
30 #include "ext2fs/ext2fs.h"
31 #include "e2freefrag.h"
33 void usage(const char *prog)
35 fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
36 "device_name\n", prog);
40 static int ul_log2(unsigned long arg)
52 void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
56 info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
57 if (info->chunkbytes) {
58 info->chunkbits = ul_log2(info->chunkbytes);
59 info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
61 info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
62 info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
66 info->max = info->avg = 0;
67 info->real_free_chunks = 0;
69 for (i = 0; i < MAX_HIST; i++) {
70 info->histogram.fc_chunks[i] = 0;
71 info->histogram.fc_blocks[i] = 0;
75 void update_chunk_stats(struct chunk_info *info, unsigned long chunk_size)
79 index = ul_log2(chunk_size) + 1;
80 if (index >= MAX_HIST)
82 info->histogram.fc_chunks[index]++;
83 info->histogram.fc_blocks[index] += chunk_size;
85 if (chunk_size > info->max)
86 info->max = chunk_size;
87 if (chunk_size < info->min)
88 info->min = chunk_size;
89 info->avg += chunk_size;
90 info->real_free_chunks++;
93 void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
95 unsigned long long blocks_count = ext2fs_blocks_count(fs->super);
96 unsigned long long chunks = (blocks_count + info->blks_in_chunk) >>
97 (info->chunkbits - info->blocksize_bits);
98 unsigned long long chunk_num;
99 unsigned long last_chunk_size = 0;
100 unsigned long long chunk_start_blk = 0;
103 for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
104 unsigned long long blk, num_blks;
107 /* Last chunk may be smaller */
108 if (chunk_start_blk + info->blks_in_chunk > blocks_count)
109 num_blks = blocks_count - chunk_start_blk;
111 num_blks = info->blks_in_chunk;
115 /* Initialize starting block for first chunk correctly else
116 * there is a segfault when blocksize = 1024 in which case
117 * block_map->start = 1 */
118 for (blk = 0; blk < num_blks; blk++, chunk_start_blk++) {
119 if (chunk_num == 0 && blk == 0) {
120 blk = fs->super->s_first_data_block;
121 chunk_start_blk = blk;
123 used = ext2fs_fast_test_block_bitmap2(fs->block_map,
124 chunk_start_blk >> fs->cluster_ratio_bits);
130 if (used && last_chunk_size != 0) {
131 update_chunk_stats(info, last_chunk_size);
136 if (chunk_free == info->blks_in_chunk)
139 if (last_chunk_size != 0)
140 update_chunk_stats(info, last_chunk_size);
143 errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info)
145 unsigned long total_chunks;
146 char *unitp = "KMGTPEZY";
148 unsigned long start = 0, end;
151 scan_block_bitmap(fs, info);
153 printf("Total blocks: %llu\nFree blocks: %u (%0.1f%%)\n",
154 ext2fs_blocks_count(fs->super), fs->super->s_free_blocks_count,
155 (double)fs->super->s_free_blocks_count * 100 /
156 ext2fs_blocks_count(fs->super));
158 if (info->chunkbytes) {
159 printf("\nChunksize: %lu bytes (%u blocks)\n",
160 info->chunkbytes, info->blks_in_chunk);
161 total_chunks = (ext2fs_blocks_count(fs->super) +
162 info->blks_in_chunk) >>
163 (info->chunkbits - info->blocksize_bits);
164 printf("Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n",
165 total_chunks, info->free_chunks,
166 (double)info->free_chunks * 100 / total_chunks);
169 /* Display chunk information in KB */
170 if (info->real_free_chunks) {
171 info->min = (info->min * fs->blocksize) >> 10;
172 info->max = (info->max * fs->blocksize) >> 10;
173 info->avg = (info->avg / info->real_free_chunks *
174 fs->blocksize) >> 10;
179 printf("\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n"
180 "Avg. free extent: %lu KB\n", info->min, info->max, info->avg);
181 printf("Num. free extent: %lu\n", info->real_free_chunks);
183 printf("\nHISTOGRAM OF FREE EXTENT SIZES:\n");
184 printf("%s : %12s %12s %7s\n", "Extent Size Range", "Free extents",
185 "Free Blocks", "Percent");
186 for (i = 0; i < MAX_HIST; i++) {
187 end = 1 << (i + info->blocksize_bits - units);
188 if (info->histogram.fc_chunks[i] != 0) {
191 sprintf(end_str, "%5lu%c-", end, *unitp);
193 strcpy(end_str, "max ");
194 printf("%5lu%c...%7s : %12lu %12lu %6.2f%%\n",
195 start, *unitp, end_str,
196 info->histogram.fc_chunks[i],
197 info->histogram.fc_blocks[i],
198 (double)info->histogram.fc_blocks[i] * 100 /
199 fs->super->s_free_blocks_count);
202 if (start == 1<<10) {
212 void close_device(char *device_name, ext2_filsys fs)
214 int retval = ext2fs_close(fs);
217 com_err(device_name, retval, "while closing the filesystem.\n");
220 void collect_info(ext2_filsys fs, struct chunk_info *chunk_info)
222 unsigned int retval = 0;
224 printf("Device: %s\n", fs->device_name);
225 printf("Blocksize: %u bytes\n", fs->blocksize);
227 retval = ext2fs_read_block_bitmap(fs);
229 com_err(fs->device_name, retval, "while reading block bitmap");
230 close_device(fs->device_name, fs);
234 init_chunk_info(fs, chunk_info);
236 retval = get_chunk_info(fs, chunk_info);
238 com_err(fs->device_name, retval, "while collecting chunk info");
239 close_device(fs->device_name, fs);
244 void open_device(char *device_name, ext2_filsys *fs)
247 int flag = EXT2_FLAG_FORCE;
249 retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
251 com_err(device_name, retval, "while opening filesystem");
256 int main(int argc, char *argv[])
258 struct chunk_info chunk_info = { };
259 errcode_t retval = 0;
260 ext2_filsys fs = NULL;
266 add_error_table(&et_ext2_error_table);
269 while ((c = getopt(argc, argv, "c:h")) != EOF) {
272 chunk_info.chunkbytes = strtoull(optarg, &end, 0);
274 fprintf(stderr, "%s: bad chunk size '%s'\n",
278 if (chunk_info.chunkbytes &
279 (chunk_info.chunkbytes - 1)) {
280 fprintf(stderr, "%s: chunk size must be a "
281 "power of 2.\n", argv[0]);
284 chunk_info.chunkbytes *= 1024;
293 if (optind == argc) {
294 fprintf(stderr, "%s: missing device name.\n", progname);
298 device_name = argv[optind];
300 open_device(device_name, &fs);
302 if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) {
303 fprintf(stderr, "%s: chunksize must be greater than or equal "
304 "to filesystem blocksize.\n", progname);
307 collect_info(fs, &chunk_info);
308 close_device(device_name, fs);