4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * Dual licensed under the GPL or LGPL version 2 licenses.
9 #define _LARGEFILE64_SOURCE
18 #ifdef HAVE_SYS_MOUNT_H
19 #include <sys/mount.h>
28 #include <blkid/blkid.h>
30 #ifdef HAVE_UUID_UUID_H
31 #include <uuid/uuid.h>
35 #include "f2fs_format_utils.h"
37 #ifdef HAVE_SYS_UTSNAME_H
38 #include <sys/utsname.h>
40 #ifdef HAVE_SPARSE_SPARSE_H
41 #include <sparse/sparse.h>
42 extern struct sparse_file *f2fs_sparse_file;
45 extern struct f2fs_configuration c;
46 static int force_overwrite = 0;
50 static void mkfs_usage()
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");
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, " -Z # of reserved sections [default:auto]\n");
79 MSG(0, "sectors: number of sectors [default: determined by device size]\n");
83 static void f2fs_show_info()
85 MSG(0, "\n F2FS-tools: mkfs.f2fs Ver: %s (%s)\n\n",
89 MSG(0, "Info: Disable heap-based policy\n");
91 MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
92 if (c.extension_list[0])
93 MSG(0, "Info: Add new cold file extension list\n");
94 if (c.extension_list[1])
95 MSG(0, "Info: Add new hot file extension list\n");
97 if (strlen(c.vol_label))
98 MSG(0, "Info: Label = %s\n", c.vol_label);
99 MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
101 if (c.defset == CONF_ANDROID)
102 MSG(0, "Info: Set conf for android\n");
104 if (c.feature & le32_to_cpu(F2FS_FEATURE_CASEFOLD))
105 MSG(0, "Info: Enable %s with casefolding\n",
106 f2fs_encoding2str(c.s_encoding));
107 if (c.feature & le32_to_cpu(F2FS_FEATURE_PRJQUOTA))
108 MSG(0, "Info: Enable Project quota\n");
110 if (c.feature & le32_to_cpu(F2FS_FEATURE_COMPRESSION))
111 MSG(0, "Info: Enable Compression\n");
114 #if defined(ANDROID_TARGET) && defined(HAVE_SYS_UTSNAME_H)
115 static bool kernel_version_over(unsigned int min_major, unsigned int min_minor)
117 unsigned int major, minor;
120 if ((uname(&uts) != 0) ||
121 (sscanf(uts.release, "%u.%u", &major, &minor) != 2))
123 if (major > min_major)
125 if (major == min_major && minor >= min_minor)
130 static bool kernel_version_over(unsigned int UNUSED(min_major),
131 unsigned int UNUSED(min_minor))
137 static void add_default_options(void)
141 /* -d1 -f -w 4096 -R 0:0 */
144 c.wanted_sector_size = 4096;
145 c.root_uid = c.root_gid = 0;
147 /* RO doesn't need any other features */
148 if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
151 /* -O encrypt -O project_quota,extra_attr,{quota} -O verity */
152 c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
153 if (!kernel_version_over(4, 14))
154 c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
155 c.feature |= cpu_to_le32(F2FS_FEATURE_PRJQUOTA);
156 c.feature |= cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR);
157 c.feature |= cpu_to_le32(F2FS_FEATURE_VERITY);
161 c.s_encoding = F2FS_ENC_UTF8_12_1;
162 c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
165 c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
166 c.feature |= cpu_to_le32(F2FS_FEATURE_PRJQUOTA);
167 c.feature |= cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR);
170 if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO))
171 c.quota_bits = QUOTA_USR_BIT | QUOTA_GRP_BIT;
172 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
173 c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
174 c.quota_bits |= QUOTA_PRJ_BIT;
178 static void f2fs_parse_options(int argc, char *argv[])
180 static const char *option_string = "qa:c:C:d:e:E:g:hil:mo:O:rR:s:S:z:t:T:U:Vfw:Z:";
181 static const struct option long_opts[] = {
182 { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
183 { .name = NULL, .has_arg = 0, .flag = NULL, .val = 0 }
189 while ((option = getopt_long(argc,argv,option_string,long_opts,NULL)) != EOF) {
195 c.heap = atoi(optarg);
198 if (c.ndevs >= MAX_DEVICES) {
199 MSG(0, "Error: Too many devices\n");
203 if (strlen(optarg) > MAX_PATH_LEN) {
204 MSG(0, "Error: device path should be less than "
205 "%d characters\n", MAX_PATH_LEN);
208 c.devices[c.ndevs++].path = strdup(optarg);
211 c.dbg_lv = atoi(optarg);
214 c.extension_list[0] = strdup(optarg);
217 c.extension_list[1] = strdup(optarg);
220 if (!strcmp(optarg, "android"))
221 c.defset = CONF_ANDROID;
227 c.large_nat_bitmap = 1;
229 case 'l': /*v: volume label */
230 if (strlen(optarg) > 512) {
231 MSG(0, "Error: Volume Label should be less than "
235 c.vol_label = optarg;
241 c.overprovision = atof(optarg);
244 if (parse_feature(feature_table, optarg))
251 if (parse_root_owner(optarg, &c.root_uid, &c.root_gid))
255 c.segs_per_sec = atoi(optarg);
258 c.device_size = atoll(optarg);
259 c.device_size &= (~((uint64_t)(F2FS_BLKSIZE - 1)));
263 c.secs_per_zone = atoi(optarg);
266 c.trim = atoi(optarg);
269 c.fixed_time = strtoul(optarg, NULL, 0);
272 c.vol_uuid = strdup(optarg);
278 c.wanted_sector_size = atoi(optarg);
281 show_version("mkfs.f2fs");
284 token = strtok(optarg, ":");
285 val = f2fs_str2encoding(token);
287 MSG(0, "\tError: Unknown encoding %s\n", token);
291 token = strtok(NULL, "");
292 val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
294 MSG(0, "\tError: Unknown flag %s\n",token);
297 c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
300 c.conf_reserved_sections = atoi(optarg);
303 MSG(0, "\tError: Unknown option %c\n",option);
309 add_default_options();
311 if (!(c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR))) {
312 if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
313 MSG(0, "\tInfo: project quota feature should always be "
314 "enabled with extra attr feature\n");
317 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) {
318 MSG(0, "\tInfo: inode checksum feature should always be "
319 "enabled with extra attr feature\n");
322 if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) {
323 MSG(0, "\tInfo: flexible inline xattr feature should always be "
324 "enabled with extra attr feature\n");
327 if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
328 MSG(0, "\tInfo: inode crtime feature should always be "
329 "enabled with extra attr feature\n");
332 if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
333 MSG(0, "\tInfo: compression feature should always be "
334 "enabled with extra attr feature\n");
339 if (optind >= argc) {
340 MSG(0, "\tError: Device not specified\n");
344 /* [0] : META, [1 to MAX_DEVICES - 1] : NODE/DATA */
345 c.devices[0].path = strdup(argv[optind]);
347 if ((optind + 1) < argc) {
349 MSG(0, "\tError: Not support custom size on multi-devs.\n");
352 c.wanted_total_sectors = atoll(argv[optind+1]);
359 c.feature |= cpu_to_le32(F2FS_FEATURE_BLKZONED);
363 static int f2fs_dev_is_overwrite(const char *device)
366 blkid_probe pr = NULL;
369 if (!device || !*device)
372 pr = blkid_new_probe_from_filename(device);
376 ret = blkid_probe_enable_partitions(pr, 1);
380 ret = blkid_do_fullprobe(pr);
385 * Blkid returns 1 for nothing found and 0 when it finds a signature,
386 * but we want the exact opposite, so reverse the return value here.
388 * In addition print some useful diagnostics about what actually is
396 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
397 MSG(0, "\t%s appears to contain an existing filesystem (%s).\n",
399 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
400 MSG(0, "\t%s appears to contain a partition table (%s).\n",
403 MSG(0, "\t%s appears to contain something weird according to blkid\n",
409 blkid_free_probe(pr);
411 MSG(0, "\tprobe of %s failed, cannot detect existing filesystem.\n",
416 static int f2fs_check_overwrite(void)
420 for (i = 0; i < c.ndevs; i++)
421 if (f2fs_dev_is_overwrite((char *)c.devices[i].path))
428 static int f2fs_check_overwrite(void)
433 #endif /* HAVE_LIBBLKID */
435 int main(int argc, char *argv[])
437 f2fs_init_configuration();
439 f2fs_parse_options(argc, argv);
445 if (f2fs_devs_are_umounted() < 0) {
447 MSG(0, "\tError: Not available on mounted device!\n");
451 if (f2fs_get_device_info() < 0)
454 if (f2fs_check_overwrite()) {
455 char *zero_buf = NULL;
458 if (!force_overwrite) {
459 MSG(0, "\tUse the -f option to force overwrite.\n");
462 zero_buf = calloc(F2FS_BLKSIZE, 1);
464 MSG(0, "\tError: Fail to allocate zero buffer.\n");
467 /* wipe out other FS magics mostly first 4MB space */
468 for (i = 0; i < 1024; i++)
469 if (dev_fill_block(zero_buf, i))
473 MSG(0, "\tError: Fail to fill zeros till %d.\n", i);
476 if (f2fs_fsync_device())
480 if (f2fs_get_f2fs_info() < 0)
484 * Some options are mandatory for host-managed
485 * zoned block devices.
487 if (c.zoned_model != F2FS_ZONED_NONE && !c.zoned_mode) {
488 MSG(0, "\tError: zoned block device feature is required\n");
492 if (c.zoned_mode && !c.trim) {
493 MSG(0, "\tError: Trim is required for zoned block devices\n");
497 if (c.conf_reserved_sections && !c.zoned_mode) {
498 MSG(0, "\tError: Reserved area can't be specified on non zoned device\n");
502 if (f2fs_format_device() < 0)
505 if (f2fs_finalize_device() < 0)
508 MSG(0, "Info: format successful\n");
513 f2fs_release_sparse_resource();