Whamcloud - gitweb
filefrag: Restore "perfect" number of extents calculation for ext2/ext3
[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 #include <unistd.h>
16
17 int main(void) {
18     fputs("This program is only supported on Linux!\n", stderr);
19     exit(EXIT_FAILURE);
20 }
21 #else
22 #define _LARGEFILE64_SOURCE
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #ifdef HAVE_GETOPT_H
32 #include <getopt.h>
33 #else
34 extern char *optarg;
35 extern int optind;
36 #endif
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/vfs.h>
40 #include <sys/ioctl.h>
41 #include <linux/fd.h>
42 #include <ext2fs/ext2_types.h>
43 #include <ext2fs/fiemap.h>
44
45 int verbose = 0;
46 int no_bs = 0;          /* Don't use the files blocksize, use 1K blocksize */
47 int sync_file = 0;      /* fsync file before getting the mapping */
48 int xattr_map = 0;      /* get xattr mapping */
49 int logical_width = 12;
50 int physical_width = 14;
51 unsigned long long filesize;
52
53 #define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
54
55 #define FIBMAP          _IO(0x00, 1)    /* bmap access */
56 #define FIGETBSZ        _IO(0x00, 2)    /* get the block size used for bmap */
57 #define FS_IOC_FIEMAP   _IOWR('f', 11, struct fiemap)
58
59 #define EXT4_EXTENTS_FL                 0x00080000 /* Inode uses extents */
60 #define EXT3_IOC_GETFLAGS               _IOR('f', 1, long)
61
62 static int int_log2(int arg)
63 {
64         int     l = 0;
65
66         arg >>= 1;
67         while (arg) {
68                 l++;
69                 arg >>= 1;
70         }
71         return l;
72 }
73
74 static int int_log10(unsigned long long arg)
75 {
76         int     l = 0;
77
78         arg = arg / 10;
79         while (arg) {
80                 l++;
81                 arg = arg / 10;
82         }
83         return l;
84 }
85
86 static unsigned int div_ceil(unsigned int a, unsigned int b)
87 {
88         if (!a)
89                 return 0;
90         return ((a - 1) / b) + 1;
91 }
92
93 static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
94 {
95         int     ret;
96         unsigned int b;
97
98         printf("Calling get_bmap for block %lu\n", block);
99         abort();
100         b = block;
101         ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
102         if (ret < 0) {
103                 if (errno == EPERM) {
104                         fprintf(stderr, "No permission to use FIBMAP ioctl; "
105                                 "must have root privileges\n");
106                         exit(1);
107                 }
108                 perror("FIBMAP");
109         }
110         *phy_blk = b;
111
112         return ret;
113 }
114
115 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
116                               unsigned long long expected, int blk_shift)
117 {
118         __u64 phy_blk;
119         unsigned long long logical_blk;
120         unsigned long ext_len;
121         char flags[256] = "";
122
123         /* For inline data all offsets should be in terms of bytes, not blocks */
124         if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
125                 blk_shift = 0;
126
127         ext_len = fm_extent->fe_length >> blk_shift;
128         logical_blk = fm_extent->fe_logical >> blk_shift;
129         phy_blk = fm_extent->fe_physical >> blk_shift;
130
131         if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN)
132                 strcat(flags, "unknown,");
133         if (fm_extent->fe_flags & FIEMAP_EXTENT_DELALLOC)
134                 strcat(flags, "delalloc,");
135         if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_ENCRYPTED)
136                 strcat(flags, "encrypted,");
137         if (fm_extent->fe_flags & FIEMAP_EXTENT_NOT_ALIGNED)
138                 strcat(flags, "not_aligned,");
139         if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
140                 strcat(flags, "inline,");
141         if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_TAIL)
142                 strcat(flags, "tail_packed,");
143         if (fm_extent->fe_flags & FIEMAP_EXTENT_UNWRITTEN)
144                 strcat(flags, "unwritten,");
145         if (fm_extent->fe_flags & FIEMAP_EXTENT_MERGED)
146                 strcat(flags, "merged,");
147
148         if (fm_extent->fe_logical + fm_extent->fe_length >= filesize)
149                 strcat(flags, "eof,");
150
151         /* Remove trailing comma, if any */
152         if (flags[0])
153                 flags[strlen(flags) - 1] = '\0';
154
155         if (expected)
156                 printf("%4d %*llu %*llu %*llu %6lu %s\n",
157                        cur_ex, logical_width, logical_blk,
158                        physical_width, phy_blk, physical_width, expected,
159                        ext_len, flags);
160         else
161                 printf("%4d %*llu %*llu %*s %6lu %s\n",
162                        cur_ex, logical_width, logical_blk,
163                        physical_width, phy_blk, physical_width, "",
164                        ext_len, flags);
165 }
166
167 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents)
168 {
169         char buf[4096] = "";
170         struct fiemap *fiemap = (struct fiemap *)buf;
171         struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
172         int count = (sizeof(buf) - sizeof(*fiemap)) /
173                         sizeof(struct fiemap_extent);
174         unsigned long long last_blk = 0;
175         unsigned long flags = 0;
176         unsigned int i;
177         static int fiemap_incompat_printed;
178         int tot_extents = 1, n = 0;
179         int last = 0;
180         int rc;
181
182         fiemap->fm_length = ~0ULL;
183
184         memset(fiemap, 0, sizeof(struct fiemap));
185
186         if (!verbose)
187                 count = 0;
188
189         if (sync_file)
190                 flags |= FIEMAP_FLAG_SYNC;
191
192         if (xattr_map)
193                 flags |= FIEMAP_FLAG_XATTR;
194
195         if (verbose)
196                 printf(" ext %*s %*s %*s length flags\n", logical_width,
197                        "logical", physical_width, "physical",
198                        physical_width, "expected");
199
200         do {
201                 fiemap->fm_length = ~0ULL;
202                 fiemap->fm_flags = flags;
203                 fiemap->fm_extent_count = count;
204                 rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
205                 if (rc < 0) {
206                         if (errno == EBADR && fiemap_incompat_printed == 0) {
207                                 printf("FIEMAP failed with unsupported "
208                                        "flags %x\n", fiemap->fm_flags);
209                                 fiemap_incompat_printed = 1;
210                         }
211                         return rc;
212                 }
213
214                 if (!verbose) {
215                         *num_extents = fiemap->fm_mapped_extents;
216                         goto out;
217                 }
218
219                 /* If 0 extents are returned, then more ioctls are not needed */
220                 if (fiemap->fm_mapped_extents == 0)
221                         break;
222
223                 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
224                         __u64 phy_blk, logical_blk;
225                         unsigned long ext_len;
226
227                         phy_blk = fm_ext[i].fe_physical >> blk_shift;
228                         ext_len = fm_ext[i].fe_length >> blk_shift;
229                         logical_blk = fm_ext[i].fe_logical >> blk_shift;
230
231                         if (logical_blk && phy_blk != last_blk + 1)
232                                 tot_extents++;
233                         else
234                                 last_blk = 0;
235                         print_extent_info(&fm_ext[i], n, last_blk, blk_shift);
236
237                         last_blk = phy_blk + ext_len - 1;
238                         if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
239                                 last = 1;
240                         n++;
241                 }
242
243                 fiemap->fm_start = (fm_ext[i-1].fe_logical +
244                                     fm_ext[i-1].fe_length);
245         } while (last == 0);
246
247         *num_extents = tot_extents;
248 out:
249         return 0;
250 }
251
252 #define EXT2_DIRECT     12
253
254 static void frag_report(const char *filename)
255 {
256         struct statfs   fsinfo;
257 #ifdef HAVE_FSTAT64
258         struct stat64   fileinfo;
259 #else
260         struct stat     fileinfo;
261 #endif
262         int             bs;
263         long            fd;
264         unsigned long   block, last_block = 0, numblocks, i, count;
265         long            bpib;   /* Blocks per indirect block */
266         long            cylgroups;
267         int             num_extents = 0, expected;
268         int             is_ext2 = 0;
269         static int      once = 1;
270         unsigned int    flags;
271         int rc;
272
273 #ifdef HAVE_OPEN64
274         fd = open64(filename, O_RDONLY);
275 #else
276         fd = open(filename, O_RDONLY);
277 #endif
278         if (fd < 0) {
279                 perror("open");
280                 return;
281         }
282
283         if (statfs(filename, &fsinfo) < 0) {
284                 perror("statfs");
285                 return;
286         }
287 #ifdef HAVE_FSTAT64
288         if (stat64(filename, &fileinfo) < 0) {
289 #else
290         if (stat(filename, &fileinfo) < 0) {
291 #endif
292                 perror("stat");
293                 return;
294         }
295         if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) < 0)
296                 flags = 0;
297         if (!(flags & EXT4_EXTENTS_FL) &&
298             ((fsinfo.f_type == 0xef51) || (fsinfo.f_type == 0xef52) ||
299              (fsinfo.f_type == 0xef53)))
300                 is_ext2++;
301         if (verbose && once)
302                 printf("Filesystem type is: %lx\n",
303                        (unsigned long) fsinfo.f_type);
304
305         cylgroups = div_ceil(fsinfo.f_blocks, fsinfo.f_bsize*8);
306         if (verbose && is_ext2 && once)
307                 printf("Filesystem cylinder groups is approximately %ld\n",
308                        cylgroups);
309
310         physical_width = int_log10(fsinfo.f_blocks);
311         if (physical_width < 8)
312                 physical_width = 8;
313
314         if (ioctl(fd, FIGETBSZ, &bs) < 0) { /* FIGETBSZ takes an int */
315                 perror("FIGETBSZ");
316                 close(fd);
317                 return;
318         }
319
320         if (no_bs)
321                 bs = 1024;
322
323         bpib = bs / 4;
324         numblocks = (fileinfo.st_size + (bs-1)) / bs;
325         logical_width = int_log10(numblocks);
326         if (logical_width < 7)
327                 logical_width = 7;
328         filesize = (long long)fileinfo.st_size;
329         if (verbose)
330                 printf("File size of %s is %lld (%ld block%s, blocksize %d)\n",
331                        filename, (long long) fileinfo.st_size, numblocks,
332                        numblocks == 1 ? "" : "s", bs);
333         if (filefrag_fiemap(fd, int_log2(bs), &num_extents) != 0) {
334                 for (i = 0, count = 0; i < numblocks; i++) {
335                         if (is_ext2 && last_block) {
336                                 if (((i-EXT2_DIRECT) % bpib) == 0)
337                                         last_block++;
338                                 if (((i-EXT2_DIRECT-bpib) % (bpib*bpib)) == 0)
339                                         last_block++;
340                                 if (((i-EXT2_DIRECT-bpib-bpib*bpib) %
341                                                         (bpib*bpib*bpib)) == 0)
342                                         last_block++;
343                         }
344                         rc = get_bmap(fd, i, &block);
345                         if (block == 0)
346                                 continue;
347                         count++;
348                         if (last_block && (block != last_block+1) ) {
349                                 if (verbose)
350                                         printf("Discontinuity: Block %ld is at "
351                                                "%lu (was %lu)\n",
352                                                i, block, last_block+1);
353                                 num_extents++;
354                         }
355                         last_block = block;
356                 }
357         }
358         if (num_extents == 1)
359                 printf("%s: 1 extent found", filename);
360         else
361                 printf("%s: %d extents found", filename, num_extents);
362         expected = (count/((bs*8)-(fsinfo.f_files/8/cylgroups)-3))+1;
363         if (is_ext2 && expected < num_extents)
364                 printf(", perfection would be %d extent%s\n", expected,
365                         (expected>1) ? "s" : "");
366         else
367                 fputc('\n', stdout);
368         close(fd);
369         once = 0;
370 }
371
372 static void usage(const char *progname)
373 {
374         fprintf(stderr, "Usage: %s [-bvsx] file ...\n", progname);
375         exit(1);
376 }
377
378 int main(int argc, char**argv)
379 {
380         char **cpp;
381         int c;
382
383         while ((c = getopt(argc, argv, "bsvx")) != EOF)
384                 switch (c) {
385                 case 'b':
386                         no_bs++;
387                         break;
388                 case 'v':
389                         verbose++;
390                         break;
391                 case 's':
392                         sync_file++;
393                         break;
394                 case 'x':
395                         xattr_map++;
396                         break;
397                 default:
398                         usage(argv[0]);
399                         break;
400                 }
401         if (optind == argc)
402                 usage(argv[0]);
403         for (cpp=argv+optind; *cpp; cpp++)
404                 frag_report(*cpp);
405         return 0;
406 }
407 #endif