Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / misc / filefrag.c
index 66a09e7..d604b6c 100644 (file)
@@ -46,6 +46,7 @@ int verbose = 0;
 int no_bs = 0;         /* Don't use the files blocksize, use 1K blocksize */
 int sync_file = 0;     /* fsync file before getting the mapping */
 int xattr_map = 0;     /* get xattr mapping */
+int force_bmap = 0;
 int logical_width = 12;
 int physical_width = 14;
 unsigned long long filesize;
@@ -172,12 +173,11 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents)
        unsigned long flags = 0;
        unsigned int i;
        static int fiemap_incompat_printed;
+       int fiemap_header_printed = 0;
        int tot_extents = 1, n = 0;
        int last = 0;
        int rc;
 
-       fiemap->fm_length = ~0ULL;
-
        memset(fiemap, 0, sizeof(struct fiemap));
 
        if (!verbose)
@@ -189,11 +189,6 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents)
        if (xattr_map)
                flags |= FIEMAP_FLAG_XATTR;
 
-       if (verbose)
-               printf(" ext %*s %*s %*s length flags\n", logical_width,
-                      "logical", physical_width, "physical",
-                      physical_width, "expected");
-
        do {
                fiemap->fm_length = ~0ULL;
                fiemap->fm_flags = flags;
@@ -208,6 +203,13 @@ static int filefrag_fiemap(int fd, int blk_shift, int *num_extents)
                        return rc;
                }
 
+               if (verbose && !fiemap_header_printed) {
+                       printf(" ext %*s %*s %*s length flags\n", logical_width,
+                              "logical", physical_width, "physical",
+                              physical_width, "expected");
+                       fiemap_header_printed = 1;
+               }
+
                if (!verbose) {
                        *num_extents = fiemap->fm_mapped_extents;
                        goto out;
@@ -327,7 +329,8 @@ static void frag_report(const char *filename)
                printf("File size of %s is %lld (%ld block%s, blocksize %d)\n",
                       filename, (long long) fileinfo.st_size, numblocks,
                       numblocks == 1 ? "" : "s", bs);
-       if (filefrag_fiemap(fd, int_log2(bs), &num_extents) != 0) {
+       if (force_bmap ||
+           filefrag_fiemap(fd, int_log2(bs), &num_extents) != 0) {
                for (i = 0, count = 0; i < numblocks; i++) {
                        if (is_ext2 && last_block) {
                                if (((i-EXT2_DIRECT) % bpib) == 0)
@@ -335,12 +338,14 @@ static void frag_report(const char *filename)
                                if (((i-EXT2_DIRECT-bpib) % (bpib*bpib)) == 0)
                                        last_block++;
                                if (((i-EXT2_DIRECT-bpib-bpib*bpib) %
-                                                       (bpib*bpib*bpib)) == 0)
+                                    (((__u64) bpib)*bpib*bpib)) == 0)
                                        last_block++;
                        }
                        rc = get_bmap(fd, i, &block);
                        if (block == 0)
                                continue;
+                       if (!num_extents)
+                               num_extents++;
                        count++;
                        if (last_block && (block != last_block+1) ) {
                                if (verbose)
@@ -368,7 +373,7 @@ static void frag_report(const char *filename)
 
 static void usage(const char *progname)
 {
-       fprintf(stderr, "Usage: %s [-bvsx] file ...\n", progname);
+       fprintf(stderr, "Usage: %s [-Bbvsx] file ...\n", progname);
        exit(1);
 }
 
@@ -377,8 +382,11 @@ int main(int argc, char**argv)
        char **cpp;
        int c;
 
-       while ((c = getopt(argc, argv, "bsvx")) != EOF)
+       while ((c = getopt(argc, argv, "Bbsvx")) != EOF)
                switch (c) {
+               case 'B':
+                       force_bmap++;
+                       break;
                case 'b':
                        no_bs++;
                        break;