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 device1[,device2,...] 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, " -i extended node bitmap, node ratio is 20%% by default\n");
60 MSG(0, " -l label\n");
61 MSG(0, " -U uuid\n");
62 MSG(0, " -m support zoned block device [default:0]\n");
63 MSG(0, " -o overprovision percentage [default:auto]\n");
64 MSG(0, " -O feature1[,feature2,...] e.g. \"encrypt\"\n");
65 MSG(0, " -C [encoding[:flag1,...]] Support casefolding with optional flags\n");
66 MSG(0, " -q quiet mode\n");
67 MSG(0, " -r set checkpointing seed (srand()) to 0\n");
68 MSG(0, " -R root_owner [default: 0:0]\n");
69 MSG(0, " -s # of segments per section [default:1]\n");
70 MSG(0, " -S sparse mode\n");
71 MSG(0, " -t 0: nodiscard, 1: discard [default:1]\n");
72 MSG(0, " -T timestamps\n");
73 MSG(0, " -w wanted sector size\n");
74 MSG(0, " -z # of sections per zone [default:1]\n");
75 MSG(0, " -V print the version number and exit\n");
76 MSG(0, " -Z # of reserved sections [default:auto]\n");
77 MSG(0, "sectors: number of sectors [default: determined by device size]\n");
78 exit(1);
79 }
80
f2fs_show_info()81 static void f2fs_show_info()
82 {
83 MSG(0, "\n F2FS-tools: mkfs.f2fs Ver: %s (%s)\n\n",
84 F2FS_TOOLS_VERSION,
85 F2FS_TOOLS_DATE);
86
87 MSG(0, "Info: Debug level = %d\n", c.dbg_lv);
88 if (c.extension_list[0])
89 MSG(0, "Info: Add new cold file extension list\n");
90 if (c.extension_list[1])
91 MSG(0, "Info: Add new hot file extension list\n");
92
93 if (strlen(c.vol_label))
94 MSG(0, "Info: Label = %s\n", c.vol_label);
95 MSG(0, "Info: Trim is %s\n", c.trim ? "enabled": "disabled");
96
97 if (c.defset == CONF_ANDROID)
98 MSG(0, "Info: Set conf for android\n");
99
100 if (c.feature & F2FS_FEATURE_CASEFOLD)
101 MSG(0, "Info: Enable %s with casefolding\n",
102 f2fs_encoding2str(c.s_encoding));
103 if (c.feature & F2FS_FEATURE_PRJQUOTA)
104 MSG(0, "Info: Enable Project quota\n");
105
106 if (c.feature & F2FS_FEATURE_COMPRESSION)
107 MSG(0, "Info: Enable Compression\n");
108 }
109
110 #if defined(ANDROID_TARGET) && defined(HAVE_SYS_UTSNAME_H)
kernel_version_over(unsigned int min_major,unsigned int min_minor)111 static bool kernel_version_over(unsigned int min_major, unsigned int min_minor)
112 {
113 unsigned int major, minor;
114 struct utsname uts;
115
116 if ((uname(&uts) != 0) ||
117 (sscanf(uts.release, "%u.%u", &major, &minor) != 2))
118 return false;
119 if (major > min_major)
120 return true;
121 if (major == min_major && minor >= min_minor)
122 return true;
123 return false;
124 }
125 #else
kernel_version_over(unsigned int UNUSED (min_major),unsigned int UNUSED (min_minor))126 static bool kernel_version_over(unsigned int UNUSED(min_major),
127 unsigned int UNUSED(min_minor))
128 {
129 return false;
130 }
131 #endif
132
add_default_options(void)133 static void add_default_options(void)
134 {
135 switch (c.defset) {
136 case CONF_ANDROID:
137 /* -d1 -f -w 4096 -R 0:0 */
138 c.dbg_lv = 1;
139 force_overwrite = 1;
140 c.wanted_sector_size = F2FS_BLKSIZE;
141 c.root_uid = c.root_gid = 0;
142
143 /* RO doesn't need any other features */
144 if (c.feature & F2FS_FEATURE_RO)
145 return;
146
147 /* -O encrypt -O project_quota,extra_attr,{quota} -O verity */
148 c.feature |= F2FS_FEATURE_ENCRYPT;
149 if (!kernel_version_over(4, 14))
150 c.feature |= F2FS_FEATURE_QUOTA_INO;
151 c.feature |= F2FS_FEATURE_PRJQUOTA;
152 c.feature |= F2FS_FEATURE_EXTRA_ATTR;
153 c.feature |= F2FS_FEATURE_VERITY;
154 break;
155 }
156 #ifdef CONF_CASEFOLD
157 c.s_encoding = F2FS_ENC_UTF8_12_1;
158 c.feature |= F2FS_FEATURE_CASEFOLD;
159 #endif
160 #ifdef CONF_PROJID
161 c.feature |= F2FS_FEATURE_QUOTA_INO;
162 c.feature |= F2FS_FEATURE_PRJQUOTA;
163 c.feature |= F2FS_FEATURE_EXTRA_ATTR;
164 #endif
165
166 if (c.feature & F2FS_FEATURE_QUOTA_INO)
167 c.quota_bits = QUOTA_USR_BIT | QUOTA_GRP_BIT;
168 if (c.feature & F2FS_FEATURE_PRJQUOTA) {
169 c.feature |= F2FS_FEATURE_QUOTA_INO;
170 c.quota_bits |= QUOTA_PRJ_BIT;
171 }
172 }
173
f2fs_parse_options(int argc,char * argv[])174 static void f2fs_parse_options(int argc, char *argv[])
175 {
176 static const char *option_string = "qa:b:c:C:d:e:E:g:hil:mo:O:rR:s:S:z:t:T:U:Vfw:Z:";
177 static const struct option long_opts[] = {
178 { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
179 { .name = NULL, .has_arg = 0, .flag = NULL, .val = 0 }
180 };
181 int32_t option=0;
182 int val;
183 char *token;
184
185 while ((option = getopt_long(argc,argv,option_string,long_opts,NULL)) != EOF) {
186 switch (option) {
187 case 'q':
188 c.dbg_lv = -1;
189 break;
190 case 'a':
191 MSG(0, "Info: heap allocation is deprecated\n");
192 break;
193 case 'b':
194 c.blksize = atoi(optarg);
195 c.blksize_bits = log_base_2(c.blksize);
196 c.sectors_per_blk = DEFAULT_SECTORS_PER_BLOCK;
197 if ((1 << c.blksize_bits) != c.blksize) {
198 MSG(0, "Error: Block size must be power of 2");
199 mkfs_usage();
200 }
201 break;
202 case 'c':
203 if (c.ndevs >= MAX_DEVICES) {
204 MSG(0, "Error: Too many devices\n");
205 mkfs_usage();
206 }
207
208 if (strlen(optarg) > MAX_PATH_LEN) {
209 MSG(0, "Error: device path should be less than "
210 "%d characters\n", MAX_PATH_LEN);
211 mkfs_usage();
212 }
213 c.devices[c.ndevs++].path = strdup(optarg);
214 break;
215 case 'd':
216 c.dbg_lv = atoi(optarg);
217 break;
218 case 'e':
219 c.extension_list[0] = strdup(optarg);
220 break;
221 case 'E':
222 c.extension_list[1] = strdup(optarg);
223 break;
224 case 'g':
225 if (!strcmp(optarg, "android"))
226 c.defset = CONF_ANDROID;
227 break;
228 case 'h':
229 mkfs_usage();
230 break;
231 case 'i':
232 c.large_nat_bitmap = 1;
233 break;
234 case 'l': /*v: volume label */
235 if (strlen(optarg) > 512) {
236 MSG(0, "Error: Volume Label should be less than "
237 "512 characters\n");
238 mkfs_usage();
239 }
240 c.vol_label = optarg;
241 break;
242 case 'm':
243 c.zoned_mode = 1;
244 break;
245 case 'o':
246 c.overprovision = atof(optarg);
247 break;
248 case 'O':
249 if (parse_feature(feature_table, optarg))
250 mkfs_usage();
251 break;
252 case 'r':
253 c.fake_seed = 1;
254 break;
255 case 'R':
256 if (parse_root_owner(optarg, &c.root_uid, &c.root_gid))
257 mkfs_usage();
258 break;
259 case 's':
260 c.segs_per_sec = atoi(optarg);
261 break;
262 case 'S':
263 c.device_size = atoll(optarg);
264 c.device_size &= (~((uint64_t)(F2FS_BLKSIZE - 1)));
265 c.sparse_mode = 1;
266 break;
267 case 'z':
268 c.secs_per_zone = atoi(optarg);
269 break;
270 case 't':
271 c.trim = atoi(optarg);
272 break;
273 case 'T':
274 c.fixed_time = strtoul(optarg, NULL, 0);
275 break;
276 case 'U':
277 c.vol_uuid = strdup(optarg);
278 break;
279 case 'f':
280 force_overwrite = 1;
281 break;
282 case 'w':
283 c.wanted_sector_size = atoi(optarg);
284 break;
285 case 'V':
286 show_version("mkfs.f2fs");
287 exit(0);
288 case 'C':
289 token = strtok(optarg, ":");
290 val = f2fs_str2encoding(token);
291 if (val < 0) {
292 MSG(0, "\tError: Unknown encoding %s\n", token);
293 mkfs_usage();
294 }
295 c.s_encoding = val;
296 token = strtok(NULL, "");
297 val = f2fs_str2encoding_flags(&token, &c.s_encoding_flags);
298 if (val) {
299 MSG(0, "\tError: Unknown flag %s\n",token);
300 mkfs_usage();
301 }
302 c.feature |= F2FS_FEATURE_CASEFOLD;
303 break;
304 case 'Z':
305 c.conf_reserved_sections = atoi(optarg);
306 break;
307 default:
308 MSG(0, "\tError: Unknown option %c\n",option);
309 mkfs_usage();
310 break;
311 }
312 }
313
314 add_default_options();
315
316 if (!(c.feature & F2FS_FEATURE_EXTRA_ATTR)) {
317 if (c.feature & F2FS_FEATURE_PRJQUOTA) {
318 MSG(0, "\tInfo: project quota feature should always be "
319 "enabled with extra attr feature\n");
320 exit(1);
321 }
322 if (c.feature & F2FS_FEATURE_INODE_CHKSUM) {
323 MSG(0, "\tInfo: inode checksum feature should always be "
324 "enabled with extra attr feature\n");
325 exit(1);
326 }
327 if (c.feature & F2FS_FEATURE_FLEXIBLE_INLINE_XATTR) {
328 MSG(0, "\tInfo: flexible inline xattr feature should always be "
329 "enabled with extra attr feature\n");
330 exit(1);
331 }
332 if (c.feature & F2FS_FEATURE_INODE_CRTIME) {
333 MSG(0, "\tInfo: inode crtime feature should always be "
334 "enabled with extra attr feature\n");
335 exit(1);
336 }
337 if (c.feature & F2FS_FEATURE_COMPRESSION) {
338 MSG(0, "\tInfo: compression feature should always be "
339 "enabled with extra attr feature\n");
340 exit(1);
341 }
342 }
343
344 if (optind >= argc) {
345 MSG(0, "\tError: Device not specified\n");
346 mkfs_usage();
347 }
348
349 /* [0] : META, [1 to MAX_DEVICES - 1] : NODE/DATA */
350 c.devices[0].path = strdup(argv[optind]);
351
352 if ((optind + 1) < argc) {
353 if (c.ndevs > 1) {
354 MSG(0, "\tError: Not support custom size on multi-devs.\n");
355 mkfs_usage();
356 }
357 c.wanted_total_sectors = atoll(argv[optind+1]);
358 }
359
360 if (c.sparse_mode)
361 c.trim = 0;
362
363 if (c.zoned_mode)
364 c.feature |= F2FS_FEATURE_BLKZONED;
365 check_block_struct_sizes();
366 }
367
368 #ifdef HAVE_LIBBLKID
f2fs_dev_is_overwrite(const char * device)369 static int f2fs_dev_is_overwrite(const char *device)
370 {
371 const char *type;
372 blkid_probe pr = NULL;
373 int ret = -1;
374
375 if (!device || !*device)
376 return 0;
377
378 pr = blkid_new_probe_from_filename(device);
379 if (!pr)
380 goto out;
381
382 ret = blkid_probe_enable_partitions(pr, 1);
383 if (ret < 0)
384 goto out;
385
386 ret = blkid_do_fullprobe(pr);
387 if (ret < 0)
388 goto out;
389
390 /*
391 * Blkid returns 1 for nothing found and 0 when it finds a signature,
392 * but we want the exact opposite, so reverse the return value here.
393 *
394 * In addition print some useful diagnostics about what actually is
395 * on the device.
396 */
397 if (ret) {
398 ret = 0;
399 goto out;
400 }
401
402 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
403 MSG(0, "\t%s appears to contain an existing filesystem (%s).\n",
404 device, type);
405 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
406 MSG(0, "\t%s appears to contain a partition table (%s).\n",
407 device, type);
408 } else {
409 MSG(0, "\t%s appears to contain something weird according to blkid\n",
410 device);
411 }
412 ret = 1;
413 out:
414 if (pr)
415 blkid_free_probe(pr);
416 if (ret == -1)
417 MSG(0, "\tprobe of %s failed, cannot detect existing filesystem.\n",
418 device);
419 return ret;
420 }
421
f2fs_check_overwrite(void)422 static int f2fs_check_overwrite(void)
423 {
424 int i;
425
426 for (i = 0; i < c.ndevs; i++)
427 if (f2fs_dev_is_overwrite((char *)c.devices[i].path))
428 return -1;
429 return 0;
430 }
431
432 #else
433
f2fs_check_overwrite(void)434 static int f2fs_check_overwrite(void)
435 {
436 return 0;
437 }
438
439 #endif /* HAVE_LIBBLKID */
440
main(int argc,char * argv[])441 int main(int argc, char *argv[])
442 {
443 int ret;
444
445 f2fs_init_configuration();
446
447 f2fs_parse_options(argc, argv);
448
449 f2fs_show_info();
450
451 c.func = MKFS;
452
453 ret = f2fs_devs_are_umounted();
454 if (ret) {
455 if (ret != -EBUSY)
456 MSG(0, "\tError: Not available on mounted device!\n");
457 goto err_format;
458 }
459
460 if (f2fs_get_device_info() < 0)
461 return -1;
462
463 if (f2fs_check_overwrite()) {
464 char *zero_buf = NULL;
465 int i;
466
467 if (!force_overwrite) {
468 MSG(0, "\tUse the -f option to force overwrite.\n");
469 goto err_format;
470 }
471 zero_buf = calloc(F2FS_BLKSIZE, 1);
472 if (!zero_buf) {
473 MSG(0, "\tError: Fail to allocate zero buffer.\n");
474 goto err_format;
475 }
476 /* wipe out other FS magics mostly first 4MB space */
477 for (i = 0; i < 1024; i++)
478 if (dev_fill_block(zero_buf, i))
479 break;
480 free(zero_buf);
481 if (i != 1024) {
482 MSG(0, "\tError: Fail to fill zeros till %d.\n", i);
483 goto err_format;
484 }
485 if (f2fs_fsync_device())
486 goto err_format;
487 }
488
489 if (f2fs_get_f2fs_info() < 0)
490 goto err_format;
491
492 /*
493 * Some options are mandatory for host-managed
494 * zoned block devices.
495 */
496 if (c.zoned_model != F2FS_ZONED_NONE && !c.zoned_mode) {
497 MSG(0, "\tError: zoned block device feature is required\n");
498 goto err_format;
499 }
500
501 if (c.zoned_mode && !c.trim) {
502 MSG(0, "\tError: Trim is required for zoned block devices\n");
503 goto err_format;
504 }
505
506 if (c.conf_reserved_sections && !c.zoned_mode) {
507 MSG(0, "\tError: Reserved area can't be specified on non zoned device\n");
508 goto err_format;
509 }
510
511 if (f2fs_format_device() < 0)
512 goto err_format;
513
514 if (f2fs_finalize_device() < 0)
515 goto err_format;
516
517 MSG(0, "Info: format successful\n");
518
519 return 0;
520
521 err_format:
522 f2fs_release_sparse_resource();
523 return -1;
524 }
525