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 SECTOR_TO_BLOCK(sectors, blocksize) \
63 ((sectors) / ((blocksize) >> 9))
64 #define CALC_SCORE(ratio) \
65 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
66 /* Wrap up the free function */
72 /* Insert list2 after list1 */
73 #define insert(list1, list2) \
75 list2->next = list1->next; \
76 list1->next->prev = list2; \
77 list2->prev = list1; \
78 list1->next = list2; \
81 /* To delete unused warning */
83 #define EXT2FS_ATTR(x) __attribute__(x)
85 #define EXT2FS_ATTR(x)
88 /* The mode of defrag */
90 #define STATISTIC 0x02
96 #define FTW_OPEN_FD 2000
98 #define FS_EXT4 "ext4"
101 #define BOUND_SCORE 55
102 #define SHOW_FRAG_FILES 5
104 /* Magic number for ext4 */
105 #define EXT4_SUPER_MAGIC 0xEF53
107 /* Definition of flex_bg */
108 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
110 /* The following macro is used for ioctl FS_IOC_FIEMAP
111 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
112 * kernel-space and user-space per ioctl
114 #define EXTENT_MAX_COUNT 512
116 /* The following macros are error message */
118 "Usage : e4defrag [-v] file...| directory...| device...\n\
119 : e4defrag -c file...| directory...| device...\n"
121 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
122 #define NGMSG_FILE_EXTENT "Failed to get file extents"
123 #define NGMSG_FILE_INFO "Failed to get file information"
124 #define NGMSG_FILE_OPEN "Failed to open"
125 #define NGMSG_FILE_UNREG "File is not regular file"
126 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
128 /* Data type for filesystem-wide blocks number */
129 typedef unsigned long long ext4_fsblk_t
;
131 struct fiemap_extent_data
{
132 __u64 len
; /* blocks count */
133 __u64 logical
; /* start logical block number */
134 ext4_fsblk_t physical
; /* start physical block number */
137 struct fiemap_extent_list
{
138 struct fiemap_extent_list
*prev
;
139 struct fiemap_extent_list
*next
;
140 struct fiemap_extent_data data
; /* extent belong to file */
143 struct fiemap_extent_group
{
144 struct fiemap_extent_group
*prev
;
145 struct fiemap_extent_group
*next
;
146 __u64 len
; /* length of this continuous region */
147 struct fiemap_extent_list
*start
; /* start ext */
148 struct fiemap_extent_list
*end
; /* end ext */
152 __s32 reserved
; /* original file descriptor */
153 __u32 donor_fd
; /* donor file descriptor */
154 __u64 orig_start
; /* logical start offset in block for orig */
155 __u64 donor_start
; /* logical start offset in block for donor */
156 __u64 len
; /* block length to be moved */
157 __u64 moved_len
; /* moved block length */
160 struct frag_statistic_ino
{
161 int now_count
; /* the file's extents count of before defrag */
162 int best_count
; /* the best file's extents count */
163 float ratio
; /* the ratio of fragmentation */
164 char msg_buffer
[PATH_MAX
+ 1]; /* pathname of the file */
167 typedef __u16 __le16
;
168 typedef __u32 __le32
;
169 typedef __u64 __le64
;
172 * Structure of the super block
174 struct ext4_super_block
{
175 /*00*/ __le32 s_inodes_count
; /* Inodes count */
176 __le32 s_blocks_count_lo
; /* Blocks count */
177 __le32 s_r_blocks_count_lo
; /* Reserved blocks count */
178 __le32 s_free_blocks_count_lo
; /* Free blocks count */
179 /*10*/ __le32 s_free_inodes_count
; /* Free inodes count */
180 __le32 s_first_data_block
; /* First Data Block */
181 __le32 s_log_block_size
; /* Block size */
182 __le32 s_obso_log_frag_size
; /* Obsoleted fragment size */
183 /*20*/ __le32 s_blocks_per_group
; /* # Blocks per group */
184 __le32 s_obso_frags_per_group
; /* Obsoleted fragments per group */
185 __le32 s_inodes_per_group
; /* # Inodes per group */
186 __le32 s_mtime
; /* Mount time */
187 /*30*/ __le32 s_wtime
; /* Write time */
188 __le16 s_mnt_count
; /* Mount count */
189 __le16 s_max_mnt_count
; /* Maximal mount count */
190 __le16 s_magic
; /* Magic signature */
191 __le16 s_state
; /* File system state */
192 __le16 s_errors
; /* Behaviour when detecting errors */
193 __le16 s_minor_rev_level
; /* minor revision level */
194 /*40*/ __le32 s_lastcheck
; /* time of last check */
195 __le32 s_checkinterval
; /* max. time between checks */
196 __le32 s_creator_os
; /* OS */
197 __le32 s_rev_level
; /* Revision level */
198 /*50*/ __le16 s_def_resuid
; /* Default uid for reserved blocks */
199 __le16 s_def_resgid
; /* Default gid for reserved blocks */
201 * These fields are for EXT4_DYNAMIC_REV superblocks only.
203 * Note: the difference between the compatible feature set and
204 * the incompatible feature set is that if there is a bit set
205 * in the incompatible feature set that the kernel doesn't
206 * know about, it should refuse to mount the filesystem.
208 * e2fsck's requirements are more strict; if it doesn't know
209 * about a feature in either the compatible or incompatible
210 * feature set, it must abort and not try to meddle with
211 * things it doesn't understand...
213 __le32 s_first_ino
; /* First non-reserved inode */
214 __le16 s_inode_size
; /* size of inode structure */
215 __le16 s_block_group_nr
; /* block group # of this superblock */
216 __le32 s_feature_compat
; /* compatible feature set */
217 /*60*/ __le32 s_feature_incompat
; /* incompatible feature set */
218 __le32 s_feature_ro_compat
; /* readonly-compatible feature set */
219 /*68*/ __u8 s_uuid
[16]; /* 128-bit uuid for volume */
220 /*78*/ char s_volume_name
[16]; /* volume name */
221 /*88*/ char s_last_mounted
[64]; /* directory where last mounted */
222 /*C8*/ __le32 s_algorithm_usage_bitmap
; /* For compression */
224 * Performance hints. Directory preallocation should only
225 * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
227 __u8 s_prealloc_blocks
; /* Nr of blocks to try to preallocate*/
228 __u8 s_prealloc_dir_blocks
; /* Nr to preallocate for dirs */
229 __le16 s_reserved_gdt_blocks
; /* Per group desc for online growth */
231 * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
233 /*D0*/ __u8 s_journal_uuid
[16]; /* uuid of journal superblock */
234 /*E0*/ __le32 s_journal_inum
; /* inode number of journal file */
235 __le32 s_journal_dev
; /* device number of journal file */
236 __le32 s_last_orphan
; /* start of list of inodes to delete */
237 __le32 s_hash_seed
[4]; /* HTREE hash seed */
238 __u8 s_def_hash_version
; /* Default hash version to use */
239 __u8 s_reserved_char_pad
;
240 __le16 s_desc_size
; /* size of group descriptor */
241 /*100*/ __le32 s_default_mount_opts
;
242 __le32 s_first_meta_bg
; /* First metablock block group */
243 __le32 s_mkfs_time
; /* When the filesystem was created */
244 __le32 s_jnl_blocks
[17]; /* Backup of the journal inode */
245 /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
246 /*150*/ __le32 s_blocks_count_hi
; /* Blocks count */
247 __le32 s_r_blocks_count_hi
; /* Reserved blocks count */
248 __le32 s_free_blocks_count_hi
; /* Free blocks count */
249 __le16 s_min_extra_isize
; /* All inodes have at least # bytes */
250 __le16 s_want_extra_isize
; /* New inodes should reserve # bytes */
251 __le32 s_flags
; /* Miscellaneous flags */
252 __le16 s_raid_stride
; /* RAID stride */
253 __le16 s_mmp_interval
; /* # seconds to wait in MMP checking */
254 __le64 s_mmp_block
; /* Block for multi-mount protection */
255 __le32 s_raid_stripe_width
; /* blocks on all data disks (N*stride)*/
256 __u8 s_log_groups_per_flex
; /* FLEX_BG group size */
257 __u8 s_reserved_char_pad2
;
258 __le16 s_reserved_pad
;
259 __u32 s_reserved
[162]; /* Padding to the end of the block */
262 char lost_found_dir
[PATH_MAX
+ 1];
264 int extents_before_defrag
;
265 int extents_after_defrag
;
267 unsigned int current_uid
;
268 unsigned int defraged_file_count
;
269 unsigned int frag_files_before_defrag
;
270 unsigned int frag_files_after_defrag
;
271 unsigned int regular_count
;
272 unsigned int succeed_cnt
;
273 unsigned int total_count
;
274 __u8 log_groups_per_flex
;
275 __le32 blocks_per_group
;
276 __le32 feature_incompat
;
277 ext4_fsblk_t files_block_count
;
278 struct frag_statistic_ino frag_rank
[SHOW_FRAG_FILES
];
281 /* Local definitions of some syscalls glibc may not yet have */
283 #ifndef HAVE_POSIX_FADVISE
284 #warning Using locally defined posix_fadvise interface.
286 #ifndef __NR_fadvise64_64
287 #error Your kernel headers dont define __NR_fadvise64_64
291 * fadvise() - Give advice about file access.
293 * @fd: defrag target file's descriptor.
294 * @offset: file offset.
296 * @advise: process flag.
298 static int posix_fadvise(int fd
, loff_t offset
, size_t len
, int advise
)
300 return syscall(__NR_fadvise64_64
, fd
, offset
, len
, advise
);
302 #endif /* ! HAVE_FADVISE64_64 */
304 #ifndef HAVE_SYNC_FILE_RANGE
305 #warning Using locally defined sync_file_range interface.
307 #ifndef __NR_sync_file_range
308 #ifndef __NR_sync_file_range2 /* ppc */
309 #error Your kernel headers dont define __NR_sync_file_range
314 * sync_file_range() - Sync file region.
316 * @fd: defrag target file's descriptor.
317 * @offset: file offset.
318 * @length: area length.
319 * @flag: process flag.
321 int sync_file_range(int fd
, loff_t offset
, loff_t length
, unsigned int flag
)
323 #ifdef __NR_sync_file_range
324 return syscall(__NR_sync_file_range
, fd
, offset
, length
, flag
);
326 return syscall(__NR_sync_file_range2
, fd
, flag
, offset
, length
);
329 #endif /* ! HAVE_SYNC_FILE_RANGE */
331 #ifndef HAVE_FALLOCATE
332 #warning Using locally defined fallocate syscall interface.
334 #ifndef __NR_fallocate
335 #error Your kernel headers dont define __NR_fallocate
339 * fallocate() - Manipulate file space.
341 * @fd: defrag target file's descriptor.
342 * @mode: process flag.
343 * @offset: file offset.
346 static int fallocate(int fd
, int mode
, loff_t offset
, loff_t len
)
348 return syscall(__NR_fallocate
, fd
, mode
, offset
, len
);
350 #endif /* ! HAVE_FALLOCATE */
353 * get_mount_point() - Get device's mount point.
355 * @devname: the device's name.
356 * @mount_point: the mount point.
357 * @dir_path_len: the length of directory.
359 static int get_mount_point(const char *devname
, char *mount_point
,
362 /* Refer to /etc/mtab */
363 const char *mtab
= MOUNTED
;
365 struct mntent
*mnt
= NULL
;
367 fp
= setmntent(mtab
, "r");
369 perror("Couldn't access /etc/mtab");
373 while ((mnt
= getmntent(fp
)) != NULL
) {
374 if (strcmp(devname
, mnt
->mnt_fsname
) != 0)
378 if (strcmp(mnt
->mnt_type
, FS_EXT4
) == 0) {
379 strncpy(mount_point
, mnt
->mnt_dir
,
383 PRINT_ERR_MSG(NGMSG_EXT4
);
387 PRINT_ERR_MSG("Filesystem is not mounted");
392 * is_ext4() - Whether on an ext4 filesystem.
394 * @file: the file's name.
396 static int is_ext4(const char *file
)
401 char *mnt_type
= NULL
;
402 /* Refer to /etc/mtab */
403 const char *mtab
= MOUNTED
;
404 char file_path
[PATH_MAX
+ 1];
405 struct mntent
*mnt
= NULL
;
406 struct statfs64 fsbuf
;
409 if (realpath(file
, file_path
) == NULL
) {
410 perror("Couldn't get full path");
411 PRINT_FILE_NAME(file
);
415 if (statfs64(file_path
, &fsbuf
) < 0) {
416 perror("Failed to get filesystem information");
417 PRINT_FILE_NAME(file
);
421 if (fsbuf
.f_type
!= EXT4_SUPER_MAGIC
) {
422 PRINT_ERR_MSG(NGMSG_EXT4
);
426 fp
= setmntent(mtab
, "r");
428 perror("Couldn't access /etc/mtab");
432 while ((mnt
= getmntent(fp
)) != NULL
) {
433 len
= strlen(mnt
->mnt_dir
);
434 ret
= memcmp(file_path
, mnt
->mnt_dir
, len
);
443 mnt_type
= realloc(mnt_type
, strlen(mnt
->mnt_type
) + 1);
444 if (mnt_type
== NULL
) {
448 memset(mnt_type
, 0, strlen(mnt
->mnt_type
) + 1);
449 strncpy(mnt_type
, mnt
->mnt_type
, strlen(mnt
->mnt_type
));
450 strncpy(lost_found_dir
, mnt
->mnt_dir
, PATH_MAX
);
454 if (strcmp(mnt_type
, FS_EXT4
) == 0) {
459 PRINT_ERR_MSG(NGMSG_EXT4
);
465 * calc_entry_counts() - Calculate file counts.
470 * @ftwbuf: the pointer of a struct FTW.
472 static int calc_entry_counts(const char *file
EXT2FS_ATTR((unused
)),
473 const struct stat64
*buf
, int flag
EXT2FS_ATTR((unused
)),
474 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
476 if (S_ISREG(buf
->st_mode
))
485 * page_in_core() - Get information on whether pages are in core.
487 * @fd: defrag target file's descriptor.
488 * @defrag_data: data used for defrag.
489 * @vec: page state array.
490 * @page_num: page number.
492 static int page_in_core(int fd
, struct move_extent defrag_data
,
493 unsigned char **vec
, unsigned int *page_num
)
495 long pagesize
= sysconf(_SC_PAGESIZE
);
497 loff_t offset
, end_offset
, length
;
499 if (vec
== NULL
|| *vec
!= NULL
)
502 /* In mmap, offset should be a multiple of the page size */
503 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
504 length
= (loff_t
)defrag_data
.len
* block_size
;
505 end_offset
= offset
+ length
;
506 /* Round the offset down to the nearest multiple of pagesize */
507 offset
= (offset
/ pagesize
) * pagesize
;
508 length
= end_offset
- offset
;
510 page
= mmap(NULL
, length
, PROT_READ
, MAP_SHARED
, fd
, offset
);
511 if (page
== MAP_FAILED
)
515 *page_num
= (length
+ pagesize
- 1) / pagesize
;
516 *vec
= (unsigned char *)calloc(*page_num
, 1);
520 /* Get information on whether pages are in core */
521 if (mincore(page
, (size_t)length
, *vec
) == -1 ||
522 munmap(page
, length
) == -1) {
531 * defrag_fadvise() - Predeclare an access pattern for file data.
533 * @fd: defrag target file's descriptor.
534 * @defrag_data: data used for defrag.
535 * @vec: page state array.
536 * @page_num: page number.
538 static int defrag_fadvise(int fd
, struct move_extent defrag_data
,
539 unsigned char *vec
, unsigned int page_num
)
542 long pagesize
= sysconf(_SC_PAGESIZE
);
543 int fadvise_flag
= POSIX_FADV_DONTNEED
;
544 int sync_flag
= SYNC_FILE_RANGE_WAIT_BEFORE
|
545 SYNC_FILE_RANGE_WRITE
|
546 SYNC_FILE_RANGE_WAIT_AFTER
;
550 offset
= (loff_t
)defrag_data
.orig_start
* block_size
;
551 offset
= (offset
/ pagesize
) * pagesize
;
553 /* Sync file for fadvise process */
554 if (sync_file_range(fd
, offset
,
555 (loff_t
)pagesize
* page_num
, sync_flag
) < 0)
558 /* Try to release buffer cache which this process used,
559 * then other process can use the released buffer
561 for (i
= 0; i
< page_num
; i
++) {
562 if ((vec
[i
] & 0x1) == 0) {
566 if (posix_fadvise(fd
, offset
, pagesize
, fadvise_flag
) < 0) {
567 if ((mode_flag
& DETAIL
) && flag
) {
568 perror("\tFailed to fadvise");
579 * check_free_size() - Check if there's enough disk space.
581 * @fd: defrag target file's descriptor.
583 * @buf: the pointer of the struct stat64.
585 static int check_free_size(int fd
, const char *file
, const struct stat64
*buf
)
587 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 /* Target file size measured by filesystem IO blocksize */
601 blk_count
= SECTOR_TO_BLOCK(buf
->st_blocks
, fsbuf
.f_bsize
);
603 /* Compute free space for root and normal user separately */
604 if (current_uid
== ROOT_UID
)
605 free_blk_count
= fsbuf
.f_bfree
;
607 free_blk_count
= fsbuf
.f_bavail
;
609 if (free_blk_count
>= blk_count
)
616 * file_frag_count() - Get file fragment count.
618 * @fd: defrag target file's descriptor.
620 static int file_frag_count(int fd
)
623 struct fiemap fiemap_buf
;
625 /* When fm_extent_count is 0,
626 * ioctl just get file fragment count.
628 memset(&fiemap_buf
, 0, sizeof(struct fiemap
));
629 fiemap_buf
.fm_start
= 0;
630 fiemap_buf
.fm_length
= FIEMAP_MAX_OFFSET
;
631 fiemap_buf
.fm_flags
|= FIEMAP_FLAG_SYNC
;
633 ret
= ioctl(fd
, FS_IOC_FIEMAP
, &fiemap_buf
);
637 return fiemap_buf
.fm_mapped_extents
;
641 * file_check() - Check file's attributes.
643 * @fd: defrag target file's descriptor.
644 * @buf: a pointer of the struct stat64.
645 * @file: the file's name.
646 * @extents: the file's extents.
648 static int file_check(int fd
, const struct stat64
*buf
, const char *file
,
654 /* Write-lock check is more reliable */
655 lock
.l_type
= F_WRLCK
;
657 lock
.l_whence
= SEEK_SET
;
661 ret
= check_free_size(fd
, file
, buf
);
663 if ((mode_flag
& DETAIL
) && ret
== -ENOSPC
) {
664 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
665 " extents: %d -> %d\n", defraged_file_count
,
666 total_count
, file
, extents
, extents
);
667 IN_FTW_PRINT_ERR_MSG(
668 "Defrag size is larger than filesystem's free space");
673 /* Access authority */
674 if (current_uid
!= ROOT_UID
&&
675 buf
->st_uid
!= current_uid
) {
676 if (mode_flag
& DETAIL
) {
677 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
678 " extents: %d -> %d\n", defraged_file_count
,
679 total_count
, file
, extents
, extents
);
680 IN_FTW_PRINT_ERR_MSG(
681 "File is not current user's file"
682 " or current user is not root");
688 if (fcntl(fd
, F_GETLK
, &lock
) < 0) {
689 if (mode_flag
& DETAIL
) {
690 PRINT_FILE_NAME(file
);
691 PRINT_ERR_MSG_WITH_ERRNO(
692 "Failed to get lock information");
695 } else if (lock
.l_type
!= F_UNLCK
) {
696 if (mode_flag
& DETAIL
) {
697 PRINT_FILE_NAME(file
);
698 IN_FTW_PRINT_ERR_MSG("File has been locked");
707 * insert_extent_by_logical() - Sequentially insert extent by logical.
709 * @ext_list_head: the head of logical extent list.
710 * @ext: the extent element which will be inserted.
712 static int insert_extent_by_logical(struct fiemap_extent_list
**ext_list_head
,
713 struct fiemap_extent_list
*ext
)
715 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
721 if (*ext_list_head
== NULL
) {
722 (*ext_list_head
) = ext
;
723 (*ext_list_head
)->prev
= *ext_list_head
;
724 (*ext_list_head
)->next
= *ext_list_head
;
728 if (ext
->data
.logical
<= ext_list_tmp
->data
.logical
) {
729 /* Insert before head */
730 if (ext_list_tmp
->data
.logical
<
731 ext
->data
.logical
+ ext
->data
.len
)
735 *ext_list_head
= ext
;
737 /* Insert into the middle or last of the list */
739 if (ext
->data
.logical
< ext_list_tmp
->data
.logical
)
741 ext_list_tmp
= ext_list_tmp
->next
;
742 } while (ext_list_tmp
!= (*ext_list_head
));
743 if (ext
->data
.logical
<
744 ext_list_tmp
->prev
->data
.logical
+
745 ext_list_tmp
->prev
->data
.len
)
749 if (ext_list_tmp
!= *ext_list_head
&&
750 ext_list_tmp
->data
.logical
<
751 ext
->data
.logical
+ ext
->data
.len
)
755 ext_list_tmp
= ext_list_tmp
->prev
;
756 /* Insert "ext" after "ext_list_tmp" */
757 insert(ext_list_tmp
, ext
);
765 * insert_extent_by_physical() - Sequentially insert extent by physical.
767 * @ext_list_head: the head of physical extent list.
768 * @ext: the extent element which will be inserted.
770 static int insert_extent_by_physical(struct fiemap_extent_list
**ext_list_head
,
771 struct fiemap_extent_list
*ext
)
773 struct fiemap_extent_list
*ext_list_tmp
= *ext_list_head
;
779 if (*ext_list_head
== NULL
) {
780 (*ext_list_head
) = ext
;
781 (*ext_list_head
)->prev
= *ext_list_head
;
782 (*ext_list_head
)->next
= *ext_list_head
;
786 if (ext
->data
.physical
<= ext_list_tmp
->data
.physical
) {
787 /* Insert before head */
788 if (ext_list_tmp
->data
.physical
<
789 ext
->data
.physical
+ ext
->data
.len
)
793 *ext_list_head
= ext
;
795 /* Insert into the middle or last of the list */
797 if (ext
->data
.physical
< ext_list_tmp
->data
.physical
)
799 ext_list_tmp
= ext_list_tmp
->next
;
800 } while (ext_list_tmp
!= (*ext_list_head
));
801 if (ext
->data
.physical
<
802 ext_list_tmp
->prev
->data
.physical
+
803 ext_list_tmp
->prev
->data
.len
)
807 if (ext_list_tmp
!= *ext_list_head
&&
808 ext_list_tmp
->data
.physical
<
809 ext
->data
.physical
+ ext
->data
.len
)
813 ext_list_tmp
= ext_list_tmp
->prev
;
814 /* Insert "ext" after "ext_list_tmp" */
815 insert(ext_list_tmp
, ext
);
823 * insert_exts_group() - Insert a exts_group.
825 * @ext_group_head: the head of a exts_group list.
826 * @exts_group: the exts_group element which will be inserted.
828 static int insert_exts_group(struct fiemap_extent_group
**ext_group_head
,
829 struct fiemap_extent_group
*exts_group
)
831 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
833 if (exts_group
== NULL
) {
838 /* Initialize list */
839 if (*ext_group_head
== NULL
) {
840 (*ext_group_head
) = exts_group
;
841 (*ext_group_head
)->prev
= *ext_group_head
;
842 (*ext_group_head
)->next
= *ext_group_head
;
846 ext_group_tmp
= (*ext_group_head
)->prev
;
847 insert(ext_group_tmp
, exts_group
);
853 * join_extents() - Find continuous region(exts_group).
855 * @ext_list_head: the head of the extent list.
856 * @ext_group_head: the head of the target exts_group list.
858 static int join_extents(struct fiemap_extent_list
*ext_list_head
,
859 struct fiemap_extent_group
**ext_group_head
)
861 __u64 len
= ext_list_head
->data
.len
;
862 struct fiemap_extent_list
*ext_list_start
= ext_list_head
;
863 struct fiemap_extent_list
*ext_list_tmp
= ext_list_head
->next
;
866 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
868 /* This extent and previous extent are not continuous,
869 * so, all previous extents are treated as an extent group.
871 if ((ext_list_tmp
->prev
->data
.logical
+
872 ext_list_tmp
->prev
->data
.len
)
873 != ext_list_tmp
->data
.logical
) {
875 malloc(sizeof(struct fiemap_extent_group
));
876 if (ext_group_tmp
== NULL
)
879 memset(ext_group_tmp
, 0,
880 sizeof(struct fiemap_extent_group
));
881 ext_group_tmp
->len
= len
;
882 ext_group_tmp
->start
= ext_list_start
;
883 ext_group_tmp
->end
= ext_list_tmp
->prev
;
885 if (insert_exts_group(ext_group_head
,
886 ext_group_tmp
) < 0) {
890 ext_list_start
= ext_list_tmp
;
891 len
= ext_list_tmp
->data
.len
;
892 ext_list_tmp
= ext_list_tmp
->next
;
896 /* This extent and previous extent are continuous,
897 * so, they belong to the same extent group, and we check
898 * if the next extent belongs to the same extent group.
900 len
+= ext_list_tmp
->data
.len
;
901 ext_list_tmp
= ext_list_tmp
->next
;
902 } while (ext_list_tmp
!= ext_list_head
->next
);
908 * get_file_extents() - Get file's extent list.
910 * @fd: defrag target file's descriptor.
911 * @ext_list_head: the head of the extent list.
913 static int get_file_extents(int fd
, struct fiemap_extent_list
**ext_list_head
)
917 int ext_buf_size
, fie_buf_size
;
919 struct fiemap
*fiemap_buf
= NULL
;
920 struct fiemap_extent
*ext_buf
= NULL
;
921 struct fiemap_extent_list
*ext_list
= NULL
;
923 /* Convert units, in bytes.
924 * Be careful : now, physical block number in extent is 48bit,
925 * and the maximum blocksize for ext4 is 4K(12bit),
926 * so there is no overflow, but in future it may be changed.
929 /* Alloc space for fiemap */
930 ext_buf_size
= EXTENT_MAX_COUNT
* sizeof(struct fiemap_extent
);
931 fie_buf_size
= sizeof(struct fiemap
) + ext_buf_size
;
933 fiemap_buf
= malloc(fie_buf_size
);
934 if (fiemap_buf
== NULL
)
937 ext_buf
= fiemap_buf
->fm_extents
;
938 memset(fiemap_buf
, 0, fie_buf_size
);
939 fiemap_buf
->fm_length
= FIEMAP_MAX_OFFSET
;
940 fiemap_buf
->fm_flags
|= FIEMAP_FLAG_SYNC
;
941 fiemap_buf
->fm_extent_count
= EXTENT_MAX_COUNT
;
944 fiemap_buf
->fm_start
= pos
;
945 memset(ext_buf
, 0, ext_buf_size
);
946 ret
= ioctl(fd
, FS_IOC_FIEMAP
, fiemap_buf
);
949 for (i
= 0; i
< fiemap_buf
->fm_mapped_extents
; i
++) {
951 ext_list
= malloc(sizeof(struct fiemap_extent_list
));
952 if (ext_list
== NULL
)
955 ext_list
->data
.physical
= ext_buf
[i
].fe_physical
957 ext_list
->data
.logical
= ext_buf
[i
].fe_logical
959 ext_list
->data
.len
= ext_buf
[i
].fe_length
962 ret
= insert_extent_by_physical(
963 ext_list_head
, ext_list
);
969 /* Record file's logical offset this time */
970 pos
= ext_buf
[EXTENT_MAX_COUNT
-1].fe_logical
+
971 ext_buf
[EXTENT_MAX_COUNT
-1].fe_length
;
973 * If fm_extents array has been filled and
974 * there are extents left, continue to cycle.
976 } while (fiemap_buf
->fm_mapped_extents
977 == EXTENT_MAX_COUNT
&&
978 !(ext_buf
[EXTENT_MAX_COUNT
-1].fe_flags
979 & FIEMAP_EXTENT_LAST
));
989 * get_logical_count() - Get the file logical extents count.
991 * @logical_list_head: the head of the logical extent list.
993 static int get_logical_count(struct fiemap_extent_list
*logical_list_head
)
996 struct fiemap_extent_list
*ext_list_tmp
= logical_list_head
;
1000 ext_list_tmp
= ext_list_tmp
->next
;
1001 } while (ext_list_tmp
!= logical_list_head
);
1007 * get_physical_count() - Get the file physical extents count.
1009 * @physical_list_head: the head of the physical extent list.
1011 static int get_physical_count(struct fiemap_extent_list
*physical_list_head
)
1014 struct fiemap_extent_list
*ext_list_tmp
= physical_list_head
;
1017 if ((ext_list_tmp
->data
.physical
+ ext_list_tmp
->data
.len
)
1018 != ext_list_tmp
->next
->data
.physical
) {
1019 /* This extent and next extent are not continuous. */
1023 ext_list_tmp
= ext_list_tmp
->next
;
1024 } while (ext_list_tmp
!= physical_list_head
);
1030 * change_physical_to_logical() - Change list from physical to logical.
1032 * @physical_list_head: the head of physical extent list.
1033 * @logical_list_head: the head of logical extent list.
1035 static int change_physical_to_logical(
1036 struct fiemap_extent_list
**physical_list_head
,
1037 struct fiemap_extent_list
**logical_list_head
)
1040 struct fiemap_extent_list
*ext_list_tmp
= *physical_list_head
;
1041 struct fiemap_extent_list
*ext_list_next
= ext_list_tmp
->next
;
1044 if (ext_list_tmp
== ext_list_next
) {
1045 ret
= insert_extent_by_logical(
1046 logical_list_head
, ext_list_tmp
);
1050 *physical_list_head
= NULL
;
1054 ext_list_tmp
->prev
->next
= ext_list_tmp
->next
;
1055 ext_list_tmp
->next
->prev
= ext_list_tmp
->prev
;
1056 *physical_list_head
= ext_list_next
;
1058 ret
= insert_extent_by_logical(
1059 logical_list_head
, ext_list_tmp
);
1064 ext_list_tmp
= ext_list_next
;
1065 ext_list_next
= ext_list_next
->next
;
1072 * free_ext() - Free the extent list.
1074 * @ext_list_head: the extent list head of which will be free.
1076 static void free_ext(struct fiemap_extent_list
*ext_list_head
)
1078 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1080 if (ext_list_head
== NULL
)
1083 while (ext_list_head
->next
!= ext_list_head
) {
1084 ext_list_tmp
= ext_list_head
;
1085 ext_list_head
->prev
->next
= ext_list_head
->next
;
1086 ext_list_head
->next
->prev
= ext_list_head
->prev
;
1087 ext_list_head
= ext_list_head
->next
;
1090 free(ext_list_head
);
1094 * free_exts_group() - Free the exts_group.
1096 * @*ext_group_head: the exts_group list head which will be free.
1098 static void free_exts_group(struct fiemap_extent_group
*ext_group_head
)
1100 struct fiemap_extent_group
*ext_group_tmp
= NULL
;
1102 if (ext_group_head
== NULL
)
1105 while (ext_group_head
->next
!= ext_group_head
) {
1106 ext_group_tmp
= ext_group_head
;
1107 ext_group_head
->prev
->next
= ext_group_head
->next
;
1108 ext_group_head
->next
->prev
= ext_group_head
->prev
;
1109 ext_group_head
= ext_group_head
->next
;
1110 free(ext_group_tmp
);
1112 free(ext_group_head
);
1116 * get_superblock_info() - Get superblock info by the file name.
1118 * @file: the file's name.
1119 * @sb: the pointer of the struct ext4_super_block.
1121 static int get_superblock_info(const char *file
, struct ext4_super_block
*sb
)
1123 /* Refer to /etc/mtab */
1124 const char *mtab
= MOUNTED
;
1131 char dev_name
[PATH_MAX
+ 1];
1132 struct mntent
*mnt
= NULL
;
1134 fp
= setmntent(mtab
, "r");
1138 while ((mnt
= getmntent(fp
)) != NULL
) {
1139 len
= strlen(mnt
->mnt_dir
);
1140 ret
= memcmp(file
, mnt
->mnt_dir
, len
);
1149 memset(dev_name
, 0, PATH_MAX
+ 1);
1150 strncpy(dev_name
, mnt
->mnt_fsname
,
1151 strnlen(mnt
->mnt_fsname
, PATH_MAX
));
1154 fd
= open64(dev_name
, O_RDONLY
);
1160 /* Set offset to read superblock */
1161 ret
= lseek64(fd
, SUPERBLOCK_OFFSET
, SEEK_SET
);
1165 ret
= read(fd
, sb
, sizeof(struct ext4_super_block
));
1177 * get_best_count() - Get the file best extents count.
1179 * @block_count: the file's physical block count.
1181 static int get_best_count(ext4_fsblk_t block_count
)
1184 unsigned int flex_bg_num
;
1186 /* Calcuate best extents count */
1187 if (feature_incompat
& EXT4_FEATURE_INCOMPAT_FLEX_BG
) {
1188 flex_bg_num
= 1 << log_groups_per_flex
;
1189 ret
= ((block_count
- 1) /
1190 ((ext4_fsblk_t
)blocks_per_group
*
1193 ret
= ((block_count
- 1) / blocks_per_group
) + 1;
1200 * file_statistic() - Get statistic info of the file's fragments.
1202 * @file: the file's name.
1203 * @buf: the pointer of the struct stat64.
1205 * @ftwbuf: the pointer of a struct FTW.
1207 static int file_statistic(const char *file
, const struct stat64
*buf
,
1208 int flag
EXT2FS_ATTR((unused
)),
1209 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1213 int now_ext_count
, best_ext_count
= 0, physical_ext_count
;
1216 ext4_fsblk_t blk_count
= 0;
1217 char msg_buffer
[PATH_MAX
+ 24];
1218 struct fiemap_extent_list
*physical_list_head
= NULL
;
1219 struct fiemap_extent_list
*logical_list_head
= NULL
;
1221 defraged_file_count
++;
1223 if (mode_flag
& DETAIL
) {
1224 if (total_count
== 1 && regular_count
== 1)
1227 printf("[%u/%u]", defraged_file_count
, total_count
);
1231 if (lost_found_dir
[0] != '\0' &&
1232 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1233 if (mode_flag
& DETAIL
) {
1234 PRINT_FILE_NAME(file
);
1235 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND
);
1240 if (!S_ISREG(buf
->st_mode
)) {
1241 if (mode_flag
& DETAIL
) {
1242 PRINT_FILE_NAME(file
);
1243 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG
);
1248 /* Access authority */
1249 if (current_uid
!= ROOT_UID
&&
1250 buf
->st_uid
!= current_uid
) {
1251 if (mode_flag
& DETAIL
) {
1252 PRINT_FILE_NAME(file
);
1254 "File is not current user's file"
1255 " or current user is not root");
1261 if (buf
->st_size
== 0) {
1262 if (mode_flag
& DETAIL
) {
1263 PRINT_FILE_NAME(file
);
1264 STATISTIC_ERR_MSG("File size is 0");
1270 if (buf
->st_blocks
== 0) {
1271 if (mode_flag
& DETAIL
) {
1272 PRINT_FILE_NAME(file
);
1273 STATISTIC_ERR_MSG("File has no blocks");
1278 fd
= open64(file
, O_RDONLY
);
1280 if (mode_flag
& DETAIL
) {
1281 PRINT_FILE_NAME(file
);
1282 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1287 /* Get file's physical extents */
1288 ret
= get_file_extents(fd
, &physical_list_head
);
1290 if (mode_flag
& DETAIL
) {
1291 PRINT_FILE_NAME(file
);
1292 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1297 /* Get the count of file's continuous physical region */
1298 physical_ext_count
= get_physical_count(physical_list_head
);
1300 /* Change list from physical to logical */
1301 ret
= change_physical_to_logical(&physical_list_head
,
1302 &logical_list_head
);
1304 if (mode_flag
& DETAIL
) {
1305 PRINT_FILE_NAME(file
);
1306 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1311 /* Count file fragments before defrag */
1312 now_ext_count
= get_logical_count(logical_list_head
);
1314 if (current_uid
== ROOT_UID
) {
1315 /* Calculate fragment ratio */
1317 SECTOR_TO_BLOCK(buf
->st_blocks
, block_size
);
1319 best_ext_count
= get_best_count(blk_count
);
1321 ratio
= (float)(physical_ext_count
- best_ext_count
) * 100 /
1324 extents_before_defrag
+= now_ext_count
;
1325 extents_after_defrag
+= best_ext_count
;
1326 files_block_count
+= blk_count
;
1329 if (total_count
== 1 && regular_count
== 1) {
1331 if (mode_flag
& DETAIL
) {
1333 struct fiemap_extent_list
*ext_list_tmp
=
1336 /* Print extents info */
1339 printf("[ext %d]:\tstart %llu:\tlogical "
1340 "%llu:\tlen %llu\n", count
,
1341 ext_list_tmp
->data
.physical
,
1342 ext_list_tmp
->data
.logical
,
1343 ext_list_tmp
->data
.len
);
1344 ext_list_tmp
= ext_list_tmp
->next
;
1345 } while (ext_list_tmp
!= logical_list_head
);
1348 printf("%-40s%10s/%-10s%9s\n",
1349 "<File>", "now", "best", "ratio");
1350 if (current_uid
== ROOT_UID
) {
1351 if (strlen(file
) > 40)
1352 printf("%s\n%50d/%-10d%8.2f%%\n",
1353 file
, now_ext_count
,
1354 best_ext_count
, ratio
);
1356 printf("%-40s%10d/%-10d%8.2f%%\n",
1357 file
, now_ext_count
,
1358 best_ext_count
, ratio
);
1360 if (strlen(file
) > 40)
1361 printf("%s\n%50d/%-10s%7s\n",
1362 file
, now_ext_count
,
1365 printf("%-40s%10d/%-10s%7s\n",
1366 file
, now_ext_count
,
1374 if (mode_flag
& DETAIL
) {
1375 /* Print statistic info */
1376 sprintf(msg_buffer
, "[%u/%u]%s",
1377 defraged_file_count
, total_count
, file
);
1378 if (current_uid
== ROOT_UID
) {
1379 if (strlen(msg_buffer
) > 40)
1380 printf("\033[79;0H\033[K%s\n"
1381 "%50d/%-10d%8.2f%%\n",
1382 msg_buffer
, now_ext_count
,
1383 best_ext_count
, ratio
);
1385 printf("\033[79;0H\033[K%-40s"
1386 "%10d/%-10d%8.2f%%\n",
1387 msg_buffer
, now_ext_count
,
1388 best_ext_count
, ratio
);
1390 if (strlen(msg_buffer
) > 40)
1391 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1392 msg_buffer
, now_ext_count
,
1395 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1396 msg_buffer
, now_ext_count
,
1401 for (i
= 0; i
< SHOW_FRAG_FILES
; i
++) {
1402 if (ratio
>= frag_rank
[i
].ratio
) {
1403 for (j
= SHOW_FRAG_FILES
- 1; j
> i
; j
--) {
1404 memcpy(&frag_rank
[j
], &frag_rank
[j
- 1],
1405 sizeof(struct frag_statistic_ino
));
1407 memset(&frag_rank
[i
], 0,
1408 sizeof(struct frag_statistic_ino
));
1409 strncpy(frag_rank
[i
].msg_buffer
, file
,
1410 strnlen(file
, PATH_MAX
));
1411 frag_rank
[i
].now_count
= now_ext_count
;
1412 frag_rank
[i
].best_count
= best_ext_count
;
1413 frag_rank
[i
].ratio
= ratio
;
1422 free_ext(physical_list_head
);
1423 free_ext(logical_list_head
);
1428 * print_progress - Print defrag progress
1431 * @start: logical offset for defrag target file
1432 * @file_size: defrag target filesize
1434 static void print_progress(const char *file
, loff_t start
, loff_t file_size
)
1436 int percent
= (start
* 100) / file_size
;
1437 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1438 defraged_file_count
, total_count
, file
, min(percent
, 100));
1445 * call_defrag() - Execute the defrag program.
1447 * @fd: target file descriptor.
1448 * @donor_fd: donor file descriptor.
1449 * @file: target file name.
1450 * @buf: pointer of the struct stat64.
1451 * @ext_list_head: head of the extent list.
1453 static int call_defrag(int fd
, int donor_fd
, const char *file
,
1454 const struct stat64
*buf
, struct fiemap_extent_list
*ext_list_head
)
1457 unsigned int page_num
;
1458 unsigned char *vec
= NULL
;
1459 int defraged_ret
= 0;
1461 struct move_extent move_data
;
1462 struct fiemap_extent_list
*ext_list_tmp
= NULL
;
1464 memset(&move_data
, 0, sizeof(struct move_extent
));
1465 move_data
.donor_fd
= donor_fd
;
1467 /* Print defrag progress */
1468 print_progress(file
, start
, buf
->st_size
);
1470 ext_list_tmp
= ext_list_head
;
1472 move_data
.orig_start
= ext_list_tmp
->data
.logical
;
1473 /* Logical offset of orig and donor should be same */
1474 move_data
.donor_start
= move_data
.orig_start
;
1475 move_data
.len
= ext_list_tmp
->data
.len
;
1476 move_data
.moved_len
= 0;
1478 ret
= page_in_core(fd
, move_data
, &vec
, &page_num
);
1480 if (mode_flag
& DETAIL
) {
1482 PRINT_ERR_MSG_WITH_ERRNO(
1483 "Failed to get file map");
1485 printf("\t[ NG ]\n");
1490 /* EXT4_IOC_MOVE_EXT */
1492 ioctl(fd
, EXT4_IOC_MOVE_EXT
, &move_data
);
1495 ret
= defrag_fadvise(fd
, move_data
, vec
, page_num
);
1501 if (mode_flag
& DETAIL
) {
1503 PRINT_ERR_MSG_WITH_ERRNO(
1504 "Failed to free page");
1506 printf("\t[ NG ]\n");
1511 if (defraged_ret
< 0) {
1512 if (mode_flag
& DETAIL
) {
1514 PRINT_ERR_MSG_WITH_ERRNO(
1515 "Failed to defrag");
1517 printf("\t[ NG ]\n");
1521 /* Adjust logical offset for next ioctl */
1522 move_data
.orig_start
+= move_data
.moved_len
;
1523 move_data
.donor_start
= move_data
.orig_start
;
1525 start
= move_data
.orig_start
* buf
->st_blksize
;
1527 /* Print defrag progress */
1528 print_progress(file
, start
, buf
->st_size
);
1531 if (start
>= buf
->st_size
)
1534 ext_list_tmp
= ext_list_tmp
->next
;
1535 } while (ext_list_tmp
!= ext_list_head
);
1541 * file_defrag() - Check file attributes and call ioctl to defrag.
1543 * @file: the file's name.
1544 * @buf: the pointer of the struct stat64.
1546 * @ftwbuf: the pointer of a struct FTW.
1548 static int file_defrag(const char *file
, const struct stat64
*buf
,
1549 int flag
EXT2FS_ATTR((unused
)),
1550 struct FTW
*ftwbuf
EXT2FS_ATTR((unused
)))
1556 int file_frags_start
, file_frags_end
;
1557 int orig_physical_cnt
, donor_physical_cnt
= 0;
1558 char tmp_inode_name
[PATH_MAX
+ 8];
1559 struct fiemap_extent_list
*orig_list_physical
= NULL
;
1560 struct fiemap_extent_list
*orig_list_logical
= NULL
;
1561 struct fiemap_extent_list
*donor_list_physical
= NULL
;
1562 struct fiemap_extent_list
*donor_list_logical
= NULL
;
1563 struct fiemap_extent_group
*orig_group_head
= NULL
;
1564 struct fiemap_extent_group
*orig_group_tmp
= NULL
;
1566 defraged_file_count
++;
1568 if (mode_flag
& DETAIL
) {
1569 printf("[%u/%u]", defraged_file_count
, total_count
);
1573 if (lost_found_dir
[0] != '\0' &&
1574 !memcmp(file
, lost_found_dir
, strnlen(lost_found_dir
, PATH_MAX
))) {
1575 if (mode_flag
& DETAIL
) {
1576 PRINT_FILE_NAME(file
);
1577 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1582 if (!S_ISREG(buf
->st_mode
)) {
1583 if (mode_flag
& DETAIL
) {
1584 PRINT_FILE_NAME(file
);
1585 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1591 if (buf
->st_size
== 0) {
1592 if (mode_flag
& DETAIL
) {
1593 PRINT_FILE_NAME(file
);
1594 IN_FTW_PRINT_ERR_MSG("File size is 0");
1600 if (buf
->st_blocks
== 0) {
1601 if (mode_flag
& DETAIL
) {
1602 PRINT_FILE_NAME(file
);
1603 STATISTIC_ERR_MSG("File has no blocks");
1608 fd
= open64(file
, O_RDONLY
);
1610 if (mode_flag
& DETAIL
) {
1611 PRINT_FILE_NAME(file
);
1612 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1617 /* Get file's extents */
1618 ret
= get_file_extents(fd
, &orig_list_physical
);
1620 if (mode_flag
& DETAIL
) {
1621 PRINT_FILE_NAME(file
);
1622 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1627 /* Get the count of file's continuous physical region */
1628 orig_physical_cnt
= get_physical_count(orig_list_physical
);
1630 /* Change list from physical to logical */
1631 ret
= change_physical_to_logical(&orig_list_physical
,
1632 &orig_list_logical
);
1634 if (mode_flag
& DETAIL
) {
1635 PRINT_FILE_NAME(file
);
1636 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1641 /* Count file fragments before defrag */
1642 file_frags_start
= get_logical_count(orig_list_logical
);
1644 if (file_check(fd
, buf
, file
, file_frags_start
) < 0)
1647 if (fsync(fd
) < 0) {
1648 if (mode_flag
& DETAIL
) {
1649 PRINT_FILE_NAME(file
);
1650 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1655 if (current_uid
== ROOT_UID
)
1657 get_best_count(SECTOR_TO_BLOCK(buf
->st_blocks
, block_size
));
1661 if (file_frags_start
<= best
)
1662 goto check_improvement
;
1664 /* Combine extents to group */
1665 ret
= join_extents(orig_list_logical
, &orig_group_head
);
1667 if (mode_flag
& DETAIL
) {
1668 PRINT_FILE_NAME(file
);
1669 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1674 /* Create donor inode */
1675 memset(tmp_inode_name
, 0, PATH_MAX
+ 8);
1676 sprintf(tmp_inode_name
, "%.*s.defrag",
1677 (int)strnlen(file
, PATH_MAX
), file
);
1678 donor_fd
= open64(tmp_inode_name
, O_WRONLY
| O_CREAT
| O_EXCL
, S_IRUSR
);
1680 if (mode_flag
& DETAIL
) {
1681 PRINT_FILE_NAME(file
);
1682 if (errno
== EEXIST
)
1683 PRINT_ERR_MSG_WITH_ERRNO(
1684 "File is being defraged by other program");
1686 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN
);
1691 /* Unlink donor inode */
1692 ret
= unlink(tmp_inode_name
);
1694 if (mode_flag
& DETAIL
) {
1695 PRINT_FILE_NAME(file
);
1696 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1701 /* Allocate space for donor inode */
1702 orig_group_tmp
= orig_group_head
;
1704 ret
= fallocate(donor_fd
, 0,
1705 (loff_t
)orig_group_tmp
->start
->data
.logical
* block_size
,
1706 (loff_t
)orig_group_tmp
->len
* block_size
);
1708 if (mode_flag
& DETAIL
) {
1709 PRINT_FILE_NAME(file
);
1710 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1715 orig_group_tmp
= orig_group_tmp
->next
;
1716 } while (orig_group_tmp
!= orig_group_head
);
1718 /* Get donor inode's extents */
1719 ret
= get_file_extents(donor_fd
, &donor_list_physical
);
1721 if (mode_flag
& DETAIL
) {
1722 PRINT_FILE_NAME(file
);
1723 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1728 /* Calcuate donor inode's continuous physical region */
1729 donor_physical_cnt
= get_physical_count(donor_list_physical
);
1731 /* Change donor extent list from physical to logical */
1732 ret
= change_physical_to_logical(&donor_list_physical
,
1733 &donor_list_logical
);
1735 if (mode_flag
& DETAIL
) {
1736 PRINT_FILE_NAME(file
);
1737 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT
);
1743 if (mode_flag
& DETAIL
) {
1744 if (file_frags_start
!= 1)
1745 frag_files_before_defrag
++;
1747 extents_before_defrag
+= file_frags_start
;
1750 if (file_frags_start
<= best
||
1751 orig_physical_cnt
<= donor_physical_cnt
) {
1752 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1753 defraged_file_count
, total_count
, file
, 100);
1754 if (mode_flag
& DETAIL
)
1755 printf(" extents: %d -> %d",
1756 file_frags_start
, file_frags_start
);
1758 printf("\t[ OK ]\n");
1761 if (file_frags_start
!= 1)
1762 frag_files_after_defrag
++;
1764 extents_after_defrag
+= file_frags_start
;
1768 /* Defrag the file */
1769 ret
= call_defrag(fd
, donor_fd
, file
, buf
, donor_list_logical
);
1771 /* Count file fragments after defrag and print extents info */
1772 if (mode_flag
& DETAIL
) {
1773 file_frags_end
= file_frag_count(fd
);
1774 if (file_frags_end
< 0) {
1776 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO
);
1780 if (file_frags_end
!= 1)
1781 frag_files_after_defrag
++;
1783 extents_after_defrag
+= file_frags_end
;
1788 printf(" extents: %d -> %d",
1789 file_frags_start
, file_frags_end
);
1796 printf("\t[ OK ]\n");
1804 free_ext(orig_list_physical
);
1805 free_ext(orig_list_logical
);
1806 free_ext(donor_list_physical
);
1807 free_exts_group(orig_group_head
);
1812 * main() - Ext4 online defrag.
1814 * @argc: the number of parameter.
1815 * @argv[]: the pointer array of parameter.
1817 int main(int argc
, char *argv
[])
1821 int flags
= FTW_PHYS
| FTW_MOUNT
;
1823 int success_flag
= 0;
1824 char dir_name
[PATH_MAX
+ 1];
1826 struct ext4_super_block sb
;
1828 /* Parse arguments */
1832 while ((opt
= getopt(argc
, argv
, "vc")) != EOF
) {
1835 mode_flag
|= DETAIL
;
1838 mode_flag
|= STATISTIC
;
1848 current_uid
= getuid();
1851 for (i
= optind
; i
< argc
; i
++) {
1855 frag_files_before_defrag
= 0;
1856 frag_files_after_defrag
= 0;
1857 extents_before_defrag
= 0;
1858 extents_after_defrag
= 0;
1859 defraged_file_count
= 0;
1860 files_block_count
= 0;
1861 blocks_per_group
= 0;
1862 feature_incompat
= 0;
1863 log_groups_per_flex
= 0;
1865 memset(dir_name
, 0, PATH_MAX
+ 1);
1866 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1867 memset(frag_rank
, 0,
1868 sizeof(struct frag_statistic_ino
) * SHOW_FRAG_FILES
);
1870 if ((mode_flag
& STATISTIC
) && i
> optind
)
1873 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1874 PRINT_ERR_MSG("Endian's type is not big/little endian");
1875 PRINT_FILE_NAME(argv
[i
]);
1879 if (lstat64(argv
[i
], &buf
) < 0) {
1880 perror(NGMSG_FILE_INFO
);
1881 PRINT_FILE_NAME(argv
[i
]);
1885 if (S_ISBLK(buf
.st_mode
)) {
1887 if (get_mount_point(argv
[i
], dir_name
, PATH_MAX
) < 0)
1889 if (lstat64(dir_name
, &buf
) < 0) {
1890 perror(NGMSG_FILE_INFO
);
1891 PRINT_FILE_NAME(argv
[i
]);
1895 if (!(mode_flag
& STATISTIC
))
1896 printf("ext4 defragmentation for device(%s)\n",
1898 } else if (S_ISDIR(buf
.st_mode
)) {
1900 if (access(argv
[i
], R_OK
) < 0) {
1905 strncpy(dir_name
, argv
[i
], strnlen(argv
[i
], PATH_MAX
));
1906 } else if (S_ISREG(buf
.st_mode
)) {
1908 arg_type
= FILENAME
;
1910 /* Irregular file */
1911 PRINT_ERR_MSG(NGMSG_FILE_UNREG
);
1912 PRINT_FILE_NAME(argv
[i
]);
1917 block_size
= buf
.st_blksize
;
1920 * filesystem type checked in get_mount_point()
1922 if (arg_type
== FILENAME
|| arg_type
== DIRNAME
) {
1923 if (is_ext4(argv
[i
]) < 0)
1925 if (realpath(argv
[i
], dir_name
) == NULL
) {
1926 perror("Couldn't get full path");
1927 PRINT_FILE_NAME(argv
[i
]);
1932 if (current_uid
== ROOT_UID
) {
1933 /* Get super block info */
1934 memset(&sb
, 0, sizeof(struct ext4_super_block
));
1935 if (get_superblock_info(dir_name
, &sb
) < 0) {
1936 if (mode_flag
& DETAIL
) {
1937 perror("Can't get super block info");
1938 PRINT_FILE_NAME(argv
[i
]);
1943 blocks_per_group
= ext2fs_swab32(sb
.s_blocks_per_group
);
1944 feature_incompat
= ext2fs_swab32(sb
.s_feature_incompat
);
1945 log_groups_per_flex
= sb
.s_log_groups_per_flex
;
1950 if (!(mode_flag
& STATISTIC
))
1951 printf("ext4 defragmentation "
1952 "for directory(%s)\n", argv
[i
]);
1954 int mount_dir_len
= 0;
1955 mount_dir_len
= strnlen(lost_found_dir
, PATH_MAX
);
1957 strncat(lost_found_dir
, "/lost+found",
1958 PATH_MAX
- strnlen(lost_found_dir
, PATH_MAX
));
1960 /* Not the case("e4defrag mount_piont_dir") */
1961 if (dir_name
[mount_dir_len
] != '\0') {
1963 * "e4defrag mount_piont_dir/lost+found"
1964 * or "e4defrag mount_piont_dir/lost+found/"
1966 if (strncmp(lost_found_dir
, dir_name
,
1967 strnlen(lost_found_dir
,
1969 (dir_name
[strnlen(lost_found_dir
,
1970 PATH_MAX
)] == '\0' ||
1971 dir_name
[strnlen(lost_found_dir
,
1972 PATH_MAX
)] == '/')) {
1973 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
1974 PRINT_FILE_NAME(argv
[i
]);
1978 /* "e4defrag mount_piont_dir/else_dir" */
1979 memset(lost_found_dir
, 0, PATH_MAX
+ 1);
1982 if (arg_type
== DEVNAME
) {
1983 strncpy(lost_found_dir
, dir_name
,
1984 strnlen(dir_name
, PATH_MAX
));
1985 strncat(lost_found_dir
, "/lost+found/",
1986 PATH_MAX
- strnlen(lost_found_dir
,
1990 nftw64(dir_name
, calc_entry_counts
, FTW_OPEN_FD
, flags
);
1992 if (mode_flag
& STATISTIC
) {
1993 if (mode_flag
& DETAIL
)
1994 printf("%-40s%10s/%-10s%9s\n",
1995 "<File>", "now", "best", "ratio");
1997 if (!(mode_flag
& DETAIL
) &&
1998 current_uid
!= ROOT_UID
) {
2003 nftw64(dir_name
, file_statistic
,
2004 FTW_OPEN_FD
, flags
);
2006 if (succeed_cnt
!= 0 &&
2007 current_uid
== ROOT_UID
) {
2008 if (mode_flag
& DETAIL
)
2010 printf("%-40s%10s/%-10s%9s\n",
2011 "<Fragmented files>", "now",
2013 for (j
= 0; j
< SHOW_FRAG_FILES
; j
++) {
2014 if (strlen(frag_rank
[j
].
2016 printf("%d. %s\n%50d/"
2017 "%-10d%8.2f%%\n", j
+ 1,
2018 frag_rank
[j
].msg_buffer
,
2019 frag_rank
[j
].now_count
,
2020 frag_rank
[j
].best_count
,
2021 frag_rank
[j
].ratio
);
2022 } else if (strlen(frag_rank
[j
].
2024 printf("%d. %-37s%10d/"
2025 "%-10d%8.2f%%\n", j
+ 1,
2026 frag_rank
[j
].msg_buffer
,
2027 frag_rank
[j
].now_count
,
2028 frag_rank
[j
].best_count
,
2029 frag_rank
[j
].ratio
);
2036 /* File tree walk */
2037 nftw64(dir_name
, file_defrag
, FTW_OPEN_FD
, flags
);
2038 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt
,
2040 printf("\tFailure:\t\t\t[ %u/%u ]\n",
2041 total_count
- succeed_cnt
, total_count
);
2042 if (mode_flag
& DETAIL
) {
2043 printf("\tTotal extents:\t\t\t%4d->%d\n",
2044 extents_before_defrag
,
2045 extents_after_defrag
);
2046 printf("\tFragmented percentage:\t\t"
2047 "%3llu%%->%llu%%\n",
2048 !regular_count
? 0 :
2049 ((unsigned long long)
2050 frag_files_before_defrag
* 100) /
2052 !regular_count
? 0 :
2053 ((unsigned long long)
2054 frag_files_after_defrag
* 100) /
2061 strncat(lost_found_dir
, "/lost+found/",
2062 PATH_MAX
- strnlen(lost_found_dir
,
2064 if (strncmp(lost_found_dir
, dir_name
,
2065 strnlen(lost_found_dir
,
2067 PRINT_ERR_MSG(NGMSG_LOST_FOUND
);
2068 PRINT_FILE_NAME(argv
[i
]);
2072 if (mode_flag
& STATISTIC
) {
2073 file_statistic(argv
[i
], &buf
, FTW_F
, NULL
);
2076 printf("ext4 defragmentation for %s\n",
2078 /* Defrag single file process */
2079 file_defrag(argv
[i
], &buf
, FTW_F
, NULL
);
2080 if (succeed_cnt
!= 0)
2081 printf(" Success:\t\t\t[1/1]\n");
2083 printf(" Success:\t\t\t[0/1]\n");
2088 if (succeed_cnt
!= 0)
2090 if (mode_flag
& STATISTIC
) {
2091 if (current_uid
!= ROOT_UID
) {
2097 if (mode_flag
& DETAIL
)
2100 if (arg_type
== DEVNAME
)
2101 printf(" In this device(%s), "
2102 "none can be defragmented.\n", argv
[i
]);
2103 else if (arg_type
== DIRNAME
)
2104 printf(" In this directory(%s), "
2105 "none can be defragmented.\n", argv
[i
]);
2107 printf(" This file(%s) "
2108 "can't be defragmented.\n", argv
[i
]);
2110 float files_ratio
= 0.0;
2112 files_ratio
= (float)(extents_before_defrag
-
2113 extents_after_defrag
) *
2114 100 / files_block_count
;
2115 score
= CALC_SCORE(files_ratio
);
2116 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2117 " Fragmentation ratio\t\t\t\t%.2f%%\n"
2118 " Fragmentation score\t\t\t\t%.2f\n",
2119 extents_before_defrag
,
2120 extents_after_defrag
,
2121 files_ratio
, score
);
2122 printf(" [0-30 no problem:"
2123 " 31-55 a little bit fragmented:"
2124 " 55- needs defrag]\n");
2126 if (arg_type
== DEVNAME
)
2127 printf(" This device(%s) ", argv
[i
]);
2128 else if (arg_type
== DIRNAME
)
2129 printf(" This directory(%s) ", argv
[i
]);
2131 printf(" This file(%s) ", argv
[i
]);
2133 if (score
> BOUND_SCORE
)
2134 printf("needs defragmentation.\n");
2136 printf("does not need "
2137 "defragmentation.\n");