1 /* $NetBSD: chio.c,v 1.6 1998/01/04 23:53:58 thorpej Exp $ */ 2 /* 3 * Copyright (c) 1996 Jason R. Thorpe <thorpej@and.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgements: 16 * This product includes software developed by Jason R. Thorpe 17 * for And Communications, http://www.and.com/ 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 /* 34 * Additional Copyright (c) 1997, by Matthew Jacob, for NASA/Ames Research Ctr. 35 * Addidional Copyright (c) 2000, by C. Stephen Gunn, Waterspout Communications 36 */ 37 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1996 Jason R. Thorpe. All rights reserved."; 41 #endif /* not lint */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/chio.h> 48 #include <err.h> 49 #include <fcntl.h> 50 #include <stdio.h> 51 #include <stdint.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include "defs.h" 57 #include "pathnames.h" 58 59 static void usage(void); 60 static void cleanup(void); 61 static u_int16_t parse_element_type(char *); 62 static u_int16_t parse_element_unit(char *); 63 static const char * element_type_name(int et); 64 static int parse_special(char *); 65 static int is_special(char *); 66 static const char *bits_to_string(ces_status_flags, const char *); 67 68 static void find_element(char *, uint16_t *, uint16_t *); 69 static struct changer_element_status *get_element_status 70 (unsigned int, unsigned int); 71 72 static int do_move(const char *, int, char **); 73 static int do_exchange(const char *, int, char **); 74 static int do_position(const char *, int, char **); 75 static int do_params(const char *, int, char **); 76 static int do_getpicker(const char *, int, char **); 77 static int do_setpicker(const char *, int, char **); 78 static int do_status(const char *, int, char **); 79 static int do_ielem(const char *, int, char **); 80 static int do_return(const char *, int, char **); 81 static int do_voltag(const char *, int, char **); 82 83 #ifndef CHET_VT 84 #define CHET_VT 10 /* Completely Arbitrary */ 85 #endif 86 87 /* Valid changer element types. */ 88 const struct element_type elements[] = { 89 { "drive", CHET_DT }, 90 { "picker", CHET_MT }, 91 { "portal", CHET_IE }, 92 { "slot", CHET_ST }, 93 { "voltag", CHET_VT }, /* Select tapes by barcode */ 94 { NULL, 0 }, 95 }; 96 97 /* Valid commands. */ 98 const struct changer_command commands[] = { 99 { "exchange", do_exchange }, 100 { "getpicker", do_getpicker }, 101 { "ielem", do_ielem }, 102 { "move", do_move }, 103 { "params", do_params }, 104 { "position", do_position }, 105 { "setpicker", do_setpicker }, 106 { "status", do_status }, 107 { "return", do_return }, 108 { "voltag", do_voltag }, 109 { NULL, 0 }, 110 }; 111 112 /* Valid special words. */ 113 const struct special_word specials[] = { 114 { "inv", SW_INVERT }, 115 { "inv1", SW_INVERT1 }, 116 { "inv2", SW_INVERT2 }, 117 { NULL, 0 }, 118 }; 119 120 static int changer_fd; 121 static const char *changer_name; 122 123 int 124 main(int argc, char **argv) 125 { 126 int ch, i; 127 128 while ((ch = getopt(argc, argv, "f:")) != -1) { 129 switch (ch) { 130 case 'f': 131 changer_name = optarg; 132 break; 133 134 default: 135 usage(); 136 } 137 } 138 argc -= optind; 139 argv += optind; 140 141 if (argc == 0) 142 usage(); 143 144 /* Get the default changer if not already specified. */ 145 if (changer_name == NULL) 146 if ((changer_name = getenv(CHANGER_ENV_VAR)) == NULL) 147 changer_name = _PATH_CH; 148 149 /* Open the changer device. */ 150 if ((changer_fd = open(changer_name, O_RDWR, 0600)) == -1) 151 err(1, "%s: open", changer_name); 152 153 /* Register cleanup function. */ 154 if (atexit(cleanup)) 155 err(1, "can't register cleanup function"); 156 157 /* Find the specified command. */ 158 for (i = 0; commands[i].cc_name != NULL; ++i) 159 if (strcmp(*argv, commands[i].cc_name) == 0) 160 break; 161 if (commands[i].cc_name == NULL) { 162 /* look for abbreviation */ 163 for (i = 0; commands[i].cc_name != NULL; ++i) 164 if (strncmp(*argv, commands[i].cc_name, 165 strlen(*argv)) == 0) 166 break; 167 } 168 169 if (commands[i].cc_name == NULL) 170 errx(1, "unknown command: %s", *argv); 171 172 exit ((*commands[i].cc_handler)(commands[i].cc_name, argc, argv)); 173 /* NOTREACHED */ 174 } 175 176 static int 177 do_move(const char *cname, int argc, char **argv) 178 { 179 struct changer_move cmd; 180 int val; 181 182 /* 183 * On a move command, we expect the following: 184 * 185 * <from ET> <from EU> <to ET> <to EU> [inv] 186 * 187 * where ET == element type and EU == element unit. 188 */ 189 190 ++argv; --argc; 191 192 if (argc < 4) { 193 warnx("%s: too few arguments", cname); 194 goto usage; 195 } else if (argc > 5) { 196 warnx("%s: too many arguments", cname); 197 goto usage; 198 } 199 (void) memset(&cmd, 0, sizeof(cmd)); 200 201 /* <from ET> */ 202 cmd.cm_fromtype = parse_element_type(*argv); 203 ++argv; --argc; 204 205 /* Check for voltag virtual type */ 206 if (CHET_VT == cmd.cm_fromtype) { 207 find_element(*argv, &cmd.cm_fromtype, &cmd.cm_fromunit); 208 } else { 209 /* <from EU> */ 210 cmd.cm_fromunit = parse_element_unit(*argv); 211 } 212 ++argv; --argc; 213 214 /* <to ET> */ 215 cmd.cm_totype = parse_element_type(*argv); 216 ++argv; --argc; 217 218 /* Check for voltag virtual type, and report error */ 219 if (CHET_VT == cmd.cm_totype) 220 errx(1,"%s: voltag only makes sense as an element source", 221 cname); 222 223 /* <to EU> */ 224 cmd.cm_tounit = parse_element_unit(*argv); 225 ++argv; --argc; 226 227 /* Deal with optional command modifier. */ 228 if (argc) { 229 val = parse_special(*argv); 230 switch (val) { 231 case SW_INVERT: 232 cmd.cm_flags |= CM_INVERT; 233 break; 234 235 default: 236 errx(1, "%s: inappropriate modifier `%s'", 237 cname, *argv); 238 /* NOTREACHED */ 239 } 240 } 241 242 /* Send command to changer. */ 243 if (ioctl(changer_fd, CHIOMOVE, &cmd)) 244 err(1, "%s: CHIOMOVE", changer_name); 245 246 return (0); 247 248 usage: 249 (void) fprintf(stderr, "usage: %s %s " 250 "<from ET> <from EU> <to ET> <to EU> [inv]\n", getprogname(), cname); 251 return (1); 252 } 253 254 static int 255 do_exchange(const char *cname, int argc, char **argv) 256 { 257 struct changer_exchange cmd; 258 int val; 259 260 /* 261 * On an exchange command, we expect the following: 262 * 263 * <src ET> <src EU> <dst1 ET> <dst1 EU> [<dst2 ET> <dst2 EU>] [inv1] [inv2] 264 * 265 * where ET == element type and EU == element unit. 266 */ 267 268 ++argv; --argc; 269 270 if (argc < 4) { 271 warnx("%s: too few arguments", cname); 272 goto usage; 273 } else if (argc > 8) { 274 warnx("%s: too many arguments", cname); 275 goto usage; 276 } 277 (void) memset(&cmd, 0, sizeof(cmd)); 278 279 /* <src ET> */ 280 cmd.ce_srctype = parse_element_type(*argv); 281 ++argv; --argc; 282 283 /* Check for voltag virtual type */ 284 if (CHET_VT == cmd.ce_srctype) { 285 find_element(*argv, &cmd.ce_srctype, &cmd.ce_srcunit); 286 } else { 287 /* <from EU> */ 288 cmd.ce_srcunit = parse_element_unit(*argv); 289 } 290 ++argv; --argc; 291 292 /* <dst1 ET> */ 293 cmd.ce_fdsttype = parse_element_type(*argv); 294 ++argv; --argc; 295 296 /* Check for voltag virtual type */ 297 if (CHET_VT == cmd.ce_fdsttype) { 298 find_element(*argv, &cmd.ce_fdsttype, &cmd.ce_fdstunit); 299 } else { 300 /* <from EU> */ 301 cmd.ce_fdstunit = parse_element_unit(*argv); 302 } 303 ++argv; --argc; 304 305 /* 306 * If the next token is a special word or there are no more 307 * arguments, then this is a case of simple exchange. 308 * dst2 == src. 309 */ 310 if ((argc == 0) || is_special(*argv)) { 311 cmd.ce_sdsttype = cmd.ce_srctype; 312 cmd.ce_sdstunit = cmd.ce_srcunit; 313 goto do_special; 314 } 315 316 /* <dst2 ET> */ 317 cmd.ce_sdsttype = parse_element_type(*argv); 318 ++argv; --argc; 319 320 if (CHET_VT == cmd.ce_sdsttype) 321 errx(1,"%s %s: voltag only makes sense as an element source", 322 cname, *argv); 323 324 /* <dst2 EU> */ 325 cmd.ce_sdstunit = parse_element_unit(*argv); 326 ++argv; --argc; 327 328 do_special: 329 /* Deal with optional command modifiers. */ 330 while (argc) { 331 val = parse_special(*argv); 332 ++argv; --argc; 333 switch (val) { 334 case SW_INVERT1: 335 cmd.ce_flags |= CE_INVERT1; 336 break; 337 338 case SW_INVERT2: 339 cmd.ce_flags |= CE_INVERT2; 340 break; 341 342 default: 343 errx(1, "%s: inappropriate modifier `%s'", 344 cname, *argv); 345 /* NOTREACHED */ 346 } 347 } 348 349 /* Send command to changer. */ 350 if (ioctl(changer_fd, CHIOEXCHANGE, &cmd)) 351 err(1, "%s: CHIOEXCHANGE", changer_name); 352 353 return (0); 354 355 usage: 356 (void) fprintf(stderr, 357 "usage: %s %s <src ET> <src EU> <dst1 ET> <dst1 EU>\n" 358 " [<dst2 ET> <dst2 EU>] [inv1] [inv2]\n", 359 getprogname(), cname); 360 return (1); 361 } 362 363 static int 364 do_position(const char *cname, int argc, char **argv) 365 { 366 struct changer_position cmd; 367 int val; 368 369 /* 370 * On a position command, we expect the following: 371 * 372 * <to ET> <to EU> [inv] 373 * 374 * where ET == element type and EU == element unit. 375 */ 376 377 ++argv; --argc; 378 379 if (argc < 2) { 380 warnx("%s: too few arguments", cname); 381 goto usage; 382 } else if (argc > 3) { 383 warnx("%s: too many arguments", cname); 384 goto usage; 385 } 386 (void) memset(&cmd, 0, sizeof(cmd)); 387 388 /* <to ET> */ 389 cmd.cp_type = parse_element_type(*argv); 390 ++argv; --argc; 391 392 /* <to EU> */ 393 cmd.cp_unit = parse_element_unit(*argv); 394 ++argv; --argc; 395 396 /* Deal with optional command modifier. */ 397 if (argc) { 398 val = parse_special(*argv); 399 switch (val) { 400 case SW_INVERT: 401 cmd.cp_flags |= CP_INVERT; 402 break; 403 404 default: 405 errx(1, "%s: inappropriate modifier `%s'", 406 cname, *argv); 407 /* NOTREACHED */ 408 } 409 } 410 411 /* Send command to changer. */ 412 if (ioctl(changer_fd, CHIOPOSITION, &cmd)) 413 err(1, "%s: CHIOPOSITION", changer_name); 414 415 return (0); 416 417 usage: 418 (void) fprintf(stderr, "usage: %s %s <to ET> <to EU> [inv]\n", 419 getprogname(), cname); 420 return (1); 421 } 422 423 /* ARGSUSED */ 424 static int 425 do_params(const char *cname, int argc, char **argv) 426 { 427 struct changer_params data; 428 int picker; 429 430 /* No arguments to this command. */ 431 432 ++argv; --argc; 433 434 if (argc) { 435 warnx("%s: no arguments expected", cname); 436 goto usage; 437 } 438 439 /* Get params from changer and display them. */ 440 (void) memset(&data, 0, sizeof(data)); 441 if (ioctl(changer_fd, CHIOGPARAMS, &data)) 442 err(1, "%s: CHIOGPARAMS", changer_name); 443 444 (void) printf("%s: %d slot%s, %d drive%s, %d picker%s", 445 changer_name, 446 data.cp_nslots, (data.cp_nslots > 1) ? "s" : "", 447 data.cp_ndrives, (data.cp_ndrives > 1) ? "s" : "", 448 data.cp_npickers, (data.cp_npickers > 1) ? "s" : ""); 449 if (data.cp_nportals) 450 (void) printf(", %d portal%s", data.cp_nportals, 451 (data.cp_nportals > 1) ? "s" : ""); 452 453 /* Get current picker from changer and display it. */ 454 if (ioctl(changer_fd, CHIOGPICKER, &picker)) 455 err(1, "%s: CHIOGPICKER", changer_name); 456 457 (void) printf("\n%s: current picker: %d\n", changer_name, picker); 458 459 return (0); 460 461 usage: 462 (void) fprintf(stderr, "usage: %s %s\n", getprogname(), cname); 463 return (1); 464 } 465 466 /* ARGSUSED */ 467 static int 468 do_getpicker(const char *cname, int argc, char **argv) 469 { 470 int picker; 471 472 /* No arguments to this command. */ 473 474 ++argv; --argc; 475 476 if (argc) { 477 warnx("%s: no arguments expected", cname); 478 goto usage; 479 } 480 481 /* Get current picker from changer and display it. */ 482 if (ioctl(changer_fd, CHIOGPICKER, &picker)) 483 err(1, "%s: CHIOGPICKER", changer_name); 484 485 (void) printf("%s: current picker: %d\n", changer_name, picker); 486 487 return (0); 488 489 usage: 490 (void) fprintf(stderr, "usage: %s %s\n", getprogname(), cname); 491 return (1); 492 } 493 494 static int 495 do_setpicker(const char *cname, int argc, char **argv) 496 { 497 int picker; 498 499 ++argv; --argc; 500 501 if (argc < 1) { 502 warnx("%s: too few arguments", cname); 503 goto usage; 504 } else if (argc > 1) { 505 warnx("%s: too many arguments", cname); 506 goto usage; 507 } 508 509 picker = parse_element_unit(*argv); 510 511 /* Set the changer picker. */ 512 if (ioctl(changer_fd, CHIOSPICKER, &picker)) 513 err(1, "%s: CHIOSPICKER", changer_name); 514 515 return (0); 516 517 usage: 518 (void) fprintf(stderr, "usage: %s %s <picker>\n", getprogname(), cname); 519 return (1); 520 } 521 522 static int 523 do_status(const char *cname, int argc, char **argv) 524 { 525 struct changer_params cp; 526 struct changer_element_status_request cesr; 527 int i; 528 u_int16_t base, count, chet, schet, echet; 529 const char *description; 530 int pvoltag = 0; 531 int avoltag = 0; 532 int sense = 0; 533 int scsi = 0; 534 int source = 0; 535 int intaddr = 0; 536 int c; 537 538 count = 0; 539 base = 0; 540 description = NULL; 541 542 optind = optreset = 1; 543 while ((c = getopt(argc, argv, "vVsSbaI")) != -1) { 544 switch (c) { 545 case 'v': 546 pvoltag = 1; 547 break; 548 case 'V': 549 avoltag = 1; 550 break; 551 case 's': 552 sense = 1; 553 break; 554 case 'S': 555 source = 1; 556 break; 557 case 'b': 558 scsi = 1; 559 break; 560 case 'I': 561 intaddr = 1; 562 break; 563 case 'a': 564 pvoltag = avoltag = source = sense = scsi = intaddr = 1; 565 break; 566 default: 567 warnx("%s: bad option", cname); 568 goto usage; 569 } 570 } 571 572 argc -= optind; 573 argv += optind; 574 575 /* 576 * On a status command, we expect the following: 577 * 578 * [<ET> [<start> [<end>] ] ] 579 * 580 * where ET == element type, start == first element to report, 581 * end == number of elements to report 582 * 583 * If we get no arguments, we get the status of all 584 * known element types. 585 */ 586 if (argc > 3) { 587 warnx("%s: too many arguments", cname); 588 goto usage; 589 } 590 591 /* 592 * Get params from changer. Specifically, we need the element 593 * counts. 594 */ 595 if (ioctl(changer_fd, CHIOGPARAMS, (char *)&cp)) 596 err(1, "%s: CHIOGPARAMS", changer_name); 597 598 if (argc > 0) 599 schet = echet = parse_element_type(argv[0]); 600 else { 601 schet = CHET_MT; 602 echet = CHET_DT; 603 } 604 if (argc > 1) { 605 base = (u_int16_t)atol(argv[1]); 606 count = 1; 607 } 608 if (argc > 2) 609 count = (u_int16_t)atol(argv[2]) - base + 1; 610 611 for (chet = schet; chet <= echet; ++chet) { 612 switch (chet) { 613 case CHET_MT: 614 if (count == 0) 615 count = cp.cp_npickers; 616 else if (count > cp.cp_npickers) 617 errx(1, "not that many pickers in device"); 618 description = "picker"; 619 break; 620 621 case CHET_ST: 622 if (count == 0) 623 count = cp.cp_nslots; 624 else if (count > cp.cp_nslots) 625 errx(1, "not that many slots in device"); 626 description = "slot"; 627 break; 628 629 case CHET_IE: 630 if (count == 0) 631 count = cp.cp_nportals; 632 else if (count > cp.cp_nportals) 633 errx(1, "not that many portals in device"); 634 description = "portal"; 635 break; 636 637 case CHET_DT: 638 if (count == 0) 639 count = cp.cp_ndrives; 640 else if (count > cp.cp_ndrives) 641 errx(1, "not that many drives in device"); 642 description = "drive"; 643 break; 644 645 default: 646 /* To appease gcc -Wuninitialized. */ 647 count = 0; 648 description = NULL; 649 } 650 651 if (count == 0) { 652 if (argc == 0) 653 continue; 654 else { 655 printf("%s: no %s elements\n", 656 changer_name, description); 657 return (0); 658 } 659 } 660 661 bzero(&cesr, sizeof(cesr)); 662 cesr.cesr_element_type = chet; 663 cesr.cesr_element_base = base; 664 cesr.cesr_element_count = count; 665 /* Allocate storage for the status structures. */ 666 cesr.cesr_element_status = 667 (struct changer_element_status *) 668 calloc((size_t)count, sizeof(struct changer_element_status)); 669 670 if (!cesr.cesr_element_status) 671 errx(1, "can't allocate status storage"); 672 673 if (avoltag || pvoltag) 674 cesr.cesr_flags |= CESR_VOLTAGS; 675 676 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr)) { 677 free(cesr.cesr_element_status); 678 err(1, "%s: CHIOGSTATUS", changer_name); 679 } 680 681 /* Dump the status for each reported element. */ 682 for (i = 0; i < count; ++i) { 683 struct changer_element_status *ces = 684 &(cesr.cesr_element_status[i]); 685 printf("%s %d: %s", description, ces->ces_addr, 686 bits_to_string(ces->ces_flags, 687 CESTATUS_BITS)); 688 if (sense) 689 printf(" sense: <0x%02x/0x%02x>", 690 ces->ces_sensecode, 691 ces->ces_sensequal); 692 if (pvoltag) 693 printf(" voltag: <%s:%d>", 694 ces->ces_pvoltag.cv_volid, 695 ces->ces_pvoltag.cv_serial); 696 if (avoltag) 697 printf(" avoltag: <%s:%d>", 698 ces->ces_avoltag.cv_volid, 699 ces->ces_avoltag.cv_serial); 700 if (source) { 701 if (ces->ces_flags & CES_SOURCE_VALID) 702 printf(" source: <%s %d>", 703 element_type_name( 704 ces->ces_source_type), 705 ces->ces_source_addr); 706 else 707 printf(" source: <>"); 708 } 709 if (intaddr) 710 printf(" intaddr: <%d>", ces->ces_int_addr); 711 if (scsi) { 712 printf(" scsi: <"); 713 if (ces->ces_flags & CES_SCSIID_VALID) 714 printf("%d", ces->ces_scsi_id); 715 else 716 putchar('?'); 717 putchar(':'); 718 if (ces->ces_flags & CES_LUN_VALID) 719 printf("%d", ces->ces_scsi_lun); 720 else 721 putchar('?'); 722 putchar('>'); 723 } 724 putchar('\n'); 725 } 726 727 free(cesr.cesr_element_status); 728 count = 0; 729 } 730 731 return (0); 732 733 usage: 734 (void) fprintf(stderr, "usage: %s %s [-vVsSbaA] [<element type> [<start-addr> [<end-addr>] ] ]\n", 735 getprogname(), cname); 736 return (1); 737 } 738 739 static int 740 do_ielem(const char *cname, int argc, char **argv) 741 { 742 int timeout = 0; 743 744 if (argc == 2) { 745 timeout = atol(argv[1]); 746 } else if (argc > 1) { 747 warnx("%s: too many arguments", cname); 748 goto usage; 749 } 750 751 if (ioctl(changer_fd, CHIOIELEM, &timeout)) 752 err(1, "%s: CHIOIELEM", changer_name); 753 754 return (0); 755 756 usage: 757 (void) fprintf(stderr, "usage: %s %s [<timeout>]\n", 758 getprogname(), cname); 759 return (1); 760 } 761 762 static int 763 do_voltag(const char *cname, int argc, char **argv) 764 { 765 int force = 0; 766 int clear = 0; 767 int alternate = 0; 768 int c; 769 struct changer_set_voltag_request csvr; 770 771 bzero(&csvr, sizeof(csvr)); 772 773 optind = optreset = 1; 774 while ((c = getopt(argc, argv, "fca")) != -1) { 775 switch (c) { 776 case 'f': 777 force = 1; 778 break; 779 case 'c': 780 clear = 1; 781 break; 782 case 'a': 783 alternate = 1; 784 break; 785 default: 786 warnx("%s: bad option", cname); 787 goto usage; 788 } 789 } 790 791 argc -= optind; 792 argv += optind; 793 794 if (argc < 2) { 795 warnx("%s: missing element specification", cname); 796 goto usage; 797 } 798 799 csvr.csvr_type = parse_element_type(argv[0]); 800 csvr.csvr_addr = (u_int16_t)atol(argv[1]); 801 802 if (!clear) { 803 if (argc < 3 || argc > 4) { 804 warnx("%s: missing argument", cname); 805 goto usage; 806 } 807 808 if (force) 809 csvr.csvr_flags = CSVR_MODE_REPLACE; 810 else 811 csvr.csvr_flags = CSVR_MODE_SET; 812 813 if (strlen(argv[2]) > sizeof(csvr.csvr_voltag.cv_volid)) { 814 warnx("%s: volume label too long", cname); 815 goto usage; 816 } 817 818 strlcpy((char *)csvr.csvr_voltag.cv_volid, argv[2], 819 sizeof(csvr.csvr_voltag.cv_volid)); 820 821 if (argc == 4) { 822 csvr.csvr_voltag.cv_serial = (u_int16_t)atol(argv[3]); 823 } 824 } else { 825 if (argc != 2) { 826 warnx("%s: unexpected argument", cname); 827 goto usage; 828 } 829 csvr.csvr_flags = CSVR_MODE_CLEAR; 830 } 831 832 if (alternate) { 833 csvr.csvr_flags |= CSVR_ALTERNATE; 834 } 835 836 if (ioctl(changer_fd, CHIOSETVOLTAG, &csvr)) 837 err(1, "%s: CHIOSETVOLTAG", changer_name); 838 839 return 0; 840 usage: 841 (void) fprintf(stderr, 842 "usage: %s %s [-fca] <element> [<voltag> [<vsn>] ]\n", 843 getprogname(), cname); 844 return 1; 845 } 846 847 static u_int16_t 848 parse_element_type(char *cp) 849 { 850 int i; 851 852 for (i = 0; elements[i].et_name != NULL; ++i) 853 if (strcmp(elements[i].et_name, cp) == 0) 854 return ((u_int16_t)elements[i].et_type); 855 856 errx(1, "invalid element type `%s'", cp); 857 /* NOTREACHED */ 858 } 859 860 static const char * 861 element_type_name(int et) 862 { 863 int i; 864 865 for (i = 0; elements[i].et_name != NULL; i++) 866 if (elements[i].et_type == et) 867 return elements[i].et_name; 868 869 return "unknown"; 870 } 871 872 static u_int16_t 873 parse_element_unit(char *cp) 874 { 875 int i; 876 char *p; 877 878 i = (int)strtol(cp, &p, 10); 879 if ((i < 0) || (*p != '\0')) 880 errx(1, "invalid unit number `%s'", cp); 881 882 return ((u_int16_t)i); 883 } 884 885 static int 886 parse_special(char *cp) 887 { 888 int val; 889 890 val = is_special(cp); 891 if (val) 892 return (val); 893 894 errx(1, "invalid modifier `%s'", cp); 895 /* NOTREACHED */ 896 } 897 898 static int 899 is_special(char *cp) 900 { 901 int i; 902 903 for (i = 0; specials[i].sw_name != NULL; ++i) 904 if (strcmp(specials[i].sw_name, cp) == 0) 905 return (specials[i].sw_value); 906 907 return (0); 908 } 909 910 static const char * 911 bits_to_string(ces_status_flags v, const char *cp) 912 { 913 const char *np; 914 char f, sep, *bp; 915 static char buf[128]; 916 917 bp = buf; 918 (void) memset(buf, 0, sizeof(buf)); 919 920 for (sep = '<'; (f = *cp++) != 0; cp = np) { 921 for (np = cp; *np >= ' ';) 922 np++; 923 if (((int)v & (1 << (f - 1))) == 0) 924 continue; 925 (void) snprintf(bp, sizeof(buf) - (size_t)(bp - &buf[0]), 926 "%c%.*s", sep, (int)(long)(np - cp), cp); 927 bp += strlen(bp); 928 sep = ','; 929 } 930 if (sep != '<') 931 *bp = '>'; 932 933 return (buf); 934 } 935 /* 936 * do_return() 937 * 938 * Given an element reference, ask the changer/picker to move that 939 * element back to its source slot. 940 */ 941 static int 942 do_return(const char *cname, int argc, char **argv) 943 { 944 struct changer_element_status *ces; 945 struct changer_move cmd; 946 uint16_t type, element; 947 948 ++argv; --argc; 949 950 if (argc < 2) { 951 warnx("%s: too few arguments", cname); 952 goto usage; 953 } else if (argc > 3) { 954 warnx("%s: too many arguments", cname); 955 goto usage; 956 } 957 958 type = parse_element_type(*argv); 959 ++argv; --argc; 960 961 /* Handle voltag virtual Changer Element Type */ 962 if (CHET_VT == type) { 963 find_element(*argv, &type, &element); 964 } else { 965 element = parse_element_unit(*argv); 966 } 967 ++argv; --argc; 968 969 /* Get the status */ 970 ces = get_element_status((unsigned int)type, (unsigned int)element); 971 972 if (NULL == ces) 973 errx(1, "%s: null element status pointer", cname); 974 975 if (!(ces->ces_flags & CES_SOURCE_VALID)) 976 errx(1, "%s: no source information", cname); 977 978 (void) memset(&cmd, 0, sizeof(cmd)); 979 980 cmd.cm_fromtype = type; 981 cmd.cm_fromunit = element; 982 cmd.cm_totype = ces->ces_source_type; 983 cmd.cm_tounit = ces->ces_source_addr; 984 985 if (ioctl(changer_fd, CHIOMOVE, &cmd) == -1) 986 err(1, "%s: CHIOMOVE", changer_name); 987 free(ces); 988 989 return(0); 990 991 usage: 992 (void) fprintf(stderr, "usage: %s %s " 993 "<from ET> <from EU>\n", getprogname(), cname); 994 return(1); 995 } 996 997 /* 998 * get_element_status() 999 * 1000 * return a *cesr for the specified changer element. This 1001 * routing will malloc()/calloc() the memory. The caller 1002 * should free() it when done. 1003 */ 1004 static struct changer_element_status * 1005 get_element_status(unsigned int type, unsigned int element) 1006 { 1007 struct changer_element_status_request cesr; 1008 struct changer_element_status *ces; 1009 1010 ces = (struct changer_element_status *) 1011 calloc((size_t)1, sizeof(struct changer_element_status)); 1012 1013 if (NULL == ces) 1014 errx(1, "can't allocate status storage"); 1015 1016 (void)memset(&cesr, 0, sizeof(cesr)); 1017 1018 cesr.cesr_element_type = (uint16_t)type; 1019 cesr.cesr_element_base = (uint16_t)element; 1020 cesr.cesr_element_count = 1; /* Only this one element */ 1021 cesr.cesr_flags |= CESR_VOLTAGS; /* Grab voltags as well */ 1022 cesr.cesr_element_status = ces; 1023 1024 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) { 1025 free(ces); 1026 err(1, "%s: CHIOGSTATUS", changer_name); 1027 /* NOTREACHED */ 1028 } 1029 1030 return ces; 1031 } 1032 1033 1034 /* 1035 * find_element() 1036 * 1037 * Given a <voltag> find the chager element and unit, or exit 1038 * with an error if it isn't found. We grab the changer status 1039 * and iterate until we find a match, or crap out. 1040 */ 1041 static void 1042 find_element(char *voltag, uint16_t *et, uint16_t *eu) 1043 { 1044 struct changer_params cp; 1045 struct changer_element_status_request cesr; 1046 struct changer_element_status *ch_ces, *ces; 1047 int found = 0; 1048 size_t elem, total_elem; 1049 1050 /* 1051 * Get the changer parameters, we're interested in the counts 1052 * for all types of elements to perform our search. 1053 */ 1054 if (ioctl(changer_fd, CHIOGPARAMS, (char *)&cp)) 1055 err(1, "%s: CHIOGPARAMS", changer_name); 1056 1057 /* Allocate some memory for the results */ 1058 total_elem = (cp.cp_nslots + cp.cp_ndrives 1059 + cp.cp_npickers + cp.cp_nportals); 1060 1061 ch_ces = (struct changer_element_status *) 1062 calloc(total_elem, sizeof(struct changer_element_status)); 1063 1064 if (NULL == ch_ces) 1065 errx(1, "can't allocate status storage"); 1066 1067 ces = ch_ces; 1068 1069 /* Read in the changer slots */ 1070 if (cp.cp_nslots > 0) { 1071 cesr.cesr_element_type = CHET_ST; 1072 cesr.cesr_element_base = 0; 1073 cesr.cesr_element_count = cp.cp_nslots; 1074 cesr.cesr_flags |= CESR_VOLTAGS; 1075 cesr.cesr_element_status = ces; 1076 1077 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) { 1078 free(ch_ces); 1079 err(1, "%s: CHIOGSTATUS", changer_name); 1080 } 1081 ces += cp.cp_nslots; 1082 } 1083 1084 /* Read in the drive information */ 1085 if (cp.cp_ndrives > 0 ) { 1086 1087 (void) memset(&cesr, 0, sizeof(cesr)); 1088 cesr.cesr_element_type = CHET_DT; 1089 cesr.cesr_element_base = 0; 1090 cesr.cesr_element_count = cp.cp_ndrives; 1091 cesr.cesr_flags |= CESR_VOLTAGS; 1092 cesr.cesr_element_status = ces; 1093 1094 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) { 1095 free(ch_ces); 1096 err(1, "%s: CHIOGSTATUS", changer_name); 1097 } 1098 ces += cp.cp_ndrives; 1099 } 1100 1101 /* Read in the portal information */ 1102 if (cp.cp_nportals > 0 ) { 1103 (void) memset(&cesr, 0, sizeof(cesr)); 1104 cesr.cesr_element_type = CHET_IE; 1105 cesr.cesr_element_base = 0; 1106 cesr.cesr_element_count = cp.cp_nportals; 1107 cesr.cesr_flags |= CESR_VOLTAGS; 1108 cesr.cesr_element_status = ces; 1109 1110 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) { 1111 free(ch_ces); 1112 err(1, "%s: CHIOGSTATUS", changer_name); 1113 } 1114 ces += cp.cp_nportals; 1115 } 1116 1117 /* Read in the picker information */ 1118 if (cp.cp_npickers > 0) { 1119 (void) memset(&cesr, 0, sizeof(cesr)); 1120 cesr.cesr_element_type = CHET_MT; 1121 cesr.cesr_element_base = 0; 1122 cesr.cesr_element_count = cp.cp_npickers; 1123 cesr.cesr_flags |= CESR_VOLTAGS; 1124 cesr.cesr_element_status = ces; 1125 1126 if (ioctl(changer_fd, CHIOGSTATUS, (char *)&cesr) == -1) { 1127 free(ch_ces); 1128 err(1, "%s: CHIOGSTATUS", changer_name); 1129 } 1130 } 1131 1132 /* 1133 * Now search the list the specified <voltag> 1134 */ 1135 for (elem = 0; elem <= total_elem; ++elem) { 1136 1137 ces = &ch_ces[elem]; 1138 1139 /* Make sure we have a tape in this element */ 1140 if ((ces->ces_flags & (CES_STATUS_ACCESS|CES_STATUS_FULL)) 1141 != (CES_STATUS_ACCESS|CES_STATUS_FULL)) 1142 continue; 1143 1144 /* Check to see if it is our target */ 1145 if (strcasecmp(voltag, 1146 (const char *)ces->ces_pvoltag.cv_volid) == 0) { 1147 *et = ces->ces_type; 1148 *eu = ces->ces_addr; 1149 ++found; 1150 break; 1151 } 1152 } 1153 if (!found) { 1154 errx(1, "%s: unable to locate voltag: %s", changer_name, 1155 voltag); 1156 } 1157 free(ch_ces); 1158 return; 1159 } 1160 1161 static void 1162 cleanup(void) 1163 { 1164 /* Simple enough... */ 1165 (void)close(changer_fd); 1166 } 1167 1168 static void 1169 usage(void) 1170 { 1171 (void)fprintf(stderr, "usage: %s [-f changer] command [-<flags>] " 1172 "arg1 arg2 [arg3 [...]]\n", getprogname()); 1173 exit(1); 1174 } 1175