xref: /spdk/test/blobfs/mkfs/mkfs.c (revision 45a053c5777494f4e8ce4bc1191c9de3920377f7)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 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
60 main(int argc, char **argv)
61 {
62 	struct spdk_app_opts opts = {};
63 	int rc = 0;
64 
65 	if (argc < 3) {
66 		SPDK_ERRLOG("usage: %s <conffile> <bdevname>\n", argv[0]);
67 		exit(1);
68 	}
69 
70 	spdk_app_opts_init(&opts, sizeof(opts));
71 	opts.name = "spdk_mkfs";
72 	opts.json_config_file = argv[1];
73 	opts.reactor_mask = "0x3";
74 	opts.shutdown_cb = NULL;
75 
76 	spdk_fs_set_cache_size(512);
77 	g_bdev_name = argv[2];
78 	if ((rc = spdk_app_parse_args(argc, argv, &opts, "C:", NULL,
79 				      mkfs_parse_arg, mkfs_usage)) !=
80 	    SPDK_APP_PARSE_ARGS_SUCCESS) {
81 		exit(rc);
82 	}
83 
84 	rc = spdk_app_start(&opts, spdk_mkfs_run, NULL);
85 	spdk_app_fini();
86 
87 	return rc;
88 }
89