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