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 #define _LARGEFILE64_SOURCE
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <sys/mount.h>
18 #include <time.h>
19 //#include <linux/fs.h>
20 #include <uuid/uuid.h>
21 
22 #include "f2fs_fs.h"
23 #include "f2fs_format_utils.h"
24 
25 extern struct f2fs_configuration config;
26 
mkfs_usage()27 static void mkfs_usage()
28 {
29 	MSG(0, "\nUsage: mkfs.f2fs [options] device [sectors]\n");
30 	MSG(0, "[options]:\n");
31 	MSG(0, "  -a heap-based allocation [default:1]\n");
32 	MSG(0, "  -d debug level [default:0]\n");
33 	MSG(0, "  -e [extension list] e.g. \"mp3,gif,mov\"\n");
34 	MSG(0, "  -l label\n");
35 	MSG(0, "  -o overprovision ratio [default:5]\n");
36 	MSG(0, "  -s # of segments per section [default:1]\n");
37 	MSG(0, "  -z # of sections per zone [default:1]\n");
38 	MSG(0, "  -t 0: nodiscard, 1: discard [default:1]\n");
39 	MSG(0, "sectors: number of sectors. [default: determined by device size]\n");
40 	exit(1);
41 }
42 
f2fs_parse_options(int argc,char * argv[])43 static void f2fs_parse_options(int argc, char *argv[])
44 {
45 	static const char *option_string = "a:d:e:l:o:s:z:t:";
46 	int32_t option=0;
47 
48 	while ((option = getopt(argc,argv,option_string)) != EOF) {
49 		switch (option) {
50 		case 'a':
51 			config.heap = atoi(optarg);
52 			if (config.heap == 0)
53 				MSG(0, "Info: Disable heap-based policy\n");
54 			break;
55 		case 'd':
56 			config.dbg_lv = atoi(optarg);
57 			MSG(0, "Info: Debug level = %d\n", config.dbg_lv);
58 			break;
59 		case 'e':
60 			config.extension_list = strdup(optarg);
61 			MSG(0, "Info: Add new extension list\n");
62 			break;
63 		case 'l':		/*v: volume label */
64 			if (strlen(optarg) > 512) {
65 				MSG(0, "Error: Volume Label should be less than\
66 						512 characters\n");
67 				mkfs_usage();
68 			}
69 			config.vol_label = optarg;
70 			MSG(0, "Info: Label = %s\n", config.vol_label);
71 			break;
72 		case 'o':
73 			config.overprovision = atoi(optarg);
74 			MSG(0, "Info: Overprovision ratio = %u%%\n",
75 								atoi(optarg));
76 			break;
77 		case 's':
78 			config.segs_per_sec = atoi(optarg);
79 			MSG(0, "Info: Segments per section = %d\n",
80 								atoi(optarg));
81 			break;
82 		case 'z':
83 			config.secs_per_zone = atoi(optarg);
84 			MSG(0, "Info: Sections per zone = %d\n", atoi(optarg));
85 			break;
86 		case 't':
87 			config.trim = atoi(optarg);
88 			MSG(0, "Info: Trim is %s\n", config.trim ? "enabled": "disabled");
89 			break;
90 		default:
91 			MSG(0, "\tError: Unknown option %c\n",option);
92 			mkfs_usage();
93 			break;
94 		}
95 	}
96 
97 	if (optind >= argc) {
98 		MSG(0, "\tError: Device not specified\n");
99 		mkfs_usage();
100 	}
101 	config.device_name = argv[optind];
102 
103 	if ((optind + 1) < argc) {
104 		/* We have a sector count. */
105 		config.total_sectors = atoll(argv[optind+1]);
106 		MSG(0, "\ttotal_sectors=%08"PRIx64" (%s bytes)\n",
107 				config.total_sectors, argv[optind+1]);
108 	}
109 
110 	config.reserved_segments  =
111 			(2 * (100 / config.overprovision + 1) + 6)
112 			* config.segs_per_sec;
113 	config.segs_per_zone = config.segs_per_sec * config.secs_per_zone;
114 }
115 
main(int argc,char * argv[])116 int main(int argc, char *argv[])
117 {
118 	MSG(0, "\n\tF2FS-tools: mkfs.f2fs Ver: %s (%s)\n\n",
119 				F2FS_TOOLS_VERSION,
120 				F2FS_TOOLS_DATE);
121 	f2fs_init_configuration(&config);
122 
123 	f2fs_parse_options(argc, argv);
124 
125 	if (f2fs_dev_is_umounted(&config) < 0)
126 		return -1;
127 
128 	if (f2fs_get_device_info(&config) < 0)
129 		return -1;
130 
131 	if (f2fs_format_device() < 0)
132 		return -1;
133 
134 	f2fs_finalize_device(&config);
135 
136 	MSG(0, "Info: format successful\n");
137 
138 	return 0;
139 }
140