1 /**
2  * f2fs_format.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * Dual licensed under the GPL or LGPL version 2 licenses.
8  */
9 #define _LARGEFILE64_SOURCE
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #ifndef ANDROID_WINDOWS_HOST
18 #include <sys/mount.h>
19 #endif
20 #include <time.h>
21 #include <uuid/uuid.h>
22 #include <errno.h>
23 
24 #include "config.h"
25 #ifdef HAVE_LIBBLKID
26 #  include <blkid/blkid.h>
27 #endif
28 
29 #include "f2fs_fs.h"
30 #include "f2fs_format_utils.h"
31 
32 #ifdef WITH_ANDROID
33 #include <sparse/sparse.h>
34 extern struct sparse_file *f2fs_sparse_file;
35 #endif
36 
37 extern struct f2fs_configuration c;
38 static int force_overwrite = 0;
39 
40 INIT_FEATURE_TABLE;
41 
mkfs_usage()42 static void mkfs_usage()
43 {
44 	MSG(0, "\nUsage: mkfs.f2fs [options] device [sectors]\n");
45 	MSG(0, "[options]:\n");
46 	MSG(0, "  -a heap-based allocation [default:0]\n");
47 	MSG(0, "  -c [device path] up to 7 devices excepts meta device\n");
48 	MSG(0, "  -d debug level [default:0]\n");
49 	MSG(0, "  -e [cold file ext list] e.g. \"mp3,gif,mov\"\n");
50 	MSG(0, "  -E [hot file ext list] e.g. \"db\"\n");
51 	MSG(0, "  -f force overwrite the exist filesystem\n");
52 	MSG(0, "  -g add default options\n");
53 	MSG(0, "  -i extended node bitmap, node ratio is 20%% by default\n");
54 	MSG(0, "  -l label\n");
55 	MSG(0, "  -m support zoned block device [default:0]\n");
56 	MSG(0, "  -o overprovision ratio [default:5]\n");
57 	MSG(0, "  -O feature1[feature2,feature3,...] e.g. \"encrypt\"\n");
58 	MSG(0, "  -q quiet mode\n");
59 	MSG(0, "  -R root_owner [default: 0:0]\n");
60 	MSG(0, "  -s # of segments per section [default:1]\n");
61 	MSG(0, "  -S sparse mode\n");
62 	MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
63 	MSG(0, "  -w wanted sector size\n");
64 	MSG(0, "  -z # of sections per zone [default:1]\n");
65 	MSG(0, "  -V print the version number and exit\n");
66 	MSG(0, "sectors: number of sectors. [default: determined by device size]\n");
67 	exit(1);
68 }
69 
f2fs_show_info()70 static void f2fs_show_info()
71 {
72 	MSG(0, "\n\tF2FS-tools: mkfs.f2fs Ver: %s (%s)\n\n",
73 				F2FS_TOOLS_VERSION,
74 				F2FS_TOOLS_DATE);
75 	if (c.heap == 0)
76 		MSG(0, "Info: Disable heap-based policy\n");
77 
78 	MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
79 	if (c.extension_list[0])
80 		MSG(0, "Info: Add new cold file extension list\n");
81 	if (c.extension_list[1])
82 		MSG(0, "Info: Add new hot file extension list\n");
83 
84 	if (strlen(c.vol_label))
85 		MSG(0, "Info: Label = %s\n", c.vol_label);
86 	MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
87 
88 	if (c.defset == CONF_ANDROID)
89 		MSG(0, "Info: Set conf for android\n");
90 }
91 
add_default_options(void)92 static void add_default_options(void)
93 {
94 	switch (c.defset) {
95 	case CONF_ANDROID:
96 		/* -d1 -f -O encrypt -O quota -O verity -w 4096 -R 0:0 */
97 		c.dbg_lv = 1;
98 		force_overwrite = 1;
99 		c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
100 		c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
101 		c.feature |= cpu_to_le32(F2FS_FEATURE_VERITY);
102 		c.wanted_sector_size = 4096;
103 		c.root_uid = c.root_gid = 0;
104 		break;
105 	}
106 }
107 
f2fs_parse_options(int argc,char * argv[])108 static void f2fs_parse_options(int argc, char *argv[])
109 {
110 	static const char *option_string = "qa:c:d:e:E:g:il:mo:O:R:s:S:z:t:fw:V";
111 	int32_t option=0;
112 
113 	while ((option = getopt(argc,argv,option_string)) != EOF) {
114 		switch (option) {
115 		case 'q':
116 			c.dbg_lv = -1;
117 			break;
118 		case 'a':
119 			c.heap = atoi(optarg);
120 			break;
121 		case 'c':
122 			if (c.ndevs >= MAX_DEVICES) {
123 				MSG(0, "Error: Too many devices\n");
124 				mkfs_usage();
125 			}
126 
127 			if (strlen(optarg) > MAX_PATH_LEN) {
128 				MSG(0, "Error: device path should be less than "
129 					"%d characters\n", MAX_PATH_LEN);
130 				mkfs_usage();
131 			}
132 			c.devices[c.ndevs++].path = strdup(optarg);
133 			break;
134 		case 'd':
135 			c.dbg_lv = atoi(optarg);
136 			break;
137 		case 'e':
138 			c.extension_list[0] = strdup(optarg);
139 			break;
140 		case 'E':
141 			c.extension_list[1] = strdup(optarg);
142 			break;
143 		case 'g':
144 			if (!strcmp(optarg, "android"))
145 				c.defset = CONF_ANDROID;
146 			break;
147 		case 'i':
148 			c.large_nat_bitmap = 1;
149 			break;
150 		case 'l':		/*v: volume label */
151 			if (strlen(optarg) > 512) {
152 				MSG(0, "Error: Volume Label should be less than "
153 						"512 characters\n");
154 				mkfs_usage();
155 			}
156 			c.vol_label = optarg;
157 			break;
158 		case 'm':
159 			c.zoned_mode = 1;
160 			break;
161 		case 'o':
162 			c.overprovision = atof(optarg);
163 			break;
164 		case 'O':
165 			if (parse_feature(feature_table, optarg))
166 				mkfs_usage();
167 			break;
168 		case 'R':
169 			if (parse_root_owner(optarg, &c.root_uid, &c.root_gid))
170 				mkfs_usage();
171 			break;
172 		case 's':
173 			c.segs_per_sec = atoi(optarg);
174 			break;
175 		case 'S':
176 			c.device_size = atoll(optarg);
177 			c.device_size &= (~((u_int64_t)(F2FS_BLKSIZE - 1)));
178 			c.sparse_mode = 1;
179 			break;
180 		case 'z':
181 			c.secs_per_zone = atoi(optarg);
182 			break;
183 		case 't':
184 			c.trim = atoi(optarg);
185 			break;
186 		case 'f':
187 			force_overwrite = 1;
188 			break;
189 		case 'w':
190 			c.wanted_sector_size = atoi(optarg);
191 			break;
192 		case 'V':
193 			show_version("mkfs.f2fs");
194 			exit(0);
195 		default:
196 			MSG(0, "\tError: Unknown option %c\n",option);
197 			mkfs_usage();
198 			break;
199 		}
200 	}
201 
202 	add_default_options();
203 
204 	if (!(c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR))) {
205 		if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
206 			MSG(0, "\tInfo: project quota feature should always be "
207 				"enabled with extra attr feature\n");
208 			exit(1);
209 		}
210 		if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) {
211 			MSG(0, "\tInfo: inode checksum feature should always be "
212 				"enabled with extra attr feature\n");
213 			exit(1);
214 		}
215 		if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) {
216 			MSG(0, "\tInfo: flexible inline xattr feature should always be "
217 				"enabled with extra attr feature\n");
218 			exit(1);
219 		}
220 		if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
221 			MSG(0, "\tInfo: inode crtime feature should always be "
222 				"enabled with extra attr feature\n");
223 			exit(1);
224 		}
225 	}
226 
227 	if (optind >= argc) {
228 		MSG(0, "\tError: Device not specified\n");
229 		mkfs_usage();
230 	}
231 
232 	/* [0] : META, [1 to MAX_DEVICES - 1] : NODE/DATA */
233 	c.devices[0].path = strdup(argv[optind]);
234 
235 	if ((optind + 1) < argc) {
236 		if (c.ndevs > 1) {
237 			MSG(0, "\tError: Not support custom size on multi-devs.\n");
238 			mkfs_usage();
239 		}
240 		c.wanted_total_sectors = atoll(argv[optind+1]);
241 	}
242 
243 	if (c.sparse_mode)
244 		c.trim = 0;
245 
246 	if (c.zoned_mode)
247 		c.feature |= cpu_to_le32(F2FS_FEATURE_BLKZONED);
248 }
249 
250 #ifdef HAVE_LIBBLKID
f2fs_dev_is_overwrite(const char * device)251 static int f2fs_dev_is_overwrite(const char *device)
252 {
253 	const char	*type;
254 	blkid_probe	pr = NULL;
255 	int		ret = -1;
256 
257 	if (!device || !*device)
258 		return 0;
259 
260 	pr = blkid_new_probe_from_filename(device);
261 	if (!pr)
262 		goto out;
263 
264 	ret = blkid_probe_enable_partitions(pr, 1);
265 	if (ret < 0)
266 		goto out;
267 
268 	ret = blkid_do_fullprobe(pr);
269 	if (ret < 0)
270 		goto out;
271 
272 	/*
273 	 * Blkid returns 1 for nothing found and 0 when it finds a signature,
274 	 * but we want the exact opposite, so reverse the return value here.
275 	 *
276 	 * In addition print some useful diagnostics about what actually is
277 	 * on the device.
278 	 */
279 	if (ret) {
280 		ret = 0;
281 		goto out;
282 	}
283 
284 	if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
285 		MSG(0, "\t%s appears to contain an existing filesystem (%s).\n",
286 			device, type);
287 	} else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
288 		MSG(0, "\t%s appears to contain a partition table (%s).\n",
289 			device, type);
290 	} else {
291 		MSG(0, "\t%s appears to contain something weird according to blkid\n",
292 			device);
293 	}
294 	ret = 1;
295 out:
296 	if (pr)
297 		blkid_free_probe(pr);
298 	if (ret == -1)
299 		MSG(0, "\tprobe of %s failed, cannot detect existing filesystem.\n",
300 			device);
301 	return ret;
302 }
303 
f2fs_check_overwrite(void)304 static int f2fs_check_overwrite(void)
305 {
306 	int i;
307 
308 	for (i = 0; i < c.ndevs; i++)
309 		if (f2fs_dev_is_overwrite((char *)c.devices[i].path))
310 			return -1;
311 	return 0;
312 }
313 
314 #else
315 
f2fs_check_overwrite(void)316 static int f2fs_check_overwrite(void)
317 {
318 	return 0;
319 }
320 
321 #endif /* HAVE_LIBBLKID */
322 
main(int argc,char * argv[])323 int main(int argc, char *argv[])
324 {
325 	f2fs_init_configuration();
326 
327 	f2fs_parse_options(argc, argv);
328 
329 	f2fs_show_info();
330 
331 	c.func = MKFS;
332 
333 	if (!force_overwrite && f2fs_check_overwrite()) {
334 		MSG(0, "\tUse the -f option to force overwrite.\n");
335 		return -1;
336 	}
337 
338 	if (f2fs_devs_are_umounted() < 0) {
339 		if (errno != EBUSY)
340 			MSG(0, "\tError: Not available on mounted device!\n");
341 		return -1;
342 	}
343 
344 	if (f2fs_get_device_info() < 0)
345 		return -1;
346 
347 	/*
348 	 * Some options are mandatory for host-managed
349 	 * zoned block devices.
350 	 */
351 	if (c.zoned_model == F2FS_ZONED_HM && !c.zoned_mode) {
352 		MSG(0, "\tError: zoned block device feature is required\n");
353 		return -1;
354 	}
355 
356 	if (c.zoned_mode && !c.trim) {
357 		MSG(0, "\tError: Trim is required for zoned block devices\n");
358 		return -1;
359 	}
360 
361 	if (c.sparse_mode) {
362 		if (f2fs_init_sparse_file())
363 			return -1;
364 	}
365 
366 	if (f2fs_format_device() < 0)
367 		return -1;
368 
369 	if (f2fs_finalize_device() < 0)
370 		return -1;
371 
372 	MSG(0, "Info: format successful\n");
373 
374 	return 0;
375 }
376