subr_disk.c revision 1.73.6.1 1 /* $NetBSD: subr_disk.c,v 1.73.6.1 2006/02/04 14:30:17 simonb 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.73.6.1 2006/02/04 14:30:17 simonb Exp $");
78
79 #include "opt_compat_netbsd.h"
80
81 #include <sys/param.h>
82 #include <sys/kernel.h>
83 #include <sys/malloc.h>
84 #include <sys/buf.h>
85 #include <sys/syslog.h>
86 #include <sys/disklabel.h>
87 #include <sys/disk.h>
88 #include <sys/sysctl.h>
89 #include <lib/libkern/libkern.h>
90
91 /*
92 * A global list of all disks attached to the system. May grow or
93 * shrink over time.
94 */
95 struct disklist_head disklist = TAILQ_HEAD_INITIALIZER(disklist);
96 int disk_count; /* number of drives in global disklist */
97 struct simplelock disklist_slock = SIMPLELOCK_INITIALIZER;
98
99 /*
100 * Compute checksum for disk label.
101 */
102 u_int
103 dkcksum(struct disklabel *lp)
104 {
105 u_short *start, *end;
106 u_short sum = 0;
107
108 start = (u_short *)lp;
109 end = (u_short *)&lp->d_partitions[lp->d_npartitions];
110 while (start < end)
111 sum ^= *start++;
112 return (sum);
113 }
114
115 /*
116 * Disk error is the preface to plaintive error messages
117 * about failing disk transfers. It prints messages of the form
118
119 hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
120
121 * if the offset of the error in the transfer and a disk label
122 * are both available. blkdone should be -1 if the position of the error
123 * is unknown; the disklabel pointer may be null from drivers that have not
124 * been converted to use them. The message is printed with printf
125 * if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
126 * The message should be completed (with at least a newline) with printf
127 * or addlog, respectively. There is no trailing space.
128 */
129 #ifndef PRIdaddr
130 #define PRIdaddr PRId64
131 #endif
132 void
133 diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
134 int blkdone, const struct disklabel *lp)
135 {
136 int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
137 void (*pr)(const char *, ...);
138 char partname = 'a' + part;
139 daddr_t sn;
140
141 if (/*CONSTCOND*/0)
142 /* Compiler will error this is the format is wrong... */
143 printf("%" PRIdaddr, bp->b_blkno);
144
145 if (pri != LOG_PRINTF) {
146 static const char fmt[] = "";
147 log(pri, fmt);
148 pr = addlog;
149 } else
150 pr = printf;
151 (*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
152 bp->b_flags & B_READ ? "read" : "writ");
153 sn = bp->b_blkno;
154 if (bp->b_bcount <= DEV_BSIZE)
155 (*pr)("%" PRIdaddr, sn);
156 else {
157 if (blkdone >= 0) {
158 sn += blkdone;
159 (*pr)("%" PRIdaddr " of ", sn);
160 }
161 (*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
162 bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
163 }
164 if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
165 sn += lp->d_partitions[part].p_offset;
166 (*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
167 dname, unit, sn, sn / lp->d_secpercyl);
168 sn %= lp->d_secpercyl;
169 (*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
170 sn / lp->d_nsectors, sn % lp->d_nsectors);
171 }
172 }
173
174 /*
175 * Searches the disklist for the disk corresponding to the
176 * name provided.
177 */
178 struct disk *
179 disk_find(char *name)
180 {
181 struct disk *diskp;
182
183 if ((name == NULL) || (disk_count <= 0))
184 return (NULL);
185
186 simple_lock(&disklist_slock);
187 for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
188 diskp = TAILQ_NEXT(diskp, dk_link))
189 if (strcmp(diskp->dk_name, name) == 0) {
190 simple_unlock(&disklist_slock);
191 return (diskp);
192 }
193 simple_unlock(&disklist_slock);
194
195 return (NULL);
196 }
197
198 static void
199 disk_init0(struct disk *diskp)
200 {
201
202 /*
203 * Initialize the wedge-related locks and other fields.
204 */
205 lockinit(&diskp->dk_rawlock, PRIBIO, "dkrawlk", 0, 0);
206 lockinit(&diskp->dk_openlock, PRIBIO, "dkoplk", 0, 0);
207 LIST_INIT(&diskp->dk_wedges);
208 diskp->dk_nwedges = 0;
209 }
210
211 static void
212 disk_attach0(struct disk *diskp)
213 {
214
215 /*
216 * Allocate and initialize the disklabel structures. Note that
217 * it's not safe to sleep here, since we're probably going to be
218 * called during autoconfiguration.
219 */
220 diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
221 diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
222 M_NOWAIT);
223 if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
224 panic("disk_attach: can't allocate storage for disklabel");
225
226 memset(diskp->dk_label, 0, sizeof(struct disklabel));
227 memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
228
229 /*
230 * Set the attached timestamp.
231 */
232 getmicrouptime(&diskp->dk_attachtime);
233
234 /*
235 * Link into the disklist.
236 */
237 simple_lock(&disklist_slock);
238 TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
239 disk_count++;
240 simple_unlock(&disklist_slock);
241 }
242
243 static void
244 disk_detach0(struct disk *diskp)
245 {
246
247 /*
248 * Remove from the disklist.
249 */
250 if (disk_count == 0)
251 panic("disk_detach: disk_count == 0");
252 simple_lock(&disklist_slock);
253 TAILQ_REMOVE(&disklist, diskp, dk_link);
254 disk_count--;
255 simple_unlock(&disklist_slock);
256
257 /*
258 * Free the space used by the disklabel structures.
259 */
260 free(diskp->dk_label, M_DEVBUF);
261 free(diskp->dk_cpulabel, M_DEVBUF);
262 }
263
264 /*
265 * Attach a disk.
266 */
267 void
268 disk_attach(struct disk *diskp)
269 {
270
271 disk_init0(diskp);
272 disk_attach0(diskp);
273 }
274
275 /*
276 * Detach a disk.
277 */
278 void
279 disk_detach(struct disk *diskp)
280 {
281
282 (void) lockmgr(&diskp->dk_openlock, LK_DRAIN, NULL);
283 disk_detach0(diskp);
284 }
285
286 /*
287 * Initialize a pseudo disk.
288 */
289 void
290 pseudo_disk_init(struct disk *diskp)
291 {
292
293 disk_init0(diskp);
294 }
295
296 /*
297 * Attach a pseudo disk.
298 */
299 void
300 pseudo_disk_attach(struct disk *diskp)
301 {
302
303 disk_attach0(diskp);
304 }
305
306 /*
307 * Detach a pseudo disk.
308 */
309 void
310 pseudo_disk_detach(struct disk *diskp)
311 {
312
313 disk_detach0(diskp);
314 }
315
316
317 /*
318 * Increment a disk's busy counter. If the counter is going from
319 * 0 to 1, set the timestamp.
320 */
321 void
322 disk_busy(struct disk *diskp)
323 {
324
325 if (diskp->dk_busy++ == 0)
326 getmicrouptime(&diskp->dk_timestamp);
327 }
328
329 /*
330 * Decrement a disk's busy counter, increment the byte count, total busy
331 * time, and reset the timestamp.
332 */
333 void
334 disk_unbusy(struct disk *diskp, long bcount, int read)
335 {
336 struct timeval dv_time, diff_time;
337
338 if (diskp->dk_busy-- == 0) {
339 printf("%s: dk_busy < 0\n", diskp->dk_name);
340 panic("disk_unbusy");
341 }
342
343 getmicrouptime(&dv_time);
344 timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
345 timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
346
347 diskp->dk_timestamp = dv_time;
348 if (bcount > 0) {
349 if (read) {
350 diskp->dk_rbytes += bcount;
351 diskp->dk_rxfer++;
352 } else {
353 diskp->dk_wbytes += bcount;
354 diskp->dk_wxfer++;
355 }
356 }
357 }
358
359 /*
360 * Reset the metrics counters on the given disk. Note that we cannot
361 * reset the busy counter, as it may case a panic in disk_unbusy().
362 * We also must avoid playing with the timestamp information, as it
363 * may skew any pending transfer results.
364 */
365 void
366 disk_resetstat(struct disk *diskp)
367 {
368 int s = splbio();
369
370 diskp->dk_rxfer = 0;
371 diskp->dk_rbytes = 0;
372 diskp->dk_wxfer = 0;
373 diskp->dk_wbytes = 0;
374
375 getmicrouptime(&diskp->dk_attachtime);
376 timerclear(&diskp->dk_time);
377
378 splx(s);
379 }
380
381 int
382 sysctl_hw_disknames(SYSCTLFN_ARGS)
383 {
384 char bf[DK_DISKNAMELEN + 1];
385 char *where = oldp;
386 struct disk *diskp;
387 size_t needed, left, slen;
388 int error, first;
389
390 if (newp != NULL)
391 return (EPERM);
392 if (namelen != 0)
393 return (EINVAL);
394
395 first = 1;
396 error = 0;
397 needed = 0;
398 left = *oldlenp;
399
400 simple_lock(&disklist_slock);
401 for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
402 diskp = TAILQ_NEXT(diskp, dk_link)) {
403 if (where == NULL)
404 needed += strlen(diskp->dk_name) + 1;
405 else {
406 memset(bf, 0, sizeof(bf));
407 if (first) {
408 strncpy(bf, diskp->dk_name, sizeof(bf));
409 first = 0;
410 } else {
411 bf[0] = ' ';
412 strncpy(bf + 1, diskp->dk_name,
413 sizeof(bf) - 1);
414 }
415 bf[DK_DISKNAMELEN] = '\0';
416 slen = strlen(bf);
417 if (left < slen + 1)
418 break;
419 /* +1 to copy out the trailing NUL byte */
420 error = copyout(bf, where, slen + 1);
421 if (error)
422 break;
423 where += slen;
424 needed += slen;
425 left -= slen;
426 }
427 }
428 simple_unlock(&disklist_slock);
429 *oldlenp = needed;
430 return (error);
431 }
432
433 int
434 sysctl_hw_diskstats(SYSCTLFN_ARGS)
435 {
436 struct disk_sysctl sdisk;
437 struct disk *diskp;
438 char *where = oldp;
439 size_t tocopy, left;
440 int error;
441
442 if (newp != NULL)
443 return (EPERM);
444
445 /*
446 * The original hw.diskstats call was broken and did not require
447 * the userland to pass in it's size of struct disk_sysctl. This
448 * was fixed after NetBSD 1.6 was released, and any applications
449 * that do not pass in the size are given an error only, unless
450 * we care about 1.6 compatibility.
451 */
452 if (namelen == 0)
453 #ifdef COMPAT_16
454 tocopy = offsetof(struct disk_sysctl, dk_rxfer);
455 #else
456 return (EINVAL);
457 #endif
458 else
459 tocopy = name[0];
460
461 if (where == NULL) {
462 *oldlenp = disk_count * tocopy;
463 return (0);
464 }
465
466 error = 0;
467 left = *oldlenp;
468 memset(&sdisk, 0, sizeof(sdisk));
469 *oldlenp = 0;
470
471 simple_lock(&disklist_slock);
472 TAILQ_FOREACH(diskp, &disklist, dk_link) {
473 if (left < tocopy)
474 break;
475 strncpy(sdisk.dk_name, diskp->dk_name, sizeof(sdisk.dk_name));
476 sdisk.dk_xfer = diskp->dk_rxfer + diskp->dk_wxfer;
477 sdisk.dk_rxfer = diskp->dk_rxfer;
478 sdisk.dk_wxfer = diskp->dk_wxfer;
479 sdisk.dk_seek = diskp->dk_seek;
480 sdisk.dk_bytes = diskp->dk_rbytes + diskp->dk_wbytes;
481 sdisk.dk_rbytes = diskp->dk_rbytes;
482 sdisk.dk_wbytes = diskp->dk_wbytes;
483 sdisk.dk_attachtime_sec = diskp->dk_attachtime.tv_sec;
484 sdisk.dk_attachtime_usec = diskp->dk_attachtime.tv_usec;
485 sdisk.dk_timestamp_sec = diskp->dk_timestamp.tv_sec;
486 sdisk.dk_timestamp_usec = diskp->dk_timestamp.tv_usec;
487 sdisk.dk_time_sec = diskp->dk_time.tv_sec;
488 sdisk.dk_time_usec = diskp->dk_time.tv_usec;
489 sdisk.dk_busy = diskp->dk_busy;
490
491 error = copyout(&sdisk, where, min(tocopy, sizeof(sdisk)));
492 if (error)
493 break;
494 where += tocopy;
495 *oldlenp += tocopy;
496 left -= tocopy;
497 }
498 simple_unlock(&disklist_slock);
499 return (error);
500 }
501
502 /*
503 * Bounds checking against the media size, used for the raw partition.
504 * The sector size passed in should currently always be DEV_BSIZE,
505 * and the media size the size of the device in DEV_BSIZE sectors.
506 */
507 int
508 bounds_check_with_mediasize(struct buf *bp, int secsize, uint64_t mediasize)
509 {
510 int64_t sz;
511
512 sz = howmany(bp->b_bcount, secsize);
513
514 if (bp->b_blkno + sz > mediasize) {
515 sz = mediasize - bp->b_blkno;
516 if (sz == 0) {
517 /* If exactly at end of disk, return EOF. */
518 bp->b_resid = bp->b_bcount;
519 goto done;
520 }
521 if (sz < 0) {
522 /* If past end of disk, return EINVAL. */
523 bp->b_error = EINVAL;
524 goto bad;
525 }
526 /* Otherwise, truncate request. */
527 bp->b_bcount = sz << DEV_BSHIFT;
528 }
529
530 return 1;
531
532 bad:
533 bp->b_flags |= B_ERROR;
534 done:
535 return 0;
536 }
537