#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)
#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 */
*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 ||
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
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;
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++;
}
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) /
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)
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);
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;
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 */
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;
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))
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,
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,