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