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