1/*	$NetBSD: bufq_readprio.c,v 1.16 2017/05/04 11:03:27 kamil Exp $	*/
2/*	NetBSD: subr_disk.c,v 1.61 2004/09/25 03:30:44 thorpej 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: bufq_readprio.c,v 1.16 2017/05/04 11:03:27 kamil Exp $");
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/buf.h>
76#include <sys/bufq.h>
77#include <sys/bufq_impl.h>
78#include <sys/kmem.h>
79#include <sys/module.h>
80
81/*
82 * Seek sort for disks.
83 *
84 * There are two queues.  The first queue holds read requests; the second
85 * holds write requests.  The read queue is first-come first-served; the
86 * write queue is sorted in ascendening block order.
87 * The read queue is processed first.  After PRIO_READ_BURST consecutive
88 * read requests with non-empty write queue PRIO_WRITE_REQ requests from
89 * the write queue will be processed.
90 */
91
92#define PRIO_READ_BURST		48
93#define PRIO_WRITE_REQ		16
94
95struct bufq_prio {
96	TAILQ_HEAD(, buf) bq_read, bq_write; /* actual list of buffers */
97	struct buf *bq_write_next;	/* next request in bq_write */
98	struct buf *bq_next;		/* current request */
99	int bq_read_burst;		/* # of consecutive reads */
100};
101
102static void bufq_readprio_init(struct bufq_state *);
103static void bufq_prio_put(struct bufq_state *, struct buf *);
104static struct buf *bufq_prio_get(struct bufq_state *, int);
105
106BUFQ_DEFINE(readprio, 30, bufq_readprio_init);
107
108static void
109bufq_prio_put(struct bufq_state *bufq, struct buf *bp)
110{
111	struct bufq_prio *prio = bufq_private(bufq);
112	struct buf *bq;
113	int sortby;
114
115	sortby = bufq->bq_flags & BUFQ_SORT_MASK;
116
117	/*
118	 * If it's a read request append it to the list.
119	 */
120	if ((bp->b_flags & B_READ) == B_READ) {
121		TAILQ_INSERT_TAIL(&prio->bq_read, bp, b_actq);
122		return;
123	}
124
125	bq = TAILQ_FIRST(&prio->bq_write);
126
127	/*
128	 * If the write list is empty, simply append it to the list.
129	 */
130	if (bq == NULL) {
131		TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
132		prio->bq_write_next = bp;
133		return;
134	}
135
136	/*
137	 * If we lie after the next request, insert after this request.
138	 */
139	if (buf_inorder(prio->bq_write_next, bp, sortby))
140		bq = prio->bq_write_next;
141
142	/*
143	 * Search for the first request at a larger block number.
144	 * We go before this request if it exists.
145	 */
146	while (bq != NULL && buf_inorder(bq, bp, sortby))
147		bq = TAILQ_NEXT(bq, b_actq);
148
149	if (bq != NULL)
150		TAILQ_INSERT_BEFORE(bq, bp, b_actq);
151	else
152		TAILQ_INSERT_TAIL(&prio->bq_write, bp, b_actq);
153}
154
155static struct buf *
156bufq_prio_get(struct bufq_state *bufq, int remove)
157{
158	struct bufq_prio *prio = bufq_private(bufq);
159	struct buf *bp;
160
161	/*
162	 * If no current request, get next from the lists.
163	 */
164	if (prio->bq_next == NULL) {
165		/*
166		 * If at least one list is empty, select the other.
167		 */
168		if (TAILQ_FIRST(&prio->bq_read) == NULL) {
169			prio->bq_next = prio->bq_write_next;
170			prio->bq_read_burst = 0;
171		} else if (prio->bq_write_next == NULL) {
172			bp = prio->bq_next = TAILQ_FIRST(&prio->bq_read);
173			prio->bq_read_burst = 0;
174			KASSERT((bp == NULL) ||
175			    ((bp->b_flags & B_READ) == B_READ));
176		} else {
177			/*
178			 * Both list have requests.  Select the read list up
179			 * to PRIO_READ_BURST times, then select the write
180			 * list PRIO_WRITE_REQ times.
181			 */
182			if (prio->bq_read_burst++ < PRIO_READ_BURST)
183				prio->bq_next = TAILQ_FIRST(&prio->bq_read);
184			else if (prio->bq_read_burst <
185			    PRIO_READ_BURST + PRIO_WRITE_REQ)
186				prio->bq_next = prio->bq_write_next;
187			else {
188				prio->bq_next = TAILQ_FIRST(&prio->bq_read);
189				prio->bq_read_burst = 0;
190			}
191		}
192	}
193
194	bp = prio->bq_next;
195
196	if (bp != NULL && remove) {
197		if ((bp->b_flags & B_READ) == B_READ)
198			TAILQ_REMOVE(&prio->bq_read, bp, b_actq);
199		else {
200			/*
201			 * Advance the write pointer before removing
202			 * bp since it is actually prio->bq_write_next.
203			 */
204			prio->bq_write_next =
205			    TAILQ_NEXT(prio->bq_write_next, b_actq);
206			TAILQ_REMOVE(&prio->bq_write, bp, b_actq);
207			if (prio->bq_write_next == NULL)
208				prio->bq_write_next =
209				    TAILQ_FIRST(&prio->bq_write);
210		}
211
212		prio->bq_next = NULL;
213	}
214
215	return (bp);
216}
217
218static struct buf *
219bufq_prio_cancel(struct bufq_state *bufq, struct buf *buf)
220{
221	struct bufq_prio *prio = bufq_private(bufq);
222	struct buf *bq;
223
224	/* search read queue */
225	TAILQ_FOREACH(bq, &prio->bq_read, b_actq) {
226		if (bq == buf) {
227			TAILQ_REMOVE(&prio->bq_read, bq, b_actq);
228			/* force new section */
229			prio->bq_next = NULL;
230			return buf;
231		}
232	}
233
234	/* not found in read queue, search write queue */
235	TAILQ_FOREACH(bq, &prio->bq_write, b_actq) {
236		if (bq == buf) {
237			if (bq == prio->bq_write_next) {
238				/*
239				 * Advance the write pointer before removing
240				 * bp since it is actually prio->bq_write_next.
241				 */
242				prio->bq_write_next =
243				    TAILQ_NEXT(prio->bq_write_next, b_actq);
244				TAILQ_REMOVE(&prio->bq_write, bq, b_actq);
245				if (prio->bq_write_next == NULL)
246					prio->bq_write_next =
247					    TAILQ_FIRST(&prio->bq_write);
248			} else {
249				TAILQ_REMOVE(&prio->bq_write, bq, b_actq);
250			}
251			/* force new section */
252			prio->bq_next = NULL;
253			return buf;
254		}
255	}
256
257	/* still not found */
258	return NULL;
259}
260
261static void
262bufq_prio_fini(struct bufq_state *bufq)
263{
264
265	KASSERT(bufq->bq_private != NULL);
266	kmem_free(bufq->bq_private, sizeof(struct bufq_prio));
267}
268
269static void
270bufq_readprio_init(struct bufq_state *bufq)
271{
272	struct bufq_prio *prio;
273
274	bufq->bq_get = bufq_prio_get;
275	bufq->bq_put = bufq_prio_put;
276	bufq->bq_cancel = bufq_prio_cancel;
277	bufq->bq_fini = bufq_prio_fini;
278	bufq->bq_private = kmem_zalloc(sizeof(struct bufq_prio), KM_SLEEP);
279	prio = (struct bufq_prio *)bufq->bq_private;
280	TAILQ_INIT(&prio->bq_read);
281	TAILQ_INIT(&prio->bq_write);
282}
283
284MODULE(MODULE_CLASS_BUFQ, bufq_readprio, NULL);
285
286static int
287bufq_readprio_modcmd(modcmd_t cmd, void *opaque)
288{
289
290        switch (cmd) {
291        case MODULE_CMD_INIT:
292                return bufq_register(&bufq_strat_readprio);
293        case MODULE_CMD_FINI:
294                return bufq_unregister(&bufq_strat_readprio);
295        default:
296                return ENOTTY;
297        }
298}
299