subr_disk.c revision 1.17 1 1.17 christos /* $NetBSD: subr_disk.c,v 1.17 1996/03/16 23:17:08 christos Exp $ */
2 1.12 cgd
3 1.11 mycroft /*
4 1.15 thorpej * Copyright (c) 1995 Jason R. Thorpe. All rights reserved.
5 1.11 mycroft * Copyright (c) 1982, 1986, 1988, 1993
6 1.11 mycroft * The Regents of the University of California. All rights reserved.
7 1.11 mycroft * (c) UNIX System Laboratories, Inc.
8 1.11 mycroft * All or some portions of this file are derived from material licensed
9 1.11 mycroft * to the University of California by American Telephone and Telegraph
10 1.11 mycroft * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 1.11 mycroft * the permission of UNIX System Laboratories, Inc.
12 1.11 mycroft *
13 1.11 mycroft * Redistribution and use in source and binary forms, with or without
14 1.11 mycroft * modification, are permitted provided that the following conditions
15 1.11 mycroft * are met:
16 1.11 mycroft * 1. Redistributions of source code must retain the above copyright
17 1.11 mycroft * notice, this list of conditions and the following disclaimer.
18 1.11 mycroft * 2. Redistributions in binary form must reproduce the above copyright
19 1.11 mycroft * notice, this list of conditions and the following disclaimer in the
20 1.11 mycroft * documentation and/or other materials provided with the distribution.
21 1.11 mycroft * 3. All advertising materials mentioning features or use of this software
22 1.11 mycroft * must display the following acknowledgement:
23 1.11 mycroft * This product includes software developed by the University of
24 1.11 mycroft * California, Berkeley and its contributors.
25 1.11 mycroft * 4. Neither the name of the University nor the names of its contributors
26 1.11 mycroft * may be used to endorse or promote products derived from this software
27 1.11 mycroft * without specific prior written permission.
28 1.11 mycroft *
29 1.11 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.11 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.11 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.11 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.11 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.11 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.11 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.11 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.11 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.11 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.11 mycroft * SUCH DAMAGE.
40 1.11 mycroft *
41 1.12 cgd * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
42 1.11 mycroft */
43 1.11 mycroft
44 1.11 mycroft #include <sys/param.h>
45 1.11 mycroft #include <sys/systm.h>
46 1.15 thorpej #include <sys/kernel.h>
47 1.15 thorpej #include <sys/malloc.h>
48 1.11 mycroft #include <sys/buf.h>
49 1.15 thorpej #include <sys/syslog.h>
50 1.15 thorpej #include <sys/time.h>
51 1.11 mycroft #include <sys/disklabel.h>
52 1.15 thorpej #include <sys/disk.h>
53 1.14 thorpej #include <sys/dkstat.h> /* XXX */
54 1.14 thorpej
55 1.14 thorpej /*
56 1.15 thorpej * A global list of all disks attached to the system. May grow or
57 1.15 thorpej * shrink over time.
58 1.15 thorpej */
59 1.15 thorpej struct disklist_head disklist; /* TAILQ_HEAD */
60 1.15 thorpej int disk_count; /* number of drives in global disklist */
61 1.15 thorpej
62 1.15 thorpej /*
63 1.14 thorpej * Old-style disk instrumentation structures. These will go away
64 1.14 thorpej * someday.
65 1.14 thorpej */
66 1.14 thorpej long dk_seek[DK_NDRIVE];
67 1.14 thorpej long dk_time[DK_NDRIVE];
68 1.14 thorpej long dk_wds[DK_NDRIVE];
69 1.14 thorpej long dk_wpms[DK_NDRIVE];
70 1.14 thorpej long dk_xfer[DK_NDRIVE];
71 1.14 thorpej int dk_busy;
72 1.15 thorpej int dk_ndrive;
73 1.14 thorpej int dkn; /* number of slots filled so far */
74 1.11 mycroft
75 1.11 mycroft /*
76 1.11 mycroft * Seek sort for disks. We depend on the driver which calls us using b_resid
77 1.11 mycroft * as the current cylinder number.
78 1.11 mycroft *
79 1.11 mycroft * The argument ap structure holds a b_actf activity chain pointer on which we
80 1.11 mycroft * keep two queues, sorted in ascending cylinder order. The first queue holds
81 1.11 mycroft * those requests which are positioned after the current cylinder (in the first
82 1.11 mycroft * request); the second holds requests which came in after their cylinder number
83 1.11 mycroft * was passed. Thus we implement a one way scan, retracting after reaching the
84 1.11 mycroft * end of the drive to the first request on the second queue, at which time it
85 1.11 mycroft * becomes the first queue.
86 1.11 mycroft *
87 1.11 mycroft * A one-way scan is natural because of the way UNIX read-ahead blocks are
88 1.11 mycroft * allocated.
89 1.11 mycroft */
90 1.11 mycroft
91 1.11 mycroft void
92 1.11 mycroft disksort(ap, bp)
93 1.11 mycroft register struct buf *ap, *bp;
94 1.11 mycroft {
95 1.11 mycroft register struct buf *bq;
96 1.11 mycroft
97 1.11 mycroft /* If the queue is empty, then it's easy. */
98 1.11 mycroft if (ap->b_actf == NULL) {
99 1.11 mycroft bp->b_actf = NULL;
100 1.11 mycroft ap->b_actf = bp;
101 1.11 mycroft return;
102 1.11 mycroft }
103 1.11 mycroft
104 1.11 mycroft /*
105 1.11 mycroft * If we lie after the first (currently active) request, then we
106 1.11 mycroft * must locate the second request list and add ourselves to it.
107 1.11 mycroft */
108 1.11 mycroft bq = ap->b_actf;
109 1.11 mycroft if (bp->b_cylinder < bq->b_cylinder) {
110 1.11 mycroft while (bq->b_actf) {
111 1.11 mycroft /*
112 1.11 mycroft * Check for an ``inversion'' in the normally ascending
113 1.11 mycroft * cylinder numbers, indicating the start of the second
114 1.11 mycroft * request list.
115 1.11 mycroft */
116 1.11 mycroft if (bq->b_actf->b_cylinder < bq->b_cylinder) {
117 1.11 mycroft /*
118 1.11 mycroft * Search the second request list for the first
119 1.11 mycroft * request at a larger cylinder number. We go
120 1.11 mycroft * before that; if there is no such request, we
121 1.11 mycroft * go at end.
122 1.11 mycroft */
123 1.11 mycroft do {
124 1.11 mycroft if (bp->b_cylinder <
125 1.11 mycroft bq->b_actf->b_cylinder)
126 1.11 mycroft goto insert;
127 1.11 mycroft if (bp->b_cylinder ==
128 1.11 mycroft bq->b_actf->b_cylinder &&
129 1.11 mycroft bp->b_blkno < bq->b_actf->b_blkno)
130 1.11 mycroft goto insert;
131 1.11 mycroft bq = bq->b_actf;
132 1.11 mycroft } while (bq->b_actf);
133 1.11 mycroft goto insert; /* after last */
134 1.11 mycroft }
135 1.11 mycroft bq = bq->b_actf;
136 1.11 mycroft }
137 1.11 mycroft /*
138 1.11 mycroft * No inversions... we will go after the last, and
139 1.11 mycroft * be the first request in the second request list.
140 1.11 mycroft */
141 1.11 mycroft goto insert;
142 1.11 mycroft }
143 1.11 mycroft /*
144 1.11 mycroft * Request is at/after the current request...
145 1.11 mycroft * sort in the first request list.
146 1.11 mycroft */
147 1.11 mycroft while (bq->b_actf) {
148 1.11 mycroft /*
149 1.11 mycroft * We want to go after the current request if there is an
150 1.11 mycroft * inversion after it (i.e. it is the end of the first
151 1.11 mycroft * request list), or if the next request is a larger cylinder
152 1.11 mycroft * than our request.
153 1.11 mycroft */
154 1.11 mycroft if (bq->b_actf->b_cylinder < bq->b_cylinder ||
155 1.11 mycroft bp->b_cylinder < bq->b_actf->b_cylinder ||
156 1.11 mycroft (bp->b_cylinder == bq->b_actf->b_cylinder &&
157 1.11 mycroft bp->b_blkno < bq->b_actf->b_blkno))
158 1.11 mycroft goto insert;
159 1.11 mycroft bq = bq->b_actf;
160 1.11 mycroft }
161 1.11 mycroft /*
162 1.11 mycroft * Neither a second list nor a larger request... we go at the end of
163 1.11 mycroft * the first list, which is the same as the end of the whole schebang.
164 1.11 mycroft */
165 1.11 mycroft insert: bp->b_actf = bq->b_actf;
166 1.11 mycroft bq->b_actf = bp;
167 1.11 mycroft }
168 1.11 mycroft
169 1.11 mycroft /* encoding of disk minor numbers, should be elsewhere... */
170 1.11 mycroft #define dkunit(dev) (minor(dev) >> 3)
171 1.11 mycroft #define dkpart(dev) (minor(dev) & 07)
172 1.11 mycroft #define dkminor(unit, part) (((unit) << 3) | (part))
173 1.11 mycroft
174 1.11 mycroft /*
175 1.11 mycroft * Compute checksum for disk label.
176 1.11 mycroft */
177 1.11 mycroft u_int
178 1.11 mycroft dkcksum(lp)
179 1.11 mycroft register struct disklabel *lp;
180 1.11 mycroft {
181 1.11 mycroft register u_short *start, *end;
182 1.11 mycroft register u_short sum = 0;
183 1.11 mycroft
184 1.11 mycroft start = (u_short *)lp;
185 1.11 mycroft end = (u_short *)&lp->d_partitions[lp->d_npartitions];
186 1.11 mycroft while (start < end)
187 1.11 mycroft sum ^= *start++;
188 1.11 mycroft return (sum);
189 1.11 mycroft }
190 1.11 mycroft
191 1.11 mycroft /*
192 1.11 mycroft * Disk error is the preface to plaintive error messages
193 1.11 mycroft * about failing disk transfers. It prints messages of the form
194 1.11 mycroft
195 1.11 mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
196 1.11 mycroft
197 1.11 mycroft * if the offset of the error in the transfer and a disk label
198 1.11 mycroft * are both available. blkdone should be -1 if the position of the error
199 1.11 mycroft * is unknown; the disklabel pointer may be null from drivers that have not
200 1.11 mycroft * been converted to use them. The message is printed with printf
201 1.11 mycroft * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
202 1.11 mycroft * The message should be completed (with at least a newline) with printf
203 1.11 mycroft * or addlog, respectively. There is no trailing space.
204 1.11 mycroft */
205 1.11 mycroft void
206 1.11 mycroft diskerr(bp, dname, what, pri, blkdone, lp)
207 1.11 mycroft register struct buf *bp;
208 1.11 mycroft char *dname, *what;
209 1.11 mycroft int pri, blkdone;
210 1.11 mycroft register struct disklabel *lp;
211 1.11 mycroft {
212 1.11 mycroft int unit = dkunit(bp->b_dev), part = dkpart(bp->b_dev);
213 1.11 mycroft register void (*pr) __P((const char *, ...));
214 1.11 mycroft char partname = 'a' + part;
215 1.11 mycroft int sn;
216 1.11 mycroft
217 1.11 mycroft if (pri != LOG_PRINTF) {
218 1.17 christos static const char fmt[] = "";
219 1.17 christos log(pri, fmt);
220 1.11 mycroft pr = addlog;
221 1.11 mycroft } else
222 1.11 mycroft pr = printf;
223 1.11 mycroft (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
224 1.11 mycroft bp->b_flags & B_READ ? "read" : "writ");
225 1.11 mycroft sn = bp->b_blkno;
226 1.11 mycroft if (bp->b_bcount <= DEV_BSIZE)
227 1.11 mycroft (*pr)("%d", sn);
228 1.11 mycroft else {
229 1.11 mycroft if (blkdone >= 0) {
230 1.11 mycroft sn += blkdone;
231 1.11 mycroft (*pr)("%d of ", sn);
232 1.11 mycroft }
233 1.11 mycroft (*pr)("%d-%d", bp->b_blkno,
234 1.11 mycroft bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
235 1.11 mycroft }
236 1.11 mycroft if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
237 1.11 mycroft #ifdef tahoe
238 1.11 mycroft sn *= DEV_BSIZE / lp->d_secsize; /* XXX */
239 1.11 mycroft #endif
240 1.11 mycroft sn += lp->d_partitions[part].p_offset;
241 1.11 mycroft (*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
242 1.11 mycroft sn / lp->d_secpercyl);
243 1.11 mycroft sn %= lp->d_secpercyl;
244 1.11 mycroft (*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
245 1.11 mycroft }
246 1.15 thorpej }
247 1.15 thorpej
248 1.15 thorpej /*
249 1.15 thorpej * Initialize the disklist. Called by main() before autoconfiguration.
250 1.15 thorpej */
251 1.15 thorpej void
252 1.15 thorpej disk_init()
253 1.15 thorpej {
254 1.15 thorpej
255 1.15 thorpej TAILQ_INIT(&disklist);
256 1.15 thorpej disk_count = 0;
257 1.15 thorpej dk_ndrive = DK_NDRIVE; /* XXX */
258 1.15 thorpej }
259 1.15 thorpej
260 1.15 thorpej /*
261 1.15 thorpej * Searches the disklist for the disk corresponding to the
262 1.15 thorpej * name provided.
263 1.15 thorpej */
264 1.15 thorpej struct disk *
265 1.15 thorpej disk_find(name)
266 1.15 thorpej char *name;
267 1.15 thorpej {
268 1.15 thorpej struct disk *diskp;
269 1.15 thorpej
270 1.15 thorpej if ((name == NULL) || (disk_count <= 0))
271 1.15 thorpej return (NULL);
272 1.15 thorpej
273 1.15 thorpej for (diskp = disklist.tqh_first; diskp != NULL;
274 1.15 thorpej diskp = diskp->dk_link.tqe_next)
275 1.15 thorpej if (strcmp(diskp->dk_name, name) == 0)
276 1.15 thorpej return (diskp);
277 1.15 thorpej
278 1.15 thorpej return (NULL);
279 1.15 thorpej }
280 1.15 thorpej
281 1.15 thorpej /*
282 1.15 thorpej * Attach a disk.
283 1.15 thorpej */
284 1.15 thorpej void
285 1.15 thorpej disk_attach(diskp)
286 1.15 thorpej struct disk *diskp;
287 1.15 thorpej {
288 1.15 thorpej int s;
289 1.15 thorpej
290 1.15 thorpej /*
291 1.15 thorpej * Allocate and initialize the disklabel structures. Note that
292 1.15 thorpej * it's not safe to sleep here, since we're probably going to be
293 1.15 thorpej * called during autoconfiguration.
294 1.15 thorpej */
295 1.15 thorpej diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
296 1.15 thorpej diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
297 1.15 thorpej M_NOWAIT);
298 1.15 thorpej if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
299 1.15 thorpej panic("disk_attach: can't allocate storage for disklabel");
300 1.15 thorpej
301 1.15 thorpej bzero(diskp->dk_label, sizeof(struct disklabel));
302 1.15 thorpej bzero(diskp->dk_cpulabel, sizeof(struct cpu_disklabel));
303 1.15 thorpej
304 1.15 thorpej /*
305 1.15 thorpej * Set the attached timestamp.
306 1.15 thorpej */
307 1.15 thorpej s = splclock();
308 1.15 thorpej diskp->dk_attachtime = mono_time;
309 1.15 thorpej splx(s);
310 1.15 thorpej
311 1.15 thorpej /*
312 1.15 thorpej * Link into the disklist.
313 1.15 thorpej */
314 1.15 thorpej TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
315 1.15 thorpej ++disk_count;
316 1.15 thorpej }
317 1.15 thorpej
318 1.15 thorpej /*
319 1.16 christos * Detach a disk.
320 1.15 thorpej */
321 1.15 thorpej void
322 1.16 christos disk_detach(diskp)
323 1.15 thorpej struct disk *diskp;
324 1.15 thorpej {
325 1.15 thorpej
326 1.15 thorpej /*
327 1.15 thorpej * Free the space used by the disklabel structures.
328 1.15 thorpej */
329 1.15 thorpej free(diskp->dk_label, M_DEVBUF);
330 1.15 thorpej free(diskp->dk_cpulabel, M_DEVBUF);
331 1.15 thorpej
332 1.15 thorpej /*
333 1.15 thorpej * Remove from the disklist.
334 1.15 thorpej */
335 1.15 thorpej TAILQ_REMOVE(&disklist, diskp, dk_link);
336 1.15 thorpej if (--disk_count < 0)
337 1.16 christos panic("disk_detach: disk_count < 0");
338 1.15 thorpej }
339 1.15 thorpej
340 1.15 thorpej /*
341 1.15 thorpej * Increment a disk's busy counter. If the counter is going from
342 1.15 thorpej * 0 to 1, set the timestamp.
343 1.15 thorpej */
344 1.15 thorpej void
345 1.15 thorpej disk_busy(diskp)
346 1.15 thorpej struct disk *diskp;
347 1.15 thorpej {
348 1.15 thorpej int s;
349 1.15 thorpej
350 1.15 thorpej /*
351 1.15 thorpej * XXX We'd like to use something as accurate as microtime(),
352 1.15 thorpej * but that doesn't depend on the system TOD clock.
353 1.15 thorpej */
354 1.15 thorpej if (diskp->dk_busy++ == 0) {
355 1.15 thorpej s = splclock();
356 1.15 thorpej diskp->dk_timestamp = mono_time;
357 1.15 thorpej splx(s);
358 1.15 thorpej }
359 1.15 thorpej }
360 1.15 thorpej
361 1.15 thorpej /*
362 1.15 thorpej * Decrement a disk's busy counter, increment the byte count, total busy
363 1.15 thorpej * time, and reset the timestamp.
364 1.15 thorpej */
365 1.15 thorpej void
366 1.15 thorpej disk_unbusy(diskp, bcount)
367 1.15 thorpej struct disk *diskp;
368 1.15 thorpej long bcount;
369 1.15 thorpej {
370 1.15 thorpej int s;
371 1.15 thorpej struct timeval dv_time, diff_time;
372 1.15 thorpej
373 1.15 thorpej if (diskp->dk_busy-- == 0)
374 1.15 thorpej panic("disk_unbusy: %s: dk_busy < 0", diskp->dk_name);
375 1.15 thorpej
376 1.15 thorpej s = splclock();
377 1.15 thorpej dv_time = mono_time;
378 1.15 thorpej splx(s);
379 1.15 thorpej
380 1.15 thorpej timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
381 1.15 thorpej timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
382 1.15 thorpej
383 1.15 thorpej diskp->dk_timestamp = dv_time;
384 1.15 thorpej if (bcount > 0) {
385 1.15 thorpej diskp->dk_bytes += bcount;
386 1.15 thorpej diskp->dk_xfer++;
387 1.15 thorpej }
388 1.15 thorpej }
389 1.15 thorpej
390 1.15 thorpej /*
391 1.15 thorpej * Reset the metrics counters on the given disk. Note that we cannot
392 1.15 thorpej * reset the busy counter, as it may case a panic in disk_unbusy().
393 1.15 thorpej * We also must avoid playing with the timestamp information, as it
394 1.15 thorpej * may skew any pending transfer results.
395 1.15 thorpej */
396 1.15 thorpej void
397 1.15 thorpej disk_resetstat(diskp)
398 1.15 thorpej struct disk *diskp;
399 1.15 thorpej {
400 1.15 thorpej int s = splbio(), t;
401 1.15 thorpej
402 1.15 thorpej diskp->dk_xfer = 0;
403 1.15 thorpej diskp->dk_bytes = 0;
404 1.15 thorpej
405 1.15 thorpej t = splclock();
406 1.15 thorpej diskp->dk_attachtime = mono_time;
407 1.15 thorpej splx(t);
408 1.15 thorpej
409 1.15 thorpej timerclear(&diskp->dk_time);
410 1.15 thorpej
411 1.15 thorpej splx(s);
412 1.11 mycroft }
413