/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
* copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
* All rights reserved.
*/
/*
* Copyrighted as an unpublished work.
* (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990
* All rights reserved.
*/
#include <ctype.h>
#include <fcntl.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "badsec.h"
#define FAILURE 0
#define CMD_READ 0
int badsl_chain_cnt;
int gbadsl_chain_cnt;
extern int alts_fd;
static void chk_badsec(void);
static void init_altsctr(void);
void wr_altsctr(void);
static void get_badsec(void);
static int count_badsec(void);
static void gen_alts_ent(void);
static void assign_altsctr(void);
static void expand_map(void);
static void compress_map(void);
static int ent_merge(
int lcnt1,
int lcnt2);
int get_altsctr(int);
static void print_altsctr(void);
int ioflag);
/*
*/
int
{
if (init_flag)
get_badsec();
gen_alts_ent();
compress_map();
return (SUCCESS);
}
/*
* read_altsctr( ptr to alternate sector partition )
* -- read the alternate sector partition tables
*/
static void
{
/* allocate buffer for the alts partition table (sector size) */
"Unable to malloc alternate partition table.\n");
exit(50);
}
/* allocate buffer for the alts partition map (sector size) */
/* buffers include the disk image bit map */
/* and the incore transformed char map */
"Unable to malloc incore alternate partition map.\n");
exit(51);
}
NBPSCTR);
"Unable to malloc alternate partition map.\n");
exit(52);
}
/* clear the buffers to zero */
/*
* if add alternate partition flag is set, then install the partition
* otherwise read the alts partition info from disk
* if failed, then assume the first installation
*/
"WARNING: Manually initializing alternate table.\n");
init_altsctr();
} else {
chk_badsec();
else
init_altsctr();
}
}
}
/*
* checking duplicate bad sectors or bad sectors in ALTSCTR partition
*/
static void
chk_badsec(void)
{
int cnt;
int status;
/* if bad sector is within the ATLSCTR partition */
"Alternate partition information table is bad.\n");
exit(53);
}
"Alternate partition map is bad.\n");
exit(54);
}
"Alternate partition entry table is bad.\n");
exit(55);
}
} else {
}
} else {
/*
* binary search for bad sector in the alts entry table
*/
/*
* if the bad sector had already been remapped(found in alts_entry)
* then ignore the bad sector
*/
if (status != -1) {
}
}
}
}
/*
* initialize the alternate partition tables
*/
static void
init_altsctr(void)
{
int cnt;
ap->ap_ent_secsiz = 0;
if (badsec == altsp_srtsec) {
"First sector of alternate partition is bad.\n");
exit(56);
}
}
}
/* allocate the alts_map on disk skipping possible bad sectors */
perror("Unable to allocate alternate map on disk: ");
exit(57);
}
}
/*
* read the alternate partition tables from disk
*/
int
int badok;
{
/* get alts partition table info */
if (badok)
return(FAILURE);
perror("Unable to read alternate sector partition: ");
exit(58);
}
if (badok)
return(FAILURE);
exit(69);
}
/* get the alts map */
if (badok)
return(FAILURE);
perror("Unable to read alternate sector partition map: ");
exit(59);
}
/* transform the disk image bit-map to incore char map */
expand_map();
ap->ap_ent_secsiz = 0;
} else {
if (badok)
return(FAILURE);
"Unable to malloc alternate sector entry table.\n");
exit(60);
}
if (badok)
return(FAILURE);
perror("Unable to read alternate sector entry table: ");
exit(61);
}
}
return(SUCCESS);
}
/*
* update the new alternate partition tables on disk
*/
void
wr_altsctr(void)
{
return;
perror("Unable to write alternate sector partition: ");
exit(62);
}
perror("Unable to write alternate sector partition map: ");
exit(63);
}
perror("Unable to write alternate sector entry table: ");
exit(64);
}
}
}
/*
* get a list of bad sector
*/
static void
get_badsec(void)
{
int cnt;
int i;
cnt = count_badsec();
if (!cnt) {
ap->ap_gbadcnt = 0;
} else {
"Ignoring bad sector %ld which is in first"
" track of the drive.\n", curbad);
continue;
}
"Ignoring bad sector %ld which is past"
" the end of the drive.\n", curbad);
continue;
}
cnt++;
}
}
}
}
/*
* count number of bad sector on list
* merging the bad sector list from surface analysis and the
* one given through the command line
*/
static int
count_badsec(void)
{
if (!badsl_chain)
else {
;
}
return(badsl_chain_cnt);
}
/*
* generate alternate entry table by merging the existing and
* the new entry list.
*/
static void
gen_alts_ent(void)
{
if (ap->ap_gbadcnt == 0)
return;
ap->ap_gbadcnt = 0;
/* assign alternate sectors to the bad sectors */
/* allocate the alts_entry on disk skipping possible bad sectors */
perror("Unable to allocate alternate entry table on disk: ");
exit(65);
}
}
/*
* assign alternate sectors for bad sector mapping
*/
static void
assign_altsctr(void)
{
uint_t i;
uint_t j;
continue;
continue;
alts_ind =
"Unable to allocate alternates for bad starting"
exit(65);
}
for (j = 0; j < cluster; j++) {
}
}
}
/*
* transform the disk image alts bit map to incore char map
*/
static void
expand_map(void)
{
int i;
}
}
/*
* transform the incore alts char map to the disk image bit map
*/
static void
compress_map(void)
{
int i;
int bytesz;
char mask = 0;
int maplen = 0;
if (bytesz < 0) {
bytesz = 7;
mask = 0;
}
}
/*
* if partition size != multiple number of bytes
* then record the last partial byte
*/
if (bytesz != 7)
}
/*
* given a bad sector number, search in the alts bit map
* and identify the sector as good or bad
*/
static int
{
return(ALTS_BAD);
return(ALTS_GOOD);
}
/*
* allocate a range of sectors from the alternate partition
*/
static blkaddr_t
{
blkaddr_t i;
total = 0;
continue;
}
total++;
return(first_ind);
}
return(0);
}
/*
* bubble sort the entry table into ascending order
*/
static void
{
int flag;
int i, j;
for (i = 0; i < cnt-1; i++) {
flag = 1;
for (j = cnt-1; j > i; j--) {
} else {
flag = 0;
}
}
if (flag) break;
}
}
/*
* compress all the contiguous bad sectors into a single entry
* in the entry table. The entry table must be sorted into ascending
* before the compression.
*/
static void
{
int keyp;
int movp;
int i;
for (i = 0; i < cnt; i++) {
continue;
continue;
break;
}
}
}
/*
* merging two entry tables into a single table. In addition,
* all empty slots in the entry table will be removed.
*/
static int
int lcnt1,
int lcnt2)
{
int i;
j1++;
continue;
}
j2++;
continue;
}
else
}
continue;
}
continue;
}
return (i);
}
/*
* binary search for bad sector in the alternate entry table
*/
static int
{
int i;
int ind;
int interval;
if (!cnt)
return(mystatus);
ind = i;
/*
printf("ind= %d, intv= %d; ",ind, interval);
*/
} else {
interval >>= 1;
if (!interval) break;
} else {
/* if key is larger than the last element then break */
}
}
}
return(mystatus);
}
/*
* check for bad sector in assigned alternate sectors
*/
static int
{
int i;
/*
* blkaddr_t intv[3];
*/
for (i = 0; i < cnt; i++) {
" alternate sector.\n", badsec);
exit(66);
/*
* if (!numsec) {
* (ap->ap_entp)[i].good_start = 0;
* return (FAILURE);
* }
* intv[0] = badsec - (ap->ap_entp)[i].good_start;
* intv[1] = 1;
* intv[2] = (ap->ap_entp)[i].good_start + numsec - badsec;
*/
}
}
/* the bad sector has already been identified as bad */
return(SUCCESS);
}
/*
* print_altsec () -- print alternate sector information
*/
int
{
read_altsctr(part, 0);
return(SUCCESS);
}
static void
print_altsctr(void)
{
int i;
int totalloc;
int avail;
/* find # of available alternate sectors */
if ((ap->ap_memmapp)[i])
totalloc++;
}
/*
* - partition table - partition map
* - entry table
*/
- ap->ap_map_sectot;
(void) printf("\nBad Sector Start\tAlternate Sector Start\t\tCount\n");
(void) printf("\t%u\t ->\t\t%u\t\t\t %u\n",
}
(void) printf("\n %d alternate sector(s) left for allocation.\n",
avail);
}
static int
{
int rc;
return(FAILURE);
switch (ioflag)
{
case CMD_READ:
break;
case CMD_WRITE:
break;
default:
break;
}
if (rc == -1)
return(FAILURE);
return(SUCCESS);
}