From 932d596a86f35b1e52110570e1b0c6e7f031cb6f Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Fri, 2 Sep 2016 10:33:44 -0700 Subject: [PATCH] mkfs: get fd for f2fs_trim_device This patch is to get a parameter for fd in f2fs_trim_device. Signed-off-by: Jaegeuk Kim --- mkfs/f2fs_format.c | 10 ++++++---- mkfs/f2fs_format_utils.c | 28 ++++++++++++---------------- mkfs/f2fs_format_utils.h | 2 +- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c index 6b1318a..466df27 100644 --- a/mkfs/f2fs_format.c +++ b/mkfs/f2fs_format.c @@ -927,10 +927,12 @@ int f2fs_format_device(void) goto exit; } - err = f2fs_trim_device(); - if (err < 0) { - MSG(0, "\tError: Failed to trim whole device!!!\n"); - goto exit; + if (config.trim) { + err = f2fs_trim_device(config.fd); + if (err < 0) { + MSG(0, "\tError: Failed to trim whole device!!!\n"); + goto exit; + } } err = f2fs_init_sit_area(); diff --git a/mkfs/f2fs_format_utils.c b/mkfs/f2fs_format_utils.c index d469ce5..eb8e3c0 100644 --- a/mkfs/f2fs_format_utils.c +++ b/mkfs/f2fs_format_utils.c @@ -34,27 +34,24 @@ #define BLKSECDISCARD _IO(0x12,125) #endif -int f2fs_trim_device() +int f2fs_trim_device(int fd) { unsigned long long range[2]; struct stat stat_buf; - if (!config.trim) - return 0; - - range[0] = 0; - range[1] = config.total_sectors * config.sector_size; - - if (fstat(config.fd, &stat_buf) < 0 ) { + if (fstat(fd, &stat_buf) < 0 ) { MSG(1, "\tError: Failed to get the device stat!!!\n"); return -1; } + range[0] = 0; + range[1] = stat_buf.st_size; + #if defined(WITH_BLKDISCARD) && defined(BLKDISCARD) MSG(0, "Info: Discarding device\n"); if (S_ISREG(stat_buf.st_mode)) { #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) - if (fallocate(config.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, range[0], range[1]) < 0) { MSG(0, "Info: fallocate(PUNCH_HOLE|KEEP_SIZE) is failed\n"); } @@ -62,23 +59,22 @@ int f2fs_trim_device() return 0; } else if (S_ISBLK(stat_buf.st_mode)) { #ifdef BLKSECDISCARD - if (ioctl(config.fd, BLKSECDISCARD, &range) < 0) { + if (ioctl(fd, BLKSECDISCARD, &range) < 0) { MSG(0, "Info: This device doesn't support BLKSECDISCARD\n"); } else { - MSG(0, "Info: Secure Discarded %lu sectors\n", - config.total_sectors); + MSG(0, "Info: Secure Discarded %lu MB\n", + stat_buf.st_size >> 20); return 0; } #endif - if (ioctl(config.fd, BLKDISCARD, &range) < 0) { + if (ioctl(fd, BLKDISCARD, &range) < 0) { MSG(0, "Info: This device doesn't support BLKDISCARD\n"); } else { - MSG(0, "Info: Discarded %lu sectors\n", - config.total_sectors); + MSG(0, "Info: Discarded %lu MB\n", + stat_buf.st_size >> 20); } } else return -1; #endif return 0; } - diff --git a/mkfs/f2fs_format_utils.h b/mkfs/f2fs_format_utils.h index 9eb2cea..8bf598e 100644 --- a/mkfs/f2fs_format_utils.h +++ b/mkfs/f2fs_format_utils.h @@ -12,5 +12,5 @@ extern struct f2fs_configuration config; -int f2fs_trim_device(void); +int f2fs_trim_device(int); int f2fs_format_device(void); -- 2.7.4