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