]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/e2freefrag.c
tune2fs: don't recover journal if device is busy.
[thirdparty/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: %llu (%0.1f%%)\n",
158 ext2fs_blocks_count(fs->super),
159 ext2fs_free_blocks_count(fs->super),
160 (double)ext2fs_free_blocks_count(fs->super) * 100 /
161 ext2fs_blocks_count(fs->super));
162
163 if (info->chunkbytes) {
164 fprintf(f, "\nChunksize: %lu bytes (%u blocks)\n",
165 info->chunkbytes, info->blks_in_chunk);
166 total_chunks = (ext2fs_blocks_count(fs->super) +
167 info->blks_in_chunk) >>
168 (info->chunkbits - info->blocksize_bits);
169 fprintf(f, "Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n",
170 total_chunks, info->free_chunks,
171 (double)info->free_chunks * 100 / total_chunks);
172 }
173
174 /* Display chunk information in KB */
175 if (info->real_free_chunks) {
176 unsigned int scale = fs->blocksize >> 10;
177 info->min = info->min * scale;
178 info->max = info->max * scale;
179 info->avg = info->avg / info->real_free_chunks * scale;
180 } else {
181 info->min = 0;
182 }
183
184 fprintf(f, "\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n"
185 "Avg. free extent: %lu KB\n", info->min, info->max, info->avg);
186 fprintf(f, "Num. free extent: %lu\n", info->real_free_chunks);
187
188 fprintf(f, "\nHISTOGRAM OF FREE EXTENT SIZES:\n");
189 fprintf(f, "%s : %12s %12s %7s\n", "Extent Size Range",
190 "Free extents", "Free Blocks", "Percent");
191 for (i = 0; i < MAX_HIST; i++) {
192 end = 1 << (i + info->blocksize_bits - units);
193 if (info->histogram.fc_chunks[i] != 0) {
194 char end_str[32];
195
196 sprintf(end_str, "%5lu%c-", end, *unitp);
197 if (i == MAX_HIST-1)
198 strcpy(end_str, "max ");
199 fprintf(f, "%5lu%c...%7s : %12lu %12lu %6.2f%%\n",
200 start, *unitp, end_str,
201 info->histogram.fc_chunks[i],
202 info->histogram.fc_blocks[i],
203 (double)info->histogram.fc_blocks[i] * 100 /
204 ext2fs_free_blocks_count(fs->super));
205 }
206 start = end;
207 if (start == 1<<10) {
208 start = 1;
209 units += 10;
210 unitp++;
211 }
212 }
213
214 return retval;
215 }
216
217 static void close_device(char *device_name, ext2_filsys fs)
218 {
219 int retval = ext2fs_close_free(&fs);
220
221 if (retval)
222 com_err(device_name, retval, "while closing the filesystem.\n");
223 }
224
225 static void collect_info(ext2_filsys fs, struct chunk_info *chunk_info, FILE *f)
226 {
227 unsigned int retval = 0;
228
229 fprintf(f, "Device: %s\n", fs->device_name);
230 fprintf(f, "Blocksize: %u bytes\n", fs->blocksize);
231
232 retval = ext2fs_read_block_bitmap(fs);
233 if (retval) {
234 com_err(fs->device_name, retval, "while reading block bitmap");
235 close_device(fs->device_name, fs);
236 exit(1);
237 }
238
239 init_chunk_info(fs, chunk_info);
240
241 retval = get_chunk_info(fs, chunk_info, f);
242 if (retval) {
243 com_err(fs->device_name, retval, "while collecting chunk info");
244 close_device(fs->device_name, fs);
245 exit(1);
246 }
247 }
248
249 #ifndef DEBUGFS
250 static void open_device(char *device_name, ext2_filsys *fs)
251 {
252 int retval;
253 int flag = EXT2_FLAG_FORCE | EXT2_FLAG_64BITS;
254
255 retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
256 if (retval) {
257 com_err(device_name, retval, "while opening filesystem");
258 exit(1);
259 }
260 (*fs)->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
261 }
262 #endif
263
264 #ifdef DEBUGFS
265 #include "debugfs.h"
266
267 void do_freefrag(int argc, char **argv)
268 #else
269 int main(int argc, char *argv[])
270 #endif
271 {
272 struct chunk_info chunk_info;
273 ext2_filsys fs = NULL;
274 char *progname;
275 char *end;
276 int c;
277
278 #ifdef DEBUGFS
279 if (check_fs_open(argv[0]))
280 return;
281 reset_getopt();
282 #else
283 char *device_name;
284
285 add_error_table(&et_ext2_error_table);
286 #endif
287 progname = argv[0];
288 memset(&chunk_info, 0, sizeof(chunk_info));
289
290 while ((c = getopt(argc, argv, "c:h")) != EOF) {
291 switch (c) {
292 case 'c':
293 chunk_info.chunkbytes = strtoull(optarg, &end, 0);
294 if (*end != '\0') {
295 fprintf(stderr, "%s: bad chunk size '%s'\n",
296 progname, optarg);
297 usage(progname);
298 }
299 if (chunk_info.chunkbytes &
300 (chunk_info.chunkbytes - 1)) {
301 fprintf(stderr, "%s: chunk size must be a "
302 "power of 2.\n", argv[0]);
303 usage(progname);
304 }
305 chunk_info.chunkbytes *= 1024;
306 break;
307 case 'h':
308 default:
309 usage(progname);
310 break;
311 }
312 }
313
314 #ifndef DEBUGFS
315 if (optind == argc) {
316 fprintf(stderr, "%s: missing device name.\n", progname);
317 usage(progname);
318 }
319
320 device_name = argv[optind];
321
322 open_device(device_name, &fs);
323 #else
324 fs = current_fs;
325 #endif
326
327 if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) {
328 fprintf(stderr, "%s: chunksize must be greater than or equal "
329 "to filesystem blocksize.\n", progname);
330 exit(1);
331 }
332 collect_info(fs, &chunk_info, stdout);
333 #ifndef DEBUGFS
334 close_device(device_name, fs);
335
336 return 0;
337 #endif
338 }