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
|
|
|
*/
|
2017-11-14 18:53:32 +00:00
|
|
|
#ifndef _LARGEFILE_SOURCE
|
2014-11-04 09:10:54 +00:00
|
|
|
#define _LARGEFILE_SOURCE
|
2017-11-14 18:53:32 +00:00
|
|
|
#endif
|
|
|
|
#ifndef _LARGEFILE64_SOURCE
|
2014-01-11 05:16:10 +00:00
|
|
|
#define _LARGEFILE64_SOURCE
|
2017-11-14 18:53:32 +00:00
|
|
|
#endif
|
2014-11-04 09:10:54 +00:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
2014-01-11 05:16:10 +00:00
|
|
|
|
2018-10-17 01:40:56 +00:00
|
|
|
#ifndef _FILE_OFFSET_BITS
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#endif
|
|
|
|
|
2017-11-14 18:53:32 +00:00
|
|
|
#include <f2fs_fs.h>
|
|
|
|
|
2014-01-11 05:16:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2018-04-02 07:59:24 +00:00
|
|
|
#include <stdlib.h>
|
2017-12-01 23:03:30 +00:00
|
|
|
#ifndef ANDROID_WINDOWS_HOST
|
2014-01-20 08:42:21 +00:00
|
|
|
#include <sys/ioctl.h>
|
2017-12-01 23:03:30 +00:00
|
|
|
#endif
|
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
|
|
|
|
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
|
|
|
|
|
2016-11-02 00:23:40 +00:00
|
|
|
static int trim_device(int i)
|
2014-01-11 05:16:10 +00:00
|
|
|
{
|
2017-12-01 23:03:30 +00:00
|
|
|
#ifndef ANDROID_WINDOWS_HOST
|
2014-01-11 05:16:10 +00:00
|
|
|
unsigned long long range[2];
|
2018-04-02 07:59:24 +00:00
|
|
|
struct stat *stat_buf;
|
2016-11-02 00:23:40 +00:00
|
|
|
struct device_info *dev = c.devices + i;
|
|
|
|
u_int64_t bytes = dev->total_sectors * dev->sector_size;
|
|
|
|
int fd = dev->fd;
|
2014-01-11 05:16:10 +00:00
|
|
|
|
2018-04-02 07:59:24 +00:00
|
|
|
stat_buf = malloc(sizeof(struct stat));
|
2018-10-11 22:20:53 +00:00
|
|
|
if (stat_buf == NULL) {
|
|
|
|
MSG(1, "\tError: Malloc Failed for trim_stat_buf!!!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-04-02 07:59:24 +00:00
|
|
|
if (fstat(fd, stat_buf) < 0 ) {
|
2014-01-11 05:16:10 +00:00
|
|
|
MSG(1, "\tError: Failed to get the device stat!!!\n");
|
2018-04-02 07:59:24 +00:00
|
|
|
free(stat_buf);
|
2014-01-11 05:16:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-02 17:33:44 +00:00
|
|
|
range[0] = 0;
|
2016-10-18 21:31:32 +00:00
|
|
|
range[1] = bytes;
|
2016-09-02 17:33:44 +00:00
|
|
|
|
2014-07-23 00:28:14 +00:00
|
|
|
#if defined(WITH_BLKDISCARD) && defined(BLKDISCARD)
|
2016-11-02 00:23:40 +00:00
|
|
|
MSG(0, "Info: [%s] Discarding device\n", dev->path);
|
2018-04-02 07:59:24 +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)
|
2016-09-02 17:33:44 +00:00
|
|
|
if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
|
2014-11-04 09:10:54 +00:00
|
|
|
range[0], range[1]) < 0) {
|
|
|
|
MSG(0, "Info: fallocate(PUNCH_HOLE|KEEP_SIZE) is failed\n");
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-02 07:59:24 +00:00
|
|
|
free(stat_buf);
|
2014-01-11 05:16:10 +00:00
|
|
|
return 0;
|
2018-04-02 07:59:24 +00:00
|
|
|
} else if (S_ISBLK(stat_buf->st_mode)) {
|
|
|
|
if (dev->zoned_model != F2FS_ZONED_NONE) {
|
|
|
|
free(stat_buf);
|
2016-11-02 00:23:40 +00:00
|
|
|
return f2fs_reset_zones(i);
|
2018-04-02 07:59:24 +00:00
|
|
|
}
|
2016-06-16 21:39:18 +00:00
|
|
|
#ifdef BLKSECDISCARD
|
2016-09-02 17:33:44 +00:00
|
|
|
if (ioctl(fd, BLKSECDISCARD, &range) < 0) {
|
2016-06-16 21:39:18 +00:00
|
|
|
MSG(0, "Info: This device doesn't support BLKSECDISCARD\n");
|
|
|
|
} else {
|
2016-09-02 17:33:44 +00:00
|
|
|
MSG(0, "Info: Secure Discarded %lu MB\n",
|
2018-04-02 07:59:24 +00:00
|
|
|
(unsigned long)stat_buf->st_size >> 20);
|
|
|
|
free(stat_buf);
|
2016-06-16 21:39:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2016-09-02 17:33:44 +00:00
|
|
|
if (ioctl(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 {
|
2016-10-18 21:31:32 +00:00
|
|
|
MSG(0, "Info: Discarded %llu MB\n", range[1] >> 20);
|
2014-06-13 08:41:58 +00:00
|
|
|
}
|
2018-04-02 07:59:24 +00:00
|
|
|
} else {
|
|
|
|
free(stat_buf);
|
2014-01-11 05:16:10 +00:00
|
|
|
return -1;
|
2018-04-02 07:59:24 +00:00
|
|
|
}
|
2017-12-01 23:03:30 +00:00
|
|
|
#endif
|
2018-04-02 07:59:24 +00:00
|
|
|
free(stat_buf);
|
2014-05-14 00:02:55 +00:00
|
|
|
#endif
|
2014-01-11 05:16:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-11-02 00:23:40 +00:00
|
|
|
|
|
|
|
int f2fs_trim_devices(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c.ndevs; i++)
|
|
|
|
if (trim_device(i))
|
|
|
|
return -1;
|
2017-04-28 09:16:31 +00:00
|
|
|
c.trimmed = 1;
|
2016-11-02 00:23:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|