2 * e4defrag.c - ext4 filesystem defragmenter
4 * Copyright (C) 2009 NEC Software Tohoku, Ltd.
6 * Author: Akira Fujita <a-fujita@rs.jp.nec.com>
7 * Takashi Sato <t-sato@yk.jp.nec.com>
10 #ifndef _LARGEFILE_SOURCE
11 #define _LARGEFILE_SOURCE
14 #ifndef _LARGEFILE64_SOURCE
15 #define _LARGEFILE64_SOURCE
35 #include <ext2fs/ext2_types.h>
36 #include <ext2fs/ext2fs.h>
37 #include <sys/ioctl.h>
38 #include <ext2fs/fiemap.h>
41 #include <sys/statfs.h>
44 #include "../version.h"
46 /* A relatively new ioctl interface ... */
47 #ifndef EXT4_IOC_MOVE_EXT
48 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
52 #define PRINT_ERR_MSG(msg) fprintf(stderr, "%s\n", (msg))
53 #define IN_FTW_PRINT_ERR_MSG(msg) \
54 fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
55 #define PRINT_FILE_NAME(file) fprintf(stderr, " \"%s\"\n", (file))
56 #define PRINT_ERR_MSG_WITH_ERRNO(msg) \
57 fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
58 #define STATISTIC_ERR_MSG(msg) \
59 fprintf(stderr, "\t%s\n", (msg))
60 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg) \
61 fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
62 #define min(x, y) (((x) > (y)) ? (y) : (x))
63 #define CALC_SCORE(ratio) \
64 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
65 /* Wrap up the free function */
71 /* Insert list2 after list1 */
72 #define insert(list1, list2) \
74 list2->next = list1->next; \
75 list1->next->prev = list2; \
76 list2->prev = list1; \
77 list1->next = list2; \
80 /* To delete unused warning */
82 #define EXT2FS_ATTR(x) __attribute__(x)
84 #define EXT2FS_ATTR(x)
87 /* The mode of defrag */
89 #define STATISTIC 0x02
95 #define FTW_OPEN_FD 2000
97 #define FS_EXT4 "ext4"
100 #define BOUND_SCORE 55
101 #define SHOW_FRAG_FILES 5
103 /* Magic number for ext4 */
104 #define EXT4_SUPER_MAGIC 0xEF53
106 /* Definition of flex_bg */
107 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
109 /* The following macro is used for ioctl FS_IOC_FIEMAP
110 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
111 * kernel-space and user-space per ioctl
113 #define EXTENT_MAX_COUNT 512
115 /* The following macros are error message */
117 "Usage : e4defrag [-v] file...| directory...| device...\n\
118 : e4defrag -c file...| directory...| device...\n"
120 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
121 #define NGMSG_FILE_EXTENT "Failed to get file extents"
122 #define NGMSG_FILE_INFO "Failed to get file information"
123 #define NGMSG_FILE_OPEN "Failed to open"
124 #define NGMSG_FILE_UNREG "File is not regular file"
125 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
127 /* Data type for filesystem-wide blocks number */
128 typedef unsigned long long ext4_fsblk_t;
130 struct fiemap_extent_data {
131 __u64 len; /* blocks count */
132 __u64 logical; /* start logical block number */
133 ext4_fsblk_t physical; /* start physical block number */
136 struct fiemap_extent_list {
137 struct fiemap_extent_list *prev;
138 struct fiemap_extent_list *next;
139 struct fiemap_extent_data data; /* extent belong to file */
142 struct fiemap_extent_group {
143 struct fiemap_extent_group *prev;
144 struct fiemap_extent_group *next;
145 __u64 len; /* length of this continuous region */
146 struct fiemap_extent_list *start; /* start ext */
147 struct fiemap_extent_list *end; /* end ext */
151 __s32 reserved; /* original file descriptor */
152 __u32 donor_fd; /* donor file descriptor */
153 __u64 orig_start; /* logical start offset in block for orig */
154 __u64 donor_start; /* logical start offset in block for donor */
155 __u64 len; /* block length to be moved */
156 __u64 moved_len; /* moved block length */
159 struct frag_statistic_ino {
160 int now_count; /* the file's extents count of before defrag */
161 int best_count; /* the best file's extents count */
162 __u64 size_per_ext; /* size(KB) per extent */
163 float ratio; /* the ratio of fragmentation */
164 char msg_buffer[PATH_MAX + 1]; /* pathname of the file */
167 static char lost_found_dir[PATH_MAX + 1];
168 static int block_size;
169 static int extents_before_defrag;
170 static int extents_after_defrag;
171 static int mode_flag;
172 static unsigned int current_uid;
173 static unsigned int defraged_file_count;
174 static unsigned int frag_files_before_defrag;
175 static unsigned int frag_files_after_defrag;
176 static unsigned int regular_count;
177 static unsigned int succeed_cnt;
178 static unsigned int total_count;
179 static __u8 log_groups_per_flex;
180 static __u32 blocks_per_group;
181 static __u32 feature_incompat;
182 static ext4_fsblk_t files_block_count;
183 static struct frag_statistic_ino frag_rank[SHOW_FRAG_FILES];
187 * We prefer posix_fadvise64 when available, as it allows 64bit offset on
190 #if defined(HAVE_POSIX_FADVISE64)
191 #define posix_fadvise posix_fadvise64
192 #elif defined(HAVE_FADVISE64)
193 #define posix_fadvise fadvise64
194 #elif !defined(HAVE_POSIX_FADVISE)
195 #error posix_fadvise not available!
199 * get_mount_point() - Get device's mount point.
201 * @devname: the device's name.
202 * @mount_point: the mount point.
203 * @dir_path_len: the length of directory.
205 static int get_mount_point(const char *devname, char *mount_point,
208 /* Refer to /etc/mtab */
209 const char *mtab = MOUNTED;
211 struct mntent *mnt = NULL;
214 if (stat64(devname, &sb) < 0) {
215 perror(NGMSG_FILE_INFO);
216 PRINT_FILE_NAME(devname);
220 fp = setmntent(mtab, "r");
222 perror("Couldn't access /etc/mtab");
226 while ((mnt = getmntent(fp)) != NULL) {
230 * To handle device symlinks, we see if the
231 * device number matches, not the name
233 if (stat64(mnt->mnt_fsname, &ms) < 0)
235 if (sb.st_rdev != ms.st_rdev)
239 if (strcmp(mnt->mnt_type, FS_EXT4) == 0) {
240 strncpy(mount_point, mnt->mnt_dir,
244 PRINT_ERR_MSG(NGMSG_EXT4);
248 PRINT_ERR_MSG("Filesystem is not mounted");
253 * is_ext4() - Whether on an ext4 filesystem.
255 * @file: the file's name.
257 static int is_ext4(const char *file, char devname[PATH_MAX + 1])
261 int type_is_ext4 = 0;
263 /* Refer to /etc/mtab */
264 const char *mtab = MOUNTED;
265 char file_path[PATH_MAX + 1];
266 struct mntent *mnt = NULL;
267 struct statfs64 fsbuf;
270 if (realpath(file, file_path) == NULL) {
271 perror("Couldn't get full path");
272 PRINT_FILE_NAME(file);
276 if (statfs64(file_path, &fsbuf) < 0) {
277 perror("Failed to get filesystem information");
278 PRINT_FILE_NAME(file);
282 if (fsbuf.f_type != EXT4_SUPER_MAGIC) {
283 PRINT_ERR_MSG(NGMSG_EXT4);
287 fp = setmntent(mtab, "r");
289 perror("Couldn't access /etc/mtab");
293 while ((mnt = getmntent(fp)) != NULL) {
294 if (mnt->mnt_fsname[0] != '/')
296 len = strlen(mnt->mnt_dir);
297 ret = memcmp(file_path, mnt->mnt_dir, len);
306 type_is_ext4 = !strcmp(mnt->mnt_type, FS_EXT4);
307 strncpy(lost_found_dir, mnt->mnt_dir, PATH_MAX);
308 strncpy(devname, mnt->mnt_fsname, PATH_MAX);
314 PRINT_ERR_MSG(NGMSG_EXT4);
319 * calc_entry_counts() - Calculate file counts.
324 * @ftwbuf: the pointer of a struct FTW.
326 static int calc_entry_counts(const char *file EXT2FS_ATTR((unused)),
327 const struct stat64 *buf, int flag EXT2FS_ATTR((unused)),
328 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
330 if (S_ISREG(buf->st_mode))
339 * page_in_core() - Get information on whether pages are in core.
341 * @fd: defrag target file's descriptor.
342 * @defrag_data: data used for defrag.
343 * @vec: page state array.
344 * @page_num: page number.
346 static int page_in_core(int fd, struct move_extent defrag_data,
347 unsigned char **vec, unsigned int *page_num)
351 ext2_loff_t offset, end_offset, length;
353 if (vec == NULL || *vec != NULL)
356 pagesize = sysconf(_SC_PAGESIZE);
359 /* In mmap, offset should be a multiple of the page size */
360 offset = (ext2_loff_t)defrag_data.orig_start * block_size;
361 length = (ext2_loff_t)defrag_data.len * block_size;
362 end_offset = offset + length;
363 /* Round the offset down to the nearest multiple of pagesize */
364 offset = (offset / pagesize) * pagesize;
365 length = end_offset - offset;
367 page = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, offset);
368 if (page == MAP_FAILED)
372 *page_num = (length + pagesize - 1) / pagesize;
373 *vec = (unsigned char *)calloc(*page_num, 1);
375 munmap(page, length);
379 /* Get information on whether pages are in core */
380 if (mincore(page, (size_t)length, *vec) == -1 ||
381 munmap(page, length) == -1) {
390 * defrag_fadvise() - Predeclare an access pattern for file data.
392 * @fd: defrag target file's descriptor.
393 * @defrag_data: data used for defrag.
394 * @vec: page state array.
395 * @page_num: page number.
397 static int defrag_fadvise(int fd, struct move_extent defrag_data,
398 unsigned char *vec, unsigned int page_num)
401 long pagesize = sysconf(_SC_PAGESIZE);
402 int fadvise_flag = POSIX_FADV_DONTNEED;
403 int sync_flag = SYNC_FILE_RANGE_WAIT_BEFORE |
404 SYNC_FILE_RANGE_WRITE |
405 SYNC_FILE_RANGE_WAIT_AFTER;
412 offset = (ext2_loff_t)defrag_data.orig_start * block_size;
413 offset = (offset / pagesize) * pagesize;
415 #ifdef HAVE_SYNC_FILE_RANGE
416 /* Sync file for fadvise process */
417 if (sync_file_range(fd, offset,
418 (ext2_loff_t)pagesize * page_num, sync_flag) < 0)
422 /* Try to release buffer cache which this process used,
423 * then other process can use the released buffer
425 for (i = 0; i < page_num; i++) {
426 if ((vec[i] & 0x1) == 0) {
430 if ((errno = posix_fadvise(fd, offset,
431 pagesize, fadvise_flag)) != 0) {
432 if ((mode_flag & DETAIL) && flag) {
433 perror("\tFailed to fadvise");
444 * check_free_size() - Check if there's enough disk space.
446 * @fd: defrag target file's descriptor.
448 * @blk_count: file blocks.
450 static int check_free_size(int fd, const char *file, ext4_fsblk_t blk_count)
452 ext4_fsblk_t free_blk_count;
453 struct statfs64 fsbuf;
455 if (fstatfs64(fd, &fsbuf) < 0) {
456 if (mode_flag & DETAIL) {
457 PRINT_FILE_NAME(file);
458 PRINT_ERR_MSG_WITH_ERRNO(
459 "Failed to get filesystem information");
464 /* Compute free space for root and normal user separately */
465 if (current_uid == ROOT_UID)
466 free_blk_count = fsbuf.f_bfree;
468 free_blk_count = fsbuf.f_bavail;
470 if (free_blk_count >= blk_count)
477 * file_frag_count() - Get file fragment count.
479 * @fd: defrag target file's descriptor.
481 static int file_frag_count(int fd)
484 struct fiemap fiemap_buf;
486 /* When fm_extent_count is 0,
487 * ioctl just get file fragment count.
489 memset(&fiemap_buf, 0, sizeof(struct fiemap));
490 fiemap_buf.fm_start = 0;
491 fiemap_buf.fm_length = FIEMAP_MAX_OFFSET;
492 fiemap_buf.fm_flags |= FIEMAP_FLAG_SYNC;
494 ret = ioctl(fd, FS_IOC_FIEMAP, &fiemap_buf);
498 return fiemap_buf.fm_mapped_extents;
502 * file_check() - Check file's attributes.
504 * @fd: defrag target file's descriptor.
505 * @buf: a pointer of the struct stat64.
507 * @extents: file extents.
508 * @blk_count: file blocks.
510 static int file_check(int fd, const struct stat64 *buf, const char *file,
511 int extents, ext4_fsblk_t blk_count)
516 /* Write-lock check is more reliable */
517 lock.l_type = F_WRLCK;
519 lock.l_whence = SEEK_SET;
523 ret = check_free_size(fd, file, blk_count);
525 if ((mode_flag & DETAIL) && ret == -ENOSPC) {
526 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
527 " extents: %d -> %d\n", defraged_file_count,
528 total_count, file, extents, extents);
529 IN_FTW_PRINT_ERR_MSG(
530 "Defrag size is larger than filesystem's free space");
535 /* Access authority */
536 if (current_uid != ROOT_UID &&
537 buf->st_uid != current_uid) {
538 if (mode_flag & DETAIL) {
539 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
540 " extents: %d -> %d\n", defraged_file_count,
541 total_count, file, extents, extents);
542 IN_FTW_PRINT_ERR_MSG(
543 "File is not current user's file"
544 " or current user is not root");
550 if (fcntl(fd, F_GETLK, &lock) < 0) {
551 if (mode_flag & DETAIL) {
552 PRINT_FILE_NAME(file);
553 PRINT_ERR_MSG_WITH_ERRNO(
554 "Failed to get lock information");
557 } else if (lock.l_type != F_UNLCK) {
558 if (mode_flag & DETAIL) {
559 PRINT_FILE_NAME(file);
560 IN_FTW_PRINT_ERR_MSG("File has been locked");
569 * insert_extent_by_logical() - Sequentially insert extent by logical.
571 * @ext_list_head: the head of logical extent list.
572 * @ext: the extent element which will be inserted.
574 static int insert_extent_by_logical(struct fiemap_extent_list **ext_list_head,
575 struct fiemap_extent_list *ext)
577 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
583 if (*ext_list_head == NULL) {
584 (*ext_list_head) = ext;
585 (*ext_list_head)->prev = *ext_list_head;
586 (*ext_list_head)->next = *ext_list_head;
590 if (ext->data.logical <= ext_list_tmp->data.logical) {
591 /* Insert before head */
592 if (ext_list_tmp->data.logical <
593 ext->data.logical + ext->data.len)
597 *ext_list_head = ext;
599 /* Insert into the middle or last of the list */
601 if (ext->data.logical < ext_list_tmp->data.logical)
603 ext_list_tmp = ext_list_tmp->next;
604 } while (ext_list_tmp != (*ext_list_head));
605 if (ext->data.logical <
606 ext_list_tmp->prev->data.logical +
607 ext_list_tmp->prev->data.len)
611 if (ext_list_tmp != *ext_list_head &&
612 ext_list_tmp->data.logical <
613 ext->data.logical + ext->data.len)
617 ext_list_tmp = ext_list_tmp->prev;
618 /* Insert "ext" after "ext_list_tmp" */
619 insert(ext_list_tmp, ext);
627 * insert_extent_by_physical() - Sequentially insert extent by physical.
629 * @ext_list_head: the head of physical extent list.
630 * @ext: the extent element which will be inserted.
632 static int insert_extent_by_physical(struct fiemap_extent_list **ext_list_head,
633 struct fiemap_extent_list *ext)
635 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
641 if (*ext_list_head == NULL) {
642 (*ext_list_head) = ext;
643 (*ext_list_head)->prev = *ext_list_head;
644 (*ext_list_head)->next = *ext_list_head;
648 if (ext->data.physical <= ext_list_tmp->data.physical) {
649 /* Insert before head */
650 if (ext_list_tmp->data.physical <
651 ext->data.physical + ext->data.len)
655 *ext_list_head = ext;
657 /* Insert into the middle or last of the list */
659 if (ext->data.physical < ext_list_tmp->data.physical)
661 ext_list_tmp = ext_list_tmp->next;
662 } while (ext_list_tmp != (*ext_list_head));
663 if (ext->data.physical <
664 ext_list_tmp->prev->data.physical +
665 ext_list_tmp->prev->data.len)
669 if (ext_list_tmp != *ext_list_head &&
670 ext_list_tmp->data.physical <
671 ext->data.physical + ext->data.len)
675 ext_list_tmp = ext_list_tmp->prev;
676 /* Insert "ext" after "ext_list_tmp" */
677 insert(ext_list_tmp, ext);
685 * insert_exts_group() - Insert a exts_group.
687 * @ext_group_head: the head of a exts_group list.
688 * @exts_group: the exts_group element which will be inserted.
690 static int insert_exts_group(struct fiemap_extent_group **ext_group_head,
691 struct fiemap_extent_group *exts_group)
693 struct fiemap_extent_group *ext_group_tmp = NULL;
695 if (exts_group == NULL) {
700 /* Initialize list */
701 if (*ext_group_head == NULL) {
702 (*ext_group_head) = exts_group;
703 (*ext_group_head)->prev = *ext_group_head;
704 (*ext_group_head)->next = *ext_group_head;
708 ext_group_tmp = (*ext_group_head)->prev;
709 insert(ext_group_tmp, exts_group);
715 * join_extents() - Find continuous region(exts_group).
717 * @ext_list_head: the head of the extent list.
718 * @ext_group_head: the head of the target exts_group list.
720 static int join_extents(struct fiemap_extent_list *ext_list_head,
721 struct fiemap_extent_group **ext_group_head)
723 __u64 len = ext_list_head->data.len;
724 struct fiemap_extent_list *ext_list_start = ext_list_head;
725 struct fiemap_extent_list *ext_list_tmp = ext_list_head->next;
728 struct fiemap_extent_group *ext_group_tmp = NULL;
730 /* This extent and previous extent are not continuous,
731 * so, all previous extents are treated as an extent group.
733 if ((ext_list_tmp->prev->data.logical +
734 ext_list_tmp->prev->data.len)
735 != ext_list_tmp->data.logical) {
737 malloc(sizeof(struct fiemap_extent_group));
738 if (ext_group_tmp == NULL)
741 memset(ext_group_tmp, 0,
742 sizeof(struct fiemap_extent_group));
743 ext_group_tmp->len = len;
744 ext_group_tmp->start = ext_list_start;
745 ext_group_tmp->end = ext_list_tmp->prev;
747 if (insert_exts_group(ext_group_head,
748 ext_group_tmp) < 0) {
752 ext_list_start = ext_list_tmp;
753 len = ext_list_tmp->data.len;
754 ext_list_tmp = ext_list_tmp->next;
758 /* This extent and previous extent are continuous,
759 * so, they belong to the same extent group, and we check
760 * if the next extent belongs to the same extent group.
762 len += ext_list_tmp->data.len;
763 ext_list_tmp = ext_list_tmp->next;
764 } while (ext_list_tmp != ext_list_head->next);
770 * get_file_extents() - Get file's extent list.
772 * @fd: defrag target file's descriptor.
773 * @ext_list_head: the head of the extent list.
775 static int get_file_extents(int fd, struct fiemap_extent_list **ext_list_head)
779 int ext_buf_size, fie_buf_size;
781 struct fiemap *fiemap_buf = NULL;
782 struct fiemap_extent *ext_buf = NULL;
783 struct fiemap_extent_list *ext_list = NULL;
785 /* Convert units, in bytes.
786 * Be careful : now, physical block number in extent is 48bit,
787 * and the maximum blocksize for ext4 is 4K(12bit),
788 * so there is no overflow, but in future it may be changed.
791 /* Alloc space for fiemap */
792 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
793 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
795 fiemap_buf = malloc(fie_buf_size);
796 if (fiemap_buf == NULL)
799 ext_buf = fiemap_buf->fm_extents;
800 memset(fiemap_buf, 0, fie_buf_size);
801 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
802 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
803 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
806 fiemap_buf->fm_start = pos;
807 memset(ext_buf, 0, ext_buf_size);
808 ret = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
809 if (ret < 0 || fiemap_buf->fm_mapped_extents == 0)
811 for (i = 0; i < fiemap_buf->fm_mapped_extents; i++) {
813 ext_list = malloc(sizeof(struct fiemap_extent_list));
814 if (ext_list == NULL)
817 ext_list->data.physical = ext_buf[i].fe_physical
819 ext_list->data.logical = ext_buf[i].fe_logical
821 ext_list->data.len = ext_buf[i].fe_length
824 ret = insert_extent_by_physical(
825 ext_list_head, ext_list);
831 /* Record file's logical offset this time */
832 pos = ext_buf[EXTENT_MAX_COUNT-1].fe_logical +
833 ext_buf[EXTENT_MAX_COUNT-1].fe_length;
835 * If fm_extents array has been filled and
836 * there are extents left, continue to cycle.
838 } while (fiemap_buf->fm_mapped_extents
839 == EXTENT_MAX_COUNT &&
840 !(ext_buf[EXTENT_MAX_COUNT-1].fe_flags
841 & FIEMAP_EXTENT_LAST));
851 * get_logical_count() - Get the file logical extents count.
853 * @logical_list_head: the head of the logical extent list.
855 static int get_logical_count(struct fiemap_extent_list *logical_list_head)
858 struct fiemap_extent_list *ext_list_tmp = logical_list_head;
862 ext_list_tmp = ext_list_tmp->next;
863 } while (ext_list_tmp != logical_list_head);
869 * get_physical_count() - Get the file physical extents count.
871 * @physical_list_head: the head of the physical extent list.
873 static int get_physical_count(struct fiemap_extent_list *physical_list_head)
876 struct fiemap_extent_list *ext_list_tmp = physical_list_head;
879 if ((ext_list_tmp->data.physical + ext_list_tmp->data.len)
880 != ext_list_tmp->next->data.physical ||
881 (ext_list_tmp->data.logical + ext_list_tmp->data.len)
882 != ext_list_tmp->next->data.logical) {
883 /* This extent and next extent are not continuous. */
887 ext_list_tmp = ext_list_tmp->next;
888 } while (ext_list_tmp != physical_list_head);
894 * change_physical_to_logical() - Change list from physical to logical.
896 * @physical_list_head: the head of physical extent list.
897 * @logical_list_head: the head of logical extent list.
899 static int change_physical_to_logical(
900 struct fiemap_extent_list **physical_list_head,
901 struct fiemap_extent_list **logical_list_head)
904 struct fiemap_extent_list *ext_list_tmp = *physical_list_head;
905 struct fiemap_extent_list *ext_list_next = ext_list_tmp->next;
908 if (ext_list_tmp == ext_list_next) {
909 ret = insert_extent_by_logical(
910 logical_list_head, ext_list_tmp);
914 *physical_list_head = NULL;
918 ext_list_tmp->prev->next = ext_list_tmp->next;
919 ext_list_tmp->next->prev = ext_list_tmp->prev;
920 *physical_list_head = ext_list_next;
922 ret = insert_extent_by_logical(
923 logical_list_head, ext_list_tmp);
928 ext_list_tmp = ext_list_next;
929 ext_list_next = ext_list_next->next;
935 /* get_file_blocks() - Get total file blocks.
937 * @ext_list_head: the extent list head of the target file
939 static ext4_fsblk_t get_file_blocks(struct fiemap_extent_list *ext_list_head)
941 ext4_fsblk_t blk_count = 0;
942 struct fiemap_extent_list *ext_list_tmp = ext_list_head;
945 blk_count += ext_list_tmp->data.len;
946 ext_list_tmp = ext_list_tmp->next;
947 } while (ext_list_tmp != ext_list_head);
953 * free_ext() - Free the extent list.
955 * @ext_list_head: the extent list head of which will be free.
957 static void free_ext(struct fiemap_extent_list *ext_list_head)
959 struct fiemap_extent_list *ext_list_tmp = NULL;
961 if (ext_list_head == NULL)
964 while (ext_list_head->next != ext_list_head) {
965 ext_list_tmp = ext_list_head;
966 ext_list_head->prev->next = ext_list_head->next;
967 ext_list_head->next->prev = ext_list_head->prev;
968 ext_list_head = ext_list_head->next;
975 * free_exts_group() - Free the exts_group.
977 * @*ext_group_head: the exts_group list head which will be free.
979 static void free_exts_group(struct fiemap_extent_group *ext_group_head)
981 struct fiemap_extent_group *ext_group_tmp = NULL;
983 if (ext_group_head == NULL)
986 while (ext_group_head->next != ext_group_head) {
987 ext_group_tmp = ext_group_head;
988 ext_group_head->prev->next = ext_group_head->next;
989 ext_group_head->next->prev = ext_group_head->prev;
990 ext_group_head = ext_group_head->next;
993 free(ext_group_head);
997 * get_best_count() - Get the file best extents count.
999 * @block_count: the file's physical block count.
1001 static int get_best_count(ext4_fsblk_t block_count)
1004 unsigned int flex_bg_num;
1006 if (blocks_per_group == 0)
1009 if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1010 flex_bg_num = 1U << log_groups_per_flex;
1011 ret = ((block_count - 1) /
1012 ((ext4_fsblk_t)blocks_per_group *
1015 ret = ((block_count - 1) / blocks_per_group) + 1;
1022 * file_statistic() - Get statistic info of the file's fragments.
1024 * @file: the file's name.
1025 * @buf: the pointer of the struct stat64.
1027 * @ftwbuf: the pointer of a struct FTW.
1029 static int file_statistic(const char *file, const struct stat64 *buf,
1030 int flag EXT2FS_ATTR((unused)),
1031 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1035 int now_ext_count, best_ext_count = 0, physical_ext_count;
1037 __u64 size_per_ext = 0;
1039 ext4_fsblk_t blk_count = 0;
1040 char msg_buffer[PATH_MAX + 48];
1041 struct fiemap_extent_list *physical_list_head = NULL;
1042 struct fiemap_extent_list *logical_list_head = NULL;
1044 defraged_file_count++;
1045 if (defraged_file_count > total_count)
1046 total_count = defraged_file_count;
1048 if (mode_flag & DETAIL) {
1049 if (total_count == 1 && regular_count == 1)
1052 printf("[%u/%u]", defraged_file_count, total_count);
1056 if (lost_found_dir[0] != '\0' &&
1057 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1058 if (mode_flag & DETAIL) {
1059 PRINT_FILE_NAME(file);
1060 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND);
1065 if (!S_ISREG(buf->st_mode)) {
1066 if (mode_flag & DETAIL) {
1067 PRINT_FILE_NAME(file);
1068 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG);
1073 /* Access authority */
1074 if (current_uid != ROOT_UID &&
1075 buf->st_uid != current_uid) {
1076 if (mode_flag & DETAIL) {
1077 PRINT_FILE_NAME(file);
1079 "File is not current user's file"
1080 " or current user is not root");
1086 if (buf->st_size == 0) {
1087 if (mode_flag & DETAIL) {
1088 PRINT_FILE_NAME(file);
1089 STATISTIC_ERR_MSG("File size is 0");
1095 if (buf->st_blocks == 0) {
1096 if (mode_flag & DETAIL) {
1097 PRINT_FILE_NAME(file);
1098 STATISTIC_ERR_MSG("File has no blocks");
1103 fd = open64(file, O_RDONLY);
1105 if (mode_flag & DETAIL) {
1106 PRINT_FILE_NAME(file);
1107 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1112 /* Get file's physical extents */
1113 ret = get_file_extents(fd, &physical_list_head);
1115 if (mode_flag & DETAIL) {
1116 PRINT_FILE_NAME(file);
1117 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1122 /* Get the count of file's continuous physical region */
1123 physical_ext_count = get_physical_count(physical_list_head);
1125 /* Change list from physical to logical */
1126 ret = change_physical_to_logical(&physical_list_head,
1127 &logical_list_head);
1129 if (mode_flag & DETAIL) {
1130 PRINT_FILE_NAME(file);
1131 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1136 /* Count file fragments before defrag */
1137 now_ext_count = get_logical_count(logical_list_head);
1139 if (current_uid == ROOT_UID) {
1140 /* Calculate the size per extent */
1141 blk_count = get_file_blocks(logical_list_head);
1143 best_ext_count = get_best_count(blk_count);
1145 /* e4defrag rounds size_per_ext up to a block size boundary */
1146 size_per_ext = blk_count * (buf->st_blksize / 1024) /
1149 ratio = (float)(physical_ext_count - best_ext_count) * 100 /
1152 extents_before_defrag += now_ext_count;
1153 extents_after_defrag += best_ext_count;
1154 files_block_count += blk_count;
1157 if (total_count == 1 && regular_count == 1) {
1159 if (mode_flag & DETAIL) {
1161 struct fiemap_extent_list *ext_list_tmp =
1164 /* Print extents info */
1167 printf("[ext %d]:\tstart %llu:\tlogical "
1168 "%llu:\tlen %llu\n", count,
1169 (unsigned long long)
1170 ext_list_tmp->data.physical,
1171 (unsigned long long)
1172 ext_list_tmp->data.logical,
1173 (unsigned long long)
1174 ext_list_tmp->data.len);
1175 ext_list_tmp = ext_list_tmp->next;
1176 } while (ext_list_tmp != logical_list_head);
1179 printf("%-40s%10s/%-10s%9s\n",
1180 "<File>", "now", "best", "size/ext");
1181 if (current_uid == ROOT_UID) {
1182 if (strlen(file) > 40)
1183 printf("%s\n%50d/%-10d%6llu KB\n",
1184 file, now_ext_count,
1186 (unsigned long long) size_per_ext);
1188 printf("%-40s%10d/%-10d%6llu KB\n",
1189 file, now_ext_count,
1191 (unsigned long long) size_per_ext);
1193 if (strlen(file) > 40)
1194 printf("%s\n%50d/%-10s%7s\n",
1195 file, now_ext_count,
1198 printf("%-40s%10d/%-10s%7s\n",
1199 file, now_ext_count,
1207 if (mode_flag & DETAIL) {
1208 /* Print statistic info */
1209 sprintf(msg_buffer, "[%u/%u]%.*s",
1210 defraged_file_count, total_count,
1212 if (current_uid == ROOT_UID) {
1213 if (strlen(msg_buffer) > 40)
1214 printf("\033[79;0H\033[K%s\n"
1215 "%50d/%-10d%6llu KB\n",
1216 msg_buffer, now_ext_count,
1218 (unsigned long long) size_per_ext);
1220 printf("\033[79;0H\033[K%-40s"
1221 "%10d/%-10d%6llu KB\n",
1222 msg_buffer, now_ext_count,
1224 (unsigned long long) size_per_ext);
1226 if (strlen(msg_buffer) > 40)
1227 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1228 msg_buffer, now_ext_count,
1231 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1232 msg_buffer, now_ext_count,
1237 for (i = 0; i < SHOW_FRAG_FILES; i++) {
1238 if (ratio >= frag_rank[i].ratio) {
1239 for (j = SHOW_FRAG_FILES - 1; j > i; j--) {
1240 memset(&frag_rank[j], 0,
1241 sizeof(struct frag_statistic_ino));
1242 strncpy(frag_rank[j].msg_buffer,
1243 frag_rank[j - 1].msg_buffer,
1244 strnlen(frag_rank[j - 1].msg_buffer,
1246 frag_rank[j].now_count =
1247 frag_rank[j - 1].now_count;
1248 frag_rank[j].best_count =
1249 frag_rank[j - 1].best_count;
1250 frag_rank[j].size_per_ext =
1251 frag_rank[j - 1].size_per_ext;
1252 frag_rank[j].ratio =
1253 frag_rank[j - 1].ratio;
1255 memset(&frag_rank[i], 0,
1256 sizeof(struct frag_statistic_ino));
1257 strncpy(frag_rank[i].msg_buffer, file,
1258 strnlen(file, PATH_MAX));
1259 frag_rank[i].now_count = now_ext_count;
1260 frag_rank[i].best_count = best_ext_count;
1261 frag_rank[i].size_per_ext = size_per_ext;
1262 frag_rank[i].ratio = ratio;
1271 free_ext(physical_list_head);
1272 free_ext(logical_list_head);
1277 * print_progress - Print defrag progress
1280 * @start: logical offset for defrag target file
1281 * @file_size: defrag target filesize
1283 static void print_progress(const char *file, ext2_loff_t start,
1284 ext2_loff_t file_size)
1286 int percent = (start * 100) / file_size;
1287 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1288 defraged_file_count, total_count, file, min(percent, 100));
1295 * call_defrag() - Execute the defrag program.
1297 * @fd: target file descriptor.
1298 * @donor_fd: donor file descriptor.
1299 * @file: target file name.
1300 * @buf: pointer of the struct stat64.
1301 * @ext_list_head: head of the extent list.
1303 static int call_defrag(int fd, int donor_fd, const char *file,
1304 const struct stat64 *buf, struct fiemap_extent_list *ext_list_head)
1306 ext2_loff_t start = 0;
1307 unsigned int page_num;
1308 unsigned char *vec = NULL;
1309 int defraged_ret = 0;
1311 struct move_extent move_data;
1312 struct fiemap_extent_list *ext_list_tmp = NULL;
1314 memset(&move_data, 0, sizeof(struct move_extent));
1315 move_data.donor_fd = donor_fd;
1317 /* Print defrag progress */
1318 print_progress(file, start, buf->st_size);
1320 ext_list_tmp = ext_list_head;
1322 move_data.orig_start = ext_list_tmp->data.logical;
1323 /* Logical offset of orig and donor should be same */
1324 move_data.donor_start = move_data.orig_start;
1325 move_data.len = ext_list_tmp->data.len;
1326 move_data.moved_len = 0;
1328 ret = page_in_core(fd, move_data, &vec, &page_num);
1330 if (mode_flag & DETAIL) {
1332 PRINT_ERR_MSG_WITH_ERRNO(
1333 "Failed to get file map");
1335 printf("\t[ NG ]\n");
1340 /* EXT4_IOC_MOVE_EXT */
1342 ioctl(fd, EXT4_IOC_MOVE_EXT, &move_data);
1345 ret = defrag_fadvise(fd, move_data, vec, page_num);
1351 if (mode_flag & DETAIL) {
1353 PRINT_ERR_MSG_WITH_ERRNO(
1354 "Failed to free page");
1356 printf("\t[ NG ]\n");
1361 if (defraged_ret < 0) {
1362 if (mode_flag & DETAIL) {
1364 PRINT_ERR_MSG_WITH_ERRNO(
1365 "Failed to defrag with "
1366 "EXT4_IOC_MOVE_EXT ioctl");
1367 if (errno == ENOTTY)
1368 printf("\tAt least 2.6.31-rc1 of "
1369 "vanilla kernel is required\n");
1371 printf("\t[ NG ]\n");
1375 /* Adjust logical offset for next ioctl */
1376 move_data.orig_start += move_data.moved_len;
1377 move_data.donor_start = move_data.orig_start;
1379 start = move_data.orig_start * buf->st_blksize;
1381 /* Print defrag progress */
1382 print_progress(file, start, buf->st_size);
1385 if (start >= buf->st_size)
1388 ext_list_tmp = ext_list_tmp->next;
1389 } while (ext_list_tmp != ext_list_head);
1395 * file_defrag() - Check file attributes and call ioctl to defrag.
1397 * @file: the file's name.
1398 * @buf: the pointer of the struct stat64.
1400 * @ftwbuf: the pointer of a struct FTW.
1402 static int file_defrag(const char *file, const struct stat64 *buf,
1403 int flag EXT2FS_ATTR((unused)),
1404 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1410 int file_frags_start, file_frags_end;
1411 int orig_physical_cnt, donor_physical_cnt = 0;
1412 char tmp_inode_name[PATH_MAX + 8];
1413 ext4_fsblk_t blk_count = 0;
1414 struct fiemap_extent_list *orig_list_physical = NULL;
1415 struct fiemap_extent_list *orig_list_logical = NULL;
1416 struct fiemap_extent_list *donor_list_physical = NULL;
1417 struct fiemap_extent_list *donor_list_logical = NULL;
1418 struct fiemap_extent_group *orig_group_head = NULL;
1419 struct fiemap_extent_group *orig_group_tmp = NULL;
1421 defraged_file_count++;
1422 if (defraged_file_count > total_count)
1423 total_count = defraged_file_count;
1425 if (mode_flag & DETAIL) {
1426 printf("[%u/%u]", defraged_file_count, total_count);
1430 if (lost_found_dir[0] != '\0' &&
1431 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1432 if (mode_flag & DETAIL) {
1433 PRINT_FILE_NAME(file);
1434 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1439 if (!S_ISREG(buf->st_mode)) {
1440 if (mode_flag & DETAIL) {
1441 PRINT_FILE_NAME(file);
1442 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1448 if (buf->st_size == 0) {
1449 if (mode_flag & DETAIL) {
1450 PRINT_FILE_NAME(file);
1451 IN_FTW_PRINT_ERR_MSG("File size is 0");
1457 if (buf->st_blocks == 0) {
1458 if (mode_flag & DETAIL) {
1459 PRINT_FILE_NAME(file);
1460 STATISTIC_ERR_MSG("File has no blocks");
1465 fd = open64(file, O_RDWR);
1467 if (mode_flag & DETAIL) {
1468 PRINT_FILE_NAME(file);
1469 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1474 /* Get file's extents */
1475 ret = get_file_extents(fd, &orig_list_physical);
1477 if (mode_flag & DETAIL) {
1478 PRINT_FILE_NAME(file);
1479 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1484 /* Get the count of file's continuous physical region */
1485 orig_physical_cnt = get_physical_count(orig_list_physical);
1487 /* Change list from physical to logical */
1488 ret = change_physical_to_logical(&orig_list_physical,
1489 &orig_list_logical);
1491 if (mode_flag & DETAIL) {
1492 PRINT_FILE_NAME(file);
1493 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1498 /* Count file fragments before defrag */
1499 file_frags_start = get_logical_count(orig_list_logical);
1501 blk_count = get_file_blocks(orig_list_logical);
1502 if (file_check(fd, buf, file, file_frags_start, blk_count) < 0)
1505 if (fsync(fd) < 0) {
1506 if (mode_flag & DETAIL) {
1507 PRINT_FILE_NAME(file);
1508 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1513 best = get_best_count(blk_count);
1515 if (file_frags_start <= best)
1516 goto check_improvement;
1518 /* Combine extents to group */
1519 ret = join_extents(orig_list_logical, &orig_group_head);
1521 if (mode_flag & DETAIL) {
1522 PRINT_FILE_NAME(file);
1523 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1528 /* Create donor inode */
1529 memset(tmp_inode_name, 0, PATH_MAX + 8);
1530 sprintf(tmp_inode_name, "%.*s.defrag",
1531 (int)strnlen(file, PATH_MAX), file);
1532 donor_fd = open64(tmp_inode_name, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
1534 if (mode_flag & DETAIL) {
1535 PRINT_FILE_NAME(file);
1536 if (errno == EEXIST)
1537 PRINT_ERR_MSG_WITH_ERRNO(
1538 "File is being defraged by other program");
1540 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1545 /* Unlink donor inode */
1546 ret = unlink(tmp_inode_name);
1548 if (mode_flag & DETAIL) {
1549 PRINT_FILE_NAME(file);
1550 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1555 /* Allocate space for donor inode */
1556 orig_group_tmp = orig_group_head;
1558 ret = fallocate(donor_fd, 0,
1559 (ext2_loff_t)orig_group_tmp->start->data.logical * block_size,
1560 (ext2_loff_t)orig_group_tmp->len * block_size);
1562 if (mode_flag & DETAIL) {
1563 PRINT_FILE_NAME(file);
1564 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1569 orig_group_tmp = orig_group_tmp->next;
1570 } while (orig_group_tmp != orig_group_head);
1572 /* Get donor inode's extents */
1573 ret = get_file_extents(donor_fd, &donor_list_physical);
1575 if (mode_flag & DETAIL) {
1576 PRINT_FILE_NAME(file);
1577 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1582 /* Calculate donor inode's continuous physical region */
1583 donor_physical_cnt = get_physical_count(donor_list_physical);
1585 /* Change donor extent list from physical to logical */
1586 ret = change_physical_to_logical(&donor_list_physical,
1587 &donor_list_logical);
1589 if (mode_flag & DETAIL) {
1590 PRINT_FILE_NAME(file);
1591 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1597 if (mode_flag & DETAIL) {
1598 if (file_frags_start != 1)
1599 frag_files_before_defrag++;
1601 extents_before_defrag += file_frags_start;
1604 if (file_frags_start <= best ||
1605 orig_physical_cnt <= donor_physical_cnt) {
1606 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1607 defraged_file_count, total_count, file, 100);
1608 if (mode_flag & DETAIL)
1609 printf(" extents: %d -> %d",
1610 file_frags_start, file_frags_start);
1612 printf("\t[ OK ]\n");
1615 if (file_frags_start != 1)
1616 frag_files_after_defrag++;
1618 extents_after_defrag += file_frags_start;
1622 /* Defrag the file */
1623 ret = call_defrag(fd, donor_fd, file, buf, donor_list_logical);
1625 /* Count file fragments after defrag and print extents info */
1626 if (mode_flag & DETAIL) {
1627 file_frags_end = file_frag_count(fd);
1628 if (file_frags_end < 0) {
1630 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO);
1634 if (file_frags_end != 1)
1635 frag_files_after_defrag++;
1637 extents_after_defrag += file_frags_end;
1642 printf(" extents: %d -> %d",
1643 file_frags_start, file_frags_end);
1650 printf("\t[ OK ]\n");
1658 free_ext(orig_list_physical);
1659 free_ext(orig_list_logical);
1660 free_ext(donor_list_physical);
1661 free_exts_group(orig_group_head);
1666 * main() - Ext4 online defrag.
1668 * @argc: the number of parameter.
1669 * @argv[]: the pointer array of parameter.
1671 int main(int argc, char *argv[])
1675 int flags = FTW_PHYS | FTW_MOUNT;
1677 int mount_dir_len = 0;
1678 int success_flag = 0;
1679 char dir_name[PATH_MAX + 1];
1680 char dev_name[PATH_MAX + 1];
1682 ext2_filsys fs = NULL;
1684 printf("e4defrag %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1686 /* Parse arguments */
1690 while ((opt = getopt(argc, argv, "vc")) != EOF) {
1693 mode_flag |= DETAIL;
1696 mode_flag |= STATISTIC;
1706 current_uid = getuid();
1709 for (i = optind; i < argc; i++) {
1713 frag_files_before_defrag = 0;
1714 frag_files_after_defrag = 0;
1715 extents_before_defrag = 0;
1716 extents_after_defrag = 0;
1717 defraged_file_count = 0;
1718 files_block_count = 0;
1719 blocks_per_group = 0;
1720 feature_incompat = 0;
1721 log_groups_per_flex = 0;
1723 memset(dir_name, 0, PATH_MAX + 1);
1724 memset(dev_name, 0, PATH_MAX + 1);
1725 memset(lost_found_dir, 0, PATH_MAX + 1);
1726 memset(frag_rank, 0,
1727 sizeof(struct frag_statistic_ino) * SHOW_FRAG_FILES);
1729 if ((mode_flag & STATISTIC) && i > optind)
1732 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1733 PRINT_ERR_MSG("Endian's type is not big/little endian");
1734 PRINT_FILE_NAME(argv[i]);
1738 if (lstat64(argv[i], &buf) < 0) {
1739 perror(NGMSG_FILE_INFO);
1740 PRINT_FILE_NAME(argv[i]);
1744 /* Handle i.e. lvm device symlinks */
1745 if (S_ISLNK(buf.st_mode)) {
1748 if (stat64(argv[i], &buf2) == 0 &&
1749 S_ISBLK(buf2.st_mode))
1753 if (S_ISBLK(buf.st_mode)) {
1755 strncpy(dev_name, argv[i], strnlen(argv[i], PATH_MAX));
1756 if (get_mount_point(argv[i], dir_name, PATH_MAX) < 0)
1758 if (lstat64(dir_name, &buf) < 0) {
1759 perror(NGMSG_FILE_INFO);
1760 PRINT_FILE_NAME(argv[i]);
1764 if (!(mode_flag & STATISTIC))
1765 printf("ext4 defragmentation for device(%s)\n",
1767 } else if (S_ISDIR(buf.st_mode)) {
1769 if (access(argv[i], R_OK) < 0) {
1774 strncpy(dir_name, argv[i], strnlen(argv[i], PATH_MAX));
1775 } else if (S_ISREG(buf.st_mode)) {
1777 arg_type = FILENAME;
1779 /* Irregular file */
1780 PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1781 PRINT_FILE_NAME(argv[i]);
1786 block_size = buf.st_blksize;
1789 * filesystem type checked in get_mount_point()
1791 if (arg_type == FILENAME || arg_type == DIRNAME) {
1792 if (is_ext4(argv[i], dev_name) < 0)
1794 if (realpath(argv[i], dir_name) == NULL) {
1795 perror("Couldn't get full path");
1796 PRINT_FILE_NAME(argv[i]);
1801 if (current_uid == ROOT_UID) {
1802 /* Get super block info */
1803 ret = ext2fs_open(dev_name, EXT2_FLAG_64BITS, 0,
1804 block_size, unix_io_manager, &fs);
1806 if (mode_flag & DETAIL)
1808 "Warning: couldn't get file "
1809 "system details for %s: %s\n",
1810 dev_name, error_message(ret));
1812 blocks_per_group = fs->super->s_blocks_per_group;
1813 feature_incompat = fs->super->s_feature_incompat;
1814 log_groups_per_flex = fs->super->s_log_groups_per_flex;
1815 ext2fs_close_free(&fs);
1822 if (!(mode_flag & STATISTIC))
1823 printf("ext4 defragmentation "
1824 "for directory(%s)\n", argv[i]);
1826 mount_dir_len = strnlen(lost_found_dir, PATH_MAX);
1828 strncat(lost_found_dir, "/lost+found",
1829 PATH_MAX - strnlen(lost_found_dir, PATH_MAX));
1831 /* Not the case("e4defrag mount_point_dir") */
1832 if (dir_name[mount_dir_len] != '\0') {
1834 * "e4defrag mount_point_dir/lost+found"
1835 * or "e4defrag mount_point_dir/lost+found/"
1837 if (strncmp(lost_found_dir, dir_name,
1838 strnlen(lost_found_dir,
1840 (dir_name[strnlen(lost_found_dir,
1841 PATH_MAX)] == '\0' ||
1842 dir_name[strnlen(lost_found_dir,
1843 PATH_MAX)] == '/')) {
1844 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1845 PRINT_FILE_NAME(argv[i]);
1849 /* "e4defrag mount_point_dir/else_dir" */
1850 memset(lost_found_dir, 0, PATH_MAX + 1);
1854 if (arg_type == DEVNAME) {
1855 strcpy(lost_found_dir, dir_name);
1856 strncat(lost_found_dir, "/lost+found/",
1857 PATH_MAX - strlen(lost_found_dir));
1860 nftw64(dir_name, calc_entry_counts, FTW_OPEN_FD, flags);
1862 if (mode_flag & STATISTIC) {
1863 if (mode_flag & DETAIL)
1864 printf("%-40s%10s/%-10s%9s\n",
1865 "<File>", "now", "best", "size/ext");
1867 if (!(mode_flag & DETAIL) &&
1868 current_uid != ROOT_UID) {
1874 nftw64(dir_name, file_statistic,
1875 FTW_OPEN_FD, flags);
1877 if (succeed_cnt != 0 &&
1878 current_uid == ROOT_UID) {
1879 if (mode_flag & DETAIL)
1881 printf("%-40s%10s/%-10s%9s\n",
1882 "<Fragmented files>", "now",
1883 "best", "size/ext");
1884 for (j = 0; j < SHOW_FRAG_FILES; j++) {
1885 if (strlen(frag_rank[j].
1887 printf("%d. %s\n%50d/"
1890 frag_rank[j].msg_buffer,
1891 frag_rank[j].now_count,
1892 frag_rank[j].best_count,
1893 (unsigned long long)
1896 } else if (strlen(frag_rank[j].
1898 printf("%d. %-37s%10d/"
1901 frag_rank[j].msg_buffer,
1902 frag_rank[j].now_count,
1903 frag_rank[j].best_count,
1904 (unsigned long long)
1913 /* File tree walk */
1914 nftw64(dir_name, file_defrag, FTW_OPEN_FD, flags);
1915 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt,
1917 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1918 total_count - succeed_cnt, total_count);
1919 if (mode_flag & DETAIL) {
1920 printf("\tTotal extents:\t\t\t%4d->%d\n",
1921 extents_before_defrag,
1922 extents_after_defrag);
1923 printf("\tFragmented percentage:\t\t"
1924 "%3llu%%->%llu%%\n",
1925 !regular_count ? 0 :
1926 ((unsigned long long)
1927 frag_files_before_defrag * 100) /
1929 !regular_count ? 0 :
1930 ((unsigned long long)
1931 frag_files_after_defrag * 100) /
1938 strncat(lost_found_dir, "/lost+found/",
1939 PATH_MAX - strnlen(lost_found_dir,
1941 if (strncmp(lost_found_dir, dir_name,
1942 strnlen(lost_found_dir,
1944 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1945 PRINT_FILE_NAME(argv[i]);
1949 if (mode_flag & STATISTIC) {
1950 file_statistic(argv[i], &buf, FTW_F, NULL);
1953 printf("ext4 defragmentation for %s\n",
1955 /* Defrag single file process */
1956 file_defrag(argv[i], &buf, FTW_F, NULL);
1957 if (succeed_cnt != 0)
1958 printf(" Success:\t\t\t[1/1]\n");
1960 printf(" Success:\t\t\t[0/1]\n");
1965 if (succeed_cnt != 0)
1967 if (mode_flag & STATISTIC) {
1968 if (current_uid != ROOT_UID) {
1974 if (mode_flag & DETAIL)
1977 if (arg_type == DEVNAME)
1978 printf(" In this device(%s), "
1979 "none can be defragmented.\n", argv[i]);
1980 else if (arg_type == DIRNAME)
1981 printf(" In this directory(%s), "
1982 "none can be defragmented.\n", argv[i]);
1984 printf(" This file(%s) "
1985 "can't be defragmented.\n", argv[i]);
1987 float files_ratio = 0.0;
1989 __u64 size_per_ext = files_block_count *
1990 (buf.st_blksize / 1024) /
1991 extents_before_defrag;
1992 files_ratio = (float)(extents_before_defrag -
1993 extents_after_defrag) *
1994 100 / files_block_count;
1995 score = CALC_SCORE(files_ratio);
1996 printf("\n Total/best extents\t\t\t\t%d/%d\n"
1997 " Average size per extent"
1999 " Fragmentation score\t\t\t\t%.0f\n",
2000 extents_before_defrag,
2001 extents_after_defrag,
2002 (unsigned long long) size_per_ext, score);
2003 printf(" [0-30 no problem:"
2004 " 31-55 a little bit fragmented:"
2005 " 56- needs defrag]\n");
2007 if (arg_type == DEVNAME)
2008 printf(" This device (%s) ", argv[i]);
2009 else if (arg_type == DIRNAME)
2010 printf(" This directory (%s) ",
2013 printf(" This file (%s) ", argv[i]);
2015 if (score > BOUND_SCORE)
2016 printf("needs defragmentation.\n");
2018 printf("does not need "
2019 "defragmentation.\n");