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.
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.
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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright (c) 2016 by Delphix. All rights reserved.
28 * This file contains functions to implement the defect menu commands.
34 #include "menu_defect.h"
36 #include "ctlr_scsi.h"
39 * This is the working defect list. All the commands here operate on
40 * the working list, except for 'commit'. This way the user can
41 * change their mind at any time without having mangled the current defect
44 struct defect_list work_list
;
48 /* Function prototypes for ANSI C Compilers */
49 static int commit_list(void);
53 /* Function prototypes for non-ANSI C Compilers */
54 static int commit_list();
59 * This routine implements the 'restore' command. It sets the working
60 * list equal to the current list.
67 assert(!EMBEDDED_SCSI
);
70 * If the working list has not been modified, there's nothing to do.
72 if (!(work_list
.flags
& LIST_DIRTY
)) {
73 err_print("working list was not modified.\n");
77 * Make sure the user is serious.
79 if (check("Ready to update working list, continue"))
82 * Lock out interrupts so the lists can't get mangled.
86 * Kill off the old working list.
88 kill_deflist(&work_list
);
90 * If the current isn't null, set the working list to be a
93 if (cur_list
.list
!= NULL
) {
94 work_list
.header
= cur_list
.header
;
95 work_list
.list
= (struct defect_entry
*)zalloc(
96 deflist_size(cur_blksz
, work_list
.header
.count
) *
98 for (i
= 0; i
< work_list
.header
.count
; i
++)
99 *(work_list
.list
+ i
) = *(cur_list
.list
+ i
);
102 * Initialize the flags since they are now in sync.
105 if (work_list
.list
== NULL
)
106 fmt_print("working list set to null.\n\n");
108 fmt_print("working list updated, total of %d defects.\n\n",
109 work_list
.header
.count
);
115 * This routine implements the 'original' command. It extracts the
116 * manufacturer's defect list from the current disk.
125 * If the controller does not support the extraction, we're out
128 if (cur_ops
->op_ex_man
== NULL
) {
129 err_print("Controller does not support extracting ");
130 err_print("manufacturer's defect list.\n");
134 * Make sure the user is serious. Note, for SCSI disks
135 * since this is instantaneous, we will just do it and
136 * not ask for confirmation.
138 if (!(cur_ctype
->ctype_flags
& CF_CONFIRM
) &&
140 "Ready to update working list. This cannot be interrupted\n\
141 and may take a long while. Continue"))
144 * Lock out interrupts so we don't get half the list.
148 * Kill off the working list.
150 kill_deflist(&work_list
);
151 fmt_print("Extracting manufacturer's defect list...");
155 status
= (*cur_ops
->op_ex_man
)(&work_list
);
157 fmt_print("Extraction failed.\n\n");
159 fmt_print("Extraction complete.\n");
160 fmt_print("Working list updated, total of %d defects.\n\n",
161 work_list
.header
.count
);
164 * Mark the working list dirty since we modified it.
166 work_list
.flags
|= LIST_DIRTY
;
175 * This routine implements the 'extract' command. It extracts the
176 * entire defect list from the current disk.
184 * If the controller does not support the extraction, we are out
187 if (cur_ops
->op_ex_cur
== NULL
) {
188 err_print("Controller does not support extracting ");
189 err_print("current defect list.\n");
194 * If disk is unformatted, you really shouldn't do this.
195 * However, ask user to be sure.
197 if (! (cur_flags
& DISK_FORMATTED
) &&
199 "Cannot extract defect list from an unformatted disk. Continue")))
203 * If this takes a long time, let's ask the user if they
204 * doesn't mind waiting. Note, for SCSI disks
205 * this operation is instantaneous so we won't ask for
208 if (! (cur_ctype
->ctype_flags
& CF_CONFIRM
) &&
210 "Ready to extract working list. This cannot be interrupted\n\
211 and may take a long while. Continue"))
214 * Lock out interrupts so we don't get half the list and
215 * Kill off the working list.
218 kill_deflist(&work_list
);
219 fmt_print("Extracting defect list...");
224 status
= (*cur_ops
->op_ex_cur
)(&work_list
);
226 if (!EMBEDDED_SCSI
) {
227 if (cur_flags
& DISK_FORMATTED
)
228 read_list(&work_list
);
230 if (work_list
.list
!= NULL
) {
232 fmt_print("Extraction complete.\n");
234 "Working list updated, total of %d defects.\n\n",
235 work_list
.header
.count
);
237 fmt_print("Extraction failed.\n\n");
240 fmt_print("Extraction failed.\n\n");
243 fmt_print("Extraction complete.\n");
244 fmt_print("Working list updated, total of %d defects.\n\n",
245 work_list
.header
.count
);
248 * Mark the working list dirty since we modified it.
250 work_list
.flags
|= LIST_DIRTY
;
259 * This routine implements the 'add' command. It allows the user to
260 * enter the working defect list manually. It loops infinitely until
261 * the user breaks out with a ctrl-C.
266 int type
, deflt
, index
;
269 struct defect_entry def
;
271 assert(!EMBEDDED_SCSI
);
274 * Ask the user which mode of input they'd like to use.
276 fmt_print(" 0. bytes-from-index\n");
277 fmt_print(" 1. logical block\n");
279 ioparam
.io_bounds
.lower
= 0;
280 ioparam
.io_bounds
.upper
= 1;
281 type
= input(FIO_INT
, "Select input format (enter its number)", ':',
282 &ioparam
, &deflt
, DATA_INPUT
);
283 fmt_print("\nEnter Control-C to terminate.\n");
287 * Mode selected is logical block. Input the defective block
288 * and fill in the defect entry with the info.
290 def
.bfi
= def
.nbits
= UNKNOWN
;
291 ioparam
.io_bounds
.lower
= 0;
292 if (cur_disk
->label_type
== L_TYPE_SOLARIS
) {
293 ioparam
.io_bounds
.upper
= physsects() - 1;
295 ioparam
.io_bounds
.upper
= cur_parts
->etoc
->efi_last_lba
;
297 bn
= input(FIO_BN
, "Enter defective block number", ':',
298 &ioparam
, NULL
, DATA_INPUT
);
304 * Mode selected is bytes-from-index. Input the information
305 * about the defect and fill in the defect entry.
308 ioparam
.io_bounds
.lower
= 0;
309 ioparam
.io_bounds
.upper
= pcyl
- 1;
310 def
.cyl
= input(FIO_INT
,
311 "Enter defect's cylinder number", ':',
312 &ioparam
, NULL
, DATA_INPUT
);
313 ioparam
.io_bounds
.upper
= nhead
- 1;
314 def
.head
= input(FIO_INT
, "Enter defect's head number",
315 ':', &ioparam
, NULL
, DATA_INPUT
);
316 ioparam
.io_bounds
.upper
= cur_dtype
->dtype_bpt
- 1;
317 def
.bfi
= input(FIO_INT
, "Enter defect's bytes-from-index",
318 ':', &ioparam
, NULL
, DATA_INPUT
);
319 ioparam
.io_bounds
.lower
= -1;
320 ioparam
.io_bounds
.upper
= (cur_dtype
->dtype_bpt
- def
.bfi
) * 8;
321 if (ioparam
.io_bounds
.upper
>= 32 * 1024)
322 ioparam
.io_bounds
.upper
= 32 * 1024 - 1;
324 * Note: a length of -1 means the length is not known. We
325 * make this the default value.
328 def
.nbits
= input(FIO_INT
, "Enter defect's length (in bits)",
329 ':', &ioparam
, &deflt
, DATA_INPUT
);
332 * Calculate where in the defect list this defect belongs
335 index
= sort_defect(&def
, &work_list
);
336 fmt_print(DEF_PRINTHEADER
);
337 pr_defect(&def
, index
);
340 * Lock out interrupts so lists don't get mangled.
341 * Also, mark the working list dirty since we are modifying it.
344 work_list
.flags
|= LIST_DIRTY
;
346 * If the list is null, create it with zero length. This is
347 * necessary because the routines to add a defect to the list
348 * assume the list is initialized.
350 if (work_list
.list
== NULL
) {
351 work_list
.header
.magicno
= (uint_t
)DEFECT_MAGIC
;
352 work_list
.header
.count
= 0;
353 work_list
.list
= (struct defect_entry
*)zalloc(
354 deflist_size(cur_blksz
, 0) * cur_blksz
);
357 * Add the defect to the working list.
359 add_def(&def
, &work_list
, index
);
360 fmt_print("defect number %d added.\n\n", index
+ 1);
363 * Loop back for the next defect.
370 * This routine implements the 'delete' command. It allows the user
371 * to manually delete a defect from the working list.
379 assert(!EMBEDDED_SCSI
);
382 * If the working list is null or zero length, there's nothing
385 count
= work_list
.header
.count
;
386 if (work_list
.list
== NULL
|| count
== 0) {
387 err_print("No defects to delete.\n");
391 * Ask the user which defect should be deleted. Bounds are off by
392 * one because user sees a one-relative list.
394 ioparam
.io_bounds
.lower
= 1;
395 ioparam
.io_bounds
.upper
= count
;
396 num
= input(FIO_INT
, "Specify defect to be deleted (enter its number)",
397 ':', &ioparam
, NULL
, DATA_INPUT
);
400 * The user thinks it's one relative but it's not really.
404 * Print the defect selected and ask the user for confirmation.
406 fmt_print(DEF_PRINTHEADER
);
407 pr_defect(work_list
.list
+ num
, num
);
409 * Lock out interrupts so the lists don't get mangled.
413 * Move down all the defects beyond the one deleted so the defect
414 * list is still fully populated.
416 for (i
= num
; i
< count
- 1; i
++)
417 *(work_list
.list
+ i
) = *(work_list
.list
+ i
+ 1);
419 * If the size of the list in sectors has changed, reallocate
420 * the list to shrink it appropriately.
422 if (deflist_size(cur_blksz
, count
- 1) <
423 deflist_size(cur_blksz
, count
))
424 work_list
.list
= (struct defect_entry
*)rezalloc(
425 (void *)work_list
.list
,
426 deflist_size(cur_blksz
, count
- 1) * cur_blksz
);
428 * Decrement the defect count.
430 work_list
.header
.count
--;
432 * Recalculate the list's checksum.
434 (void) checkdefsum(&work_list
, CK_MAKESUM
);
436 * Mark the working list dirty since we modified it.
438 work_list
.flags
|= LIST_DIRTY
;
439 fmt_print("defect number %d deleted.\n\n", ++num
);
445 * This routine implements the 'print' command. It prints the working
446 * defect list out in human-readable format.
453 int tty_lines
= get_tty_lines();
456 * If the working list is null, there's nothing to print.
458 if (work_list
.list
== NULL
) {
461 "No list defined,extract primary or grown or both defects list first.\n");
463 err_print("No working list defined.\n");
467 * If we're running from a file, don't use the paging scheme.
468 * If we are running interactive, turn off echoing.
470 if (option_f
|| (!isatty(0)) || (!isatty(1)))
478 /* Print out the banner. */
479 if (work_list
.header
.count
!= 0)
480 fmt_print(DEF_PRINTHEADER
);
483 * Loop through the each defect in the working list.
485 for (i
= 0; i
< work_list
.header
.count
; i
++) {
487 * If we are paging and hit the end of a page, wait for
488 * the user to hit either space-bar, "q", or return
492 (!nomore
&& ((i
+ 1) % (tty_lines
- 1) == 0))) {
494 * Get the next character.
496 fmt_print("- hit space for more - ");
500 /* Handle display one line command (return key) */
504 /* Handle Quit command */
516 pr_defect(work_list
.list
+ i
, i
);
518 fmt_print("total of %d defects.\n\n", i
);
520 * If we were doing paging, turn echoing back on.
533 * This routine implements the 'dump' command. It writes the working
534 * defect list to a file.
542 struct defect_entry
*dptr
;
545 * If the working list is null, there's nothing to do.
547 if (work_list
.list
== NULL
) {
550 "No list defined,extract primary or grown or both defects list first.\n");
552 err_print("No working list defined.\n");
556 * Ask the user for the name of the defect file. Note that the
557 * input will be in malloc'd space since we are inputting
560 str
= (char *)(uintptr_t)input(FIO_OSTR
, "Enter name of defect file",
561 ':', (u_ioparam_t
*)NULL
, NULL
, DATA_INPUT
);
563 * Lock out interrupts so the file doesn't get half written.
567 * Open the file for writing.
569 if ((fptr
= fopen(str
, "w+")) == NULL
) {
570 err_print("unable to open defect file.\n");
575 * Print a header containing the magic number, count, and checksum.
577 (void) fprintf(fptr
, "0x%08x%8d 0x%08x\n",
578 work_list
.header
.magicno
,
579 work_list
.header
.count
, work_list
.header
.cksum
);
581 * Loop through each defect in the working list. Write the
582 * defect info to the defect file.
584 for (i
= 0; i
< work_list
.header
.count
; i
++) {
585 dptr
= work_list
.list
+ i
;
586 (void) fprintf(fptr
, "%4d%8d%7d%8d%8d%8d\n",
587 i
+1, dptr
->cyl
, dptr
->head
,
588 dptr
->bfi
, dptr
->nbits
, dptr
->sect
);
590 fmt_print("defect file updated, total of %d defects.\n", i
);
592 * Close the defect file.
597 * Destroy the string used for the file name.
606 * This routine implements the 'load' command. It reads the working
607 * list in from a file.
612 int i
, items
, status
= 0, count
, cksum
;
617 struct defect_entry
*dptr
;
619 assert(!EMBEDDED_SCSI
);
622 * Ask the user for the name of the defect file. Note that the
623 * input will be malloc'd space since we inputted type OSTR.
625 str
= (char *)(uintptr_t)input(FIO_OSTR
, "Enter name of defect file",
626 ':', (u_ioparam_t
*)NULL
, NULL
, DATA_INPUT
);
628 * Copy the file name into local space then destroy the string
629 * it came in. This is simply a precaution against later having
630 * to remember to destroy this space.
633 (void) strcpy(filename
, str
);
637 * See if the defect file is accessable. If not, we can't load
638 * from it. We do this here just so we can get out before asking
639 * the user for confirmation.
641 status
= access(filename
, 4);
643 err_print("defect file not accessable.\n");
647 * Make sure the user is serious.
649 if (check("ready to update working list, continue"))
652 * Lock out interrupts so the list doesn't get half loaded.
656 * Open the defect file.
658 if ((fptr
= fopen(filename
, "r")) == NULL
) {
659 err_print("unable to open defect file.\n");
664 * Scan in the header.
666 items
= fscanf(fptr
, "0x%x%d 0x%x\n", &magicno
,
667 &count
, (uint_t
*)&cksum
);
669 * If the header is wrong, this isn't a good defect file.
671 if (items
!= 3 || count
< 0 ||
672 (magicno
!= (uint_t
)DEFECT_MAGIC
&&
673 magicno
!= (uint_t
)NO_CHECKSUM
)) {
674 err_print("Defect file is corrupted.\n");
679 * Kill off any old defects in the working list.
681 kill_deflist(&work_list
);
683 * Load the working list header with the header info.
685 if (magicno
== NO_CHECKSUM
)
686 work_list
.header
.magicno
= (uint_t
)DEFECT_MAGIC
;
688 work_list
.header
.magicno
= magicno
;
689 work_list
.header
.count
= count
;
690 work_list
.header
.cksum
= cksum
;
692 * Allocate space for the new list.
694 work_list
.list
= (struct defect_entry
*)zalloc(
695 deflist_size(cur_blksz
, count
) * cur_blksz
);
697 * Mark the working list dirty since we are modifying it.
699 work_list
.flags
|= LIST_DIRTY
;
701 * Loop through each defect in the defect file.
703 for (i
= 0; i
< count
; i
++) {
704 dptr
= work_list
.list
+ i
;
706 * Scan the info into the defect entry.
708 items
= fscanf(fptr
, "%*d%hd%hd%d%hd%hd\n", &dptr
->cyl
,
709 &dptr
->head
, &dptr
->bfi
, &dptr
->nbits
, &dptr
->sect
);
711 * If it didn't scan right, give up.
717 * Check to be sure the checksum from the defect file was correct
718 * unless there wasn't supposed to be a checksum.
719 * If there was supposed to be a valid checksum and there isn't
722 if (magicno
!= NO_CHECKSUM
&& checkdefsum(&work_list
, CK_CHECKSUM
))
724 fmt_print("working list updated, total of %d defects.\n", i
);
729 * Some kind of error occurred. Kill off the working list and
730 * mark the status bad.
732 err_print("Defect file is corrupted, working list set to NULL.\n");
733 kill_deflist(&work_list
);
737 * Close the defect file.
746 * This routine implements the 'commit' command. It causes the current
747 * defect list to be set equal to the working defect list. It is the only
748 * way that changes made to the working list can actually take effect in
755 * If the working list wasn't modified, no commit is necessary.
757 if (work_list
.list
!= NULL
&& !(work_list
.flags
& LIST_DIRTY
)) {
758 err_print("working list was not modified.\n");
763 * Make sure the user is serious.
765 if (check("Ready to update Current Defect List, continue"))
767 return (do_commit());
775 if ((status
= commit_list()) == 0) {
777 * Remind the user to format the drive, since changing
778 * the list does nothing unless a format is performed.
781 "Disk must be reformatted for changes to take effect.\n\n");
793 * Lock out interrupts so the list doesn't get half copied.
797 * Kill off any current defect list.
799 kill_deflist(&cur_list
);
801 * If the working list is null, initialize it to zero length.
802 * This is so the user can do a commit on a null list and get
803 * a zero length list. Otherwise there would be no way to get
804 * a zero length list conveniently.
806 if (work_list
.list
== NULL
) {
807 work_list
.header
.magicno
= (uint_t
)DEFECT_MAGIC
;
808 work_list
.header
.count
= 0;
809 work_list
.list
= (struct defect_entry
*)zalloc(
810 deflist_size(cur_blksz
, 0) * cur_blksz
);
813 * Copy the working list into the current list.
815 cur_list
.header
= work_list
.header
;
816 cur_list
.list
= (struct defect_entry
*)zalloc(
817 deflist_size(cur_blksz
, cur_list
.header
.count
) * cur_blksz
);
818 for (i
= 0; i
< cur_list
.header
.count
; i
++)
819 *(cur_list
.list
+ i
) = *(work_list
.list
+ i
);
821 * Mark the working list clean, since it is now the same as the
822 * current list. Note we do not mark the current list dirty,
823 * even though it has been changed. This is because it does
824 * not reflect the state of disk, so we don't want it written
825 * out until a format has been done. The format will mark it
826 * dirty and write it out.
828 work_list
.flags
&= ~(LIST_DIRTY
|LIST_RELOAD
);
829 cur_list
.flags
= work_list
.flags
;
831 fmt_print("Defect List has a total of %d defects.\n",
832 cur_list
.header
.count
);
834 fmt_print("Current Defect List updated, total of %d defects.\n",
835 cur_list
.header
.count
);
842 * This routine implements the 'create' command. It creates the
843 * manufacturer's defect on the current disk from the defect list
850 assert(!EMBEDDED_SCSI
);
853 * If the controller does not support the extraction, we're out
856 if (cur_ops
->op_create
== NULL
) {
857 err_print("Controller does not support creating ");
858 err_print("manufacturer's defect list.\n");
862 * Make sure the user is serious. Note, for SCSI disks
863 * since this is instantaneous, we will just do it and
864 * not ask for confirmation.
866 if (! (cur_ctype
->ctype_flags
& CF_SCSI
) &&
868 "Ready to create the manufacturers defect information on the disk.\n\
869 This cannot be interrupted and may take a long while.\n\
870 IT WILL DESTROY ALL OF THE DATA ON THE DISK! Continue"))
873 * Lock out interrupts so we don't get half the list.
876 fmt_print("Creating manufacturer's defect list...");
880 status
= (*cur_ops
->op_create
)(&work_list
);
882 fmt_print("Creation failed.\n\n");
884 fmt_print("Creation complete.\n");
895 * Extract primary defect list - SCSI only
902 assert(EMBEDDED_SCSI
);
905 * Lock out interrupts so we don't get half the list and
906 * Kill off the working list.
909 kill_deflist(&work_list
);
910 fmt_print("Extracting primary defect list...");
915 status
= scsi_ex_man(&work_list
);
917 fmt_print("Extraction failed.\n\n");
919 fmt_print("Extraction complete.\n");
921 * Mark the working list dirty since we modified it.
922 * Automatically commit it, for SCSI only.
924 work_list
.flags
|= LIST_DIRTY
;
925 status
= commit_list();
938 * Extract grown defects list - SCSI only
945 assert(EMBEDDED_SCSI
);
948 * Lock out interrupts so we don't get half the list and
949 * Kill off the working list.
952 kill_deflist(&work_list
);
953 fmt_print("Extracting grown defects list...");
958 status
= scsi_ex_grown(&work_list
);
960 fmt_print("Extraction failed.\n\n");
962 fmt_print("Extraction complete.\n");
964 * Mark the working list dirty since we modified it.
965 * Automatically commit it, for SCSI only.
967 work_list
.flags
|= LIST_DIRTY
;
968 status
= commit_list();
981 * Extract both primary and grown defects list - SCSI only
988 assert(EMBEDDED_SCSI
);
991 * Lock out interrupts so we don't get half the list and
992 * Kill off the working list.
995 kill_deflist(&work_list
);
996 fmt_print("Extracting both primary and grown defects lists...");
1001 status
= scsi_ex_cur(&work_list
);
1003 fmt_print("Extraction failed.\n\n");
1005 fmt_print("Extraction complete.\n");
1007 * Mark the working list dirty since we modified it.
1008 * Automatically commit it, for SCSI only.
1010 work_list
.flags
|= LIST_DIRTY
;
1011 status
= commit_list();