e2freefrag.c revision 0b2681f45b7f67eedfea6411eddbb858699ae553
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->chunkbits = ul_log2(info->chunkbytes); 56 info->blocksize_bits = ul_log2((unsigned long)fs->blocksize); 57 info->blks_in_chunk = info->chunkbytes >> info->blocksize_bits; 58 59 info->min = ~0UL; 60 info->max = info->avg = 0; 61 info->real_free_chunks = 0; 62 63 for (i = 0; i < MAX_HIST; i++) 64 info->histogram.fc_buckets[i] = 0; 65} 66 67void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info) 68{ 69 unsigned long long blocks_count = fs->super->s_blocks_count; 70 unsigned long long chunks = (blocks_count + info->blks_in_chunk) >> 71 (info->chunkbits - info->blocksize_bits); 72 unsigned long long chunk_num; 73 unsigned long last_chunk_size = 0; 74 unsigned long long chunk_start_blk = 0; 75 76 for (chunk_num = 0; chunk_num < chunks; chunk_num++) { 77 unsigned long long blk, num_blks; 78 int chunk_free; 79 80 /* Last chunk may be smaller */ 81 if (chunk_start_blk + info->blks_in_chunk > blocks_count) 82 num_blks = blocks_count - chunk_start_blk; 83 else 84 num_blks = info->blks_in_chunk; 85 86 chunk_free = 0; 87 88 /* Initialize starting block for first chunk correctly else 89 * there is a segfault when blocksize = 1024 in which case 90 * block_map->start = 1 */ 91 for (blk = (chunk_num == 0 ? fs->super->s_first_data_block : 0); 92 blk < num_blks; blk++, chunk_start_blk++) { 93 int used = ext2fs_fast_test_block_bitmap(fs->block_map, 94 chunk_start_blk); 95 if (!used) { 96 last_chunk_size++; 97 chunk_free++; 98 } 99 100 if (used && last_chunk_size != 0) { 101 unsigned long index; 102 103 index = ul_log2(last_chunk_size) + 1; 104 info->histogram.fc_buckets[index]++; 105 106 if (last_chunk_size > info->max) 107 info->max = last_chunk_size; 108 if (last_chunk_size < info->min) 109 info->min = last_chunk_size; 110 info->avg += last_chunk_size; 111 112 info->real_free_chunks++; 113 last_chunk_size = 0; 114 } 115 } 116 117 if (chunk_free == info->blks_in_chunk) 118 info->free_chunks++; 119 } 120} 121 122errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info) 123{ 124 unsigned long total_chunks; 125 char *unitp = "KMGTPEZY"; 126 int units = 10; 127 unsigned long start = 0, end, cum; 128 int i, retval = 0; 129 130 scan_block_bitmap(fs, info); 131 132 printf("Total blocks: %u\nFree blocks: %u (%0.1f%%)\n", 133 fs->super->s_blocks_count, fs->super->s_free_blocks_count, 134 (double)fs->super->s_free_blocks_count * 100 / 135 fs->super->s_blocks_count); 136 137 printf("\nChunksize: %lu bytes (%u blocks)\n", 138 info->chunkbytes, info->blks_in_chunk); 139 total_chunks = (fs->super->s_blocks_count + info->blks_in_chunk) >> 140 (info->chunkbits - info->blocksize_bits); 141 printf("Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n", 142 total_chunks, info->free_chunks, 143 (double)info->free_chunks * 100 / total_chunks); 144 145 /* Display chunk information in KB */ 146 if (info->real_free_chunks) { 147 info->min = (info->min * fs->blocksize) >> 10; 148 info->max = (info->max * fs->blocksize) >> 10; 149 info->avg = (info->avg / info->real_free_chunks * 150 fs->blocksize) >> 10; 151 } else { 152 info->min = 0; 153 } 154 155 printf("\nMin free chunk: %lu KB \nMax free chunk: %lu KB\n" 156 "Avg free chunk: %lu KB\n", info->min, info->max, info->avg); 157 158 printf("\nHISTOGRAM OF FREE CHUNK SIZES:\n"); 159 printf("%s\t%10s\n", "Chunk Size Range :", "Free chunks"); 160 for (i = 0; i < MAX_HIST; i++) { 161 end = 1 << (i + info->blocksize_bits - units); 162 if (info->histogram.fc_buckets[i] != 0) 163 printf("%5lu%c...%5lu%c- : %10lu\n", start, *unitp, 164 end, *unitp, info->histogram.fc_buckets[i]); 165 start = end; 166 if (start == 1<<10) { 167 start = 1; 168 units += 10; 169 unitp++; 170 } 171 } 172 173 return retval; 174} 175 176void close_device(char *device_name, ext2_filsys fs) 177{ 178 int retval = ext2fs_close(fs); 179 180 if (retval) 181 com_err(device_name, retval, "while closing the filesystem.\n"); 182} 183 184void collect_info(ext2_filsys fs, struct chunk_info *chunk_info) 185{ 186 unsigned int retval = 0, i, free_blks; 187 188 printf("Device: %s\n", fs->device_name); 189 printf("Blocksize: %u bytes\n", fs->blocksize); 190 191 retval = ext2fs_read_block_bitmap(fs); 192 if (retval) { 193 com_err(fs->device_name, retval, "while reading block bitmap"); 194 close_device(fs->device_name, fs); 195 exit(1); 196 } 197 198 init_chunk_info(fs, chunk_info); 199 200 retval = get_chunk_info(fs, chunk_info); 201 if (retval) { 202 com_err(fs->device_name, retval, "while collecting chunk info"); 203 close_device(fs->device_name, fs); 204 exit(1); 205 } 206} 207 208void open_device(char *device_name, ext2_filsys *fs) 209{ 210 int retval; 211 int flag = EXT2_FLAG_FORCE; 212 213 retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs); 214 if (retval) { 215 com_err(device_name, retval, "while opening filesystem"); 216 exit(1); 217 } 218} 219 220int main(int argc, char *argv[]) 221{ 222 struct chunk_info chunk_info = { .chunkbytes = DEFAULT_CHUNKSIZE }; 223 errcode_t retval = 0; 224 ext2_filsys fs = NULL; 225 char *device_name; 226 char *progname; 227 char c, *end; 228 229 progname = argv[0]; 230 231 while ((c = getopt(argc, argv, "c:h")) != EOF) { 232 switch (c) { 233 case 'c': 234 chunk_info.chunkbytes = strtoull(optarg, &end, 0); 235 if (*end != '\0') { 236 fprintf(stderr, "%s: bad chunk size '%s'\n", 237 progname, optarg); 238 usage(progname); 239 } 240 if (chunk_info.chunkbytes & 241 (chunk_info.chunkbytes - 1)) { 242 fprintf(stderr, "%s: chunk size must be a " 243 "power of 2.", argv[0]); 244 usage(progname); 245 } 246 chunk_info.chunkbytes *= 1024; 247 break; 248 default: 249 fprintf(stderr, "%s: bad option '%c'\n", 250 progname, c); 251 case 'h': 252 usage(progname); 253 break; 254 } 255 } 256 257 if (optind == argc) { 258 fprintf(stderr, "%s: missing device name.\n", progname); 259 usage(progname); 260 } 261 262 device_name = argv[optind]; 263 264 open_device(device_name, &fs); 265 266 if (chunk_info.chunkbytes < fs->blocksize) { 267 fprintf(stderr, "%s: chunksize must be greater than or equal " 268 "to filesystem blocksize.\n", progname); 269 exit(1); 270 } 271 collect_info(fs, &chunk_info); 272 close_device(device_name, fs); 273 274 return retval; 275} 276