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