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