sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / cmd / addbadsec / ix_altsctr.c
blobd4daa02698e3e25c1d8a6b30c5b0463e4e6494c7
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
26 * copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T
27 * All rights reserved.
31 * Copyrighted as an unpublished work.
32 * (c) Copyright INTERACTIVE Systems Corporation 1986, 1988, 1990
33 * All rights reserved.
36 #include <sys/types.h>
37 #include <ctype.h>
38 #include <fcntl.h>
39 #include <malloc.h>
40 #include <sys/stat.h>
41 #include <sys/swap.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 #include <sys/vtoc.h>
47 #include <sys/param.h>
48 #include <sys/dkio.h>
49 #include <sys/dktp/altsctr.h>
50 #include <sys/dktp/fdisk.h>
51 #include "badsec.h"
53 #define FAILURE 0
54 #define SUCCESS 1
56 #define CMD_READ 0
57 #define CMD_WRITE 1
59 struct badsec_lst *badsl_chain;
60 int badsl_chain_cnt;
61 struct badsec_lst *gbadsl_chain;
62 int gbadsl_chain_cnt;
64 extern struct dk_geom dkg;
65 extern int alts_fd;
67 struct alts_mempart alts_part = { 0, NULL, 0 };
68 struct alts_mempart *ap = &alts_part; /* pointer to incore alts tables */
70 static void read_altsctr(struct extpartition *part, int badok);
71 static void chk_badsec(void);
72 static void init_altsctr(void);
73 void wr_altsctr(void);
74 static void get_badsec(void);
75 static int count_badsec(void);
76 static void gen_alts_ent(void);
77 static void assign_altsctr(void);
78 static void expand_map(void);
79 static void compress_map(void);
80 static int altsmap_getbit(blkaddr_t badsec);
81 static blkaddr_t altsmap_alloc(blkaddr_t srt_ind, blkaddr_t end_ind,
82 int cnt, int dir);
83 static void ent_sort(struct alts_ent buf[], int cnt);
84 static void ent_compress(struct alts_ent buf[], int cnt);
85 static int ent_merge(
86 struct alts_ent buf[],
87 struct alts_ent list1[],
88 int lcnt1,
89 struct alts_ent list2[],
90 int lcnt2);
91 static int ent_bsearch(struct alts_ent buf[], int cnt, struct alts_ent *key);
92 static int chk_bad_altsctr(blkaddr_t badsec);
93 int print_altsec(struct extpartition *part);
94 int get_altsctr(int);
95 static void print_altsctr(void);
96 static int absdsk_io(int fd, uint_t srtsec, char *bufp, uint_t len,
97 int ioflag);
100 * updatebadsec () -- update bad sector/track mapping tables
103 updatebadsec(struct extpartition *part, int init_flag)
105 if (init_flag)
106 ap->ap_flag |= ALTS_ADDPART;
107 get_badsec();
108 read_altsctr(part, 1);
109 ent_sort(ap->ap_gbadp, ap->ap_gbadcnt);
110 ent_compress(ap->ap_gbadp, ap->ap_gbadcnt);
111 gen_alts_ent();
112 compress_map();
113 return (SUCCESS);
117 * read_altsctr( ptr to alternate sector partition )
118 * -- read the alternate sector partition tables
120 static void
121 read_altsctr(struct extpartition *part, int badok)
123 if (ap->ap_tblp == NULL) {
124 /* allocate buffer for the alts partition table (sector size) */
125 ap->ap_tbl_secsiz = byte_to_secsiz(ALTS_PARTTBL_SIZE, NBPSCTR);
126 ap->ap_tblp = (struct alts_parttbl *)malloc(ap->ap_tbl_secsiz);
127 if (ap->ap_tblp == NULL) {
128 (void) fprintf(stderr,
129 "Unable to malloc alternate partition table.\n");
130 exit(50);
133 /* allocate buffer for the alts partition map (sector size) */
134 /* buffers include the disk image bit map */
135 /* and the incore transformed char map */
137 if ((ap->ap_memmapp = (uchar_t *)malloc(part->p_size)) == NULL) {
138 (void) fprintf(stderr,
139 "Unable to malloc incore alternate partition map.\n");
140 exit(51);
142 ap->ap_tblp->alts_map_len = (part->p_size + 8 - 1) / 8;
143 ap->ap_map_secsiz = byte_to_secsiz(ap->ap_tblp->alts_map_len,
144 NBPSCTR);
145 ap->ap_map_sectot = ap->ap_map_secsiz / NBPSCTR;
146 if ((ap->ap_mapp = (uchar_t *)malloc(ap->ap_map_secsiz)) == NULL) {
147 (void) fprintf(stderr,
148 "Unable to malloc alternate partition map.\n");
149 exit(52);
151 /* clear the buffers to zero */
152 (void) memset(ap->ap_memmapp, 0, part->p_size);
153 (void) memset(ap->ap_mapp, 0, ap->ap_map_secsiz);
154 ap->part = *part; /* struct copy */
157 * if add alternate partition flag is set, then install the partition
158 * otherwise read the alts partition info from disk
159 * if failed, then assume the first installation
161 if (ap->ap_flag & ALTS_ADDPART) {
162 (void) fprintf(stderr,
163 "WARNING: Manually initializing alternate table.\n");
164 init_altsctr();
165 } else {
166 if (get_altsctr(badok) == SUCCESS)
167 chk_badsec();
168 else
169 init_altsctr();
176 * checking duplicate bad sectors or bad sectors in ALTSCTR partition
178 static void
179 chk_badsec(void)
181 blkaddr_t badsec;
182 blkaddr_t altsp_srtsec = ap->part.p_start;
183 blkaddr_t altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
184 int cnt;
185 int status;
187 for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
188 badsec = (ap->ap_gbadp)[cnt].bad_start;
190 /* if bad sector is within the ATLSCTR partition */
191 if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
192 if ((ap->ap_memmapp)[badsec - altsp_srtsec] != ALTS_BAD) {
193 if ((badsec >= altsp_srtsec) && (badsec <= (altsp_srtsec +
194 ap->ap_tbl_secsiz / NBPSCTR - 1))) {
195 (void) fprintf(stderr,
196 "Alternate partition information table is bad.\n");
197 exit(53);
199 if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_map_base) &&
200 (badsec <= (altsp_srtsec + ap->ap_tblp->alts_map_base +
201 ap->ap_map_sectot - 1))) {
202 (void) fprintf(stderr,
203 "Alternate partition map is bad.\n");
204 exit(54);
206 if ((badsec >= altsp_srtsec+ap->ap_tblp->alts_ent_base) &&
207 (badsec <= (altsp_srtsec + ap->ap_tblp->alts_ent_base +
208 ap->ap_ent_secsiz / NBPSCTR - 1))) {
209 (void) fprintf(stderr,
210 "Alternate partition entry table is bad.\n");
211 exit(55);
213 (ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
214 (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
215 } else {
216 status = chk_bad_altsctr(badsec);
217 (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
219 } else {
221 * binary search for bad sector in the alts entry table
223 status = ent_bsearch(ap->ap_entp, ap->ap_tblp->alts_ent_used,
224 &((ap->ap_gbadp)[cnt]));
226 * if the bad sector had already been remapped(found in alts_entry)
227 * then ignore the bad sector
229 if (status != -1) {
230 (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
238 * initialize the alternate partition tables
240 static void
241 init_altsctr(void)
243 blkaddr_t badsec;
244 blkaddr_t altsp_srtsec = ap->part.p_start;
245 blkaddr_t altsp_endsec = ap->part.p_start + ap->part.p_size - 1;
246 int cnt;
248 ap->ap_entp = NULL;
249 ap->ap_ent_secsiz = 0;
250 ap->ap_tblp->alts_sanity = ALTS_SANITY;
251 ap->ap_tblp->alts_version = ALTS_VERSION1;
252 ap->ap_tblp->alts_map_len = (ap->part.p_size + 8 - 1) / 8;
253 ap->ap_tblp->alts_ent_used = 0;
254 ap->ap_tblp->alts_ent_base = 0;
255 ap->ap_tblp->alts_ent_end = 0;
256 ap->ap_tblp->alts_resv_base = ap->part.p_size - 1;
257 for (cnt = 0; cnt < 5; cnt++)
258 ap->ap_tblp->alts_pad[cnt] = 0;
260 for (cnt = 0; cnt < ap->ap_gbadcnt; cnt++) {
261 badsec = (ap->ap_gbadp)[cnt].bad_start;
262 if ((badsec >= altsp_srtsec) && (badsec <= altsp_endsec)) {
263 if (badsec == altsp_srtsec) {
264 (void) fprintf(stderr,
265 "First sector of alternate partition is bad.\n");
266 exit(56);
268 (ap->ap_memmapp)[badsec - altsp_srtsec] = ALTS_BAD;
269 (ap->ap_gbadp)[cnt].bad_start = (uint32_t)ALTS_ENT_EMPTY;
273 /* allocate the alts_map on disk skipping possible bad sectors */
274 ap->ap_tblp->alts_map_base =
275 altsmap_alloc(ap->ap_tbl_secsiz / NBPSCTR,
276 ap->part.p_size, ap->ap_map_sectot, ALTS_MAP_UP);
277 if (ap->ap_tblp->alts_map_base == 0) {
278 perror("Unable to allocate alternate map on disk: ");
279 exit(57);
286 * read the alternate partition tables from disk
289 get_altsctr(badok)
290 int badok;
292 /* get alts partition table info */
293 if (absdsk_io(alts_fd, 0, (char *)ap->ap_tblp,
294 ap->ap_tbl_secsiz, CMD_READ) == FAILURE) {
295 if (badok)
296 return(FAILURE);
297 perror("Unable to read alternate sector partition: ");
298 exit(58);
300 if (ap->ap_tblp->alts_sanity != ALTS_SANITY) {
301 if (badok)
302 return(FAILURE);
303 (void) fprintf(stderr, "Bad alternate sector magic number.\n");
304 exit(69);
307 /* get the alts map */
308 if (absdsk_io(alts_fd, ap->ap_tblp->alts_map_base,
309 (char *)ap->ap_mapp, ap->ap_map_secsiz, CMD_READ) == FAILURE) {
310 if (badok)
311 return(FAILURE);
312 perror("Unable to read alternate sector partition map: ");
313 exit(59);
316 /* transform the disk image bit-map to incore char map */
317 expand_map();
319 if (ap->ap_tblp->alts_ent_used == 0) {
320 ap->ap_entp = NULL;
321 ap->ap_ent_secsiz = 0;
322 } else {
323 ap->ap_ent_secsiz = byte_to_secsiz(
324 (ap->ap_tblp->alts_ent_used*ALTS_ENT_SIZE),NBPSCTR);
325 if ((ap->ap_entp =
326 (struct alts_ent *)malloc(ap->ap_ent_secsiz)) == NULL) {
327 if (badok)
328 return(FAILURE);
329 (void) fprintf(stderr,
330 "Unable to malloc alternate sector entry table.\n");
331 exit(60);
334 if (absdsk_io(alts_fd, ap->ap_tblp->alts_ent_base,
335 (char *)ap->ap_entp, ap->ap_ent_secsiz,
336 CMD_READ) ==FAILURE){
337 if (badok)
338 return(FAILURE);
339 perror("Unable to read alternate sector entry table: ");
340 exit(61);
343 return(SUCCESS);
348 * update the new alternate partition tables on disk
350 void
351 wr_altsctr(void)
353 if (ap->ap_tblp == NULL)
354 return;
355 if (absdsk_io(alts_fd, 0, (char *)ap->ap_tblp,
356 ap->ap_tbl_secsiz, CMD_WRITE) == FAILURE) {
357 perror("Unable to write alternate sector partition: ");
358 exit(62);
361 if (absdsk_io(alts_fd, ap->ap_tblp->alts_map_base,
362 (char *)ap->ap_mapp, ap->ap_map_secsiz, CMD_WRITE) == FAILURE) {
363 perror("Unable to write alternate sector partition map: ");
364 exit(63);
367 if (ap->ap_tblp->alts_ent_used != 0) {
368 if (absdsk_io(alts_fd, ap->ap_tblp->alts_ent_base,
369 (char *)ap->ap_entp, ap->ap_ent_secsiz,
370 CMD_WRITE) == FAILURE) {
371 perror("Unable to write alternate sector entry table: ");
372 exit(64);
379 * get a list of bad sector
381 static void
382 get_badsec(void)
384 int cnt;
385 struct badsec_lst *blc_p;
386 blkaddr_t curbad;
387 blkaddr_t maxsec = (blkaddr_t)dkg.dkg_nhead *
388 dkg.dkg_ncyl * dkg.dkg_nsect;
389 struct alts_ent *growbadp;
390 int i;
392 cnt = count_badsec();
393 if (!cnt) {
394 ap->ap_gbadp = NULL;
395 ap->ap_gbadcnt = 0;
396 } else {
397 ap->ap_gbadp = (struct alts_ent *)malloc(cnt*ALTS_ENT_SIZE);
398 (void) memset(ap->ap_gbadp,0,cnt*ALTS_ENT_SIZE);
400 for (growbadp = ap->ap_gbadp, cnt=0, blc_p=badsl_chain;
401 blc_p; blc_p=blc_p->bl_nxt) {
402 for (i=0; i<blc_p->bl_cnt; i++) {
403 curbad = blc_p->bl_sec[i];
404 if (curbad < (blkaddr_t)dkg.dkg_nsect) {
405 (void) fprintf(stderr,
406 "Ignoring bad sector %ld which is in first"
407 " track of the drive.\n", curbad);
408 continue;
410 if (curbad >= maxsec) {
411 (void) fprintf(stderr,
412 "Ignoring bad sector %ld which is past"
413 " the end of the drive.\n", curbad);
414 continue;
416 growbadp[cnt].bad_start = curbad;
417 growbadp[cnt].bad_end = curbad;
418 cnt++;
422 ap->ap_gbadcnt = cnt;
426 * count number of bad sector on list
427 * merging the bad sector list from surface analysis and the
428 * one given through the command line
430 static int
431 count_badsec(void)
434 struct badsec_lst *blc_p;
436 if (!badsl_chain)
437 badsl_chain = gbadsl_chain;
438 else {
439 for (blc_p = badsl_chain; blc_p->bl_nxt; blc_p = blc_p->bl_nxt)
441 blc_p->bl_nxt = gbadsl_chain;
444 badsl_chain_cnt += gbadsl_chain_cnt;
445 return(badsl_chain_cnt);
450 * generate alternate entry table by merging the existing and
451 * the new entry list.
453 static void
454 gen_alts_ent(void)
456 uint_t ent_used;
457 struct alts_ent *entp;
459 if (ap->ap_gbadcnt == 0)
460 return;
462 ent_used = ap->ap_tblp->alts_ent_used + ap->ap_gbadcnt;
463 ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE,NBPSCTR);
464 entp=(struct alts_ent *) malloc (ap->ap_ent_secsiz);
465 ent_used = ent_merge(entp, ap->ap_entp, ap->ap_tblp->alts_ent_used,
466 ap->ap_gbadp, ap->ap_gbadcnt);
467 free(ap->ap_entp);
468 free(ap->ap_gbadp);
469 ap->ap_entp = entp;
470 ap->ap_ent_secsiz = byte_to_secsiz(ent_used*ALTS_ENT_SIZE, NBPSCTR);
471 ap->ap_tblp->alts_ent_used = ent_used;
472 ap->ap_gbadp = NULL;
473 ap->ap_gbadcnt = 0;
475 /* assign alternate sectors to the bad sectors */
476 assign_altsctr();
478 /* allocate the alts_entry on disk skipping possible bad sectors */
479 ap->ap_tblp->alts_ent_base =
480 altsmap_alloc(ap->ap_tblp->alts_map_base + ap->ap_map_sectot,
481 ap->part.p_size,
482 ap->ap_ent_secsiz / NBPSCTR, ALTS_MAP_UP);
483 if (ap->ap_tblp->alts_ent_base == 0) {
484 perror("Unable to allocate alternate entry table on disk: ");
485 exit(65);
488 ap->ap_tblp->alts_ent_end = ap->ap_tblp->alts_ent_base +
489 (ap->ap_ent_secsiz / NBPSCTR) - 1;
494 * assign alternate sectors for bad sector mapping
496 static void
497 assign_altsctr(void)
499 uint_t i;
500 uint_t j;
501 blkaddr_t alts_ind;
502 uint_t cluster;
504 for (i = 0; i < ap->ap_tblp->alts_ent_used; i++) {
505 if ((ap->ap_entp)[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
506 continue;
507 if ((ap->ap_entp)[i].good_start != 0)
508 continue;
509 cluster = (ap->ap_entp)[i].bad_end-(ap->ap_entp)[i].bad_start +1;
510 alts_ind =
511 altsmap_alloc(ap->part.p_size-1, ap->ap_tblp->alts_map_base +
512 ap->ap_map_sectot - 1, cluster, ALTS_MAP_DOWN);
513 if (alts_ind == 0) {
514 (void) fprintf(stderr,
515 "Unable to allocate alternates for bad starting"
516 " sector %u.\n", (ap->ap_entp)[i].bad_start);
517 exit(65);
519 alts_ind = alts_ind - cluster + 1;
520 (ap->ap_entp)[i].good_start = alts_ind + ap->part.p_start;
521 for (j = 0; j < cluster; j++) {
522 (ap->ap_memmapp)[alts_ind+j] = ALTS_BAD;
530 * transform the disk image alts bit map to incore char map
532 static void
533 expand_map(void)
535 int i;
537 for (i = 0; i < ap->part.p_size; i++) {
538 (ap->ap_memmapp)[i] = altsmap_getbit(i);
543 * transform the incore alts char map to the disk image bit map
545 static void
546 compress_map(void)
549 int i;
550 int bytesz;
551 char mask = 0;
552 int maplen = 0;
554 for (i = 0, bytesz = 7; i < ap->part.p_size; i++) {
555 mask |= ((ap->ap_memmapp)[i] << bytesz--);
556 if (bytesz < 0) {
557 (ap->ap_mapp)[maplen++] = mask;
558 bytesz = 7;
559 mask = 0;
563 * if partition size != multiple number of bytes
564 * then record the last partial byte
566 if (bytesz != 7)
567 (ap->ap_mapp)[maplen] = mask;
572 * given a bad sector number, search in the alts bit map
573 * and identify the sector as good or bad
575 static int
576 altsmap_getbit(blkaddr_t badsec)
578 uint_t slot = badsec / 8;
579 uint_t field = badsec % 8;
580 uchar_t mask;
582 mask = ALTS_BAD<<7;
583 mask >>= field;
584 if ((ap->ap_mapp)[slot] & mask)
585 return(ALTS_BAD);
586 return(ALTS_GOOD);
591 * allocate a range of sectors from the alternate partition
593 static blkaddr_t
594 altsmap_alloc(blkaddr_t srt_ind, blkaddr_t end_ind, int cnt, int dir)
596 blkaddr_t i;
597 blkaddr_t total;
598 blkaddr_t first_ind;
600 for (i = srt_ind, first_ind = srt_ind, total = 0;
601 i != end_ind; i += dir) {
602 if ((ap->ap_memmapp)[i] == ALTS_BAD) {
603 total = 0;
604 first_ind = i + dir;
605 continue;
607 total++;
608 if (total == cnt)
609 return(first_ind);
612 return(0);
618 * bubble sort the entry table into ascending order
620 static void
621 ent_sort(struct alts_ent buf[], int cnt)
623 struct alts_ent temp;
624 int flag;
625 int i, j;
627 for (i = 0; i < cnt-1; i++) {
628 temp = buf[cnt-1];
629 flag = 1;
631 for (j = cnt-1; j > i; j--) {
632 if (buf[j-1].bad_start < temp.bad_start) {
633 buf[j] = temp;
634 temp = buf[j-1];
635 } else {
636 buf[j] = buf[j-1];
637 flag = 0;
640 buf[i] = temp;
641 if (flag) break;
648 * compress all the contiguous bad sectors into a single entry
649 * in the entry table. The entry table must be sorted into ascending
650 * before the compression.
652 static void
653 ent_compress(struct alts_ent buf[], int cnt)
655 int keyp;
656 int movp;
657 int i;
659 for (i = 0; i < cnt; i++) {
660 if (buf[i].bad_start == (uint32_t)ALTS_ENT_EMPTY)
661 continue;
662 for (keyp = i, movp = i+1; movp < cnt; movp++) {
663 if (buf[movp].bad_start == (uint32_t)ALTS_ENT_EMPTY)
664 continue;
665 if (buf[keyp].bad_end+1 != buf[movp].bad_start)
666 break;
667 buf[keyp].bad_end++;
668 buf[movp].bad_start = (uint32_t)ALTS_ENT_EMPTY;
670 if (movp == cnt) break;
676 * merging two entry tables into a single table. In addition,
677 * all empty slots in the entry table will be removed.
679 static int
680 ent_merge(
681 struct alts_ent buf[],
682 struct alts_ent list1[],
683 int lcnt1,
684 struct alts_ent list2[],
685 int lcnt2)
687 int i;
688 int j1, j2;
690 for (i = 0, j1 = 0, j2 = 0; j1 < lcnt1 && j2 < lcnt2; ) {
691 if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
692 j1++;
693 continue;
695 if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY) {
696 j2++;
697 continue;
699 if (list1[j1].bad_start < list2[j2].bad_start)
700 buf[i++] = list1[j1++];
701 else
702 buf[i++] = list2[j2++];
704 for (; j1 < lcnt1; j1++) {
705 if (list1[j1].bad_start == (uint32_t)ALTS_ENT_EMPTY)
706 continue;
707 buf[i++] = list1[j1];
709 for (; j2 < lcnt2; j2++) {
710 if (list2[j2].bad_start == (uint32_t)ALTS_ENT_EMPTY)
711 continue;
712 buf[i++] = list2[j2];
714 return (i);
719 * binary search for bad sector in the alternate entry table
721 static int
722 ent_bsearch(struct alts_ent buf[], int cnt, struct alts_ent *key)
724 int i;
725 int ind;
726 int interval;
727 int mystatus = -1;
729 if (!cnt)
730 return(mystatus);
732 for (i = 1; i <= cnt; i <<= 1)
733 ind = i;
735 for (interval = ind; interval; ) {
737 printf("ind= %d, intv= %d; ",ind, interval);
739 if ((key->bad_start >= buf[ind-1].bad_start) &&
740 (key->bad_start <= buf[ind-1].bad_end)) {
741 return(mystatus = ind-1);
742 } else {
743 interval >>= 1;
744 if (!interval) break;
745 if (key->bad_start < buf[ind-1].bad_start) {
746 ind = ind - interval;
747 } else {
748 /* if key is larger than the last element then break */
749 if (ind == cnt) break;
750 if ((ind+interval) <= cnt)
751 ind += interval;
755 return(mystatus);
759 * check for bad sector in assigned alternate sectors
761 static int
762 chk_bad_altsctr(blkaddr_t badsec)
764 int i;
765 blkaddr_t numsec;
766 int cnt = ap->ap_tblp->alts_ent_used;
768 * blkaddr_t intv[3];
771 for (i = 0; i < cnt; i++) {
772 numsec = (ap->ap_entp)[i].bad_end - (ap->ap_entp)[i].bad_start;
773 if ((badsec >= (ap->ap_entp)[i].good_start) &&
774 (badsec <= ((ap->ap_entp)[i].good_start + numsec))) {
775 (void) fprintf(stderr, "Bad sector %ld is an assigned"
776 " alternate sector.\n", badsec);
777 exit(66);
779 * if (!numsec) {
780 * (ap->ap_entp)[i].good_start = 0;
781 * return (FAILURE);
783 * intv[0] = badsec - (ap->ap_entp)[i].good_start;
784 * intv[1] = 1;
785 * intv[2] = (ap->ap_entp)[i].good_start + numsec - badsec;
789 /* the bad sector has already been identified as bad */
790 return(SUCCESS);
795 * print_altsec () -- print alternate sector information
798 print_altsec(struct extpartition *part)
800 ap->ap_tblp = NULL;
801 ap->ap_flag &= ~ALTS_ADDPART;
802 read_altsctr(part, 0);
803 print_altsctr();
804 return(SUCCESS);
807 static void
808 print_altsctr(void)
810 int i;
811 int totalloc;
812 int avail;
814 /* find # of available alternate sectors */
815 for (i=0, totalloc=0; i<ap->part.p_size; i++) {
816 if ((ap->ap_memmapp)[i])
817 totalloc++;
820 * available = size of partition - allocated sectors/bad sectors
821 * - partition table - partition map
822 * - entry table
824 avail = ap->part.p_size - totalloc;
825 avail = avail - (ap->ap_tbl_secsiz/NBPSCTR)
826 - ap->ap_map_sectot;
827 avail = avail-(ap->ap_tblp->alts_ent_end -ap->ap_tblp->alts_ent_base+1);
828 if (avail < 0) avail = 0;
830 (void) printf("\nALTERNATE SECTOR/TRACK MAPPING TABLE:\n");
831 (void) printf("\nBad Sector Start\tAlternate Sector Start\t\tCount\n");
833 for (i=0; i<ap->ap_tblp->alts_ent_used; i++) {
834 (void) printf("\t%u\t ->\t\t%u\t\t\t %u\n",
835 (ap->ap_entp)[i].bad_start,
836 (ap->ap_entp)[i].good_start,
837 ((ap->ap_entp)[i].bad_end - (ap->ap_entp)[i].bad_start + 1));
839 (void) printf("\n %d alternate sector(s) left for allocation.\n",
840 avail);
844 static int
845 absdsk_io(int fd, uint_t srtsec, char *bufp, uint_t len, int ioflag)
847 int rc;
849 if (llseek (fd, (offset_t)srtsec * NBPSCTR, SEEK_SET) == -1)
850 return(FAILURE);
851 switch (ioflag)
853 case CMD_READ:
854 rc = read (fd, bufp, len);
855 break;
856 case CMD_WRITE:
857 rc = write (fd, bufp, len);
858 break;
859 default:
860 break;
862 if (rc == -1)
863 return(FAILURE);
864 return(SUCCESS);