subr_disk.c revision 1.47 1 1.47 mrg /* $NetBSD: subr_disk.c,v 1.47 2002/11/04 03:50:07 mrg Exp $ */
2 1.22 thorpej
3 1.22 thorpej /*-
4 1.26 thorpej * 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.31 lukem
80 1.31 lukem #include <sys/cdefs.h>
81 1.47 mrg __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.47 2002/11/04 03:50:07 mrg Exp $");
82 1.11 mycroft
83 1.11 mycroft #include <sys/param.h>
84 1.15 thorpej #include <sys/kernel.h>
85 1.15 thorpej #include <sys/malloc.h>
86 1.11 mycroft #include <sys/buf.h>
87 1.15 thorpej #include <sys/syslog.h>
88 1.11 mycroft #include <sys/disklabel.h>
89 1.15 thorpej #include <sys/disk.h>
90 1.33 simonb #include <sys/sysctl.h>
91 1.47 mrg #include <lib/libkern/libkern.h>
92 1.14 thorpej
93 1.14 thorpej /*
94 1.15 thorpej * A global list of all disks attached to the system. May grow or
95 1.15 thorpej * shrink over time.
96 1.15 thorpej */
97 1.15 thorpej struct disklist_head disklist; /* TAILQ_HEAD */
98 1.15 thorpej int disk_count; /* number of drives in global disklist */
99 1.33 simonb struct simplelock disklist_slock = SIMPLELOCK_INITIALIZER;
100 1.39 hannken
101 1.39 hannken /*
102 1.11 mycroft * Compute checksum for disk label.
103 1.11 mycroft */
104 1.11 mycroft u_int
105 1.30 simonb dkcksum(struct disklabel *lp)
106 1.11 mycroft {
107 1.29 augustss u_short *start, *end;
108 1.29 augustss u_short sum = 0;
109 1.11 mycroft
110 1.11 mycroft start = (u_short *)lp;
111 1.11 mycroft end = (u_short *)&lp->d_partitions[lp->d_npartitions];
112 1.11 mycroft while (start < end)
113 1.11 mycroft sum ^= *start++;
114 1.11 mycroft return (sum);
115 1.11 mycroft }
116 1.11 mycroft
117 1.11 mycroft /*
118 1.11 mycroft * Disk error is the preface to plaintive error messages
119 1.11 mycroft * about failing disk transfers. It prints messages of the form
120 1.11 mycroft
121 1.11 mycroft hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
122 1.11 mycroft
123 1.11 mycroft * if the offset of the error in the transfer and a disk label
124 1.11 mycroft * are both available. blkdone should be -1 if the position of the error
125 1.11 mycroft * is unknown; the disklabel pointer may be null from drivers that have not
126 1.20 christos * been converted to use them. The message is printed with printf
127 1.11 mycroft * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
128 1.20 christos * The message should be completed (with at least a newline) with printf
129 1.11 mycroft * or addlog, respectively. There is no trailing space.
130 1.11 mycroft */
131 1.11 mycroft void
132 1.38 yamt diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
133 1.38 yamt int blkdone, const struct disklabel *lp)
134 1.11 mycroft {
135 1.25 drochner int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
136 1.30 simonb void (*pr)(const char *, ...);
137 1.11 mycroft char partname = 'a' + part;
138 1.11 mycroft int sn;
139 1.11 mycroft
140 1.11 mycroft if (pri != LOG_PRINTF) {
141 1.17 christos static const char fmt[] = "";
142 1.17 christos log(pri, fmt);
143 1.11 mycroft pr = addlog;
144 1.11 mycroft } else
145 1.20 christos pr = printf;
146 1.11 mycroft (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
147 1.11 mycroft bp->b_flags & B_READ ? "read" : "writ");
148 1.11 mycroft sn = bp->b_blkno;
149 1.11 mycroft if (bp->b_bcount <= DEV_BSIZE)
150 1.11 mycroft (*pr)("%d", sn);
151 1.11 mycroft else {
152 1.11 mycroft if (blkdone >= 0) {
153 1.11 mycroft sn += blkdone;
154 1.11 mycroft (*pr)("%d of ", sn);
155 1.11 mycroft }
156 1.11 mycroft (*pr)("%d-%d", bp->b_blkno,
157 1.11 mycroft bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
158 1.11 mycroft }
159 1.11 mycroft if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
160 1.11 mycroft sn += lp->d_partitions[part].p_offset;
161 1.11 mycroft (*pr)(" (%s%d bn %d; cn %d", dname, unit, sn,
162 1.11 mycroft sn / lp->d_secpercyl);
163 1.11 mycroft sn %= lp->d_secpercyl;
164 1.36 enami (*pr)(" tn %d sn %d)", sn / lp->d_nsectors,
165 1.36 enami sn % lp->d_nsectors);
166 1.11 mycroft }
167 1.15 thorpej }
168 1.15 thorpej
169 1.15 thorpej /*
170 1.15 thorpej * Initialize the disklist. Called by main() before autoconfiguration.
171 1.15 thorpej */
172 1.15 thorpej void
173 1.30 simonb disk_init(void)
174 1.15 thorpej {
175 1.15 thorpej
176 1.15 thorpej TAILQ_INIT(&disklist);
177 1.15 thorpej disk_count = 0;
178 1.15 thorpej }
179 1.15 thorpej
180 1.15 thorpej /*
181 1.15 thorpej * Searches the disklist for the disk corresponding to the
182 1.15 thorpej * name provided.
183 1.15 thorpej */
184 1.15 thorpej struct disk *
185 1.30 simonb disk_find(char *name)
186 1.15 thorpej {
187 1.15 thorpej struct disk *diskp;
188 1.15 thorpej
189 1.15 thorpej if ((name == NULL) || (disk_count <= 0))
190 1.15 thorpej return (NULL);
191 1.15 thorpej
192 1.33 simonb simple_lock(&disklist_slock);
193 1.33 simonb for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
194 1.33 simonb diskp = TAILQ_NEXT(diskp, dk_link))
195 1.33 simonb if (strcmp(diskp->dk_name, name) == 0) {
196 1.33 simonb simple_unlock(&disklist_slock);
197 1.15 thorpej return (diskp);
198 1.33 simonb }
199 1.33 simonb simple_unlock(&disklist_slock);
200 1.15 thorpej
201 1.15 thorpej return (NULL);
202 1.15 thorpej }
203 1.15 thorpej
204 1.15 thorpej /*
205 1.15 thorpej * Attach a disk.
206 1.15 thorpej */
207 1.15 thorpej void
208 1.30 simonb disk_attach(struct disk *diskp)
209 1.15 thorpej {
210 1.15 thorpej int s;
211 1.15 thorpej
212 1.15 thorpej /*
213 1.15 thorpej * Allocate and initialize the disklabel structures. Note that
214 1.15 thorpej * it's not safe to sleep here, since we're probably going to be
215 1.15 thorpej * called during autoconfiguration.
216 1.15 thorpej */
217 1.15 thorpej diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
218 1.15 thorpej diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
219 1.15 thorpej M_NOWAIT);
220 1.15 thorpej if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
221 1.15 thorpej panic("disk_attach: can't allocate storage for disklabel");
222 1.15 thorpej
223 1.24 perry memset(diskp->dk_label, 0, sizeof(struct disklabel));
224 1.24 perry memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
225 1.15 thorpej
226 1.15 thorpej /*
227 1.15 thorpej * Set the attached timestamp.
228 1.15 thorpej */
229 1.15 thorpej s = splclock();
230 1.15 thorpej diskp->dk_attachtime = mono_time;
231 1.15 thorpej splx(s);
232 1.15 thorpej
233 1.15 thorpej /*
234 1.15 thorpej * Link into the disklist.
235 1.15 thorpej */
236 1.33 simonb simple_lock(&disklist_slock);
237 1.15 thorpej TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
238 1.33 simonb simple_unlock(&disklist_slock);
239 1.15 thorpej ++disk_count;
240 1.15 thorpej }
241 1.15 thorpej
242 1.15 thorpej /*
243 1.16 christos * Detach a disk.
244 1.15 thorpej */
245 1.15 thorpej void
246 1.30 simonb disk_detach(struct disk *diskp)
247 1.15 thorpej {
248 1.15 thorpej
249 1.15 thorpej /*
250 1.23 thorpej * Remove from the disklist.
251 1.23 thorpej */
252 1.23 thorpej if (--disk_count < 0)
253 1.23 thorpej panic("disk_detach: disk_count < 0");
254 1.33 simonb simple_lock(&disklist_slock);
255 1.23 thorpej TAILQ_REMOVE(&disklist, diskp, dk_link);
256 1.33 simonb simple_unlock(&disklist_slock);
257 1.23 thorpej
258 1.23 thorpej /*
259 1.15 thorpej * Free the space used by the disklabel structures.
260 1.15 thorpej */
261 1.15 thorpej free(diskp->dk_label, M_DEVBUF);
262 1.15 thorpej free(diskp->dk_cpulabel, M_DEVBUF);
263 1.15 thorpej }
264 1.15 thorpej
265 1.15 thorpej /*
266 1.15 thorpej * Increment a disk's busy counter. If the counter is going from
267 1.15 thorpej * 0 to 1, set the timestamp.
268 1.15 thorpej */
269 1.15 thorpej void
270 1.30 simonb disk_busy(struct disk *diskp)
271 1.15 thorpej {
272 1.15 thorpej int s;
273 1.15 thorpej
274 1.15 thorpej /*
275 1.15 thorpej * XXX We'd like to use something as accurate as microtime(),
276 1.15 thorpej * but that doesn't depend on the system TOD clock.
277 1.15 thorpej */
278 1.15 thorpej if (diskp->dk_busy++ == 0) {
279 1.15 thorpej s = splclock();
280 1.15 thorpej diskp->dk_timestamp = mono_time;
281 1.15 thorpej splx(s);
282 1.15 thorpej }
283 1.15 thorpej }
284 1.15 thorpej
285 1.15 thorpej /*
286 1.15 thorpej * Decrement a disk's busy counter, increment the byte count, total busy
287 1.15 thorpej * time, and reset the timestamp.
288 1.15 thorpej */
289 1.15 thorpej void
290 1.45 mrg disk_unbusy(struct disk *diskp, long bcount, int read)
291 1.15 thorpej {
292 1.15 thorpej int s;
293 1.15 thorpej struct timeval dv_time, diff_time;
294 1.15 thorpej
295 1.23 thorpej if (diskp->dk_busy-- == 0) {
296 1.23 thorpej printf("%s: dk_busy < 0\n", diskp->dk_name);
297 1.23 thorpej panic("disk_unbusy");
298 1.23 thorpej }
299 1.15 thorpej
300 1.15 thorpej s = splclock();
301 1.15 thorpej dv_time = mono_time;
302 1.15 thorpej splx(s);
303 1.15 thorpej
304 1.15 thorpej timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
305 1.15 thorpej timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
306 1.15 thorpej
307 1.15 thorpej diskp->dk_timestamp = dv_time;
308 1.15 thorpej if (bcount > 0) {
309 1.45 mrg if (read) {
310 1.45 mrg diskp->dk_rbytes += bcount;
311 1.45 mrg diskp->dk_rxfer++;
312 1.45 mrg } else {
313 1.45 mrg diskp->dk_wbytes += bcount;
314 1.45 mrg diskp->dk_wxfer++;
315 1.45 mrg }
316 1.15 thorpej }
317 1.15 thorpej }
318 1.15 thorpej
319 1.15 thorpej /*
320 1.15 thorpej * Reset the metrics counters on the given disk. Note that we cannot
321 1.15 thorpej * reset the busy counter, as it may case a panic in disk_unbusy().
322 1.15 thorpej * We also must avoid playing with the timestamp information, as it
323 1.15 thorpej * may skew any pending transfer results.
324 1.15 thorpej */
325 1.15 thorpej void
326 1.30 simonb disk_resetstat(struct disk *diskp)
327 1.15 thorpej {
328 1.15 thorpej int s = splbio(), t;
329 1.15 thorpej
330 1.45 mrg diskp->dk_rxfer = 0;
331 1.45 mrg diskp->dk_rbytes = 0;
332 1.45 mrg diskp->dk_wxfer = 0;
333 1.45 mrg diskp->dk_wbytes = 0;
334 1.15 thorpej
335 1.15 thorpej t = splclock();
336 1.15 thorpej diskp->dk_attachtime = mono_time;
337 1.15 thorpej splx(t);
338 1.15 thorpej
339 1.15 thorpej timerclear(&diskp->dk_time);
340 1.15 thorpej
341 1.15 thorpej splx(s);
342 1.33 simonb }
343 1.33 simonb
344 1.33 simonb int
345 1.33 simonb sysctl_disknames(void *vwhere, size_t *sizep)
346 1.33 simonb {
347 1.33 simonb char buf[DK_DISKNAMELEN + 1];
348 1.33 simonb char *where = vwhere;
349 1.33 simonb struct disk *diskp;
350 1.33 simonb size_t needed, left, slen;
351 1.33 simonb int error, first;
352 1.33 simonb
353 1.33 simonb first = 1;
354 1.33 simonb error = 0;
355 1.33 simonb needed = 0;
356 1.33 simonb left = *sizep;
357 1.35 simonb
358 1.33 simonb simple_lock(&disklist_slock);
359 1.33 simonb for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
360 1.33 simonb diskp = TAILQ_NEXT(diskp, dk_link)) {
361 1.33 simonb if (where == NULL)
362 1.33 simonb needed += strlen(diskp->dk_name) + 1;
363 1.33 simonb else {
364 1.33 simonb memset(buf, 0, sizeof(buf));
365 1.33 simonb if (first) {
366 1.33 simonb strncpy(buf, diskp->dk_name, sizeof(buf));
367 1.33 simonb first = 0;
368 1.33 simonb } else {
369 1.33 simonb buf[0] = ' ';
370 1.36 enami strncpy(buf + 1, diskp->dk_name,
371 1.37 enami sizeof(buf) - 1);
372 1.33 simonb }
373 1.33 simonb buf[DK_DISKNAMELEN] = '\0';
374 1.33 simonb slen = strlen(buf);
375 1.33 simonb if (left < slen + 1)
376 1.33 simonb break;
377 1.33 simonb /* +1 to copy out the trailing NUL byte */
378 1.33 simonb error = copyout(buf, where, slen + 1);
379 1.33 simonb if (error)
380 1.33 simonb break;
381 1.33 simonb where += slen;
382 1.33 simonb needed += slen;
383 1.33 simonb left -= slen;
384 1.33 simonb }
385 1.33 simonb }
386 1.33 simonb simple_unlock(&disklist_slock);
387 1.33 simonb *sizep = needed;
388 1.33 simonb return (error);
389 1.33 simonb }
390 1.33 simonb
391 1.33 simonb int
392 1.33 simonb sysctl_diskstats(int *name, u_int namelen, void *vwhere, size_t *sizep)
393 1.33 simonb {
394 1.33 simonb struct disk_sysctl sdisk;
395 1.33 simonb struct disk *diskp;
396 1.33 simonb char *where = vwhere;
397 1.33 simonb size_t tocopy, left;
398 1.33 simonb int error;
399 1.33 simonb
400 1.33 simonb if (where == NULL) {
401 1.46 simonb if (namelen == 0)
402 1.46 simonb *sizep = disk_count * sizeof(sdisk);
403 1.46 simonb else
404 1.46 simonb *sizep = disk_count * name[0];
405 1.33 simonb return (0);
406 1.33 simonb }
407 1.33 simonb
408 1.33 simonb if (namelen == 0)
409 1.47 mrg /*
410 1.47 mrg * The original hw.diskstats call was broken and did not
411 1.47 mrg * require the userland to pass in it's size of struct
412 1.47 mrg * disk_sysctl. This was fixed after NetBSD 1.6 was
413 1.47 mrg * released, and any applications that do not pass in
414 1.47 mrg * the size are given an error only, unless we care about
415 1.47 mrg * 1.6 compatibility.
416 1.47 mrg */
417 1.47 mrg #ifdef COMPAT_16
418 1.47 mrg tocopy = offsetof(struct disk_sysctl, dk_rxfer);
419 1.47 mrg #else
420 1.47 mrg return (EINVAL);
421 1.47 mrg #endif
422 1.33 simonb else
423 1.33 simonb tocopy = name[0];
424 1.33 simonb
425 1.33 simonb error = 0;
426 1.33 simonb left = *sizep;
427 1.33 simonb memset(&sdisk, 0, sizeof(sdisk));
428 1.35 simonb *sizep = 0;
429 1.33 simonb
430 1.33 simonb simple_lock(&disklist_slock);
431 1.34 simonb TAILQ_FOREACH(diskp, &disklist, dk_link) {
432 1.46 simonb if (left < tocopy)
433 1.33 simonb break;
434 1.36 enami strncpy(sdisk.dk_name, diskp->dk_name, sizeof(sdisk.dk_name));
435 1.45 mrg sdisk.dk_xfer = diskp->dk_rxfer + diskp->dk_wxfer;
436 1.45 mrg sdisk.dk_rxfer = diskp->dk_rxfer;
437 1.45 mrg sdisk.dk_wxfer = diskp->dk_wxfer;
438 1.33 simonb sdisk.dk_seek = diskp->dk_seek;
439 1.45 mrg sdisk.dk_bytes = diskp->dk_rbytes + diskp->dk_wbytes;
440 1.45 mrg sdisk.dk_rbytes = diskp->dk_rbytes;
441 1.45 mrg sdisk.dk_wbytes = diskp->dk_wbytes;
442 1.33 simonb sdisk.dk_attachtime_sec = diskp->dk_attachtime.tv_sec;
443 1.33 simonb sdisk.dk_attachtime_usec = diskp->dk_attachtime.tv_usec;
444 1.33 simonb sdisk.dk_timestamp_sec = diskp->dk_timestamp.tv_sec;
445 1.33 simonb sdisk.dk_timestamp_usec = diskp->dk_timestamp.tv_usec;
446 1.33 simonb sdisk.dk_time_sec = diskp->dk_time.tv_sec;
447 1.33 simonb sdisk.dk_time_usec = diskp->dk_time.tv_usec;
448 1.33 simonb sdisk.dk_busy = diskp->dk_busy;
449 1.35 simonb
450 1.33 simonb error = copyout(&sdisk, where, min(tocopy, sizeof(sdisk)));
451 1.33 simonb if (error)
452 1.33 simonb break;
453 1.33 simonb where += tocopy;
454 1.35 simonb *sizep += tocopy;
455 1.33 simonb left -= tocopy;
456 1.33 simonb }
457 1.33 simonb simple_unlock(&disklist_slock);
458 1.33 simonb return (error);
459 1.39 hannken }
460 1.39 hannken
461 1.39 hannken struct bufq_fcfs {
462 1.39 hannken TAILQ_HEAD(, buf) bq_head; /* actual list of buffers */
463 1.39 hannken };
464 1.39 hannken
465 1.39 hannken struct bufq_disksort {
466 1.39 hannken TAILQ_HEAD(, buf) bq_head; /* actual list of buffers */
467 1.39 hannken };
468 1.39 hannken
469 1.39 hannken #define PRIO_READ_BURST 48
470 1.39 hannken #define PRIO_WRITE_REQ 16
471 1.39 hannken
472 1.39 hannken struct bufq_prio {
473 1.39 hannken TAILQ_HEAD(, buf) bq_read, bq_write; /* actual list of buffers */
474 1.39 hannken struct buf *bq_write_next; /* next request in bq_write */
475 1.41 hannken struct buf *bq_next; /* current request */
476 1.39 hannken int bq_read_burst; /* # of consecutive reads */
477 1.39 hannken };
478 1.39 hannken
479 1.39 hannken
480 1.39 hannken /*
481 1.39 hannken * Check if two buf's are in ascending order.
482 1.39 hannken */
483 1.39 hannken static __inline int
484 1.39 hannken buf_inorder(struct buf *bp, struct buf *bq, int sortby)
485 1.39 hannken {
486 1.39 hannken int r;
487 1.39 hannken
488 1.39 hannken if (bp == NULL || bq == NULL)
489 1.43 enami return (bq == NULL);
490 1.39 hannken
491 1.39 hannken if (sortby == BUFQ_SORT_CYLINDER)
492 1.39 hannken r = bp->b_cylinder - bq->b_cylinder;
493 1.39 hannken else
494 1.39 hannken r = 0;
495 1.39 hannken
496 1.39 hannken if (r == 0)
497 1.39 hannken r = bp->b_rawblkno - bq->b_rawblkno;
498 1.39 hannken
499 1.43 enami return (r <= 0);
500 1.39 hannken }
501 1.39 hannken
502 1.39 hannken
503 1.39 hannken /*
504 1.39 hannken * First-come first-served sort for disks.
505 1.39 hannken *
506 1.39 hannken * Requests are appended to the queue without any reordering.
507 1.39 hannken */
508 1.39 hannken static void
509 1.39 hannken bufq_fcfs_put(struct bufq_state *bufq, struct buf *bp)
510 1.39 hannken {
511 1.39 hannken struct bufq_fcfs *fcfs = bufq->bq_private;
512 1.39 hannken
513 1.39 hannken TAILQ_INSERT_TAIL(&fcfs->bq_head, bp, b_actq);
514 1.39 hannken }
515 1.39 hannken
516 1.39 hannken static struct buf *
517 1.39 hannken bufq_fcfs_get(struct bufq_state *bufq, int remove)
518 1.39 hannken {
519 1.39 hannken struct bufq_fcfs *fcfs = bufq->bq_private;
520 1.39 hannken struct buf *bp;
521 1.39 hannken
522 1.39 hannken bp = TAILQ_FIRST(&fcfs->bq_head);
523 1.39 hannken
524 1.39 hannken if (bp != NULL && remove)
525 1.39 hannken TAILQ_REMOVE(&fcfs->bq_head, bp, b_actq);
526 1.39 hannken
527 1.43 enami return (bp);
528 1.39 hannken }
529 1.39 hannken
530 1.39 hannken
531 1.39 hannken /*
532 1.39 hannken * Seek sort for disks.
533 1.39 hannken *
534 1.39 hannken * There are actually two queues, sorted in ascendening order. The first
535 1.39 hannken * queue holds those requests which are positioned after the current block;
536 1.39 hannken * the second holds requests which came in after their position was passed.
537 1.39 hannken * Thus we implement a one-way scan, retracting after reaching the end of
538 1.39 hannken * the drive to the first request on the second queue, at which time it
539 1.39 hannken * becomes the first queue.
540 1.39 hannken *
541 1.39 hannken * A one-way scan is natural because of the way UNIX read-ahead blocks are
542 1.39 hannken * allocated.
543 1.39 hannken */
544 1.39 hannken static void
545 1.39 hannken bufq_disksort_put(struct bufq_state *bufq, struct buf *bp)
546 1.39 hannken {
547 1.39 hannken struct bufq_disksort *disksort = bufq->bq_private;
548 1.39 hannken struct buf *bq, *nbq;
549 1.39 hannken int sortby;
550 1.39 hannken
551 1.39 hannken sortby = bufq->bq_flags & BUFQ_SORT_MASK;
552 1.39 hannken
553 1.39 hannken bq = TAILQ_FIRST(&disksort->bq_head);
554 1.39 hannken
555 1.39 hannken /*
556 1.39 hannken * If the queue is empty it's easy; we just go on the end.
557 1.39 hannken */
558 1.39 hannken if (bq == NULL) {
559 1.39 hannken TAILQ_INSERT_TAIL(&disksort->bq_head, bp, b_actq);
560 1.39 hannken return;
561 1.39 hannken }
562 1.39 hannken
563 1.39 hannken /*
564 1.39 hannken * If we lie before the currently active request, then we
565 1.39 hannken * must locate the second request list and add ourselves to it.
566 1.39 hannken */
567 1.39 hannken if (buf_inorder(bp, bq, sortby)) {
568 1.39 hannken while ((nbq = TAILQ_NEXT(bq, b_actq)) != NULL) {
569 1.39 hannken /*
570 1.39 hannken * Check for an ``inversion'' in the normally ascending
571 1.39 hannken * block numbers, indicating the start of the second
572 1.39 hannken * request list.
573 1.39 hannken */
574 1.39 hannken if (buf_inorder(nbq, bq, sortby)) {
575 1.39 hannken /*
576 1.39 hannken * Search the second request list for the first
577 1.39 hannken * request at a larger block number. We go
578 1.39 hannken * after that; if there is no such request, we
579 1.39 hannken * go at the end.
580 1.39 hannken */
581 1.39 hannken do {
582 1.39 hannken if (buf_inorder(bp, nbq, sortby))
583 1.39 hannken goto insert;
584 1.39 hannken bq = nbq;
585 1.43 enami } while ((nbq =
586 1.43 enami TAILQ_NEXT(bq, b_actq)) != NULL);
587 1.39 hannken goto insert; /* after last */
588 1.39 hannken }
589 1.39 hannken bq = nbq;
590 1.39 hannken }
591 1.39 hannken /*
592 1.39 hannken * No inversions... we will go after the last, and
593 1.39 hannken * be the first request in the second request list.
594 1.39 hannken */
595 1.39 hannken goto insert;
596 1.39 hannken }
597 1.39 hannken /*
598 1.39 hannken * Request is at/after the current request...
599 1.39 hannken * sort in the first request list.
600 1.39 hannken */
601 1.39 hannken while ((nbq = TAILQ_NEXT(bq, b_actq)) != NULL) {
602 1.39 hannken /*
603 1.39 hannken * We want to go after the current request if there is an
604 1.39 hannken * inversion after it (i.e. it is the end of the first
605 1.39 hannken * request list), or if the next request is a larger cylinder
606 1.39 hannken * than our request.
607 1.39 hannken */
608 1.39 hannken if (buf_inorder(nbq, bq, sortby) ||
609 1.39 hannken buf_inorder(bp, nbq, sortby))
610 1.39 hannken goto insert;
611 1.39 hannken bq = nbq;
612 1.39 hannken }
613 1.39 hannken /*
614 1.39 hannken * Neither a second list nor a larger request... we go at the end of
615 1.39 hannken * the first list, which is the same as the end of the whole schebang.
616 1.39 hannken */
617 1.39 hannken insert: TAILQ_INSERT_AFTER(&disksort->bq_head, bq, bp, b_actq);
618 1.39 hannken }
619 1.39 hannken
620 1.39 hannken static struct buf *
621 1.39 hannken bufq_disksort_get(struct bufq_state *bufq, int remove)
622 1.39 hannken {
623 1.39 hannken struct bufq_disksort *disksort = bufq->bq_private;
624 1.39 hannken struct buf *bp;
625 1.39 hannken
626 1.39 hannken bp = TAILQ_FIRST(&disksort->bq_head);
627 1.39 hannken
628 1.39 hannken if (bp != NULL && remove)
629 1.39 hannken TAILQ_REMOVE(&disksort->bq_head, bp, b_actq);
630 1.39 hannken
631 1.43 enami return (bp);
632 1.39 hannken }
633 1.39 hannken
634 1.39 hannken
635 1.39 hannken /*
636 1.39 hannken * Seek sort for disks.
637 1.39 hannken *
638 1.39 hannken * There are two queues. The first queue holds read requests; the second
639 1.39 hannken * holds write requests. The read queue is first-come first-served; the
640 1.39 hannken * write queue is sorted in ascendening block order.
641 1.39 hannken * The read queue is processed first. After PRIO_READ_BURST consecutive
642 1.39 hannken * read requests with non-empty write queue PRIO_WRITE_REQ requests from
643 1.39 hannken * the write queue will be processed.
644 1.39 hannken */
645 1.39 hannken static void
646 1.39 hannken bufq_prio_put(struct bufq_state *bufq, struct buf *bp)
647 1.39 hannken {
648 1.39 hannken struct bufq_prio *prio = bufq->bq_private;
649 1.39 hannken struct buf *bq;
650 1.39 hannken int sortby;
651 1.39 hannken
652 1.39 hannken sortby = bufq->bq_flags & BUFQ_SORT_MASK;
653 1.39 hannken
654 1.39 hannken /*
655 1.39 hannken * If it's a read request append it to the list.
656 1.39 hannken */
657 1.39 hannken if ((bp->b_flags & B_READ) == B_READ) {
658 1.39 hannken TAILQ_INSERT_TAIL(&prio->bq_read, bp, b_actq);
659 1.39 hannken return;
660 1.39 hannken }
661 1.39 hannken
662 1.39 hannken bq = TAILQ_FIRST(&prio->bq_write);
663 1.39 hannken
664 1.39 hannken /*
665 1.39 hannken * If the write list is empty, simply append it to the list.
666 1.39 hannken */
667 1.39 hannken if (bq == NULL) {
668 1.39 hannken TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
669 1.39 hannken prio->bq_write_next = bp;
670 1.39 hannken return;
671 1.39 hannken }
672 1.39 hannken
673 1.39 hannken /*
674 1.39 hannken * If we lie after the next request, insert after this request.
675 1.39 hannken */
676 1.39 hannken if (buf_inorder(prio->bq_write_next, bp, sortby))
677 1.39 hannken bq = prio->bq_write_next;
678 1.39 hannken
679 1.39 hannken /*
680 1.39 hannken * Search for the first request at a larger block number.
681 1.39 hannken * We go before this request if it exists.
682 1.39 hannken */
683 1.39 hannken while (bq != NULL && buf_inorder(bq, bp, sortby))
684 1.39 hannken bq = TAILQ_NEXT(bq, b_actq);
685 1.39 hannken
686 1.39 hannken if (bq != NULL)
687 1.39 hannken TAILQ_INSERT_BEFORE(bq, bp, b_actq);
688 1.39 hannken else
689 1.39 hannken TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
690 1.39 hannken }
691 1.39 hannken
692 1.39 hannken static struct buf *
693 1.39 hannken bufq_prio_get(struct bufq_state *bufq, int remove)
694 1.39 hannken {
695 1.39 hannken struct bufq_prio *prio = bufq->bq_private;
696 1.39 hannken struct buf *bp;
697 1.39 hannken
698 1.39 hannken /*
699 1.41 hannken * If no current request, get next from the lists.
700 1.39 hannken */
701 1.41 hannken if (prio->bq_next == NULL) {
702 1.39 hannken /*
703 1.41 hannken * If at least one list is empty, select the other.
704 1.39 hannken */
705 1.41 hannken if (TAILQ_FIRST(&prio->bq_read) == NULL) {
706 1.41 hannken prio->bq_next = prio->bq_write_next;
707 1.41 hannken prio->bq_read_burst = 0;
708 1.41 hannken } else if (prio->bq_write_next == NULL) {
709 1.41 hannken prio->bq_next = TAILQ_FIRST(&prio->bq_read);
710 1.39 hannken prio->bq_read_burst = 0;
711 1.41 hannken } else {
712 1.41 hannken /*
713 1.41 hannken * Both list have requests. Select the read list up
714 1.41 hannken * to PRIO_READ_BURST times, then select the write
715 1.41 hannken * list PRIO_WRITE_REQ times.
716 1.41 hannken */
717 1.41 hannken if (prio->bq_read_burst++ < PRIO_READ_BURST)
718 1.41 hannken prio->bq_next = TAILQ_FIRST(&prio->bq_read);
719 1.41 hannken else if (prio->bq_read_burst <
720 1.43 enami PRIO_READ_BURST + PRIO_WRITE_REQ)
721 1.41 hannken prio->bq_next = prio->bq_write_next;
722 1.41 hannken else {
723 1.41 hannken prio->bq_next = TAILQ_FIRST(&prio->bq_read);
724 1.41 hannken prio->bq_read_burst = 0;
725 1.41 hannken }
726 1.39 hannken }
727 1.39 hannken }
728 1.39 hannken
729 1.41 hannken bp = prio->bq_next;
730 1.41 hannken
731 1.44 enami if (bp != NULL && remove) {
732 1.44 enami if ((bp->b_flags & B_READ) == B_READ)
733 1.44 enami TAILQ_REMOVE(&prio->bq_read, bp, b_actq);
734 1.39 hannken else {
735 1.39 hannken /*
736 1.44 enami * Advance the write pointer before removing
737 1.44 enami * bp since it is actually prio->bq_write_next.
738 1.39 hannken */
739 1.39 hannken prio->bq_write_next =
740 1.39 hannken TAILQ_NEXT(prio->bq_write_next, b_actq);
741 1.44 enami TAILQ_REMOVE(&prio->bq_write, bp, b_actq);
742 1.39 hannken if (prio->bq_write_next == NULL)
743 1.39 hannken prio->bq_write_next =
744 1.39 hannken TAILQ_FIRST(&prio->bq_write);
745 1.39 hannken }
746 1.41 hannken
747 1.41 hannken prio->bq_next = NULL;
748 1.39 hannken }
749 1.39 hannken
750 1.43 enami return (bp);
751 1.39 hannken }
752 1.39 hannken
753 1.40 hannken /*
754 1.40 hannken * Create a device buffer queue.
755 1.40 hannken */
756 1.39 hannken void
757 1.40 hannken bufq_alloc(struct bufq_state *bufq, int flags)
758 1.39 hannken {
759 1.39 hannken struct bufq_fcfs *fcfs;
760 1.39 hannken struct bufq_disksort *disksort;
761 1.39 hannken struct bufq_prio *prio;
762 1.39 hannken
763 1.39 hannken bufq->bq_flags = flags;
764 1.39 hannken
765 1.39 hannken switch (flags & BUFQ_SORT_MASK) {
766 1.39 hannken case BUFQ_SORT_RAWBLOCK:
767 1.39 hannken case BUFQ_SORT_CYLINDER:
768 1.39 hannken break;
769 1.39 hannken case 0:
770 1.39 hannken if ((flags & BUFQ_METHOD_MASK) == BUFQ_FCFS)
771 1.39 hannken break;
772 1.39 hannken /* FALLTHROUGH */
773 1.39 hannken default:
774 1.40 hannken panic("bufq_alloc: sort out of range");
775 1.39 hannken }
776 1.39 hannken
777 1.39 hannken switch (flags & BUFQ_METHOD_MASK) {
778 1.39 hannken case BUFQ_FCFS:
779 1.39 hannken bufq->bq_get = bufq_fcfs_get;
780 1.39 hannken bufq->bq_put = bufq_fcfs_put;
781 1.40 hannken MALLOC(bufq->bq_private, struct bufq_fcfs *,
782 1.40 hannken sizeof(struct bufq_fcfs), M_DEVBUF, M_ZERO);
783 1.39 hannken fcfs = (struct bufq_fcfs *)bufq->bq_private;
784 1.39 hannken TAILQ_INIT(&fcfs->bq_head);
785 1.39 hannken break;
786 1.39 hannken case BUFQ_DISKSORT:
787 1.39 hannken bufq->bq_get = bufq_disksort_get;
788 1.39 hannken bufq->bq_put = bufq_disksort_put;
789 1.40 hannken MALLOC(bufq->bq_private, struct bufq_disksort *,
790 1.40 hannken sizeof(struct bufq_disksort), M_DEVBUF, M_ZERO);
791 1.39 hannken disksort = (struct bufq_disksort *)bufq->bq_private;
792 1.39 hannken TAILQ_INIT(&disksort->bq_head);
793 1.39 hannken break;
794 1.39 hannken case BUFQ_READ_PRIO:
795 1.39 hannken bufq->bq_get = bufq_prio_get;
796 1.39 hannken bufq->bq_put = bufq_prio_put;
797 1.40 hannken MALLOC(bufq->bq_private, struct bufq_prio *,
798 1.40 hannken sizeof(struct bufq_prio), M_DEVBUF, M_ZERO);
799 1.39 hannken prio = (struct bufq_prio *)bufq->bq_private;
800 1.39 hannken TAILQ_INIT(&prio->bq_read);
801 1.39 hannken TAILQ_INIT(&prio->bq_write);
802 1.39 hannken break;
803 1.39 hannken default:
804 1.40 hannken panic("bufq_alloc: method out of range");
805 1.39 hannken }
806 1.40 hannken }
807 1.40 hannken
808 1.40 hannken /*
809 1.40 hannken * Destroy a device buffer queue.
810 1.40 hannken */
811 1.40 hannken void
812 1.40 hannken bufq_free(struct bufq_state *bufq)
813 1.40 hannken {
814 1.43 enami
815 1.40 hannken KASSERT(bufq->bq_private != NULL);
816 1.40 hannken KASSERT(BUFQ_PEEK(bufq) == NULL);
817 1.40 hannken
818 1.40 hannken FREE(bufq->bq_private, M_DEVBUF);
819 1.40 hannken bufq->bq_get = NULL;
820 1.40 hannken bufq->bq_put = NULL;
821 1.11 mycroft }
822