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