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
34 #include <ext2fs/ext2_types.h>
35 #include <ext2fs/ext2fs.h>
37 #include <sys/ioctl.h>
38 #include <ext2fs/fiemap.h>
41 #include <sys/statfs.h>
42 #include <sys/syscall.h>
45 /* A relatively new ioctl interface ... */
46 #ifndef EXT4_IOC_MOVE_EXT
47 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
51 #define PRINT_ERR_MSG(msg) fprintf(stderr, "%s\n", (msg))
52 #define IN_FTW_PRINT_ERR_MSG(msg) \
53 fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
54 #define PRINT_FILE_NAME(file) fprintf(stderr, " \"%s\"\n", (file))
55 #define PRINT_ERR_MSG_WITH_ERRNO(msg) \
56 fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
57 #define STATISTIC_ERR_MSG(msg) \
58 fprintf(stderr, "\t%s\n", (msg))
59 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg) \
60 fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
61 #define min(x, y) (((x) > (y)) ? (y) : (x))
62 #define CALC_SCORE(ratio) \
63 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
64 /* Wrap up the free function */
70 /* Insert list2 after list1 */
71 #define insert(list1, list2) \
73 list2->next = list1->next; \
74 list1->next->prev = list2; \
75 list2->prev = list1; \
76 list1->next = list2; \
79 /* To delete unused warning */
81 #define EXT2FS_ATTR(x) __attribute__(x)
83 #define EXT2FS_ATTR(x)
86 /* The mode of defrag */
88 #define STATISTIC 0x02
94 #define FTW_OPEN_FD 2000
96 #define FS_EXT4 "ext4"
99 #define BOUND_SCORE 55
100 #define SHOW_FRAG_FILES 5
102 /* Magic number for ext4 */
103 #define EXT4_SUPER_MAGIC 0xEF53
105 /* Definition of flex_bg */
106 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
108 /* The following macro is used for ioctl FS_IOC_FIEMAP
109 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
110 * kernel-space and user-space per ioctl
112 #define EXTENT_MAX_COUNT 512
114 /* The following macros are error message */
116 "Usage : e4defrag [-v] file...| directory...| device...\n\
117 : e4defrag -c file...| directory...| device...\n"
119 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
120 #define NGMSG_FILE_EXTENT "Failed to get file extents"
121 #define NGMSG_FILE_INFO "Failed to get file information"
122 #define NGMSG_FILE_OPEN "Failed to open"
123 #define NGMSG_FILE_UNREG "File is not regular file"
124 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
126 /* Data type for filesystem-wide blocks number */
127 typedef unsigned long long ext4_fsblk_t;
129 struct fiemap_extent_data {
130 __u64 len; /* blocks count */
131 __u64 logical; /* start logical block number */
132 ext4_fsblk_t physical; /* start physical block number */
135 struct fiemap_extent_list {
136 struct fiemap_extent_list *prev;
137 struct fiemap_extent_list *next;
138 struct fiemap_extent_data data; /* extent belong to file */
141 struct fiemap_extent_group {
142 struct fiemap_extent_group *prev;
143 struct fiemap_extent_group *next;
144 __u64 len; /* length of this continuous region */
145 struct fiemap_extent_list *start; /* start ext */
146 struct fiemap_extent_list *end; /* end ext */
150 __s32 reserved; /* original file descriptor */
151 __u32 donor_fd; /* donor file descriptor */
152 __u64 orig_start; /* logical start offset in block for orig */
153 __u64 donor_start; /* logical start offset in block for donor */
154 __u64 len; /* block length to be moved */
155 __u64 moved_len; /* moved block length */
158 struct frag_statistic_ino {
159 int now_count; /* the file's extents count of before defrag */
160 int best_count; /* the best file's extents count */
161 __u64 size_per_ext; /* size(KB) per extent */
162 float ratio; /* the ratio of fragmentation */
163 char msg_buffer[PATH_MAX + 1]; /* pathname of the file */
166 char lost_found_dir[PATH_MAX + 1];
168 int extents_before_defrag;
169 int extents_after_defrag;
171 unsigned int current_uid;
172 unsigned int defraged_file_count;
173 unsigned int frag_files_before_defrag;
174 unsigned int frag_files_after_defrag;
175 unsigned int regular_count;
176 unsigned int succeed_cnt;
177 unsigned int total_count;
178 __u8 log_groups_per_flex;
179 __u32 blocks_per_group;
180 __u32 feature_incompat;
181 ext4_fsblk_t files_block_count;
182 struct frag_statistic_ino frag_rank[SHOW_FRAG_FILES];
185 /* Local definitions of some syscalls glibc may not yet have */
187 #ifndef HAVE_POSIX_FADVISE
188 #warning Using locally defined posix_fadvise interface.
190 #ifndef __NR_fadvise64_64
191 #error Your kernel headers dont define __NR_fadvise64_64
195 * fadvise() - Give advice about file access.
197 * @fd: defrag target file's descriptor.
198 * @offset: file offset.
200 * @advise: process flag.
202 static int posix_fadvise(int fd, loff_t offset, size_t len, int advise)
204 return syscall(__NR_fadvise64_64, fd, offset, len, advise);
206 #endif /* ! HAVE_FADVISE64_64 */
208 #ifndef HAVE_SYNC_FILE_RANGE
209 #warning Using locally defined sync_file_range interface.
211 #ifndef __NR_sync_file_range
212 #ifndef __NR_sync_file_range2 /* ppc */
213 #error Your kernel headers dont define __NR_sync_file_range
218 * sync_file_range() - Sync file region.
220 * @fd: defrag target file's descriptor.
221 * @offset: file offset.
222 * @length: area length.
223 * @flag: process flag.
225 int sync_file_range(int fd, loff_t offset, loff_t length, unsigned int flag)
227 #ifdef __NR_sync_file_range
228 return syscall(__NR_sync_file_range, fd, offset, length, flag);
230 return syscall(__NR_sync_file_range2, fd, flag, offset, length);
233 #endif /* ! HAVE_SYNC_FILE_RANGE */
235 #ifndef HAVE_FALLOCATE64
236 #warning Using locally defined fallocate syscall interface.
238 #ifndef __NR_fallocate
239 #error Your kernel headers dont define __NR_fallocate
243 * fallocate64() - Manipulate file space.
245 * @fd: defrag target file's descriptor.
246 * @mode: process flag.
247 * @offset: file offset.
250 static int fallocate64(int fd, int mode, loff_t offset, loff_t len)
252 return syscall(__NR_fallocate, fd, mode, offset, len);
254 #endif /* ! HAVE_FALLOCATE */
257 * get_mount_point() - Get device's mount point.
259 * @devname: the device's name.
260 * @mount_point: the mount point.
261 * @dir_path_len: the length of directory.
263 static int get_mount_point(const char *devname, char *mount_point,
266 /* Refer to /etc/mtab */
267 const char *mtab = MOUNTED;
269 struct mntent *mnt = NULL;
271 fp = setmntent(mtab, "r");
273 perror("Couldn't access /etc/mtab");
277 while ((mnt = getmntent(fp)) != NULL) {
278 if (strcmp(devname, mnt->mnt_fsname) != 0)
282 if (strcmp(mnt->mnt_type, FS_EXT4) == 0) {
283 strncpy(mount_point, mnt->mnt_dir,
287 PRINT_ERR_MSG(NGMSG_EXT4);
291 PRINT_ERR_MSG("Filesystem is not mounted");
296 * is_ext4() - Whether on an ext4 filesystem.
298 * @file: the file's name.
300 static int is_ext4(const char *file, char *devname)
305 char *mnt_type = NULL;
306 /* Refer to /etc/mtab */
307 const char *mtab = MOUNTED;
308 char file_path[PATH_MAX + 1];
309 struct mntent *mnt = NULL;
310 struct statfs64 fsbuf;
313 if (realpath(file, file_path) == NULL) {
314 perror("Couldn't get full path");
315 PRINT_FILE_NAME(file);
319 if (statfs64(file_path, &fsbuf) < 0) {
320 perror("Failed to get filesystem information");
321 PRINT_FILE_NAME(file);
325 if (fsbuf.f_type != EXT4_SUPER_MAGIC) {
326 PRINT_ERR_MSG(NGMSG_EXT4);
330 fp = setmntent(mtab, "r");
332 perror("Couldn't access /etc/mtab");
336 while ((mnt = getmntent(fp)) != NULL) {
337 if (mnt->mnt_fsname[0] != '/')
339 len = strlen(mnt->mnt_dir);
340 ret = memcmp(file_path, mnt->mnt_dir, len);
349 mnt_type = realloc(mnt_type, strlen(mnt->mnt_type) + 1);
350 if (mnt_type == NULL) {
354 memset(mnt_type, 0, strlen(mnt->mnt_type) + 1);
355 strncpy(mnt_type, mnt->mnt_type, strlen(mnt->mnt_type));
356 strncpy(lost_found_dir, mnt->mnt_dir, PATH_MAX);
357 strncpy(devname, mnt->mnt_fsname, strlen(mnt->mnt_fsname) + 1);
361 if (strcmp(mnt_type, FS_EXT4) == 0) {
366 PRINT_ERR_MSG(NGMSG_EXT4);
372 * calc_entry_counts() - Calculate file counts.
377 * @ftwbuf: the pointer of a struct FTW.
379 static int calc_entry_counts(const char *file EXT2FS_ATTR((unused)),
380 const struct stat64 *buf, int flag EXT2FS_ATTR((unused)),
381 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
383 if (S_ISREG(buf->st_mode))
392 * page_in_core() - Get information on whether pages are in core.
394 * @fd: defrag target file's descriptor.
395 * @defrag_data: data used for defrag.
396 * @vec: page state array.
397 * @page_num: page number.
399 static int page_in_core(int fd, struct move_extent defrag_data,
400 unsigned char **vec, unsigned int *page_num)
402 long pagesize = sysconf(_SC_PAGESIZE);
404 loff_t offset, end_offset, length;
406 if (vec == NULL || *vec != NULL)
409 /* In mmap, offset should be a multiple of the page size */
410 offset = (loff_t)defrag_data.orig_start * block_size;
411 length = (loff_t)defrag_data.len * block_size;
412 end_offset = offset + length;
413 /* Round the offset down to the nearest multiple of pagesize */
414 offset = (offset / pagesize) * pagesize;
415 length = end_offset - offset;
417 page = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, offset);
418 if (page == MAP_FAILED)
422 *page_num = (length + pagesize - 1) / pagesize;
423 *vec = (unsigned char *)calloc(*page_num, 1);
427 /* Get information on whether pages are in core */
428 if (mincore(page, (size_t)length, *vec) == -1 ||
429 munmap(page, length) == -1) {
438 * defrag_fadvise() - Predeclare an access pattern for file data.
440 * @fd: defrag target file's descriptor.
441 * @defrag_data: data used for defrag.
442 * @vec: page state array.
443 * @page_num: page number.
445 static int defrag_fadvise(int fd, struct move_extent defrag_data,
446 unsigned char *vec, unsigned int page_num)
449 long pagesize = sysconf(_SC_PAGESIZE);
450 int fadvise_flag = POSIX_FADV_DONTNEED;
451 int sync_flag = SYNC_FILE_RANGE_WAIT_BEFORE |
452 SYNC_FILE_RANGE_WRITE |
453 SYNC_FILE_RANGE_WAIT_AFTER;
457 offset = (loff_t)defrag_data.orig_start * block_size;
458 offset = (offset / pagesize) * pagesize;
460 /* Sync file for fadvise process */
461 if (sync_file_range(fd, offset,
462 (loff_t)pagesize * page_num, sync_flag) < 0)
465 /* Try to release buffer cache which this process used,
466 * then other process can use the released buffer
468 for (i = 0; i < page_num; i++) {
469 if ((vec[i] & 0x1) == 0) {
473 if (posix_fadvise(fd, offset, pagesize, fadvise_flag) < 0) {
474 if ((mode_flag & DETAIL) && flag) {
475 perror("\tFailed to fadvise");
486 * check_free_size() - Check if there's enough disk space.
488 * @fd: defrag target file's descriptor.
490 * @blk_count: file blocks.
492 static int check_free_size(int fd, const char *file, ext4_fsblk_t blk_count)
494 ext4_fsblk_t free_blk_count;
495 struct statfs64 fsbuf;
497 if (fstatfs64(fd, &fsbuf) < 0) {
498 if (mode_flag & DETAIL) {
499 PRINT_FILE_NAME(file);
500 PRINT_ERR_MSG_WITH_ERRNO(
501 "Failed to get filesystem information");
506 /* Compute free space for root and normal user separately */
507 if (current_uid == ROOT_UID)
508 free_blk_count = fsbuf.f_bfree;
510 free_blk_count = fsbuf.f_bavail;
512 if (free_blk_count >= blk_count)
519 * file_frag_count() - Get file fragment count.
521 * @fd: defrag target file's descriptor.
523 static int file_frag_count(int fd)
526 struct fiemap fiemap_buf;
528 /* When fm_extent_count is 0,
529 * ioctl just get file fragment count.
531 memset(&fiemap_buf, 0, sizeof(struct fiemap));
532 fiemap_buf.fm_start = 0;
533 fiemap_buf.fm_length = FIEMAP_MAX_OFFSET;
534 fiemap_buf.fm_flags |= FIEMAP_FLAG_SYNC;
536 ret = ioctl(fd, FS_IOC_FIEMAP, &fiemap_buf);
540 return fiemap_buf.fm_mapped_extents;
544 * file_check() - Check file's attributes.
546 * @fd: defrag target file's descriptor.
547 * @buf: a pointer of the struct stat64.
549 * @extents: file extents.
550 * @blk_count: file blocks.
552 static int file_check(int fd, const struct stat64 *buf, const char *file,
553 int extents, ext4_fsblk_t blk_count)
558 /* Write-lock check is more reliable */
559 lock.l_type = F_WRLCK;
561 lock.l_whence = SEEK_SET;
565 ret = check_free_size(fd, file, blk_count);
567 if ((mode_flag & DETAIL) && ret == -ENOSPC) {
568 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
569 " extents: %d -> %d\n", defraged_file_count,
570 total_count, file, extents, extents);
571 IN_FTW_PRINT_ERR_MSG(
572 "Defrag size is larger than filesystem's free space");
577 /* Access authority */
578 if (current_uid != ROOT_UID &&
579 buf->st_uid != current_uid) {
580 if (mode_flag & DETAIL) {
581 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
582 " extents: %d -> %d\n", defraged_file_count,
583 total_count, file, extents, extents);
584 IN_FTW_PRINT_ERR_MSG(
585 "File is not current user's file"
586 " or current user is not root");
592 if (fcntl(fd, F_GETLK, &lock) < 0) {
593 if (mode_flag & DETAIL) {
594 PRINT_FILE_NAME(file);
595 PRINT_ERR_MSG_WITH_ERRNO(
596 "Failed to get lock information");
599 } else if (lock.l_type != F_UNLCK) {
600 if (mode_flag & DETAIL) {
601 PRINT_FILE_NAME(file);
602 IN_FTW_PRINT_ERR_MSG("File has been locked");
611 * insert_extent_by_logical() - Sequentially insert extent by logical.
613 * @ext_list_head: the head of logical extent list.
614 * @ext: the extent element which will be inserted.
616 static int insert_extent_by_logical(struct fiemap_extent_list **ext_list_head,
617 struct fiemap_extent_list *ext)
619 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
625 if (*ext_list_head == NULL) {
626 (*ext_list_head) = ext;
627 (*ext_list_head)->prev = *ext_list_head;
628 (*ext_list_head)->next = *ext_list_head;
632 if (ext->data.logical <= ext_list_tmp->data.logical) {
633 /* Insert before head */
634 if (ext_list_tmp->data.logical <
635 ext->data.logical + ext->data.len)
639 *ext_list_head = ext;
641 /* Insert into the middle or last of the list */
643 if (ext->data.logical < ext_list_tmp->data.logical)
645 ext_list_tmp = ext_list_tmp->next;
646 } while (ext_list_tmp != (*ext_list_head));
647 if (ext->data.logical <
648 ext_list_tmp->prev->data.logical +
649 ext_list_tmp->prev->data.len)
653 if (ext_list_tmp != *ext_list_head &&
654 ext_list_tmp->data.logical <
655 ext->data.logical + ext->data.len)
659 ext_list_tmp = ext_list_tmp->prev;
660 /* Insert "ext" after "ext_list_tmp" */
661 insert(ext_list_tmp, ext);
669 * insert_extent_by_physical() - Sequentially insert extent by physical.
671 * @ext_list_head: the head of physical extent list.
672 * @ext: the extent element which will be inserted.
674 static int insert_extent_by_physical(struct fiemap_extent_list **ext_list_head,
675 struct fiemap_extent_list *ext)
677 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
683 if (*ext_list_head == NULL) {
684 (*ext_list_head) = ext;
685 (*ext_list_head)->prev = *ext_list_head;
686 (*ext_list_head)->next = *ext_list_head;
690 if (ext->data.physical <= ext_list_tmp->data.physical) {
691 /* Insert before head */
692 if (ext_list_tmp->data.physical <
693 ext->data.physical + ext->data.len)
697 *ext_list_head = ext;
699 /* Insert into the middle or last of the list */
701 if (ext->data.physical < ext_list_tmp->data.physical)
703 ext_list_tmp = ext_list_tmp->next;
704 } while (ext_list_tmp != (*ext_list_head));
705 if (ext->data.physical <
706 ext_list_tmp->prev->data.physical +
707 ext_list_tmp->prev->data.len)
711 if (ext_list_tmp != *ext_list_head &&
712 ext_list_tmp->data.physical <
713 ext->data.physical + ext->data.len)
717 ext_list_tmp = ext_list_tmp->prev;
718 /* Insert "ext" after "ext_list_tmp" */
719 insert(ext_list_tmp, ext);
727 * insert_exts_group() - Insert a exts_group.
729 * @ext_group_head: the head of a exts_group list.
730 * @exts_group: the exts_group element which will be inserted.
732 static int insert_exts_group(struct fiemap_extent_group **ext_group_head,
733 struct fiemap_extent_group *exts_group)
735 struct fiemap_extent_group *ext_group_tmp = NULL;
737 if (exts_group == NULL) {
742 /* Initialize list */
743 if (*ext_group_head == NULL) {
744 (*ext_group_head) = exts_group;
745 (*ext_group_head)->prev = *ext_group_head;
746 (*ext_group_head)->next = *ext_group_head;
750 ext_group_tmp = (*ext_group_head)->prev;
751 insert(ext_group_tmp, exts_group);
757 * join_extents() - Find continuous region(exts_group).
759 * @ext_list_head: the head of the extent list.
760 * @ext_group_head: the head of the target exts_group list.
762 static int join_extents(struct fiemap_extent_list *ext_list_head,
763 struct fiemap_extent_group **ext_group_head)
765 __u64 len = ext_list_head->data.len;
766 struct fiemap_extent_list *ext_list_start = ext_list_head;
767 struct fiemap_extent_list *ext_list_tmp = ext_list_head->next;
770 struct fiemap_extent_group *ext_group_tmp = NULL;
772 /* This extent and previous extent are not continuous,
773 * so, all previous extents are treated as an extent group.
775 if ((ext_list_tmp->prev->data.logical +
776 ext_list_tmp->prev->data.len)
777 != ext_list_tmp->data.logical) {
779 malloc(sizeof(struct fiemap_extent_group));
780 if (ext_group_tmp == NULL)
783 memset(ext_group_tmp, 0,
784 sizeof(struct fiemap_extent_group));
785 ext_group_tmp->len = len;
786 ext_group_tmp->start = ext_list_start;
787 ext_group_tmp->end = ext_list_tmp->prev;
789 if (insert_exts_group(ext_group_head,
790 ext_group_tmp) < 0) {
794 ext_list_start = ext_list_tmp;
795 len = ext_list_tmp->data.len;
796 ext_list_tmp = ext_list_tmp->next;
800 /* This extent and previous extent are continuous,
801 * so, they belong to the same extent group, and we check
802 * if the next extent belongs to the same extent group.
804 len += ext_list_tmp->data.len;
805 ext_list_tmp = ext_list_tmp->next;
806 } while (ext_list_tmp != ext_list_head->next);
812 * get_file_extents() - Get file's extent list.
814 * @fd: defrag target file's descriptor.
815 * @ext_list_head: the head of the extent list.
817 static int get_file_extents(int fd, struct fiemap_extent_list **ext_list_head)
821 int ext_buf_size, fie_buf_size;
823 struct fiemap *fiemap_buf = NULL;
824 struct fiemap_extent *ext_buf = NULL;
825 struct fiemap_extent_list *ext_list = NULL;
827 /* Convert units, in bytes.
828 * Be careful : now, physical block number in extent is 48bit,
829 * and the maximum blocksize for ext4 is 4K(12bit),
830 * so there is no overflow, but in future it may be changed.
833 /* Alloc space for fiemap */
834 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
835 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
837 fiemap_buf = malloc(fie_buf_size);
838 if (fiemap_buf == NULL)
841 ext_buf = fiemap_buf->fm_extents;
842 memset(fiemap_buf, 0, fie_buf_size);
843 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
844 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
845 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
848 fiemap_buf->fm_start = pos;
849 memset(ext_buf, 0, ext_buf_size);
850 ret = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
851 if (ret < 0 || fiemap_buf->fm_mapped_extents == 0)
853 for (i = 0; i < fiemap_buf->fm_mapped_extents; i++) {
855 ext_list = malloc(sizeof(struct fiemap_extent_list));
856 if (ext_list == NULL)
859 ext_list->data.physical = ext_buf[i].fe_physical
861 ext_list->data.logical = ext_buf[i].fe_logical
863 ext_list->data.len = ext_buf[i].fe_length
866 ret = insert_extent_by_physical(
867 ext_list_head, ext_list);
873 /* Record file's logical offset this time */
874 pos = ext_buf[EXTENT_MAX_COUNT-1].fe_logical +
875 ext_buf[EXTENT_MAX_COUNT-1].fe_length;
877 * If fm_extents array has been filled and
878 * there are extents left, continue to cycle.
880 } while (fiemap_buf->fm_mapped_extents
881 == EXTENT_MAX_COUNT &&
882 !(ext_buf[EXTENT_MAX_COUNT-1].fe_flags
883 & FIEMAP_EXTENT_LAST));
893 * get_logical_count() - Get the file logical extents count.
895 * @logical_list_head: the head of the logical extent list.
897 static int get_logical_count(struct fiemap_extent_list *logical_list_head)
900 struct fiemap_extent_list *ext_list_tmp = logical_list_head;
904 ext_list_tmp = ext_list_tmp->next;
905 } while (ext_list_tmp != logical_list_head);
911 * get_physical_count() - Get the file physical extents count.
913 * @physical_list_head: the head of the physical extent list.
915 static int get_physical_count(struct fiemap_extent_list *physical_list_head)
918 struct fiemap_extent_list *ext_list_tmp = physical_list_head;
921 if ((ext_list_tmp->data.physical + ext_list_tmp->data.len)
922 != ext_list_tmp->next->data.physical) {
923 /* This extent and next extent are not continuous. */
927 ext_list_tmp = ext_list_tmp->next;
928 } while (ext_list_tmp != physical_list_head);
934 * change_physical_to_logical() - Change list from physical to logical.
936 * @physical_list_head: the head of physical extent list.
937 * @logical_list_head: the head of logical extent list.
939 static int change_physical_to_logical(
940 struct fiemap_extent_list **physical_list_head,
941 struct fiemap_extent_list **logical_list_head)
944 struct fiemap_extent_list *ext_list_tmp = *physical_list_head;
945 struct fiemap_extent_list *ext_list_next = ext_list_tmp->next;
948 if (ext_list_tmp == ext_list_next) {
949 ret = insert_extent_by_logical(
950 logical_list_head, ext_list_tmp);
954 *physical_list_head = NULL;
958 ext_list_tmp->prev->next = ext_list_tmp->next;
959 ext_list_tmp->next->prev = ext_list_tmp->prev;
960 *physical_list_head = ext_list_next;
962 ret = insert_extent_by_logical(
963 logical_list_head, ext_list_tmp);
968 ext_list_tmp = ext_list_next;
969 ext_list_next = ext_list_next->next;
975 /* get_file_blocks() - Get total file blocks.
977 * @ext_list_head: the extent list head of the target file
979 static ext4_fsblk_t get_file_blocks(struct fiemap_extent_list *ext_list_head)
981 ext4_fsblk_t blk_count = 0;
982 struct fiemap_extent_list *ext_list_tmp = ext_list_head;
985 blk_count += ext_list_tmp->data.len;
986 ext_list_tmp = ext_list_tmp->next;
987 } while (ext_list_tmp != ext_list_head);
993 * free_ext() - Free the extent list.
995 * @ext_list_head: the extent list head of which will be free.
997 static void free_ext(struct fiemap_extent_list *ext_list_head)
999 struct fiemap_extent_list *ext_list_tmp = NULL;
1001 if (ext_list_head == NULL)
1004 while (ext_list_head->next != ext_list_head) {
1005 ext_list_tmp = ext_list_head;
1006 ext_list_head->prev->next = ext_list_head->next;
1007 ext_list_head->next->prev = ext_list_head->prev;
1008 ext_list_head = ext_list_head->next;
1011 free(ext_list_head);
1015 * free_exts_group() - Free the exts_group.
1017 * @*ext_group_head: the exts_group list head which will be free.
1019 static void free_exts_group(struct fiemap_extent_group *ext_group_head)
1021 struct fiemap_extent_group *ext_group_tmp = NULL;
1023 if (ext_group_head == NULL)
1026 while (ext_group_head->next != ext_group_head) {
1027 ext_group_tmp = ext_group_head;
1028 ext_group_head->prev->next = ext_group_head->next;
1029 ext_group_head->next->prev = ext_group_head->prev;
1030 ext_group_head = ext_group_head->next;
1031 free(ext_group_tmp);
1033 free(ext_group_head);
1037 * get_best_count() - Get the file best extents count.
1039 * @block_count: the file's physical block count.
1041 static int get_best_count(ext4_fsblk_t block_count)
1044 unsigned int flex_bg_num;
1046 /* Calcuate best extents count */
1047 if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1048 flex_bg_num = 1 << log_groups_per_flex;
1049 ret = ((block_count - 1) /
1050 ((ext4_fsblk_t)blocks_per_group *
1053 ret = ((block_count - 1) / blocks_per_group) + 1;
1060 * file_statistic() - Get statistic info of the file's fragments.
1062 * @file: the file's name.
1063 * @buf: the pointer of the struct stat64.
1065 * @ftwbuf: the pointer of a struct FTW.
1067 static int file_statistic(const char *file, const struct stat64 *buf,
1068 int flag EXT2FS_ATTR((unused)),
1069 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1073 int now_ext_count, best_ext_count = 0, physical_ext_count;
1075 __u64 size_per_ext = 0;
1077 ext4_fsblk_t blk_count = 0;
1078 char msg_buffer[PATH_MAX + 24];
1079 struct fiemap_extent_list *physical_list_head = NULL;
1080 struct fiemap_extent_list *logical_list_head = NULL;
1082 defraged_file_count++;
1084 if (mode_flag & DETAIL) {
1085 if (total_count == 1 && regular_count == 1)
1088 printf("[%u/%u]", defraged_file_count, total_count);
1092 if (lost_found_dir[0] != '\0' &&
1093 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1094 if (mode_flag & DETAIL) {
1095 PRINT_FILE_NAME(file);
1096 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND);
1101 if (!S_ISREG(buf->st_mode)) {
1102 if (mode_flag & DETAIL) {
1103 PRINT_FILE_NAME(file);
1104 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG);
1109 /* Access authority */
1110 if (current_uid != ROOT_UID &&
1111 buf->st_uid != current_uid) {
1112 if (mode_flag & DETAIL) {
1113 PRINT_FILE_NAME(file);
1115 "File is not current user's file"
1116 " or current user is not root");
1122 if (buf->st_size == 0) {
1123 if (mode_flag & DETAIL) {
1124 PRINT_FILE_NAME(file);
1125 STATISTIC_ERR_MSG("File size is 0");
1131 if (buf->st_blocks == 0) {
1132 if (mode_flag & DETAIL) {
1133 PRINT_FILE_NAME(file);
1134 STATISTIC_ERR_MSG("File has no blocks");
1139 fd = open64(file, O_RDONLY);
1141 if (mode_flag & DETAIL) {
1142 PRINT_FILE_NAME(file);
1143 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1148 /* Get file's physical extents */
1149 ret = get_file_extents(fd, &physical_list_head);
1151 if (mode_flag & DETAIL) {
1152 PRINT_FILE_NAME(file);
1153 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1158 /* Get the count of file's continuous physical region */
1159 physical_ext_count = get_physical_count(physical_list_head);
1161 /* Change list from physical to logical */
1162 ret = change_physical_to_logical(&physical_list_head,
1163 &logical_list_head);
1165 if (mode_flag & DETAIL) {
1166 PRINT_FILE_NAME(file);
1167 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1172 /* Count file fragments before defrag */
1173 now_ext_count = get_logical_count(logical_list_head);
1175 if (current_uid == ROOT_UID) {
1176 /* Calculate the size per extent */
1177 blk_count = get_file_blocks(logical_list_head);
1179 best_ext_count = get_best_count(blk_count);
1181 /* e4defrag rounds size_per_ext up to a block size boundary */
1182 size_per_ext = blk_count * (buf->st_blksize / 1024) /
1185 ratio = (float)(physical_ext_count - best_ext_count) * 100 /
1188 extents_before_defrag += now_ext_count;
1189 extents_after_defrag += best_ext_count;
1190 files_block_count += blk_count;
1193 if (total_count == 1 && regular_count == 1) {
1195 if (mode_flag & DETAIL) {
1197 struct fiemap_extent_list *ext_list_tmp =
1200 /* Print extents info */
1203 printf("[ext %d]:\tstart %llu:\tlogical "
1204 "%llu:\tlen %llu\n", count,
1205 ext_list_tmp->data.physical,
1206 ext_list_tmp->data.logical,
1207 ext_list_tmp->data.len);
1208 ext_list_tmp = ext_list_tmp->next;
1209 } while (ext_list_tmp != logical_list_head);
1212 printf("%-40s%10s/%-10s%9s\n",
1213 "<File>", "now", "best", "size/ext");
1214 if (current_uid == ROOT_UID) {
1215 if (strlen(file) > 40)
1216 printf("%s\n%50d/%-10d%6llu KB\n",
1217 file, now_ext_count,
1218 best_ext_count, size_per_ext);
1220 printf("%-40s%10d/%-10d%6llu KB\n",
1221 file, now_ext_count,
1222 best_ext_count, size_per_ext);
1224 if (strlen(file) > 40)
1225 printf("%s\n%50d/%-10s%7s\n",
1226 file, now_ext_count,
1229 printf("%-40s%10d/%-10s%7s\n",
1230 file, now_ext_count,
1238 if (mode_flag & DETAIL) {
1239 /* Print statistic info */
1240 sprintf(msg_buffer, "[%u/%u]%s",
1241 defraged_file_count, total_count, file);
1242 if (current_uid == ROOT_UID) {
1243 if (strlen(msg_buffer) > 40)
1244 printf("\033[79;0H\033[K%s\n"
1245 "%50d/%-10d%6llu KB\n",
1246 msg_buffer, now_ext_count,
1247 best_ext_count, size_per_ext);
1249 printf("\033[79;0H\033[K%-40s"
1250 "%10d/%-10d%6llu KB\n",
1251 msg_buffer, now_ext_count,
1252 best_ext_count, size_per_ext);
1254 if (strlen(msg_buffer) > 40)
1255 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1256 msg_buffer, now_ext_count,
1259 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1260 msg_buffer, now_ext_count,
1265 for (i = 0; i < SHOW_FRAG_FILES; i++) {
1266 if (ratio >= frag_rank[i].ratio) {
1267 for (j = SHOW_FRAG_FILES - 1; j > i; j--) {
1268 memset(&frag_rank[j], 0,
1269 sizeof(struct frag_statistic_ino));
1270 strncpy(frag_rank[j].msg_buffer,
1271 frag_rank[j - 1].msg_buffer,
1272 strnlen(frag_rank[j - 1].msg_buffer,
1274 frag_rank[j].now_count =
1275 frag_rank[j - 1].now_count;
1276 frag_rank[j].best_count =
1277 frag_rank[j - 1].best_count;
1278 frag_rank[j].size_per_ext =
1279 frag_rank[j - 1].size_per_ext;
1280 frag_rank[j].ratio =
1281 frag_rank[j - 1].ratio;
1283 memset(&frag_rank[i], 0,
1284 sizeof(struct frag_statistic_ino));
1285 strncpy(frag_rank[i].msg_buffer, file,
1286 strnlen(file, PATH_MAX));
1287 frag_rank[i].now_count = now_ext_count;
1288 frag_rank[i].best_count = best_ext_count;
1289 frag_rank[i].size_per_ext = size_per_ext;
1290 frag_rank[i].ratio = ratio;
1299 free_ext(physical_list_head);
1300 free_ext(logical_list_head);
1305 * print_progress - Print defrag progress
1308 * @start: logical offset for defrag target file
1309 * @file_size: defrag target filesize
1311 static void print_progress(const char *file, loff_t start, loff_t file_size)
1313 int percent = (start * 100) / file_size;
1314 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1315 defraged_file_count, total_count, file, min(percent, 100));
1322 * call_defrag() - Execute the defrag program.
1324 * @fd: target file descriptor.
1325 * @donor_fd: donor file descriptor.
1326 * @file: target file name.
1327 * @buf: pointer of the struct stat64.
1328 * @ext_list_head: head of the extent list.
1330 static int call_defrag(int fd, int donor_fd, const char *file,
1331 const struct stat64 *buf, struct fiemap_extent_list *ext_list_head)
1334 unsigned int page_num;
1335 unsigned char *vec = NULL;
1336 int defraged_ret = 0;
1338 struct move_extent move_data;
1339 struct fiemap_extent_list *ext_list_tmp = NULL;
1341 memset(&move_data, 0, sizeof(struct move_extent));
1342 move_data.donor_fd = donor_fd;
1344 /* Print defrag progress */
1345 print_progress(file, start, buf->st_size);
1347 ext_list_tmp = ext_list_head;
1349 move_data.orig_start = ext_list_tmp->data.logical;
1350 /* Logical offset of orig and donor should be same */
1351 move_data.donor_start = move_data.orig_start;
1352 move_data.len = ext_list_tmp->data.len;
1353 move_data.moved_len = 0;
1355 ret = page_in_core(fd, move_data, &vec, &page_num);
1357 if (mode_flag & DETAIL) {
1359 PRINT_ERR_MSG_WITH_ERRNO(
1360 "Failed to get file map");
1362 printf("\t[ NG ]\n");
1367 /* EXT4_IOC_MOVE_EXT */
1369 ioctl(fd, EXT4_IOC_MOVE_EXT, &move_data);
1372 ret = defrag_fadvise(fd, move_data, vec, page_num);
1378 if (mode_flag & DETAIL) {
1380 PRINT_ERR_MSG_WITH_ERRNO(
1381 "Failed to free page");
1383 printf("\t[ NG ]\n");
1388 if (defraged_ret < 0) {
1389 if (mode_flag & DETAIL) {
1391 PRINT_ERR_MSG_WITH_ERRNO(
1392 "Failed to defrag with "
1393 "EXT4_IOC_MOVE_EXT ioctl");
1394 if (errno == ENOTTY)
1395 printf("\tAt least 2.6.31-rc1 of "
1396 "vanilla kernel is required\n");
1398 printf("\t[ NG ]\n");
1402 /* Adjust logical offset for next ioctl */
1403 move_data.orig_start += move_data.moved_len;
1404 move_data.donor_start = move_data.orig_start;
1406 start = move_data.orig_start * buf->st_blksize;
1408 /* Print defrag progress */
1409 print_progress(file, start, buf->st_size);
1412 if (start >= buf->st_size)
1415 ext_list_tmp = ext_list_tmp->next;
1416 } while (ext_list_tmp != ext_list_head);
1422 * file_defrag() - Check file attributes and call ioctl to defrag.
1424 * @file: the file's name.
1425 * @buf: the pointer of the struct stat64.
1427 * @ftwbuf: the pointer of a struct FTW.
1429 static int file_defrag(const char *file, const struct stat64 *buf,
1430 int flag EXT2FS_ATTR((unused)),
1431 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1437 int file_frags_start, file_frags_end;
1438 int orig_physical_cnt, donor_physical_cnt = 0;
1439 char tmp_inode_name[PATH_MAX + 8];
1440 ext4_fsblk_t blk_count = 0;
1441 struct fiemap_extent_list *orig_list_physical = NULL;
1442 struct fiemap_extent_list *orig_list_logical = NULL;
1443 struct fiemap_extent_list *donor_list_physical = NULL;
1444 struct fiemap_extent_list *donor_list_logical = NULL;
1445 struct fiemap_extent_group *orig_group_head = NULL;
1446 struct fiemap_extent_group *orig_group_tmp = NULL;
1448 defraged_file_count++;
1450 if (mode_flag & DETAIL) {
1451 printf("[%u/%u]", defraged_file_count, total_count);
1455 if (lost_found_dir[0] != '\0' &&
1456 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1457 if (mode_flag & DETAIL) {
1458 PRINT_FILE_NAME(file);
1459 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1464 if (!S_ISREG(buf->st_mode)) {
1465 if (mode_flag & DETAIL) {
1466 PRINT_FILE_NAME(file);
1467 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1473 if (buf->st_size == 0) {
1474 if (mode_flag & DETAIL) {
1475 PRINT_FILE_NAME(file);
1476 IN_FTW_PRINT_ERR_MSG("File size is 0");
1482 if (buf->st_blocks == 0) {
1483 if (mode_flag & DETAIL) {
1484 PRINT_FILE_NAME(file);
1485 STATISTIC_ERR_MSG("File has no blocks");
1490 fd = open64(file, O_RDWR);
1492 if (mode_flag & DETAIL) {
1493 PRINT_FILE_NAME(file);
1494 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1499 /* Get file's extents */
1500 ret = get_file_extents(fd, &orig_list_physical);
1502 if (mode_flag & DETAIL) {
1503 PRINT_FILE_NAME(file);
1504 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1509 /* Get the count of file's continuous physical region */
1510 orig_physical_cnt = get_physical_count(orig_list_physical);
1512 /* Change list from physical to logical */
1513 ret = change_physical_to_logical(&orig_list_physical,
1514 &orig_list_logical);
1516 if (mode_flag & DETAIL) {
1517 PRINT_FILE_NAME(file);
1518 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1523 /* Count file fragments before defrag */
1524 file_frags_start = get_logical_count(orig_list_logical);
1526 blk_count = get_file_blocks(orig_list_logical);
1527 if (file_check(fd, buf, file, file_frags_start, blk_count) < 0)
1530 if (fsync(fd) < 0) {
1531 if (mode_flag & DETAIL) {
1532 PRINT_FILE_NAME(file);
1533 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1538 if (current_uid == ROOT_UID)
1539 best = get_best_count(blk_count);
1543 if (file_frags_start <= best)
1544 goto check_improvement;
1546 /* Combine extents to group */
1547 ret = join_extents(orig_list_logical, &orig_group_head);
1549 if (mode_flag & DETAIL) {
1550 PRINT_FILE_NAME(file);
1551 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1556 /* Create donor inode */
1557 memset(tmp_inode_name, 0, PATH_MAX + 8);
1558 sprintf(tmp_inode_name, "%.*s.defrag",
1559 (int)strnlen(file, PATH_MAX), file);
1560 donor_fd = open64(tmp_inode_name, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
1562 if (mode_flag & DETAIL) {
1563 PRINT_FILE_NAME(file);
1564 if (errno == EEXIST)
1565 PRINT_ERR_MSG_WITH_ERRNO(
1566 "File is being defraged by other program");
1568 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1573 /* Unlink donor inode */
1574 ret = unlink(tmp_inode_name);
1576 if (mode_flag & DETAIL) {
1577 PRINT_FILE_NAME(file);
1578 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1583 /* Allocate space for donor inode */
1584 orig_group_tmp = orig_group_head;
1586 ret = fallocate64(donor_fd, 0,
1587 (loff_t)orig_group_tmp->start->data.logical * block_size,
1588 (loff_t)orig_group_tmp->len * block_size);
1590 if (mode_flag & DETAIL) {
1591 PRINT_FILE_NAME(file);
1592 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1597 orig_group_tmp = orig_group_tmp->next;
1598 } while (orig_group_tmp != orig_group_head);
1600 /* Get donor inode's extents */
1601 ret = get_file_extents(donor_fd, &donor_list_physical);
1603 if (mode_flag & DETAIL) {
1604 PRINT_FILE_NAME(file);
1605 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1610 /* Calcuate donor inode's continuous physical region */
1611 donor_physical_cnt = get_physical_count(donor_list_physical);
1613 /* Change donor extent list from physical to logical */
1614 ret = change_physical_to_logical(&donor_list_physical,
1615 &donor_list_logical);
1617 if (mode_flag & DETAIL) {
1618 PRINT_FILE_NAME(file);
1619 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1625 if (mode_flag & DETAIL) {
1626 if (file_frags_start != 1)
1627 frag_files_before_defrag++;
1629 extents_before_defrag += file_frags_start;
1632 if (file_frags_start <= best ||
1633 orig_physical_cnt <= donor_physical_cnt) {
1634 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1635 defraged_file_count, total_count, file, 100);
1636 if (mode_flag & DETAIL)
1637 printf(" extents: %d -> %d",
1638 file_frags_start, file_frags_start);
1640 printf("\t[ OK ]\n");
1643 if (file_frags_start != 1)
1644 frag_files_after_defrag++;
1646 extents_after_defrag += file_frags_start;
1650 /* Defrag the file */
1651 ret = call_defrag(fd, donor_fd, file, buf, donor_list_logical);
1653 /* Count file fragments after defrag and print extents info */
1654 if (mode_flag & DETAIL) {
1655 file_frags_end = file_frag_count(fd);
1656 if (file_frags_end < 0) {
1658 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO);
1662 if (file_frags_end != 1)
1663 frag_files_after_defrag++;
1665 extents_after_defrag += file_frags_end;
1670 printf(" extents: %d -> %d",
1671 file_frags_start, file_frags_end);
1678 printf("\t[ OK ]\n");
1686 free_ext(orig_list_physical);
1687 free_ext(orig_list_logical);
1688 free_ext(donor_list_physical);
1689 free_exts_group(orig_group_head);
1694 * main() - Ext4 online defrag.
1696 * @argc: the number of parameter.
1697 * @argv[]: the pointer array of parameter.
1699 int main(int argc, char *argv[])
1703 int flags = FTW_PHYS | FTW_MOUNT;
1705 int success_flag = 0;
1706 char dir_name[PATH_MAX + 1];
1707 char dev_name[PATH_MAX + 1];
1709 ext2_filsys fs = NULL;
1711 /* Parse arguments */
1715 while ((opt = getopt(argc, argv, "vc")) != EOF) {
1718 mode_flag |= DETAIL;
1721 mode_flag |= STATISTIC;
1731 current_uid = getuid();
1734 for (i = optind; i < argc; i++) {
1738 frag_files_before_defrag = 0;
1739 frag_files_after_defrag = 0;
1740 extents_before_defrag = 0;
1741 extents_after_defrag = 0;
1742 defraged_file_count = 0;
1743 files_block_count = 0;
1744 blocks_per_group = 0;
1745 feature_incompat = 0;
1746 log_groups_per_flex = 0;
1748 memset(dir_name, 0, PATH_MAX + 1);
1749 memset(dev_name, 0, PATH_MAX + 1);
1750 memset(lost_found_dir, 0, PATH_MAX + 1);
1751 memset(frag_rank, 0,
1752 sizeof(struct frag_statistic_ino) * SHOW_FRAG_FILES);
1754 if ((mode_flag & STATISTIC) && i > optind)
1757 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1758 PRINT_ERR_MSG("Endian's type is not big/little endian");
1759 PRINT_FILE_NAME(argv[i]);
1763 if (lstat64(argv[i], &buf) < 0) {
1764 perror(NGMSG_FILE_INFO);
1765 PRINT_FILE_NAME(argv[i]);
1769 if (S_ISBLK(buf.st_mode)) {
1771 strncpy(dev_name, argv[i], strnlen(argv[i], PATH_MAX));
1772 if (get_mount_point(argv[i], dir_name, PATH_MAX) < 0)
1774 if (lstat64(dir_name, &buf) < 0) {
1775 perror(NGMSG_FILE_INFO);
1776 PRINT_FILE_NAME(argv[i]);
1780 if (!(mode_flag & STATISTIC))
1781 printf("ext4 defragmentation for device(%s)\n",
1783 } else if (S_ISDIR(buf.st_mode)) {
1785 if (access(argv[i], R_OK) < 0) {
1790 strncpy(dir_name, argv[i], strnlen(argv[i], PATH_MAX));
1791 } else if (S_ISREG(buf.st_mode)) {
1793 arg_type = FILENAME;
1795 /* Irregular file */
1796 PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1797 PRINT_FILE_NAME(argv[i]);
1802 block_size = buf.st_blksize;
1805 * filesystem type checked in get_mount_point()
1807 if (arg_type == FILENAME || arg_type == DIRNAME) {
1808 if (is_ext4(argv[i], dev_name) < 0)
1810 if (realpath(argv[i], dir_name) == NULL) {
1811 perror("Couldn't get full path");
1812 PRINT_FILE_NAME(argv[i]);
1817 if (current_uid == ROOT_UID) {
1818 /* Get super block info */
1819 ret = ext2fs_open(dev_name, 0, 0, block_size,
1820 unix_io_manager, &fs);
1822 if (mode_flag & DETAIL) {
1823 perror("Can't get super block info");
1824 PRINT_FILE_NAME(argv[i]);
1829 blocks_per_group = fs->super->s_blocks_per_group;
1830 feature_incompat = fs->super->s_feature_incompat;
1831 log_groups_per_flex = fs->super->s_log_groups_per_flex;
1838 if (!(mode_flag & STATISTIC))
1839 printf("ext4 defragmentation "
1840 "for directory(%s)\n", argv[i]);
1842 int mount_dir_len = 0;
1843 mount_dir_len = strnlen(lost_found_dir, PATH_MAX);
1845 strncat(lost_found_dir, "/lost+found",
1846 PATH_MAX - strnlen(lost_found_dir, PATH_MAX));
1848 /* Not the case("e4defrag mount_piont_dir") */
1849 if (dir_name[mount_dir_len] != '\0') {
1851 * "e4defrag mount_piont_dir/lost+found"
1852 * or "e4defrag mount_piont_dir/lost+found/"
1854 if (strncmp(lost_found_dir, dir_name,
1855 strnlen(lost_found_dir,
1857 (dir_name[strnlen(lost_found_dir,
1858 PATH_MAX)] == '\0' ||
1859 dir_name[strnlen(lost_found_dir,
1860 PATH_MAX)] == '/')) {
1861 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1862 PRINT_FILE_NAME(argv[i]);
1866 /* "e4defrag mount_piont_dir/else_dir" */
1867 memset(lost_found_dir, 0, PATH_MAX + 1);
1870 if (arg_type == DEVNAME) {
1871 strncpy(lost_found_dir, dir_name,
1872 strnlen(dir_name, PATH_MAX));
1873 strncat(lost_found_dir, "/lost+found/",
1874 PATH_MAX - strnlen(lost_found_dir,
1878 nftw64(dir_name, calc_entry_counts, FTW_OPEN_FD, flags);
1880 if (mode_flag & STATISTIC) {
1881 if (mode_flag & DETAIL)
1882 printf("%-40s%10s/%-10s%9s\n",
1883 "<File>", "now", "best", "size/ext");
1885 if (!(mode_flag & DETAIL) &&
1886 current_uid != ROOT_UID) {
1892 nftw64(dir_name, file_statistic,
1893 FTW_OPEN_FD, flags);
1895 if (succeed_cnt != 0 &&
1896 current_uid == ROOT_UID) {
1897 if (mode_flag & DETAIL)
1899 printf("%-40s%10s/%-10s%9s\n",
1900 "<Fragmented files>", "now",
1901 "best", "size/ext");
1902 for (j = 0; j < SHOW_FRAG_FILES; j++) {
1903 if (strlen(frag_rank[j].
1905 printf("%d. %s\n%50d/"
1908 frag_rank[j].msg_buffer,
1909 frag_rank[j].now_count,
1910 frag_rank[j].best_count,
1913 } else if (strlen(frag_rank[j].
1915 printf("%d. %-37s%10d/"
1918 frag_rank[j].msg_buffer,
1919 frag_rank[j].now_count,
1920 frag_rank[j].best_count,
1929 /* File tree walk */
1930 nftw64(dir_name, file_defrag, FTW_OPEN_FD, flags);
1931 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt,
1933 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1934 total_count - succeed_cnt, total_count);
1935 if (mode_flag & DETAIL) {
1936 printf("\tTotal extents:\t\t\t%4d->%d\n",
1937 extents_before_defrag,
1938 extents_after_defrag);
1939 printf("\tFragmented percentage:\t\t"
1940 "%3llu%%->%llu%%\n",
1941 !regular_count ? 0 :
1942 ((unsigned long long)
1943 frag_files_before_defrag * 100) /
1945 !regular_count ? 0 :
1946 ((unsigned long long)
1947 frag_files_after_defrag * 100) /
1954 strncat(lost_found_dir, "/lost+found/",
1955 PATH_MAX - strnlen(lost_found_dir,
1957 if (strncmp(lost_found_dir, dir_name,
1958 strnlen(lost_found_dir,
1960 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1961 PRINT_FILE_NAME(argv[i]);
1965 if (mode_flag & STATISTIC) {
1966 file_statistic(argv[i], &buf, FTW_F, NULL);
1969 printf("ext4 defragmentation for %s\n",
1971 /* Defrag single file process */
1972 file_defrag(argv[i], &buf, FTW_F, NULL);
1973 if (succeed_cnt != 0)
1974 printf(" Success:\t\t\t[1/1]\n");
1976 printf(" Success:\t\t\t[0/1]\n");
1981 if (succeed_cnt != 0)
1983 if (mode_flag & STATISTIC) {
1984 if (current_uid != ROOT_UID) {
1990 if (mode_flag & DETAIL)
1993 if (arg_type == DEVNAME)
1994 printf(" In this device(%s), "
1995 "none can be defragmented.\n", argv[i]);
1996 else if (arg_type == DIRNAME)
1997 printf(" In this directory(%s), "
1998 "none can be defragmented.\n", argv[i]);
2000 printf(" This file(%s) "
2001 "can't be defragmented.\n", argv[i]);
2003 float files_ratio = 0.0;
2005 __u64 size_per_ext = files_block_count *
2006 (buf.st_blksize / 1024) /
2007 extents_before_defrag;
2008 files_ratio = (float)(extents_before_defrag -
2009 extents_after_defrag) *
2010 100 / files_block_count;
2011 score = CALC_SCORE(files_ratio);
2012 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2013 " Average size per extent"
2015 " Fragmentation score\t\t\t\t%.0f\n",
2016 extents_before_defrag,
2017 extents_after_defrag,
2018 size_per_ext, score);
2019 printf(" [0-30 no problem:"
2020 " 31-55 a little bit fragmented:"
2021 " 56- needs defrag]\n");
2023 if (arg_type == DEVNAME)
2024 printf(" This device (%s) ", argv[i]);
2025 else if (arg_type == DIRNAME)
2026 printf(" This directory (%s) ",
2029 printf(" This file (%s) ", argv[i]);
2031 if (score > BOUND_SCORE)
2032 printf("needs defragmentation.\n");
2034 printf("does not need "
2035 "defragmentation.\n");