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 #include <stdbool.h>
29 #ifndef ANDROID_WINDOWS_HOST
30 #include <sys/ioctl.h>
31 #endif
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35 #ifdef HAVE_LINUX_FS_H
36 #include <linux/fs.h>
37 #endif
38 #ifdef HAVE_LINUX_FALLOC_H
39 #include <linux/falloc.h>
40 #endif
41
42 #ifndef BLKDISCARD
43 #define BLKDISCARD _IO(0x12,119)
44 #endif
45 #ifndef BLKSECDISCARD
46 #define BLKSECDISCARD _IO(0x12,125)
47 #endif
48
trim_device(int i)49 static int trim_device(int i)
50 {
51 #ifndef ANDROID_WINDOWS_HOST
52 unsigned long long range[2];
53 struct stat *stat_buf;
54 struct device_info *dev = c.devices + i;
55 u_int64_t bytes = dev->total_sectors * dev->sector_size;
56 int fd = dev->fd;
57
58 stat_buf = malloc(sizeof(struct stat));
59 if (stat_buf == NULL) {
60 MSG(1, "\tError: Malloc Failed for trim_stat_buf!!!\n");
61 return -1;
62 }
63
64 if (fstat(fd, stat_buf) < 0 ) {
65 MSG(1, "\tError: Failed to get the device stat!!!\n");
66 free(stat_buf);
67 return -1;
68 }
69
70 range[0] = 0;
71 range[1] = bytes;
72
73 #if defined(WITH_BLKDISCARD) && defined(BLKDISCARD)
74 MSG(0, "Info: [%s] Discarding device\n", dev->path);
75 if (S_ISREG(stat_buf->st_mode)) {
76 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE)
77 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
78 range[0], range[1]) < 0) {
79 MSG(0, "Info: fallocate(PUNCH_HOLE|KEEP_SIZE) is failed\n");
80 }
81 #endif
82 free(stat_buf);
83 return 0;
84 } else if (S_ISBLK(stat_buf->st_mode)) {
85 if (dev->zoned_model != F2FS_ZONED_NONE) {
86 free(stat_buf);
87 return f2fs_reset_zones(i);
88 }
89 #ifdef BLKSECDISCARD
90 if (ioctl(fd, BLKSECDISCARD, &range) < 0) {
91 MSG(0, "Info: This device doesn't support BLKSECDISCARD\n");
92 } else {
93 MSG(0, "Info: Secure Discarded %lu MB\n",
94 (unsigned long)stat_buf->st_size >> 20);
95 free(stat_buf);
96 return 0;
97 }
98 #endif
99 if (ioctl(fd, BLKDISCARD, &range) < 0) {
100 MSG(0, "Info: This device doesn't support BLKDISCARD\n");
101 } else {
102 MSG(0, "Info: Discarded %llu MB\n", range[1] >> 20);
103 }
104 } else {
105 free(stat_buf);
106 return -1;
107 }
108 #endif
109 free(stat_buf);
110 #endif
111 return 0;
112 }
113
is_wiped_device(int i)114 static bool is_wiped_device(int i)
115 {
116 #ifdef WITH_ANDROID
117 struct device_info *dev = c.devices + i;
118 int fd = dev->fd;
119 char *buf, *zero_buf;
120 bool wiped = true;
121 int nblocks = 4096; /* 16MB size */
122 int j;
123
124 buf = malloc(F2FS_BLKSIZE);
125 if (buf == NULL) {
126 MSG(1, "\tError: Malloc Failed for buf!!!\n");
127 return false;
128 }
129 zero_buf = calloc(1, F2FS_BLKSIZE);
130 if (zero_buf == NULL) {
131 MSG(1, "\tError: Calloc Failed for zero buf!!!\n");
132 free(buf);
133 return false;
134 }
135
136 if (lseek(fd, 0, SEEK_SET) < 0) {
137 free(zero_buf);
138 free(buf);
139 return false;
140 }
141
142 /* check first n blocks */
143 for (j = 0; j < nblocks; j++) {
144 if (read(fd, buf, F2FS_BLKSIZE) != F2FS_BLKSIZE ||
145 memcmp(buf, zero_buf, F2FS_BLKSIZE)) {
146 wiped = false;
147 break;
148 }
149 }
150 free(zero_buf);
151 free(buf);
152
153 if (wiped)
154 MSG(0, "Info: Found all zeros in first %d blocks\n", nblocks);
155 return wiped;
156 #else
157 return false;
158 #endif
159 }
160
f2fs_trim_devices(void)161 int f2fs_trim_devices(void)
162 {
163 int i;
164
165 for (i = 0; i < c.ndevs; i++) {
166 if (!is_wiped_device(i) && trim_device(i))
167 return -1;
168 }
169 c.trimmed = 1;
170 return 0;
171 }
172