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 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
= (loff_t
)defrag_data
.orig_start
* block_size
;
375 length
= (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
= (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 (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 ext_list_tmp
->data
.physical
,
1184 ext_list_tmp
->data
.logical
,
1185 ext_list_tmp
->data
.len
);
1186 ext_list_tmp
= ext_list_tmp
->next
;
1187 } while (ext_list_tmp
!= logical_list_head
);
1190 printf("%-40s%10s/%-10s%9s\n",
1191 "<File>", "now", "best", "size/ext");
1192 if (current_uid
== ROOT_UID
) {
1193 if (strlen(file
) > 40)
1194 printf("%s\n%50d/%-10d%6llu KB\n",
1195 file
, now_ext_count
,
1196 best_ext_count
, size_per_ext
);
1198 printf("%-40s%10d/%-10d%6llu KB\n",
1199 file
, now_ext_count
,
1200 best_ext_count
, size_per_ext
);
1202 if (strlen(file
) > 40)
1203 printf("%s\n%50d/%-10s%7s\n",
1204 file
, now_ext_count
,
1207 printf("%-40s%10d/%-10s%7s\n",
1208 file
, now_ext_count
,
1216 if (mode_flag
& DETAIL
) {
1217 /* Print statistic info */
1218 sprintf(msg_buffer
, "[%u/%u]%s",
1219 defraged_file_count
, total_count
, file
);
1220 if (current_uid
== ROOT_UID
) {
1221 if (strlen(msg_buffer
) > 40)
1222 printf("\033[79;0H\033[K%s\n"
1223 "%50d/%-10d%6llu KB\n",
1224 msg_buffer
, now_ext_count
,
1225 best_ext_count
, size_per_ext
);
1227 printf("\033[79;0H\033[K%-40s"
1228 "%10d/%-10d%6llu KB\n",
1229 msg_buffer
, now_ext_count
,
1230 best_ext_count
, size_per_ext
);
1232 if (strlen(msg_buffer
) > 40)
1233 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1234 msg_buffer
, now_ext_count
,
1237 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1238 msg_buffer
, now_ext_count
,
1243 for (i
= 0; i
< SHOW_FRAG_FILES
; i
++) {
1244 if (ratio
>= frag_rank
[i
].ratio
) {
1245 for (j
= SHOW_FRAG_FILES
- 1; j
> i
; j
--) {
1246 memset(&frag_rank
[j
], 0,
1247 sizeof(struct frag_statistic_ino
));
1248 strncpy(frag_rank
[j
].msg_buffer
,
1249 frag_rank
[j
- 1].msg_buffer
,
1250 strnlen(frag_rank
[j
- 1].msg_buffer
,
1252 frag_rank
[j
].now_count
=
1253 frag_rank
[j
- 1].now_count
;
1254 frag_rank
[j
].best_count
=
1255 frag_rank
[j
- 1].best_count
;
1256 frag_rank
[j
].size_per_ext
=
1257 frag_rank
[j
- 1].size_per_ext
;
1258 frag_rank
[j
].ratio
=
1259 frag_rank
[j
- 1].ratio
;
1261 memset(&frag_rank
[i
], 0,
1262 sizeof(struct frag_statistic_ino
));
1263 strncpy(frag_rank
[i
].msg_buffer
, file
,
1264 strnlen(file
, PATH_MAX
));
1265 frag_rank
[i
].now_count
= now_ext_count
;
1266 frag_rank
[i
].best_count
= best_ext_count
;
1267 frag_rank
[i
].size_per_ext
= size_per_ext
;
1268 frag_rank
[i
].ratio
= ratio
;
1277 free_ext(physical_list_head
);
1278 free_ext(logical_list_head
);
1283 * print_progress - Print defrag progress
1286 * @start: logical offset for defrag target file
1287 * @file_size: defrag target filesize
1289 static void print_progress(const char *file
, loff_t start
, loff_t file_size
)
1291 int percent
= (start
* 100) / file_size
;
1292 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1293 defraged_file_count
, total_count
, file
, min(percent
, 100));
1300 * call_defrag() - Execute the defrag program.
1302 * @fd: target file descriptor.
1303 * @donor_fd: donor file descriptor.
1304 * @file: target file name.
1305 * @buf: pointer of the struct stat64.
1306 * @ext_list_head: head of the extent list.
1308 static int call_defrag(int fd
, int donor_fd
, const char *file
,
1309 const struct stat64
*buf
, struct fiemap_extent_list
*ext_list_head
)
1312 unsigned int page_num
;
1313 unsigned char *vec
= NULL
;
1314 int defraged_ret
= 0;
1316 struct move_extent move_data
;
1317 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1319 memset(&move_data
, 0, sizeof(struct move_extent
));
1320 move_data
.donor_fd
= donor_fd
;
1322 /* Print defrag progress */
1323 print_progress(file
, start
, buf
->st_size
);
1325 ext_list_tmp
= ext_list_head
;
1327 move_data
.orig_start
= ext_list_tmp
->data
.logical
;
1328 /* Logical offset of orig and donor should be same */
1329 move_data
.donor_start
= move_data
.orig_start
;
1330 move_data
.len
= ext_list_tmp
->data
.len
;
1331 move_data
.moved_len
= 0;
1333 ret
= page_in_core(fd
, move_data
, &vec
, &page_num
);
1335 if (mode_flag
& DETAIL
) {
1337 PRINT_ERR_MSG_WITH_ERRNO(
1338 "Failed to get file map");
1340 printf("\t[ NG ]\n");
1345 /* EXT4_IOC_MOVE_EXT */
1347 ioctl(fd
, EXT4_IOC_MOVE_EXT
, &move_data
);
1350 ret
= defrag_fadvise(fd
, move_data
, vec
, page_num
);
1356 if (mode_flag
& DETAIL
) {
1358 PRINT_ERR_MSG_WITH_ERRNO(
1359 "Failed to free page");
1361 printf("\t[ NG ]\n");
1366 if (defraged_ret
< 0) {
1367 if (mode_flag
& DETAIL
) {
1369 PRINT_ERR_MSG_WITH_ERRNO(
1370 "Failed to defrag with "
1371 "EXT4_IOC_MOVE_EXT ioctl");
1372 if (errno
== ENOTTY
)
1373 printf("\tAt least 2.6.31-rc1 of "
1374 "vanilla kernel is required\n");
1376 printf("\t[ NG ]\n");
1380 /* Adjust logical offset for next ioctl */
1381 move_data
.orig_start
+= move_data
.moved_len
;
1382 move_data
.donor_start
= move_data
.orig_start
;
1384 start
= move_data
.orig_start
* buf
->st_blksize
;
1386 /* Print defrag progress */
1387 print_progress(file
, start
, buf
->st_size
);
1390 if (start
>= buf
->st_size
)
1393 ext_list_tmp
= ext_list_tmp
->next
;
1394 } while (ext_list_tmp
!= ext_list_head
);
1400 * file_defrag() - Check file attributes and call ioctl to defrag.
1402 * @file: the file's name.
1403 * @buf: the pointer of the struct stat64.
1405 * @ftwbuf: the pointer of a struct FTW.
1407 static int file_defrag(const char *file
, const struct stat64
*buf
,
1408 int flag
EXT2FS_ATTR((unused
)),
1409 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1415 int file_frags_start
, file_frags_end
;
1416 int orig_physical_cnt
, donor_physical_cnt
= 0;
1417 char tmp_inode_name
[PATH_MAX
+ 8];
1418 ext4_fsblk_t blk_count
= 0;
1419 struct fiemap_extent_list
*orig_list_physical
= NULL
;
1420 struct fiemap_extent_list
*orig_list_logical
= NULL
;
1421 struct fiemap_extent_list
*donor_list_physical
= NULL
;
1422 struct fiemap_extent_list
*donor_list_logical
= NULL
;
1423 struct fiemap_extent_group
*orig_group_head
= NULL
;
1424 struct fiemap_extent_group
*orig_group_tmp
= NULL
;
1426 defraged_file_count
++;
1427 if (defraged_file_count
> total_count
)
1428 total_count
= defraged_file_count
;
1430 if (mode_flag
& DETAIL
) {
1431 printf("[%u/%u]", defraged_file_count
, total_count
);
1435 if (lost_found_dir
[0] != '\0' &&
1436 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1437 if (mode_flag
& DETAIL
) {
1438 PRINT_FILE_NAME(file
);
1439 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1444 if (!S_ISREG(buf
->st_mode
)) {
1445 if (mode_flag
& DETAIL
) {
1446 PRINT_FILE_NAME(file
);
1447 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1453 if (buf
->st_size
== 0) {
1454 if (mode_flag
& DETAIL
) {
1455 PRINT_FILE_NAME(file
);
1456 IN_FTW_PRINT_ERR_MSG("File size is 0");
1462 if (buf
->st_blocks
== 0) {
1463 if (mode_flag
& DETAIL
) {
1464 PRINT_FILE_NAME(file
);
1465 STATISTIC_ERR_MSG("File has no blocks");
1470 fd
= open64(file
, O_RDWR
);
1472 if (mode_flag
& DETAIL
) {
1473 PRINT_FILE_NAME(file
);
1474 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1479 /* Get file's extents */
1480 ret
= get_file_extents(fd
, &orig_list_physical
);
1482 if (mode_flag
& DETAIL
) {
1483 PRINT_FILE_NAME(file
);
1484 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1489 /* Get the count of file's continuous physical region */
1490 orig_physical_cnt
= get_physical_count(orig_list_physical
);
1492 /* Change list from physical to logical */
1493 ret
= change_physical_to_logical(&orig_list_physical
,
1494 &orig_list_logical
);
1496 if (mode_flag
& DETAIL
) {
1497 PRINT_FILE_NAME(file
);
1498 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1503 /* Count file fragments before defrag */
1504 file_frags_start
= get_logical_count(orig_list_logical
);
1506 blk_count
= get_file_blocks(orig_list_logical
);
1507 if (file_check(fd
, buf
, file
, file_frags_start
, blk_count
) < 0)
1510 if (fsync(fd
) < 0) {
1511 if (mode_flag
& DETAIL
) {
1512 PRINT_FILE_NAME(file
);
1513 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1518 best
= get_best_count(blk_count
);
1520 if (file_frags_start
<= best
)
1521 goto check_improvement
;
1523 /* Combine extents to group */
1524 ret
= join_extents(orig_list_logical
, &orig_group_head
);
1526 if (mode_flag
& DETAIL
) {
1527 PRINT_FILE_NAME(file
);
1528 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1533 /* Create donor inode */
1534 memset(tmp_inode_name
, 0, PATH_MAX
+ 8);
1535 sprintf(tmp_inode_name
, "%.*s.defrag",
1536 (int)strnlen(file
, PATH_MAX
), file
);
1537 donor_fd
= open64(tmp_inode_name
, O_WRONLY
| O_CREAT
| O_EXCL
, S_IRUSR
);
1539 if (mode_flag
& DETAIL
) {
1540 PRINT_FILE_NAME(file
);
1541 if (errno
== EEXIST
)
1542 PRINT_ERR_MSG_WITH_ERRNO(
1543 "File is being defraged by other program");
1545 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1550 /* Unlink donor inode */
1551 ret
= unlink(tmp_inode_name
);
1553 if (mode_flag
& DETAIL
) {
1554 PRINT_FILE_NAME(file
);
1555 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1560 /* Allocate space for donor inode */
1561 orig_group_tmp
= orig_group_head
;
1563 ret
= fallocate64(donor_fd
, 0,
1564 (loff_t
)orig_group_tmp
->start
->data
.logical
* block_size
,
1565 (loff_t
)orig_group_tmp
->len
* block_size
);
1567 if (mode_flag
& DETAIL
) {
1568 PRINT_FILE_NAME(file
);
1569 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1574 orig_group_tmp
= orig_group_tmp
->next
;
1575 } while (orig_group_tmp
!= orig_group_head
);
1577 /* Get donor inode's extents */
1578 ret
= get_file_extents(donor_fd
, &donor_list_physical
);
1580 if (mode_flag
& DETAIL
) {
1581 PRINT_FILE_NAME(file
);
1582 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1587 /* Calculate donor inode's continuous physical region */
1588 donor_physical_cnt
= get_physical_count(donor_list_physical
);
1590 /* Change donor extent list from physical to logical */
1591 ret
= change_physical_to_logical(&donor_list_physical
,
1592 &donor_list_logical
);
1594 if (mode_flag
& DETAIL
) {
1595 PRINT_FILE_NAME(file
);
1596 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1602 if (mode_flag
& DETAIL
) {
1603 if (file_frags_start
!= 1)
1604 frag_files_before_defrag
++;
1606 extents_before_defrag
+= file_frags_start
;
1609 if (file_frags_start
<= best
||
1610 orig_physical_cnt
<= donor_physical_cnt
) {
1611 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1612 defraged_file_count
, total_count
, file
, 100);
1613 if (mode_flag
& DETAIL
)
1614 printf(" extents: %d -> %d",
1615 file_frags_start
, file_frags_start
);
1617 printf("\t[ OK ]\n");
1620 if (file_frags_start
!= 1)
1621 frag_files_after_defrag
++;
1623 extents_after_defrag
+= file_frags_start
;
1627 /* Defrag the file */
1628 ret
= call_defrag(fd
, donor_fd
, file
, buf
, donor_list_logical
);
1630 /* Count file fragments after defrag and print extents info */
1631 if (mode_flag
& DETAIL
) {
1632 file_frags_end
= file_frag_count(fd
);
1633 if (file_frags_end
< 0) {
1635 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO
);
1639 if (file_frags_end
!= 1)
1640 frag_files_after_defrag
++;
1642 extents_after_defrag
+= file_frags_end
;
1647 printf(" extents: %d -> %d",
1648 file_frags_start
, file_frags_end
);
1655 printf("\t[ OK ]\n");
1663 free_ext(orig_list_physical
);
1664 free_ext(orig_list_logical
);
1665 free_ext(donor_list_physical
);
1666 free_exts_group(orig_group_head
);
1671 * main() - Ext4 online defrag.
1673 * @argc: the number of parameter.
1674 * @argv[]: the pointer array of parameter.
1676 int main(int argc
, char *argv
[])
1680 int flags
= FTW_PHYS
| FTW_MOUNT
;
1682 int mount_dir_len
= 0;
1683 int success_flag
= 0;
1684 char dir_name
[PATH_MAX
+ 1];
1685 char dev_name
[PATH_MAX
+ 1];
1687 ext2_filsys fs
= NULL
;
1689 printf("e4defrag %s (%s)\n", E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
1691 /* Parse arguments */
1695 while ((opt
= getopt(argc
, argv
, "vc")) != EOF
) {
1698 mode_flag
|= DETAIL
;
1701 mode_flag
|= STATISTIC
;
1711 current_uid
= getuid();
1714 for (i
= optind
; i
< argc
; i
++) {
1718 frag_files_before_defrag
= 0;
1719 frag_files_after_defrag
= 0;
1720 extents_before_defrag
= 0;
1721 extents_after_defrag
= 0;
1722 defraged_file_count
= 0;
1723 files_block_count
= 0;
1724 blocks_per_group
= 0;
1725 feature_incompat
= 0;
1726 log_groups_per_flex
= 0;
1728 memset(dir_name
, 0, PATH_MAX
+ 1);
1729 memset(dev_name
, 0, PATH_MAX
+ 1);
1730 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1731 memset(frag_rank
, 0,
1732 sizeof(struct frag_statistic_ino
) * SHOW_FRAG_FILES
);
1734 if ((mode_flag
& STATISTIC
) && i
> optind
)
1737 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1738 PRINT_ERR_MSG("Endian's type is not big/little endian");
1739 PRINT_FILE_NAME(argv
[i
]);
1743 if (lstat64(argv
[i
], &buf
) < 0) {
1744 perror(NGMSG_FILE_INFO
);
1745 PRINT_FILE_NAME(argv
[i
]);
1749 /* Handle i.e. lvm device symlinks */
1750 if (S_ISLNK(buf
.st_mode
)) {
1753 if (stat64(argv
[i
], &buf2
) == 0 &&
1754 S_ISBLK(buf2
.st_mode
))
1758 if (S_ISBLK(buf
.st_mode
)) {
1760 strncpy(dev_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1761 if (get_mount_point(argv
[i
], dir_name
, PATH_MAX
) < 0)
1763 if (lstat64(dir_name
, &buf
) < 0) {
1764 perror(NGMSG_FILE_INFO
);
1765 PRINT_FILE_NAME(argv
[i
]);
1769 if (!(mode_flag
& STATISTIC
))
1770 printf("ext4 defragmentation for device(%s)\n",
1772 } else if (S_ISDIR(buf
.st_mode
)) {
1774 if (access(argv
[i
], R_OK
) < 0) {
1779 strncpy(dir_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1780 } else if (S_ISREG(buf
.st_mode
)) {
1782 arg_type
= FILENAME
;
1784 /* Irregular file */
1785 PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1786 PRINT_FILE_NAME(argv
[i
]);
1791 block_size
= buf
.st_blksize
;
1794 * filesystem type checked in get_mount_point()
1796 if (arg_type
== FILENAME
|| arg_type
== DIRNAME
) {
1797 if (is_ext4(argv
[i
], dev_name
) < 0)
1799 if (realpath(argv
[i
], dir_name
) == NULL
) {
1800 perror("Couldn't get full path");
1801 PRINT_FILE_NAME(argv
[i
]);
1806 if (current_uid
== ROOT_UID
) {
1807 /* Get super block info */
1808 ret
= ext2fs_open(dev_name
, EXT2_FLAG_64BITS
, 0,
1809 block_size
, unix_io_manager
, &fs
);
1811 if (mode_flag
& DETAIL
)
1813 "Warning: couldn't get file "
1814 "system details for %s: %s\n",
1815 dev_name
, error_message(ret
));
1817 blocks_per_group
= fs
->super
->s_blocks_per_group
;
1818 feature_incompat
= fs
->super
->s_feature_incompat
;
1819 log_groups_per_flex
= fs
->super
->s_log_groups_per_flex
;
1820 ext2fs_close_free(&fs
);
1827 if (!(mode_flag
& STATISTIC
))
1828 printf("ext4 defragmentation "
1829 "for directory(%s)\n", argv
[i
]);
1831 mount_dir_len
= strnlen(lost_found_dir
, PATH_MAX
);
1833 strncat(lost_found_dir
, "/lost+found",
1834 PATH_MAX
- strnlen(lost_found_dir
, PATH_MAX
));
1836 /* Not the case("e4defrag mount_point_dir") */
1837 if (dir_name
[mount_dir_len
] != '\0') {
1839 * "e4defrag mount_point_dir/lost+found"
1840 * or "e4defrag mount_point_dir/lost+found/"
1842 if (strncmp(lost_found_dir
, dir_name
,
1843 strnlen(lost_found_dir
,
1845 (dir_name
[strnlen(lost_found_dir
,
1846 PATH_MAX
)] == '\0' ||
1847 dir_name
[strnlen(lost_found_dir
,
1848 PATH_MAX
)] == '/')) {
1849 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1850 PRINT_FILE_NAME(argv
[i
]);
1854 /* "e4defrag mount_point_dir/else_dir" */
1855 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1859 if (arg_type
== DEVNAME
) {
1860 strncpy(lost_found_dir
, dir_name
,
1861 strnlen(dir_name
, PATH_MAX
));
1862 strncat(lost_found_dir
, "/lost+found/",
1863 PATH_MAX
- strnlen(lost_found_dir
,
1867 nftw64(dir_name
, calc_entry_counts
, FTW_OPEN_FD
, flags
);
1869 if (mode_flag
& STATISTIC
) {
1870 if (mode_flag
& DETAIL
)
1871 printf("%-40s%10s/%-10s%9s\n",
1872 "<File>", "now", "best", "size/ext");
1874 if (!(mode_flag
& DETAIL
) &&
1875 current_uid
!= ROOT_UID
) {
1881 nftw64(dir_name
, file_statistic
,
1882 FTW_OPEN_FD
, flags
);
1884 if (succeed_cnt
!= 0 &&
1885 current_uid
== ROOT_UID
) {
1886 if (mode_flag
& DETAIL
)
1888 printf("%-40s%10s/%-10s%9s\n",
1889 "<Fragmented files>", "now",
1890 "best", "size/ext");
1891 for (j
= 0; j
< SHOW_FRAG_FILES
; j
++) {
1892 if (strlen(frag_rank
[j
].
1894 printf("%d. %s\n%50d/"
1897 frag_rank
[j
].msg_buffer
,
1898 frag_rank
[j
].now_count
,
1899 frag_rank
[j
].best_count
,
1902 } else if (strlen(frag_rank
[j
].
1904 printf("%d. %-37s%10d/"
1907 frag_rank
[j
].msg_buffer
,
1908 frag_rank
[j
].now_count
,
1909 frag_rank
[j
].best_count
,
1918 /* File tree walk */
1919 nftw64(dir_name
, file_defrag
, FTW_OPEN_FD
, flags
);
1920 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt
,
1922 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1923 total_count
- succeed_cnt
, total_count
);
1924 if (mode_flag
& DETAIL
) {
1925 printf("\tTotal extents:\t\t\t%4d->%d\n",
1926 extents_before_defrag
,
1927 extents_after_defrag
);
1928 printf("\tFragmented percentage:\t\t"
1929 "%3llu%%->%llu%%\n",
1930 !regular_count
? 0 :
1931 ((unsigned long long)
1932 frag_files_before_defrag
* 100) /
1934 !regular_count
? 0 :
1935 ((unsigned long long)
1936 frag_files_after_defrag
* 100) /
1943 strncat(lost_found_dir
, "/lost+found/",
1944 PATH_MAX
- strnlen(lost_found_dir
,
1946 if (strncmp(lost_found_dir
, dir_name
,
1947 strnlen(lost_found_dir
,
1949 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1950 PRINT_FILE_NAME(argv
[i
]);
1954 if (mode_flag
& STATISTIC
) {
1955 file_statistic(argv
[i
], &buf
, FTW_F
, NULL
);
1958 printf("ext4 defragmentation for %s\n",
1960 /* Defrag single file process */
1961 file_defrag(argv
[i
], &buf
, FTW_F
, NULL
);
1962 if (succeed_cnt
!= 0)
1963 printf(" Success:\t\t\t[1/1]\n");
1965 printf(" Success:\t\t\t[0/1]\n");
1970 if (succeed_cnt
!= 0)
1972 if (mode_flag
& STATISTIC
) {
1973 if (current_uid
!= ROOT_UID
) {
1979 if (mode_flag
& DETAIL
)
1982 if (arg_type
== DEVNAME
)
1983 printf(" In this device(%s), "
1984 "none can be defragmented.\n", argv
[i
]);
1985 else if (arg_type
== DIRNAME
)
1986 printf(" In this directory(%s), "
1987 "none can be defragmented.\n", argv
[i
]);
1989 printf(" This file(%s) "
1990 "can't be defragmented.\n", argv
[i
]);
1992 float files_ratio
= 0.0;
1994 __u64 size_per_ext
= files_block_count
*
1995 (buf
.st_blksize
/ 1024) /
1996 extents_before_defrag
;
1997 files_ratio
= (float)(extents_before_defrag
-
1998 extents_after_defrag
) *
1999 100 / files_block_count
;
2000 score
= CALC_SCORE(files_ratio
);
2001 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2002 " Average size per extent"
2004 " Fragmentation score\t\t\t\t%.0f\n",
2005 extents_before_defrag
,
2006 extents_after_defrag
,
2007 size_per_ext
, score
);
2008 printf(" [0-30 no problem:"
2009 " 31-55 a little bit fragmented:"
2010 " 56- needs defrag]\n");
2012 if (arg_type
== DEVNAME
)
2013 printf(" This device (%s) ", argv
[i
]);
2014 else if (arg_type
== DIRNAME
)
2015 printf(" This directory (%s) ",
2018 printf(" This file (%s) ", argv
[i
]);
2020 if (score
> BOUND_SCORE
)
2021 printf("needs defragmentation.\n");
2023 printf("does not need "
2024 "defragmentation.\n");