Skip to content

Commit ce24fde

Browse files
Aquarius223Jaegeuk Kim
authored andcommitted
f2fs_io: defrag_file supports automatic file length handling
Previously, executing defrag_file required specifying the starting position and file length, such as [0, 3276800] or [122880, 368640]. For the former, sometimes we only want to defrag the entire file, not just a portion of it, so the file length had to be manually entered regardless. To improve usability, a feature to automatically handle file length has been added. The following is a test: - When parameters are incorrect: root@android-pc:/home/android# ./f2fs_io defrag_file Excess arguments f2fs_io defrag_file [file_path] - defrag whole file automatically f2fs_io defrag_file [start] [length] [file_path] start : start offset of defragment region, unit: bytes length : bytes number of defragment region - When only a file is specified: root@android-pc:/home/android# ./f2fs_io defrag_file /mnt/uotan-rom/test/disk defrag /mnt/uotan-rom/test/disk in region[0, 4294967296] - When specifying the sorting range: root@android-pc:/home/android# ./f2fs_io defrag_file 2408448 9998336 /mnt/uotan-rom/test/disk defrag /mnt/uotan-rom/test/disk in region[2408448, 12406784] Signed-off-by: zhaoyuenan <amktiao030215@gmail.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 8731801 commit ce24fde

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

tools/f2fs_io/f2fs_io.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,33 +1469,47 @@ static void do_gc_urgent(int argc, char **argv, const struct cmd_desc *cmd)
14691469

14701470
#define defrag_file_desc "do defragment on file"
14711471
#define defrag_file_help \
1472+
"f2fs_io defrag_file [file_path] - defrag whole file automatically\n" \
14721473
"f2fs_io defrag_file [start] [length] [file_path]\n\n" \
14731474
" start : start offset of defragment region, unit: bytes\n" \
14741475
" length : bytes number of defragment region\n" \
14751476

14761477
static void do_defrag_file(int argc, char **argv, const struct cmd_desc *cmd)
14771478
{
1479+
struct stat st;
14781480
struct f2fs_defragment df;
1479-
u64 len;
14801481
int ret, fd;
1482+
u64 len;
14811483

1482-
if (argc != 4) {
1484+
if (argc != 2 && argc != 4) {
14831485
fputs("Excess arguments\n\n", stderr);
14841486
fputs(cmd->cmd_help, stderr);
14851487
exit(1);
14861488
}
14871489

1488-
df.start = atoll(argv[1]);
1489-
df.len = len = atoll(argv[2]);
1490+
if (argc == 2) {
1491+
fd = xopen(argv[1], O_RDWR, 0);
1492+
if (fstat(fd, &st) != 0)
1493+
die_errno("fstat failed");
1494+
1495+
df.start = 0;
1496+
df.len = ((u64)st.st_size + F2FS_DEFAULT_BLKSIZE - 1)
1497+
& ~(F2FS_DEFAULT_BLKSIZE - 1);
1498+
} else {
1499+
fd = xopen(argv[3], O_RDWR, 0);
1500+
df.start = atoll(argv[1]);
1501+
df.len = atoll(argv[2]);
1502+
}
14901503

1491-
fd = xopen(argv[3], O_RDWR, 0);
1504+
len = df.len;
14921505

14931506
ret = ioctl(fd, F2FS_IOC_DEFRAGMENT, &df);
14941507
if (ret < 0)
14951508
die_errno("F2FS_IOC_DEFRAGMENT failed");
14961509

14971510
printf("defrag %s in region[%"PRIu64", %"PRIu64"]\n",
1498-
argv[3], df.start, df.start + len);
1511+
(argc == 2) ? argv[1] : argv[3],
1512+
df.start, df.start + len);
14991513
exit(0);
15001514
}
15011515

0 commit comments

Comments
 (0)