Whamcloud - gitweb
misc: cleanup gcc -Wall warnings
[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 #include "config.h"
13 #ifndef __linux__
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17
18 int main(void) {
19         fputs("This program is only supported on Linux!\n", stderr);
20         exit(EXIT_FAILURE);
21 }
22 #else
23 #ifndef _LARGEFILE_SOURCE
24 #define _LARGEFILE_SOURCE
25 #endif
26 #ifndef _LARGEFILE64_SOURCE
27 #define _LARGEFILE64_SOURCE
28 #endif
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #else
41 extern char *optarg;
42 extern int optind;
43 #endif
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/vfs.h>
47 #include <sys/ioctl.h>
48 #include <linux/fd.h>
49 #include <ext2fs/ext2fs.h>
50 #include <ext2fs/ext2_types.h>
51 #include <ext2fs/fiemap.h>
52
53 int verbose = 0;
54 int blocksize;          /* Use specified blocksize (default 1kB) */
55 int sync_file = 0;      /* fsync file before getting the mapping */
56 int xattr_map = 0;      /* get xattr mapping */
57 int force_bmap; /* force use of FIBMAP instead of FIEMAP */
58 int force_extent;       /* print output in extent format always */
59 int logical_width = 8;
60 int physical_width = 10;
61 const char *ext_fmt = "%4d: %*llu..%*llu: %*llu..%*llu: %6llu: %s\n";
62 const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
63
64 #define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
65
66 #define FIBMAP          _IO(0x00, 1)    /* bmap access */
67 #define FIGETBSZ        _IO(0x00, 2)    /* get the block size used for bmap */
68
69 #define LUSTRE_SUPER_MAGIC 0x0BD00BD0
70
71 #define EXT4_EXTENTS_FL                 0x00080000 /* Inode uses extents */
72 #define EXT3_IOC_GETFLAGS               _IOR('f', 1, long)
73
74 static int int_log2(int arg)
75 {
76         int     l = 0;
77
78         arg >>= 1;
79         while (arg) {
80                 l++;
81                 arg >>= 1;
82         }
83         return l;
84 }
85
86 static int int_log10(unsigned long long arg)
87 {
88         int     l = 0;
89
90         arg = arg / 10;
91         while (arg) {
92                 l++;
93                 arg = arg / 10;
94         }
95         return l;
96 }
97
98 static unsigned int div_ceil(unsigned int a, unsigned int b)
99 {
100         if (!a)
101                 return 0;
102         return ((a - 1) / b) + 1;
103 }
104
105 static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
106 {
107         int     ret;
108         unsigned int b;
109
110         b = block;
111         ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
112         if (ret < 0)
113                 return -errno;
114         *phy_blk = b;
115
116         return ret;
117 }
118
119 static void print_extent_header(void)
120 {
121         printf(" ext: %*s %*s length: %*s flags:\n",
122                logical_width * 2 + 3,
123                "logical_offset:",
124                physical_width * 2 + 3, "physical_offset:",
125                physical_width + 1,
126                "expected:");
127 }
128
129 static void print_flag(__u32 *flags, __u32 mask, char *buf, const char *name)
130 {
131         if ((*flags & mask) == 0)
132                 return;
133
134         strcat(buf, name);
135         *flags &= ~mask;
136 }
137
138 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
139                               unsigned long long expected, int blk_shift,
140                               ext2fs_struct_stat *st)
141 {
142         unsigned long long physical_blk;
143         unsigned long long logical_blk;
144         unsigned long long ext_len;
145         unsigned long long ext_blks;
146         __u32 fe_flags, mask;
147         char flags[256] = "";
148
149         /* For inline data all offsets should be in bytes, not blocks */
150         if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
151                 blk_shift = 0;
152
153         ext_len = fm_extent->fe_length >> blk_shift;
154         ext_blks = (fm_extent->fe_length - 1) >> blk_shift;
155         logical_blk = fm_extent->fe_logical >> blk_shift;
156         if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) {
157                 physical_blk = 0;
158         } else {
159                 physical_blk = fm_extent->fe_physical >> blk_shift;
160         }
161
162         if (expected)
163                 sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
164                         physical_width, expected >> blk_shift);
165         else
166                 sprintf(flags, "%.*s  ", physical_width, "                   ");
167
168         fe_flags = fm_extent->fe_flags;
169         print_flag(&fe_flags, FIEMAP_EXTENT_LAST, flags, "last,");
170         print_flag(&fe_flags, FIEMAP_EXTENT_UNKNOWN, flags, "unknown_loc,");
171         print_flag(&fe_flags, FIEMAP_EXTENT_DELALLOC, flags, "delalloc,");
172         print_flag(&fe_flags, FIEMAP_EXTENT_ENCODED, flags, "encoded,");
173         print_flag(&fe_flags, FIEMAP_EXTENT_DATA_ENCRYPTED, flags,"encrypted,");
174         print_flag(&fe_flags, FIEMAP_EXTENT_NOT_ALIGNED, flags, "not_aligned,");
175         print_flag(&fe_flags, FIEMAP_EXTENT_DATA_INLINE, flags, "inline,");
176         print_flag(&fe_flags, FIEMAP_EXTENT_DATA_TAIL, flags, "tail_packed,");
177         print_flag(&fe_flags, FIEMAP_EXTENT_UNWRITTEN, flags, "unwritten,");
178         print_flag(&fe_flags, FIEMAP_EXTENT_MERGED, flags, "merged,");
179         print_flag(&fe_flags, FIEMAP_EXTENT_SHARED, flags, "shared,");
180         /* print any unknown flags as hex values */
181         for (mask = 1; fe_flags != 0 && mask != 0; mask <<= 1) {
182                 char hex[6];
183
184                 if ((fe_flags & mask) == 0)
185                         continue;
186                 sprintf(hex, "%#04x,", mask);
187                 print_flag(&fe_flags, mask, flags, hex);
188         }
189
190         if (fm_extent->fe_logical + fm_extent->fe_length >=
191             (unsigned long long) st->st_size)
192                 strcat(flags, "eof,");
193
194         /* Remove trailing comma, if any */
195         if (flags[0] != '\0')
196                 flags[strnlen(flags, sizeof(flags)) - 1] = '\0';
197
198         printf(ext_fmt, cur_ex, logical_width, logical_blk,
199                logical_width, logical_blk + ext_blks,
200                physical_width, physical_blk,
201                physical_width, physical_blk + ext_blks,
202                ext_len, flags);
203 }
204
205 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
206                            ext2fs_struct_stat *st)
207 {
208         __u64 buf[2048];        /* __u64 for proper field alignment */
209         struct fiemap *fiemap = (struct fiemap *)buf;
210         struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
211         int count = (sizeof(buf) - sizeof(*fiemap)) /
212                         sizeof(struct fiemap_extent);
213         unsigned long long expected = 0;
214         unsigned long flags = 0;
215         unsigned int i;
216         int fiemap_header_printed = 0;
217         int tot_extents = 0, n = 0;
218         int last = 0;
219         int rc;
220
221         memset(fiemap, 0, sizeof(struct fiemap));
222
223         if (sync_file)
224                 flags |= FIEMAP_FLAG_SYNC;
225
226         if (xattr_map)
227                 flags |= FIEMAP_FLAG_XATTR;
228
229         do {
230                 fiemap->fm_length = ~0ULL;
231                 fiemap->fm_flags = flags;
232                 fiemap->fm_extent_count = count;
233                 rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
234                 if (rc < 0) {
235                         static int fiemap_incompat_printed;
236
237                         rc = -errno;
238                         if (rc == -EBADR && !fiemap_incompat_printed) {
239                                 fprintf(stderr, "FIEMAP failed with unknown "
240                                                 "flags %x\n",
241                                        fiemap->fm_flags);
242                                 fiemap_incompat_printed = 1;
243                         }
244                         return rc;
245                 }
246
247                 /* If 0 extents are returned, then more ioctls are not needed */
248                 if (fiemap->fm_mapped_extents == 0)
249                         break;
250
251                 if (verbose && !fiemap_header_printed) {
252                         print_extent_header();
253                         fiemap_header_printed = 1;
254                 }
255
256                 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
257                         if (fm_ext[i].fe_logical != 0 &&
258                             fm_ext[i].fe_physical != expected) {
259                                 tot_extents++;
260                         } else {
261                                 expected = 0;
262                                 if (!tot_extents)
263                                         tot_extents = 1;
264                         }
265                         if (verbose)
266                                 print_extent_info(&fm_ext[i], n, expected,
267                                                   blk_shift, st);
268
269                         expected = fm_ext[i].fe_physical + fm_ext[i].fe_length;
270                         if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
271                                 last = 1;
272                         n++;
273                 }
274
275                 fiemap->fm_start = (fm_ext[i - 1].fe_logical +
276                                     fm_ext[i - 1].fe_length);
277         } while (last == 0);
278
279         *num_extents = tot_extents;
280
281         return 0;
282 }
283
284 #define EXT2_DIRECT     12
285
286 static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
287                            ext2fs_struct_stat *st,
288                            unsigned long numblocks, int is_ext2)
289 {
290         struct fiemap_extent    fm_ext;
291         unsigned long           i, last_block;
292         unsigned long long      logical;
293                                 /* Blocks per indirect block */
294         const long              bpib = st->st_blksize / 4;
295         int                     count;
296
297         memset(&fm_ext, 0, sizeof(fm_ext));
298         if (force_extent) {
299                 fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
300         }
301
302         if (sync_file)
303                 fsync(fd);
304
305         for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
306              i < numblocks;
307              i++, logical += st->st_blksize) {
308                 unsigned long block = 0;
309                 int rc;
310
311                 if (is_ext2 && last_block) {
312                         if (((i - EXT2_DIRECT) % bpib) == 0)
313                                 last_block++;
314                         if (((i - EXT2_DIRECT - bpib) % (bpib * bpib)) == 0)
315                                 last_block++;
316                         if (((i - EXT2_DIRECT - bpib - bpib * bpib) %
317                              (((unsigned long long)bpib) * bpib * bpib)) == 0)
318                                 last_block++;
319                 }
320                 rc = get_bmap(fd, i, &block);
321                 if (rc < 0)
322                         return rc;
323                 if (block == 0)
324                         continue;
325                 if (*num_extents == 0) {
326                         (*num_extents)++;
327                         if (force_extent) {
328                                 print_extent_header();
329                                 fm_ext.fe_physical = block * st->st_blksize;
330                         }
331                 }
332                 count++;
333                 if (force_extent && last_block != 0 &&
334                     (block != last_block + 1 ||
335                      fm_ext.fe_logical + fm_ext.fe_length != logical)) {
336                         print_extent_info(&fm_ext, *num_extents - 1,
337                                           (last_block + 1) * st->st_blksize,
338                                           blk_shift, st);
339                         fm_ext.fe_length = 0;
340                         (*num_extents)++;
341                         fm_ext.fe_logical = logical;
342                         fm_ext.fe_physical = block * st->st_blksize;
343                 } else if (last_block && (block != last_block + 1)) {
344                         if (verbose)
345                                 printf("Discontinuity: Block %ld is at %lu (was "
346                                        "%lu)\n", i, block, last_block + 1);
347                         fm_ext.fe_length = 0;
348                         (*num_extents)++;
349                         fm_ext.fe_logical = logical;
350                         fm_ext.fe_physical = block * st->st_blksize;
351                 }
352                 fm_ext.fe_length += st->st_blksize;
353                 last_block = block;
354         }
355
356         if (force_extent)
357                 print_extent_info(&fm_ext, *num_extents - 1,
358                                   last_block * st->st_blksize, blk_shift, st);
359
360         return count;
361 }
362
363 static int frag_report(const char *filename)
364 {
365         static struct statfs fsinfo;
366         static unsigned int blksize;
367         ext2fs_struct_stat st;
368         int             blk_shift;
369         long            fd;
370         unsigned long long      numblocks;
371         int             data_blocks_per_cyl = 1;
372         int             num_extents = 1, expected = ~0;
373         int             is_ext2 = 0;
374         static dev_t    last_device;
375         int             width;
376         int             rc = 0;
377
378 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
379         fd = open64(filename, O_RDONLY);
380 #else
381         fd = open(filename, O_RDONLY);
382 #endif
383         if (fd < 0) {
384                 rc = -errno;
385                 perror("open");
386                 return rc;
387         }
388
389 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
390         if (fstat64(fd, &st) < 0) {
391 #else
392         if (fstat(fd, &st) < 0) {
393 #endif
394                 rc = -errno;
395                 perror("stat");
396                 goto out_close;
397         }
398
399         if (last_device != st.st_dev) {
400                 if (fstatfs(fd, &fsinfo) < 0) {
401                         rc = -errno;
402                         perror("fstatfs");
403                         goto out_close;
404                 }
405                 if (ioctl(fd, FIGETBSZ, &blksize) < 0)
406                         blksize = fsinfo.f_bsize;
407                 if (verbose)
408                         printf("Filesystem type is: %lx\n",
409                                (unsigned long)fsinfo.f_type);
410         }
411         st.st_blksize = blksize;
412         if (fsinfo.f_type == 0xef51 || fsinfo.f_type == 0xef52 ||
413             fsinfo.f_type == 0xef53) {
414                 unsigned int    flags;
415
416                 if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) == 0 &&
417                     !(flags & EXT4_EXTENTS_FL))
418                         is_ext2 = 1;
419         }
420
421         if (is_ext2) {
422                 long cylgroups = div_ceil(fsinfo.f_blocks, blksize * 8);
423
424                 if (verbose && last_device != st.st_dev)
425                         printf("Filesystem cylinder groups approximately %ld\n",
426                                cylgroups);
427
428                 data_blocks_per_cyl = blksize * 8 -
429                                         (fsinfo.f_files / 8 / cylgroups) - 3;
430         }
431         last_device = st.st_dev;
432
433         width = int_log10(fsinfo.f_blocks);
434         if (width > physical_width)
435                 physical_width = width;
436
437         numblocks = (st.st_size + blksize - 1) / blksize;
438         if (blocksize != 0)
439                 blk_shift = int_log2(blocksize);
440         else
441                 blk_shift = int_log2(blksize);
442
443         width = int_log10(numblocks);
444         if (width > logical_width)
445                 logical_width = width;
446         if (verbose)
447                 printf("File size of %s is %llu (%llu block%s of %d bytes)\n",
448                        filename, (unsigned long long)st.st_size,
449                        numblocks * blksize >> blk_shift,
450                        numblocks == 1 ? "" : "s", 1 << blk_shift);
451
452         if (!force_bmap) {
453                 rc = filefrag_fiemap(fd, blk_shift, &num_extents, &st);
454                 expected = 0;
455         }
456
457         if (force_bmap || rc < 0) { /* FIEMAP failed, try FIBMAP instead */
458                 expected = filefrag_fibmap(fd, blk_shift, &num_extents,
459                                            &st, numblocks, is_ext2);
460                 if (expected < 0) {
461                         if (expected == -EINVAL || expected == -ENOTTY) {
462                                 fprintf(stderr, "%s: FIBMAP unsupported\n",
463                                         filename);
464                         } else if (expected == -EPERM) {
465                                 fprintf(stderr,
466                                         "%s: FIBMAP requires root privileges\n",
467                                         filename);
468                         } else {
469                                 fprintf(stderr, "%s: FIBMAP error: %s",
470                                         filename, strerror(expected));
471                         }
472                         rc = expected;
473                         goto out_close;
474                 } else {
475                         rc = 0;
476                 }
477                 expected = expected / data_blocks_per_cyl + 1;
478         }
479
480         if (num_extents == 1)
481                 printf("%s: 1 extent found", filename);
482         else
483                 printf("%s: %d extents found", filename, num_extents);
484         /* count, and thus expected, only set for indirect FIBMAP'd files */
485         if (is_ext2 && expected && expected < num_extents)
486                 printf(", perfection would be %d extent%s\n", expected,
487                         (expected > 1) ? "s" : "");
488         else
489                 fputc('\n', stdout);
490 out_close:
491         close(fd);
492
493         return rc;
494 }
495
496 static void usage(const char *progname)
497 {
498         fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeklsvxX] file ...\n",
499                 progname);
500         exit(1);
501 }
502
503 int main(int argc, char**argv)
504 {
505         char **cpp;
506         int rc = 0, c;
507
508         while ((c = getopt(argc, argv, "Bb::eksvxX")) != EOF) {
509                 switch (c) {
510                 case 'B':
511                         force_bmap++;
512                         break;
513                 case 'b':
514                         if (optarg) {
515                                 char *end;
516                                 blocksize = strtoul(optarg, &end, 0);
517                                 if (end) {
518                                         switch (end[0]) {
519                                         case 'g':
520                                         case 'G':
521                                                 blocksize *= 1024;
522                                                 /* no break */
523                                         case 'm':
524                                         case 'M':
525                                                 blocksize *= 1024;
526                                                 /* no break */
527                                         case 'k':
528                                         case 'K':
529                                                 blocksize *= 1024;
530                                                 break;
531                                         default:
532                                                 break;
533                                         }
534                                 }
535                         } else { /* Allow -b without argument for compat. Remove
536                                   * this eventually so "-b {blocksize}" works */
537                                 fprintf(stderr, "%s: -b needs a blocksize "
538                                         "option, assuming 1024-byte blocks.\n",
539                                         argv[0]);
540                                 blocksize = 1024;
541                         }
542                         break;
543                 case 'e':
544                         force_extent++;
545                         if (!verbose)
546                                 verbose++;
547                         break;
548                 case 'k':
549                         blocksize = 1024;
550                         break;
551                 case 's':
552                         sync_file++;
553                         break;
554                 case 'v':
555                         verbose++;
556                         break;
557                 case 'x':
558                         xattr_map++;
559                         break;
560                 case 'X':
561                         ext_fmt = hex_fmt;
562                         break;
563                 default:
564                         usage(argv[0]);
565                         break;
566                 }
567         }
568
569         if (optind == argc)
570                 usage(argv[0]);
571
572         for (cpp = argv + optind; *cpp != '\0'; cpp++) {
573                 int rc2 = frag_report(*cpp);
574
575                 if (rc2 < 0 && rc == 0)
576                         rc = rc2;
577         }
578
579         return -rc;
580 }
581 #endif