10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53525Sshidokht * Common Development and Distribution License (the "License").
63525Sshidokht * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9889SLarry.Liu@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * This file contains routines that manipulate the defect list.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate #include "global.h"
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate
330Sstevel@tonic-gate #if defined(sparc)
340Sstevel@tonic-gate #include <sys/hdio.h>
350Sstevel@tonic-gate #endif /* defined(sparc) */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <sys/buf.h>
380Sstevel@tonic-gate #include <sys/ioctl.h>
390Sstevel@tonic-gate #include <sys/uio.h>
400Sstevel@tonic-gate #include <sys/fcntl.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <memory.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate #if defined(sparc)
460Sstevel@tonic-gate #include <sys/dkbad.h>
470Sstevel@tonic-gate #endif /* defined(sparc) */
480Sstevel@tonic-gate
490Sstevel@tonic-gate #include "misc.h"
500Sstevel@tonic-gate #include "param.h"
510Sstevel@tonic-gate
520Sstevel@tonic-gate
530Sstevel@tonic-gate #if defined(sparc)
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * This structure is the bad block table for the current disk if
560Sstevel@tonic-gate * the disk uses bad-144 defect mapping.
570Sstevel@tonic-gate */
580Sstevel@tonic-gate struct dkbad badmap;
590Sstevel@tonic-gate #endif /* defined(sparc) */
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * This routine reads the defect list off the disk. It also reads in the
630Sstevel@tonic-gate * bad block table if the disk is a BAD144 type. The defect list is
640Sstevel@tonic-gate * located on the first 2 tracks of the 2nd alternate cylinder of all
650Sstevel@tonic-gate * disks. The bad block map is located on the first 5 even sectors of
660Sstevel@tonic-gate * the last track of the last cylinder.
670Sstevel@tonic-gate */
680Sstevel@tonic-gate void
read_list(struct defect_list * list)690Sstevel@tonic-gate read_list(struct defect_list *list)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate int size, head;
720Sstevel@tonic-gate
730Sstevel@tonic-gate #if defined(sparc)
740Sstevel@tonic-gate int sec, status;
750Sstevel@tonic-gate struct bt_bad *bt;
760Sstevel@tonic-gate #endif /* defined(sparc) */
770Sstevel@tonic-gate
780Sstevel@tonic-gate assert(!EMBEDDED_SCSI);
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * This flags has been introduced only for Sparc ATA IDE.
820Sstevel@tonic-gate * This indicates that no list manipulation is done in this controller
830Sstevel@tonic-gate * and hence return without any other checking.
840Sstevel@tonic-gate */
850Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_NOWLIST) {
860Sstevel@tonic-gate return;
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate * Panther's working list is maintained by the controller
910Sstevel@tonic-gate */
920Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_WLIST) {
930Sstevel@tonic-gate if (*cur_ops->op_ex_cur != NULL &&
940Sstevel@tonic-gate ((*cur_ops->op_ex_cur)(list)) == 0) {
950Sstevel@tonic-gate if (list->header.magicno != DEFECT_MAGIC) {
960Sstevel@tonic-gate fmt_print("Defect list BAD\n");
970Sstevel@tonic-gate } else {
980Sstevel@tonic-gate fmt_print("Controller working list found\n");
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate return;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if (*cur_ops->op_ex_man != NULL &&
1040Sstevel@tonic-gate ((*cur_ops->op_ex_man)(list)) == 0) {
1050Sstevel@tonic-gate if (list->header.magicno != DEFECT_MAGIC) {
1060Sstevel@tonic-gate fmt_print("Defect list BAD\n");
1070Sstevel@tonic-gate } else {
1080Sstevel@tonic-gate fmt_print("MANUFACTURER's list found\n");
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate return;
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate fmt_print("No defect list found\n");
1130Sstevel@tonic-gate return;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate * Loop for each copy of the defect list until we get a good one.
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate for (head = 0; head < LISTCOUNT; head++) {
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate * Try to read the list header.
1220Sstevel@tonic-gate */
1230Sstevel@tonic-gate if ((*cur_ops->op_rdwr)(DIR_READ, cur_file,
1240Sstevel@tonic-gate (diskaddr_t)chs2bn(ncyl + 1, head, 0), 1,
1250Sstevel@tonic-gate (char *)&list->header, NULL), F_NORMAL)
1260Sstevel@tonic-gate continue;
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate * If the magic number is wrong, this copy is corrupt.
1290Sstevel@tonic-gate */
1300Sstevel@tonic-gate if (list->header.magicno != DEFECT_MAGIC)
1310Sstevel@tonic-gate continue;
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * Allocate space for the rest of the list.
1340Sstevel@tonic-gate */
135*9889SLarry.Liu@Sun.COM size = deflist_size(cur_blksz, list->header.count);
136*9889SLarry.Liu@Sun.COM list->list = (struct defect_entry *)zalloc(size * cur_blksz);
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate * Try to read in the rest of the list. If there is an
1390Sstevel@tonic-gate * error, or the checksum is wrong, this copy is corrupt.
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate if ((*cur_ops->op_rdwr)(DIR_READ, cur_file,
1420Sstevel@tonic-gate (diskaddr_t)chs2bn(ncyl + 1, head, 1), size,
1430Sstevel@tonic-gate (char *)list->list, F_NORMAL, NULL) ||
1440Sstevel@tonic-gate checkdefsum(list, CK_CHECKSUM)) {
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate * Destroy the list and go on.
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate kill_deflist(list);
1490Sstevel@tonic-gate continue;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate * Got a good copy, stop searching.
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate break;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate #if defined(sparc)
1570Sstevel@tonic-gate if (!(cur_ctlr->ctlr_flags & DKI_BAD144))
1580Sstevel@tonic-gate return;
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate * The disk uses BAD144, read in the bad-block table.
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate for (sec = 0; ((sec < BAD_LISTCNT * 2) && (sec < nsect)); sec += 2) {
1630Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_READ, cur_file,
1640Sstevel@tonic-gate (diskaddr_t)chs2bn(ncyl + acyl - 1, nhead - 1, sec), 1,
1650Sstevel@tonic-gate &badmap, F_NORMAL, NULL);
1660Sstevel@tonic-gate if (status)
1670Sstevel@tonic-gate continue;
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * Do a sanity check on the list read in. If it passes,
1700Sstevel@tonic-gate * stop searching.
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate if (badmap.bt_mbz != 0)
1730Sstevel@tonic-gate continue;
1740Sstevel@tonic-gate for (bt = badmap.bt_bad; bt - badmap.bt_bad < NDKBAD; bt++) {
1750Sstevel@tonic-gate if (bt->bt_cyl < 0)
1760Sstevel@tonic-gate break;
1770Sstevel@tonic-gate if (bt->bt_trksec < 0)
1780Sstevel@tonic-gate continue;
1790Sstevel@tonic-gate head = bt->bt_trksec >> 8;
1800Sstevel@tonic-gate if ((bt->bt_cyl >= pcyl) || (head >= nhead) ||
1810Sstevel@tonic-gate ((bt->bt_trksec & 0xff) >= sectors(head))) {
1820Sstevel@tonic-gate status = -1;
1830Sstevel@tonic-gate break;
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate if (status)
1870Sstevel@tonic-gate continue;
1880Sstevel@tonic-gate return;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate * If we couldn't find the bad block table, initialize it to
1920Sstevel@tonic-gate * zero entries.
1930Sstevel@tonic-gate */
1940Sstevel@tonic-gate for (bt = badmap.bt_bad; bt - badmap.bt_bad < NDKBAD; bt++)
1950Sstevel@tonic-gate bt->bt_cyl = bt->bt_trksec = -1;
1960Sstevel@tonic-gate badmap.bt_mbz = badmap.bt_csn = badmap.bt_flag = 0;
1970Sstevel@tonic-gate #endif /* defined(sparc) */
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate * This routine either checks or calculates the checksum for a defect
2020Sstevel@tonic-gate * list, depending on the mode parameter. In check mode, it returns
2030Sstevel@tonic-gate * whether or not the checksum is correct.
2040Sstevel@tonic-gate */
2050Sstevel@tonic-gate int
checkdefsum(struct defect_list * list,int mode)2060Sstevel@tonic-gate checkdefsum(struct defect_list *list, int mode)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate register int *lp, i, sum = 0;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate * Perform the rolling xor to get what the checksum should be.
2120Sstevel@tonic-gate */
2130Sstevel@tonic-gate lp = (int *)list->list;
2140Sstevel@tonic-gate for (i = 0; i < (list->header.count *
2157563SPrasad.Singamsetty@Sun.COM sizeof (struct defect_entry) / sizeof (int)); i++)
2160Sstevel@tonic-gate sum ^= *(lp + i);
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * If in check mode, return whether header checksum was correct.
2190Sstevel@tonic-gate */
2200Sstevel@tonic-gate if (mode == CK_CHECKSUM)
2210Sstevel@tonic-gate return (sum != list->header.cksum);
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate * If in create mode, set the header checksum.
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate else {
2260Sstevel@tonic-gate list->header.cksum = sum;
2270Sstevel@tonic-gate return (0);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate * This routine prints a single defect to stdout in a readable format.
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate void
pr_defect(struct defect_entry * def,int num)2350Sstevel@tonic-gate pr_defect(struct defect_entry *def, int num)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate * Make defect numbering look 1 relative.
2400Sstevel@tonic-gate */
2410Sstevel@tonic-gate ++num;
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate * Print out common values.
2440Sstevel@tonic-gate */
2450Sstevel@tonic-gate fmt_print("%4d%8d%7d", num, def->cyl, def->head);
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate * The rest of the values may be unknown. If they are, just
2480Sstevel@tonic-gate * print blanks instead. Also, only print length only if bfi is
2490Sstevel@tonic-gate * known, and assume that a known bfi implies an unknown sect.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate if (def->bfi != UNKNOWN) {
2520Sstevel@tonic-gate fmt_print("%8d", def->bfi);
2530Sstevel@tonic-gate if (def->nbits != UNKNOWN)
2540Sstevel@tonic-gate fmt_print("%8d", def->nbits);
2550Sstevel@tonic-gate } else {
2560Sstevel@tonic-gate fmt_print(" ");
2570Sstevel@tonic-gate fmt_print("%8d", def->sect);
2587563SPrasad.Singamsetty@Sun.COM fmt_print("%8llu", chs2bn(def->cyl, def->head, def->sect));
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate fmt_print("\n");
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate * This routine calculates where in a defect list a given defect should
2650Sstevel@tonic-gate * be sorted. It returns the index that the defect should become. The
2660Sstevel@tonic-gate * algorithm used sorts all bfi based defects by cylinder/head/bfi, and
2670Sstevel@tonic-gate * adds all logical sector defects to the end of the list. This is
2680Sstevel@tonic-gate * necessary because the ordering of logical sector defects is significant
2690Sstevel@tonic-gate * when sector slipping is employed.
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate int
sort_defect(struct defect_entry * def,struct defect_list * list)2720Sstevel@tonic-gate sort_defect(struct defect_entry *def, struct defect_list *list)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate struct defect_entry *ptr;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate * If it's a logical sector defect, return the entry at the end
2780Sstevel@tonic-gate * of the list.
2790Sstevel@tonic-gate */
2800Sstevel@tonic-gate if (def->bfi == UNKNOWN)
2810Sstevel@tonic-gate return (list->header.count);
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate * It's a bfi defect. Loop through the defect list.
2840Sstevel@tonic-gate */
2850Sstevel@tonic-gate for (ptr = list->list; ptr - list->list < list->header.count; ptr++) {
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate * If we get to a logical sector defect, put this defect
2880Sstevel@tonic-gate * right before it.
2890Sstevel@tonic-gate */
2900Sstevel@tonic-gate if (ptr->bfi == UNKNOWN)
2910Sstevel@tonic-gate goto found;
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * If we get to a defect that is past this one in
2940Sstevel@tonic-gate * cylinder/head/bfi, put this defect right before it.
2950Sstevel@tonic-gate */
2960Sstevel@tonic-gate if (def->cyl < ptr->cyl)
2970Sstevel@tonic-gate goto found;
2980Sstevel@tonic-gate if (def->cyl != ptr->cyl)
2990Sstevel@tonic-gate continue;
3000Sstevel@tonic-gate if (def->head < ptr->head)
3010Sstevel@tonic-gate goto found;
3020Sstevel@tonic-gate if (def->head != ptr->head)
3030Sstevel@tonic-gate continue;
3040Sstevel@tonic-gate if (def->bfi < ptr->bfi)
3050Sstevel@tonic-gate goto found;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate found:
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * Return the index to put the defect at.
3100Sstevel@tonic-gate */
3110Sstevel@tonic-gate return (ptr - list->list);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * This routine writes the defect list on the back on the disk. It also
3160Sstevel@tonic-gate * writes the bad block table to disk if bad-144 mapping applies to the
3170Sstevel@tonic-gate * current disk.
3180Sstevel@tonic-gate */
3190Sstevel@tonic-gate void
write_deflist(struct defect_list * list)3200Sstevel@tonic-gate write_deflist(struct defect_list *list)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate int size, head, status;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate #if defined(sparc)
3250Sstevel@tonic-gate int sec;
3260Sstevel@tonic-gate caddr_t bad_ptr = (caddr_t)&badmap;
3270Sstevel@tonic-gate #endif /* defined(sparc) */
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate assert(!EMBEDDED_SCSI);
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate * Sparc ATA IDE.
3330Sstevel@tonic-gate * This indicates that no list manipulation is done in this controller
3340Sstevel@tonic-gate * and hence return without any other checking.
3350Sstevel@tonic-gate */
3360Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_NOWLIST) {
3370Sstevel@tonic-gate return;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate * Panther's working list is maintained by the controller
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate if (cur_ctype->ctype_flags & CF_WLIST) {
3440Sstevel@tonic-gate (*cur_ops->op_wr_cur)(list);
3450Sstevel@tonic-gate return;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate * If the list is null, there is nothing to write.
3500Sstevel@tonic-gate */
3510Sstevel@tonic-gate if (list->list != NULL) {
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate * calculate how many sectors the defect list will occupy.
3540Sstevel@tonic-gate */
355*9889SLarry.Liu@Sun.COM size = deflist_size(cur_blksz, list->header.count);
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate * Loop for each copy of the list to be written. Write
3580Sstevel@tonic-gate * out the header of the list followed by the data.
3590Sstevel@tonic-gate */
3600Sstevel@tonic-gate for (head = 0; head < LISTCOUNT; head++) {
3610Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file,
3620Sstevel@tonic-gate (diskaddr_t)chs2bn(ncyl + 1, head, 0), 1,
3630Sstevel@tonic-gate (char *)&list->header, F_NORMAL, NULL);
3640Sstevel@tonic-gate if (status) {
3650Sstevel@tonic-gate err_print(
3660Sstevel@tonic-gate "Warning: error saving defect list.\n");
3670Sstevel@tonic-gate continue;
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file,
3700Sstevel@tonic-gate (diskaddr_t)chs2bn(ncyl + 1, head, 1), size,
3710Sstevel@tonic-gate (char *)list->list, F_NORMAL, NULL);
3720Sstevel@tonic-gate if (status)
3730Sstevel@tonic-gate err_print(
3740Sstevel@tonic-gate "Warning: error saving defect list.\n");
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate if (!(cur_ctlr->ctlr_flags & DKI_BAD144))
3780Sstevel@tonic-gate return;
3790Sstevel@tonic-gate #if defined(sparc)
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate * Current disk uses bad-144 mapping. Loop for each copy of the
3820Sstevel@tonic-gate * bad block table to be written and write it out.
3830Sstevel@tonic-gate */
3840Sstevel@tonic-gate for (sec = 0; ((sec < BAD_LISTCNT * 2) && (sec < nsect)); sec += 2) {
3850Sstevel@tonic-gate status = (*cur_ops->op_rdwr)(DIR_WRITE, cur_file,
3860Sstevel@tonic-gate (diskaddr_t)chs2bn(ncyl + acyl - 1, nhead - 1, sec), 1,
3870Sstevel@tonic-gate &badmap, F_NORMAL, NULL);
3880Sstevel@tonic-gate if (status) {
3890Sstevel@tonic-gate err_print(
3900Sstevel@tonic-gate "Warning: error saving bad block map table.\n");
3910Sstevel@tonic-gate continue;
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate * Execute an ioctl to tell unix about the new bad block table.
3960Sstevel@tonic-gate */
3970Sstevel@tonic-gate if (ioctl(cur_file, HDKIOCSBAD, &bad_ptr))
3980Sstevel@tonic-gate err_print(
3990Sstevel@tonic-gate "Warning: error telling SunOS bad block map table.\n");
4000Sstevel@tonic-gate #endif /* defined(sparc) */
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate * This routine adds a logical sector to the given defect list.
4050Sstevel@tonic-gate */
4060Sstevel@tonic-gate void
add_ldef(diskaddr_t blkno,struct defect_list * list)4070Sstevel@tonic-gate add_ldef(diskaddr_t blkno, struct defect_list *list)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate struct defect_entry def;
4100Sstevel@tonic-gate int index;
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate * Calculate the fields for the defect struct.
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate def.cyl = bn2c(blkno);
4170Sstevel@tonic-gate def.head = bn2h(blkno);
4180Sstevel@tonic-gate def.sect = bn2s(blkno);
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * Initialize the unknown fields.
4210Sstevel@tonic-gate */
4220Sstevel@tonic-gate def.bfi = def.nbits = UNKNOWN;
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate * Calculate the index into the list that the defect belongs at.
4250Sstevel@tonic-gate */
4260Sstevel@tonic-gate index = sort_defect(&def, list);
4270Sstevel@tonic-gate /*
4280Sstevel@tonic-gate * Add the defect to the list.
4290Sstevel@tonic-gate */
4300Sstevel@tonic-gate add_def(&def, list, index);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate * This routine adds the given defect struct to the defect list at
4350Sstevel@tonic-gate * a precalculated index.
4360Sstevel@tonic-gate */
4370Sstevel@tonic-gate void
add_def(struct defect_entry * def,struct defect_list * list,int index)4380Sstevel@tonic-gate add_def(struct defect_entry *def, struct defect_list *list, int index)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate int count, i;
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate * If adding this defect makes the list overflow into another
4440Sstevel@tonic-gate * sector, allocate the necessary space.
4450Sstevel@tonic-gate */
4460Sstevel@tonic-gate count = list->header.count;
447*9889SLarry.Liu@Sun.COM if (deflist_size(cur_blksz, count + 1) > deflist_size(cur_blksz, count))
4480Sstevel@tonic-gate list->list = (struct defect_entry *)rezalloc((void *)list->list,
449*9889SLarry.Liu@Sun.COM deflist_size(cur_blksz, count + 1) * cur_blksz);
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate * Slip all the defects after this one down one slot in the list.
4520Sstevel@tonic-gate */
4530Sstevel@tonic-gate for (i = count; i > index; i--)
4540Sstevel@tonic-gate *(list->list + i) = *(list->list + i - 1);
4550Sstevel@tonic-gate /*
4560Sstevel@tonic-gate * Fill in the created hole with this defect.
4570Sstevel@tonic-gate */
4580Sstevel@tonic-gate *(list->list + i) = *def;
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate * Increment the count and calculate a new checksum.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate list->header.count++;
4630Sstevel@tonic-gate (void) checkdefsum(list, CK_MAKESUM);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate * This routine sets the given defect list back to null.
4680Sstevel@tonic-gate */
4690Sstevel@tonic-gate void
kill_deflist(struct defect_list * list)4700Sstevel@tonic-gate kill_deflist(struct defect_list *list)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate /*
4740Sstevel@tonic-gate * If it's already null, we're done.
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate if (list->list == NULL)
4770Sstevel@tonic-gate return;
4780Sstevel@tonic-gate /*
4790Sstevel@tonic-gate * Free the malloc'd space it's using.
4800Sstevel@tonic-gate */
4810Sstevel@tonic-gate destroy_data((char *)list->list);
4820Sstevel@tonic-gate /*
4830Sstevel@tonic-gate * Mark it as null, and clear any flags.
4840Sstevel@tonic-gate */
4850Sstevel@tonic-gate list->list = NULL;
4860Sstevel@tonic-gate list->flags = 0;
4870Sstevel@tonic-gate }
488*9889SLarry.Liu@Sun.COM
489*9889SLarry.Liu@Sun.COM /*
490*9889SLarry.Liu@Sun.COM * This routine returns the defect list size
491*9889SLarry.Liu@Sun.COM * according to the sector size.
492*9889SLarry.Liu@Sun.COM */
493*9889SLarry.Liu@Sun.COM int
deflist_size(int secsz,int sz)494*9889SLarry.Liu@Sun.COM deflist_size(int secsz, int sz)
495*9889SLarry.Liu@Sun.COM {
496*9889SLarry.Liu@Sun.COM int rval;
497*9889SLarry.Liu@Sun.COM
498*9889SLarry.Liu@Sun.COM if (secsz == 0) {
499*9889SLarry.Liu@Sun.COM secsz = SECSIZE;
500*9889SLarry.Liu@Sun.COM }
501*9889SLarry.Liu@Sun.COM
502*9889SLarry.Liu@Sun.COM rval = sz ? ((sz * sizeof (struct defect_entry) +
503*9889SLarry.Liu@Sun.COM secsz - 1) / secsz) : 1;
504*9889SLarry.Liu@Sun.COM
505*9889SLarry.Liu@Sun.COM return (rval);
506*9889SLarry.Liu@Sun.COM }
507