Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / misc / e4defrag.c
index 620f4e7..c6c6f13 100644 (file)
@@ -41,6 +41,8 @@
 #include <sys/statfs.h>
 #include <sys/vfs.h>
 
+#include "../version.h"
+
 /* A relatively new ioctl interface ... */
 #ifndef EXT4_IOC_MOVE_EXT
 #define EXT4_IOC_MOVE_EXT      _IOWR('f', 15, struct move_extent)
@@ -193,10 +195,6 @@ static struct frag_statistic_ino   frag_rank[SHOW_FRAG_FILES];
 #error posix_fadvise not available!
 #endif
 
-#ifndef HAVE_SYNC_FILE_RANGE
-#error sync_file_range not available!
-#endif /* ! HAVE_SYNC_FILE_RANGE */
-
 #ifndef HAVE_FALLOCATE64
 #error fallocate64 not available!
 #endif /* ! HAVE_FALLOCATE64 */
@@ -387,8 +385,10 @@ static int page_in_core(int fd, struct move_extent defrag_data,
        *page_num = 0;
        *page_num = (length + pagesize - 1) / pagesize;
        *vec = (unsigned char *)calloc(*page_num, 1);
-       if (*vec == NULL)
+       if (*vec == NULL) {
+               munmap(page, length);
                return -1;
+       }
 
        /* Get information on whether pages are in core */
        if (mincore(page, (size_t)length, *vec) == -1 ||
@@ -426,10 +426,12 @@ static int defrag_fadvise(int fd, struct move_extent defrag_data,
        offset = (loff_t)defrag_data.orig_start * block_size;
        offset = (offset / pagesize) * pagesize;
 
+#ifdef HAVE_SYNC_FILE_RANGE
        /* Sync file for fadvise process */
        if (sync_file_range(fd, offset,
                (loff_t)pagesize * page_num, sync_flag) < 0)
                return -1;
+#endif
 
        /* Try to release buffer cache which this process used,
         * then other process can use the released buffer
@@ -439,7 +441,8 @@ static int defrag_fadvise(int fd, struct move_extent defrag_data,
                        offset += pagesize;
                        continue;
                }
-               if (posix_fadvise(fd, offset, pagesize, fadvise_flag) < 0) {
+               if ((errno = posix_fadvise(fd, offset,
+                                          pagesize, fadvise_flag)) != 0) {
                        if ((mode_flag & DETAIL) && flag) {
                                perror("\tFailed to fadvise");
                                flag = 0;
@@ -888,7 +891,9 @@ static int get_physical_count(struct fiemap_extent_list *physical_list_head)
 
        do {
                if ((ext_list_tmp->data.physical + ext_list_tmp->data.len)
-                               != ext_list_tmp->next->data.physical) {
+                               != ext_list_tmp->next->data.physical ||
+                   (ext_list_tmp->data.logical + ext_list_tmp->data.len)
+                               != ext_list_tmp->next->data.logical) {
                        /* This extent and next extent are not continuous. */
                        ret++;
                }
@@ -1012,7 +1017,9 @@ static int get_best_count(ext4_fsblk_t block_count)
        int ret;
        unsigned int flex_bg_num;
 
-       /* Calcuate best extents count */
+       if (blocks_per_group == 0)
+               return 1;
+
        if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
                flex_bg_num = 1 << log_groups_per_flex;
                ret = ((block_count - 1) /
@@ -1049,6 +1056,8 @@ static int file_statistic(const char *file, const struct stat64 *buf,
        struct fiemap_extent_list *logical_list_head = NULL;
 
        defraged_file_count++;
+       if (defraged_file_count > total_count)
+               total_count = defraged_file_count;
 
        if (mode_flag & DETAIL) {
                if (total_count == 1 && regular_count == 1)
@@ -1415,6 +1424,8 @@ static int file_defrag(const char *file, const struct stat64 *buf,
        struct fiemap_extent_group      *orig_group_tmp = NULL;
 
        defraged_file_count++;
+       if (defraged_file_count > total_count)
+               total_count = defraged_file_count;
 
        if (mode_flag & DETAIL) {
                printf("[%u/%u]", defraged_file_count, total_count);
@@ -1504,10 +1515,7 @@ static int file_defrag(const char *file, const struct stat64 *buf,
                goto out;
        }
 
-       if (current_uid == ROOT_UID)
-               best = get_best_count(blk_count);
-       else
-               best = 1;
+       best = get_best_count(blk_count);
 
        if (file_frags_start <= best)
                goto check_improvement;
@@ -1576,7 +1584,7 @@ static int file_defrag(const char *file, const struct stat64 *buf,
                goto out;
        }
 
-       /* Calcuate donor inode's continuous physical region */
+       /* Calculate donor inode's continuous physical region */
        donor_physical_cnt = get_physical_count(donor_list_physical);
 
        /* Change donor extent list from physical to logical */
@@ -1671,12 +1679,15 @@ int main(int argc, char *argv[])
        int     i, j, ret = 0;
        int     flags = FTW_PHYS | FTW_MOUNT;
        int     arg_type = -1;
+       int     mount_dir_len = 0;
        int     success_flag = 0;
        char    dir_name[PATH_MAX + 1];
        char    dev_name[PATH_MAX + 1];
        struct stat64   buf;
        ext2_filsys fs = NULL;
 
+       printf("e4defrag %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
+
        /* Parse arguments */
        if (argc == 1)
                goto out;
@@ -1794,25 +1805,23 @@ int main(int argc, char *argv[])
 
                if (current_uid == ROOT_UID) {
                        /* Get super block info */
-                       ret = ext2fs_open(dev_name, 0, 0, block_size,
-                                       unix_io_manager, &fs);
+                       ret = ext2fs_open(dev_name, EXT2_FLAG_64BITS, 0,
+                                         block_size, unix_io_manager, &fs);
                        if (ret) {
-                               if (mode_flag & DETAIL) {
-                                       perror("Can't get super block info");
-                                       PRINT_FILE_NAME(argv[i]);
-                               }
-                               continue;
+                               if (mode_flag & DETAIL)
+                                       fprintf(stderr,
+                                               "Warning: couldn't get file "
+                                               "system details for %s: %s\n",
+                                               dev_name, error_message(ret));
+                       } else {
+                               blocks_per_group = fs->super->s_blocks_per_group;
+                               feature_incompat = fs->super->s_feature_incompat;
+                               log_groups_per_flex = fs->super->s_log_groups_per_flex;
+                               ext2fs_close_free(&fs);
                        }
-
-                       blocks_per_group = fs->super->s_blocks_per_group;
-                       feature_incompat = fs->super->s_feature_incompat;
-                       log_groups_per_flex = fs->super->s_log_groups_per_flex;
-
-                       ext2fs_close(fs);
                }
 
                switch (arg_type) {
-                       int mount_dir_len = 0;
 
                case DIRNAME:
                        if (!(mode_flag & STATISTIC))
@@ -1824,11 +1833,11 @@ int main(int argc, char *argv[])
                        strncat(lost_found_dir, "/lost+found",
                                PATH_MAX - strnlen(lost_found_dir, PATH_MAX));
 
-                       /* Not the case("e4defrag  mount_piont_dir") */
+                       /* Not the case("e4defrag  mount_point_dir") */
                        if (dir_name[mount_dir_len] != '\0') {
                                /*
-                                * "e4defrag mount_piont_dir/lost+found"
-                                * or "e4defrag mount_piont_dir/lost+found/"
+                                * "e4defrag mount_point_dir/lost+found"
+                                * or "e4defrag mount_point_dir/lost+found/"
                                 */
                                if (strncmp(lost_found_dir, dir_name,
                                            strnlen(lost_found_dir,
@@ -1842,9 +1851,10 @@ int main(int argc, char *argv[])
                                        continue;
                                }
 
-                               /* "e4defrag mount_piont_dir/else_dir" */
+                               /* "e4defrag mount_point_dir/else_dir" */
                                memset(lost_found_dir, 0, PATH_MAX + 1);
                        }
+                       /* fall through */
                case DEVNAME:
                        if (arg_type == DEVNAME) {
                                strncpy(lost_found_dir, dir_name,