Whamcloud - gitweb
Fix filefrag to be 32-bit clean
[tools/e2fsprogs.git] / misc / filefrag.c
1 /*
2  * filefrag.c -- report if a particular file is fragmented
3  * 
4  * Copyright 2003 by Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #ifndef __linux__
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 int main(void) {
17     fputs("This program is only supported on Linux!\n", stderr);
18     exit(EXIT_FAILURE);
19 }
20 #else
21 #define _LARGEFILE64_SOURCE
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #ifdef HAVE_GETOPT_H
31 #include <getopt.h>
32 #else
33 extern char *optarg;
34 extern int optind;
35 #endif
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/vfs.h>
39 #include <sys/ioctl.h>
40 #include <linux/fd.h>
41
42 int verbose = 0;
43
44 #define FIBMAP     _IO(0x00,1)  /* bmap access */
45 #define FIGETBSZ   _IO(0x00,2)  /* get the block size used for bmap */
46
47 #define EXT3_EXTENTS_FL                 0x00080000 /* Inode uses extents */
48 #define EXT3_IOC_GETFLAGS               _IOR('f', 1, long)
49
50 static unsigned long get_bmap(int fd, unsigned long block)
51 {
52         int     ret;
53         unsigned int b;
54
55         b = block;
56         ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes a pointer to an integer */
57         if (ret < 0) {
58                 if (errno == EPERM) {
59                         fprintf(stderr, "No permission to use FIBMAP ioctl; must have root privileges\n");
60                         exit(1);
61                 }
62                 perror("FIBMAP");
63         }
64         return b;
65 }
66
67 #define EXT2_DIRECT     12
68
69 static void frag_report(const char *filename)
70 {
71         struct statfs   fsinfo;
72         struct stat64   fileinfo;
73         int             bs;
74         long            i, fd;
75         unsigned long   block, last_block = 0, numblocks;
76         long            bpib;   /* Blocks per indirect block */
77         long            cylgroups;
78         int             discont = 0, expected;
79         int             is_ext2 = 0;
80         unsigned int    flags;
81
82         if (statfs(filename, &fsinfo) < 0) {
83                 perror("statfs");
84                 return;
85         }
86         if (stat64(filename, &fileinfo) < 0) {
87                 perror("stat");
88                 return;
89         }
90         if (!S_ISREG(fileinfo.st_mode)) {
91                 printf("%s: Not a regular file\n", filename);
92                 return;
93         }
94         if ((fsinfo.f_type == 0xef51) || (fsinfo.f_type == 0xef52) || 
95             (fsinfo.f_type == 0xef53))
96                 is_ext2++;
97         if (verbose) {
98                 printf("Filesystem type is: %x\n", fsinfo.f_type);
99         }
100         cylgroups = (fsinfo.f_blocks + fsinfo.f_bsize*8-1) / fsinfo.f_bsize*8;
101         if (verbose) {
102                 printf("Filesystem cylinder groups is approximately %ld\n", 
103                        cylgroups);
104         }
105         fd = open(filename, O_RDONLY | O_LARGEFILE);
106         if (fd < 0) {
107                 perror("open");
108                 return;
109         }
110         if (ioctl(fd, FIGETBSZ, &bs) < 0) { /* FIGETBSZ takes an int */
111                 perror("FIGETBSZ");
112                 close(fd);
113                 return;
114         }
115         if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) < 0)
116                 flags = 0;
117         if (flags & EXT3_EXTENTS_FL) {
118                 printf("File is stored in extents format\n");
119                 is_ext2 = 0;
120         }
121         if (verbose)
122                 printf("Blocksize of file %s is %d\n", filename, bs);
123         bpib = bs / 4;
124         numblocks = (fileinfo.st_size + (bs-1)) / bs;
125         if (verbose) {
126                 printf("File size of %s is %lld (%ld blocks)\n", filename, 
127                        (long long) fileinfo.st_size, numblocks);
128                 printf("First block: %lu\nLast block: %lu\n",
129                        get_bmap(fd, 0), get_bmap(fd, numblocks - 1));
130         }
131         for (i=0; i < numblocks; i++) {
132                 if (is_ext2 && last_block) {
133                         if (((i-EXT2_DIRECT) % bpib) == 0)
134                                 last_block++;
135                         if (((i-EXT2_DIRECT-bpib) % (bpib*bpib)) == 0)
136                                 last_block++;
137                         if (((i-EXT2_DIRECT-bpib-bpib*bpib) % (bpib*bpib*bpib)) == 0)
138                                 last_block++;
139                 }
140                 block = get_bmap(fd, i);
141                 if (block == 0)
142                         continue;
143                 if (last_block && (block != last_block +1) ) {
144                         if (verbose)
145                                 printf("Discontinuity: Block %ld is at %lu (was %lu)\n",
146                                        i, block, last_block);
147                         discont++;
148                 }
149                 last_block = block;
150         }
151         if (discont==0)
152                 printf("%s: 1 extent found", filename);
153         else
154                 printf("%s: %d extents found", filename, discont+1);
155         expected = (numblocks/((bs*8)-(fsinfo.f_files/8/cylgroups)-3))+1;
156         if (is_ext2 && expected != discont+1)
157                 printf(", perfection would be %d extent%s\n", expected,
158                         (expected>1) ? "s" : "");
159         else
160                 fputc('\n', stdout);
161         close(fd);
162 }
163
164 static void usage(const char *progname)
165 {
166         fprintf(stderr, "Usage: %s [-v] file ...\n", progname);
167         exit(1);
168 }
169
170 int main(int argc, char**argv)
171 {
172         char **cpp;
173         int c;
174
175         while ((c = getopt(argc, argv, "v")) != EOF)
176                 switch (c) {
177                 case 'v':
178                         verbose++;
179                         break;
180                 default:
181                         usage(argv[0]);
182                         break;
183                 }
184         if (optind == argc)
185                 usage(argv[0]);
186         for (cpp=argv+optind; *cpp; cpp++) {
187                 if (verbose)
188                         printf("Checking %s\n", *cpp);
189                 frag_report(*cpp);
190         }
191         return 0;
192 }
193 #endif