subr_disk.c revision 1.11 1 1.11 mycroft /*
2 1.11 mycroft * Copyright (c) 1982, 1986, 1988, 1993
3 1.11 mycroft * The Regents of the University of California. All rights reserved.
4 1.11 mycroft * (c) UNIX System Laboratories, Inc.
5 1.11 mycroft * All or some portions of this file are derived from material licensed
6 1.11 mycroft * to the University of California by American Telephone and Telegraph
7 1.11 mycroft * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.11 mycroft * the permission of UNIX System Laboratories, Inc.
9 1.11 mycroft *
10 1.11 mycroft * Redistribution and use in source and binary forms, with or without
11 1.11 mycroft * modification, are permitted provided that the following conditions
12 1.11 mycroft * are met:
13 1.11 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.11 mycroft * notice, this list of conditions and the following disclaimer.
15 1.11 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.11 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.11 mycroft * documentation and/or other materials provided with the distribution.
18 1.11 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.11 mycroft * must display the following acknowledgement:
20 1.11 mycroft * This product includes software developed by the University of
21 1.11 mycroft * California, Berkeley and its contributors.
22 1.11 mycroft * 4. Neither the name of the University nor the names of its contributors
23 1.11 mycroft * may be used to endorse or promote products derived from this software
24 1.11 mycroft * without specific prior written permission.
25 1.11 mycroft *
26 1.11 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.11 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.11 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.11 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.11 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.11 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.11 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.11 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.11 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.11 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.11 mycroft * SUCH DAMAGE.
37 1.11 mycroft *
38 1.11 mycroft * from: @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
39 1.11 mycroft * $Id: subr_disk.c,v 1.11 1994/05/19 03:43:13 mycroft Exp $
40 1.11 mycroft */
41 1.11 mycroft
42 1.11 mycroft #include <sys/param.h>
43 1.11 mycroft #include <sys/systm.h>
44 1.11 mycroft #include <sys/buf.h>
45 1.11 mycroft #include <sys/disklabel.h>
46 1.11 mycroft #include <sys/syslog.h>
47 1.11 mycroft
48 1.11 mycroft /*
49 1.11 mycroft * Seek sort for disks. We depend on the driver which calls us using b_resid
50 1.11 mycroft * as the current cylinder number.
51 1.11 mycroft *
52 1.11 mycroft * The argument ap structure holds a b_actf activity chain pointer on which we
53 1.11 mycroft * keep two queues, sorted in ascending cylinder order. The first queue holds
54 1.11 mycroft * those requests which are positioned after the current cylinder (in the first
55 1.11 mycroft * request); the second holds requests which came in after their cylinder number
56 1.11 mycroft * was passed. Thus we implement a one way scan, retracting after reaching the
57 1.11 mycroft * end of the drive to the first request on the second queue, at which time it
58 1.11 mycroft * becomes the first queue.
59 1.11 mycroft *
60 1.11 mycroft * A one-way scan is natural because of the way UNIX read-ahead blocks are
61 1.11 mycroft * allocated.
62 1.11 mycroft */
63 1.11 mycroft
64 1.11 mycroft /*
65 1.11 mycroft * For portability with historic industry practice, the
66 1.11 mycroft * cylinder number has to be maintained in the `b_resid'
67 1.11 mycroft * field.
68 1.11 mycroft */
69 1.11 mycroft #define b_cylinder b_resid
70 1.11 mycroft
71 1.11 mycroft void
72 1.11 mycroft disksort(ap, bp)
73 1.11 mycroft register struct buf *ap, *bp;
74 1.11 mycroft {
75 1.11 mycroft register struct buf *bq;
76 1.11 mycroft
77 1.11 mycroft /* If the queue is empty, then it's easy. */
78 1.11 mycroft if (ap->b_actf == NULL) {
79 1.11 mycroft bp->b_actf = NULL;
80 1.11 mycroft ap->b_actf = bp;
81 1.11 mycroft return;
82 1.11 mycroft }
83 1.11 mycroft
84 1.11 mycroft /*
85 1.11 mycroft * If we lie after the first (currently active) request, then we
86 1.11 mycroft * must locate the second request list and add ourselves to it.
87 1.11 mycroft */
88 1.11 mycroft bq = ap->b_actf;
89 1.11 mycroft if (bp->b_cylinder < bq->b_cylinder) {
90 1.11 mycroft while (bq->b_actf) {
91 1.11 mycroft /*
92 1.11 mycroft * Check for an ``inversion'' in the normally ascending
93 1.11 mycroft * cylinder numbers, indicating the start of the second
94 1.11 mycroft * request list.
95 1.11 mycroft */
96 1.11 mycroft if (bq->b_actf->b_cylinder < bq->b_cylinder) {
97 1.11 mycroft /*
98 1.11 mycroft * Search the second request list for the first
99 1.11 mycroft * request at a larger cylinder number. We go
100 1.11 mycroft * before that; if there is no such request, we
101 1.11 mycroft * go at end.
102 1.11 mycroft */
103 1.11 mycroft do {
104 1.11 mycroft if (bp->b_cylinder <
105 1.11 mycroft bq->b_actf->b_cylinder)
106 1.11 mycroft goto insert;
107 1.11 mycroft if (bp->b_cylinder ==
108 1.11 mycroft bq->b_actf->b_cylinder &&
109 1.11 mycroft bp->b_blkno < bq->b_actf->b_blkno)
110 1.11 mycroft goto insert;
111 1.11 mycroft bq = bq->b_actf;
112 1.11 mycroft } while (bq->b_actf);
113 1.11 mycroft goto insert; /* after last */
114 1.11 mycroft }
115 1.11 mycroft bq = bq->b_actf;
116 1.11 mycroft }
117 1.11 mycroft /*
118 1.11 mycroft * No inversions... we will go after the last, and
119 1.11 mycroft * be the first request in the second request list.
120 1.11 mycroft */
121 1.11 mycroft goto insert;
122 1.11 mycroft }
123 1.11 mycroft /*
124 1.11 mycroft * Request is at/after the current request...
125 1.11 mycroft * sort in the first request list.
126 1.11 mycroft */
127 1.11 mycroft while (bq->b_actf) {
128 1.11 mycroft /*
129 1.11 mycroft * We want to go after the current request if there is an
130 1.11 mycroft * inversion after it (i.e. it is the end of the first
131 1.11 mycroft * request list), or if the next request is a larger cylinder
132 1.11 mycroft * than our request.
133 1.11 mycroft */
134 1.11 mycroft if (bq->b_actf->b_cylinder < bq->b_cylinder ||
135 1.11 mycroft bp->b_cylinder < bq->b_actf->b_cylinder ||
136 1.11 mycroft (bp->b_cylinder == bq->b_actf->b_cylinder &&
137 1.11 mycroft bp->b_blkno < bq->b_actf->b_blkno))
138 1.11 mycroft goto insert;
139 1.11 mycroft bq = bq->b_actf;
140 1.11 mycroft }
141 1.11 mycroft /*
142 1.11 mycroft * Neither a second list nor a larger request... we go at the end of
143 1.11 mycroft * the first list, which is the same as the end of the whole schebang.
144 1.11 mycroft */
145 1.11 mycroft insert: bp->b_actf = bq->b_actf;
146 1.11 mycroft bq->b_actf = bp;
147 1.11 mycroft }
148 1.11 mycroft
149 1.11 mycroft /* encoding of disk minor numbers, should be elsewhere... */
150 1.11 mycroft #define dkunit(dev) (minor(dev) >> 3)
151 1.11 mycroft #define dkpart(dev) (minor(dev) & 07)
152 1.11 mycroft #define dkminor(unit, part) (((unit) << 3) | (part))
153 1.11 mycroft
154 1.11 mycroft /*
155 1.11 mycroft * Compute checksum for disk label.
156 1.11 mycroft */
157 1.11 mycroft u_int
158 1.11 mycroft dkcksum(lp)
159 1.11 mycroft register struct disklabel *lp;
160 1.11 mycroft {
161 1.11 mycroft register u_short *start, *end;
162 1.11 mycroft register u_short sum = 0;
163 1.11 mycroft
164 1.11 mycroft start = (u_short *)lp;
165 1.11 mycroft end = (u_short *)&lp->d_partitions[lp->d_npartitions];
166 1.11 mycroft while (start < end)
167 1.11 mycroft sum ^= *start++;
168 1.11 mycroft return (sum);
169 1.11 mycroft }
170 1.11 mycroft
171 1.11 mycroft /*
172 1.11 mycroft * Disk error is the preface to plaintive error messages
173 1.11 mycroft * about failing disk transfers. It prints messages of the form
174 1.11 mycroft
175 1.11 mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
176 1.11 mycroft
177 1.11 mycroft * if the offset of the error in the transfer and a disk label
178 1.11 mycroft * are both available. blkdone should be -1 if the position of the error
179 1.11 mycroft * is unknown; the disklabel pointer may be null from drivers that have not
180 1.11 mycroft * been converted to use them. The message is printed with printf
181 1.11 mycroft * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
182 1.11 mycroft * The message should be completed (with at least a newline) with printf
183 1.11 mycroft * or addlog, respectively. There is no trailing space.
184 1.11 mycroft */
185 1.11 mycroft void
186 1.11 mycroft diskerr(bp, dname, what, pri, blkdone, lp)
187 1.11 mycroft register struct buf *bp;
188 1.11 mycroft char *dname, *what;
189 1.11 mycroft int pri, blkdone;
190 1.11 mycroft register struct disklabel *lp;
191 1.11 mycroft {
192 1.11 mycroft int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
193 1.11 mycroft register void (*pr) __P((const char *, ...));
194 1.11 mycroft char partname = 'a' + part;
195 1.11 mycroft int sn;
196 1.11 mycroft
197 1.11 mycroft if (pri != LOG_PRINTF) {
198 1.11 mycroft log(pri, "");
199 1.11 mycroft pr = addlog;
200 1.11 mycroft } else
201 1.11 mycroft pr = printf;
202 1.11 mycroft (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
203 1.11 mycroft bp->b_flags & B_READ ? "read" : "writ");
204 1.11 mycroft sn = bp->b_blkno;
205 1.11 mycroft if (bp->b_bcount <= DEV_BSIZE)
206 1.11 mycroft (*pr)("%d", sn);
207 1.11 mycroft else {
208 1.11 mycroft if (blkdone >= 0) {
209 1.11 mycroft sn += blkdone;
210 1.11 mycroft (*pr)("%d of ", sn);
211 1.11 mycroft }
212 1.11 mycroft (*pr)("%d-%d", bp->b_blkno,
213 1.11 mycroft bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
214 1.11 mycroft }
215 1.11 mycroft if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
216 1.11 mycroft #ifdef tahoe
217 1.11 mycroft sn *= DEV_BSIZE / lp->d_secsize; /* XXX */
218 1.11 mycroft #endif
219 1.11 mycroft sn += lp->d_partitions[part].p_offset;
220 1.11 mycroft (*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
221 1.11 mycroft sn / lp->d_secpercyl);
222 1.11 mycroft sn %= lp->d_secpercyl;
223 1.11 mycroft (*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
224 1.11 mycroft }
225 1.11 mycroft }
226