]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/e2freefrag.c
debugfs: build read-only variant of debugfs
[thirdparty/e2fsprogs.git] / misc / e2freefrag.c
CommitLineData
0b2681f4
TT
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 */
d1154eb4 14#include "config.h"
0b2681f4
TT
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
25extern char *optarg;
26extern int optind;
27#endif
28
29#include "ext2fs/ext2_fs.h"
30#include "ext2fs/ext2fs.h"
31#include "e2freefrag.h"
32
33void usage(const char *prog)
34{
35 fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
36 "device_name\n", prog);
37 exit(1);
38}
39
40static int ul_log2(unsigned long arg)
41{
42 int l = 0;
43
44 arg >>= 1;
45 while (arg) {
46 l++;
47 arg >>= 1;
48 }
49 return l;
50}
51
52void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
53{
54 int i;
55
0b2681f4 56 info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
cba91c4b
TT
57 if (info->chunkbytes) {
58 info->chunkbits = ul_log2(info->chunkbytes);
59 info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
60 } else {
61 info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
62 info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
63 }
0b2681f4
TT
64
65 info->min = ~0UL;
66 info->max = info->avg = 0;
67 info->real_free_chunks = 0;
68
ad751f11
AD
69 for (i = 0; i < MAX_HIST; i++) {
70 info->histogram.fc_chunks[i] = 0;
71 info->histogram.fc_blocks[i] = 0;
72 }
0b2681f4
TT
73}
74
1e48a456
TT
75void update_chunk_stats(struct chunk_info *info, unsigned long chunk_size)
76{
77 unsigned long index;
78
79 index = ul_log2(chunk_size) + 1;
80 if (index >= MAX_HIST)
81 index = MAX_HIST-1;
82 info->histogram.fc_chunks[index]++;
83 info->histogram.fc_blocks[index] += chunk_size;
84
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++;
91}
92
0b2681f4
TT
93void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
94{
4efbac6f 95 unsigned long long blocks_count = ext2fs_blocks_count(fs->super);
0b2681f4
TT
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;
3e343b8d 101 int used;
0b2681f4
TT
102
103 for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
104 unsigned long long blk, num_blks;
105 int chunk_free;
106
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;
110 else
111 num_blks = info->blks_in_chunk;
112
113 chunk_free = 0;
114
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 */
3e343b8d
TT
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;
122 }
3c041a51 123 used = ext2fs_fast_test_block_bitmap2(fs->block_map,
554bc091 124 chunk_start_blk >> fs->cluster_ratio_bits);
0b2681f4
TT
125 if (!used) {
126 last_chunk_size++;
127 chunk_free++;
128 }
129
130 if (used && last_chunk_size != 0) {
1e48a456 131 update_chunk_stats(info, last_chunk_size);
0b2681f4
TT
132 last_chunk_size = 0;
133 }
134 }
135
136 if (chunk_free == info->blks_in_chunk)
137 info->free_chunks++;
138 }
1e48a456
TT
139 if (last_chunk_size != 0)
140 update_chunk_stats(info, last_chunk_size);
0b2681f4
TT
141}
142
143errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info)
144{
145 unsigned long total_chunks;
146 char *unitp = "KMGTPEZY";
147 int units = 10;
cf5301d7 148 unsigned long start = 0, end;
0b2681f4
TT
149 int i, retval = 0;
150
151 scan_block_bitmap(fs, info);
152
4efbac6f
VAH
153 printf("Total blocks: %llu\nFree blocks: %u (%0.1f%%)\n",
154 ext2fs_blocks_count(fs->super), fs->super->s_free_blocks_count,
0b2681f4 155 (double)fs->super->s_free_blocks_count * 100 /
4efbac6f 156 ext2fs_blocks_count(fs->super));
0b2681f4 157
cba91c4b
TT
158 if (info->chunkbytes) {
159 printf("\nChunksize: %lu bytes (%u blocks)\n",
160 info->chunkbytes, info->blks_in_chunk);
4efbac6f 161 total_chunks = (ext2fs_blocks_count(fs->super) +
cba91c4b
TT
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);
167 }
0b2681f4
TT
168
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;
175 } else {
176 info->min = 0;
177 }
178
aff2cf8e
TT
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);
f96cb89e 181 printf("Num. free extent: %lu\n", info->real_free_chunks);
0b2681f4 182
aff2cf8e
TT
183 printf("\nHISTOGRAM OF FREE EXTENT SIZES:\n");
184 printf("%s : %12s %12s %7s\n", "Extent Size Range", "Free extents",
ad751f11 185 "Free Blocks", "Percent");
0b2681f4
TT
186 for (i = 0; i < MAX_HIST; i++) {
187 end = 1 << (i + info->blocksize_bits - units);
1e48a456
TT
188 if (info->histogram.fc_chunks[i] != 0) {
189 char end_str[32];
190
191 sprintf(end_str, "%5lu%c-", end, *unitp);
192 if (i == MAX_HIST-1)
193 strcpy(end_str, "max ");
194 printf("%5lu%c...%7s : %12lu %12lu %6.2f%%\n",
195 start, *unitp, end_str,
ad751f11
AD
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);
1e48a456 200 }
0b2681f4
TT
201 start = end;
202 if (start == 1<<10) {
203 start = 1;
204 units += 10;
205 unitp++;
206 }
207 }
208
209 return retval;
210}
211
212void close_device(char *device_name, ext2_filsys fs)
213{
214 int retval = ext2fs_close(fs);
215
216 if (retval)
217 com_err(device_name, retval, "while closing the filesystem.\n");
218}
219
220void collect_info(ext2_filsys fs, struct chunk_info *chunk_info)
221{
cf5301d7 222 unsigned int retval = 0;
0b2681f4
TT
223
224 printf("Device: %s\n", fs->device_name);
225 printf("Blocksize: %u bytes\n", fs->blocksize);
226
227 retval = ext2fs_read_block_bitmap(fs);
228 if (retval) {
229 com_err(fs->device_name, retval, "while reading block bitmap");
230 close_device(fs->device_name, fs);
231 exit(1);
232 }
233
234 init_chunk_info(fs, chunk_info);
235
236 retval = get_chunk_info(fs, chunk_info);
237 if (retval) {
238 com_err(fs->device_name, retval, "while collecting chunk info");
239 close_device(fs->device_name, fs);
240 exit(1);
241 }
242}
243
244void open_device(char *device_name, ext2_filsys *fs)
245{
246 int retval;
247 int flag = EXT2_FLAG_FORCE;
248
249 retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
250 if (retval) {
251 com_err(device_name, retval, "while opening filesystem");
252 exit(1);
253 }
254}
255
256int main(int argc, char *argv[])
257{
cba91c4b 258 struct chunk_info chunk_info = { };
0b2681f4
TT
259 errcode_t retval = 0;
260 ext2_filsys fs = NULL;
261 char *device_name;
262 char *progname;
b887f08f
MF
263 char *end;
264 int c;
0b2681f4 265
137a7dc0 266 add_error_table(&et_ext2_error_table);
0b2681f4
TT
267 progname = argv[0];
268
269 while ((c = getopt(argc, argv, "c:h")) != EOF) {
270 switch (c) {
271 case 'c':
272 chunk_info.chunkbytes = strtoull(optarg, &end, 0);
273 if (*end != '\0') {
274 fprintf(stderr, "%s: bad chunk size '%s'\n",
275 progname, optarg);
276 usage(progname);
277 }
278 if (chunk_info.chunkbytes &
279 (chunk_info.chunkbytes - 1)) {
280 fprintf(stderr, "%s: chunk size must be a "
aff2cf8e 281 "power of 2.\n", argv[0]);
0b2681f4
TT
282 usage(progname);
283 }
284 chunk_info.chunkbytes *= 1024;
285 break;
0b2681f4 286 case 'h':
4ebbc0a3 287 default:
0b2681f4
TT
288 usage(progname);
289 break;
290 }
291 }
292
293 if (optind == argc) {
294 fprintf(stderr, "%s: missing device name.\n", progname);
295 usage(progname);
296 }
297
298 device_name = argv[optind];
299
300 open_device(device_name, &fs);
301
cba91c4b 302 if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) {
0b2681f4
TT
303 fprintf(stderr, "%s: chunksize must be greater than or equal "
304 "to filesystem blocksize.\n", progname);
305 exit(1);
306 }
307 collect_info(fs, &chunk_info);
308 close_device(device_name, fs);
309
310 return retval;
311}