xref: /spdk/test/blobfs/mkfs/mkfs.c (revision 22898a91b9b6f289933db19b0175821cfb7e7820)
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/blobfs.h"
37 #include "spdk/bdev.h"
38 #include "spdk/event.h"
39 #include "spdk/blob_bdev.h"
40 #include "spdk/log.h"
41 #include "spdk/string.h"
42 
43 struct spdk_bs_dev *g_bs_dev;
44 const char *g_bdev_name;
45 static uint64_t g_cluster_size;
46 
47 static void
48 stop_cb(void *ctx, int fserrno)
49 {
50 	spdk_app_stop(0);
51 }
52 
53 static void
54 shutdown_cb(void *arg1, void *arg2)
55 {
56 	struct spdk_filesystem *fs = arg1;
57 
58 	printf("done.\n");
59 	spdk_fs_unload(fs, stop_cb, NULL);
60 }
61 
62 static void
63 init_cb(void *ctx, struct spdk_filesystem *fs, int fserrno)
64 {
65 	struct spdk_event *event;
66 
67 	event = spdk_event_allocate(0, shutdown_cb, fs, NULL);
68 	spdk_event_call(event);
69 }
70 
71 static void
72 spdk_mkfs_run(void *arg1, void *arg2)
73 {
74 	struct spdk_bdev *bdev;
75 	struct spdk_blobfs_opts blobfs_opt;
76 
77 	bdev = spdk_bdev_get_by_name(g_bdev_name);
78 
79 	if (bdev == NULL) {
80 		SPDK_ERRLOG("bdev %s not found\n", g_bdev_name);
81 		spdk_app_stop(-1);
82 		return;
83 	}
84 
85 	printf("Initializing filesystem on bdev %s...", g_bdev_name);
86 	fflush(stdout);
87 
88 	spdk_fs_opts_init(&blobfs_opt);
89 	if (g_cluster_size) {
90 		blobfs_opt.cluster_sz = g_cluster_size;
91 	}
92 	g_bs_dev = spdk_bdev_create_bs_dev(bdev, NULL, NULL);
93 	if (blobfs_opt.cluster_sz) {
94 		spdk_fs_init(g_bs_dev, &blobfs_opt, NULL, init_cb, NULL);
95 	} else {
96 		spdk_fs_init(g_bs_dev, NULL, NULL, init_cb, NULL);
97 	}
98 }
99 
100 static void
101 mkfs_usage(void)
102 {
103 	printf(" -C cluster size\n");
104 }
105 
106 static void
107 mkfs_parse_arg(int ch, char *arg)
108 {
109 	bool has_prefix;
110 
111 	switch (ch) {
112 	case 'C':
113 		spdk_parse_capacity(arg, &g_cluster_size, &has_prefix);
114 		break;
115 	default:
116 		break;
117 	}
118 
119 }
120 
121 int main(int argc, char **argv)
122 {
123 	struct spdk_app_opts opts = {};
124 	int rc = 0;
125 
126 	if (argc < 3) {
127 		SPDK_ERRLOG("usage: %s <conffile> <bdevname>\n", argv[0]);
128 		exit(1);
129 	}
130 
131 	spdk_app_opts_init(&opts);
132 	opts.name = "spdk_mkfs";
133 	opts.config_file = argv[1];
134 	opts.reactor_mask = "0x3";
135 	opts.mem_size = 1024;
136 	opts.shutdown_cb = NULL;
137 
138 	spdk_fs_set_cache_size(512);
139 	g_bdev_name = argv[2];
140 	if ((rc = spdk_app_parse_args(argc, argv, &opts, "C:",
141 				      mkfs_parse_arg, mkfs_usage)) !=
142 	    SPDK_APP_PARSE_ARGS_SUCCESS) {
143 		exit(rc);
144 	}
145 
146 	rc = spdk_app_start(&opts, spdk_mkfs_run, NULL, NULL);
147 	spdk_app_fini();
148 
149 	return rc;
150 }
151