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