2014-02-06 03:53:19 +00:00
|
|
|
/**
|
|
|
|
* f2fs_format.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
|
|
|
* http://www.samsung.com/
|
|
|
|
*
|
2014-04-07 03:10:59 +00:00
|
|
|
* Dual licensed under the GPL or LGPL version 2 licenses.
|
2014-02-06 03:53:19 +00:00
|
|
|
*/
|
2021-12-15 02:45:47 +00:00
|
|
|
#ifndef _LARGEFILE64_SOURCE
|
2014-02-06 03:53:19 +00:00
|
|
|
#define _LARGEFILE64_SOURCE
|
2021-12-15 02:45:47 +00:00
|
|
|
#endif
|
2014-02-06 03:53:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2022-01-13 18:24:56 +00:00
|
|
|
#include <stdbool.h>
|
2014-02-06 03:53:19 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2022-04-21 22:18:29 +00:00
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
2014-02-06 03:53:19 +00:00
|
|
|
#include <sys/mount.h>
|
2017-12-01 23:03:30 +00:00
|
|
|
#endif
|
2014-02-06 03:53:19 +00:00
|
|
|
#include <time.h>
|
2017-05-10 13:16:46 +00:00
|
|
|
#include <errno.h>
|
2020-10-04 18:08:35 +00:00
|
|
|
#include <getopt.h>
|
2014-02-06 03:53:19 +00:00
|
|
|
|
2022-05-25 21:04:26 +00:00
|
|
|
#include <f2fs_fs.h>
|
|
|
|
|
2017-03-05 13:05:01 +00:00
|
|
|
#ifdef HAVE_LIBBLKID
|
2022-04-21 22:18:10 +00:00
|
|
|
#include <blkid/blkid.h>
|
2017-03-05 13:05:01 +00:00
|
|
|
#endif
|
2022-04-21 22:18:22 +00:00
|
|
|
#ifdef HAVE_UUID_UUID_H
|
2024-07-06 09:18:59 +00:00
|
|
|
#include <uuid.h>
|
2022-04-21 22:18:22 +00:00
|
|
|
#endif
|
2017-03-05 13:05:01 +00:00
|
|
|
|
2022-01-04 19:56:16 +00:00
|
|
|
#include "quota.h"
|
2014-02-06 03:53:19 +00:00
|
|
|
#include "f2fs_format_utils.h"
|
|
|
|
|
2022-01-13 18:24:56 +00:00
|
|
|
#ifdef HAVE_SYS_UTSNAME_H
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
2022-04-21 22:18:23 +00:00
|
|
|
#ifdef HAVE_SPARSE_SPARSE_H
|
2017-06-16 18:32:58 +00:00
|
|
|
#include <sparse/sparse.h>
|
|
|
|
extern struct sparse_file *f2fs_sparse_file;
|
|
|
|
#endif
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
extern struct f2fs_configuration c;
|
2017-03-05 13:05:01 +00:00
|
|
|
static int force_overwrite = 0;
|
2014-02-06 03:53:19 +00:00
|
|
|
|
2018-04-19 20:41:31 +00:00
|
|
|
INIT_FEATURE_TABLE;
|
2018-04-15 14:36:33 +00:00
|
|
|
|
2014-02-06 03:53:19 +00:00
|
|
|
static void mkfs_usage()
|
|
|
|
{
|
2014-06-12 04:55:38 +00:00
|
|
|
MSG(0, "\nUsage: mkfs.f2fs [options] device [sectors]\n");
|
2014-02-06 03:53:19 +00:00
|
|
|
MSG(0, "[options]:\n");
|
2017-03-25 00:56:24 +00:00
|
|
|
MSG(0, " -a heap-based allocation [default:0]\n");
|
2020-04-12 21:34:35 +00:00
|
|
|
MSG(0, " -c device1[,device2,...] up to 7 additional devices, except meta device\n");
|
2014-02-06 03:53:19 +00:00
|
|
|
MSG(0, " -d debug level [default:0]\n");
|
2018-03-01 02:48:47 +00:00
|
|
|
MSG(0, " -e [cold file ext list] e.g. \"mp3,gif,mov\"\n");
|
|
|
|
MSG(0, " -E [hot file ext list] e.g. \"db\"\n");
|
2020-04-12 21:34:35 +00:00
|
|
|
MSG(0, " -f force overwrite of the existing filesystem\n");
|
2018-04-19 18:33:14 +00:00
|
|
|
MSG(0, " -g add default options\n");
|
2018-01-25 11:45:13 +00:00
|
|
|
MSG(0, " -i extended node bitmap, node ratio is 20%% by default\n");
|
2014-02-06 03:53:19 +00:00
|
|
|
MSG(0, " -l label\n");
|
2020-07-09 11:21:11 +00:00
|
|
|
MSG(0, " -U uuid\n");
|
2017-03-05 13:04:46 +00:00
|
|
|
MSG(0, " -m support zoned block device [default:0]\n");
|
2020-04-12 21:34:35 +00:00
|
|
|
MSG(0, " -o overprovision percentage [default:auto]\n");
|
|
|
|
MSG(0, " -O feature1[,feature2,...] e.g. \"encrypt\"\n");
|
|
|
|
MSG(0, " -C [encoding[:flag1,...]] Support casefolding with optional flags\n");
|
2015-03-24 07:10:15 +00:00
|
|
|
MSG(0, " -q quiet mode\n");
|
2020-08-18 11:18:07 +00:00
|
|
|
MSG(0, " -r set checkpointing seed (srand()) to 0\n");
|
2018-07-27 08:12:32 +00:00
|
|
|
MSG(0, " -R root_owner [default: 0:0]\n");
|
2014-02-06 03:53:19 +00:00
|
|
|
MSG(0, " -s # of segments per section [default:1]\n");
|
2017-06-16 18:32:58 +00:00
|
|
|
MSG(0, " -S sparse mode\n");
|
2014-02-06 03:53:19 +00:00
|
|
|
MSG(0, " -t 0: nodiscard, 1: discard [default:1]\n");
|
2020-08-18 11:18:50 +00:00
|
|
|
MSG(0, " -T timestamps\n");
|
2018-03-27 05:25:46 +00:00
|
|
|
MSG(0, " -w wanted sector size\n");
|
2017-03-05 13:04:46 +00:00
|
|
|
MSG(0, " -z # of sections per zone [default:1]\n");
|
2018-04-10 03:28:19 +00:00
|
|
|
MSG(0, " -V print the version number and exit\n");
|
2020-04-12 21:34:35 +00:00
|
|
|
MSG(0, "sectors: number of sectors [default: determined by device size]\n");
|
2014-02-06 03:53:19 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-03-24 07:10:15 +00:00
|
|
|
static void f2fs_show_info()
|
|
|
|
{
|
2022-02-09 21:07:49 +00:00
|
|
|
MSG(0, "\n F2FS-tools: mkfs.f2fs Ver: %s (%s)\n\n",
|
2015-03-24 07:10:15 +00:00
|
|
|
F2FS_TOOLS_VERSION,
|
|
|
|
F2FS_TOOLS_DATE);
|
2016-09-17 01:41:00 +00:00
|
|
|
if (c.heap == 0)
|
2015-03-24 07:10:15 +00:00
|
|
|
MSG(0, "Info: Disable heap-based policy\n");
|
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
|
2018-03-01 02:48:47 +00:00
|
|
|
if (c.extension_list[0])
|
|
|
|
MSG(0, "Info: Add new cold file extension list\n");
|
|
|
|
if (c.extension_list[1])
|
|
|
|
MSG(0, "Info: Add new hot file extension list\n");
|
2015-03-24 07:10:15 +00:00
|
|
|
|
2018-10-08 12:39:07 +00:00
|
|
|
if (strlen(c.vol_label))
|
2016-09-17 01:41:00 +00:00
|
|
|
MSG(0, "Info: Label = %s\n", c.vol_label);
|
|
|
|
MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
|
2018-04-19 18:33:14 +00:00
|
|
|
|
|
|
|
if (c.defset == CONF_ANDROID)
|
|
|
|
MSG(0, "Info: Set conf for android\n");
|
2020-07-18 03:10:21 +00:00
|
|
|
|
|
|
|
if (c.feature & le32_to_cpu(F2FS_FEATURE_CASEFOLD))
|
|
|
|
MSG(0, "Info: Enable %s with casefolding\n",
|
|
|
|
f2fs_encoding2str(c.s_encoding));
|
|
|
|
if (c.feature & le32_to_cpu(F2FS_FEATURE_PRJQUOTA))
|
|
|
|
MSG(0, "Info: Enable Project quota\n");
|
2020-11-09 18:03:06 +00:00
|
|
|
|
|
|
|
if (c.feature & le32_to_cpu(F2FS_FEATURE_COMPRESSION))
|
|
|
|
MSG(0, "Info: Enable Compression\n");
|
2015-03-24 07:10:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 18:24:56 +00:00
|
|
|
#if defined(ANDROID_TARGET) && defined(HAVE_SYS_UTSNAME_H)
|
|
|
|
static bool kernel_version_over(unsigned int min_major, unsigned int min_minor)
|
|
|
|
{
|
|
|
|
unsigned int major, minor;
|
|
|
|
struct utsname uts;
|
|
|
|
|
|
|
|
if ((uname(&uts) != 0) ||
|
|
|
|
(sscanf(uts.release, "%u.%u", &major, &minor) != 2))
|
|
|
|
return false;
|
|
|
|
if (major > min_major)
|
|
|
|
return true;
|
|
|
|
if (major == min_major && minor >= min_minor)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static bool kernel_version_over(unsigned int UNUSED(min_major),
|
|
|
|
unsigned int UNUSED(min_minor))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-19 18:33:14 +00:00
|
|
|
static void add_default_options(void)
|
|
|
|
{
|
|
|
|
switch (c.defset) {
|
|
|
|
case CONF_ANDROID:
|
2022-01-13 18:24:56 +00:00
|
|
|
/* -d1 -f -w 4096 -R 0:0 */
|
2018-04-19 18:33:14 +00:00
|
|
|
c.dbg_lv = 1;
|
|
|
|
force_overwrite = 1;
|
2021-06-13 05:09:22 +00:00
|
|
|
c.wanted_sector_size = 4096;
|
|
|
|
c.root_uid = c.root_gid = 0;
|
|
|
|
|
|
|
|
/* RO doesn't need any other features */
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_RO))
|
|
|
|
return;
|
|
|
|
|
2022-01-13 18:24:56 +00:00
|
|
|
/* -O encrypt -O project_quota,extra_attr,{quota} -O verity */
|
2018-04-19 18:33:14 +00:00
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
|
2022-01-13 18:24:56 +00:00
|
|
|
if (!kernel_version_over(4, 14))
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_PRJQUOTA);
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR);
|
2018-11-22 07:18:14 +00:00
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_VERITY);
|
2018-04-19 18:33:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-07-18 03:10:21 +00:00
|
|
|
#ifdef CONF_CASEFOLD
|
|
|
|
c.s_encoding = F2FS_ENC_UTF8_12_1;
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
|
|
|
|
#endif
|
|
|
|
#ifdef CONF_PROJID
|
2022-01-04 19:56:16 +00:00
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
|
2020-07-18 03:10:21 +00:00
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_PRJQUOTA);
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR);
|
|
|
|
#endif
|
2022-01-04 19:56:16 +00:00
|
|
|
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_QUOTA_INO))
|
|
|
|
c.quota_bits = QUOTA_USR_BIT | QUOTA_GRP_BIT;
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_QUOTA_INO);
|
|
|
|
c.quota_bits |= QUOTA_PRJ_BIT;
|
|
|
|
}
|
2018-04-19 18:33:14 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 03:53:19 +00:00
|
|
|
static void f2fs_parse_options(int argc, char *argv[])
|
|
|
|
{
|
2020-10-04 18:08:35 +00:00
|
|
|
static const char *option_string = "qa:c:C:d:e:E:g:hil:mo:O:rR:s:S:z:t:T:U:Vfw:";
|
|
|
|
static const struct option long_opts[] = {
|
|
|
|
{ .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
|
|
|
|
{ .name = NULL, .has_arg = 0, .flag = NULL, .val = 0 }
|
|
|
|
};
|
2014-02-06 03:53:19 +00:00
|
|
|
int32_t option=0;
|
2019-07-11 20:45:41 +00:00
|
|
|
int val;
|
|
|
|
char *token;
|
2014-02-06 03:53:19 +00:00
|
|
|
|
2020-10-04 18:08:35 +00:00
|
|
|
while ((option = getopt_long(argc,argv,option_string,long_opts,NULL)) != EOF) {
|
2014-02-06 03:53:19 +00:00
|
|
|
switch (option) {
|
2015-03-24 07:10:15 +00:00
|
|
|
case 'q':
|
2016-09-17 01:41:00 +00:00
|
|
|
c.dbg_lv = -1;
|
2015-03-24 07:10:15 +00:00
|
|
|
break;
|
2014-02-06 03:53:19 +00:00
|
|
|
case 'a':
|
2016-09-17 01:41:00 +00:00
|
|
|
c.heap = atoi(optarg);
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
2016-11-02 00:23:40 +00:00
|
|
|
case 'c':
|
2017-01-22 09:16:39 +00:00
|
|
|
if (c.ndevs >= MAX_DEVICES) {
|
|
|
|
MSG(0, "Error: Too many devices\n");
|
|
|
|
mkfs_usage();
|
|
|
|
}
|
|
|
|
|
2016-11-02 00:23:40 +00:00
|
|
|
if (strlen(optarg) > MAX_PATH_LEN) {
|
|
|
|
MSG(0, "Error: device path should be less than "
|
|
|
|
"%d characters\n", MAX_PATH_LEN);
|
|
|
|
mkfs_usage();
|
|
|
|
}
|
|
|
|
c.devices[c.ndevs++].path = strdup(optarg);
|
|
|
|
break;
|
2014-02-06 03:53:19 +00:00
|
|
|
case 'd':
|
2016-09-17 01:41:00 +00:00
|
|
|
c.dbg_lv = atoi(optarg);
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
2018-03-01 02:48:47 +00:00
|
|
|
c.extension_list[0] = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
c.extension_list[1] = strdup(optarg);
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
2018-04-19 18:33:14 +00:00
|
|
|
case 'g':
|
|
|
|
if (!strcmp(optarg, "android"))
|
|
|
|
c.defset = CONF_ANDROID;
|
|
|
|
break;
|
2020-10-04 18:08:35 +00:00
|
|
|
case 'h':
|
|
|
|
mkfs_usage();
|
|
|
|
break;
|
2018-01-25 11:45:13 +00:00
|
|
|
case 'i':
|
|
|
|
c.large_nat_bitmap = 1;
|
|
|
|
break;
|
2014-02-06 03:53:19 +00:00
|
|
|
case 'l': /*v: volume label */
|
2015-04-03 17:49:34 +00:00
|
|
|
if (strlen(optarg) > 512) {
|
2016-07-15 12:01:24 +00:00
|
|
|
MSG(0, "Error: Volume Label should be less than "
|
|
|
|
"512 characters\n");
|
2014-02-06 03:53:19 +00:00
|
|
|
mkfs_usage();
|
|
|
|
}
|
2016-09-17 01:41:00 +00:00
|
|
|
c.vol_label = optarg;
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
2016-05-27 21:20:43 +00:00
|
|
|
case 'm':
|
2016-10-28 07:56:59 +00:00
|
|
|
c.zoned_mode = 1;
|
2016-05-27 21:20:43 +00:00
|
|
|
break;
|
2014-02-06 03:53:19 +00:00
|
|
|
case 'o':
|
2016-09-17 01:41:00 +00:00
|
|
|
c.overprovision = atof(optarg);
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
2015-04-22 03:03:40 +00:00
|
|
|
case 'O':
|
2018-04-19 20:41:31 +00:00
|
|
|
if (parse_feature(feature_table, optarg))
|
|
|
|
mkfs_usage();
|
2015-04-22 03:03:40 +00:00
|
|
|
break;
|
2020-08-18 11:18:07 +00:00
|
|
|
case 'r':
|
|
|
|
c.fake_seed = 1;
|
|
|
|
break;
|
2018-07-27 08:12:32 +00:00
|
|
|
case 'R':
|
|
|
|
if (parse_root_owner(optarg, &c.root_uid, &c.root_gid))
|
|
|
|
mkfs_usage();
|
|
|
|
break;
|
2014-02-06 03:53:19 +00:00
|
|
|
case 's':
|
2016-09-17 01:41:00 +00:00
|
|
|
c.segs_per_sec = atoi(optarg);
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
2017-06-16 18:32:58 +00:00
|
|
|
case 'S':
|
|
|
|
c.device_size = atoll(optarg);
|
2022-04-21 22:18:11 +00:00
|
|
|
c.device_size &= (~((uint64_t)(F2FS_BLKSIZE - 1)));
|
2017-06-16 18:32:58 +00:00
|
|
|
c.sparse_mode = 1;
|
|
|
|
break;
|
2014-02-06 03:53:19 +00:00
|
|
|
case 'z':
|
2016-09-17 01:41:00 +00:00
|
|
|
c.secs_per_zone = atoi(optarg);
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2016-09-17 01:41:00 +00:00
|
|
|
c.trim = atoi(optarg);
|
2014-02-06 03:53:19 +00:00
|
|
|
break;
|
2020-08-18 11:18:50 +00:00
|
|
|
case 'T':
|
|
|
|
c.fixed_time = strtoul(optarg, NULL, 0);
|
|
|
|
break;
|
2020-07-09 11:21:11 +00:00
|
|
|
case 'U':
|
|
|
|
c.vol_uuid = strdup(optarg);
|
|
|
|
break;
|
2017-03-05 13:05:01 +00:00
|
|
|
case 'f':
|
|
|
|
force_overwrite = 1;
|
|
|
|
break;
|
2018-03-27 05:25:46 +00:00
|
|
|
case 'w':
|
|
|
|
c.wanted_sector_size = atoi(optarg);
|
|
|
|
break;
|
2018-04-10 03:28:19 +00:00
|
|
|
case 'V':
|
|
|
|
show_version("mkfs.f2fs");
|
|
|
|
exit(0);
|
2019-07-11 20:45:41 +00:00
|
|
|
case 'C':
|
|
|
|
token = strtok(optarg, ":");
|
|
|
|
val = f2fs_str2encoding(token);
|
|
|
|
if (val < 0) {
|
|
|
|
MSG(0, "\tError: Unknown encoding %s\n", token);
|
|
|
|
mkfs_usage();
|
|
|
|
}
|
|
|
|
c.s_encoding = val;
|
|
|
|
token = strtok(NULL, "");
|
|
|
|
val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
|
|
|
|
if (val) {
|
|
|
|
MSG(0, "\tError: Unknown flag %s\n",token);
|
|
|
|
mkfs_usage();
|
|
|
|
}
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
|
|
|
|
break;
|
2014-02-06 03:53:19 +00:00
|
|
|
default:
|
|
|
|
MSG(0, "\tError: Unknown option %c\n",option);
|
|
|
|
mkfs_usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 18:33:14 +00:00
|
|
|
add_default_options();
|
|
|
|
|
2017-07-26 14:54:18 +00:00
|
|
|
if (!(c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR))) {
|
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_PRJQUOTA)) {
|
2018-07-26 22:02:02 +00:00
|
|
|
MSG(0, "\tInfo: project quota feature should always be "
|
2017-07-26 14:54:18 +00:00
|
|
|
"enabled with extra attr feature\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-07-26 15:01:35 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) {
|
2018-07-26 22:02:02 +00:00
|
|
|
MSG(0, "\tInfo: inode checksum feature should always be "
|
2017-07-26 15:01:35 +00:00
|
|
|
"enabled with extra attr feature\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-09-04 10:58:29 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) {
|
2018-07-26 22:02:02 +00:00
|
|
|
MSG(0, "\tInfo: flexible inline xattr feature should always be "
|
2017-09-04 10:58:29 +00:00
|
|
|
"enabled with extra attr feature\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-01-25 06:58:45 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CRTIME)) {
|
2018-07-26 22:02:02 +00:00
|
|
|
MSG(0, "\tInfo: inode crtime feature should always be "
|
2018-01-25 06:58:45 +00:00
|
|
|
"enabled with extra attr feature\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-03-25 13:19:35 +00:00
|
|
|
if (c.feature & cpu_to_le32(F2FS_FEATURE_COMPRESSION)) {
|
|
|
|
MSG(0, "\tInfo: compression feature should always be "
|
|
|
|
"enabled with extra attr feature\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-07-26 14:54:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 04:55:38 +00:00
|
|
|
if (optind >= argc) {
|
2014-02-06 03:53:19 +00:00
|
|
|
MSG(0, "\tError: Device not specified\n");
|
|
|
|
mkfs_usage();
|
|
|
|
}
|
2016-11-02 00:23:40 +00:00
|
|
|
|
2017-01-22 09:16:39 +00:00
|
|
|
/* [0] : META, [1 to MAX_DEVICES - 1] : NODE/DATA */
|
2016-11-02 00:23:40 +00:00
|
|
|
c.devices[0].path = strdup(argv[optind]);
|
|
|
|
|
|
|
|
if ((optind + 1) < argc) {
|
|
|
|
if (c.ndevs > 1) {
|
|
|
|
MSG(0, "\tError: Not support custom size on multi-devs.\n");
|
|
|
|
mkfs_usage();
|
|
|
|
}
|
|
|
|
c.wanted_total_sectors = atoll(argv[optind+1]);
|
|
|
|
}
|
2014-06-12 04:55:38 +00:00
|
|
|
|
2017-06-16 18:32:58 +00:00
|
|
|
if (c.sparse_mode)
|
|
|
|
c.trim = 0;
|
|
|
|
|
2016-10-28 07:56:59 +00:00
|
|
|
if (c.zoned_mode)
|
|
|
|
c.feature |= cpu_to_le32(F2FS_FEATURE_BLKZONED);
|
2014-02-06 03:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 13:05:01 +00:00
|
|
|
#ifdef HAVE_LIBBLKID
|
|
|
|
static int f2fs_dev_is_overwrite(const char *device)
|
|
|
|
{
|
|
|
|
const char *type;
|
|
|
|
blkid_probe pr = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!device || !*device)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pr = blkid_new_probe_from_filename(device);
|
|
|
|
if (!pr)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = blkid_probe_enable_partitions(pr, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = blkid_do_fullprobe(pr);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Blkid returns 1 for nothing found and 0 when it finds a signature,
|
|
|
|
* but we want the exact opposite, so reverse the return value here.
|
|
|
|
*
|
|
|
|
* In addition print some useful diagnostics about what actually is
|
|
|
|
* on the device.
|
|
|
|
*/
|
|
|
|
if (ret) {
|
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
|
|
|
|
MSG(0, "\t%s appears to contain an existing filesystem (%s).\n",
|
|
|
|
device, type);
|
|
|
|
} else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
|
|
|
|
MSG(0, "\t%s appears to contain a partition table (%s).\n",
|
|
|
|
device, type);
|
|
|
|
} else {
|
|
|
|
MSG(0, "\t%s appears to contain something weird according to blkid\n",
|
|
|
|
device);
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
out:
|
|
|
|
if (pr)
|
|
|
|
blkid_free_probe(pr);
|
|
|
|
if (ret == -1)
|
|
|
|
MSG(0, "\tprobe of %s failed, cannot detect existing filesystem.\n",
|
|
|
|
device);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int f2fs_check_overwrite(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c.ndevs; i++)
|
|
|
|
if (f2fs_dev_is_overwrite((char *)c.devices[i].path))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int f2fs_check_overwrite(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LIBBLKID */
|
|
|
|
|
2014-02-06 03:53:19 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2016-09-17 01:41:00 +00:00
|
|
|
f2fs_init_configuration();
|
2014-02-06 03:53:19 +00:00
|
|
|
|
|
|
|
f2fs_parse_options(argc, argv);
|
|
|
|
|
2015-03-24 07:10:15 +00:00
|
|
|
f2fs_show_info();
|
|
|
|
|
2017-11-30 04:34:37 +00:00
|
|
|
c.func = MKFS;
|
|
|
|
|
2016-11-02 00:23:40 +00:00
|
|
|
if (f2fs_devs_are_umounted() < 0) {
|
2017-05-10 13:16:46 +00:00
|
|
|
if (errno != EBUSY)
|
|
|
|
MSG(0, "\tError: Not available on mounted device!\n");
|
2021-09-10 22:38:11 +00:00
|
|
|
goto err_format;
|
2015-11-22 02:53:07 +00:00
|
|
|
}
|
2014-02-06 03:53:19 +00:00
|
|
|
|
2016-09-17 01:41:00 +00:00
|
|
|
if (f2fs_get_device_info() < 0)
|
2021-09-10 22:38:11 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (f2fs_check_overwrite()) {
|
|
|
|
char *zero_buf = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!force_overwrite) {
|
|
|
|
MSG(0, "\tUse the -f option to force overwrite.\n");
|
|
|
|
goto err_format;
|
|
|
|
}
|
|
|
|
zero_buf = calloc(F2FS_BLKSIZE, 1);
|
|
|
|
if (!zero_buf) {
|
|
|
|
MSG(0, "\tError: Fail to allocate zero buffer.\n");
|
|
|
|
goto err_format;
|
|
|
|
}
|
|
|
|
/* wipe out other FS magics mostly first 4MB space */
|
|
|
|
for (i = 0; i < 1024; i++)
|
|
|
|
if (dev_fill_block(zero_buf, i))
|
|
|
|
break;
|
|
|
|
free(zero_buf);
|
|
|
|
if (i != 1024) {
|
|
|
|
MSG(0, "\tError: Fail to fill zeros till %d.\n", i);
|
|
|
|
goto err_format;
|
|
|
|
}
|
|
|
|
if (f2fs_fsync_device())
|
|
|
|
goto err_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f2fs_get_f2fs_info() < 0)
|
2019-10-31 13:57:59 +00:00
|
|
|
goto err_format;
|
2014-02-06 03:53:19 +00:00
|
|
|
|
2016-10-28 07:57:01 +00:00
|
|
|
/*
|
|
|
|
* Some options are mandatory for host-managed
|
|
|
|
* zoned block devices.
|
|
|
|
*/
|
2022-09-30 15:51:13 +00:00
|
|
|
if (c.zoned_model != F2FS_ZONED_NONE && !c.zoned_mode) {
|
2016-10-28 07:57:01 +00:00
|
|
|
MSG(0, "\tError: zoned block device feature is required\n");
|
2019-10-31 13:57:59 +00:00
|
|
|
goto err_format;
|
2016-10-28 07:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c.zoned_mode && !c.trim) {
|
|
|
|
MSG(0, "\tError: Trim is required for zoned block devices\n");
|
2019-10-31 13:57:59 +00:00
|
|
|
goto err_format;
|
2017-06-16 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-06 03:53:19 +00:00
|
|
|
if (f2fs_format_device() < 0)
|
2019-10-31 13:57:59 +00:00
|
|
|
goto err_format;
|
2014-02-06 03:53:19 +00:00
|
|
|
|
2017-11-24 09:33:39 +00:00
|
|
|
if (f2fs_finalize_device() < 0)
|
2019-10-31 13:57:59 +00:00
|
|
|
goto err_format;
|
2014-06-13 07:51:32 +00:00
|
|
|
|
2014-02-06 03:53:19 +00:00
|
|
|
MSG(0, "Info: format successful\n");
|
|
|
|
|
|
|
|
return 0;
|
2019-10-31 13:57:59 +00:00
|
|
|
|
|
|
|
err_format:
|
|
|
|
f2fs_release_sparse_resource();
|
|
|
|
return -1;
|
2014-02-06 03:53:19 +00:00
|
|
|
}
|