2014-01-11 05:16:10 +00:00
|
|
|
/**
|
|
|
|
* f2fs_format_utils.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
|
|
|
|
* http://www.samsung.com/
|
|
|
|
*
|
2014-04-07 03:10:59 +00:00
|
|
|
* Dual licensed under the GPL or LGPL version 2 licenses.
|
2014-01-11 05:16:10 +00:00
|
|
|
*/
|
2014-11-04 09:10:54 +00:00
|
|
|
#define _LARGEFILE_SOURCE
|
2014-01-11 05:16:10 +00:00
|
|
|
#define _LARGEFILE64_SOURCE
|
2014-11-04 09:10:54 +00:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
2014-01-11 05:16:10 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2014-01-20 08:42:21 +00:00
|
|
|
#include <sys/ioctl.h>
|
2014-01-11 05:16:10 +00:00
|
|
|
#include <sys/stat.h>
|
2014-11-04 09:10:54 +00:00
|
|
|
#include <fcntl.h>
|
2014-01-11 05:16:10 +00:00
|
|
|
|
|
|
|
#include "f2fs_fs.h"
|
|
|
|
|
2014-07-23 00:28:14 +00:00
|
|
|
#ifdef HAVE_LINUX_FS_H
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#endif
|
2014-11-04 09:10:54 +00:00
|
|
|
#ifdef HAVE_LINUX_FALLOC_H
|
|
|
|
#include <linux/falloc.h>
|
|
|
|
#endif
|
2014-07-23 00:28:14 +00:00
|
|
|
|
2016-06-16 21:39:18 +00:00
|
|
|
#ifndef BLKDISCARD
|
|
|
|
#define BLKDISCARD _IO(0x12,119)
|
|
|
|
#endif
|
|
|
|
#ifndef BLKSECDISCARD
|
|
|
|
#define BLKSECDISCARD _IO(0x12,125)
|
|
|
|
#endif
|
|
|
|
|
2014-01-11 05:16:10 +00:00
|
|
|
int f2fs_trim_device()
|
|
|
|
{
|
|
|
|
unsigned long long range[2];
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
if (!config.trim)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
range[0] = 0;
|
2015-02-05 09:36:39 +00:00
|
|
|
range[1] = config.total_sectors * config.sector_size;
|
2014-01-11 05:16:10 +00:00
|
|
|
|
|
|
|
if (fstat(config.fd, &stat_buf) < 0 ) {
|
|
|
|
MSG(1, "\tError: Failed to get the device stat!!!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-23 00:28:14 +00:00
|
|
|
#if defined(WITH_BLKDISCARD) && defined(BLKDISCARD)
|
2014-01-11 05:16:10 +00:00
|
|
|
MSG(0, "Info: Discarding device\n");
|
2014-11-04 09:10:54 +00:00
|
|
|
if (S_ISREG(stat_buf.st_mode)) {
|
2015-03-10 18:53:17 +00:00
|
|
|
#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
|
2014-11-04 09:10:54 +00:00
|
|
|
if (fallocate(config.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");
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-11 05:16:10 +00:00
|
|
|
return 0;
|
2014-11-04 09:10:54 +00:00
|
|
|
} else if (S_ISBLK(stat_buf.st_mode)) {
|
2016-06-16 21:39:18 +00:00
|
|
|
#ifdef BLKSECDISCARD
|
|
|
|
if (ioctl(config.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);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-06-13 08:41:58 +00:00
|
|
|
if (ioctl(config.fd, BLKDISCARD, &range) < 0) {
|
2016-06-16 21:39:18 +00:00
|
|
|
MSG(0, "Info: This device doesn't support BLKDISCARD\n");
|
2014-06-13 08:41:58 +00:00
|
|
|
} else {
|
|
|
|
MSG(0, "Info: Discarded %lu sectors\n",
|
|
|
|
config.total_sectors);
|
|
|
|
}
|
2014-01-11 05:16:10 +00:00
|
|
|
} else
|
|
|
|
return -1;
|
2014-05-14 00:02:55 +00:00
|
|
|
#endif
|
2014-01-11 05:16:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|