bufq_disksort.c revision 1.12 1 1.12 pgoyette /* $NetBSD: bufq_disksort.c,v 1.12 2016/11/16 00:46:46 pgoyette 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.12 pgoyette __KERNEL_RCSID(0, "$NetBSD: bufq_disksort.c,v 1.12 2016/11/16 00:46:46 pgoyette 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.4 yamt #include <sys/bufq_impl.h>
78 1.11 yamt #include <sys/kmem.h>
79 1.12 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 actually two queues, sorted in ascendening order. The first
85 1.1 yamt * queue holds those requests which are positioned after the current block;
86 1.1 yamt * the second holds requests which came in after their position was passed.
87 1.1 yamt * Thus we implement a one-way scan, retracting after reaching the end of
88 1.1 yamt * the drive to the first request on the second queue, at which time it
89 1.1 yamt * becomes the first queue.
90 1.1 yamt *
91 1.1 yamt * A one-way scan is natural because of the way UNIX read-ahead blocks are
92 1.1 yamt * allocated.
93 1.1 yamt */
94 1.1 yamt
95 1.1 yamt struct bufq_disksort {
96 1.1 yamt TAILQ_HEAD(, buf) bq_head; /* actual list of buffers */
97 1.1 yamt };
98 1.1 yamt
99 1.3 yamt static void bufq_disksort_init(struct bufq_state *);
100 1.3 yamt static void bufq_disksort_put(struct bufq_state *, struct buf *);
101 1.3 yamt static struct buf *bufq_disksort_get(struct bufq_state *, int);
102 1.3 yamt
103 1.4 yamt BUFQ_DEFINE(disksort, 20, bufq_disksort_init);
104 1.3 yamt
105 1.1 yamt static void
106 1.1 yamt bufq_disksort_put(struct bufq_state *bufq, struct buf *bp)
107 1.1 yamt {
108 1.4 yamt struct bufq_disksort *disksort = bufq_private(bufq);
109 1.1 yamt struct buf *bq, *nbq;
110 1.1 yamt int sortby;
111 1.1 yamt
112 1.1 yamt sortby = bufq->bq_flags & BUFQ_SORT_MASK;
113 1.1 yamt
114 1.1 yamt bq = TAILQ_FIRST(&disksort->bq_head);
115 1.1 yamt
116 1.1 yamt /*
117 1.1 yamt * If the queue is empty it's easy; we just go on the end.
118 1.1 yamt */
119 1.1 yamt if (bq == NULL) {
120 1.1 yamt TAILQ_INSERT_TAIL(&disksort->bq_head, bp, b_actq);
121 1.1 yamt return;
122 1.1 yamt }
123 1.1 yamt
124 1.1 yamt /*
125 1.1 yamt * If we lie before the currently active request, then we
126 1.1 yamt * must locate the second request list and add ourselves to it.
127 1.1 yamt */
128 1.1 yamt if (buf_inorder(bp, bq, sortby)) {
129 1.1 yamt while ((nbq = TAILQ_NEXT(bq, b_actq)) != NULL) {
130 1.1 yamt /*
131 1.1 yamt * Check for an ``inversion'' in the normally ascending
132 1.1 yamt * block numbers, indicating the start of the second
133 1.1 yamt * request list.
134 1.1 yamt */
135 1.1 yamt if (buf_inorder(nbq, bq, sortby)) {
136 1.1 yamt /*
137 1.1 yamt * Search the second request list for the first
138 1.1 yamt * request at a larger block number. We go
139 1.1 yamt * after that; if there is no such request, we
140 1.1 yamt * go at the end.
141 1.1 yamt */
142 1.1 yamt do {
143 1.1 yamt if (buf_inorder(bp, nbq, sortby))
144 1.1 yamt goto insert;
145 1.1 yamt bq = nbq;
146 1.1 yamt } while ((nbq =
147 1.1 yamt TAILQ_NEXT(bq, b_actq)) != NULL);
148 1.1 yamt goto insert; /* after last */
149 1.1 yamt }
150 1.1 yamt bq = nbq;
151 1.1 yamt }
152 1.1 yamt /*
153 1.1 yamt * No inversions... we will go after the last, and
154 1.1 yamt * be the first request in the second request list.
155 1.1 yamt */
156 1.1 yamt goto insert;
157 1.1 yamt }
158 1.1 yamt /*
159 1.1 yamt * Request is at/after the current request...
160 1.1 yamt * sort in the first request list.
161 1.1 yamt */
162 1.1 yamt while ((nbq = TAILQ_NEXT(bq, b_actq)) != NULL) {
163 1.1 yamt /*
164 1.1 yamt * We want to go after the current request if there is an
165 1.1 yamt * inversion after it (i.e. it is the end of the first
166 1.1 yamt * request list), or if the next request is a larger cylinder
167 1.1 yamt * than our request.
168 1.1 yamt */
169 1.1 yamt if (buf_inorder(nbq, bq, sortby) ||
170 1.1 yamt buf_inorder(bp, nbq, sortby))
171 1.1 yamt goto insert;
172 1.1 yamt bq = nbq;
173 1.1 yamt }
174 1.1 yamt /*
175 1.1 yamt * Neither a second list nor a larger request... we go at the end of
176 1.1 yamt * the first list, which is the same as the end of the whole schebang.
177 1.1 yamt */
178 1.1 yamt insert: TAILQ_INSERT_AFTER(&disksort->bq_head, bq, bp, b_actq);
179 1.1 yamt }
180 1.1 yamt
181 1.1 yamt static struct buf *
182 1.1 yamt bufq_disksort_get(struct bufq_state *bufq, int remove)
183 1.1 yamt {
184 1.1 yamt struct bufq_disksort *disksort = bufq->bq_private;
185 1.1 yamt struct buf *bp;
186 1.1 yamt
187 1.1 yamt bp = TAILQ_FIRST(&disksort->bq_head);
188 1.1 yamt
189 1.1 yamt if (bp != NULL && remove)
190 1.1 yamt TAILQ_REMOVE(&disksort->bq_head, bp, b_actq);
191 1.1 yamt
192 1.1 yamt return (bp);
193 1.1 yamt }
194 1.1 yamt
195 1.9 reinoud static struct buf *
196 1.9 reinoud bufq_disksort_cancel(struct bufq_state *bufq, struct buf *buf)
197 1.9 reinoud {
198 1.9 reinoud struct bufq_disksort *disksort = bufq->bq_private;
199 1.9 reinoud struct buf *bq;
200 1.9 reinoud
201 1.10 yamt TAILQ_FOREACH(bq, &disksort->bq_head, b_actq) {
202 1.9 reinoud if (bq == buf) {
203 1.9 reinoud TAILQ_REMOVE(&disksort->bq_head, bq, b_actq);
204 1.9 reinoud return buf;
205 1.9 reinoud }
206 1.9 reinoud }
207 1.9 reinoud return NULL;
208 1.9 reinoud }
209 1.9 reinoud
210 1.3 yamt static void
211 1.11 yamt bufq_disksort_fini(struct bufq_state *bufq)
212 1.11 yamt {
213 1.11 yamt
214 1.11 yamt KASSERT(bufq->bq_private != NULL);
215 1.11 yamt kmem_free(bufq->bq_private, sizeof(struct bufq_disksort));
216 1.11 yamt }
217 1.11 yamt
218 1.11 yamt static void
219 1.1 yamt bufq_disksort_init(struct bufq_state *bufq)
220 1.1 yamt {
221 1.1 yamt struct bufq_disksort *disksort;
222 1.1 yamt
223 1.11 yamt disksort = kmem_zalloc(sizeof(*disksort), KM_SLEEP);
224 1.7 cbiere bufq->bq_private = disksort;
225 1.1 yamt bufq->bq_get = bufq_disksort_get;
226 1.1 yamt bufq->bq_put = bufq_disksort_put;
227 1.9 reinoud bufq->bq_cancel = bufq_disksort_cancel;
228 1.11 yamt bufq->bq_fini = bufq_disksort_fini;
229 1.1 yamt TAILQ_INIT(&disksort->bq_head);
230 1.1 yamt }
231 1.12 pgoyette
232 1.12 pgoyette MODULE(MODULE_CLASS_MISC, bufq_disksort, NULL);
233 1.12 pgoyette
234 1.12 pgoyette static int
235 1.12 pgoyette bufq_disksort_modcmd(modcmd_t cmd, void *opaque)
236 1.12 pgoyette {
237 1.12 pgoyette
238 1.12 pgoyette switch (cmd) {
239 1.12 pgoyette case MODULE_CMD_INIT:
240 1.12 pgoyette return bufq_register(&bufq_strat_disksort);
241 1.12 pgoyette case MODULE_CMD_FINI:
242 1.12 pgoyette return bufq_unregister(&bufq_strat_disksort);
243 1.12 pgoyette default:
244 1.12 pgoyette return ENOTTY;
245 1.12 pgoyette }
246 1.12 pgoyette }
247