1/**
2 * f2fs_format_utils.c
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 *             http://www.samsung.com/
6 *
7 * Dual licensed under the GPL or LGPL version 2 licenses.
8 */
9#define _LARGEFILE64_SOURCE
10
11#include <stdio.h>
12#include <unistd.h>
13#include <sys/ioctl.h>
14#include <sys/stat.h>
15
16#include "f2fs_fs.h"
17
18#ifdef HAVE_LINUX_FS_H
19#include <linux/fs.h>
20#endif
21
22int f2fs_trim_device()
23{
24	unsigned long long range[2];
25	struct stat stat_buf;
26
27	if (!config.trim)
28		return 0;
29
30	range[0] = 0;
31	range[1] = config.total_sectors * DEFAULT_SECTOR_SIZE;
32
33	if (fstat(config.fd, &stat_buf) < 0 ) {
34		MSG(1, "\tError: Failed to get the device stat!!!\n");
35		return -1;
36	}
37
38#if defined(WITH_BLKDISCARD) && defined(BLKDISCARD)
39	MSG(0, "Info: Discarding device\n");
40	if (S_ISREG(stat_buf.st_mode))
41		return 0;
42	else if (S_ISBLK(stat_buf.st_mode)) {
43		if (ioctl(config.fd, BLKDISCARD, &range) < 0) {
44			MSG(0, "Info: This device doesn't support TRIM\n");
45		} else {
46			MSG(0, "Info: Discarded %lu sectors\n",
47						config.total_sectors);
48		}
49	} else
50		return -1;
51#endif
52	return 0;
53}
54
55