f2fs_format_utils.c revision 437dbf67730d2765c4dfc8539b07258e1ca3966f
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/stat.h>
16#include <linux/fs.h>
17
18#include "f2fs_fs.h"
19
20void f2fs_finalize_device()
21{
22	/*
23	 * We should call fsync() to flush out all the dirty pages
24	 * in the block device page cache.
25	 */
26	if (fsync(config.fd) < 0)
27		MSG(0, "\tError: Could not conduct fsync!!!\n");
28
29	if (close(config.fd) < 0)
30		MSG(0, "\tError: Failed to close device file!!!\n");
31
32}
33
34int f2fs_trim_device()
35{
36	unsigned long long range[2];
37	struct stat stat_buf;
38
39	if (!config.trim)
40		return 0;
41
42	range[0] = 0;
43	range[1] = config.total_sectors * DEFAULT_SECTOR_SIZE;
44
45	if (fstat(config.fd, &stat_buf) < 0 ) {
46		MSG(1, "\tError: Failed to get the device stat!!!\n");
47		return -1;
48	}
49
50	MSG(0, "Info: Discarding device\n");
51	if (S_ISREG(stat_buf.st_mode))
52		return 0;
53	else if (S_ISBLK(stat_buf.st_mode)) {
54		if (ioctl(config.fd, BLKDISCARD, &range) < 0)
55			MSG(0, "Info: This device doesn't support TRIM\n");
56	} else
57		return -1;
58	return 0;
59}
60
61