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 #ifndef _LARGEFILE_SOURCE
10 #define _LARGEFILE_SOURCE
11 #endif
12 #ifndef _LARGEFILE64_SOURCE
13 #define _LARGEFILE64_SOURCE
14 #endif
15 #ifndef _GNU_SOURCE
16 #define _GNU_SOURCE
17 #endif
18 
19 #ifndef _FILE_OFFSET_BITS
20 #define _FILE_OFFSET_BITS 64
21 #endif
22 
23 #include <f2fs_fs.h>
24 
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #ifndef ANDROID_WINDOWS_HOST
29 #include <sys/ioctl.h>
30 #endif
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 
34 #ifdef HAVE_LINUX_FS_H
35 #include <linux/fs.h>
36 #endif
37 #ifdef HAVE_LINUX_FALLOC_H
38 #include <linux/falloc.h>
39 #endif
40 
41 #ifndef BLKDISCARD
42 #define BLKDISCARD	_IO(0x12,119)
43 #endif
44 #ifndef BLKSECDISCARD
45 #define BLKSECDISCARD	_IO(0x12,125)
46 #endif
47 
trim_device(int i)48 static int trim_device(int i)
49 {
50 #ifndef ANDROID_WINDOWS_HOST
51 	unsigned long long range[2];
52 	struct stat *stat_buf;
53 	struct device_info *dev = c.devices + i;
54 	u_int64_t bytes = dev->total_sectors * dev->sector_size;
55 	int fd = dev->fd;
56 
57 	stat_buf = malloc(sizeof(struct stat));
58 	if (stat_buf == NULL) {
59 		MSG(1, "\tError: Malloc Failed for trim_stat_buf!!!\n");
60 		return -1;
61 	}
62 
63 	if (fstat(fd, stat_buf) < 0 ) {
64 		MSG(1, "\tError: Failed to get the device stat!!!\n");
65 		free(stat_buf);
66 		return -1;
67 	}
68 
69 	range[0] = 0;
70 	range[1] = bytes;
71 
72 #if defined(WITH_BLKDISCARD) && defined(BLKDISCARD)
73 	MSG(0, "Info: [%s] Discarding device\n", dev->path);
74 	if (S_ISREG(stat_buf->st_mode)) {
75 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
76 		if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
77 				range[0], range[1]) < 0) {
78 			MSG(0, "Info: fallocate(PUNCH_HOLE|KEEP_SIZE) is failed\n");
79 		}
80 #endif
81 		free(stat_buf);
82 		return 0;
83 	} else if (S_ISBLK(stat_buf->st_mode)) {
84 		if (dev->zoned_model != F2FS_ZONED_NONE) {
85 			free(stat_buf);
86 			return f2fs_reset_zones(i);
87 		}
88 #ifdef BLKSECDISCARD
89 		if (ioctl(fd, BLKSECDISCARD, &range) < 0) {
90 			MSG(0, "Info: This device doesn't support BLKSECDISCARD\n");
91 		} else {
92 			MSG(0, "Info: Secure Discarded %lu MB\n",
93 					(unsigned long)stat_buf->st_size >> 20);
94 			free(stat_buf);
95 			return 0;
96 		}
97 #endif
98 		if (ioctl(fd, BLKDISCARD, &range) < 0) {
99 			MSG(0, "Info: This device doesn't support BLKDISCARD\n");
100 		} else {
101 			MSG(0, "Info: Discarded %llu MB\n", range[1] >> 20);
102 		}
103 	} else {
104 		free(stat_buf);
105 		return -1;
106 	}
107 #endif
108 	free(stat_buf);
109 #endif
110 	return 0;
111 }
112 
f2fs_trim_devices(void)113 int f2fs_trim_devices(void)
114 {
115 	int i;
116 
117 	for (i = 0; i < c.ndevs; i++)
118 		if (trim_device(i))
119 			return -1;
120 	c.trimmed = 1;
121 	return 0;
122 }
123