• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdbool.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #ifndef ANDROID_WINDOWS_HOST
19 #include <sys/mount.h>
20 #endif
21 #include <time.h>
22 #include <uuid.h>
23 #include <errno.h>
24 #include <getopt.h>
25 
26 #include "config.h"
27 #ifdef HAVE_LIBBLKID
28 #include <blkid.h>
29 #endif
30 
31 #include "f2fs_fs.h"
32 #include "quota.h"
33 #include "f2fs_format_utils.h"
34 
35 #ifdef HAVE_SYS_UTSNAME_H
36 #include <sys/utsname.h>
37 #endif
38 #ifdef WITH_ANDROID
39 #include <sparse/sparse.h>
40 extern struct sparse_file *f2fs_sparse_file;
41 #endif
42 
43 extern struct f2fs_configuration c;
44 static int force_overwrite = 0;
45 
46 INIT_FEATURE_TABLE;
47 
mkfs_usage()48 static void mkfs_usage()
49 {
50 	MSG(0, "\nUsage: mkfs.f2fs [options] device [sectors]\n");
51 	MSG(0, "[options]:\n");
52 	MSG(0, "  -a heap-based allocation [default:0]\n");
53 	MSG(0, "  -c device1[,device2,...] up to 7 additional devices, except meta device\n");
54 	MSG(0, "  -d debug level [default:0]\n");
55 	MSG(0, "  -e [cold file ext list] e.g. \"mp3,gif,mov\"\n");
56 	MSG(0, "  -E [hot file ext list] e.g. \"db\"\n");
57 	MSG(0, "  -f force overwrite of the existing filesystem\n");
58 	MSG(0, "  -g add default options\n");
59 	MSG(0, "  -i extended node bitmap, node ratio is 20%% by default\n");
60 	MSG(0, "  -l label\n");
61 	MSG(0, "  -U uuid\n");
62 	MSG(0, "  -m support zoned block device [default:0]\n");
63 	MSG(0, "  -o overprovision percentage [default:auto]\n");
64 	MSG(0, "  -O feature1[,feature2,...] e.g. \"encrypt\"\n");
65 	MSG(0, "  -C [encoding[:flag1,...]] Support casefolding with optional flags\n");
66 	MSG(0, "  -q quiet mode\n");
67 	MSG(0, "  -r set checkpointing seed (srand()) to 0\n");
68 	MSG(0, "  -R root_owner [default: 0:0]\n");
69 	MSG(0, "  -s # of segments per section [default:1]\n");
70 	MSG(0, "  -S sparse mode\n");
71 	MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
72 	MSG(0, "  -T timestamps\n");
73 	MSG(0, "  -w wanted sector size\n");
74 	MSG(0, "  -z # of sections per zone [default:1]\n");
75 	MSG(0, "  -V print the version number and exit\n");
76 	MSG(0, "sectors: number of sectors [default: determined by device size]\n");
77 	exit(1);
78 }
79 
f2fs_show_info()80 static void f2fs_show_info()
81 {
82 	MSG(0, "\n    F2FS-tools: mkfs.f2fs Ver: %s (%s)\n\n",
83 				F2FS_TOOLS_VERSION,
84 				F2FS_TOOLS_DATE);
85 	if (c.heap == 0)
86 		MSG(0, "Info: Disable heap-based policy\n");
87 
88 	MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
89 	if (c.extension_list[0])
90 		MSG(0, "Info: Add new cold file extension list\n");
91 	if (c.extension_list[1])
92 		MSG(0, "Info: Add new hot file extension list\n");
93 
94 	if (strlen(c.vol_label))
95 		MSG(0, "Info: Label = %s\n", c.vol_label);
96 	MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
97 
98 	if (c.defset == CONF_ANDROID)
99 		MSG(0, "Info: Set conf for android\n");
100 
101 	if (c.feature & le32_to_cpu(F2FS_FEATURE_CASEFOLD))
102 		MSG(0, "Info: Enable %s with casefolding\n",
103 					f2fs_encoding2str(c.s_encoding));
104 	if (c.feature & le32_to_cpu(F2FS_FEATURE_PRJQUOTA))
105 		MSG(0, "Info: Enable Project quota\n");
106 
107 	if (c.feature & le32_to_cpu(F2FS_FEATURE_COMPRESSION))
108 		MSG(0, "Info: Enable Compression\n");
109 }
110 
111 #if defined(ANDROID_TARGET) && defined(HAVE_SYS_UTSNAME_H)
kernel_version_over(unsigned int min_major,unsigned int min_minor)112 static bool kernel_version_over(unsigned int min_major, unsigned int min_minor)
113 {
114 	unsigned int major, minor;
115 	struct utsname uts;
116 
117 	if ((uname(&uts) != 0) ||
118 			(sscanf(uts.release, "%u.%u", &major, &minor) != 2))
119 		return false;
120 	if (major > min_major)
121 		return true;
122 	if (major == min_major && minor >= min_minor)
123 		return true;
124 	return false;
125 }
126 #else
kernel_version_over(unsigned int UNUSED (min_major),unsigned int UNUSED (min_minor))127 static bool kernel_version_over(unsigned int UNUSED(min_major),
128 				unsigned int UNUSED(min_minor))
129 {
130 	return false;
131 }
132 #endif
133 
add_default_options(void)134 static void add_default_options(void)
135 {
136 	switch (c.defset) {
137 	case CONF_ANDROID:
138 		/* -d1 -f -w 4096 -R 0:0 */
139 		c.dbg_lv = 1;
140 		force_overwrite = 1;
141 		c.wanted_sector_size = 4096;
142 		c.root_uid = c.root_gid = 0;
143 
144 		/* RO doesn't need any other features */
145 		if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
146 			return;
147 
148 		/* -O encrypt -O project_quota,extra_attr,{quota} -O verity */
149 		c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
150 		if (!kernel_version_over(4, 14))
151 			c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
152 		c.feature |= cpu_to_le32(F2FS_FEATURE_PRJQUOTA);
153 		c.feature |= cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR);
154 		c.feature |= cpu_to_le32(F2FS_FEATURE_VERITY);
155 		break;
156 	}
157 #ifdef CONF_CASEFOLD
158 	c.s_encoding = F2FS_ENC_UTF8_12_1;
159 	c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
160 #endif
161 #ifdef CONF_PROJID
162 	c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
163 	c.feature |= cpu_to_le32(F2FS_FEATURE_PRJQUOTA);
164 	c.feature |= cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR);
165 #endif
166 
167 	if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO))
168 		c.quota_bits = QUOTA_USR_BIT | QUOTA_GRP_BIT;
169 	if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
170 		c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
171 		c.quota_bits |= QUOTA_PRJ_BIT;
172 	}
173 }
174 
f2fs_parse_options(int argc,char * argv[])175 static void f2fs_parse_options(int argc, char *argv[])
176 {
177 	static const char *option_string = "qa:c:C:d:e:E:g:hil:mo:O:rR:s:S:z:t:T:U:Vfw:";
178 	static const struct option long_opts[] = {
179 		{ .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
180 		{ .name = NULL, .has_arg = 0, .flag = NULL, .val = 0 }
181 	};
182 	int32_t option=0;
183 	int val;
184 	char *token;
185 
186 	while ((option = getopt_long(argc,argv,option_string,long_opts,NULL)) != EOF) {
187 		switch (option) {
188 		case 'q':
189 			c.dbg_lv = -1;
190 			break;
191 		case 'a':
192 			c.heap = atoi(optarg);
193 			break;
194 		case 'c':
195 			if (c.ndevs >= MAX_DEVICES) {
196 				MSG(0, "Error: Too many devices\n");
197 				mkfs_usage();
198 			}
199 
200 			if (strlen(optarg) > MAX_PATH_LEN) {
201 				MSG(0, "Error: device path should be less than "
202 					"%d characters\n", MAX_PATH_LEN);
203 				mkfs_usage();
204 			}
205 			c.devices[c.ndevs++].path = strdup(optarg);
206 			break;
207 		case 'd':
208 			c.dbg_lv = atoi(optarg);
209 			break;
210 		case 'e':
211 			c.extension_list[0] = strdup(optarg);
212 			break;
213 		case 'E':
214 			c.extension_list[1] = strdup(optarg);
215 			break;
216 		case 'g':
217 			if (!strcmp(optarg, "android"))
218 				c.defset = CONF_ANDROID;
219 			break;
220 		case 'h':
221 			mkfs_usage();
222 			break;
223 		case 'i':
224 			c.large_nat_bitmap = 1;
225 			break;
226 		case 'l':		/*v: volume label */
227 			if (strlen(optarg) > 512) {
228 				MSG(0, "Error: Volume Label should be less than "
229 						"512 characters\n");
230 				mkfs_usage();
231 			}
232 			c.vol_label = optarg;
233 			break;
234 		case 'm':
235 			c.zoned_mode = 1;
236 			break;
237 		case 'o':
238 			c.overprovision = atof(optarg);
239 			break;
240 		case 'O':
241 			if (parse_feature(feature_table, optarg))
242 				mkfs_usage();
243 			break;
244 		case 'r':
245 			c.fake_seed = 1;
246 			break;
247 		case 'R':
248 			if (parse_root_owner(optarg, &c.root_uid, &c.root_gid))
249 				mkfs_usage();
250 			break;
251 		case 's':
252 			c.segs_per_sec = atoi(optarg);
253 			break;
254 		case 'S':
255 			c.device_size = atoll(optarg);
256 			c.device_size &= (~((u_int64_t)(F2FS_BLKSIZE - 1)));
257 			c.sparse_mode = 1;
258 			break;
259 		case 'z':
260 			c.secs_per_zone = atoi(optarg);
261 			break;
262 		case 't':
263 			c.trim = atoi(optarg);
264 			break;
265 		case 'T':
266 			c.fixed_time = strtoul(optarg, NULL, 0);
267 			break;
268 		case 'U':
269 			c.vol_uuid = strdup(optarg);
270 			break;
271 		case 'f':
272 			force_overwrite = 1;
273 			break;
274 		case 'w':
275 			c.wanted_sector_size = atoi(optarg);
276 			break;
277 		case 'V':
278 			show_version("mkfs.f2fs");
279 			exit(0);
280 		case 'C':
281 			token = strtok(optarg, ":");
282 			val = f2fs_str2encoding(token);
283 			if (val < 0) {
284 				MSG(0, "\tError: Unknown encoding %s\n", token);
285 				mkfs_usage();
286 			}
287 			c.s_encoding = val;
288 			token = strtok(NULL, "");
289 			val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
290 			if (val) {
291 				MSG(0, "\tError: Unknown flag %s\n",token);
292 				mkfs_usage();
293 			}
294 			c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
295 			break;
296 		default:
297 			MSG(0, "\tError: Unknown option %c\n",option);
298 			mkfs_usage();
299 			break;
300 		}
301 	}
302 
303 	add_default_options();
304 
305 	if (!(c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR))) {
306 		if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
307 			MSG(0, "\tInfo: project quota feature should always be "
308 				"enabled with extra attr feature\n");
309 			exit(1);
310 		}
311 		if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) {
312 			MSG(0, "\tInfo: inode checksum feature should always be "
313 				"enabled with extra attr feature\n");
314 			exit(1);
315 		}
316 		if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) {
317 			MSG(0, "\tInfo: flexible inline xattr feature should always be "
318 				"enabled with extra attr feature\n");
319 			exit(1);
320 		}
321 		if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
322 			MSG(0, "\tInfo: inode crtime feature should always be "
323 				"enabled with extra attr feature\n");
324 			exit(1);
325 		}
326 		if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
327 			MSG(0, "\tInfo: compression feature should always be "
328 				"enabled with extra attr feature\n");
329 			exit(1);
330 		}
331 	}
332 
333 	if (optind >= argc) {
334 		MSG(0, "\tError: Device not specified\n");
335 		mkfs_usage();
336 	}
337 
338 	/* [0] : META, [1 to MAX_DEVICES - 1] : NODE/DATA */
339 	c.devices[0].path = strdup(argv[optind]);
340 
341 	if ((optind + 1) < argc) {
342 		if (c.ndevs > 1) {
343 			MSG(0, "\tError: Not support custom size on multi-devs.\n");
344 			mkfs_usage();
345 		}
346 		c.wanted_total_sectors = atoll(argv[optind+1]);
347 	}
348 
349 	if (c.sparse_mode)
350 		c.trim = 0;
351 
352 	if (c.zoned_mode)
353 		c.feature |= cpu_to_le32(F2FS_FEATURE_BLKZONED);
354 }
355 
356 #ifdef HAVE_LIBBLKID
f2fs_dev_is_overwrite(const char * device)357 static int f2fs_dev_is_overwrite(const char *device)
358 {
359 	const char	*type;
360 	blkid_probe	pr = NULL;
361 	int		ret = -1;
362 
363 	if (!device || !*device)
364 		return 0;
365 
366 	pr = blkid_new_probe_from_filename(device);
367 	if (!pr)
368 		goto out;
369 
370 	ret = blkid_probe_enable_partitions(pr, 1);
371 	if (ret < 0)
372 		goto out;
373 
374 	ret = blkid_do_fullprobe(pr);
375 	if (ret < 0)
376 		goto out;
377 
378 	/*
379 	 * Blkid returns 1 for nothing found and 0 when it finds a signature,
380 	 * but we want the exact opposite, so reverse the return value here.
381 	 *
382 	 * In addition print some useful diagnostics about what actually is
383 	 * on the device.
384 	 */
385 	if (ret) {
386 		ret = 0;
387 		goto out;
388 	}
389 
390 	if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
391 		MSG(0, "\t%s appears to contain an existing filesystem (%s).\n",
392 			device, type);
393 	} else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
394 		MSG(0, "\t%s appears to contain a partition table (%s).\n",
395 			device, type);
396 	} else {
397 		MSG(0, "\t%s appears to contain something weird according to blkid\n",
398 			device);
399 	}
400 	ret = 1;
401 out:
402 	if (pr)
403 		blkid_free_probe(pr);
404 	if (ret == -1)
405 		MSG(0, "\tprobe of %s failed, cannot detect existing filesystem.\n",
406 			device);
407 	return ret;
408 }
409 
f2fs_check_overwrite(void)410 static int f2fs_check_overwrite(void)
411 {
412 	int i;
413 
414 	for (i = 0; i < c.ndevs; i++)
415 		if (f2fs_dev_is_overwrite((char *)c.devices[i].path))
416 			return -1;
417 	return 0;
418 }
419 
420 #else
421 
f2fs_check_overwrite(void)422 static int f2fs_check_overwrite(void)
423 {
424 	return 0;
425 }
426 
427 #endif /* HAVE_LIBBLKID */
428 
main(int argc,char * argv[])429 int main(int argc, char *argv[])
430 {
431 	f2fs_init_configuration();
432 
433 	f2fs_parse_options(argc, argv);
434 
435 	f2fs_show_info();
436 
437 	c.func = MKFS;
438 
439 	if (f2fs_devs_are_umounted() < 0) {
440 		if (errno != EBUSY)
441 			MSG(0, "\tError: Not available on mounted device!\n");
442 		goto err_format;
443 	}
444 
445 	if (f2fs_get_device_info() < 0)
446 		return -1;
447 
448 	if (f2fs_check_overwrite()) {
449 		char *zero_buf = NULL;
450 		int i;
451 
452 		if (!force_overwrite) {
453 			MSG(0, "\tUse the -f option to force overwrite.\n");
454 			goto err_format;
455 		}
456 		zero_buf = calloc(F2FS_BLKSIZE, 1);
457 		if (!zero_buf) {
458 			MSG(0, "\tError: Fail to allocate zero buffer.\n");
459 			goto err_format;
460 		}
461 		/* wipe out other FS magics mostly first 4MB space */
462 		for (i = 0; i < 1024; i++)
463 			if (dev_fill_block(zero_buf, i))
464 				break;
465 		free(zero_buf);
466 		if (i != 1024) {
467 			MSG(0, "\tError: Fail to fill zeros till %d.\n", i);
468 			goto err_format;
469 		}
470 		if (f2fs_fsync_device())
471 			goto err_format;
472 	}
473 
474 	if (f2fs_get_f2fs_info() < 0)
475 		goto err_format;
476 
477 	/*
478 	 * Some options are mandatory for host-managed
479 	 * zoned block devices.
480 	 */
481 	if (c.zoned_model == F2FS_ZONED_HM && !c.zoned_mode) {
482 		MSG(0, "\tError: zoned block device feature is required\n");
483 		goto err_format;
484 	}
485 
486 	if (c.zoned_mode && !c.trim) {
487 		MSG(0, "\tError: Trim is required for zoned block devices\n");
488 		goto err_format;
489 	}
490 
491 	if (f2fs_format_device() < 0)
492 		goto err_format;
493 
494 	if (f2fs_finalize_device() < 0)
495 		goto err_format;
496 
497 	MSG(0, "Info: format successful\n");
498 
499 	return 0;
500 
501 err_format:
502 	f2fs_release_sparse_resource();
503 	return -1;
504 }
505