Home | History | Annotate | Line # | Download | only in kern
      1  1.16     kamil /*	$NetBSD: bufq_readprio.c,v 1.16 2017/05/04 11:03:27 kamil Exp $	*/
      2   1.1      yamt /*	NetBSD: subr_disk.c,v 1.61 2004/09/25 03:30:44 thorpej Exp 	*/
      3   1.1      yamt 
      4   1.1      yamt /*-
      5   1.1      yamt  * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
      6   1.1      yamt  * All rights reserved.
      7   1.1      yamt  *
      8   1.1      yamt  * This code is derived from software contributed to The NetBSD Foundation
      9   1.1      yamt  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     10   1.1      yamt  * NASA Ames Research Center.
     11   1.1      yamt  *
     12   1.1      yamt  * Redistribution and use in source and binary forms, with or without
     13   1.1      yamt  * modification, are permitted provided that the following conditions
     14   1.1      yamt  * are met:
     15   1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     16   1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     17   1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     19   1.1      yamt  *    documentation and/or other materials provided with the distribution.
     20   1.1      yamt  *
     21   1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22   1.1      yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23   1.1      yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.1      yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25   1.1      yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.1      yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1      yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1      yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1      yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1      yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1      yamt  * POSSIBILITY OF SUCH DAMAGE.
     32   1.1      yamt  */
     33   1.1      yamt 
     34   1.1      yamt /*
     35   1.1      yamt  * Copyright (c) 1982, 1986, 1988, 1993
     36   1.1      yamt  *	The Regents of the University of California.  All rights reserved.
     37   1.1      yamt  * (c) UNIX System Laboratories, Inc.
     38   1.1      yamt  * All or some portions of this file are derived from material licensed
     39   1.1      yamt  * to the University of California by American Telephone and Telegraph
     40   1.1      yamt  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     41   1.1      yamt  * the permission of UNIX System Laboratories, Inc.
     42   1.1      yamt  *
     43   1.1      yamt  * Redistribution and use in source and binary forms, with or without
     44   1.1      yamt  * modification, are permitted provided that the following conditions
     45   1.1      yamt  * are met:
     46   1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     47   1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     48   1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     50   1.1      yamt  *    documentation and/or other materials provided with the distribution.
     51   1.1      yamt  * 3. Neither the name of the University nor the names of its contributors
     52   1.1      yamt  *    may be used to endorse or promote products derived from this software
     53   1.1      yamt  *    without specific prior written permission.
     54   1.1      yamt  *
     55   1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1      yamt  * SUCH DAMAGE.
     66   1.1      yamt  *
     67   1.1      yamt  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
     68   1.1      yamt  */
     69   1.1      yamt 
     70   1.1      yamt #include <sys/cdefs.h>
     71  1.16     kamil __KERNEL_RCSID(0, "$NetBSD: bufq_readprio.c,v 1.16 2017/05/04 11:03:27 kamil Exp $");
     72   1.1      yamt 
     73   1.1      yamt #include <sys/param.h>
     74   1.1      yamt #include <sys/systm.h>
     75   1.1      yamt #include <sys/buf.h>
     76   1.2      yamt #include <sys/bufq.h>
     77   1.5   xtraeme #include <sys/bufq_impl.h>
     78  1.13      yamt #include <sys/kmem.h>
     79  1.14  pgoyette #include <sys/module.h>
     80   1.1      yamt 
     81   1.1      yamt /*
     82   1.1      yamt  * Seek sort for disks.
     83   1.1      yamt  *
     84   1.1      yamt  * There are two queues.  The first queue holds read requests; the second
     85   1.1      yamt  * holds write requests.  The read queue is first-come first-served; the
     86   1.1      yamt  * write queue is sorted in ascendening block order.
     87   1.1      yamt  * The read queue is processed first.  After PRIO_READ_BURST consecutive
     88   1.1      yamt  * read requests with non-empty write queue PRIO_WRITE_REQ requests from
     89   1.1      yamt  * the write queue will be processed.
     90   1.1      yamt  */
     91   1.1      yamt 
     92   1.1      yamt #define PRIO_READ_BURST		48
     93   1.1      yamt #define PRIO_WRITE_REQ		16
     94   1.1      yamt 
     95   1.1      yamt struct bufq_prio {
     96   1.1      yamt 	TAILQ_HEAD(, buf) bq_read, bq_write; /* actual list of buffers */
     97   1.1      yamt 	struct buf *bq_write_next;	/* next request in bq_write */
     98   1.1      yamt 	struct buf *bq_next;		/* current request */
     99   1.1      yamt 	int bq_read_burst;		/* # of consecutive reads */
    100   1.1      yamt };
    101   1.1      yamt 
    102   1.3      yamt static void bufq_readprio_init(struct bufq_state *);
    103   1.3      yamt static void bufq_prio_put(struct bufq_state *, struct buf *);
    104   1.3      yamt static struct buf *bufq_prio_get(struct bufq_state *, int);
    105   1.3      yamt 
    106   1.4      yamt BUFQ_DEFINE(readprio, 30, bufq_readprio_init);
    107   1.3      yamt 
    108   1.1      yamt static void
    109   1.1      yamt bufq_prio_put(struct bufq_state *bufq, struct buf *bp)
    110   1.1      yamt {
    111  1.16     kamil 	struct bufq_prio *prio = bufq_private(bufq);
    112   1.1      yamt 	struct buf *bq;
    113   1.1      yamt 	int sortby;
    114   1.1      yamt 
    115   1.1      yamt 	sortby = bufq->bq_flags & BUFQ_SORT_MASK;
    116   1.1      yamt 
    117   1.1      yamt 	/*
    118   1.1      yamt 	 * If it's a read request append it to the list.
    119   1.1      yamt 	 */
    120   1.1      yamt 	if ((bp->b_flags & B_READ) == B_READ) {
    121   1.1      yamt 		TAILQ_INSERT_TAIL(&prio->bq_read, bp, b_actq);
    122   1.1      yamt 		return;
    123   1.1      yamt 	}
    124   1.1      yamt 
    125   1.1      yamt 	bq = TAILQ_FIRST(&prio->bq_write);
    126   1.1      yamt 
    127   1.1      yamt 	/*
    128   1.1      yamt 	 * If the write list is empty, simply append it to the list.
    129   1.1      yamt 	 */
    130   1.1      yamt 	if (bq == NULL) {
    131   1.1      yamt 		TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
    132   1.1      yamt 		prio->bq_write_next = bp;
    133   1.1      yamt 		return;
    134   1.1      yamt 	}
    135   1.1      yamt 
    136   1.1      yamt 	/*
    137   1.1      yamt 	 * If we lie after the next request, insert after this request.
    138   1.1      yamt 	 */
    139   1.1      yamt 	if (buf_inorder(prio->bq_write_next, bp, sortby))
    140   1.1      yamt 		bq = prio->bq_write_next;
    141   1.1      yamt 
    142   1.1      yamt 	/*
    143   1.1      yamt 	 * Search for the first request at a larger block number.
    144   1.1      yamt 	 * We go before this request if it exists.
    145   1.1      yamt 	 */
    146   1.1      yamt 	while (bq != NULL && buf_inorder(bq, bp, sortby))
    147   1.1      yamt 		bq = TAILQ_NEXT(bq, b_actq);
    148   1.1      yamt 
    149   1.1      yamt 	if (bq != NULL)
    150   1.1      yamt 		TAILQ_INSERT_BEFORE(bq, bp, b_actq);
    151   1.1      yamt 	else
    152   1.1      yamt 		TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
    153   1.1      yamt }
    154   1.1      yamt 
    155   1.1      yamt static struct buf *
    156   1.1      yamt bufq_prio_get(struct bufq_state *bufq, int remove)
    157   1.1      yamt {
    158  1.16     kamil 	struct bufq_prio *prio = bufq_private(bufq);
    159   1.1      yamt 	struct buf *bp;
    160   1.1      yamt 
    161   1.1      yamt 	/*
    162   1.1      yamt 	 * If no current request, get next from the lists.
    163   1.1      yamt 	 */
    164   1.1      yamt 	if (prio->bq_next == NULL) {
    165   1.1      yamt 		/*
    166   1.1      yamt 		 * If at least one list is empty, select the other.
    167   1.1      yamt 		 */
    168   1.1      yamt 		if (TAILQ_FIRST(&prio->bq_read) == NULL) {
    169   1.1      yamt 			prio->bq_next = prio->bq_write_next;
    170   1.1      yamt 			prio->bq_read_burst = 0;
    171   1.1      yamt 		} else if (prio->bq_write_next == NULL) {
    172   1.8  wrstuden 			bp = prio->bq_next = TAILQ_FIRST(&prio->bq_read);
    173   1.1      yamt 			prio->bq_read_burst = 0;
    174   1.9        ad 			KASSERT((bp == NULL) ||
    175   1.9        ad 			    ((bp->b_flags & B_READ) == B_READ));
    176   1.1      yamt 		} else {
    177   1.1      yamt 			/*
    178   1.1      yamt 			 * Both list have requests.  Select the read list up
    179   1.1      yamt 			 * to PRIO_READ_BURST times, then select the write
    180   1.1      yamt 			 * list PRIO_WRITE_REQ times.
    181   1.1      yamt 			 */
    182   1.1      yamt 			if (prio->bq_read_burst++ < PRIO_READ_BURST)
    183   1.1      yamt 				prio->bq_next = TAILQ_FIRST(&prio->bq_read);
    184   1.1      yamt 			else if (prio->bq_read_burst <
    185   1.1      yamt 			    PRIO_READ_BURST + PRIO_WRITE_REQ)
    186   1.1      yamt 				prio->bq_next = prio->bq_write_next;
    187   1.1      yamt 			else {
    188   1.1      yamt 				prio->bq_next = TAILQ_FIRST(&prio->bq_read);
    189   1.1      yamt 				prio->bq_read_burst = 0;
    190   1.1      yamt 			}
    191   1.1      yamt 		}
    192   1.1      yamt 	}
    193   1.1      yamt 
    194   1.1      yamt 	bp = prio->bq_next;
    195   1.1      yamt 
    196   1.1      yamt 	if (bp != NULL && remove) {
    197   1.1      yamt 		if ((bp->b_flags & B_READ) == B_READ)
    198   1.1      yamt 			TAILQ_REMOVE(&prio->bq_read, bp, b_actq);
    199   1.1      yamt 		else {
    200   1.1      yamt 			/*
    201   1.1      yamt 			 * Advance the write pointer before removing
    202   1.1      yamt 			 * bp since it is actually prio->bq_write_next.
    203   1.1      yamt 			 */
    204   1.1      yamt 			prio->bq_write_next =
    205   1.1      yamt 			    TAILQ_NEXT(prio->bq_write_next, b_actq);
    206   1.1      yamt 			TAILQ_REMOVE(&prio->bq_write, bp, b_actq);
    207   1.1      yamt 			if (prio->bq_write_next == NULL)
    208   1.1      yamt 				prio->bq_write_next =
    209   1.1      yamt 				    TAILQ_FIRST(&prio->bq_write);
    210   1.1      yamt 		}
    211   1.1      yamt 
    212   1.1      yamt 		prio->bq_next = NULL;
    213   1.1      yamt 	}
    214   1.1      yamt 
    215   1.1      yamt 	return (bp);
    216   1.1      yamt }
    217   1.1      yamt 
    218  1.11   reinoud static struct buf *
    219  1.11   reinoud bufq_prio_cancel(struct bufq_state *bufq, struct buf *buf)
    220  1.11   reinoud {
    221  1.16     kamil 	struct bufq_prio *prio = bufq_private(bufq);
    222  1.11   reinoud 	struct buf *bq;
    223  1.11   reinoud 
    224  1.11   reinoud 	/* search read queue */
    225  1.12      yamt 	TAILQ_FOREACH(bq, &prio->bq_read, b_actq) {
    226  1.11   reinoud 		if (bq == buf) {
    227  1.11   reinoud 			TAILQ_REMOVE(&prio->bq_read, bq, b_actq);
    228  1.11   reinoud 			/* force new section */
    229  1.11   reinoud 			prio->bq_next = NULL;
    230  1.11   reinoud 			return buf;
    231  1.11   reinoud 		}
    232  1.11   reinoud 	}
    233  1.11   reinoud 
    234  1.11   reinoud 	/* not found in read queue, search write queue */
    235  1.12      yamt 	TAILQ_FOREACH(bq, &prio->bq_write, b_actq) {
    236  1.11   reinoud 		if (bq == buf) {
    237  1.11   reinoud 			if (bq == prio->bq_write_next) {
    238  1.11   reinoud 				/*
    239  1.11   reinoud 				 * Advance the write pointer before removing
    240  1.11   reinoud 				 * bp since it is actually prio->bq_write_next.
    241  1.11   reinoud 				 */
    242  1.11   reinoud 				prio->bq_write_next =
    243  1.11   reinoud 				    TAILQ_NEXT(prio->bq_write_next, b_actq);
    244  1.11   reinoud 				TAILQ_REMOVE(&prio->bq_write, bq, b_actq);
    245  1.11   reinoud 				if (prio->bq_write_next == NULL)
    246  1.11   reinoud 					prio->bq_write_next =
    247  1.11   reinoud 					    TAILQ_FIRST(&prio->bq_write);
    248  1.11   reinoud 			} else {
    249  1.11   reinoud 				TAILQ_REMOVE(&prio->bq_write, bq, b_actq);
    250  1.11   reinoud 			}
    251  1.11   reinoud 			/* force new section */
    252  1.11   reinoud 			prio->bq_next = NULL;
    253  1.11   reinoud 			return buf;
    254  1.11   reinoud 		}
    255  1.11   reinoud 	}
    256  1.11   reinoud 
    257  1.11   reinoud 	/* still not found */
    258  1.11   reinoud 	return NULL;
    259  1.11   reinoud }
    260  1.11   reinoud 
    261   1.3      yamt static void
    262  1.13      yamt bufq_prio_fini(struct bufq_state *bufq)
    263  1.13      yamt {
    264  1.13      yamt 
    265  1.13      yamt 	KASSERT(bufq->bq_private != NULL);
    266  1.13      yamt 	kmem_free(bufq->bq_private, sizeof(struct bufq_prio));
    267  1.13      yamt }
    268  1.13      yamt 
    269  1.13      yamt static void
    270   1.1      yamt bufq_readprio_init(struct bufq_state *bufq)
    271   1.1      yamt {
    272   1.1      yamt 	struct bufq_prio *prio;
    273   1.1      yamt 
    274   1.1      yamt 	bufq->bq_get = bufq_prio_get;
    275   1.1      yamt 	bufq->bq_put = bufq_prio_put;
    276  1.11   reinoud 	bufq->bq_cancel = bufq_prio_cancel;
    277  1.13      yamt 	bufq->bq_fini = bufq_prio_fini;
    278  1.13      yamt 	bufq->bq_private = kmem_zalloc(sizeof(struct bufq_prio), KM_SLEEP);
    279   1.1      yamt 	prio = (struct bufq_prio *)bufq->bq_private;
    280   1.1      yamt 	TAILQ_INIT(&prio->bq_read);
    281   1.1      yamt 	TAILQ_INIT(&prio->bq_write);
    282   1.1      yamt }
    283   1.1      yamt 
    284  1.15  pgoyette MODULE(MODULE_CLASS_BUFQ, bufq_readprio, NULL);
    285  1.14  pgoyette 
    286  1.14  pgoyette static int
    287  1.14  pgoyette bufq_readprio_modcmd(modcmd_t cmd, void *opaque)
    288  1.14  pgoyette {
    289  1.14  pgoyette 
    290  1.14  pgoyette         switch (cmd) {
    291  1.14  pgoyette         case MODULE_CMD_INIT:
    292  1.14  pgoyette                 return bufq_register(&bufq_strat_readprio);
    293  1.14  pgoyette         case MODULE_CMD_FINI:
    294  1.14  pgoyette                 return bufq_unregister(&bufq_strat_readprio);
    295  1.14  pgoyette         default:
    296  1.14  pgoyette                 return ENOTTY;
    297  1.14  pgoyette         }
    298  1.14  pgoyette }
    299