1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2012 Milan Jurik. All rights reserved. 24 * Copyright 2014 Toomas Soome <tsoome@me.com> 25 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 27 * Copyright (c) 2016 by Delphix. All rights reserved. 28 */ 29 30 /* 31 * This file contains functions that implement the command menu commands. 32 */ 33 34 #include "global.h" 35 #include <time.h> 36 #include <sys/time.h> 37 #include <sys/resource.h> 38 #include <sys/wait.h> 39 #include <strings.h> 40 #include <signal.h> 41 #include <stdlib.h> 42 #include <string.h> 43 44 #if defined(sparc) 45 #include <sys/hdio.h> 46 #endif /* defined(sparc) */ 47 48 #include "main.h" 49 #include "analyze.h" 50 #include "menu.h" 51 #include "menu_command.h" 52 #include "menu_defect.h" 53 #include "menu_partition.h" 54 #include "param.h" 55 #include "misc.h" 56 #include "label.h" 57 #include "startup.h" 58 #include "partition.h" 59 #include "prompts.h" 60 #include "checkdev.h" 61 #include "io.h" 62 #include "ctlr_scsi.h" 63 #include "auto_sense.h" 64 #include "modify_partition.h" 65 66 67 extern struct menu_item menu_partition[]; 68 extern struct menu_item menu_analyze[]; 69 extern struct menu_item menu_defect[]; 70 int prot_type; 71 72 /* 73 * Choices for the p_tag vtoc field 74 */ 75 slist_t ptag_choices[] = { 76 { "unassigned", "", V_UNASSIGNED }, 77 { "boot", "", V_BOOT }, 78 { "root", "", V_ROOT }, 79 { "swap", "", V_SWAP }, 80 { "usr", "", V_USR }, 81 { "backup", "", V_BACKUP }, 82 { "stand", "", V_STAND }, 83 { "var", "", V_VAR }, 84 { "home", "", V_HOME }, 85 { "alternates", "", V_ALTSCTR }, 86 { "reserved", "", V_RESERVED }, 87 { "system", "", V_SYSTEM }, 88 { "BIOS_boot", "", V_BIOS_BOOT }, 89 { "FreeBSD boot", "", V_FREEBSD_BOOT }, 90 { "FreeBSD swap", "", V_FREEBSD_SWAP }, 91 { "FreeBSD UFS", "", V_FREEBSD_UFS }, 92 { "FreeBSD ZFS", "", V_FREEBSD_ZFS }, 93 94 { NULL } 95 }; 96 97 98 /* 99 * Choices for the p_flag vtoc field 100 */ 101 slist_t pflag_choices[] = { 102 { "wm", "read-write, mountable", 0 }, 103 { "wu", "read-write, unmountable", V_UNMNT }, 104 { "rm", "read-only, mountable", V_RONLY }, 105 { "ru", "read-only, unmountable", V_RONLY|V_UNMNT }, 106 { NULL } 107 }; 108 109 110 /* 111 * This routine implements the 'disk' command. It allows the user to 112 * select a disk to be current. The list of choices is the list of 113 * disks that were found at startup time. 114 */ 115 int 116 c_disk(void) 117 { 118 struct disk_info *disk; 119 u_ioparam_t ioparam; 120 int i; 121 int ndisks = 0; 122 int blind_select = 0; 123 int deflt; 124 int index; 125 int *defltptr = NULL; 126 int more = 0; 127 int more_quit = 0; 128 int one_line = 0; 129 int tty_lines = 24; 130 131 /* 132 * This buffer holds the check() prompt that verifies we've got the right 133 * disk when performing a blind selection. The size should be sufficient 134 * to hold the prompt string, plus 256 characters for the disk name - 135 * way more than should ever be necessary. See the #define in misc.h. 136 */ 137 char chk_buf[BLIND_SELECT_VER_PROMPT]; 138 139 if (istokenpresent()) { 140 /* 141 * disk number to be selected is already in the 142 * input stream . 143 */ 144 TOKEN token, cleantoken; 145 146 /* 147 * Get the disk number the user has given. 148 */ 149 i = 0; 150 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 151 i++; 152 } 153 154 ioparam.io_bounds.lower = 0; 155 ioparam.io_bounds.upper = i - 1; 156 (void) gettoken(token); 157 clean_token(cleantoken, token); 158 159 /* 160 * Convert the token into an integer. 161 */ 162 if (geti(cleantoken, &index, NULL)) 163 return (0); 164 165 /* 166 * Check to be sure it is within the legal bounds. 167 */ 168 if ((index < 0) || (index >= i)) { 169 err_print("`%d' is out of range.\n", index); 170 return (0); 171 } 172 goto checkdisk; 173 } 174 175 fmt_print("\n\nAVAILABLE DISK SELECTIONS:\n"); 176 177 i = 0; 178 if ((option_f == NULL) && isatty(0) == 1 && isatty(1) == 1) { 179 /* 180 * We have a real terminal for std input and output, enable 181 * more style of output for disk selection list. 182 */ 183 more = 1; 184 tty_lines = get_tty_lines(); 185 enter_critical(); 186 echo_off(); 187 charmode_on(); 188 exit_critical(); 189 } 190 191 /* 192 * Loop through the list of found disks. 193 */ 194 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 195 /* 196 * If using more output, account 2 lines for each disk. 197 */ 198 if (more && !more_quit && i && (one_line || 199 ((2 * i + 1) % (tty_lines - 2) <= 1))) { 200 int c; 201 202 /* 203 * Get the next character. 204 */ 205 fmt_print("- hit space for more or s to select - "); 206 c = getchar(); 207 fmt_print("\015"); 208 one_line = 0; 209 /* 210 * Handle display one line command 211 * (return key) 212 */ 213 if (c == '\012') { 214 one_line++; 215 } 216 /* Handle Quit command */ 217 if (c == 'q') { 218 fmt_print( 219 " \015"); 220 more_quit++; 221 } 222 /* Handle ^D command */ 223 if (c == '\004') 224 fullabort(); 225 /* or get on with the show */ 226 if (c == 's' || c == 'S') { 227 fmt_print("%80s\n", " "); 228 break; 229 } 230 } 231 /* 232 * If this is the current disk, mark it as 233 * the default. 234 */ 235 if (cur_disk == disk) { 236 deflt = i; 237 defltptr = &deflt; 238 } 239 if (!more || !more_quit) 240 pr_diskline(disk, i); 241 i++; 242 } 243 if (more) { 244 enter_critical(); 245 charmode_off(); 246 echo_on(); 247 exit_critical(); 248 } 249 250 /* 251 * Determine total number of disks, and ask the user which disk they 252 * would like to make current. 253 */ 254 255 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 256 ndisks++; 257 } 258 259 ioparam.io_bounds.lower = 0; 260 ioparam.io_bounds.upper = ndisks - 1; 261 index = input(FIO_INT, "Specify disk (enter its number)", ':', 262 &ioparam, defltptr, DATA_INPUT); 263 264 if (index >= i) { 265 blind_select = 1; 266 } 267 268 /* 269 * Find the disk chosen. Search through controllers/disks 270 * in the same original order, so we match what the user 271 * chose. 272 */ 273 checkdisk: 274 i = 0; 275 for (disk = disk_list; disk != NULL; disk = disk->disk_next) { 276 if (i == index) 277 goto found; 278 i++; 279 } 280 /* 281 * Should never happen. 282 */ 283 impossible("no disk found"); 284 285 found: 286 if (blind_select) { 287 (void) snprintf(chk_buf, sizeof (chk_buf), 288 "Disk %s selected - is this the desired disk? ", disk->disk_name); 289 if (check(chk_buf)) { 290 return (-1); 291 } 292 } 293 294 /* 295 * Update the state. We lock out interrupts so the state can't 296 * get half-updated. 297 */ 298 299 enter_critical(); 300 init_globals(disk); 301 exit_critical(); 302 303 /* 304 * If type unknown and interactive, ask user to specify type. 305 * Also, set partition table (best guess) too. 306 */ 307 if (!option_f && ncyl == 0 && nhead == 0 && nsect == 0 && 308 (disk->label_type != L_TYPE_EFI)) { 309 (void) c_type(); 310 } 311 312 /* 313 * Get the Solaris Fdisk Partition information 314 */ 315 if (nhead != 0 && nsect != 0) 316 (void) copy_solaris_part(&cur_disk->fdisk_part); 317 318 if ((cur_disk->label_type == L_TYPE_EFI) && 319 (cur_disk->disk_parts->etoc->efi_flags & 320 EFI_GPT_PRIMARY_CORRUPT)) { 321 err_print("Reading the primary EFI GPT label "); 322 err_print("failed. Using backup label.\n"); 323 err_print("Use the 'backup' command to restore "); 324 err_print("the primary label.\n"); 325 } 326 327 #if defined(_SUNOS_VTOC_16) 328 /* 329 * If there is no fdisk solaris partition. 330 */ 331 if (cur_disk->fdisk_part.numsect == 0) { 332 err_print("No Solaris fdisk partition found.\n"); 333 goto exit; 334 } 335 #endif /* defined(_SUNOS_VTOC_16) */ 336 337 /* 338 * If the label of the disk is marked dirty, 339 * see if they'd like to label the disk now. 340 */ 341 if (cur_disk->disk_flags & DSK_LABEL_DIRTY) { 342 if (check("Disk not labeled. Label it now") == 0) { 343 if (write_label()) { 344 err_print("Write label failed\n"); 345 } else { 346 cur_disk->disk_flags &= ~DSK_LABEL_DIRTY; 347 } 348 } 349 } 350 exit: 351 return (0); 352 } 353 354 /* 355 * This routine implements the 'type' command. It allows the user to 356 * specify the type of the current disk. It should be necessary only 357 * if the disk was not labelled or was somehow labelled incorrectly. 358 * The list of legal types for the disk comes from information that was 359 * in the data file. 360 */ 361 int 362 c_type(void) 363 { 364 struct disk_type *type, *tptr, *oldtype; 365 u_ioparam_t ioparam; 366 int i, index, deflt, *defltptr = NULL; 367 struct disk_type disk_type; 368 struct disk_type *d = &disk_type; 369 int first_disk; 370 int auto_conf_choice; 371 int other_choice; 372 struct dk_label label; 373 struct efi_info efi_info; 374 uint64_t maxLBA; 375 char volname[LEN_DKL_VVOL + 1]; 376 int volinit = 0; 377 378 /* 379 * There must be a current disk. 380 */ 381 if (cur_disk == NULL) { 382 err_print("Current Disk is not set.\n"); 383 return (-1); 384 } 385 oldtype = cur_disk->disk_type; 386 type = cur_ctype->ctype_dlist; 387 /* 388 * Print out the list of choices. 389 */ 390 fmt_print("\n\nAVAILABLE DRIVE TYPES:\n"); 391 first_disk = 0; 392 if (cur_ctype->ctype_ctype == DKC_SCSI_CCS) { 393 auto_conf_choice = 0; 394 fmt_print(" %d. Auto configure\n", first_disk++); 395 } else { 396 auto_conf_choice = -1; 397 } 398 399 i = first_disk; 400 for (tptr = type; tptr != NULL; tptr = tptr->dtype_next) { 401 /* 402 * If we pass the current type, mark it to be the default. 403 */ 404 if (cur_dtype == tptr) { 405 deflt = i; 406 defltptr = &deflt; 407 } 408 if (cur_disk->label_type == L_TYPE_EFI) { 409 continue; 410 } 411 if (tptr->dtype_asciilabel) 412 fmt_print(" %d. %s\n", i++, 413 tptr->dtype_asciilabel); 414 } 415 other_choice = i; 416 fmt_print(" %d. other\n", i); 417 ioparam.io_bounds.lower = 0; 418 ioparam.io_bounds.upper = i; 419 /* 420 * Ask the user which type the disk is. 421 */ 422 index = input(FIO_INT, "Specify disk type (enter its number)", ':', 423 &ioparam, defltptr, DATA_INPUT); 424 /* 425 * Find the type they chose. 426 */ 427 if (index == auto_conf_choice) { 428 float scaled; 429 diskaddr_t nblks; 430 int nparts; 431 432 /* 433 * User chose "auto configure". 434 */ 435 (void) strcpy(x86_devname, cur_disk->disk_name); 436 switch (cur_disk->label_type) { 437 case L_TYPE_SOLARIS: 438 if ((tptr = auto_sense(cur_file, 1, &label)) == NULL) { 439 err_print("Auto configure failed\n"); 440 return (-1); 441 } 442 fmt_print("%s: configured with capacity of ", 443 cur_disk->disk_name); 444 nblks = (diskaddr_t)tptr->dtype_ncyl * 445 tptr->dtype_nhead * tptr->dtype_nsect; 446 scaled = bn2mb(nblks); 447 if (scaled > 1024.0) { 448 fmt_print("%1.2fGB\n", scaled/1024.0); 449 } else { 450 fmt_print("%1.2fMB\n", scaled); 451 } 452 fmt_print("<%s cyl %d alt %d hd %d sec %d>\n", 453 tptr->dtype_asciilabel, tptr->dtype_ncyl, 454 tptr->dtype_acyl, tptr->dtype_nhead, 455 tptr->dtype_nsect); 456 break; 457 case L_TYPE_EFI: 458 if ((tptr = auto_efi_sense(cur_file, &efi_info)) 459 == NULL) { 460 err_print("Auto configure failed\n"); 461 return (-1); 462 } 463 fmt_print("%s: configured with capacity of ", 464 cur_disk->disk_name); 465 scaled = bn2mb(efi_info.capacity); 466 if (scaled > 1024.0) { 467 fmt_print("%1.2fGB\n", scaled/1024.0); 468 } else { 469 fmt_print("%1.2fMB\n", scaled); 470 } 471 cur_blksz = efi_info.e_parts->efi_lbasize; 472 print_efi_string(efi_info.vendor, efi_info.product, 473 efi_info.revision, efi_info.capacity); 474 fmt_print("\n"); 475 for (nparts = 0; nparts < cur_parts->etoc->efi_nparts; 476 nparts++) { 477 if (cur_parts->etoc->efi_parts[nparts].p_tag == 478 V_RESERVED) { 479 (void) memcpy(volname, 480 cur_parts->etoc->efi_parts 481 [nparts].p_name, LEN_DKL_VVOL); 482 volname[LEN_DKL_VVOL] = '\0'; 483 volinit = 1; 484 break; 485 } 486 } 487 enter_critical(); 488 if (delete_disk_type(cur_disk->disk_type) != 0) { 489 fmt_print("Autoconfiguration failed.\n"); 490 return (-1); 491 } 492 cur_disk->disk_type = tptr; 493 cur_disk->disk_parts = tptr->dtype_plist; 494 init_globals(cur_disk); 495 exit_critical(); 496 if (volinit) { 497 for (nparts = 0; nparts < 498 cur_parts->etoc->efi_nparts; nparts++) { 499 if (cur_parts->etoc->efi_parts[nparts].p_tag == 500 V_RESERVED) { 501 (void) strcpy( 502 cur_parts->etoc->efi_parts[nparts].p_name, 503 volname); 504 (void) memcpy(cur_disk->v_volume, volname, 505 LEN_DKL_VVOL); 506 break; 507 } 508 } 509 } 510 return (0); 511 default: 512 /* Should never happen */ 513 return (-1); 514 } 515 } else if ((index == other_choice) && (cur_label == L_TYPE_SOLARIS)) { 516 /* 517 * User chose "other". 518 * Get the standard information on the new type. 519 * Put all information in a tmp structure, in 520 * case user aborts. 521 */ 522 bzero((char *)d, sizeof (struct disk_type)); 523 524 d->dtype_ncyl = get_ncyl(); 525 d->dtype_acyl = get_acyl(d->dtype_ncyl); 526 d->dtype_pcyl = get_pcyl(d->dtype_ncyl, d->dtype_acyl); 527 d->dtype_nhead = get_nhead(); 528 d->dtype_phead = get_phead(d->dtype_nhead, &d->dtype_options); 529 d->dtype_nsect = get_nsect(); 530 d->dtype_psect = get_psect(&d->dtype_options); 531 d->dtype_bpt = get_bpt(d->dtype_nsect, &d->dtype_options); 532 d->dtype_rpm = get_rpm(); 533 d->dtype_fmt_time = get_fmt_time(&d->dtype_options); 534 d->dtype_cyl_skew = get_cyl_skew(&d->dtype_options); 535 d->dtype_trk_skew = get_trk_skew(&d->dtype_options); 536 d->dtype_trks_zone = get_trks_zone(&d->dtype_options); 537 d->dtype_atrks = get_atrks(&d->dtype_options); 538 d->dtype_asect = get_asect(&d->dtype_options); 539 d->dtype_cache = get_cache(&d->dtype_options); 540 d->dtype_threshold = get_threshold(&d->dtype_options); 541 d->dtype_prefetch_min = get_min_prefetch(&d->dtype_options); 542 d->dtype_prefetch_max = get_max_prefetch(d->dtype_prefetch_min, 543 &d->dtype_options); 544 d->dtype_bps = get_bps(); 545 #if defined(sparc) 546 d->dtype_dr_type = 0; 547 #endif /* defined(sparc) */ 548 549 d->dtype_asciilabel = get_asciilabel(); 550 /* 551 * Add the new type to the list of possible types for 552 * this controller. We lock out interrupts so the lists 553 * can't get munged. We put off actually allocating the 554 * structure till here in case the user wanted to 555 * interrupt while still inputting information. 556 */ 557 enter_critical(); 558 tptr = (struct disk_type *)zalloc(sizeof (struct disk_type)); 559 if (type == NULL) 560 cur_ctype->ctype_dlist = tptr; 561 else { 562 while (type->dtype_next != NULL) 563 type = type->dtype_next; 564 type->dtype_next = tptr; 565 } 566 bcopy((char *)d, (char *)tptr, sizeof (disk_type)); 567 tptr->dtype_next = NULL; 568 /* 569 * the new disk type does not have any defined 570 * partition table . Hence copy the current partition 571 * table if possible else create a default 572 * paritition table. 573 */ 574 new_partitiontable(tptr, oldtype); 575 } else if ((index == other_choice) && (cur_label == L_TYPE_EFI)) { 576 uint64_t start_lba = cur_parts->etoc->efi_first_u_lba; 577 uint64_t reserved; 578 579 reserved = efi_reserved_sectors(cur_parts->etoc); 580 maxLBA = get_mlba(); 581 cur_parts->etoc->efi_last_lba = maxLBA; 582 cur_parts->etoc->efi_last_u_lba = maxLBA - start_lba; 583 for (i = 0; i < cur_parts->etoc->efi_nparts; i++) { 584 cur_parts->etoc->efi_parts[i].p_start = 0; 585 cur_parts->etoc->efi_parts[i].p_size = 0; 586 cur_parts->etoc->efi_parts[i].p_tag = V_UNASSIGNED; 587 } 588 cur_parts->etoc->efi_parts[8].p_start = 589 maxLBA - start_lba - reserved; 590 cur_parts->etoc->efi_parts[8].p_size = reserved; 591 cur_parts->etoc->efi_parts[8].p_tag = V_RESERVED; 592 if (write_label()) { 593 err_print("Write label failed\n"); 594 } else { 595 cur_disk->disk_flags &= ~DSK_LABEL_DIRTY; 596 } 597 return (0); 598 } else { 599 /* 600 * User picked an existing disk type. 601 */ 602 i = first_disk; 603 tptr = type; 604 while (i < index) { 605 if (tptr->dtype_asciilabel) { 606 i++; 607 } 608 tptr = tptr->dtype_next; 609 } 610 if ((tptr->dtype_asciilabel == NULL) && 611 (tptr->dtype_next != NULL)) { 612 while (tptr->dtype_asciilabel == NULL) { 613 tptr = tptr->dtype_next; 614 } 615 } 616 } 617 /* 618 * Check for mounted file systems in the format zone. 619 * One potential problem with this would be that check() 620 * always returns 'yes' when running out of a file. However, 621 * it is actually ok because we don't let the program get 622 * started if there are mounted file systems and we are 623 * running from a file. 624 */ 625 if ((tptr != oldtype) && 626 checkmount((diskaddr_t)-1, (diskaddr_t)-1)) { 627 err_print( 628 "Cannot set disk type while it has mounted " 629 "partitions.\n\n"); 630 return (-1); 631 } 632 /* 633 * check for partitions being used for swapping in format zone 634 */ 635 if ((tptr != oldtype) && 636 checkswap((diskaddr_t)-1, (diskaddr_t)-1)) { 637 err_print("Cannot set disk type while its partition are " 638 "currently being used for swapping.\n"); 639 return (-1); 640 } 641 642 /* 643 * Check for partitions being used in SVM, VxVM or LU devices 644 */ 645 646 if ((tptr != oldtype) && 647 checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1, 648 (diskaddr_t)-1, 0, 0)) { 649 err_print("Cannot set disk type while its " 650 "partitions are currently in use.\n"); 651 return (-1); 652 } 653 /* 654 * If the type selected is different from the previous type, 655 * mark the disk as not labelled and reload the current 656 * partition info. This is not essential but probably the 657 * right thing to do, since the size of the disk has probably 658 * changed. 659 */ 660 enter_critical(); 661 if (tptr != oldtype) { 662 cur_disk->disk_type = tptr; 663 cur_disk->disk_parts = NULL; 664 cur_disk->disk_flags &= ~DSK_LABEL; 665 } 666 /* 667 * Initialize the state of the current disk. 668 */ 669 init_globals(cur_disk); 670 (void) get_partition(); 671 exit_critical(); 672 673 /* 674 * If the label of the disk is marked dirty, 675 * see if they'd like to label the disk now. 676 */ 677 if (cur_disk->disk_flags & DSK_LABEL_DIRTY) { 678 if (check("Disk not labeled. Label it now") == 0) { 679 if (write_label()) { 680 err_print("Write label failed\n"); 681 } else { 682 cur_disk->disk_flags &= ~DSK_LABEL_DIRTY; 683 } 684 } 685 } 686 687 return (0); 688 } 689 690 /* 691 * This routine implements the 'partition' command. It simply runs 692 * the partition menu. 693 */ 694 int 695 c_partition(void) 696 { 697 698 /* 699 * There must be a current disk type and a current disk 700 */ 701 if (cur_dtype == NULL) { 702 err_print("Current Disk Type is not set.\n"); 703 return (-1); 704 } 705 /* 706 * Check for a valid fdisk table entry for Solaris 707 */ 708 if (!good_fdisk()) { 709 return (-1); 710 } 711 712 cur_menu++; 713 last_menu = cur_menu; 714 715 #ifdef not 716 /* 717 * If there is no current partition table, make one. This is 718 * so the commands within the menu never have to check for 719 * a non-existent table. 720 */ 721 if (cur_parts == NULL) 722 err_print("making partition.\n"); 723 make_partition(); 724 #endif /* not */ 725 726 /* 727 * Run the menu. 728 */ 729 run_menu(menu_partition, "PARTITION", "partition", 0); 730 cur_menu--; 731 return (0); 732 } 733 734 /* 735 * This routine implements the 'current' command. It describes the 736 * current disk. 737 */ 738 int 739 c_current(void) 740 { 741 742 /* 743 * If there is no current disk, say so. Note that this is 744 * not an error since it is a legitimate response to the inquiry. 745 */ 746 if (cur_disk == NULL) { 747 fmt_print("No Current Disk.\n"); 748 return (0); 749 } 750 /* 751 * Print out the info we have on the current disk. 752 */ 753 fmt_print("Current Disk = %s", cur_disk->disk_name); 754 if (chk_volname(cur_disk)) { 755 fmt_print(": "); 756 print_volname(cur_disk); 757 } 758 fmt_print("\n"); 759 if (cur_disk->devfs_name != NULL) { 760 if (cur_dtype == NULL) { 761 fmt_print("<type unknown>\n"); 762 } else if (cur_label == L_TYPE_SOLARIS) { 763 fmt_print("<%s cyl %d alt %d hd %d sec %d>\n", 764 cur_dtype->dtype_asciilabel, ncyl, 765 acyl, nhead, nsect); 766 } else if (cur_label == L_TYPE_EFI) { 767 print_efi_string(cur_dtype->vendor, 768 cur_dtype->product, cur_dtype->revision, 769 cur_dtype->capacity); 770 fmt_print("\n"); 771 } 772 fmt_print("%s\n", cur_disk->devfs_name); 773 } else { 774 fmt_print("%s%d: <", cur_ctlr->ctlr_dname, 775 cur_disk->disk_dkinfo.dki_unit); 776 if (cur_dtype == NULL) { 777 fmt_print("type unknown"); 778 } else if (cur_label == L_TYPE_SOLARIS) { 779 fmt_print("%s cyl %d alt %d hd %d sec %d", 780 cur_dtype->dtype_asciilabel, ncyl, 781 acyl, nhead, nsect); 782 } else if (cur_label == L_TYPE_EFI) { 783 print_efi_string(cur_dtype->vendor, 784 cur_dtype->product, cur_dtype->revision, 785 cur_dtype->capacity); 786 fmt_print("\n"); 787 } 788 fmt_print(">\n"); 789 } 790 fmt_print("\n"); 791 return (0); 792 } 793 /* 794 * This routine implements the 'format' command. It allows the user 795 * to format and verify any portion of the disk. 796 */ 797 int 798 c_format(void) 799 { 800 diskaddr_t start, end; 801 time_t clock; 802 int format_time, format_tracks, format_cyls; 803 int format_interval; 804 diskaddr_t deflt; 805 int status; 806 u_ioparam_t ioparam; 807 struct scsi_inquiry *inq; 808 char rawbuf[MAX_MODE_SENSE_SIZE]; 809 struct scsi_capacity_16 capacity; 810 struct vpd_hdr *vpdhdr; 811 uint8_t protect; 812 uint8_t pagecode; 813 uint8_t spt; 814 uint8_t p_type; 815 uint8_t prot_flag[NUM_PROT_TYPE] = {1, 0, 0, 0}; 816 int i; 817 char *prot_descriptor[NUM_PROT_TYPE] = { 818 "Protection Information is disabled.", 819 "Protection Information is enabled.", 820 "Protection Information is enabled.", 821 "Protection Information is enabled.", }; 822 823 /* 824 * There must be a current disk type and a current disk 825 */ 826 if (cur_dtype == NULL) { 827 err_print("Current Disk Type is not set.\n"); 828 return (-1); 829 } 830 831 /* 832 * There must be a format routine in cur_ops structure to have 833 * this routine work. 834 */ 835 if (cur_ops->op_format == NULL) { 836 err_print( 837 "Cannot format this drive. Please use your Manufacturer supplied formatting " 838 "utility.\n"); 839 return (-1); 840 } 841 842 /* 843 * There must be a current defect list. Except for 844 * unformatted SCSI disks. For them the defect list 845 * can only be retrieved after formatting the disk. 846 */ 847 if ((cur_ctype->ctype_flags & CF_SCSI) && !EMBEDDED_SCSI && 848 (cur_ctype->ctype_flags & CF_DEFECTS) && 849 ! (cur_flags & DISK_FORMATTED)) { 850 cur_list.flags |= LIST_RELOAD; 851 852 } else if (cur_list.list == NULL && !EMBEDDED_SCSI) { 853 err_print("Current Defect List must be initialized.\n"); 854 return (-1); 855 } 856 /* 857 * Ask for the bounds of the format. We always use the whole 858 * disk as the default, since that is the most likely case. 859 * Note, for disks which must be formatted accross the whole disk, 860 * don't bother the user. 861 */ 862 ioparam.io_bounds.lower = start = 0; 863 if (cur_label == L_TYPE_SOLARIS) { 864 if (cur_ctype->ctype_flags & CF_SCSI) { 865 ioparam.io_bounds.upper = end = datasects() - 1; 866 } else { 867 ioparam.io_bounds.upper = end = physsects() - 1; 868 } 869 } else { 870 ioparam.io_bounds.upper = end = cur_parts->etoc->efi_last_lba; 871 } 872 873 if (! (cur_ctlr->ctlr_flags & DKI_FMTVOL)) { 874 deflt = ioparam.io_bounds.lower; 875 start = input(FIO_BN, 876 "Enter starting block number", ':', 877 &ioparam, (int *)&deflt, DATA_INPUT); 878 ioparam.io_bounds.lower = start; 879 deflt = ioparam.io_bounds.upper; 880 end = input(FIO_BN, 881 "Enter ending block number", ':', 882 &ioparam, (int *)&deflt, DATA_INPUT); 883 } 884 /* 885 * Some disks can format tracks. Make sure the whole track is 886 * specified for them. 887 */ 888 if (cur_ctlr->ctlr_flags & DKI_FMTTRK) { 889 if (bn2s(start) != 0 || 890 bn2s(end) != sectors(bn2h(end)) - 1) { 891 err_print("Controller requires formatting of "); 892 err_print("entire tracks.\n"); 893 return (-1); 894 } 895 } 896 /* 897 * Check for mounted file systems in the format zone, and if we 898 * find any, make sure they are really serious. One potential 899 * problem with this would be that check() always returns 'yes' 900 * when running out of a file. However, it is actually ok 901 * because we don't let the program get started if there are 902 * mounted file systems and we are running from a file. 903 */ 904 if (checkmount(start, end)) { 905 err_print( 906 "Cannot format disk while it has mounted partitions.\n\n"); 907 return (-1); 908 } 909 /* 910 * check for partitions being used for swapping in format zone 911 */ 912 if (checkswap(start, end)) { 913 err_print("Cannot format disk while its partition are \ 914 currently being used for swapping.\n"); 915 return (-1); 916 } 917 /* 918 * Check for partitions being used in SVM, VxVM or LU devices 919 * in this format zone 920 */ 921 if (checkdevinuse(cur_disk->disk_name, start, end, 0, 0)) { 922 err_print("Cannot format disk while its partitions " 923 "are currently in use.\n"); 924 return (-1); 925 } 926 927 if (cur_disk->disk_lbasize != DEV_BSIZE) { 928 fmt_print("Current disk sector size is %d Byte, format\n" 929 "will change the sector size to 512 Byte. ", 930 cur_disk->disk_lbasize); 931 if (check("Continue")) { 932 return (-1); 933 } 934 } 935 936 /* 937 * set the default protection type 938 */ 939 prot_type = PROT_TYPE_0; 940 941 /* 942 * Check if the protect information of this disk is enabled 943 */ 944 if (uscsi_inquiry(cur_file, rawbuf, sizeof (rawbuf))) { 945 err_print("Inquiry failed\n"); 946 return (-1); 947 } 948 inq = (struct scsi_inquiry *)rawbuf; 949 protect = inq->inq_protect; 950 if (protect == 0) { 951 fmt_print("The protection information is not enabled\n"); 952 fmt_print( 953 "The disk will be formatted with protection type 0\n"); 954 } else { 955 (void) memset(rawbuf, 0, MAX_MODE_SENSE_SIZE); 956 if (uscsi_inquiry_page_86h(cur_file, rawbuf, sizeof (rawbuf))) { 957 err_print("Inquiry with page 86h failed\n"); 958 return (-1); 959 } 960 vpdhdr = (struct vpd_hdr *)rawbuf; 961 pagecode = vpdhdr->page_code; 962 if (pagecode != 0x86) { 963 err_print("Inquiry with page 86h failed\n"); 964 return (-1); 965 } 966 spt = (rawbuf[4] << 2) >> 5; 967 fmt_print("This disk can support protection types:\n"); 968 969 switch (spt) { 970 case 0: 971 prot_flag[1] = 1; 972 break; 973 case 1: 974 prot_flag[1] = 1; 975 prot_flag[2] = 1; 976 break; 977 case 2: 978 prot_flag[2] = 1; 979 break; 980 case 3: 981 prot_flag[1] = 1; 982 prot_flag[3] = 1; 983 break; 984 case 4: 985 prot_flag[3] = 1; 986 break; 987 case 5: 988 prot_flag[2] = 1; 989 prot_flag[3] = 1; 990 break; 991 case 7: 992 prot_flag[1] = 1; 993 prot_flag[2] = 1; 994 prot_flag[3] = 1; 995 break; 996 default: 997 err_print( 998 "Invalid supported protection types\n"); 999 return (-1); 1000 } 1001 for (i = 0; i < NUM_PROT_TYPE; i++) { 1002 if (prot_flag[i] == 1) { 1003 fmt_print("[%d] TYPE_%d : ", i, i); 1004 fmt_print("%s\n", prot_descriptor[i]); 1005 } 1006 } 1007 1008 /* 1009 * Get the current protection type 1010 */ 1011 if (uscsi_read_capacity_16(cur_file, &capacity)) { 1012 err_print("Read capacity_16 failed\n"); 1013 return (-1); 1014 } 1015 p_type = get_cur_protection_type(&capacity); 1016 fmt_print("\nThe disk is currently formatted with TYPE_%d.\n", 1017 p_type); 1018 1019 /* 1020 * Ask user what protection type to use 1021 */ 1022 ioparam.io_bounds.lower = PROT_TYPE_0; 1023 ioparam.io_bounds.upper = PROT_TYPE_3; 1024 prot_type = input(FIO_INT, "Specify the New Protection Type", 1025 ':', &ioparam, NULL, DATA_INPUT); 1026 /* 1027 * if get a unsupported protection type, then use the 1028 * current type: p_type. 1029 */ 1030 if (prot_flag[prot_type] == 0) { 1031 fmt_print("Unsupported protection type.\n"); 1032 prot_type = p_type; 1033 } 1034 fmt_print("The disk will be formatted to type %d\n", prot_type); 1035 } 1036 1037 if (SCSI && (format_time = scsi_format_time()) > 0) { 1038 fmt_print( 1039 "\nReady to format. Formatting cannot be interrupted\n" 1040 "and takes %d minutes (estimated). ", format_time); 1041 1042 } else if (cur_dtype->dtype_options & SUP_FMTTIME) { 1043 /* 1044 * Formatting time is (2 * time of 1 spin * number of 1045 * tracks) + (step rate * number of cylinders) rounded 1046 * up to the nearest minute. Note, a 10% fudge factor 1047 * is thrown in for insurance. 1048 */ 1049 if (cur_dtype->dtype_fmt_time == 0) 1050 cur_dtype->dtype_fmt_time = 2; 1051 1052 format_tracks = ((end-start) / cur_dtype->dtype_nsect) + 1; 1053 format_cyls = format_tracks / cur_dtype->dtype_nhead; 1054 format_tracks = format_tracks * cur_dtype->dtype_fmt_time; 1055 1056 /* 1057 * ms. 1058 */ 1059 format_time = ((60000 / cur_dtype->dtype_rpm) +1) * 1060 format_tracks + format_cyls * 7; 1061 /* 1062 * 20% done tick (sec) 1063 */ 1064 format_interval = format_time / 5000; 1065 /* 1066 * min. 1067 */ 1068 format_time = (format_time + 59999) / 60000; 1069 1070 /* 1071 * Check format time values and make adjustments 1072 * to prevent sleeping too long (forever?) or 1073 * too short. 1074 */ 1075 if (format_time <= 1) { 1076 /* 1077 * Format time is less than 1 min.. 1078 */ 1079 format_time = 1; 1080 } 1081 1082 if (format_interval < 11) { 1083 /* Format time is less than 1 minute. */ 1084 if (format_interval < 2) 1085 format_interval = 2; /* failsafe */ 1086 format_interval = 10; 1087 } else { 1088 /* Format time is greater than 1 minute. */ 1089 format_interval -= 10; 1090 } 1091 1092 fmt_print( 1093 "Ready to format. Formatting cannot be interrupted\n" 1094 "and takes %d minutes (estimated). ", format_time); 1095 } else { 1096 fmt_print( 1097 "Ready to format. Formatting cannot be interrupted.\n"); 1098 } 1099 if (check("Continue")) { 1100 return (-1); 1101 } 1102 1103 /* 1104 * Print the time so that the user will know when format started. 1105 * Lock out interrupts. This could be a problem, since it could 1106 * cause the user to sit for quite awhile with no control, but we 1107 * don't have any other good way of keeping their gun from going off. 1108 */ 1109 clock = time(NULL); 1110 fmt_print("Beginning format. The current time is %s\n", 1111 ctime(&clock)); 1112 enter_critical(); 1113 /* 1114 * Mark the defect list dirty so it will be rewritten when we are 1115 * done. It is possible to qualify this so it doesn't always 1116 * get rewritten, but it's not worth the trouble. 1117 * Note: no defect lists for embedded scsi drives. 1118 */ 1119 if (!EMBEDDED_SCSI) { 1120 cur_list.flags |= LIST_DIRTY; 1121 } 1122 /* 1123 * If we are formatting over any of the labels, mark the label 1124 * dirty so it will be rewritten. 1125 */ 1126 if (cur_disk->label_type == L_TYPE_SOLARIS) { 1127 if (start < totalsects() && end >= datasects()) { 1128 if (cur_disk->disk_flags & DSK_LABEL) 1129 cur_flags |= LABEL_DIRTY; 1130 } 1131 } else if (cur_disk->label_type == L_TYPE_EFI) { 1132 if (start < cur_parts->etoc->efi_first_u_lba) { 1133 if (cur_disk->disk_flags & DSK_LABEL) 1134 cur_flags |= LABEL_DIRTY; 1135 } 1136 } 1137 if (start == 0) { 1138 cur_flags |= LABEL_DIRTY; 1139 } 1140 /* 1141 * Do the format. bugid 1009138 removed the use of fork to 1142 * background the format and print a tick. 1143 */ 1144 1145 status = (*cur_ops->op_format)(start, end, &cur_list); 1146 if (status) { 1147 exit_critical(); 1148 err_print("failed\n"); 1149 return (-1); 1150 } 1151 fmt_print("done\n"); 1152 if (option_msg && diag_msg) { 1153 clock = time((time_t *)0); 1154 fmt_print("The current time is %s\n", ctime(&clock)); 1155 } 1156 cur_flags |= DISK_FORMATTED; 1157 /* 1158 * If the defect list or label is dirty, write them out again. 1159 * Note, for SCSI we have to wait til now to load defect list 1160 * since we can't access it until after formatting a virgin disk. 1161 */ 1162 /* enter_critical(); */ 1163 if (cur_list.flags & LIST_RELOAD) { 1164 assert(!EMBEDDED_SCSI); 1165 if (*cur_ops->op_ex_man == NULL || 1166 (*cur_ops->op_ex_man)(&cur_list)) { 1167 err_print("Warning: unable to reload defect list\n"); 1168 cur_list.flags &= ~LIST_DIRTY; 1169 return (-1); 1170 } 1171 cur_list.flags |= LIST_DIRTY; 1172 } 1173 1174 if (cur_list.flags & LIST_DIRTY) { 1175 assert(!EMBEDDED_SCSI); 1176 write_deflist(&cur_list); 1177 cur_list.flags = 0; 1178 } 1179 if (cur_flags & LABEL_DIRTY) { 1180 (void) write_label(); 1181 cur_flags &= ~LABEL_DIRTY; 1182 } 1183 /* 1184 * Come up for air, since the verify step does not need to 1185 * be atomic (it does it's own lockouts when necessary). 1186 */ 1187 exit_critical(); 1188 /* 1189 * If we are supposed to verify, we do the 'write' test over 1190 * the format zone. The rest of the analysis parameters are 1191 * left the way they were. 1192 */ 1193 if (scan_auto) { 1194 scan_entire = 0; 1195 scan_lower = start; 1196 scan_upper = end; 1197 fmt_print("\nVerifying media..."); 1198 status = do_scan(SCAN_PATTERN, F_SILENT); 1199 } 1200 /* 1201 * If the defect list or label is dirty, write them out again. 1202 */ 1203 if (cur_list.flags & LIST_DIRTY) { 1204 assert(!EMBEDDED_SCSI); 1205 cur_list.flags = 0; 1206 write_deflist(&cur_list); 1207 } 1208 if (cur_flags & LABEL_DIRTY) { 1209 cur_flags &= ~LABEL_DIRTY; 1210 (void) write_label(); 1211 } 1212 return (status); 1213 } 1214 1215 /* 1216 * This routine implements the 'repair' command. It allows the user 1217 * to reallocate sectors on the disk that have gone bad. 1218 */ 1219 int 1220 c_repair(void) 1221 { 1222 diskaddr_t bn; 1223 int status; 1224 u_ioparam_t ioparam; 1225 char *buf; 1226 int buf_is_good; 1227 int block_has_error; 1228 int i; 1229 1230 /* 1231 * There must be a current disk type (and therefore a current disk). 1232 */ 1233 if (cur_dtype == NULL) { 1234 err_print("Current Disk Type is not set.\n"); 1235 return (-1); 1236 } 1237 /* 1238 * The current disk must be formatted for repair to work. 1239 */ 1240 if (!(cur_flags & DISK_FORMATTED)) { 1241 err_print("Current Disk is unformatted.\n"); 1242 return (-1); 1243 } 1244 /* 1245 * Check for a valid fdisk table entry for Solaris 1246 */ 1247 if (!good_fdisk()) { 1248 return (-1); 1249 } 1250 /* 1251 * Repair is an optional command for controllers, so it may 1252 * not be supported. 1253 */ 1254 if (cur_ops->op_repair == NULL) { 1255 err_print("Controller does not support repairing.\n"); 1256 err_print("or disk supports automatic defect management.\n"); 1257 return (-1); 1258 } 1259 /* 1260 * There must be a defect list for non-embedded scsi devices, 1261 * since we will add to it. 1262 */ 1263 if (!EMBEDDED_SCSI && cur_list.list == NULL) { 1264 err_print("Current Defect List must be initialized.\n"); 1265 return (-1); 1266 } 1267 /* 1268 * Ask the user which sector has gone bad. 1269 */ 1270 ioparam.io_bounds.lower = 0; 1271 if (cur_disk->label_type == L_TYPE_SOLARIS) { 1272 ioparam.io_bounds.upper = physsects() - 1; 1273 } else { 1274 ioparam.io_bounds.upper = cur_parts->etoc->efi_last_lba; 1275 } 1276 bn = input(FIO_BN, 1277 "Enter absolute block number of defect", ':', 1278 &ioparam, NULL, DATA_INPUT); 1279 /* 1280 * Check to see if there is a mounted file system over the 1281 * specified sector. If there is, make sure the user is 1282 * really serious. 1283 */ 1284 if (checkmount(bn, bn)) { 1285 if (check("Repair is in a mounted partition, continue")) 1286 return (-1); 1287 } 1288 /* 1289 * check for partitions being used for swapping in format zone 1290 */ 1291 if (checkswap(bn, bn)) { 1292 if (check("Repair is in a partition which is currently " 1293 "being used for swapping.\ncontinue")) 1294 return (-1); 1295 } 1296 1297 if (checkdevinuse(cur_disk->disk_name, bn, bn, 0, 0)) { 1298 if (check("Repair is in a partition which is currently " 1299 "in use.\ncontinue")) 1300 return (-1); 1301 } 1302 1303 buf = zalloc((cur_disk->disk_lbasize == 0) ? 1304 SECSIZE : cur_disk->disk_lbasize); 1305 1306 /* 1307 * Try to read the sector before repairing it. If we can 1308 * get good data out of it, we can write that data back 1309 * after the repair. If the sector looks ok, ask the 1310 * user to confirm the repair, since it doesn't appear 1311 * necessary. Try reading the block several times to 1312 * see if we can read it consistently. 1313 * 1314 * First, let's see if the block appears to have problems... 1315 */ 1316 block_has_error = 1; 1317 for (i = 0; i < 5; i++) { 1318 status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1319 1, buf, (F_SILENT | F_ALLERRS), NULL); 1320 if (status) 1321 break; /* one of the tries failed */ 1322 } 1323 if (status == 0) { 1324 block_has_error = 0; 1325 if (check("\ 1326 This block doesn't appear to be bad. Repair it anyway")) { 1327 free(buf); 1328 return (0); 1329 } 1330 } 1331 /* 1332 * Last chance... 1333 */ 1334 if (check("Ready to repair defect, continue")) { 1335 free(buf); 1336 return (-1); 1337 } 1338 /* 1339 * We're committed to repairing it. Try to get any good 1340 * data out of the block if possible. Note that we do 1341 * not set the F_ALLERRS flag. 1342 */ 1343 buf_is_good = 0; 1344 for (i = 0; i < 5; i++) { 1345 status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1346 1, buf, F_SILENT, NULL); 1347 if (status == 0) { 1348 buf_is_good = 1; 1349 break; 1350 } 1351 } 1352 /* 1353 * Lock out interrupts so the disk can't get out of sync with 1354 * the defect list. 1355 */ 1356 enter_critical(); 1357 1358 fmt_print("Repairing "); 1359 if (block_has_error) { 1360 fmt_print("%s error on ", buf_is_good ? "soft" : "hard"); 1361 } 1362 fmt_print("block %llu (", bn); 1363 pr_dblock(fmt_print, bn); 1364 fmt_print(")..."); 1365 /* 1366 * Do the repair. 1367 */ 1368 status = (*cur_ops->op_repair)(bn, F_NORMAL); 1369 if (status) { 1370 fmt_print("failed.\n\n"); 1371 } else { 1372 /* 1373 * The repair worked. Write the old data to the new 1374 * block if we were able to read it, otherwise 1375 * zero out the new block. If it looks like the 1376 * new block is bad, let the user know that, too. 1377 * Should we attempt auto-repair in this case? 1378 */ 1379 fmt_print("ok.\n"); 1380 if (!buf_is_good) { 1381 bzero(buf, cur_disk->disk_lbasize); 1382 } 1383 status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file, bn, 1384 1, buf, (F_SILENT | F_ALLERRS), NULL); 1385 if (status == 0) { 1386 status = (*cur_ops->op_rdwr)(DIR_READ, cur_file, 1387 bn, 1, buf, (F_SILENT | F_ALLERRS), NULL); 1388 } 1389 if (status) { 1390 fmt_print("The new block %llu (", bn); 1391 pr_dblock(fmt_print, bn); 1392 fmt_print(") also appears defective.\n"); 1393 } 1394 fmt_print("\n"); 1395 /* 1396 * Add the bad sector to the defect list, write out 1397 * the defect list, and kill off the working list so 1398 * it will get synced up with the current defect list 1399 * next time we need it. 1400 * 1401 * For embedded scsi, we don't require a defect list. 1402 * However, if we have one, add the defect if the 1403 * list includes the grown list. If not, kill it 1404 * to force a resync if we need the list later. 1405 */ 1406 if (EMBEDDED_SCSI) { 1407 if (cur_list.list != NULL) { 1408 if (cur_list.flags & LIST_PGLIST) { 1409 add_ldef(bn, &cur_list); 1410 } else { 1411 kill_deflist(&cur_list); 1412 } 1413 } 1414 } else if (cur_ctype->ctype_flags & CF_WLIST) { 1415 kill_deflist(&cur_list); 1416 if (*cur_ops->op_ex_cur != NULL) { 1417 (void) (*cur_ops->op_ex_cur)(&cur_list); 1418 fmt_print("Current list updated\n"); 1419 } 1420 } else { 1421 add_ldef(bn, &cur_list); 1422 write_deflist(&cur_list); 1423 } 1424 kill_deflist(&work_list); 1425 } 1426 exit_critical(); 1427 free(buf); 1428 1429 /* 1430 * Return status. 1431 */ 1432 return (status); 1433 } 1434 1435 /* 1436 * This routine implements the 'show' command. It translates a disk 1437 * block given in any format into decimal, hexadecimal, and 1438 * cylinder/head/sector format. 1439 */ 1440 int 1441 c_show(void) 1442 { 1443 u_ioparam_t ioparam; 1444 diskaddr_t bn; 1445 1446 /* 1447 * There must be a current disk type, so we will know the geometry. 1448 */ 1449 if (cur_dtype == NULL) { 1450 err_print("Current Disk Type is not set.\n"); 1451 return (-1); 1452 } 1453 /* 1454 * Ask the user for a disk block. 1455 */ 1456 ioparam.io_bounds.lower = 0; 1457 if (cur_disk->label_type == L_TYPE_SOLARIS) { 1458 ioparam.io_bounds.upper = physsects() - 1; 1459 } else { 1460 ioparam.io_bounds.upper = cur_parts->etoc->efi_last_lba; 1461 } 1462 bn = input(FIO_BN, "Enter a disk block", ':', 1463 &ioparam, NULL, DATA_INPUT); 1464 /* 1465 * Echo it back. 1466 */ 1467 fmt_print("Disk block = %lld = 0x%llx = (", bn, bn); 1468 pr_dblock(fmt_print, bn); 1469 fmt_print(")\n\n"); 1470 return (0); 1471 } 1472 1473 /* 1474 * This routine implements the 'label' command. It writes the 1475 * primary and backup labels onto the current disk. 1476 */ 1477 int 1478 c_label(void) 1479 { 1480 int status; 1481 int deflt, *defltptr = NULL; 1482 1483 /* 1484 * There must be a current disk type (and therefore a current disk). 1485 */ 1486 if (cur_dtype == NULL) { 1487 err_print("Current Disk Type is not set.\n"); 1488 return (-1); 1489 } 1490 /* 1491 * The current disk must be formatted to label it. 1492 */ 1493 if (!(cur_flags & DISK_FORMATTED)) { 1494 err_print("Current Disk is unformatted.\n"); 1495 return (-1); 1496 } 1497 /* 1498 * Check for a valid fdisk table entry for Solaris 1499 */ 1500 if (!good_fdisk()) { 1501 return (-1); 1502 } 1503 /* 1504 * Check to see if there are any mounted file systems anywhere 1505 * on the current disk. If so, refuse to label the disk, but 1506 * only if the partitions would change for the mounted partitions. 1507 * 1508 */ 1509 if (checkmount((diskaddr_t)-1, (diskaddr_t)-1)) { 1510 /* Bleagh, too descriptive */ 1511 if (check_label_with_mount()) { 1512 err_print("Cannot label disk while it has " 1513 "mounted partitions.\n\n"); 1514 return (-1); 1515 } 1516 } 1517 1518 /* 1519 * check to see if there any partitions being used for swapping 1520 * on the current disk. If so, refuse to label the disk, but 1521 * only if the partitions would change for the mounted partitions. 1522 */ 1523 if (checkswap((diskaddr_t)-1, (diskaddr_t)-1)) { 1524 if (check_label_with_swap()) { 1525 err_print("Cannot label disk while its " 1526 "partitions are currently being used for " 1527 "swapping.\n"); 1528 return (-1); 1529 } 1530 } 1531 1532 /* 1533 * Check to see if any partitions used for svm, vxvm or live upgrade 1534 * are on the disk. If so, refuse to label the disk, but only 1535 * if we are trying to shrink a partition in use. 1536 */ 1537 if (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1, 1538 (diskaddr_t)-1, 0, 1)) { 1539 err_print("Cannot label disk when " 1540 "partitions are in use as described.\n"); 1541 return (-1); 1542 } 1543 1544 /* 1545 * If there is not a current partition map, warn the user we 1546 * are going to use the default. The default is the first 1547 * partition map we encountered in the data file. If there is 1548 * no default we give up. 1549 */ 1550 if (cur_parts == NULL) { 1551 fmt_print("Current Partition Table is not set, " 1552 "using default.\n"); 1553 cur_disk->disk_parts = cur_parts = cur_dtype->dtype_plist; 1554 if (cur_parts == NULL) { 1555 err_print("No default available, cannot label.\n"); 1556 return (-1); 1557 } 1558 } 1559 /* 1560 * If expert (-e) mode, then ask user if they wish 1561 * to change the current solaris label into an EFI one 1562 */ 1563 if (expert_mode) { 1564 #if defined(_SUNOS_VTOC_8) 1565 int i; 1566 #endif 1567 int choice; 1568 u_ioparam_t ioparam; 1569 struct extvtoc vtoc; 1570 struct dk_label label; 1571 struct dk_gpt *vtoc64; 1572 struct efi_info efinfo; 1573 struct disk_type *dptr; 1574 1575 /* Ask user what label to use */ 1576 fmt_print("[0] SMI Label\n"); 1577 fmt_print("[1] EFI Label\n"); 1578 ioparam.io_bounds.lower = 0; 1579 ioparam.io_bounds.upper = 1; 1580 if ((cur_label == L_TYPE_SOLARIS) && 1581 (cur_disk->fdisk_part.systid != EFI_PMBR)) 1582 deflt = L_TYPE_SOLARIS; 1583 else 1584 deflt = L_TYPE_EFI; 1585 defltptr = &deflt; 1586 choice = input(FIO_INT, "Specify Label type", ':', 1587 &ioparam, defltptr, DATA_INPUT); 1588 if ((choice == L_TYPE_SOLARIS) && 1589 (cur_label == L_TYPE_SOLARIS) && 1590 (cur_disk->fdisk_part.systid != EFI_PMBR)) { 1591 goto expert_end; 1592 } else if ((choice == L_TYPE_EFI) && 1593 (cur_label == L_TYPE_EFI)) { 1594 goto expert_end; 1595 } 1596 switch (choice) { 1597 case L_TYPE_SOLARIS: 1598 /* 1599 * EFI label to SMI label 1600 */ 1601 if (cur_dtype->capacity > INFINITY) { 1602 fmt_print("Warning: SMI labels only support up to " 1603 "2 TB.\n"); 1604 } 1605 1606 if (cur_disk->fdisk_part.systid == EFI_PMBR) { 1607 fmt_print("Warning: This disk has an EFI label. " 1608 "Changing to SMI label will erase all\n" 1609 "current partitions.\n"); 1610 if (check("Continue")) 1611 return (-1); 1612 #if defined(_FIRMWARE_NEEDS_FDISK) 1613 fmt_print("You must use fdisk to delete the current " 1614 "EFI partition and create a new\n" 1615 "Solaris partition before you can convert the " 1616 "label.\n"); 1617 return (-1); 1618 #endif 1619 } 1620 1621 #if defined(_FIRMWARE_NEEDS_FDISK) 1622 if ((cur_disk->fdisk_part.systid != SUNIXOS && 1623 cur_disk->fdisk_part.systid != SUNIXOS2) || 1624 cur_disk->fdisk_part.numsect == 0) { 1625 fmt_print("You must use fdisk to create a Solaris " 1626 "partition before you can convert the label.\n"); 1627 return (-1); 1628 } 1629 #endif 1630 1631 (void) memset(&label, 0, sizeof (struct dk_label)); 1632 1633 (void) strcpy(x86_devname, cur_disk->disk_name); 1634 if (cur_ctype->ctype_ctype == DKC_DIRECT || 1635 cur_ctype->ctype_ctype == DKC_BLKDEV) 1636 dptr = auto_direct_get_geom_label(cur_file, &label); 1637 else 1638 dptr = auto_sense(cur_file, 1, &label); 1639 if (dptr == NULL) { 1640 fmt_print("Autoconfiguration failed.\n"); 1641 return (-1); 1642 } 1643 1644 pcyl = label.dkl_pcyl; 1645 ncyl = label.dkl_ncyl; 1646 acyl = label.dkl_acyl; 1647 nhead = label.dkl_nhead; 1648 nsect = label.dkl_nsect; 1649 1650 if (delete_disk_type(cur_disk->disk_type) == 0) { 1651 cur_label = L_TYPE_SOLARIS; 1652 cur_disk->label_type = L_TYPE_SOLARIS; 1653 cur_disk->disk_type = dptr; 1654 cur_disk->disk_parts = dptr->dtype_plist; 1655 cur_dtype = dptr; 1656 cur_parts = dptr->dtype_plist; 1657 1658 status = write_label(); 1659 if (status != 0) 1660 err_print("Label failed.\n"); 1661 else 1662 cur_disk->disk_flags &= ~DSK_LABEL_DIRTY; 1663 1664 return (status); 1665 } else { 1666 err_print("Label failed.\n"); 1667 return (-1); 1668 } 1669 1670 1671 case L_TYPE_EFI: 1672 /* 1673 * SMI label to EFI label 1674 */ 1675 1676 if ((cur_disk->fdisk_part.systid == SUNIXOS) || 1677 (cur_disk->fdisk_part.systid == SUNIXOS2)) { 1678 fmt_print("Warning: This disk has an SMI label. " 1679 "Changing to EFI label will erase all\ncurrent " 1680 "partitions.\n"); 1681 if (check("Continue")) { 1682 return (-1); 1683 } 1684 } 1685 1686 if (get_disk_info(cur_file, &efinfo, cur_disk) != 0) { 1687 return (-1); 1688 } 1689 (void) memset((char *)&label, 0, sizeof (struct dk_label)); 1690 label.dkl_pcyl = pcyl; 1691 label.dkl_ncyl = ncyl; 1692 label.dkl_acyl = acyl; 1693 #if defined(_SUNOS_VTOC_16) 1694 label.dkl_bcyl = bcyl; 1695 #endif /* defined(_SUNOC_VTOC_16) */ 1696 label.dkl_nhead = nhead; 1697 label.dkl_nsect = nsect; 1698 #if defined(_SUNOS_VTOC_8) 1699 for (i = 0; i < NDKMAP; i++) { 1700 label.dkl_map[i] = cur_parts->pinfo_map[i]; 1701 } 1702 #endif /* defined(_SUNOS_VTOC_8) */ 1703 label.dkl_magic = DKL_MAGIC; 1704 label.dkl_vtoc = cur_parts->vtoc; 1705 if (label_to_vtoc(&vtoc, &label) == -1) { 1706 return (-1); 1707 } 1708 if (SMI_vtoc_to_EFI(cur_file, &vtoc64) == -1) { 1709 return (-1); 1710 } 1711 if (efi_write(cur_file, vtoc64) != 0) { 1712 efi_err_check(vtoc64); 1713 err_print("Warning: error writing EFI.\n"); 1714 return (-1); 1715 } else { 1716 cur_disk->disk_flags &= ~DSK_LABEL_DIRTY; 1717 } 1718 /* 1719 * copy over the EFI vtoc onto the SMI vtoc and return 1720 * okay. 1721 */ 1722 dptr = auto_efi_sense(cur_file, &efinfo); 1723 if (dptr == NULL) { 1724 fmt_print("Autoconfiguration failed.\n"); 1725 return (-1); 1726 } 1727 1728 cur_label = L_TYPE_EFI; 1729 cur_disk->label_type = L_TYPE_EFI; 1730 cur_disk->disk_type = dptr; 1731 cur_disk->disk_parts = dptr->dtype_plist; 1732 cur_dtype = dptr; 1733 cur_parts = dptr->dtype_plist; 1734 cur_parts->etoc = vtoc64; 1735 1736 ncyl = pcyl = nsect = psect = acyl = phead = 0; 1737 1738 /* 1739 * Get the Solais Fdisk Partition information. 1740 */ 1741 (void) copy_solaris_part(&cur_disk->fdisk_part); 1742 1743 return (0); 1744 } 1745 } 1746 1747 expert_end: 1748 /* 1749 * Make sure the user is serious. 1750 */ 1751 if (check("Ready to label disk, continue")) { 1752 return (-1); 1753 } 1754 /* 1755 * Write the labels out (this will also notify unix) and 1756 * return status. 1757 */ 1758 fmt_print("\n"); 1759 status = write_label(); 1760 if (status != 0) 1761 err_print("Label failed.\n"); 1762 return (status); 1763 } 1764 1765 /* 1766 * This routine implements the 'analyze' command. It simply runs 1767 * the analyze menu. 1768 */ 1769 int 1770 c_analyze(void) 1771 { 1772 1773 /* 1774 * There must be a current disk type (and therefor a current disk). 1775 */ 1776 if (cur_dtype == NULL) { 1777 err_print("Current Disk Type is not set.\n"); 1778 return (-1); 1779 } 1780 cur_menu++; 1781 last_menu = cur_menu; 1782 1783 /* 1784 * Run the menu. 1785 */ 1786 run_menu(menu_analyze, "ANALYZE", "analyze", 0); 1787 cur_menu--; 1788 return (0); 1789 } 1790 1791 /* 1792 * This routine implements the 'defect' command. It simply runs 1793 * the defect menu. 1794 */ 1795 int 1796 c_defect(void) 1797 { 1798 int i; 1799 1800 /* 1801 * There must be a current disk type (and therefor a current disk). 1802 */ 1803 if (cur_dtype == NULL) { 1804 err_print("Current Disk Type is not set.\n"); 1805 return (-1); 1806 } 1807 1808 /* 1809 * Check for the defect management and list management ops and 1810 * display appropriate message. 1811 */ 1812 if ((cur_ops->op_ex_man == NULL) && (cur_ops->op_ex_cur == NULL) && 1813 (cur_ops->op_create == NULL) && (cur_ops->op_wr_cur == NULL)) { 1814 err_print("Controller does not support defect management\n"); 1815 err_print("or disk supports automatic defect management.\n"); 1816 return (-1); 1817 } 1818 cur_menu++; 1819 last_menu = cur_menu; 1820 1821 /* 1822 * Lock out interrupt while we manipulate the defect lists. 1823 */ 1824 enter_critical(); 1825 /* 1826 * If the working list is null but there is a current list, 1827 * update the working list to be a copy of the current list. 1828 */ 1829 if ((work_list.list == NULL) && (cur_list.list != NULL)) { 1830 work_list.header = cur_list.header; 1831 work_list.list = (struct defect_entry *)zalloc( 1832 deflist_size(cur_blksz, work_list.header.count) * 1833 cur_blksz); 1834 for (i = 0; i < work_list.header.count; i++) 1835 *(work_list.list + i) = *(cur_list.list + i); 1836 work_list.flags = cur_list.flags & LIST_PGLIST; 1837 } 1838 exit_critical(); 1839 /* 1840 * Run the menu. 1841 */ 1842 run_menu(menu_defect, "DEFECT", "defect", 0); 1843 cur_menu--; 1844 1845 /* 1846 * If the user has modified the working list but not committed 1847 * it, warn them that they are probably making a mistake. 1848 */ 1849 if (work_list.flags & LIST_DIRTY) { 1850 if (!EMBEDDED_SCSI) { 1851 err_print("Warning: working defect list modified; " 1852 "but not committed.\n"); 1853 if (!check("Do you wish to commit changes to current " 1854 "defect list")) 1855 (void) do_commit(); 1856 } 1857 } 1858 return (0); 1859 } 1860 1861 /* 1862 * This routine implements the 'backup' command. It allows the user 1863 * to search for backup labels on the current disk. This is useful 1864 * if the primary label was lost and the user wishes to recover the 1865 * partition information for the disk. The disk is relabeled and 1866 * the current defect list is written out if a backup label is found. 1867 */ 1868 int 1869 c_backup(void) 1870 { 1871 struct dk_label label; 1872 struct disk_type *dtype; 1873 struct partition_info *parts, *plist; 1874 diskaddr_t bn; 1875 int sec, head, i; 1876 char *buf; 1877 1878 /* 1879 * There must be a current disk type (and therefore a current disk). 1880 */ 1881 if (cur_dtype == NULL) { 1882 err_print("Current Disk Type is not set.\n"); 1883 return (-1); 1884 } 1885 /* 1886 * The disk must be formatted to read backup labels. 1887 */ 1888 if (!(cur_flags & DISK_FORMATTED)) { 1889 err_print("Current Disk is unformatted.\n"); 1890 return (-1); 1891 } 1892 /* 1893 * Check for a valid fdisk table entry for Solaris 1894 */ 1895 if (!good_fdisk()) { 1896 return (-1); 1897 } 1898 /* 1899 * If we found a primary label on this disk, make sure 1900 * the user is serious. 1901 */ 1902 if (cur_disk->label_type == L_TYPE_EFI) { 1903 if (((cur_disk->disk_parts->etoc->efi_flags & 1904 EFI_GPT_PRIMARY_CORRUPT) == 0) && 1905 check("Disk has a primary label, still continue")) 1906 return (-1); 1907 fmt_print("Restoring primary label.\n"); 1908 if (write_label()) { 1909 err_print("Failed\n"); 1910 return (-1); 1911 } 1912 return (0); 1913 } else if (((cur_disk->disk_flags & (DSK_LABEL | DSK_LABEL_DIRTY)) == 1914 DSK_LABEL) && 1915 (check("Disk has a primary label, still continue"))) { 1916 return (-1); 1917 } 1918 1919 buf = zalloc(cur_blksz); 1920 fmt_print("Searching for backup labels..."); 1921 (void) fflush(stdout); 1922 1923 /* 1924 * Some disks have the backup labels in a strange place. 1925 */ 1926 if (cur_ctype->ctype_flags & CF_BLABEL) 1927 head = 2; 1928 else 1929 head = nhead - 1; 1930 /* 1931 * Loop through each copy of the backup label. 1932 */ 1933 for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect)); 1934 sec += 2) { 1935 bn = chs2bn(ncyl + acyl - 1, head, sec) + solaris_offset; 1936 /* 1937 * Attempt to read it. 1938 */ 1939 if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 1940 1, buf, F_NORMAL, NULL)) { 1941 continue; 1942 } 1943 1944 (void) memcpy((char *)&label, buf, sizeof (struct dk_label)); 1945 1946 /* 1947 * Verify that it is a reasonable label. 1948 */ 1949 if (!checklabel(&label)) 1950 continue; 1951 if (trim_id(label.dkl_asciilabel)) 1952 continue; 1953 /* 1954 * Lock out interrupts while we manipulate lists. 1955 */ 1956 enter_critical(); 1957 fmt_print("found.\n"); 1958 /* 1959 * Find out which disk type the backup label claims. 1960 */ 1961 for (dtype = cur_ctype->ctype_dlist; dtype != NULL; 1962 dtype = dtype->dtype_next) 1963 if (dtype_match(&label, dtype)) 1964 break; 1965 /* 1966 * If it disagrees with our current type, something 1967 * real bad is happening. 1968 */ 1969 if (dtype != cur_dtype) { 1970 if (dtype == NULL) { 1971 fmt_print("\ 1972 Unknown disk type in backup label\n"); 1973 exit_critical(); 1974 free(buf); 1975 return (-1); 1976 } 1977 fmt_print("Backup label claims different type:\n"); 1978 fmt_print(" <%s cyl %d alt %d hd %d sec %d>\n", 1979 label.dkl_asciilabel, label.dkl_ncyl, 1980 label.dkl_acyl, label.dkl_nhead, 1981 label.dkl_nsect); 1982 if (check("Continue")) { 1983 exit_critical(); 1984 free(buf); 1985 return (-1); 1986 } 1987 cur_dtype = dtype; 1988 } 1989 /* 1990 * Try to match the partition map with a known map. 1991 */ 1992 for (parts = dtype->dtype_plist; parts != NULL; 1993 parts = parts->pinfo_next) 1994 if (parts_match(&label, parts)) 1995 break; 1996 /* 1997 * If we couldn't match it, allocate space for a new one, 1998 * fill in the info, and add it to the list. The name 1999 * for the new map is derived from the disk name. 2000 */ 2001 if (parts == NULL) { 2002 parts = (struct partition_info *) 2003 zalloc(sizeof (struct partition_info)); 2004 plist = dtype->dtype_plist; 2005 if (plist == NULL) 2006 dtype->dtype_plist = parts; 2007 else { 2008 while (plist->pinfo_next != NULL) 2009 plist = plist->pinfo_next; 2010 plist->pinfo_next = parts; 2011 } 2012 parts->pinfo_name = alloc_string("original"); 2013 for (i = 0; i < NDKMAP; i++) { 2014 2015 #if defined(_SUNOS_VTOC_8) 2016 parts->pinfo_map[i] = label.dkl_map[i]; 2017 2018 #elif defined(_SUNOS_VTOC_16) 2019 parts->pinfo_map[i].dkl_cylno = 2020 label.dkl_vtoc.v_part[i].p_start / spc(); 2021 parts->pinfo_map[i].dkl_nblk = 2022 label.dkl_vtoc.v_part[i].p_size; 2023 #else 2024 #error No VTOC layout defined. 2025 #endif /* defined(_SUNOS_VTOC_8) */ 2026 } 2027 parts->vtoc = label.dkl_vtoc; 2028 } 2029 /* 2030 * We now have a partition map. Make it the current map. 2031 */ 2032 cur_disk->disk_parts = cur_parts = parts; 2033 exit_critical(); 2034 /* 2035 * Rewrite the labels and defect lists, as appropriate. 2036 */ 2037 if (EMBEDDED_SCSI) { 2038 fmt_print("Restoring primary label.\n"); 2039 if (write_label()) { 2040 free(buf); 2041 return (-1); 2042 } 2043 } else { 2044 fmt_print("Restoring primary label and defect list.\n"); 2045 if (write_label()) { 2046 free(buf); 2047 return (-1); 2048 } 2049 if (cur_list.list != NULL) 2050 write_deflist(&cur_list); 2051 } 2052 fmt_print("\n"); 2053 free(buf); 2054 return (0); 2055 } 2056 /* 2057 * If we didn't find any backup labels, say so. 2058 */ 2059 fmt_print("not found.\n\n"); 2060 free(buf); 2061 return (0); 2062 } 2063 2064 /* 2065 * This routine is called by c_verify() for an EFI labeled disk 2066 */ 2067 static int 2068 c_verify_efi(void) 2069 { 2070 struct efi_info efi_info; 2071 struct partition_info tmp_pinfo; 2072 int status; 2073 2074 status = read_efi_label(cur_file, &efi_info, cur_disk); 2075 if (status != 0) { 2076 err_print("Warning: Could not read label.\n"); 2077 return (-1); 2078 } 2079 if (cur_parts->etoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) { 2080 err_print("Reading the primary EFI GPT label "); 2081 err_print("failed. Using backup label.\n"); 2082 err_print("Use the 'backup' command to restore "); 2083 err_print("the primary label.\n"); 2084 } 2085 tmp_pinfo.etoc = efi_info.e_parts; 2086 fmt_print("\n"); 2087 fmt_print("Volume name = <%8s>\n", 2088 cur_parts->etoc->efi_parts[8].p_name); 2089 fmt_print("ascii name = "); 2090 print_efi_string(efi_info.vendor, efi_info.product, 2091 efi_info.revision, efi_info.capacity); 2092 fmt_print("\n"); 2093 2094 fmt_print("bytes/sector = %d\n", cur_blksz); 2095 fmt_print("sectors = %llu\n", cur_parts->etoc->efi_last_lba + 1); 2096 fmt_print("accessible sectors = %llu\n", 2097 cur_parts->etoc->efi_last_u_lba - 2098 cur_parts->etoc->efi_first_u_lba - 2099 efi_reserved_sectors(cur_parts->etoc) + 1); 2100 fmt_print("first usable sector = %llu\n", 2101 cur_parts->etoc->efi_first_u_lba); 2102 fmt_print("last usable sector = %llu\n", 2103 cur_parts->etoc->efi_last_u_lba); 2104 2105 print_map(&tmp_pinfo); 2106 2107 free(efi_info.vendor); 2108 free(efi_info.product); 2109 free(efi_info.revision); 2110 return (0); 2111 } 2112 2113 /* 2114 * This routine implements the 'verify' command. It allows the user 2115 * to read the labels on the current disk. 2116 */ 2117 int 2118 c_verify(void) 2119 { 2120 struct dk_label p_label, b_label, *label; 2121 struct partition_info tmp_pinfo; 2122 diskaddr_t bn; 2123 int sec, head, i, status; 2124 int p_label_bad = 0; 2125 int b_label_bad = 0; 2126 int p_label_found = 0; 2127 int b_label_found = 0; 2128 char id_str[128]; 2129 char *buf; 2130 2131 /* 2132 * There must be a current disk type (and therefore a current disk). 2133 */ 2134 if (cur_dtype == NULL) { 2135 err_print("Current Disk Type is not set.\n"); 2136 return (-1); 2137 } 2138 /* 2139 * The disk must be formatted to read labels. 2140 */ 2141 if (!(cur_flags & DISK_FORMATTED)) { 2142 err_print("Current Disk is unformatted.\n"); 2143 return (-1); 2144 } 2145 /* 2146 * Check for a valid fdisk table entry for Solaris 2147 */ 2148 if (!good_fdisk()) { 2149 return (-1); 2150 } 2151 /* 2152 * Branch off here if the disk is EFI labelled. 2153 */ 2154 if (cur_label == L_TYPE_EFI) { 2155 return (c_verify_efi()); 2156 } 2157 /* 2158 * Attempt to read the primary label. 2159 */ 2160 status = read_label(cur_file, &p_label); 2161 if (status == -1) { 2162 err_print("Warning: Could not read primary label.\n"); 2163 p_label_bad = 1; 2164 } else { 2165 /* 2166 * Verify that it is a reasonable label. 2167 */ 2168 /* 2169 * Save complete ascii string for printing later. 2170 */ 2171 (void) strncpy(id_str, p_label.dkl_asciilabel, 128); 2172 2173 if ((!checklabel((struct dk_label *)&p_label)) || 2174 (trim_id(p_label.dkl_asciilabel))) { 2175 err_print("\ 2176 Warning: Primary label appears to be corrupt.\n"); 2177 p_label_bad = 1; 2178 } else { 2179 p_label_found = 1; 2180 /* 2181 * Make sure it matches current label 2182 */ 2183 if ((!dtype_match(&p_label, cur_dtype)) || 2184 (!parts_match(&p_label, cur_parts))) { 2185 err_print("\ 2186 Warning: Primary label on disk appears to be different from\ncurrent label.\n"); 2187 p_label_bad = 1; 2188 } 2189 } 2190 } 2191 2192 /* 2193 * Read backup labels. 2194 * Some disks have the backup labels in a strange place. 2195 */ 2196 if (cur_ctype->ctype_flags & CF_BLABEL) 2197 head = 2; 2198 else 2199 head = nhead - 1; 2200 2201 buf = zalloc(cur_blksz); 2202 /* 2203 * Loop through each copy of the backup label. 2204 */ 2205 for (sec = 1; ((sec < BAD_LISTCNT * 2 + 1) && (sec < nsect)); 2206 sec += 2) { 2207 bn = chs2bn(ncyl + acyl - 1, head, sec) + solaris_offset; 2208 /* 2209 * Attempt to read it. 2210 */ 2211 if ((*cur_ops->op_rdwr)(DIR_READ, cur_file, bn, 2212 1, buf, F_NORMAL, NULL)) 2213 continue; 2214 2215 (void) memcpy((char *)&b_label, buf, 2216 sizeof (struct dk_label)); 2217 2218 /* 2219 * Verify that it is a reasonable label. 2220 */ 2221 if (!checklabel(&b_label)) 2222 continue; 2223 2224 /* 2225 * Save complete label only if no primary label exists 2226 */ 2227 if (!p_label_found) 2228 (void) strncpy(id_str, b_label.dkl_asciilabel, 128); 2229 2230 if (trim_id(b_label.dkl_asciilabel)) 2231 continue; 2232 b_label_found = 1; 2233 /* 2234 * Compare against primary label 2235 */ 2236 if (p_label_found) { 2237 if ((strcmp(b_label.dkl_asciilabel, 2238 p_label.dkl_asciilabel) != 0) || 2239 (b_label.dkl_ncyl != p_label.dkl_ncyl) || 2240 (b_label.dkl_acyl != p_label.dkl_acyl) || 2241 (b_label.dkl_nhead != p_label.dkl_nhead) || 2242 (b_label.dkl_nsect != p_label.dkl_nsect)) { 2243 b_label_bad = 1; 2244 } else { 2245 for (i = 0; i < NDKMAP; i++) { 2246 #if defined(_SUNOS_VTOC_8) 2247 if ((b_label.dkl_map[i].dkl_cylno != 2248 p_label.dkl_map[i].dkl_cylno) || 2249 (b_label.dkl_map[i].dkl_nblk != 2250 p_label.dkl_map[i].dkl_nblk)) { 2251 b_label_bad = 1; 2252 break; 2253 } 2254 2255 #elif defined(_SUNOS_VTOC_16) 2256 if ((b_label.dkl_vtoc.v_part[i].p_tag != 2257 p_label.dkl_vtoc.v_part[i].p_tag) || 2258 (b_label.dkl_vtoc.v_part[i].p_flag 2259 != p_label.dkl_vtoc.v_part[i]. 2260 p_flag) || 2261 (b_label.dkl_vtoc.v_part[i].p_start 2262 != p_label.dkl_vtoc.v_part[i]. 2263 p_start) || 2264 (b_label.dkl_vtoc.v_part[i].p_size 2265 != p_label.dkl_vtoc.v_part[i]. 2266 p_size)) { 2267 b_label_bad = 1; 2268 break; 2269 } 2270 #else 2271 #error No VTOC layout defined. 2272 #endif /* defined(_SUNOS_VTOC_8) */ 2273 } 2274 } 2275 } 2276 if (b_label_bad) 2277 err_print( 2278 "Warning: Primary and backup labels do not match.\n"); 2279 break; 2280 } 2281 /* 2282 * If we didn't find any backup labels, say so. 2283 */ 2284 if (!b_label_found) 2285 err_print("Warning: Could not read backup labels.\n"); 2286 2287 if ((!b_label_found) || (p_label_bad) || (b_label_bad)) 2288 err_print("\n\ 2289 Warning: Check the current partitioning and 'label' the disk or use the\n\ 2290 \t 'backup' command.\n"); 2291 2292 /* 2293 * Print label information. 2294 */ 2295 if (p_label_found) { 2296 fmt_print("\nPrimary label contents:\n"); 2297 label = &p_label; 2298 } else if (b_label_found) { 2299 fmt_print("\nBackup label contents:\n"); 2300 label = &b_label; 2301 } else { 2302 free(buf); 2303 return (0); 2304 } 2305 2306 /* 2307 * Must put info into partition_info struct for 2308 * for print routine. 2309 */ 2310 bzero(&tmp_pinfo, sizeof (struct partition_info)); 2311 for (i = 0; i < NDKMAP; i++) { 2312 2313 #if defined(_SUNOS_VTOC_8) 2314 tmp_pinfo.pinfo_map[i] = label->dkl_map[i]; 2315 2316 #elif defined(_SUNOS_VTOC_16) 2317 tmp_pinfo.pinfo_map[i].dkl_cylno = 2318 label->dkl_vtoc.v_part[i].p_start / spc(); 2319 tmp_pinfo.pinfo_map[i].dkl_nblk = 2320 label->dkl_vtoc.v_part[i].p_size; 2321 #else 2322 #error No VTOC layout defined. 2323 #endif /* defined(_SUNOS_VTOC_8) */ 2324 } 2325 tmp_pinfo.vtoc = label->dkl_vtoc; 2326 2327 fmt_print("\n"); 2328 fmt_print("Volume name = <%8s>\n", label->dkl_vtoc.v_volume); 2329 fmt_print("ascii name = <%s>\n", id_str); 2330 fmt_print("pcyl = %4d\n", label->dkl_pcyl); 2331 fmt_print("ncyl = %4d\n", label->dkl_ncyl); 2332 fmt_print("acyl = %4d\n", label->dkl_acyl); 2333 2334 #if defined(_SUNOS_VTOC_16) 2335 fmt_print("bcyl = %4d\n", label->dkl_bcyl); 2336 #endif /* defined(_SUNOS_VTOC_16) */ 2337 2338 fmt_print("nhead = %4d\n", label->dkl_nhead); 2339 fmt_print("nsect = %4d\n", label->dkl_nsect); 2340 2341 print_map(&tmp_pinfo); 2342 free(buf); 2343 return (0); 2344 } 2345 2346 2347 /* 2348 * This command implements the inquiry command, for embedded SCSI 2349 * disks only, which issues a SCSI inquiry command, and 2350 * displays the resulting vendor, product id and revision level. 2351 */ 2352 int 2353 c_inquiry(void) 2354 { 2355 char inqbuf[255]; 2356 struct scsi_inquiry *inq; 2357 2358 assert(SCSI); 2359 2360 inq = (struct scsi_inquiry *)inqbuf; 2361 2362 if (uscsi_inquiry(cur_file, inqbuf, sizeof (inqbuf))) { 2363 err_print("Failed\n"); 2364 return (-1); 2365 } else { 2366 fmt_print("Vendor: "); 2367 print_buf(inq->inq_vid, sizeof (inq->inq_vid)); 2368 fmt_print("\nProduct: "); 2369 print_buf(inq->inq_pid, sizeof (inq->inq_pid)); 2370 fmt_print("\nRevision: "); 2371 print_buf(inq->inq_revision, sizeof (inq->inq_revision)); 2372 fmt_print("\n"); 2373 } 2374 2375 return (0); 2376 } 2377 2378 2379 /* 2380 * This routine allows the user to set the 8-character 2381 * volume name in the vtoc. It then writes both the 2382 * primary and backup labels onto the current disk. 2383 */ 2384 int 2385 c_volname(void) 2386 { 2387 int status; 2388 char *prompt; 2389 union { 2390 int xfoo; 2391 char defvolname[LEN_DKL_VVOL+1]; 2392 } x; 2393 char s1[MAXPATHLEN], nclean[MAXPATHLEN]; 2394 char *volname; 2395 2396 2397 /* 2398 * There must be a current disk type (and therefore a current disk). 2399 */ 2400 if (cur_dtype == NULL) { 2401 err_print("Current Disk Type is not set.\n"); 2402 return (-1); 2403 } 2404 /* 2405 * The current disk must be formatted to label it. 2406 */ 2407 if (!(cur_flags & DISK_FORMATTED)) { 2408 err_print("Current Disk is unformatted.\n"); 2409 return (-1); 2410 } 2411 /* 2412 * Check for a valid fdisk table entry for Solaris 2413 */ 2414 if (!good_fdisk()) { 2415 return (-1); 2416 } 2417 /* 2418 * The current disk must be formatted to label it. 2419 */ 2420 if (cur_parts == NULL) { 2421 err_print( 2422 "Please select a partition map for the disk first.\n"); 2423 return (-1); 2424 } 2425 2426 /* 2427 * Check to see if there are any mounted file systems anywhere 2428 * on the current disk. If so, refuse to label the disk, but 2429 * only if the partitions would change for the mounted partitions. 2430 * 2431 */ 2432 if (checkmount((diskaddr_t)-1, (diskaddr_t)-1)) { 2433 /* Bleagh, too descriptive */ 2434 if (check_label_with_mount()) { 2435 err_print( 2436 "Cannot label disk while it has mounted partitions.\n\n"); 2437 return (-1); 2438 } 2439 } 2440 2441 /* 2442 * Check to see if there are partitions being used for swapping 2443 * on the current disk. If so, refuse to label the disk, but 2444 * only if the partitions would change for the swap partitions. 2445 * 2446 */ 2447 if (checkswap((diskaddr_t)-1, (diskaddr_t)-1)) { 2448 /* Bleagh, too descriptive */ 2449 if (check_label_with_swap()) { 2450 err_print( 2451 "Cannot label disk while its partitions are currently \ 2452 being used for swapping.\n\n"); 2453 return (-1); 2454 } 2455 } 2456 2457 /* 2458 * Check to see if any partitions used for svm, vxvm, ZFS zpool 2459 * or live upgrade are on the disk. If so, refuse to label the 2460 * disk, but only if we are trying to shrink a partition in 2461 * use. 2462 */ 2463 if (checkdevinuse(cur_disk->disk_name, (diskaddr_t)-1, 2464 (diskaddr_t)-1, 0, 1)) { 2465 err_print("Cannot label disk while its partitions " 2466 "are in use as described.\n"); 2467 return (-1); 2468 } 2469 2470 /* 2471 * Prompt for the disk volume name. 2472 */ 2473 prompt = "Enter 8-character volume name (remember quotes)"; 2474 bzero(x.defvolname, LEN_DKL_VVOL+1); 2475 bcopy(cur_disk->v_volume, x.defvolname, LEN_DKL_VVOL); 2476 /* 2477 * Get the input using "get_inputline" since 2478 * input would never return null string. 2479 */ 2480 fmt_print("%s[\"%s\"]:", prompt, x.defvolname); 2481 2482 /* 2483 * Get input from the user. 2484 */ 2485 get_inputline(nclean, MAXPATHLEN); 2486 clean_token(s1, nclean); 2487 /* 2488 * check for return. 2489 */ 2490 if (s1[0] == 0) { 2491 volname = x.defvolname; 2492 } else { 2493 /* 2494 * remove the " mark from volname. 2495 */ 2496 if (s1[0] == '"') { 2497 int i = 1; 2498 volname = &s1[1]; 2499 while (s1[i] != '"' && s1[i] != '\0') 2500 i++; 2501 s1[i] = '\0'; 2502 clean_token(nclean, volname); 2503 volname = nclean; 2504 } else { 2505 (void) sscanf(&s1[0], "%1024s", nclean); 2506 volname = nclean; 2507 }; 2508 } 2509 /* 2510 * Make sure the user is serious. 2511 */ 2512 if (check("Ready to label disk, continue")) { 2513 fmt_print("\n"); 2514 return (-1); 2515 } 2516 /* 2517 * Use the volume name chosen above 2518 */ 2519 bzero(cur_disk->v_volume, LEN_DKL_VVOL); 2520 bcopy(volname, cur_disk->v_volume, min((int)strlen(volname), 2521 LEN_DKL_VVOL)); 2522 if (cur_label == L_TYPE_EFI) { 2523 bzero(cur_parts->etoc->efi_parts[8].p_name, LEN_DKL_VVOL); 2524 bcopy(volname, cur_parts->etoc->efi_parts[8].p_name, 2525 LEN_DKL_VVOL); 2526 } 2527 /* 2528 * Write the labels out (this will also notify unix) and 2529 * return status. 2530 */ 2531 fmt_print("\n"); 2532 status = write_label(); 2533 if (status != 0) 2534 err_print("Label failed.\n"); 2535 return (status); 2536 } 2537