2 * filefrag.c -- report if a particular file is fragmented
4 * Copyright 2003 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
17 fputs("This program is only supported on Linux!\n", stderr);
21 #define _LARGEFILE64_SOURCE
36 #include <sys/types.h>
39 #include <sys/ioctl.h>
44 #define FIBMAP _IO(0x00,1) /* bmap access */
45 #define FIGETBSZ _IO(0x00,2) /* get the block size used for bmap */
47 #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
48 #define EXT3_IOC_GETFLAGS _IOR('f', 1, long)
50 static unsigned int div_ceil(unsigned int a, unsigned int b)
54 return ((a - 1) / b) + 1;
57 static unsigned long get_bmap(int fd, unsigned long block)
63 ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes a pointer to an integer */
66 fprintf(stderr, "No permission to use FIBMAP ioctl; must have root privileges\n");
74 #define EXT2_DIRECT 12
76 static void frag_report(const char *filename)
80 struct stat64 fileinfo;
86 unsigned long block, last_block = 0, numblocks, i;
87 long bpib; /* Blocks per indirect block */
89 int discont = 0, expected;
93 if (statfs(filename, &fsinfo) < 0) {
98 if (stat64(filename, &fileinfo) < 0) {
100 if (stat(filename, &fileinfo) < 0) {
105 if (!S_ISREG(fileinfo.st_mode)) {
106 printf("%s: Not a regular file\n", filename);
109 if ((fsinfo.f_type == 0xef51) || (fsinfo.f_type == 0xef52) ||
110 (fsinfo.f_type == 0xef53))
113 printf("Filesystem type is: %lx\n",
114 (unsigned long) fsinfo.f_type);
116 cylgroups = div_ceil(fsinfo.f_blocks, fsinfo.f_bsize*8);
118 printf("Filesystem cylinder groups is approximately %ld\n",
122 fd = open64(filename, O_RDONLY);
124 fd = open(filename, O_RDONLY);
130 if (ioctl(fd, FIGETBSZ, &bs) < 0) { /* FIGETBSZ takes an int */
135 if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) < 0)
137 if (flags & EXT4_EXTENTS_FL) {
138 printf("File is stored in extents format\n");
142 printf("Blocksize of file %s is %d\n", filename, bs);
144 numblocks = (fileinfo.st_size + (bs-1)) / bs;
146 printf("File size of %s is %lld (%ld blocks)\n", filename,
147 (long long) fileinfo.st_size, numblocks);
148 printf("First block: %lu\nLast block: %lu\n",
149 get_bmap(fd, 0), get_bmap(fd, numblocks - 1));
151 for (i=0; i < numblocks; i++) {
152 if (is_ext2 && last_block) {
153 if (((i-EXT2_DIRECT) % bpib) == 0)
155 if (((i-EXT2_DIRECT-bpib) % (bpib*bpib)) == 0)
157 if (((i-EXT2_DIRECT-bpib-bpib*bpib) % (bpib*bpib*bpib)) == 0)
160 block = get_bmap(fd, i);
163 if (last_block && (block != last_block +1) ) {
165 printf("Discontinuity: Block %ld is at %lu (was %lu)\n",
166 i, block, last_block);
172 printf("%s: 1 extent found", filename);
174 printf("%s: %d extents found", filename, discont+1);
175 expected = (numblocks/((bs*8)-(fsinfo.f_files/8/cylgroups)-3))+1;
176 if (is_ext2 && expected != discont+1)
177 printf(", perfection would be %d extent%s\n", expected,
178 (expected>1) ? "s" : "");
184 static void usage(const char *progname)
186 fprintf(stderr, "Usage: %s [-v] file ...\n", progname);
190 int main(int argc, char**argv)
195 while ((c = getopt(argc, argv, "v")) != EOF)
206 for (cpp=argv+optind; *cpp; cpp++) {
208 printf("Checking %s\n", *cpp);