Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: subr_bufq.c,v 1.29 2026/01/04 02:11:09 riastradh 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  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Copyright (c) 1982, 1986, 1988, 1993
     36  *	The Regents of the University of California.  All rights reserved.
     37  * (c) UNIX System Laboratories, Inc.
     38  * All or some portions of this file are derived from material licensed
     39  * to the University of California by American Telephone and Telegraph
     40  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     41  * the permission of UNIX System Laboratories, Inc.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     68  */
     69 
     70 #include <sys/cdefs.h>
     71 __KERNEL_RCSID(0, "$NetBSD: subr_bufq.c,v 1.29 2026/01/04 02:11:09 riastradh Exp $");
     72 
     73 #include <sys/param.h>
     74 #include <sys/types.h>
     75 
     76 #include <sys/buf.h>
     77 #include <sys/bufq.h>
     78 #include <sys/bufq_impl.h>
     79 #include <sys/kmem.h>
     80 #include <sys/module.h>
     81 #include <sys/sdt.h>
     82 #include <sys/sysctl.h>
     83 #include <sys/systm.h>
     84 
     85 #define	STRAT_MATCH(id, bs)	(strcmp((id), (bs)->bs_name) == 0)
     86 
     87 static void sysctl_kern_bufq_strategies_setup(struct sysctllog **);
     88 static SLIST_HEAD(, bufq_strat) bufq_strat_list =
     89     SLIST_HEAD_INITIALIZER(bufq_strat_list);
     90 
     91 static kmutex_t bufq_mutex;
     92 
     93 static struct sysctllog *sysctllog;
     94 
     95 void
     96 bufq_init(void)
     97 {
     98 
     99 	mutex_init(&bufq_mutex, MUTEX_DEFAULT, IPL_NONE);
    100 	sysctl_kern_bufq_strategies_setup(&sysctllog);
    101 }
    102 
    103 int
    104 bufq_register(struct bufq_strat *bs)
    105 {
    106 
    107 	mutex_enter(&bufq_mutex);
    108 	SLIST_INSERT_HEAD(&bufq_strat_list, bs, bs_next);
    109 	bs->bs_refcnt = 0;
    110 	mutex_exit(&bufq_mutex);
    111 
    112 	return 0;
    113 }
    114 
    115 int
    116 bufq_unregister(struct bufq_strat *bs)
    117 {
    118 
    119 	mutex_enter(&bufq_mutex);
    120 	if (bs->bs_refcnt != 0) {
    121 		mutex_exit(&bufq_mutex);
    122 		return SET_ERROR(EBUSY);
    123 	}
    124 	SLIST_REMOVE(&bufq_strat_list, bs, bufq_strat, bs_next);
    125 	mutex_exit(&bufq_mutex);
    126 
    127 	return 0;
    128 }
    129 
    130 /*
    131  * Create a device buffer queue.
    132  */
    133 int
    134 bufq_alloc(struct bufq_state **bufqp, const char *strategy, int flags)
    135 {
    136 	struct bufq_strat *bsp, *it;
    137 	struct bufq_state *bufq;
    138 	int error = 0;
    139 	u_int gen;
    140 	bool found_exact;
    141 	char strategy_module_name[MAXPATHLEN];
    142 
    143 	KASSERT((flags & BUFQ_EXACT) == 0 || strategy != BUFQ_STRAT_ANY);
    144 
    145 	switch (flags & BUFQ_SORT_MASK) {
    146 	case BUFQ_SORT_RAWBLOCK:
    147 	case BUFQ_SORT_CYLINDER:
    148 		break;
    149 	case 0:
    150 		/*
    151 		 * for strategies which don't care about block numbers.
    152 		 * eg. fcfs
    153 		 */
    154 		flags |= BUFQ_SORT_RAWBLOCK;
    155 		break;
    156 	default:
    157 		panic("bufq_alloc: sort out of range");
    158 	}
    159 
    160 	/*
    161 	 * select strategy.
    162 	 * if a strategy specified by flags is found, use it.
    163 	 * otherwise, select one with the largest bs_prio.
    164 	 */
    165 	mutex_enter(&bufq_mutex);
    166 	do {
    167 		gen = module_gen;
    168 		bsp = NULL;
    169 		found_exact = false;
    170 
    171 		SLIST_FOREACH(it, &bufq_strat_list, bs_next) {
    172 			if (strategy != BUFQ_STRAT_ANY &&
    173 			    STRAT_MATCH(strategy, (it))) {
    174 				bsp = it;
    175 				found_exact = true;
    176 				break;
    177 			}
    178 			if (bsp == NULL || (it)->bs_prio > bsp->bs_prio)
    179 				bsp = it;
    180 		}
    181 		if (strategy == BUFQ_STRAT_ANY || found_exact)
    182 			break;
    183 
    184 		/* Try to autoload the bufq strategy module */
    185 		strlcpy(strategy_module_name, "bufq_",
    186 			sizeof(strategy_module_name));
    187 		strlcat(strategy_module_name, strategy,
    188 			sizeof(strategy_module_name));
    189 		mutex_exit(&bufq_mutex);
    190 		(void) module_autoload(strategy_module_name, MODULE_CLASS_BUFQ);
    191 		mutex_enter(&bufq_mutex);
    192 	} while (gen != module_gen);
    193 
    194 	if (bsp == NULL) {
    195 		panic("bufq_alloc: no strategy");
    196 	}
    197 	if (strategy != BUFQ_STRAT_ANY && !found_exact) {
    198 		if ((flags & BUFQ_EXACT)) {
    199 			error = SET_ERROR(ENOENT);
    200 			mutex_exit(&bufq_mutex);
    201 			goto out;
    202 		}
    203 #if defined(DEBUG)
    204 		printf("bufq_alloc: '%s' is not available. using '%s'.\n",
    205 		    strategy, bsp->bs_name);
    206 #endif
    207 	}
    208 #if defined(BUFQ_DEBUG)
    209 	/* XXX aprint? */
    210 	printf("bufq_alloc: using '%s'\n", bsp->bs_name);
    211 #endif
    212 
    213 	bsp->bs_refcnt++;
    214 	mutex_exit(&bufq_mutex);
    215 	*bufqp = bufq = kmem_zalloc(sizeof(*bufq), KM_SLEEP);
    216 	bufq->bq_flags = flags;
    217 	bufq->bq_strat = bsp;
    218 	(*bsp->bs_initfn)(bufq);
    219 
    220 out:
    221 	return error;
    222 }
    223 
    224 void
    225 bufq_put(struct bufq_state *bufq, struct buf *bp)
    226 {
    227 
    228 	(*bufq->bq_put)(bufq, bp);
    229 }
    230 
    231 struct buf *
    232 bufq_get(struct bufq_state *bufq)
    233 {
    234 
    235 	return (*bufq->bq_get)(bufq, 1);
    236 }
    237 
    238 struct buf *
    239 bufq_peek(struct bufq_state *bufq)
    240 {
    241 
    242 	return (*bufq->bq_get)(bufq, 0);
    243 }
    244 
    245 struct buf *
    246 bufq_cancel(struct bufq_state *bufq, struct buf *bp)
    247 {
    248 
    249 	return (*bufq->bq_cancel)(bufq, bp);
    250 }
    251 
    252 /*
    253  * Drain a device buffer queue.
    254  */
    255 void
    256 bufq_drain(struct bufq_state *bufq)
    257 {
    258 	struct buf *bp;
    259 
    260 	while ((bp = bufq_get(bufq)) != NULL) {
    261 		bp->b_error = SET_ERROR(EIO);
    262 		bp->b_resid = bp->b_bcount;
    263 		biodone(bp);
    264 	}
    265 }
    266 
    267 /*
    268  * Destroy a device buffer queue.
    269  */
    270 void
    271 bufq_free(struct bufq_state *bufq)
    272 {
    273 
    274 	KASSERT(bufq_peek(bufq) == NULL);
    275 
    276 	bufq->bq_fini(bufq);
    277 
    278 	mutex_enter(&bufq_mutex);
    279 	bufq->bq_strat->bs_refcnt--;
    280 	mutex_exit(&bufq_mutex);
    281 
    282 	kmem_free(bufq, sizeof(*bufq));
    283 }
    284 
    285 /*
    286  * get a strategy identifier of a buffer queue.
    287  */
    288 const char *
    289 bufq_getstrategyname(struct bufq_state *bufq)
    290 {
    291 
    292 	return bufq->bq_strat->bs_name;
    293 }
    294 
    295 /*
    296  * move all requests on a buffer queue to another.
    297  */
    298 void
    299 bufq_move(struct bufq_state *dst, struct bufq_state *src)
    300 {
    301 	struct buf *bp;
    302 
    303 	while ((bp = bufq_get(src)) != NULL) {
    304 		bufq_put(dst, bp);
    305 	}
    306 }
    307 
    308 static int
    309 docopy(char *buf, size_t *bufoffp, size_t buflen,
    310     const char *datap, size_t datalen)
    311 {
    312 	int error = 0;
    313 
    314 	if (buf != NULL && datalen > 0) {
    315 
    316 		if (*bufoffp + datalen > buflen) {
    317 			goto out;
    318 		}
    319 		error = copyout(datap, buf + *bufoffp, datalen);
    320 		if (error) {
    321 			goto out;
    322 		}
    323 	}
    324 out:
    325 	if (error == 0) {
    326 		*bufoffp += datalen;
    327 	}
    328 
    329 	return error;
    330 }
    331 
    332 static int
    333 docopystr(char *buf, size_t *bufoffp, size_t buflen, const char *datap)
    334 {
    335 
    336 	return docopy(buf, bufoffp, buflen, datap, strlen(datap));
    337 }
    338 
    339 static int
    340 docopynul(char *buf, size_t *bufoffp, size_t buflen)
    341 {
    342 
    343 	return docopy(buf, bufoffp, buflen, "", 1);
    344 }
    345 
    346 /*
    347  * sysctl function that will print all bufq strategies
    348  * currently available to the kernel.
    349  */
    350 static int
    351 sysctl_kern_bufq_strategies(SYSCTLFN_ARGS)
    352 {
    353 	const struct bufq_strat *bq_strat;
    354 	const char *delim = "";
    355 	size_t off = 0;
    356 	size_t buflen = *oldlenp;
    357 	int error;
    358 
    359 	SLIST_FOREACH(bq_strat, &bufq_strat_list, bs_next) {
    360 		error = docopystr(oldp, &off, buflen, delim);
    361 		if (error) {
    362 			goto out;
    363 		}
    364 		error = docopystr(oldp, &off, buflen, (bq_strat)->bs_name);
    365 		if (error) {
    366 			goto out;
    367 		}
    368 		delim = " ";
    369 	}
    370 
    371 	/* In case there are no registered strategies ... */
    372 	if (off == 0) {
    373 		error = docopystr(oldp, &off, buflen, "NULL");
    374 		if (error) {
    375 			goto out;
    376 		}
    377 	}
    378 
    379 	/* NUL terminate */
    380 	error = docopynul(oldp, &off, buflen);
    381 out:
    382 	*oldlenp = off;
    383 	return error;
    384 }
    385 
    386 static void
    387 sysctl_kern_bufq_strategies_setup(struct sysctllog **clog)
    388 {
    389 	const struct sysctlnode *node;
    390 
    391 	node = NULL;
    392 	sysctl_createv(clog, 0, NULL, &node,
    393 			CTLFLAG_PERMANENT,
    394 			CTLTYPE_NODE, "bufq",
    395 			SYSCTL_DESCR("buffer queue subtree"),
    396 			NULL, 0, NULL, 0,
    397 			CTL_KERN, CTL_CREATE, CTL_EOL);
    398 	if (node != NULL) {
    399 		sysctl_createv(clog, 0, NULL, NULL,
    400 			CTLFLAG_PERMANENT,
    401 			CTLTYPE_STRING, "strategies",
    402 			SYSCTL_DESCR("List of bufq strategies present"),
    403 			sysctl_kern_bufq_strategies, 0, NULL, 0,
    404 			CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
    405 	}
    406 }
    407