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