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>
42 #include <geom/geom.h>
43 #include <geom/cache/g_cache.h>
45 static MALLOC_DEFINE(M_GCACHE, "gcache_data", "GEOM_CACHE Data");
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,
52 static u_int g_cache_enable = 1;
53 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, enable, CTLFLAG_RW, &g_cache_enable, 0,
55 static u_int g_cache_timeout = 10;
56 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, timeout, CTLFLAG_RW, &g_cache_timeout,
58 static u_int g_cache_idletime = 5;
59 SYSCTL_UINT(_kern_geom_cache, OID_AUTO, idletime, CTLFLAG_RW, &g_cache_idletime,
61 static u_int g_cache_used_lo = 5;
62 static u_int g_cache_used_hi = 20;
64 sysctl_handle_pct(SYSCTL_HANDLER_ARGS)
66 u_int val = *(u_int *)arg1;
69 error = sysctl_handle_int(oidp, &val, 0, req);
70 if (error || !req->newptr)
72 if (val < 0 || val > 100)
74 if ((arg1 == &g_cache_used_lo && val > g_cache_used_hi) ||
75 (arg1 == &g_cache_used_hi && g_cache_used_lo > val))
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", "");
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;
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;
93 struct g_class g_cache_class = {
94 .name = G_CACHE_CLASS_NAME,
96 .ctlreq = g_cache_config,
97 .taste = g_cache_taste,
98 .destroy_geom = g_cache_destroy_geom
101 #define OFF2BNO(off, sc) ((off) >> (sc)->sc_bshift)
102 #define BNO2OFF(bno, sc) ((bno) << (sc)->sc_bshift)
105 static struct g_cache_desc *
106 g_cache_alloc(struct g_cache_softc *sc)
108 struct g_cache_desc *dp;
110 mtx_assert(&sc->sc_mtx, MA_OWNED);
112 if (!TAILQ_EMPTY(&sc->sc_usedlist)) {
113 dp = TAILQ_FIRST(&sc->sc_usedlist);
114 TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used);
117 LIST_REMOVE(dp, d_next);
120 if (sc->sc_nent > sc->sc_maxent) {
124 dp = malloc(sizeof(*dp), M_GCACHE, M_NOWAIT | M_ZERO);
127 dp->d_data = uma_zalloc(sc->sc_zone, M_NOWAIT);
128 if (dp->d_data == NULL) {
137 g_cache_free(struct g_cache_softc *sc, struct g_cache_desc *dp)
140 mtx_assert(&sc->sc_mtx, MA_OWNED);
142 uma_zfree(sc->sc_zone, dp->d_data);
148 g_cache_free_used(struct g_cache_softc *sc)
150 struct g_cache_desc *dp;
153 mtx_assert(&sc->sc_mtx, MA_OWNED);
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);
161 LIST_REMOVE(dp, d_next);
162 g_cache_free(sc, dp);
167 g_cache_deliver(struct g_cache_softc *sc, struct bio *bp,
168 struct g_cache_desc *dp, int error)
170 off_t off1, off, len;
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"));
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;
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);
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);
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);
201 dp->d_flags |= D_FLAG_USED;
203 dp->d_atime = time_uptime;
207 g_cache_done(struct bio *bp)
209 struct g_cache_softc *sc;
210 struct g_cache_desc *dp;
211 struct bio *bp2, *tmpbp;
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);
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);
224 dp->d_biolist = NULL;
225 if (dp->d_flags & D_FLAG_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);
234 g_cache_free(sc, dp);
236 mtx_unlock(&sc->sc_mtx);
240 static struct g_cache_desc *
241 g_cache_lookup(struct g_cache_softc *sc, off_t bno)
243 struct g_cache_desc *dp;
245 mtx_assert(&sc->sc_mtx, MA_OWNED);
247 LIST_FOREACH(dp, &sc->sc_desclist[G_CACHE_BUCKET(bno)], d_next)
248 if (dp->d_bno == bno)
254 g_cache_read(struct g_cache_softc *sc, struct bio *bp)
257 struct g_cache_desc *dp;
259 mtx_lock(&sc->sc_mtx);
260 dp = g_cache_lookup(sc,
261 OFF2BNO(bp->bio_offset + bp->bio_completed, sc));
263 /* Add to waiters list or deliver. */
265 if (dp->d_biolist != NULL) {
266 G_CACHE_NEXT_BIO1(bp) = sc;
267 G_CACHE_NEXT_BIO2(bp) = dp->d_biolist;
270 g_cache_deliver(sc, bp, dp, 0);
271 mtx_unlock(&sc->sc_mtx);
275 /* Cache miss. Allocate entry and schedule bio. */
276 sc->sc_cachemisses++;
277 dp = g_cache_alloc(sc);
279 mtx_unlock(&sc->sc_mtx);
282 cbp = g_clone_bio(bp);
284 g_cache_free(sc, dp);
285 mtx_unlock(&sc->sc_mtx);
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;
293 LIST_INSERT_HEAD(&sc->sc_desclist[G_CACHE_BUCKET(dp->d_bno)],
295 mtx_unlock(&sc->sc_mtx);
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));
308 g_cache_invalidate(struct g_cache_softc *sc, struct bio *bp)
310 struct g_cache_desc *dp;
313 mtx_lock(&sc->sc_mtx);
314 bno = OFF2BNO(bp->bio_offset, sc);
315 lim = OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc);
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);
323 if (dp->d_biolist == NULL)
324 g_cache_free(sc, dp);
326 dp->d_flags = D_FLAG_INVALID;
331 } while (bno <= lim);
332 mtx_unlock(&sc->sc_mtx);
336 g_cache_start(struct bio *bp)
338 struct g_cache_softc *sc;
340 struct g_cache_desc *dp;
343 gp = bp->bio_to->geom;
345 G_CACHE_LOGREQ(bp, "Request received.");
346 switch (bp->bio_cmd) {
349 sc->sc_readbytes += bp->bio_length;
352 if (bp->bio_offset + bp->bio_length > sc->sc_tail)
354 if (OFF2BNO(bp->bio_offset, sc) ==
355 OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) {
357 sc->sc_cachereadbytes += bp->bio_length;
358 if (g_cache_read(sc, bp) == 0)
361 sc->sc_cachereadbytes -= bp->bio_length;
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);
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)
378 sc->sc_cachereadbytes -= bp->bio_length;
384 sc->sc_wrotebytes += bp->bio_length;
385 g_cache_invalidate(sc, bp);
388 cbp = g_clone_bio(bp);
390 g_io_deliver(bp, ENOMEM);
393 cbp->bio_done = g_std_done;
394 G_CACHE_LOGREQ(cbp, "Sending request.");
395 g_io_request(cbp, LIST_FIRST(&gp->consumer));
399 g_cache_go(void *arg)
401 struct g_cache_softc *sc = arg;
402 struct g_cache_desc *dp;
405 mtx_assert(&sc->sc_mtx, MA_OWNED);
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)
414 TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used);
416 dp->d_flags |= D_FLAG_USED;
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);
424 callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc);
428 g_cache_access(struct g_provider *pp, int dr, int dw, int de)
431 struct g_consumer *cp;
435 cp = LIST_FIRST(&gp->consumer);
436 error = g_access(cp, dr, dw, de);
442 g_cache_orphan(struct g_consumer *cp)
446 g_cache_destroy(cp->geom->softc, 1);
449 static struct g_cache_softc *
450 g_cache_find_device(struct g_class *mp, const char *name)
454 LIST_FOREACH(gp, &mp->geom, geom) {
455 if (strcmp(gp->name, name) == 0)
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)
465 struct g_cache_softc *sc;
467 struct g_provider *newpp;
468 struct g_consumer *cp;
478 G_CACHE_DEBUG(1, "Creating device %s.", md->md_name);
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);
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);
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);
501 gp = g_new_geomf(mp, md->md_name);
503 G_CACHE_DEBUG(0, "Cannot create geom %s.", md->md_name);
506 gp->softc = NULL; /* for a moment */
508 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
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,
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);
522 gp->start = g_cache_start;
523 gp->orphan = g_cache_orphan;
524 gp->access = g_cache_access;
525 gp->dumpconf = g_cache_dumpconf;
527 newpp = g_new_providerf(gp, "cache/%s", gp->name);
529 G_CACHE_DEBUG(0, "Cannot create provider cache/%s.", gp->name);
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);
538 cp = g_new_consumer(gp);
540 G_CACHE_DEBUG(0, "Cannot create consumer for %s.", gp->name);
543 if (g_attach(cp, pp) != 0) {
544 G_CACHE_DEBUG(0, "Cannot attach to provider %s.", pp->name);
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);
554 if (cp->provider != NULL)
556 g_destroy_consumer(cp);
559 g_destroy_provider(newpp);
561 if (gp->softc != NULL) {
562 mtx_destroy(&sc->sc_mtx);
571 g_cache_destroy(struct g_cache_softc *sc, boolean_t force)
574 struct g_provider *pp;
575 struct g_cache_desc *dp, *dp2;
582 pp = LIST_FIRST(&gp->provider);
583 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
585 G_CACHE_DEBUG(0, "Device %s is still open, so it "
586 "can't be definitely removed.", pp->name);
588 G_CACHE_DEBUG(1, "Device %s is still open (r%dw%de%d).",
589 pp->name, pp->acr, pp->acw, pp->ace);
593 G_CACHE_DEBUG(0, "Device %s removed.", gp->name);
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]);
600 dp2 = LIST_NEXT(dp, d_next);
601 g_cache_free(sc, dp);
605 mtx_unlock(&sc->sc_mtx);
606 mtx_destroy(&sc->sc_mtx);
607 uma_zdestroy(sc->sc_zone);
610 g_wither_geom(gp, ENXIO);
616 g_cache_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
619 return (g_cache_destroy(gp->softc, 0));
623 g_cache_read_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
625 struct g_provider *pp;
631 error = g_access(cp, 1, 0, 0);
636 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
639 g_access(cp, -1, 0, 0);
643 /* Decode metadata. */
644 cache_metadata_decode(buf, md);
651 g_cache_write_metadata(struct g_consumer *cp, struct g_cache_metadata *md)
653 struct g_provider *pp;
659 error = g_access(cp, 0, 1, 0);
663 buf = malloc((size_t)pp->sectorsize, M_GCACHE, M_WAITOK | M_ZERO);
664 cache_metadata_encode(md, buf);
666 error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf, pp->sectorsize);
668 g_access(cp, 0, -1, 0);
674 static struct g_geom *
675 g_cache_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
677 struct g_cache_metadata md;
678 struct g_consumer *cp;
682 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
685 G_CACHE_DEBUG(3, "Tasting %s.", pp->name);
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);
693 error = g_cache_read_metadata(cp, &md);
695 g_destroy_consumer(cp);
700 if (strcmp(md.md_magic, G_CACHE_MAGIC) != 0)
702 if (md.md_version > G_CACHE_VERSION) {
703 printf("geom_cache.ko module is too old to handle %s.\n",
707 if (md.md_provsize != pp->mediasize)
710 gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_AUTOMATIC);
712 G_CACHE_DEBUG(0, "Can't create %s.", md.md_name);
719 g_cache_ctl_create(struct gctl_req *req, struct g_class *mp)
721 struct g_cache_metadata md;
722 struct g_provider *pp;
724 intmax_t *bsize, *size;
730 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
732 gctl_error(req, "No '%s' argument", "nargs");
736 gctl_error(req, "Invalid number of arguments.");
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");
744 gctl_error(req, "No 'arg0' argument");
747 strlcpy(md.md_name, name, sizeof(md.md_name));
749 size = gctl_get_paraml(req, "size", sizeof(*size));
751 gctl_error(req, "No '%s' argument", "size");
754 if ((u_int)*size < 100) {
755 gctl_error(req, "Invalid '%s' argument", "size");
758 md.md_size = (u_int)*size;
760 bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
762 gctl_error(req, "No '%s' argument", "blocksize");
766 gctl_error(req, "Invalid '%s' argument", "blocksize");
769 md.md_bsize = (u_int)*bsize;
771 /* This field is not important here. */
774 name = gctl_get_asciiparam(req, "arg1");
776 gctl_error(req, "No 'arg1' argument");
779 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
780 name += strlen("/dev/");
781 pp = g_provider_by_name(name);
783 G_CACHE_DEBUG(1, "Provider %s is invalid.", name);
784 gctl_error(req, "Provider %s is invalid.", name);
787 gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_MANUAL);
789 gctl_error(req, "Can't create %s.", md.md_name);
795 g_cache_ctl_configure(struct gctl_req *req, struct g_class *mp)
797 struct g_cache_metadata md;
798 struct g_cache_softc *sc;
799 struct g_consumer *cp;
800 intmax_t *bsize, *size;
806 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
808 gctl_error(req, "No '%s' argument", "nargs");
812 gctl_error(req, "Missing device.");
816 name = gctl_get_asciiparam(req, "arg0");
818 gctl_error(req, "No 'arg0' argument");
821 sc = g_cache_find_device(mp, name);
823 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
824 gctl_error(req, "Device %s is invalid.", name);
828 size = gctl_get_paraml(req, "size", sizeof(*size));
830 gctl_error(req, "No '%s' argument", "size");
833 if ((u_int)*size != 0 && (u_int)*size < 100) {
834 gctl_error(req, "Invalid '%s' argument", "size");
837 if ((u_int)*size != 0)
838 sc->sc_maxent = (u_int)*size;
840 bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize));
842 gctl_error(req, "No '%s' argument", "blocksize");
846 gctl_error(req, "Invalid '%s' argument", "blocksize");
850 if (sc->sc_type != G_CACHE_TYPE_AUTOMATIC)
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;
859 md.md_size = sc->sc_maxent;
860 if ((u_int)*bsize != 0)
861 md.md_bsize = (u_int)*bsize;
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);
868 G_CACHE_DEBUG(2, "Metadata on %s updated.", cp->provider->name);
870 G_CACHE_DEBUG(0, "Cannot update metadata on %s (error=%d).",
871 cp->provider->name, error);
875 g_cache_ctl_destroy(struct gctl_req *req, struct g_class *mp)
877 int *nargs, *force, error, i;
878 struct g_cache_softc *sc;
884 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
886 gctl_error(req, "No '%s' argument", "nargs");
890 gctl_error(req, "Missing device(s).");
893 force = gctl_get_paraml(req, "force", sizeof(*force));
895 gctl_error(req, "No 'force' argument");
899 for (i = 0; i < *nargs; i++) {
900 snprintf(param, sizeof(param), "arg%d", i);
901 name = gctl_get_asciiparam(req, param);
903 gctl_error(req, "No 'arg%d' argument", i);
906 sc = g_cache_find_device(mp, name);
908 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
909 gctl_error(req, "Device %s is invalid.", name);
912 error = g_cache_destroy(sc, *force);
914 gctl_error(req, "Cannot destroy device %s (error=%d).",
922 g_cache_ctl_reset(struct gctl_req *req, struct g_class *mp)
924 struct g_cache_softc *sc;
931 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
933 gctl_error(req, "No '%s' argument", "nargs");
937 gctl_error(req, "Missing device(s).");
941 for (i = 0; i < *nargs; i++) {
942 snprintf(param, sizeof(param), "arg%d", i);
943 name = gctl_get_asciiparam(req, param);
945 gctl_error(req, "No 'arg%d' argument", i);
948 sc = g_cache_find_device(mp, name);
950 G_CACHE_DEBUG(1, "Device %s is invalid.", name);
951 gctl_error(req, "Device %s is invalid.", name);
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;
962 sc->sc_wrotebytes = 0;
967 g_cache_config(struct gctl_req *req, struct g_class *mp, const char *verb)
973 version = gctl_get_paraml(req, "version", sizeof(*version));
974 if (version == NULL) {
975 gctl_error(req, "No '%s' argument.", "version");
978 if (*version != G_CACHE_VERSION) {
979 gctl_error(req, "Userland and kernel parts are out of sync.");
983 if (strcmp(verb, "create") == 0) {
984 g_cache_ctl_create(req, mp);
986 } else if (strcmp(verb, "configure") == 0) {
987 g_cache_ctl_configure(req, mp);
989 } else if (strcmp(verb, "destroy") == 0 ||
990 strcmp(verb, "stop") == 0) {
991 g_cache_ctl_destroy(req, mp);
993 } else if (strcmp(verb, "reset") == 0) {
994 g_cache_ctl_reset(req, mp);
998 gctl_error(req, "Unknown verb.");
1002 g_cache_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1003 struct g_consumer *cp, struct g_provider *pp)
1005 struct g_cache_softc *sc;
1007 if (pp != NULL || cp != NULL)
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,
1017 sbuf_printf(sb, "%s<InvalidEntries>%u</InvalidEntries>\n", indent,
1019 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
1020 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
1022 sbuf_printf(sb, "%s<CacheReads>%ju</CacheReads>\n", indent,
1024 sbuf_printf(sb, "%s<CacheReadBytes>%ju</CacheReadBytes>\n", indent,
1025 sc->sc_cachereadbytes);
1026 sbuf_printf(sb, "%s<CacheHits>%ju</CacheHits>\n", indent,
1028 sbuf_printf(sb, "%s<CacheMisses>%ju</CacheMisses>\n", indent,
1029 sc->sc_cachemisses);
1030 sbuf_printf(sb, "%s<CacheFull>%ju</CacheFull>\n", indent,
1032 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
1033 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
1037 DECLARE_GEOM_CLASS(g_cache_class, g_cache);