Whamcloud - gitweb
e2fsck: map PROMPT_* values to prompt messages
[tools/e2fsprogs.git] / misc / e2freefrag.c
index c5aeab9..49b6346 100644 (file)
@@ -44,6 +44,10 @@ extern int optind;
 # include "fsmap.h"
 #endif
 
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
 static void usage(const char *prog)
 {
        fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] "
@@ -159,7 +163,8 @@ static void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info)
 
 #if defined(HAVE_EXT2_IOCTLS) && !defined(DEBUGFS)
 # define FSMAP_EXTENTS 1024
-static int scan_online(ext2_filsys fs, struct chunk_info *info)
+static int scan_online(ext2_filsys fs, struct chunk_info *info,
+                      blk64_t *free_blks)
 {
        struct fsmap_head *fsmap;
        struct fsmap *extent;
@@ -169,7 +174,7 @@ static int scan_online(ext2_filsys fs, struct chunk_info *info)
        int mount_flags;
        int fd;
        int ret;
-       int i;
+       unsigned int i;
 
        /* Try to open the mountpoint for a live query. */
        retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
@@ -178,7 +183,7 @@ static int scan_online(ext2_filsys fs, struct chunk_info *info)
                com_err(fs->device_name, retval, "while checking mount status");
                return 0;
        }
-       if (!mount_flags & EXT2_MF_MOUNTED)
+       if (!(mount_flags & EXT2_MF_MOUNTED))
                return 0;
        fd = open(mntpoint, O_RDONLY);
        if (fd < 0) {
@@ -200,6 +205,7 @@ static int scan_online(ext2_filsys fs, struct chunk_info *info)
        fsmap->fmh_keys[1].fmr_offset = ULLONG_MAX;
        fsmap->fmh_keys[1].fmr_flags = UINT_MAX;
 
+       *free_blks = 0;
        /* Fill the extent histogram with live data */
        while (1) {
                ret = ioctl(fd, FS_IOC_GETFSMAP, fsmap);
@@ -221,6 +227,7 @@ static int scan_online(ext2_filsys fs, struct chunk_info *info)
                                continue;
                        update_chunk_stats(info,
                                           extent->fmr_length / fs->blocksize);
+                       *free_blks += (extent->fmr_length / fs->blocksize);
                }
 
                p = &fsmap->fmh_recs[fsmap->fmh_entries - 1];
@@ -228,17 +235,19 @@ static int scan_online(ext2_filsys fs, struct chunk_info *info)
                        break;
                fsmap_advance(fsmap);
        }
-
+       free(fsmap);
        return 1;
 }
 #else
-# define scan_online(fs, info) (0)
+# define scan_online(fs, info, free_blks)      (0)
 #endif /* HAVE_EXT2_IOCTLS */
 
-static errcode_t scan_offline(ext2_filsys fs, struct chunk_info *info)
+static errcode_t scan_offline(ext2_filsys fs, struct chunk_info *info,
+                             blk64_t *free_blks)
 {
        errcode_t retval;
 
+       *free_blks = ext2fs_free_blocks_count(fs->super);
        retval = ext2fs_read_block_bitmap(fs);
        if (retval)
                return retval;
@@ -247,7 +256,7 @@ static errcode_t scan_offline(ext2_filsys fs, struct chunk_info *info)
 }
 
 static errcode_t dump_chunk_info(ext2_filsys fs, struct chunk_info *info,
-                                FILE *f)
+                                FILE *f, blk64_t free_blks)
 {
        unsigned long total_chunks;
        const char *unitp = "KMGTPEZY";
@@ -256,9 +265,9 @@ static errcode_t dump_chunk_info(ext2_filsys fs, struct chunk_info *info,
        int i, retval = 0;
 
        fprintf(f, "Total blocks: %llu\nFree blocks: %llu (%0.1f%%)\n",
-               ext2fs_blocks_count(fs->super),
-               ext2fs_free_blocks_count(fs->super),
-               (double)ext2fs_free_blocks_count(fs->super) * 100 /
+               (unsigned long long) ext2fs_blocks_count(fs->super),
+               (unsigned long long) free_blks,
+               (double)free_blks * 100 /
                ext2fs_blocks_count(fs->super));
 
        if (info->chunkbytes) {
@@ -302,7 +311,7 @@ static errcode_t dump_chunk_info(ext2_filsys fs, struct chunk_info *info,
                                info->histogram.fc_chunks[i],
                                info->histogram.fc_blocks[i],
                                (double)info->histogram.fc_blocks[i] * 100 /
-                               ext2fs_free_blocks_count(fs->super));
+                               free_blks);
                }
                start = end;
                if (start == 1<<10) {
@@ -326,14 +335,15 @@ static void close_device(char *device_name, ext2_filsys fs)
 static void collect_info(ext2_filsys fs, struct chunk_info *chunk_info, FILE *f)
 {
        unsigned int retval = 0;
+       blk64_t free_blks = 0;
 
        fprintf(f, "Device: %s\n", fs->device_name);
        fprintf(f, "Blocksize: %u bytes\n", fs->blocksize);
 
        init_chunk_info(fs, chunk_info);
-       if (!scan_online(fs, chunk_info)) {
+       if (!scan_online(fs, chunk_info, &free_blks)) {
                init_chunk_info(fs, chunk_info);
-               retval = scan_offline(fs, chunk_info);
+               retval = scan_offline(fs, chunk_info, &free_blks);
        }
        if (retval) {
                com_err(fs->device_name, retval, "while reading block bitmap");
@@ -341,7 +351,7 @@ static void collect_info(ext2_filsys fs, struct chunk_info *chunk_info, FILE *f)
                exit(1);
        }
 
-       retval = dump_chunk_info(fs, chunk_info, f);
+       retval = dump_chunk_info(fs, chunk_info, f, free_blks);
        if (retval) {
                com_err(fs->device_name, retval, "while dumping chunk info");
                 close_device(fs->device_name, fs);
@@ -353,7 +363,7 @@ static void collect_info(ext2_filsys fs, struct chunk_info *chunk_info, FILE *f)
 static void open_device(char *device_name, ext2_filsys *fs)
 {
        int retval;
-       int flag = EXT2_FLAG_FORCE | EXT2_FLAG_64BITS;
+       int flag = EXT2_FLAG_FORCE | EXT2_FLAG_64BITS | EXT2_FLAG_THREADS;
 
        retval = ext2fs_open(device_name, flag, 0, 0, unix_io_manager, fs);
        if (retval) {
@@ -367,7 +377,8 @@ static void open_device(char *device_name, ext2_filsys *fs)
 #ifdef DEBUGFS
 #include "debugfs.h"
 
-void do_freefrag(int argc, char **argv)
+void do_freefrag(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
+                void *infop EXT2FS_ATTR((unused)))
 #else
 int main(int argc, char *argv[])
 #endif