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 /* A relatively new ioctl interface ... */
45 #ifndef EXT4_IOC_MOVE_EXT
46 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
50 #define PRINT_ERR_MSG(msg) fprintf(stderr, "%s\n", (msg))
51 #define IN_FTW_PRINT_ERR_MSG(msg) \
52 fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
53 #define PRINT_FILE_NAME(file) fprintf(stderr, " \"%s\"\n", (file))
54 #define PRINT_ERR_MSG_WITH_ERRNO(msg) \
55 fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
56 #define STATISTIC_ERR_MSG(msg) \
57 fprintf(stderr, "\t%s\n", (msg))
58 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg) \
59 fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
60 #define min(x, y) (((x) > (y)) ? (y) : (x))
61 #define CALC_SCORE(ratio) \
62 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
63 /* Wrap up the free function */
69 /* Insert list2 after list1 */
70 #define insert(list1, list2) \
72 list2->next = list1->next; \
73 list1->next->prev = list2; \
74 list2->prev = list1; \
75 list1->next = list2; \
78 /* To delete unused warning */
80 #define EXT2FS_ATTR(x) __attribute__(x)
82 #define EXT2FS_ATTR(x)
85 /* The mode of defrag */
87 #define STATISTIC 0x02
93 #define FTW_OPEN_FD 2000
95 #define FS_EXT4 "ext4"
98 #define BOUND_SCORE 55
99 #define SHOW_FRAG_FILES 5
101 /* Magic number for ext4 */
102 #define EXT4_SUPER_MAGIC 0xEF53
104 /* Definition of flex_bg */
105 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
107 /* The following macro is used for ioctl FS_IOC_FIEMAP
108 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
109 * kernel-space and user-space per ioctl
111 #define EXTENT_MAX_COUNT 512
113 /* The following macros are error message */
115 "Usage : e4defrag [-v] file...| directory...| device...\n\
116 : e4defrag -c file...| directory...| device...\n"
118 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
119 #define NGMSG_FILE_EXTENT "Failed to get file extents"
120 #define NGMSG_FILE_INFO "Failed to get file information"
121 #define NGMSG_FILE_OPEN "Failed to open"
122 #define NGMSG_FILE_UNREG "File is not regular file"
123 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
125 /* Data type for filesystem-wide blocks number */
126 typedef unsigned long long ext4_fsblk_t
;
128 struct fiemap_extent_data
{
129 __u64 len
; /* blocks count */
130 __u64 logical
; /* start logical block number */
131 ext4_fsblk_t physical
; /* start physical block number */
134 struct fiemap_extent_list
{
135 struct fiemap_extent_list
*prev
;
136 struct fiemap_extent_list
*next
;
137 struct fiemap_extent_data data
; /* extent belong to file */
140 struct fiemap_extent_group
{
141 struct fiemap_extent_group
*prev
;
142 struct fiemap_extent_group
*next
;
143 __u64 len
; /* length of this continuous region */
144 struct fiemap_extent_list
*start
; /* start ext */
145 struct fiemap_extent_list
*end
; /* end ext */
149 __s32 reserved
; /* original file descriptor */
150 __u32 donor_fd
; /* donor file descriptor */
151 __u64 orig_start
; /* logical start offset in block for orig */
152 __u64 donor_start
; /* logical start offset in block for donor */
153 __u64 len
; /* block length to be moved */
154 __u64 moved_len
; /* moved block length */
157 struct frag_statistic_ino
{
158 int now_count
; /* the file's extents count of before defrag */
159 int best_count
; /* the best file's extents count */
160 __u64 size_per_ext
; /* size(KB) per extent */
161 float ratio
; /* the ratio of fragmentation */
162 char msg_buffer
[PATH_MAX
+ 1]; /* pathname of the file */
165 static char lost_found_dir
[PATH_MAX
+ 1];
166 static int block_size
;
167 static int extents_before_defrag
;
168 static int extents_after_defrag
;
169 static int mode_flag
;
170 static unsigned int current_uid
;
171 static unsigned int defraged_file_count
;
172 static unsigned int frag_files_before_defrag
;
173 static unsigned int frag_files_after_defrag
;
174 static unsigned int regular_count
;
175 static unsigned int succeed_cnt
;
176 static unsigned int total_count
;
177 static __u8 log_groups_per_flex
;
178 static __u32 blocks_per_group
;
179 static __u32 feature_incompat
;
180 static ext4_fsblk_t files_block_count
;
181 static struct frag_statistic_ino frag_rank
[SHOW_FRAG_FILES
];
185 * We prefer posix_fadvise64 when available, as it allows 64bit offset on
188 #if defined(HAVE_POSIX_FADVISE64)
189 #define posix_fadvise posix_fadvise64
190 #elif defined(HAVE_FADVISE64)
191 #define posix_fadvise fadvise64
192 #elif !defined(HAVE_POSIX_FADVISE)
193 #error posix_fadvise not available!
196 #ifndef HAVE_SYNC_FILE_RANGE
197 #error sync_file_range not available!
198 #endif /* ! HAVE_SYNC_FILE_RANGE */
200 #ifndef HAVE_FALLOCATE64
201 #error fallocate64 not available!
202 #endif /* ! HAVE_FALLOCATE64 */
205 * get_mount_point() - Get device's mount point.
207 * @devname: the device's name.
208 * @mount_point: the mount point.
209 * @dir_path_len: the length of directory.
211 static int get_mount_point(const char *devname
, char *mount_point
,
214 /* Refer to /etc/mtab */
215 const char *mtab
= MOUNTED
;
217 struct mntent
*mnt
= NULL
;
220 if (stat64(devname
, &sb
) < 0) {
221 perror(NGMSG_FILE_INFO
);
222 PRINT_FILE_NAME(devname
);
226 fp
= setmntent(mtab
, "r");
228 perror("Couldn't access /etc/mtab");
232 while ((mnt
= getmntent(fp
)) != NULL
) {
236 * To handle device symlinks, we see if the
237 * device number matches, not the name
239 if (stat64(mnt
->mnt_fsname
, &ms
) < 0)
241 if (sb
.st_rdev
!= ms
.st_rdev
)
245 if (strcmp(mnt
->mnt_type
, FS_EXT4
) == 0) {
246 strncpy(mount_point
, mnt
->mnt_dir
,
250 PRINT_ERR_MSG(NGMSG_EXT4
);
254 PRINT_ERR_MSG("Filesystem is not mounted");
259 * is_ext4() - Whether on an ext4 filesystem.
261 * @file: the file's name.
263 static int is_ext4(const char *file
, char *devname
)
268 char *mnt_type
= NULL
;
269 /* Refer to /etc/mtab */
270 const char *mtab
= MOUNTED
;
271 char file_path
[PATH_MAX
+ 1];
272 struct mntent
*mnt
= NULL
;
273 struct statfs64 fsbuf
;
276 if (realpath(file
, file_path
) == NULL
) {
277 perror("Couldn't get full path");
278 PRINT_FILE_NAME(file
);
282 if (statfs64(file_path
, &fsbuf
) < 0) {
283 perror("Failed to get filesystem information");
284 PRINT_FILE_NAME(file
);
288 if (fsbuf
.f_type
!= EXT4_SUPER_MAGIC
) {
289 PRINT_ERR_MSG(NGMSG_EXT4
);
293 fp
= setmntent(mtab
, "r");
295 perror("Couldn't access /etc/mtab");
299 while ((mnt
= getmntent(fp
)) != NULL
) {
300 if (mnt
->mnt_fsname
[0] != '/')
302 len
= strlen(mnt
->mnt_dir
);
303 ret
= memcmp(file_path
, mnt
->mnt_dir
, len
);
312 mnt_type
= realloc(mnt_type
, strlen(mnt
->mnt_type
) + 1);
313 if (mnt_type
== NULL
) {
317 memset(mnt_type
, 0, strlen(mnt
->mnt_type
) + 1);
318 strncpy(mnt_type
, mnt
->mnt_type
, strlen(mnt
->mnt_type
));
319 strncpy(lost_found_dir
, mnt
->mnt_dir
, PATH_MAX
);
320 strncpy(devname
, mnt
->mnt_fsname
, strlen(mnt
->mnt_fsname
) + 1);
324 if (mnt_type
&& strcmp(mnt_type
, FS_EXT4
) == 0) {
329 PRINT_ERR_MSG(NGMSG_EXT4
);
335 * calc_entry_counts() - Calculate file counts.
340 * @ftwbuf: the pointer of a struct FTW.
342 static int calc_entry_counts(const char *file
EXT2FS_ATTR((unused
)),
343 const struct stat64
*buf
, int flag
EXT2FS_ATTR((unused
)),
344 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
346 if (S_ISREG(buf
->st_mode
))
355 * page_in_core() - Get information on whether pages are in core.
357 * @fd: defrag target file's descriptor.
358 * @defrag_data: data used for defrag.
359 * @vec: page state array.
360 * @page_num: page number.
362 static int page_in_core(int fd
, struct move_extent defrag_data
,
363 unsigned char **vec
, unsigned int *page_num
)
367 loff_t offset
, end_offset
, length
;
369 if (vec
== NULL
|| *vec
!= NULL
)
372 pagesize
= sysconf(_SC_PAGESIZE
);
375 /* In mmap, offset should be a multiple of the page size */
376 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
377 length
= (loff_t
)defrag_data
.len
* block_size
;
378 end_offset
= offset
+ length
;
379 /* Round the offset down to the nearest multiple of pagesize */
380 offset
= (offset
/ pagesize
) * pagesize
;
381 length
= end_offset
- offset
;
383 page
= mmap(NULL
, length
, PROT_READ
, MAP_SHARED
, fd
, offset
);
384 if (page
== MAP_FAILED
)
388 *page_num
= (length
+ pagesize
- 1) / pagesize
;
389 *vec
= (unsigned char *)calloc(*page_num
, 1);
391 munmap(page
, length
);
395 /* Get information on whether pages are in core */
396 if (mincore(page
, (size_t)length
, *vec
) == -1 ||
397 munmap(page
, length
) == -1) {
406 * defrag_fadvise() - Predeclare an access pattern for file data.
408 * @fd: defrag target file's descriptor.
409 * @defrag_data: data used for defrag.
410 * @vec: page state array.
411 * @page_num: page number.
413 static int defrag_fadvise(int fd
, struct move_extent defrag_data
,
414 unsigned char *vec
, unsigned int page_num
)
417 long pagesize
= sysconf(_SC_PAGESIZE
);
418 int fadvise_flag
= POSIX_FADV_DONTNEED
;
419 int sync_flag
= SYNC_FILE_RANGE_WAIT_BEFORE
|
420 SYNC_FILE_RANGE_WRITE
|
421 SYNC_FILE_RANGE_WAIT_AFTER
;
428 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
429 offset
= (offset
/ pagesize
) * pagesize
;
431 /* Sync file for fadvise process */
432 if (sync_file_range(fd
, offset
,
433 (loff_t
)pagesize
* page_num
, sync_flag
) < 0)
436 /* Try to release buffer cache which this process used,
437 * then other process can use the released buffer
439 for (i
= 0; i
< page_num
; i
++) {
440 if ((vec
[i
] & 0x1) == 0) {
444 if (posix_fadvise(fd
, offset
, pagesize
, fadvise_flag
) < 0) {
445 if ((mode_flag
& DETAIL
) && flag
) {
446 perror("\tFailed to fadvise");
457 * check_free_size() - Check if there's enough disk space.
459 * @fd: defrag target file's descriptor.
461 * @blk_count: file blocks.
463 static int check_free_size(int fd
, const char *file
, ext4_fsblk_t blk_count
)
465 ext4_fsblk_t free_blk_count
;
466 struct statfs64 fsbuf
;
468 if (fstatfs64(fd
, &fsbuf
) < 0) {
469 if (mode_flag
& DETAIL
) {
470 PRINT_FILE_NAME(file
);
471 PRINT_ERR_MSG_WITH_ERRNO(
472 "Failed to get filesystem information");
477 /* Compute free space for root and normal user separately */
478 if (current_uid
== ROOT_UID
)
479 free_blk_count
= fsbuf
.f_bfree
;
481 free_blk_count
= fsbuf
.f_bavail
;
483 if (free_blk_count
>= blk_count
)
490 * file_frag_count() - Get file fragment count.
492 * @fd: defrag target file's descriptor.
494 static int file_frag_count(int fd
)
497 struct fiemap fiemap_buf
;
499 /* When fm_extent_count is 0,
500 * ioctl just get file fragment count.
502 memset(&fiemap_buf
, 0, sizeof(struct fiemap
));
503 fiemap_buf
.fm_start
= 0;
504 fiemap_buf
.fm_length
= FIEMAP_MAX_OFFSET
;
505 fiemap_buf
.fm_flags
|= FIEMAP_FLAG_SYNC
;
507 ret
= ioctl(fd
, FS_IOC_FIEMAP
, &fiemap_buf
);
511 return fiemap_buf
.fm_mapped_extents
;
515 * file_check() - Check file's attributes.
517 * @fd: defrag target file's descriptor.
518 * @buf: a pointer of the struct stat64.
520 * @extents: file extents.
521 * @blk_count: file blocks.
523 static int file_check(int fd
, const struct stat64
*buf
, const char *file
,
524 int extents
, ext4_fsblk_t blk_count
)
529 /* Write-lock check is more reliable */
530 lock
.l_type
= F_WRLCK
;
532 lock
.l_whence
= SEEK_SET
;
536 ret
= check_free_size(fd
, file
, blk_count
);
538 if ((mode_flag
& DETAIL
) && ret
== -ENOSPC
) {
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 "Defrag size is larger than filesystem's free space");
548 /* Access authority */
549 if (current_uid
!= ROOT_UID
&&
550 buf
->st_uid
!= current_uid
) {
551 if (mode_flag
& DETAIL
) {
552 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
553 " extents: %d -> %d\n", defraged_file_count
,
554 total_count
, file
, extents
, extents
);
555 IN_FTW_PRINT_ERR_MSG(
556 "File is not current user's file"
557 " or current user is not root");
563 if (fcntl(fd
, F_GETLK
, &lock
) < 0) {
564 if (mode_flag
& DETAIL
) {
565 PRINT_FILE_NAME(file
);
566 PRINT_ERR_MSG_WITH_ERRNO(
567 "Failed to get lock information");
570 } else if (lock
.l_type
!= F_UNLCK
) {
571 if (mode_flag
& DETAIL
) {
572 PRINT_FILE_NAME(file
);
573 IN_FTW_PRINT_ERR_MSG("File has been locked");
582 * insert_extent_by_logical() - Sequentially insert extent by logical.
584 * @ext_list_head: the head of logical extent list.
585 * @ext: the extent element which will be inserted.
587 static int insert_extent_by_logical(struct fiemap_extent_list
**ext_list_head
,
588 struct fiemap_extent_list
*ext
)
590 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
596 if (*ext_list_head
== NULL
) {
597 (*ext_list_head
) = ext
;
598 (*ext_list_head
)->prev
= *ext_list_head
;
599 (*ext_list_head
)->next
= *ext_list_head
;
603 if (ext
->data
.logical
<= ext_list_tmp
->data
.logical
) {
604 /* Insert before head */
605 if (ext_list_tmp
->data
.logical
<
606 ext
->data
.logical
+ ext
->data
.len
)
610 *ext_list_head
= ext
;
612 /* Insert into the middle or last of the list */
614 if (ext
->data
.logical
< ext_list_tmp
->data
.logical
)
616 ext_list_tmp
= ext_list_tmp
->next
;
617 } while (ext_list_tmp
!= (*ext_list_head
));
618 if (ext
->data
.logical
<
619 ext_list_tmp
->prev
->data
.logical
+
620 ext_list_tmp
->prev
->data
.len
)
624 if (ext_list_tmp
!= *ext_list_head
&&
625 ext_list_tmp
->data
.logical
<
626 ext
->data
.logical
+ ext
->data
.len
)
630 ext_list_tmp
= ext_list_tmp
->prev
;
631 /* Insert "ext" after "ext_list_tmp" */
632 insert(ext_list_tmp
, ext
);
640 * insert_extent_by_physical() - Sequentially insert extent by physical.
642 * @ext_list_head: the head of physical extent list.
643 * @ext: the extent element which will be inserted.
645 static int insert_extent_by_physical(struct fiemap_extent_list
**ext_list_head
,
646 struct fiemap_extent_list
*ext
)
648 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
654 if (*ext_list_head
== NULL
) {
655 (*ext_list_head
) = ext
;
656 (*ext_list_head
)->prev
= *ext_list_head
;
657 (*ext_list_head
)->next
= *ext_list_head
;
661 if (ext
->data
.physical
<= ext_list_tmp
->data
.physical
) {
662 /* Insert before head */
663 if (ext_list_tmp
->data
.physical
<
664 ext
->data
.physical
+ ext
->data
.len
)
668 *ext_list_head
= ext
;
670 /* Insert into the middle or last of the list */
672 if (ext
->data
.physical
< ext_list_tmp
->data
.physical
)
674 ext_list_tmp
= ext_list_tmp
->next
;
675 } while (ext_list_tmp
!= (*ext_list_head
));
676 if (ext
->data
.physical
<
677 ext_list_tmp
->prev
->data
.physical
+
678 ext_list_tmp
->prev
->data
.len
)
682 if (ext_list_tmp
!= *ext_list_head
&&
683 ext_list_tmp
->data
.physical
<
684 ext
->data
.physical
+ ext
->data
.len
)
688 ext_list_tmp
= ext_list_tmp
->prev
;
689 /* Insert "ext" after "ext_list_tmp" */
690 insert(ext_list_tmp
, ext
);
698 * insert_exts_group() - Insert a exts_group.
700 * @ext_group_head: the head of a exts_group list.
701 * @exts_group: the exts_group element which will be inserted.
703 static int insert_exts_group(struct fiemap_extent_group
**ext_group_head
,
704 struct fiemap_extent_group
*exts_group
)
706 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
708 if (exts_group
== NULL
) {
713 /* Initialize list */
714 if (*ext_group_head
== NULL
) {
715 (*ext_group_head
) = exts_group
;
716 (*ext_group_head
)->prev
= *ext_group_head
;
717 (*ext_group_head
)->next
= *ext_group_head
;
721 ext_group_tmp
= (*ext_group_head
)->prev
;
722 insert(ext_group_tmp
, exts_group
);
728 * join_extents() - Find continuous region(exts_group).
730 * @ext_list_head: the head of the extent list.
731 * @ext_group_head: the head of the target exts_group list.
733 static int join_extents(struct fiemap_extent_list
*ext_list_head
,
734 struct fiemap_extent_group
**ext_group_head
)
736 __u64 len
= ext_list_head
->data
.len
;
737 struct fiemap_extent_list
*ext_list_start
= ext_list_head
;
738 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
->next
;
741 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
743 /* This extent and previous extent are not continuous,
744 * so, all previous extents are treated as an extent group.
746 if ((ext_list_tmp
->prev
->data
.logical
+
747 ext_list_tmp
->prev
->data
.len
)
748 != ext_list_tmp
->data
.logical
) {
750 malloc(sizeof(struct fiemap_extent_group
));
751 if (ext_group_tmp
== NULL
)
754 memset(ext_group_tmp
, 0,
755 sizeof(struct fiemap_extent_group
));
756 ext_group_tmp
->len
= len
;
757 ext_group_tmp
->start
= ext_list_start
;
758 ext_group_tmp
->end
= ext_list_tmp
->prev
;
760 if (insert_exts_group(ext_group_head
,
761 ext_group_tmp
) < 0) {
765 ext_list_start
= ext_list_tmp
;
766 len
= ext_list_tmp
->data
.len
;
767 ext_list_tmp
= ext_list_tmp
->next
;
771 /* This extent and previous extent are continuous,
772 * so, they belong to the same extent group, and we check
773 * if the next extent belongs to the same extent group.
775 len
+= ext_list_tmp
->data
.len
;
776 ext_list_tmp
= ext_list_tmp
->next
;
777 } while (ext_list_tmp
!= ext_list_head
->next
);
783 * get_file_extents() - Get file's extent list.
785 * @fd: defrag target file's descriptor.
786 * @ext_list_head: the head of the extent list.
788 static int get_file_extents(int fd
, struct fiemap_extent_list
**ext_list_head
)
792 int ext_buf_size
, fie_buf_size
;
794 struct fiemap
*fiemap_buf
= NULL
;
795 struct fiemap_extent
*ext_buf
= NULL
;
796 struct fiemap_extent_list
*ext_list
= NULL
;
798 /* Convert units, in bytes.
799 * Be careful : now, physical block number in extent is 48bit,
800 * and the maximum blocksize for ext4 is 4K(12bit),
801 * so there is no overflow, but in future it may be changed.
804 /* Alloc space for fiemap */
805 ext_buf_size
= EXTENT_MAX_COUNT
* sizeof(struct fiemap_extent
);
806 fie_buf_size
= sizeof(struct fiemap
) + ext_buf_size
;
808 fiemap_buf
= malloc(fie_buf_size
);
809 if (fiemap_buf
== NULL
)
812 ext_buf
= fiemap_buf
->fm_extents
;
813 memset(fiemap_buf
, 0, fie_buf_size
);
814 fiemap_buf
->fm_length
= FIEMAP_MAX_OFFSET
;
815 fiemap_buf
->fm_flags
|= FIEMAP_FLAG_SYNC
;
816 fiemap_buf
->fm_extent_count
= EXTENT_MAX_COUNT
;
819 fiemap_buf
->fm_start
= pos
;
820 memset(ext_buf
, 0, ext_buf_size
);
821 ret
= ioctl(fd
, FS_IOC_FIEMAP
, fiemap_buf
);
822 if (ret
< 0 || fiemap_buf
->fm_mapped_extents
== 0)
824 for (i
= 0; i
< fiemap_buf
->fm_mapped_extents
; i
++) {
826 ext_list
= malloc(sizeof(struct fiemap_extent_list
));
827 if (ext_list
== NULL
)
830 ext_list
->data
.physical
= ext_buf
[i
].fe_physical
832 ext_list
->data
.logical
= ext_buf
[i
].fe_logical
834 ext_list
->data
.len
= ext_buf
[i
].fe_length
837 ret
= insert_extent_by_physical(
838 ext_list_head
, ext_list
);
844 /* Record file's logical offset this time */
845 pos
= ext_buf
[EXTENT_MAX_COUNT
-1].fe_logical
+
846 ext_buf
[EXTENT_MAX_COUNT
-1].fe_length
;
848 * If fm_extents array has been filled and
849 * there are extents left, continue to cycle.
851 } while (fiemap_buf
->fm_mapped_extents
852 == EXTENT_MAX_COUNT
&&
853 !(ext_buf
[EXTENT_MAX_COUNT
-1].fe_flags
854 & FIEMAP_EXTENT_LAST
));
864 * get_logical_count() - Get the file logical extents count.
866 * @logical_list_head: the head of the logical extent list.
868 static int get_logical_count(struct fiemap_extent_list
*logical_list_head
)
871 struct fiemap_extent_list
*ext_list_tmp
= logical_list_head
;
875 ext_list_tmp
= ext_list_tmp
->next
;
876 } while (ext_list_tmp
!= logical_list_head
);
882 * get_physical_count() - Get the file physical extents count.
884 * @physical_list_head: the head of the physical extent list.
886 static int get_physical_count(struct fiemap_extent_list
*physical_list_head
)
889 struct fiemap_extent_list
*ext_list_tmp
= physical_list_head
;
892 if ((ext_list_tmp
->data
.physical
+ ext_list_tmp
->data
.len
)
893 != ext_list_tmp
->next
->data
.physical
||
894 (ext_list_tmp
->data
.logical
+ ext_list_tmp
->data
.len
)
895 != ext_list_tmp
->next
->data
.logical
) {
896 /* This extent and next extent are not continuous. */
900 ext_list_tmp
= ext_list_tmp
->next
;
901 } while (ext_list_tmp
!= physical_list_head
);
907 * change_physical_to_logical() - Change list from physical to logical.
909 * @physical_list_head: the head of physical extent list.
910 * @logical_list_head: the head of logical extent list.
912 static int change_physical_to_logical(
913 struct fiemap_extent_list
**physical_list_head
,
914 struct fiemap_extent_list
**logical_list_head
)
917 struct fiemap_extent_list
*ext_list_tmp
= *physical_list_head
;
918 struct fiemap_extent_list
*ext_list_next
= ext_list_tmp
->next
;
921 if (ext_list_tmp
== ext_list_next
) {
922 ret
= insert_extent_by_logical(
923 logical_list_head
, ext_list_tmp
);
927 *physical_list_head
= NULL
;
931 ext_list_tmp
->prev
->next
= ext_list_tmp
->next
;
932 ext_list_tmp
->next
->prev
= ext_list_tmp
->prev
;
933 *physical_list_head
= ext_list_next
;
935 ret
= insert_extent_by_logical(
936 logical_list_head
, ext_list_tmp
);
941 ext_list_tmp
= ext_list_next
;
942 ext_list_next
= ext_list_next
->next
;
948 /* get_file_blocks() - Get total file blocks.
950 * @ext_list_head: the extent list head of the target file
952 static ext4_fsblk_t
get_file_blocks(struct fiemap_extent_list
*ext_list_head
)
954 ext4_fsblk_t blk_count
= 0;
955 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
;
958 blk_count
+= ext_list_tmp
->data
.len
;
959 ext_list_tmp
= ext_list_tmp
->next
;
960 } while (ext_list_tmp
!= ext_list_head
);
966 * free_ext() - Free the extent list.
968 * @ext_list_head: the extent list head of which will be free.
970 static void free_ext(struct fiemap_extent_list
*ext_list_head
)
972 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
974 if (ext_list_head
== NULL
)
977 while (ext_list_head
->next
!= ext_list_head
) {
978 ext_list_tmp
= ext_list_head
;
979 ext_list_head
->prev
->next
= ext_list_head
->next
;
980 ext_list_head
->next
->prev
= ext_list_head
->prev
;
981 ext_list_head
= ext_list_head
->next
;
988 * free_exts_group() - Free the exts_group.
990 * @*ext_group_head: the exts_group list head which will be free.
992 static void free_exts_group(struct fiemap_extent_group
*ext_group_head
)
994 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
996 if (ext_group_head
== NULL
)
999 while (ext_group_head
->next
!= ext_group_head
) {
1000 ext_group_tmp
= ext_group_head
;
1001 ext_group_head
->prev
->next
= ext_group_head
->next
;
1002 ext_group_head
->next
->prev
= ext_group_head
->prev
;
1003 ext_group_head
= ext_group_head
->next
;
1004 free(ext_group_tmp
);
1006 free(ext_group_head
);
1010 * get_best_count() - Get the file best extents count.
1012 * @block_count: the file's physical block count.
1014 static int get_best_count(ext4_fsblk_t block_count
)
1017 unsigned int flex_bg_num
;
1019 /* Calcuate best extents count */
1020 if (feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
1021 flex_bg_num
= 1 << log_groups_per_flex
;
1022 ret
= ((block_count
- 1) /
1023 ((ext4_fsblk_t
)blocks_per_group
*
1026 ret
= ((block_count
- 1) / blocks_per_group
) + 1;
1033 * file_statistic() - Get statistic info of the file's fragments.
1035 * @file: the file's name.
1036 * @buf: the pointer of the struct stat64.
1038 * @ftwbuf: the pointer of a struct FTW.
1040 static int file_statistic(const char *file
, const struct stat64
*buf
,
1041 int flag
EXT2FS_ATTR((unused
)),
1042 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1046 int now_ext_count
, best_ext_count
= 0, physical_ext_count
;
1048 __u64 size_per_ext
= 0;
1050 ext4_fsblk_t blk_count
= 0;
1051 char msg_buffer
[PATH_MAX
+ 24];
1052 struct fiemap_extent_list
*physical_list_head
= NULL
;
1053 struct fiemap_extent_list
*logical_list_head
= NULL
;
1055 defraged_file_count
++;
1057 if (mode_flag
& DETAIL
) {
1058 if (total_count
== 1 && regular_count
== 1)
1061 printf("[%u/%u]", defraged_file_count
, total_count
);
1065 if (lost_found_dir
[0] != '\0' &&
1066 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1067 if (mode_flag
& DETAIL
) {
1068 PRINT_FILE_NAME(file
);
1069 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND
);
1074 if (!S_ISREG(buf
->st_mode
)) {
1075 if (mode_flag
& DETAIL
) {
1076 PRINT_FILE_NAME(file
);
1077 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG
);
1082 /* Access authority */
1083 if (current_uid
!= ROOT_UID
&&
1084 buf
->st_uid
!= current_uid
) {
1085 if (mode_flag
& DETAIL
) {
1086 PRINT_FILE_NAME(file
);
1088 "File is not current user's file"
1089 " or current user is not root");
1095 if (buf
->st_size
== 0) {
1096 if (mode_flag
& DETAIL
) {
1097 PRINT_FILE_NAME(file
);
1098 STATISTIC_ERR_MSG("File size is 0");
1104 if (buf
->st_blocks
== 0) {
1105 if (mode_flag
& DETAIL
) {
1106 PRINT_FILE_NAME(file
);
1107 STATISTIC_ERR_MSG("File has no blocks");
1112 fd
= open64(file
, O_RDONLY
);
1114 if (mode_flag
& DETAIL
) {
1115 PRINT_FILE_NAME(file
);
1116 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1121 /* Get file's physical extents */
1122 ret
= get_file_extents(fd
, &physical_list_head
);
1124 if (mode_flag
& DETAIL
) {
1125 PRINT_FILE_NAME(file
);
1126 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1131 /* Get the count of file's continuous physical region */
1132 physical_ext_count
= get_physical_count(physical_list_head
);
1134 /* Change list from physical to logical */
1135 ret
= change_physical_to_logical(&physical_list_head
,
1136 &logical_list_head
);
1138 if (mode_flag
& DETAIL
) {
1139 PRINT_FILE_NAME(file
);
1140 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1145 /* Count file fragments before defrag */
1146 now_ext_count
= get_logical_count(logical_list_head
);
1148 if (current_uid
== ROOT_UID
) {
1149 /* Calculate the size per extent */
1150 blk_count
= get_file_blocks(logical_list_head
);
1152 best_ext_count
= get_best_count(blk_count
);
1154 /* e4defrag rounds size_per_ext up to a block size boundary */
1155 size_per_ext
= blk_count
* (buf
->st_blksize
/ 1024) /
1158 ratio
= (float)(physical_ext_count
- best_ext_count
) * 100 /
1161 extents_before_defrag
+= now_ext_count
;
1162 extents_after_defrag
+= best_ext_count
;
1163 files_block_count
+= blk_count
;
1166 if (total_count
== 1 && regular_count
== 1) {
1168 if (mode_flag
& DETAIL
) {
1170 struct fiemap_extent_list
*ext_list_tmp
=
1173 /* Print extents info */
1176 printf("[ext %d]:\tstart %llu:\tlogical "
1177 "%llu:\tlen %llu\n", count
,
1178 ext_list_tmp
->data
.physical
,
1179 ext_list_tmp
->data
.logical
,
1180 ext_list_tmp
->data
.len
);
1181 ext_list_tmp
= ext_list_tmp
->next
;
1182 } while (ext_list_tmp
!= logical_list_head
);
1185 printf("%-40s%10s/%-10s%9s\n",
1186 "<File>", "now", "best", "size/ext");
1187 if (current_uid
== ROOT_UID
) {
1188 if (strlen(file
) > 40)
1189 printf("%s\n%50d/%-10d%6llu KB\n",
1190 file
, now_ext_count
,
1191 best_ext_count
, size_per_ext
);
1193 printf("%-40s%10d/%-10d%6llu KB\n",
1194 file
, now_ext_count
,
1195 best_ext_count
, size_per_ext
);
1197 if (strlen(file
) > 40)
1198 printf("%s\n%50d/%-10s%7s\n",
1199 file
, now_ext_count
,
1202 printf("%-40s%10d/%-10s%7s\n",
1203 file
, now_ext_count
,
1211 if (mode_flag
& DETAIL
) {
1212 /* Print statistic info */
1213 sprintf(msg_buffer
, "[%u/%u]%s",
1214 defraged_file_count
, total_count
, file
);
1215 if (current_uid
== ROOT_UID
) {
1216 if (strlen(msg_buffer
) > 40)
1217 printf("\033[79;0H\033[K%s\n"
1218 "%50d/%-10d%6llu KB\n",
1219 msg_buffer
, now_ext_count
,
1220 best_ext_count
, size_per_ext
);
1222 printf("\033[79;0H\033[K%-40s"
1223 "%10d/%-10d%6llu KB\n",
1224 msg_buffer
, now_ext_count
,
1225 best_ext_count
, size_per_ext
);
1227 if (strlen(msg_buffer
) > 40)
1228 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1229 msg_buffer
, now_ext_count
,
1232 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1233 msg_buffer
, now_ext_count
,
1238 for (i
= 0; i
< SHOW_FRAG_FILES
; i
++) {
1239 if (ratio
>= frag_rank
[i
].ratio
) {
1240 for (j
= SHOW_FRAG_FILES
- 1; j
> i
; j
--) {
1241 memset(&frag_rank
[j
], 0,
1242 sizeof(struct frag_statistic_ino
));
1243 strncpy(frag_rank
[j
].msg_buffer
,
1244 frag_rank
[j
- 1].msg_buffer
,
1245 strnlen(frag_rank
[j
- 1].msg_buffer
,
1247 frag_rank
[j
].now_count
=
1248 frag_rank
[j
- 1].now_count
;
1249 frag_rank
[j
].best_count
=
1250 frag_rank
[j
- 1].best_count
;
1251 frag_rank
[j
].size_per_ext
=
1252 frag_rank
[j
- 1].size_per_ext
;
1253 frag_rank
[j
].ratio
=
1254 frag_rank
[j
- 1].ratio
;
1256 memset(&frag_rank
[i
], 0,
1257 sizeof(struct frag_statistic_ino
));
1258 strncpy(frag_rank
[i
].msg_buffer
, file
,
1259 strnlen(file
, PATH_MAX
));
1260 frag_rank
[i
].now_count
= now_ext_count
;
1261 frag_rank
[i
].best_count
= best_ext_count
;
1262 frag_rank
[i
].size_per_ext
= size_per_ext
;
1263 frag_rank
[i
].ratio
= ratio
;
1272 free_ext(physical_list_head
);
1273 free_ext(logical_list_head
);
1278 * print_progress - Print defrag progress
1281 * @start: logical offset for defrag target file
1282 * @file_size: defrag target filesize
1284 static void print_progress(const char *file
, loff_t start
, 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
)
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
++;
1423 if (mode_flag
& DETAIL
) {
1424 printf("[%u/%u]", defraged_file_count
, total_count
);
1428 if (lost_found_dir
[0] != '\0' &&
1429 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1430 if (mode_flag
& DETAIL
) {
1431 PRINT_FILE_NAME(file
);
1432 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1437 if (!S_ISREG(buf
->st_mode
)) {
1438 if (mode_flag
& DETAIL
) {
1439 PRINT_FILE_NAME(file
);
1440 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1446 if (buf
->st_size
== 0) {
1447 if (mode_flag
& DETAIL
) {
1448 PRINT_FILE_NAME(file
);
1449 IN_FTW_PRINT_ERR_MSG("File size is 0");
1455 if (buf
->st_blocks
== 0) {
1456 if (mode_flag
& DETAIL
) {
1457 PRINT_FILE_NAME(file
);
1458 STATISTIC_ERR_MSG("File has no blocks");
1463 fd
= open64(file
, O_RDWR
);
1465 if (mode_flag
& DETAIL
) {
1466 PRINT_FILE_NAME(file
);
1467 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1472 /* Get file's extents */
1473 ret
= get_file_extents(fd
, &orig_list_physical
);
1475 if (mode_flag
& DETAIL
) {
1476 PRINT_FILE_NAME(file
);
1477 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1482 /* Get the count of file's continuous physical region */
1483 orig_physical_cnt
= get_physical_count(orig_list_physical
);
1485 /* Change list from physical to logical */
1486 ret
= change_physical_to_logical(&orig_list_physical
,
1487 &orig_list_logical
);
1489 if (mode_flag
& DETAIL
) {
1490 PRINT_FILE_NAME(file
);
1491 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1496 /* Count file fragments before defrag */
1497 file_frags_start
= get_logical_count(orig_list_logical
);
1499 blk_count
= get_file_blocks(orig_list_logical
);
1500 if (file_check(fd
, buf
, file
, file_frags_start
, blk_count
) < 0)
1503 if (fsync(fd
) < 0) {
1504 if (mode_flag
& DETAIL
) {
1505 PRINT_FILE_NAME(file
);
1506 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1511 if (current_uid
== ROOT_UID
)
1512 best
= get_best_count(blk_count
);
1516 if (file_frags_start
<= best
)
1517 goto check_improvement
;
1519 /* Combine extents to group */
1520 ret
= join_extents(orig_list_logical
, &orig_group_head
);
1522 if (mode_flag
& DETAIL
) {
1523 PRINT_FILE_NAME(file
);
1524 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1529 /* Create donor inode */
1530 memset(tmp_inode_name
, 0, PATH_MAX
+ 8);
1531 sprintf(tmp_inode_name
, "%.*s.defrag",
1532 (int)strnlen(file
, PATH_MAX
), file
);
1533 donor_fd
= open64(tmp_inode_name
, O_WRONLY
| O_CREAT
| O_EXCL
, S_IRUSR
);
1535 if (mode_flag
& DETAIL
) {
1536 PRINT_FILE_NAME(file
);
1537 if (errno
== EEXIST
)
1538 PRINT_ERR_MSG_WITH_ERRNO(
1539 "File is being defraged by other program");
1541 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1546 /* Unlink donor inode */
1547 ret
= unlink(tmp_inode_name
);
1549 if (mode_flag
& DETAIL
) {
1550 PRINT_FILE_NAME(file
);
1551 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1556 /* Allocate space for donor inode */
1557 orig_group_tmp
= orig_group_head
;
1559 ret
= fallocate64(donor_fd
, 0,
1560 (loff_t
)orig_group_tmp
->start
->data
.logical
* block_size
,
1561 (loff_t
)orig_group_tmp
->len
* block_size
);
1563 if (mode_flag
& DETAIL
) {
1564 PRINT_FILE_NAME(file
);
1565 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1570 orig_group_tmp
= orig_group_tmp
->next
;
1571 } while (orig_group_tmp
!= orig_group_head
);
1573 /* Get donor inode's extents */
1574 ret
= get_file_extents(donor_fd
, &donor_list_physical
);
1576 if (mode_flag
& DETAIL
) {
1577 PRINT_FILE_NAME(file
);
1578 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1583 /* Calcuate donor inode's continuous physical region */
1584 donor_physical_cnt
= get_physical_count(donor_list_physical
);
1586 /* Change donor extent list from physical to logical */
1587 ret
= change_physical_to_logical(&donor_list_physical
,
1588 &donor_list_logical
);
1590 if (mode_flag
& DETAIL
) {
1591 PRINT_FILE_NAME(file
);
1592 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1598 if (mode_flag
& DETAIL
) {
1599 if (file_frags_start
!= 1)
1600 frag_files_before_defrag
++;
1602 extents_before_defrag
+= file_frags_start
;
1605 if (file_frags_start
<= best
||
1606 orig_physical_cnt
<= donor_physical_cnt
) {
1607 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1608 defraged_file_count
, total_count
, file
, 100);
1609 if (mode_flag
& DETAIL
)
1610 printf(" extents: %d -> %d",
1611 file_frags_start
, file_frags_start
);
1613 printf("\t[ OK ]\n");
1616 if (file_frags_start
!= 1)
1617 frag_files_after_defrag
++;
1619 extents_after_defrag
+= file_frags_start
;
1623 /* Defrag the file */
1624 ret
= call_defrag(fd
, donor_fd
, file
, buf
, donor_list_logical
);
1626 /* Count file fragments after defrag and print extents info */
1627 if (mode_flag
& DETAIL
) {
1628 file_frags_end
= file_frag_count(fd
);
1629 if (file_frags_end
< 0) {
1631 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO
);
1635 if (file_frags_end
!= 1)
1636 frag_files_after_defrag
++;
1638 extents_after_defrag
+= file_frags_end
;
1643 printf(" extents: %d -> %d",
1644 file_frags_start
, file_frags_end
);
1651 printf("\t[ OK ]\n");
1659 free_ext(orig_list_physical
);
1660 free_ext(orig_list_logical
);
1661 free_ext(donor_list_physical
);
1662 free_exts_group(orig_group_head
);
1667 * main() - Ext4 online defrag.
1669 * @argc: the number of parameter.
1670 * @argv[]: the pointer array of parameter.
1672 int main(int argc
, char *argv
[])
1676 int flags
= FTW_PHYS
| FTW_MOUNT
;
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 /* Parse arguments */
1688 while ((opt
= getopt(argc
, argv
, "vc")) != EOF
) {
1691 mode_flag
|= DETAIL
;
1694 mode_flag
|= STATISTIC
;
1704 current_uid
= getuid();
1707 for (i
= optind
; i
< argc
; i
++) {
1711 frag_files_before_defrag
= 0;
1712 frag_files_after_defrag
= 0;
1713 extents_before_defrag
= 0;
1714 extents_after_defrag
= 0;
1715 defraged_file_count
= 0;
1716 files_block_count
= 0;
1717 blocks_per_group
= 0;
1718 feature_incompat
= 0;
1719 log_groups_per_flex
= 0;
1721 memset(dir_name
, 0, PATH_MAX
+ 1);
1722 memset(dev_name
, 0, PATH_MAX
+ 1);
1723 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1724 memset(frag_rank
, 0,
1725 sizeof(struct frag_statistic_ino
) * SHOW_FRAG_FILES
);
1727 if ((mode_flag
& STATISTIC
) && i
> optind
)
1730 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1731 PRINT_ERR_MSG("Endian's type is not big/little endian");
1732 PRINT_FILE_NAME(argv
[i
]);
1736 if (lstat64(argv
[i
], &buf
) < 0) {
1737 perror(NGMSG_FILE_INFO
);
1738 PRINT_FILE_NAME(argv
[i
]);
1742 /* Handle i.e. lvm device symlinks */
1743 if (S_ISLNK(buf
.st_mode
)) {
1746 if (stat64(argv
[i
], &buf2
) == 0 &&
1747 S_ISBLK(buf2
.st_mode
))
1751 if (S_ISBLK(buf
.st_mode
)) {
1753 strncpy(dev_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1754 if (get_mount_point(argv
[i
], dir_name
, PATH_MAX
) < 0)
1756 if (lstat64(dir_name
, &buf
) < 0) {
1757 perror(NGMSG_FILE_INFO
);
1758 PRINT_FILE_NAME(argv
[i
]);
1762 if (!(mode_flag
& STATISTIC
))
1763 printf("ext4 defragmentation for device(%s)\n",
1765 } else if (S_ISDIR(buf
.st_mode
)) {
1767 if (access(argv
[i
], R_OK
) < 0) {
1772 strncpy(dir_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1773 } else if (S_ISREG(buf
.st_mode
)) {
1775 arg_type
= FILENAME
;
1777 /* Irregular file */
1778 PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1779 PRINT_FILE_NAME(argv
[i
]);
1784 block_size
= buf
.st_blksize
;
1787 * filesystem type checked in get_mount_point()
1789 if (arg_type
== FILENAME
|| arg_type
== DIRNAME
) {
1790 if (is_ext4(argv
[i
], dev_name
) < 0)
1792 if (realpath(argv
[i
], dir_name
) == NULL
) {
1793 perror("Couldn't get full path");
1794 PRINT_FILE_NAME(argv
[i
]);
1799 if (current_uid
== ROOT_UID
) {
1800 /* Get super block info */
1801 ret
= ext2fs_open(dev_name
, EXT2_FLAG_64BITS
, 0,
1802 block_size
, unix_io_manager
, &fs
);
1804 if (mode_flag
& DETAIL
)
1805 com_err(argv
[1], ret
,
1806 "while trying to open file system: %s",
1811 blocks_per_group
= fs
->super
->s_blocks_per_group
;
1812 feature_incompat
= fs
->super
->s_feature_incompat
;
1813 log_groups_per_flex
= fs
->super
->s_log_groups_per_flex
;
1815 ext2fs_close_free(&fs
);
1819 int mount_dir_len
= 0;
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_piont_dir") */
1832 if (dir_name
[mount_dir_len
] != '\0') {
1834 * "e4defrag mount_piont_dir/lost+found"
1835 * or "e4defrag mount_piont_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_piont_dir/else_dir" */
1850 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1853 if (arg_type
== DEVNAME
) {
1854 strncpy(lost_found_dir
, dir_name
,
1855 strnlen(dir_name
, PATH_MAX
));
1856 strncat(lost_found_dir
, "/lost+found/",
1857 PATH_MAX
- strnlen(lost_found_dir
,
1861 nftw64(dir_name
, calc_entry_counts
, FTW_OPEN_FD
, flags
);
1863 if (mode_flag
& STATISTIC
) {
1864 if (mode_flag
& DETAIL
)
1865 printf("%-40s%10s/%-10s%9s\n",
1866 "<File>", "now", "best", "size/ext");
1868 if (!(mode_flag
& DETAIL
) &&
1869 current_uid
!= ROOT_UID
) {
1875 nftw64(dir_name
, file_statistic
,
1876 FTW_OPEN_FD
, flags
);
1878 if (succeed_cnt
!= 0 &&
1879 current_uid
== ROOT_UID
) {
1880 if (mode_flag
& DETAIL
)
1882 printf("%-40s%10s/%-10s%9s\n",
1883 "<Fragmented files>", "now",
1884 "best", "size/ext");
1885 for (j
= 0; j
< SHOW_FRAG_FILES
; j
++) {
1886 if (strlen(frag_rank
[j
].
1888 printf("%d. %s\n%50d/"
1891 frag_rank
[j
].msg_buffer
,
1892 frag_rank
[j
].now_count
,
1893 frag_rank
[j
].best_count
,
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
,
1912 /* File tree walk */
1913 nftw64(dir_name
, file_defrag
, FTW_OPEN_FD
, flags
);
1914 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt
,
1916 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1917 total_count
- succeed_cnt
, total_count
);
1918 if (mode_flag
& DETAIL
) {
1919 printf("\tTotal extents:\t\t\t%4d->%d\n",
1920 extents_before_defrag
,
1921 extents_after_defrag
);
1922 printf("\tFragmented percentage:\t\t"
1923 "%3llu%%->%llu%%\n",
1924 !regular_count
? 0 :
1925 ((unsigned long long)
1926 frag_files_before_defrag
* 100) /
1928 !regular_count
? 0 :
1929 ((unsigned long long)
1930 frag_files_after_defrag
* 100) /
1937 strncat(lost_found_dir
, "/lost+found/",
1938 PATH_MAX
- strnlen(lost_found_dir
,
1940 if (strncmp(lost_found_dir
, dir_name
,
1941 strnlen(lost_found_dir
,
1943 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1944 PRINT_FILE_NAME(argv
[i
]);
1948 if (mode_flag
& STATISTIC
) {
1949 file_statistic(argv
[i
], &buf
, FTW_F
, NULL
);
1952 printf("ext4 defragmentation for %s\n",
1954 /* Defrag single file process */
1955 file_defrag(argv
[i
], &buf
, FTW_F
, NULL
);
1956 if (succeed_cnt
!= 0)
1957 printf(" Success:\t\t\t[1/1]\n");
1959 printf(" Success:\t\t\t[0/1]\n");
1964 if (succeed_cnt
!= 0)
1966 if (mode_flag
& STATISTIC
) {
1967 if (current_uid
!= ROOT_UID
) {
1973 if (mode_flag
& DETAIL
)
1976 if (arg_type
== DEVNAME
)
1977 printf(" In this device(%s), "
1978 "none can be defragmented.\n", argv
[i
]);
1979 else if (arg_type
== DIRNAME
)
1980 printf(" In this directory(%s), "
1981 "none can be defragmented.\n", argv
[i
]);
1983 printf(" This file(%s) "
1984 "can't be defragmented.\n", argv
[i
]);
1986 float files_ratio
= 0.0;
1988 __u64 size_per_ext
= files_block_count
*
1989 (buf
.st_blksize
/ 1024) /
1990 extents_before_defrag
;
1991 files_ratio
= (float)(extents_before_defrag
-
1992 extents_after_defrag
) *
1993 100 / files_block_count
;
1994 score
= CALC_SCORE(files_ratio
);
1995 printf("\n Total/best extents\t\t\t\t%d/%d\n"
1996 " Average size per extent"
1998 " Fragmentation score\t\t\t\t%.0f\n",
1999 extents_before_defrag
,
2000 extents_after_defrag
,
2001 size_per_ext
, score
);
2002 printf(" [0-30 no problem:"
2003 " 31-55 a little bit fragmented:"
2004 " 56- needs defrag]\n");
2006 if (arg_type
== DEVNAME
)
2007 printf(" This device (%s) ", argv
[i
]);
2008 else if (arg_type
== DIRNAME
)
2009 printf(" This directory (%s) ",
2012 printf(" This file (%s) ", argv
[i
]);
2014 if (score
> BOUND_SCORE
)
2015 printf("needs defragmentation.\n");
2017 printf("does not need "
2018 "defragmentation.\n");