summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_dkbad.c
blob: 0336795a4a6acfc164f956644cb151d5f4000fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*-
 * Copyright (c) 1994 Bruce D. Evans.
 * All rights reserved.
 *
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * William Jolitz.
 *
 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: @(#)wd.c	7.2 (Berkeley) 5/9/91
 *	from: wd.c,v 1.55 1994/10/22 01:57:12 phk Exp $
 *	from: @(#)ufs_disksubr.c	7.16 (Berkeley) 5/4/91
 *	from: ufs_disksubr.c,v 1.8 1994/06/07 01:21:39 phk Exp $
 *	$FreeBSD$
 */

#include <sys/param.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/disklabel.h>
#include <sys/dkbad.h>
#include <sys/malloc.h>

/*
 * Internalize the bad sector table.
 * TODO:
 *	o Fix types.
 *	  Type long should be daddr_t since we compare with blkno's.
 *	  Sentinel -1 should be ((daddr_t)-1).
 *	o Can remove explicit test for sentinel if it is a positive
 *	  (unsigned or not) value larger than all possible blkno's.
 *	o Check that the table is sorted.
 *	o Use faster searches.
 *	o Use the internal table in wddump().
 *	o Don't duplicate so much code.
 *	o Do all bad block handing in a driver-independent file.
 *	o Remove limit of 126 spare sectors.
 */
struct dkbad_intern *
internbad144(btp, lp)
	struct dkbad *btp;
	struct disklabel *lp;
{
	struct dkbad_intern *bip;
	int i;

	bip = malloc(sizeof *bip, M_DEVBUF, M_WAITOK);
	/*
	 * Spare sectors are allocated beginning with the last sector of
	 * the second last track of the disk (the last track is used for
	 * the bad sector list).
	 */
	bip->bi_maxspare = lp->d_secperunit - lp->d_nsectors - 1;
	bip->bi_nbad = DKBAD_MAXBAD;
	i = 0;
	for (; i < DKBAD_MAXBAD && btp->bt_bad[i].bt_cyl != DKBAD_NOCYL; i++)
		bip->bi_bad[i] = btp->bt_bad[i].bt_cyl * lp->d_secpercyl
				 + (btp->bt_bad[i].bt_trksec >> 8)
				   * lp->d_nsectors
				 + (btp->bt_bad[i].bt_trksec & 0x00ff);
	bip->bi_bad[i] = -1;
	return (bip);
}

char *
readbad144(dev, strat, lp, bdp)
	dev_t	dev;
	d_strategy_t *strat;
	struct disklabel *lp;
	struct dkbad *bdp;
{
	struct buf *bp;
	struct dkbad *db;
	int	i;
	char	*msg;

	bp = geteblk((int)lp->d_secsize);
	i = 0;
	do {
		/* Read a bad sector table. */
		bp->b_dev = dev;
		bp->b_blkno = lp->d_secperunit - lp->d_nsectors + i;
		if (lp->d_secsize > DEV_BSIZE)
			bp->b_blkno *= lp->d_secsize / DEV_BSIZE;
		else
			bp->b_blkno /= DEV_BSIZE / lp->d_secsize;
		bp->b_bcount = lp->d_secsize;
		bp->b_flags = B_BUSY | B_READ;
		(*strat)(bp);

		/* If successful, validate, otherwise try another. */
		if (biowait(bp) == 0) {
			db = (struct dkbad *)(bp->b_un.b_addr);
			if (db->bt_mbz == 0 && db->bt_flag == DKBAD_MAGIC) {
				msg = NULL;
				*bdp = *db;
				break;
			}
			msg = "bad sector table corrupted";
		} else
			msg = "bad sector table I/O error";
	} while ((bp->b_flags & B_ERROR) && (i += 2) < 10 &&
		 i < lp->d_nsectors);
	bp->b_flags |= B_INVAL | B_AGE;
	brelse(bp);
	return (msg);
}

daddr_t
transbad144(bip, blkno)
	struct dkbad_intern *bip;
	daddr_t	blkno;
{
	int	i;

	/*
	 * List is sorted, so the search can terminate when it is past our
	 * sector.
	 */
	for (i = 0; bip->bi_bad[i] != -1 && bip->bi_bad[i] <= blkno; i++)
		if (bip->bi_bad[i] == blkno)
			/*
			 * Spare sectors are allocated in decreasing order.
			 */
			return (bip->bi_maxspare - i);
	return (blkno);
}
OpenPOWER on IntegriCloud