aboutsummaryrefslogtreecommitdiffstats
path: root/mkfs/f2fs_format_main.c
blob: fa928452fd26259944129e7bfdbf367f82be0c1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
 * f2fs_format.c
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 *
 * Dual licensed under the GPL or LGPL version 2 licenses.
 */
#define _LARGEFILE64_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <time.h>
//#include <linux/fs.h>
#ifndef ANDROID
#include <uuid/uuid.h>

#include "f2fs_fs.h"
#else
#include "include/f2fs_fs.h"
#endif
#include "f2fs_format_utils.h"

extern struct f2fs_configuration config;

static void mkfs_usage()
{
	MSG(0, "\nUsage: mkfs.f2fs [options] device [sectors]\n");
	MSG(0, "[options]:\n");
	MSG(0, "  -a heap-based allocation [default:1]\n");
	MSG(0, "  -d debug level [default:0]\n");
	MSG(0, "  -e [extension list] e.g. \"mp3,gif,mov\"\n");
	MSG(0, "  -l label\n");
	MSG(0, "  -o overprovision ratio [default:5]\n");
	MSG(0, "  -s # of segments per section [default:1]\n");
	MSG(0, "  -z # of sections per zone [default:1]\n");
	MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
	MSG(0, "sectors: number of sectors. [default: determined by device size]\n");
	exit(1);
}

static void f2fs_parse_options(int argc, char *argv[])
{
	static const char *option_string = "a:d:e:l:o:s:z:t:";
	int32_t option=0;

	while ((option = getopt(argc,argv,option_string)) != EOF) {
		switch (option) {
		case 'a':
			config.heap = atoi(optarg);
			if (config.heap == 0)
				MSG(0, "Info: Disable heap-based policy\n");
			break;
		case 'd':
			config.dbg_lv = atoi(optarg);
			MSG(0, "Info: Debug level = %d\n", config.dbg_lv);
			break;
		case 'e':
			config.extension_list = strdup(optarg);
			MSG(0, "Info: Add new extension list\n");
			break;
		case 'l':		/*v: volume label */
			if (strlen(optarg) > 512) {
				MSG(0, "Error: Volume Label should be less than\
						512 characters\n");
				mkfs_usage();
			}
			config.vol_label = optarg;
			MSG(0, "Info: Label = %s\n", config.vol_label);
			break;
		case 'o':
			config.overprovision = atoi(optarg);
			MSG(0, "Info: Overprovision ratio = %u%%\n",
								atoi(optarg));
			break;
		case 's':
			config.segs_per_sec = atoi(optarg);
			MSG(0, "Info: Segments per section = %d\n",
								atoi(optarg));
			break;
		case 'z':
			config.secs_per_zone = atoi(optarg);
			MSG(0, "Info: Sections per zone = %d\n", atoi(optarg));
			break;
		case 't':
			config.trim = atoi(optarg);
			MSG(0, "Info: Trim is %s\n", config.trim ? "enabled": "disabled");
			break;
		default:
			mkfs_usage();
			break;
		}
	}

	if (optind >= argc) {
		MSG(0, "\tError: Device not specified\n");
		mkfs_usage();
	}
	config.device_name = argv[optind];

	if ((optind + 1) < argc) {
		/* We have a sector count. */
		config.total_sectors = atoll(argv[optind+1]);
		MSG(0, "\ttotal_sectors=%lu (%s bytes)\n", config.total_sectors, argv[optind+1]);
	}

	config.reserved_segments  =
			(2 * (100 / config.overprovision + 1) + 6)
			* config.segs_per_sec;
}

#ifndef ANDROID
int main(int argc, char *argv[])
#else
int make_f2fs_main(int argc, char *argv[])
#endif
{
	MSG(0, "\n\tF2FS-tools: mkfs.f2fs Ver: %s (%s)\n\n",
				F2FS_TOOLS_VERSION,
				F2FS_TOOLS_DATE);
	f2fs_init_configuration(&config);

	f2fs_parse_options(argc, argv);

#ifndef ANDROID
	if (f2fs_dev_is_umounted(&config) < 0)
		return -1;
#endif

	if (f2fs_get_device_info(&config) < 0)
		return -1;

	if (f2fs_format_device() < 0)
		return -1;

	f2fs_finalize_device(&config);

	MSG(0, "Info: format successful\n");

	return 0;
}