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