Deleted Added
full compact
g_bde_work.c (105520) g_bde_work.c (106407)
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
35 * $FreeBSD: head/sys/geom/bde/g_bde_work.c 105520 2002-10-20 14:30:28Z phk $
32 * $FreeBSD: head/sys/geom/bde/g_bde_work.c 106407 2002-11-04 09:27:01Z phk $
36 *
37 * This source file contains the state-engine which makes things happen in the
38 * right order.
39 *
40 * Outline:
41 * 1) g_bde_start1()
42 * Break the struct bio into multiple work packets one per zone.
43 * 2) g_bde_start2()

--- 21 unchanged lines hidden (view full) ---

65#include <sys/bio.h>
66#include <sys/lock.h>
67#include <sys/mutex.h>
68#include <sys/queue.h>
69#include <sys/malloc.h>
70#include <sys/systm.h>
71#include <sys/kernel.h>
72#include <sys/sysctl.h>
33 *
34 * This source file contains the state-engine which makes things happen in the
35 * right order.
36 *
37 * Outline:
38 * 1) g_bde_start1()
39 * Break the struct bio into multiple work packets one per zone.
40 * 2) g_bde_start2()

--- 21 unchanged lines hidden (view full) ---

62#include <sys/bio.h>
63#include <sys/lock.h>
64#include <sys/mutex.h>
65#include <sys/queue.h>
66#include <sys/malloc.h>
67#include <sys/systm.h>
68#include <sys/kernel.h>
69#include <sys/sysctl.h>
70#include <sys/time.h>
73#include <sys/proc.h>
74#include <sys/kthread.h>
75
71#include <sys/proc.h>
72#include <sys/kthread.h>
73
74#include <crypto/rijndael/rijndael.h>
75#include <crypto/sha2/sha2.h>
76#include <geom/geom.h>
77#include <geom/bde/g_bde.h>
78
79static void g_bde_delete_sector(struct g_bde_softc *wp, struct g_bde_sector *sp);
80static struct g_bde_sector * g_bde_new_sector(struct g_bde_work *wp, u_int len);
81static void g_bde_release_sector(struct g_bde_work *wp, struct g_bde_sector *sp);
82static struct g_bde_sector *g_bde_get_sector(struct g_bde_work *wp, off_t offset);
83static int g_bde_start_read(struct g_bde_sector *sp);
76#include <geom/geom.h>
77#include <geom/bde/g_bde.h>
78
79static void g_bde_delete_sector(struct g_bde_softc *wp, struct g_bde_sector *sp);
80static struct g_bde_sector * g_bde_new_sector(struct g_bde_work *wp, u_int len);
81static void g_bde_release_sector(struct g_bde_work *wp, struct g_bde_sector *sp);
82static struct g_bde_sector *g_bde_get_sector(struct g_bde_work *wp, off_t offset);
83static int g_bde_start_read(struct g_bde_sector *sp);
84static void g_bde_purge_sector(struct g_bde_softc *sc, int fraction);
84
85/*
86 * Work item allocation.
87 *
88 * C++ would call these constructors and destructors.
89 */
90static u_int g_bde_nwork;
91SYSCTL_UINT(_debug, OID_AUTO, gbde_nwork, CTLFLAG_RD, &g_bde_nwork, 0, "");

--- 84 unchanged lines hidden (view full) ---

176 *
177 * XXX: GEOM may be able to grow a generic caching facility at some point
178 * XXX: to support such needs.
179 */
180
181static u_int g_bde_ncache;
182SYSCTL_UINT(_debug, OID_AUTO, gbde_ncache, CTLFLAG_RD, &g_bde_ncache, 0, "");
183
85
86/*
87 * Work item allocation.
88 *
89 * C++ would call these constructors and destructors.
90 */
91static u_int g_bde_nwork;
92SYSCTL_UINT(_debug, OID_AUTO, gbde_nwork, CTLFLAG_RD, &g_bde_nwork, 0, "");

--- 84 unchanged lines hidden (view full) ---

177 *
178 * XXX: GEOM may be able to grow a generic caching facility at some point
179 * XXX: to support such needs.
180 */
181
182static u_int g_bde_ncache;
183SYSCTL_UINT(_debug, OID_AUTO, gbde_ncache, CTLFLAG_RD, &g_bde_ncache, 0, "");
184
185static void
186g_bde_purge_one_sector(struct g_bde_softc *sc, struct g_bde_sector *sp)
187{
188
189 g_trace(G_T_TOPOLOGY, "g_bde_purge_one_sector(%p, %p)", sc, sp);
190 if (sp->ref != 0)
191 return;
192 TAILQ_REMOVE(&sc->freelist, sp, list);
193 g_bde_ncache--;
194 sc->ncache--;
195 bzero(sp->data, sp->size);
196 g_bde_delete_sector(sc, sp);
197}
198
184static struct g_bde_sector *
185g_bde_get_sector(struct g_bde_work *wp, off_t offset)
186{
187 struct g_bde_sector *sp;
188 struct g_bde_softc *sc;
189
190 g_trace(G_T_TOPOLOGY, "g_bde_get_sector(%p, %jd)", wp, (intmax_t)offset);
191 sc = wp->softc;
199static struct g_bde_sector *
200g_bde_get_sector(struct g_bde_work *wp, off_t offset)
201{
202 struct g_bde_sector *sp;
203 struct g_bde_softc *sc;
204
205 g_trace(G_T_TOPOLOGY, "g_bde_get_sector(%p, %jd)", wp, (intmax_t)offset);
206 sc = wp->softc;
207
208 if (malloc_last_fail() < g_bde_ncache)
209 g_bde_purge_sector(sc, -1);
210
211 sp = TAILQ_FIRST(&sc->freelist);
212 if (sp != NULL && sp->ref == 0 && sp->used + 300 < time_uptime)
213 g_bde_purge_one_sector(sc, sp);
214
192 TAILQ_FOREACH(sp, &sc->freelist, list) {
193 if (sp->offset == offset)
194 break;
195 }
196 if (sp != NULL) {
197 sp->ref++;
198 KASSERT(sp->offset == offset, ("wrong offset"));
199 KASSERT(sp->softc == wp->softc, ("wrong softc"));
200 if (sp->ref == 1)
201 sp->owner = wp;
202 } else {
215 TAILQ_FOREACH(sp, &sc->freelist, list) {
216 if (sp->offset == offset)
217 break;
218 }
219 if (sp != NULL) {
220 sp->ref++;
221 KASSERT(sp->offset == offset, ("wrong offset"));
222 KASSERT(sp->softc == wp->softc, ("wrong softc"));
223 if (sp->ref == 1)
224 sp->owner = wp;
225 } else {
203 if (!TAILQ_EMPTY(&sc->freelist))
226 if (malloc_last_fail() < g_bde_ncache) {
227 TAILQ_FOREACH(sp, &sc->freelist, list)
228 if (sp->ref == 0)
229 break;
230 }
231 if (sp == NULL && !TAILQ_EMPTY(&sc->freelist))
204 sp = TAILQ_FIRST(&sc->freelist);
205 if (sp != NULL && sp->ref > 0)
206 sp = NULL;
207 if (sp == NULL) {
208 g_bde_ncache++;
209 sc->ncache++;
210 sp = g_bde_new_sector(wp, sc->sectorsize);
211 if (sp != NULL) {

--- 10 unchanged lines hidden (view full) ---

222 sp->error = 0;
223 }
224 }
225 if (sp != NULL) {
226 TAILQ_REMOVE(&sc->freelist, sp, list);
227 TAILQ_INSERT_TAIL(&sc->freelist, sp, list);
228 }
229 wp->ksp = sp;
232 sp = TAILQ_FIRST(&sc->freelist);
233 if (sp != NULL && sp->ref > 0)
234 sp = NULL;
235 if (sp == NULL) {
236 g_bde_ncache++;
237 sc->ncache++;
238 sp = g_bde_new_sector(wp, sc->sectorsize);
239 if (sp != NULL) {

--- 10 unchanged lines hidden (view full) ---

250 sp->error = 0;
251 }
252 }
253 if (sp != NULL) {
254 TAILQ_REMOVE(&sc->freelist, sp, list);
255 TAILQ_INSERT_TAIL(&sc->freelist, sp, list);
256 }
257 wp->ksp = sp;
258 if (sp == NULL) {
259 g_bde_purge_sector(sc, -1);
260 sp = g_bde_get_sector(wp, offset);
261 }
262 if (sp != NULL)
263 sp->used = time_uptime;
230 KASSERT(sp != NULL, ("get_sector failed"));
231 return(sp);
232}
233
234static void
235g_bde_release_sector(struct g_bde_work *wp, struct g_bde_sector *sp)
236{
237 struct g_bde_softc *sc;

--- 30 unchanged lines hidden (view full) ---

268
269static void
270g_bde_purge_sector(struct g_bde_softc *sc, int fraction)
271{
272 struct g_bde_sector *sp;
273 int n;
274
275 g_trace(G_T_TOPOLOGY, "g_bde_purge_sector(%p)", sc);
264 KASSERT(sp != NULL, ("get_sector failed"));
265 return(sp);
266}
267
268static void
269g_bde_release_sector(struct g_bde_work *wp, struct g_bde_sector *sp)
270{
271 struct g_bde_softc *sc;

--- 30 unchanged lines hidden (view full) ---

302
303static void
304g_bde_purge_sector(struct g_bde_softc *sc, int fraction)
305{
306 struct g_bde_sector *sp;
307 int n;
308
309 g_trace(G_T_TOPOLOGY, "g_bde_purge_sector(%p)", sc);
276 n = sc->ncache / fraction + 1;
310 if (fraction > 0)
311 n = sc->ncache / fraction + 1;
312 else
313 n = g_bde_ncache - malloc_last_fail();
314 if (n < 0)
315 return;
316 if (n > sc->ncache)
317 n = sc->ncache;
277 while(n--) {
278 TAILQ_FOREACH(sp, &sc->freelist, list) {
279 if (sp->ref != 0)
280 continue;
281 TAILQ_REMOVE(&sc->freelist, sp, list);
282 g_bde_ncache--;
283 sc->ncache--;
284 bzero(sp->data, sp->size);

--- 447 unchanged lines hidden ---
318 while(n--) {
319 TAILQ_FOREACH(sp, &sc->freelist, list) {
320 if (sp->ref != 0)
321 continue;
322 TAILQ_REMOVE(&sc->freelist, sp, list);
323 g_bde_ncache--;
324 sc->ncache--;
325 bzero(sp->data, sp->size);

--- 447 unchanged lines hidden ---