]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blobdiff - misc/e4defrag.c
e4crypt: remove unneeded stat(2) call in do_get_policy()
[thirdparty/e2fsprogs.git] / misc / e4defrag.c
index e795987c187ac473e1c36633b86d07251cae2fb2..5ac251dc5c1aa33faee0808567bfee46768653a9 100644 (file)
@@ -19,6 +19,7 @@
 #define _GNU_SOURCE
 #endif
 
+#include "config.h"
 #include <ctype.h>
 #include <dirent.h>
 #include <endian.h>
 #include <unistd.h>
 #include <ext2fs/ext2_types.h>
 #include <ext2fs/ext2fs.h>
-#include <linux/fs.h>
 #include <sys/ioctl.h>
 #include <ext2fs/fiemap.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
-#include <sys/syscall.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)
@@ -163,190 +164,40 @@ struct frag_statistic_ino {
        char msg_buffer[PATH_MAX + 1];  /* pathname of the file */
 };
 
-typedef __u16 __le16;
-typedef __u32 __le32;
-typedef __u64 __le64;
-
-/*
- * Structure of the super block
- */
-struct ext4_super_block {
-/*00*/ __le32  s_inodes_count;         /* Inodes count */
-       __le32  s_blocks_count_lo;      /* Blocks count */
-       __le32  s_r_blocks_count_lo;    /* Reserved blocks count */
-       __le32  s_free_blocks_count_lo; /* Free blocks count */
-/*10*/ __le32  s_free_inodes_count;    /* Free inodes count */
-       __le32  s_first_data_block;     /* First Data Block */
-       __le32  s_log_block_size;       /* Block size */
-       __le32  s_obso_log_frag_size;   /* Obsoleted fragment size */
-/*20*/ __le32  s_blocks_per_group;     /* # Blocks per group */
-       __le32  s_obso_frags_per_group; /* Obsoleted fragments per group */
-       __le32  s_inodes_per_group;     /* # Inodes per group */
-       __le32  s_mtime;                /* Mount time */
-/*30*/ __le32  s_wtime;                /* Write time */
-       __le16  s_mnt_count;            /* Mount count */
-       __le16  s_max_mnt_count;        /* Maximal mount count */
-       __le16  s_magic;                /* Magic signature */
-       __le16  s_state;                /* File system state */
-       __le16  s_errors;               /* Behaviour when detecting errors */
-       __le16  s_minor_rev_level;      /* minor revision level */
-/*40*/ __le32  s_lastcheck;            /* time of last check */
-       __le32  s_checkinterval;        /* max. time between checks */
-       __le32  s_creator_os;           /* OS */
-       __le32  s_rev_level;            /* Revision level */
-/*50*/ __le16  s_def_resuid;           /* Default uid for reserved blocks */
-       __le16  s_def_resgid;           /* Default gid for reserved blocks */
-       /*
-        * These fields are for EXT4_DYNAMIC_REV superblocks only.
-        *
-        * Note: the difference between the compatible feature set and
-        * the incompatible feature set is that if there is a bit set
-        * in the incompatible feature set that the kernel doesn't
-        * know about, it should refuse to mount the filesystem.
-        *
-        * e2fsck's requirements are more strict; if it doesn't know
-        * about a feature in either the compatible or incompatible
-        * feature set, it must abort and not try to meddle with
-        * things it doesn't understand...
-        */
-       __le32  s_first_ino;            /* First non-reserved inode */
-       __le16  s_inode_size;           /* size of inode structure */
-       __le16  s_block_group_nr;       /* block group # of this superblock */
-       __le32  s_feature_compat;       /* compatible feature set */
-/*60*/ __le32  s_feature_incompat;     /* incompatible feature set */
-       __le32  s_feature_ro_compat;    /* readonly-compatible feature set */
-/*68*/ __u8    s_uuid[16];             /* 128-bit uuid for volume */
-/*78*/ char    s_volume_name[16];      /* volume name */
-/*88*/ char    s_last_mounted[64];     /* directory where last mounted */
-/*C8*/ __le32  s_algorithm_usage_bitmap; /* For compression */
-       /*
-        * Performance hints.  Directory preallocation should only
-        * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
-        */
-       __u8    s_prealloc_blocks;      /* Nr of blocks to try to preallocate*/
-       __u8    s_prealloc_dir_blocks;  /* Nr to preallocate for dirs */
-       __le16  s_reserved_gdt_blocks;  /* Per group desc for online growth */
-       /*
-        * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
-        */
-/*D0*/ __u8    s_journal_uuid[16];     /* uuid of journal superblock */
-/*E0*/ __le32  s_journal_inum;         /* inode number of journal file */
-       __le32  s_journal_dev;          /* device number of journal file */
-       __le32  s_last_orphan;          /* start of list of inodes to delete */
-       __le32  s_hash_seed[4];         /* HTREE hash seed */
-       __u8    s_def_hash_version;     /* Default hash version to use */
-       __u8    s_reserved_char_pad;
-       __le16  s_desc_size;            /* size of group descriptor */
-/*100*/        __le32  s_default_mount_opts;
-       __le32  s_first_meta_bg;        /* First metablock block group */
-       __le32  s_mkfs_time;            /* When the filesystem was created */
-       __le32  s_jnl_blocks[17];       /* Backup of the journal inode */
-       /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
-/*150*/        __le32  s_blocks_count_hi;      /* Blocks count */
-       __le32  s_r_blocks_count_hi;    /* Reserved blocks count */
-       __le32  s_free_blocks_count_hi; /* Free blocks count */
-       __le16  s_min_extra_isize;      /* All inodes have at least # bytes */
-       __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
-       __le32  s_flags;                /* Miscellaneous flags */
-       __le16  s_raid_stride;          /* RAID stride */
-       __le16  s_mmp_interval;         /* # seconds to wait in MMP checking */
-       __le64  s_mmp_block;            /* Block for multi-mount protection */
-       __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
-       __u8    s_log_groups_per_flex;  /* FLEX_BG group size */
-       __u8    s_reserved_char_pad2;
-       __le16  s_reserved_pad;
-       __u32   s_reserved[162];        /* Padding to the end of the block */
-};
-
-char   lost_found_dir[PATH_MAX + 1];
-int    block_size;
-int    extents_before_defrag;
-int    extents_after_defrag;
-int    mode_flag;
-unsigned int   current_uid;
-unsigned int   defraged_file_count;
-unsigned int   frag_files_before_defrag;
-unsigned int   frag_files_after_defrag;
-unsigned int   regular_count;
-unsigned int   succeed_cnt;
-unsigned int   total_count;
-__u8 log_groups_per_flex;
-__le32 blocks_per_group;
-__le32 feature_incompat;
-ext4_fsblk_t   files_block_count;
-struct frag_statistic_ino      frag_rank[SHOW_FRAG_FILES];
-
-
-/* Local definitions of some syscalls glibc may not yet have */
-
-#ifndef HAVE_POSIX_FADVISE
-#warning Using locally defined posix_fadvise interface.
-
-#ifndef __NR_fadvise64_64
-#error Your kernel headers dont define __NR_fadvise64_64
-#endif
-
-/*
- * fadvise() -         Give advice about file access.
- *
- * @fd:                        defrag target file's descriptor.
- * @offset:            file offset.
- * @len:               area length.
- * @advise:            process flag.
- */
-static int posix_fadvise(int fd, loff_t offset, size_t len, int advise)
-{
-       return syscall(__NR_fadvise64_64, fd, offset, len, advise);
-}
-#endif /* ! HAVE_FADVISE64_64 */
-
-#ifndef HAVE_SYNC_FILE_RANGE
-#warning Using locally defined sync_file_range interface.
+static char    lost_found_dir[PATH_MAX + 1];
+static int     block_size;
+static int     extents_before_defrag;
+static int     extents_after_defrag;
+static int     mode_flag;
+static unsigned int    current_uid;
+static unsigned int    defraged_file_count;
+static unsigned int    frag_files_before_defrag;
+static unsigned int    frag_files_after_defrag;
+static unsigned int    regular_count;
+static unsigned int    succeed_cnt;
+static unsigned int    total_count;
+static __u8 log_groups_per_flex;
+static __u32 blocks_per_group;
+static __u32 feature_incompat;
+static ext4_fsblk_t    files_block_count;
+static struct frag_statistic_ino       frag_rank[SHOW_FRAG_FILES];
 
-#ifndef __NR_sync_file_range
-#ifndef __NR_sync_file_range2 /* ppc */
-#error Your kernel headers dont define __NR_sync_file_range
-#endif
-#endif
 
 /*
- * sync_file_range() - Sync file region.
- *
- * @fd:                        defrag target file's descriptor.
- * @offset:            file offset.
- * @length:            area length.
- * @flag:              process flag.
+ * We prefer posix_fadvise64 when available, as it allows 64bit offset on
+ * 32bit systems
  */
-int sync_file_range(int fd, loff_t offset, loff_t length, unsigned int flag)
-{
-#ifdef __NR_sync_file_range
-       return syscall(__NR_sync_file_range, fd, offset, length, flag);
-#else
-       return syscall(__NR_sync_file_range2, fd, flag, offset, length);
+#if defined(HAVE_POSIX_FADVISE64)
+#define posix_fadvise  posix_fadvise64
+#elif defined(HAVE_FADVISE64)
+#define posix_fadvise  fadvise64
+#elif !defined(HAVE_POSIX_FADVISE)
+#error posix_fadvise not available!
 #endif
-}
-#endif /* ! HAVE_SYNC_FILE_RANGE */
 
 #ifndef HAVE_FALLOCATE64
-#warning Using locally defined fallocate syscall interface.
-
-#ifndef __NR_fallocate
-#error Your kernel headers dont define __NR_fallocate
-#endif
-
-/*
- * fallocate64() -     Manipulate file space.
- *
- * @fd:                        defrag target file's descriptor.
- * @mode:              process flag.
- * @offset:            file offset.
- * @len:               file size.
- */
-static int fallocate64(int fd, int mode, loff_t offset, loff_t len)
-{
-       return syscall(__NR_fallocate, fd, mode, offset, len);
-}
-#endif /* ! HAVE_FALLOCATE */
+#error fallocate64 not available!
+#endif /* ! HAVE_FALLOCATE64 */
 
 /*
  * get_mount_point() - Get device's mount point.
@@ -360,8 +211,15 @@ static int get_mount_point(const char *devname, char *mount_point,
 {
        /* Refer to /etc/mtab */
        const char      *mtab = MOUNTED;
-       FILE    *fp = NULL;
+       FILE            *fp = NULL;
        struct mntent   *mnt = NULL;
+       struct stat64   sb;
+
+       if (stat64(devname, &sb) < 0) {
+               perror(NGMSG_FILE_INFO);
+               PRINT_FILE_NAME(devname);
+               return -1;
+       }
 
        fp = setmntent(mtab, "r");
        if (fp == NULL) {
@@ -370,7 +228,15 @@ static int get_mount_point(const char *devname, char *mount_point,
        }
 
        while ((mnt = getmntent(fp)) != NULL) {
-               if (strcmp(devname, mnt->mnt_fsname) != 0)
+               struct stat64 ms;
+
+               /*
+                * To handle device symlinks, we see if the
+                * device number matches, not the name
+                */
+               if (stat64(mnt->mnt_fsname, &ms) < 0)
+                       continue;
+               if (sb.st_rdev != ms.st_rdev)
                        continue;
 
                endmntent(fp);
@@ -392,7 +258,7 @@ static int get_mount_point(const char *devname, char *mount_point,
  *
  * @file:              the file's name.
  */
-static int is_ext4(const char *file)
+static int is_ext4(const char *file, char *devname)
 {
        int     maxlen = 0;
        int     len, ret;
@@ -449,10 +315,11 @@ static int is_ext4(const char *file)
                memset(mnt_type, 0, strlen(mnt->mnt_type) + 1);
                strncpy(mnt_type, mnt->mnt_type, strlen(mnt->mnt_type));
                strncpy(lost_found_dir, mnt->mnt_dir, PATH_MAX);
+               strncpy(devname, mnt->mnt_fsname, strlen(mnt->mnt_fsname) + 1);
        }
 
        endmntent(fp);
-       if (strcmp(mnt_type, FS_EXT4) == 0) {
+       if (mnt_type && strcmp(mnt_type, FS_EXT4) == 0) {
                FREE(mnt_type);
                return 0;
        } else {
@@ -493,13 +360,16 @@ static int calc_entry_counts(const char *file EXT2FS_ATTR((unused)),
 static int page_in_core(int fd, struct move_extent defrag_data,
                        unsigned char **vec, unsigned int *page_num)
 {
-       long    pagesize = sysconf(_SC_PAGESIZE);
+       long    pagesize;
        void    *page = NULL;
        loff_t  offset, end_offset, length;
 
        if (vec == NULL || *vec != NULL)
                return -1;
 
+       pagesize = sysconf(_SC_PAGESIZE);
+       if (pagesize < 0)
+               return -1;
        /* In mmap, offset should be a multiple of the page size */
        offset = (loff_t)defrag_data.orig_start * block_size;
        length = (loff_t)defrag_data.len * block_size;
@@ -515,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 ||
@@ -548,13 +420,18 @@ static int defrag_fadvise(int fd, struct move_extent defrag_data,
        unsigned int    i;
        loff_t  offset;
 
+       if (pagesize < 1)
+               return -1;
+
        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
@@ -942,7 +819,7 @@ static int get_file_extents(int fd, struct fiemap_extent_list **ext_list_head)
                fiemap_buf->fm_start = pos;
                memset(ext_buf, 0, ext_buf_size);
                ret = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
-               if (ret < 0)
+               if (ret < 0 || fiemap_buf->fm_mapped_extents == 0)
                        goto out;
                for (i = 0; i < fiemap_buf->fm_mapped_extents; i++) {
                        ext_list = NULL;
@@ -1013,7 +890,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++;
                }
@@ -1127,67 +1006,6 @@ static void free_exts_group(struct fiemap_extent_group *ext_group_head)
        free(ext_group_head);
 }
 
-/*
- * get_superblock_info() -     Get superblock info by the file name.
- *
- * @file:              the file's name.
- * @sb:                the pointer of the struct ext4_super_block.
- */
-static int get_superblock_info(const char *file, struct ext4_super_block *sb)
-{
-       /* Refer to /etc/mtab */
-       const char      *mtab = MOUNTED;
-       FILE    *fp = NULL;
-
-       int     fd = -1;
-       int     ret;
-       size_t maxlen = 0;
-       size_t len;
-       char    dev_name[PATH_MAX + 1];
-       struct mntent   *mnt = NULL;
-
-       fp = setmntent(mtab, "r");
-       if (fp == NULL)
-               return -1;
-
-       while ((mnt = getmntent(fp)) != NULL) {
-               len = strlen(mnt->mnt_dir);
-               ret = memcmp(file, mnt->mnt_dir, len);
-               if (ret != 0)
-                       continue;
-
-               if (len < maxlen)
-                       continue;
-
-               maxlen = len;
-
-               memset(dev_name, 0, PATH_MAX + 1);
-               strncpy(dev_name, mnt->mnt_fsname,
-                               strnlen(mnt->mnt_fsname, PATH_MAX));
-       }
-
-       fd = open64(dev_name, O_RDONLY);
-       if (fd < 0) {
-               ret = -1;
-               goto out;
-       }
-
-       /* Set offset to read superblock */
-       ret = lseek64(fd, SUPERBLOCK_OFFSET, SEEK_SET);
-       if (ret < 0)
-               goto out;
-
-       ret = read(fd, sb, sizeof(struct ext4_super_block));
-       if (ret < 0)
-               goto out;
-
-out:
-       if (fd != -1)
-               close(fd);
-       endmntent(fp);
-       return ret;
-}
-
 /*
  * get_best_count() -  Get the file best extents count.
  *
@@ -1198,7 +1016,7 @@ static int get_best_count(ext4_fsblk_t block_count)
        int ret;
        unsigned int flex_bg_num;
 
-       /* Calcuate best extents count */
+       /* Calculate best extents count */
        if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
                flex_bg_num = 1 << log_groups_per_flex;
                ret = ((block_count - 1) /
@@ -1762,7 +1580,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 */
@@ -1854,13 +1672,17 @@ out:
 int main(int argc, char *argv[])
 {
        int     opt;
-       int     i, j;
+       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;
-       struct ext4_super_block sb;
+       ext2_filsys fs = NULL;
+
+       printf("e4defrag %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
 
        /* Parse arguments */
        if (argc == 1)
@@ -1900,6 +1722,7 @@ int main(int argc, char *argv[])
                log_groups_per_flex = 0;
 
                memset(dir_name, 0, PATH_MAX + 1);
+               memset(dev_name, 0, PATH_MAX + 1);
                memset(lost_found_dir, 0, PATH_MAX + 1);
                memset(frag_rank, 0,
                        sizeof(struct frag_statistic_ino) * SHOW_FRAG_FILES);
@@ -1919,8 +1742,18 @@ int main(int argc, char *argv[])
                        continue;
                }
 
+               /* Handle i.e. lvm device symlinks */
+               if (S_ISLNK(buf.st_mode)) {
+                       struct stat64   buf2;
+
+                       if (stat64(argv[i], &buf2) == 0 &&
+                           S_ISBLK(buf2.st_mode))
+                               buf = buf2;
+               }
+
                if (S_ISBLK(buf.st_mode)) {
                        /* Block device */
+                       strncpy(dev_name, argv[i], strnlen(argv[i], PATH_MAX));
                        if (get_mount_point(argv[i], dir_name, PATH_MAX) < 0)
                                continue;
                        if (lstat64(dir_name, &buf) < 0) {
@@ -1957,7 +1790,7 @@ int main(int argc, char *argv[])
                 * filesystem type checked in get_mount_point()
                 */
                if (arg_type == FILENAME || arg_type == DIRNAME) {
-                       if (is_ext4(argv[i]) < 0)
+                       if (is_ext4(argv[i], dev_name) < 0)
                                continue;
                        if (realpath(argv[i], dir_name) == NULL) {
                                perror("Couldn't get full path");
@@ -1968,37 +1801,40 @@ int main(int argc, char *argv[])
 
                if (current_uid == ROOT_UID) {
                        /* Get super block info */
-                       memset(&sb, 0, sizeof(struct ext4_super_block));
-                       if (get_superblock_info(dir_name, &sb) < 0) {
-                               if (mode_flag & DETAIL) {
-                                       perror("Can't get super block info");
-                                       PRINT_FILE_NAME(argv[i]);
-                               }
+                       ret = ext2fs_open(dev_name, EXT2_FLAG_64BITS, 0,
+                                         block_size, unix_io_manager, &fs);
+                       if (ret) {
+                               if (mode_flag & DETAIL)
+                                       com_err(argv[1], ret,
+                                               "while trying to open file system: %s",
+                                               dev_name);
                                continue;
                        }
 
-                       blocks_per_group = ext2fs_swab32(sb.s_blocks_per_group);
-                       feature_incompat = ext2fs_swab32(sb.s_feature_incompat);
-                       log_groups_per_flex = sb.s_log_groups_per_flex;
+                       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);
                }
 
                switch (arg_type) {
+
                case DIRNAME:
                        if (!(mode_flag & STATISTIC))
                                printf("ext4 defragmentation "
                                        "for directory(%s)\n", argv[i]);
 
-                       int mount_dir_len = 0;
                        mount_dir_len = strnlen(lost_found_dir, PATH_MAX);
 
                        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,
@@ -2012,9 +1848,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,