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
28 #include "ext2fs/ext2_fs.h"
29 #include "ext2fs/ext2fs.h"
30 #include "e2freefrag.h"
32 void usage(const char *prog)
34 fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
35 "device_name\n", prog);
39 static int ul_log2(unsigned long arg)
51 void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
55 info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
56 if (info->chunkbytes) {
57 info->chunkbits = ul_log2(info->chunkbytes);
58 info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
60 info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
61 info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
65 info->max = info->avg = 0;
66 info->real_free_chunks = 0;
68 for (i = 0; i < MAX_HIST; i++) {
69 info->histogram.fc_chunks[i] = 0;
70 info->histogram.fc_blocks[i] = 0;
74 void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
76 unsigned long long blocks_count = fs->super->s_blocks_count;
77 unsigned long long chunks = (blocks_count + info->blks_in_chunk) >>
78 (info->chunkbits - info->blocksize_bits);
79 unsigned long long chunk_num;
80 unsigned long last_chunk_size = 0;
81 unsigned long long chunk_start_blk = 0;
84 for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
85 unsigned long long blk, num_blks;
88 /* Last chunk may be smaller */
89 if (chunk_start_blk + info->blks_in_chunk > blocks_count)
90 num_blks = blocks_count - chunk_start_blk;
92 num_blks = info->blks_in_chunk;
96 /* Initialize starting block for first chunk correctly else
97 * there is a segfault when blocksize = 1024 in which case
98 * block_map->start = 1 */
99 for (blk = 0; blk < num_blks; blk++, chunk_start_blk++) {
100 if (chunk_num == 0 && blk == 0) {
101 blk = fs->super->s_first_data_block;
102 chunk_start_blk = blk;
104 used = ext2fs_fast_test_block_bitmap(fs->block_map,
111 if (used && last_chunk_size != 0) {
114 index = ul_log2(last_chunk_size) + 1;
115 info->histogram.fc_chunks[index]++;
116 info->histogram.fc_blocks[index] +=
119 if (last_chunk_size > info->max)
120 info->max = last_chunk_size;
121 if (last_chunk_size < info->min)
122 info->min = last_chunk_size;
123 info->avg += last_chunk_size;
125 info->real_free_chunks++;
130 if (chunk_free == info->blks_in_chunk)
135 errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info)
137 unsigned long total_chunks;
138 char *unitp = "KMGTPEZY";
140 unsigned long start = 0, end, cum;
143 scan_block_bitmap(fs, info);
145 printf("Total blocks: %u\nFree blocks: %u (%0.1f%%)\n",
146 fs->super->s_blocks_count, fs->super->s_free_blocks_count,
147 (double)fs->super->s_free_blocks_count * 100 /
148 fs->super->s_blocks_count);
150 if (info->chunkbytes) {
151 printf("\nChunksize: %lu bytes (%u blocks)\n",
152 info->chunkbytes, info->blks_in_chunk);
153 total_chunks = (fs->super->s_blocks_count +
154 info->blks_in_chunk) >>
155 (info->chunkbits - info->blocksize_bits);
156 printf("Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n",
157 total_chunks, info->free_chunks,
158 (double)info->free_chunks * 100 / total_chunks);
161 /* Display chunk information in KB */
162 if (info->real_free_chunks) {
163 info->min = (info->min * fs->blocksize) >> 10;
164 info->max = (info->max * fs->blocksize) >> 10;
165 info->avg = (info->avg / info->real_free_chunks *
166 fs->blocksize) >> 10;
171 printf("\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n"
172 "Avg. free extent: %lu KB\n", info->min, info->max, info->avg);
174 printf("\nHISTOGRAM OF FREE EXTENT SIZES:\n");
175 printf("%s : %12s %12s %7s\n", "Extent Size Range", "Free extents",
176 "Free Blocks", "Percent");
177 for (i = 0; i < MAX_HIST; i++) {
178 end = 1 << (i + info->blocksize_bits - units);
179 if (info->histogram.fc_chunks[i] != 0)
180 printf("%5lu%c...%5lu%c- : %12lu %12lu %6.2f%%\n",
181 start, *unitp, end, *unitp,
182 info->histogram.fc_chunks[i],
183 info->histogram.fc_blocks[i],
184 (double)info->histogram.fc_blocks[i] * 100 /
185 fs->super->s_free_blocks_count);
187 if (start == 1<<10) {
197 void close_device(char *device_name, ext2_filsys fs)
199 int retval = ext2fs_close(fs);
202 com_err(device_name, retval, "while closing the filesystem.\n");
205 void collect_info(ext2_filsys fs, struct chunk_info *chunk_info)
207 unsigned int retval = 0, i, free_blks;
209 printf("Device: %s\n", fs->device_name);
210 printf("Blocksize: %u bytes\n", fs->blocksize);
212 retval = ext2fs_read_block_bitmap(fs);
214 com_err(fs->device_name, retval, "while reading block bitmap");
215 close_device(fs->device_name, fs);
219 init_chunk_info(fs, chunk_info);
221 retval = get_chunk_info(fs, chunk_info);
223 com_err(fs->device_name, retval, "while collecting chunk info");
224 close_device(fs->device_name, fs);
229 void open_device(char *device_name, ext2_filsys *fs)
232 int flag = EXT2_FLAG_FORCE;
234 retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
236 com_err(device_name, retval, "while opening filesystem");
241 int main(int argc, char *argv[])
243 struct chunk_info chunk_info = { };
244 errcode_t retval = 0;
245 ext2_filsys fs = NULL;
250 add_error_table(&et_ext2_error_table);
253 while ((c = getopt(argc, argv, "c:h")) != EOF) {
256 chunk_info.chunkbytes = strtoull(optarg, &end, 0);
258 fprintf(stderr, "%s: bad chunk size '%s'\n",
262 if (chunk_info.chunkbytes &
263 (chunk_info.chunkbytes - 1)) {
264 fprintf(stderr, "%s: chunk size must be a "
265 "power of 2.\n", argv[0]);
268 chunk_info.chunkbytes *= 1024;
271 fprintf(stderr, "%s: bad option '%c'\n",
279 if (optind == argc) {
280 fprintf(stderr, "%s: missing device name.\n", progname);
284 device_name = argv[optind];
286 open_device(device_name, &fs);
288 if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) {
289 fprintf(stderr, "%s: chunksize must be greater than or equal "
290 "to filesystem blocksize.\n", progname);
293 collect_info(fs, &chunk_info);
294 close_device(device_name, fs);