Home | History | Annotate | Line # | Download | only in kern
subr_bufq.c revision 1.9.6.2
      1  1.9.6.2  skrll /*	$NetBSD: subr_bufq.c,v 1.9.6.2 2005/11/10 14:09:45 skrll Exp $	*/
      2  1.9.6.2  skrll /*	NetBSD: subr_disk.c,v 1.70 2005/08/20 12:00:01 yamt Exp $	*/
      3  1.9.6.2  skrll 
      4  1.9.6.2  skrll /*-
      5  1.9.6.2  skrll  * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
      6  1.9.6.2  skrll  * All rights reserved.
      7  1.9.6.2  skrll  *
      8  1.9.6.2  skrll  * This code is derived from software contributed to The NetBSD Foundation
      9  1.9.6.2  skrll  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10  1.9.6.2  skrll  * NASA Ames Research Center.
     11  1.9.6.2  skrll  *
     12  1.9.6.2  skrll  * Redistribution and use in source and binary forms, with or without
     13  1.9.6.2  skrll  * modification, are permitted provided that the following conditions
     14  1.9.6.2  skrll  * are met:
     15  1.9.6.2  skrll  * 1. Redistributions of source code must retain the above copyright
     16  1.9.6.2  skrll  *    notice, this list of conditions and the following disclaimer.
     17  1.9.6.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.9.6.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     19  1.9.6.2  skrll  *    documentation and/or other materials provided with the distribution.
     20  1.9.6.2  skrll  * 3. All advertising materials mentioning features or use of this software
     21  1.9.6.2  skrll  *    must display the following acknowledgement:
     22  1.9.6.2  skrll  *	This product includes software developed by the NetBSD
     23  1.9.6.2  skrll  *	Foundation, Inc. and its contributors.
     24  1.9.6.2  skrll  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  1.9.6.2  skrll  *    contributors may be used to endorse or promote products derived
     26  1.9.6.2  skrll  *    from this software without specific prior written permission.
     27  1.9.6.2  skrll  *
     28  1.9.6.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  1.9.6.2  skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  1.9.6.2  skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  1.9.6.2  skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  1.9.6.2  skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  1.9.6.2  skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  1.9.6.2  skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  1.9.6.2  skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  1.9.6.2  skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  1.9.6.2  skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  1.9.6.2  skrll  * POSSIBILITY OF SUCH DAMAGE.
     39  1.9.6.2  skrll  */
     40  1.9.6.2  skrll 
     41  1.9.6.2  skrll /*
     42  1.9.6.2  skrll  * Copyright (c) 1982, 1986, 1988, 1993
     43  1.9.6.2  skrll  *	The Regents of the University of California.  All rights reserved.
     44  1.9.6.2  skrll  * (c) UNIX System Laboratories, Inc.
     45  1.9.6.2  skrll  * All or some portions of this file are derived from material licensed
     46  1.9.6.2  skrll  * to the University of California by American Telephone and Telegraph
     47  1.9.6.2  skrll  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     48  1.9.6.2  skrll  * the permission of UNIX System Laboratories, Inc.
     49  1.9.6.2  skrll  *
     50  1.9.6.2  skrll  * Redistribution and use in source and binary forms, with or without
     51  1.9.6.2  skrll  * modification, are permitted provided that the following conditions
     52  1.9.6.2  skrll  * are met:
     53  1.9.6.2  skrll  * 1. Redistributions of source code must retain the above copyright
     54  1.9.6.2  skrll  *    notice, this list of conditions and the following disclaimer.
     55  1.9.6.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     56  1.9.6.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     57  1.9.6.2  skrll  *    documentation and/or other materials provided with the distribution.
     58  1.9.6.2  skrll  * 3. Neither the name of the University nor the names of its contributors
     59  1.9.6.2  skrll  *    may be used to endorse or promote products derived from this software
     60  1.9.6.2  skrll  *    without specific prior written permission.
     61  1.9.6.2  skrll  *
     62  1.9.6.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63  1.9.6.2  skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  1.9.6.2  skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  1.9.6.2  skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66  1.9.6.2  skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  1.9.6.2  skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  1.9.6.2  skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  1.9.6.2  skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  1.9.6.2  skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  1.9.6.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  1.9.6.2  skrll  * SUCH DAMAGE.
     73  1.9.6.2  skrll  *
     74  1.9.6.2  skrll  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     75  1.9.6.2  skrll  */
     76  1.9.6.2  skrll 
     77  1.9.6.2  skrll #include <sys/cdefs.h>
     78  1.9.6.2  skrll __KERNEL_RCSID(0, "$NetBSD: subr_bufq.c,v 1.9.6.2 2005/11/10 14:09:45 skrll Exp $");
     79  1.9.6.2  skrll 
     80  1.9.6.2  skrll #include <sys/param.h>
     81  1.9.6.2  skrll #include <sys/systm.h>
     82  1.9.6.2  skrll #include <sys/buf.h>
     83  1.9.6.2  skrll #include <sys/bufq.h>
     84  1.9.6.2  skrll #include <sys/bufq_impl.h>
     85  1.9.6.2  skrll #include <sys/malloc.h>
     86  1.9.6.2  skrll #include <sys/sysctl.h>
     87  1.9.6.2  skrll 
     88  1.9.6.2  skrll BUFQ_DEFINE(dummy, 0, NULL); /* so that bufq_strats won't be empty */
     89  1.9.6.2  skrll 
     90  1.9.6.2  skrll #define	STRAT_MATCH(id, bs)	(strcmp((id), (bs)->bs_name) == 0)
     91  1.9.6.2  skrll 
     92  1.9.6.2  skrll /*
     93  1.9.6.2  skrll  * Create a device buffer queue.
     94  1.9.6.2  skrll  */
     95  1.9.6.2  skrll int
     96  1.9.6.2  skrll bufq_alloc(struct bufq_state **bufqp, const char *strategy, int flags)
     97  1.9.6.2  skrll {
     98  1.9.6.2  skrll 	__link_set_decl(bufq_strats, const struct bufq_strat);
     99  1.9.6.2  skrll 	const struct bufq_strat *bsp;
    100  1.9.6.2  skrll 	const struct bufq_strat * const *it;
    101  1.9.6.2  skrll 	struct bufq_state *bufq;
    102  1.9.6.2  skrll 	int error = 0;
    103  1.9.6.2  skrll 
    104  1.9.6.2  skrll 	KASSERT((flags & BUFQ_EXACT) == 0 || strategy != BUFQ_STRAT_ANY);
    105  1.9.6.2  skrll 
    106  1.9.6.2  skrll 	switch (flags & BUFQ_SORT_MASK) {
    107  1.9.6.2  skrll 	case BUFQ_SORT_RAWBLOCK:
    108  1.9.6.2  skrll 	case BUFQ_SORT_CYLINDER:
    109  1.9.6.2  skrll 		break;
    110  1.9.6.2  skrll 	case 0:
    111  1.9.6.2  skrll 		/*
    112  1.9.6.2  skrll 		 * for strategies which don't care about block numbers.
    113  1.9.6.2  skrll 		 * eg. fcfs
    114  1.9.6.2  skrll 		 */
    115  1.9.6.2  skrll 		flags |= BUFQ_SORT_RAWBLOCK;
    116  1.9.6.2  skrll 		break;
    117  1.9.6.2  skrll 	default:
    118  1.9.6.2  skrll 		panic("bufq_alloc: sort out of range");
    119  1.9.6.2  skrll 	}
    120  1.9.6.2  skrll 
    121  1.9.6.2  skrll 	/*
    122  1.9.6.2  skrll 	 * select strategy.
    123  1.9.6.2  skrll 	 * if a strategy specified by flags is found, use it.
    124  1.9.6.2  skrll 	 * otherwise, select one with the largest bs_prio.
    125  1.9.6.2  skrll 	 */
    126  1.9.6.2  skrll 	bsp = NULL;
    127  1.9.6.2  skrll 	__link_set_foreach(it, bufq_strats) {
    128  1.9.6.2  skrll 		if ((*it) == &bufq_strat_dummy)
    129  1.9.6.2  skrll 			continue;
    130  1.9.6.2  skrll 		if (strategy != BUFQ_STRAT_ANY &&
    131  1.9.6.2  skrll 		    STRAT_MATCH(strategy, (*it))) {
    132  1.9.6.2  skrll 			bsp = *it;
    133  1.9.6.2  skrll 			break;
    134  1.9.6.2  skrll 		}
    135  1.9.6.2  skrll 		if (bsp == NULL || (*it)->bs_prio > bsp->bs_prio)
    136  1.9.6.2  skrll 			bsp = *it;
    137  1.9.6.2  skrll 	}
    138  1.9.6.2  skrll 
    139  1.9.6.2  skrll 	if (bsp == NULL) {
    140  1.9.6.2  skrll 		panic("bufq_alloc: no strategy");
    141  1.9.6.2  skrll 	}
    142  1.9.6.2  skrll 	if (strategy != BUFQ_STRAT_ANY && !STRAT_MATCH(strategy, bsp)) {
    143  1.9.6.2  skrll 		if ((flags & BUFQ_EXACT)) {
    144  1.9.6.2  skrll 			error = ENOENT;
    145  1.9.6.2  skrll 			goto out;
    146  1.9.6.2  skrll 		}
    147  1.9.6.2  skrll #if defined(DEBUG)
    148  1.9.6.2  skrll 		printf("bufq_alloc: '%s' is not available. using '%s'.\n",
    149  1.9.6.2  skrll 		    strategy, bsp->bs_name);
    150  1.9.6.2  skrll #endif
    151  1.9.6.2  skrll 	}
    152  1.9.6.2  skrll #if defined(BUFQ_DEBUG)
    153  1.9.6.2  skrll 	/* XXX aprint? */
    154  1.9.6.2  skrll 	printf("bufq_alloc: using '%s'\n", bsp->bs_name);
    155  1.9.6.2  skrll #endif
    156  1.9.6.2  skrll 
    157  1.9.6.2  skrll 	*bufqp = bufq = malloc(sizeof(*bufq), M_DEVBUF, M_WAITOK | M_ZERO);
    158  1.9.6.2  skrll 	bufq->bq_flags = flags;
    159  1.9.6.2  skrll 	bufq->bq_strat = bsp;
    160  1.9.6.2  skrll 	(*bsp->bs_initfn)(bufq);
    161  1.9.6.2  skrll 
    162  1.9.6.2  skrll out:
    163  1.9.6.2  skrll 	return 0;
    164  1.9.6.2  skrll }
    165  1.9.6.2  skrll 
    166  1.9.6.2  skrll void
    167  1.9.6.2  skrll bufq_put(struct bufq_state *bufq, struct buf *bp)
    168  1.9.6.2  skrll {
    169  1.9.6.2  skrll 
    170  1.9.6.2  skrll 	(*bufq->bq_put)(bufq, bp);
    171  1.9.6.2  skrll }
    172  1.9.6.2  skrll 
    173  1.9.6.2  skrll struct buf *
    174  1.9.6.2  skrll bufq_get(struct bufq_state *bufq)
    175  1.9.6.2  skrll {
    176  1.9.6.2  skrll 
    177  1.9.6.2  skrll 	return (*bufq->bq_get)(bufq, 1);
    178  1.9.6.2  skrll }
    179  1.9.6.2  skrll 
    180  1.9.6.2  skrll struct buf *
    181  1.9.6.2  skrll bufq_peek(struct bufq_state *bufq)
    182  1.9.6.2  skrll {
    183  1.9.6.2  skrll 
    184  1.9.6.2  skrll 	return (*bufq->bq_get)(bufq, 0);
    185  1.9.6.2  skrll }
    186  1.9.6.2  skrll 
    187  1.9.6.2  skrll /*
    188  1.9.6.2  skrll  * Drain a device buffer queue.
    189  1.9.6.2  skrll  */
    190  1.9.6.2  skrll void
    191  1.9.6.2  skrll bufq_drain(struct bufq_state *bufq)
    192  1.9.6.2  skrll {
    193  1.9.6.2  skrll 	struct buf *bp;
    194  1.9.6.2  skrll 
    195  1.9.6.2  skrll 	while ((bp = BUFQ_GET(bufq)) != NULL) {
    196  1.9.6.2  skrll 		bp->b_error = EIO;
    197  1.9.6.2  skrll 		bp->b_flags |= B_ERROR;
    198  1.9.6.2  skrll 		bp->b_resid = bp->b_bcount;
    199  1.9.6.2  skrll 		biodone(bp);
    200  1.9.6.2  skrll 	}
    201  1.9.6.2  skrll }
    202  1.9.6.2  skrll 
    203  1.9.6.2  skrll /*
    204  1.9.6.2  skrll  * Destroy a device buffer queue.
    205  1.9.6.2  skrll  */
    206  1.9.6.2  skrll void
    207  1.9.6.2  skrll bufq_free(struct bufq_state *bufq)
    208  1.9.6.2  skrll {
    209  1.9.6.2  skrll 
    210  1.9.6.2  skrll 	KASSERT(bufq->bq_private != NULL);
    211  1.9.6.2  skrll 	KASSERT(BUFQ_PEEK(bufq) == NULL);
    212  1.9.6.2  skrll 
    213  1.9.6.2  skrll 	free(bufq->bq_private, M_DEVBUF);
    214  1.9.6.2  skrll 	free(bufq, M_DEVBUF);
    215  1.9.6.2  skrll }
    216  1.9.6.2  skrll 
    217  1.9.6.2  skrll #if 0
    218  1.9.6.2  skrll /*
    219  1.9.6.2  skrll  * get a strategy identifier of a buffer queue.
    220  1.9.6.2  skrll  */
    221  1.9.6.2  skrll const char *
    222  1.9.6.2  skrll bufq_getstrategyname(struct bufq_state *bufq)
    223  1.9.6.2  skrll {
    224  1.9.6.2  skrll 
    225  1.9.6.2  skrll 	return bufq->bq_strat->bs_name;
    226  1.9.6.2  skrll }
    227  1.9.6.2  skrll 
    228  1.9.6.2  skrll /*
    229  1.9.6.2  skrll  * move all requests on a buffer queue to another.
    230  1.9.6.2  skrll  */
    231  1.9.6.2  skrll void
    232  1.9.6.2  skrll bufq_move(struct bufq_state *dst, struct bufq_state *src)
    233  1.9.6.2  skrll {
    234  1.9.6.2  skrll 	struct buf *bp;
    235  1.9.6.2  skrll 
    236  1.9.6.2  skrll 	while ((bp = BUFQ_GET(src)) != NULL) {
    237  1.9.6.2  skrll 		BUFQ_PUT(dst, bp);
    238  1.9.6.2  skrll 	}
    239  1.9.6.2  skrll }
    240  1.9.6.2  skrll #endif
    241  1.9.6.2  skrll 
    242  1.9.6.2  skrll static int
    243  1.9.6.2  skrll docopy(char *buf, size_t *bufoffp, size_t buflen,
    244  1.9.6.2  skrll     const char *datap, size_t datalen)
    245  1.9.6.2  skrll {
    246  1.9.6.2  skrll 	int error = 0;
    247  1.9.6.2  skrll 
    248  1.9.6.2  skrll 	if (buf != NULL && datalen > 0) {
    249  1.9.6.2  skrll 
    250  1.9.6.2  skrll 		if (*bufoffp + datalen > buflen) {
    251  1.9.6.2  skrll 			goto out;
    252  1.9.6.2  skrll 		}
    253  1.9.6.2  skrll 		error = copyout(datap, buf + *bufoffp, datalen);
    254  1.9.6.2  skrll 		if (error) {
    255  1.9.6.2  skrll 			goto out;
    256  1.9.6.2  skrll 		}
    257  1.9.6.2  skrll 	}
    258  1.9.6.2  skrll out:
    259  1.9.6.2  skrll 	if (error == 0) {
    260  1.9.6.2  skrll 		*bufoffp += datalen;
    261  1.9.6.2  skrll 	}
    262  1.9.6.2  skrll 
    263  1.9.6.2  skrll 	return error;
    264  1.9.6.2  skrll }
    265  1.9.6.2  skrll 
    266  1.9.6.2  skrll static int
    267  1.9.6.2  skrll docopystr(char *buf, size_t *bufoffp, size_t buflen, const char *datap)
    268  1.9.6.2  skrll {
    269  1.9.6.2  skrll 
    270  1.9.6.2  skrll 	return docopy(buf, bufoffp, buflen, datap, strlen(datap));
    271  1.9.6.2  skrll }
    272  1.9.6.2  skrll 
    273  1.9.6.2  skrll static int
    274  1.9.6.2  skrll docopynul(char *buf, size_t *bufoffp, size_t buflen)
    275  1.9.6.2  skrll {
    276  1.9.6.2  skrll 
    277  1.9.6.2  skrll 	return docopy(buf, bufoffp, buflen, "", 1);
    278  1.9.6.2  skrll }
    279  1.9.6.2  skrll 
    280  1.9.6.2  skrll /*
    281  1.9.6.2  skrll  * sysctl function that will print all bufq strategies
    282  1.9.6.2  skrll  * built in the kernel.
    283  1.9.6.2  skrll  */
    284  1.9.6.2  skrll static int
    285  1.9.6.2  skrll sysctl_kern_bufq_strategies(SYSCTLFN_ARGS)
    286  1.9.6.2  skrll {
    287  1.9.6.2  skrll 	__link_set_decl(bufq_strats, const struct bufq_strat);
    288  1.9.6.2  skrll 	const struct bufq_strat * const *bq_strat;
    289  1.9.6.2  skrll 	const char *delim = "";
    290  1.9.6.2  skrll 	size_t off = 0;
    291  1.9.6.2  skrll 	size_t buflen = *oldlenp;
    292  1.9.6.2  skrll 	int error;
    293  1.9.6.2  skrll 
    294  1.9.6.2  skrll 	__link_set_foreach(bq_strat, bufq_strats) {
    295  1.9.6.2  skrll 		if ((*bq_strat) == &bufq_strat_dummy) {
    296  1.9.6.2  skrll 			continue;
    297  1.9.6.2  skrll 		}
    298  1.9.6.2  skrll 		error = docopystr(oldp, &off, buflen, delim);
    299  1.9.6.2  skrll 		if (error) {
    300  1.9.6.2  skrll 			goto out;
    301  1.9.6.2  skrll 		}
    302  1.9.6.2  skrll 		error = docopystr(oldp, &off, buflen, (*bq_strat)->bs_name);
    303  1.9.6.2  skrll 		if (error) {
    304  1.9.6.2  skrll 			goto out;
    305  1.9.6.2  skrll 		}
    306  1.9.6.2  skrll 		delim = " ";
    307  1.9.6.2  skrll 	}
    308  1.9.6.2  skrll 
    309  1.9.6.2  skrll 	/* NUL terminate */
    310  1.9.6.2  skrll 	error = docopynul(oldp, &off, buflen);
    311  1.9.6.2  skrll out:
    312  1.9.6.2  skrll 	*oldlenp = off;
    313  1.9.6.2  skrll 	return error;
    314  1.9.6.2  skrll }
    315  1.9.6.2  skrll 
    316  1.9.6.2  skrll SYSCTL_SETUP(sysctl_kern_bufq_strategies_setup, "sysctl kern.bufq tree setup")
    317  1.9.6.2  skrll {
    318  1.9.6.2  skrll 	const struct sysctlnode *node;
    319  1.9.6.2  skrll 
    320  1.9.6.2  skrll 	sysctl_createv(clog, 0, NULL, NULL,
    321  1.9.6.2  skrll 			CTLFLAG_PERMANENT,
    322  1.9.6.2  skrll 			CTLTYPE_NODE, "kern", NULL,
    323  1.9.6.2  skrll 			NULL, 0, NULL, 0,
    324  1.9.6.2  skrll 			CTL_KERN, CTL_EOL);
    325  1.9.6.2  skrll 	node = NULL;
    326  1.9.6.2  skrll 	sysctl_createv(clog, 0, NULL, &node,
    327  1.9.6.2  skrll 			CTLFLAG_PERMANENT,
    328  1.9.6.2  skrll 			CTLTYPE_NODE, "bufq",
    329  1.9.6.2  skrll 			SYSCTL_DESCR("buffer queue subtree"),
    330  1.9.6.2  skrll 			NULL, 0, NULL, 0,
    331  1.9.6.2  skrll 			CTL_KERN, CTL_CREATE, CTL_EOL);
    332  1.9.6.2  skrll 	if (node != NULL) {
    333  1.9.6.2  skrll 		sysctl_createv(clog, 0, NULL, NULL,
    334  1.9.6.2  skrll 			CTLFLAG_PERMANENT,
    335  1.9.6.2  skrll 			CTLTYPE_STRING, "strategies",
    336  1.9.6.2  skrll 			SYSCTL_DESCR("List of bufq strategies present"),
    337  1.9.6.2  skrll 			sysctl_kern_bufq_strategies, 0, NULL, 0,
    338  1.9.6.2  skrll 			CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    339  1.9.6.2  skrll 	}
    340  1.9.6.2  skrll }
    341