Whamcloud - gitweb
LU-17117 misc: deduplicate log2/log10 functions
[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 #ifdef HAVE_LINUX_FD_H
49 #include <linux/fd.h>
50 #endif
51 #include <ext2fs/ext2fs.h>
52 #include <ext2fs/ext2_types.h>
53 #include <ext2fs/fiemap.h>
54 #include "../version.h"
55
56 int verbose = 0;
57 unsigned int blocksize; /* Use specified blocksize (default 1kB) */
58 int sync_file = 0;      /* fsync file before getting the mapping */
59 int precache_file = 0;  /* precache the file before getting the mapping */
60 int xattr_map = 0;      /* get xattr mapping */
61 int force_bmap;         /* force use of FIBMAP instead of FIEMAP */
62 int force_extent;       /* print output in extent format always */
63 int use_extent_cache;   /* Use extent cache */
64 int device_offset;      /* extents report device-relative offsets */
65 int logical_width = 8;
66 int physical_width = 10;
67 const char *ext_fmt = "%4d: %*llu..%*llu: %*llu..%*llu: %6llu: %s\n";
68 const char *hex_fmt = "%4d: %*llx..%*llx: %*llx..%*llx: %6llx: %s\n";
69
70 #define FILEFRAG_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR |\
71                                       FIEMAP_FLAG_DEVICE_ORDER)
72
73 #define FIBMAP          _IO(0x00, 1)    /* bmap access */
74 #define FIGETBSZ        _IO(0x00, 2)    /* get the block size used for bmap */
75
76 #define LUSTRE_SUPER_MAGIC 0x0BD00BD0
77
78 #define EXT4_EXTENTS_FL                 0x00080000 /* Inode uses extents */
79 #define EXT3_IOC_GETFLAGS               _IOR('f', 1, long)
80
81 static unsigned int div_ceil(unsigned int a, unsigned int b)
82 {
83         if (!a)
84                 return 0;
85         return ((a - 1) / b) + 1;
86 }
87
88 static int get_bmap(int fd, unsigned long block, unsigned long *phy_blk)
89 {
90         int     ret;
91         unsigned int b;
92
93         b = block;
94         ret = ioctl(fd, FIBMAP, &b); /* FIBMAP takes pointer to integer */
95         if (ret < 0)
96                 return -errno;
97         *phy_blk = b;
98
99         return ret;
100 }
101
102 static void print_extent_header(void)
103 {
104         printf(" ext: %*s %*s length: %*s flags:\n",
105                logical_width * 2 + 3,
106                device_offset ? "device_logical:" : "logical_offset:",
107                physical_width * 2 + 3, "physical_offset:",
108                device_offset ? 5 : physical_width + 1,
109                device_offset ? " dev:" : "expected:");
110 }
111
112 static void print_flag(__u32 *flags, __u32 mask, char *buf, const char *name)
113 {
114         char hex[sizeof(mask) * 2 + 4]; /* 2 chars/byte + 0x, + NUL */
115
116         if ((*flags & mask) == 0)
117                 return;
118
119         if (name == NULL) {
120                 sprintf(hex, "%#04x,", mask);
121                 name = hex;
122         }
123         strcat(buf, name);
124         *flags &= ~mask;
125 }
126
127 static void print_flags(__u32 fe_flags, char *flags, int len, int print_unknown)
128 {
129         __u32 mask;
130
131         print_flag(&fe_flags, FIEMAP_EXTENT_LAST, flags, "last,");
132         print_flag(&fe_flags, FIEMAP_EXTENT_UNKNOWN, flags, "unknown_loc,");
133         print_flag(&fe_flags, FIEMAP_EXTENT_DELALLOC, flags, "delalloc,");
134         print_flag(&fe_flags, FIEMAP_EXTENT_ENCODED, flags, "encoded,");
135         print_flag(&fe_flags, FIEMAP_EXTENT_DATA_ENCRYPTED, flags,"encrypted,");
136         print_flag(&fe_flags, FIEMAP_EXTENT_NOT_ALIGNED, flags, "not_aligned,");
137         print_flag(&fe_flags, FIEMAP_EXTENT_DATA_INLINE, flags, "inline,");
138         print_flag(&fe_flags, FIEMAP_EXTENT_DATA_TAIL, flags, "tail_packed,");
139         print_flag(&fe_flags, FIEMAP_EXTENT_UNWRITTEN, flags, "unwritten,");
140         print_flag(&fe_flags, FIEMAP_EXTENT_MERGED, flags, "merged,");
141         print_flag(&fe_flags, FIEMAP_EXTENT_SHARED, flags, "shared,");
142         print_flag(&fe_flags, EXT4_FIEMAP_EXTENT_HOLE, flags, "hole,");
143
144         print_flag(&fe_flags, FIEMAP_EXTENT_NET, flags, "net,");
145
146         if (!print_unknown)
147                 goto out;
148
149         /* print any unknown flags as hex values */
150         for (mask = 1; fe_flags != 0 && mask != 0; mask <<= 1)
151                 print_flag(&fe_flags, mask, flags, NULL);
152 out:
153         /* Remove trailing comma, if any */
154         if (flags[0])
155                 flags[strnlen(flags, len) - 1] = '\0';
156
157 }
158
159 static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
160                               unsigned long long expected, int blk_shift,
161                               ext2fs_struct_stat *st)
162 {
163         unsigned long long physical_blk;
164         unsigned long long logical_blk;
165         unsigned long long ext_len;
166         unsigned long long ext_blks;
167         unsigned long long ext_blks_phys;
168         char flags[256] = "";
169
170         /* For inline data all offsets should be in bytes, not blocks */
171         if (fm_extent->fe_flags & FIEMAP_EXTENT_DATA_INLINE)
172                 blk_shift = 0;
173
174         ext_len = fm_extent->fe_length >> blk_shift;
175         ext_blks = (fm_extent->fe_length - 1) >> blk_shift;
176         logical_blk = fm_extent->fe_logical >> blk_shift;
177         if (fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) {
178                 physical_blk = 0;
179         } else {
180                 physical_blk = fm_extent->fe_physical >> blk_shift;
181         }
182
183         if (device_offset)
184                 sprintf(flags, "%04x: ", fm_extent->fe_device & 0xffff);
185         else if (expected &&
186             !(fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) &&
187             !(fm_extent->fe_flags & EXT4_FIEMAP_EXTENT_HOLE))
188                 sprintf(flags, ext_fmt == hex_fmt ? "%*llx: " : "%*llu: ",
189                         physical_width, expected >> blk_shift);
190
191         print_flags(fm_extent->fe_flags, flags, sizeof(flags), 1);
192
193         if (fm_extent->fe_logical + fm_extent->fe_length >=
194             (unsigned long long)st->st_size)
195                 strcat(flags, flags[0] ? ",eof" : "eof");
196
197         if ((fm_extent->fe_flags & FIEMAP_EXTENT_UNKNOWN) ||
198             (fm_extent->fe_flags & EXT4_FIEMAP_EXTENT_HOLE)) {
199                 ext_len = 0;
200                 ext_blks_phys = 0;
201         } else
202                 ext_blks_phys = ext_blks;
203
204         printf(ext_fmt, cur_ex, logical_width, logical_blk,
205                logical_width, logical_blk + ext_blks,
206                physical_width, physical_blk,
207                physical_width, physical_blk + ext_blks_phys,
208                ext_len, flags);
209 }
210
211 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
212                            ext2fs_struct_stat *st)
213 {
214         __u64 buf[2048];        /* __u64 for proper field alignment */
215         struct fiemap *fiemap = (struct fiemap *)buf;
216         struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
217         struct fiemap_extent fm_last;
218         int count = (sizeof(buf) - sizeof(*fiemap)) /
219                         sizeof(struct fiemap_extent);
220         unsigned long long expected = 0;
221         unsigned long long expected_dense = 0;
222         unsigned long flags = 0;
223         unsigned int i;
224         unsigned long cmd = FS_IOC_FIEMAP;
225         int fiemap_header_printed = 0;
226         int tot_extents = 0, n = 0;
227         int previous_device = -1;
228         int last = 0;
229         int rc;
230
231         memset(fiemap, 0, sizeof(struct fiemap));
232         memset(&fm_last, 0, sizeof(fm_last));
233
234         if (sync_file)
235                 flags |= FIEMAP_FLAG_SYNC;
236
237         if (precache_file)
238                 flags |= FIEMAP_FLAG_CACHE;
239
240         if (xattr_map)
241                 flags |= FIEMAP_FLAG_XATTR;
242
243         if (use_extent_cache)
244                 cmd = EXT4_IOC_GET_ES_CACHE;
245
246         if (device_offset) {
247                 flags |= FIEMAP_FLAG_DEVICE_ORDER;
248                 memset(fm_ext, 0, sizeof(struct fiemap_extent));
249         }
250
251 retry_wo_device_order:
252         do {
253                 fiemap->fm_length = ~0ULL;
254                 fiemap->fm_flags = flags;
255                 fiemap->fm_extent_count = count;
256                 rc = ioctl(fd, cmd, (unsigned long) fiemap);
257                 if (rc < 0) {
258                         static int fiemap_incompat_printed;
259
260                         rc = -errno;
261                         if (rc == -EBADR && !fiemap_incompat_printed) {
262                                 fprintf(stderr, "FIEMAP failed with unknown "
263                                                 "flags %x\n",
264                                        fiemap->fm_flags);
265                                 fiemap_incompat_printed = 1;
266                         } else if (rc == EBADR && (fiemap->fm_flags &
267                                                    FIEMAP_FLAG_DEVICE_ORDER)) {
268                                 flags &= ~FIEMAP_FLAG_DEVICE_ORDER;
269                                 goto retry_wo_device_order;
270                         }
271                         return rc;
272                 }
273
274                 /* If 0 extents are returned, then more ioctls are not needed */
275                 if (fiemap->fm_mapped_extents == 0)
276                         break;
277
278                 if (verbose && !fiemap_header_printed) {
279                         print_extent_header();
280                         fiemap_header_printed = 1;
281                 }
282
283                 for (i = 0; i < fiemap->fm_mapped_extents; i++) {
284                         expected_dense = fm_last.fe_physical +
285                                          fm_last.fe_length;
286                         expected = fm_last.fe_physical +
287                                    fm_ext[i].fe_logical - fm_last.fe_logical;
288                         if ((fm_ext[i].fe_logical != 0 &&
289                              fm_ext[i].fe_physical != expected &&
290                              fm_ext[i].fe_physical != expected_dense) ||
291                             ((fm_ext[i].fe_device & 0xffff) != previous_device)) {
292                                 tot_extents++;
293                         } else {
294                                 expected = 0;
295                                 if (!tot_extents)
296                                         tot_extents = 1;
297                         }
298                         if (verbose)
299                                 print_extent_info(&fm_ext[i], n, expected,
300                                                   blk_shift, st);
301                         if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
302                                 last = 1;
303                         fm_last = fm_ext[i];
304                         n++;
305                         previous_device = fm_ext[i].fe_device & 0xffff;
306                 }
307
308                 /* For DEVICE_ORDER mappings, if EXTENT_LAST not yet found then
309                  * fm_start needs to be the same as it was for earlier ioctl.
310                  * The first extent is used to pass the end offset and device
311                  * of the last FIEMAP call.  Otherwise, we ask for extents
312                  * starting from where the last mapping ended. */
313                 if (flags & FIEMAP_FLAG_DEVICE_ORDER) {
314                         fm_ext[0].fe_logical =  fm_ext[i - 1].fe_logical +
315                                                 fm_ext[i - 1].fe_length;
316                         fm_ext[0].fe_device =   fm_ext[i - 1].fe_device;
317                         fiemap->fm_start =      0;
318                 } else {
319                         fiemap->fm_start =      fm_ext[i - 1].fe_logical +
320                                                 fm_ext[i - 1].fe_length;
321                 }
322         } while (last == 0);
323
324         *num_extents = tot_extents;
325
326         return 0;
327 }
328
329 #define EXT2_DIRECT     12
330
331 static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
332                            ext2fs_struct_stat *st,
333                            unsigned long numblocks, int is_ext2)
334 {
335         struct fiemap_extent    fm_ext, fm_last;
336         unsigned long           i, last_block;
337         unsigned long long      logical, expected = 0;
338                                 /* Blocks per indirect block */
339         const long              bpib = st->st_blksize / 4;
340         int                     count;
341
342         memset(&fm_ext, 0, sizeof(fm_ext));
343         memset(&fm_last, 0, sizeof(fm_last));
344         if (force_extent) {
345                 fm_ext.fe_device = st->st_dev;
346                 fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
347         }
348
349         if (sync_file && fsync(fd) != 0)
350                 return -errno;
351
352         for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
353              i < numblocks;
354              i++, logical += st->st_blksize) {
355                 unsigned long block = 0;
356                 int rc;
357
358                 if (is_ext2 && last_block) {
359                         if (((i - EXT2_DIRECT) % bpib) == 0)
360                                 last_block++;
361                         if (((i - EXT2_DIRECT - bpib) % (bpib * bpib)) == 0)
362                                 last_block++;
363                         if (((i - EXT2_DIRECT - bpib - bpib * bpib) %
364                              (((unsigned long long)bpib) * bpib * bpib)) == 0)
365                                 last_block++;
366                 }
367                 rc = get_bmap(fd, i, &block);
368                 if (rc < 0)
369                         return rc;
370                 if (block == 0)
371                         continue;
372
373                 if (*num_extents == 0 || block != last_block + 1 ||
374                     fm_ext.fe_logical + fm_ext.fe_length != logical) {
375                         /*
376                          * This is the start of a new extent; figure out where
377                          * we expected it to be and report the extent.
378                          */
379                         if (*num_extents != 0 && fm_last.fe_length) {
380                                 expected = fm_last.fe_physical +
381                                         (fm_ext.fe_logical - fm_last.fe_logical);
382                                 if (expected == fm_ext.fe_physical)
383                                         expected = 0;
384                         }
385                         if (force_extent && *num_extents == 0)
386                                 print_extent_header();
387                         if (force_extent && *num_extents != 0) {
388                                 print_extent_info(&fm_ext, *num_extents - 1,
389                                                   expected, blk_shift, st);
390                         }
391                         if (verbose && expected != 0) {
392                                 printf("Discontinuity: Block %llu is at %llu "
393                                        "(was %llu)\n",
394                                        (unsigned long long) (fm_ext.fe_logical / st->st_blksize),
395                                        (unsigned long long) (fm_ext.fe_physical / st->st_blksize),
396                                        (unsigned long long) (expected / st->st_blksize));
397                         }
398                         /* create the new extent */
399                         fm_last = fm_ext;
400                         (*num_extents)++;
401                         fm_ext.fe_physical = block * st->st_blksize;
402                         fm_ext.fe_logical = logical;
403                         fm_ext.fe_length = 0;
404                 }
405                 fm_ext.fe_length += st->st_blksize;
406                 last_block = block;
407         }
408         if (force_extent && *num_extents != 0) {
409                 if (fm_last.fe_length) {
410                         expected = fm_last.fe_physical +
411                                    (fm_ext.fe_logical - fm_last.fe_logical);
412                         if (expected == fm_ext.fe_physical)
413                                 expected = 0;
414                 }
415                 print_extent_info(&fm_ext, *num_extents - 1, expected,
416                                   blk_shift, st);
417         }
418
419         return count;
420 }
421
422 static int frag_report(const char *filename)
423 {
424         static struct statfs fsinfo;
425         static unsigned int blksize;
426         ext2fs_struct_stat st;
427         int             blk_shift;
428         long            fd;
429         unsigned long long      numblocks;
430         int             data_blocks_per_cyl = 1;
431         int             num_extents = 1, expected = ~0;
432         int             is_ext2 = 0;
433         static dev_t    last_device;
434         int             width;
435         int             rc = 0;
436
437 #if defined(HAVE_OPEN64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
438         fd = open64(filename, O_RDONLY);
439 #else
440         fd = open(filename, O_RDONLY);
441 #endif
442         if (fd < 0) {
443                 rc = -errno;
444                 perror("open");
445                 return rc;
446         }
447
448 #if defined(HAVE_FSTAT64) && !defined(__OSX_AVAILABLE_BUT_DEPRECATED)
449         if (fstat64(fd, &st) < 0) {
450 #else
451         if (fstat(fd, &st) < 0) {
452 #endif
453                 rc = -errno;
454                 perror("stat");
455                 goto out_close;
456         }
457
458         if ((last_device != st.st_dev) || !st.st_dev) {
459                 if (fstatfs(fd, &fsinfo) < 0) {
460                         rc = -errno;
461                         perror("fstatfs");
462                         goto out_close;
463                 }
464                 if ((ioctl(fd, FIGETBSZ, &blksize) < 0) || !blksize)
465                         blksize = fsinfo.f_bsize;
466                 if (verbose)
467                         printf("Filesystem type is: %lx\n",
468                                (unsigned long)fsinfo.f_type);
469         }
470         st.st_blksize = blksize;
471         if (fsinfo.f_type == 0xef51 || fsinfo.f_type == 0xef52 ||
472             fsinfo.f_type == 0xef53) {
473                 unsigned int    flags;
474
475                 if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) == 0 &&
476                     !(flags & EXT4_EXTENTS_FL))
477                         is_ext2 = 1;
478         }
479
480         /* Check if filesystem is Lustre.  Always print in extent format
481          * with 1kB blocks, using the device-relative logical offsets. */
482         if (fsinfo.f_type == LUSTRE_SUPER_MAGIC) {
483                 device_offset = 1;
484                 blocksize = blocksize ?: 1024;
485         }
486
487         if (is_ext2) {
488                 long cylgroups = div_ceil(fsinfo.f_blocks, blksize * 8);
489
490                 if (verbose && last_device != st.st_dev)
491                         printf("Filesystem cylinder groups approximately %ld\n",
492                                cylgroups);
493
494                 data_blocks_per_cyl = blksize * 8 -
495                                         (fsinfo.f_files / 8 / cylgroups) - 3;
496         }
497         last_device = st.st_dev;
498
499         width = ext2fs_log10(fsinfo.f_blocks);
500         if (width > physical_width)
501                 physical_width = width;
502
503         numblocks = (st.st_size + blksize - 1) / blksize;
504         if (blocksize != 0)
505                 blk_shift = ext2fs_log2(blocksize);
506         else
507                 blk_shift = ext2fs_log2(blksize);
508
509         if (use_extent_cache)
510                 width = 10;
511         else
512                 width = ext2fs_log10(numblocks);
513         if (width > logical_width)
514                 logical_width = width;
515         if (verbose) {
516                 __u32 state;
517
518                 printf("File size of %s is %llu (%llu block%s of %d bytes)",
519                        filename, (unsigned long long) st.st_size,
520                        (unsigned long long) (numblocks * blksize >> blk_shift),
521                        numblocks == 1 ? "" : "s", 1 << blk_shift);
522                 if (use_extent_cache &&
523                     ioctl(fd, EXT4_IOC_GETSTATE, &state) == 0 &&
524                     (state & EXT4_STATE_FLAG_EXT_PRECACHED))
525                         fputs(" -- pre-cached", stdout);
526                 fputc('\n', stdout);
527         }
528
529         if (!force_bmap) {
530                 rc = filefrag_fiemap(fd, blk_shift, &num_extents, &st);
531                 expected = 0;
532                 if (rc < 0 &&
533                     (use_extent_cache || precache_file || xattr_map)) {
534                         if (rc != -EBADR)
535                                 fprintf(stderr, "%s: %s: %s\n ",
536                                         filename,
537                                         use_extent_cache ?
538                                         "EXT4_IOC_GET_ES_CACHE" :
539                                         "FS_IOC_FIEMAP", strerror(-rc));
540                         goto out_close;
541                 }
542         }
543
544         if (force_bmap || rc < 0) { /* FIEMAP failed, try FIBMAP instead */
545                 expected = filefrag_fibmap(fd, blk_shift, &num_extents,
546                                            &st, numblocks, is_ext2);
547                 if (expected < 0) {
548                         if (expected == -EINVAL || expected == -ENOTTY) {
549                                 fprintf(stderr, "%s: FIBMAP%s unsupported\n",
550                                         filename, force_bmap ? "" : "/FIEMAP");
551                         } else if (expected == -EPERM) {
552                                 fprintf(stderr,
553                                         "%s: FIBMAP requires root privileges\n",
554                                         filename);
555                         } else {
556                                 fprintf(stderr, "%s: FIBMAP error: %s",
557                                         filename, strerror(expected));
558                         }
559                         rc = expected;
560                         goto out_close;
561                 } else {
562                         rc = 0;
563                 }
564                 expected = expected / data_blocks_per_cyl + 1;
565         }
566
567         if (num_extents == 1)
568                 printf("%s: 1 extent found", filename);
569         else
570                 printf("%s: %d extents found", filename, num_extents);
571         /* count, and thus expected, only set for indirect FIBMAP'd files */
572         if (is_ext2 && expected && expected < num_extents)
573                 printf(", perfection would be %d extent%s\n", expected,
574                         (expected > 1) ? "s" : "");
575         else
576                 fputc('\n', stdout);
577 out_close:
578         close(fd);
579
580         return rc;
581 }
582
583 static void usage(const char *progname)
584 {
585         fprintf(stderr, "Usage: %s [-b{blocksize}[KMG]] [-BeEksvxX] file ...\n",
586                 progname);
587         exit(1);
588 }
589
590 int main(int argc, char**argv)
591 {
592         char **cpp;
593         int rc = 0, c;
594         int version = 0;
595
596         while ((c = getopt(argc, argv, "Bb::eEkPlsvxXV")) != EOF) {
597                 switch (c) {
598                 case 'B':
599                         force_bmap++;
600                         force_extent = 0;
601                         break;
602                 case 'b':
603                         if (optarg) {
604                                 char *end;
605                                 unsigned long val;
606
607                                 val = strtoul(optarg, &end, 0);
608                                 if (end) {
609 #if __GNUC_PREREQ (7, 0)
610 #pragma GCC diagnostic push
611 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
612 #endif
613                                         switch (end[0]) {
614                                         case 'g':
615                                         case 'G':
616                                                 val *= 1024;
617                                                 /* fall through */
618                                         case 'm':
619                                         case 'M':
620                                                 val *= 1024;
621                                                 /* fall through */
622                                         case 'k':
623                                         case 'K':
624                                                 val *= 1024;
625                                                 break;
626                                         default:
627                                                 break;
628                                         }
629 #if __GNUC_PREREQ (7, 0)
630 #pragma GCC diagnostic pop
631 #endif
632                                 }
633                                 /* Specifying too large a blocksize will just
634                                  * shift all extents down to zero length. Even
635                                  * 1GB is questionable, but caveat emptor. */
636                                 if (val > 1024 * 1024 * 1024) {
637                                         fprintf(stderr,
638                                                 "%s: blocksize %lu over 1GB\n",
639                                                 argv[0], val);
640                                         usage(argv[0]);
641                                 }
642                                 blocksize = val;
643                         } else { /* Allow -b without argument for compat. Remove
644                                   * this eventually so "-b {blocksize}" works */
645                                 fprintf(stderr, "%s: -b needs a blocksize "
646                                         "option, assuming 1024-byte blocks.\n",
647                                         argv[0]);
648                                 blocksize = 1024;
649                         }
650                         break;
651                 case 'E':
652                         use_extent_cache++;
653                         /* fallthrough */
654                 case 'e':
655                         force_extent++;
656                         if (!verbose)
657                                 verbose++;
658                         break;
659                 case 'k':
660                         blocksize = 1024;
661                         break;
662                 case 'P':
663                         precache_file++;
664                         break;
665                 case 'l':
666                         device_offset++;
667                         break;
668                 case 's':
669                         sync_file++;
670                         break;
671                 case 'v':
672                         verbose++;
673                         break;
674                 case 'V':
675                         version++;
676                         break;
677                 case 'x':
678                         xattr_map++;
679                         break;
680                 case 'X':
681                         ext_fmt = hex_fmt;
682                         break;
683                 default:
684                         usage(argv[0]);
685                         break;
686                 }
687         }
688         if (version) {
689                 /* Print version number and exit */
690                 printf("filefrag %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
691                 if (version + verbose > 1) {
692                         char flags[256] = "";
693
694                         print_flags(0xffffffff, flags, sizeof(flags), 0);
695                         printf("supported: %s\n", flags);
696                 }
697                 exit(0);
698         }
699
700         if (optind == argc)
701                 usage(argv[0]);
702
703         for (cpp = argv + optind; *cpp != NULL; cpp++) {
704                 int rc2 = frag_report(*cpp);
705
706                 if (rc2 < 0 && rc == 0)
707                         rc = rc2;
708         }
709
710         return -rc;
711 }
712 #endif