2005-02-07 02:57:29 +03:00
|
|
|
/* $NetBSD: subr_disk.c,v 1.66 2005/02/06 23:57:29 christos Exp $ */
|
1997-10-05 22:37:01 +04:00
|
|
|
|
|
|
|
/*-
|
2000-01-22 02:20:51 +03:00
|
|
|
* Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
|
1997-10-05 22:37:01 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
|
|
|
* NASA Ames Research Center.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1994-06-29 10:29:24 +04:00
|
|
|
|
1994-05-19 07:43:13 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1988, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-19 07:43:13 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-06-29 10:29:24 +04:00
|
|
|
* @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
|
1994-05-19 07:43:13 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-13 16:33:44 +03:00
|
|
|
#include <sys/cdefs.h>
|
2005-02-07 02:57:29 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.66 2005/02/06 23:57:29 christos Exp $");
|
2002-11-05 16:22:32 +03:00
|
|
|
|
|
|
|
#include "opt_compat_netbsd.h"
|
2001-11-13 16:33:44 +03:00
|
|
|
|
1994-05-19 07:43:13 +04:00
|
|
|
#include <sys/param.h>
|
1996-01-08 01:01:38 +03:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
1994-05-19 07:43:13 +04:00
|
|
|
#include <sys/buf.h>
|
2004-10-28 11:07:35 +04:00
|
|
|
#include <sys/bufq.h>
|
1994-05-19 07:43:13 +04:00
|
|
|
#include <sys/syslog.h>
|
1996-01-08 01:01:38 +03:00
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/disk.h>
|
2002-01-27 15:41:07 +03:00
|
|
|
#include <sys/sysctl.h>
|
2002-11-04 06:50:07 +03:00
|
|
|
#include <lib/libkern/libkern.h>
|
1995-12-28 22:16:31 +03:00
|
|
|
|
1996-01-08 01:01:38 +03:00
|
|
|
/*
|
|
|
|
* A global list of all disks attached to the system. May grow or
|
|
|
|
* shrink over time.
|
|
|
|
*/
|
2004-10-15 11:19:01 +04:00
|
|
|
struct disklist_head disklist = TAILQ_HEAD_INITIALIZER(disklist);
|
1996-01-08 01:01:38 +03:00
|
|
|
int disk_count; /* number of drives in global disklist */
|
2002-01-27 15:41:07 +03:00
|
|
|
struct simplelock disklist_slock = SIMPLELOCK_INITIALIZER;
|
1996-01-08 01:01:38 +03:00
|
|
|
|
2004-11-25 07:52:23 +03:00
|
|
|
int bufq_disk_default_strat = _BUFQ_DEFAULT;
|
|
|
|
|
|
|
|
BUFQ_DEFINE(dummy, 0, NULL); /* so that bufq_strats won't be empty */
|
2004-02-28 09:28:47 +03:00
|
|
|
|
1994-05-19 07:43:13 +04:00
|
|
|
/*
|
|
|
|
* Compute checksum for disk label.
|
|
|
|
*/
|
|
|
|
u_int
|
2001-07-09 14:54:12 +04:00
|
|
|
dkcksum(struct disklabel *lp)
|
1994-05-19 07:43:13 +04:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
u_short *start, *end;
|
|
|
|
u_short sum = 0;
|
1994-05-19 07:43:13 +04:00
|
|
|
|
|
|
|
start = (u_short *)lp;
|
|
|
|
end = (u_short *)&lp->d_partitions[lp->d_npartitions];
|
|
|
|
while (start < end)
|
|
|
|
sum ^= *start++;
|
|
|
|
return (sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disk error is the preface to plaintive error messages
|
|
|
|
* about failing disk transfers. It prints messages of the form
|
|
|
|
|
|
|
|
hp0g: hard error reading fsbn 12345 of 12344-12347 (hp0 bn %d cn %d tn %d sn %d)
|
|
|
|
|
|
|
|
* if the offset of the error in the transfer and a disk label
|
|
|
|
* are both available. blkdone should be -1 if the position of the error
|
|
|
|
* is unknown; the disklabel pointer may be null from drivers that have not
|
1996-10-13 06:32:29 +04:00
|
|
|
* been converted to use them. The message is printed with printf
|
1994-05-19 07:43:13 +04:00
|
|
|
* if pri is LOG_PRINTF, otherwise it uses log at the specified priority.
|
1996-10-13 06:32:29 +04:00
|
|
|
* The message should be completed (with at least a newline) with printf
|
1994-05-19 07:43:13 +04:00
|
|
|
* or addlog, respectively. There is no trailing space.
|
|
|
|
*/
|
2003-04-13 11:51:30 +04:00
|
|
|
#ifndef PRIdaddr
|
|
|
|
#define PRIdaddr PRId64
|
|
|
|
#endif
|
1994-05-19 07:43:13 +04:00
|
|
|
void
|
2002-06-28 20:37:20 +04:00
|
|
|
diskerr(const struct buf *bp, const char *dname, const char *what, int pri,
|
|
|
|
int blkdone, const struct disklabel *lp)
|
1994-05-19 07:43:13 +04:00
|
|
|
{
|
1999-02-22 19:00:01 +03:00
|
|
|
int unit = DISKUNIT(bp->b_dev), part = DISKPART(bp->b_dev);
|
2001-07-09 14:54:12 +04:00
|
|
|
void (*pr)(const char *, ...);
|
1994-05-19 07:43:13 +04:00
|
|
|
char partname = 'a' + part;
|
2003-04-13 11:51:30 +04:00
|
|
|
daddr_t sn;
|
|
|
|
|
2003-04-13 13:08:04 +04:00
|
|
|
if (/*CONSTCOND*/0)
|
2003-04-13 11:51:30 +04:00
|
|
|
/* Compiler will error this is the format is wrong... */
|
|
|
|
printf("%" PRIdaddr, bp->b_blkno);
|
1994-05-19 07:43:13 +04:00
|
|
|
|
|
|
|
if (pri != LOG_PRINTF) {
|
1996-03-17 02:17:04 +03:00
|
|
|
static const char fmt[] = "";
|
|
|
|
log(pri, fmt);
|
1994-05-19 07:43:13 +04:00
|
|
|
pr = addlog;
|
|
|
|
} else
|
1996-10-13 06:32:29 +04:00
|
|
|
pr = printf;
|
1994-05-19 07:43:13 +04:00
|
|
|
(*pr)("%s%d%c: %s %sing fsbn ", dname, unit, partname, what,
|
|
|
|
bp->b_flags & B_READ ? "read" : "writ");
|
|
|
|
sn = bp->b_blkno;
|
|
|
|
if (bp->b_bcount <= DEV_BSIZE)
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)("%" PRIdaddr, sn);
|
1994-05-19 07:43:13 +04:00
|
|
|
else {
|
|
|
|
if (blkdone >= 0) {
|
|
|
|
sn += blkdone;
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)("%" PRIdaddr " of ", sn);
|
1994-05-19 07:43:13 +04:00
|
|
|
}
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)("%" PRIdaddr "-%" PRIdaddr "", bp->b_blkno,
|
1994-05-19 07:43:13 +04:00
|
|
|
bp->b_blkno + (bp->b_bcount - 1) / DEV_BSIZE);
|
|
|
|
}
|
|
|
|
if (lp && (blkdone >= 0 || bp->b_bcount <= lp->d_secsize)) {
|
|
|
|
sn += lp->d_partitions[part].p_offset;
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)(" (%s%d bn %" PRIdaddr "; cn %" PRIdaddr "",
|
|
|
|
dname, unit, sn, sn / lp->d_secpercyl);
|
1994-05-19 07:43:13 +04:00
|
|
|
sn %= lp->d_secpercyl;
|
2003-04-13 11:51:30 +04:00
|
|
|
(*pr)(" tn %" PRIdaddr " sn %" PRIdaddr ")",
|
|
|
|
sn / lp->d_nsectors, sn % lp->d_nsectors);
|
1994-05-19 07:43:13 +04:00
|
|
|
}
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Searches the disklist for the disk corresponding to the
|
|
|
|
* name provided.
|
|
|
|
*/
|
|
|
|
struct disk *
|
2001-07-09 14:54:12 +04:00
|
|
|
disk_find(char *name)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
|
|
|
struct disk *diskp;
|
|
|
|
|
|
|
|
if ((name == NULL) || (disk_count <= 0))
|
|
|
|
return (NULL);
|
|
|
|
|
2002-01-27 15:41:07 +03:00
|
|
|
simple_lock(&disklist_slock);
|
|
|
|
for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
|
|
|
|
diskp = TAILQ_NEXT(diskp, dk_link))
|
|
|
|
if (strcmp(diskp->dk_name, name) == 0) {
|
|
|
|
simple_unlock(&disklist_slock);
|
1996-01-08 01:01:38 +03:00
|
|
|
return (diskp);
|
2002-01-27 15:41:07 +03:00
|
|
|
}
|
|
|
|
simple_unlock(&disklist_slock);
|
1996-01-08 01:01:38 +03:00
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach a disk.
|
|
|
|
*/
|
|
|
|
void
|
2001-07-09 14:54:12 +04:00
|
|
|
disk_attach(struct disk *diskp)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate and initialize the disklabel structures. Note that
|
|
|
|
* it's not safe to sleep here, since we're probably going to be
|
|
|
|
* called during autoconfiguration.
|
|
|
|
*/
|
|
|
|
diskp->dk_label = malloc(sizeof(struct disklabel), M_DEVBUF, M_NOWAIT);
|
|
|
|
diskp->dk_cpulabel = malloc(sizeof(struct cpu_disklabel), M_DEVBUF,
|
|
|
|
M_NOWAIT);
|
|
|
|
if ((diskp->dk_label == NULL) || (diskp->dk_cpulabel == NULL))
|
|
|
|
panic("disk_attach: can't allocate storage for disklabel");
|
|
|
|
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memset(diskp->dk_label, 0, sizeof(struct disklabel));
|
|
|
|
memset(diskp->dk_cpulabel, 0, sizeof(struct cpu_disklabel));
|
1996-01-08 01:01:38 +03:00
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
/*
|
|
|
|
* Initialize the wedge-related locks and other fields.
|
|
|
|
*/
|
|
|
|
lockinit(&diskp->dk_rawlock, PRIBIO, "dkrawlk", 0, 0);
|
|
|
|
lockinit(&diskp->dk_openlock, PRIBIO, "dkoplk", 0, 0);
|
|
|
|
LIST_INIT(&diskp->dk_wedges);
|
|
|
|
diskp->dk_nwedges = 0;
|
|
|
|
|
1996-01-08 01:01:38 +03:00
|
|
|
/*
|
|
|
|
* Set the attached timestamp.
|
|
|
|
*/
|
|
|
|
s = splclock();
|
|
|
|
diskp->dk_attachtime = mono_time;
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Link into the disklist.
|
|
|
|
*/
|
2002-01-27 15:41:07 +03:00
|
|
|
simple_lock(&disklist_slock);
|
1996-01-08 01:01:38 +03:00
|
|
|
TAILQ_INSERT_TAIL(&disklist, diskp, dk_link);
|
2004-10-15 11:19:01 +04:00
|
|
|
disk_count++;
|
2002-01-27 15:41:07 +03:00
|
|
|
simple_unlock(&disklist_slock);
|
1996-01-08 01:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-02-09 21:59:18 +03:00
|
|
|
* Detach a disk.
|
1996-01-08 01:01:38 +03:00
|
|
|
*/
|
|
|
|
void
|
2001-07-09 14:54:12 +04:00
|
|
|
disk_detach(struct disk *diskp)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
|
|
|
|
2004-09-25 07:30:44 +04:00
|
|
|
(void) lockmgr(&diskp->dk_openlock, LK_DRAIN, NULL);
|
|
|
|
|
1996-01-08 01:01:38 +03:00
|
|
|
/*
|
|
|
|
* Remove from the disklist.
|
|
|
|
*/
|
2004-10-15 11:19:01 +04:00
|
|
|
if (disk_count == 0)
|
|
|
|
panic("disk_detach: disk_count == 0");
|
2002-01-27 15:41:07 +03:00
|
|
|
simple_lock(&disklist_slock);
|
1997-12-30 12:51:24 +03:00
|
|
|
TAILQ_REMOVE(&disklist, diskp, dk_link);
|
2004-10-15 11:19:01 +04:00
|
|
|
disk_count--;
|
2002-01-27 15:41:07 +03:00
|
|
|
simple_unlock(&disklist_slock);
|
1997-12-30 12:51:24 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the space used by the disklabel structures.
|
|
|
|
*/
|
|
|
|
free(diskp->dk_label, M_DEVBUF);
|
|
|
|
free(diskp->dk_cpulabel, M_DEVBUF);
|
1996-01-08 01:01:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Increment a disk's busy counter. If the counter is going from
|
|
|
|
* 0 to 1, set the timestamp.
|
|
|
|
*/
|
|
|
|
void
|
2001-07-09 14:54:12 +04:00
|
|
|
disk_busy(struct disk *diskp)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX We'd like to use something as accurate as microtime(),
|
|
|
|
* but that doesn't depend on the system TOD clock.
|
|
|
|
*/
|
|
|
|
if (diskp->dk_busy++ == 0) {
|
|
|
|
s = splclock();
|
|
|
|
diskp->dk_timestamp = mono_time;
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrement a disk's busy counter, increment the byte count, total busy
|
|
|
|
* time, and reset the timestamp.
|
|
|
|
*/
|
|
|
|
void
|
2002-11-01 14:31:50 +03:00
|
|
|
disk_unbusy(struct disk *diskp, long bcount, int read)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct timeval dv_time, diff_time;
|
|
|
|
|
1997-12-30 12:51:24 +03:00
|
|
|
if (diskp->dk_busy-- == 0) {
|
|
|
|
printf("%s: dk_busy < 0\n", diskp->dk_name);
|
|
|
|
panic("disk_unbusy");
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
|
|
|
|
s = splclock();
|
|
|
|
dv_time = mono_time;
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
timersub(&dv_time, &diskp->dk_timestamp, &diff_time);
|
|
|
|
timeradd(&diskp->dk_time, &diff_time, &diskp->dk_time);
|
|
|
|
|
|
|
|
diskp->dk_timestamp = dv_time;
|
|
|
|
if (bcount > 0) {
|
2002-11-01 14:31:50 +03:00
|
|
|
if (read) {
|
|
|
|
diskp->dk_rbytes += bcount;
|
|
|
|
diskp->dk_rxfer++;
|
|
|
|
} else {
|
|
|
|
diskp->dk_wbytes += bcount;
|
|
|
|
diskp->dk_wxfer++;
|
|
|
|
}
|
1996-01-08 01:01:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset the metrics counters on the given disk. Note that we cannot
|
|
|
|
* reset the busy counter, as it may case a panic in disk_unbusy().
|
|
|
|
* We also must avoid playing with the timestamp information, as it
|
|
|
|
* may skew any pending transfer results.
|
|
|
|
*/
|
|
|
|
void
|
2001-07-09 14:54:12 +04:00
|
|
|
disk_resetstat(struct disk *diskp)
|
1996-01-08 01:01:38 +03:00
|
|
|
{
|
|
|
|
int s = splbio(), t;
|
|
|
|
|
2002-11-01 14:31:50 +03:00
|
|
|
diskp->dk_rxfer = 0;
|
|
|
|
diskp->dk_rbytes = 0;
|
|
|
|
diskp->dk_wxfer = 0;
|
|
|
|
diskp->dk_wbytes = 0;
|
1996-01-08 01:01:38 +03:00
|
|
|
|
|
|
|
t = splclock();
|
|
|
|
diskp->dk_attachtime = mono_time;
|
|
|
|
splx(t);
|
|
|
|
|
|
|
|
timerclear(&diskp->dk_time);
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
}
|
2002-01-27 15:41:07 +03:00
|
|
|
|
|
|
|
int
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
sysctl_hw_disknames(SYSCTLFN_ARGS)
|
2002-01-27 15:41:07 +03:00
|
|
|
{
|
|
|
|
char buf[DK_DISKNAMELEN + 1];
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
char *where = oldp;
|
2002-01-27 15:41:07 +03:00
|
|
|
struct disk *diskp;
|
|
|
|
size_t needed, left, slen;
|
|
|
|
int error, first;
|
|
|
|
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
if (newp != NULL)
|
|
|
|
return (EPERM);
|
|
|
|
if (namelen != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2002-01-27 15:41:07 +03:00
|
|
|
first = 1;
|
|
|
|
error = 0;
|
|
|
|
needed = 0;
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
left = *oldlenp;
|
2002-01-28 06:33:55 +03:00
|
|
|
|
2002-01-27 15:41:07 +03:00
|
|
|
simple_lock(&disklist_slock);
|
|
|
|
for (diskp = TAILQ_FIRST(&disklist); diskp != NULL;
|
|
|
|
diskp = TAILQ_NEXT(diskp, dk_link)) {
|
|
|
|
if (where == NULL)
|
|
|
|
needed += strlen(diskp->dk_name) + 1;
|
|
|
|
else {
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
if (first) {
|
|
|
|
strncpy(buf, diskp->dk_name, sizeof(buf));
|
|
|
|
first = 0;
|
|
|
|
} else {
|
|
|
|
buf[0] = ' ';
|
2002-02-16 05:07:56 +03:00
|
|
|
strncpy(buf + 1, diskp->dk_name,
|
2002-02-16 05:11:43 +03:00
|
|
|
sizeof(buf) - 1);
|
2002-01-27 15:41:07 +03:00
|
|
|
}
|
|
|
|
buf[DK_DISKNAMELEN] = '\0';
|
|
|
|
slen = strlen(buf);
|
|
|
|
if (left < slen + 1)
|
|
|
|
break;
|
|
|
|
/* +1 to copy out the trailing NUL byte */
|
|
|
|
error = copyout(buf, where, slen + 1);
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
where += slen;
|
|
|
|
needed += slen;
|
|
|
|
left -= slen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
simple_unlock(&disklist_slock);
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
*oldlenp = needed;
|
2002-01-27 15:41:07 +03:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
sysctl_hw_diskstats(SYSCTLFN_ARGS)
|
2002-01-27 15:41:07 +03:00
|
|
|
{
|
|
|
|
struct disk_sysctl sdisk;
|
|
|
|
struct disk *diskp;
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
char *where = oldp;
|
2002-01-27 15:41:07 +03:00
|
|
|
size_t tocopy, left;
|
|
|
|
int error;
|
|
|
|
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
if (newp != NULL)
|
|
|
|
return (EPERM);
|
|
|
|
|
2002-11-05 16:22:32 +03:00
|
|
|
/*
|
|
|
|
* The original hw.diskstats call was broken and did not require
|
|
|
|
* the userland to pass in it's size of struct disk_sysctl. This
|
|
|
|
* was fixed after NetBSD 1.6 was released, and any applications
|
|
|
|
* that do not pass in the size are given an error only, unless
|
|
|
|
* we care about 1.6 compatibility.
|
|
|
|
*/
|
2002-01-27 15:41:07 +03:00
|
|
|
if (namelen == 0)
|
2002-11-04 06:50:07 +03:00
|
|
|
#ifdef COMPAT_16
|
2002-11-06 05:31:34 +03:00
|
|
|
tocopy = offsetof(struct disk_sysctl, dk_rxfer);
|
2002-11-04 06:50:07 +03:00
|
|
|
#else
|
|
|
|
return (EINVAL);
|
|
|
|
#endif
|
2002-01-27 15:41:07 +03:00
|
|
|
else
|
|
|
|
tocopy = name[0];
|
|
|
|
|
2002-11-06 05:31:34 +03:00
|
|
|
if (where == NULL) {
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
*oldlenp = disk_count * tocopy;
|
2002-11-06 05:31:34 +03:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-01-27 15:41:07 +03:00
|
|
|
error = 0;
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
left = *oldlenp;
|
2002-01-27 15:41:07 +03:00
|
|
|
memset(&sdisk, 0, sizeof(sdisk));
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
*oldlenp = 0;
|
2002-01-27 15:41:07 +03:00
|
|
|
|
|
|
|
simple_lock(&disklist_slock);
|
2002-01-28 06:12:13 +03:00
|
|
|
TAILQ_FOREACH(diskp, &disklist, dk_link) {
|
2002-11-01 18:20:03 +03:00
|
|
|
if (left < tocopy)
|
2002-01-27 15:41:07 +03:00
|
|
|
break;
|
2002-02-16 05:07:56 +03:00
|
|
|
strncpy(sdisk.dk_name, diskp->dk_name, sizeof(sdisk.dk_name));
|
2002-11-01 14:31:50 +03:00
|
|
|
sdisk.dk_xfer = diskp->dk_rxfer + diskp->dk_wxfer;
|
|
|
|
sdisk.dk_rxfer = diskp->dk_rxfer;
|
|
|
|
sdisk.dk_wxfer = diskp->dk_wxfer;
|
2002-01-27 15:41:07 +03:00
|
|
|
sdisk.dk_seek = diskp->dk_seek;
|
2002-11-01 14:31:50 +03:00
|
|
|
sdisk.dk_bytes = diskp->dk_rbytes + diskp->dk_wbytes;
|
|
|
|
sdisk.dk_rbytes = diskp->dk_rbytes;
|
|
|
|
sdisk.dk_wbytes = diskp->dk_wbytes;
|
2002-01-27 15:41:07 +03:00
|
|
|
sdisk.dk_attachtime_sec = diskp->dk_attachtime.tv_sec;
|
|
|
|
sdisk.dk_attachtime_usec = diskp->dk_attachtime.tv_usec;
|
|
|
|
sdisk.dk_timestamp_sec = diskp->dk_timestamp.tv_sec;
|
|
|
|
sdisk.dk_timestamp_usec = diskp->dk_timestamp.tv_usec;
|
|
|
|
sdisk.dk_time_sec = diskp->dk_time.tv_sec;
|
|
|
|
sdisk.dk_time_usec = diskp->dk_time.tv_usec;
|
|
|
|
sdisk.dk_busy = diskp->dk_busy;
|
2002-01-28 06:33:55 +03:00
|
|
|
|
2002-01-27 15:41:07 +03:00
|
|
|
error = copyout(&sdisk, where, min(tocopy, sizeof(sdisk)));
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
where += tocopy;
|
Dynamic sysctl.
Gone are the old kern_sysctl(), cpu_sysctl(), hw_sysctl(),
vfs_sysctl(), etc, routines, along with sysctl_int() et al. Now all
nodes are registered with the tree, and nodes can be added (or
removed) easily, and I/O to and from the tree is handled generically.
Since the nodes are registered with the tree, the mapping from name to
number (and back again) can now be discovered, instead of having to be
hard coded. Adding new nodes to the tree is likewise much simpler --
the new infrastructure handles almost all the work for simple types,
and just about anything else can be done with a small helper function.
All existing nodes are where they were before (numerically speaking),
so all existing consumers of sysctl information should notice no
difference.
PS - I'm sorry, but there's a distinct lack of documentation at the
moment. I'm working on sysctl(3/8/9) right now, and I promise to
watch out for buses.
2003-12-04 22:38:21 +03:00
|
|
|
*oldlenp += tocopy;
|
2002-01-27 15:41:07 +03:00
|
|
|
left -= tocopy;
|
|
|
|
}
|
|
|
|
simple_unlock(&disklist_slock);
|
|
|
|
return (error);
|
|
|
|
}
|
2002-07-16 22:03:17 +04:00
|
|
|
|
2002-07-21 19:32:17 +04:00
|
|
|
/*
|
|
|
|
* Create a device buffer queue.
|
|
|
|
*/
|
2002-07-16 22:03:17 +04:00
|
|
|
void
|
2002-07-21 19:32:17 +04:00
|
|
|
bufq_alloc(struct bufq_state *bufq, int flags)
|
2002-07-16 22:03:17 +04:00
|
|
|
{
|
2004-11-25 07:52:23 +03:00
|
|
|
__link_set_decl(bufq_strats, const struct bufq_strat);
|
|
|
|
int methodid;
|
|
|
|
const struct bufq_strat *bsp;
|
|
|
|
const struct bufq_strat * const *it;
|
2002-07-16 22:03:17 +04:00
|
|
|
|
|
|
|
bufq->bq_flags = flags;
|
2004-11-25 07:52:23 +03:00
|
|
|
methodid = flags & BUFQ_METHOD_MASK;
|
2002-07-16 22:03:17 +04:00
|
|
|
|
|
|
|
switch (flags & BUFQ_SORT_MASK) {
|
|
|
|
case BUFQ_SORT_RAWBLOCK:
|
|
|
|
case BUFQ_SORT_CYLINDER:
|
|
|
|
break;
|
|
|
|
case 0:
|
2004-11-25 07:52:23 +03:00
|
|
|
if (methodid == BUFQ_FCFS)
|
2002-07-16 22:03:17 +04:00
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
2002-07-21 19:32:17 +04:00
|
|
|
panic("bufq_alloc: sort out of range");
|
2002-07-16 22:03:17 +04:00
|
|
|
}
|
|
|
|
|
2004-11-25 07:52:23 +03:00
|
|
|
/*
|
|
|
|
* select strategy.
|
|
|
|
* if a strategy specified by flags is found, use it.
|
|
|
|
* otherwise, select one with the largest id number. XXX
|
|
|
|
*/
|
|
|
|
bsp = NULL;
|
|
|
|
__link_set_foreach(it, bufq_strats) {
|
|
|
|
if ((*it) == &bufq_strat_dummy)
|
|
|
|
continue;
|
|
|
|
if (methodid == (*it)->bs_id) {
|
|
|
|
bsp = *it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (bsp == NULL || (*it)->bs_id > bsp->bs_id)
|
|
|
|
bsp = *it;
|
|
|
|
}
|
|
|
|
|
2005-02-07 02:57:29 +03:00
|
|
|
KASSERT(bsp != NULL);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (bsp->bs_id != methodid && methodid != _BUFQ_DEFAULT)
|
|
|
|
printf("bufq_alloc: method 0x%04x is not available.\n",
|
|
|
|
methodid);
|
2004-11-25 07:52:23 +03:00
|
|
|
#endif
|
2005-02-07 02:57:29 +03:00
|
|
|
#ifdef BUFQ_DEBUG
|
|
|
|
/* XXX aprint? */
|
|
|
|
printf("bufq_alloc: using %s\n", bsp->bs_name);
|
|
|
|
#endif
|
|
|
|
(*bsp->bs_initfn)(bufq);
|
2002-07-16 22:03:17 +04:00
|
|
|
}
|
2002-07-21 19:32:17 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a device buffer queue.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
bufq_free(struct bufq_state *bufq)
|
|
|
|
{
|
2002-11-01 06:32:21 +03:00
|
|
|
|
2002-07-21 19:32:17 +04:00
|
|
|
KASSERT(bufq->bq_private != NULL);
|
|
|
|
KASSERT(BUFQ_PEEK(bufq) == NULL);
|
|
|
|
|
|
|
|
FREE(bufq->bq_private, M_DEVBUF);
|
|
|
|
bufq->bq_get = NULL;
|
|
|
|
bufq->bq_put = NULL;
|
|
|
|
}
|
2003-04-04 02:20:24 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bounds checking against the media size, used for the raw partition.
|
|
|
|
* The sector size passed in should currently always be DEV_BSIZE,
|
|
|
|
* and the media size the size of the device in DEV_BSIZE sectors.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
bounds_check_with_mediasize(struct buf *bp, int secsize, u_int64_t mediasize)
|
|
|
|
{
|
|
|
|
int sz;
|
|
|
|
|
|
|
|
sz = howmany(bp->b_bcount, secsize);
|
|
|
|
|
|
|
|
if (bp->b_blkno + sz > mediasize) {
|
|
|
|
sz = mediasize - bp->b_blkno;
|
|
|
|
if (sz == 0) {
|
|
|
|
/* If exactly at end of disk, return EOF. */
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (sz < 0) {
|
|
|
|
/* If past end of disk, return EINVAL. */
|
|
|
|
bp->b_error = EINVAL;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
/* Otherwise, truncate request. */
|
|
|
|
bp->b_bcount = sz << DEV_BSHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
done:
|
|
|
|
return 0;
|
|
|
|
}
|