Merge branch 'debian/master' into debian/backports
[e2fsprogs.git] / misc / e4defrag.c
blob86e97ee3ae40b014375cb91983df1e2155e7a832
1 /*
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>
8 */
10 #ifndef _LARGEFILE_SOURCE
11 #define _LARGEFILE_SOURCE
12 #endif
14 #ifndef _LARGEFILE64_SOURCE
15 #define _LARGEFILE64_SOURCE
16 #endif
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
22 #include "config.h"
23 #include <ctype.h>
24 #include <dirent.h>
25 #include <endian.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <ftw.h>
29 #include <limits.h>
30 #include <mntent.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <ext2fs/ext2_types.h>
36 #include <ext2fs/ext2fs.h>
37 #include <sys/ioctl.h>
38 #include <ext2fs/fiemap.h>
39 #include <sys/mman.h>
40 #include <sys/stat.h>
41 #include <sys/statfs.h>
42 #include <sys/vfs.h>
44 #include "../version.h"
46 /* A relatively new ioctl interface ... */
47 #ifndef EXT4_IOC_MOVE_EXT
48 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
49 #endif
51 /* Macro functions */
52 #define PRINT_ERR_MSG(msg) fprintf(stderr, "%s\n", (msg))
53 #define IN_FTW_PRINT_ERR_MSG(msg) \
54 fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
55 #define PRINT_FILE_NAME(file) fprintf(stderr, " \"%s\"\n", (file))
56 #define PRINT_ERR_MSG_WITH_ERRNO(msg) \
57 fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
58 #define STATISTIC_ERR_MSG(msg) \
59 fprintf(stderr, "\t%s\n", (msg))
60 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg) \
61 fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
62 #define min(x, y) (((x) > (y)) ? (y) : (x))
63 #define CALC_SCORE(ratio) \
64 ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
65 /* Wrap up the free function */
66 #define FREE(tmp) \
67 do { \
68 if ((tmp) != NULL) \
69 free(tmp); \
70 } while (0) \
71 /* Insert list2 after list1 */
72 #define insert(list1, list2) \
73 do { \
74 list2->next = list1->next; \
75 list1->next->prev = list2; \
76 list2->prev = list1; \
77 list1->next = list2; \
78 } while (0)
80 /* To delete unused warning */
81 #ifdef __GNUC__
82 #define EXT2FS_ATTR(x) __attribute__(x)
83 #else
84 #define EXT2FS_ATTR(x)
85 #endif
87 /* The mode of defrag */
88 #define DETAIL 0x01
89 #define STATISTIC 0x02
91 #define DEVNAME 0
92 #define DIRNAME 1
93 #define FILENAME 2
95 #define FTW_OPEN_FD 2000
97 #define FS_EXT4 "ext4"
98 #define ROOT_UID 0
100 #define BOUND_SCORE 55
101 #define SHOW_FRAG_FILES 5
103 /* Magic number for ext4 */
104 #define EXT4_SUPER_MAGIC 0xEF53
106 /* Definition of flex_bg */
107 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
109 /* The following macro is used for ioctl FS_IOC_FIEMAP
110 * EXTENT_MAX_COUNT: the maximum number of extents for exchanging between
111 * kernel-space and user-space per ioctl
113 #define EXTENT_MAX_COUNT 512
115 /* The following macros are error message */
116 #define MSG_USAGE \
117 "Usage : e4defrag [-v] file...| directory...| device...\n\
118 : e4defrag -c file...| directory...| device...\n"
120 #define NGMSG_EXT4 "Filesystem is not ext4 filesystem"
121 #define NGMSG_FILE_EXTENT "Failed to get file extents"
122 #define NGMSG_FILE_INFO "Failed to get file information"
123 #define NGMSG_FILE_OPEN "Failed to open"
124 #define NGMSG_FILE_UNREG "File is not regular file"
125 #define NGMSG_LOST_FOUND "Can not process \"lost+found\""
127 /* Data type for filesystem-wide blocks number */
128 typedef unsigned long long ext4_fsblk_t;
130 struct fiemap_extent_data {
131 __u64 len; /* blocks count */
132 __u64 logical; /* start logical block number */
133 ext4_fsblk_t physical; /* start physical block number */
136 struct fiemap_extent_list {
137 struct fiemap_extent_list *prev;
138 struct fiemap_extent_list *next;
139 struct fiemap_extent_data data; /* extent belong to file */
142 struct fiemap_extent_group {
143 struct fiemap_extent_group *prev;
144 struct fiemap_extent_group *next;
145 __u64 len; /* length of this continuous region */
146 struct fiemap_extent_list *start; /* start ext */
147 struct fiemap_extent_list *end; /* end ext */
150 struct move_extent {
151 __s32 reserved; /* original file descriptor */
152 __u32 donor_fd; /* donor file descriptor */
153 __u64 orig_start; /* logical start offset in block for orig */
154 __u64 donor_start; /* logical start offset in block for donor */
155 __u64 len; /* block length to be moved */
156 __u64 moved_len; /* moved block length */
159 struct frag_statistic_ino {
160 int now_count; /* the file's extents count of before defrag */
161 int best_count; /* the best file's extents count */
162 __u64 size_per_ext; /* size(KB) per extent */
163 float ratio; /* the ratio of fragmentation */
164 char msg_buffer[PATH_MAX + 1]; /* pathname of the file */
167 static char lost_found_dir[PATH_MAX + 1];
168 static int block_size;
169 static int extents_before_defrag;
170 static int extents_after_defrag;
171 static int mode_flag;
172 static unsigned int current_uid;
173 static unsigned int defraged_file_count;
174 static unsigned int frag_files_before_defrag;
175 static unsigned int frag_files_after_defrag;
176 static unsigned int regular_count;
177 static unsigned int succeed_cnt;
178 static unsigned int total_count;
179 static __u8 log_groups_per_flex;
180 static __u32 blocks_per_group;
181 static __u32 feature_incompat;
182 static ext4_fsblk_t files_block_count;
183 static struct frag_statistic_ino frag_rank[SHOW_FRAG_FILES];
187 * We prefer posix_fadvise64 when available, as it allows 64bit offset on
188 * 32bit systems
190 #if defined(HAVE_POSIX_FADVISE64)
191 #define posix_fadvise posix_fadvise64
192 #elif defined(HAVE_FADVISE64)
193 #define posix_fadvise fadvise64
194 #elif !defined(HAVE_POSIX_FADVISE)
195 #error posix_fadvise not available!
196 #endif
198 #ifndef HAVE_FALLOCATE64
199 #error fallocate64 not available!
200 #endif /* ! HAVE_FALLOCATE64 */
203 * get_mount_point() - Get device's mount point.
205 * @devname: the device's name.
206 * @mount_point: the mount point.
207 * @dir_path_len: the length of directory.
209 static int get_mount_point(const char *devname, char *mount_point,
210 int dir_path_len)
212 /* Refer to /etc/mtab */
213 const char *mtab = MOUNTED;
214 FILE *fp = NULL;
215 struct mntent *mnt = NULL;
216 struct stat64 sb;
218 if (stat64(devname, &sb) < 0) {
219 perror(NGMSG_FILE_INFO);
220 PRINT_FILE_NAME(devname);
221 return -1;
224 fp = setmntent(mtab, "r");
225 if (fp == NULL) {
226 perror("Couldn't access /etc/mtab");
227 return -1;
230 while ((mnt = getmntent(fp)) != NULL) {
231 struct stat64 ms;
234 * To handle device symlinks, we see if the
235 * device number matches, not the name
237 if (stat64(mnt->mnt_fsname, &ms) < 0)
238 continue;
239 if (sb.st_rdev != ms.st_rdev)
240 continue;
242 endmntent(fp);
243 if (strcmp(mnt->mnt_type, FS_EXT4) == 0) {
244 strncpy(mount_point, mnt->mnt_dir,
245 dir_path_len);
246 return 0;
248 PRINT_ERR_MSG(NGMSG_EXT4);
249 return -1;
251 endmntent(fp);
252 PRINT_ERR_MSG("Filesystem is not mounted");
253 return -1;
257 * is_ext4() - Whether on an ext4 filesystem.
259 * @file: the file's name.
261 static int is_ext4(const char *file, char *devname)
263 int maxlen = 0;
264 int len, ret;
265 FILE *fp = NULL;
266 char *mnt_type = NULL;
267 /* Refer to /etc/mtab */
268 const char *mtab = MOUNTED;
269 char file_path[PATH_MAX + 1];
270 struct mntent *mnt = NULL;
271 struct statfs64 fsbuf;
273 /* Get full path */
274 if (realpath(file, file_path) == NULL) {
275 perror("Couldn't get full path");
276 PRINT_FILE_NAME(file);
277 return -1;
280 if (statfs64(file_path, &fsbuf) < 0) {
281 perror("Failed to get filesystem information");
282 PRINT_FILE_NAME(file);
283 return -1;
286 if (fsbuf.f_type != EXT4_SUPER_MAGIC) {
287 PRINT_ERR_MSG(NGMSG_EXT4);
288 return -1;
291 fp = setmntent(mtab, "r");
292 if (fp == NULL) {
293 perror("Couldn't access /etc/mtab");
294 return -1;
297 while ((mnt = getmntent(fp)) != NULL) {
298 if (mnt->mnt_fsname[0] != '/')
299 continue;
300 len = strlen(mnt->mnt_dir);
301 ret = memcmp(file_path, mnt->mnt_dir, len);
302 if (ret != 0)
303 continue;
305 if (maxlen >= len)
306 continue;
308 maxlen = len;
310 mnt_type = realloc(mnt_type, strlen(mnt->mnt_type) + 1);
311 if (mnt_type == NULL) {
312 endmntent(fp);
313 return -1;
315 memset(mnt_type, 0, strlen(mnt->mnt_type) + 1);
316 strncpy(mnt_type, mnt->mnt_type, strlen(mnt->mnt_type));
317 strncpy(lost_found_dir, mnt->mnt_dir, PATH_MAX);
318 strncpy(devname, mnt->mnt_fsname, strlen(mnt->mnt_fsname) + 1);
321 endmntent(fp);
322 if (mnt_type && strcmp(mnt_type, FS_EXT4) == 0) {
323 FREE(mnt_type);
324 return 0;
325 } else {
326 FREE(mnt_type);
327 PRINT_ERR_MSG(NGMSG_EXT4);
328 return -1;
333 * calc_entry_counts() - Calculate file counts.
335 * @file: file name.
336 * @buf: file info.
337 * @flag: file type.
338 * @ftwbuf: the pointer of a struct FTW.
340 static int calc_entry_counts(const char *file EXT2FS_ATTR((unused)),
341 const struct stat64 *buf, int flag EXT2FS_ATTR((unused)),
342 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
344 if (S_ISREG(buf->st_mode))
345 regular_count++;
347 total_count++;
349 return 0;
353 * page_in_core() - Get information on whether pages are in core.
355 * @fd: defrag target file's descriptor.
356 * @defrag_data: data used for defrag.
357 * @vec: page state array.
358 * @page_num: page number.
360 static int page_in_core(int fd, struct move_extent defrag_data,
361 unsigned char **vec, unsigned int *page_num)
363 long pagesize;
364 void *page = NULL;
365 ext2_loff_t offset, end_offset, length;
367 if (vec == NULL || *vec != NULL)
368 return -1;
370 pagesize = sysconf(_SC_PAGESIZE);
371 if (pagesize < 0)
372 return -1;
373 /* In mmap, offset should be a multiple of the page size */
374 offset = (ext2_loff_t)defrag_data.orig_start * block_size;
375 length = (ext2_loff_t)defrag_data.len * block_size;
376 end_offset = offset + length;
377 /* Round the offset down to the nearest multiple of pagesize */
378 offset = (offset / pagesize) * pagesize;
379 length = end_offset - offset;
381 page = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, offset);
382 if (page == MAP_FAILED)
383 return -1;
385 *page_num = 0;
386 *page_num = (length + pagesize - 1) / pagesize;
387 *vec = (unsigned char *)calloc(*page_num, 1);
388 if (*vec == NULL) {
389 munmap(page, length);
390 return -1;
393 /* Get information on whether pages are in core */
394 if (mincore(page, (size_t)length, *vec) == -1 ||
395 munmap(page, length) == -1) {
396 FREE(*vec);
397 return -1;
400 return 0;
404 * defrag_fadvise() - Predeclare an access pattern for file data.
406 * @fd: defrag target file's descriptor.
407 * @defrag_data: data used for defrag.
408 * @vec: page state array.
409 * @page_num: page number.
411 static int defrag_fadvise(int fd, struct move_extent defrag_data,
412 unsigned char *vec, unsigned int page_num)
414 int flag = 1;
415 long pagesize = sysconf(_SC_PAGESIZE);
416 int fadvise_flag = POSIX_FADV_DONTNEED;
417 int sync_flag = SYNC_FILE_RANGE_WAIT_BEFORE |
418 SYNC_FILE_RANGE_WRITE |
419 SYNC_FILE_RANGE_WAIT_AFTER;
420 unsigned int i;
421 ext2_loff_t offset;
423 if (pagesize < 1)
424 return -1;
426 offset = (ext2_loff_t)defrag_data.orig_start * block_size;
427 offset = (offset / pagesize) * pagesize;
429 #ifdef HAVE_SYNC_FILE_RANGE
430 /* Sync file for fadvise process */
431 if (sync_file_range(fd, offset,
432 (ext2_loff_t)pagesize * page_num, sync_flag) < 0)
433 return -1;
434 #endif
436 /* Try to release buffer cache which this process used,
437 * then other process can use the released buffer
439 for (i = 0; i < page_num; i++) {
440 if ((vec[i] & 0x1) == 0) {
441 offset += pagesize;
442 continue;
444 if ((errno = posix_fadvise(fd, offset,
445 pagesize, fadvise_flag)) != 0) {
446 if ((mode_flag & DETAIL) && flag) {
447 perror("\tFailed to fadvise");
448 flag = 0;
451 offset += pagesize;
454 return 0;
458 * check_free_size() - Check if there's enough disk space.
460 * @fd: defrag target file's descriptor.
461 * @file: file name.
462 * @blk_count: file blocks.
464 static int check_free_size(int fd, const char *file, ext4_fsblk_t blk_count)
466 ext4_fsblk_t free_blk_count;
467 struct statfs64 fsbuf;
469 if (fstatfs64(fd, &fsbuf) < 0) {
470 if (mode_flag & DETAIL) {
471 PRINT_FILE_NAME(file);
472 PRINT_ERR_MSG_WITH_ERRNO(
473 "Failed to get filesystem information");
475 return -1;
478 /* Compute free space for root and normal user separately */
479 if (current_uid == ROOT_UID)
480 free_blk_count = fsbuf.f_bfree;
481 else
482 free_blk_count = fsbuf.f_bavail;
484 if (free_blk_count >= blk_count)
485 return 0;
487 return -ENOSPC;
491 * file_frag_count() - Get file fragment count.
493 * @fd: defrag target file's descriptor.
495 static int file_frag_count(int fd)
497 int ret;
498 struct fiemap fiemap_buf;
500 /* When fm_extent_count is 0,
501 * ioctl just get file fragment count.
503 memset(&fiemap_buf, 0, sizeof(struct fiemap));
504 fiemap_buf.fm_start = 0;
505 fiemap_buf.fm_length = FIEMAP_MAX_OFFSET;
506 fiemap_buf.fm_flags |= FIEMAP_FLAG_SYNC;
508 ret = ioctl(fd, FS_IOC_FIEMAP, &fiemap_buf);
509 if (ret < 0)
510 return ret;
512 return fiemap_buf.fm_mapped_extents;
516 * file_check() - Check file's attributes.
518 * @fd: defrag target file's descriptor.
519 * @buf: a pointer of the struct stat64.
520 * @file: file name.
521 * @extents: file extents.
522 * @blk_count: file blocks.
524 static int file_check(int fd, const struct stat64 *buf, const char *file,
525 int extents, ext4_fsblk_t blk_count)
527 int ret;
528 struct flock lock;
530 /* Write-lock check is more reliable */
531 lock.l_type = F_WRLCK;
532 lock.l_start = 0;
533 lock.l_whence = SEEK_SET;
534 lock.l_len = 0;
536 /* Free space */
537 ret = check_free_size(fd, file, blk_count);
538 if (ret < 0) {
539 if ((mode_flag & DETAIL) && ret == -ENOSPC) {
540 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
541 " extents: %d -> %d\n", defraged_file_count,
542 total_count, file, extents, extents);
543 IN_FTW_PRINT_ERR_MSG(
544 "Defrag size is larger than filesystem's free space");
546 return -1;
549 /* Access authority */
550 if (current_uid != ROOT_UID &&
551 buf->st_uid != current_uid) {
552 if (mode_flag & DETAIL) {
553 printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
554 " extents: %d -> %d\n", defraged_file_count,
555 total_count, file, extents, extents);
556 IN_FTW_PRINT_ERR_MSG(
557 "File is not current user's file"
558 " or current user is not root");
560 return -1;
563 /* Lock status */
564 if (fcntl(fd, F_GETLK, &lock) < 0) {
565 if (mode_flag & DETAIL) {
566 PRINT_FILE_NAME(file);
567 PRINT_ERR_MSG_WITH_ERRNO(
568 "Failed to get lock information");
570 return -1;
571 } else if (lock.l_type != F_UNLCK) {
572 if (mode_flag & DETAIL) {
573 PRINT_FILE_NAME(file);
574 IN_FTW_PRINT_ERR_MSG("File has been locked");
576 return -1;
579 return 0;
583 * insert_extent_by_logical() - Sequentially insert extent by logical.
585 * @ext_list_head: the head of logical extent list.
586 * @ext: the extent element which will be inserted.
588 static int insert_extent_by_logical(struct fiemap_extent_list **ext_list_head,
589 struct fiemap_extent_list *ext)
591 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
593 if (ext == NULL)
594 goto out;
596 /* First element */
597 if (*ext_list_head == NULL) {
598 (*ext_list_head) = ext;
599 (*ext_list_head)->prev = *ext_list_head;
600 (*ext_list_head)->next = *ext_list_head;
601 return 0;
604 if (ext->data.logical <= ext_list_tmp->data.logical) {
605 /* Insert before head */
606 if (ext_list_tmp->data.logical <
607 ext->data.logical + ext->data.len)
608 /* Overlap */
609 goto out;
610 /* Adjust head */
611 *ext_list_head = ext;
612 } else {
613 /* Insert into the middle or last of the list */
614 do {
615 if (ext->data.logical < ext_list_tmp->data.logical)
616 break;
617 ext_list_tmp = ext_list_tmp->next;
618 } while (ext_list_tmp != (*ext_list_head));
619 if (ext->data.logical <
620 ext_list_tmp->prev->data.logical +
621 ext_list_tmp->prev->data.len)
622 /* Overlap */
623 goto out;
625 if (ext_list_tmp != *ext_list_head &&
626 ext_list_tmp->data.logical <
627 ext->data.logical + ext->data.len)
628 /* Overlap */
629 goto out;
631 ext_list_tmp = ext_list_tmp->prev;
632 /* Insert "ext" after "ext_list_tmp" */
633 insert(ext_list_tmp, ext);
634 return 0;
635 out:
636 errno = EINVAL;
637 return -1;
641 * insert_extent_by_physical() - Sequentially insert extent by physical.
643 * @ext_list_head: the head of physical extent list.
644 * @ext: the extent element which will be inserted.
646 static int insert_extent_by_physical(struct fiemap_extent_list **ext_list_head,
647 struct fiemap_extent_list *ext)
649 struct fiemap_extent_list *ext_list_tmp = *ext_list_head;
651 if (ext == NULL)
652 goto out;
654 /* First element */
655 if (*ext_list_head == NULL) {
656 (*ext_list_head) = ext;
657 (*ext_list_head)->prev = *ext_list_head;
658 (*ext_list_head)->next = *ext_list_head;
659 return 0;
662 if (ext->data.physical <= ext_list_tmp->data.physical) {
663 /* Insert before head */
664 if (ext_list_tmp->data.physical <
665 ext->data.physical + ext->data.len)
666 /* Overlap */
667 goto out;
668 /* Adjust head */
669 *ext_list_head = ext;
670 } else {
671 /* Insert into the middle or last of the list */
672 do {
673 if (ext->data.physical < ext_list_tmp->data.physical)
674 break;
675 ext_list_tmp = ext_list_tmp->next;
676 } while (ext_list_tmp != (*ext_list_head));
677 if (ext->data.physical <
678 ext_list_tmp->prev->data.physical +
679 ext_list_tmp->prev->data.len)
680 /* Overlap */
681 goto out;
683 if (ext_list_tmp != *ext_list_head &&
684 ext_list_tmp->data.physical <
685 ext->data.physical + ext->data.len)
686 /* Overlap */
687 goto out;
689 ext_list_tmp = ext_list_tmp->prev;
690 /* Insert "ext" after "ext_list_tmp" */
691 insert(ext_list_tmp, ext);
692 return 0;
693 out:
694 errno = EINVAL;
695 return -1;
699 * insert_exts_group() - Insert a exts_group.
701 * @ext_group_head: the head of a exts_group list.
702 * @exts_group: the exts_group element which will be inserted.
704 static int insert_exts_group(struct fiemap_extent_group **ext_group_head,
705 struct fiemap_extent_group *exts_group)
707 struct fiemap_extent_group *ext_group_tmp = NULL;
709 if (exts_group == NULL) {
710 errno = EINVAL;
711 return -1;
714 /* Initialize list */
715 if (*ext_group_head == NULL) {
716 (*ext_group_head) = exts_group;
717 (*ext_group_head)->prev = *ext_group_head;
718 (*ext_group_head)->next = *ext_group_head;
719 return 0;
722 ext_group_tmp = (*ext_group_head)->prev;
723 insert(ext_group_tmp, exts_group);
725 return 0;
729 * join_extents() - Find continuous region(exts_group).
731 * @ext_list_head: the head of the extent list.
732 * @ext_group_head: the head of the target exts_group list.
734 static int join_extents(struct fiemap_extent_list *ext_list_head,
735 struct fiemap_extent_group **ext_group_head)
737 __u64 len = ext_list_head->data.len;
738 struct fiemap_extent_list *ext_list_start = ext_list_head;
739 struct fiemap_extent_list *ext_list_tmp = ext_list_head->next;
741 do {
742 struct fiemap_extent_group *ext_group_tmp = NULL;
744 /* This extent and previous extent are not continuous,
745 * so, all previous extents are treated as an extent group.
747 if ((ext_list_tmp->prev->data.logical +
748 ext_list_tmp->prev->data.len)
749 != ext_list_tmp->data.logical) {
750 ext_group_tmp =
751 malloc(sizeof(struct fiemap_extent_group));
752 if (ext_group_tmp == NULL)
753 return -1;
755 memset(ext_group_tmp, 0,
756 sizeof(struct fiemap_extent_group));
757 ext_group_tmp->len = len;
758 ext_group_tmp->start = ext_list_start;
759 ext_group_tmp->end = ext_list_tmp->prev;
761 if (insert_exts_group(ext_group_head,
762 ext_group_tmp) < 0) {
763 FREE(ext_group_tmp);
764 return -1;
766 ext_list_start = ext_list_tmp;
767 len = ext_list_tmp->data.len;
768 ext_list_tmp = ext_list_tmp->next;
769 continue;
772 /* This extent and previous extent are continuous,
773 * so, they belong to the same extent group, and we check
774 * if the next extent belongs to the same extent group.
776 len += ext_list_tmp->data.len;
777 ext_list_tmp = ext_list_tmp->next;
778 } while (ext_list_tmp != ext_list_head->next);
780 return 0;
784 * get_file_extents() - Get file's extent list.
786 * @fd: defrag target file's descriptor.
787 * @ext_list_head: the head of the extent list.
789 static int get_file_extents(int fd, struct fiemap_extent_list **ext_list_head)
791 __u32 i;
792 int ret;
793 int ext_buf_size, fie_buf_size;
794 __u64 pos = 0;
795 struct fiemap *fiemap_buf = NULL;
796 struct fiemap_extent *ext_buf = NULL;
797 struct fiemap_extent_list *ext_list = NULL;
799 /* Convert units, in bytes.
800 * Be careful : now, physical block number in extent is 48bit,
801 * and the maximum blocksize for ext4 is 4K(12bit),
802 * so there is no overflow, but in future it may be changed.
805 /* Alloc space for fiemap */
806 ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
807 fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
809 fiemap_buf = malloc(fie_buf_size);
810 if (fiemap_buf == NULL)
811 return -1;
813 ext_buf = fiemap_buf->fm_extents;
814 memset(fiemap_buf, 0, fie_buf_size);
815 fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
816 fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
817 fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
819 do {
820 fiemap_buf->fm_start = pos;
821 memset(ext_buf, 0, ext_buf_size);
822 ret = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
823 if (ret < 0 || fiemap_buf->fm_mapped_extents == 0)
824 goto out;
825 for (i = 0; i < fiemap_buf->fm_mapped_extents; i++) {
826 ext_list = NULL;
827 ext_list = malloc(sizeof(struct fiemap_extent_list));
828 if (ext_list == NULL)
829 goto out;
831 ext_list->data.physical = ext_buf[i].fe_physical
832 / block_size;
833 ext_list->data.logical = ext_buf[i].fe_logical
834 / block_size;
835 ext_list->data.len = ext_buf[i].fe_length
836 / block_size;
838 ret = insert_extent_by_physical(
839 ext_list_head, ext_list);
840 if (ret < 0) {
841 FREE(ext_list);
842 goto out;
845 /* Record file's logical offset this time */
846 pos = ext_buf[EXTENT_MAX_COUNT-1].fe_logical +
847 ext_buf[EXTENT_MAX_COUNT-1].fe_length;
849 * If fm_extents array has been filled and
850 * there are extents left, continue to cycle.
852 } while (fiemap_buf->fm_mapped_extents
853 == EXTENT_MAX_COUNT &&
854 !(ext_buf[EXTENT_MAX_COUNT-1].fe_flags
855 & FIEMAP_EXTENT_LAST));
857 FREE(fiemap_buf);
858 return 0;
859 out:
860 FREE(fiemap_buf);
861 return -1;
865 * get_logical_count() - Get the file logical extents count.
867 * @logical_list_head: the head of the logical extent list.
869 static int get_logical_count(struct fiemap_extent_list *logical_list_head)
871 int ret = 0;
872 struct fiemap_extent_list *ext_list_tmp = logical_list_head;
874 do {
875 ret++;
876 ext_list_tmp = ext_list_tmp->next;
877 } while (ext_list_tmp != logical_list_head);
879 return ret;
883 * get_physical_count() - Get the file physical extents count.
885 * @physical_list_head: the head of the physical extent list.
887 static int get_physical_count(struct fiemap_extent_list *physical_list_head)
889 int ret = 0;
890 struct fiemap_extent_list *ext_list_tmp = physical_list_head;
892 do {
893 if ((ext_list_tmp->data.physical + ext_list_tmp->data.len)
894 != ext_list_tmp->next->data.physical ||
895 (ext_list_tmp->data.logical + ext_list_tmp->data.len)
896 != ext_list_tmp->next->data.logical) {
897 /* This extent and next extent are not continuous. */
898 ret++;
901 ext_list_tmp = ext_list_tmp->next;
902 } while (ext_list_tmp != physical_list_head);
904 return ret;
908 * change_physical_to_logical() - Change list from physical to logical.
910 * @physical_list_head: the head of physical extent list.
911 * @logical_list_head: the head of logical extent list.
913 static int change_physical_to_logical(
914 struct fiemap_extent_list **physical_list_head,
915 struct fiemap_extent_list **logical_list_head)
917 int ret;
918 struct fiemap_extent_list *ext_list_tmp = *physical_list_head;
919 struct fiemap_extent_list *ext_list_next = ext_list_tmp->next;
921 while (1) {
922 if (ext_list_tmp == ext_list_next) {
923 ret = insert_extent_by_logical(
924 logical_list_head, ext_list_tmp);
925 if (ret < 0)
926 return -1;
928 *physical_list_head = NULL;
929 break;
932 ext_list_tmp->prev->next = ext_list_tmp->next;
933 ext_list_tmp->next->prev = ext_list_tmp->prev;
934 *physical_list_head = ext_list_next;
936 ret = insert_extent_by_logical(
937 logical_list_head, ext_list_tmp);
938 if (ret < 0) {
939 FREE(ext_list_tmp);
940 return -1;
942 ext_list_tmp = ext_list_next;
943 ext_list_next = ext_list_next->next;
946 return 0;
949 /* get_file_blocks() - Get total file blocks.
951 * @ext_list_head: the extent list head of the target file
953 static ext4_fsblk_t get_file_blocks(struct fiemap_extent_list *ext_list_head)
955 ext4_fsblk_t blk_count = 0;
956 struct fiemap_extent_list *ext_list_tmp = ext_list_head;
958 do {
959 blk_count += ext_list_tmp->data.len;
960 ext_list_tmp = ext_list_tmp->next;
961 } while (ext_list_tmp != ext_list_head);
963 return blk_count;
967 * free_ext() - Free the extent list.
969 * @ext_list_head: the extent list head of which will be free.
971 static void free_ext(struct fiemap_extent_list *ext_list_head)
973 struct fiemap_extent_list *ext_list_tmp = NULL;
975 if (ext_list_head == NULL)
976 return;
978 while (ext_list_head->next != ext_list_head) {
979 ext_list_tmp = ext_list_head;
980 ext_list_head->prev->next = ext_list_head->next;
981 ext_list_head->next->prev = ext_list_head->prev;
982 ext_list_head = ext_list_head->next;
983 free(ext_list_tmp);
985 free(ext_list_head);
989 * free_exts_group() - Free the exts_group.
991 * @*ext_group_head: the exts_group list head which will be free.
993 static void free_exts_group(struct fiemap_extent_group *ext_group_head)
995 struct fiemap_extent_group *ext_group_tmp = NULL;
997 if (ext_group_head == NULL)
998 return;
1000 while (ext_group_head->next != ext_group_head) {
1001 ext_group_tmp = ext_group_head;
1002 ext_group_head->prev->next = ext_group_head->next;
1003 ext_group_head->next->prev = ext_group_head->prev;
1004 ext_group_head = ext_group_head->next;
1005 free(ext_group_tmp);
1007 free(ext_group_head);
1011 * get_best_count() - Get the file best extents count.
1013 * @block_count: the file's physical block count.
1015 static int get_best_count(ext4_fsblk_t block_count)
1017 int ret;
1018 unsigned int flex_bg_num;
1020 if (blocks_per_group == 0)
1021 return 1;
1023 if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1024 flex_bg_num = 1 << log_groups_per_flex;
1025 ret = ((block_count - 1) /
1026 ((ext4_fsblk_t)blocks_per_group *
1027 flex_bg_num)) + 1;
1028 } else
1029 ret = ((block_count - 1) / blocks_per_group) + 1;
1031 return ret;
1036 * file_statistic() - Get statistic info of the file's fragments.
1038 * @file: the file's name.
1039 * @buf: the pointer of the struct stat64.
1040 * @flag: file type.
1041 * @ftwbuf: the pointer of a struct FTW.
1043 static int file_statistic(const char *file, const struct stat64 *buf,
1044 int flag EXT2FS_ATTR((unused)),
1045 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1047 int fd;
1048 int ret;
1049 int now_ext_count, best_ext_count = 0, physical_ext_count;
1050 int i, j;
1051 __u64 size_per_ext = 0;
1052 float ratio = 0.0;
1053 ext4_fsblk_t blk_count = 0;
1054 char msg_buffer[PATH_MAX + 24];
1055 struct fiemap_extent_list *physical_list_head = NULL;
1056 struct fiemap_extent_list *logical_list_head = NULL;
1058 defraged_file_count++;
1059 if (defraged_file_count > total_count)
1060 total_count = defraged_file_count;
1062 if (mode_flag & DETAIL) {
1063 if (total_count == 1 && regular_count == 1)
1064 printf("<File>\n");
1065 else {
1066 printf("[%u/%u]", defraged_file_count, total_count);
1067 fflush(stdout);
1070 if (lost_found_dir[0] != '\0' &&
1071 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1072 if (mode_flag & DETAIL) {
1073 PRINT_FILE_NAME(file);
1074 STATISTIC_ERR_MSG(NGMSG_LOST_FOUND);
1076 return 0;
1079 if (!S_ISREG(buf->st_mode)) {
1080 if (mode_flag & DETAIL) {
1081 PRINT_FILE_NAME(file);
1082 STATISTIC_ERR_MSG(NGMSG_FILE_UNREG);
1084 return 0;
1087 /* Access authority */
1088 if (current_uid != ROOT_UID &&
1089 buf->st_uid != current_uid) {
1090 if (mode_flag & DETAIL) {
1091 PRINT_FILE_NAME(file);
1092 STATISTIC_ERR_MSG(
1093 "File is not current user's file"
1094 " or current user is not root");
1096 return 0;
1099 /* Empty file */
1100 if (buf->st_size == 0) {
1101 if (mode_flag & DETAIL) {
1102 PRINT_FILE_NAME(file);
1103 STATISTIC_ERR_MSG("File size is 0");
1105 return 0;
1108 /* Has no blocks */
1109 if (buf->st_blocks == 0) {
1110 if (mode_flag & DETAIL) {
1111 PRINT_FILE_NAME(file);
1112 STATISTIC_ERR_MSG("File has no blocks");
1114 return 0;
1117 fd = open64(file, O_RDONLY);
1118 if (fd < 0) {
1119 if (mode_flag & DETAIL) {
1120 PRINT_FILE_NAME(file);
1121 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1123 return 0;
1126 /* Get file's physical extents */
1127 ret = get_file_extents(fd, &physical_list_head);
1128 if (ret < 0) {
1129 if (mode_flag & DETAIL) {
1130 PRINT_FILE_NAME(file);
1131 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1133 goto out;
1136 /* Get the count of file's continuous physical region */
1137 physical_ext_count = get_physical_count(physical_list_head);
1139 /* Change list from physical to logical */
1140 ret = change_physical_to_logical(&physical_list_head,
1141 &logical_list_head);
1142 if (ret < 0) {
1143 if (mode_flag & DETAIL) {
1144 PRINT_FILE_NAME(file);
1145 STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1147 goto out;
1150 /* Count file fragments before defrag */
1151 now_ext_count = get_logical_count(logical_list_head);
1153 if (current_uid == ROOT_UID) {
1154 /* Calculate the size per extent */
1155 blk_count = get_file_blocks(logical_list_head);
1157 best_ext_count = get_best_count(blk_count);
1159 /* e4defrag rounds size_per_ext up to a block size boundary */
1160 size_per_ext = blk_count * (buf->st_blksize / 1024) /
1161 now_ext_count;
1163 ratio = (float)(physical_ext_count - best_ext_count) * 100 /
1164 blk_count;
1166 extents_before_defrag += now_ext_count;
1167 extents_after_defrag += best_ext_count;
1168 files_block_count += blk_count;
1171 if (total_count == 1 && regular_count == 1) {
1172 /* File only */
1173 if (mode_flag & DETAIL) {
1174 int count = 0;
1175 struct fiemap_extent_list *ext_list_tmp =
1176 logical_list_head;
1178 /* Print extents info */
1179 do {
1180 count++;
1181 printf("[ext %d]:\tstart %llu:\tlogical "
1182 "%llu:\tlen %llu\n", count,
1183 (unsigned long long)
1184 ext_list_tmp->data.physical,
1185 (unsigned long long)
1186 ext_list_tmp->data.logical,
1187 (unsigned long long)
1188 ext_list_tmp->data.len);
1189 ext_list_tmp = ext_list_tmp->next;
1190 } while (ext_list_tmp != logical_list_head);
1192 } else {
1193 printf("%-40s%10s/%-10s%9s\n",
1194 "<File>", "now", "best", "size/ext");
1195 if (current_uid == ROOT_UID) {
1196 if (strlen(file) > 40)
1197 printf("%s\n%50d/%-10d%6llu KB\n",
1198 file, now_ext_count,
1199 best_ext_count,
1200 (unsigned long long) size_per_ext);
1201 else
1202 printf("%-40s%10d/%-10d%6llu KB\n",
1203 file, now_ext_count,
1204 best_ext_count,
1205 (unsigned long long) size_per_ext);
1206 } else {
1207 if (strlen(file) > 40)
1208 printf("%s\n%50d/%-10s%7s\n",
1209 file, now_ext_count,
1210 "-", "-");
1211 else
1212 printf("%-40s%10d/%-10s%7s\n",
1213 file, now_ext_count,
1214 "-", "-");
1217 succeed_cnt++;
1218 goto out;
1221 if (mode_flag & DETAIL) {
1222 /* Print statistic info */
1223 sprintf(msg_buffer, "[%u/%u]%s",
1224 defraged_file_count, total_count, file);
1225 if (current_uid == ROOT_UID) {
1226 if (strlen(msg_buffer) > 40)
1227 printf("\033[79;0H\033[K%s\n"
1228 "%50d/%-10d%6llu KB\n",
1229 msg_buffer, now_ext_count,
1230 best_ext_count,
1231 (unsigned long long) size_per_ext);
1232 else
1233 printf("\033[79;0H\033[K%-40s"
1234 "%10d/%-10d%6llu KB\n",
1235 msg_buffer, now_ext_count,
1236 best_ext_count,
1237 (unsigned long long) size_per_ext);
1238 } else {
1239 if (strlen(msg_buffer) > 40)
1240 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1241 msg_buffer, now_ext_count,
1242 "-", "-");
1243 else
1244 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1245 msg_buffer, now_ext_count,
1246 "-", "-");
1250 for (i = 0; i < SHOW_FRAG_FILES; i++) {
1251 if (ratio >= frag_rank[i].ratio) {
1252 for (j = SHOW_FRAG_FILES - 1; j > i; j--) {
1253 memset(&frag_rank[j], 0,
1254 sizeof(struct frag_statistic_ino));
1255 strncpy(frag_rank[j].msg_buffer,
1256 frag_rank[j - 1].msg_buffer,
1257 strnlen(frag_rank[j - 1].msg_buffer,
1258 PATH_MAX));
1259 frag_rank[j].now_count =
1260 frag_rank[j - 1].now_count;
1261 frag_rank[j].best_count =
1262 frag_rank[j - 1].best_count;
1263 frag_rank[j].size_per_ext =
1264 frag_rank[j - 1].size_per_ext;
1265 frag_rank[j].ratio =
1266 frag_rank[j - 1].ratio;
1268 memset(&frag_rank[i], 0,
1269 sizeof(struct frag_statistic_ino));
1270 strncpy(frag_rank[i].msg_buffer, file,
1271 strnlen(file, PATH_MAX));
1272 frag_rank[i].now_count = now_ext_count;
1273 frag_rank[i].best_count = best_ext_count;
1274 frag_rank[i].size_per_ext = size_per_ext;
1275 frag_rank[i].ratio = ratio;
1276 break;
1280 succeed_cnt++;
1282 out:
1283 close(fd);
1284 free_ext(physical_list_head);
1285 free_ext(logical_list_head);
1286 return 0;
1290 * print_progress - Print defrag progress
1292 * @file: file name.
1293 * @start: logical offset for defrag target file
1294 * @file_size: defrag target filesize
1296 static void print_progress(const char *file, ext2_loff_t start,
1297 ext2_loff_t file_size)
1299 int percent = (start * 100) / file_size;
1300 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1301 defraged_file_count, total_count, file, min(percent, 100));
1302 fflush(stdout);
1304 return;
1308 * call_defrag() - Execute the defrag program.
1310 * @fd: target file descriptor.
1311 * @donor_fd: donor file descriptor.
1312 * @file: target file name.
1313 * @buf: pointer of the struct stat64.
1314 * @ext_list_head: head of the extent list.
1316 static int call_defrag(int fd, int donor_fd, const char *file,
1317 const struct stat64 *buf, struct fiemap_extent_list *ext_list_head)
1319 ext2_loff_t start = 0;
1320 unsigned int page_num;
1321 unsigned char *vec = NULL;
1322 int defraged_ret = 0;
1323 int ret;
1324 struct move_extent move_data;
1325 struct fiemap_extent_list *ext_list_tmp = NULL;
1327 memset(&move_data, 0, sizeof(struct move_extent));
1328 move_data.donor_fd = donor_fd;
1330 /* Print defrag progress */
1331 print_progress(file, start, buf->st_size);
1333 ext_list_tmp = ext_list_head;
1334 do {
1335 move_data.orig_start = ext_list_tmp->data.logical;
1336 /* Logical offset of orig and donor should be same */
1337 move_data.donor_start = move_data.orig_start;
1338 move_data.len = ext_list_tmp->data.len;
1339 move_data.moved_len = 0;
1341 ret = page_in_core(fd, move_data, &vec, &page_num);
1342 if (ret < 0) {
1343 if (mode_flag & DETAIL) {
1344 printf("\n");
1345 PRINT_ERR_MSG_WITH_ERRNO(
1346 "Failed to get file map");
1347 } else {
1348 printf("\t[ NG ]\n");
1350 return -1;
1353 /* EXT4_IOC_MOVE_EXT */
1354 defraged_ret =
1355 ioctl(fd, EXT4_IOC_MOVE_EXT, &move_data);
1357 /* Free pages */
1358 ret = defrag_fadvise(fd, move_data, vec, page_num);
1359 if (vec) {
1360 free(vec);
1361 vec = NULL;
1363 if (ret < 0) {
1364 if (mode_flag & DETAIL) {
1365 printf("\n");
1366 PRINT_ERR_MSG_WITH_ERRNO(
1367 "Failed to free page");
1368 } else {
1369 printf("\t[ NG ]\n");
1371 return -1;
1374 if (defraged_ret < 0) {
1375 if (mode_flag & DETAIL) {
1376 printf("\n");
1377 PRINT_ERR_MSG_WITH_ERRNO(
1378 "Failed to defrag with "
1379 "EXT4_IOC_MOVE_EXT ioctl");
1380 if (errno == ENOTTY)
1381 printf("\tAt least 2.6.31-rc1 of "
1382 "vanilla kernel is required\n");
1383 } else {
1384 printf("\t[ NG ]\n");
1386 return -1;
1388 /* Adjust logical offset for next ioctl */
1389 move_data.orig_start += move_data.moved_len;
1390 move_data.donor_start = move_data.orig_start;
1392 start = move_data.orig_start * buf->st_blksize;
1394 /* Print defrag progress */
1395 print_progress(file, start, buf->st_size);
1397 /* End of file */
1398 if (start >= buf->st_size)
1399 break;
1401 ext_list_tmp = ext_list_tmp->next;
1402 } while (ext_list_tmp != ext_list_head);
1404 return 0;
1408 * file_defrag() - Check file attributes and call ioctl to defrag.
1410 * @file: the file's name.
1411 * @buf: the pointer of the struct stat64.
1412 * @flag: file type.
1413 * @ftwbuf: the pointer of a struct FTW.
1415 static int file_defrag(const char *file, const struct stat64 *buf,
1416 int flag EXT2FS_ATTR((unused)),
1417 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1419 int fd;
1420 int donor_fd = -1;
1421 int ret;
1422 int best;
1423 int file_frags_start, file_frags_end;
1424 int orig_physical_cnt, donor_physical_cnt = 0;
1425 char tmp_inode_name[PATH_MAX + 8];
1426 ext4_fsblk_t blk_count = 0;
1427 struct fiemap_extent_list *orig_list_physical = NULL;
1428 struct fiemap_extent_list *orig_list_logical = NULL;
1429 struct fiemap_extent_list *donor_list_physical = NULL;
1430 struct fiemap_extent_list *donor_list_logical = NULL;
1431 struct fiemap_extent_group *orig_group_head = NULL;
1432 struct fiemap_extent_group *orig_group_tmp = NULL;
1434 defraged_file_count++;
1435 if (defraged_file_count > total_count)
1436 total_count = defraged_file_count;
1438 if (mode_flag & DETAIL) {
1439 printf("[%u/%u]", defraged_file_count, total_count);
1440 fflush(stdout);
1443 if (lost_found_dir[0] != '\0' &&
1444 !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1445 if (mode_flag & DETAIL) {
1446 PRINT_FILE_NAME(file);
1447 IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1449 return 0;
1452 if (!S_ISREG(buf->st_mode)) {
1453 if (mode_flag & DETAIL) {
1454 PRINT_FILE_NAME(file);
1455 IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1457 return 0;
1460 /* Empty file */
1461 if (buf->st_size == 0) {
1462 if (mode_flag & DETAIL) {
1463 PRINT_FILE_NAME(file);
1464 IN_FTW_PRINT_ERR_MSG("File size is 0");
1466 return 0;
1469 /* Has no blocks */
1470 if (buf->st_blocks == 0) {
1471 if (mode_flag & DETAIL) {
1472 PRINT_FILE_NAME(file);
1473 STATISTIC_ERR_MSG("File has no blocks");
1475 return 0;
1478 fd = open64(file, O_RDWR);
1479 if (fd < 0) {
1480 if (mode_flag & DETAIL) {
1481 PRINT_FILE_NAME(file);
1482 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1484 return 0;
1487 /* Get file's extents */
1488 ret = get_file_extents(fd, &orig_list_physical);
1489 if (ret < 0) {
1490 if (mode_flag & DETAIL) {
1491 PRINT_FILE_NAME(file);
1492 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1494 goto out;
1497 /* Get the count of file's continuous physical region */
1498 orig_physical_cnt = get_physical_count(orig_list_physical);
1500 /* Change list from physical to logical */
1501 ret = change_physical_to_logical(&orig_list_physical,
1502 &orig_list_logical);
1503 if (ret < 0) {
1504 if (mode_flag & DETAIL) {
1505 PRINT_FILE_NAME(file);
1506 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1508 goto out;
1511 /* Count file fragments before defrag */
1512 file_frags_start = get_logical_count(orig_list_logical);
1514 blk_count = get_file_blocks(orig_list_logical);
1515 if (file_check(fd, buf, file, file_frags_start, blk_count) < 0)
1516 goto out;
1518 if (fsync(fd) < 0) {
1519 if (mode_flag & DETAIL) {
1520 PRINT_FILE_NAME(file);
1521 PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1523 goto out;
1526 best = get_best_count(blk_count);
1528 if (file_frags_start <= best)
1529 goto check_improvement;
1531 /* Combine extents to group */
1532 ret = join_extents(orig_list_logical, &orig_group_head);
1533 if (ret < 0) {
1534 if (mode_flag & DETAIL) {
1535 PRINT_FILE_NAME(file);
1536 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1538 goto out;
1541 /* Create donor inode */
1542 memset(tmp_inode_name, 0, PATH_MAX + 8);
1543 sprintf(tmp_inode_name, "%.*s.defrag",
1544 (int)strnlen(file, PATH_MAX), file);
1545 donor_fd = open64(tmp_inode_name, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
1546 if (donor_fd < 0) {
1547 if (mode_flag & DETAIL) {
1548 PRINT_FILE_NAME(file);
1549 if (errno == EEXIST)
1550 PRINT_ERR_MSG_WITH_ERRNO(
1551 "File is being defraged by other program");
1552 else
1553 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1555 goto out;
1558 /* Unlink donor inode */
1559 ret = unlink(tmp_inode_name);
1560 if (ret < 0) {
1561 if (mode_flag & DETAIL) {
1562 PRINT_FILE_NAME(file);
1563 PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1565 goto out;
1568 /* Allocate space for donor inode */
1569 orig_group_tmp = orig_group_head;
1570 do {
1571 ret = fallocate64(donor_fd, 0,
1572 (ext2_loff_t)orig_group_tmp->start->data.logical * block_size,
1573 (ext2_loff_t)orig_group_tmp->len * block_size);
1574 if (ret < 0) {
1575 if (mode_flag & DETAIL) {
1576 PRINT_FILE_NAME(file);
1577 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1579 goto out;
1582 orig_group_tmp = orig_group_tmp->next;
1583 } while (orig_group_tmp != orig_group_head);
1585 /* Get donor inode's extents */
1586 ret = get_file_extents(donor_fd, &donor_list_physical);
1587 if (ret < 0) {
1588 if (mode_flag & DETAIL) {
1589 PRINT_FILE_NAME(file);
1590 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1592 goto out;
1595 /* Calculate donor inode's continuous physical region */
1596 donor_physical_cnt = get_physical_count(donor_list_physical);
1598 /* Change donor extent list from physical to logical */
1599 ret = change_physical_to_logical(&donor_list_physical,
1600 &donor_list_logical);
1601 if (ret < 0) {
1602 if (mode_flag & DETAIL) {
1603 PRINT_FILE_NAME(file);
1604 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1606 goto out;
1609 check_improvement:
1610 if (mode_flag & DETAIL) {
1611 if (file_frags_start != 1)
1612 frag_files_before_defrag++;
1614 extents_before_defrag += file_frags_start;
1617 if (file_frags_start <= best ||
1618 orig_physical_cnt <= donor_physical_cnt) {
1619 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1620 defraged_file_count, total_count, file, 100);
1621 if (mode_flag & DETAIL)
1622 printf(" extents: %d -> %d",
1623 file_frags_start, file_frags_start);
1625 printf("\t[ OK ]\n");
1626 succeed_cnt++;
1628 if (file_frags_start != 1)
1629 frag_files_after_defrag++;
1631 extents_after_defrag += file_frags_start;
1632 goto out;
1635 /* Defrag the file */
1636 ret = call_defrag(fd, donor_fd, file, buf, donor_list_logical);
1638 /* Count file fragments after defrag and print extents info */
1639 if (mode_flag & DETAIL) {
1640 file_frags_end = file_frag_count(fd);
1641 if (file_frags_end < 0) {
1642 printf("\n");
1643 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO);
1644 goto out;
1647 if (file_frags_end != 1)
1648 frag_files_after_defrag++;
1650 extents_after_defrag += file_frags_end;
1652 if (ret < 0)
1653 goto out;
1655 printf(" extents: %d -> %d",
1656 file_frags_start, file_frags_end);
1657 fflush(stdout);
1660 if (ret < 0)
1661 goto out;
1663 printf("\t[ OK ]\n");
1664 fflush(stdout);
1665 succeed_cnt++;
1667 out:
1668 close(fd);
1669 if (donor_fd != -1)
1670 close(donor_fd);
1671 free_ext(orig_list_physical);
1672 free_ext(orig_list_logical);
1673 free_ext(donor_list_physical);
1674 free_exts_group(orig_group_head);
1675 return 0;
1679 * main() - Ext4 online defrag.
1681 * @argc: the number of parameter.
1682 * @argv[]: the pointer array of parameter.
1684 int main(int argc, char *argv[])
1686 int opt;
1687 int i, j, ret = 0;
1688 int flags = FTW_PHYS | FTW_MOUNT;
1689 int arg_type = -1;
1690 int mount_dir_len = 0;
1691 int success_flag = 0;
1692 char dir_name[PATH_MAX + 1];
1693 char dev_name[PATH_MAX + 1];
1694 struct stat64 buf;
1695 ext2_filsys fs = NULL;
1697 printf("e4defrag %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1699 /* Parse arguments */
1700 if (argc == 1)
1701 goto out;
1703 while ((opt = getopt(argc, argv, "vc")) != EOF) {
1704 switch (opt) {
1705 case 'v':
1706 mode_flag |= DETAIL;
1707 break;
1708 case 'c':
1709 mode_flag |= STATISTIC;
1710 break;
1711 default:
1712 goto out;
1716 if (argc == optind)
1717 goto out;
1719 current_uid = getuid();
1721 /* Main process */
1722 for (i = optind; i < argc; i++) {
1723 succeed_cnt = 0;
1724 regular_count = 0;
1725 total_count = 0;
1726 frag_files_before_defrag = 0;
1727 frag_files_after_defrag = 0;
1728 extents_before_defrag = 0;
1729 extents_after_defrag = 0;
1730 defraged_file_count = 0;
1731 files_block_count = 0;
1732 blocks_per_group = 0;
1733 feature_incompat = 0;
1734 log_groups_per_flex = 0;
1736 memset(dir_name, 0, PATH_MAX + 1);
1737 memset(dev_name, 0, PATH_MAX + 1);
1738 memset(lost_found_dir, 0, PATH_MAX + 1);
1739 memset(frag_rank, 0,
1740 sizeof(struct frag_statistic_ino) * SHOW_FRAG_FILES);
1742 if ((mode_flag & STATISTIC) && i > optind)
1743 printf("\n");
1745 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1746 PRINT_ERR_MSG("Endian's type is not big/little endian");
1747 PRINT_FILE_NAME(argv[i]);
1748 continue;
1749 #endif
1751 if (lstat64(argv[i], &buf) < 0) {
1752 perror(NGMSG_FILE_INFO);
1753 PRINT_FILE_NAME(argv[i]);
1754 continue;
1757 /* Handle i.e. lvm device symlinks */
1758 if (S_ISLNK(buf.st_mode)) {
1759 struct stat64 buf2;
1761 if (stat64(argv[i], &buf2) == 0 &&
1762 S_ISBLK(buf2.st_mode))
1763 buf = buf2;
1766 if (S_ISBLK(buf.st_mode)) {
1767 /* Block device */
1768 strncpy(dev_name, argv[i], strnlen(argv[i], PATH_MAX));
1769 if (get_mount_point(argv[i], dir_name, PATH_MAX) < 0)
1770 continue;
1771 if (lstat64(dir_name, &buf) < 0) {
1772 perror(NGMSG_FILE_INFO);
1773 PRINT_FILE_NAME(argv[i]);
1774 continue;
1776 arg_type = DEVNAME;
1777 if (!(mode_flag & STATISTIC))
1778 printf("ext4 defragmentation for device(%s)\n",
1779 argv[i]);
1780 } else if (S_ISDIR(buf.st_mode)) {
1781 /* Directory */
1782 if (access(argv[i], R_OK) < 0) {
1783 perror(argv[i]);
1784 continue;
1786 arg_type = DIRNAME;
1787 strncpy(dir_name, argv[i], strnlen(argv[i], PATH_MAX));
1788 } else if (S_ISREG(buf.st_mode)) {
1789 /* Regular file */
1790 arg_type = FILENAME;
1791 } else {
1792 /* Irregular file */
1793 PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1794 PRINT_FILE_NAME(argv[i]);
1795 continue;
1798 /* Set blocksize */
1799 block_size = buf.st_blksize;
1801 /* For device case,
1802 * filesystem type checked in get_mount_point()
1804 if (arg_type == FILENAME || arg_type == DIRNAME) {
1805 if (is_ext4(argv[i], dev_name) < 0)
1806 continue;
1807 if (realpath(argv[i], dir_name) == NULL) {
1808 perror("Couldn't get full path");
1809 PRINT_FILE_NAME(argv[i]);
1810 continue;
1814 if (current_uid == ROOT_UID) {
1815 /* Get super block info */
1816 ret = ext2fs_open(dev_name, EXT2_FLAG_64BITS, 0,
1817 block_size, unix_io_manager, &fs);
1818 if (ret) {
1819 if (mode_flag & DETAIL)
1820 fprintf(stderr,
1821 "Warning: couldn't get file "
1822 "system details for %s: %s\n",
1823 dev_name, error_message(ret));
1824 } else {
1825 blocks_per_group = fs->super->s_blocks_per_group;
1826 feature_incompat = fs->super->s_feature_incompat;
1827 log_groups_per_flex = fs->super->s_log_groups_per_flex;
1828 ext2fs_close_free(&fs);
1832 switch (arg_type) {
1834 case DIRNAME:
1835 if (!(mode_flag & STATISTIC))
1836 printf("ext4 defragmentation "
1837 "for directory(%s)\n", argv[i]);
1839 mount_dir_len = strnlen(lost_found_dir, PATH_MAX);
1841 strncat(lost_found_dir, "/lost+found",
1842 PATH_MAX - strnlen(lost_found_dir, PATH_MAX));
1844 /* Not the case("e4defrag mount_point_dir") */
1845 if (dir_name[mount_dir_len] != '\0') {
1847 * "e4defrag mount_point_dir/lost+found"
1848 * or "e4defrag mount_point_dir/lost+found/"
1850 if (strncmp(lost_found_dir, dir_name,
1851 strnlen(lost_found_dir,
1852 PATH_MAX)) == 0 &&
1853 (dir_name[strnlen(lost_found_dir,
1854 PATH_MAX)] == '\0' ||
1855 dir_name[strnlen(lost_found_dir,
1856 PATH_MAX)] == '/')) {
1857 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1858 PRINT_FILE_NAME(argv[i]);
1859 continue;
1862 /* "e4defrag mount_point_dir/else_dir" */
1863 memset(lost_found_dir, 0, PATH_MAX + 1);
1865 /* fall through */
1866 case DEVNAME:
1867 if (arg_type == DEVNAME) {
1868 strncpy(lost_found_dir, dir_name,
1869 strnlen(dir_name, PATH_MAX));
1870 strncat(lost_found_dir, "/lost+found/",
1871 PATH_MAX - strnlen(lost_found_dir,
1872 PATH_MAX));
1875 nftw64(dir_name, calc_entry_counts, FTW_OPEN_FD, flags);
1877 if (mode_flag & STATISTIC) {
1878 if (mode_flag & DETAIL)
1879 printf("%-40s%10s/%-10s%9s\n",
1880 "<File>", "now", "best", "size/ext");
1882 if (!(mode_flag & DETAIL) &&
1883 current_uid != ROOT_UID) {
1884 printf(" Done.\n");
1885 success_flag = 1;
1886 continue;
1889 nftw64(dir_name, file_statistic,
1890 FTW_OPEN_FD, flags);
1892 if (succeed_cnt != 0 &&
1893 current_uid == ROOT_UID) {
1894 if (mode_flag & DETAIL)
1895 printf("\n");
1896 printf("%-40s%10s/%-10s%9s\n",
1897 "<Fragmented files>", "now",
1898 "best", "size/ext");
1899 for (j = 0; j < SHOW_FRAG_FILES; j++) {
1900 if (strlen(frag_rank[j].
1901 msg_buffer) > 37) {
1902 printf("%d. %s\n%50d/"
1903 "%-10d%6llu KB\n",
1904 j + 1,
1905 frag_rank[j].msg_buffer,
1906 frag_rank[j].now_count,
1907 frag_rank[j].best_count,
1908 (unsigned long long)
1909 frag_rank[j].
1910 size_per_ext);
1911 } else if (strlen(frag_rank[j].
1912 msg_buffer) > 0) {
1913 printf("%d. %-37s%10d/"
1914 "%-10d%6llu KB\n",
1915 j + 1,
1916 frag_rank[j].msg_buffer,
1917 frag_rank[j].now_count,
1918 frag_rank[j].best_count,
1919 (unsigned long long)
1920 frag_rank[j].
1921 size_per_ext);
1922 } else
1923 break;
1926 break;
1928 /* File tree walk */
1929 nftw64(dir_name, file_defrag, FTW_OPEN_FD, flags);
1930 printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt,
1931 total_count);
1932 printf("\tFailure:\t\t\t[ %u/%u ]\n",
1933 total_count - succeed_cnt, total_count);
1934 if (mode_flag & DETAIL) {
1935 printf("\tTotal extents:\t\t\t%4d->%d\n",
1936 extents_before_defrag,
1937 extents_after_defrag);
1938 printf("\tFragmented percentage:\t\t"
1939 "%3llu%%->%llu%%\n",
1940 !regular_count ? 0 :
1941 ((unsigned long long)
1942 frag_files_before_defrag * 100) /
1943 regular_count,
1944 !regular_count ? 0 :
1945 ((unsigned long long)
1946 frag_files_after_defrag * 100) /
1947 regular_count);
1949 break;
1950 case FILENAME:
1951 total_count = 1;
1952 regular_count = 1;
1953 strncat(lost_found_dir, "/lost+found/",
1954 PATH_MAX - strnlen(lost_found_dir,
1955 PATH_MAX));
1956 if (strncmp(lost_found_dir, dir_name,
1957 strnlen(lost_found_dir,
1958 PATH_MAX)) == 0) {
1959 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1960 PRINT_FILE_NAME(argv[i]);
1961 continue;
1964 if (mode_flag & STATISTIC) {
1965 file_statistic(argv[i], &buf, FTW_F, NULL);
1966 break;
1967 } else
1968 printf("ext4 defragmentation for %s\n",
1969 argv[i]);
1970 /* Defrag single file process */
1971 file_defrag(argv[i], &buf, FTW_F, NULL);
1972 if (succeed_cnt != 0)
1973 printf(" Success:\t\t\t[1/1]\n");
1974 else
1975 printf(" Success:\t\t\t[0/1]\n");
1977 break;
1980 if (succeed_cnt != 0)
1981 success_flag = 1;
1982 if (mode_flag & STATISTIC) {
1983 if (current_uid != ROOT_UID) {
1984 printf(" Done.\n");
1985 continue;
1988 if (!succeed_cnt) {
1989 if (mode_flag & DETAIL)
1990 printf("\n");
1992 if (arg_type == DEVNAME)
1993 printf(" In this device(%s), "
1994 "none can be defragmented.\n", argv[i]);
1995 else if (arg_type == DIRNAME)
1996 printf(" In this directory(%s), "
1997 "none can be defragmented.\n", argv[i]);
1998 else
1999 printf(" This file(%s) "
2000 "can't be defragmented.\n", argv[i]);
2001 } else {
2002 float files_ratio = 0.0;
2003 float score = 0.0;
2004 __u64 size_per_ext = files_block_count *
2005 (buf.st_blksize / 1024) /
2006 extents_before_defrag;
2007 files_ratio = (float)(extents_before_defrag -
2008 extents_after_defrag) *
2009 100 / files_block_count;
2010 score = CALC_SCORE(files_ratio);
2011 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2012 " Average size per extent"
2013 "\t\t\t%llu KB\n"
2014 " Fragmentation score\t\t\t\t%.0f\n",
2015 extents_before_defrag,
2016 extents_after_defrag,
2017 (unsigned long long) size_per_ext, score);
2018 printf(" [0-30 no problem:"
2019 " 31-55 a little bit fragmented:"
2020 " 56- needs defrag]\n");
2022 if (arg_type == DEVNAME)
2023 printf(" This device (%s) ", argv[i]);
2024 else if (arg_type == DIRNAME)
2025 printf(" This directory (%s) ",
2026 argv[i]);
2027 else
2028 printf(" This file (%s) ", argv[i]);
2030 if (score > BOUND_SCORE)
2031 printf("needs defragmentation.\n");
2032 else
2033 printf("does not need "
2034 "defragmentation.\n");
2036 printf(" Done.\n");
2041 if (success_flag)
2042 return 0;
2044 exit(1);
2046 out:
2047 printf(MSG_USAGE);
2048 exit(1);