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