2 * Copyright (c) 2006 Ruslan Ermilov <ru@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/mutex.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/queue.h>
43 #include <geom/geom.h>
44 #include <geom/cache/g_cache.h>
46 FEATURE(geom_cache, "GEOM cache module");
48 static MALLOC_DEFINE(M_GCACHE, "gcache_data", "GEOM_CACHE Data");
50 SYSCTL_DECL(_kern_geom);
51 SYSCTL_NODE(_kern_geom, OID_AUTO, cache, CTLFLAG_RW, 0, "GEOM_CACHE stuff");
52 static u_int g_cache_debug = 0;
53 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, debug, CTLFLAG_RW, &g_cache_debug, 0,
55 static u_int g_cache_enable = 1;
56 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, enable, CTLFLAG_RW, &g_cache_enable, 0,
58 static u_int g_cache_timeout = 10;
59 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, timeout, CTLFLAG_RW, &g_cache_timeout,
61 static u_int g_cache_idletime = 5;
62 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, idletime, CTLFLAG_RW, &g_cache_idletime,
64 static u_int g_cache_used_lo = 5;
65 static u_int g_cache_used_hi = 20;
67 sysctl_handle_pct(SYSCTL_HANDLER_ARGS)
69 u_int val = *(u_int *)arg1;
72 error = sysctl_handle_int(oidp, &val, 0, req);
73 if (error || !req->newptr)
75 if (val < 0 || val > 100)
77 if ((arg1 == &g_cache_used_lo && val > g_cache_used_hi) ||
78 (arg1 == &g_cache_used_hi && g_cache_used_lo > val))
83 SYSCTL_PROC(_kern_geom_cache, OID_AUTO, used_lo, CTLTYPE_UINT|CTLFLAG_RW,
84 &g_cache_used_lo, 0, sysctl_handle_pct, "IU", "");
85 SYSCTL_PROC(_kern_geom_cache, OID_AUTO, used_hi, CTLTYPE_UINT|CTLFLAG_RW,
86 &g_cache_used_hi, 0, sysctl_handle_pct, "IU", "");
89 static int g_cache_destroy(struct g_cache_softc *sc, boolean_t force);
90 static g_ctl_destroy_geom_t g_cache_destroy_geom;
92 static g_taste_t g_cache_taste;
93 static g_ctl_req_t g_cache_config;
94 static g_dumpconf_t g_cache_dumpconf;
96 struct g_class g_cache_class = {
97 .name = G_CACHE_CLASS_NAME,
99 .ctlreq = g_cache_config,
100 .taste = g_cache_taste,
101 .destroy_geom = g_cache_destroy_geom
104 #define OFF2BNO(off, sc) ((off) >> (sc)->sc_bshift)
105 #define BNO2OFF(bno, sc) ((bno) << (sc)->sc_bshift)
108 static struct g_cache_desc *
109 g_cache_alloc(struct g_cache_softc *sc)
111 struct g_cache_desc *dp;
113 mtx_assert(&sc->sc_mtx, MA_OWNED);
115 if (!TAILQ_EMPTY(&sc->sc_usedlist)) {
116 dp = TAILQ_FIRST(&sc->sc_usedlist);
117 TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
120 LIST_REMOVE(dp, d_next);
123 if (sc->sc_nent > sc->sc_maxent) {
127 dp = malloc(sizeof(*dp), M_GCACHE, M_NOWAIT | M_ZERO);
130 dp->d_data = uma_zalloc(sc->sc_zone, M_NOWAIT);
131 if (dp->d_data == NULL) {
140 g_cache_free(struct g_cache_softc *sc, struct g_cache_desc *dp)
143 mtx_assert(&sc->sc_mtx, MA_OWNED);
145 uma_zfree(sc->sc_zone, dp->d_data);
151 g_cache_free_used(struct g_cache_softc *sc)
153 struct g_cache_desc *dp;
156 mtx_assert(&sc->sc_mtx, MA_OWNED);
158 n = g_cache_used_lo * sc->sc_maxent / 100;
159 while (sc->sc_nused > n) {
160 KASSERT(!TAILQ_EMPTY(&sc->sc_usedlist), ("used list empty"));
161 dp = TAILQ_FIRST(&sc->sc_usedlist);
162 TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
164 LIST_REMOVE(dp, d_next);
165 g_cache_free(sc, dp);
170 g_cache_deliver(struct g_cache_softc *sc, struct bio *bp,
171 struct g_cache_desc *dp, int error)
173 off_t off1, off, len;
175 mtx_assert(&sc->sc_mtx, MA_OWNED);
176 KASSERT(OFF2BNO(bp->bio_offset, sc) <= dp->d_bno, ("wrong entry"));
177 KASSERT(OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc) >=
178 dp->d_bno, ("wrong entry"));
180 off1 = BNO2OFF(dp->d_bno, sc);
181 off = MAX(bp->bio_offset, off1);
182 len = MIN(bp->bio_offset + bp->bio_length, off1 + sc->sc_bsize) - off;
184 if (bp->bio_error == 0)
185 bp->bio_error = error;
186 if (bp->bio_error == 0) {
187 bcopy(dp->d_data + (off - off1),
188 bp->bio_data + (off - bp->bio_offset), len);
190 bp->bio_completed += len;
191 KASSERT(bp->bio_completed <= bp->bio_length, ("extra data"));
192 if (bp->bio_completed == bp->bio_length) {
193 if (bp->bio_error != 0)
194 bp->bio_completed = 0;
195 g_io_deliver(bp, bp->bio_error);
198 if (dp->d_flags & D_FLAG_USED) {
199 TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
200 TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
201 } else if (OFF2BNO(off + len, sc) > dp->d_bno) {
202 TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
204 dp->d_flags |= D_FLAG_USED;
206 dp->d_atime = time_uptime;
210 g_cache_done(struct bio *bp)
212 struct g_cache_softc *sc;
213 struct g_cache_desc *dp;
214 struct bio *bp2, *tmpbp;
216 sc = bp->bio_from->geom->softc;
217 KASSERT(G_CACHE_DESC1(bp) == sc, ("corrupt bio_caller in g_cache_done()"));
218 dp = G_CACHE_DESC2(bp);
219 mtx_lock(&sc->sc_mtx);
221 while (bp2 != NULL) {
222 KASSERT(G_CACHE_NEXT_BIO1(bp2) == sc, ("corrupt bio_driver in g_cache_done()"));
223 tmpbp = G_CACHE_NEXT_BIO2(bp2);
224 g_cache_deliver(sc, bp2, dp, bp->bio_error);
227 dp->d_biolist = NULL;
228 if (dp->d_flags & D_FLAG_INVALID) {
230 g_cache_free(sc, dp);
231 } else if (bp->bio_error) {
232 LIST_REMOVE(dp, d_next);
233 if (dp->d_flags & D_FLAG_USED) {
234 TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
237 g_cache_free(sc, dp);
239 mtx_unlock(&sc->sc_mtx);
243 static struct g_cache_desc *
244 g_cache_lookup(struct g_cache_softc *sc, off_t bno)
246 struct g_cache_desc *dp;
248 mtx_assert(&sc->sc_mtx, MA_OWNED);
250 LIST_FOREACH(dp, &sc->sc_desclist[G_CACHE_BUCKET(bno)], d_next)
251 if (dp->d_bno == bno)
257 g_cache_read(struct g_cache_softc *sc, struct bio *bp)
260 struct g_cache_desc *dp;
262 mtx_lock(&sc->sc_mtx);
263 dp = g_cache_lookup(sc,
264 OFF2BNO(bp->bio_offset + bp->bio_completed, sc));
266 /* Add to waiters list or deliver. */
268 if (dp->d_biolist != NULL) {
269 G_CACHE_NEXT_BIO1(bp) = sc;
270 G_CACHE_NEXT_BIO2(bp) = dp->d_biolist;
273 g_cache_deliver(sc, bp, dp, 0);
274 mtx_unlock(&sc->sc_mtx);
278 /* Cache miss. Allocate entry and schedule bio. */
279 sc->sc_cachemisses++;
280 dp = g_cache_alloc(sc);
282 mtx_unlock(&sc->sc_mtx);
285 cbp = g_clone_bio(bp);
287 g_cache_free(sc, dp);
288 mtx_unlock(&sc->sc_mtx);
292 dp->d_bno = OFF2BNO(bp->bio_offset + bp->bio_completed, sc);
293 G_CACHE_NEXT_BIO1(bp) = sc;
294 G_CACHE_NEXT_BIO2(bp) = NULL;
296 LIST_INSERT_HEAD(&sc->sc_desclist[G_CACHE_BUCKET(dp->d_bno)],
298 mtx_unlock(&sc->sc_mtx);
300 G_CACHE_DESC1(cbp) = sc;
301 G_CACHE_DESC2(cbp) = dp;
302 cbp->bio_done = g_cache_done;
303 cbp->bio_offset = BNO2OFF(dp->d_bno, sc);
304 cbp->bio_data = dp->d_data;
305 cbp->bio_length = sc->sc_bsize;
306 g_io_request(cbp, LIST_FIRST(&bp->bio_to->geom->consumer));
311 g_cache_invalidate(struct g_cache_softc *sc, struct bio *bp)
313 struct g_cache_desc *dp;
316 mtx_lock(&sc->sc_mtx);
317 bno = OFF2BNO(bp->bio_offset, sc);
318 lim = OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc);
320 if ((dp = g_cache_lookup(sc, bno)) != NULL) {
321 LIST_REMOVE(dp, d_next);
322 if (dp->d_flags & D_FLAG_USED) {
323 TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
326 if (dp->d_biolist == NULL)
327 g_cache_free(sc, dp);
329 dp->d_flags = D_FLAG_INVALID;
334 } while (bno <= lim);
335 mtx_unlock(&sc->sc_mtx);
339 g_cache_start(struct bio *bp)
341 struct g_cache_softc *sc;
343 struct g_cache_desc *dp;
346 gp = bp->bio_to->geom;
348 G_CACHE_LOGREQ(bp, "Request received.");
349 switch (bp->bio_cmd) {
352 sc->sc_readbytes += bp->bio_length;
355 if (bp->bio_offset + bp->bio_length > sc->sc_tail)
357 if (OFF2BNO(bp->bio_offset, sc) ==
358 OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) {
360 sc->sc_cachereadbytes += bp->bio_length;
361 if (g_cache_read(sc, bp) == 0)
364 sc->sc_cachereadbytes -= bp->bio_length;
366 } else if (OFF2BNO(bp->bio_offset, sc) + 1 ==
367 OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) {
368 mtx_lock(&sc->sc_mtx);
369 dp = g_cache_lookup(sc, OFF2BNO(bp->bio_offset, sc));
370 if (dp == NULL || dp->d_biolist != NULL) {
371 mtx_unlock(&sc->sc_mtx);
375 sc->sc_cachereadbytes += bp->bio_length;
376 g_cache_deliver(sc, bp, dp, 0);
377 mtx_unlock(&sc->sc_mtx);
378 if (g_cache_read(sc, bp) == 0)
381 sc->sc_cachereadbytes -= bp->bio_length;
387 sc->sc_wrotebytes += bp->bio_length;
388 g_cache_invalidate(sc, bp);
391 cbp = g_clone_bio(bp);
393 g_io_deliver(bp, ENOMEM);
396 cbp->bio_done = g_std_done;
397 G_CACHE_LOGREQ(cbp, "Sending request.");
398 g_io_request(cbp, LIST_FIRST(&gp->consumer));
402 g_cache_go(void *arg)
404 struct g_cache_softc *sc = arg;
405 struct g_cache_desc *dp;
408 mtx_assert(&sc->sc_mtx, MA_OWNED);
410 /* Forcibly mark idle ready entries as used. */
411 for (i = 0; i < G_CACHE_BUCKETS; i++) {
412 LIST_FOREACH(dp, &sc->sc_desclist[i], d_next) {
413 if (dp->d_flags & D_FLAG_USED ||
414 dp->d_biolist != NULL ||
415 time_uptime - dp->d_atime < g_cache_idletime)
417 TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
419 dp->d_flags |= D_FLAG_USED;
423 /* Keep the number of used entries low. */
424 if (sc->sc_nused > g_cache_used_hi * sc->sc_maxent / 100)
425 g_cache_free_used(sc);
427 callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc);
431 g_cache_access(struct g_provider *pp, int dr, int dw, int de)
434 struct g_consumer *cp;
438 cp = LIST_FIRST(&gp->consumer);
439 error = g_access(cp, dr, dw, de);
445 g_cache_orphan(struct g_consumer *cp)
449 g_cache_destroy(cp->geom->softc, 1);
452 static struct g_cache_softc *
453 g_cache_find_device(struct g_class *mp, const char *name)
457 LIST_FOREACH(gp, &mp->geom, geom) {
458 if (strcmp(gp->name, name) == 0)
464 static struct g_geom *
465 g_cache_create(struct g_class *mp, struct g_provider *pp,
466 const struct g_cache_metadata *md, u_int type)
468 struct g_cache_softc *sc;
470 struct g_provider *newpp;
471 struct g_consumer *cp;
481 G_CACHE_DEBUG(1, "Creating device %s.", md->md_name);
483 /* Cache size is minimum 100. */
484 if (md->md_size < 100) {
485 G_CACHE_DEBUG(0, "Invalid size for device %s.", md->md_name);
489 /* Block size restrictions. */
490 bshift = ffs(md->md_bsize) - 1;
491 if (md->md_bsize == 0 || md->md_bsize > MAXPHYS ||
492 md->md_bsize != 1 << bshift ||
493 (md->md_bsize % pp->sectorsize) != 0) {
494 G_CACHE_DEBUG(0, "Invalid blocksize for provider %s.", pp->name);
498 /* Check for duplicate unit. */
499 if (g_cache_find_device(mp, (const char *)&md->md_name) != NULL) {
500 G_CACHE_DEBUG(0, "Provider %s already exists.", md->md_name);
504 gp = g_new_geomf(mp, md->md_name);
505 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
507 sc->sc_bshift = bshift;
508 sc->sc_bsize = 1 << bshift;
509 sc->sc_zone = uma_zcreate("gcache", sc->sc_bsize, NULL, NULL, NULL, NULL,
511 mtx_init(&sc->sc_mtx, "GEOM CACHE mutex", NULL, MTX_DEF);
512 for (i = 0; i < G_CACHE_BUCKETS; i++)
513 LIST_INIT(&sc->sc_desclist[i]);
514 TAILQ_INIT(&sc->sc_usedlist);
515 sc->sc_maxent = md->md_size;
516 callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
519 gp->start = g_cache_start;
520 gp->orphan = g_cache_orphan;
521 gp->access = g_cache_access;
522 gp->dumpconf = g_cache_dumpconf;
524 newpp = g_new_providerf(gp, "cache/%s", gp->name);
525 newpp->sectorsize = pp->sectorsize;
526 newpp->mediasize = pp->mediasize;
527 if (type == G_CACHE_TYPE_AUTOMATIC)
528 newpp->mediasize -= pp->sectorsize;
529 sc->sc_tail = BNO2OFF(OFF2BNO(newpp->mediasize, sc), sc);
531 cp = g_new_consumer(gp);
532 if (g_attach(cp, pp) != 0) {
533 G_CACHE_DEBUG(0, "Cannot attach to provider %s.", pp->name);
534 g_destroy_consumer(cp);
535 g_destroy_provider(newpp);
536 mtx_destroy(&sc->sc_mtx);
542 g_error_provider(newpp, 0);
543 G_CACHE_DEBUG(0, "Device %s created.", gp->name);
544 callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc);
549 g_cache_destroy(struct g_cache_softc *sc, boolean_t force)
552 struct g_provider *pp;
553 struct g_cache_desc *dp, *dp2;
560 pp = LIST_FIRST(&gp->provider);
561 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
563 G_CACHE_DEBUG(0, "Device %s is still open, so it "
564 "can't be definitely removed.", pp->name);
566 G_CACHE_DEBUG(1, "Device %s is still open (r%dw%de%d).",
567 pp->name, pp->acr, pp->acw, pp->ace);
571 G_CACHE_DEBUG(0, "Device %s removed.", gp->name);
573 callout_drain(&sc->sc_callout);
574 mtx_lock(&sc->sc_mtx);
575 for (i = 0; i < G_CACHE_BUCKETS; i++) {
576 dp = LIST_FIRST(&sc->sc_desclist[i]);
578 dp2 = LIST_NEXT(dp, d_next);
579 g_cache_free(sc, dp);
583 mtx_unlock(&sc->sc_mtx);
584 mtx_destroy(&sc->sc_mtx);
585 uma_zdestroy(sc->sc_zone);
588 g_wither_geom(gp, ENXIO);
594 g_cache_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
597 return (g_cache_destroy(gp->softc, 0));
601 g_cache_read_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
603 struct g_provider *pp;
609 error = g_access(cp, 1, 0, 0);
614 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
617 g_access(cp, -1, 0, 0);
621 /* Decode metadata. */
622 cache_metadata_decode(buf, md);
629 g_cache_write_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
631 struct g_provider *pp;
637 error = g_access(cp, 0, 1, 0);
641 buf = malloc((size_t)pp->sectorsize, M_GCACHE, M_WAITOK | M_ZERO);
642 cache_metadata_encode(md, buf);
644 error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
646 g_access(cp, 0, -1, 0);
652 static struct g_geom *
653 g_cache_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
655 struct g_cache_metadata md;
656 struct g_consumer *cp;
660 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
663 G_CACHE_DEBUG(3, "Tasting %s.", pp->name);
665 gp = g_new_geomf(mp, "cache:taste");
666 gp->start = g_cache_start;
667 gp->orphan = g_cache_orphan;
668 gp->access = g_cache_access;
669 cp = g_new_consumer(gp);
671 error = g_cache_read_metadata(cp, &md);
673 g_destroy_consumer(cp);
678 if (strcmp(md.md_magic, G_CACHE_MAGIC) != 0)
680 if (md.md_version > G_CACHE_VERSION) {
681 printf("geom_cache.ko module is too old to handle %s.\n",
685 if (md.md_provsize != pp->mediasize)
688 gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_AUTOMATIC);
690 G_CACHE_DEBUG(0, "Can't create %s.", md.md_name);
697 g_cache_ctl_create(struct gctl_req *req, struct g_class *mp)
699 struct g_cache_metadata md;
700 struct g_provider *pp;
702 intmax_t *bsize, *size;
708 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
710 gctl_error(req, "No '%s' argument", "nargs");
714 gctl_error(req, "Invalid number of arguments.");
718 strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
719 md.md_version = G_CACHE_VERSION;
720 name = gctl_get_asciiparam(req, "arg0");
722 gctl_error(req, "No 'arg0' argument");
725 strlcpy(md.md_name, name, sizeof(md.md_name));
727 size = gctl_get_paraml(req, "size", sizeof(*size));
729 gctl_error(req, "No '%s' argument", "size");
732 if ((u_int)*size < 100) {
733 gctl_error(req, "Invalid '%s' argument", "size");
736 md.md_size = (u_int)*size;
738 bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
740 gctl_error(req, "No '%s' argument", "blocksize");
744 gctl_error(req, "Invalid '%s' argument", "blocksize");
747 md.md_bsize = (u_int)*bsize;
749 /* This field is not important here. */
752 name = gctl_get_asciiparam(req, "arg1");
754 gctl_error(req, "No 'arg1' argument");
757 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
758 name += strlen("/dev/");
759 pp = g_provider_by_name(name);
761 G_CACHE_DEBUG(1, "Provider %s is invalid.", name);
762 gctl_error(req, "Provider %s is invalid.", name);
765 gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_MANUAL);
767 gctl_error(req, "Can't create %s.", md.md_name);
773 g_cache_ctl_configure(struct gctl_req *req, struct g_class *mp)
775 struct g_cache_metadata md;
776 struct g_cache_softc *sc;
777 struct g_consumer *cp;
778 intmax_t *bsize, *size;
784 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
786 gctl_error(req, "No '%s' argument", "nargs");
790 gctl_error(req, "Missing device.");
794 name = gctl_get_asciiparam(req, "arg0");
796 gctl_error(req, "No 'arg0' argument");
799 sc = g_cache_find_device(mp, name);
801 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
802 gctl_error(req, "Device %s is invalid.", name);
806 size = gctl_get_paraml(req, "size", sizeof(*size));
808 gctl_error(req, "No '%s' argument", "size");
811 if ((u_int)*size != 0 && (u_int)*size < 100) {
812 gctl_error(req, "Invalid '%s' argument", "size");
815 if ((u_int)*size != 0)
816 sc->sc_maxent = (u_int)*size;
818 bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
820 gctl_error(req, "No '%s' argument", "blocksize");
824 gctl_error(req, "Invalid '%s' argument", "blocksize");
828 if (sc->sc_type != G_CACHE_TYPE_AUTOMATIC)
831 strlcpy(md.md_name, name, sizeof(md.md_name));
832 strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
833 md.md_version = G_CACHE_VERSION;
834 if ((u_int)*size != 0)
835 md.md_size = (u_int)*size;
837 md.md_size = sc->sc_maxent;
838 if ((u_int)*bsize != 0)
839 md.md_bsize = (u_int)*bsize;
841 md.md_bsize = sc->sc_bsize;
842 cp = LIST_FIRST(&sc->sc_geom->consumer);
843 md.md_provsize = cp->provider->mediasize;
844 error = g_cache_write_metadata(cp, &md);
846 G_CACHE_DEBUG(2, "Metadata on %s updated.", cp->provider->name);
848 G_CACHE_DEBUG(0, "Cannot update metadata on %s (error=%d).",
849 cp->provider->name, error);
853 g_cache_ctl_destroy(struct gctl_req *req, struct g_class *mp)
855 int *nargs, *force, error, i;
856 struct g_cache_softc *sc;
862 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
864 gctl_error(req, "No '%s' argument", "nargs");
868 gctl_error(req, "Missing device(s).");
871 force = gctl_get_paraml(req, "force", sizeof(*force));
873 gctl_error(req, "No 'force' argument");
877 for (i = 0; i < *nargs; i++) {
878 snprintf(param, sizeof(param), "arg%d", i);
879 name = gctl_get_asciiparam(req, param);
881 gctl_error(req, "No 'arg%d' argument", i);
884 sc = g_cache_find_device(mp, name);
886 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
887 gctl_error(req, "Device %s is invalid.", name);
890 error = g_cache_destroy(sc, *force);
892 gctl_error(req, "Cannot destroy device %s (error=%d).",
900 g_cache_ctl_reset(struct gctl_req *req, struct g_class *mp)
902 struct g_cache_softc *sc;
909 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
911 gctl_error(req, "No '%s' argument", "nargs");
915 gctl_error(req, "Missing device(s).");
919 for (i = 0; i < *nargs; i++) {
920 snprintf(param, sizeof(param), "arg%d", i);
921 name = gctl_get_asciiparam(req, param);
923 gctl_error(req, "No 'arg%d' argument", i);
926 sc = g_cache_find_device(mp, name);
928 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
929 gctl_error(req, "Device %s is invalid.", name);
933 sc->sc_readbytes = 0;
934 sc->sc_cachereads = 0;
935 sc->sc_cachereadbytes = 0;
936 sc->sc_cachehits = 0;
937 sc->sc_cachemisses = 0;
938 sc->sc_cachefull = 0;
940 sc->sc_wrotebytes = 0;
945 g_cache_config(struct gctl_req *req, struct g_class *mp, const char *verb)
951 version = gctl_get_paraml(req, "version", sizeof(*version));
952 if (version == NULL) {
953 gctl_error(req, "No '%s' argument.", "version");
956 if (*version != G_CACHE_VERSION) {
957 gctl_error(req, "Userland and kernel parts are out of sync.");
961 if (strcmp(verb, "create") == 0) {
962 g_cache_ctl_create(req, mp);
964 } else if (strcmp(verb, "configure") == 0) {
965 g_cache_ctl_configure(req, mp);
967 } else if (strcmp(verb, "destroy") == 0 ||
968 strcmp(verb, "stop") == 0) {
969 g_cache_ctl_destroy(req, mp);
971 } else if (strcmp(verb, "reset") == 0) {
972 g_cache_ctl_reset(req, mp);
976 gctl_error(req, "Unknown verb.");
980 g_cache_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
981 struct g_consumer *cp, struct g_provider *pp)
983 struct g_cache_softc *sc;
985 if (pp != NULL || cp != NULL)
988 sbuf_printf(sb, "%s<Size>%u</Size>\n", indent, sc->sc_maxent);
989 sbuf_printf(sb, "%s<BlockSize>%u</BlockSize>\n", indent, sc->sc_bsize);
990 sbuf_printf(sb, "%s<TailOffset>%ju</TailOffset>\n", indent,
991 (uintmax_t)sc->sc_tail);
992 sbuf_printf(sb, "%s<Entries>%u</Entries>\n", indent, sc->sc_nent);
993 sbuf_printf(sb, "%s<UsedEntries>%u</UsedEntries>\n", indent,
995 sbuf_printf(sb, "%s<InvalidEntries>%u</InvalidEntries>\n", indent,
997 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
998 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
1000 sbuf_printf(sb, "%s<CacheReads>%ju</CacheReads>\n", indent,
1002 sbuf_printf(sb, "%s<CacheReadBytes>%ju</CacheReadBytes>\n", indent,
1003 sc->sc_cachereadbytes);
1004 sbuf_printf(sb, "%s<CacheHits>%ju</CacheHits>\n", indent,
1006 sbuf_printf(sb, "%s<CacheMisses>%ju</CacheMisses>\n", indent,
1007 sc->sc_cachemisses);
1008 sbuf_printf(sb, "%s<CacheFull>%ju</CacheFull>\n", indent,
1010 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
1011 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
1015 DECLARE_GEOM_CLASS(g_cache_class, g_cache);