e2freefrag.c revision 3e343b8d9af349301a2acd6b4328fb5663deb60c
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 <stdio.h>
15#ifdef HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#ifdef HAVE_STDLIB_H
19#include <stdlib.h>
20#endif
21#ifdef HAVE_GETOPT_H
22#include <getopt.h>
23#else
24extern char *optarg;
25extern int optind;
26#endif
27
28#include "ext2fs/ext2_fs.h"
29#include "ext2fs/ext2fs.h"
30#include "e2freefrag.h"
31
32void usage(const char *prog)
33{
34	fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
35		"device_name\n", prog);
36	exit(1);
37}
38
39static int ul_log2(unsigned long arg)
40{
41        int     l = 0;
42
43        arg >>= 1;
44        while (arg) {
45                l++;
46                arg >>= 1;
47        }
48        return l;
49}
50
51void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
52{
53	int i;
54
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;
59	} else {
60		info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
61		info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
62	}
63
64	info->min = ~0UL;
65	info->max = info->avg = 0;
66	info->real_free_chunks = 0;
67
68	for (i = 0; i < MAX_HIST; i++) {
69		info->histogram.fc_chunks[i] = 0;
70		info->histogram.fc_blocks[i] = 0;
71	}
72}
73
74void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
75{
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;
82	int used;
83
84	for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
85		unsigned long long blk, num_blks;
86		int chunk_free;
87
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;
91		else
92			num_blks = info->blks_in_chunk;
93
94		chunk_free = 0;
95
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;
103			}
104			used = ext2fs_fast_test_block_bitmap(fs->block_map,
105							     chunk_start_blk);
106			if (!used) {
107				last_chunk_size++;
108				chunk_free++;
109			}
110
111			if (used && last_chunk_size != 0) {
112				unsigned long index;
113
114				index = ul_log2(last_chunk_size) + 1;
115				info->histogram.fc_chunks[index]++;
116				info->histogram.fc_blocks[index] +=
117							last_chunk_size;
118
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;
124
125				info->real_free_chunks++;
126				last_chunk_size = 0;
127			}
128		}
129
130		if (chunk_free == info->blks_in_chunk)
131			info->free_chunks++;
132	}
133}
134
135errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info)
136{
137	unsigned long total_chunks;
138	char *unitp = "KMGTPEZY";
139	int units = 10;
140	unsigned long start = 0, end, cum;
141	int i, retval = 0;
142
143	scan_block_bitmap(fs, info);
144
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);
149
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);
159	}
160
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;
167	} else {
168		info->min = 0;
169	}
170
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);
173
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);
186		start = end;
187		if (start == 1<<10) {
188			start = 1;
189			units += 10;
190			unitp++;
191		}
192	}
193
194	return retval;
195}
196
197void close_device(char *device_name, ext2_filsys fs)
198{
199	int retval = ext2fs_close(fs);
200
201	if (retval)
202		com_err(device_name, retval, "while closing the filesystem.\n");
203}
204
205void collect_info(ext2_filsys fs, struct chunk_info *chunk_info)
206{
207	unsigned int retval = 0, i, free_blks;
208
209	printf("Device: %s\n", fs->device_name);
210	printf("Blocksize: %u bytes\n", fs->blocksize);
211
212	retval = ext2fs_read_block_bitmap(fs);
213	if (retval) {
214		com_err(fs->device_name, retval, "while reading block bitmap");
215		close_device(fs->device_name, fs);
216		exit(1);
217	}
218
219	init_chunk_info(fs, chunk_info);
220
221	retval = get_chunk_info(fs, chunk_info);
222	if (retval) {
223		com_err(fs->device_name, retval, "while collecting chunk info");
224                close_device(fs->device_name, fs);
225		exit(1);
226	}
227}
228
229void open_device(char *device_name, ext2_filsys *fs)
230{
231	int retval;
232	int flag = EXT2_FLAG_FORCE;
233
234	retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
235	if (retval) {
236		com_err(device_name, retval, "while opening filesystem");
237		exit(1);
238	}
239}
240
241int main(int argc, char *argv[])
242{
243	struct chunk_info chunk_info = { };
244	errcode_t retval = 0;
245	ext2_filsys fs = NULL;
246	char *device_name;
247	char *progname;
248	char c, *end;
249
250	add_error_table(&et_ext2_error_table);
251	progname = argv[0];
252
253	while ((c = getopt(argc, argv, "c:h")) != EOF) {
254		switch (c) {
255		case 'c':
256			chunk_info.chunkbytes = strtoull(optarg, &end, 0);
257			if (*end != '\0') {
258				fprintf(stderr, "%s: bad chunk size '%s'\n",
259					progname, optarg);
260				usage(progname);
261			}
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]);
266				usage(progname);
267			}
268			chunk_info.chunkbytes *= 1024;
269			break;
270		default:
271			fprintf(stderr, "%s: bad option '%c'\n",
272				progname, c);
273		case 'h':
274			usage(progname);
275			break;
276		}
277	}
278
279	if (optind == argc) {
280		fprintf(stderr, "%s: missing device name.\n", progname);
281		usage(progname);
282	}
283
284	device_name = argv[optind];
285
286	open_device(device_name, &fs);
287
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);
291		exit(1);
292	}
293	collect_info(fs, &chunk_info);
294	close_device(device_name, fs);
295
296	return retval;
297}
298