Whamcloud - gitweb
libcom_err: deal with the fact that the Hurd error messages are not zero-based
[tools/e2fsprogs.git] / misc / filefrag.c
index 37c4416..1eec146 100644 (file)
@@ -20,7 +20,13 @@ int main(void) {
        exit(EXIT_FAILURE);
 }
 #else
+#ifndef _LARGEFILE_SOURCE
+#define _LARGEFILE_SOURCE
+#endif
+#ifndef _LARGEFILE64_SOURCE
 #define _LARGEFILE64_SOURCE
+#endif
+
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -39,7 +45,9 @@ extern int optind;
 #include <sys/stat.h>
 #include <sys/vfs.h>
 #include <sys/ioctl.h>
+#ifdef HAVE_LINUX_FD_H
 #include <linux/fd.h>
+#endif
 #include <ext2fs/ext2fs.h>
 #include <ext2fs/ext2_types.h>
 #include <ext2fs/fiemap.h>
@@ -173,15 +181,16 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
        print_flag(&fe_flags, FIEMAP_EXTENT_SHARED, flags, "shared,");
        /* print any unknown flags as hex values */
        for (mask = 1; fe_flags != 0 && mask != 0; mask <<= 1) {
-               char hex[6];
+               char hex[sizeof(mask) * 2 + 4]; /* 2 chars/byte + 0x, + NUL */
 
-               if (fe_flags & mask == 0)
+               if ((fe_flags & mask) == 0)
                        continue;
                sprintf(hex, "%#04x,", mask);
                print_flag(&fe_flags, mask, flags, hex);
        }
 
-       if (fm_extent->fe_logical + fm_extent->fe_length >= st->st_size)
+       if (fm_extent->fe_logical + fm_extent->fe_length >=
+           (unsigned long long) st->st_size)
                strcat(flags, "eof,");
 
        /* Remove trailing comma, if any */
@@ -198,12 +207,14 @@ static void print_extent_info(struct fiemap_extent *fm_extent, int cur_ex,
 static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
                           ext2fs_struct_stat *st)
 {
-       char buf[16384];
+       __u64 buf[2048];        /* __u64 for proper field alignment */
        struct fiemap *fiemap = (struct fiemap *)buf;
        struct fiemap_extent *fm_ext = &fiemap->fm_extents[0];
+       struct fiemap_extent fm_last;
        int count = (sizeof(buf) - sizeof(*fiemap)) /
                        sizeof(struct fiemap_extent);
        unsigned long long expected = 0;
+       unsigned long long expected_dense = 0;
        unsigned long flags = 0;
        unsigned int i;
        int fiemap_header_printed = 0;
@@ -212,6 +223,7 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
        int rc;
 
        memset(fiemap, 0, sizeof(struct fiemap));
+       memset(&fm_last, 0, sizeof(fm_last));
 
        if (sync_file)
                flags |= FIEMAP_FLAG_SYNC;
@@ -226,9 +238,11 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
                rc = ioctl(fd, FS_IOC_FIEMAP, (unsigned long) fiemap);
                if (rc < 0) {
                        static int fiemap_incompat_printed;
+
                        rc = -errno;
                        if (rc == -EBADR && !fiemap_incompat_printed) {
-                               printf("FIEMAP failed with unknown flags %#x\n",
+                               fprintf(stderr, "FIEMAP failed with unknown "
+                                               "flags %x\n",
                                       fiemap->fm_flags);
                                fiemap_incompat_printed = 1;
                        }
@@ -245,8 +259,13 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
                }
 
                for (i = 0; i < fiemap->fm_mapped_extents; i++) {
+                       expected_dense = fm_last.fe_physical +
+                                        fm_last.fe_length;
+                       expected = fm_last.fe_physical +
+                                  fm_ext[i].fe_logical - fm_last.fe_logical;
                        if (fm_ext[i].fe_logical != 0 &&
-                           fm_ext[i].fe_physical != expected) {
+                           fm_ext[i].fe_physical != expected &&
+                           fm_ext[i].fe_physical != expected_dense) {
                                tot_extents++;
                        } else {
                                expected = 0;
@@ -256,10 +275,9 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents,
                        if (verbose)
                                print_extent_info(&fm_ext[i], n, expected,
                                                  blk_shift, st);
-
-                       expected = fm_ext[i].fe_physical + fm_ext[i].fe_length;
                        if (fm_ext[i].fe_flags & FIEMAP_EXTENT_LAST)
                                last = 1;
+                       fm_last = fm_ext[i];
                        n++;
                }
 
@@ -278,20 +296,21 @@ static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
                           ext2fs_struct_stat *st,
                           unsigned long numblocks, int is_ext2)
 {
-       struct fiemap_extent    fm_ext;
+       struct fiemap_extent    fm_ext, fm_last;
        unsigned long           i, last_block;
-       unsigned long long      logical;
+       unsigned long long      logical, expected = 0;
                                /* Blocks per indirect block */
        const long              bpib = st->st_blksize / 4;
        int                     count;
 
+       memset(&fm_ext, 0, sizeof(fm_ext));
+       memset(&fm_last, 0, sizeof(fm_last));
        if (force_extent) {
-               memset(&fm_ext, 0, sizeof(fm_ext));
                fm_ext.fe_flags = FIEMAP_EXTENT_MERGED;
        }
 
-       if (sync_file)
-               fsync(fd);
+       if (sync_file && fsync(fd) != 0)
+               return -errno;
 
        for (i = 0, logical = 0, *num_extents = 0, count = last_block = 0;
             i < numblocks;
@@ -313,36 +332,52 @@ static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
                        return rc;
                if (block == 0)
                        continue;
-               if (*num_extents == 0) {
-                       (*num_extents)++;
-                       if (force_extent) {
+
+               if (*num_extents == 0 || block != last_block + 1 ||
+                   fm_ext.fe_logical + fm_ext.fe_length != logical) {
+                       /*
+                        * This is the start of a new extent; figure out where
+                        * we expected it to be and report the extent.
+                        */
+                       if (*num_extents != 0 && fm_last.fe_length) {
+                               expected = fm_last.fe_physical +
+                                       (fm_ext.fe_logical - fm_last.fe_logical);
+                               if (expected == fm_ext.fe_physical)
+                                       expected = 0;
+                       }
+                       if (force_extent && *num_extents == 0)
                                print_extent_header();
-                               fm_ext.fe_physical = block * st->st_blksize;
+                       if (force_extent && *num_extents != 0) {
+                               print_extent_info(&fm_ext, *num_extents - 1,
+                                                 expected, blk_shift, st);
                        }
-               }
-               count++;
-               if (force_extent && last_block != 0 &&
-                   (block != last_block + 1 ||
-                    fm_ext.fe_logical + fm_ext.fe_length != logical)) {
-                       print_extent_info(&fm_ext, *num_extents - 1,
-                                         (last_block + 1) * st->st_blksize,
-                                         blk_shift, st);
-                       fm_ext.fe_logical = logical;
+                       if (verbose && expected != 0) {
+                               printf("Discontinuity: Block %llu is at %llu "
+                                      "(was %llu)\n",
+                                       fm_ext.fe_logical / st->st_blksize,
+                                       fm_ext.fe_physical / st->st_blksize,
+                                       expected / st->st_blksize);
+                       }
+                       /* create the new extent */
+                       fm_last = fm_ext;
+                       (*num_extents)++;
                        fm_ext.fe_physical = block * st->st_blksize;
+                       fm_ext.fe_logical = logical;
                        fm_ext.fe_length = 0;
-                       (*num_extents)++;
-               } else if (verbose && last_block && (block != last_block + 1)) {
-                       printf("Discontinuity: Block %ld is at %lu (was %lu)\n",
-                              i, block, last_block + 1);
-                       (*num_extents)++;
                }
                fm_ext.fe_length += st->st_blksize;
                last_block = block;
        }
-
-       if (force_extent)
-               print_extent_info(&fm_ext, *num_extents - 1,
-                                 last_block * st->st_blksize, blk_shift, st);
+       if (force_extent && *num_extents != 0) {
+               if (fm_last.fe_length) {
+                       expected = fm_last.fe_physical +
+                                  (fm_ext.fe_logical - fm_last.fe_logical);
+                       if (expected == fm_ext.fe_physical)
+                               expected = 0;
+               }
+               print_extent_info(&fm_ext, *num_extents - 1, expected,
+                                 blk_shift, st);
+       }
 
        return count;
 }
@@ -350,6 +385,7 @@ static int filefrag_fibmap(int fd, int blk_shift, int *num_extents,
 static int frag_report(const char *filename)
 {
        static struct statfs fsinfo;
+       static unsigned int blksize;
        ext2fs_struct_stat st;
        int             blk_shift;
        long            fd;
@@ -358,7 +394,6 @@ static int frag_report(const char *filename)
        int             num_extents = 1, expected = ~0;
        int             is_ext2 = 0;
        static dev_t    last_device;
-       unsigned int    flags;
        int             width;
        int             rc = 0;
 
@@ -378,38 +413,41 @@ static int frag_report(const char *filename)
 #else
        if (fstat(fd, &st) < 0) {
 #endif
-               close(fd);
                rc = -errno;
                perror("stat");
-               return rc;
+               goto out_close;
        }
 
        if (last_device != st.st_dev) {
                if (fstatfs(fd, &fsinfo) < 0) {
-                       close(fd);
+                       rc = -errno;
                        perror("fstatfs");
-                       return;
+                       goto out_close;
                }
+               if (ioctl(fd, FIGETBSZ, &blksize) < 0)
+                       blksize = fsinfo.f_bsize;
                if (verbose)
                        printf("Filesystem type is: %lx\n",
                               (unsigned long)fsinfo.f_type);
        }
-       st.st_blksize = fsinfo.f_bsize;
-       if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) < 0)
-               flags = 0;
-       if (!(flags & EXT4_EXTENTS_FL) &&
-           ((fsinfo.f_type == 0xef51) || (fsinfo.f_type == 0xef52) ||
-            (fsinfo.f_type == 0xef53)))
-               is_ext2++;
+       st.st_blksize = blksize;
+       if (fsinfo.f_type == 0xef51 || fsinfo.f_type == 0xef52 ||
+           fsinfo.f_type == 0xef53) {
+               unsigned int    flags;
+
+               if (ioctl(fd, EXT3_IOC_GETFLAGS, &flags) == 0 &&
+                   !(flags & EXT4_EXTENTS_FL))
+                       is_ext2 = 1;
+       }
 
        if (is_ext2) {
-               long cylgroups = div_ceil(fsinfo.f_blocks, fsinfo.f_bsize * 8);
+               long cylgroups = div_ceil(fsinfo.f_blocks, blksize * 8);
 
                if (verbose && last_device != st.st_dev)
                        printf("Filesystem cylinder groups approximately %ld\n",
                               cylgroups);
 
-               data_blocks_per_cyl = fsinfo.f_bsize * 8 -
+               data_blocks_per_cyl = blksize * 8 -
                                        (fsinfo.f_files / 8 / cylgroups) - 3;
        }
        last_device = st.st_dev;
@@ -418,11 +456,11 @@ static int frag_report(const char *filename)
        if (width > physical_width)
                physical_width = width;
 
-       numblocks = (st.st_size + fsinfo.f_bsize - 1) / fsinfo.f_bsize;
+       numblocks = (st.st_size + blksize - 1) / blksize;
        if (blocksize != 0)
                blk_shift = int_log2(blocksize);
        else
-               blk_shift = int_log2(fsinfo.f_bsize);
+               blk_shift = int_log2(blksize);
 
        width = int_log10(numblocks);
        if (width > logical_width)
@@ -430,7 +468,7 @@ static int frag_report(const char *filename)
        if (verbose)
                printf("File size of %s is %llu (%llu block%s of %d bytes)\n",
                       filename, (unsigned long long)st.st_size,
-                      numblocks * fsinfo.f_bsize >> blk_shift,
+                      numblocks * blksize >> blk_shift,
                       numblocks == 1 ? "" : "s", 1 << blk_shift);
 
        if (!force_bmap) {
@@ -438,7 +476,7 @@ static int frag_report(const char *filename)
                expected = 0;
        }
 
-       if (rc < 0) {
+       if (force_bmap || rc < 0) { /* FIEMAP failed, try FIBMAP instead */
                expected = filefrag_fibmap(fd, blk_shift, &num_extents,
                                           &st, numblocks, is_ext2);
                if (expected < 0) {
@@ -479,7 +517,7 @@ out_close:
 
 static void usage(const char *progname)
 {
-       fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeklsvxX] file ...\n",
+       fprintf(stderr, "Usage: %s [-b{blocksize}] [-BeksvxX] file ...\n",
                progname);
        exit(1);
 }
@@ -489,7 +527,7 @@ int main(int argc, char**argv)
        char **cpp;
        int rc = 0, c;
 
-       while ((c = getopt(argc, argv, "Bb::eksvxX")) != EOF)
+       while ((c = getopt(argc, argv, "Bb::eksvxX")) != EOF) {
                switch (c) {
                case 'B':
                        force_bmap++;
@@ -499,15 +537,19 @@ int main(int argc, char**argv)
                                char *end;
                                blocksize = strtoul(optarg, &end, 0);
                                if (end) {
+#if __GNUC_PREREQ (7, 0)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
+#endif
                                        switch (end[0]) {
                                        case 'g':
                                        case 'G':
                                                blocksize *= 1024;
-                                               /* no break */
+                                               /* fall through */
                                        case 'm':
                                        case 'M':
                                                blocksize *= 1024;
-                                               /* no break */
+                                               /* fall through */
                                        case 'k':
                                        case 'K':
                                                blocksize *= 1024;
@@ -515,6 +557,9 @@ int main(int argc, char**argv)
                                        default:
                                                break;
                                        }
+#if __GNUC_PREREQ (7, 0)
+#pragma GCC diagnostic pop
+#endif
                                }
                        } else { /* Allow -b without argument for compat. Remove
                                  * this eventually so "-b {blocksize}" works */
@@ -548,17 +593,18 @@ int main(int argc, char**argv)
                        usage(argv[0]);
                        break;
                }
+       }
 
        if (optind == argc)
                usage(argv[0]);
 
-       for (cpp = argv + optind; *cpp != '\0'; cpp++) {
+       for (cpp = argv + optind; *cpp != NULL; cpp++) {
                int rc2 = frag_report(*cpp);
 
                if (rc2 < 0 && rc == 0)
-                       rc == rc2;
+                       rc = rc2;
        }
 
-       return rc;
+       return -rc;
 }
 #endif