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