]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/e2freefrag.c
Merge branch 'maint' into next
[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 #if defined(HAVE_EXT2_IOCTLS) && !defined(DEBUGFS)
29 # include <sys/ioctl.h>
30 # include <sys/types.h>
31 # include <sys/stat.h>
32 # include <fcntl.h>
33 # include <limits.h>
34 #endif
35
36 #include "ext2fs/ext2_fs.h"
37 #include "ext2fs/ext2fs.h"
38 #include "e2freefrag.h"
39
40 #if defined(HAVE_EXT2_IOCTLS) && !defined(DEBUGFS)
41 # ifdef HAVE_LINUX_FSMAP_H
42 # include <linux/fsmap.h>
43 # endif
44 # include "fsmap.h"
45 #endif
46
47 static void usage(const char *prog)
48 {
49 fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
50 "device_name\n", prog);
51 #ifndef DEBUGFS
52 exit(1);
53 #endif
54 }
55
56 static int ul_log2(unsigned long arg)
57 {
58 int l = 0;
59
60 arg >>= 1;
61 while (arg) {
62 l++;
63 arg >>= 1;
64 }
65 return l;
66 }
67
68 static void init_chunk_info(ext2_filsys fs, struct chunk_info *info)
69 {
70 int i;
71
72 info->blocksize_bits = ul_log2((unsigned long)fs->blocksize);
73 if (info->chunkbytes) {
74 info->chunkbits = ul_log2(info->chunkbytes);
75 info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits;
76 } else {
77 info->chunkbits = ul_log2(DEFAULT_CHUNKSIZE);
78 info->blks_in_chunk = DEFAULT_CHUNKSIZE >> info->blocksize_bits;
79 }
80
81 info->min = ~0UL;
82 info->max = info->avg = 0;
83 info->real_free_chunks = 0;
84
85 for (i = 0; i < MAX_HIST; i++) {
86 info->histogram.fc_chunks[i] = 0;
87 info->histogram.fc_blocks[i] = 0;
88 }
89 }
90
91 static void update_chunk_stats(struct chunk_info *info,
92 unsigned long chunk_size)
93 {
94 unsigned long idx;
95
96 idx = ul_log2(chunk_size) + 1;
97 if (idx >= MAX_HIST)
98 idx = MAX_HIST-1;
99 info->histogram.fc_chunks[idx]++;
100 info->histogram.fc_blocks[idx] += chunk_size;
101
102 if (chunk_size > info->max)
103 info->max = chunk_size;
104 if (chunk_size < info->min)
105 info->min = chunk_size;
106 info->avg += chunk_size;
107 info->real_free_chunks++;
108 }
109
110 static void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
111 {
112 unsigned long long blocks_count = ext2fs_blocks_count(fs->super);
113 unsigned long long chunks = (blocks_count + info->blks_in_chunk) >>
114 (info->chunkbits - info->blocksize_bits);
115 unsigned long long chunk_num;
116 unsigned long last_chunk_size = 0;
117 unsigned long long chunk_start_blk = 0;
118 int used;
119
120 for (chunk_num = 0; chunk_num < chunks; chunk_num++) {
121 unsigned long long blk, num_blks;
122 int chunk_free;
123
124 /* Last chunk may be smaller */
125 if (chunk_start_blk + info->blks_in_chunk > blocks_count)
126 num_blks = blocks_count - chunk_start_blk;
127 else
128 num_blks = info->blks_in_chunk;
129
130 chunk_free = 0;
131
132 /* Initialize starting block for first chunk correctly else
133 * there is a segfault when blocksize = 1024 in which case
134 * block_map->start = 1 */
135 for (blk = 0; blk < num_blks; blk++, chunk_start_blk++) {
136 if (chunk_num == 0 && blk == 0) {
137 blk = fs->super->s_first_data_block;
138 chunk_start_blk = blk;
139 }
140 used = ext2fs_fast_test_block_bitmap2(fs->block_map,
141 chunk_start_blk >> fs->cluster_ratio_bits);
142 if (!used) {
143 last_chunk_size++;
144 chunk_free++;
145 }
146
147 if (used && last_chunk_size != 0) {
148 update_chunk_stats(info, last_chunk_size);
149 last_chunk_size = 0;
150 }
151 }
152
153 if (chunk_free == info->blks_in_chunk)
154 info->free_chunks++;
155 }
156 if (last_chunk_size != 0)
157 update_chunk_stats(info, last_chunk_size);
158 }
159
160 #if defined(HAVE_EXT2_IOCTLS) && !defined(DEBUGFS)
161 # define FSMAP_EXTENTS 1024
162 static int scan_online(ext2_filsys fs, struct chunk_info *info)
163 {
164 struct fsmap_head *fsmap;
165 struct fsmap *extent;
166 struct fsmap *p;
167 char mntpoint[PATH_MAX + 1];
168 errcode_t retval;
169 int mount_flags;
170 int fd;
171 int ret;
172 int i;
173
174 /* Try to open the mountpoint for a live query. */
175 retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
176 mntpoint, PATH_MAX);
177 if (retval) {
178 com_err(fs->device_name, retval, "while checking mount status");
179 return 0;
180 }
181 if (!mount_flags & EXT2_MF_MOUNTED)
182 return 0;
183 fd = open(mntpoint, O_RDONLY);
184 if (fd < 0) {
185 com_err(mntpoint, errno, "while opening mount point");
186 return 0;
187 }
188
189 fsmap = malloc(fsmap_sizeof(FSMAP_EXTENTS));
190 if (!fsmap) {
191 com_err(fs->device_name, errno, "while allocating memory");
192 return 0;
193 }
194
195 memset(fsmap, 0, sizeof(*fsmap));
196 fsmap->fmh_count = FSMAP_EXTENTS;
197 fsmap->fmh_keys[1].fmr_device = UINT_MAX;
198 fsmap->fmh_keys[1].fmr_physical = ULLONG_MAX;
199 fsmap->fmh_keys[1].fmr_owner = ULLONG_MAX;
200 fsmap->fmh_keys[1].fmr_offset = ULLONG_MAX;
201 fsmap->fmh_keys[1].fmr_flags = UINT_MAX;
202
203 /* Fill the extent histogram with live data */
204 while (1) {
205 ret = ioctl(fd, FS_IOC_GETFSMAP, fsmap);
206 if (ret < 0) {
207 com_err(fs->device_name, errno, "while calling fsmap");
208 free(fsmap);
209 return 0;
210 }
211
212 /* No more extents to map, exit */
213 if (!fsmap->fmh_entries)
214 break;
215
216 for (i = 0, extent = fsmap->fmh_recs;
217 i < fsmap->fmh_entries;
218 i++, extent++) {
219 if (!(extent->fmr_flags & FMR_OF_SPECIAL_OWNER) ||
220 extent->fmr_owner != FMR_OWN_FREE)
221 continue;
222 update_chunk_stats(info,
223 extent->fmr_length / fs->blocksize);
224 }
225
226 p = &fsmap->fmh_recs[fsmap->fmh_entries - 1];
227 if (p->fmr_flags & FMR_OF_LAST)
228 break;
229 fsmap_advance(fsmap);
230 }
231
232 return 1;
233 }
234 #else
235 # define scan_online(fs, info) (0)
236 #endif /* HAVE_EXT2_IOCTLS */
237
238 static errcode_t scan_offline(ext2_filsys fs, struct chunk_info *info)
239 {
240 errcode_t retval;
241
242 retval = ext2fs_read_block_bitmap(fs);
243 if (retval)
244 return retval;
245 scan_block_bitmap(fs, info);
246 return 0;
247 }
248
249 static errcode_t dump_chunk_info(ext2_filsys fs, struct chunk_info *info,
250 FILE *f)
251 {
252 unsigned long total_chunks;
253 const char *unitp = "KMGTPEZY";
254 int units = 10;
255 unsigned long start = 0, end;
256 int i, retval = 0;
257
258 fprintf(f, "Total blocks: %llu\nFree blocks: %u (%0.1f%%)\n",
259 ext2fs_blocks_count(fs->super), fs->super->s_free_blocks_count,
260 (double)fs->super->s_free_blocks_count * 100 /
261 ext2fs_blocks_count(fs->super));
262
263 if (info->chunkbytes) {
264 fprintf(f, "\nChunksize: %lu bytes (%u blocks)\n",
265 info->chunkbytes, info->blks_in_chunk);
266 total_chunks = (ext2fs_blocks_count(fs->super) +
267 info->blks_in_chunk) >>
268 (info->chunkbits - info->blocksize_bits);
269 fprintf(f, "Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n",
270 total_chunks, info->free_chunks,
271 (double)info->free_chunks * 100 / total_chunks);
272 }
273
274 /* Display chunk information in KB */
275 if (info->real_free_chunks) {
276 unsigned int scale = fs->blocksize >> 10;
277 info->min = info->min * scale;
278 info->max = info->max * scale;
279 info->avg = info->avg / info->real_free_chunks * scale;
280 } else {
281 info->min = 0;
282 }
283
284 fprintf(f, "\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n"
285 "Avg. free extent: %lu KB\n", info->min, info->max, info->avg);
286 fprintf(f, "Num. free extent: %lu\n", info->real_free_chunks);
287
288 fprintf(f, "\nHISTOGRAM OF FREE EXTENT SIZES:\n");
289 fprintf(f, "%s : %12s %12s %7s\n", "Extent Size Range",
290 "Free extents", "Free Blocks", "Percent");
291 for (i = 0; i < MAX_HIST; i++) {
292 end = 1 << (i + info->blocksize_bits - units);
293 if (info->histogram.fc_chunks[i] != 0) {
294 char end_str[32];
295
296 sprintf(end_str, "%5lu%c-", end, *unitp);
297 if (i == MAX_HIST-1)
298 strcpy(end_str, "max ");
299 fprintf(f, "%5lu%c...%7s : %12lu %12lu %6.2f%%\n",
300 start, *unitp, end_str,
301 info->histogram.fc_chunks[i],
302 info->histogram.fc_blocks[i],
303 (double)info->histogram.fc_blocks[i] * 100 /
304 fs->super->s_free_blocks_count);
305 }
306 start = end;
307 if (start == 1<<10) {
308 start = 1;
309 units += 10;
310 unitp++;
311 }
312 }
313
314 return retval;
315 }
316
317 static void close_device(char *device_name, ext2_filsys fs)
318 {
319 int retval = ext2fs_close_free(&fs);
320
321 if (retval)
322 com_err(device_name, retval, "while closing the filesystem.\n");
323 }
324
325 static void collect_info(ext2_filsys fs, struct chunk_info *chunk_info, FILE *f)
326 {
327 unsigned int retval = 0;
328
329 fprintf(f, "Device: %s\n", fs->device_name);
330 fprintf(f, "Blocksize: %u bytes\n", fs->blocksize);
331
332 init_chunk_info(fs, chunk_info);
333 if (!scan_online(fs, chunk_info)) {
334 init_chunk_info(fs, chunk_info);
335 retval = scan_offline(fs, chunk_info);
336 }
337 if (retval) {
338 com_err(fs->device_name, retval, "while reading block bitmap");
339 close_device(fs->device_name, fs);
340 exit(1);
341 }
342
343 retval = dump_chunk_info(fs, chunk_info, f);
344 if (retval) {
345 com_err(fs->device_name, retval, "while dumping chunk info");
346 close_device(fs->device_name, fs);
347 exit(1);
348 }
349 }
350
351 #ifndef DEBUGFS
352 static void open_device(char *device_name, ext2_filsys *fs)
353 {
354 int retval;
355 int flag = EXT2_FLAG_FORCE | EXT2_FLAG_64BITS;
356
357 retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
358 if (retval) {
359 com_err(device_name, retval, "while opening filesystem");
360 exit(1);
361 }
362 (*fs)->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
363 }
364 #endif
365
366 #ifdef DEBUGFS
367 #include "debugfs.h"
368
369 void do_freefrag(int argc, char **argv)
370 #else
371 int main(int argc, char *argv[])
372 #endif
373 {
374 struct chunk_info chunk_info;
375 ext2_filsys fs = NULL;
376 char *progname;
377 char *end;
378 int c;
379
380 #ifdef DEBUGFS
381 if (check_fs_open(argv[0]))
382 return;
383 reset_getopt();
384 #else
385 char *device_name;
386
387 add_error_table(&et_ext2_error_table);
388 #endif
389 progname = argv[0];
390 memset(&chunk_info, 0, sizeof(chunk_info));
391
392 while ((c = getopt(argc, argv, "c:h")) != EOF) {
393 switch (c) {
394 case 'c':
395 chunk_info.chunkbytes = strtoull(optarg, &end, 0);
396 if (*end != '\0') {
397 fprintf(stderr, "%s: bad chunk size '%s'\n",
398 progname, optarg);
399 usage(progname);
400 }
401 if (chunk_info.chunkbytes &
402 (chunk_info.chunkbytes - 1)) {
403 fprintf(stderr, "%s: chunk size must be a "
404 "power of 2.\n", argv[0]);
405 usage(progname);
406 }
407 chunk_info.chunkbytes *= 1024;
408 break;
409 case 'h':
410 default:
411 usage(progname);
412 break;
413 }
414 }
415
416 #ifndef DEBUGFS
417 if (optind == argc) {
418 fprintf(stderr, "%s: missing device name.\n", progname);
419 usage(progname);
420 }
421
422 device_name = argv[optind];
423
424 open_device(device_name, &fs);
425 #else
426 fs = current_fs;
427 #endif
428
429 if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) {
430 fprintf(stderr, "%s: chunksize must be greater than or equal "
431 "to filesystem blocksize.\n", progname);
432 exit(1);
433 }
434 collect_info(fs, &chunk_info, stdout);
435 #ifndef DEBUGFS
436 close_device(device_name, fs);
437
438 return 0;
439 #endif
440 }