Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / misc / e4defrag.c
1 /*
2  * e4defrag.c - ext4 filesystem defragmenter
3  *
4  * Copyright (C) 2009 NEC Software Tohoku, Ltd.
5  *
6  * Author: Akira Fujita <a-fujita@rs.jp.nec.com>
7  *         Takashi Sato <t-sato@yk.jp.nec.com>
8  */
9
10 #ifndef _LARGEFILE_SOURCE
11 #define _LARGEFILE_SOURCE
12 #endif
13
14 #ifndef _LARGEFILE64_SOURCE
15 #define _LARGEFILE64_SOURCE
16 #endif
17
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
21
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <endian.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <ftw.h>
28 #include <limits.h>
29 #include <mntent.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <ext2fs/ext2_types.h>
35 #include <ext2fs/ext2fs.h>
36 #include <linux/fs.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/syscall.h>
43 #include <sys/vfs.h>
44
45 /* A relatively new ioctl interface ... */
46 #ifndef EXT4_IOC_MOVE_EXT
47 #define EXT4_IOC_MOVE_EXT      _IOWR('f', 15, struct move_extent)
48 #endif
49
50 /* Macro functions */
51 #define PRINT_ERR_MSG(msg)      fprintf(stderr, "%s\n", (msg))
52 #define IN_FTW_PRINT_ERR_MSG(msg)       \
53         fprintf(stderr, "\t%s\t\t[ NG ]\n", (msg))
54 #define PRINT_FILE_NAME(file)   fprintf(stderr, " \"%s\"\n", (file))
55 #define PRINT_ERR_MSG_WITH_ERRNO(msg)   \
56         fprintf(stderr, "\t%s:%s\t[ NG ]\n", (msg), strerror(errno))
57 #define STATISTIC_ERR_MSG(msg)  \
58         fprintf(stderr, "\t%s\n", (msg))
59 #define STATISTIC_ERR_MSG_WITH_ERRNO(msg)       \
60         fprintf(stderr, "\t%s:%s\n", (msg), strerror(errno))
61 #define min(x, y) (((x) > (y)) ? (y) : (x))
62 #define CALC_SCORE(ratio) \
63         ((ratio) > 10 ? (80 + 20 * (ratio) / 100) : (8 * (ratio)))
64 /* Wrap up the free function */
65 #define FREE(tmp)                               \
66         do {                                    \
67                 if ((tmp) != NULL)              \
68                         free(tmp);              \
69         } while (0)                             \
70 /* Insert list2 after list1 */
71 #define insert(list1, list2)                    \
72         do {                                    \
73                 list2->next = list1->next;      \
74                 list1->next->prev = list2;      \
75                 list2->prev = list1;            \
76                 list1->next = list2;            \
77         } while (0)
78
79 /* To delete unused warning */
80 #ifdef __GNUC__
81 #define EXT2FS_ATTR(x) __attribute__(x)
82 #else
83 #define EXT2FS_ATTR(x)
84 #endif
85
86 /* The mode of defrag */
87 #define DETAIL                  0x01
88 #define STATISTIC               0x02
89
90 #define DEVNAME                 0
91 #define DIRNAME                 1
92 #define FILENAME                2
93
94 #define FTW_OPEN_FD             2000
95
96 #define FS_EXT4                 "ext4"
97 #define ROOT_UID                0
98
99 #define BOUND_SCORE             55
100 #define SHOW_FRAG_FILES 5
101
102 /* Magic number for ext4 */
103 #define EXT4_SUPER_MAGIC        0xEF53
104
105 /* Definition of flex_bg */
106 #define EXT4_FEATURE_INCOMPAT_FLEX_BG           0x0200
107
108 /* The following macro is used for ioctl FS_IOC_FIEMAP
109  * EXTENT_MAX_COUNT:    the maximum number of extents for exchanging between
110  *                      kernel-space and user-space per ioctl
111  */
112 #define EXTENT_MAX_COUNT        512
113
114 /* The following macros are error message */
115 #define MSG_USAGE               \
116 "Usage  : e4defrag [-v] file...| directory...| device...\n\
117         : e4defrag  -c  file...| directory...| device...\n"
118
119 #define NGMSG_EXT4              "Filesystem is not ext4 filesystem"
120 #define NGMSG_FILE_EXTENT       "Failed to get file extents"
121 #define NGMSG_FILE_INFO         "Failed to get file information"
122 #define NGMSG_FILE_OPEN         "Failed to open"
123 #define NGMSG_FILE_UNREG        "File is not regular file"
124 #define NGMSG_LOST_FOUND        "Can not process \"lost+found\""
125
126 /* Data type for filesystem-wide blocks number */
127 typedef unsigned long long ext4_fsblk_t;
128
129 struct fiemap_extent_data {
130         __u64 len;                      /* blocks count */
131         __u64 logical;          /* start logical block number */
132         ext4_fsblk_t physical;          /* start physical block number */
133 };
134
135 struct fiemap_extent_list {
136         struct fiemap_extent_list *prev;
137         struct fiemap_extent_list *next;
138         struct fiemap_extent_data data; /* extent belong to file */
139 };
140
141 struct fiemap_extent_group {
142         struct fiemap_extent_group *prev;
143         struct fiemap_extent_group *next;
144         __u64 len;      /* length of this continuous region */
145         struct fiemap_extent_list *start;       /* start ext */
146         struct fiemap_extent_list *end;         /* end ext */
147 };
148
149 struct move_extent {
150         __s32 reserved; /* original file descriptor */
151         __u32 donor_fd; /* donor file descriptor */
152         __u64 orig_start;       /* logical start offset in block for orig */
153         __u64 donor_start;      /* logical start offset in block for donor */
154         __u64 len;      /* block length to be moved */
155         __u64 moved_len;        /* moved block length */
156 };
157
158 struct frag_statistic_ino {
159         int now_count;  /* the file's extents count of before defrag */
160         int best_count; /* the best file's extents count */
161         __u64 size_per_ext;     /* size(KB) per extent */
162         float ratio;    /* the ratio of fragmentation */
163         char msg_buffer[PATH_MAX + 1];  /* pathname of the file */
164 };
165
166 typedef __u16 __le16;
167 typedef __u32 __le32;
168 typedef __u64 __le64;
169
170 /*
171  * Structure of the super block
172  */
173 struct ext4_super_block {
174 /*00*/  __le32  s_inodes_count;         /* Inodes count */
175         __le32  s_blocks_count_lo;      /* Blocks count */
176         __le32  s_r_blocks_count_lo;    /* Reserved blocks count */
177         __le32  s_free_blocks_count_lo; /* Free blocks count */
178 /*10*/  __le32  s_free_inodes_count;    /* Free inodes count */
179         __le32  s_first_data_block;     /* First Data Block */
180         __le32  s_log_block_size;       /* Block size */
181         __le32  s_obso_log_frag_size;   /* Obsoleted fragment size */
182 /*20*/  __le32  s_blocks_per_group;     /* # Blocks per group */
183         __le32  s_obso_frags_per_group; /* Obsoleted fragments per group */
184         __le32  s_inodes_per_group;     /* # Inodes per group */
185         __le32  s_mtime;                /* Mount time */
186 /*30*/  __le32  s_wtime;                /* Write time */
187         __le16  s_mnt_count;            /* Mount count */
188         __le16  s_max_mnt_count;        /* Maximal mount count */
189         __le16  s_magic;                /* Magic signature */
190         __le16  s_state;                /* File system state */
191         __le16  s_errors;               /* Behaviour when detecting errors */
192         __le16  s_minor_rev_level;      /* minor revision level */
193 /*40*/  __le32  s_lastcheck;            /* time of last check */
194         __le32  s_checkinterval;        /* max. time between checks */
195         __le32  s_creator_os;           /* OS */
196         __le32  s_rev_level;            /* Revision level */
197 /*50*/  __le16  s_def_resuid;           /* Default uid for reserved blocks */
198         __le16  s_def_resgid;           /* Default gid for reserved blocks */
199         /*
200          * These fields are for EXT4_DYNAMIC_REV superblocks only.
201          *
202          * Note: the difference between the compatible feature set and
203          * the incompatible feature set is that if there is a bit set
204          * in the incompatible feature set that the kernel doesn't
205          * know about, it should refuse to mount the filesystem.
206          *
207          * e2fsck's requirements are more strict; if it doesn't know
208          * about a feature in either the compatible or incompatible
209          * feature set, it must abort and not try to meddle with
210          * things it doesn't understand...
211          */
212         __le32  s_first_ino;            /* First non-reserved inode */
213         __le16  s_inode_size;           /* size of inode structure */
214         __le16  s_block_group_nr;       /* block group # of this superblock */
215         __le32  s_feature_compat;       /* compatible feature set */
216 /*60*/  __le32  s_feature_incompat;     /* incompatible feature set */
217         __le32  s_feature_ro_compat;    /* readonly-compatible feature set */
218 /*68*/  __u8    s_uuid[16];             /* 128-bit uuid for volume */
219 /*78*/  char    s_volume_name[16];      /* volume name */
220 /*88*/  char    s_last_mounted[64];     /* directory where last mounted */
221 /*C8*/  __le32  s_algorithm_usage_bitmap; /* For compression */
222         /*
223          * Performance hints.  Directory preallocation should only
224          * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
225          */
226         __u8    s_prealloc_blocks;      /* Nr of blocks to try to preallocate*/
227         __u8    s_prealloc_dir_blocks;  /* Nr to preallocate for dirs */
228         __le16  s_reserved_gdt_blocks;  /* Per group desc for online growth */
229         /*
230          * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
231          */
232 /*D0*/  __u8    s_journal_uuid[16];     /* uuid of journal superblock */
233 /*E0*/  __le32  s_journal_inum;         /* inode number of journal file */
234         __le32  s_journal_dev;          /* device number of journal file */
235         __le32  s_last_orphan;          /* start of list of inodes to delete */
236         __le32  s_hash_seed[4];         /* HTREE hash seed */
237         __u8    s_def_hash_version;     /* Default hash version to use */
238         __u8    s_reserved_char_pad;
239         __le16  s_desc_size;            /* size of group descriptor */
240 /*100*/ __le32  s_default_mount_opts;
241         __le32  s_first_meta_bg;        /* First metablock block group */
242         __le32  s_mkfs_time;            /* When the filesystem was created */
243         __le32  s_jnl_blocks[17];       /* Backup of the journal inode */
244         /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
245 /*150*/ __le32  s_blocks_count_hi;      /* Blocks count */
246         __le32  s_r_blocks_count_hi;    /* Reserved blocks count */
247         __le32  s_free_blocks_count_hi; /* Free blocks count */
248         __le16  s_min_extra_isize;      /* All inodes have at least # bytes */
249         __le16  s_want_extra_isize;     /* New inodes should reserve # bytes */
250         __le32  s_flags;                /* Miscellaneous flags */
251         __le16  s_raid_stride;          /* RAID stride */
252         __le16  s_mmp_interval;         /* # seconds to wait in MMP checking */
253         __le64  s_mmp_block;            /* Block for multi-mount protection */
254         __le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
255         __u8    s_log_groups_per_flex;  /* FLEX_BG group size */
256         __u8    s_reserved_char_pad2;
257         __le16  s_reserved_pad;
258         __u32   s_reserved[162];        /* Padding to the end of the block */
259 };
260
261 char    lost_found_dir[PATH_MAX + 1];
262 int     block_size;
263 int     extents_before_defrag;
264 int     extents_after_defrag;
265 int     mode_flag;
266 unsigned int    current_uid;
267 unsigned int    defraged_file_count;
268 unsigned int    frag_files_before_defrag;
269 unsigned int    frag_files_after_defrag;
270 unsigned int    regular_count;
271 unsigned int    succeed_cnt;
272 unsigned int    total_count;
273 __u8 log_groups_per_flex;
274 __le32 blocks_per_group;
275 __le32 feature_incompat;
276 ext4_fsblk_t    files_block_count;
277 struct frag_statistic_ino       frag_rank[SHOW_FRAG_FILES];
278
279
280 /* Local definitions of some syscalls glibc may not yet have */
281
282 #ifndef HAVE_POSIX_FADVISE
283 #warning Using locally defined posix_fadvise interface.
284
285 #ifndef __NR_fadvise64_64
286 #error Your kernel headers dont define __NR_fadvise64_64
287 #endif
288
289 /*
290  * fadvise() -          Give advice about file access.
291  *
292  * @fd:                 defrag target file's descriptor.
293  * @offset:             file offset.
294  * @len:                area length.
295  * @advise:             process flag.
296  */
297 static int posix_fadvise(int fd, loff_t offset, size_t len, int advise)
298 {
299         return syscall(__NR_fadvise64_64, fd, offset, len, advise);
300 }
301 #endif /* ! HAVE_FADVISE64_64 */
302
303 #ifndef HAVE_SYNC_FILE_RANGE
304 #warning Using locally defined sync_file_range interface.
305
306 #ifndef __NR_sync_file_range
307 #ifndef __NR_sync_file_range2 /* ppc */
308 #error Your kernel headers dont define __NR_sync_file_range
309 #endif
310 #endif
311
312 /*
313  * sync_file_range() -  Sync file region.
314  *
315  * @fd:                 defrag target file's descriptor.
316  * @offset:             file offset.
317  * @length:             area length.
318  * @flag:               process flag.
319  */
320 int sync_file_range(int fd, loff_t offset, loff_t length, unsigned int flag)
321 {
322 #ifdef __NR_sync_file_range
323         return syscall(__NR_sync_file_range, fd, offset, length, flag);
324 #else
325         return syscall(__NR_sync_file_range2, fd, flag, offset, length);
326 #endif
327 }
328 #endif /* ! HAVE_SYNC_FILE_RANGE */
329
330 #ifndef HAVE_FALLOCATE64
331 #warning Using locally defined fallocate syscall interface.
332
333 #ifndef __NR_fallocate
334 #error Your kernel headers dont define __NR_fallocate
335 #endif
336
337 /*
338  * fallocate64() -      Manipulate file space.
339  *
340  * @fd:                 defrag target file's descriptor.
341  * @mode:               process flag.
342  * @offset:             file offset.
343  * @len:                file size.
344  */
345 static int fallocate64(int fd, int mode, loff_t offset, loff_t len)
346 {
347         return syscall(__NR_fallocate, fd, mode, offset, len);
348 }
349 #endif /* ! HAVE_FALLOCATE */
350
351 /*
352  * get_mount_point() -  Get device's mount point.
353  *
354  * @devname:            the device's name.
355  * @mount_point:        the mount point.
356  * @dir_path_len:       the length of directory.
357  */
358 static int get_mount_point(const char *devname, char *mount_point,
359                                                         int dir_path_len)
360 {
361         /* Refer to /etc/mtab */
362         const char      *mtab = MOUNTED;
363         FILE    *fp = NULL;
364         struct mntent   *mnt = NULL;
365
366         fp = setmntent(mtab, "r");
367         if (fp == NULL) {
368                 perror("Couldn't access /etc/mtab");
369                 return -1;
370         }
371
372         while ((mnt = getmntent(fp)) != NULL) {
373                 if (strcmp(devname, mnt->mnt_fsname) != 0)
374                         continue;
375
376                 endmntent(fp);
377                 if (strcmp(mnt->mnt_type, FS_EXT4) == 0) {
378                         strncpy(mount_point, mnt->mnt_dir,
379                                 dir_path_len);
380                         return 0;
381                 }
382                 PRINT_ERR_MSG(NGMSG_EXT4);
383                 return -1;
384         }
385         endmntent(fp);
386         PRINT_ERR_MSG("Filesystem is not mounted");
387         return -1;
388 }
389
390 /*
391  * is_ext4() -          Whether on an ext4 filesystem.
392  *
393  * @file:               the file's name.
394  */
395 static int is_ext4(const char *file)
396 {
397         int     maxlen = 0;
398         int     len, ret;
399         FILE    *fp = NULL;
400         char    *mnt_type = NULL;
401         /* Refer to /etc/mtab */
402         const char      *mtab = MOUNTED;
403         char    file_path[PATH_MAX + 1];
404         struct mntent   *mnt = NULL;
405         struct statfs64 fsbuf;
406
407         /* Get full path */
408         if (realpath(file, file_path) == NULL) {
409                 perror("Couldn't get full path");
410                 PRINT_FILE_NAME(file);
411                 return -1;
412         }
413
414         if (statfs64(file_path, &fsbuf) < 0) {
415                 perror("Failed to get filesystem information");
416                 PRINT_FILE_NAME(file);
417                 return -1;
418         }
419
420         if (fsbuf.f_type != EXT4_SUPER_MAGIC) {
421                 PRINT_ERR_MSG(NGMSG_EXT4);
422                 return -1;
423         }
424
425         fp = setmntent(mtab, "r");
426         if (fp == NULL) {
427                 perror("Couldn't access /etc/mtab");
428                 return -1;
429         }
430
431         while ((mnt = getmntent(fp)) != NULL) {
432                 if (mnt->mnt_fsname[0] != '/')
433                         continue;
434                 len = strlen(mnt->mnt_dir);
435                 ret = memcmp(file_path, mnt->mnt_dir, len);
436                 if (ret != 0)
437                         continue;
438
439                 if (maxlen >= len)
440                         continue;
441
442                 maxlen = len;
443
444                 mnt_type = realloc(mnt_type, strlen(mnt->mnt_type) + 1);
445                 if (mnt_type == NULL) {
446                         endmntent(fp);
447                         return -1;
448                 }
449                 memset(mnt_type, 0, strlen(mnt->mnt_type) + 1);
450                 strncpy(mnt_type, mnt->mnt_type, strlen(mnt->mnt_type));
451                 strncpy(lost_found_dir, mnt->mnt_dir, PATH_MAX);
452         }
453
454         endmntent(fp);
455         if (strcmp(mnt_type, FS_EXT4) == 0) {
456                 FREE(mnt_type);
457                 return 0;
458         } else {
459                 FREE(mnt_type);
460                 PRINT_ERR_MSG(NGMSG_EXT4);
461                 return -1;
462         }
463 }
464
465 /*
466  * calc_entry_counts() -        Calculate file counts.
467  *
468  * @file:               file name.
469  * @buf:                file info.
470  * @flag:               file type.
471  * @ftwbuf:             the pointer of a struct FTW.
472  */
473 static int calc_entry_counts(const char *file EXT2FS_ATTR((unused)),
474                 const struct stat64 *buf, int flag EXT2FS_ATTR((unused)),
475                 struct FTW *ftwbuf EXT2FS_ATTR((unused)))
476 {
477         if (S_ISREG(buf->st_mode))
478                 regular_count++;
479
480         total_count++;
481
482         return 0;
483 }
484
485 /*
486  * page_in_core() -     Get information on whether pages are in core.
487  *
488  * @fd:                 defrag target file's descriptor.
489  * @defrag_data:        data used for defrag.
490  * @vec:                page state array.
491  * @page_num:           page number.
492  */
493 static int page_in_core(int fd, struct move_extent defrag_data,
494                         unsigned char **vec, unsigned int *page_num)
495 {
496         long    pagesize = sysconf(_SC_PAGESIZE);
497         void    *page = NULL;
498         loff_t  offset, end_offset, length;
499
500         if (vec == NULL || *vec != NULL)
501                 return -1;
502
503         /* In mmap, offset should be a multiple of the page size */
504         offset = (loff_t)defrag_data.orig_start * block_size;
505         length = (loff_t)defrag_data.len * block_size;
506         end_offset = offset + length;
507         /* Round the offset down to the nearest multiple of pagesize */
508         offset = (offset / pagesize) * pagesize;
509         length = end_offset - offset;
510
511         page = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, offset);
512         if (page == MAP_FAILED)
513                 return -1;
514
515         *page_num = 0;
516         *page_num = (length + pagesize - 1) / pagesize;
517         *vec = (unsigned char *)calloc(*page_num, 1);
518         if (*vec == NULL)
519                 return -1;
520
521         /* Get information on whether pages are in core */
522         if (mincore(page, (size_t)length, *vec) == -1 ||
523                 munmap(page, length) == -1) {
524                 FREE(*vec);
525                 return -1;
526         }
527
528         return 0;
529 }
530
531 /*
532  * defrag_fadvise() -   Predeclare an access pattern for file data.
533  *
534  * @fd:                 defrag target file's descriptor.
535  * @defrag_data:        data used for defrag.
536  * @vec:                page state array.
537  * @page_num:           page number.
538  */
539 static int defrag_fadvise(int fd, struct move_extent defrag_data,
540                    unsigned char *vec, unsigned int page_num)
541 {
542         int     flag = 1;
543         long    pagesize = sysconf(_SC_PAGESIZE);
544         int     fadvise_flag = POSIX_FADV_DONTNEED;
545         int     sync_flag = SYNC_FILE_RANGE_WAIT_BEFORE |
546                             SYNC_FILE_RANGE_WRITE |
547                             SYNC_FILE_RANGE_WAIT_AFTER;
548         unsigned int    i;
549         loff_t  offset;
550
551         offset = (loff_t)defrag_data.orig_start * block_size;
552         offset = (offset / pagesize) * pagesize;
553
554         /* Sync file for fadvise process */
555         if (sync_file_range(fd, offset,
556                 (loff_t)pagesize * page_num, sync_flag) < 0)
557                 return -1;
558
559         /* Try to release buffer cache which this process used,
560          * then other process can use the released buffer
561          */
562         for (i = 0; i < page_num; i++) {
563                 if ((vec[i] & 0x1) == 0) {
564                         offset += pagesize;
565                         continue;
566                 }
567                 if (posix_fadvise(fd, offset, pagesize, fadvise_flag) < 0) {
568                         if ((mode_flag & DETAIL) && flag) {
569                                 perror("\tFailed to fadvise");
570                                 flag = 0;
571                         }
572                 }
573                 offset += pagesize;
574         }
575
576         return 0;
577 }
578
579 /*
580  * check_free_size() -  Check if there's enough disk space.
581  *
582  * @fd:                 defrag target file's descriptor.
583  * @file:               file name.
584  * @blk_count:          file blocks.
585  */
586 static int check_free_size(int fd, const char *file, ext4_fsblk_t blk_count)
587 {
588         ext4_fsblk_t    free_blk_count;
589         struct statfs64 fsbuf;
590
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");
596                 }
597                 return -1;
598         }
599
600         /* Compute free space for root and normal user separately */
601         if (current_uid == ROOT_UID)
602                 free_blk_count = fsbuf.f_bfree;
603         else
604                 free_blk_count = fsbuf.f_bavail;
605
606         if (free_blk_count >= blk_count)
607                 return 0;
608
609         return -ENOSPC;
610 }
611
612 /*
613  * file_frag_count() -  Get file fragment count.
614  *
615  * @fd:                 defrag target file's descriptor.
616  */
617 static int file_frag_count(int fd)
618 {
619         int     ret;
620         struct fiemap   fiemap_buf;
621
622         /* When fm_extent_count is 0,
623          * ioctl just get file fragment count.
624          */
625         memset(&fiemap_buf, 0, sizeof(struct fiemap));
626         fiemap_buf.fm_start = 0;
627         fiemap_buf.fm_length = FIEMAP_MAX_OFFSET;
628         fiemap_buf.fm_flags |= FIEMAP_FLAG_SYNC;
629
630         ret = ioctl(fd, FS_IOC_FIEMAP, &fiemap_buf);
631         if (ret < 0)
632                 return ret;
633
634         return fiemap_buf.fm_mapped_extents;
635 }
636
637 /*
638  * file_check() -       Check file's attributes.
639  *
640  * @fd:                 defrag target file's descriptor.
641  * @buf:                a pointer of the struct stat64.
642  * @file:               file name.
643  * @extents:            file extents.
644  * @blk_count:          file blocks.
645  */
646 static int file_check(int fd, const struct stat64 *buf, const char *file,
647                 int extents, ext4_fsblk_t blk_count)
648 {
649         int     ret;
650         struct flock    lock;
651
652         /* Write-lock check is more reliable */
653         lock.l_type = F_WRLCK;
654         lock.l_start = 0;
655         lock.l_whence = SEEK_SET;
656         lock.l_len = 0;
657
658         /* Free space */
659         ret = check_free_size(fd, file, blk_count);
660         if (ret < 0) {
661                 if ((mode_flag & DETAIL) && ret == -ENOSPC) {
662                         printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
663                                 "  extents: %d -> %d\n", defraged_file_count,
664                                 total_count, file, extents, extents);
665                         IN_FTW_PRINT_ERR_MSG(
666                         "Defrag size is larger than filesystem's free space");
667                 }
668                 return -1;
669         }
670
671         /* Access authority */
672         if (current_uid != ROOT_UID &&
673                 buf->st_uid != current_uid) {
674                 if (mode_flag & DETAIL) {
675                         printf("\033[79;0H\033[K[%u/%u] \"%s\"\t\t"
676                                 "  extents: %d -> %d\n", defraged_file_count,
677                                 total_count, file, extents, extents);
678                         IN_FTW_PRINT_ERR_MSG(
679                                 "File is not current user's file"
680                                 " or current user is not root");
681                 }
682                 return -1;
683         }
684
685         /* Lock status */
686         if (fcntl(fd, F_GETLK, &lock) < 0) {
687                 if (mode_flag & DETAIL) {
688                         PRINT_FILE_NAME(file);
689                         PRINT_ERR_MSG_WITH_ERRNO(
690                                 "Failed to get lock information");
691                 }
692                 return -1;
693         } else if (lock.l_type != F_UNLCK) {
694                 if (mode_flag & DETAIL) {
695                         PRINT_FILE_NAME(file);
696                         IN_FTW_PRINT_ERR_MSG("File has been locked");
697                 }
698                 return -1;
699         }
700
701         return 0;
702 }
703
704 /*
705  * insert_extent_by_logical() - Sequentially insert extent by logical.
706  *
707  * @ext_list_head:      the head of logical extent list.
708  * @ext:                the extent element which will be inserted.
709  */
710 static int insert_extent_by_logical(struct fiemap_extent_list **ext_list_head,
711                         struct fiemap_extent_list *ext)
712 {
713         struct fiemap_extent_list       *ext_list_tmp = *ext_list_head;
714
715         if (ext == NULL)
716                 goto out;
717
718         /* First element */
719         if (*ext_list_head == NULL) {
720                 (*ext_list_head) = ext;
721                 (*ext_list_head)->prev = *ext_list_head;
722                 (*ext_list_head)->next = *ext_list_head;
723                 return 0;
724         }
725
726         if (ext->data.logical <= ext_list_tmp->data.logical) {
727                 /* Insert before head */
728                 if (ext_list_tmp->data.logical <
729                         ext->data.logical + ext->data.len)
730                         /* Overlap */
731                         goto out;
732                 /* Adjust head */
733                 *ext_list_head = ext;
734         } else {
735                 /* Insert into the middle or last of the list */
736                 do {
737                         if (ext->data.logical < ext_list_tmp->data.logical)
738                                 break;
739                         ext_list_tmp = ext_list_tmp->next;
740                 } while (ext_list_tmp != (*ext_list_head));
741                 if (ext->data.logical <
742                     ext_list_tmp->prev->data.logical +
743                         ext_list_tmp->prev->data.len)
744                         /* Overlap */
745                         goto out;
746
747                 if (ext_list_tmp != *ext_list_head &&
748                     ext_list_tmp->data.logical <
749                     ext->data.logical + ext->data.len)
750                         /* Overlap */
751                         goto out;
752         }
753         ext_list_tmp = ext_list_tmp->prev;
754         /* Insert "ext" after "ext_list_tmp" */
755         insert(ext_list_tmp, ext);
756         return 0;
757 out:
758         errno = EINVAL;
759         return -1;
760 }
761
762 /*
763  * insert_extent_by_physical() -        Sequentially insert extent by physical.
764  *
765  * @ext_list_head:      the head of physical extent list.
766  * @ext:                the extent element which will be inserted.
767  */
768 static int insert_extent_by_physical(struct fiemap_extent_list **ext_list_head,
769                         struct fiemap_extent_list *ext)
770 {
771         struct fiemap_extent_list       *ext_list_tmp = *ext_list_head;
772
773         if (ext == NULL)
774                 goto out;
775
776         /* First element */
777         if (*ext_list_head == NULL) {
778                 (*ext_list_head) = ext;
779                 (*ext_list_head)->prev = *ext_list_head;
780                 (*ext_list_head)->next = *ext_list_head;
781                 return 0;
782         }
783
784         if (ext->data.physical <= ext_list_tmp->data.physical) {
785                 /* Insert before head */
786                 if (ext_list_tmp->data.physical <
787                                         ext->data.physical + ext->data.len)
788                         /* Overlap */
789                         goto out;
790                 /* Adjust head */
791                 *ext_list_head = ext;
792         } else {
793                 /* Insert into the middle or last of the list */
794                 do {
795                         if (ext->data.physical < ext_list_tmp->data.physical)
796                                 break;
797                         ext_list_tmp = ext_list_tmp->next;
798                 } while (ext_list_tmp != (*ext_list_head));
799                 if (ext->data.physical <
800                     ext_list_tmp->prev->data.physical +
801                                 ext_list_tmp->prev->data.len)
802                         /* Overlap */
803                         goto out;
804
805                 if (ext_list_tmp != *ext_list_head &&
806                     ext_list_tmp->data.physical <
807                                 ext->data.physical + ext->data.len)
808                         /* Overlap */
809                         goto out;
810         }
811         ext_list_tmp = ext_list_tmp->prev;
812         /* Insert "ext" after "ext_list_tmp" */
813         insert(ext_list_tmp, ext);
814         return 0;
815 out:
816         errno = EINVAL;
817         return -1;
818 }
819
820 /*
821  * insert_exts_group() -        Insert a exts_group.
822  *
823  * @ext_group_head:             the head of a exts_group list.
824  * @exts_group:                 the exts_group element which will be inserted.
825  */
826 static int insert_exts_group(struct fiemap_extent_group **ext_group_head,
827                                 struct fiemap_extent_group *exts_group)
828 {
829         struct fiemap_extent_group      *ext_group_tmp = NULL;
830
831         if (exts_group == NULL) {
832                 errno = EINVAL;
833                 return -1;
834         }
835
836         /* Initialize list */
837         if (*ext_group_head == NULL) {
838                 (*ext_group_head) = exts_group;
839                 (*ext_group_head)->prev = *ext_group_head;
840                 (*ext_group_head)->next = *ext_group_head;
841                 return 0;
842         }
843
844         ext_group_tmp = (*ext_group_head)->prev;
845         insert(ext_group_tmp, exts_group);
846
847         return 0;
848 }
849
850 /*
851  * join_extents() -             Find continuous region(exts_group).
852  *
853  * @ext_list_head:              the head of the extent list.
854  * @ext_group_head:             the head of the target exts_group list.
855  */
856 static int join_extents(struct fiemap_extent_list *ext_list_head,
857                 struct fiemap_extent_group **ext_group_head)
858 {
859         __u64   len = ext_list_head->data.len;
860         struct fiemap_extent_list *ext_list_start = ext_list_head;
861         struct fiemap_extent_list *ext_list_tmp = ext_list_head->next;
862
863         do {
864                 struct fiemap_extent_group      *ext_group_tmp = NULL;
865
866                 /* This extent and previous extent are not continuous,
867                  * so, all previous extents are treated as an extent group.
868                  */
869                 if ((ext_list_tmp->prev->data.logical +
870                         ext_list_tmp->prev->data.len)
871                                 != ext_list_tmp->data.logical) {
872                         ext_group_tmp =
873                                 malloc(sizeof(struct fiemap_extent_group));
874                         if (ext_group_tmp == NULL)
875                                 return -1;
876
877                         memset(ext_group_tmp, 0,
878                                 sizeof(struct fiemap_extent_group));
879                         ext_group_tmp->len = len;
880                         ext_group_tmp->start = ext_list_start;
881                         ext_group_tmp->end = ext_list_tmp->prev;
882
883                         if (insert_exts_group(ext_group_head,
884                                 ext_group_tmp) < 0) {
885                                 FREE(ext_group_tmp);
886                                 return -1;
887                         }
888                         ext_list_start = ext_list_tmp;
889                         len = ext_list_tmp->data.len;
890                         ext_list_tmp = ext_list_tmp->next;
891                         continue;
892                 }
893
894                 /* This extent and previous extent are continuous,
895                  * so, they belong to the same extent group, and we check
896                  * if the next extent belongs to the same extent group.
897                  */
898                 len += ext_list_tmp->data.len;
899                 ext_list_tmp = ext_list_tmp->next;
900         } while (ext_list_tmp != ext_list_head->next);
901
902         return 0;
903 }
904
905 /*
906  * get_file_extents() - Get file's extent list.
907  *
908  * @fd:                 defrag target file's descriptor.
909  * @ext_list_head:      the head of the extent list.
910  */
911 static int get_file_extents(int fd, struct fiemap_extent_list **ext_list_head)
912 {
913         __u32   i;
914         int     ret;
915         int     ext_buf_size, fie_buf_size;
916         __u64   pos = 0;
917         struct fiemap   *fiemap_buf = NULL;
918         struct fiemap_extent    *ext_buf = NULL;
919         struct fiemap_extent_list       *ext_list = NULL;
920
921         /* Convert units, in bytes.
922          * Be careful : now, physical block number in extent is 48bit,
923          * and the maximum blocksize for ext4 is 4K(12bit),
924          * so there is no overflow, but in future it may be changed.
925          */
926
927         /* Alloc space for fiemap */
928         ext_buf_size = EXTENT_MAX_COUNT * sizeof(struct fiemap_extent);
929         fie_buf_size = sizeof(struct fiemap) + ext_buf_size;
930
931         fiemap_buf = malloc(fie_buf_size);
932         if (fiemap_buf == NULL)
933                 return -1;
934
935         ext_buf = fiemap_buf->fm_extents;
936         memset(fiemap_buf, 0, fie_buf_size);
937         fiemap_buf->fm_length = FIEMAP_MAX_OFFSET;
938         fiemap_buf->fm_flags |= FIEMAP_FLAG_SYNC;
939         fiemap_buf->fm_extent_count = EXTENT_MAX_COUNT;
940
941         do {
942                 fiemap_buf->fm_start = pos;
943                 memset(ext_buf, 0, ext_buf_size);
944                 ret = ioctl(fd, FS_IOC_FIEMAP, fiemap_buf);
945                 if (ret < 0)
946                         goto out;
947                 for (i = 0; i < fiemap_buf->fm_mapped_extents; i++) {
948                         ext_list = NULL;
949                         ext_list = malloc(sizeof(struct fiemap_extent_list));
950                         if (ext_list == NULL)
951                                 goto out;
952
953                         ext_list->data.physical = ext_buf[i].fe_physical
954                                                 / block_size;
955                         ext_list->data.logical = ext_buf[i].fe_logical
956                                                 / block_size;
957                         ext_list->data.len = ext_buf[i].fe_length
958                                                 / block_size;
959
960                         ret = insert_extent_by_physical(
961                                         ext_list_head, ext_list);
962                         if (ret < 0) {
963                                 FREE(ext_list);
964                                 goto out;
965                         }
966                 }
967                 /* Record file's logical offset this time */
968                 pos = ext_buf[EXTENT_MAX_COUNT-1].fe_logical +
969                         ext_buf[EXTENT_MAX_COUNT-1].fe_length;
970                 /*
971                  * If fm_extents array has been filled and
972                  * there are extents left, continue to cycle.
973                  */
974         } while (fiemap_buf->fm_mapped_extents
975                                         == EXTENT_MAX_COUNT &&
976                 !(ext_buf[EXTENT_MAX_COUNT-1].fe_flags
977                                         & FIEMAP_EXTENT_LAST));
978
979         FREE(fiemap_buf);
980         return 0;
981 out:
982         FREE(fiemap_buf);
983         return -1;
984 }
985
986 /*
987  * get_logical_count() -        Get the file logical extents count.
988  *
989  * @logical_list_head:  the head of the logical extent list.
990  */
991 static int get_logical_count(struct fiemap_extent_list *logical_list_head)
992 {
993         int ret = 0;
994         struct fiemap_extent_list *ext_list_tmp  = logical_list_head;
995
996         do {
997                 ret++;
998                 ext_list_tmp = ext_list_tmp->next;
999         } while (ext_list_tmp != logical_list_head);
1000
1001         return ret;
1002 }
1003
1004 /*
1005  * get_physical_count() -       Get the file physical extents count.
1006  *
1007  * @physical_list_head: the head of the physical extent list.
1008  */
1009 static int get_physical_count(struct fiemap_extent_list *physical_list_head)
1010 {
1011         int ret = 0;
1012         struct fiemap_extent_list *ext_list_tmp = physical_list_head;
1013
1014         do {
1015                 if ((ext_list_tmp->data.physical + ext_list_tmp->data.len)
1016                                 != ext_list_tmp->next->data.physical) {
1017                         /* This extent and next extent are not continuous. */
1018                         ret++;
1019                 }
1020
1021                 ext_list_tmp = ext_list_tmp->next;
1022         } while (ext_list_tmp != physical_list_head);
1023
1024         return ret;
1025 }
1026
1027 /*
1028  * change_physical_to_logical() -       Change list from physical to logical.
1029  *
1030  * @physical_list_head: the head of physical extent list.
1031  * @logical_list_head:  the head of logical extent list.
1032  */
1033 static int change_physical_to_logical(
1034                         struct fiemap_extent_list **physical_list_head,
1035                         struct fiemap_extent_list **logical_list_head)
1036 {
1037         int ret;
1038         struct fiemap_extent_list *ext_list_tmp = *physical_list_head;
1039         struct fiemap_extent_list *ext_list_next = ext_list_tmp->next;
1040
1041         while (1) {
1042                 if (ext_list_tmp == ext_list_next) {
1043                         ret = insert_extent_by_logical(
1044                                 logical_list_head, ext_list_tmp);
1045                         if (ret < 0)
1046                                 return -1;
1047
1048                         *physical_list_head = NULL;
1049                         break;
1050                 }
1051
1052                 ext_list_tmp->prev->next = ext_list_tmp->next;
1053                 ext_list_tmp->next->prev = ext_list_tmp->prev;
1054                 *physical_list_head = ext_list_next;
1055
1056                 ret = insert_extent_by_logical(
1057                         logical_list_head, ext_list_tmp);
1058                 if (ret < 0) {
1059                         FREE(ext_list_tmp);
1060                         return -1;
1061                 }
1062                 ext_list_tmp = ext_list_next;
1063                 ext_list_next = ext_list_next->next;
1064         }
1065
1066         return 0;
1067 }
1068
1069 /* get_file_blocks() -  Get total file blocks.
1070  *
1071  * @ext_list_head:      the extent list head of the target file
1072  */
1073 static ext4_fsblk_t get_file_blocks(struct fiemap_extent_list *ext_list_head)
1074 {
1075         ext4_fsblk_t blk_count = 0;
1076         struct fiemap_extent_list *ext_list_tmp = ext_list_head;
1077
1078         do {
1079                 blk_count += ext_list_tmp->data.len;
1080                 ext_list_tmp = ext_list_tmp->next;
1081         } while (ext_list_tmp != ext_list_head);
1082
1083         return blk_count;
1084 }
1085
1086 /*
1087  * free_ext() -         Free the extent list.
1088  *
1089  * @ext_list_head:      the extent list head of which will be free.
1090  */
1091 static void free_ext(struct fiemap_extent_list *ext_list_head)
1092 {
1093         struct fiemap_extent_list       *ext_list_tmp = NULL;
1094
1095         if (ext_list_head == NULL)
1096                 return;
1097
1098         while (ext_list_head->next != ext_list_head) {
1099                 ext_list_tmp = ext_list_head;
1100                 ext_list_head->prev->next = ext_list_head->next;
1101                 ext_list_head->next->prev = ext_list_head->prev;
1102                 ext_list_head = ext_list_head->next;
1103                 free(ext_list_tmp);
1104         }
1105         free(ext_list_head);
1106 }
1107
1108 /*
1109  * free_exts_group() -          Free the exts_group.
1110  *
1111  * @*ext_group_head:    the exts_group list head which will be free.
1112  */
1113 static void free_exts_group(struct fiemap_extent_group *ext_group_head)
1114 {
1115         struct fiemap_extent_group      *ext_group_tmp = NULL;
1116
1117         if (ext_group_head == NULL)
1118                 return;
1119
1120         while (ext_group_head->next != ext_group_head) {
1121                 ext_group_tmp = ext_group_head;
1122                 ext_group_head->prev->next = ext_group_head->next;
1123                 ext_group_head->next->prev = ext_group_head->prev;
1124                 ext_group_head = ext_group_head->next;
1125                 free(ext_group_tmp);
1126         }
1127         free(ext_group_head);
1128 }
1129
1130 /*
1131  * get_superblock_info() -      Get superblock info by the file name.
1132  *
1133  * @file:               the file's name.
1134  * @sb:         the pointer of the struct ext4_super_block.
1135  */
1136 static int get_superblock_info(const char *file, struct ext4_super_block *sb)
1137 {
1138         /* Refer to /etc/mtab */
1139         const char      *mtab = MOUNTED;
1140         FILE    *fp = NULL;
1141
1142         int     fd = -1;
1143         int     ret;
1144         size_t maxlen = 0;
1145         size_t len;
1146         char    dev_name[PATH_MAX + 1];
1147         struct mntent   *mnt = NULL;
1148
1149         fp = setmntent(mtab, "r");
1150         if (fp == NULL)
1151                 return -1;
1152
1153         while ((mnt = getmntent(fp)) != NULL) {
1154                 len = strlen(mnt->mnt_dir);
1155                 ret = memcmp(file, mnt->mnt_dir, len);
1156                 if (ret != 0)
1157                         continue;
1158
1159                 if (len < maxlen)
1160                         continue;
1161
1162                 maxlen = len;
1163
1164                 memset(dev_name, 0, PATH_MAX + 1);
1165                 strncpy(dev_name, mnt->mnt_fsname,
1166                                 strnlen(mnt->mnt_fsname, PATH_MAX));
1167         }
1168
1169         fd = open64(dev_name, O_RDONLY);
1170         if (fd < 0) {
1171                 ret = -1;
1172                 goto out;
1173         }
1174
1175         /* Set offset to read superblock */
1176         ret = lseek64(fd, SUPERBLOCK_OFFSET, SEEK_SET);
1177         if (ret < 0)
1178                 goto out;
1179
1180         ret = read(fd, sb, sizeof(struct ext4_super_block));
1181         if (ret < 0)
1182                 goto out;
1183
1184 out:
1185         if (fd != -1)
1186                 close(fd);
1187         endmntent(fp);
1188         return ret;
1189 }
1190
1191 /*
1192  * get_best_count() -   Get the file best extents count.
1193  *
1194  * @block_count:                the file's physical block count.
1195  */
1196 static int get_best_count(ext4_fsblk_t block_count)
1197 {
1198         int ret;
1199         unsigned int flex_bg_num;
1200
1201         /* Calcuate best extents count */
1202         if (feature_incompat & EXT4_FEATURE_INCOMPAT_FLEX_BG) {
1203                 flex_bg_num = 1 << log_groups_per_flex;
1204                 ret = ((block_count - 1) /
1205                         ((ext4_fsblk_t)blocks_per_group *
1206                                 flex_bg_num)) + 1;
1207         } else
1208                 ret = ((block_count - 1) / blocks_per_group) + 1;
1209
1210         return ret;
1211 }
1212
1213
1214 /*
1215  * file_statistic() -   Get statistic info of the file's fragments.
1216  *
1217  * @file:               the file's name.
1218  * @buf:                the pointer of the struct stat64.
1219  * @flag:               file type.
1220  * @ftwbuf:             the pointer of a struct FTW.
1221  */
1222 static int file_statistic(const char *file, const struct stat64 *buf,
1223                         int flag EXT2FS_ATTR((unused)),
1224                         struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1225 {
1226         int     fd;
1227         int     ret;
1228         int     now_ext_count, best_ext_count = 0, physical_ext_count;
1229         int     i, j;
1230         __u64   size_per_ext = 0;
1231         float   ratio = 0.0;
1232         ext4_fsblk_t    blk_count = 0;
1233         char    msg_buffer[PATH_MAX + 24];
1234         struct fiemap_extent_list *physical_list_head = NULL;
1235         struct fiemap_extent_list *logical_list_head = NULL;
1236
1237         defraged_file_count++;
1238
1239         if (mode_flag & DETAIL) {
1240                 if (total_count == 1 && regular_count == 1)
1241                         printf("<File>\n");
1242                 else {
1243                         printf("[%u/%u]", defraged_file_count, total_count);
1244                         fflush(stdout);
1245                 }
1246         }
1247         if (lost_found_dir[0] != '\0' &&
1248             !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1249                 if (mode_flag & DETAIL) {
1250                         PRINT_FILE_NAME(file);
1251                         STATISTIC_ERR_MSG(NGMSG_LOST_FOUND);
1252                 }
1253                         return 0;
1254         }
1255
1256         if (!S_ISREG(buf->st_mode)) {
1257                 if (mode_flag & DETAIL) {
1258                         PRINT_FILE_NAME(file);
1259                         STATISTIC_ERR_MSG(NGMSG_FILE_UNREG);
1260                 }
1261                 return 0;
1262         }
1263
1264         /* Access authority */
1265         if (current_uid != ROOT_UID &&
1266                 buf->st_uid != current_uid) {
1267                 if (mode_flag & DETAIL) {
1268                         PRINT_FILE_NAME(file);
1269                         STATISTIC_ERR_MSG(
1270                                 "File is not current user's file"
1271                                 " or current user is not root");
1272                 }
1273                 return 0;
1274         }
1275
1276         /* Empty file */
1277         if (buf->st_size == 0) {
1278                 if (mode_flag & DETAIL) {
1279                         PRINT_FILE_NAME(file);
1280                         STATISTIC_ERR_MSG("File size is 0");
1281                 }
1282                 return 0;
1283         }
1284
1285         /* Has no blocks */
1286         if (buf->st_blocks == 0) {
1287                 if (mode_flag & DETAIL) {
1288                         PRINT_FILE_NAME(file);
1289                         STATISTIC_ERR_MSG("File has no blocks");
1290                 }
1291                 return 0;
1292         }
1293
1294         fd = open64(file, O_RDONLY);
1295         if (fd < 0) {
1296                 if (mode_flag & DETAIL) {
1297                         PRINT_FILE_NAME(file);
1298                         STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1299                 }
1300                 return 0;
1301         }
1302
1303         /* Get file's physical extents  */
1304         ret = get_file_extents(fd, &physical_list_head);
1305         if (ret < 0) {
1306                 if (mode_flag & DETAIL) {
1307                         PRINT_FILE_NAME(file);
1308                         STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1309                 }
1310                 goto out;
1311         }
1312
1313         /* Get the count of file's continuous physical region */
1314         physical_ext_count = get_physical_count(physical_list_head);
1315
1316         /* Change list from physical to logical */
1317         ret = change_physical_to_logical(&physical_list_head,
1318                                                         &logical_list_head);
1319         if (ret < 0) {
1320                 if (mode_flag & DETAIL) {
1321                         PRINT_FILE_NAME(file);
1322                         STATISTIC_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1323                 }
1324                 goto out;
1325         }
1326
1327         /* Count file fragments before defrag */
1328         now_ext_count = get_logical_count(logical_list_head);
1329
1330         if (current_uid == ROOT_UID) {
1331                 /* Calculate the size per extent */
1332                 blk_count = get_file_blocks(logical_list_head);
1333
1334                 best_ext_count = get_best_count(blk_count);
1335
1336                 /* e4defrag rounds size_per_ext up to a block size boundary */
1337                 size_per_ext = blk_count * (buf->st_blksize / 1024) /
1338                                                         now_ext_count;
1339
1340                 ratio = (float)(physical_ext_count - best_ext_count) * 100 /
1341                                                         blk_count;
1342
1343                 extents_before_defrag += now_ext_count;
1344                 extents_after_defrag += best_ext_count;
1345                 files_block_count += blk_count;
1346         }
1347
1348         if (total_count == 1 && regular_count == 1) {
1349                 /* File only */
1350                 if (mode_flag & DETAIL) {
1351                         int count = 0;
1352                         struct fiemap_extent_list *ext_list_tmp =
1353                                                 logical_list_head;
1354
1355                         /* Print extents info */
1356                         do {
1357                                 count++;
1358                                 printf("[ext %d]:\tstart %llu:\tlogical "
1359                                                 "%llu:\tlen %llu\n", count,
1360                                                 ext_list_tmp->data.physical,
1361                                                 ext_list_tmp->data.logical,
1362                                                 ext_list_tmp->data.len);
1363                                 ext_list_tmp = ext_list_tmp->next;
1364                         } while (ext_list_tmp != logical_list_head);
1365
1366                 } else {
1367                         printf("%-40s%10s/%-10s%9s\n",
1368                                         "<File>", "now", "best", "size/ext");
1369                         if (current_uid == ROOT_UID) {
1370                                 if (strlen(file) > 40)
1371                                         printf("%s\n%50d/%-10d%6llu KB\n",
1372                                                 file, now_ext_count,
1373                                                 best_ext_count, size_per_ext);
1374                                 else
1375                                         printf("%-40s%10d/%-10d%6llu KB\n",
1376                                                 file, now_ext_count,
1377                                                 best_ext_count, size_per_ext);
1378                         } else {
1379                                 if (strlen(file) > 40)
1380                                         printf("%s\n%50d/%-10s%7s\n",
1381                                                         file, now_ext_count,
1382                                                         "-", "-");
1383                                 else
1384                                         printf("%-40s%10d/%-10s%7s\n",
1385                                                         file, now_ext_count,
1386                                                         "-", "-");
1387                         }
1388                 }
1389                 succeed_cnt++;
1390                 goto out;
1391         }
1392
1393         if (mode_flag & DETAIL) {
1394                 /* Print statistic info */
1395                 sprintf(msg_buffer, "[%u/%u]%s",
1396                                 defraged_file_count, total_count, file);
1397                 if (current_uid == ROOT_UID) {
1398                         if (strlen(msg_buffer) > 40)
1399                                 printf("\033[79;0H\033[K%s\n"
1400                                                 "%50d/%-10d%6llu KB\n",
1401                                                 msg_buffer, now_ext_count,
1402                                                 best_ext_count, size_per_ext);
1403                         else
1404                                 printf("\033[79;0H\033[K%-40s"
1405                                                 "%10d/%-10d%6llu KB\n",
1406                                                 msg_buffer, now_ext_count,
1407                                                 best_ext_count, size_per_ext);
1408                 } else {
1409                         if (strlen(msg_buffer) > 40)
1410                                 printf("\033[79;0H\033[K%s\n%50d/%-10s%7s\n",
1411                                                 msg_buffer, now_ext_count,
1412                                                         "-", "-");
1413                         else
1414                                 printf("\033[79;0H\033[K%-40s%10d/%-10s%7s\n",
1415                                                 msg_buffer, now_ext_count,
1416                                                         "-", "-");
1417                 }
1418         }
1419
1420         for (i = 0; i < SHOW_FRAG_FILES; i++) {
1421                 if (ratio >= frag_rank[i].ratio) {
1422                         for (j = SHOW_FRAG_FILES - 1; j > i; j--) {
1423                                 memset(&frag_rank[j], 0,
1424                                         sizeof(struct frag_statistic_ino));
1425                                 strncpy(frag_rank[j].msg_buffer,
1426                                         frag_rank[j - 1].msg_buffer,
1427                                         strnlen(frag_rank[j - 1].msg_buffer,
1428                                         PATH_MAX));
1429                                 frag_rank[j].now_count =
1430                                         frag_rank[j - 1].now_count;
1431                                 frag_rank[j].best_count =
1432                                         frag_rank[j - 1].best_count;
1433                                 frag_rank[j].size_per_ext =
1434                                         frag_rank[j - 1].size_per_ext;
1435                                 frag_rank[j].ratio =
1436                                         frag_rank[j - 1].ratio;
1437                         }
1438                         memset(&frag_rank[i], 0,
1439                                         sizeof(struct frag_statistic_ino));
1440                         strncpy(frag_rank[i].msg_buffer, file,
1441                                                 strnlen(file, PATH_MAX));
1442                         frag_rank[i].now_count = now_ext_count;
1443                         frag_rank[i].best_count = best_ext_count;
1444                         frag_rank[i].size_per_ext = size_per_ext;
1445                         frag_rank[i].ratio = ratio;
1446                         break;
1447                 }
1448         }
1449
1450         succeed_cnt++;
1451
1452 out:
1453         close(fd);
1454         free_ext(physical_list_head);
1455         free_ext(logical_list_head);
1456         return 0;
1457 }
1458
1459 /*
1460  * print_progress -     Print defrag progress
1461  *
1462  * @file:               file name.
1463  * @start:              logical offset for defrag target file
1464  * @file_size:          defrag target filesize
1465  */
1466 static void print_progress(const char *file, loff_t start, loff_t file_size)
1467 {
1468         int percent = (start * 100) / file_size;
1469         printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1470                 defraged_file_count, total_count, file, min(percent, 100));
1471         fflush(stdout);
1472
1473         return;
1474 }
1475
1476 /*
1477  * call_defrag() -      Execute the defrag program.
1478  *
1479  * @fd:                 target file descriptor.
1480  * @donor_fd:           donor file descriptor.
1481  * @file:                       target file name.
1482  * @buf:                        pointer of the struct stat64.
1483  * @ext_list_head:      head of the extent list.
1484  */
1485 static int call_defrag(int fd, int donor_fd, const char *file,
1486         const struct stat64 *buf, struct fiemap_extent_list *ext_list_head)
1487 {
1488         loff_t  start = 0;
1489         unsigned int    page_num;
1490         unsigned char   *vec = NULL;
1491         int     defraged_ret = 0;
1492         int     ret;
1493         struct move_extent      move_data;
1494         struct fiemap_extent_list       *ext_list_tmp = NULL;
1495
1496         memset(&move_data, 0, sizeof(struct move_extent));
1497         move_data.donor_fd = donor_fd;
1498
1499         /* Print defrag progress */
1500         print_progress(file, start, buf->st_size);
1501
1502         ext_list_tmp = ext_list_head;
1503         do {
1504                 move_data.orig_start = ext_list_tmp->data.logical;
1505                 /* Logical offset of orig and donor should be same */
1506                 move_data.donor_start = move_data.orig_start;
1507                 move_data.len = ext_list_tmp->data.len;
1508                 move_data.moved_len = 0;
1509
1510                 ret = page_in_core(fd, move_data, &vec, &page_num);
1511                 if (ret < 0) {
1512                         if (mode_flag & DETAIL) {
1513                                 printf("\n");
1514                                 PRINT_ERR_MSG_WITH_ERRNO(
1515                                                 "Failed to get file map");
1516                         } else {
1517                                 printf("\t[ NG ]\n");
1518                         }
1519                         return -1;
1520                 }
1521
1522                 /* EXT4_IOC_MOVE_EXT */
1523                 defraged_ret =
1524                         ioctl(fd, EXT4_IOC_MOVE_EXT, &move_data);
1525
1526                 /* Free pages */
1527                 ret = defrag_fadvise(fd, move_data, vec, page_num);
1528                 if (vec) {
1529                         free(vec);
1530                         vec = NULL;
1531                 }
1532                 if (ret < 0) {
1533                         if (mode_flag & DETAIL) {
1534                                 printf("\n");
1535                                 PRINT_ERR_MSG_WITH_ERRNO(
1536                                         "Failed to free page");
1537                         } else {
1538                                 printf("\t[ NG ]\n");
1539                         }
1540                         return -1;
1541                 }
1542
1543                 if (defraged_ret < 0) {
1544                         if (mode_flag & DETAIL) {
1545                                 printf("\n");
1546                                 PRINT_ERR_MSG_WITH_ERRNO(
1547                                         "Failed to defrag with "
1548                                         "EXT4_IOC_MOVE_EXT ioctl");
1549                                 if (errno == ENOTTY)
1550                                         printf("\tAt least 2.6.31-rc1 of "
1551                                                 "vanilla kernel is required\n");
1552                         } else {
1553                                 printf("\t[ NG ]\n");
1554                         }
1555                         return -1;
1556                 }
1557                 /* Adjust logical offset for next ioctl */
1558                 move_data.orig_start += move_data.moved_len;
1559                 move_data.donor_start = move_data.orig_start;
1560
1561                 start = move_data.orig_start * buf->st_blksize;
1562
1563                 /* Print defrag progress */
1564                 print_progress(file, start, buf->st_size);
1565
1566                 /* End of file */
1567                 if (start >= buf->st_size)
1568                         break;
1569
1570                 ext_list_tmp = ext_list_tmp->next;
1571         } while (ext_list_tmp != ext_list_head);
1572
1573         return 0;
1574 }
1575
1576 /*
1577  * file_defrag() -              Check file attributes and call ioctl to defrag.
1578  *
1579  * @file:               the file's name.
1580  * @buf:                the pointer of the struct stat64.
1581  * @flag:               file type.
1582  * @ftwbuf:             the pointer of a struct FTW.
1583  */
1584 static int file_defrag(const char *file, const struct stat64 *buf,
1585                         int flag EXT2FS_ATTR((unused)),
1586                         struct FTW *ftwbuf EXT2FS_ATTR((unused)))
1587 {
1588         int     fd;
1589         int     donor_fd = -1;
1590         int     ret;
1591         int     best;
1592         int     file_frags_start, file_frags_end;
1593         int     orig_physical_cnt, donor_physical_cnt = 0;
1594         char    tmp_inode_name[PATH_MAX + 8];
1595         ext4_fsblk_t                    blk_count = 0;
1596         struct fiemap_extent_list       *orig_list_physical = NULL;
1597         struct fiemap_extent_list       *orig_list_logical = NULL;
1598         struct fiemap_extent_list       *donor_list_physical = NULL;
1599         struct fiemap_extent_list       *donor_list_logical = NULL;
1600         struct fiemap_extent_group      *orig_group_head = NULL;
1601         struct fiemap_extent_group      *orig_group_tmp = NULL;
1602
1603         defraged_file_count++;
1604
1605         if (mode_flag & DETAIL) {
1606                 printf("[%u/%u]", defraged_file_count, total_count);
1607                 fflush(stdout);
1608         }
1609
1610         if (lost_found_dir[0] != '\0' &&
1611             !memcmp(file, lost_found_dir, strnlen(lost_found_dir, PATH_MAX))) {
1612                 if (mode_flag & DETAIL) {
1613                         PRINT_FILE_NAME(file);
1614                         IN_FTW_PRINT_ERR_MSG(NGMSG_LOST_FOUND);
1615                 }
1616                 return 0;
1617         }
1618
1619         if (!S_ISREG(buf->st_mode)) {
1620                 if (mode_flag & DETAIL) {
1621                         PRINT_FILE_NAME(file);
1622                         IN_FTW_PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1623                 }
1624                 return 0;
1625         }
1626
1627         /* Empty file */
1628         if (buf->st_size == 0) {
1629                 if (mode_flag & DETAIL) {
1630                         PRINT_FILE_NAME(file);
1631                         IN_FTW_PRINT_ERR_MSG("File size is 0");
1632                 }
1633                 return 0;
1634         }
1635
1636         /* Has no blocks */
1637         if (buf->st_blocks == 0) {
1638                 if (mode_flag & DETAIL) {
1639                         PRINT_FILE_NAME(file);
1640                         STATISTIC_ERR_MSG("File has no blocks");
1641                 }
1642                 return 0;
1643         }
1644
1645         fd = open64(file, O_RDWR);
1646         if (fd < 0) {
1647                 if (mode_flag & DETAIL) {
1648                         PRINT_FILE_NAME(file);
1649                         PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1650                 }
1651                 return 0;
1652         }
1653
1654         /* Get file's extents */
1655         ret = get_file_extents(fd, &orig_list_physical);
1656         if (ret < 0) {
1657                 if (mode_flag & DETAIL) {
1658                         PRINT_FILE_NAME(file);
1659                         PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1660                 }
1661                 goto out;
1662         }
1663
1664         /* Get the count of file's continuous physical region */
1665         orig_physical_cnt = get_physical_count(orig_list_physical);
1666
1667         /* Change list from physical to logical */
1668         ret = change_physical_to_logical(&orig_list_physical,
1669                                                         &orig_list_logical);
1670         if (ret < 0) {
1671                 if (mode_flag & DETAIL) {
1672                         PRINT_FILE_NAME(file);
1673                         PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1674                 }
1675                 goto out;
1676         }
1677
1678         /* Count file fragments before defrag */
1679         file_frags_start = get_logical_count(orig_list_logical);
1680
1681         blk_count = get_file_blocks(orig_list_logical);
1682         if (file_check(fd, buf, file, file_frags_start, blk_count) < 0)
1683                 goto out;
1684
1685         if (fsync(fd) < 0) {
1686                 if (mode_flag & DETAIL) {
1687                         PRINT_FILE_NAME(file);
1688                         PRINT_ERR_MSG_WITH_ERRNO("Failed to sync(fsync)");
1689                 }
1690                 goto out;
1691         }
1692
1693         if (current_uid == ROOT_UID)
1694                 best = get_best_count(blk_count);
1695         else
1696                 best = 1;
1697
1698         if (file_frags_start <= best)
1699                 goto check_improvement;
1700
1701         /* Combine extents to group */
1702         ret = join_extents(orig_list_logical, &orig_group_head);
1703         if (ret < 0) {
1704                 if (mode_flag & DETAIL) {
1705                         PRINT_FILE_NAME(file);
1706                         PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1707                 }
1708                 goto out;
1709         }
1710
1711         /* Create donor inode */
1712         memset(tmp_inode_name, 0, PATH_MAX + 8);
1713         sprintf(tmp_inode_name, "%.*s.defrag",
1714                                 (int)strnlen(file, PATH_MAX), file);
1715         donor_fd = open64(tmp_inode_name, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
1716         if (donor_fd < 0) {
1717                 if (mode_flag & DETAIL) {
1718                         PRINT_FILE_NAME(file);
1719                         if (errno == EEXIST)
1720                                 PRINT_ERR_MSG_WITH_ERRNO(
1721                                 "File is being defraged by other program");
1722                         else
1723                                 PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_OPEN);
1724                 }
1725                 goto out;
1726         }
1727
1728         /* Unlink donor inode */
1729         ret = unlink(tmp_inode_name);
1730         if (ret < 0) {
1731                 if (mode_flag & DETAIL) {
1732                         PRINT_FILE_NAME(file);
1733                         PRINT_ERR_MSG_WITH_ERRNO("Failed to unlink");
1734                 }
1735                 goto out;
1736         }
1737
1738         /* Allocate space for donor inode */
1739         orig_group_tmp = orig_group_head;
1740         do {
1741                 ret = fallocate64(donor_fd, 0,
1742                   (loff_t)orig_group_tmp->start->data.logical * block_size,
1743                   (loff_t)orig_group_tmp->len * block_size);
1744                 if (ret < 0) {
1745                         if (mode_flag & DETAIL) {
1746                                 PRINT_FILE_NAME(file);
1747                                 PRINT_ERR_MSG_WITH_ERRNO("Failed to fallocate");
1748                         }
1749                         goto out;
1750                 }
1751
1752                 orig_group_tmp = orig_group_tmp->next;
1753         } while (orig_group_tmp != orig_group_head);
1754
1755         /* Get donor inode's extents */
1756         ret = get_file_extents(donor_fd, &donor_list_physical);
1757         if (ret < 0) {
1758                 if (mode_flag & DETAIL) {
1759                         PRINT_FILE_NAME(file);
1760                         PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1761                 }
1762                 goto out;
1763         }
1764
1765         /* Calcuate donor inode's continuous physical region */
1766         donor_physical_cnt = get_physical_count(donor_list_physical);
1767
1768         /* Change donor extent list from physical to logical */
1769         ret = change_physical_to_logical(&donor_list_physical,
1770                                                         &donor_list_logical);
1771         if (ret < 0) {
1772                 if (mode_flag & DETAIL) {
1773                         PRINT_FILE_NAME(file);
1774                         PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_EXTENT);
1775                 }
1776                 goto out;
1777         }
1778
1779 check_improvement:
1780         if (mode_flag & DETAIL) {
1781                 if (file_frags_start != 1)
1782                         frag_files_before_defrag++;
1783
1784                 extents_before_defrag += file_frags_start;
1785         }
1786
1787         if (file_frags_start <= best ||
1788                         orig_physical_cnt <= donor_physical_cnt) {
1789                 printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
1790                         defraged_file_count, total_count, file, 100);
1791                 if (mode_flag & DETAIL)
1792                         printf("  extents: %d -> %d",
1793                                 file_frags_start, file_frags_start);
1794
1795                 printf("\t[ OK ]\n");
1796                 succeed_cnt++;
1797
1798                 if (file_frags_start != 1)
1799                         frag_files_after_defrag++;
1800
1801                 extents_after_defrag += file_frags_start;
1802                 goto out;
1803         }
1804
1805         /* Defrag the file */
1806         ret = call_defrag(fd, donor_fd, file, buf, donor_list_logical);
1807
1808         /* Count file fragments after defrag and print extents info */
1809         if (mode_flag & DETAIL) {
1810                 file_frags_end = file_frag_count(fd);
1811                 if (file_frags_end < 0) {
1812                         printf("\n");
1813                         PRINT_ERR_MSG_WITH_ERRNO(NGMSG_FILE_INFO);
1814                         goto out;
1815                 }
1816
1817                 if (file_frags_end != 1)
1818                         frag_files_after_defrag++;
1819
1820                 extents_after_defrag += file_frags_end;
1821
1822                 if (ret < 0)
1823                         goto out;
1824
1825                 printf("  extents: %d -> %d",
1826                         file_frags_start, file_frags_end);
1827                 fflush(stdout);
1828         }
1829
1830         if (ret < 0)
1831                 goto out;
1832
1833         printf("\t[ OK ]\n");
1834         fflush(stdout);
1835         succeed_cnt++;
1836
1837 out:
1838         close(fd);
1839         if (donor_fd != -1)
1840                 close(donor_fd);
1841         free_ext(orig_list_physical);
1842         free_ext(orig_list_logical);
1843         free_ext(donor_list_physical);
1844         free_exts_group(orig_group_head);
1845         return 0;
1846 }
1847
1848 /*
1849  * main() -             Ext4 online defrag.
1850  *
1851  * @argc:               the number of parameter.
1852  * @argv[]:             the pointer array of parameter.
1853  */
1854 int main(int argc, char *argv[])
1855 {
1856         int     opt;
1857         int     i, j;
1858         int     flags = FTW_PHYS | FTW_MOUNT;
1859         int     arg_type = -1;
1860         int     success_flag = 0;
1861         char    dir_name[PATH_MAX + 1];
1862         struct stat64   buf;
1863         struct ext4_super_block sb;
1864
1865         /* Parse arguments */
1866         if (argc == 1)
1867                 goto out;
1868
1869         while ((opt = getopt(argc, argv, "vc")) != EOF) {
1870                 switch (opt) {
1871                 case 'v':
1872                         mode_flag |= DETAIL;
1873                         break;
1874                 case 'c':
1875                         mode_flag |= STATISTIC;
1876                         break;
1877                 default:
1878                         goto out;
1879                 }
1880         }
1881
1882         if (argc == optind)
1883                 goto out;
1884
1885         current_uid = getuid();
1886
1887         /* Main process */
1888         for (i = optind; i < argc; i++) {
1889                 succeed_cnt = 0;
1890                 regular_count = 0;
1891                 total_count = 0;
1892                 frag_files_before_defrag = 0;
1893                 frag_files_after_defrag = 0;
1894                 extents_before_defrag = 0;
1895                 extents_after_defrag = 0;
1896                 defraged_file_count = 0;
1897                 files_block_count = 0;
1898                 blocks_per_group = 0;
1899                 feature_incompat = 0;
1900                 log_groups_per_flex = 0;
1901
1902                 memset(dir_name, 0, PATH_MAX + 1);
1903                 memset(lost_found_dir, 0, PATH_MAX + 1);
1904                 memset(frag_rank, 0,
1905                         sizeof(struct frag_statistic_ino) * SHOW_FRAG_FILES);
1906
1907                 if ((mode_flag & STATISTIC) && i > optind)
1908                         printf("\n");
1909
1910 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN
1911                 PRINT_ERR_MSG("Endian's type is not big/little endian");
1912                 PRINT_FILE_NAME(argv[i]);
1913                 continue;
1914 #endif
1915
1916                 if (lstat64(argv[i], &buf) < 0) {
1917                         perror(NGMSG_FILE_INFO);
1918                         PRINT_FILE_NAME(argv[i]);
1919                         continue;
1920                 }
1921
1922                 if (S_ISBLK(buf.st_mode)) {
1923                         /* Block device */
1924                         if (get_mount_point(argv[i], dir_name, PATH_MAX) < 0)
1925                                 continue;
1926                         if (lstat64(dir_name, &buf) < 0) {
1927                                 perror(NGMSG_FILE_INFO);
1928                                 PRINT_FILE_NAME(argv[i]);
1929                                 continue;
1930                         }
1931                         arg_type = DEVNAME;
1932                         if (!(mode_flag & STATISTIC))
1933                                 printf("ext4 defragmentation for device(%s)\n",
1934                                         argv[i]);
1935                 } else if (S_ISDIR(buf.st_mode)) {
1936                         /* Directory */
1937                         if (access(argv[i], R_OK) < 0) {
1938                                 perror(argv[i]);
1939                                 continue;
1940                         }
1941                         arg_type = DIRNAME;
1942                         strncpy(dir_name, argv[i], strnlen(argv[i], PATH_MAX));
1943                 } else if (S_ISREG(buf.st_mode)) {
1944                         /* Regular file */
1945                         arg_type = FILENAME;
1946                 } else {
1947                         /* Irregular file */
1948                         PRINT_ERR_MSG(NGMSG_FILE_UNREG);
1949                         PRINT_FILE_NAME(argv[i]);
1950                         continue;
1951                 }
1952
1953                 /* Set blocksize */
1954                 block_size = buf.st_blksize;
1955
1956                 /* For device case,
1957                  * filesystem type checked in get_mount_point()
1958                  */
1959                 if (arg_type == FILENAME || arg_type == DIRNAME) {
1960                         if (is_ext4(argv[i]) < 0)
1961                                 continue;
1962                         if (realpath(argv[i], dir_name) == NULL) {
1963                                 perror("Couldn't get full path");
1964                                 PRINT_FILE_NAME(argv[i]);
1965                                 continue;
1966                         }
1967                 }
1968
1969                 if (current_uid == ROOT_UID) {
1970                         /* Get super block info */
1971                         memset(&sb, 0, sizeof(struct ext4_super_block));
1972                         if (get_superblock_info(dir_name, &sb) < 0) {
1973                                 if (mode_flag & DETAIL) {
1974                                         perror("Can't get super block info");
1975                                         PRINT_FILE_NAME(argv[i]);
1976                                 }
1977                                 continue;
1978                         }
1979
1980                         blocks_per_group = ext2fs_swab32(sb.s_blocks_per_group);
1981                         feature_incompat = ext2fs_swab32(sb.s_feature_incompat);
1982                         log_groups_per_flex = sb.s_log_groups_per_flex;
1983                 }
1984
1985                 switch (arg_type) {
1986                 case DIRNAME:
1987                         if (!(mode_flag & STATISTIC))
1988                                 printf("ext4 defragmentation "
1989                                         "for directory(%s)\n", argv[i]);
1990
1991                         int mount_dir_len = 0;
1992                         mount_dir_len = strnlen(lost_found_dir, PATH_MAX);
1993
1994                         strncat(lost_found_dir, "/lost+found",
1995                                 PATH_MAX - strnlen(lost_found_dir, PATH_MAX));
1996
1997                         /* Not the case("e4defrag  mount_piont_dir") */
1998                         if (dir_name[mount_dir_len] != '\0') {
1999                                 /*
2000                                  * "e4defrag mount_piont_dir/lost+found"
2001                                  * or "e4defrag mount_piont_dir/lost+found/"
2002                                  */
2003                                 if (strncmp(lost_found_dir, dir_name,
2004                                             strnlen(lost_found_dir,
2005                                                     PATH_MAX)) == 0 &&
2006                                     (dir_name[strnlen(lost_found_dir,
2007                                                       PATH_MAX)] == '\0' ||
2008                                      dir_name[strnlen(lost_found_dir,
2009                                                       PATH_MAX)] == '/')) {
2010                                         PRINT_ERR_MSG(NGMSG_LOST_FOUND);
2011                                         PRINT_FILE_NAME(argv[i]);
2012                                         continue;
2013                                 }
2014
2015                                 /* "e4defrag mount_piont_dir/else_dir" */
2016                                 memset(lost_found_dir, 0, PATH_MAX + 1);
2017                         }
2018                 case DEVNAME:
2019                         if (arg_type == DEVNAME) {
2020                                 strncpy(lost_found_dir, dir_name,
2021                                         strnlen(dir_name, PATH_MAX));
2022                                 strncat(lost_found_dir, "/lost+found/",
2023                                         PATH_MAX - strnlen(lost_found_dir,
2024                                                            PATH_MAX));
2025                         }
2026
2027                         nftw64(dir_name, calc_entry_counts, FTW_OPEN_FD, flags);
2028
2029                         if (mode_flag & STATISTIC) {
2030                                 if (mode_flag & DETAIL)
2031                                         printf("%-40s%10s/%-10s%9s\n",
2032                                         "<File>", "now", "best", "size/ext");
2033
2034                                 if (!(mode_flag & DETAIL) &&
2035                                                 current_uid != ROOT_UID) {
2036                                         printf(" Done.\n");
2037                                         success_flag = 1;
2038                                         continue;
2039                                 }
2040
2041                                 nftw64(dir_name, file_statistic,
2042                                                         FTW_OPEN_FD, flags);
2043
2044                                 if (succeed_cnt != 0 &&
2045                                         current_uid == ROOT_UID) {
2046                                         if (mode_flag & DETAIL)
2047                                                 printf("\n");
2048                                         printf("%-40s%10s/%-10s%9s\n",
2049                                                 "<Fragmented files>", "now",
2050                                                 "best", "size/ext");
2051                                         for (j = 0; j < SHOW_FRAG_FILES; j++) {
2052                                                 if (strlen(frag_rank[j].
2053                                                         msg_buffer) > 37) {
2054                                                         printf("%d. %s\n%50d/"
2055                                                         "%-10d%6llu KB\n",
2056                                                         j + 1,
2057                                                         frag_rank[j].msg_buffer,
2058                                                         frag_rank[j].now_count,
2059                                                         frag_rank[j].best_count,
2060                                                         frag_rank[j].
2061                                                                 size_per_ext);
2062                                                 } else if (strlen(frag_rank[j].
2063                                                         msg_buffer) > 0) {
2064                                                         printf("%d. %-37s%10d/"
2065                                                         "%-10d%6llu KB\n",
2066                                                         j + 1,
2067                                                         frag_rank[j].msg_buffer,
2068                                                         frag_rank[j].now_count,
2069                                                         frag_rank[j].best_count,
2070                                                         frag_rank[j].
2071                                                                 size_per_ext);
2072                                                 } else
2073                                                         break;
2074                                         }
2075                                 }
2076                                 break;
2077                         }
2078                         /* File tree walk */
2079                         nftw64(dir_name, file_defrag, FTW_OPEN_FD, flags);
2080                         printf("\n\tSuccess:\t\t\t[ %u/%u ]\n", succeed_cnt,
2081                                 total_count);
2082                         printf("\tFailure:\t\t\t[ %u/%u ]\n",
2083                                 total_count - succeed_cnt, total_count);
2084                         if (mode_flag & DETAIL) {
2085                                 printf("\tTotal extents:\t\t\t%4d->%d\n",
2086                                         extents_before_defrag,
2087                                         extents_after_defrag);
2088                                 printf("\tFragmented percentage:\t\t"
2089                                         "%3llu%%->%llu%%\n",
2090                                         !regular_count ? 0 :
2091                                         ((unsigned long long)
2092                                         frag_files_before_defrag * 100) /
2093                                         regular_count,
2094                                         !regular_count ? 0 :
2095                                         ((unsigned long long)
2096                                         frag_files_after_defrag * 100) /
2097                                         regular_count);
2098                         }
2099                         break;
2100                 case FILENAME:
2101                         total_count = 1;
2102                         regular_count = 1;
2103                         strncat(lost_found_dir, "/lost+found/",
2104                                 PATH_MAX - strnlen(lost_found_dir,
2105                                                    PATH_MAX));
2106                         if (strncmp(lost_found_dir, dir_name,
2107                                     strnlen(lost_found_dir,
2108                                             PATH_MAX)) == 0) {
2109                                 PRINT_ERR_MSG(NGMSG_LOST_FOUND);
2110                                 PRINT_FILE_NAME(argv[i]);
2111                                 continue;
2112                         }
2113
2114                         if (mode_flag & STATISTIC) {
2115                                 file_statistic(argv[i], &buf, FTW_F, NULL);
2116                                 break;
2117                         } else
2118                                 printf("ext4 defragmentation for %s\n",
2119                                                                  argv[i]);
2120                         /* Defrag single file process */
2121                         file_defrag(argv[i], &buf, FTW_F, NULL);
2122                         if (succeed_cnt != 0)
2123                                 printf(" Success:\t\t\t[1/1]\n");
2124                         else
2125                                 printf(" Success:\t\t\t[0/1]\n");
2126
2127                         break;
2128                 }
2129
2130                 if (succeed_cnt != 0)
2131                         success_flag = 1;
2132                 if (mode_flag & STATISTIC) {
2133                         if (current_uid != ROOT_UID) {
2134                                 printf(" Done.\n");
2135                                 continue;
2136                         }
2137
2138                         if (!succeed_cnt) {
2139                                 if (mode_flag & DETAIL)
2140                                         printf("\n");
2141
2142                                 if (arg_type == DEVNAME)
2143                                         printf(" In this device(%s), "
2144                                         "none can be defragmented.\n", argv[i]);
2145                                 else if (arg_type == DIRNAME)
2146                                         printf(" In this directory(%s), "
2147                                         "none can be defragmented.\n", argv[i]);
2148                                 else
2149                                         printf(" This file(%s) "
2150                                         "can't be defragmented.\n", argv[i]);
2151                         } else {
2152                                 float files_ratio = 0.0;
2153                                 float score = 0.0;
2154                                 __u64 size_per_ext = files_block_count *
2155                                                 (buf.st_blksize / 1024) /
2156                                                 extents_before_defrag;
2157                                 files_ratio = (float)(extents_before_defrag -
2158                                                 extents_after_defrag) *
2159                                                 100 / files_block_count;
2160                                 score = CALC_SCORE(files_ratio);
2161                                 printf("\n Total/best extents\t\t\t\t%d/%d\n"
2162                                         " Average size per extent"
2163                                         "\t\t\t%llu KB\n"
2164                                         " Fragmentation score\t\t\t\t%.0f\n",
2165                                                 extents_before_defrag,
2166                                                 extents_after_defrag,
2167                                                 size_per_ext, score);
2168                                 printf(" [0-30 no problem:"
2169                                         " 31-55 a little bit fragmented:"
2170                                         " 56- needs defrag]\n");
2171
2172                                 if (arg_type == DEVNAME)
2173                                         printf(" This device (%s) ", argv[i]);
2174                                 else if (arg_type == DIRNAME)
2175                                         printf(" This directory (%s) ",
2176                                                                 argv[i]);
2177                                 else
2178                                         printf(" This file (%s) ", argv[i]);
2179
2180                                 if (score > BOUND_SCORE)
2181                                         printf("needs defragmentation.\n");
2182                                 else
2183                                         printf("does not need "
2184                                                         "defragmentation.\n");
2185                         }
2186                         printf(" Done.\n");
2187                 }
2188
2189         }
2190
2191         if (success_flag)
2192                 return 0;
2193
2194         exit(1);
2195
2196 out:
2197         printf(MSG_USAGE);
2198         exit(1);
2199 }
2200