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