2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Ruslan Ermilov <ru@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
45 #include <geom/geom.h>
46 #include <geom/geom_dbg.h>
47 #include <geom/cache/g_cache.h>
49 FEATURE(geom_cache, "GEOM cache module");
51 static MALLOC_DEFINE(M_GCACHE, "gcache_data", "GEOM_CACHE Data");
53 SYSCTL_DECL(_kern_geom);
54 static SYSCTL_NODE(_kern_geom, OID_AUTO, cache, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
56 static u_int g_cache_debug = 0;
57 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, debug, CTLFLAG_RW, &g_cache_debug, 0,
59 static u_int g_cache_enable = 1;
60 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, enable, CTLFLAG_RW, &g_cache_enable, 0,
62 static u_int g_cache_timeout = 10;
63 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, timeout, CTLFLAG_RW, &g_cache_timeout,
65 static u_int g_cache_idletime = 5;
66 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, idletime, CTLFLAG_RW, &g_cache_idletime,
68 static u_int g_cache_used_lo = 5;
69 static u_int g_cache_used_hi = 20;
71 sysctl_handle_pct(SYSCTL_HANDLER_ARGS)
73 u_int val = *(u_int *)arg1;
76 error = sysctl_handle_int(oidp, &val, 0, req);
77 if (error || !req->newptr)
81 if ((arg1 == &g_cache_used_lo && val > g_cache_used_hi) ||
82 (arg1 == &g_cache_used_hi && g_cache_used_lo > val))
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",
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",
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;
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;
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
112 #define OFF2BNO(off, sc) ((off) >> (sc)->sc_bshift)
113 #define BNO2OFF(bno, sc) ((bno) << (sc)->sc_bshift)
116 static struct g_cache_desc *
117 g_cache_alloc(struct g_cache_softc *sc)
119 struct g_cache_desc *dp;
121 mtx_assert(&sc->sc_mtx, MA_OWNED);
123 if (!TAILQ_EMPTY(&sc->sc_usedlist)) {
124 dp = TAILQ_FIRST(&sc->sc_usedlist);
125 TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
128 LIST_REMOVE(dp, d_next);
131 if (sc->sc_nent > sc->sc_maxent) {
135 dp = malloc(sizeof(*dp), M_GCACHE, M_NOWAIT | M_ZERO);
138 dp->d_data = uma_zalloc(sc->sc_zone, M_NOWAIT);
139 if (dp->d_data == NULL) {
148 g_cache_free(struct g_cache_softc *sc, struct g_cache_desc *dp)
151 mtx_assert(&sc->sc_mtx, MA_OWNED);
153 uma_zfree(sc->sc_zone, dp->d_data);
159 g_cache_free_used(struct g_cache_softc *sc)
161 struct g_cache_desc *dp;
164 mtx_assert(&sc->sc_mtx, MA_OWNED);
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);
172 LIST_REMOVE(dp, d_next);
173 g_cache_free(sc, dp);
178 g_cache_deliver(struct g_cache_softc *sc, struct bio *bp,
179 struct g_cache_desc *dp, int error)
181 off_t off1, off, len;
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"));
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;
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);
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);
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);
212 dp->d_flags |= D_FLAG_USED;
214 dp->d_atime = time_uptime;
218 g_cache_done(struct bio *bp)
220 struct g_cache_softc *sc;
221 struct g_cache_desc *dp;
222 struct bio *bp2, *tmpbp;
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);
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);
235 dp->d_biolist = NULL;
236 if (dp->d_flags & D_FLAG_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);
245 g_cache_free(sc, dp);
247 mtx_unlock(&sc->sc_mtx);
251 static struct g_cache_desc *
252 g_cache_lookup(struct g_cache_softc *sc, off_t bno)
254 struct g_cache_desc *dp;
256 mtx_assert(&sc->sc_mtx, MA_OWNED);
258 LIST_FOREACH(dp, &sc->sc_desclist[G_CACHE_BUCKET(bno)], d_next)
259 if (dp->d_bno == bno)
265 g_cache_read(struct g_cache_softc *sc, struct bio *bp)
268 struct g_cache_desc *dp;
270 mtx_lock(&sc->sc_mtx);
271 dp = g_cache_lookup(sc,
272 OFF2BNO(bp->bio_offset + bp->bio_completed, sc));
274 /* Add to waiters list or deliver. */
276 if (dp->d_biolist != NULL) {
277 G_CACHE_NEXT_BIO1(bp) = sc;
278 G_CACHE_NEXT_BIO2(bp) = dp->d_biolist;
281 g_cache_deliver(sc, bp, dp, 0);
282 mtx_unlock(&sc->sc_mtx);
286 /* Cache miss. Allocate entry and schedule bio. */
287 sc->sc_cachemisses++;
288 dp = g_cache_alloc(sc);
290 mtx_unlock(&sc->sc_mtx);
293 cbp = g_clone_bio(bp);
295 g_cache_free(sc, dp);
296 mtx_unlock(&sc->sc_mtx);
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;
304 LIST_INSERT_HEAD(&sc->sc_desclist[G_CACHE_BUCKET(dp->d_bno)],
306 mtx_unlock(&sc->sc_mtx);
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));
319 g_cache_invalidate(struct g_cache_softc *sc, struct bio *bp)
321 struct g_cache_desc *dp;
324 mtx_lock(&sc->sc_mtx);
325 bno = OFF2BNO(bp->bio_offset, sc);
326 lim = OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc);
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);
334 if (dp->d_biolist == NULL)
335 g_cache_free(sc, dp);
337 dp->d_flags = D_FLAG_INVALID;
342 } while (bno <= lim);
343 mtx_unlock(&sc->sc_mtx);
347 g_cache_start(struct bio *bp)
349 struct g_cache_softc *sc;
351 struct g_cache_desc *dp;
354 gp = bp->bio_to->geom;
356 G_CACHE_LOGREQ(bp, "Request received.");
357 switch (bp->bio_cmd) {
360 sc->sc_readbytes += bp->bio_length;
363 if (bp->bio_offset + bp->bio_length > sc->sc_tail)
365 if (OFF2BNO(bp->bio_offset, sc) ==
366 OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) {
368 sc->sc_cachereadbytes += bp->bio_length;
369 if (g_cache_read(sc, bp) == 0)
372 sc->sc_cachereadbytes -= bp->bio_length;
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);
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)
389 sc->sc_cachereadbytes -= bp->bio_length;
395 sc->sc_wrotebytes += bp->bio_length;
396 g_cache_invalidate(sc, bp);
399 cbp = g_clone_bio(bp);
401 g_io_deliver(bp, ENOMEM);
404 cbp->bio_done = g_std_done;
405 G_CACHE_LOGREQ(cbp, "Sending request.");
406 g_io_request(cbp, LIST_FIRST(&gp->consumer));
410 g_cache_go(void *arg)
412 struct g_cache_softc *sc = arg;
413 struct g_cache_desc *dp;
416 mtx_assert(&sc->sc_mtx, MA_OWNED);
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)
425 TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
427 dp->d_flags |= D_FLAG_USED;
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);
435 callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc);
439 g_cache_access(struct g_provider *pp, int dr, int dw, int de)
442 struct g_consumer *cp;
446 cp = LIST_FIRST(&gp->consumer);
447 error = g_access(cp, dr, dw, de);
453 g_cache_orphan(struct g_consumer *cp)
457 g_cache_destroy(cp->geom->softc, 1);
460 static struct g_cache_softc *
461 g_cache_find_device(struct g_class *mp, const char *name)
465 LIST_FOREACH(gp, &mp->geom, geom) {
466 if (strcmp(gp->name, name) == 0)
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)
476 struct g_cache_softc *sc;
478 struct g_provider *newpp;
479 struct g_consumer *cp;
489 G_CACHE_DEBUG(1, "Creating device %s.", md->md_name);
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);
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);
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);
512 gp = g_new_geomf(mp, "%s", md->md_name);
513 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
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,
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);
527 gp->start = g_cache_start;
528 gp->orphan = g_cache_orphan;
529 gp->access = g_cache_access;
530 gp->dumpconf = g_cache_dumpconf;
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);
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);
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);
557 g_cache_destroy(struct g_cache_softc *sc, boolean_t force)
560 struct g_provider *pp;
561 struct g_cache_desc *dp, *dp2;
568 pp = LIST_FIRST(&gp->provider);
569 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
571 G_CACHE_DEBUG(0, "Device %s is still open, so it "
572 "can't be definitely removed.", pp->name);
574 G_CACHE_DEBUG(1, "Device %s is still open (r%dw%de%d).",
575 pp->name, pp->acr, pp->acw, pp->ace);
579 G_CACHE_DEBUG(0, "Device %s removed.", gp->name);
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]);
586 dp2 = LIST_NEXT(dp, d_next);
587 g_cache_free(sc, dp);
591 mtx_unlock(&sc->sc_mtx);
592 mtx_destroy(&sc->sc_mtx);
593 uma_zdestroy(sc->sc_zone);
596 g_wither_geom(gp, ENXIO);
602 g_cache_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
605 return (g_cache_destroy(gp->softc, 0));
609 g_cache_read_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
611 struct g_provider *pp;
617 error = g_access(cp, 1, 0, 0);
622 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
625 g_access(cp, -1, 0, 0);
629 /* Decode metadata. */
630 cache_metadata_decode(buf, md);
637 g_cache_write_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
639 struct g_provider *pp;
645 error = g_access(cp, 0, 1, 0);
649 buf = malloc((size_t)pp->sectorsize, M_GCACHE, M_WAITOK | M_ZERO);
650 cache_metadata_encode(md, buf);
652 error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
654 g_access(cp, 0, -1, 0);
660 static struct g_geom *
661 g_cache_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
663 struct g_cache_metadata md;
664 struct g_consumer *cp;
668 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
671 G_CACHE_DEBUG(3, "Tasting %s.", pp->name);
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);
679 error = g_cache_read_metadata(cp, &md);
681 g_destroy_consumer(cp);
686 if (strcmp(md.md_magic, G_CACHE_MAGIC) != 0)
688 if (md.md_version > G_CACHE_VERSION) {
689 printf("geom_cache.ko module is too old to handle %s.\n",
693 if (md.md_provsize != pp->mediasize)
696 gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_AUTOMATIC);
698 G_CACHE_DEBUG(0, "Can't create %s.", md.md_name);
705 g_cache_ctl_create(struct gctl_req *req, struct g_class *mp)
707 struct g_cache_metadata md;
708 struct g_provider *pp;
710 intmax_t *bsize, *size;
716 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
718 gctl_error(req, "No '%s' argument", "nargs");
722 gctl_error(req, "Invalid number of arguments.");
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");
730 gctl_error(req, "No 'arg0' argument");
733 strlcpy(md.md_name, name, sizeof(md.md_name));
735 size = gctl_get_paraml(req, "size", sizeof(*size));
737 gctl_error(req, "No '%s' argument", "size");
740 if ((u_int)*size < 100) {
741 gctl_error(req, "Invalid '%s' argument", "size");
744 md.md_size = (u_int)*size;
746 bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
748 gctl_error(req, "No '%s' argument", "blocksize");
752 gctl_error(req, "Invalid '%s' argument", "blocksize");
755 md.md_bsize = (u_int)*bsize;
757 /* This field is not important here. */
760 name = gctl_get_asciiparam(req, "arg1");
762 gctl_error(req, "No 'arg1' argument");
765 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
766 name += strlen(_PATH_DEV);
767 pp = g_provider_by_name(name);
769 G_CACHE_DEBUG(1, "Provider %s is invalid.", name);
770 gctl_error(req, "Provider %s is invalid.", name);
773 gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_MANUAL);
775 gctl_error(req, "Can't create %s.", md.md_name);
781 g_cache_ctl_configure(struct gctl_req *req, struct g_class *mp)
783 struct g_cache_metadata md;
784 struct g_cache_softc *sc;
785 struct g_consumer *cp;
786 intmax_t *bsize, *size;
792 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
794 gctl_error(req, "No '%s' argument", "nargs");
798 gctl_error(req, "Missing device.");
802 name = gctl_get_asciiparam(req, "arg0");
804 gctl_error(req, "No 'arg0' argument");
807 sc = g_cache_find_device(mp, name);
809 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
810 gctl_error(req, "Device %s is invalid.", name);
814 size = gctl_get_paraml(req, "size", sizeof(*size));
816 gctl_error(req, "No '%s' argument", "size");
819 if ((u_int)*size != 0 && (u_int)*size < 100) {
820 gctl_error(req, "Invalid '%s' argument", "size");
823 if ((u_int)*size != 0)
824 sc->sc_maxent = (u_int)*size;
826 bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
828 gctl_error(req, "No '%s' argument", "blocksize");
832 gctl_error(req, "Invalid '%s' argument", "blocksize");
836 if (sc->sc_type != G_CACHE_TYPE_AUTOMATIC)
839 strlcpy(md.md_name, name, sizeof(md.md_name));
840 strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic));
841 md.md_version = G_CACHE_VERSION;
842 if ((u_int)*size != 0)
843 md.md_size = (u_int)*size;
845 md.md_size = sc->sc_maxent;
846 if ((u_int)*bsize != 0)
847 md.md_bsize = (u_int)*bsize;
849 md.md_bsize = sc->sc_bsize;
850 cp = LIST_FIRST(&sc->sc_geom->consumer);
851 md.md_provsize = cp->provider->mediasize;
852 error = g_cache_write_metadata(cp, &md);
854 G_CACHE_DEBUG(2, "Metadata on %s updated.", cp->provider->name);
856 G_CACHE_DEBUG(0, "Cannot update metadata on %s (error=%d).",
857 cp->provider->name, error);
861 g_cache_ctl_destroy(struct gctl_req *req, struct g_class *mp)
863 int *nargs, *force, error, i;
864 struct g_cache_softc *sc;
870 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
872 gctl_error(req, "No '%s' argument", "nargs");
876 gctl_error(req, "Missing device(s).");
879 force = gctl_get_paraml(req, "force", sizeof(*force));
881 gctl_error(req, "No 'force' argument");
885 for (i = 0; i < *nargs; i++) {
886 snprintf(param, sizeof(param), "arg%d", i);
887 name = gctl_get_asciiparam(req, param);
889 gctl_error(req, "No 'arg%d' argument", i);
892 sc = g_cache_find_device(mp, name);
894 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
895 gctl_error(req, "Device %s is invalid.", name);
898 error = g_cache_destroy(sc, *force);
900 gctl_error(req, "Cannot destroy device %s (error=%d).",
908 g_cache_ctl_reset(struct gctl_req *req, struct g_class *mp)
910 struct g_cache_softc *sc;
917 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
919 gctl_error(req, "No '%s' argument", "nargs");
923 gctl_error(req, "Missing device(s).");
927 for (i = 0; i < *nargs; i++) {
928 snprintf(param, sizeof(param), "arg%d", i);
929 name = gctl_get_asciiparam(req, param);
931 gctl_error(req, "No 'arg%d' argument", i);
934 sc = g_cache_find_device(mp, name);
936 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
937 gctl_error(req, "Device %s is invalid.", name);
941 sc->sc_readbytes = 0;
942 sc->sc_cachereads = 0;
943 sc->sc_cachereadbytes = 0;
944 sc->sc_cachehits = 0;
945 sc->sc_cachemisses = 0;
946 sc->sc_cachefull = 0;
948 sc->sc_wrotebytes = 0;
953 g_cache_config(struct gctl_req *req, struct g_class *mp, const char *verb)
959 version = gctl_get_paraml(req, "version", sizeof(*version));
960 if (version == NULL) {
961 gctl_error(req, "No '%s' argument.", "version");
964 if (*version != G_CACHE_VERSION) {
965 gctl_error(req, "Userland and kernel parts are out of sync.");
969 if (strcmp(verb, "create") == 0) {
970 g_cache_ctl_create(req, mp);
972 } else if (strcmp(verb, "configure") == 0) {
973 g_cache_ctl_configure(req, mp);
975 } else if (strcmp(verb, "destroy") == 0 ||
976 strcmp(verb, "stop") == 0) {
977 g_cache_ctl_destroy(req, mp);
979 } else if (strcmp(verb, "reset") == 0) {
980 g_cache_ctl_reset(req, mp);
984 gctl_error(req, "Unknown verb.");
988 g_cache_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
989 struct g_consumer *cp, struct g_provider *pp)
991 struct g_cache_softc *sc;
993 if (pp != NULL || cp != NULL)
996 sbuf_printf(sb, "%s<Size>%u</Size>\n", indent, sc->sc_maxent);
997 sbuf_printf(sb, "%s<BlockSize>%u</BlockSize>\n", indent, sc->sc_bsize);
998 sbuf_printf(sb, "%s<TailOffset>%ju</TailOffset>\n", indent,
999 (uintmax_t)sc->sc_tail);
1000 sbuf_printf(sb, "%s<Entries>%u</Entries>\n", indent, sc->sc_nent);
1001 sbuf_printf(sb, "%s<UsedEntries>%u</UsedEntries>\n", indent,
1003 sbuf_printf(sb, "%s<InvalidEntries>%u</InvalidEntries>\n", indent,
1005 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
1006 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
1008 sbuf_printf(sb, "%s<CacheReads>%ju</CacheReads>\n", indent,
1010 sbuf_printf(sb, "%s<CacheReadBytes>%ju</CacheReadBytes>\n", indent,
1011 sc->sc_cachereadbytes);
1012 sbuf_printf(sb, "%s<CacheHits>%ju</CacheHits>\n", indent,
1014 sbuf_printf(sb, "%s<CacheMisses>%ju</CacheMisses>\n", indent,
1015 sc->sc_cachemisses);
1016 sbuf_printf(sb, "%s<CacheFull>%ju</CacheFull>\n", indent,
1018 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
1019 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
1023 DECLARE_GEOM_CLASS(g_cache_class, g_cache);
1024 MODULE_VERSION(geom_cache, 0);