xref: /spdk/test/blobfs/mkfs/mkfs.c (revision 488570ebd418ba07c9e69e65106dcc964f3bb41b)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk/event.h"
9 #include "spdk/blobfs.h"
10 #include "spdk/blobfs_bdev.h"
11 #include "spdk/log.h"
12 #include "spdk/string.h"
13 
14 const char *g_bdev_name;
15 static uint64_t g_cluster_size;
16 
17 static void
18 shutdown_cb(void *cb_arg, int fserrno)
19 {
20 	if (fserrno) {
21 		printf("\nFailed to initialize filesystem on bdev %s...", g_bdev_name);
22 	}
23 
24 	printf("done.\n");
25 
26 	spdk_app_stop(0);
27 }
28 
29 static void
30 spdk_mkfs_run(void *arg1)
31 {
32 	printf("Initializing filesystem on bdev %s...", g_bdev_name);
33 	fflush(stdout);
34 
35 	spdk_blobfs_bdev_create(g_bdev_name, g_cluster_size, shutdown_cb, NULL);
36 }
37 
38 static void
39 mkfs_usage(void)
40 {
41 	printf(" -C <size>                 cluster size\n");
42 }
43 
44 static int
45 mkfs_parse_arg(int ch, char *arg)
46 {
47 	bool has_prefix;
48 
49 	switch (ch) {
50 	case 'C':
51 		spdk_parse_capacity(arg, &g_cluster_size, &has_prefix);
52 		break;
53 	default:
54 		return -EINVAL;
55 	}
56 	return 0;
57 }
58 
59 int main(int argc, char **argv)
60 {
61 	struct spdk_app_opts opts = {};
62 	int rc = 0;
63 
64 	if (argc < 3) {
65 		SPDK_ERRLOG("usage: %s <conffile> <bdevname>\n", argv[0]);
66 		exit(1);
67 	}
68 
69 	spdk_app_opts_init(&opts, sizeof(opts));
70 	opts.name = "spdk_mkfs";
71 	opts.json_config_file = argv[1];
72 	opts.reactor_mask = "0x3";
73 	opts.shutdown_cb = NULL;
74 
75 	spdk_fs_set_cache_size(512);
76 	g_bdev_name = argv[2];
77 	if ((rc = spdk_app_parse_args(argc, argv, &opts, "C:", NULL,
78 				      mkfs_parse_arg, mkfs_usage)) !=
79 	    SPDK_APP_PARSE_ARGS_SUCCESS) {
80 		exit(rc);
81 	}
82 
83 	rc = spdk_app_start(&opts, spdk_mkfs_run, NULL);
84 	spdk_app_fini();
85 
86 	return rc;
87 }
88