xref: /spdk/examples/blob/cli/blobcli.c (revision d1165a653907c85812beba21bf9cb6c657cf3bf1)
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/bdev.h"
37 #include "spdk/env.h"
38 #include "spdk/event.h"
39 #include "spdk/blob_bdev.h"
40 #include "spdk/blob.h"
41 #include "spdk/log.h"
42 #include "spdk/version.h"
43 #include "spdk/string.h"
44 
45 /*
46  * The following is not a public header file, but the CLI does expose
47  * some internals of blobstore for dev/debug puposes so we
48  * include it here.
49  */
50 #include "../lib/blob/blobstore.h"
51 static void cli_start(void *arg1, void *arg2);
52 
53 static const char *program_name = "blobcli";
54 /* default name for .conf file, any name can be used however with -c switch */
55 static const char *program_conf = "blobcli.conf";
56 
57 /*
58  * CMD mode runs one command at a time which can be annoying as the init takes
59  * a few seconds, so the shell mode, invoked with -S, does the init once and gives
60  * the user an interactive shell instead. With script mode init is also done just
61  * once.
62  */
63 enum cli_mode_type {
64 	CLI_MODE_CMD,
65 	CLI_MODE_SHELL,
66 	CLI_MODE_SCRIPT
67 };
68 
69 enum cli_action_type {
70 	CLI_NONE,
71 	CLI_IMPORT_BLOB,
72 	CLI_DUMP_BLOB,
73 	CLI_FILL,
74 	CLI_REM_XATTR,
75 	CLI_SET_XATTR,
76 	CLI_SET_SUPER,
77 	CLI_SHOW_BS,
78 	CLI_SHOW_BLOB,
79 	CLI_CREATE_BLOB,
80 	CLI_LIST_BDEVS,
81 	CLI_LIST_BLOBS,
82 	CLI_INIT_BS,
83 	CLI_SHELL_EXIT,
84 	CLI_HELP,
85 };
86 
87 #define BUFSIZE 255
88 #define MAX_ARGS 16
89 #define ALIGN_4K 4096
90 #define STARTING_PAGE 0
91 #define NUM_PAGES 1
92 
93 /*
94  * The CLI uses the SPDK app framework so is async and callback driven. A
95  * pointer to this structure is passed to SPDK calls and returned in the
96  * callbacks for easy access to all the info we may need.
97  */
98 struct cli_context_t {
99 	struct spdk_blob_store *bs;
100 	struct spdk_blob *blob;
101 	struct spdk_bs_dev *bs_dev;
102 	spdk_blob_id blobid;
103 	spdk_blob_id superid;
104 	struct spdk_io_channel *channel;
105 	uint8_t *buff;
106 	uint64_t page_size;
107 	uint64_t page_count;
108 	uint64_t blob_pages;
109 	uint64_t bytes_so_far;
110 	FILE *fp;
111 	enum cli_action_type action;
112 	char key[BUFSIZE + 1];
113 	char value[BUFSIZE + 1];
114 	char file[BUFSIZE + 1];
115 	uint64_t filesize;
116 	int fill_value;
117 	char bdev_name[BUFSIZE];
118 	int rc;
119 	int num_clusters;
120 	enum cli_mode_type cli_mode;
121 	const char *config_file;
122 	int argc;
123 	char *argv[MAX_ARGS];
124 	bool app_started;
125 	char script_file[BUFSIZE + 1];
126 };
127 
128 /* we store a bunch of stuff in a global struct for use by scripting mode */
129 #define MAX_SCRIPT_LINES 64
130 #define MAX_SCRIPT_BLOBS 16
131 struct cli_script_t {
132 	spdk_blob_id blobid[MAX_SCRIPT_BLOBS];
133 	int blobid_idx;
134 	int max_index;
135 	int cmdline_idx;
136 	bool ignore_errors;
137 	char *cmdline[MAX_SCRIPT_LINES];
138 };
139 struct cli_script_t g_script;
140 
141 /*
142  * Common printing of commands for CLI and shell modes.
143  */
144 static void
145 print_cmds(void)
146 {
147 	printf("\nCommands include:\n");
148 	printf("\t-b bdev - name of the block device to use (example: Nvme0n1)\n");
149 	printf("\t-d <blobid> filename - dump contents of a blob to a file\n");
150 	printf("\t-f <blobid> value - fill a blob with a decimal value\n");
151 	printf("\t-h - this help screen\n");
152 	printf("\t-i - initialize a blobstore\n");
153 	printf("\t-l bdevs | blobs - list either available bdevs or existing blobs\n");
154 	printf("\t-m <blobid> filename - import contents of a file to a blob\n");
155 	printf("\t-n <# clusters> - create new blob\n");
156 	printf("\t-p <blobid> - set the superblob to the ID provided\n");
157 	printf("\t-r <blobid> name - remove xattr name/value pair\n");
158 	printf("\t-s <blobid> | bs - show blob info or blobstore info\n");
159 	printf("\t-x <blobid> name value - set xattr name/value pair\n");
160 	printf("\t-X - exit when in interactive shell mode\n");
161 	printf("\t-S - enter interactive shell mode\n");
162 	printf("\t-T <filename> - automated script mode\n");
163 	printf("\n");
164 }
165 
166 /*
167  * Prints usage and relevant error message.
168  */
169 static void
170 usage(struct cli_context_t *cli_context, char *msg)
171 {
172 	if (msg) {
173 		printf("%s", msg);
174 	}
175 
176 	if (!cli_context || cli_context->cli_mode == CLI_MODE_CMD) {
177 		printf("Version %s\n", SPDK_VERSION_STRING);
178 		printf("Usage: %s [-c SPDK config_file] Command\n", program_name);
179 		printf("\n%s is a command line tool for interacting with blobstore\n",
180 		       program_name);
181 		printf("on the underlying device specified in the conf file passed\n");
182 		printf("in as a command line option.\n");
183 	}
184 	if (!cli_context || cli_context->cli_mode != CLI_MODE_SCRIPT) {
185 		print_cmds();
186 	}
187 }
188 
189 /*
190  * Free up memory that we allocated.
191  */
192 static void
193 cli_cleanup(struct cli_context_t *cli_context)
194 {
195 	if (cli_context->buff) {
196 		spdk_dma_free(cli_context->buff);
197 	}
198 	if (cli_context->cli_mode == CLI_MODE_SCRIPT) {
199 		int i;
200 
201 		for (i = 0; i <= g_script.max_index; i++) {
202 			free(g_script.cmdline[i]);
203 		}
204 	}
205 	free(cli_context);
206 }
207 
208 /*
209  * Callback routine for the blobstore unload.
210  */
211 static void
212 unload_complete(void *cb_arg, int bserrno)
213 {
214 	struct cli_context_t *cli_context = cb_arg;
215 
216 	if (bserrno) {
217 		printf("Error %d unloading the bobstore\n", bserrno);
218 		cli_context->rc = bserrno;
219 	}
220 
221 	/*
222 	 * Quit if we're in cmd mode or exiting shell mode, otherwise
223 	 * clear the action field and start the main function again.
224 	 */
225 	if (cli_context->cli_mode == CLI_MODE_CMD ||
226 	    cli_context->action == CLI_SHELL_EXIT) {
227 		spdk_app_stop(cli_context->rc);
228 	} else {
229 		/* when action is CLI_NONE, we know we need to remain in the shell */
230 		cli_context->bs = NULL;
231 		cli_context->action = CLI_NONE;
232 		cli_start(cli_context, NULL);
233 	}
234 }
235 
236 /*
237  * Unload the blobstore.
238  */
239 static void
240 unload_bs(struct cli_context_t *cli_context, char *msg, int bserrno)
241 {
242 	if (bserrno) {
243 		printf("%s (err %d)\n", msg, bserrno);
244 		cli_context->rc = bserrno;
245 	}
246 
247 	if (cli_context->bs) {
248 		if (cli_context->channel) {
249 			spdk_bs_free_io_channel(cli_context->channel);
250 			cli_context->channel = NULL;
251 		}
252 		spdk_bs_unload(cli_context->bs, unload_complete, cli_context);
253 	} else if (cli_context->cli_mode != CLI_MODE_SCRIPT) {
254 		spdk_app_stop(bserrno);
255 
256 	}
257 }
258 
259 /*
260  * Callback for closing a blob.
261  */
262 static void
263 close_cb(void *arg1, int bserrno)
264 {
265 	struct cli_context_t *cli_context = arg1;
266 
267 	if (bserrno) {
268 		unload_bs(cli_context, "Error in close callback",
269 			  bserrno);
270 		return;
271 	}
272 	unload_bs(cli_context, "", 0);
273 }
274 
275 /*
276  * Callback function for sync'ing metadata.
277  */
278 static void
279 sync_cb(void *arg1, int bserrno)
280 {
281 	struct cli_context_t *cli_context = arg1;
282 
283 	if (bserrno) {
284 		unload_bs(cli_context, "Error in sync callback",
285 			  bserrno);
286 		return;
287 	}
288 
289 	spdk_blob_close(cli_context->blob, close_cb, cli_context);
290 }
291 
292 /*
293  * Callback function for opening a blob after creating.
294  */
295 static void
296 open_now_resize_cb(void *cb_arg, struct spdk_blob *blob, int bserrno)
297 {
298 	struct cli_context_t *cli_context = cb_arg;
299 	int rc = 0;
300 	uint64_t total = 0;
301 
302 	if (bserrno) {
303 		unload_bs(cli_context, "Error in open completion",
304 			  bserrno);
305 		return;
306 	}
307 	cli_context->blob = blob;
308 
309 	rc = spdk_blob_resize(cli_context->blob, cli_context->num_clusters);
310 	if (rc) {
311 		unload_bs(cli_context, "Error in blob resize",
312 			  bserrno);
313 		return;
314 	}
315 
316 	total = spdk_blob_get_num_clusters(cli_context->blob);
317 	printf("blob now has USED clusters of %" PRIu64 "\n",
318 	       total);
319 
320 	/*
321 	 * Always a good idea to sync after MD changes or the changes
322 	 * may be lost if things aren't closed cleanly.
323 	 */
324 	spdk_blob_sync_md(cli_context->blob, sync_cb, cli_context);
325 }
326 
327 /*
328  * Callback function for creating a blob.
329  */
330 static void
331 blob_create_cb(void *arg1, spdk_blob_id blobid, int bserrno)
332 {
333 	struct cli_context_t *cli_context = arg1;
334 
335 	if (bserrno) {
336 		unload_bs(cli_context, "Error in blob create callback",
337 			  bserrno);
338 		return;
339 	}
340 
341 	cli_context->blobid = blobid;
342 	printf("New blob id %" PRIu64 "\n", cli_context->blobid);
343 
344 	/* if we're in script mode, we need info on all blobids for later */
345 	if (cli_context->cli_mode == CLI_MODE_SCRIPT) {
346 		g_script.blobid[g_script.blobid_idx++] = blobid;
347 	}
348 
349 	/* We have to open the blob before we can do things like resize. */
350 	spdk_bs_open_blob(cli_context->bs, cli_context->blobid,
351 			  open_now_resize_cb, cli_context);
352 }
353 
354 /*
355  * Callback for get_super where we'll continue on to show blobstore info.
356  */
357 static void
358 show_bs_cb(void *arg1, spdk_blob_id blobid, int bserrno)
359 {
360 	struct cli_context_t *cli_context = arg1;
361 	struct spdk_bs_type bstype;
362 	uint64_t val;
363 	struct spdk_bdev *bdev = NULL;
364 
365 	if (bserrno && bserrno != -ENOENT) {
366 		unload_bs(cli_context, "Error in get_super callback",
367 			  bserrno);
368 		return;
369 	}
370 	cli_context->superid = blobid;
371 
372 	bdev = spdk_bdev_get_by_name(cli_context->bdev_name);
373 	if (bdev == NULL) {
374 		unload_bs(cli_context, "Error w/bdev in get_super callback",
375 			  bserrno);
376 		return;
377 	}
378 
379 	printf("Blobstore Public Info:\n");
380 	printf("\tUsing bdev Product Name: %s\n",
381 	       spdk_bdev_get_product_name(bdev));
382 	printf("\tAPI Version: %d\n", SPDK_BS_VERSION);
383 
384 	if (bserrno != -ENOENT) {
385 		printf("\tsuper blob ID: %" PRIu64 "\n", cli_context->superid);
386 	} else {
387 		printf("\tsuper blob ID: none assigned\n");
388 	}
389 
390 	printf("\tpage size: %" PRIu64 "\n", cli_context->page_size);
391 
392 	val = spdk_bs_get_cluster_size(cli_context->bs);
393 	printf("\tcluster size: %" PRIu64 "\n", val);
394 
395 	val = spdk_bs_free_cluster_count(cli_context->bs);
396 	printf("\t# free clusters: %" PRIu64 "\n", val);
397 
398 	bstype = spdk_bs_get_bstype(cli_context->bs);
399 	spdk_trace_dump(stdout, "\tblobstore type:", &bstype, sizeof(bstype));
400 
401 	/*
402 	 * Private info isn't accessible via the public API but
403 	 * may be useful for debug of blobstore based applications.
404 	 */
405 	printf("\nBlobstore Private Info:\n");
406 	printf("\tMetadata start (pages): %" PRIu64 "\n",
407 	       cli_context->bs->md_start);
408 	printf("\tMetadata length (pages): %d\n",
409 	       cli_context->bs->md_len);
410 
411 	unload_bs(cli_context, "", 0);
412 }
413 
414 /*
415  * Show detailed info about a particular blob.
416  */
417 static void
418 show_blob(struct cli_context_t *cli_context)
419 {
420 	uint64_t val;
421 	struct spdk_xattr_names *names;
422 	const void *value;
423 	size_t value_len;
424 	char data[BUFSIZE];
425 	unsigned int i;
426 
427 	printf("Blob Public Info:\n");
428 
429 	printf("blob ID: %" PRIu64 "\n", cli_context->blobid);
430 
431 	val = spdk_blob_get_num_clusters(cli_context->blob);
432 	printf("# of clusters: %" PRIu64 "\n", val);
433 
434 	printf("# of bytes: %" PRIu64 "\n",
435 	       val * spdk_bs_get_cluster_size(cli_context->bs));
436 
437 	val = spdk_blob_get_num_pages(cli_context->blob);
438 	printf("# of pages: %" PRIu64 "\n", val);
439 
440 	spdk_blob_get_xattr_names(cli_context->blob, &names);
441 
442 	printf("# of xattrs: %d\n", spdk_xattr_names_get_count(names));
443 	printf("xattrs:\n");
444 	for (i = 0; i < spdk_xattr_names_get_count(names); i++) {
445 		spdk_blob_get_xattr_value(cli_context->blob,
446 					  spdk_xattr_names_get_name(names, i),
447 					  &value, &value_len);
448 		if ((value_len + 1) > sizeof(data)) {
449 			printf("FYI: adjusting size of xattr due to CLI limits.\n");
450 			value_len = sizeof(data) - 1;
451 		}
452 		memcpy(&data, value, value_len);
453 		data[value_len] = '\0';
454 		printf("\n(%d) Name:%s\n", i,
455 		       spdk_xattr_names_get_name(names, i));
456 		printf("(%d) Value:\n", i);
457 		spdk_trace_dump(stdout, "", value, value_len);
458 	}
459 
460 	/*
461 	 * Private info isn't accessible via the public API but
462 	 * may be useful for debug of blobstore based applications.
463 	 */
464 	printf("\nBlob Private Info:\n");
465 	switch (cli_context->blob->state) {
466 	case SPDK_BLOB_STATE_DIRTY:
467 		printf("state: DIRTY\n");
468 		break;
469 	case SPDK_BLOB_STATE_CLEAN:
470 		printf("state: CLEAN\n");
471 		break;
472 	case SPDK_BLOB_STATE_LOADING:
473 		printf("state: LOADING\n");
474 		break;
475 	default:
476 		printf("state: UNKNOWN\n");
477 		break;
478 	}
479 	printf("open ref count: %d\n",
480 	       cli_context->blob->open_ref);
481 
482 	spdk_xattr_names_free(names);
483 }
484 
485 /*
486  * Callback for getting the first blob, shared with simple blob listing as well.
487  */
488 static void
489 blob_iter_cb(void *arg1, struct spdk_blob *blob, int bserrno)
490 {
491 	struct cli_context_t *cli_context = arg1;
492 
493 	if (bserrno) {
494 		if (bserrno == -ENOENT) {
495 			/* this simply means there are no more blobs */
496 			unload_bs(cli_context, "", 0);
497 		} else {
498 			unload_bs(cli_context, "Error in blob iter callback",
499 				  bserrno);
500 		}
501 		return;
502 	}
503 
504 	if (cli_context->action == CLI_LIST_BLOBS) {
505 		printf("\nList BLOBS:\n");
506 		printf("Found blob with ID# %" PRIu64 "\n",
507 		       spdk_blob_get_id(blob));
508 	} else if (spdk_blob_get_id(blob) == cli_context->blobid) {
509 		/*
510 		 * Found the blob we're looking for, but we need to finish
511 		 * iterating even after showing the info so that internally
512 		 * the blobstore logic will close the blob. Or we could
513 		 * chose to close it now, either way.
514 		 */
515 		cli_context->blob = blob;
516 		show_blob(cli_context);
517 	}
518 
519 	spdk_bs_iter_next(cli_context->bs, blob, blob_iter_cb, cli_context);
520 }
521 
522 /*
523  * Callback for setting the super blob ID.
524  */
525 static void
526 set_super_cb(void *arg1, int bserrno)
527 {
528 	struct cli_context_t *cli_context = arg1;
529 
530 	if (bserrno) {
531 		unload_bs(cli_context, "Error in set_super callback",
532 			  bserrno);
533 		return;
534 	}
535 
536 	printf("Super Blob ID has been set.\n");
537 	unload_bs(cli_context, "", 0);
538 }
539 
540 /*
541  * Callback for set_xattr_open where we set or delete xattrs.
542  */
543 static void
544 set_xattr_cb(void *cb_arg, struct spdk_blob *blob, int bserrno)
545 {
546 	struct cli_context_t *cli_context = cb_arg;
547 
548 	if (bserrno) {
549 		unload_bs(cli_context, "Error in blob open callback",
550 			  bserrno);
551 		return;
552 	}
553 	cli_context->blob = blob;
554 
555 	if (cli_context->action == CLI_SET_XATTR) {
556 		spdk_blob_set_xattr(cli_context->blob, cli_context->key,
557 				    cli_context->value, strlen(cli_context->value) + 1);
558 		printf("Xattr has been set.\n");
559 	} else {
560 		spdk_blob_remove_xattr(cli_context->blob, cli_context->key);
561 		printf("Xattr has been removed.\n");
562 	}
563 
564 	spdk_blob_sync_md(cli_context->blob, sync_cb, cli_context);
565 }
566 
567 /*
568  * Callback function for reading a blob for dumping to a file.
569  */
570 static void
571 read_dump_cb(void *arg1, int bserrno)
572 {
573 	struct cli_context_t *cli_context = arg1;
574 	uint64_t bytes_written;
575 
576 	if (bserrno) {
577 		fclose(cli_context->fp);
578 		unload_bs(cli_context, "Error in read completion",
579 			  bserrno);
580 		return;
581 	}
582 
583 	bytes_written = fwrite(cli_context->buff, NUM_PAGES, cli_context->page_size,
584 			       cli_context->fp);
585 	if (bytes_written != cli_context->page_size) {
586 		fclose(cli_context->fp);
587 		unload_bs(cli_context, "Error with fwrite",
588 			  bserrno);
589 		return;
590 	}
591 
592 	printf(".");
593 	if (++cli_context->page_count < cli_context->blob_pages) {
594 		/* perform another read */
595 		spdk_blob_io_read(cli_context->blob, cli_context->channel,
596 				  cli_context->buff, cli_context->page_count,
597 				  NUM_PAGES, read_dump_cb, cli_context);
598 	} else {
599 		/* done reading */
600 		printf("\nFile write complete (to %s).\n", cli_context->file);
601 		fclose(cli_context->fp);
602 		spdk_blob_close(cli_context->blob, close_cb, cli_context);
603 	}
604 }
605 
606 /*
607  * Callback for write completion on the import of a file to a blob.
608  */
609 static void
610 write_imp_cb(void *arg1, int bserrno)
611 {
612 	struct cli_context_t *cli_context = arg1;
613 	uint64_t bytes_read;
614 
615 	if (bserrno) {
616 		fclose(cli_context->fp);
617 		unload_bs(cli_context, "Error in write completion",
618 			  bserrno);
619 		return;
620 	}
621 
622 	if (cli_context->bytes_so_far < cli_context->filesize) {
623 		/* perform another file read */
624 		bytes_read = fread(cli_context->buff, 1,
625 				   cli_context->page_size,
626 				   cli_context->fp);
627 		cli_context->bytes_so_far += bytes_read;
628 
629 		/* if this read is < 1 page, fill with 0s */
630 		if (bytes_read < cli_context->page_size) {
631 			uint8_t *offset = cli_context->buff + bytes_read;
632 			memset(offset, 0, cli_context->page_size - bytes_read);
633 		}
634 	} else {
635 		/*
636 		 * Done reading the file, fill the rest of the blob with 0s,
637 		 * yeah we're memsetting the same page over and over here
638 		 */
639 		memset(cli_context->buff, 0, cli_context->page_size);
640 	}
641 	if (++cli_context->page_count < cli_context->blob_pages) {
642 		printf(".");
643 		spdk_blob_io_write(cli_context->blob, cli_context->channel,
644 				   cli_context->buff, cli_context->page_count,
645 				   NUM_PAGES, write_imp_cb, cli_context);
646 	} else {
647 		/* done writing */
648 		printf("\nBlob import complete (from %s).\n", cli_context->file);
649 		fclose(cli_context->fp);
650 		spdk_blob_close(cli_context->blob, close_cb, cli_context);
651 	}
652 }
653 
654 /*
655  * Callback for open blobs where we'll continue on dump a blob to a file or
656  * import a file to a blob. For dump, the resulting file will always be the
657  * full size of the blob.  For import, the blob will fill with the file
658  * contents first and then 0 out the rest of the blob.
659  */
660 static void
661 dump_imp_open_cb(void *cb_arg, struct spdk_blob *blob, int bserrno)
662 {
663 	struct cli_context_t *cli_context = cb_arg;
664 
665 	if (bserrno) {
666 		unload_bs(cli_context, "Error in blob open callback",
667 			  bserrno);
668 		return;
669 	}
670 	cli_context->blob = blob;
671 
672 	/*
673 	 * We'll transfer just one page at a time to keep the buffer
674 	 * small. This could be bigger of course.
675 	 */
676 	cli_context->buff = spdk_dma_malloc(cli_context->page_size,
677 					    ALIGN_4K, NULL);
678 	if (cli_context->buff == NULL) {
679 		printf("Error in allocating memory\n");
680 		spdk_blob_close(cli_context->blob, close_cb, cli_context);
681 		return;
682 	}
683 	printf("Working");
684 	cli_context->blob_pages = spdk_blob_get_num_pages(cli_context->blob);
685 	cli_context->page_count = 0;
686 	if (cli_context->action == CLI_DUMP_BLOB) {
687 		cli_context->fp = fopen(cli_context->file, "w");
688 		if (cli_context->fp == NULL) {
689 			printf("Error in opening file\n");
690 			spdk_blob_close(cli_context->blob, close_cb, cli_context);
691 			return;
692 		}
693 
694 		/* read a page of data from the blob */
695 		spdk_blob_io_read(cli_context->blob, cli_context->channel,
696 				  cli_context->buff, cli_context->page_count,
697 				  NUM_PAGES, read_dump_cb, cli_context);
698 	} else {
699 		cli_context->fp = fopen(cli_context->file, "r");
700 		if (cli_context->fp == NULL) {
701 			printf("Error in opening file: errno %d\n", errno);
702 			spdk_blob_close(cli_context->blob, close_cb, cli_context);
703 			return;
704 		}
705 
706 		/* get the filesize then rewind read a page of data from file */
707 		fseek(cli_context->fp, 0L, SEEK_END);
708 		cli_context->filesize = ftell(cli_context->fp);
709 		rewind(cli_context->fp);
710 		cli_context->bytes_so_far = fread(cli_context->buff, NUM_PAGES,
711 						  cli_context->page_size,
712 						  cli_context->fp);
713 
714 		/* if the file is < a page, fill the rest with 0s */
715 		if (cli_context->filesize < cli_context->page_size) {
716 			uint8_t *offset =
717 				cli_context->buff + cli_context->filesize;
718 
719 			memset(offset, 0,
720 			       cli_context->page_size - cli_context->filesize);
721 		}
722 
723 		spdk_blob_io_write(cli_context->blob, cli_context->channel,
724 				   cli_context->buff, cli_context->page_count,
725 				   NUM_PAGES, write_imp_cb, cli_context);
726 	}
727 }
728 
729 /*
730  * Callback function for writing a specific pattern to page 0.
731  */
732 static void
733 write_cb(void *arg1, int bserrno)
734 {
735 	struct cli_context_t *cli_context = arg1;
736 
737 	if (bserrno) {
738 		unload_bs(cli_context, "Error in write completion",
739 			  bserrno);
740 		return;
741 	}
742 	printf(".");
743 	if (++cli_context->page_count < cli_context->blob_pages) {
744 		spdk_blob_io_write(cli_context->blob, cli_context->channel,
745 				   cli_context->buff, cli_context->page_count,
746 				   NUM_PAGES, write_cb, cli_context);
747 	} else {
748 		/* done writing */
749 		printf("\nBlob fill complete (with 0x%x).\n", cli_context->fill_value);
750 		spdk_blob_close(cli_context->blob, close_cb, cli_context);
751 	}
752 }
753 
754 /*
755  * Callback function to fill a blob with a value, callback from open.
756  */
757 static void
758 fill_blob_cb(void *arg1, struct spdk_blob *blob, int bserrno)
759 {
760 	struct cli_context_t *cli_context = arg1;
761 
762 	if (bserrno) {
763 		unload_bs(cli_context, "Error in open callback",
764 			  bserrno);
765 		return;
766 	}
767 
768 	cli_context->blob = blob;
769 	cli_context->page_count = 0;
770 	cli_context->blob_pages = spdk_blob_get_num_pages(cli_context->blob);
771 	cli_context->buff = spdk_dma_malloc(cli_context->page_size,
772 					    ALIGN_4K, NULL);
773 	if (cli_context->buff == NULL) {
774 		unload_bs(cli_context, "Error in allocating memory",
775 			  -ENOMEM);
776 		return;
777 	}
778 
779 	memset(cli_context->buff, cli_context->fill_value,
780 	       cli_context->page_size);
781 	printf("Working");
782 	spdk_blob_io_write(cli_context->blob, cli_context->channel,
783 			   cli_context->buff,
784 			   STARTING_PAGE, NUM_PAGES, write_cb, cli_context);
785 }
786 
787 /*
788  * Multiple actions require us to open the bs first so here we use
789  * a common callback to set a bunch of values and then move on to
790  * the next step saved off via function pointer.
791  */
792 static void
793 load_bs_cb(void *arg1, struct spdk_blob_store *bs, int bserrno)
794 {
795 	struct cli_context_t *cli_context = arg1;
796 
797 	if (bserrno) {
798 		unload_bs(cli_context, "Error in load callback",
799 			  bserrno);
800 		return;
801 	}
802 
803 	cli_context->bs = bs;
804 	cli_context->page_size = spdk_bs_get_page_size(cli_context->bs);
805 	cli_context->channel = spdk_bs_alloc_io_channel(cli_context->bs);
806 	if (cli_context->channel == NULL) {
807 		unload_bs(cli_context, "Error in allocating channel",
808 			  -ENOMEM);
809 		return;
810 	}
811 
812 	switch (cli_context->action) {
813 	case CLI_SET_SUPER:
814 		spdk_bs_set_super(cli_context->bs, cli_context->superid,
815 				  set_super_cb, cli_context);
816 		break;
817 	case CLI_SHOW_BS:
818 		spdk_bs_get_super(cli_context->bs, show_bs_cb, cli_context);
819 		break;
820 	case CLI_CREATE_BLOB:
821 		spdk_bs_create_blob(cli_context->bs, blob_create_cb, cli_context);
822 		break;
823 	case CLI_SET_XATTR:
824 	case CLI_REM_XATTR:
825 		spdk_bs_open_blob(cli_context->bs, cli_context->blobid,
826 				  set_xattr_cb, cli_context);
827 		break;
828 	case CLI_SHOW_BLOB:
829 	case CLI_LIST_BLOBS:
830 		spdk_bs_iter_first(cli_context->bs, blob_iter_cb, cli_context);
831 
832 		break;
833 	case CLI_DUMP_BLOB:
834 	case CLI_IMPORT_BLOB:
835 		spdk_bs_open_blob(cli_context->bs, cli_context->blobid,
836 				  dump_imp_open_cb, cli_context);
837 		break;
838 	case CLI_FILL:
839 		spdk_bs_open_blob(cli_context->bs, cli_context->blobid,
840 				  fill_blob_cb, cli_context);
841 		break;
842 
843 	default:
844 		/* should never get here */
845 		exit(-1);
846 		break;
847 	}
848 }
849 
850 /*
851  * Load the blobstore.
852  */
853 static void
854 load_bs(struct cli_context_t *cli_context)
855 {
856 	struct spdk_bdev *bdev = NULL;
857 	struct spdk_bs_dev *bs_dev = NULL;
858 
859 	bdev = spdk_bdev_get_by_name(cli_context->bdev_name);
860 	if (bdev == NULL) {
861 		printf("Could not find a bdev\n");
862 		spdk_app_stop(-1);
863 		return;
864 	}
865 
866 	bs_dev = spdk_bdev_create_bs_dev(bdev, NULL, NULL);
867 	if (bs_dev == NULL) {
868 		printf("Could not create blob bdev!!\n");
869 		spdk_app_stop(-1);
870 		return;
871 	}
872 
873 	spdk_bs_load(bs_dev, NULL, load_bs_cb, cli_context);
874 }
875 
876 /*
877  * Lists all the blobs on this blobstore.
878  */
879 static void
880 list_bdevs(struct cli_context_t *cli_context)
881 {
882 	struct spdk_bdev *bdev = NULL;
883 
884 	printf("\nList bdevs:\n");
885 
886 	bdev = spdk_bdev_first();
887 	if (bdev == NULL) {
888 		printf("Could not find a bdev\n");
889 	}
890 	while (bdev) {
891 		printf("\tbdev Name: %s\n", spdk_bdev_get_name(bdev));
892 		printf("\tbdev Product Name: %s\n",
893 		       spdk_bdev_get_product_name(bdev));
894 		bdev = spdk_bdev_next(bdev);
895 	}
896 
897 	printf("\n");
898 	if (cli_context->cli_mode == CLI_MODE_CMD) {
899 		spdk_app_stop(0);
900 	} else {
901 		cli_context->action = CLI_NONE;
902 		cli_start(cli_context, NULL);
903 	}
904 }
905 
906 /*
907  * Callback function for initializing a blob.
908  */
909 static void
910 bs_init_cb(void *cb_arg, struct spdk_blob_store *bs,
911 	   int bserrno)
912 {
913 	struct cli_context_t *cli_context = cb_arg;
914 
915 	if (bserrno) {
916 		unload_bs(cli_context, "Error in bs init callback",
917 			  bserrno);
918 		return;
919 	}
920 	cli_context->bs = bs;
921 	printf("blobstore init'd: (%p)\n", cli_context->bs);
922 
923 	unload_bs(cli_context, "", 0);
924 }
925 
926 /*
927  * Initialize a new blobstore.
928  */
929 static void
930 init_bs(struct cli_context_t *cli_context)
931 {
932 	struct spdk_bdev *bdev = NULL;
933 
934 	bdev = spdk_bdev_get_by_name(cli_context->bdev_name);
935 	if (bdev == NULL) {
936 		printf("Could not find a bdev\n");
937 		spdk_app_stop(-1);
938 		return;
939 	}
940 	printf("Init blobstore using bdev Product Name: %s\n",
941 	       spdk_bdev_get_product_name(bdev));
942 
943 	cli_context->bs_dev = spdk_bdev_create_bs_dev(bdev, NULL, NULL);
944 	if (cli_context->bs_dev == NULL) {
945 		printf("Could not create blob bdev!!\n");
946 		spdk_app_stop(-1);
947 		return;
948 	}
949 
950 	spdk_bs_init(cli_context->bs_dev, NULL, bs_init_cb,
951 		     cli_context);
952 }
953 
954 /*
955  * Common cmd/option parser for command and shell modes.
956  */
957 static bool
958 cmd_parser(int argc, char **argv, struct cli_context_t *cli_context)
959 {
960 	int op;
961 	int cmd_chosen = 0;
962 	char resp;
963 
964 	while ((op = getopt(argc, argv, "b:c:d:f:hil:m:n:p:r:s:ST:Xx:")) != -1) {
965 		switch (op) {
966 		case 'b':
967 			if (strcmp(cli_context->bdev_name, "") == 0) {
968 				snprintf(cli_context->bdev_name, BUFSIZE, "%s", optarg);
969 			} else {
970 				printf("Current setting for -b is: %s\n", cli_context->bdev_name);
971 				usage(cli_context, "ERROR: -b option can only be set once.\n");
972 			}
973 			break;
974 		case 'c':
975 			if (cli_context->app_started == false) {
976 				cli_context->config_file = optarg;
977 			} else {
978 				usage(cli_context, "ERROR: -c option not valid during shell mode.\n");
979 			}
980 			break;
981 		case 'd':
982 			if (argv[optind] != NULL) {
983 				cmd_chosen++;
984 				cli_context->action = CLI_DUMP_BLOB;
985 				cli_context->blobid = atoll(optarg);
986 				snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]);
987 			} else {
988 				usage(cli_context, "ERROR: missing parameter.\n");
989 			}
990 			break;
991 		case 'f':
992 			if (argv[optind] != NULL) {
993 				cmd_chosen++;
994 				cli_context->action = CLI_FILL;
995 				cli_context->blobid = atoll(optarg);
996 				cli_context->fill_value = atoi(argv[optind]);
997 			} else {
998 				usage(cli_context, "ERROR: missing parameter.\n");
999 			}
1000 			break;
1001 		case 'h':
1002 			cmd_chosen++;
1003 			cli_context->action = CLI_HELP;
1004 			break;
1005 		case 'i':
1006 			if (cli_context->cli_mode != CLI_MODE_SCRIPT) {
1007 				printf("Your entire blobstore will be destroyed. Are you sure? (y/n) ");
1008 				if (scanf("%c%*c", &resp)) {
1009 					if (resp == 'y' || resp == 'Y') {
1010 						cmd_chosen++;
1011 						cli_context->action = CLI_INIT_BS;
1012 					} else {
1013 						if (cli_context->cli_mode == CLI_MODE_CMD) {
1014 							spdk_app_stop(0);
1015 							return false;
1016 						}
1017 					}
1018 				}
1019 			} else {
1020 				cmd_chosen++;
1021 				cli_context->action = CLI_INIT_BS;
1022 			}
1023 			break;
1024 		case 'r':
1025 			if (argv[optind] != NULL) {
1026 				cmd_chosen++;
1027 				cli_context->action = CLI_REM_XATTR;
1028 				cli_context->blobid = atoll(optarg);
1029 				snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]);
1030 			} else {
1031 				usage(cli_context, "ERROR: missing parameter.\n");
1032 			}
1033 			break;
1034 		case 'l':
1035 			if (strcmp("bdevs", optarg) == 0) {
1036 				cmd_chosen++;
1037 				cli_context->action = CLI_LIST_BDEVS;
1038 			} else if (strcmp("blobs", optarg) == 0) {
1039 				cmd_chosen++;
1040 				cli_context->action = CLI_LIST_BLOBS;
1041 			} else {
1042 				usage(cli_context, "ERROR: invalid option for list\n");
1043 			}
1044 			break;
1045 		case 'm':
1046 			if (argv[optind] != NULL) {
1047 				cmd_chosen++;
1048 				cli_context->action = CLI_IMPORT_BLOB;
1049 				cli_context->blobid = atoll(optarg);
1050 				snprintf(cli_context->file, BUFSIZE, "%s", argv[optind]);
1051 			} else {
1052 				usage(cli_context, "ERROR: missing parameter.\n");
1053 			}
1054 			break;
1055 		case 'n':
1056 			cli_context->num_clusters = atoi(optarg);
1057 			if (cli_context->num_clusters > 0) {
1058 				cmd_chosen++;
1059 				cli_context->action = CLI_CREATE_BLOB;
1060 			} else {
1061 				usage(cli_context, "ERROR: invalid option for new\n");
1062 			}
1063 			break;
1064 		case 'p':
1065 			cmd_chosen++;
1066 			cli_context->action = CLI_SET_SUPER;
1067 			cli_context->superid = atoll(optarg);
1068 			break;
1069 		case 'S':
1070 			if (cli_context->cli_mode == CLI_MODE_CMD) {
1071 				cmd_chosen++;
1072 				cli_context->cli_mode = CLI_MODE_SHELL;
1073 			}
1074 			cli_context->action = CLI_NONE;
1075 			break;
1076 		case 's':
1077 			cmd_chosen++;
1078 			if (strcmp("bs", optarg) == 0) {
1079 				cli_context->action = CLI_SHOW_BS;
1080 			} else {
1081 				cli_context->action = CLI_SHOW_BLOB;
1082 				cli_context->blobid = atoll(optarg);
1083 			}
1084 			break;
1085 		case 'T':
1086 			if (cli_context->cli_mode == CLI_MODE_CMD) {
1087 				cmd_chosen++;
1088 				cli_context->cli_mode = CLI_MODE_SCRIPT;
1089 				if (argv[optind] && (strcmp("ignore", argv[optind]) == 0)) {
1090 					g_script.ignore_errors = true;
1091 				} else {
1092 					g_script.ignore_errors = false;
1093 				}
1094 				snprintf(cli_context->script_file, BUFSIZE, "%s", optarg);
1095 			} else {
1096 				cli_context->action = CLI_NONE;
1097 			}
1098 			break;
1099 		case 'X':
1100 			cmd_chosen++;
1101 			cli_context->action = CLI_SHELL_EXIT;
1102 			break;
1103 		case 'x':
1104 			if (argv[optind] != NULL || argv[optind + 1] != NULL) {
1105 				cmd_chosen++;
1106 				cli_context->action = CLI_SET_XATTR;
1107 				cli_context->blobid = atoll(optarg);
1108 				snprintf(cli_context->key, BUFSIZE, "%s", argv[optind]);
1109 				snprintf(cli_context->value, BUFSIZE, "%s", argv[optind + 1]);
1110 			} else {
1111 				usage(cli_context, "ERROR: missing parameter.\n");
1112 			}
1113 			break;
1114 		default:
1115 			usage(cli_context, "ERROR: invalid option\n");
1116 		}
1117 		/* only one actual command can be done at a time */
1118 		if (cmd_chosen > 1) {
1119 			usage(cli_context, "Error: Please choose only one command\n");
1120 		}
1121 	}
1122 
1123 	if (cli_context->cli_mode == CLI_MODE_CMD && cmd_chosen == 0) {
1124 		usage(cli_context, "Error: Please choose a command.\n");
1125 	}
1126 
1127 	/*
1128 	 * We don't check the local boolean because in some modes it will have been set
1129 	 * on and earlier command.
1130 	 */
1131 	if (strcmp(cli_context->bdev_name, "") == 0) {
1132 		usage(cli_context, "Error: -b option is required.\n");
1133 		cmd_chosen = 0;
1134 	}
1135 
1136 	/* in shell mode we'll call getopt multiple times so need to reset its index */
1137 	optind = 0;
1138 	return (cmd_chosen == 1);
1139 }
1140 
1141 /*
1142  * In script mode, we parsed a script file at startup and saved off a bunch of cmd
1143  * lines that we now parse with each run of cli_start so we us the same cmd parser
1144  * as cmd and shell modes.
1145  */
1146 static bool
1147 line_parser(struct cli_context_t *cli_context)
1148 {
1149 	bool cmd_chosen;
1150 	char *tok = NULL;
1151 	int blob_num = 0;
1152 	int start_idx = cli_context->argc;
1153 	int i;
1154 
1155 	printf("\nSCRIPT NOW PROCESSING: %s\n", g_script.cmdline[g_script.cmdline_idx]);
1156 	tok = strtok(g_script.cmdline[g_script.cmdline_idx], " ");
1157 	while (tok != NULL) {
1158 		/*
1159 		 * We support one replaceable token right now, a $Bn
1160 		 * represents the blobid that was created in position n
1161 		 * so fish this out now and use it here.
1162 		 */
1163 		cli_context->argv[cli_context->argc] = strdup(tok);
1164 		if (tok[0] == '$' && tok[1] == 'B') {
1165 			tok += 2;
1166 			blob_num = atoi(tok);
1167 			if (blob_num >= 0 && blob_num < MAX_SCRIPT_BLOBS) {
1168 				cli_context->argv[cli_context->argc] =
1169 					realloc(cli_context->argv[cli_context->argc], BUFSIZE);
1170 				if (cli_context->argv[cli_context->argc] == NULL) {
1171 					printf("ERROR: unable to realloc memory\n");
1172 					spdk_app_stop(-1);
1173 				}
1174 				if (g_script.blobid[blob_num] == 0) {
1175 					printf("ERROR: There is no blob for $B%d\n",
1176 					       blob_num);
1177 				}
1178 				snprintf(cli_context->argv[cli_context->argc], BUFSIZE,
1179 					 "%" PRIu64, g_script.blobid[blob_num]);
1180 			} else {
1181 				printf("ERROR: Invalid token or exceeded max blobs of %d\n",
1182 				       MAX_SCRIPT_BLOBS);
1183 			}
1184 		}
1185 		cli_context->argc++;
1186 		tok = strtok(NULL, " ");
1187 	}
1188 
1189 	/* call parse cmd line with user input as args */
1190 	cmd_chosen = cmd_parser(cli_context->argc, &cli_context->argv[0], cli_context);
1191 
1192 	/* free strdup memory and reset arg count for next shell interaction */
1193 	for (i = start_idx; i < cli_context->argc; i++) {
1194 		free(cli_context->argv[i]);
1195 		cli_context->argv[i] = NULL;
1196 	}
1197 	cli_context->argc = 1;
1198 
1199 	g_script.cmdline_idx++;
1200 	assert(g_script.cmdline_idx < MAX_SCRIPT_LINES);
1201 
1202 	if (cmd_chosen == false) {
1203 		printf("ERROR: Invalid script line starting with: %s\n\n",
1204 		       g_script.cmdline[g_script.cmdline_idx - 1]);
1205 		if (g_script.ignore_errors == false) {
1206 			printf("** Aborting **\n");
1207 			cli_context->action = CLI_SHELL_EXIT;
1208 			cmd_chosen = true;
1209 			unload_bs(cli_context, "", 0);
1210 		} else {
1211 			printf("** Skipping **\n");
1212 		}
1213 	}
1214 
1215 	return cmd_chosen;
1216 }
1217 
1218 /*
1219  * For script mode, we read a series of commands from a text file and store them
1220  * in a global struct. That, along with the cli_mode that tells us we're in
1221  * script mode is what feeds the rest of the app in the same way as is it were
1222  * getting commands from shell mode.
1223  */
1224 static void
1225 parse_script(struct cli_context_t *cli_context)
1226 {
1227 	FILE *fp = NULL;
1228 	size_t bufsize = BUFSIZE;
1229 	int64_t bytes_in = 0;
1230 	int i = 0;
1231 
1232 	/* initialize global script values */
1233 	for (i = 0; i < MAX_SCRIPT_BLOBS; i++) {
1234 		g_script.blobid[i] = 0;
1235 	}
1236 	g_script.blobid_idx = 0;
1237 	g_script.cmdline_idx = 0;
1238 	i = 0;
1239 
1240 	fp = fopen(cli_context->script_file, "r");
1241 	if (fp == NULL) {
1242 		printf("ERROR: unable to open script: %s\n",
1243 		       cli_context->script_file);
1244 		cli_cleanup(cli_context);
1245 		exit(-1);
1246 	}
1247 
1248 	do {
1249 		bytes_in = getline(&g_script.cmdline[i], &bufsize, fp);
1250 		if (bytes_in > 0) {
1251 			/* replace newline with null */
1252 			spdk_str_chomp(g_script.cmdline[i]);
1253 
1254 			/* ignore comments */
1255 			if (g_script.cmdline[i][0] != '#') {
1256 				i++;
1257 			}
1258 		}
1259 	} while (bytes_in != -1 && i < MAX_SCRIPT_LINES - 1);
1260 	fclose(fp);
1261 
1262 	/* add an exit cmd in case they didn't */
1263 	g_script.cmdline[i] = realloc(g_script.cmdline[i], BUFSIZE);
1264 	if (g_script.cmdline[i] == NULL)  {
1265 		int j;
1266 
1267 		for (j = 0; j < i; j++) {
1268 			free(g_script.cmdline[j]);
1269 			g_script.cmdline[j] = NULL;
1270 		}
1271 		unload_bs(cli_context, "ERROR: unable to alloc memory.\n", 0);
1272 	}
1273 	snprintf(g_script.cmdline[i], BUFSIZE, "%s", "-X");
1274 	g_script.max_index = i;
1275 }
1276 
1277 /*
1278  * Provides for a shell interface as opposed to one shot command line.
1279  */
1280 static bool
1281 cli_shell(void *arg1, void *arg2)
1282 {
1283 	struct cli_context_t *cli_context = arg1;
1284 	char *line = NULL;
1285 	ssize_t buf_size = 0;
1286 	ssize_t bytes_in = 0;
1287 	ssize_t tok_len = 0;
1288 	char *tok = NULL;
1289 	bool cmd_chosen = false;
1290 	int start_idx = cli_context->argc;
1291 	int i;
1292 
1293 	printf("blob> ");
1294 	bytes_in = getline(&line, &buf_size, stdin);
1295 
1296 	/* If getline() failed (EOF), exit the shell. */
1297 	if (bytes_in < 0) {
1298 		free(line);
1299 		cli_context->action = CLI_SHELL_EXIT;
1300 		return true;
1301 	}
1302 
1303 	/* parse input and update cli_context so we can use common option parser */
1304 	if (bytes_in > 0) {
1305 		tok = strtok(line, " ");
1306 	}
1307 	while ((tok != NULL) && (cli_context->argc < MAX_ARGS)) {
1308 		cli_context->argv[cli_context->argc] = strdup(tok);
1309 		tok_len = strlen(tok);
1310 		cli_context->argc++;
1311 		tok = strtok(NULL, " ");
1312 	}
1313 
1314 	/* replace newline on last arg with null */
1315 	if (tok_len) {
1316 		spdk_str_chomp(cli_context->argv[cli_context->argc - 1]);
1317 	}
1318 
1319 	/* call parse cmd line with user input as args */
1320 	cmd_chosen = cmd_parser(cli_context->argc, &cli_context->argv[0], cli_context);
1321 
1322 	/* free strdup mem & reset arg count for next shell interaction */
1323 	for (i = start_idx; i < cli_context->argc; i++) {
1324 		free(cli_context->argv[i]);
1325 		cli_context->argv[i] = NULL;
1326 	}
1327 	cli_context->argc = 1;
1328 
1329 	free(line);
1330 
1331 	return cmd_chosen;
1332 }
1333 
1334 /*
1335  * This is the function we pass into the SPDK framework that gets
1336  * called first.
1337  */
1338 static void
1339 cli_start(void *arg1, void *arg2)
1340 {
1341 	struct cli_context_t *cli_context = arg1;
1342 
1343 	/*
1344 	 * If we're in script mode, we already have a list of commands so
1345 	 * just need to pull them out one at a time and process them.
1346 	 */
1347 	if (cli_context->cli_mode == CLI_MODE_SCRIPT) {
1348 		while (line_parser(cli_context) == false);
1349 	}
1350 
1351 	/*
1352 	 * The initial cmd line options are parsed once before this function is
1353 	 * called so if there is no action, we're in shell mode and will loop
1354 	 * here until a a valid option is parsed and returned.
1355 	 */
1356 	if (cli_context->action == CLI_NONE) {
1357 		while (cli_shell(cli_context, NULL) == false);
1358 	}
1359 
1360 	/* Decide what to do next based on cmd line parsing. */
1361 	switch (cli_context->action) {
1362 	case CLI_SET_SUPER:
1363 	case CLI_SHOW_BS:
1364 	case CLI_CREATE_BLOB:
1365 	case CLI_SET_XATTR:
1366 	case CLI_REM_XATTR:
1367 	case CLI_SHOW_BLOB:
1368 	case CLI_LIST_BLOBS:
1369 	case CLI_DUMP_BLOB:
1370 	case CLI_IMPORT_BLOB:
1371 	case CLI_FILL:
1372 		load_bs(cli_context);
1373 		break;
1374 	case CLI_INIT_BS:
1375 		init_bs(cli_context);
1376 		break;
1377 	case CLI_LIST_BDEVS:
1378 		list_bdevs(cli_context);
1379 		break;
1380 	case CLI_SHELL_EXIT:
1381 		/*
1382 		 * Because shell mode reuses cmd mode functions, the blobstore
1383 		 * is loaded/unloaded with every action so we just need to
1384 		 * stop the framework. For this app there's no need to optimize
1385 		 * and keep the blobstore open while the app is in shell mode.
1386 		 */
1387 		spdk_app_stop(0);
1388 		break;
1389 	case CLI_HELP:
1390 		usage(cli_context, "");
1391 		unload_complete(cli_context, 0);
1392 		break;
1393 	default:
1394 		/* should never get here */
1395 		exit(-1);
1396 		break;
1397 	}
1398 }
1399 
1400 int
1401 main(int argc, char **argv)
1402 {
1403 	struct spdk_app_opts opts = {};
1404 	struct cli_context_t *cli_context = NULL;
1405 	bool cmd_chosen;
1406 	int rc = 0;
1407 
1408 	if (argc < 2) {
1409 		usage(cli_context, "ERROR: Invalid option\n");
1410 		exit(-1);
1411 	}
1412 
1413 	cli_context = calloc(1, sizeof(struct cli_context_t));
1414 	if (cli_context == NULL) {
1415 		printf("ERROR: could not allocate context structure\n");
1416 		exit(-1);
1417 	}
1418 
1419 	/* default to CMD mode until we've parsed the first parms */
1420 	cli_context->cli_mode = CLI_MODE_CMD;
1421 	cli_context->argv[0] = strdup(argv[0]);
1422 	cli_context->argc = 1;
1423 
1424 	/* parse command line */
1425 	cmd_chosen = cmd_parser(argc, argv, cli_context);
1426 	free(cli_context->argv[0]);
1427 	cli_context->argv[0] = NULL;
1428 	if (cmd_chosen == false) {
1429 		cli_cleanup(cli_context);
1430 		exit(-1);
1431 	}
1432 
1433 	/* after displaying help, just exit */
1434 	if (cli_context->action == CLI_HELP) {
1435 		usage(cli_context, "");
1436 		cli_cleanup(cli_context);
1437 		exit(-1);
1438 	}
1439 
1440 	/* if they don't supply a conf name, use the default */
1441 	if (!cli_context->config_file) {
1442 		cli_context->config_file = program_conf;
1443 	}
1444 
1445 	/* if the config file doesn't exist, tell them how to make one */
1446 	if (access(cli_context->config_file, F_OK) == -1) {
1447 		printf("Error: No config file found.\n");
1448 		printf("To create a config file named 'blobcli.conf' for your NVMe device:\n");
1449 		printf("   <path to spdk>/scripts/gen_nvme.sh > blobcli.conf\n");
1450 		printf("and then re-run the cli tool.\n");
1451 		exit(-1);
1452 	}
1453 
1454 	/*
1455 	 * For script mode we keep a bunch of stuff in a global since
1456 	 * none if it is passed back and forth to SPDK.
1457 	 */
1458 	if (cli_context->cli_mode == CLI_MODE_SCRIPT) {
1459 		/*
1460 		 * Now we'll build up the global which will direct this run of the app
1461 		 * as it will have a list (g_script) of all of the commands line by
1462 		 * line as if they were typed in on the shell at cmd line.
1463 		 */
1464 		parse_script(cli_context);
1465 	}
1466 
1467 	/* Set default values in opts struct along with name and conf file. */
1468 	spdk_app_opts_init(&opts);
1469 	opts.name = "blobcli";
1470 	opts.config_file = cli_context->config_file;
1471 
1472 	cli_context->app_started = true;
1473 	rc = spdk_app_start(&opts, cli_start, cli_context, NULL);
1474 	if (rc) {
1475 		printf("ERROR!\n");
1476 	}
1477 
1478 	/* Free up memory that we allocated */
1479 	cli_cleanup(cli_context);
1480 
1481 	/* Gracefully close out all of the SPDK subsystems. */
1482 	spdk_app_fini();
1483 	return rc;
1484 }
1485