xref: /freebsd/sys/geom/cache/g_cache.c (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Ruslan Ermilov <ru@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/bio.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <sys/sbuf.h>
43 #include <sys/time.h>
44 #include <vm/uma.h>
45 #include <geom/geom.h>
46 #include <geom/geom_dbg.h>
47 #include <geom/cache/g_cache.h>
48 
49 FEATURE(geom_cache, "GEOM cache module");
50 
51 static MALLOC_DEFINE(M_GCACHE, "gcache_data", "GEOM_CACHE Data");
52 
53 SYSCTL_DECL(_kern_geom);
54 static SYSCTL_NODE(_kern_geom, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
55     "GEOM_CACHE stuff");
56 static u_int g_cache_debug = 0;
57 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, debug, CTLFLAG_RW, &g_cache_debug, 0,
58     "Debug level");
59 static u_int g_cache_enable = 1;
60 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, enable, CTLFLAG_RW, &g_cache_enable, 0,
61     "");
62 static u_int g_cache_timeout = 10;
63 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, timeout, CTLFLAG_RW, &g_cache_timeout,
64     0, "");
65 static u_int g_cache_idletime = 5;
66 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, idletime, CTLFLAG_RW, &g_cache_idletime,
67     0, "");
68 static u_int g_cache_used_lo = 5;
69 static u_int g_cache_used_hi = 20;
70 static int
71 sysctl_handle_pct(SYSCTL_HANDLER_ARGS)
72 {
73 	u_int val = *(u_int *)arg1;
74 	int error;
75 
76 	error = sysctl_handle_int(oidp, &val, 0, req);
77 	if (error || !req->newptr)
78 		return (error);
79 	if (val > 100)
80 		return (EINVAL);
81 	if ((arg1 == &g_cache_used_lo && val > g_cache_used_hi) ||
82 	    (arg1 == &g_cache_used_hi && g_cache_used_lo > val))
83 		return (EINVAL);
84 	*(u_int *)arg1 = val;
85 	return (0);
86 }
87 SYSCTL_PROC(_kern_geom_cache, OID_AUTO, used_lo,
88     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &g_cache_used_lo, 0,
89     sysctl_handle_pct, "IU",
90     "");
91 SYSCTL_PROC(_kern_geom_cache, OID_AUTO, used_hi,
92     CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &g_cache_used_hi, 0,
93     sysctl_handle_pct, "IU",
94     "");
95 
96 
97 static int g_cache_destroy(struct g_cache_softc *sc, boolean_t force);
98 static g_ctl_destroy_geom_t g_cache_destroy_geom;
99 
100 static g_taste_t g_cache_taste;
101 static g_ctl_req_t g_cache_config;
102 static g_dumpconf_t g_cache_dumpconf;
103 
104 struct g_class g_cache_class = {
105 	.name = G_CACHE_CLASS_NAME,
106 	.version = G_VERSION,
107 	.ctlreq = g_cache_config,
108 	.taste = g_cache_taste,
109 	.destroy_geom = g_cache_destroy_geom
110 };
111 
112 #define	OFF2BNO(off, sc)	((off) >> (sc)->sc_bshift)
113 #define	BNO2OFF(bno, sc)	((bno) << (sc)->sc_bshift)
114 
115 
116 static struct g_cache_desc *
117 g_cache_alloc(struct g_cache_softc *sc)
118 {
119 	struct g_cache_desc *dp;
120 
121 	mtx_assert(&sc->sc_mtx, MA_OWNED);
122 
123 	if (!TAILQ_EMPTY(&sc->sc_usedlist)) {
124 		dp = TAILQ_FIRST(&sc->sc_usedlist);
125 		TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
126 		sc->sc_nused--;
127 		dp->d_flags = 0;
128 		LIST_REMOVE(dp, d_next);
129 		return (dp);
130 	}
131 	if (sc->sc_nent > sc->sc_maxent) {
132 		sc->sc_cachefull++;
133 		return (NULL);
134 	}
135 	dp = malloc(sizeof(*dp), M_GCACHE, M_NOWAIT | M_ZERO);
136 	if (dp == NULL)
137 		return (NULL);
138 	dp->d_data = uma_zalloc(sc->sc_zone, M_NOWAIT);
139 	if (dp->d_data == NULL) {
140 		free(dp, M_GCACHE);
141 		return (NULL);
142 	}
143 	sc->sc_nent++;
144 	return (dp);
145 }
146 
147 static void
148 g_cache_free(struct g_cache_softc *sc, struct g_cache_desc *dp)
149 {
150 
151 	mtx_assert(&sc->sc_mtx, MA_OWNED);
152 
153 	uma_zfree(sc->sc_zone, dp->d_data);
154 	free(dp, M_GCACHE);
155 	sc->sc_nent--;
156 }
157 
158 static void
159 g_cache_free_used(struct g_cache_softc *sc)
160 {
161 	struct g_cache_desc *dp;
162 	u_int n;
163 
164 	mtx_assert(&sc->sc_mtx, MA_OWNED);
165 
166 	n = g_cache_used_lo * sc->sc_maxent / 100;
167 	while (sc->sc_nused > n) {
168 		KASSERT(!TAILQ_EMPTY(&sc->sc_usedlist), ("used list empty"));
169 		dp = TAILQ_FIRST(&sc->sc_usedlist);
170 		TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
171 		sc->sc_nused--;
172 		LIST_REMOVE(dp, d_next);
173 		g_cache_free(sc, dp);
174 	}
175 }
176 
177 static void
178 g_cache_deliver(struct g_cache_softc *sc, struct bio *bp,
179     struct g_cache_desc *dp, int error)
180 {
181 	off_t off1, off, len;
182 
183 	mtx_assert(&sc->sc_mtx, MA_OWNED);
184 	KASSERT(OFF2BNO(bp->bio_offset, sc) <= dp->d_bno, ("wrong entry"));
185 	KASSERT(OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc) >=
186 	    dp->d_bno, ("wrong entry"));
187 
188 	off1 = BNO2OFF(dp->d_bno, sc);
189 	off = MAX(bp->bio_offset, off1);
190 	len = MIN(bp->bio_offset + bp->bio_length, off1 + sc->sc_bsize) - off;
191 
192 	if (bp->bio_error == 0)
193 		bp->bio_error = error;
194 	if (bp->bio_error == 0) {
195 		bcopy(dp->d_data + (off - off1),
196 		    bp->bio_data + (off - bp->bio_offset), len);
197 	}
198 	bp->bio_completed += len;
199 	KASSERT(bp->bio_completed <= bp->bio_length, ("extra data"));
200 	if (bp->bio_completed == bp->bio_length) {
201 		if (bp->bio_error != 0)
202 			bp->bio_completed = 0;
203 		g_io_deliver(bp, bp->bio_error);
204 	}
205 
206 	if (dp->d_flags & D_FLAG_USED) {
207 		TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
208 		TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
209 	} else if (OFF2BNO(off + len, sc) > dp->d_bno) {
210 		TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
211 		sc->sc_nused++;
212 		dp->d_flags |= D_FLAG_USED;
213 	}
214 	dp->d_atime = time_uptime;
215 }
216 
217 static void
218 g_cache_done(struct bio *bp)
219 {
220 	struct g_cache_softc *sc;
221 	struct g_cache_desc *dp;
222 	struct bio *bp2, *tmpbp;
223 
224 	sc = bp->bio_from->geom->softc;
225 	KASSERT(G_CACHE_DESC1(bp) == sc, ("corrupt bio_caller in g_cache_done()"));
226 	dp = G_CACHE_DESC2(bp);
227 	mtx_lock(&sc->sc_mtx);
228 	bp2 = dp->d_biolist;
229 	while (bp2 != NULL) {
230 		KASSERT(G_CACHE_NEXT_BIO1(bp2) == sc, ("corrupt bio_driver in g_cache_done()"));
231 		tmpbp = G_CACHE_NEXT_BIO2(bp2);
232 		g_cache_deliver(sc, bp2, dp, bp->bio_error);
233 		bp2 = tmpbp;
234 	}
235 	dp->d_biolist = NULL;
236 	if (dp->d_flags & D_FLAG_INVALID) {
237 		sc->sc_invalid--;
238 		g_cache_free(sc, dp);
239 	} else if (bp->bio_error) {
240 		LIST_REMOVE(dp, d_next);
241 		if (dp->d_flags & D_FLAG_USED) {
242 			TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
243 			sc->sc_nused--;
244 		}
245 		g_cache_free(sc, dp);
246 	}
247 	mtx_unlock(&sc->sc_mtx);
248 	g_destroy_bio(bp);
249 }
250 
251 static struct g_cache_desc *
252 g_cache_lookup(struct g_cache_softc *sc, off_t bno)
253 {
254 	struct g_cache_desc *dp;
255 
256 	mtx_assert(&sc->sc_mtx, MA_OWNED);
257 
258 	LIST_FOREACH(dp, &sc->sc_desclist[G_CACHE_BUCKET(bno)], d_next)
259 		if (dp->d_bno == bno)
260 			return (dp);
261 	return (NULL);
262 }
263 
264 static int
265 g_cache_read(struct g_cache_softc *sc, struct bio *bp)
266 {
267 	struct bio *cbp;
268 	struct g_cache_desc *dp;
269 
270 	mtx_lock(&sc->sc_mtx);
271 	dp = g_cache_lookup(sc,
272 	    OFF2BNO(bp->bio_offset + bp->bio_completed, sc));
273 	if (dp != NULL) {
274 		/* Add to waiters list or deliver. */
275 		sc->sc_cachehits++;
276 		if (dp->d_biolist != NULL) {
277 			G_CACHE_NEXT_BIO1(bp) = sc;
278 			G_CACHE_NEXT_BIO2(bp) = dp->d_biolist;
279 			dp->d_biolist = bp;
280 		} else
281 			g_cache_deliver(sc, bp, dp, 0);
282 		mtx_unlock(&sc->sc_mtx);
283 		return (0);
284 	}
285 
286 	/* Cache miss.  Allocate entry and schedule bio.  */
287 	sc->sc_cachemisses++;
288 	dp = g_cache_alloc(sc);
289 	if (dp == NULL) {
290 		mtx_unlock(&sc->sc_mtx);
291 		return (ENOMEM);
292 	}
293 	cbp = g_clone_bio(bp);
294 	if (cbp == NULL) {
295 		g_cache_free(sc, dp);
296 		mtx_unlock(&sc->sc_mtx);
297 		return (ENOMEM);
298 	}
299 
300 	dp->d_bno = OFF2BNO(bp->bio_offset + bp->bio_completed, sc);
301 	G_CACHE_NEXT_BIO1(bp) = sc;
302 	G_CACHE_NEXT_BIO2(bp) = NULL;
303 	dp->d_biolist = bp;
304 	LIST_INSERT_HEAD(&sc->sc_desclist[G_CACHE_BUCKET(dp->d_bno)],
305 	    dp, d_next);
306 	mtx_unlock(&sc->sc_mtx);
307 
308 	G_CACHE_DESC1(cbp) = sc;
309 	G_CACHE_DESC2(cbp) = dp;
310 	cbp->bio_done = g_cache_done;
311 	cbp->bio_offset = BNO2OFF(dp->d_bno, sc);
312 	cbp->bio_data = dp->d_data;
313 	cbp->bio_length = sc->sc_bsize;
314 	g_io_request(cbp, LIST_FIRST(&bp->bio_to->geom->consumer));
315 	return (0);
316 }
317 
318 static void
319 g_cache_invalidate(struct g_cache_softc *sc, struct bio *bp)
320 {
321 	struct g_cache_desc *dp;
322 	off_t bno, lim;
323 
324 	mtx_lock(&sc->sc_mtx);
325 	bno = OFF2BNO(bp->bio_offset, sc);
326 	lim = OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc);
327 	do {
328 		if ((dp = g_cache_lookup(sc, bno)) != NULL) {
329 			LIST_REMOVE(dp, d_next);
330 			if (dp->d_flags & D_FLAG_USED) {
331 				TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
332 				sc->sc_nused--;
333 			}
334 			if (dp->d_biolist == NULL)
335 				g_cache_free(sc, dp);
336 			else {
337 				dp->d_flags = D_FLAG_INVALID;
338 				sc->sc_invalid++;
339 			}
340 		}
341 		bno++;
342 	} while (bno <= lim);
343 	mtx_unlock(&sc->sc_mtx);
344 }
345 
346 static void
347 g_cache_start(struct bio *bp)
348 {
349 	struct g_cache_softc *sc;
350 	struct g_geom *gp;
351 	struct g_cache_desc *dp;
352 	struct bio *cbp;
353 
354 	gp = bp->bio_to->geom;
355 	sc = gp->softc;
356 	G_CACHE_LOGREQ(bp, "Request received.");
357 	switch (bp->bio_cmd) {
358 	case BIO_READ:
359 		sc->sc_reads++;
360 		sc->sc_readbytes += bp->bio_length;
361 		if (!g_cache_enable)
362 			break;
363 		if (bp->bio_offset + bp->bio_length > sc->sc_tail)
364 			break;
365 		if (OFF2BNO(bp->bio_offset, sc) ==
366 		    OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) {
367 			sc->sc_cachereads++;
368 			sc->sc_cachereadbytes += bp->bio_length;
369 			if (g_cache_read(sc, bp) == 0)
370 				return;
371 			sc->sc_cachereads--;
372 			sc->sc_cachereadbytes -= bp->bio_length;
373 			break;
374 		} else if (OFF2BNO(bp->bio_offset, sc) + 1 ==
375 		    OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) {
376 			mtx_lock(&sc->sc_mtx);
377 			dp = g_cache_lookup(sc, OFF2BNO(bp->bio_offset, sc));
378 			if (dp == NULL || dp->d_biolist != NULL) {
379 				mtx_unlock(&sc->sc_mtx);
380 				break;
381 			}
382 			sc->sc_cachereads++;
383 			sc->sc_cachereadbytes += bp->bio_length;
384 			g_cache_deliver(sc, bp, dp, 0);
385 			mtx_unlock(&sc->sc_mtx);
386 			if (g_cache_read(sc, bp) == 0)
387 				return;
388 			sc->sc_cachereads--;
389 			sc->sc_cachereadbytes -= bp->bio_length;
390 			break;
391 		}
392 		break;
393 	case BIO_WRITE:
394 		sc->sc_writes++;
395 		sc->sc_wrotebytes += bp->bio_length;
396 		g_cache_invalidate(sc, bp);
397 		break;
398 	}
399 	cbp = g_clone_bio(bp);
400 	if (cbp == NULL) {
401 		g_io_deliver(bp, ENOMEM);
402 		return;
403 	}
404 	cbp->bio_done = g_std_done;
405 	G_CACHE_LOGREQ(cbp, "Sending request.");
406 	g_io_request(cbp, LIST_FIRST(&gp->consumer));
407 }
408 
409 static void
410 g_cache_go(void *arg)
411 {
412 	struct g_cache_softc *sc = arg;
413 	struct g_cache_desc *dp;
414 	int i;
415 
416 	mtx_assert(&sc->sc_mtx, MA_OWNED);
417 
418 	/* Forcibly mark idle ready entries as used. */
419 	for (i = 0; i < G_CACHE_BUCKETS; i++) {
420 		LIST_FOREACH(dp, &sc->sc_desclist[i], d_next) {
421 			if (dp->d_flags & D_FLAG_USED ||
422 			    dp->d_biolist != NULL ||
423 			    time_uptime - dp->d_atime < g_cache_idletime)
424 				continue;
425 			TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
426 			sc->sc_nused++;
427 			dp->d_flags |= D_FLAG_USED;
428 		}
429 	}
430 
431 	/* Keep the number of used entries low. */
432 	if (sc->sc_nused > g_cache_used_hi * sc->sc_maxent / 100)
433 		g_cache_free_used(sc);
434 
435 	callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc);
436 }
437 
438 static int
439 g_cache_access(struct g_provider *pp, int dr, int dw, int de)
440 {
441 	struct g_geom *gp;
442 	struct g_consumer *cp;
443 	int error;
444 
445 	gp = pp->geom;
446 	cp = LIST_FIRST(&gp->consumer);
447 	error = g_access(cp, dr, dw, de);
448 
449 	return (error);
450 }
451 
452 static void
453 g_cache_orphan(struct g_consumer *cp)
454 {
455 
456 	g_topology_assert();
457 	g_cache_destroy(cp->geom->softc, 1);
458 }
459 
460 static struct g_cache_softc *
461 g_cache_find_device(struct g_class *mp, const char *name)
462 {
463 	struct g_geom *gp;
464 
465 	LIST_FOREACH(gp, &mp->geom, geom) {
466 		if (strcmp(gp->name, name) == 0)
467 			return (gp->softc);
468 	}
469 	return (NULL);
470 }
471 
472 static struct g_geom *
473 g_cache_create(struct g_class *mp, struct g_provider *pp,
474     const struct g_cache_metadata *md, u_int type)
475 {
476 	struct g_cache_softc *sc;
477 	struct g_geom *gp;
478 	struct g_provider *newpp;
479 	struct g_consumer *cp;
480 	u_int bshift;
481 	int i;
482 
483 	g_topology_assert();
484 
485 	gp = NULL;
486 	newpp = NULL;
487 	cp = NULL;
488 
489 	G_CACHE_DEBUG(1, "Creating device %s.", md->md_name);
490 
491 	/* Cache size is minimum 100. */
492 	if (md->md_size < 100) {
493 		G_CACHE_DEBUG(0, "Invalid size for device %s.", md->md_name);
494 		return (NULL);
495 	}
496 
497 	/* Block size restrictions. */
498 	bshift = ffs(md->md_bsize) - 1;
499 	if (md->md_bsize == 0 || md->md_bsize > MAXPHYS ||
500 	    md->md_bsize != 1 << bshift ||
501 	    (md->md_bsize % pp->sectorsize) != 0) {
502 		G_CACHE_DEBUG(0, "Invalid blocksize for provider %s.", pp->name);
503 		return (NULL);
504 	}
505 
506 	/* Check for duplicate unit. */
507 	if (g_cache_find_device(mp, (const char *)&md->md_name) != NULL) {
508 		G_CACHE_DEBUG(0, "Provider %s already exists.", md->md_name);
509 		return (NULL);
510 	}
511 
512 	gp = g_new_geomf(mp, "%s", md->md_name);
513 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
514 	sc->sc_type = type;
515 	sc->sc_bshift = bshift;
516 	sc->sc_bsize = 1 << bshift;
517 	sc->sc_zone = uma_zcreate("gcache", sc->sc_bsize, NULL, NULL, NULL, NULL,
518 	    UMA_ALIGN_PTR, 0);
519 	mtx_init(&sc->sc_mtx, "GEOM CACHE mutex", NULL, MTX_DEF);
520 	for (i = 0; i < G_CACHE_BUCKETS; i++)
521 		LIST_INIT(&sc->sc_desclist[i]);
522 	TAILQ_INIT(&sc->sc_usedlist);
523 	sc->sc_maxent = md->md_size;
524 	callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
525 	gp->softc = sc;
526 	sc->sc_geom = gp;
527 	gp->start = g_cache_start;
528 	gp->orphan = g_cache_orphan;
529 	gp->access = g_cache_access;
530 	gp->dumpconf = g_cache_dumpconf;
531 
532 	newpp = g_new_providerf(gp, "cache/%s", gp->name);
533 	newpp->sectorsize = pp->sectorsize;
534 	newpp->mediasize = pp->mediasize;
535 	if (type == G_CACHE_TYPE_AUTOMATIC)
536 		newpp->mediasize -= pp->sectorsize;
537 	sc->sc_tail = BNO2OFF(OFF2BNO(newpp->mediasize, sc), sc);
538 
539 	cp = g_new_consumer(gp);
540 	if (g_attach(cp, pp) != 0) {
541 		G_CACHE_DEBUG(0, "Cannot attach to provider %s.", pp->name);
542 		g_destroy_consumer(cp);
543 		g_destroy_provider(newpp);
544 		mtx_destroy(&sc->sc_mtx);
545 		g_free(sc);
546 		g_destroy_geom(gp);
547 		return (NULL);
548 	}
549 
550 	g_error_provider(newpp, 0);
551 	G_CACHE_DEBUG(0, "Device %s created.", gp->name);
552 	callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc);
553 	return (gp);
554 }
555 
556 static int
557 g_cache_destroy(struct g_cache_softc *sc, boolean_t force)
558 {
559 	struct g_geom *gp;
560 	struct g_provider *pp;
561 	struct g_cache_desc *dp, *dp2;
562 	int i;
563 
564 	g_topology_assert();
565 	if (sc == NULL)
566 		return (ENXIO);
567 	gp = sc->sc_geom;
568 	pp = LIST_FIRST(&gp->provider);
569 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
570 		if (force) {
571 			G_CACHE_DEBUG(0, "Device %s is still open, so it "
572 			    "can't be definitely removed.", pp->name);
573 		} else {
574 			G_CACHE_DEBUG(1, "Device %s is still open (r%dw%de%d).",
575 			    pp->name, pp->acr, pp->acw, pp->ace);
576 			return (EBUSY);
577 		}
578 	} else {
579 		G_CACHE_DEBUG(0, "Device %s removed.", gp->name);
580 	}
581 	callout_drain(&sc->sc_callout);
582 	mtx_lock(&sc->sc_mtx);
583 	for (i = 0; i < G_CACHE_BUCKETS; i++) {
584 		dp = LIST_FIRST(&sc->sc_desclist[i]);
585 		while (dp != NULL) {
586 			dp2 = LIST_NEXT(dp, d_next);
587 			g_cache_free(sc, dp);
588 			dp = dp2;
589 		}
590 	}
591 	mtx_unlock(&sc->sc_mtx);
592 	mtx_destroy(&sc->sc_mtx);
593 	uma_zdestroy(sc->sc_zone);
594 	g_free(sc);
595 	gp->softc = NULL;
596 	g_wither_geom(gp, ENXIO);
597 
598 	return (0);
599 }
600 
601 static int
602 g_cache_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
603 {
604 
605 	return (g_cache_destroy(gp->softc, 0));
606 }
607 
608 static int
609 g_cache_read_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
610 {
611 	struct g_provider *pp;
612 	u_char *buf;
613 	int error;
614 
615 	g_topology_assert();
616 
617 	error = g_access(cp, 1, 0, 0);
618 	if (error != 0)
619 		return (error);
620 	pp = cp->provider;
621 	g_topology_unlock();
622 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
623 	    &error);
624 	g_topology_lock();
625 	g_access(cp, -1, 0, 0);
626 	if (buf == NULL)
627 		return (error);
628 
629 	/* Decode metadata. */
630 	cache_metadata_decode(buf, md);
631 	g_free(buf);
632 
633 	return (0);
634 }
635 
636 static int
637 g_cache_write_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
638 {
639 	struct g_provider *pp;
640 	u_char *buf;
641 	int error;
642 
643 	g_topology_assert();
644 
645 	error = g_access(cp, 0, 1, 0);
646 	if (error != 0)
647 		return (error);
648 	pp = cp->provider;
649 	buf = malloc((size_t)pp->sectorsize, M_GCACHE, M_WAITOK | M_ZERO);
650 	cache_metadata_encode(md, buf);
651 	g_topology_unlock();
652 	error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
653 	g_topology_lock();
654 	g_access(cp, 0, -1, 0);
655 	free(buf, M_GCACHE);
656 
657 	return (error);
658 }
659 
660 static struct g_geom *
661 g_cache_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
662 {
663 	struct g_cache_metadata md;
664 	struct g_consumer *cp;
665 	struct g_geom *gp;
666 	int error;
667 
668 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
669 	g_topology_assert();
670 
671 	G_CACHE_DEBUG(3, "Tasting %s.", pp->name);
672 
673 	gp = g_new_geomf(mp, "cache:taste");
674 	gp->start = g_cache_start;
675 	gp->orphan = g_cache_orphan;
676 	gp->access = g_cache_access;
677 	cp = g_new_consumer(gp);
678 	g_attach(cp, pp);
679 	error = g_cache_read_metadata(cp, &md);
680 	g_detach(cp);
681 	g_destroy_consumer(cp);
682 	g_destroy_geom(gp);
683 	if (error != 0)
684 		return (NULL);
685 
686 	if (strcmp(md.md_magic, G_CACHE_MAGIC) != 0)
687 		return (NULL);
688 	if (md.md_version > G_CACHE_VERSION) {
689 		printf("geom_cache.ko module is too old to handle %s.\n",
690 		    pp->name);
691 		return (NULL);
692 	}
693 	if (md.md_provsize != pp->mediasize)
694 		return (NULL);
695 
696 	gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_AUTOMATIC);
697 	if (gp == NULL) {
698 		G_CACHE_DEBUG(0, "Can't create %s.", md.md_name);
699 		return (NULL);
700 	}
701 	return (gp);
702 }
703 
704 static void
705 g_cache_ctl_create(struct gctl_req *req, struct g_class *mp)
706 {
707 	struct g_cache_metadata md;
708 	struct g_provider *pp;
709 	struct g_geom *gp;
710 	intmax_t *bsize, *size;
711 	const char *name;
712 	int *nargs;
713 
714 	g_topology_assert();
715 
716 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
717 	if (nargs == NULL) {
718 		gctl_error(req, "No '%s' argument", "nargs");
719 		return;
720 	}
721 	if (*nargs != 2) {
722 		gctl_error(req, "Invalid number of arguments.");
723 		return;
724 	}
725 
726 	strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
727 	md.md_version = G_CACHE_VERSION;
728 	name = gctl_get_asciiparam(req, "arg0");
729 	if (name == NULL) {
730 		gctl_error(req, "No 'arg0' argument");
731 		return;
732 	}
733 	strlcpy(md.md_name, name, sizeof(md.md_name));
734 
735 	size = gctl_get_paraml(req, "size", sizeof(*size));
736 	if (size == NULL) {
737 		gctl_error(req, "No '%s' argument", "size");
738 		return;
739 	}
740 	if ((u_int)*size < 100) {
741 		gctl_error(req, "Invalid '%s' argument", "size");
742 		return;
743 	}
744 	md.md_size = (u_int)*size;
745 
746 	bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
747 	if (bsize == NULL) {
748 		gctl_error(req, "No '%s' argument", "blocksize");
749 		return;
750 	}
751 	if (*bsize < 0) {
752 		gctl_error(req, "Invalid '%s' argument", "blocksize");
753 		return;
754 	}
755 	md.md_bsize = (u_int)*bsize;
756 
757 	/* This field is not important here. */
758 	md.md_provsize = 0;
759 
760 	pp = gctl_get_provider(req, "arg1");
761 	if (pp == NULL)
762 		return;
763 	gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_MANUAL);
764 	if (gp == NULL) {
765 		gctl_error(req, "Can't create %s.", md.md_name);
766 		return;
767 	}
768 }
769 
770 static void
771 g_cache_ctl_configure(struct gctl_req *req, struct g_class *mp)
772 {
773 	struct g_cache_metadata md;
774 	struct g_cache_softc *sc;
775 	struct g_consumer *cp;
776 	intmax_t *bsize, *size;
777 	const char *name;
778 	int error, *nargs;
779 
780 	g_topology_assert();
781 
782 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
783 	if (nargs == NULL) {
784 		gctl_error(req, "No '%s' argument", "nargs");
785 		return;
786 	}
787 	if (*nargs != 1) {
788 		gctl_error(req, "Missing device.");
789 		return;
790 	}
791 
792 	name = gctl_get_asciiparam(req, "arg0");
793 	if (name == NULL) {
794 		gctl_error(req, "No 'arg0' argument");
795 		return;
796 	}
797 	sc = g_cache_find_device(mp, name);
798 	if (sc == NULL) {
799 		G_CACHE_DEBUG(1, "Device %s is invalid.", name);
800 		gctl_error(req, "Device %s is invalid.", name);
801 		return;
802 	}
803 
804 	size = gctl_get_paraml(req, "size", sizeof(*size));
805 	if (size == NULL) {
806 		gctl_error(req, "No '%s' argument", "size");
807 		return;
808 	}
809 	if ((u_int)*size != 0 && (u_int)*size < 100) {
810 		gctl_error(req, "Invalid '%s' argument", "size");
811 		return;
812 	}
813 	if ((u_int)*size != 0)
814 		sc->sc_maxent = (u_int)*size;
815 
816 	bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
817 	if (bsize == NULL) {
818 		gctl_error(req, "No '%s' argument", "blocksize");
819 		return;
820 	}
821 	if (*bsize < 0) {
822 		gctl_error(req, "Invalid '%s' argument", "blocksize");
823 		return;
824 	}
825 
826 	if (sc->sc_type != G_CACHE_TYPE_AUTOMATIC)
827 		return;
828 
829 	strlcpy(md.md_name, name, sizeof(md.md_name));
830 	strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
831 	md.md_version = G_CACHE_VERSION;
832 	if ((u_int)*size != 0)
833 		md.md_size = (u_int)*size;
834 	else
835 		md.md_size = sc->sc_maxent;
836 	if ((u_int)*bsize != 0)
837 		md.md_bsize = (u_int)*bsize;
838 	else
839 		md.md_bsize = sc->sc_bsize;
840 	cp = LIST_FIRST(&sc->sc_geom->consumer);
841 	md.md_provsize = cp->provider->mediasize;
842 	error = g_cache_write_metadata(cp, &md);
843 	if (error == 0)
844 		G_CACHE_DEBUG(2, "Metadata on %s updated.", cp->provider->name);
845 	else
846 		G_CACHE_DEBUG(0, "Cannot update metadata on %s (error=%d).",
847 		    cp->provider->name, error);
848 }
849 
850 static void
851 g_cache_ctl_destroy(struct gctl_req *req, struct g_class *mp)
852 {
853 	int *nargs, *force, error, i;
854 	struct g_cache_softc *sc;
855 	const char *name;
856 	char param[16];
857 
858 	g_topology_assert();
859 
860 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
861 	if (nargs == NULL) {
862 		gctl_error(req, "No '%s' argument", "nargs");
863 		return;
864 	}
865 	if (*nargs <= 0) {
866 		gctl_error(req, "Missing device(s).");
867 		return;
868 	}
869 	force = gctl_get_paraml(req, "force", sizeof(*force));
870 	if (force == NULL) {
871 		gctl_error(req, "No 'force' argument");
872 		return;
873 	}
874 
875 	for (i = 0; i < *nargs; i++) {
876 		snprintf(param, sizeof(param), "arg%d", i);
877 		name = gctl_get_asciiparam(req, param);
878 		if (name == NULL) {
879 			gctl_error(req, "No 'arg%d' argument", i);
880 			return;
881 		}
882 		sc = g_cache_find_device(mp, name);
883 		if (sc == NULL) {
884 			G_CACHE_DEBUG(1, "Device %s is invalid.", name);
885 			gctl_error(req, "Device %s is invalid.", name);
886 			return;
887 		}
888 		error = g_cache_destroy(sc, *force);
889 		if (error != 0) {
890 			gctl_error(req, "Cannot destroy device %s (error=%d).",
891 			    sc->sc_name, error);
892 			return;
893 		}
894 	}
895 }
896 
897 static void
898 g_cache_ctl_reset(struct gctl_req *req, struct g_class *mp)
899 {
900 	struct g_cache_softc *sc;
901 	const char *name;
902 	char param[16];
903 	int i, *nargs;
904 
905 	g_topology_assert();
906 
907 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
908 	if (nargs == NULL) {
909 		gctl_error(req, "No '%s' argument", "nargs");
910 		return;
911 	}
912 	if (*nargs <= 0) {
913 		gctl_error(req, "Missing device(s).");
914 		return;
915 	}
916 
917 	for (i = 0; i < *nargs; i++) {
918 		snprintf(param, sizeof(param), "arg%d", i);
919 		name = gctl_get_asciiparam(req, param);
920 		if (name == NULL) {
921 			gctl_error(req, "No 'arg%d' argument", i);
922 			return;
923 		}
924 		sc = g_cache_find_device(mp, name);
925 		if (sc == NULL) {
926 			G_CACHE_DEBUG(1, "Device %s is invalid.", name);
927 			gctl_error(req, "Device %s is invalid.", name);
928 			return;
929 		}
930 		sc->sc_reads = 0;
931 		sc->sc_readbytes = 0;
932 		sc->sc_cachereads = 0;
933 		sc->sc_cachereadbytes = 0;
934 		sc->sc_cachehits = 0;
935 		sc->sc_cachemisses = 0;
936 		sc->sc_cachefull = 0;
937 		sc->sc_writes = 0;
938 		sc->sc_wrotebytes = 0;
939 	}
940 }
941 
942 static void
943 g_cache_config(struct gctl_req *req, struct g_class *mp, const char *verb)
944 {
945 	uint32_t *version;
946 
947 	g_topology_assert();
948 
949 	version = gctl_get_paraml(req, "version", sizeof(*version));
950 	if (version == NULL) {
951 		gctl_error(req, "No '%s' argument.", "version");
952 		return;
953 	}
954 	if (*version != G_CACHE_VERSION) {
955 		gctl_error(req, "Userland and kernel parts are out of sync.");
956 		return;
957 	}
958 
959 	if (strcmp(verb, "create") == 0) {
960 		g_cache_ctl_create(req, mp);
961 		return;
962 	} else if (strcmp(verb, "configure") == 0) {
963 		g_cache_ctl_configure(req, mp);
964 		return;
965 	} else if (strcmp(verb, "destroy") == 0 ||
966 	    strcmp(verb, "stop") == 0) {
967 		g_cache_ctl_destroy(req, mp);
968 		return;
969 	} else if (strcmp(verb, "reset") == 0) {
970 		g_cache_ctl_reset(req, mp);
971 		return;
972 	}
973 
974 	gctl_error(req, "Unknown verb.");
975 }
976 
977 static void
978 g_cache_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
979     struct g_consumer *cp, struct g_provider *pp)
980 {
981 	struct g_cache_softc *sc;
982 
983 	if (pp != NULL || cp != NULL)
984 		return;
985 	sc = gp->softc;
986 	sbuf_printf(sb, "%s<Size>%u</Size>\n", indent, sc->sc_maxent);
987 	sbuf_printf(sb, "%s<BlockSize>%u</BlockSize>\n", indent, sc->sc_bsize);
988 	sbuf_printf(sb, "%s<TailOffset>%ju</TailOffset>\n", indent,
989 	    (uintmax_t)sc->sc_tail);
990 	sbuf_printf(sb, "%s<Entries>%u</Entries>\n", indent, sc->sc_nent);
991 	sbuf_printf(sb, "%s<UsedEntries>%u</UsedEntries>\n", indent,
992 	    sc->sc_nused);
993 	sbuf_printf(sb, "%s<InvalidEntries>%u</InvalidEntries>\n", indent,
994 	    sc->sc_invalid);
995 	sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
996 	sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
997 	    sc->sc_readbytes);
998 	sbuf_printf(sb, "%s<CacheReads>%ju</CacheReads>\n", indent,
999 	    sc->sc_cachereads);
1000 	sbuf_printf(sb, "%s<CacheReadBytes>%ju</CacheReadBytes>\n", indent,
1001 	    sc->sc_cachereadbytes);
1002 	sbuf_printf(sb, "%s<CacheHits>%ju</CacheHits>\n", indent,
1003 	    sc->sc_cachehits);
1004 	sbuf_printf(sb, "%s<CacheMisses>%ju</CacheMisses>\n", indent,
1005 	    sc->sc_cachemisses);
1006 	sbuf_printf(sb, "%s<CacheFull>%ju</CacheFull>\n", indent,
1007 	    sc->sc_cachefull);
1008 	sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
1009 	sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
1010 	    sc->sc_wrotebytes);
1011 }
1012 
1013 DECLARE_GEOM_CLASS(g_cache_class, g_cache);
1014 MODULE_VERSION(geom_cache, 0);
1015