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