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