subr_disk.c revision 1.25.8.1 1 1.25.8.1 bouyer /* $NetBSD: subr_disk.c,v 1.25.8.1 2000/11/20 18:09:07 bouyer Exp $ */
2 1.22 thorpej
3 1.22 thorpej /*-
4 1.25.8.1 bouyer * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
5 1.22 thorpej * All rights reserved.
6 1.22 thorpej *
7 1.22 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.22 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.22 thorpej * NASA Ames Research Center.
10 1.22 thorpej *
11 1.22 thorpej * Redistribution and use in source and binary forms, with or without
12 1.22 thorpej * modification, are permitted provided that the following conditions
13 1.22 thorpej * are met:
14 1.22 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.22 thorpej * notice, this list of conditions and the following disclaimer.
16 1.22 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.22 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.22 thorpej * documentation and/or other materials provided with the distribution.
19 1.22 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.22 thorpej * must display the following acknowledgement:
21 1.22 thorpej * This product includes software developed by the NetBSD
22 1.22 thorpej * Foundation, Inc. and its contributors.
23 1.22 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.22 thorpej * contributors may be used to endorse or promote products derived
25 1.22 thorpej * from this software without specific prior written permission.
26 1.22 thorpej *
27 1.22 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.22 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.22 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.22 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.22 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.22 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.22 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.22 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.22 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.22 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.22 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.22 thorpej */
39 1.12 cgd
40 1.11 mycroft /*
41 1.11 mycroft * Copyright (c) 1982, 1986, 1988, 1993
42 1.11 mycroft * The Regents of the University of California. All rights reserved.
43 1.11 mycroft * (c) UNIX System Laboratories, Inc.
44 1.11 mycroft * All or some portions of this file are derived from material licensed
45 1.11 mycroft * to the University of California by American Telephone and Telegraph
46 1.11 mycroft * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 1.11 mycroft * the permission of UNIX System Laboratories, Inc.
48 1.11 mycroft *
49 1.11 mycroft * Redistribution and use in source and binary forms, with or without
50 1.11 mycroft * modification, are permitted provided that the following conditions
51 1.11 mycroft * are met:
52 1.11 mycroft * 1. Redistributions of source code must retain the above copyright
53 1.11 mycroft * notice, this list of conditions and the following disclaimer.
54 1.11 mycroft * 2. Redistributions in binary form must reproduce the above copyright
55 1.11 mycroft * notice, this list of conditions and the following disclaimer in the
56 1.11 mycroft * documentation and/or other materials provided with the distribution.
57 1.11 mycroft * 3. All advertising materials mentioning features or use of this software
58 1.11 mycroft * must display the following acknowledgement:
59 1.11 mycroft * This product includes software developed by the University of
60 1.11 mycroft * California, Berkeley and its contributors.
61 1.11 mycroft * 4. Neither the name of the University nor the names of its contributors
62 1.11 mycroft * may be used to endorse or promote products derived from this software
63 1.11 mycroft * without specific prior written permission.
64 1.11 mycroft *
65 1.11 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 1.11 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 1.11 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 1.11 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 1.11 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 1.11 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 1.11 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 1.11 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 1.11 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 1.11 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 1.11 mycroft * SUCH DAMAGE.
76 1.11 mycroft *
77 1.12 cgd * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
78 1.11 mycroft */
79 1.11 mycroft
80 1.11 mycroft #include <sys/param.h>
81 1.11 mycroft #include <sys/systm.h>
82 1.15 thorpej #include <sys/kernel.h>
83 1.15 thorpej #include <sys/malloc.h>
84 1.11 mycroft #include <sys/buf.h>
85 1.15 thorpej #include <sys/syslog.h>
86 1.15 thorpej #include <sys/time.h>
87 1.11 mycroft #include <sys/disklabel.h>
88 1.15 thorpej #include <sys/disk.h>
89 1.14 thorpej
90 1.14 thorpej /*
91 1.15 thorpej * A global list of all disks attached to the system. May grow or
92 1.15 thorpej * shrink over time.
93 1.15 thorpej */
94 1.15 thorpej struct disklist_head disklist; /* TAILQ_HEAD */
95 1.15 thorpej int disk_count; /* number of drives in global disklist */
96 1.15 thorpej
97 1.15 thorpej /*
98 1.11 mycroft * Seek sort for disks. We depend on the driver which calls us using b_resid
99 1.11 mycroft * as the current cylinder number.
100 1.11 mycroft *
101 1.25.8.1 bouyer * The argument bufq is an I/O queue for the device, on which there are
102 1.25.8.1 bouyer * actually two queues, sorted in ascending cylinder order. The first
103 1.25.8.1 bouyer * queue holds those requests which are positioned after the current
104 1.25.8.1 bouyer * cylinder (in the first request); the second holds requests which came
105 1.25.8.1 bouyer * in after their cylinder number was passed. Thus we implement a one-way
106 1.25.8.1 bouyer * scan, retracting after reaching the end of the drive to the first request
107 1.25.8.1 bouyer * on the second queue, at which time it becomes the first queue.
108 1.11 mycroft *
109 1.11 mycroft * A one-way scan is natural because of the way UNIX read-ahead blocks are
110 1.11 mycroft * allocated.
111 1.25.8.1 bouyer *
112 1.25.8.1 bouyer * This is further adjusted by any `barriers' which may exist in the queue.
113 1.25.8.1 bouyer * The bufq points to the last such ordered request.
114 1.11 mycroft */
115 1.11 mycroft void
116 1.25.8.1 bouyer disksort_cylinder(bufq, bp)
117 1.25.8.1 bouyer struct buf_queue *bufq;
118 1.25.8.1 bouyer struct buf *bp;
119 1.11 mycroft {
120 1.25.8.1 bouyer struct buf *bq, *nbq;
121 1.25.8.1 bouyer
122 1.25.8.1 bouyer /*
123 1.25.8.1 bouyer * If there are ordered requests on the queue, we must start
124 1.25.8.1 bouyer * the elevator sort after the last of these.
125 1.25.8.1 bouyer */
126 1.25.8.1 bouyer if ((bq = bufq->bq_barrier) == NULL)
127 1.25.8.1 bouyer bq = BUFQ_FIRST(bufq);
128 1.11 mycroft
129 1.25.8.1 bouyer /*
130 1.25.8.1 bouyer * If the queue is empty, of if it's an ordered request,
131 1.25.8.1 bouyer * it's easy; we just go on the end.
132 1.25.8.1 bouyer */
133 1.25.8.1 bouyer if (bq == NULL || (bp->b_flags & B_ORDERED) != 0) {
134 1.25.8.1 bouyer BUFQ_INSERT_TAIL(bufq, bp);
135 1.11 mycroft return;
136 1.11 mycroft }
137 1.11 mycroft
138 1.11 mycroft /*
139 1.11 mycroft * If we lie after the first (currently active) request, then we
140 1.11 mycroft * must locate the second request list and add ourselves to it.
141 1.11 mycroft */
142 1.25.8.1 bouyer if (bp->b_cylinder < bq->b_cylinder ||
143 1.25.8.1 bouyer (bp->b_cylinder == bq->b_cylinder &&
144 1.25.8.1 bouyer bp->b_rawblkno < bq->b_rawblkno)) {
145 1.25.8.1 bouyer while ((nbq = BUFQ_NEXT(bq)) != NULL) {
146 1.11 mycroft /*
147 1.11 mycroft * Check for an ``inversion'' in the normally ascending
148 1.11 mycroft * cylinder numbers, indicating the start of the second
149 1.11 mycroft * request list.
150 1.11 mycroft */
151 1.25.8.1 bouyer if (nbq->b_cylinder < bq->b_cylinder) {
152 1.11 mycroft /*
153 1.11 mycroft * Search the second request list for the first
154 1.11 mycroft * request at a larger cylinder number. We go
155 1.11 mycroft * before that; if there is no such request, we
156 1.11 mycroft * go at end.
157 1.11 mycroft */
158 1.11 mycroft do {
159 1.25.8.1 bouyer if (bp->b_cylinder < nbq->b_cylinder)
160 1.11 mycroft goto insert;
161 1.25.8.1 bouyer if (bp->b_cylinder == nbq->b_cylinder &&
162 1.25.8.1 bouyer bp->b_rawblkno < nbq->b_rawblkno)
163 1.25.8.1 bouyer goto insert;
164 1.25.8.1 bouyer bq = nbq;
165 1.25.8.1 bouyer } while ((nbq = BUFQ_NEXT(bq)) != NULL);
166 1.25.8.1 bouyer goto insert; /* after last */
167 1.25.8.1 bouyer }
168 1.25.8.1 bouyer bq = BUFQ_NEXT(bq);
169 1.25.8.1 bouyer }
170 1.25.8.1 bouyer /*
171 1.25.8.1 bouyer * No inversions... we will go after the last, and
172 1.25.8.1 bouyer * be the first request in the second request list.
173 1.25.8.1 bouyer */
174 1.25.8.1 bouyer goto insert;
175 1.25.8.1 bouyer }
176 1.25.8.1 bouyer /*
177 1.25.8.1 bouyer * Request is at/after the current request...
178 1.25.8.1 bouyer * sort in the first request list.
179 1.25.8.1 bouyer */
180 1.25.8.1 bouyer while ((nbq = BUFQ_NEXT(bq)) != NULL) {
181 1.25.8.1 bouyer /*
182 1.25.8.1 bouyer * We want to go after the current request if there is an
183 1.25.8.1 bouyer * inversion after it (i.e. it is the end of the first
184 1.25.8.1 bouyer * request list), or if the next request is a larger cylinder
185 1.25.8.1 bouyer * than our request.
186 1.25.8.1 bouyer */
187 1.25.8.1 bouyer if (nbq->b_cylinder < bq->b_cylinder ||
188 1.25.8.1 bouyer bp->b_cylinder < nbq->b_cylinder ||
189 1.25.8.1 bouyer (bp->b_cylinder == nbq->b_cylinder &&
190 1.25.8.1 bouyer bp->b_rawblkno < nbq->b_rawblkno))
191 1.25.8.1 bouyer goto insert;
192 1.25.8.1 bouyer bq = nbq;
193 1.25.8.1 bouyer }
194 1.25.8.1 bouyer /*
195 1.25.8.1 bouyer * Neither a second list nor a larger request... we go at the end of
196 1.25.8.1 bouyer * the first list, which is the same as the end of the whole schebang.
197 1.25.8.1 bouyer */
198 1.25.8.1 bouyer insert: BUFQ_INSERT_AFTER(bufq, bq, bp);
199 1.25.8.1 bouyer }
200 1.25.8.1 bouyer
201 1.25.8.1 bouyer /*
202 1.25.8.1 bouyer * Seek sort for disks. This version sorts based on b_rawblkno, which
203 1.25.8.1 bouyer * indicates the block number.
204 1.25.8.1 bouyer *
205 1.25.8.1 bouyer * As before, there are actually two queues, sorted in ascendening block
206 1.25.8.1 bouyer * order. The first queue holds those requests which are positioned after
207 1.25.8.1 bouyer * the current block (in the first request); the second holds requests which
208 1.25.8.1 bouyer * came in after their block number was passed. Thus we implement a one-way
209 1.25.8.1 bouyer * scan, retracting after reaching the end of the driver to the first request
210 1.25.8.1 bouyer * on the second queue, at which time it becomes the first queue.
211 1.25.8.1 bouyer *
212 1.25.8.1 bouyer * A one-way scan is natural because of the way UNIX read-ahead blocks are
213 1.25.8.1 bouyer * allocated.
214 1.25.8.1 bouyer *
215 1.25.8.1 bouyer * This is further adjusted by any `barriers' which may exist in the queue.
216 1.25.8.1 bouyer * The bufq points to the last such ordered request.
217 1.25.8.1 bouyer */
218 1.25.8.1 bouyer void
219 1.25.8.1 bouyer disksort_blkno(bufq, bp)
220 1.25.8.1 bouyer struct buf_queue *bufq;
221 1.25.8.1 bouyer struct buf *bp;
222 1.25.8.1 bouyer {
223 1.25.8.1 bouyer struct buf *bq, *nbq;
224 1.25.8.1 bouyer
225 1.25.8.1 bouyer /*
226 1.25.8.1 bouyer * If there are ordered requests on the queue, we must start
227 1.25.8.1 bouyer * the elevator sort after the last of these.
228 1.25.8.1 bouyer */
229 1.25.8.1 bouyer if ((bq = bufq->bq_barrier) == NULL)
230 1.25.8.1 bouyer bq = BUFQ_FIRST(bufq);
231 1.25.8.1 bouyer
232 1.25.8.1 bouyer /*
233 1.25.8.1 bouyer * If the queue is empty, or if it's an ordered request,
234 1.25.8.1 bouyer * it's easy; we just go on the end.
235 1.25.8.1 bouyer */
236 1.25.8.1 bouyer if (bq == NULL || (bp->b_flags & B_ORDERED) != 0) {
237 1.25.8.1 bouyer BUFQ_INSERT_TAIL(bufq, bp);
238 1.25.8.1 bouyer return;
239 1.25.8.1 bouyer }
240 1.25.8.1 bouyer
241 1.25.8.1 bouyer /*
242 1.25.8.1 bouyer * If we lie after the first (currently active) request, then we
243 1.25.8.1 bouyer * must locate the second request list and add ourselves to it.
244 1.25.8.1 bouyer */
245 1.25.8.1 bouyer if (bp->b_rawblkno < bq->b_rawblkno) {
246 1.25.8.1 bouyer while ((nbq = BUFQ_NEXT(bq)) != NULL) {
247 1.25.8.1 bouyer /*
248 1.25.8.1 bouyer * Check for an ``inversion'' in the normally ascending
249 1.25.8.1 bouyer * block numbers, indicating the start of the second
250 1.25.8.1 bouyer * request list.
251 1.25.8.1 bouyer */
252 1.25.8.1 bouyer if (nbq->b_rawblkno < bq->b_rawblkno) {
253 1.25.8.1 bouyer /*
254 1.25.8.1 bouyer * Search the second request list for the first
255 1.25.8.1 bouyer * request at a larger block number. We go
256 1.25.8.1 bouyer * after that; if there is no such request, we
257 1.25.8.1 bouyer * go at the end.
258 1.25.8.1 bouyer */
259 1.25.8.1 bouyer do {
260 1.25.8.1 bouyer if (bp->b_rawblkno < nbq->b_rawblkno)
261 1.11 mycroft goto insert;
262 1.25.8.1 bouyer bq = nbq;
263 1.25.8.1 bouyer } while ((nbq = BUFQ_NEXT(bq)) != NULL);
264 1.11 mycroft goto insert; /* after last */
265 1.11 mycroft }
266 1.25.8.1 bouyer bq = BUFQ_NEXT(bq);
267 1.11 mycroft }
268 1.11 mycroft /*
269 1.11 mycroft * No inversions... we will go after the last, and
270 1.11 mycroft * be the first request in the second request list.
271 1.11 mycroft */
272 1.11 mycroft goto insert;
273 1.11 mycroft }
274 1.11 mycroft /*
275 1.11 mycroft * Request is at/after the current request...
276 1.11 mycroft * sort in the first request list.
277 1.11 mycroft */
278 1.25.8.1 bouyer while ((nbq = BUFQ_NEXT(bq)) != NULL) {
279 1.11 mycroft /*
280 1.11 mycroft * We want to go after the current request if there is an
281 1.11 mycroft * inversion after it (i.e. it is the end of the first
282 1.11 mycroft * request list), or if the next request is a larger cylinder
283 1.11 mycroft * than our request.
284 1.11 mycroft */
285 1.25.8.1 bouyer if (nbq->b_rawblkno < bq->b_rawblkno ||
286 1.25.8.1 bouyer bp->b_rawblkno < nbq->b_rawblkno)
287 1.11 mycroft goto insert;
288 1.25.8.1 bouyer bq = nbq;
289 1.11 mycroft }
290 1.11 mycroft /*
291 1.11 mycroft * Neither a second list nor a larger request... we go at the end of
292 1.11 mycroft * the first list, which is the same as the end of the whole schebang.
293 1.11 mycroft */
294 1.25.8.1 bouyer insert: BUFQ_INSERT_AFTER(bufq, bq, bp);
295 1.25.8.1 bouyer }
296 1.25.8.1 bouyer
297 1.25.8.1 bouyer /*
298 1.25.8.1 bouyer * Seek non-sort for disks. This version simply inserts requests at
299 1.25.8.1 bouyer * the tail of the queue.
300 1.25.8.1 bouyer */
301 1.25.8.1 bouyer void
302 1.25.8.1 bouyer disksort_tail(bufq, bp)
303 1.25.8.1 bouyer struct buf_queue *bufq;
304 1.25.8.1 bouyer struct buf *bp;
305 1.25.8.1 bouyer {
306 1.25.8.1 bouyer
307 1.25.8.1 bouyer BUFQ_INSERT_TAIL(bufq, bp);
308 1.11 mycroft }
309 1.11 mycroft
310 1.11 mycroft /*
311 1.11 mycroft * Compute checksum for disk label.
312 1.11 mycroft */
313 1.11 mycroft u_int
314 1.11 mycroft dkcksum(lp)
315 1.25.8.1 bouyer struct disklabel *lp;
316 1.11 mycroft {
317 1.25.8.1 bouyer u_short *start, *end;
318 1.25.8.1 bouyer u_short sum = 0;
319 1.11 mycroft
320 1.11 mycroft start = (u_short *)lp;
321 1.11 mycroft end = (u_short *)&lp->d_partitions[lp->d_npartitions];
322 1.11 mycroft while (start < end)
323 1.11 mycroft sum ^= *start++;
324 1.11 mycroft return (sum);
325 1.11 mycroft }
326 1.11 mycroft
327 1.11 mycroft /*
328 1.11 mycroft * Disk error is the preface to plaintive error messages
329 1.11 mycroft * about failing disk transfers. It prints messages of the form
330 1.11 mycroft
331 1.11 mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
332 1.11 mycroft
333 1.11 mycroft * if the offset of the error in the transfer and a disk label
334 1.11 mycroft * are both available. blkdone should be -1 if the position of the error
335 1.11 mycroft * is unknown; the disklabel pointer may be null from drivers that have not
336 1.20 christos * been converted to use them. The message is printed with printf
337 1.11 mycroft * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
338 1.20 christos * The message should be completed (with at least a newline) with printf
339 1.11 mycroft * or addlog, respectively. There is no trailing space.
340 1.11 mycroft */
341 1.11 mycroft void
342 1.11 mycroft diskerr(bp, dname, what, pri, blkdone, lp)
343 1.25.8.1 bouyer struct buf *bp;
344 1.11 mycroft char *dname, *what;
345 1.11 mycroft int pri, blkdone;
346 1.25.8.1 bouyer struct disklabel *lp;
347 1.11 mycroft {
348 1.25 drochner int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
349 1.25.8.1 bouyer void (*pr) __P((const char *, ...));
350 1.11 mycroft char partname = 'a' + part;
351 1.11 mycroft int sn;
352 1.11 mycroft
353 1.11 mycroft if (pri != LOG_PRINTF) {
354 1.17 christos static const char fmt[] = "";
355 1.17 christos log(pri, fmt);
356 1.11 mycroft pr = addlog;
357 1.11 mycroft } else
358 1.20 christos pr = printf;
359 1.11 mycroft (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
360 1.11 mycroft bp->b_flags & B_READ ? "read" : "writ");
361 1.11 mycroft sn = bp->b_blkno;
362 1.11 mycroft if (bp->b_bcount <= DEV_BSIZE)
363 1.11 mycroft (*pr)("%d", sn);
364 1.11 mycroft else {
365 1.11 mycroft if (blkdone >= 0) {
366 1.11 mycroft sn += blkdone;
367 1.11 mycroft (*pr)("%d of ", sn);
368 1.11 mycroft }
369 1.11 mycroft (*pr)("%d-%d", bp->b_blkno,
370 1.11 mycroft bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
371 1.11 mycroft }
372 1.11 mycroft if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
373 1.11 mycroft sn += lp->d_partitions[part].p_offset;
374 1.11 mycroft (*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
375 1.11 mycroft sn / lp->d_secpercyl);
376 1.11 mycroft sn %= lp->d_secpercyl;
377 1.11 mycroft (*pr)(" tn %d sn %d)", sn / lp->d_nsectors, sn % lp->d_nsectors);
378 1.11 mycroft }
379 1.15 thorpej }
380 1.15 thorpej
381 1.15 thorpej /*
382 1.15 thorpej * Initialize the disklist. Called by main() before autoconfiguration.
383 1.15 thorpej */
384 1.15 thorpej void
385 1.15 thorpej disk_init()
386 1.15 thorpej {
387 1.15 thorpej
388 1.15 thorpej TAILQ_INIT(&disklist);
389 1.15 thorpej disk_count = 0;
390 1.15 thorpej }
391 1.15 thorpej
392 1.15 thorpej /*
393 1.15 thorpej * Searches the disklist for the disk corresponding to the
394 1.15 thorpej * name provided.
395 1.15 thorpej */
396 1.15 thorpej struct disk *
397 1.15 thorpej disk_find(name)
398 1.15 thorpej char *name;
399 1.15 thorpej {
400 1.15 thorpej struct disk *diskp;
401 1.15 thorpej
402 1.15 thorpej if ((name == NULL) || (disk_count <= 0))
403 1.15 thorpej return (NULL);
404 1.15 thorpej
405 1.15 thorpej for (diskp = disklist.tqh_first; diskp != NULL;
406 1.15 thorpej diskp = diskp->dk_link.tqe_next)
407 1.15 thorpej if (strcmp(diskp->dk_name, name) == 0)
408 1.15 thorpej return (diskp);
409 1.15 thorpej
410 1.15 thorpej return (NULL);
411 1.15 thorpej }
412 1.15 thorpej
413 1.15 thorpej /*
414 1.15 thorpej * Attach a disk.
415 1.15 thorpej */
416 1.15 thorpej void
417 1.15 thorpej disk_attach(diskp)
418 1.15 thorpej struct disk *diskp;
419 1.15 thorpej {
420 1.15 thorpej int s;
421 1.15 thorpej
422 1.15 thorpej /*
423 1.15 thorpej * Allocate and initialize the disklabel structures. Note that
424 1.15 thorpej * it's not safe to sleep here, since we're probably going to be
425 1.15 thorpej * called during autoconfiguration.
426 1.15 thorpej */
427 1.15 thorpej diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
428 1.15 thorpej diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
429 1.15 thorpej M_NOWAIT);
430 1.15 thorpej if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
431 1.15 thorpej panic("disk_attach: can't allocate storage for disklabel");
432 1.15 thorpej
433 1.24 perry memset(diskp->dk_label, 0, sizeof(struct disklabel));
434 1.24 perry memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
435 1.15 thorpej
436 1.15 thorpej /*
437 1.15 thorpej * Set the attached timestamp.
438 1.15 thorpej */
439 1.15 thorpej s = splclock();
440 1.15 thorpej diskp->dk_attachtime = mono_time;
441 1.15 thorpej splx(s);
442 1.15 thorpej
443 1.15 thorpej /*
444 1.15 thorpej * Link into the disklist.
445 1.15 thorpej */
446 1.15 thorpej TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
447 1.15 thorpej ++disk_count;
448 1.15 thorpej }
449 1.15 thorpej
450 1.15 thorpej /*
451 1.16 christos * Detach a disk.
452 1.15 thorpej */
453 1.15 thorpej void
454 1.16 christos disk_detach(diskp)
455 1.15 thorpej struct disk *diskp;
456 1.15 thorpej {
457 1.15 thorpej
458 1.15 thorpej /*
459 1.23 thorpej * Remove from the disklist.
460 1.23 thorpej */
461 1.23 thorpej if (--disk_count < 0)
462 1.23 thorpej panic("disk_detach: disk_count < 0");
463 1.23 thorpej TAILQ_REMOVE(&disklist, diskp, dk_link);
464 1.23 thorpej
465 1.23 thorpej /*
466 1.15 thorpej * Free the space used by the disklabel structures.
467 1.15 thorpej */
468 1.15 thorpej free(diskp->dk_label, M_DEVBUF);
469 1.15 thorpej free(diskp->dk_cpulabel, M_DEVBUF);
470 1.15 thorpej }
471 1.15 thorpej
472 1.15 thorpej /*
473 1.15 thorpej * Increment a disk's busy counter. If the counter is going from
474 1.15 thorpej * 0 to 1, set the timestamp.
475 1.15 thorpej */
476 1.15 thorpej void
477 1.15 thorpej disk_busy(diskp)
478 1.15 thorpej struct disk *diskp;
479 1.15 thorpej {
480 1.15 thorpej int s;
481 1.15 thorpej
482 1.15 thorpej /*
483 1.15 thorpej * XXX We'd like to use something as accurate as microtime(),
484 1.15 thorpej * but that doesn't depend on the system TOD clock.
485 1.15 thorpej */
486 1.15 thorpej if (diskp->dk_busy++ == 0) {
487 1.15 thorpej s = splclock();
488 1.15 thorpej diskp->dk_timestamp = mono_time;
489 1.15 thorpej splx(s);
490 1.15 thorpej }
491 1.15 thorpej }
492 1.15 thorpej
493 1.15 thorpej /*
494 1.15 thorpej * Decrement a disk's busy counter, increment the byte count, total busy
495 1.15 thorpej * time, and reset the timestamp.
496 1.15 thorpej */
497 1.15 thorpej void
498 1.15 thorpej disk_unbusy(diskp, bcount)
499 1.15 thorpej struct disk *diskp;
500 1.15 thorpej long bcount;
501 1.15 thorpej {
502 1.15 thorpej int s;
503 1.15 thorpej struct timeval dv_time, diff_time;
504 1.15 thorpej
505 1.23 thorpej if (diskp->dk_busy-- == 0) {
506 1.23 thorpej printf("%s: dk_busy < 0\n", diskp->dk_name);
507 1.23 thorpej panic("disk_unbusy");
508 1.23 thorpej }
509 1.15 thorpej
510 1.15 thorpej s = splclock();
511 1.15 thorpej dv_time = mono_time;
512 1.15 thorpej splx(s);
513 1.15 thorpej
514 1.15 thorpej timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
515 1.15 thorpej timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
516 1.15 thorpej
517 1.15 thorpej diskp->dk_timestamp = dv_time;
518 1.15 thorpej if (bcount > 0) {
519 1.15 thorpej diskp->dk_bytes += bcount;
520 1.15 thorpej diskp->dk_xfer++;
521 1.15 thorpej }
522 1.15 thorpej }
523 1.15 thorpej
524 1.15 thorpej /*
525 1.15 thorpej * Reset the metrics counters on the given disk. Note that we cannot
526 1.15 thorpej * reset the busy counter, as it may case a panic in disk_unbusy().
527 1.15 thorpej * We also must avoid playing with the timestamp information, as it
528 1.15 thorpej * may skew any pending transfer results.
529 1.15 thorpej */
530 1.15 thorpej void
531 1.15 thorpej disk_resetstat(diskp)
532 1.15 thorpej struct disk *diskp;
533 1.15 thorpej {
534 1.15 thorpej int s = splbio(), t;
535 1.15 thorpej
536 1.15 thorpej diskp->dk_xfer = 0;
537 1.15 thorpej diskp->dk_bytes = 0;
538 1.15 thorpej
539 1.15 thorpej t = splclock();
540 1.15 thorpej diskp->dk_attachtime = mono_time;
541 1.15 thorpej splx(t);
542 1.15 thorpej
543 1.15 thorpej timerclear(&diskp->dk_time);
544 1.15 thorpej
545 1.15 thorpej splx(s);
546 1.11 mycroft }
547