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>
42 #include <sys/syscall.h>
45 /* A relatively new ioctl interface ... */
46 #ifndef EXT4_IOC_MOVE_EXT
47 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
51 #define PRINT_ERR_MSG(msg) fprintf(stderr, "%s\n", (msg))
52 #define IN_FTW_PRINT_ERR_MSG(msg) \
53 fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
54 #define PRINT_FILE_NAME(file) fprintf(stderr, " \"%s\"\n", (file))
55 #define PRINT_ERR_MSG_WITH_ERRNO(msg) \
56 fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
57 #define STATISTIC_ERR_MSG(msg) \
58 fprintf(stderr, "\t%s\n", (msg))
59 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg) \
60 fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
61 #define min(x, y) (((x) > (y)) ? (y) : (x))
62 #define CALC_SCORE(ratio) \
63 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
64 /* Wrap up the free function */
70 /* Insert list2 after list1 */
71 #define insert(list1, list2) \
73 list2->next = list1->next; \
74 list1->next->prev = list2; \
75 list2->prev = list1; \
76 list1->next = list2; \
79 /* To delete unused warning */
81 #define EXT2FS_ATTR(x) __attribute__(x)
83 #define EXT2FS_ATTR(x)
86 /* The mode of defrag */
88 #define STATISTIC 0x02
94 #define FTW_OPEN_FD 2000
96 #define FS_EXT4 "ext4"
99 #define BOUND_SCORE 55
100 #define SHOW_FRAG_FILES 5
102 /* Magic number for ext4 */
103 #define EXT4_SUPER_MAGIC 0xEF53
105 /* Definition of flex_bg */
106 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
108 /* The following macro is used for ioctl FS_IOC_FIEMAP
109 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
110 * kernel-space and user-space per ioctl
112 #define EXTENT_MAX_COUNT 512
114 /* The following macros are error message */
116 "Usage : e4defrag [-v] file...| directory...| device...\n\
117 : e4defrag -c file...| directory...| device...\n"
119 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
120 #define NGMSG_FILE_EXTENT "Failed to get file extents"
121 #define NGMSG_FILE_INFO "Failed to get file information"
122 #define NGMSG_FILE_OPEN "Failed to open"
123 #define NGMSG_FILE_UNREG "File is not regular file"
124 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
126 /* Data type for filesystem-wide blocks number */
127 typedef unsigned long long ext4_fsblk_t
;
129 struct fiemap_extent_data
{
130 __u64 len
; /* blocks count */
131 __u64 logical
; /* start logical block number */
132 ext4_fsblk_t physical
; /* start physical block number */
135 struct fiemap_extent_list
{
136 struct fiemap_extent_list
*prev
;
137 struct fiemap_extent_list
*next
;
138 struct fiemap_extent_data data
; /* extent belong to file */
141 struct fiemap_extent_group
{
142 struct fiemap_extent_group
*prev
;
143 struct fiemap_extent_group
*next
;
144 __u64 len
; /* length of this continuous region */
145 struct fiemap_extent_list
*start
; /* start ext */
146 struct fiemap_extent_list
*end
; /* end ext */
150 __s32 reserved
; /* original file descriptor */
151 __u32 donor_fd
; /* donor file descriptor */
152 __u64 orig_start
; /* logical start offset in block for orig */
153 __u64 donor_start
; /* logical start offset in block for donor */
154 __u64 len
; /* block length to be moved */
155 __u64 moved_len
; /* moved block length */
158 struct frag_statistic_ino
{
159 int now_count
; /* the file's extents count of before defrag */
160 int best_count
; /* the best file's extents count */
161 __u64 size_per_ext
; /* size(KB) per extent */
162 float ratio
; /* the ratio of fragmentation */
163 char msg_buffer
[PATH_MAX
+ 1]; /* pathname of the file */
166 static char lost_found_dir
[PATH_MAX
+ 1];
167 static int block_size
;
168 static int extents_before_defrag
;
169 static int extents_after_defrag
;
170 static int mode_flag
;
171 static unsigned int current_uid
;
172 static unsigned int defraged_file_count
;
173 static unsigned int frag_files_before_defrag
;
174 static unsigned int frag_files_after_defrag
;
175 static unsigned int regular_count
;
176 static unsigned int succeed_cnt
;
177 static unsigned int total_count
;
178 static __u8 log_groups_per_flex
;
179 static __u32 blocks_per_group
;
180 static __u32 feature_incompat
;
181 static ext4_fsblk_t files_block_count
;
182 static struct frag_statistic_ino frag_rank
[SHOW_FRAG_FILES
];
186 * We prefer posix_fadvise64 when available, as it allows 64bit offset on
189 #if defined(HAVE_POSIX_FADVISE64)
190 #define posix_fadvise posix_fadvise64
191 #elif defined(HAVE_FADVISE64)
192 #define posix_fadvise fadvise64
193 #elif !defined(HAVE_POSIX_FADVISE)
194 #error posix_fadvise not available!
198 * Local definitions of some syscalls glibc may not yet have
200 #ifndef HAVE_SYNC_FILE_RANGE
201 #warning Using locally defined sync_file_range interface.
203 #ifndef __NR_sync_file_range
204 #ifndef __NR_sync_file_range2 /* ppc */
205 #error Your kernel headers dont define __NR_sync_file_range
210 * sync_file_range() - Sync file region.
212 * @fd: defrag target file's descriptor.
213 * @offset: file offset.
214 * @length: area length.
215 * @flag: process flag.
217 int sync_file_range(int fd
, loff_t offset
, loff_t length
, unsigned int flag
)
219 #ifdef __NR_sync_file_range
220 return syscall(__NR_sync_file_range
, fd
, offset
, length
, flag
);
222 return syscall(__NR_sync_file_range2
, fd
, flag
, offset
, length
);
225 #endif /* ! HAVE_SYNC_FILE_RANGE */
227 #ifndef HAVE_FALLOCATE64
228 #warning Using locally defined fallocate syscall interface.
230 #ifndef __NR_fallocate
231 #error Your kernel headers dont define __NR_fallocate
235 * fallocate64() - Manipulate file space.
237 * @fd: defrag target file's descriptor.
238 * @mode: process flag.
239 * @offset: file offset.
242 static int fallocate64(int fd
, int mode
, loff_t offset
, loff_t len
)
244 return syscall(__NR_fallocate
, fd
, mode
, offset
, len
);
246 #endif /* ! HAVE_FALLOCATE */
249 * get_mount_point() - Get device's mount point.
251 * @devname: the device's name.
252 * @mount_point: the mount point.
253 * @dir_path_len: the length of directory.
255 static int get_mount_point(const char *devname
, char *mount_point
,
258 /* Refer to /etc/mtab */
259 const char *mtab
= MOUNTED
;
261 struct mntent
*mnt
= NULL
;
264 if (stat64(devname
, &sb
) < 0) {
265 perror(NGMSG_FILE_INFO
);
266 PRINT_FILE_NAME(devname
);
270 fp
= setmntent(mtab
, "r");
272 perror("Couldn't access /etc/mtab");
276 while ((mnt
= getmntent(fp
)) != NULL
) {
280 * To handle device symlinks, we see if the
281 * device number matches, not the name
283 if (stat64(mnt
->mnt_fsname
, &ms
) < 0)
285 if (sb
.st_rdev
!= ms
.st_rdev
)
289 if (strcmp(mnt
->mnt_type
, FS_EXT4
) == 0) {
290 strncpy(mount_point
, mnt
->mnt_dir
,
294 PRINT_ERR_MSG(NGMSG_EXT4
);
298 PRINT_ERR_MSG("Filesystem is not mounted");
303 * is_ext4() - Whether on an ext4 filesystem.
305 * @file: the file's name.
307 static int is_ext4(const char *file
, char *devname
)
312 char *mnt_type
= NULL
;
313 /* Refer to /etc/mtab */
314 const char *mtab
= MOUNTED
;
315 char file_path
[PATH_MAX
+ 1];
316 struct mntent
*mnt
= NULL
;
317 struct statfs64 fsbuf
;
320 if (realpath(file
, file_path
) == NULL
) {
321 perror("Couldn't get full path");
322 PRINT_FILE_NAME(file
);
326 if (statfs64(file_path
, &fsbuf
) < 0) {
327 perror("Failed to get filesystem information");
328 PRINT_FILE_NAME(file
);
332 if (fsbuf
.f_type
!= EXT4_SUPER_MAGIC
) {
333 PRINT_ERR_MSG(NGMSG_EXT4
);
337 fp
= setmntent(mtab
, "r");
339 perror("Couldn't access /etc/mtab");
343 while ((mnt
= getmntent(fp
)) != NULL
) {
344 if (mnt
->mnt_fsname
[0] != '/')
346 len
= strlen(mnt
->mnt_dir
);
347 ret
= memcmp(file_path
, mnt
->mnt_dir
, len
);
356 mnt_type
= realloc(mnt_type
, strlen(mnt
->mnt_type
) + 1);
357 if (mnt_type
== NULL
) {
361 memset(mnt_type
, 0, strlen(mnt
->mnt_type
) + 1);
362 strncpy(mnt_type
, mnt
->mnt_type
, strlen(mnt
->mnt_type
));
363 strncpy(lost_found_dir
, mnt
->mnt_dir
, PATH_MAX
);
364 strncpy(devname
, mnt
->mnt_fsname
, strlen(mnt
->mnt_fsname
) + 1);
368 if (mnt_type
&& strcmp(mnt_type
, FS_EXT4
) == 0) {
373 PRINT_ERR_MSG(NGMSG_EXT4
);
379 * calc_entry_counts() - Calculate file counts.
384 * @ftwbuf: the pointer of a struct FTW.
386 static int calc_entry_counts(const char *file
EXT2FS_ATTR((unused
)),
387 const struct stat64
*buf
, int flag
EXT2FS_ATTR((unused
)),
388 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
390 if (S_ISREG(buf
->st_mode
))
399 * page_in_core() - Get information on whether pages are in core.
401 * @fd: defrag target file's descriptor.
402 * @defrag_data: data used for defrag.
403 * @vec: page state array.
404 * @page_num: page number.
406 static int page_in_core(int fd
, struct move_extent defrag_data
,
407 unsigned char **vec
, unsigned int *page_num
)
411 loff_t offset
, end_offset
, length
;
413 if (vec
== NULL
|| *vec
!= NULL
)
416 pagesize
= sysconf(_SC_PAGESIZE
);
419 /* In mmap, offset should be a multiple of the page size */
420 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
421 length
= (loff_t
)defrag_data
.len
* block_size
;
422 end_offset
= offset
+ length
;
423 /* Round the offset down to the nearest multiple of pagesize */
424 offset
= (offset
/ pagesize
) * pagesize
;
425 length
= end_offset
- offset
;
427 page
= mmap(NULL
, length
, PROT_READ
, MAP_SHARED
, fd
, offset
);
428 if (page
== MAP_FAILED
)
432 *page_num
= (length
+ pagesize
- 1) / pagesize
;
433 *vec
= (unsigned char *)calloc(*page_num
, 1);
437 /* Get information on whether pages are in core */
438 if (mincore(page
, (size_t)length
, *vec
) == -1 ||
439 munmap(page
, length
) == -1) {
448 * defrag_fadvise() - Predeclare an access pattern for file data.
450 * @fd: defrag target file's descriptor.
451 * @defrag_data: data used for defrag.
452 * @vec: page state array.
453 * @page_num: page number.
455 static int defrag_fadvise(int fd
, struct move_extent defrag_data
,
456 unsigned char *vec
, unsigned int page_num
)
459 long pagesize
= sysconf(_SC_PAGESIZE
);
460 int fadvise_flag
= POSIX_FADV_DONTNEED
;
461 int sync_flag
= SYNC_FILE_RANGE_WAIT_BEFORE
|
462 SYNC_FILE_RANGE_WRITE
|
463 SYNC_FILE_RANGE_WAIT_AFTER
;
470 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
471 offset
= (offset
/ pagesize
) * pagesize
;
473 /* Sync file for fadvise process */
474 if (sync_file_range(fd
, offset
,
475 (loff_t
)pagesize
* page_num
, sync_flag
) < 0)
478 /* Try to release buffer cache which this process used,
479 * then other process can use the released buffer
481 for (i
= 0; i
< page_num
; i
++) {
482 if ((vec
[i
] & 0x1) == 0) {
486 if (posix_fadvise(fd
, offset
, pagesize
, fadvise_flag
) < 0) {
487 if ((mode_flag
& DETAIL
) && flag
) {
488 perror("\tFailed to fadvise");
499 * check_free_size() - Check if there's enough disk space.
501 * @fd: defrag target file's descriptor.
503 * @blk_count: file blocks.
505 static int check_free_size(int fd
, const char *file
, ext4_fsblk_t blk_count
)
507 ext4_fsblk_t free_blk_count
;
508 struct statfs64 fsbuf
;
510 if (fstatfs64(fd
, &fsbuf
) < 0) {
511 if (mode_flag
& DETAIL
) {
512 PRINT_FILE_NAME(file
);
513 PRINT_ERR_MSG_WITH_ERRNO(
514 "Failed to get filesystem information");
519 /* Compute free space for root and normal user separately */
520 if (current_uid
== ROOT_UID
)
521 free_blk_count
= fsbuf
.f_bfree
;
523 free_blk_count
= fsbuf
.f_bavail
;
525 if (free_blk_count
>= blk_count
)
532 * file_frag_count() - Get file fragment count.
534 * @fd: defrag target file's descriptor.
536 static int file_frag_count(int fd
)
539 struct fiemap fiemap_buf
;
541 /* When fm_extent_count is 0,
542 * ioctl just get file fragment count.
544 memset(&fiemap_buf
, 0, sizeof(struct fiemap
));
545 fiemap_buf
.fm_start
= 0;
546 fiemap_buf
.fm_length
= FIEMAP_MAX_OFFSET
;
547 fiemap_buf
.fm_flags
|= FIEMAP_FLAG_SYNC
;
549 ret
= ioctl(fd
, FS_IOC_FIEMAP
, &fiemap_buf
);
553 return fiemap_buf
.fm_mapped_extents
;
557 * file_check() - Check file's attributes.
559 * @fd: defrag target file's descriptor.
560 * @buf: a pointer of the struct stat64.
562 * @extents: file extents.
563 * @blk_count: file blocks.
565 static int file_check(int fd
, const struct stat64
*buf
, const char *file
,
566 int extents
, ext4_fsblk_t blk_count
)
571 /* Write-lock check is more reliable */
572 lock
.l_type
= F_WRLCK
;
574 lock
.l_whence
= SEEK_SET
;
578 ret
= check_free_size(fd
, file
, blk_count
);
580 if ((mode_flag
& DETAIL
) && ret
== -ENOSPC
) {
581 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
582 " extents: %d -> %d\n", defraged_file_count
,
583 total_count
, file
, extents
, extents
);
584 IN_FTW_PRINT_ERR_MSG(
585 "Defrag size is larger than filesystem's free space");
590 /* Access authority */
591 if (current_uid
!= ROOT_UID
&&
592 buf
->st_uid
!= current_uid
) {
593 if (mode_flag
& DETAIL
) {
594 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
595 " extents: %d -> %d\n", defraged_file_count
,
596 total_count
, file
, extents
, extents
);
597 IN_FTW_PRINT_ERR_MSG(
598 "File is not current user's file"
599 " or current user is not root");
605 if (fcntl(fd
, F_GETLK
, &lock
) < 0) {
606 if (mode_flag
& DETAIL
) {
607 PRINT_FILE_NAME(file
);
608 PRINT_ERR_MSG_WITH_ERRNO(
609 "Failed to get lock information");
612 } else if (lock
.l_type
!= F_UNLCK
) {
613 if (mode_flag
& DETAIL
) {
614 PRINT_FILE_NAME(file
);
615 IN_FTW_PRINT_ERR_MSG("File has been locked");
624 * insert_extent_by_logical() - Sequentially insert extent by logical.
626 * @ext_list_head: the head of logical extent list.
627 * @ext: the extent element which will be inserted.
629 static int insert_extent_by_logical(struct fiemap_extent_list
**ext_list_head
,
630 struct fiemap_extent_list
*ext
)
632 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
638 if (*ext_list_head
== NULL
) {
639 (*ext_list_head
) = ext
;
640 (*ext_list_head
)->prev
= *ext_list_head
;
641 (*ext_list_head
)->next
= *ext_list_head
;
645 if (ext
->data
.logical
<= ext_list_tmp
->data
.logical
) {
646 /* Insert before head */
647 if (ext_list_tmp
->data
.logical
<
648 ext
->data
.logical
+ ext
->data
.len
)
652 *ext_list_head
= ext
;
654 /* Insert into the middle or last of the list */
656 if (ext
->data
.logical
< ext_list_tmp
->data
.logical
)
658 ext_list_tmp
= ext_list_tmp
->next
;
659 } while (ext_list_tmp
!= (*ext_list_head
));
660 if (ext
->data
.logical
<
661 ext_list_tmp
->prev
->data
.logical
+
662 ext_list_tmp
->prev
->data
.len
)
666 if (ext_list_tmp
!= *ext_list_head
&&
667 ext_list_tmp
->data
.logical
<
668 ext
->data
.logical
+ ext
->data
.len
)
672 ext_list_tmp
= ext_list_tmp
->prev
;
673 /* Insert "ext" after "ext_list_tmp" */
674 insert(ext_list_tmp
, ext
);
682 * insert_extent_by_physical() - Sequentially insert extent by physical.
684 * @ext_list_head: the head of physical extent list.
685 * @ext: the extent element which will be inserted.
687 static int insert_extent_by_physical(struct fiemap_extent_list
**ext_list_head
,
688 struct fiemap_extent_list
*ext
)
690 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
696 if (*ext_list_head
== NULL
) {
697 (*ext_list_head
) = ext
;
698 (*ext_list_head
)->prev
= *ext_list_head
;
699 (*ext_list_head
)->next
= *ext_list_head
;
703 if (ext
->data
.physical
<= ext_list_tmp
->data
.physical
) {
704 /* Insert before head */
705 if (ext_list_tmp
->data
.physical
<
706 ext
->data
.physical
+ ext
->data
.len
)
710 *ext_list_head
= ext
;
712 /* Insert into the middle or last of the list */
714 if (ext
->data
.physical
< ext_list_tmp
->data
.physical
)
716 ext_list_tmp
= ext_list_tmp
->next
;
717 } while (ext_list_tmp
!= (*ext_list_head
));
718 if (ext
->data
.physical
<
719 ext_list_tmp
->prev
->data
.physical
+
720 ext_list_tmp
->prev
->data
.len
)
724 if (ext_list_tmp
!= *ext_list_head
&&
725 ext_list_tmp
->data
.physical
<
726 ext
->data
.physical
+ ext
->data
.len
)
730 ext_list_tmp
= ext_list_tmp
->prev
;
731 /* Insert "ext" after "ext_list_tmp" */
732 insert(ext_list_tmp
, ext
);
740 * insert_exts_group() - Insert a exts_group.
742 * @ext_group_head: the head of a exts_group list.
743 * @exts_group: the exts_group element which will be inserted.
745 static int insert_exts_group(struct fiemap_extent_group
**ext_group_head
,
746 struct fiemap_extent_group
*exts_group
)
748 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
750 if (exts_group
== NULL
) {
755 /* Initialize list */
756 if (*ext_group_head
== NULL
) {
757 (*ext_group_head
) = exts_group
;
758 (*ext_group_head
)->prev
= *ext_group_head
;
759 (*ext_group_head
)->next
= *ext_group_head
;
763 ext_group_tmp
= (*ext_group_head
)->prev
;
764 insert(ext_group_tmp
, exts_group
);
770 * join_extents() - Find continuous region(exts_group).
772 * @ext_list_head: the head of the extent list.
773 * @ext_group_head: the head of the target exts_group list.
775 static int join_extents(struct fiemap_extent_list
*ext_list_head
,
776 struct fiemap_extent_group
**ext_group_head
)
778 __u64 len
= ext_list_head
->data
.len
;
779 struct fiemap_extent_list
*ext_list_start
= ext_list_head
;
780 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
->next
;
783 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
785 /* This extent and previous extent are not continuous,
786 * so, all previous extents are treated as an extent group.
788 if ((ext_list_tmp
->prev
->data
.logical
+
789 ext_list_tmp
->prev
->data
.len
)
790 != ext_list_tmp
->data
.logical
) {
792 malloc(sizeof(struct fiemap_extent_group
));
793 if (ext_group_tmp
== NULL
)
796 memset(ext_group_tmp
, 0,
797 sizeof(struct fiemap_extent_group
));
798 ext_group_tmp
->len
= len
;
799 ext_group_tmp
->start
= ext_list_start
;
800 ext_group_tmp
->end
= ext_list_tmp
->prev
;
802 if (insert_exts_group(ext_group_head
,
803 ext_group_tmp
) < 0) {
807 ext_list_start
= ext_list_tmp
;
808 len
= ext_list_tmp
->data
.len
;
809 ext_list_tmp
= ext_list_tmp
->next
;
813 /* This extent and previous extent are continuous,
814 * so, they belong to the same extent group, and we check
815 * if the next extent belongs to the same extent group.
817 len
+= ext_list_tmp
->data
.len
;
818 ext_list_tmp
= ext_list_tmp
->next
;
819 } while (ext_list_tmp
!= ext_list_head
->next
);
825 * get_file_extents() - Get file's extent list.
827 * @fd: defrag target file's descriptor.
828 * @ext_list_head: the head of the extent list.
830 static int get_file_extents(int fd
, struct fiemap_extent_list
**ext_list_head
)
834 int ext_buf_size
, fie_buf_size
;
836 struct fiemap
*fiemap_buf
= NULL
;
837 struct fiemap_extent
*ext_buf
= NULL
;
838 struct fiemap_extent_list
*ext_list
= NULL
;
840 /* Convert units, in bytes.
841 * Be careful : now, physical block number in extent is 48bit,
842 * and the maximum blocksize for ext4 is 4K(12bit),
843 * so there is no overflow, but in future it may be changed.
846 /* Alloc space for fiemap */
847 ext_buf_size
= EXTENT_MAX_COUNT
* sizeof(struct fiemap_extent
);
848 fie_buf_size
= sizeof(struct fiemap
) + ext_buf_size
;
850 fiemap_buf
= malloc(fie_buf_size
);
851 if (fiemap_buf
== NULL
)
854 ext_buf
= fiemap_buf
->fm_extents
;
855 memset(fiemap_buf
, 0, fie_buf_size
);
856 fiemap_buf
->fm_length
= FIEMAP_MAX_OFFSET
;
857 fiemap_buf
->fm_flags
|= FIEMAP_FLAG_SYNC
;
858 fiemap_buf
->fm_extent_count
= EXTENT_MAX_COUNT
;
861 fiemap_buf
->fm_start
= pos
;
862 memset(ext_buf
, 0, ext_buf_size
);
863 ret
= ioctl(fd
, FS_IOC_FIEMAP
, fiemap_buf
);
864 if (ret
< 0 || fiemap_buf
->fm_mapped_extents
== 0)
866 for (i
= 0; i
< fiemap_buf
->fm_mapped_extents
; i
++) {
868 ext_list
= malloc(sizeof(struct fiemap_extent_list
));
869 if (ext_list
== NULL
)
872 ext_list
->data
.physical
= ext_buf
[i
].fe_physical
874 ext_list
->data
.logical
= ext_buf
[i
].fe_logical
876 ext_list
->data
.len
= ext_buf
[i
].fe_length
879 ret
= insert_extent_by_physical(
880 ext_list_head
, ext_list
);
886 /* Record file's logical offset this time */
887 pos
= ext_buf
[EXTENT_MAX_COUNT
-1].fe_logical
+
888 ext_buf
[EXTENT_MAX_COUNT
-1].fe_length
;
890 * If fm_extents array has been filled and
891 * there are extents left, continue to cycle.
893 } while (fiemap_buf
->fm_mapped_extents
894 == EXTENT_MAX_COUNT
&&
895 !(ext_buf
[EXTENT_MAX_COUNT
-1].fe_flags
896 & FIEMAP_EXTENT_LAST
));
906 * get_logical_count() - Get the file logical extents count.
908 * @logical_list_head: the head of the logical extent list.
910 static int get_logical_count(struct fiemap_extent_list
*logical_list_head
)
913 struct fiemap_extent_list
*ext_list_tmp
= logical_list_head
;
917 ext_list_tmp
= ext_list_tmp
->next
;
918 } while (ext_list_tmp
!= logical_list_head
);
924 * get_physical_count() - Get the file physical extents count.
926 * @physical_list_head: the head of the physical extent list.
928 static int get_physical_count(struct fiemap_extent_list
*physical_list_head
)
931 struct fiemap_extent_list
*ext_list_tmp
= physical_list_head
;
934 if ((ext_list_tmp
->data
.physical
+ ext_list_tmp
->data
.len
)
935 != ext_list_tmp
->next
->data
.physical
) {
936 /* This extent and next extent are not continuous. */
940 ext_list_tmp
= ext_list_tmp
->next
;
941 } while (ext_list_tmp
!= physical_list_head
);
947 * change_physical_to_logical() - Change list from physical to logical.
949 * @physical_list_head: the head of physical extent list.
950 * @logical_list_head: the head of logical extent list.
952 static int change_physical_to_logical(
953 struct fiemap_extent_list
**physical_list_head
,
954 struct fiemap_extent_list
**logical_list_head
)
957 struct fiemap_extent_list
*ext_list_tmp
= *physical_list_head
;
958 struct fiemap_extent_list
*ext_list_next
= ext_list_tmp
->next
;
961 if (ext_list_tmp
== ext_list_next
) {
962 ret
= insert_extent_by_logical(
963 logical_list_head
, ext_list_tmp
);
967 *physical_list_head
= NULL
;
971 ext_list_tmp
->prev
->next
= ext_list_tmp
->next
;
972 ext_list_tmp
->next
->prev
= ext_list_tmp
->prev
;
973 *physical_list_head
= ext_list_next
;
975 ret
= insert_extent_by_logical(
976 logical_list_head
, ext_list_tmp
);
981 ext_list_tmp
= ext_list_next
;
982 ext_list_next
= ext_list_next
->next
;
988 /* get_file_blocks() - Get total file blocks.
990 * @ext_list_head: the extent list head of the target file
992 static ext4_fsblk_t
get_file_blocks(struct fiemap_extent_list
*ext_list_head
)
994 ext4_fsblk_t blk_count
= 0;
995 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
;
998 blk_count
+= ext_list_tmp
->data
.len
;
999 ext_list_tmp
= ext_list_tmp
->next
;
1000 } while (ext_list_tmp
!= ext_list_head
);
1006 * free_ext() - Free the extent list.
1008 * @ext_list_head: the extent list head of which will be free.
1010 static void free_ext(struct fiemap_extent_list
*ext_list_head
)
1012 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1014 if (ext_list_head
== NULL
)
1017 while (ext_list_head
->next
!= ext_list_head
) {
1018 ext_list_tmp
= ext_list_head
;
1019 ext_list_head
->prev
->next
= ext_list_head
->next
;
1020 ext_list_head
->next
->prev
= ext_list_head
->prev
;
1021 ext_list_head
= ext_list_head
->next
;
1024 free(ext_list_head
);
1028 * free_exts_group() - Free the exts_group.
1030 * @*ext_group_head: the exts_group list head which will be free.
1032 static void free_exts_group(struct fiemap_extent_group
*ext_group_head
)
1034 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
1036 if (ext_group_head
== NULL
)
1039 while (ext_group_head
->next
!= ext_group_head
) {
1040 ext_group_tmp
= ext_group_head
;
1041 ext_group_head
->prev
->next
= ext_group_head
->next
;
1042 ext_group_head
->next
->prev
= ext_group_head
->prev
;
1043 ext_group_head
= ext_group_head
->next
;
1044 free(ext_group_tmp
);
1046 free(ext_group_head
);
1050 * get_best_count() - Get the file best extents count.
1052 * @block_count: the file's physical block count.
1054 static int get_best_count(ext4_fsblk_t block_count
)
1057 unsigned int flex_bg_num
;
1059 /* Calcuate best extents count */
1060 if (feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
1061 flex_bg_num
= 1 << log_groups_per_flex
;
1062 ret
= ((block_count
- 1) /
1063 ((ext4_fsblk_t
)blocks_per_group
*
1066 ret
= ((block_count
- 1) / blocks_per_group
) + 1;
1073 * file_statistic() - Get statistic info of the file's fragments.
1075 * @file: the file's name.
1076 * @buf: the pointer of the struct stat64.
1078 * @ftwbuf: the pointer of a struct FTW.
1080 static int file_statistic(const char *file
, const struct stat64
*buf
,
1081 int flag
EXT2FS_ATTR((unused
)),
1082 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1086 int now_ext_count
, best_ext_count
= 0, physical_ext_count
;
1088 __u64 size_per_ext
= 0;
1090 ext4_fsblk_t blk_count
= 0;
1091 char msg_buffer
[PATH_MAX
+ 24];
1092 struct fiemap_extent_list
*physical_list_head
= NULL
;
1093 struct fiemap_extent_list
*logical_list_head
= NULL
;
1095 defraged_file_count
++;
1097 if (mode_flag
& DETAIL
) {
1098 if (total_count
== 1 && regular_count
== 1)
1101 printf("[%u/%u]", defraged_file_count
, total_count
);
1105 if (lost_found_dir
[0] != '\0' &&
1106 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1107 if (mode_flag
& DETAIL
) {
1108 PRINT_FILE_NAME(file
);
1109 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND
);
1114 if (!S_ISREG(buf
->st_mode
)) {
1115 if (mode_flag
& DETAIL
) {
1116 PRINT_FILE_NAME(file
);
1117 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG
);
1122 /* Access authority */
1123 if (current_uid
!= ROOT_UID
&&
1124 buf
->st_uid
!= current_uid
) {
1125 if (mode_flag
& DETAIL
) {
1126 PRINT_FILE_NAME(file
);
1128 "File is not current user's file"
1129 " or current user is not root");
1135 if (buf
->st_size
== 0) {
1136 if (mode_flag
& DETAIL
) {
1137 PRINT_FILE_NAME(file
);
1138 STATISTIC_ERR_MSG("File size is 0");
1144 if (buf
->st_blocks
== 0) {
1145 if (mode_flag
& DETAIL
) {
1146 PRINT_FILE_NAME(file
);
1147 STATISTIC_ERR_MSG("File has no blocks");
1152 fd
= open64(file
, O_RDONLY
);
1154 if (mode_flag
& DETAIL
) {
1155 PRINT_FILE_NAME(file
);
1156 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1161 /* Get file's physical extents */
1162 ret
= get_file_extents(fd
, &physical_list_head
);
1164 if (mode_flag
& DETAIL
) {
1165 PRINT_FILE_NAME(file
);
1166 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1171 /* Get the count of file's continuous physical region */
1172 physical_ext_count
= get_physical_count(physical_list_head
);
1174 /* Change list from physical to logical */
1175 ret
= change_physical_to_logical(&physical_list_head
,
1176 &logical_list_head
);
1178 if (mode_flag
& DETAIL
) {
1179 PRINT_FILE_NAME(file
);
1180 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1185 /* Count file fragments before defrag */
1186 now_ext_count
= get_logical_count(logical_list_head
);
1188 if (current_uid
== ROOT_UID
) {
1189 /* Calculate the size per extent */
1190 blk_count
= get_file_blocks(logical_list_head
);
1192 best_ext_count
= get_best_count(blk_count
);
1194 /* e4defrag rounds size_per_ext up to a block size boundary */
1195 size_per_ext
= blk_count
* (buf
->st_blksize
/ 1024) /
1198 ratio
= (float)(physical_ext_count
- best_ext_count
) * 100 /
1201 extents_before_defrag
+= now_ext_count
;
1202 extents_after_defrag
+= best_ext_count
;
1203 files_block_count
+= blk_count
;
1206 if (total_count
== 1 && regular_count
== 1) {
1208 if (mode_flag
& DETAIL
) {
1210 struct fiemap_extent_list
*ext_list_tmp
=
1213 /* Print extents info */
1216 printf("[ext %d]:\tstart %llu:\tlogical "
1217 "%llu:\tlen %llu\n", count
,
1218 ext_list_tmp
->data
.physical
,
1219 ext_list_tmp
->data
.logical
,
1220 ext_list_tmp
->data
.len
);
1221 ext_list_tmp
= ext_list_tmp
->next
;
1222 } while (ext_list_tmp
!= logical_list_head
);
1225 printf("%-40s%10s/%-10s%9s\n",
1226 "<File>", "now", "best", "size/ext");
1227 if (current_uid
== ROOT_UID
) {
1228 if (strlen(file
) > 40)
1229 printf("%s\n%50d/%-10d%6llu KB\n",
1230 file
, now_ext_count
,
1231 best_ext_count
, size_per_ext
);
1233 printf("%-40s%10d/%-10d%6llu KB\n",
1234 file
, now_ext_count
,
1235 best_ext_count
, size_per_ext
);
1237 if (strlen(file
) > 40)
1238 printf("%s\n%50d/%-10s%7s\n",
1239 file
, now_ext_count
,
1242 printf("%-40s%10d/%-10s%7s\n",
1243 file
, now_ext_count
,
1251 if (mode_flag
& DETAIL
) {
1252 /* Print statistic info */
1253 sprintf(msg_buffer
, "[%u/%u]%s",
1254 defraged_file_count
, total_count
, file
);
1255 if (current_uid
== ROOT_UID
) {
1256 if (strlen(msg_buffer
) > 40)
1257 printf("\033[79;0H\033[K%s\n"
1258 "%50d/%-10d%6llu KB\n",
1259 msg_buffer
, now_ext_count
,
1260 best_ext_count
, size_per_ext
);
1262 printf("\033[79;0H\033[K%-40s"
1263 "%10d/%-10d%6llu KB\n",
1264 msg_buffer
, now_ext_count
,
1265 best_ext_count
, size_per_ext
);
1267 if (strlen(msg_buffer
) > 40)
1268 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1269 msg_buffer
, now_ext_count
,
1272 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1273 msg_buffer
, now_ext_count
,
1278 for (i
= 0; i
< SHOW_FRAG_FILES
; i
++) {
1279 if (ratio
>= frag_rank
[i
].ratio
) {
1280 for (j
= SHOW_FRAG_FILES
- 1; j
> i
; j
--) {
1281 memset(&frag_rank
[j
], 0,
1282 sizeof(struct frag_statistic_ino
));
1283 strncpy(frag_rank
[j
].msg_buffer
,
1284 frag_rank
[j
- 1].msg_buffer
,
1285 strnlen(frag_rank
[j
- 1].msg_buffer
,
1287 frag_rank
[j
].now_count
=
1288 frag_rank
[j
- 1].now_count
;
1289 frag_rank
[j
].best_count
=
1290 frag_rank
[j
- 1].best_count
;
1291 frag_rank
[j
].size_per_ext
=
1292 frag_rank
[j
- 1].size_per_ext
;
1293 frag_rank
[j
].ratio
=
1294 frag_rank
[j
- 1].ratio
;
1296 memset(&frag_rank
[i
], 0,
1297 sizeof(struct frag_statistic_ino
));
1298 strncpy(frag_rank
[i
].msg_buffer
, file
,
1299 strnlen(file
, PATH_MAX
));
1300 frag_rank
[i
].now_count
= now_ext_count
;
1301 frag_rank
[i
].best_count
= best_ext_count
;
1302 frag_rank
[i
].size_per_ext
= size_per_ext
;
1303 frag_rank
[i
].ratio
= ratio
;
1312 free_ext(physical_list_head
);
1313 free_ext(logical_list_head
);
1318 * print_progress - Print defrag progress
1321 * @start: logical offset for defrag target file
1322 * @file_size: defrag target filesize
1324 static void print_progress(const char *file
, loff_t start
, loff_t file_size
)
1326 int percent
= (start
* 100) / file_size
;
1327 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1328 defraged_file_count
, total_count
, file
, min(percent
, 100));
1335 * call_defrag() - Execute the defrag program.
1337 * @fd: target file descriptor.
1338 * @donor_fd: donor file descriptor.
1339 * @file: target file name.
1340 * @buf: pointer of the struct stat64.
1341 * @ext_list_head: head of the extent list.
1343 static int call_defrag(int fd
, int donor_fd
, const char *file
,
1344 const struct stat64
*buf
, struct fiemap_extent_list
*ext_list_head
)
1347 unsigned int page_num
;
1348 unsigned char *vec
= NULL
;
1349 int defraged_ret
= 0;
1351 struct move_extent move_data
;
1352 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1354 memset(&move_data
, 0, sizeof(struct move_extent
));
1355 move_data
.donor_fd
= donor_fd
;
1357 /* Print defrag progress */
1358 print_progress(file
, start
, buf
->st_size
);
1360 ext_list_tmp
= ext_list_head
;
1362 move_data
.orig_start
= ext_list_tmp
->data
.logical
;
1363 /* Logical offset of orig and donor should be same */
1364 move_data
.donor_start
= move_data
.orig_start
;
1365 move_data
.len
= ext_list_tmp
->data
.len
;
1366 move_data
.moved_len
= 0;
1368 ret
= page_in_core(fd
, move_data
, &vec
, &page_num
);
1370 if (mode_flag
& DETAIL
) {
1372 PRINT_ERR_MSG_WITH_ERRNO(
1373 "Failed to get file map");
1375 printf("\t[ NG ]\n");
1380 /* EXT4_IOC_MOVE_EXT */
1382 ioctl(fd
, EXT4_IOC_MOVE_EXT
, &move_data
);
1385 ret
= defrag_fadvise(fd
, move_data
, vec
, page_num
);
1391 if (mode_flag
& DETAIL
) {
1393 PRINT_ERR_MSG_WITH_ERRNO(
1394 "Failed to free page");
1396 printf("\t[ NG ]\n");
1401 if (defraged_ret
< 0) {
1402 if (mode_flag
& DETAIL
) {
1404 PRINT_ERR_MSG_WITH_ERRNO(
1405 "Failed to defrag with "
1406 "EXT4_IOC_MOVE_EXT ioctl");
1407 if (errno
== ENOTTY
)
1408 printf("\tAt least 2.6.31-rc1 of "
1409 "vanilla kernel is required\n");
1411 printf("\t[ NG ]\n");
1415 /* Adjust logical offset for next ioctl */
1416 move_data
.orig_start
+= move_data
.moved_len
;
1417 move_data
.donor_start
= move_data
.orig_start
;
1419 start
= move_data
.orig_start
* buf
->st_blksize
;
1421 /* Print defrag progress */
1422 print_progress(file
, start
, buf
->st_size
);
1425 if (start
>= buf
->st_size
)
1428 ext_list_tmp
= ext_list_tmp
->next
;
1429 } while (ext_list_tmp
!= ext_list_head
);
1435 * file_defrag() - Check file attributes and call ioctl to defrag.
1437 * @file: the file's name.
1438 * @buf: the pointer of the struct stat64.
1440 * @ftwbuf: the pointer of a struct FTW.
1442 static int file_defrag(const char *file
, const struct stat64
*buf
,
1443 int flag
EXT2FS_ATTR((unused
)),
1444 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1450 int file_frags_start
, file_frags_end
;
1451 int orig_physical_cnt
, donor_physical_cnt
= 0;
1452 char tmp_inode_name
[PATH_MAX
+ 8];
1453 ext4_fsblk_t blk_count
= 0;
1454 struct fiemap_extent_list
*orig_list_physical
= NULL
;
1455 struct fiemap_extent_list
*orig_list_logical
= NULL
;
1456 struct fiemap_extent_list
*donor_list_physical
= NULL
;
1457 struct fiemap_extent_list
*donor_list_logical
= NULL
;
1458 struct fiemap_extent_group
*orig_group_head
= NULL
;
1459 struct fiemap_extent_group
*orig_group_tmp
= NULL
;
1461 defraged_file_count
++;
1463 if (mode_flag
& DETAIL
) {
1464 printf("[%u/%u]", defraged_file_count
, total_count
);
1468 if (lost_found_dir
[0] != '\0' &&
1469 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1470 if (mode_flag
& DETAIL
) {
1471 PRINT_FILE_NAME(file
);
1472 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1477 if (!S_ISREG(buf
->st_mode
)) {
1478 if (mode_flag
& DETAIL
) {
1479 PRINT_FILE_NAME(file
);
1480 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1486 if (buf
->st_size
== 0) {
1487 if (mode_flag
& DETAIL
) {
1488 PRINT_FILE_NAME(file
);
1489 IN_FTW_PRINT_ERR_MSG("File size is 0");
1495 if (buf
->st_blocks
== 0) {
1496 if (mode_flag
& DETAIL
) {
1497 PRINT_FILE_NAME(file
);
1498 STATISTIC_ERR_MSG("File has no blocks");
1503 fd
= open64(file
, O_RDWR
);
1505 if (mode_flag
& DETAIL
) {
1506 PRINT_FILE_NAME(file
);
1507 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1512 /* Get file's extents */
1513 ret
= get_file_extents(fd
, &orig_list_physical
);
1515 if (mode_flag
& DETAIL
) {
1516 PRINT_FILE_NAME(file
);
1517 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1522 /* Get the count of file's continuous physical region */
1523 orig_physical_cnt
= get_physical_count(orig_list_physical
);
1525 /* Change list from physical to logical */
1526 ret
= change_physical_to_logical(&orig_list_physical
,
1527 &orig_list_logical
);
1529 if (mode_flag
& DETAIL
) {
1530 PRINT_FILE_NAME(file
);
1531 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1536 /* Count file fragments before defrag */
1537 file_frags_start
= get_logical_count(orig_list_logical
);
1539 blk_count
= get_file_blocks(orig_list_logical
);
1540 if (file_check(fd
, buf
, file
, file_frags_start
, blk_count
) < 0)
1543 if (fsync(fd
) < 0) {
1544 if (mode_flag
& DETAIL
) {
1545 PRINT_FILE_NAME(file
);
1546 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1551 if (current_uid
== ROOT_UID
)
1552 best
= get_best_count(blk_count
);
1556 if (file_frags_start
<= best
)
1557 goto check_improvement
;
1559 /* Combine extents to group */
1560 ret
= join_extents(orig_list_logical
, &orig_group_head
);
1562 if (mode_flag
& DETAIL
) {
1563 PRINT_FILE_NAME(file
);
1564 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1569 /* Create donor inode */
1570 memset(tmp_inode_name
, 0, PATH_MAX
+ 8);
1571 sprintf(tmp_inode_name
, "%.*s.defrag",
1572 (int)strnlen(file
, PATH_MAX
), file
);
1573 donor_fd
= open64(tmp_inode_name
, O_WRONLY
| O_CREAT
| O_EXCL
, S_IRUSR
);
1575 if (mode_flag
& DETAIL
) {
1576 PRINT_FILE_NAME(file
);
1577 if (errno
== EEXIST
)
1578 PRINT_ERR_MSG_WITH_ERRNO(
1579 "File is being defraged by other program");
1581 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1586 /* Unlink donor inode */
1587 ret
= unlink(tmp_inode_name
);
1589 if (mode_flag
& DETAIL
) {
1590 PRINT_FILE_NAME(file
);
1591 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1596 /* Allocate space for donor inode */
1597 orig_group_tmp
= orig_group_head
;
1599 ret
= fallocate64(donor_fd
, 0,
1600 (loff_t
)orig_group_tmp
->start
->data
.logical
* block_size
,
1601 (loff_t
)orig_group_tmp
->len
* block_size
);
1603 if (mode_flag
& DETAIL
) {
1604 PRINT_FILE_NAME(file
);
1605 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1610 orig_group_tmp
= orig_group_tmp
->next
;
1611 } while (orig_group_tmp
!= orig_group_head
);
1613 /* Get donor inode's extents */
1614 ret
= get_file_extents(donor_fd
, &donor_list_physical
);
1616 if (mode_flag
& DETAIL
) {
1617 PRINT_FILE_NAME(file
);
1618 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1623 /* Calcuate donor inode's continuous physical region */
1624 donor_physical_cnt
= get_physical_count(donor_list_physical
);
1626 /* Change donor extent list from physical to logical */
1627 ret
= change_physical_to_logical(&donor_list_physical
,
1628 &donor_list_logical
);
1630 if (mode_flag
& DETAIL
) {
1631 PRINT_FILE_NAME(file
);
1632 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1638 if (mode_flag
& DETAIL
) {
1639 if (file_frags_start
!= 1)
1640 frag_files_before_defrag
++;
1642 extents_before_defrag
+= file_frags_start
;
1645 if (file_frags_start
<= best
||
1646 orig_physical_cnt
<= donor_physical_cnt
) {
1647 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1648 defraged_file_count
, total_count
, file
, 100);
1649 if (mode_flag
& DETAIL
)
1650 printf(" extents: %d -> %d",
1651 file_frags_start
, file_frags_start
);
1653 printf("\t[ OK ]\n");
1656 if (file_frags_start
!= 1)
1657 frag_files_after_defrag
++;
1659 extents_after_defrag
+= file_frags_start
;
1663 /* Defrag the file */
1664 ret
= call_defrag(fd
, donor_fd
, file
, buf
, donor_list_logical
);
1666 /* Count file fragments after defrag and print extents info */
1667 if (mode_flag
& DETAIL
) {
1668 file_frags_end
= file_frag_count(fd
);
1669 if (file_frags_end
< 0) {
1671 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO
);
1675 if (file_frags_end
!= 1)
1676 frag_files_after_defrag
++;
1678 extents_after_defrag
+= file_frags_end
;
1683 printf(" extents: %d -> %d",
1684 file_frags_start
, file_frags_end
);
1691 printf("\t[ OK ]\n");
1699 free_ext(orig_list_physical
);
1700 free_ext(orig_list_logical
);
1701 free_ext(donor_list_physical
);
1702 free_exts_group(orig_group_head
);
1707 * main() - Ext4 online defrag.
1709 * @argc: the number of parameter.
1710 * @argv[]: the pointer array of parameter.
1712 int main(int argc
, char *argv
[])
1716 int flags
= FTW_PHYS
| FTW_MOUNT
;
1718 int success_flag
= 0;
1719 char dir_name
[PATH_MAX
+ 1];
1720 char dev_name
[PATH_MAX
+ 1];
1722 ext2_filsys fs
= NULL
;
1724 /* Parse arguments */
1728 while ((opt
= getopt(argc
, argv
, "vc")) != EOF
) {
1731 mode_flag
|= DETAIL
;
1734 mode_flag
|= STATISTIC
;
1744 current_uid
= getuid();
1747 for (i
= optind
; i
< argc
; i
++) {
1751 frag_files_before_defrag
= 0;
1752 frag_files_after_defrag
= 0;
1753 extents_before_defrag
= 0;
1754 extents_after_defrag
= 0;
1755 defraged_file_count
= 0;
1756 files_block_count
= 0;
1757 blocks_per_group
= 0;
1758 feature_incompat
= 0;
1759 log_groups_per_flex
= 0;
1761 memset(dir_name
, 0, PATH_MAX
+ 1);
1762 memset(dev_name
, 0, PATH_MAX
+ 1);
1763 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1764 memset(frag_rank
, 0,
1765 sizeof(struct frag_statistic_ino
) * SHOW_FRAG_FILES
);
1767 if ((mode_flag
& STATISTIC
) && i
> optind
)
1770 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1771 PRINT_ERR_MSG("Endian's type is not big/little endian");
1772 PRINT_FILE_NAME(argv
[i
]);
1776 if (lstat64(argv
[i
], &buf
) < 0) {
1777 perror(NGMSG_FILE_INFO
);
1778 PRINT_FILE_NAME(argv
[i
]);
1782 /* Handle i.e. lvm device symlinks */
1783 if (S_ISLNK(buf
.st_mode
)) {
1786 if (stat64(argv
[i
], &buf2
) == 0 &&
1787 S_ISBLK(buf2
.st_mode
))
1791 if (S_ISBLK(buf
.st_mode
)) {
1793 strncpy(dev_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1794 if (get_mount_point(argv
[i
], dir_name
, PATH_MAX
) < 0)
1796 if (lstat64(dir_name
, &buf
) < 0) {
1797 perror(NGMSG_FILE_INFO
);
1798 PRINT_FILE_NAME(argv
[i
]);
1802 if (!(mode_flag
& STATISTIC
))
1803 printf("ext4 defragmentation for device(%s)\n",
1805 } else if (S_ISDIR(buf
.st_mode
)) {
1807 if (access(argv
[i
], R_OK
) < 0) {
1812 strncpy(dir_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1813 } else if (S_ISREG(buf
.st_mode
)) {
1815 arg_type
= FILENAME
;
1817 /* Irregular file */
1818 PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1819 PRINT_FILE_NAME(argv
[i
]);
1824 block_size
= buf
.st_blksize
;
1827 * filesystem type checked in get_mount_point()
1829 if (arg_type
== FILENAME
|| arg_type
== DIRNAME
) {
1830 if (is_ext4(argv
[i
], dev_name
) < 0)
1832 if (realpath(argv
[i
], dir_name
) == NULL
) {
1833 perror("Couldn't get full path");
1834 PRINT_FILE_NAME(argv
[i
]);
1839 if (current_uid
== ROOT_UID
) {
1840 /* Get super block info */
1841 ret
= ext2fs_open(dev_name
, 0, 0, block_size
,
1842 unix_io_manager
, &fs
);
1844 if (mode_flag
& DETAIL
) {
1845 perror("Can't get super block info");
1846 PRINT_FILE_NAME(argv
[i
]);
1851 blocks_per_group
= fs
->super
->s_blocks_per_group
;
1852 feature_incompat
= fs
->super
->s_feature_incompat
;
1853 log_groups_per_flex
= fs
->super
->s_log_groups_per_flex
;
1859 int mount_dir_len
= 0;
1862 if (!(mode_flag
& STATISTIC
))
1863 printf("ext4 defragmentation "
1864 "for directory(%s)\n", argv
[i
]);
1866 mount_dir_len
= strnlen(lost_found_dir
, PATH_MAX
);
1868 strncat(lost_found_dir
, "/lost+found",
1869 PATH_MAX
- strnlen(lost_found_dir
, PATH_MAX
));
1871 /* Not the case("e4defrag mount_piont_dir") */
1872 if (dir_name
[mount_dir_len
] != '\0') {
1874 * "e4defrag mount_piont_dir/lost+found"
1875 * or "e4defrag mount_piont_dir/lost+found/"
1877 if (strncmp(lost_found_dir
, dir_name
,
1878 strnlen(lost_found_dir
,
1880 (dir_name
[strnlen(lost_found_dir
,
1881 PATH_MAX
)] == '\0' ||
1882 dir_name
[strnlen(lost_found_dir
,
1883 PATH_MAX
)] == '/')) {
1884 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1885 PRINT_FILE_NAME(argv
[i
]);
1889 /* "e4defrag mount_piont_dir/else_dir" */
1890 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1893 if (arg_type
== DEVNAME
) {
1894 strncpy(lost_found_dir
, dir_name
,
1895 strnlen(dir_name
, PATH_MAX
));
1896 strncat(lost_found_dir
, "/lost+found/",
1897 PATH_MAX
- strnlen(lost_found_dir
,
1901 nftw64(dir_name
, calc_entry_counts
, FTW_OPEN_FD
, flags
);
1903 if (mode_flag
& STATISTIC
) {
1904 if (mode_flag
& DETAIL
)
1905 printf("%-40s%10s/%-10s%9s\n",
1906 "<File>", "now", "best", "size/ext");
1908 if (!(mode_flag
& DETAIL
) &&
1909 current_uid
!= ROOT_UID
) {
1915 nftw64(dir_name
, file_statistic
,
1916 FTW_OPEN_FD
, flags
);
1918 if (succeed_cnt
!= 0 &&
1919 current_uid
== ROOT_UID
) {
1920 if (mode_flag
& DETAIL
)
1922 printf("%-40s%10s/%-10s%9s\n",
1923 "<Fragmented files>", "now",
1924 "best", "size/ext");
1925 for (j
= 0; j
< SHOW_FRAG_FILES
; j
++) {
1926 if (strlen(frag_rank
[j
].
1928 printf("%d. %s\n%50d/"
1931 frag_rank
[j
].msg_buffer
,
1932 frag_rank
[j
].now_count
,
1933 frag_rank
[j
].best_count
,
1936 } else if (strlen(frag_rank
[j
].
1938 printf("%d. %-37s%10d/"
1941 frag_rank
[j
].msg_buffer
,
1942 frag_rank
[j
].now_count
,
1943 frag_rank
[j
].best_count
,
1952 /* File tree walk */
1953 nftw64(dir_name
, file_defrag
, FTW_OPEN_FD
, flags
);
1954 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt
,
1956 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1957 total_count
- succeed_cnt
, total_count
);
1958 if (mode_flag
& DETAIL
) {
1959 printf("\tTotal extents:\t\t\t%4d->%d\n",
1960 extents_before_defrag
,
1961 extents_after_defrag
);
1962 printf("\tFragmented percentage:\t\t"
1963 "%3llu%%->%llu%%\n",
1964 !regular_count
? 0 :
1965 ((unsigned long long)
1966 frag_files_before_defrag
* 100) /
1968 !regular_count
? 0 :
1969 ((unsigned long long)
1970 frag_files_after_defrag
* 100) /
1977 strncat(lost_found_dir
, "/lost+found/",
1978 PATH_MAX
- strnlen(lost_found_dir
,
1980 if (strncmp(lost_found_dir
, dir_name
,
1981 strnlen(lost_found_dir
,
1983 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1984 PRINT_FILE_NAME(argv
[i
]);
1988 if (mode_flag
& STATISTIC
) {
1989 file_statistic(argv
[i
], &buf
, FTW_F
, NULL
);
1992 printf("ext4 defragmentation for %s\n",
1994 /* Defrag single file process */
1995 file_defrag(argv
[i
], &buf
, FTW_F
, NULL
);
1996 if (succeed_cnt
!= 0)
1997 printf(" Success:\t\t\t[1/1]\n");
1999 printf(" Success:\t\t\t[0/1]\n");
2004 if (succeed_cnt
!= 0)
2006 if (mode_flag
& STATISTIC
) {
2007 if (current_uid
!= ROOT_UID
) {
2013 if (mode_flag
& DETAIL
)
2016 if (arg_type
== DEVNAME
)
2017 printf(" In this device(%s), "
2018 "none can be defragmented.\n", argv
[i
]);
2019 else if (arg_type
== DIRNAME
)
2020 printf(" In this directory(%s), "
2021 "none can be defragmented.\n", argv
[i
]);
2023 printf(" This file(%s) "
2024 "can't be defragmented.\n", argv
[i
]);
2026 float files_ratio
= 0.0;
2028 __u64 size_per_ext
= files_block_count
*
2029 (buf
.st_blksize
/ 1024) /
2030 extents_before_defrag
;
2031 files_ratio
= (float)(extents_before_defrag
-
2032 extents_after_defrag
) *
2033 100 / files_block_count
;
2034 score
= CALC_SCORE(files_ratio
);
2035 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2036 " Average size per extent"
2038 " Fragmentation score\t\t\t\t%.0f\n",
2039 extents_before_defrag
,
2040 extents_after_defrag
,
2041 size_per_ext
, score
);
2042 printf(" [0-30 no problem:"
2043 " 31-55 a little bit fragmented:"
2044 " 56- needs defrag]\n");
2046 if (arg_type
== DEVNAME
)
2047 printf(" This device (%s) ", argv
[i
]);
2048 else if (arg_type
== DIRNAME
)
2049 printf(" This directory (%s) ",
2052 printf(" This file (%s) ", argv
[i
]);
2054 if (score
> BOUND_SCORE
)
2055 printf("needs defragmentation.\n");
2057 printf("does not need "
2058 "defragmentation.\n");