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