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