2 * e4defrag.c - ext4 filesystem defragmenter
4 * Copyright (C) 2009 NEC Software Tohoku, Ltd.
6 * Author: Akira Fujita <a-fujita@rs.jp.nec.com>
7 * Takashi Sato <t-sato@yk.jp.nec.com>
10 #ifndef _LARGEFILE_SOURCE
11 #define _LARGEFILE_SOURCE
14 #ifndef _LARGEFILE64_SOURCE
15 #define _LARGEFILE64_SOURCE
34 #include <ext2fs/ext2_types.h>
35 #include <ext2fs/ext2fs.h>
37 #include <sys/ioctl.h>
38 #include <ext2fs/fiemap.h>
41 #include <sys/statfs.h>
42 #include <sys/syscall.h>
45 /* A relatively new ioctl interface ... */
46 #ifndef EXT4_IOC_MOVE_EXT
47 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
51 #define PRINT_ERR_MSG(msg) fprintf(stderr, "%s\n", (msg))
52 #define IN_FTW_PRINT_ERR_MSG(msg) \
53 fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
54 #define PRINT_FILE_NAME(file) fprintf(stderr, " \"%s\"\n", (file))
55 #define PRINT_ERR_MSG_WITH_ERRNO(msg) \
56 fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
57 #define STATISTIC_ERR_MSG(msg) \
58 fprintf(stderr, "\t%s\n", (msg))
59 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg) \
60 fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
61 #define min(x, y) (((x) > (y)) ? (y) : (x))
62 #define CALC_SCORE(ratio) \
63 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
64 /* Wrap up the free function */
70 /* Insert list2 after list1 */
71 #define insert(list1, list2) \
73 list2->next = list1->next; \
74 list1->next->prev = list2; \
75 list2->prev = list1; \
76 list1->next = list2; \
79 /* To delete unused warning */
81 #define EXT2FS_ATTR(x) __attribute__(x)
83 #define EXT2FS_ATTR(x)
86 /* The mode of defrag */
88 #define STATISTIC 0x02
94 #define FTW_OPEN_FD 2000
96 #define FS_EXT4 "ext4"
99 #define BOUND_SCORE 55
100 #define SHOW_FRAG_FILES 5
102 /* Magic number for ext4 */
103 #define EXT4_SUPER_MAGIC 0xEF53
105 /* Definition of flex_bg */
106 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
108 /* The following macro is used for ioctl FS_IOC_FIEMAP
109 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
110 * kernel-space and user-space per ioctl
112 #define EXTENT_MAX_COUNT 512
114 /* The following macros are error message */
116 "Usage : e4defrag [-v] file...| directory...| device...\n\
117 : e4defrag -c file...| directory...| device...\n"
119 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
120 #define NGMSG_FILE_EXTENT "Failed to get file extents"
121 #define NGMSG_FILE_INFO "Failed to get file information"
122 #define NGMSG_FILE_OPEN "Failed to open"
123 #define NGMSG_FILE_UNREG "File is not regular file"
124 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
126 /* Data type for filesystem-wide blocks number */
127 typedef unsigned long long ext4_fsblk_t
;
129 struct fiemap_extent_data
{
130 __u64 len
; /* blocks count */
131 __u64 logical
; /* start logical block number */
132 ext4_fsblk_t physical
; /* start physical block number */
135 struct fiemap_extent_list
{
136 struct fiemap_extent_list
*prev
;
137 struct fiemap_extent_list
*next
;
138 struct fiemap_extent_data data
; /* extent belong to file */
141 struct fiemap_extent_group
{
142 struct fiemap_extent_group
*prev
;
143 struct fiemap_extent_group
*next
;
144 __u64 len
; /* length of this continuous region */
145 struct fiemap_extent_list
*start
; /* start ext */
146 struct fiemap_extent_list
*end
; /* end ext */
150 __s32 reserved
; /* original file descriptor */
151 __u32 donor_fd
; /* donor file descriptor */
152 __u64 orig_start
; /* logical start offset in block for orig */
153 __u64 donor_start
; /* logical start offset in block for donor */
154 __u64 len
; /* block length to be moved */
155 __u64 moved_len
; /* moved block length */
158 struct frag_statistic_ino
{
159 int now_count
; /* the file's extents count of before defrag */
160 int best_count
; /* the best file's extents count */
161 __u64 size_per_ext
; /* size(KB) per extent */
162 float ratio
; /* the ratio of fragmentation */
163 char msg_buffer
[PATH_MAX
+ 1]; /* pathname of the file */
166 typedef __u16 __le16
;
167 typedef __u32 __le32
;
168 typedef __u64 __le64
;
171 * Structure of the super block
173 struct ext4_super_block
{
174 /*00*/ __le32 s_inodes_count
; /* Inodes count */
175 __le32 s_blocks_count_lo
; /* Blocks count */
176 __le32 s_r_blocks_count_lo
; /* Reserved blocks count */
177 __le32 s_free_blocks_count_lo
; /* Free blocks count */
178 /*10*/ __le32 s_free_inodes_count
; /* Free inodes count */
179 __le32 s_first_data_block
; /* First Data Block */
180 __le32 s_log_block_size
; /* Block size */
181 __le32 s_obso_log_frag_size
; /* Obsoleted fragment size */
182 /*20*/ __le32 s_blocks_per_group
; /* # Blocks per group */
183 __le32 s_obso_frags_per_group
; /* Obsoleted fragments per group */
184 __le32 s_inodes_per_group
; /* # Inodes per group */
185 __le32 s_mtime
; /* Mount time */
186 /*30*/ __le32 s_wtime
; /* Write time */
187 __le16 s_mnt_count
; /* Mount count */
188 __le16 s_max_mnt_count
; /* Maximal mount count */
189 __le16 s_magic
; /* Magic signature */
190 __le16 s_state
; /* File system state */
191 __le16 s_errors
; /* Behaviour when detecting errors */
192 __le16 s_minor_rev_level
; /* minor revision level */
193 /*40*/ __le32 s_lastcheck
; /* time of last check */
194 __le32 s_checkinterval
; /* max. time between checks */
195 __le32 s_creator_os
; /* OS */
196 __le32 s_rev_level
; /* Revision level */
197 /*50*/ __le16 s_def_resuid
; /* Default uid for reserved blocks */
198 __le16 s_def_resgid
; /* Default gid for reserved blocks */
200 * These fields are for EXT4_DYNAMIC_REV superblocks only.
202 * Note: the difference between the compatible feature set and
203 * the incompatible feature set is that if there is a bit set
204 * in the incompatible feature set that the kernel doesn't
205 * know about, it should refuse to mount the filesystem.
207 * e2fsck's requirements are more strict; if it doesn't know
208 * about a feature in either the compatible or incompatible
209 * feature set, it must abort and not try to meddle with
210 * things it doesn't understand...
212 __le32 s_first_ino
; /* First non-reserved inode */
213 __le16 s_inode_size
; /* size of inode structure */
214 __le16 s_block_group_nr
; /* block group # of this superblock */
215 __le32 s_feature_compat
; /* compatible feature set */
216 /*60*/ __le32 s_feature_incompat
; /* incompatible feature set */
217 __le32 s_feature_ro_compat
; /* readonly-compatible feature set */
218 /*68*/ __u8 s_uuid
[16]; /* 128-bit uuid for volume */
219 /*78*/ char s_volume_name
[16]; /* volume name */
220 /*88*/ char s_last_mounted
[64]; /* directory where last mounted */
221 /*C8*/ __le32 s_algorithm_usage_bitmap
; /* For compression */
223 * Performance hints. Directory preallocation should only
224 * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
226 __u8 s_prealloc_blocks
; /* Nr of blocks to try to preallocate*/
227 __u8 s_prealloc_dir_blocks
; /* Nr to preallocate for dirs */
228 __le16 s_reserved_gdt_blocks
; /* Per group desc for online growth */
230 * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
232 /*D0*/ __u8 s_journal_uuid
[16]; /* uuid of journal superblock */
233 /*E0*/ __le32 s_journal_inum
; /* inode number of journal file */
234 __le32 s_journal_dev
; /* device number of journal file */
235 __le32 s_last_orphan
; /* start of list of inodes to delete */
236 __le32 s_hash_seed
[4]; /* HTREE hash seed */
237 __u8 s_def_hash_version
; /* Default hash version to use */
238 __u8 s_reserved_char_pad
;
239 __le16 s_desc_size
; /* size of group descriptor */
240 /*100*/ __le32 s_default_mount_opts
;
241 __le32 s_first_meta_bg
; /* First metablock block group */
242 __le32 s_mkfs_time
; /* When the filesystem was created */
243 __le32 s_jnl_blocks
[17]; /* Backup of the journal inode */
244 /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
245 /*150*/ __le32 s_blocks_count_hi
; /* Blocks count */
246 __le32 s_r_blocks_count_hi
; /* Reserved blocks count */
247 __le32 s_free_blocks_count_hi
; /* Free blocks count */
248 __le16 s_min_extra_isize
; /* All inodes have at least # bytes */
249 __le16 s_want_extra_isize
; /* New inodes should reserve # bytes */
250 __le32 s_flags
; /* Miscellaneous flags */
251 __le16 s_raid_stride
; /* RAID stride */
252 __le16 s_mmp_interval
; /* # seconds to wait in MMP checking */
253 __le64 s_mmp_block
; /* Block for multi-mount protection */
254 __le32 s_raid_stripe_width
; /* blocks on all data disks (N*stride)*/
255 __u8 s_log_groups_per_flex
; /* FLEX_BG group size */
256 __u8 s_reserved_char_pad2
;
257 __le16 s_reserved_pad
;
258 __u32 s_reserved
[162]; /* Padding to the end of the block */
261 char lost_found_dir
[PATH_MAX
+ 1];
263 int extents_before_defrag
;
264 int extents_after_defrag
;
266 unsigned int current_uid
;
267 unsigned int defraged_file_count
;
268 unsigned int frag_files_before_defrag
;
269 unsigned int frag_files_after_defrag
;
270 unsigned int regular_count
;
271 unsigned int succeed_cnt
;
272 unsigned int total_count
;
273 __u8 log_groups_per_flex
;
274 __le32 blocks_per_group
;
275 __le32 feature_incompat
;
276 ext4_fsblk_t files_block_count
;
277 struct frag_statistic_ino frag_rank
[SHOW_FRAG_FILES
];
280 /* Local definitions of some syscalls glibc may not yet have */
282 #ifndef HAVE_POSIX_FADVISE
283 #warning Using locally defined posix_fadvise interface.
285 #ifndef __NR_fadvise64_64
286 #error Your kernel headers dont define __NR_fadvise64_64
290 * fadvise() - Give advice about file access.
292 * @fd: defrag target file's descriptor.
293 * @offset: file offset.
295 * @advise: process flag.
297 static int posix_fadvise(int fd
, loff_t offset
, size_t len
, int advise
)
299 return syscall(__NR_fadvise64_64
, fd
, offset
, len
, advise
);
301 #endif /* ! HAVE_FADVISE64_64 */
303 #ifndef HAVE_SYNC_FILE_RANGE
304 #warning Using locally defined sync_file_range interface.
306 #ifndef __NR_sync_file_range
307 #ifndef __NR_sync_file_range2 /* ppc */
308 #error Your kernel headers dont define __NR_sync_file_range
313 * sync_file_range() - Sync file region.
315 * @fd: defrag target file's descriptor.
316 * @offset: file offset.
317 * @length: area length.
318 * @flag: process flag.
320 int sync_file_range(int fd
, loff_t offset
, loff_t length
, unsigned int flag
)
322 #ifdef __NR_sync_file_range
323 return syscall(__NR_sync_file_range
, fd
, offset
, length
, flag
);
325 return syscall(__NR_sync_file_range2
, fd
, flag
, offset
, length
);
328 #endif /* ! HAVE_SYNC_FILE_RANGE */
330 #ifndef HAVE_FALLOCATE64
331 #warning Using locally defined fallocate syscall interface.
333 #ifndef __NR_fallocate
334 #error Your kernel headers dont define __NR_fallocate
338 * fallocate64() - Manipulate file space.
340 * @fd: defrag target file's descriptor.
341 * @mode: process flag.
342 * @offset: file offset.
345 static int fallocate64(int fd
, int mode
, loff_t offset
, loff_t len
)
347 return syscall(__NR_fallocate
, fd
, mode
, offset
, len
);
349 #endif /* ! HAVE_FALLOCATE */
352 * get_mount_point() - Get device's mount point.
354 * @devname: the device's name.
355 * @mount_point: the mount point.
356 * @dir_path_len: the length of directory.
358 static int get_mount_point(const char *devname
, char *mount_point
,
361 /* Refer to /etc/mtab */
362 const char *mtab
= MOUNTED
;
364 struct mntent
*mnt
= NULL
;
366 fp
= setmntent(mtab
, "r");
368 perror("Couldn't access /etc/mtab");
372 while ((mnt
= getmntent(fp
)) != NULL
) {
373 if (strcmp(devname
, mnt
->mnt_fsname
) != 0)
377 if (strcmp(mnt
->mnt_type
, FS_EXT4
) == 0) {
378 strncpy(mount_point
, mnt
->mnt_dir
,
382 PRINT_ERR_MSG(NGMSG_EXT4
);
386 PRINT_ERR_MSG("Filesystem is not mounted");
391 * is_ext4() - Whether on an ext4 filesystem.
393 * @file: the file's name.
395 static int is_ext4(const char *file
)
400 char *mnt_type
= NULL
;
401 /* Refer to /etc/mtab */
402 const char *mtab
= MOUNTED
;
403 char file_path
[PATH_MAX
+ 1];
404 struct mntent
*mnt
= NULL
;
405 struct statfs64 fsbuf
;
408 if (realpath(file
, file_path
) == NULL
) {
409 perror("Couldn't get full path");
410 PRINT_FILE_NAME(file
);
414 if (statfs64(file_path
, &fsbuf
) < 0) {
415 perror("Failed to get filesystem information");
416 PRINT_FILE_NAME(file
);
420 if (fsbuf
.f_type
!= EXT4_SUPER_MAGIC
) {
421 PRINT_ERR_MSG(NGMSG_EXT4
);
425 fp
= setmntent(mtab
, "r");
427 perror("Couldn't access /etc/mtab");
431 while ((mnt
= getmntent(fp
)) != NULL
) {
432 if (mnt
->mnt_fsname
[0] != '/')
434 len
= strlen(mnt
->mnt_dir
);
435 ret
= memcmp(file_path
, mnt
->mnt_dir
, len
);
444 mnt_type
= realloc(mnt_type
, strlen(mnt
->mnt_type
) + 1);
445 if (mnt_type
== NULL
) {
449 memset(mnt_type
, 0, strlen(mnt
->mnt_type
) + 1);
450 strncpy(mnt_type
, mnt
->mnt_type
, strlen(mnt
->mnt_type
));
451 strncpy(lost_found_dir
, mnt
->mnt_dir
, PATH_MAX
);
455 if (strcmp(mnt_type
, FS_EXT4
) == 0) {
460 PRINT_ERR_MSG(NGMSG_EXT4
);
466 * calc_entry_counts() - Calculate file counts.
471 * @ftwbuf: the pointer of a struct FTW.
473 static int calc_entry_counts(const char *file
EXT2FS_ATTR((unused
)),
474 const struct stat64
*buf
, int flag
EXT2FS_ATTR((unused
)),
475 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
477 if (S_ISREG(buf
->st_mode
))
486 * page_in_core() - Get information on whether pages are in core.
488 * @fd: defrag target file's descriptor.
489 * @defrag_data: data used for defrag.
490 * @vec: page state array.
491 * @page_num: page number.
493 static int page_in_core(int fd
, struct move_extent defrag_data
,
494 unsigned char **vec
, unsigned int *page_num
)
496 long pagesize
= sysconf(_SC_PAGESIZE
);
498 loff_t offset
, end_offset
, length
;
500 if (vec
== NULL
|| *vec
!= NULL
)
503 /* In mmap, offset should be a multiple of the page size */
504 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
505 length
= (loff_t
)defrag_data
.len
* block_size
;
506 end_offset
= offset
+ length
;
507 /* Round the offset down to the nearest multiple of pagesize */
508 offset
= (offset
/ pagesize
) * pagesize
;
509 length
= end_offset
- offset
;
511 page
= mmap(NULL
, length
, PROT_READ
, MAP_SHARED
, fd
, offset
);
512 if (page
== MAP_FAILED
)
516 *page_num
= (length
+ pagesize
- 1) / pagesize
;
517 *vec
= (unsigned char *)calloc(*page_num
, 1);
521 /* Get information on whether pages are in core */
522 if (mincore(page
, (size_t)length
, *vec
) == -1 ||
523 munmap(page
, length
) == -1) {
532 * defrag_fadvise() - Predeclare an access pattern for file data.
534 * @fd: defrag target file's descriptor.
535 * @defrag_data: data used for defrag.
536 * @vec: page state array.
537 * @page_num: page number.
539 static int defrag_fadvise(int fd
, struct move_extent defrag_data
,
540 unsigned char *vec
, unsigned int page_num
)
543 long pagesize
= sysconf(_SC_PAGESIZE
);
544 int fadvise_flag
= POSIX_FADV_DONTNEED
;
545 int sync_flag
= SYNC_FILE_RANGE_WAIT_BEFORE
|
546 SYNC_FILE_RANGE_WRITE
|
547 SYNC_FILE_RANGE_WAIT_AFTER
;
551 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
552 offset
= (offset
/ pagesize
) * pagesize
;
554 /* Sync file for fadvise process */
555 if (sync_file_range(fd
, offset
,
556 (loff_t
)pagesize
* page_num
, sync_flag
) < 0)
559 /* Try to release buffer cache which this process used,
560 * then other process can use the released buffer
562 for (i
= 0; i
< page_num
; i
++) {
563 if ((vec
[i
] & 0x1) == 0) {
567 if (posix_fadvise(fd
, offset
, pagesize
, fadvise_flag
) < 0) {
568 if ((mode_flag
& DETAIL
) && flag
) {
569 perror("\tFailed to fadvise");
580 * check_free_size() - Check if there's enough disk space.
582 * @fd: defrag target file's descriptor.
584 * @blk_count: file blocks.
586 static int check_free_size(int fd
, const char *file
, ext4_fsblk_t blk_count
)
588 ext4_fsblk_t free_blk_count
;
589 struct statfs64 fsbuf
;
591 if (fstatfs64(fd
, &fsbuf
) < 0) {
592 if (mode_flag
& DETAIL
) {
593 PRINT_FILE_NAME(file
);
594 PRINT_ERR_MSG_WITH_ERRNO(
595 "Failed to get filesystem information");
600 /* Compute free space for root and normal user separately */
601 if (current_uid
== ROOT_UID
)
602 free_blk_count
= fsbuf
.f_bfree
;
604 free_blk_count
= fsbuf
.f_bavail
;
606 if (free_blk_count
>= blk_count
)
613 * file_frag_count() - Get file fragment count.
615 * @fd: defrag target file's descriptor.
617 static int file_frag_count(int fd
)
620 struct fiemap fiemap_buf
;
622 /* When fm_extent_count is 0,
623 * ioctl just get file fragment count.
625 memset(&fiemap_buf
, 0, sizeof(struct fiemap
));
626 fiemap_buf
.fm_start
= 0;
627 fiemap_buf
.fm_length
= FIEMAP_MAX_OFFSET
;
628 fiemap_buf
.fm_flags
|= FIEMAP_FLAG_SYNC
;
630 ret
= ioctl(fd
, FS_IOC_FIEMAP
, &fiemap_buf
);
634 return fiemap_buf
.fm_mapped_extents
;
638 * file_check() - Check file's attributes.
640 * @fd: defrag target file's descriptor.
641 * @buf: a pointer of the struct stat64.
643 * @extents: file extents.
644 * @blk_count: file blocks.
646 static int file_check(int fd
, const struct stat64
*buf
, const char *file
,
647 int extents
, ext4_fsblk_t blk_count
)
652 /* Write-lock check is more reliable */
653 lock
.l_type
= F_WRLCK
;
655 lock
.l_whence
= SEEK_SET
;
659 ret
= check_free_size(fd
, file
, blk_count
);
661 if ((mode_flag
& DETAIL
) && ret
== -ENOSPC
) {
662 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
663 " extents: %d -> %d\n", defraged_file_count
,
664 total_count
, file
, extents
, extents
);
665 IN_FTW_PRINT_ERR_MSG(
666 "Defrag size is larger than filesystem's free space");
671 /* Access authority */
672 if (current_uid
!= ROOT_UID
&&
673 buf
->st_uid
!= current_uid
) {
674 if (mode_flag
& DETAIL
) {
675 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
676 " extents: %d -> %d\n", defraged_file_count
,
677 total_count
, file
, extents
, extents
);
678 IN_FTW_PRINT_ERR_MSG(
679 "File is not current user's file"
680 " or current user is not root");
686 if (fcntl(fd
, F_GETLK
, &lock
) < 0) {
687 if (mode_flag
& DETAIL
) {
688 PRINT_FILE_NAME(file
);
689 PRINT_ERR_MSG_WITH_ERRNO(
690 "Failed to get lock information");
693 } else if (lock
.l_type
!= F_UNLCK
) {
694 if (mode_flag
& DETAIL
) {
695 PRINT_FILE_NAME(file
);
696 IN_FTW_PRINT_ERR_MSG("File has been locked");
705 * insert_extent_by_logical() - Sequentially insert extent by logical.
707 * @ext_list_head: the head of logical extent list.
708 * @ext: the extent element which will be inserted.
710 static int insert_extent_by_logical(struct fiemap_extent_list
**ext_list_head
,
711 struct fiemap_extent_list
*ext
)
713 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
719 if (*ext_list_head
== NULL
) {
720 (*ext_list_head
) = ext
;
721 (*ext_list_head
)->prev
= *ext_list_head
;
722 (*ext_list_head
)->next
= *ext_list_head
;
726 if (ext
->data
.logical
<= ext_list_tmp
->data
.logical
) {
727 /* Insert before head */
728 if (ext_list_tmp
->data
.logical
<
729 ext
->data
.logical
+ ext
->data
.len
)
733 *ext_list_head
= ext
;
735 /* Insert into the middle or last of the list */
737 if (ext
->data
.logical
< ext_list_tmp
->data
.logical
)
739 ext_list_tmp
= ext_list_tmp
->next
;
740 } while (ext_list_tmp
!= (*ext_list_head
));
741 if (ext
->data
.logical
<
742 ext_list_tmp
->prev
->data
.logical
+
743 ext_list_tmp
->prev
->data
.len
)
747 if (ext_list_tmp
!= *ext_list_head
&&
748 ext_list_tmp
->data
.logical
<
749 ext
->data
.logical
+ ext
->data
.len
)
753 ext_list_tmp
= ext_list_tmp
->prev
;
754 /* Insert "ext" after "ext_list_tmp" */
755 insert(ext_list_tmp
, ext
);
763 * insert_extent_by_physical() - Sequentially insert extent by physical.
765 * @ext_list_head: the head of physical extent list.
766 * @ext: the extent element which will be inserted.
768 static int insert_extent_by_physical(struct fiemap_extent_list
**ext_list_head
,
769 struct fiemap_extent_list
*ext
)
771 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
777 if (*ext_list_head
== NULL
) {
778 (*ext_list_head
) = ext
;
779 (*ext_list_head
)->prev
= *ext_list_head
;
780 (*ext_list_head
)->next
= *ext_list_head
;
784 if (ext
->data
.physical
<= ext_list_tmp
->data
.physical
) {
785 /* Insert before head */
786 if (ext_list_tmp
->data
.physical
<
787 ext
->data
.physical
+ ext
->data
.len
)
791 *ext_list_head
= ext
;
793 /* Insert into the middle or last of the list */
795 if (ext
->data
.physical
< ext_list_tmp
->data
.physical
)
797 ext_list_tmp
= ext_list_tmp
->next
;
798 } while (ext_list_tmp
!= (*ext_list_head
));
799 if (ext
->data
.physical
<
800 ext_list_tmp
->prev
->data
.physical
+
801 ext_list_tmp
->prev
->data
.len
)
805 if (ext_list_tmp
!= *ext_list_head
&&
806 ext_list_tmp
->data
.physical
<
807 ext
->data
.physical
+ ext
->data
.len
)
811 ext_list_tmp
= ext_list_tmp
->prev
;
812 /* Insert "ext" after "ext_list_tmp" */
813 insert(ext_list_tmp
, ext
);
821 * insert_exts_group() - Insert a exts_group.
823 * @ext_group_head: the head of a exts_group list.
824 * @exts_group: the exts_group element which will be inserted.
826 static int insert_exts_group(struct fiemap_extent_group
**ext_group_head
,
827 struct fiemap_extent_group
*exts_group
)
829 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
831 if (exts_group
== NULL
) {
836 /* Initialize list */
837 if (*ext_group_head
== NULL
) {
838 (*ext_group_head
) = exts_group
;
839 (*ext_group_head
)->prev
= *ext_group_head
;
840 (*ext_group_head
)->next
= *ext_group_head
;
844 ext_group_tmp
= (*ext_group_head
)->prev
;
845 insert(ext_group_tmp
, exts_group
);
851 * join_extents() - Find continuous region(exts_group).
853 * @ext_list_head: the head of the extent list.
854 * @ext_group_head: the head of the target exts_group list.
856 static int join_extents(struct fiemap_extent_list
*ext_list_head
,
857 struct fiemap_extent_group
**ext_group_head
)
859 __u64 len
= ext_list_head
->data
.len
;
860 struct fiemap_extent_list
*ext_list_start
= ext_list_head
;
861 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
->next
;
864 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
866 /* This extent and previous extent are not continuous,
867 * so, all previous extents are treated as an extent group.
869 if ((ext_list_tmp
->prev
->data
.logical
+
870 ext_list_tmp
->prev
->data
.len
)
871 != ext_list_tmp
->data
.logical
) {
873 malloc(sizeof(struct fiemap_extent_group
));
874 if (ext_group_tmp
== NULL
)
877 memset(ext_group_tmp
, 0,
878 sizeof(struct fiemap_extent_group
));
879 ext_group_tmp
->len
= len
;
880 ext_group_tmp
->start
= ext_list_start
;
881 ext_group_tmp
->end
= ext_list_tmp
->prev
;
883 if (insert_exts_group(ext_group_head
,
884 ext_group_tmp
) < 0) {
888 ext_list_start
= ext_list_tmp
;
889 len
= ext_list_tmp
->data
.len
;
890 ext_list_tmp
= ext_list_tmp
->next
;
894 /* This extent and previous extent are continuous,
895 * so, they belong to the same extent group, and we check
896 * if the next extent belongs to the same extent group.
898 len
+= ext_list_tmp
->data
.len
;
899 ext_list_tmp
= ext_list_tmp
->next
;
900 } while (ext_list_tmp
!= ext_list_head
->next
);
906 * get_file_extents() - Get file's extent list.
908 * @fd: defrag target file's descriptor.
909 * @ext_list_head: the head of the extent list.
911 static int get_file_extents(int fd
, struct fiemap_extent_list
**ext_list_head
)
915 int ext_buf_size
, fie_buf_size
;
917 struct fiemap
*fiemap_buf
= NULL
;
918 struct fiemap_extent
*ext_buf
= NULL
;
919 struct fiemap_extent_list
*ext_list
= NULL
;
921 /* Convert units, in bytes.
922 * Be careful : now, physical block number in extent is 48bit,
923 * and the maximum blocksize for ext4 is 4K(12bit),
924 * so there is no overflow, but in future it may be changed.
927 /* Alloc space for fiemap */
928 ext_buf_size
= EXTENT_MAX_COUNT
* sizeof(struct fiemap_extent
);
929 fie_buf_size
= sizeof(struct fiemap
) + ext_buf_size
;
931 fiemap_buf
= malloc(fie_buf_size
);
932 if (fiemap_buf
== NULL
)
935 ext_buf
= fiemap_buf
->fm_extents
;
936 memset(fiemap_buf
, 0, fie_buf_size
);
937 fiemap_buf
->fm_length
= FIEMAP_MAX_OFFSET
;
938 fiemap_buf
->fm_flags
|= FIEMAP_FLAG_SYNC
;
939 fiemap_buf
->fm_extent_count
= EXTENT_MAX_COUNT
;
942 fiemap_buf
->fm_start
= pos
;
943 memset(ext_buf
, 0, ext_buf_size
);
944 ret
= ioctl(fd
, FS_IOC_FIEMAP
, fiemap_buf
);
945 if (ret
< 0 || fiemap_buf
->fm_mapped_extents
== 0)
947 for (i
= 0; i
< fiemap_buf
->fm_mapped_extents
; i
++) {
949 ext_list
= malloc(sizeof(struct fiemap_extent_list
));
950 if (ext_list
== NULL
)
953 ext_list
->data
.physical
= ext_buf
[i
].fe_physical
955 ext_list
->data
.logical
= ext_buf
[i
].fe_logical
957 ext_list
->data
.len
= ext_buf
[i
].fe_length
960 ret
= insert_extent_by_physical(
961 ext_list_head
, ext_list
);
967 /* Record file's logical offset this time */
968 pos
= ext_buf
[EXTENT_MAX_COUNT
-1].fe_logical
+
969 ext_buf
[EXTENT_MAX_COUNT
-1].fe_length
;
971 * If fm_extents array has been filled and
972 * there are extents left, continue to cycle.
974 } while (fiemap_buf
->fm_mapped_extents
975 == EXTENT_MAX_COUNT
&&
976 !(ext_buf
[EXTENT_MAX_COUNT
-1].fe_flags
977 & FIEMAP_EXTENT_LAST
));
987 * get_logical_count() - Get the file logical extents count.
989 * @logical_list_head: the head of the logical extent list.
991 static int get_logical_count(struct fiemap_extent_list
*logical_list_head
)
994 struct fiemap_extent_list
*ext_list_tmp
= logical_list_head
;
998 ext_list_tmp
= ext_list_tmp
->next
;
999 } while (ext_list_tmp
!= logical_list_head
);
1005 * get_physical_count() - Get the file physical extents count.
1007 * @physical_list_head: the head of the physical extent list.
1009 static int get_physical_count(struct fiemap_extent_list
*physical_list_head
)
1012 struct fiemap_extent_list
*ext_list_tmp
= physical_list_head
;
1015 if ((ext_list_tmp
->data
.physical
+ ext_list_tmp
->data
.len
)
1016 != ext_list_tmp
->next
->data
.physical
) {
1017 /* This extent and next extent are not continuous. */
1021 ext_list_tmp
= ext_list_tmp
->next
;
1022 } while (ext_list_tmp
!= physical_list_head
);
1028 * change_physical_to_logical() - Change list from physical to logical.
1030 * @physical_list_head: the head of physical extent list.
1031 * @logical_list_head: the head of logical extent list.
1033 static int change_physical_to_logical(
1034 struct fiemap_extent_list
**physical_list_head
,
1035 struct fiemap_extent_list
**logical_list_head
)
1038 struct fiemap_extent_list
*ext_list_tmp
= *physical_list_head
;
1039 struct fiemap_extent_list
*ext_list_next
= ext_list_tmp
->next
;
1042 if (ext_list_tmp
== ext_list_next
) {
1043 ret
= insert_extent_by_logical(
1044 logical_list_head
, ext_list_tmp
);
1048 *physical_list_head
= NULL
;
1052 ext_list_tmp
->prev
->next
= ext_list_tmp
->next
;
1053 ext_list_tmp
->next
->prev
= ext_list_tmp
->prev
;
1054 *physical_list_head
= ext_list_next
;
1056 ret
= insert_extent_by_logical(
1057 logical_list_head
, ext_list_tmp
);
1062 ext_list_tmp
= ext_list_next
;
1063 ext_list_next
= ext_list_next
->next
;
1069 /* get_file_blocks() - Get total file blocks.
1071 * @ext_list_head: the extent list head of the target file
1073 static ext4_fsblk_t
get_file_blocks(struct fiemap_extent_list
*ext_list_head
)
1075 ext4_fsblk_t blk_count
= 0;
1076 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
;
1079 blk_count
+= ext_list_tmp
->data
.len
;
1080 ext_list_tmp
= ext_list_tmp
->next
;
1081 } while (ext_list_tmp
!= ext_list_head
);
1087 * free_ext() - Free the extent list.
1089 * @ext_list_head: the extent list head of which will be free.
1091 static void free_ext(struct fiemap_extent_list
*ext_list_head
)
1093 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1095 if (ext_list_head
== NULL
)
1098 while (ext_list_head
->next
!= ext_list_head
) {
1099 ext_list_tmp
= ext_list_head
;
1100 ext_list_head
->prev
->next
= ext_list_head
->next
;
1101 ext_list_head
->next
->prev
= ext_list_head
->prev
;
1102 ext_list_head
= ext_list_head
->next
;
1105 free(ext_list_head
);
1109 * free_exts_group() - Free the exts_group.
1111 * @*ext_group_head: the exts_group list head which will be free.
1113 static void free_exts_group(struct fiemap_extent_group
*ext_group_head
)
1115 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
1117 if (ext_group_head
== NULL
)
1120 while (ext_group_head
->next
!= ext_group_head
) {
1121 ext_group_tmp
= ext_group_head
;
1122 ext_group_head
->prev
->next
= ext_group_head
->next
;
1123 ext_group_head
->next
->prev
= ext_group_head
->prev
;
1124 ext_group_head
= ext_group_head
->next
;
1125 free(ext_group_tmp
);
1127 free(ext_group_head
);
1131 * get_superblock_info() - Get superblock info by the file name.
1133 * @file: the file's name.
1134 * @sb: the pointer of the struct ext4_super_block.
1136 static int get_superblock_info(const char *file
, struct ext4_super_block
*sb
)
1138 /* Refer to /etc/mtab */
1139 const char *mtab
= MOUNTED
;
1146 char dev_name
[PATH_MAX
+ 1];
1147 struct mntent
*mnt
= NULL
;
1149 fp
= setmntent(mtab
, "r");
1153 while ((mnt
= getmntent(fp
)) != NULL
) {
1154 len
= strlen(mnt
->mnt_dir
);
1155 ret
= memcmp(file
, mnt
->mnt_dir
, len
);
1164 memset(dev_name
, 0, PATH_MAX
+ 1);
1165 strncpy(dev_name
, mnt
->mnt_fsname
,
1166 strnlen(mnt
->mnt_fsname
, PATH_MAX
));
1169 fd
= open64(dev_name
, O_RDONLY
);
1175 /* Set offset to read superblock */
1176 ret
= lseek64(fd
, SUPERBLOCK_OFFSET
, SEEK_SET
);
1180 ret
= read(fd
, sb
, sizeof(struct ext4_super_block
));
1192 * get_best_count() - Get the file best extents count.
1194 * @block_count: the file's physical block count.
1196 static int get_best_count(ext4_fsblk_t block_count
)
1199 unsigned int flex_bg_num
;
1201 /* Calcuate best extents count */
1202 if (feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
1203 flex_bg_num
= 1 << log_groups_per_flex
;
1204 ret
= ((block_count
- 1) /
1205 ((ext4_fsblk_t
)blocks_per_group
*
1208 ret
= ((block_count
- 1) / blocks_per_group
) + 1;
1215 * file_statistic() - Get statistic info of the file's fragments.
1217 * @file: the file's name.
1218 * @buf: the pointer of the struct stat64.
1220 * @ftwbuf: the pointer of a struct FTW.
1222 static int file_statistic(const char *file
, const struct stat64
*buf
,
1223 int flag
EXT2FS_ATTR((unused
)),
1224 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1228 int now_ext_count
, best_ext_count
= 0, physical_ext_count
;
1230 __u64 size_per_ext
= 0;
1232 ext4_fsblk_t blk_count
= 0;
1233 char msg_buffer
[PATH_MAX
+ 24];
1234 struct fiemap_extent_list
*physical_list_head
= NULL
;
1235 struct fiemap_extent_list
*logical_list_head
= NULL
;
1237 defraged_file_count
++;
1239 if (mode_flag
& DETAIL
) {
1240 if (total_count
== 1 && regular_count
== 1)
1243 printf("[%u/%u]", defraged_file_count
, total_count
);
1247 if (lost_found_dir
[0] != '\0' &&
1248 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1249 if (mode_flag
& DETAIL
) {
1250 PRINT_FILE_NAME(file
);
1251 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND
);
1256 if (!S_ISREG(buf
->st_mode
)) {
1257 if (mode_flag
& DETAIL
) {
1258 PRINT_FILE_NAME(file
);
1259 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG
);
1264 /* Access authority */
1265 if (current_uid
!= ROOT_UID
&&
1266 buf
->st_uid
!= current_uid
) {
1267 if (mode_flag
& DETAIL
) {
1268 PRINT_FILE_NAME(file
);
1270 "File is not current user's file"
1271 " or current user is not root");
1277 if (buf
->st_size
== 0) {
1278 if (mode_flag
& DETAIL
) {
1279 PRINT_FILE_NAME(file
);
1280 STATISTIC_ERR_MSG("File size is 0");
1286 if (buf
->st_blocks
== 0) {
1287 if (mode_flag
& DETAIL
) {
1288 PRINT_FILE_NAME(file
);
1289 STATISTIC_ERR_MSG("File has no blocks");
1294 fd
= open64(file
, O_RDONLY
);
1296 if (mode_flag
& DETAIL
) {
1297 PRINT_FILE_NAME(file
);
1298 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1303 /* Get file's physical extents */
1304 ret
= get_file_extents(fd
, &physical_list_head
);
1306 if (mode_flag
& DETAIL
) {
1307 PRINT_FILE_NAME(file
);
1308 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1313 /* Get the count of file's continuous physical region */
1314 physical_ext_count
= get_physical_count(physical_list_head
);
1316 /* Change list from physical to logical */
1317 ret
= change_physical_to_logical(&physical_list_head
,
1318 &logical_list_head
);
1320 if (mode_flag
& DETAIL
) {
1321 PRINT_FILE_NAME(file
);
1322 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1327 /* Count file fragments before defrag */
1328 now_ext_count
= get_logical_count(logical_list_head
);
1330 if (current_uid
== ROOT_UID
) {
1331 /* Calculate the size per extent */
1332 blk_count
= get_file_blocks(logical_list_head
);
1334 best_ext_count
= get_best_count(blk_count
);
1336 /* e4defrag rounds size_per_ext up to a block size boundary */
1337 size_per_ext
= blk_count
* (buf
->st_blksize
/ 1024) /
1340 ratio
= (float)(physical_ext_count
- best_ext_count
) * 100 /
1343 extents_before_defrag
+= now_ext_count
;
1344 extents_after_defrag
+= best_ext_count
;
1345 files_block_count
+= blk_count
;
1348 if (total_count
== 1 && regular_count
== 1) {
1350 if (mode_flag
& DETAIL
) {
1352 struct fiemap_extent_list
*ext_list_tmp
=
1355 /* Print extents info */
1358 printf("[ext %d]:\tstart %llu:\tlogical "
1359 "%llu:\tlen %llu\n", count
,
1360 ext_list_tmp
->data
.physical
,
1361 ext_list_tmp
->data
.logical
,
1362 ext_list_tmp
->data
.len
);
1363 ext_list_tmp
= ext_list_tmp
->next
;
1364 } while (ext_list_tmp
!= logical_list_head
);
1367 printf("%-40s%10s/%-10s%9s\n",
1368 "<File>", "now", "best", "size/ext");
1369 if (current_uid
== ROOT_UID
) {
1370 if (strlen(file
) > 40)
1371 printf("%s\n%50d/%-10d%6llu KB\n",
1372 file
, now_ext_count
,
1373 best_ext_count
, size_per_ext
);
1375 printf("%-40s%10d/%-10d%6llu KB\n",
1376 file
, now_ext_count
,
1377 best_ext_count
, size_per_ext
);
1379 if (strlen(file
) > 40)
1380 printf("%s\n%50d/%-10s%7s\n",
1381 file
, now_ext_count
,
1384 printf("%-40s%10d/%-10s%7s\n",
1385 file
, now_ext_count
,
1393 if (mode_flag
& DETAIL
) {
1394 /* Print statistic info */
1395 sprintf(msg_buffer
, "[%u/%u]%s",
1396 defraged_file_count
, total_count
, file
);
1397 if (current_uid
== ROOT_UID
) {
1398 if (strlen(msg_buffer
) > 40)
1399 printf("\033[79;0H\033[K%s\n"
1400 "%50d/%-10d%6llu KB\n",
1401 msg_buffer
, now_ext_count
,
1402 best_ext_count
, size_per_ext
);
1404 printf("\033[79;0H\033[K%-40s"
1405 "%10d/%-10d%6llu KB\n",
1406 msg_buffer
, now_ext_count
,
1407 best_ext_count
, size_per_ext
);
1409 if (strlen(msg_buffer
) > 40)
1410 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1411 msg_buffer
, now_ext_count
,
1414 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1415 msg_buffer
, now_ext_count
,
1420 for (i
= 0; i
< SHOW_FRAG_FILES
; i
++) {
1421 if (ratio
>= frag_rank
[i
].ratio
) {
1422 for (j
= SHOW_FRAG_FILES
- 1; j
> i
; j
--) {
1423 memset(&frag_rank
[j
], 0,
1424 sizeof(struct frag_statistic_ino
));
1425 strncpy(frag_rank
[j
].msg_buffer
,
1426 frag_rank
[j
- 1].msg_buffer
,
1427 strnlen(frag_rank
[j
- 1].msg_buffer
,
1429 frag_rank
[j
].now_count
=
1430 frag_rank
[j
- 1].now_count
;
1431 frag_rank
[j
].best_count
=
1432 frag_rank
[j
- 1].best_count
;
1433 frag_rank
[j
].size_per_ext
=
1434 frag_rank
[j
- 1].size_per_ext
;
1435 frag_rank
[j
].ratio
=
1436 frag_rank
[j
- 1].ratio
;
1438 memset(&frag_rank
[i
], 0,
1439 sizeof(struct frag_statistic_ino
));
1440 strncpy(frag_rank
[i
].msg_buffer
, file
,
1441 strnlen(file
, PATH_MAX
));
1442 frag_rank
[i
].now_count
= now_ext_count
;
1443 frag_rank
[i
].best_count
= best_ext_count
;
1444 frag_rank
[i
].size_per_ext
= size_per_ext
;
1445 frag_rank
[i
].ratio
= ratio
;
1454 free_ext(physical_list_head
);
1455 free_ext(logical_list_head
);
1460 * print_progress - Print defrag progress
1463 * @start: logical offset for defrag target file
1464 * @file_size: defrag target filesize
1466 static void print_progress(const char *file
, loff_t start
, loff_t file_size
)
1468 int percent
= (start
* 100) / file_size
;
1469 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1470 defraged_file_count
, total_count
, file
, min(percent
, 100));
1477 * call_defrag() - Execute the defrag program.
1479 * @fd: target file descriptor.
1480 * @donor_fd: donor file descriptor.
1481 * @file: target file name.
1482 * @buf: pointer of the struct stat64.
1483 * @ext_list_head: head of the extent list.
1485 static int call_defrag(int fd
, int donor_fd
, const char *file
,
1486 const struct stat64
*buf
, struct fiemap_extent_list
*ext_list_head
)
1489 unsigned int page_num
;
1490 unsigned char *vec
= NULL
;
1491 int defraged_ret
= 0;
1493 struct move_extent move_data
;
1494 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1496 memset(&move_data
, 0, sizeof(struct move_extent
));
1497 move_data
.donor_fd
= donor_fd
;
1499 /* Print defrag progress */
1500 print_progress(file
, start
, buf
->st_size
);
1502 ext_list_tmp
= ext_list_head
;
1504 move_data
.orig_start
= ext_list_tmp
->data
.logical
;
1505 /* Logical offset of orig and donor should be same */
1506 move_data
.donor_start
= move_data
.orig_start
;
1507 move_data
.len
= ext_list_tmp
->data
.len
;
1508 move_data
.moved_len
= 0;
1510 ret
= page_in_core(fd
, move_data
, &vec
, &page_num
);
1512 if (mode_flag
& DETAIL
) {
1514 PRINT_ERR_MSG_WITH_ERRNO(
1515 "Failed to get file map");
1517 printf("\t[ NG ]\n");
1522 /* EXT4_IOC_MOVE_EXT */
1524 ioctl(fd
, EXT4_IOC_MOVE_EXT
, &move_data
);
1527 ret
= defrag_fadvise(fd
, move_data
, vec
, page_num
);
1533 if (mode_flag
& DETAIL
) {
1535 PRINT_ERR_MSG_WITH_ERRNO(
1536 "Failed to free page");
1538 printf("\t[ NG ]\n");
1543 if (defraged_ret
< 0) {
1544 if (mode_flag
& DETAIL
) {
1546 PRINT_ERR_MSG_WITH_ERRNO(
1547 "Failed to defrag with "
1548 "EXT4_IOC_MOVE_EXT ioctl");
1549 if (errno
== ENOTTY
)
1550 printf("\tAt least 2.6.31-rc1 of "
1551 "vanilla kernel is required\n");
1553 printf("\t[ NG ]\n");
1557 /* Adjust logical offset for next ioctl */
1558 move_data
.orig_start
+= move_data
.moved_len
;
1559 move_data
.donor_start
= move_data
.orig_start
;
1561 start
= move_data
.orig_start
* buf
->st_blksize
;
1563 /* Print defrag progress */
1564 print_progress(file
, start
, buf
->st_size
);
1567 if (start
>= buf
->st_size
)
1570 ext_list_tmp
= ext_list_tmp
->next
;
1571 } while (ext_list_tmp
!= ext_list_head
);
1577 * file_defrag() - Check file attributes and call ioctl to defrag.
1579 * @file: the file's name.
1580 * @buf: the pointer of the struct stat64.
1582 * @ftwbuf: the pointer of a struct FTW.
1584 static int file_defrag(const char *file
, const struct stat64
*buf
,
1585 int flag
EXT2FS_ATTR((unused
)),
1586 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1592 int file_frags_start
, file_frags_end
;
1593 int orig_physical_cnt
, donor_physical_cnt
= 0;
1594 char tmp_inode_name
[PATH_MAX
+ 8];
1595 ext4_fsblk_t blk_count
= 0;
1596 struct fiemap_extent_list
*orig_list_physical
= NULL
;
1597 struct fiemap_extent_list
*orig_list_logical
= NULL
;
1598 struct fiemap_extent_list
*donor_list_physical
= NULL
;
1599 struct fiemap_extent_list
*donor_list_logical
= NULL
;
1600 struct fiemap_extent_group
*orig_group_head
= NULL
;
1601 struct fiemap_extent_group
*orig_group_tmp
= NULL
;
1603 defraged_file_count
++;
1605 if (mode_flag
& DETAIL
) {
1606 printf("[%u/%u]", defraged_file_count
, total_count
);
1610 if (lost_found_dir
[0] != '\0' &&
1611 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1612 if (mode_flag
& DETAIL
) {
1613 PRINT_FILE_NAME(file
);
1614 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1619 if (!S_ISREG(buf
->st_mode
)) {
1620 if (mode_flag
& DETAIL
) {
1621 PRINT_FILE_NAME(file
);
1622 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1628 if (buf
->st_size
== 0) {
1629 if (mode_flag
& DETAIL
) {
1630 PRINT_FILE_NAME(file
);
1631 IN_FTW_PRINT_ERR_MSG("File size is 0");
1637 if (buf
->st_blocks
== 0) {
1638 if (mode_flag
& DETAIL
) {
1639 PRINT_FILE_NAME(file
);
1640 STATISTIC_ERR_MSG("File has no blocks");
1645 fd
= open64(file
, O_RDWR
);
1647 if (mode_flag
& DETAIL
) {
1648 PRINT_FILE_NAME(file
);
1649 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1654 /* Get file's extents */
1655 ret
= get_file_extents(fd
, &orig_list_physical
);
1657 if (mode_flag
& DETAIL
) {
1658 PRINT_FILE_NAME(file
);
1659 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1664 /* Get the count of file's continuous physical region */
1665 orig_physical_cnt
= get_physical_count(orig_list_physical
);
1667 /* Change list from physical to logical */
1668 ret
= change_physical_to_logical(&orig_list_physical
,
1669 &orig_list_logical
);
1671 if (mode_flag
& DETAIL
) {
1672 PRINT_FILE_NAME(file
);
1673 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1678 /* Count file fragments before defrag */
1679 file_frags_start
= get_logical_count(orig_list_logical
);
1681 blk_count
= get_file_blocks(orig_list_logical
);
1682 if (file_check(fd
, buf
, file
, file_frags_start
, blk_count
) < 0)
1685 if (fsync(fd
) < 0) {
1686 if (mode_flag
& DETAIL
) {
1687 PRINT_FILE_NAME(file
);
1688 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1693 if (current_uid
== ROOT_UID
)
1694 best
= get_best_count(blk_count
);
1698 if (file_frags_start
<= best
)
1699 goto check_improvement
;
1701 /* Combine extents to group */
1702 ret
= join_extents(orig_list_logical
, &orig_group_head
);
1704 if (mode_flag
& DETAIL
) {
1705 PRINT_FILE_NAME(file
);
1706 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1711 /* Create donor inode */
1712 memset(tmp_inode_name
, 0, PATH_MAX
+ 8);
1713 sprintf(tmp_inode_name
, "%.*s.defrag",
1714 (int)strnlen(file
, PATH_MAX
), file
);
1715 donor_fd
= open64(tmp_inode_name
, O_WRONLY
| O_CREAT
| O_EXCL
, S_IRUSR
);
1717 if (mode_flag
& DETAIL
) {
1718 PRINT_FILE_NAME(file
);
1719 if (errno
== EEXIST
)
1720 PRINT_ERR_MSG_WITH_ERRNO(
1721 "File is being defraged by other program");
1723 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1728 /* Unlink donor inode */
1729 ret
= unlink(tmp_inode_name
);
1731 if (mode_flag
& DETAIL
) {
1732 PRINT_FILE_NAME(file
);
1733 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1738 /* Allocate space for donor inode */
1739 orig_group_tmp
= orig_group_head
;
1741 ret
= fallocate64(donor_fd
, 0,
1742 (loff_t
)orig_group_tmp
->start
->data
.logical
* block_size
,
1743 (loff_t
)orig_group_tmp
->len
* block_size
);
1745 if (mode_flag
& DETAIL
) {
1746 PRINT_FILE_NAME(file
);
1747 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1752 orig_group_tmp
= orig_group_tmp
->next
;
1753 } while (orig_group_tmp
!= orig_group_head
);
1755 /* Get donor inode's extents */
1756 ret
= get_file_extents(donor_fd
, &donor_list_physical
);
1758 if (mode_flag
& DETAIL
) {
1759 PRINT_FILE_NAME(file
);
1760 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1765 /* Calcuate donor inode's continuous physical region */
1766 donor_physical_cnt
= get_physical_count(donor_list_physical
);
1768 /* Change donor extent list from physical to logical */
1769 ret
= change_physical_to_logical(&donor_list_physical
,
1770 &donor_list_logical
);
1772 if (mode_flag
& DETAIL
) {
1773 PRINT_FILE_NAME(file
);
1774 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1780 if (mode_flag
& DETAIL
) {
1781 if (file_frags_start
!= 1)
1782 frag_files_before_defrag
++;
1784 extents_before_defrag
+= file_frags_start
;
1787 if (file_frags_start
<= best
||
1788 orig_physical_cnt
<= donor_physical_cnt
) {
1789 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1790 defraged_file_count
, total_count
, file
, 100);
1791 if (mode_flag
& DETAIL
)
1792 printf(" extents: %d -> %d",
1793 file_frags_start
, file_frags_start
);
1795 printf("\t[ OK ]\n");
1798 if (file_frags_start
!= 1)
1799 frag_files_after_defrag
++;
1801 extents_after_defrag
+= file_frags_start
;
1805 /* Defrag the file */
1806 ret
= call_defrag(fd
, donor_fd
, file
, buf
, donor_list_logical
);
1808 /* Count file fragments after defrag and print extents info */
1809 if (mode_flag
& DETAIL
) {
1810 file_frags_end
= file_frag_count(fd
);
1811 if (file_frags_end
< 0) {
1813 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO
);
1817 if (file_frags_end
!= 1)
1818 frag_files_after_defrag
++;
1820 extents_after_defrag
+= file_frags_end
;
1825 printf(" extents: %d -> %d",
1826 file_frags_start
, file_frags_end
);
1833 printf("\t[ OK ]\n");
1841 free_ext(orig_list_physical
);
1842 free_ext(orig_list_logical
);
1843 free_ext(donor_list_physical
);
1844 free_exts_group(orig_group_head
);
1849 * main() - Ext4 online defrag.
1851 * @argc: the number of parameter.
1852 * @argv[]: the pointer array of parameter.
1854 int main(int argc
, char *argv
[])
1858 int flags
= FTW_PHYS
| FTW_MOUNT
;
1860 int success_flag
= 0;
1861 char dir_name
[PATH_MAX
+ 1];
1863 struct ext4_super_block sb
;
1865 /* Parse arguments */
1869 while ((opt
= getopt(argc
, argv
, "vc")) != EOF
) {
1872 mode_flag
|= DETAIL
;
1875 mode_flag
|= STATISTIC
;
1885 current_uid
= getuid();
1888 for (i
= optind
; i
< argc
; i
++) {
1892 frag_files_before_defrag
= 0;
1893 frag_files_after_defrag
= 0;
1894 extents_before_defrag
= 0;
1895 extents_after_defrag
= 0;
1896 defraged_file_count
= 0;
1897 files_block_count
= 0;
1898 blocks_per_group
= 0;
1899 feature_incompat
= 0;
1900 log_groups_per_flex
= 0;
1902 memset(dir_name
, 0, PATH_MAX
+ 1);
1903 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1904 memset(frag_rank
, 0,
1905 sizeof(struct frag_statistic_ino
) * SHOW_FRAG_FILES
);
1907 if ((mode_flag
& STATISTIC
) && i
> optind
)
1910 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1911 PRINT_ERR_MSG("Endian's type is not big/little endian");
1912 PRINT_FILE_NAME(argv
[i
]);
1916 if (lstat64(argv
[i
], &buf
) < 0) {
1917 perror(NGMSG_FILE_INFO
);
1918 PRINT_FILE_NAME(argv
[i
]);
1922 if (S_ISBLK(buf
.st_mode
)) {
1924 if (get_mount_point(argv
[i
], dir_name
, PATH_MAX
) < 0)
1926 if (lstat64(dir_name
, &buf
) < 0) {
1927 perror(NGMSG_FILE_INFO
);
1928 PRINT_FILE_NAME(argv
[i
]);
1932 if (!(mode_flag
& STATISTIC
))
1933 printf("ext4 defragmentation for device(%s)\n",
1935 } else if (S_ISDIR(buf
.st_mode
)) {
1937 if (access(argv
[i
], R_OK
) < 0) {
1942 strncpy(dir_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1943 } else if (S_ISREG(buf
.st_mode
)) {
1945 arg_type
= FILENAME
;
1947 /* Irregular file */
1948 PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1949 PRINT_FILE_NAME(argv
[i
]);
1954 block_size
= buf
.st_blksize
;
1957 * filesystem type checked in get_mount_point()
1959 if (arg_type
== FILENAME
|| arg_type
== DIRNAME
) {
1960 if (is_ext4(argv
[i
]) < 0)
1962 if (realpath(argv
[i
], dir_name
) == NULL
) {
1963 perror("Couldn't get full path");
1964 PRINT_FILE_NAME(argv
[i
]);
1969 if (current_uid
== ROOT_UID
) {
1970 /* Get super block info */
1971 memset(&sb
, 0, sizeof(struct ext4_super_block
));
1972 if (get_superblock_info(dir_name
, &sb
) < 0) {
1973 if (mode_flag
& DETAIL
) {
1974 perror("Can't get super block info");
1975 PRINT_FILE_NAME(argv
[i
]);
1980 blocks_per_group
= ext2fs_swab32(sb
.s_blocks_per_group
);
1981 feature_incompat
= ext2fs_swab32(sb
.s_feature_incompat
);
1982 log_groups_per_flex
= sb
.s_log_groups_per_flex
;
1987 if (!(mode_flag
& STATISTIC
))
1988 printf("ext4 defragmentation "
1989 "for directory(%s)\n", argv
[i
]);
1991 int mount_dir_len
= 0;
1992 mount_dir_len
= strnlen(lost_found_dir
, PATH_MAX
);
1994 strncat(lost_found_dir
, "/lost+found",
1995 PATH_MAX
- strnlen(lost_found_dir
, PATH_MAX
));
1997 /* Not the case("e4defrag mount_piont_dir") */
1998 if (dir_name
[mount_dir_len
] != '\0') {
2000 * "e4defrag mount_piont_dir/lost+found"
2001 * or "e4defrag mount_piont_dir/lost+found/"
2003 if (strncmp(lost_found_dir
, dir_name
,
2004 strnlen(lost_found_dir
,
2006 (dir_name
[strnlen(lost_found_dir
,
2007 PATH_MAX
)] == '\0' ||
2008 dir_name
[strnlen(lost_found_dir
,
2009 PATH_MAX
)] == '/')) {
2010 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
2011 PRINT_FILE_NAME(argv
[i
]);
2015 /* "e4defrag mount_piont_dir/else_dir" */
2016 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
2019 if (arg_type
== DEVNAME
) {
2020 strncpy(lost_found_dir
, dir_name
,
2021 strnlen(dir_name
, PATH_MAX
));
2022 strncat(lost_found_dir
, "/lost+found/",
2023 PATH_MAX
- strnlen(lost_found_dir
,
2027 nftw64(dir_name
, calc_entry_counts
, FTW_OPEN_FD
, flags
);
2029 if (mode_flag
& STATISTIC
) {
2030 if (mode_flag
& DETAIL
)
2031 printf("%-40s%10s/%-10s%9s\n",
2032 "<File>", "now", "best", "size/ext");
2034 if (!(mode_flag
& DETAIL
) &&
2035 current_uid
!= ROOT_UID
) {
2041 nftw64(dir_name
, file_statistic
,
2042 FTW_OPEN_FD
, flags
);
2044 if (succeed_cnt
!= 0 &&
2045 current_uid
== ROOT_UID
) {
2046 if (mode_flag
& DETAIL
)
2048 printf("%-40s%10s/%-10s%9s\n",
2049 "<Fragmented files>", "now",
2050 "best", "size/ext");
2051 for (j
= 0; j
< SHOW_FRAG_FILES
; j
++) {
2052 if (strlen(frag_rank
[j
].
2054 printf("%d. %s\n%50d/"
2057 frag_rank
[j
].msg_buffer
,
2058 frag_rank
[j
].now_count
,
2059 frag_rank
[j
].best_count
,
2062 } else if (strlen(frag_rank
[j
].
2064 printf("%d. %-37s%10d/"
2067 frag_rank
[j
].msg_buffer
,
2068 frag_rank
[j
].now_count
,
2069 frag_rank
[j
].best_count
,
2078 /* File tree walk */
2079 nftw64(dir_name
, file_defrag
, FTW_OPEN_FD
, flags
);
2080 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt
,
2082 printf("\tFailure:\t\t\t[ %u/%u ]\n",
2083 total_count
- succeed_cnt
, total_count
);
2084 if (mode_flag
& DETAIL
) {
2085 printf("\tTotal extents:\t\t\t%4d->%d\n",
2086 extents_before_defrag
,
2087 extents_after_defrag
);
2088 printf("\tFragmented percentage:\t\t"
2089 "%3llu%%->%llu%%\n",
2090 !regular_count
? 0 :
2091 ((unsigned long long)
2092 frag_files_before_defrag
* 100) /
2094 !regular_count
? 0 :
2095 ((unsigned long long)
2096 frag_files_after_defrag
* 100) /
2103 strncat(lost_found_dir
, "/lost+found/",
2104 PATH_MAX
- strnlen(lost_found_dir
,
2106 if (strncmp(lost_found_dir
, dir_name
,
2107 strnlen(lost_found_dir
,
2109 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
2110 PRINT_FILE_NAME(argv
[i
]);
2114 if (mode_flag
& STATISTIC
) {
2115 file_statistic(argv
[i
], &buf
, FTW_F
, NULL
);
2118 printf("ext4 defragmentation for %s\n",
2120 /* Defrag single file process */
2121 file_defrag(argv
[i
], &buf
, FTW_F
, NULL
);
2122 if (succeed_cnt
!= 0)
2123 printf(" Success:\t\t\t[1/1]\n");
2125 printf(" Success:\t\t\t[0/1]\n");
2130 if (succeed_cnt
!= 0)
2132 if (mode_flag
& STATISTIC
) {
2133 if (current_uid
!= ROOT_UID
) {
2139 if (mode_flag
& DETAIL
)
2142 if (arg_type
== DEVNAME
)
2143 printf(" In this device(%s), "
2144 "none can be defragmented.\n", argv
[i
]);
2145 else if (arg_type
== DIRNAME
)
2146 printf(" In this directory(%s), "
2147 "none can be defragmented.\n", argv
[i
]);
2149 printf(" This file(%s) "
2150 "can't be defragmented.\n", argv
[i
]);
2152 float files_ratio
= 0.0;
2154 __u64 size_per_ext
= files_block_count
*
2155 (buf
.st_blksize
/ 1024) /
2156 extents_before_defrag
;
2157 files_ratio
= (float)(extents_before_defrag
-
2158 extents_after_defrag
) *
2159 100 / files_block_count
;
2160 score
= CALC_SCORE(files_ratio
);
2161 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2162 " Average size per extent"
2164 " Fragmentation score\t\t\t\t%.0f\n",
2165 extents_before_defrag
,
2166 extents_after_defrag
,
2167 size_per_ext
, score
);
2168 printf(" [0-30 no problem:"
2169 " 31-55 a little bit fragmented:"
2170 " 56- needs defrag]\n");
2172 if (arg_type
== DEVNAME
)
2173 printf(" This device (%s) ", argv
[i
]);
2174 else if (arg_type
== DIRNAME
)
2175 printf(" This directory (%s) ",
2178 printf(" This file (%s) ", argv
[i
]);
2180 if (score
> BOUND_SCORE
)
2181 printf("needs defragmentation.\n");
2183 printf("does not need "
2184 "defragmentation.\n");