f2fs_format_utils.c revision 1ddf6367a3b5e8e586dee1bcbf9c363439ea3ec8
1/**
2 * f2fs_format_utils.c
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 *             http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#define _LARGEFILE64_SOURCE
12
13#include <stdio.h>
14#include <unistd.h>
15#include <sys/ioctl.h>
16#include <sys/stat.h>
17
18#include "f2fs_fs.h"
19
20int f2fs_trim_device()
21{
22	unsigned long long range[2];
23	struct stat stat_buf;
24
25	if (!config.trim)
26		return 0;
27
28	range[0] = 0;
29	range[1] = config.total_sectors * DEFAULT_SECTOR_SIZE;
30
31	if (fstat(config.fd, &stat_buf) < 0 ) {
32		MSG(1, "\tError: Failed to get the device stat!!!\n");
33		return -1;
34	}
35
36#if defined(BLKDISCARD)
37	MSG(0, "Info: Discarding device\n");
38	if (S_ISREG(stat_buf.st_mode))
39		return 0;
40	else if (S_ISBLK(stat_buf.st_mode)) {
41		if (ioctl(config.fd, BLKDISCARD, &range) < 0)
42			MSG(0, "Info: This device doesn't support TRIM\n");
43	} else
44		return -1;
45#endif
46	return 0;
47}
48
49