2 * e4defrag.c - ext4 filesystem defragmenter
4 * Copyright (C) 2009 NEC Software Tohoku, Ltd.
6 * Author: Akira Fujita <a-fujita@rs.jp.nec.com>
7 * Takashi Sato <t-sato@yk.jp.nec.com>
10 #ifndef _LARGEFILE_SOURCE
11 #define _LARGEFILE_SOURCE
14 #ifndef _LARGEFILE64_SOURCE
15 #define _LARGEFILE64_SOURCE
35 #include <ext2fs/ext2_types.h>
36 #include <ext2fs/ext2fs.h>
37 #include <sys/ioctl.h>
38 #include <ext2fs/fiemap.h>
41 #include <sys/statfs.h>
44 #include "../version.h"
46 /* A relatively new ioctl interface ... */
47 #ifndef EXT4_IOC_MOVE_EXT
48 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
52 #define PRINT_ERR_MSG(msg) fprintf(stderr, "%s\n", (msg))
53 #define IN_FTW_PRINT_ERR_MSG(msg) \
54 fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
55 #define PRINT_FILE_NAME(file) fprintf(stderr, " \"%s\"\n", (file))
56 #define PRINT_ERR_MSG_WITH_ERRNO(msg) \
57 fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
58 #define STATISTIC_ERR_MSG(msg) \
59 fprintf(stderr, "\t%s\n", (msg))
60 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg) \
61 fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
62 #define min(x, y) (((x) > (y)) ? (y) : (x))
63 #define CALC_SCORE(ratio) \
64 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
65 /* Wrap up the free function */
71 /* Insert list2 after list1 */
72 #define insert(list1, list2) \
74 list2->next = list1->next; \
75 list1->next->prev = list2; \
76 list2->prev = list1; \
77 list1->next = list2; \
80 /* To delete unused warning */
82 #define EXT2FS_ATTR(x) __attribute__(x)
84 #define EXT2FS_ATTR(x)
87 /* The mode of defrag */
89 #define STATISTIC 0x02
95 #define FTW_OPEN_FD 2000
97 #define FS_EXT4 "ext4"
100 #define BOUND_SCORE 55
101 #define SHOW_FRAG_FILES 5
103 /* Magic number for ext4 */
104 #define EXT4_SUPER_MAGIC 0xEF53
106 /* Definition of flex_bg */
107 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
109 /* The following macro is used for ioctl FS_IOC_FIEMAP
110 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
111 * kernel-space and user-space per ioctl
113 #define EXTENT_MAX_COUNT 512
115 /* The following macros are error message */
117 "Usage : e4defrag [-v] file...| directory...| device...\n\
118 : e4defrag -c file...| directory...| device...\n"
120 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
121 #define NGMSG_FILE_EXTENT "Failed to get file extents"
122 #define NGMSG_FILE_INFO "Failed to get file information"
123 #define NGMSG_FILE_OPEN "Failed to open"
124 #define NGMSG_FILE_UNREG "File is not regular file"
125 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
127 /* Data type for filesystem-wide blocks number */
128 typedef unsigned long long ext4_fsblk_t
;
130 struct fiemap_extent_data
{
131 __u64 len
; /* blocks count */
132 __u64 logical
; /* start logical block number */
133 ext4_fsblk_t physical
; /* start physical block number */
136 struct fiemap_extent_list
{
137 struct fiemap_extent_list
*prev
;
138 struct fiemap_extent_list
*next
;
139 struct fiemap_extent_data data
; /* extent belong to file */
142 struct fiemap_extent_group
{
143 struct fiemap_extent_group
*prev
;
144 struct fiemap_extent_group
*next
;
145 __u64 len
; /* length of this continuous region */
146 struct fiemap_extent_list
*start
; /* start ext */
147 struct fiemap_extent_list
*end
; /* end ext */
151 __s32 reserved
; /* original file descriptor */
152 __u32 donor_fd
; /* donor file descriptor */
153 __u64 orig_start
; /* logical start offset in block for orig */
154 __u64 donor_start
; /* logical start offset in block for donor */
155 __u64 len
; /* block length to be moved */
156 __u64 moved_len
; /* moved block length */
159 struct frag_statistic_ino
{
160 int now_count
; /* the file's extents count of before defrag */
161 int best_count
; /* the best file's extents count */
162 __u64 size_per_ext
; /* size(KB) per extent */
163 float ratio
; /* the ratio of fragmentation */
164 char msg_buffer
[PATH_MAX
+ 1]; /* pathname of the file */
167 static char lost_found_dir
[PATH_MAX
+ 1];
168 static int block_size
;
169 static int extents_before_defrag
;
170 static int extents_after_defrag
;
171 static int mode_flag
;
172 static unsigned int current_uid
;
173 static unsigned int defraged_file_count
;
174 static unsigned int frag_files_before_defrag
;
175 static unsigned int frag_files_after_defrag
;
176 static unsigned int regular_count
;
177 static unsigned int succeed_cnt
;
178 static unsigned int total_count
;
179 static __u8 log_groups_per_flex
;
180 static __u32 blocks_per_group
;
181 static __u32 feature_incompat
;
182 static ext4_fsblk_t files_block_count
;
183 static struct frag_statistic_ino frag_rank
[SHOW_FRAG_FILES
];
187 * We prefer posix_fadvise64 when available, as it allows 64bit offset on
190 #if defined(HAVE_POSIX_FADVISE64)
191 #define posix_fadvise posix_fadvise64
192 #elif defined(HAVE_FADVISE64)
193 #define posix_fadvise fadvise64
194 #elif !defined(HAVE_POSIX_FADVISE)
195 #error posix_fadvise not available!
198 #ifndef HAVE_FALLOCATE64
199 #error fallocate64 not available!
200 #endif /* ! HAVE_FALLOCATE64 */
203 * get_mount_point() - Get device's mount point.
205 * @devname: the device's name.
206 * @mount_point: the mount point.
207 * @dir_path_len: the length of directory.
209 static int get_mount_point(const char *devname
, char *mount_point
,
212 /* Refer to /etc/mtab */
213 const char *mtab
= MOUNTED
;
215 struct mntent
*mnt
= NULL
;
218 if (stat64(devname
, &sb
) < 0) {
219 perror(NGMSG_FILE_INFO
);
220 PRINT_FILE_NAME(devname
);
224 fp
= setmntent(mtab
, "r");
226 perror("Couldn't access /etc/mtab");
230 while ((mnt
= getmntent(fp
)) != NULL
) {
234 * To handle device symlinks, we see if the
235 * device number matches, not the name
237 if (stat64(mnt
->mnt_fsname
, &ms
) < 0)
239 if (sb
.st_rdev
!= ms
.st_rdev
)
243 if (strcmp(mnt
->mnt_type
, FS_EXT4
) == 0) {
244 strncpy(mount_point
, mnt
->mnt_dir
,
248 PRINT_ERR_MSG(NGMSG_EXT4
);
252 PRINT_ERR_MSG("Filesystem is not mounted");
257 * is_ext4() - Whether on an ext4 filesystem.
259 * @file: the file's name.
261 static int is_ext4(const char *file
, char *devname
)
266 char *mnt_type
= NULL
;
267 /* Refer to /etc/mtab */
268 const char *mtab
= MOUNTED
;
269 char file_path
[PATH_MAX
+ 1];
270 struct mntent
*mnt
= NULL
;
271 struct statfs64 fsbuf
;
274 if (realpath(file
, file_path
) == NULL
) {
275 perror("Couldn't get full path");
276 PRINT_FILE_NAME(file
);
280 if (statfs64(file_path
, &fsbuf
) < 0) {
281 perror("Failed to get filesystem information");
282 PRINT_FILE_NAME(file
);
286 if (fsbuf
.f_type
!= EXT4_SUPER_MAGIC
) {
287 PRINT_ERR_MSG(NGMSG_EXT4
);
291 fp
= setmntent(mtab
, "r");
293 perror("Couldn't access /etc/mtab");
297 while ((mnt
= getmntent(fp
)) != NULL
) {
298 if (mnt
->mnt_fsname
[0] != '/')
300 len
= strlen(mnt
->mnt_dir
);
301 ret
= memcmp(file_path
, mnt
->mnt_dir
, len
);
310 mnt_type
= realloc(mnt_type
, strlen(mnt
->mnt_type
) + 1);
311 if (mnt_type
== NULL
) {
315 memset(mnt_type
, 0, strlen(mnt
->mnt_type
) + 1);
316 strncpy(mnt_type
, mnt
->mnt_type
, strlen(mnt
->mnt_type
));
317 strncpy(lost_found_dir
, mnt
->mnt_dir
, PATH_MAX
);
318 strncpy(devname
, mnt
->mnt_fsname
, strlen(mnt
->mnt_fsname
) + 1);
322 if (mnt_type
&& strcmp(mnt_type
, FS_EXT4
) == 0) {
327 PRINT_ERR_MSG(NGMSG_EXT4
);
333 * calc_entry_counts() - Calculate file counts.
338 * @ftwbuf: the pointer of a struct FTW.
340 static int calc_entry_counts(const char *file
EXT2FS_ATTR((unused
)),
341 const struct stat64
*buf
, int flag
EXT2FS_ATTR((unused
)),
342 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
344 if (S_ISREG(buf
->st_mode
))
353 * page_in_core() - Get information on whether pages are in core.
355 * @fd: defrag target file's descriptor.
356 * @defrag_data: data used for defrag.
357 * @vec: page state array.
358 * @page_num: page number.
360 static int page_in_core(int fd
, struct move_extent defrag_data
,
361 unsigned char **vec
, unsigned int *page_num
)
365 ext2_loff_t offset
, end_offset
, length
;
367 if (vec
== NULL
|| *vec
!= NULL
)
370 pagesize
= sysconf(_SC_PAGESIZE
);
373 /* In mmap, offset should be a multiple of the page size */
374 offset
= (ext2_loff_t
)defrag_data
.orig_start
* block_size
;
375 length
= (ext2_loff_t
)defrag_data
.len
* block_size
;
376 end_offset
= offset
+ length
;
377 /* Round the offset down to the nearest multiple of pagesize */
378 offset
= (offset
/ pagesize
) * pagesize
;
379 length
= end_offset
- offset
;
381 page
= mmap(NULL
, length
, PROT_READ
, MAP_SHARED
, fd
, offset
);
382 if (page
== MAP_FAILED
)
386 *page_num
= (length
+ pagesize
- 1) / pagesize
;
387 *vec
= (unsigned char *)calloc(*page_num
, 1);
389 munmap(page
, length
);
393 /* Get information on whether pages are in core */
394 if (mincore(page
, (size_t)length
, *vec
) == -1 ||
395 munmap(page
, length
) == -1) {
404 * defrag_fadvise() - Predeclare an access pattern for file data.
406 * @fd: defrag target file's descriptor.
407 * @defrag_data: data used for defrag.
408 * @vec: page state array.
409 * @page_num: page number.
411 static int defrag_fadvise(int fd
, struct move_extent defrag_data
,
412 unsigned char *vec
, unsigned int page_num
)
415 long pagesize
= sysconf(_SC_PAGESIZE
);
416 int fadvise_flag
= POSIX_FADV_DONTNEED
;
417 int sync_flag
= SYNC_FILE_RANGE_WAIT_BEFORE
|
418 SYNC_FILE_RANGE_WRITE
|
419 SYNC_FILE_RANGE_WAIT_AFTER
;
426 offset
= (ext2_loff_t
)defrag_data
.orig_start
* block_size
;
427 offset
= (offset
/ pagesize
) * pagesize
;
429 #ifdef HAVE_SYNC_FILE_RANGE
430 /* Sync file for fadvise process */
431 if (sync_file_range(fd
, offset
,
432 (ext2_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 ((errno
= posix_fadvise(fd
, offset
,
445 pagesize
, fadvise_flag
)) != 0) {
446 if ((mode_flag
& DETAIL
) && flag
) {
447 perror("\tFailed to fadvise");
458 * check_free_size() - Check if there's enough disk space.
460 * @fd: defrag target file's descriptor.
462 * @blk_count: file blocks.
464 static int check_free_size(int fd
, const char *file
, ext4_fsblk_t blk_count
)
466 ext4_fsblk_t free_blk_count
;
467 struct statfs64 fsbuf
;
469 if (fstatfs64(fd
, &fsbuf
) < 0) {
470 if (mode_flag
& DETAIL
) {
471 PRINT_FILE_NAME(file
);
472 PRINT_ERR_MSG_WITH_ERRNO(
473 "Failed to get filesystem information");
478 /* Compute free space for root and normal user separately */
479 if (current_uid
== ROOT_UID
)
480 free_blk_count
= fsbuf
.f_bfree
;
482 free_blk_count
= fsbuf
.f_bavail
;
484 if (free_blk_count
>= blk_count
)
491 * file_frag_count() - Get file fragment count.
493 * @fd: defrag target file's descriptor.
495 static int file_frag_count(int fd
)
498 struct fiemap fiemap_buf
;
500 /* When fm_extent_count is 0,
501 * ioctl just get file fragment count.
503 memset(&fiemap_buf
, 0, sizeof(struct fiemap
));
504 fiemap_buf
.fm_start
= 0;
505 fiemap_buf
.fm_length
= FIEMAP_MAX_OFFSET
;
506 fiemap_buf
.fm_flags
|= FIEMAP_FLAG_SYNC
;
508 ret
= ioctl(fd
, FS_IOC_FIEMAP
, &fiemap_buf
);
512 return fiemap_buf
.fm_mapped_extents
;
516 * file_check() - Check file's attributes.
518 * @fd: defrag target file's descriptor.
519 * @buf: a pointer of the struct stat64.
521 * @extents: file extents.
522 * @blk_count: file blocks.
524 static int file_check(int fd
, const struct stat64
*buf
, const char *file
,
525 int extents
, ext4_fsblk_t blk_count
)
530 /* Write-lock check is more reliable */
531 lock
.l_type
= F_WRLCK
;
533 lock
.l_whence
= SEEK_SET
;
537 ret
= check_free_size(fd
, file
, blk_count
);
539 if ((mode_flag
& DETAIL
) && ret
== -ENOSPC
) {
540 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
541 " extents: %d -> %d\n", defraged_file_count
,
542 total_count
, file
, extents
, extents
);
543 IN_FTW_PRINT_ERR_MSG(
544 "Defrag size is larger than filesystem's free space");
549 /* Access authority */
550 if (current_uid
!= ROOT_UID
&&
551 buf
->st_uid
!= current_uid
) {
552 if (mode_flag
& DETAIL
) {
553 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
554 " extents: %d -> %d\n", defraged_file_count
,
555 total_count
, file
, extents
, extents
);
556 IN_FTW_PRINT_ERR_MSG(
557 "File is not current user's file"
558 " or current user is not root");
564 if (fcntl(fd
, F_GETLK
, &lock
) < 0) {
565 if (mode_flag
& DETAIL
) {
566 PRINT_FILE_NAME(file
);
567 PRINT_ERR_MSG_WITH_ERRNO(
568 "Failed to get lock information");
571 } else if (lock
.l_type
!= F_UNLCK
) {
572 if (mode_flag
& DETAIL
) {
573 PRINT_FILE_NAME(file
);
574 IN_FTW_PRINT_ERR_MSG("File has been locked");
583 * insert_extent_by_logical() - Sequentially insert extent by logical.
585 * @ext_list_head: the head of logical extent list.
586 * @ext: the extent element which will be inserted.
588 static int insert_extent_by_logical(struct fiemap_extent_list
**ext_list_head
,
589 struct fiemap_extent_list
*ext
)
591 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
597 if (*ext_list_head
== NULL
) {
598 (*ext_list_head
) = ext
;
599 (*ext_list_head
)->prev
= *ext_list_head
;
600 (*ext_list_head
)->next
= *ext_list_head
;
604 if (ext
->data
.logical
<= ext_list_tmp
->data
.logical
) {
605 /* Insert before head */
606 if (ext_list_tmp
->data
.logical
<
607 ext
->data
.logical
+ ext
->data
.len
)
611 *ext_list_head
= ext
;
613 /* Insert into the middle or last of the list */
615 if (ext
->data
.logical
< ext_list_tmp
->data
.logical
)
617 ext_list_tmp
= ext_list_tmp
->next
;
618 } while (ext_list_tmp
!= (*ext_list_head
));
619 if (ext
->data
.logical
<
620 ext_list_tmp
->prev
->data
.logical
+
621 ext_list_tmp
->prev
->data
.len
)
625 if (ext_list_tmp
!= *ext_list_head
&&
626 ext_list_tmp
->data
.logical
<
627 ext
->data
.logical
+ ext
->data
.len
)
631 ext_list_tmp
= ext_list_tmp
->prev
;
632 /* Insert "ext" after "ext_list_tmp" */
633 insert(ext_list_tmp
, ext
);
641 * insert_extent_by_physical() - Sequentially insert extent by physical.
643 * @ext_list_head: the head of physical extent list.
644 * @ext: the extent element which will be inserted.
646 static int insert_extent_by_physical(struct fiemap_extent_list
**ext_list_head
,
647 struct fiemap_extent_list
*ext
)
649 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
655 if (*ext_list_head
== NULL
) {
656 (*ext_list_head
) = ext
;
657 (*ext_list_head
)->prev
= *ext_list_head
;
658 (*ext_list_head
)->next
= *ext_list_head
;
662 if (ext
->data
.physical
<= ext_list_tmp
->data
.physical
) {
663 /* Insert before head */
664 if (ext_list_tmp
->data
.physical
<
665 ext
->data
.physical
+ ext
->data
.len
)
669 *ext_list_head
= ext
;
671 /* Insert into the middle or last of the list */
673 if (ext
->data
.physical
< ext_list_tmp
->data
.physical
)
675 ext_list_tmp
= ext_list_tmp
->next
;
676 } while (ext_list_tmp
!= (*ext_list_head
));
677 if (ext
->data
.physical
<
678 ext_list_tmp
->prev
->data
.physical
+
679 ext_list_tmp
->prev
->data
.len
)
683 if (ext_list_tmp
!= *ext_list_head
&&
684 ext_list_tmp
->data
.physical
<
685 ext
->data
.physical
+ ext
->data
.len
)
689 ext_list_tmp
= ext_list_tmp
->prev
;
690 /* Insert "ext" after "ext_list_tmp" */
691 insert(ext_list_tmp
, ext
);
699 * insert_exts_group() - Insert a exts_group.
701 * @ext_group_head: the head of a exts_group list.
702 * @exts_group: the exts_group element which will be inserted.
704 static int insert_exts_group(struct fiemap_extent_group
**ext_group_head
,
705 struct fiemap_extent_group
*exts_group
)
707 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
709 if (exts_group
== NULL
) {
714 /* Initialize list */
715 if (*ext_group_head
== NULL
) {
716 (*ext_group_head
) = exts_group
;
717 (*ext_group_head
)->prev
= *ext_group_head
;
718 (*ext_group_head
)->next
= *ext_group_head
;
722 ext_group_tmp
= (*ext_group_head
)->prev
;
723 insert(ext_group_tmp
, exts_group
);
729 * join_extents() - Find continuous region(exts_group).
731 * @ext_list_head: the head of the extent list.
732 * @ext_group_head: the head of the target exts_group list.
734 static int join_extents(struct fiemap_extent_list
*ext_list_head
,
735 struct fiemap_extent_group
**ext_group_head
)
737 __u64 len
= ext_list_head
->data
.len
;
738 struct fiemap_extent_list
*ext_list_start
= ext_list_head
;
739 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
->next
;
742 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
744 /* This extent and previous extent are not continuous,
745 * so, all previous extents are treated as an extent group.
747 if ((ext_list_tmp
->prev
->data
.logical
+
748 ext_list_tmp
->prev
->data
.len
)
749 != ext_list_tmp
->data
.logical
) {
751 malloc(sizeof(struct fiemap_extent_group
));
752 if (ext_group_tmp
== NULL
)
755 memset(ext_group_tmp
, 0,
756 sizeof(struct fiemap_extent_group
));
757 ext_group_tmp
->len
= len
;
758 ext_group_tmp
->start
= ext_list_start
;
759 ext_group_tmp
->end
= ext_list_tmp
->prev
;
761 if (insert_exts_group(ext_group_head
,
762 ext_group_tmp
) < 0) {
766 ext_list_start
= ext_list_tmp
;
767 len
= ext_list_tmp
->data
.len
;
768 ext_list_tmp
= ext_list_tmp
->next
;
772 /* This extent and previous extent are continuous,
773 * so, they belong to the same extent group, and we check
774 * if the next extent belongs to the same extent group.
776 len
+= ext_list_tmp
->data
.len
;
777 ext_list_tmp
= ext_list_tmp
->next
;
778 } while (ext_list_tmp
!= ext_list_head
->next
);
784 * get_file_extents() - Get file's extent list.
786 * @fd: defrag target file's descriptor.
787 * @ext_list_head: the head of the extent list.
789 static int get_file_extents(int fd
, struct fiemap_extent_list
**ext_list_head
)
793 int ext_buf_size
, fie_buf_size
;
795 struct fiemap
*fiemap_buf
= NULL
;
796 struct fiemap_extent
*ext_buf
= NULL
;
797 struct fiemap_extent_list
*ext_list
= NULL
;
799 /* Convert units, in bytes.
800 * Be careful : now, physical block number in extent is 48bit,
801 * and the maximum blocksize for ext4 is 4K(12bit),
802 * so there is no overflow, but in future it may be changed.
805 /* Alloc space for fiemap */
806 ext_buf_size
= EXTENT_MAX_COUNT
* sizeof(struct fiemap_extent
);
807 fie_buf_size
= sizeof(struct fiemap
) + ext_buf_size
;
809 fiemap_buf
= malloc(fie_buf_size
);
810 if (fiemap_buf
== NULL
)
813 ext_buf
= fiemap_buf
->fm_extents
;
814 memset(fiemap_buf
, 0, fie_buf_size
);
815 fiemap_buf
->fm_length
= FIEMAP_MAX_OFFSET
;
816 fiemap_buf
->fm_flags
|= FIEMAP_FLAG_SYNC
;
817 fiemap_buf
->fm_extent_count
= EXTENT_MAX_COUNT
;
820 fiemap_buf
->fm_start
= pos
;
821 memset(ext_buf
, 0, ext_buf_size
);
822 ret
= ioctl(fd
, FS_IOC_FIEMAP
, fiemap_buf
);
823 if (ret
< 0 || fiemap_buf
->fm_mapped_extents
== 0)
825 for (i
= 0; i
< fiemap_buf
->fm_mapped_extents
; i
++) {
827 ext_list
= malloc(sizeof(struct fiemap_extent_list
));
828 if (ext_list
== NULL
)
831 ext_list
->data
.physical
= ext_buf
[i
].fe_physical
833 ext_list
->data
.logical
= ext_buf
[i
].fe_logical
835 ext_list
->data
.len
= ext_buf
[i
].fe_length
838 ret
= insert_extent_by_physical(
839 ext_list_head
, ext_list
);
845 /* Record file's logical offset this time */
846 pos
= ext_buf
[EXTENT_MAX_COUNT
-1].fe_logical
+
847 ext_buf
[EXTENT_MAX_COUNT
-1].fe_length
;
849 * If fm_extents array has been filled and
850 * there are extents left, continue to cycle.
852 } while (fiemap_buf
->fm_mapped_extents
853 == EXTENT_MAX_COUNT
&&
854 !(ext_buf
[EXTENT_MAX_COUNT
-1].fe_flags
855 & FIEMAP_EXTENT_LAST
));
865 * get_logical_count() - Get the file logical extents count.
867 * @logical_list_head: the head of the logical extent list.
869 static int get_logical_count(struct fiemap_extent_list
*logical_list_head
)
872 struct fiemap_extent_list
*ext_list_tmp
= logical_list_head
;
876 ext_list_tmp
= ext_list_tmp
->next
;
877 } while (ext_list_tmp
!= logical_list_head
);
883 * get_physical_count() - Get the file physical extents count.
885 * @physical_list_head: the head of the physical extent list.
887 static int get_physical_count(struct fiemap_extent_list
*physical_list_head
)
890 struct fiemap_extent_list
*ext_list_tmp
= physical_list_head
;
893 if ((ext_list_tmp
->data
.physical
+ ext_list_tmp
->data
.len
)
894 != ext_list_tmp
->next
->data
.physical
||
895 (ext_list_tmp
->data
.logical
+ ext_list_tmp
->data
.len
)
896 != ext_list_tmp
->next
->data
.logical
) {
897 /* This extent and next extent are not continuous. */
901 ext_list_tmp
= ext_list_tmp
->next
;
902 } while (ext_list_tmp
!= physical_list_head
);
908 * change_physical_to_logical() - Change list from physical to logical.
910 * @physical_list_head: the head of physical extent list.
911 * @logical_list_head: the head of logical extent list.
913 static int change_physical_to_logical(
914 struct fiemap_extent_list
**physical_list_head
,
915 struct fiemap_extent_list
**logical_list_head
)
918 struct fiemap_extent_list
*ext_list_tmp
= *physical_list_head
;
919 struct fiemap_extent_list
*ext_list_next
= ext_list_tmp
->next
;
922 if (ext_list_tmp
== ext_list_next
) {
923 ret
= insert_extent_by_logical(
924 logical_list_head
, ext_list_tmp
);
928 *physical_list_head
= NULL
;
932 ext_list_tmp
->prev
->next
= ext_list_tmp
->next
;
933 ext_list_tmp
->next
->prev
= ext_list_tmp
->prev
;
934 *physical_list_head
= ext_list_next
;
936 ret
= insert_extent_by_logical(
937 logical_list_head
, ext_list_tmp
);
942 ext_list_tmp
= ext_list_next
;
943 ext_list_next
= ext_list_next
->next
;
949 /* get_file_blocks() - Get total file blocks.
951 * @ext_list_head: the extent list head of the target file
953 static ext4_fsblk_t
get_file_blocks(struct fiemap_extent_list
*ext_list_head
)
955 ext4_fsblk_t blk_count
= 0;
956 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
;
959 blk_count
+= ext_list_tmp
->data
.len
;
960 ext_list_tmp
= ext_list_tmp
->next
;
961 } while (ext_list_tmp
!= ext_list_head
);
967 * free_ext() - Free the extent list.
969 * @ext_list_head: the extent list head of which will be free.
971 static void free_ext(struct fiemap_extent_list
*ext_list_head
)
973 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
975 if (ext_list_head
== NULL
)
978 while (ext_list_head
->next
!= ext_list_head
) {
979 ext_list_tmp
= ext_list_head
;
980 ext_list_head
->prev
->next
= ext_list_head
->next
;
981 ext_list_head
->next
->prev
= ext_list_head
->prev
;
982 ext_list_head
= ext_list_head
->next
;
989 * free_exts_group() - Free the exts_group.
991 * @*ext_group_head: the exts_group list head which will be free.
993 static void free_exts_group(struct fiemap_extent_group
*ext_group_head
)
995 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
997 if (ext_group_head
== NULL
)
1000 while (ext_group_head
->next
!= ext_group_head
) {
1001 ext_group_tmp
= ext_group_head
;
1002 ext_group_head
->prev
->next
= ext_group_head
->next
;
1003 ext_group_head
->next
->prev
= ext_group_head
->prev
;
1004 ext_group_head
= ext_group_head
->next
;
1005 free(ext_group_tmp
);
1007 free(ext_group_head
);
1011 * get_best_count() - Get the file best extents count.
1013 * @block_count: the file's physical block count.
1015 static int get_best_count(ext4_fsblk_t block_count
)
1018 unsigned int flex_bg_num
;
1020 if (blocks_per_group
== 0)
1023 if (feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
1024 flex_bg_num
= 1 << log_groups_per_flex
;
1025 ret
= ((block_count
- 1) /
1026 ((ext4_fsblk_t
)blocks_per_group
*
1029 ret
= ((block_count
- 1) / blocks_per_group
) + 1;
1036 * file_statistic() - Get statistic info of the file's fragments.
1038 * @file: the file's name.
1039 * @buf: the pointer of the struct stat64.
1041 * @ftwbuf: the pointer of a struct FTW.
1043 static int file_statistic(const char *file
, const struct stat64
*buf
,
1044 int flag
EXT2FS_ATTR((unused
)),
1045 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1049 int now_ext_count
, best_ext_count
= 0, physical_ext_count
;
1051 __u64 size_per_ext
= 0;
1053 ext4_fsblk_t blk_count
= 0;
1054 char msg_buffer
[PATH_MAX
+ 24];
1055 struct fiemap_extent_list
*physical_list_head
= NULL
;
1056 struct fiemap_extent_list
*logical_list_head
= NULL
;
1058 defraged_file_count
++;
1059 if (defraged_file_count
> total_count
)
1060 total_count
= defraged_file_count
;
1062 if (mode_flag
& DETAIL
) {
1063 if (total_count
== 1 && regular_count
== 1)
1066 printf("[%u/%u]", defraged_file_count
, total_count
);
1070 if (lost_found_dir
[0] != '\0' &&
1071 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1072 if (mode_flag
& DETAIL
) {
1073 PRINT_FILE_NAME(file
);
1074 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND
);
1079 if (!S_ISREG(buf
->st_mode
)) {
1080 if (mode_flag
& DETAIL
) {
1081 PRINT_FILE_NAME(file
);
1082 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG
);
1087 /* Access authority */
1088 if (current_uid
!= ROOT_UID
&&
1089 buf
->st_uid
!= current_uid
) {
1090 if (mode_flag
& DETAIL
) {
1091 PRINT_FILE_NAME(file
);
1093 "File is not current user's file"
1094 " or current user is not root");
1100 if (buf
->st_size
== 0) {
1101 if (mode_flag
& DETAIL
) {
1102 PRINT_FILE_NAME(file
);
1103 STATISTIC_ERR_MSG("File size is 0");
1109 if (buf
->st_blocks
== 0) {
1110 if (mode_flag
& DETAIL
) {
1111 PRINT_FILE_NAME(file
);
1112 STATISTIC_ERR_MSG("File has no blocks");
1117 fd
= open64(file
, O_RDONLY
);
1119 if (mode_flag
& DETAIL
) {
1120 PRINT_FILE_NAME(file
);
1121 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1126 /* Get file's physical extents */
1127 ret
= get_file_extents(fd
, &physical_list_head
);
1129 if (mode_flag
& DETAIL
) {
1130 PRINT_FILE_NAME(file
);
1131 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1136 /* Get the count of file's continuous physical region */
1137 physical_ext_count
= get_physical_count(physical_list_head
);
1139 /* Change list from physical to logical */
1140 ret
= change_physical_to_logical(&physical_list_head
,
1141 &logical_list_head
);
1143 if (mode_flag
& DETAIL
) {
1144 PRINT_FILE_NAME(file
);
1145 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1150 /* Count file fragments before defrag */
1151 now_ext_count
= get_logical_count(logical_list_head
);
1153 if (current_uid
== ROOT_UID
) {
1154 /* Calculate the size per extent */
1155 blk_count
= get_file_blocks(logical_list_head
);
1157 best_ext_count
= get_best_count(blk_count
);
1159 /* e4defrag rounds size_per_ext up to a block size boundary */
1160 size_per_ext
= blk_count
* (buf
->st_blksize
/ 1024) /
1163 ratio
= (float)(physical_ext_count
- best_ext_count
) * 100 /
1166 extents_before_defrag
+= now_ext_count
;
1167 extents_after_defrag
+= best_ext_count
;
1168 files_block_count
+= blk_count
;
1171 if (total_count
== 1 && regular_count
== 1) {
1173 if (mode_flag
& DETAIL
) {
1175 struct fiemap_extent_list
*ext_list_tmp
=
1178 /* Print extents info */
1181 printf("[ext %d]:\tstart %llu:\tlogical "
1182 "%llu:\tlen %llu\n", count
,
1183 (unsigned long long)
1184 ext_list_tmp
->data
.physical
,
1185 (unsigned long long)
1186 ext_list_tmp
->data
.logical
,
1187 (unsigned long long)
1188 ext_list_tmp
->data
.len
);
1189 ext_list_tmp
= ext_list_tmp
->next
;
1190 } while (ext_list_tmp
!= logical_list_head
);
1193 printf("%-40s%10s/%-10s%9s\n",
1194 "<File>", "now", "best", "size/ext");
1195 if (current_uid
== ROOT_UID
) {
1196 if (strlen(file
) > 40)
1197 printf("%s\n%50d/%-10d%6llu KB\n",
1198 file
, now_ext_count
,
1200 (unsigned long long) size_per_ext
);
1202 printf("%-40s%10d/%-10d%6llu KB\n",
1203 file
, now_ext_count
,
1205 (unsigned long long) size_per_ext
);
1207 if (strlen(file
) > 40)
1208 printf("%s\n%50d/%-10s%7s\n",
1209 file
, now_ext_count
,
1212 printf("%-40s%10d/%-10s%7s\n",
1213 file
, now_ext_count
,
1221 if (mode_flag
& DETAIL
) {
1222 /* Print statistic info */
1223 sprintf(msg_buffer
, "[%u/%u]%s",
1224 defraged_file_count
, total_count
, file
);
1225 if (current_uid
== ROOT_UID
) {
1226 if (strlen(msg_buffer
) > 40)
1227 printf("\033[79;0H\033[K%s\n"
1228 "%50d/%-10d%6llu KB\n",
1229 msg_buffer
, now_ext_count
,
1231 (unsigned long long) size_per_ext
);
1233 printf("\033[79;0H\033[K%-40s"
1234 "%10d/%-10d%6llu KB\n",
1235 msg_buffer
, now_ext_count
,
1237 (unsigned long long) size_per_ext
);
1239 if (strlen(msg_buffer
) > 40)
1240 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1241 msg_buffer
, now_ext_count
,
1244 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1245 msg_buffer
, now_ext_count
,
1250 for (i
= 0; i
< SHOW_FRAG_FILES
; i
++) {
1251 if (ratio
>= frag_rank
[i
].ratio
) {
1252 for (j
= SHOW_FRAG_FILES
- 1; j
> i
; j
--) {
1253 memset(&frag_rank
[j
], 0,
1254 sizeof(struct frag_statistic_ino
));
1255 strncpy(frag_rank
[j
].msg_buffer
,
1256 frag_rank
[j
- 1].msg_buffer
,
1257 strnlen(frag_rank
[j
- 1].msg_buffer
,
1259 frag_rank
[j
].now_count
=
1260 frag_rank
[j
- 1].now_count
;
1261 frag_rank
[j
].best_count
=
1262 frag_rank
[j
- 1].best_count
;
1263 frag_rank
[j
].size_per_ext
=
1264 frag_rank
[j
- 1].size_per_ext
;
1265 frag_rank
[j
].ratio
=
1266 frag_rank
[j
- 1].ratio
;
1268 memset(&frag_rank
[i
], 0,
1269 sizeof(struct frag_statistic_ino
));
1270 strncpy(frag_rank
[i
].msg_buffer
, file
,
1271 strnlen(file
, PATH_MAX
));
1272 frag_rank
[i
].now_count
= now_ext_count
;
1273 frag_rank
[i
].best_count
= best_ext_count
;
1274 frag_rank
[i
].size_per_ext
= size_per_ext
;
1275 frag_rank
[i
].ratio
= ratio
;
1284 free_ext(physical_list_head
);
1285 free_ext(logical_list_head
);
1290 * print_progress - Print defrag progress
1293 * @start: logical offset for defrag target file
1294 * @file_size: defrag target filesize
1296 static void print_progress(const char *file
, ext2_loff_t start
,
1297 ext2_loff_t file_size
)
1299 int percent
= (start
* 100) / file_size
;
1300 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1301 defraged_file_count
, total_count
, file
, min(percent
, 100));
1308 * call_defrag() - Execute the defrag program.
1310 * @fd: target file descriptor.
1311 * @donor_fd: donor file descriptor.
1312 * @file: target file name.
1313 * @buf: pointer of the struct stat64.
1314 * @ext_list_head: head of the extent list.
1316 static int call_defrag(int fd
, int donor_fd
, const char *file
,
1317 const struct stat64
*buf
, struct fiemap_extent_list
*ext_list_head
)
1319 ext2_loff_t start
= 0;
1320 unsigned int page_num
;
1321 unsigned char *vec
= NULL
;
1322 int defraged_ret
= 0;
1324 struct move_extent move_data
;
1325 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1327 memset(&move_data
, 0, sizeof(struct move_extent
));
1328 move_data
.donor_fd
= donor_fd
;
1330 /* Print defrag progress */
1331 print_progress(file
, start
, buf
->st_size
);
1333 ext_list_tmp
= ext_list_head
;
1335 move_data
.orig_start
= ext_list_tmp
->data
.logical
;
1336 /* Logical offset of orig and donor should be same */
1337 move_data
.donor_start
= move_data
.orig_start
;
1338 move_data
.len
= ext_list_tmp
->data
.len
;
1339 move_data
.moved_len
= 0;
1341 ret
= page_in_core(fd
, move_data
, &vec
, &page_num
);
1343 if (mode_flag
& DETAIL
) {
1345 PRINT_ERR_MSG_WITH_ERRNO(
1346 "Failed to get file map");
1348 printf("\t[ NG ]\n");
1353 /* EXT4_IOC_MOVE_EXT */
1355 ioctl(fd
, EXT4_IOC_MOVE_EXT
, &move_data
);
1358 ret
= defrag_fadvise(fd
, move_data
, vec
, page_num
);
1364 if (mode_flag
& DETAIL
) {
1366 PRINT_ERR_MSG_WITH_ERRNO(
1367 "Failed to free page");
1369 printf("\t[ NG ]\n");
1374 if (defraged_ret
< 0) {
1375 if (mode_flag
& DETAIL
) {
1377 PRINT_ERR_MSG_WITH_ERRNO(
1378 "Failed to defrag with "
1379 "EXT4_IOC_MOVE_EXT ioctl");
1380 if (errno
== ENOTTY
)
1381 printf("\tAt least 2.6.31-rc1 of "
1382 "vanilla kernel is required\n");
1384 printf("\t[ NG ]\n");
1388 /* Adjust logical offset for next ioctl */
1389 move_data
.orig_start
+= move_data
.moved_len
;
1390 move_data
.donor_start
= move_data
.orig_start
;
1392 start
= move_data
.orig_start
* buf
->st_blksize
;
1394 /* Print defrag progress */
1395 print_progress(file
, start
, buf
->st_size
);
1398 if (start
>= buf
->st_size
)
1401 ext_list_tmp
= ext_list_tmp
->next
;
1402 } while (ext_list_tmp
!= ext_list_head
);
1408 * file_defrag() - Check file attributes and call ioctl to defrag.
1410 * @file: the file's name.
1411 * @buf: the pointer of the struct stat64.
1413 * @ftwbuf: the pointer of a struct FTW.
1415 static int file_defrag(const char *file
, const struct stat64
*buf
,
1416 int flag
EXT2FS_ATTR((unused
)),
1417 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1423 int file_frags_start
, file_frags_end
;
1424 int orig_physical_cnt
, donor_physical_cnt
= 0;
1425 char tmp_inode_name
[PATH_MAX
+ 8];
1426 ext4_fsblk_t blk_count
= 0;
1427 struct fiemap_extent_list
*orig_list_physical
= NULL
;
1428 struct fiemap_extent_list
*orig_list_logical
= NULL
;
1429 struct fiemap_extent_list
*donor_list_physical
= NULL
;
1430 struct fiemap_extent_list
*donor_list_logical
= NULL
;
1431 struct fiemap_extent_group
*orig_group_head
= NULL
;
1432 struct fiemap_extent_group
*orig_group_tmp
= NULL
;
1434 defraged_file_count
++;
1435 if (defraged_file_count
> total_count
)
1436 total_count
= defraged_file_count
;
1438 if (mode_flag
& DETAIL
) {
1439 printf("[%u/%u]", defraged_file_count
, total_count
);
1443 if (lost_found_dir
[0] != '\0' &&
1444 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1445 if (mode_flag
& DETAIL
) {
1446 PRINT_FILE_NAME(file
);
1447 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1452 if (!S_ISREG(buf
->st_mode
)) {
1453 if (mode_flag
& DETAIL
) {
1454 PRINT_FILE_NAME(file
);
1455 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1461 if (buf
->st_size
== 0) {
1462 if (mode_flag
& DETAIL
) {
1463 PRINT_FILE_NAME(file
);
1464 IN_FTW_PRINT_ERR_MSG("File size is 0");
1470 if (buf
->st_blocks
== 0) {
1471 if (mode_flag
& DETAIL
) {
1472 PRINT_FILE_NAME(file
);
1473 STATISTIC_ERR_MSG("File has no blocks");
1478 fd
= open64(file
, O_RDWR
);
1480 if (mode_flag
& DETAIL
) {
1481 PRINT_FILE_NAME(file
);
1482 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1487 /* Get file's extents */
1488 ret
= get_file_extents(fd
, &orig_list_physical
);
1490 if (mode_flag
& DETAIL
) {
1491 PRINT_FILE_NAME(file
);
1492 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1497 /* Get the count of file's continuous physical region */
1498 orig_physical_cnt
= get_physical_count(orig_list_physical
);
1500 /* Change list from physical to logical */
1501 ret
= change_physical_to_logical(&orig_list_physical
,
1502 &orig_list_logical
);
1504 if (mode_flag
& DETAIL
) {
1505 PRINT_FILE_NAME(file
);
1506 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1511 /* Count file fragments before defrag */
1512 file_frags_start
= get_logical_count(orig_list_logical
);
1514 blk_count
= get_file_blocks(orig_list_logical
);
1515 if (file_check(fd
, buf
, file
, file_frags_start
, blk_count
) < 0)
1518 if (fsync(fd
) < 0) {
1519 if (mode_flag
& DETAIL
) {
1520 PRINT_FILE_NAME(file
);
1521 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1526 best
= get_best_count(blk_count
);
1528 if (file_frags_start
<= best
)
1529 goto check_improvement
;
1531 /* Combine extents to group */
1532 ret
= join_extents(orig_list_logical
, &orig_group_head
);
1534 if (mode_flag
& DETAIL
) {
1535 PRINT_FILE_NAME(file
);
1536 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1541 /* Create donor inode */
1542 memset(tmp_inode_name
, 0, PATH_MAX
+ 8);
1543 sprintf(tmp_inode_name
, "%.*s.defrag",
1544 (int)strnlen(file
, PATH_MAX
), file
);
1545 donor_fd
= open64(tmp_inode_name
, O_WRONLY
| O_CREAT
| O_EXCL
, S_IRUSR
);
1547 if (mode_flag
& DETAIL
) {
1548 PRINT_FILE_NAME(file
);
1549 if (errno
== EEXIST
)
1550 PRINT_ERR_MSG_WITH_ERRNO(
1551 "File is being defraged by other program");
1553 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1558 /* Unlink donor inode */
1559 ret
= unlink(tmp_inode_name
);
1561 if (mode_flag
& DETAIL
) {
1562 PRINT_FILE_NAME(file
);
1563 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1568 /* Allocate space for donor inode */
1569 orig_group_tmp
= orig_group_head
;
1571 ret
= fallocate64(donor_fd
, 0,
1572 (ext2_loff_t
)orig_group_tmp
->start
->data
.logical
* block_size
,
1573 (ext2_loff_t
)orig_group_tmp
->len
* block_size
);
1575 if (mode_flag
& DETAIL
) {
1576 PRINT_FILE_NAME(file
);
1577 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1582 orig_group_tmp
= orig_group_tmp
->next
;
1583 } while (orig_group_tmp
!= orig_group_head
);
1585 /* Get donor inode's extents */
1586 ret
= get_file_extents(donor_fd
, &donor_list_physical
);
1588 if (mode_flag
& DETAIL
) {
1589 PRINT_FILE_NAME(file
);
1590 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1595 /* Calculate donor inode's continuous physical region */
1596 donor_physical_cnt
= get_physical_count(donor_list_physical
);
1598 /* Change donor extent list from physical to logical */
1599 ret
= change_physical_to_logical(&donor_list_physical
,
1600 &donor_list_logical
);
1602 if (mode_flag
& DETAIL
) {
1603 PRINT_FILE_NAME(file
);
1604 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1610 if (mode_flag
& DETAIL
) {
1611 if (file_frags_start
!= 1)
1612 frag_files_before_defrag
++;
1614 extents_before_defrag
+= file_frags_start
;
1617 if (file_frags_start
<= best
||
1618 orig_physical_cnt
<= donor_physical_cnt
) {
1619 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1620 defraged_file_count
, total_count
, file
, 100);
1621 if (mode_flag
& DETAIL
)
1622 printf(" extents: %d -> %d",
1623 file_frags_start
, file_frags_start
);
1625 printf("\t[ OK ]\n");
1628 if (file_frags_start
!= 1)
1629 frag_files_after_defrag
++;
1631 extents_after_defrag
+= file_frags_start
;
1635 /* Defrag the file */
1636 ret
= call_defrag(fd
, donor_fd
, file
, buf
, donor_list_logical
);
1638 /* Count file fragments after defrag and print extents info */
1639 if (mode_flag
& DETAIL
) {
1640 file_frags_end
= file_frag_count(fd
);
1641 if (file_frags_end
< 0) {
1643 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO
);
1647 if (file_frags_end
!= 1)
1648 frag_files_after_defrag
++;
1650 extents_after_defrag
+= file_frags_end
;
1655 printf(" extents: %d -> %d",
1656 file_frags_start
, file_frags_end
);
1663 printf("\t[ OK ]\n");
1671 free_ext(orig_list_physical
);
1672 free_ext(orig_list_logical
);
1673 free_ext(donor_list_physical
);
1674 free_exts_group(orig_group_head
);
1679 * main() - Ext4 online defrag.
1681 * @argc: the number of parameter.
1682 * @argv[]: the pointer array of parameter.
1684 int main(int argc
, char *argv
[])
1688 int flags
= FTW_PHYS
| FTW_MOUNT
;
1690 int mount_dir_len
= 0;
1691 int success_flag
= 0;
1692 char dir_name
[PATH_MAX
+ 1];
1693 char dev_name
[PATH_MAX
+ 1];
1695 ext2_filsys fs
= NULL
;
1697 printf("e4defrag %s (%s)\n", E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
1699 /* Parse arguments */
1703 while ((opt
= getopt(argc
, argv
, "vc")) != EOF
) {
1706 mode_flag
|= DETAIL
;
1709 mode_flag
|= STATISTIC
;
1719 current_uid
= getuid();
1722 for (i
= optind
; i
< argc
; i
++) {
1726 frag_files_before_defrag
= 0;
1727 frag_files_after_defrag
= 0;
1728 extents_before_defrag
= 0;
1729 extents_after_defrag
= 0;
1730 defraged_file_count
= 0;
1731 files_block_count
= 0;
1732 blocks_per_group
= 0;
1733 feature_incompat
= 0;
1734 log_groups_per_flex
= 0;
1736 memset(dir_name
, 0, PATH_MAX
+ 1);
1737 memset(dev_name
, 0, PATH_MAX
+ 1);
1738 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1739 memset(frag_rank
, 0,
1740 sizeof(struct frag_statistic_ino
) * SHOW_FRAG_FILES
);
1742 if ((mode_flag
& STATISTIC
) && i
> optind
)
1745 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1746 PRINT_ERR_MSG("Endian's type is not big/little endian");
1747 PRINT_FILE_NAME(argv
[i
]);
1751 if (lstat64(argv
[i
], &buf
) < 0) {
1752 perror(NGMSG_FILE_INFO
);
1753 PRINT_FILE_NAME(argv
[i
]);
1757 /* Handle i.e. lvm device symlinks */
1758 if (S_ISLNK(buf
.st_mode
)) {
1761 if (stat64(argv
[i
], &buf2
) == 0 &&
1762 S_ISBLK(buf2
.st_mode
))
1766 if (S_ISBLK(buf
.st_mode
)) {
1768 strncpy(dev_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1769 if (get_mount_point(argv
[i
], dir_name
, PATH_MAX
) < 0)
1771 if (lstat64(dir_name
, &buf
) < 0) {
1772 perror(NGMSG_FILE_INFO
);
1773 PRINT_FILE_NAME(argv
[i
]);
1777 if (!(mode_flag
& STATISTIC
))
1778 printf("ext4 defragmentation for device(%s)\n",
1780 } else if (S_ISDIR(buf
.st_mode
)) {
1782 if (access(argv
[i
], R_OK
) < 0) {
1787 strncpy(dir_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1788 } else if (S_ISREG(buf
.st_mode
)) {
1790 arg_type
= FILENAME
;
1792 /* Irregular file */
1793 PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1794 PRINT_FILE_NAME(argv
[i
]);
1799 block_size
= buf
.st_blksize
;
1802 * filesystem type checked in get_mount_point()
1804 if (arg_type
== FILENAME
|| arg_type
== DIRNAME
) {
1805 if (is_ext4(argv
[i
], dev_name
) < 0)
1807 if (realpath(argv
[i
], dir_name
) == NULL
) {
1808 perror("Couldn't get full path");
1809 PRINT_FILE_NAME(argv
[i
]);
1814 if (current_uid
== ROOT_UID
) {
1815 /* Get super block info */
1816 ret
= ext2fs_open(dev_name
, EXT2_FLAG_64BITS
, 0,
1817 block_size
, unix_io_manager
, &fs
);
1819 if (mode_flag
& DETAIL
)
1821 "Warning: couldn't get file "
1822 "system details for %s: %s\n",
1823 dev_name
, error_message(ret
));
1825 blocks_per_group
= fs
->super
->s_blocks_per_group
;
1826 feature_incompat
= fs
->super
->s_feature_incompat
;
1827 log_groups_per_flex
= fs
->super
->s_log_groups_per_flex
;
1828 ext2fs_close_free(&fs
);
1835 if (!(mode_flag
& STATISTIC
))
1836 printf("ext4 defragmentation "
1837 "for directory(%s)\n", argv
[i
]);
1839 mount_dir_len
= strnlen(lost_found_dir
, PATH_MAX
);
1841 strncat(lost_found_dir
, "/lost+found",
1842 PATH_MAX
- strnlen(lost_found_dir
, PATH_MAX
));
1844 /* Not the case("e4defrag mount_point_dir") */
1845 if (dir_name
[mount_dir_len
] != '\0') {
1847 * "e4defrag mount_point_dir/lost+found"
1848 * or "e4defrag mount_point_dir/lost+found/"
1850 if (strncmp(lost_found_dir
, dir_name
,
1851 strnlen(lost_found_dir
,
1853 (dir_name
[strnlen(lost_found_dir
,
1854 PATH_MAX
)] == '\0' ||
1855 dir_name
[strnlen(lost_found_dir
,
1856 PATH_MAX
)] == '/')) {
1857 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1858 PRINT_FILE_NAME(argv
[i
]);
1862 /* "e4defrag mount_point_dir/else_dir" */
1863 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1867 if (arg_type
== DEVNAME
) {
1868 strncpy(lost_found_dir
, dir_name
,
1869 strnlen(dir_name
, PATH_MAX
));
1870 strncat(lost_found_dir
, "/lost+found/",
1871 PATH_MAX
- strnlen(lost_found_dir
,
1875 nftw64(dir_name
, calc_entry_counts
, FTW_OPEN_FD
, flags
);
1877 if (mode_flag
& STATISTIC
) {
1878 if (mode_flag
& DETAIL
)
1879 printf("%-40s%10s/%-10s%9s\n",
1880 "<File>", "now", "best", "size/ext");
1882 if (!(mode_flag
& DETAIL
) &&
1883 current_uid
!= ROOT_UID
) {
1889 nftw64(dir_name
, file_statistic
,
1890 FTW_OPEN_FD
, flags
);
1892 if (succeed_cnt
!= 0 &&
1893 current_uid
== ROOT_UID
) {
1894 if (mode_flag
& DETAIL
)
1896 printf("%-40s%10s/%-10s%9s\n",
1897 "<Fragmented files>", "now",
1898 "best", "size/ext");
1899 for (j
= 0; j
< SHOW_FRAG_FILES
; j
++) {
1900 if (strlen(frag_rank
[j
].
1902 printf("%d. %s\n%50d/"
1905 frag_rank
[j
].msg_buffer
,
1906 frag_rank
[j
].now_count
,
1907 frag_rank
[j
].best_count
,
1908 (unsigned long long)
1911 } else if (strlen(frag_rank
[j
].
1913 printf("%d. %-37s%10d/"
1916 frag_rank
[j
].msg_buffer
,
1917 frag_rank
[j
].now_count
,
1918 frag_rank
[j
].best_count
,
1919 (unsigned long long)
1928 /* File tree walk */
1929 nftw64(dir_name
, file_defrag
, FTW_OPEN_FD
, flags
);
1930 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt
,
1932 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1933 total_count
- succeed_cnt
, total_count
);
1934 if (mode_flag
& DETAIL
) {
1935 printf("\tTotal extents:\t\t\t%4d->%d\n",
1936 extents_before_defrag
,
1937 extents_after_defrag
);
1938 printf("\tFragmented percentage:\t\t"
1939 "%3llu%%->%llu%%\n",
1940 !regular_count
? 0 :
1941 ((unsigned long long)
1942 frag_files_before_defrag
* 100) /
1944 !regular_count
? 0 :
1945 ((unsigned long long)
1946 frag_files_after_defrag
* 100) /
1953 strncat(lost_found_dir
, "/lost+found/",
1954 PATH_MAX
- strnlen(lost_found_dir
,
1956 if (strncmp(lost_found_dir
, dir_name
,
1957 strnlen(lost_found_dir
,
1959 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1960 PRINT_FILE_NAME(argv
[i
]);
1964 if (mode_flag
& STATISTIC
) {
1965 file_statistic(argv
[i
], &buf
, FTW_F
, NULL
);
1968 printf("ext4 defragmentation for %s\n",
1970 /* Defrag single file process */
1971 file_defrag(argv
[i
], &buf
, FTW_F
, NULL
);
1972 if (succeed_cnt
!= 0)
1973 printf(" Success:\t\t\t[1/1]\n");
1975 printf(" Success:\t\t\t[0/1]\n");
1980 if (succeed_cnt
!= 0)
1982 if (mode_flag
& STATISTIC
) {
1983 if (current_uid
!= ROOT_UID
) {
1989 if (mode_flag
& DETAIL
)
1992 if (arg_type
== DEVNAME
)
1993 printf(" In this device(%s), "
1994 "none can be defragmented.\n", argv
[i
]);
1995 else if (arg_type
== DIRNAME
)
1996 printf(" In this directory(%s), "
1997 "none can be defragmented.\n", argv
[i
]);
1999 printf(" This file(%s) "
2000 "can't be defragmented.\n", argv
[i
]);
2002 float files_ratio
= 0.0;
2004 __u64 size_per_ext
= files_block_count
*
2005 (buf
.st_blksize
/ 1024) /
2006 extents_before_defrag
;
2007 files_ratio
= (float)(extents_before_defrag
-
2008 extents_after_defrag
) *
2009 100 / files_block_count
;
2010 score
= CALC_SCORE(files_ratio
);
2011 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2012 " Average size per extent"
2014 " Fragmentation score\t\t\t\t%.0f\n",
2015 extents_before_defrag
,
2016 extents_after_defrag
,
2017 (unsigned long long) size_per_ext
, score
);
2018 printf(" [0-30 no problem:"
2019 " 31-55 a little bit fragmented:"
2020 " 56- needs defrag]\n");
2022 if (arg_type
== DEVNAME
)
2023 printf(" This device (%s) ", argv
[i
]);
2024 else if (arg_type
== DIRNAME
)
2025 printf(" This directory (%s) ",
2028 printf(" This file (%s) ", argv
[i
]);
2030 if (score
> BOUND_SCORE
)
2031 printf("needs defragmentation.\n");
2033 printf("does not need "
2034 "defragmentation.\n");