xref: /spdk/test/blobfs/mkfs/mkfs.c (revision 03e3fc4f5835983a4e6602b4e770922e798ce263)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/event.h"
37 #include "spdk/blobfs.h"
38 #include "spdk/blobfs_bdev.h"
39 #include "spdk/log.h"
40 #include "spdk/string.h"
41 
42 const char *g_bdev_name;
43 static uint64_t g_cluster_size;
44 
45 static void
46 shutdown_cb(void *cb_arg, int fserrno)
47 {
48 	if (fserrno) {
49 		printf("\nFailed to initialize filesystem on bdev %s...", g_bdev_name);
50 	}
51 
52 	printf("done.\n");
53 
54 	spdk_app_stop(0);
55 }
56 
57 static void
58 spdk_mkfs_run(void *arg1)
59 {
60 	printf("Initializing filesystem on bdev %s...", g_bdev_name);
61 	fflush(stdout);
62 
63 	spdk_blobfs_bdev_create(g_bdev_name, g_cluster_size, shutdown_cb, NULL);
64 }
65 
66 static void
67 mkfs_usage(void)
68 {
69 	printf(" -C <size>                 cluster size\n");
70 }
71 
72 static int
73 mkfs_parse_arg(int ch, char *arg)
74 {
75 	bool has_prefix;
76 
77 	switch (ch) {
78 	case 'C':
79 		spdk_parse_capacity(arg, &g_cluster_size, &has_prefix);
80 		break;
81 	default:
82 		return -EINVAL;
83 	}
84 	return 0;
85 }
86 
87 int main(int argc, char **argv)
88 {
89 	struct spdk_app_opts opts = {};
90 	int rc = 0;
91 
92 	if (argc < 3) {
93 		SPDK_ERRLOG("usage: %s <conffile> <bdevname>\n", argv[0]);
94 		exit(1);
95 	}
96 
97 	spdk_app_opts_init(&opts);
98 	opts.name = "spdk_mkfs";
99 	opts.config_file = argv[1];
100 	opts.reactor_mask = "0x3";
101 	opts.shutdown_cb = NULL;
102 
103 	spdk_fs_set_cache_size(512);
104 	g_bdev_name = argv[2];
105 	if ((rc = spdk_app_parse_args(argc, argv, &opts, "C:", NULL,
106 				      mkfs_parse_arg, mkfs_usage)) !=
107 	    SPDK_APP_PARSE_ARGS_SUCCESS) {
108 		exit(rc);
109 	}
110 
111 	rc = spdk_app_start(&opts, spdk_mkfs_run, NULL);
112 	spdk_app_fini();
113 
114 	return rc;
115 }
116