subr_disk.c revision 1.62 1 /* $NetBSD: subr_disk.c,v 1.62 2004/10/14 05:12:28 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1993
42 * The Regents of the University of California. All rights reserved.
43 * (c) UNIX System Laboratories, Inc.
44 * All or some portions of this file are derived from material licensed
45 * to the University of California by American Telephone and Telegraph
46 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
47 * the permission of UNIX System Laboratories, Inc.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.62 2004/10/14 05:12:28 yamt Exp $");
78
79 #include "opt_compat_netbsd.h"
80 #include "opt_bufq.h"
81
82 #include <sys/param.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/buf.h>
86 #include <sys/syslog.h>
87 #include <sys/disklabel.h>
88 #include <sys/disk.h>
89 #include <sys/sysctl.h>
90 #include <lib/libkern/libkern.h>
91
92 /*
93 * A global list of all disks attached to the system. May grow or
94 * shrink over time.
95 */
96 struct disklist_head disklist; /* TAILQ_HEAD */
97 int disk_count; /* number of drives in global disklist */
98 struct simplelock disklist_slock = SIMPLELOCK_INITIALIZER;
99
100 #ifdef NEW_BUFQ_STRATEGY
101 int bufq_disk_default_strat = BUFQ_READ_PRIO;
102 #else /* NEW_BUFQ_STRATEGY */
103 int bufq_disk_default_strat = BUFQ_DISKSORT;
104 #endif /* NEW_BUFQ_STRATEGY */
105
106 /*
107 * Compute checksum for disk label.
108 */
109 u_int
110 dkcksum(struct disklabel *lp)
111 {
112 u_short *start, *end;
113 u_short sum = 0;
114
115 start = (u_short *)lp;
116 end = (u_short *)&lp->d_partitions[lp->d_npartitions];
117 while (start < end)
118 sum ^= *start++;
119 return (sum);
120 }
121
122 /*
123 * Disk error is the preface to plaintive error messages
124 * about failing disk transfers. It prints messages of the form
125
126 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
127
128 * if the offset of the error in the transfer and a disk label
129 * are both available. blkdone should be -1 if the position of the error
130 * is unknown; the disklabel pointer may be null from drivers that have not
131 * been converted to use them. The message is printed with printf
132 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
133 * The message should be completed (with at least a newline) with printf
134 * or addlog, respectively. There is no trailing space.
135 */
136 #ifndef PRIdaddr
137 #define PRIdaddr PRId64
138 #endif
139 void
140 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
141 int blkdone, const struct disklabel *lp)
142 {
143 int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
144 void (*pr)(const char *, ...);
145 char partname = 'a' + part;
146 daddr_t sn;
147
148 if (/*CONSTCOND*/0)
149 /* Compiler will error this is the format is wrong... */
150 printf("%" PRIdaddr, bp->b_blkno);
151
152 if (pri != LOG_PRINTF) {
153 static const char fmt[] = "";
154 log(pri, fmt);
155 pr = addlog;
156 } else
157 pr = printf;
158 (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
159 bp->b_flags & B_READ ? "read" : "writ");
160 sn = bp->b_blkno;
161 if (bp->b_bcount <= DEV_BSIZE)
162 (*pr)("%" PRIdaddr, sn);
163 else {
164 if (blkdone >= 0) {
165 sn += blkdone;
166 (*pr)("%" PRIdaddr " of ", sn);
167 }
168 (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
169 bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
170 }
171 if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
172 sn += lp->d_partitions[part].p_offset;
173 (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
174 dname, unit, sn, sn / lp->d_secpercyl);
175 sn %= lp->d_secpercyl;
176 (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
177 sn / lp->d_nsectors, sn % lp->d_nsectors);
178 }
179 }
180
181 /*
182 * Initialize the disklist. Called by main() before autoconfiguration.
183 */
184 void
185 disk_init(void)
186 {
187
188 TAILQ_INIT(&disklist);
189 disk_count = 0;
190 }
191
192 /*
193 * Searches the disklist for the disk corresponding to the
194 * name provided.
195 */
196 struct disk *
197 disk_find(char *name)
198 {
199 struct disk *diskp;
200
201 if ((name == NULL) || (disk_count <= 0))
202 return (NULL);
203
204 simple_lock(&disklist_slock);
205 for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
206 diskp = TAILQ_NEXT(diskp, dk_link))
207 if (strcmp(diskp->dk_name, name) == 0) {
208 simple_unlock(&disklist_slock);
209 return (diskp);
210 }
211 simple_unlock(&disklist_slock);
212
213 return (NULL);
214 }
215
216 /*
217 * Attach a disk.
218 */
219 void
220 disk_attach(struct disk *diskp)
221 {
222 int s;
223
224 /*
225 * Allocate and initialize the disklabel structures. Note that
226 * it's not safe to sleep here, since we're probably going to be
227 * called during autoconfiguration.
228 */
229 diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
230 diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
231 M_NOWAIT);
232 if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
233 panic("disk_attach: can't allocate storage for disklabel");
234
235 memset(diskp->dk_label, 0, sizeof(struct disklabel));
236 memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
237
238 /*
239 * Initialize the wedge-related locks and other fields.
240 */
241 lockinit(&diskp->dk_rawlock, PRIBIO, "dkrawlk", 0, 0);
242 lockinit(&diskp->dk_openlock, PRIBIO, "dkoplk", 0, 0);
243 LIST_INIT(&diskp->dk_wedges);
244 diskp->dk_nwedges = 0;
245
246 /*
247 * Set the attached timestamp.
248 */
249 s = splclock();
250 diskp->dk_attachtime = mono_time;
251 splx(s);
252
253 /*
254 * Link into the disklist.
255 */
256 simple_lock(&disklist_slock);
257 TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
258 simple_unlock(&disklist_slock);
259 ++disk_count;
260 }
261
262 /*
263 * Detach a disk.
264 */
265 void
266 disk_detach(struct disk *diskp)
267 {
268
269 (void) lockmgr(&diskp->dk_openlock, LK_DRAIN, NULL);
270
271 /*
272 * Remove from the disklist.
273 */
274 if (--disk_count < 0)
275 panic("disk_detach: disk_count < 0");
276 simple_lock(&disklist_slock);
277 TAILQ_REMOVE(&disklist, diskp, dk_link);
278 simple_unlock(&disklist_slock);
279
280 /*
281 * Free the space used by the disklabel structures.
282 */
283 free(diskp->dk_label, M_DEVBUF);
284 free(diskp->dk_cpulabel, M_DEVBUF);
285 }
286
287 /*
288 * Increment a disk's busy counter. If the counter is going from
289 * 0 to 1, set the timestamp.
290 */
291 void
292 disk_busy(struct disk *diskp)
293 {
294 int s;
295
296 /*
297 * XXX We'd like to use something as accurate as microtime(),
298 * but that doesn't depend on the system TOD clock.
299 */
300 if (diskp->dk_busy++ == 0) {
301 s = splclock();
302 diskp->dk_timestamp = mono_time;
303 splx(s);
304 }
305 }
306
307 /*
308 * Decrement a disk's busy counter, increment the byte count, total busy
309 * time, and reset the timestamp.
310 */
311 void
312 disk_unbusy(struct disk *diskp, long bcount, int read)
313 {
314 int s;
315 struct timeval dv_time, diff_time;
316
317 if (diskp->dk_busy-- == 0) {
318 printf("%s: dk_busy < 0\n", diskp->dk_name);
319 panic("disk_unbusy");
320 }
321
322 s = splclock();
323 dv_time = mono_time;
324 splx(s);
325
326 timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
327 timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
328
329 diskp->dk_timestamp = dv_time;
330 if (bcount > 0) {
331 if (read) {
332 diskp->dk_rbytes += bcount;
333 diskp->dk_rxfer++;
334 } else {
335 diskp->dk_wbytes += bcount;
336 diskp->dk_wxfer++;
337 }
338 }
339 }
340
341 /*
342 * Reset the metrics counters on the given disk. Note that we cannot
343 * reset the busy counter, as it may case a panic in disk_unbusy().
344 * We also must avoid playing with the timestamp information, as it
345 * may skew any pending transfer results.
346 */
347 void
348 disk_resetstat(struct disk *diskp)
349 {
350 int s = splbio(), t;
351
352 diskp->dk_rxfer = 0;
353 diskp->dk_rbytes = 0;
354 diskp->dk_wxfer = 0;
355 diskp->dk_wbytes = 0;
356
357 t = splclock();
358 diskp->dk_attachtime = mono_time;
359 splx(t);
360
361 timerclear(&diskp->dk_time);
362
363 splx(s);
364 }
365
366 int
367 sysctl_hw_disknames(SYSCTLFN_ARGS)
368 {
369 char buf[DK_DISKNAMELEN + 1];
370 char *where = oldp;
371 struct disk *diskp;
372 size_t needed, left, slen;
373 int error, first;
374
375 if (newp != NULL)
376 return (EPERM);
377 if (namelen != 0)
378 return (EINVAL);
379
380 first = 1;
381 error = 0;
382 needed = 0;
383 left = *oldlenp;
384
385 simple_lock(&disklist_slock);
386 for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
387 diskp = TAILQ_NEXT(diskp, dk_link)) {
388 if (where == NULL)
389 needed += strlen(diskp->dk_name) + 1;
390 else {
391 memset(buf, 0, sizeof(buf));
392 if (first) {
393 strncpy(buf, diskp->dk_name, sizeof(buf));
394 first = 0;
395 } else {
396 buf[0] = ' ';
397 strncpy(buf + 1, diskp->dk_name,
398 sizeof(buf) - 1);
399 }
400 buf[DK_DISKNAMELEN] = '\0';
401 slen = strlen(buf);
402 if (left < slen + 1)
403 break;
404 /* +1 to copy out the trailing NUL byte */
405 error = copyout(buf, where, slen + 1);
406 if (error)
407 break;
408 where += slen;
409 needed += slen;
410 left -= slen;
411 }
412 }
413 simple_unlock(&disklist_slock);
414 *oldlenp = needed;
415 return (error);
416 }
417
418 int
419 sysctl_hw_diskstats(SYSCTLFN_ARGS)
420 {
421 struct disk_sysctl sdisk;
422 struct disk *diskp;
423 char *where = oldp;
424 size_t tocopy, left;
425 int error;
426
427 if (newp != NULL)
428 return (EPERM);
429
430 /*
431 * The original hw.diskstats call was broken and did not require
432 * the userland to pass in it's size of struct disk_sysctl. This
433 * was fixed after NetBSD 1.6 was released, and any applications
434 * that do not pass in the size are given an error only, unless
435 * we care about 1.6 compatibility.
436 */
437 if (namelen == 0)
438 #ifdef COMPAT_16
439 tocopy = offsetof(struct disk_sysctl, dk_rxfer);
440 #else
441 return (EINVAL);
442 #endif
443 else
444 tocopy = name[0];
445
446 if (where == NULL) {
447 *oldlenp = disk_count * tocopy;
448 return (0);
449 }
450
451 error = 0;
452 left = *oldlenp;
453 memset(&sdisk, 0, sizeof(sdisk));
454 *oldlenp = 0;
455
456 simple_lock(&disklist_slock);
457 TAILQ_FOREACH(diskp, &disklist, dk_link) {
458 if (left < tocopy)
459 break;
460 strncpy(sdisk.dk_name, diskp->dk_name, sizeof(sdisk.dk_name));
461 sdisk.dk_xfer = diskp->dk_rxfer + diskp->dk_wxfer;
462 sdisk.dk_rxfer = diskp->dk_rxfer;
463 sdisk.dk_wxfer = diskp->dk_wxfer;
464 sdisk.dk_seek = diskp->dk_seek;
465 sdisk.dk_bytes = diskp->dk_rbytes + diskp->dk_wbytes;
466 sdisk.dk_rbytes = diskp->dk_rbytes;
467 sdisk.dk_wbytes = diskp->dk_wbytes;
468 sdisk.dk_attachtime_sec = diskp->dk_attachtime.tv_sec;
469 sdisk.dk_attachtime_usec = diskp->dk_attachtime.tv_usec;
470 sdisk.dk_timestamp_sec = diskp->dk_timestamp.tv_sec;
471 sdisk.dk_timestamp_usec = diskp->dk_timestamp.tv_usec;
472 sdisk.dk_time_sec = diskp->dk_time.tv_sec;
473 sdisk.dk_time_usec = diskp->dk_time.tv_usec;
474 sdisk.dk_busy = diskp->dk_busy;
475
476 error = copyout(&sdisk, where, min(tocopy, sizeof(sdisk)));
477 if (error)
478 break;
479 where += tocopy;
480 *oldlenp += tocopy;
481 left -= tocopy;
482 }
483 simple_unlock(&disklist_slock);
484 return (error);
485 }
486
487 /*
488 * Create a device buffer queue.
489 */
490 void
491 bufq_alloc(struct bufq_state *bufq, int flags)
492 {
493 void (*initfn)(struct bufq_state *);
494
495 bufq->bq_flags = flags;
496
497 switch (flags & BUFQ_SORT_MASK) {
498 case BUFQ_SORT_RAWBLOCK:
499 case BUFQ_SORT_CYLINDER:
500 break;
501 case 0:
502 if ((flags & BUFQ_METHOD_MASK) == BUFQ_FCFS)
503 break;
504 /* FALLTHROUGH */
505 default:
506 panic("bufq_alloc: sort out of range");
507 }
508
509 switch (flags & BUFQ_METHOD_MASK) {
510 case BUFQ_FCFS:
511 initfn = bufq_fcfs_init;
512 break;
513 case BUFQ_DISKSORT:
514 initfn = bufq_disksort_init;
515 break;
516 case BUFQ_READ_PRIO:
517 initfn = bufq_readprio_init;
518 break;
519 case BUFQ_PRIOCSCAN:
520 initfn = bufq_priocscan_init;
521 break;
522 default:
523 panic("bufq_alloc: method out of range");
524 }
525 (*initfn)(bufq);
526 }
527
528 /*
529 * Destroy a device buffer queue.
530 */
531 void
532 bufq_free(struct bufq_state *bufq)
533 {
534
535 KASSERT(bufq->bq_private != NULL);
536 KASSERT(BUFQ_PEEK(bufq) == NULL);
537
538 FREE(bufq->bq_private, M_DEVBUF);
539 bufq->bq_get = NULL;
540 bufq->bq_put = NULL;
541 }
542
543 /*
544 * Bounds checking against the media size, used for the raw partition.
545 * The sector size passed in should currently always be DEV_BSIZE,
546 * and the media size the size of the device in DEV_BSIZE sectors.
547 */
548 int
549 bounds_check_with_mediasize(struct buf *bp, int secsize, u_int64_t mediasize)
550 {
551 int sz;
552
553 sz = howmany(bp->b_bcount, secsize);
554
555 if (bp->b_blkno + sz > mediasize) {
556 sz = mediasize - bp->b_blkno;
557 if (sz == 0) {
558 /* If exactly at end of disk, return EOF. */
559 bp->b_resid = bp->b_bcount;
560 goto done;
561 }
562 if (sz < 0) {
563 /* If past end of disk, return EINVAL. */
564 bp->b_error = EINVAL;
565 goto bad;
566 }
567 /* Otherwise, truncate request. */
568 bp->b_bcount = sz << DEV_BSHIFT;
569 }
570
571 return 1;
572
573 bad:
574 bp->b_flags |= B_ERROR;
575 done:
576 return 0;
577 }
578