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>
38 #include <sys/ioctl.h>
39 #include <ext2fs/fiemap.h>
42 #include <sys/statfs.h>
43 #include <sys/syscall.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 char lost_found_dir[PATH_MAX + 1];
169 int extents_before_defrag;
170 int extents_after_defrag;
172 unsigned int current_uid;
173 unsigned int defraged_file_count;
174 unsigned int frag_files_before_defrag;
175 unsigned int frag_files_after_defrag;
176 unsigned int regular_count;
177 unsigned int succeed_cnt;
178 unsigned int total_count;
179 __u8 log_groups_per_flex;
180 __u32 blocks_per_group;
181 __u32 feature_incompat;
182 ext4_fsblk_t files_block_count;
183 struct frag_statistic_ino frag_rank[SHOW_FRAG_FILES];
186 /* Local definitions of some syscalls glibc may not yet have */
188 #ifndef HAVE_POSIX_FADVISE
189 #warning Using locally defined posix_fadvise interface.
191 #ifndef __NR_fadvise64_64
192 #error Your kernel headers dont define __NR_fadvise64_64
196 * fadvise() - Give advice about file access.
198 * @fd: defrag target file's descriptor.
199 * @offset: file offset.
201 * @advise: process flag.
203 static int posix_fadvise(int fd, loff_t offset, size_t len, int advise)
205 return syscall(__NR_fadvise64_64, fd, offset, len, advise);
207 #endif /* ! HAVE_FADVISE64_64 */
209 #ifndef HAVE_SYNC_FILE_RANGE
210 #warning Using locally defined sync_file_range interface.
212 #ifndef __NR_sync_file_range
213 #ifndef __NR_sync_file_range2 /* ppc */
214 #error Your kernel headers dont define __NR_sync_file_range
219 * sync_file_range() - Sync file region.
221 * @fd: defrag target file's descriptor.
222 * @offset: file offset.
223 * @length: area length.
224 * @flag: process flag.
226 int sync_file_range(int fd, loff_t offset, loff_t length, unsigned int flag)
228 #ifdef __NR_sync_file_range
229 return syscall(__NR_sync_file_range, fd, offset, length, flag);
231 return syscall(__NR_sync_file_range2, fd, flag, offset, length);
234 #endif /* ! HAVE_SYNC_FILE_RANGE */
236 #ifndef HAVE_FALLOCATE64
237 #warning Using locally defined fallocate syscall interface.
239 #ifndef __NR_fallocate
240 #error Your kernel headers dont define __NR_fallocate
244 * fallocate64() - Manipulate file space.
246 * @fd: defrag target file's descriptor.
247 * @mode: process flag.
248 * @offset: file offset.
251 static int fallocate64(int fd, int mode, loff_t offset, loff_t len)
253 return syscall(__NR_fallocate, fd, mode, offset, len);
255 #endif /* ! HAVE_FALLOCATE */
258 * get_mount_point() - Get device's mount point.
260 * @devname: the device's name.
261 * @mount_point: the mount point.
262 * @dir_path_len: the length of directory.
264 static int get_mount_point(const char *devname, char *mount_point,
267 /* Refer to /etc/mtab */
268 const char *mtab = MOUNTED;
270 struct mntent *mnt = NULL;
273 if (stat64(devname, &sb) < 0) {
274 perror(NGMSG_FILE_INFO);
275 PRINT_FILE_NAME(devname);
279 fp = setmntent(mtab, "r");
281 perror("Couldn't access /etc/mtab");
285 while ((mnt = getmntent(fp)) != NULL) {
289 * To handle device symlinks, we see if the
290 * device number matches, not the name
292 if (stat64(mnt->mnt_fsname, &ms) < 0)
294 if (sb.st_rdev != ms.st_rdev)
298 if (strcmp(mnt->mnt_type, FS_EXT4) == 0) {
299 strncpy(mount_point, mnt->mnt_dir,
303 PRINT_ERR_MSG(NGMSG_EXT4);
307 PRINT_ERR_MSG("Filesystem is not mounted");
312 * is_ext4() - Whether on an ext4 filesystem.
314 * @file: the file's name.
316 static int is_ext4(const char *file, char *devname)
321 char *mnt_type = NULL;
322 /* Refer to /etc/mtab */
323 const char *mtab = MOUNTED;
324 char file_path[PATH_MAX + 1];
325 struct mntent *mnt = NULL;
326 struct statfs64 fsbuf;
329 if (realpath(file, file_path) == NULL) {
330 perror("Couldn't get full path");
331 PRINT_FILE_NAME(file);
335 if (statfs64(file_path, &fsbuf) < 0) {
336 perror("Failed to get filesystem information");
337 PRINT_FILE_NAME(file);
341 if (fsbuf.f_type != EXT4_SUPER_MAGIC) {
342 PRINT_ERR_MSG(NGMSG_EXT4);
346 fp = setmntent(mtab, "r");
348 perror("Couldn't access /etc/mtab");
352 while ((mnt = getmntent(fp)) != NULL) {
353 if (mnt->mnt_fsname[0] != '/')
355 len = strlen(mnt->mnt_dir);
356 ret = memcmp(file_path, mnt->mnt_dir, len);
365 mnt_type = realloc(mnt_type, strlen(mnt->mnt_type) + 1);
366 if (mnt_type == NULL) {
370 memset(mnt_type, 0, strlen(mnt->mnt_type) + 1);
371 strncpy(mnt_type, mnt->mnt_type, strlen(mnt->mnt_type));
372 strncpy(lost_found_dir, mnt->mnt_dir, PATH_MAX);
373 strncpy(devname, mnt->mnt_fsname, strlen(mnt->mnt_fsname) + 1);
377 if (strcmp(mnt_type, FS_EXT4) == 0) {
382 PRINT_ERR_MSG(NGMSG_EXT4);
388 * calc_entry_counts() - Calculate file counts.
393 * @ftwbuf: the pointer of a struct FTW.
395 static int calc_entry_counts(const char *file EXT2FS_ATTR((unused)),
396 const struct stat64 *buf, int flag EXT2FS_ATTR((unused)),
397 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
399 if (S_ISREG(buf->st_mode))
408 * page_in_core() - Get information on whether pages are in core.
410 * @fd: defrag target file's descriptor.
411 * @defrag_data: data used for defrag.
412 * @vec: page state array.
413 * @page_num: page number.
415 static int page_in_core(int fd, struct move_extent defrag_data,
416 unsigned char **vec, unsigned int *page_num)
420 loff_t offset, end_offset, length;
422 if (vec == NULL || *vec != NULL)
425 pagesize = sysconf(_SC_PAGESIZE);
428 /* In mmap, offset should be a multiple of the page size */
429 offset = (loff_t)defrag_data.orig_start * block_size;
430 length = (loff_t)defrag_data.len * block_size;
431 end_offset = offset + length;
432 /* Round the offset down to the nearest multiple of pagesize */
433 offset = (offset / pagesize) * pagesize;
434 length = end_offset - offset;
436 page = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, offset);
437 if (page == MAP_FAILED)
441 *page_num = (length + pagesize - 1) / pagesize;
442 *vec = (unsigned char *)calloc(*page_num, 1);
446 /* Get information on whether pages are in core */
447 if (mincore(page, (size_t)length, *vec) == -1 ||
448 munmap(page, length) == -1) {
457 * defrag_fadvise() - Predeclare an access pattern for file data.
459 * @fd: defrag target file's descriptor.
460 * @defrag_data: data used for defrag.
461 * @vec: page state array.
462 * @page_num: page number.
464 static int defrag_fadvise(int fd, struct move_extent defrag_data,
465 unsigned char *vec, unsigned int page_num)
468 long pagesize = sysconf(_SC_PAGESIZE);
469 int fadvise_flag = POSIX_FADV_DONTNEED;
470 int sync_flag = SYNC_FILE_RANGE_WAIT_BEFORE |
471 SYNC_FILE_RANGE_WRITE |
472 SYNC_FILE_RANGE_WAIT_AFTER;
476 offset = (loff_t)defrag_data.orig_start * block_size;
477 offset = (offset / pagesize) * pagesize;
479 /* Sync file for fadvise process */
480 if (sync_file_range(fd, offset,
481 (loff_t)pagesize * page_num, sync_flag) < 0)
484 /* Try to release buffer cache which this process used,
485 * then other process can use the released buffer
487 for (i = 0; i < page_num; i++) {
488 if ((vec[i] & 0x1) == 0) {
492 if (posix_fadvise(fd, offset, pagesize, fadvise_flag) < 0) {
493 if ((mode_flag & DETAIL) && flag) {
494 perror("\tFailed to fadvise");
505 * check_free_size() - Check if there's enough disk space.
507 * @fd: defrag target file's descriptor.
509 * @blk_count: file blocks.
511 static int check_free_size(int fd, const char *file, ext4_fsblk_t blk_count)
513 ext4_fsblk_t free_blk_count;
514 struct statfs64 fsbuf;
516 if (fstatfs64(fd, &fsbuf) < 0) {
517 if (mode_flag & DETAIL) {
518 PRINT_FILE_NAME(file);
519 PRINT_ERR_MSG_WITH_ERRNO(
520 "Failed to get filesystem information");
525 /* Compute free space for root and normal user separately */
526 if (current_uid == ROOT_UID)
527 free_blk_count = fsbuf.f_bfree;
529 free_blk_count = fsbuf.f_bavail;
531 if (free_blk_count >= blk_count)
538 * file_frag_count() - Get file fragment count.
540 * @fd: defrag target file's descriptor.
542 static int file_frag_count(int fd)
545 struct fiemap fiemap_buf;
547 /* When fm_extent_count is 0,
548 * ioctl just get file fragment count.
550 memset(&fiemap_buf, 0, sizeof(struct fiemap));
551 fiemap_buf.fm_start = 0;
552 fiemap_buf.fm_length = FIEMAP_MAX_OFFSET;
553 fiemap_buf.fm_flags |= FIEMAP_FLAG_SYNC;
555 ret = ioctl(fd, FS_IOC_FIEMAP, &fiemap_buf);
559 return fiemap_buf.fm_mapped_extents;
563 * file_check() - Check file's attributes.
565 * @fd: defrag target file's descriptor.
566 * @buf: a pointer of the struct stat64.
568 * @extents: file extents.
569 * @blk_count: file blocks.
571 static int file_check(int fd, const struct stat64 *buf, const char *file,
572 int extents, ext4_fsblk_t blk_count)
577 /* Write-lock check is more reliable */
578 lock.l_type = F_WRLCK;
580 lock.l_whence = SEEK_SET;
584 ret = check_free_size(fd, file, blk_count);
586 if ((mode_flag & DETAIL) && ret == -ENOSPC) {
587 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
588 " extents: %d -> %d\n", defraged_file_count,
589 total_count, file, extents, extents);
590 IN_FTW_PRINT_ERR_MSG(
591 "Defrag size is larger than filesystem's free space");
596 /* Access authority */
597 if (current_uid != ROOT_UID &&
598 buf->st_uid != current_uid) {
599 if (mode_flag & DETAIL) {
600 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
601 " extents: %d -> %d\n", defraged_file_count,
602 total_count, file, extents, extents);
603 IN_FTW_PRINT_ERR_MSG(
604 "File is not current user's file"
605 " or current user is not root");
611 if (fcntl(fd, F_GETLK, &lock) < 0) {
612 if (mode_flag & DETAIL) {
613 PRINT_FILE_NAME(file);
614 PRINT_ERR_MSG_WITH_ERRNO(
615 "Failed to get lock information");
618 } else if (lock.l_type != F_UNLCK) {
619 if (mode_flag & DETAIL) {
620 PRINT_FILE_NAME(file);
621 IN_FTW_PRINT_ERR_MSG("File has been locked");
630 * insert_extent_by_logical() - Sequentially insert extent by logical.
632 * @ext_list_head: the head of logical extent list.
633 * @ext: the extent element which will be inserted.
635 static int insert_extent_by_logical(struct fiemap_extent_list **ext_list_head,
636 struct fiemap_extent_list *ext)
638 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
644 if (*ext_list_head == NULL) {
645 (*ext_list_head) = ext;
646 (*ext_list_head)->prev = *ext_list_head;
647 (*ext_list_head)->next = *ext_list_head;
651 if (ext->data.logical <= ext_list_tmp->data.logical) {
652 /* Insert before head */
653 if (ext_list_tmp->data.logical <
654 ext->data.logical + ext->data.len)
658 *ext_list_head = ext;
660 /* Insert into the middle or last of the list */
662 if (ext->data.logical < ext_list_tmp->data.logical)
664 ext_list_tmp = ext_list_tmp->next;
665 } while (ext_list_tmp != (*ext_list_head));
666 if (ext->data.logical <
667 ext_list_tmp->prev->data.logical +
668 ext_list_tmp->prev->data.len)
672 if (ext_list_tmp != *ext_list_head &&
673 ext_list_tmp->data.logical <
674 ext->data.logical + ext->data.len)
678 ext_list_tmp = ext_list_tmp->prev;
679 /* Insert "ext" after "ext_list_tmp" */
680 insert(ext_list_tmp, ext);
688 * insert_extent_by_physical() - Sequentially insert extent by physical.
690 * @ext_list_head: the head of physical extent list.
691 * @ext: the extent element which will be inserted.
693 static int insert_extent_by_physical(struct fiemap_extent_list **ext_list_head,
694 struct fiemap_extent_list *ext)
696 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
702 if (*ext_list_head == NULL) {
703 (*ext_list_head) = ext;
704 (*ext_list_head)->prev = *ext_list_head;
705 (*ext_list_head)->next = *ext_list_head;
709 if (ext->data.physical <= ext_list_tmp->data.physical) {
710 /* Insert before head */
711 if (ext_list_tmp->data.physical <
712 ext->data.physical + ext->data.len)
716 *ext_list_head = ext;
718 /* Insert into the middle or last of the list */
720 if (ext->data.physical < ext_list_tmp->data.physical)
722 ext_list_tmp = ext_list_tmp->next;
723 } while (ext_list_tmp != (*ext_list_head));
724 if (ext->data.physical <
725 ext_list_tmp->prev->data.physical +
726 ext_list_tmp->prev->data.len)
730 if (ext_list_tmp != *ext_list_head &&
731 ext_list_tmp->data.physical <
732 ext->data.physical + ext->data.len)
736 ext_list_tmp = ext_list_tmp->prev;
737 /* Insert "ext" after "ext_list_tmp" */
738 insert(ext_list_tmp, ext);
746 * insert_exts_group() - Insert a exts_group.
748 * @ext_group_head: the head of a exts_group list.
749 * @exts_group: the exts_group element which will be inserted.
751 static int insert_exts_group(struct fiemap_extent_group **ext_group_head,
752 struct fiemap_extent_group *exts_group)
754 struct fiemap_extent_group *ext_group_tmp = NULL;
756 if (exts_group == NULL) {
761 /* Initialize list */
762 if (*ext_group_head == NULL) {
763 (*ext_group_head) = exts_group;
764 (*ext_group_head)->prev = *ext_group_head;
765 (*ext_group_head)->next = *ext_group_head;
769 ext_group_tmp = (*ext_group_head)->prev;
770 insert(ext_group_tmp, exts_group);
776 * join_extents() - Find continuous region(exts_group).
778 * @ext_list_head: the head of the extent list.
779 * @ext_group_head: the head of the target exts_group list.
781 static int join_extents(struct fiemap_extent_list *ext_list_head,
782 struct fiemap_extent_group **ext_group_head)
784 __u64 len = ext_list_head->data.len;
785 struct fiemap_extent_list *ext_list_start = ext_list_head;
786 struct fiemap_extent_list *ext_list_tmp = ext_list_head->next;
789 struct fiemap_extent_group *ext_group_tmp = NULL;
791 /* This extent and previous extent are not continuous,
792 * so, all previous extents are treated as an extent group.
794 if ((ext_list_tmp->prev->data.logical +
795 ext_list_tmp->prev->data.len)
796 != ext_list_tmp->data.logical) {
798 malloc(sizeof(struct fiemap_extent_group));
799 if (ext_group_tmp == NULL)
802 memset(ext_group_tmp, 0,
803 sizeof(struct fiemap_extent_group));
804 ext_group_tmp->len = len;
805 ext_group_tmp->start = ext_list_start;
806 ext_group_tmp->end = ext_list_tmp->prev;
808 if (insert_exts_group(ext_group_head,
809 ext_group_tmp) < 0) {
813 ext_list_start = ext_list_tmp;
814 len = ext_list_tmp->data.len;
815 ext_list_tmp = ext_list_tmp->next;
819 /* This extent and previous extent are continuous,
820 * so, they belong to the same extent group, and we check
821 * if the next extent belongs to the same extent group.
823 len += ext_list_tmp->data.len;
824 ext_list_tmp = ext_list_tmp->next;
825 } while (ext_list_tmp != ext_list_head->next);
831 * get_file_extents() - Get file's extent list.
833 * @fd: defrag target file's descriptor.
834 * @ext_list_head: the head of the extent list.
836 static int get_file_extents(int fd, struct fiemap_extent_list **ext_list_head)
840 int ext_buf_size, fie_buf_size;
842 struct fiemap *fiemap_buf = NULL;
843 struct fiemap_extent *ext_buf = NULL;
844 struct fiemap_extent_list *ext_list = NULL;
846 /* Convert units, in bytes.
847 * Be careful : now, physical block number in extent is 48bit,
848 * and the maximum blocksize for ext4 is 4K(12bit),
849 * so there is no overflow, but in future it may be changed.
852 /* Alloc space for fiemap */
853 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
854 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
856 fiemap_buf = malloc(fie_buf_size);
857 if (fiemap_buf == NULL)
860 ext_buf = fiemap_buf->fm_extents;
861 memset(fiemap_buf, 0, fie_buf_size);
862 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
863 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
864 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
867 fiemap_buf->fm_start = pos;
868 memset(ext_buf, 0, ext_buf_size);
869 ret = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
870 if (ret < 0 || fiemap_buf->fm_mapped_extents == 0)
872 for (i = 0; i < fiemap_buf->fm_mapped_extents; i++) {
874 ext_list = malloc(sizeof(struct fiemap_extent_list));
875 if (ext_list == NULL)
878 ext_list->data.physical = ext_buf[i].fe_physical
880 ext_list->data.logical = ext_buf[i].fe_logical
882 ext_list->data.len = ext_buf[i].fe_length
885 ret = insert_extent_by_physical(
886 ext_list_head, ext_list);
892 /* Record file's logical offset this time */
893 pos = ext_buf[EXTENT_MAX_COUNT-1].fe_logical +
894 ext_buf[EXTENT_MAX_COUNT-1].fe_length;
896 * If fm_extents array has been filled and
897 * there are extents left, continue to cycle.
899 } while (fiemap_buf->fm_mapped_extents
900 == EXTENT_MAX_COUNT &&
901 !(ext_buf[EXTENT_MAX_COUNT-1].fe_flags
902 & FIEMAP_EXTENT_LAST));
912 * get_logical_count() - Get the file logical extents count.
914 * @logical_list_head: the head of the logical extent list.
916 static int get_logical_count(struct fiemap_extent_list *logical_list_head)
919 struct fiemap_extent_list *ext_list_tmp = logical_list_head;
923 ext_list_tmp = ext_list_tmp->next;
924 } while (ext_list_tmp != logical_list_head);
930 * get_physical_count() - Get the file physical extents count.
932 * @physical_list_head: the head of the physical extent list.
934 static int get_physical_count(struct fiemap_extent_list *physical_list_head)
937 struct fiemap_extent_list *ext_list_tmp = physical_list_head;
940 if ((ext_list_tmp->data.physical + ext_list_tmp->data.len)
941 != ext_list_tmp->next->data.physical) {
942 /* This extent and next extent are not continuous. */
946 ext_list_tmp = ext_list_tmp->next;
947 } while (ext_list_tmp != physical_list_head);
953 * change_physical_to_logical() - Change list from physical to logical.
955 * @physical_list_head: the head of physical extent list.
956 * @logical_list_head: the head of logical extent list.
958 static int change_physical_to_logical(
959 struct fiemap_extent_list **physical_list_head,
960 struct fiemap_extent_list **logical_list_head)
963 struct fiemap_extent_list *ext_list_tmp = *physical_list_head;
964 struct fiemap_extent_list *ext_list_next = ext_list_tmp->next;
967 if (ext_list_tmp == ext_list_next) {
968 ret = insert_extent_by_logical(
969 logical_list_head, ext_list_tmp);
973 *physical_list_head = NULL;
977 ext_list_tmp->prev->next = ext_list_tmp->next;
978 ext_list_tmp->next->prev = ext_list_tmp->prev;
979 *physical_list_head = ext_list_next;
981 ret = insert_extent_by_logical(
982 logical_list_head, ext_list_tmp);
987 ext_list_tmp = ext_list_next;
988 ext_list_next = ext_list_next->next;
994 /* get_file_blocks() - Get total file blocks.
996 * @ext_list_head: the extent list head of the target file
998 static ext4_fsblk_t get_file_blocks(struct fiemap_extent_list *ext_list_head)
1000 ext4_fsblk_t blk_count = 0;
1001 struct fiemap_extent_list *ext_list_tmp = ext_list_head;
1004 blk_count += ext_list_tmp->data.len;
1005 ext_list_tmp = ext_list_tmp->next;
1006 } while (ext_list_tmp != ext_list_head);
1012 * free_ext() - Free the extent list.
1014 * @ext_list_head: the extent list head of which will be free.
1016 static void free_ext(struct fiemap_extent_list *ext_list_head)
1018 struct fiemap_extent_list *ext_list_tmp = NULL;
1020 if (ext_list_head == NULL)
1023 while (ext_list_head->next != ext_list_head) {
1024 ext_list_tmp = ext_list_head;
1025 ext_list_head->prev->next = ext_list_head->next;
1026 ext_list_head->next->prev = ext_list_head->prev;
1027 ext_list_head = ext_list_head->next;
1030 free(ext_list_head);
1034 * free_exts_group() - Free the exts_group.
1036 * @*ext_group_head: the exts_group list head which will be free.
1038 static void free_exts_group(struct fiemap_extent_group *ext_group_head)
1040 struct fiemap_extent_group *ext_group_tmp = NULL;
1042 if (ext_group_head == NULL)
1045 while (ext_group_head->next != ext_group_head) {
1046 ext_group_tmp = ext_group_head;
1047 ext_group_head->prev->next = ext_group_head->next;
1048 ext_group_head->next->prev = ext_group_head->prev;
1049 ext_group_head = ext_group_head->next;
1050 free(ext_group_tmp);
1052 free(ext_group_head);
1056 * get_best_count() - Get the file best extents count.
1058 * @block_count: the file's physical block count.
1060 static int get_best_count(ext4_fsblk_t block_count)
1063 unsigned int flex_bg_num;
1065 /* Calcuate best extents count */
1066 if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1067 flex_bg_num = 1 << log_groups_per_flex;
1068 ret = ((block_count - 1) /
1069 ((ext4_fsblk_t)blocks_per_group *
1072 ret = ((block_count - 1) / blocks_per_group) + 1;
1079 * file_statistic() - Get statistic info of the file's fragments.
1081 * @file: the file's name.
1082 * @buf: the pointer of the struct stat64.
1084 * @ftwbuf: the pointer of a struct FTW.
1086 static int file_statistic(const char *file, const struct stat64 *buf,
1087 int flag EXT2FS_ATTR((unused)),
1088 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1092 int now_ext_count, best_ext_count = 0, physical_ext_count;
1094 __u64 size_per_ext = 0;
1096 ext4_fsblk_t blk_count = 0;
1097 char msg_buffer[PATH_MAX + 24];
1098 struct fiemap_extent_list *physical_list_head = NULL;
1099 struct fiemap_extent_list *logical_list_head = NULL;
1101 defraged_file_count++;
1103 if (mode_flag & DETAIL) {
1104 if (total_count == 1 && regular_count == 1)
1107 printf("[%u/%u]", defraged_file_count, total_count);
1111 if (lost_found_dir[0] != '\0' &&
1112 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1113 if (mode_flag & DETAIL) {
1114 PRINT_FILE_NAME(file);
1115 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND);
1120 if (!S_ISREG(buf->st_mode)) {
1121 if (mode_flag & DETAIL) {
1122 PRINT_FILE_NAME(file);
1123 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG);
1128 /* Access authority */
1129 if (current_uid != ROOT_UID &&
1130 buf->st_uid != current_uid) {
1131 if (mode_flag & DETAIL) {
1132 PRINT_FILE_NAME(file);
1134 "File is not current user's file"
1135 " or current user is not root");
1141 if (buf->st_size == 0) {
1142 if (mode_flag & DETAIL) {
1143 PRINT_FILE_NAME(file);
1144 STATISTIC_ERR_MSG("File size is 0");
1150 if (buf->st_blocks == 0) {
1151 if (mode_flag & DETAIL) {
1152 PRINT_FILE_NAME(file);
1153 STATISTIC_ERR_MSG("File has no blocks");
1158 fd = open64(file, O_RDONLY);
1160 if (mode_flag & DETAIL) {
1161 PRINT_FILE_NAME(file);
1162 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1167 /* Get file's physical extents */
1168 ret = get_file_extents(fd, &physical_list_head);
1170 if (mode_flag & DETAIL) {
1171 PRINT_FILE_NAME(file);
1172 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1177 /* Get the count of file's continuous physical region */
1178 physical_ext_count = get_physical_count(physical_list_head);
1180 /* Change list from physical to logical */
1181 ret = change_physical_to_logical(&physical_list_head,
1182 &logical_list_head);
1184 if (mode_flag & DETAIL) {
1185 PRINT_FILE_NAME(file);
1186 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1191 /* Count file fragments before defrag */
1192 now_ext_count = get_logical_count(logical_list_head);
1194 if (current_uid == ROOT_UID) {
1195 /* Calculate the size per extent */
1196 blk_count = get_file_blocks(logical_list_head);
1198 best_ext_count = get_best_count(blk_count);
1200 /* e4defrag rounds size_per_ext up to a block size boundary */
1201 size_per_ext = blk_count * (buf->st_blksize / 1024) /
1204 ratio = (float)(physical_ext_count - best_ext_count) * 100 /
1207 extents_before_defrag += now_ext_count;
1208 extents_after_defrag += best_ext_count;
1209 files_block_count += blk_count;
1212 if (total_count == 1 && regular_count == 1) {
1214 if (mode_flag & DETAIL) {
1216 struct fiemap_extent_list *ext_list_tmp =
1219 /* Print extents info */
1222 printf("[ext %d]:\tstart %llu:\tlogical "
1223 "%llu:\tlen %llu\n", count,
1224 ext_list_tmp->data.physical,
1225 ext_list_tmp->data.logical,
1226 ext_list_tmp->data.len);
1227 ext_list_tmp = ext_list_tmp->next;
1228 } while (ext_list_tmp != logical_list_head);
1231 printf("%-40s%10s/%-10s%9s\n",
1232 "<File>", "now", "best", "size/ext");
1233 if (current_uid == ROOT_UID) {
1234 if (strlen(file) > 40)
1235 printf("%s\n%50d/%-10d%6llu KB\n",
1236 file, now_ext_count,
1237 best_ext_count, size_per_ext);
1239 printf("%-40s%10d/%-10d%6llu KB\n",
1240 file, now_ext_count,
1241 best_ext_count, size_per_ext);
1243 if (strlen(file) > 40)
1244 printf("%s\n%50d/%-10s%7s\n",
1245 file, now_ext_count,
1248 printf("%-40s%10d/%-10s%7s\n",
1249 file, now_ext_count,
1257 if (mode_flag & DETAIL) {
1258 /* Print statistic info */
1259 sprintf(msg_buffer, "[%u/%u]%s",
1260 defraged_file_count, total_count, file);
1261 if (current_uid == ROOT_UID) {
1262 if (strlen(msg_buffer) > 40)
1263 printf("\033[79;0H\033[K%s\n"
1264 "%50d/%-10d%6llu KB\n",
1265 msg_buffer, now_ext_count,
1266 best_ext_count, size_per_ext);
1268 printf("\033[79;0H\033[K%-40s"
1269 "%10d/%-10d%6llu KB\n",
1270 msg_buffer, now_ext_count,
1271 best_ext_count, size_per_ext);
1273 if (strlen(msg_buffer) > 40)
1274 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1275 msg_buffer, now_ext_count,
1278 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1279 msg_buffer, now_ext_count,
1284 for (i = 0; i < SHOW_FRAG_FILES; i++) {
1285 if (ratio >= frag_rank[i].ratio) {
1286 for (j = SHOW_FRAG_FILES - 1; j > i; j--) {
1287 memset(&frag_rank[j], 0,
1288 sizeof(struct frag_statistic_ino));
1289 strncpy(frag_rank[j].msg_buffer,
1290 frag_rank[j - 1].msg_buffer,
1291 strnlen(frag_rank[j - 1].msg_buffer,
1293 frag_rank[j].now_count =
1294 frag_rank[j - 1].now_count;
1295 frag_rank[j].best_count =
1296 frag_rank[j - 1].best_count;
1297 frag_rank[j].size_per_ext =
1298 frag_rank[j - 1].size_per_ext;
1299 frag_rank[j].ratio =
1300 frag_rank[j - 1].ratio;
1302 memset(&frag_rank[i], 0,
1303 sizeof(struct frag_statistic_ino));
1304 strncpy(frag_rank[i].msg_buffer, file,
1305 strnlen(file, PATH_MAX));
1306 frag_rank[i].now_count = now_ext_count;
1307 frag_rank[i].best_count = best_ext_count;
1308 frag_rank[i].size_per_ext = size_per_ext;
1309 frag_rank[i].ratio = ratio;
1318 free_ext(physical_list_head);
1319 free_ext(logical_list_head);
1324 * print_progress - Print defrag progress
1327 * @start: logical offset for defrag target file
1328 * @file_size: defrag target filesize
1330 static void print_progress(const char *file, loff_t start, loff_t file_size)
1332 int percent = (start * 100) / file_size;
1333 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1334 defraged_file_count, total_count, file, min(percent, 100));
1341 * call_defrag() - Execute the defrag program.
1343 * @fd: target file descriptor.
1344 * @donor_fd: donor file descriptor.
1345 * @file: target file name.
1346 * @buf: pointer of the struct stat64.
1347 * @ext_list_head: head of the extent list.
1349 static int call_defrag(int fd, int donor_fd, const char *file,
1350 const struct stat64 *buf, struct fiemap_extent_list *ext_list_head)
1353 unsigned int page_num;
1354 unsigned char *vec = NULL;
1355 int defraged_ret = 0;
1357 struct move_extent move_data;
1358 struct fiemap_extent_list *ext_list_tmp = NULL;
1360 memset(&move_data, 0, sizeof(struct move_extent));
1361 move_data.donor_fd = donor_fd;
1363 /* Print defrag progress */
1364 print_progress(file, start, buf->st_size);
1366 ext_list_tmp = ext_list_head;
1368 move_data.orig_start = ext_list_tmp->data.logical;
1369 /* Logical offset of orig and donor should be same */
1370 move_data.donor_start = move_data.orig_start;
1371 move_data.len = ext_list_tmp->data.len;
1372 move_data.moved_len = 0;
1374 ret = page_in_core(fd, move_data, &vec, &page_num);
1376 if (mode_flag & DETAIL) {
1378 PRINT_ERR_MSG_WITH_ERRNO(
1379 "Failed to get file map");
1381 printf("\t[ NG ]\n");
1386 /* EXT4_IOC_MOVE_EXT */
1388 ioctl(fd, EXT4_IOC_MOVE_EXT, &move_data);
1391 ret = defrag_fadvise(fd, move_data, vec, page_num);
1397 if (mode_flag & DETAIL) {
1399 PRINT_ERR_MSG_WITH_ERRNO(
1400 "Failed to free page");
1402 printf("\t[ NG ]\n");
1407 if (defraged_ret < 0) {
1408 if (mode_flag & DETAIL) {
1410 PRINT_ERR_MSG_WITH_ERRNO(
1411 "Failed to defrag with "
1412 "EXT4_IOC_MOVE_EXT ioctl");
1413 if (errno == ENOTTY)
1414 printf("\tAt least 2.6.31-rc1 of "
1415 "vanilla kernel is required\n");
1417 printf("\t[ NG ]\n");
1421 /* Adjust logical offset for next ioctl */
1422 move_data.orig_start += move_data.moved_len;
1423 move_data.donor_start = move_data.orig_start;
1425 start = move_data.orig_start * buf->st_blksize;
1427 /* Print defrag progress */
1428 print_progress(file, start, buf->st_size);
1431 if (start >= buf->st_size)
1434 ext_list_tmp = ext_list_tmp->next;
1435 } while (ext_list_tmp != ext_list_head);
1441 * file_defrag() - Check file attributes and call ioctl to defrag.
1443 * @file: the file's name.
1444 * @buf: the pointer of the struct stat64.
1446 * @ftwbuf: the pointer of a struct FTW.
1448 static int file_defrag(const char *file, const struct stat64 *buf,
1449 int flag EXT2FS_ATTR((unused)),
1450 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1456 int file_frags_start, file_frags_end;
1457 int orig_physical_cnt, donor_physical_cnt = 0;
1458 char tmp_inode_name[PATH_MAX + 8];
1459 ext4_fsblk_t blk_count = 0;
1460 struct fiemap_extent_list *orig_list_physical = NULL;
1461 struct fiemap_extent_list *orig_list_logical = NULL;
1462 struct fiemap_extent_list *donor_list_physical = NULL;
1463 struct fiemap_extent_list *donor_list_logical = NULL;
1464 struct fiemap_extent_group *orig_group_head = NULL;
1465 struct fiemap_extent_group *orig_group_tmp = NULL;
1467 defraged_file_count++;
1469 if (mode_flag & DETAIL) {
1470 printf("[%u/%u]", defraged_file_count, total_count);
1474 if (lost_found_dir[0] != '\0' &&
1475 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1476 if (mode_flag & DETAIL) {
1477 PRINT_FILE_NAME(file);
1478 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1483 if (!S_ISREG(buf->st_mode)) {
1484 if (mode_flag & DETAIL) {
1485 PRINT_FILE_NAME(file);
1486 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1492 if (buf->st_size == 0) {
1493 if (mode_flag & DETAIL) {
1494 PRINT_FILE_NAME(file);
1495 IN_FTW_PRINT_ERR_MSG("File size is 0");
1501 if (buf->st_blocks == 0) {
1502 if (mode_flag & DETAIL) {
1503 PRINT_FILE_NAME(file);
1504 STATISTIC_ERR_MSG("File has no blocks");
1509 fd = open64(file, O_RDWR);
1511 if (mode_flag & DETAIL) {
1512 PRINT_FILE_NAME(file);
1513 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1518 /* Get file's extents */
1519 ret = get_file_extents(fd, &orig_list_physical);
1521 if (mode_flag & DETAIL) {
1522 PRINT_FILE_NAME(file);
1523 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1528 /* Get the count of file's continuous physical region */
1529 orig_physical_cnt = get_physical_count(orig_list_physical);
1531 /* Change list from physical to logical */
1532 ret = change_physical_to_logical(&orig_list_physical,
1533 &orig_list_logical);
1535 if (mode_flag & DETAIL) {
1536 PRINT_FILE_NAME(file);
1537 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1542 /* Count file fragments before defrag */
1543 file_frags_start = get_logical_count(orig_list_logical);
1545 blk_count = get_file_blocks(orig_list_logical);
1546 if (file_check(fd, buf, file, file_frags_start, blk_count) < 0)
1549 if (fsync(fd) < 0) {
1550 if (mode_flag & DETAIL) {
1551 PRINT_FILE_NAME(file);
1552 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1557 if (current_uid == ROOT_UID)
1558 best = get_best_count(blk_count);
1562 if (file_frags_start <= best)
1563 goto check_improvement;
1565 /* Combine extents to group */
1566 ret = join_extents(orig_list_logical, &orig_group_head);
1568 if (mode_flag & DETAIL) {
1569 PRINT_FILE_NAME(file);
1570 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1575 /* Create donor inode */
1576 memset(tmp_inode_name, 0, PATH_MAX + 8);
1577 sprintf(tmp_inode_name, "%.*s.defrag",
1578 (int)strnlen(file, PATH_MAX), file);
1579 donor_fd = open64(tmp_inode_name, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
1581 if (mode_flag & DETAIL) {
1582 PRINT_FILE_NAME(file);
1583 if (errno == EEXIST)
1584 PRINT_ERR_MSG_WITH_ERRNO(
1585 "File is being defraged by other program");
1587 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1592 /* Unlink donor inode */
1593 ret = unlink(tmp_inode_name);
1595 if (mode_flag & DETAIL) {
1596 PRINT_FILE_NAME(file);
1597 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1602 /* Allocate space for donor inode */
1603 orig_group_tmp = orig_group_head;
1605 ret = fallocate64(donor_fd, 0,
1606 (loff_t)orig_group_tmp->start->data.logical * block_size,
1607 (loff_t)orig_group_tmp->len * block_size);
1609 if (mode_flag & DETAIL) {
1610 PRINT_FILE_NAME(file);
1611 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1616 orig_group_tmp = orig_group_tmp->next;
1617 } while (orig_group_tmp != orig_group_head);
1619 /* Get donor inode's extents */
1620 ret = get_file_extents(donor_fd, &donor_list_physical);
1622 if (mode_flag & DETAIL) {
1623 PRINT_FILE_NAME(file);
1624 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1629 /* Calcuate donor inode's continuous physical region */
1630 donor_physical_cnt = get_physical_count(donor_list_physical);
1632 /* Change donor extent list from physical to logical */
1633 ret = change_physical_to_logical(&donor_list_physical,
1634 &donor_list_logical);
1636 if (mode_flag & DETAIL) {
1637 PRINT_FILE_NAME(file);
1638 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1644 if (mode_flag & DETAIL) {
1645 if (file_frags_start != 1)
1646 frag_files_before_defrag++;
1648 extents_before_defrag += file_frags_start;
1651 if (file_frags_start <= best ||
1652 orig_physical_cnt <= donor_physical_cnt) {
1653 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1654 defraged_file_count, total_count, file, 100);
1655 if (mode_flag & DETAIL)
1656 printf(" extents: %d -> %d",
1657 file_frags_start, file_frags_start);
1659 printf("\t[ OK ]\n");
1662 if (file_frags_start != 1)
1663 frag_files_after_defrag++;
1665 extents_after_defrag += file_frags_start;
1669 /* Defrag the file */
1670 ret = call_defrag(fd, donor_fd, file, buf, donor_list_logical);
1672 /* Count file fragments after defrag and print extents info */
1673 if (mode_flag & DETAIL) {
1674 file_frags_end = file_frag_count(fd);
1675 if (file_frags_end < 0) {
1677 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO);
1681 if (file_frags_end != 1)
1682 frag_files_after_defrag++;
1684 extents_after_defrag += file_frags_end;
1689 printf(" extents: %d -> %d",
1690 file_frags_start, file_frags_end);
1697 printf("\t[ OK ]\n");
1705 free_ext(orig_list_physical);
1706 free_ext(orig_list_logical);
1707 free_ext(donor_list_physical);
1708 free_exts_group(orig_group_head);
1713 * main() - Ext4 online defrag.
1715 * @argc: the number of parameter.
1716 * @argv[]: the pointer array of parameter.
1718 int main(int argc, char *argv[])
1722 int flags = FTW_PHYS | FTW_MOUNT;
1724 int success_flag = 0;
1725 char dir_name[PATH_MAX + 1];
1726 char dev_name[PATH_MAX + 1];
1728 ext2_filsys fs = NULL;
1730 /* Parse arguments */
1734 while ((opt = getopt(argc, argv, "vc")) != EOF) {
1737 mode_flag |= DETAIL;
1740 mode_flag |= STATISTIC;
1750 current_uid = getuid();
1753 for (i = optind; i < argc; i++) {
1757 frag_files_before_defrag = 0;
1758 frag_files_after_defrag = 0;
1759 extents_before_defrag = 0;
1760 extents_after_defrag = 0;
1761 defraged_file_count = 0;
1762 files_block_count = 0;
1763 blocks_per_group = 0;
1764 feature_incompat = 0;
1765 log_groups_per_flex = 0;
1767 memset(dir_name, 0, PATH_MAX + 1);
1768 memset(dev_name, 0, PATH_MAX + 1);
1769 memset(lost_found_dir, 0, PATH_MAX + 1);
1770 memset(frag_rank, 0,
1771 sizeof(struct frag_statistic_ino) * SHOW_FRAG_FILES);
1773 if ((mode_flag & STATISTIC) && i > optind)
1776 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1777 PRINT_ERR_MSG("Endian's type is not big/little endian");
1778 PRINT_FILE_NAME(argv[i]);
1782 if (lstat64(argv[i], &buf) < 0) {
1783 perror(NGMSG_FILE_INFO);
1784 PRINT_FILE_NAME(argv[i]);
1788 /* Handle i.e. lvm device symlinks */
1789 if (S_ISLNK(buf.st_mode)) {
1792 if (stat64(argv[i], &buf2) == 0 &&
1793 S_ISBLK(buf2.st_mode))
1797 if (S_ISBLK(buf.st_mode)) {
1799 strncpy(dev_name, argv[i], strnlen(argv[i], PATH_MAX));
1800 if (get_mount_point(argv[i], dir_name, PATH_MAX) < 0)
1802 if (lstat64(dir_name, &buf) < 0) {
1803 perror(NGMSG_FILE_INFO);
1804 PRINT_FILE_NAME(argv[i]);
1808 if (!(mode_flag & STATISTIC))
1809 printf("ext4 defragmentation for device(%s)\n",
1811 } else if (S_ISDIR(buf.st_mode)) {
1813 if (access(argv[i], R_OK) < 0) {
1818 strncpy(dir_name, argv[i], strnlen(argv[i], PATH_MAX));
1819 } else if (S_ISREG(buf.st_mode)) {
1821 arg_type = FILENAME;
1823 /* Irregular file */
1824 PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1825 PRINT_FILE_NAME(argv[i]);
1830 block_size = buf.st_blksize;
1833 * filesystem type checked in get_mount_point()
1835 if (arg_type == FILENAME || arg_type == DIRNAME) {
1836 if (is_ext4(argv[i], dev_name) < 0)
1838 if (realpath(argv[i], dir_name) == NULL) {
1839 perror("Couldn't get full path");
1840 PRINT_FILE_NAME(argv[i]);
1845 if (current_uid == ROOT_UID) {
1846 /* Get super block info */
1847 ret = ext2fs_open(dev_name, 0, 0, block_size,
1848 unix_io_manager, &fs);
1850 if (mode_flag & DETAIL) {
1851 perror("Can't get super block info");
1852 PRINT_FILE_NAME(argv[i]);
1857 blocks_per_group = fs->super->s_blocks_per_group;
1858 feature_incompat = fs->super->s_feature_incompat;
1859 log_groups_per_flex = fs->super->s_log_groups_per_flex;
1866 if (!(mode_flag & STATISTIC))
1867 printf("ext4 defragmentation "
1868 "for directory(%s)\n", argv[i]);
1870 int mount_dir_len = 0;
1871 mount_dir_len = strnlen(lost_found_dir, PATH_MAX);
1873 strncat(lost_found_dir, "/lost+found",
1874 PATH_MAX - strnlen(lost_found_dir, PATH_MAX));
1876 /* Not the case("e4defrag mount_piont_dir") */
1877 if (dir_name[mount_dir_len] != '\0') {
1879 * "e4defrag mount_piont_dir/lost+found"
1880 * or "e4defrag mount_piont_dir/lost+found/"
1882 if (strncmp(lost_found_dir, dir_name,
1883 strnlen(lost_found_dir,
1885 (dir_name[strnlen(lost_found_dir,
1886 PATH_MAX)] == '\0' ||
1887 dir_name[strnlen(lost_found_dir,
1888 PATH_MAX)] == '/')) {
1889 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1890 PRINT_FILE_NAME(argv[i]);
1894 /* "e4defrag mount_piont_dir/else_dir" */
1895 memset(lost_found_dir, 0, PATH_MAX + 1);
1898 if (arg_type == DEVNAME) {
1899 strncpy(lost_found_dir, dir_name,
1900 strnlen(dir_name, PATH_MAX));
1901 strncat(lost_found_dir, "/lost+found/",
1902 PATH_MAX - strnlen(lost_found_dir,
1906 nftw64(dir_name, calc_entry_counts, FTW_OPEN_FD, flags);
1908 if (mode_flag & STATISTIC) {
1909 if (mode_flag & DETAIL)
1910 printf("%-40s%10s/%-10s%9s\n",
1911 "<File>", "now", "best", "size/ext");
1913 if (!(mode_flag & DETAIL) &&
1914 current_uid != ROOT_UID) {
1920 nftw64(dir_name, file_statistic,
1921 FTW_OPEN_FD, flags);
1923 if (succeed_cnt != 0 &&
1924 current_uid == ROOT_UID) {
1925 if (mode_flag & DETAIL)
1927 printf("%-40s%10s/%-10s%9s\n",
1928 "<Fragmented files>", "now",
1929 "best", "size/ext");
1930 for (j = 0; j < SHOW_FRAG_FILES; j++) {
1931 if (strlen(frag_rank[j].
1933 printf("%d. %s\n%50d/"
1936 frag_rank[j].msg_buffer,
1937 frag_rank[j].now_count,
1938 frag_rank[j].best_count,
1941 } else if (strlen(frag_rank[j].
1943 printf("%d. %-37s%10d/"
1946 frag_rank[j].msg_buffer,
1947 frag_rank[j].now_count,
1948 frag_rank[j].best_count,
1957 /* File tree walk */
1958 nftw64(dir_name, file_defrag, FTW_OPEN_FD, flags);
1959 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt,
1961 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1962 total_count - succeed_cnt, total_count);
1963 if (mode_flag & DETAIL) {
1964 printf("\tTotal extents:\t\t\t%4d->%d\n",
1965 extents_before_defrag,
1966 extents_after_defrag);
1967 printf("\tFragmented percentage:\t\t"
1968 "%3llu%%->%llu%%\n",
1969 !regular_count ? 0 :
1970 ((unsigned long long)
1971 frag_files_before_defrag * 100) /
1973 !regular_count ? 0 :
1974 ((unsigned long long)
1975 frag_files_after_defrag * 100) /
1982 strncat(lost_found_dir, "/lost+found/",
1983 PATH_MAX - strnlen(lost_found_dir,
1985 if (strncmp(lost_found_dir, dir_name,
1986 strnlen(lost_found_dir,
1988 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1989 PRINT_FILE_NAME(argv[i]);
1993 if (mode_flag & STATISTIC) {
1994 file_statistic(argv[i], &buf, FTW_F, NULL);
1997 printf("ext4 defragmentation for %s\n",
1999 /* Defrag single file process */
2000 file_defrag(argv[i], &buf, FTW_F, NULL);
2001 if (succeed_cnt != 0)
2002 printf(" Success:\t\t\t[1/1]\n");
2004 printf(" Success:\t\t\t[0/1]\n");
2009 if (succeed_cnt != 0)
2011 if (mode_flag & STATISTIC) {
2012 if (current_uid != ROOT_UID) {
2018 if (mode_flag & DETAIL)
2021 if (arg_type == DEVNAME)
2022 printf(" In this device(%s), "
2023 "none can be defragmented.\n", argv[i]);
2024 else if (arg_type == DIRNAME)
2025 printf(" In this directory(%s), "
2026 "none can be defragmented.\n", argv[i]);
2028 printf(" This file(%s) "
2029 "can't be defragmented.\n", argv[i]);
2031 float files_ratio = 0.0;
2033 __u64 size_per_ext = files_block_count *
2034 (buf.st_blksize / 1024) /
2035 extents_before_defrag;
2036 files_ratio = (float)(extents_before_defrag -
2037 extents_after_defrag) *
2038 100 / files_block_count;
2039 score = CALC_SCORE(files_ratio);
2040 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2041 " Average size per extent"
2043 " Fragmentation score\t\t\t\t%.0f\n",
2044 extents_before_defrag,
2045 extents_after_defrag,
2046 size_per_ext, score);
2047 printf(" [0-30 no problem:"
2048 " 31-55 a little bit fragmented:"
2049 " 56- needs defrag]\n");
2051 if (arg_type == DEVNAME)
2052 printf(" This device (%s) ", argv[i]);
2053 else if (arg_type == DIRNAME)
2054 printf(" This directory (%s) ",
2057 printf(" This file (%s) ", argv[i]);
2059 if (score > BOUND_SCORE)
2060 printf("needs defragmentation.\n");
2062 printf("does not need "
2063 "defragmentation.\n");