From 04c7da702f70007d6252854c3fa763addb822122 Mon Sep 17 00:00:00 2001 From: Ruslan Ermilov Date: Fri, 6 Oct 2006 08:27:07 +0000 Subject: [PATCH] A GEOM cache can speed up read performance by sending fixed size read requests to its consumer. It has been developed to address the problem of a horrible read performance of a 64k blocksize FS residing on a RAID3 array with 8 data components, where a single disk component would only get 8k read requests, thus effectively killing disk performance under high load. Documentation will be provided later. I'd like to thank Vsevolod Lobko for his bright ideas, and Pawel Jakub Dawidek for helping me fix the nasty bug. --- etc/mtree/BSD.include.dist | 2 + include/Makefile | 4 +- sbin/geom/class/Makefile | 3 +- sbin/geom/class/cache/Makefile | 8 + sbin/geom/class/cache/geom_cache.c | 241 ++++++ sys/geom/cache/g_cache.c | 1037 ++++++++++++++++++++++++++ sys/geom/cache/g_cache.h | 146 ++++ sys/modules/geom/Makefile | 1 + sys/modules/geom/geom_cache/Makefile | 8 + 9 files changed, 1447 insertions(+), 3 deletions(-) create mode 100644 sbin/geom/class/cache/Makefile create mode 100644 sbin/geom/class/cache/geom_cache.c create mode 100644 sys/geom/cache/g_cache.c create mode 100644 sys/geom/cache/g_cache.h create mode 100644 sys/modules/geom/geom_cache/Makefile diff --git a/etc/mtree/BSD.include.dist b/etc/mtree/BSD.include.dist index 71de1fc989f1..f17cf1c9ce91 100644 --- a/etc/mtree/BSD.include.dist +++ b/etc/mtree/BSD.include.dist @@ -100,6 +100,8 @@ .. .. geom + cache + .. concat .. eli diff --git a/include/Makefile b/include/Makefile index 8c44cb1d2ab6..cad04d98d018 100644 --- a/include/Makefile +++ b/include/Makefile @@ -45,8 +45,8 @@ LSUBDIRS= cam/scsi \ fs/devfs fs/fdescfs fs/fifofs fs/msdosfs fs/ntfs fs/nullfs \ ${_fs_nwfs} fs/portalfs fs/procfs fs/smbfs fs/udf fs/umapfs \ fs/unionfs \ - geom/concat geom/eli geom/gate geom/label geom/mirror geom/nop \ - geom/raid3 geom/shsec geom/stripe \ + geom/cache geom/concat geom/eli geom/gate geom/label geom/mirror \ + geom/nop geom/raid3 geom/shsec geom/stripe \ isofs/cd9660 \ netatm/ipatm netatm/sigpvc netatm/spans netatm/uni \ netgraph/atm netgraph/netflow \ diff --git a/sbin/geom/class/Makefile b/sbin/geom/class/Makefile index 6ebdc6619c33..9c78e168ff4c 100644 --- a/sbin/geom/class/Makefile +++ b/sbin/geom/class/Makefile @@ -2,7 +2,8 @@ .include -SUBDIR= concat +SUBDIR= cache +SUBDIR+=concat .if ${MK_OPENSSL} != "no" SUBDIR+=eli .endif diff --git a/sbin/geom/class/cache/Makefile b/sbin/geom/class/cache/Makefile new file mode 100644 index 000000000000..6ca37e6c05e8 --- /dev/null +++ b/sbin/geom/class/cache/Makefile @@ -0,0 +1,8 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/../../misc + +CLASS= cache +NO_MAN= # notyet + +.include diff --git a/sbin/geom/class/cache/geom_cache.c b/sbin/geom/class/cache/geom_cache.c new file mode 100644 index 000000000000..19a0bf5a03fd --- /dev/null +++ b/sbin/geom/class/cache/geom_cache.c @@ -0,0 +1,241 @@ +/*- + * Copyright (c) 2006 Ruslan Ermilov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include "core/geom.h" +#include "misc/subr.h" + + +uint32_t lib_version = G_LIB_VERSION; +uint32_t version = G_CACHE_VERSION; + +static intmax_t blocksize_label = 65536; +static intmax_t size_label = 100; +static intmax_t blocksize_configure = 0; +static intmax_t size_configure = 0; + +static void cache_main(struct gctl_req *req, unsigned flags); +static void cache_clear(struct gctl_req *req); +static void cache_dump(struct gctl_req *req); +static void cache_label(struct gctl_req *req); + +struct g_command class_commands[] = { + { "clear", G_FLAG_VERBOSE, cache_main, G_NULL_OPTS, + "[-v] prov ..." + }, + { "configure", G_FLAG_VERBOSE, NULL, + { + { 'b', "blocksize", &blocksize_configure, G_TYPE_NUMBER }, + { 's', "size", &size_configure, G_TYPE_NUMBER }, + G_OPT_SENTINEL + }, + "[-v] [-b blocksize] [-s size] name" + }, + { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, + { + { 'b', "blocksize", &blocksize_label, G_TYPE_NUMBER }, + { 's', "size", &size_label, G_TYPE_NUMBER }, + G_OPT_SENTINEL + }, + "[-v] [-b blocksize] [-s size] name prov" + }, + { "destroy", G_FLAG_VERBOSE, NULL, + { + { 'f', "force", NULL, G_TYPE_NONE }, + G_OPT_SENTINEL + }, + "[-fv] name ..." + }, + { "dump", 0, cache_main, G_NULL_OPTS, + "prov ..." + }, + { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, cache_main, + { + { 'b', "blocksize", &blocksize_label, G_TYPE_NUMBER }, + { 's', "size", &size_label, G_TYPE_NUMBER }, + G_OPT_SENTINEL + }, + "[-v] [-b blocksize] [-s size] name prov" + }, + { "reset", G_FLAG_VERBOSE, NULL, G_NULL_OPTS, + "[-v] name ..." + }, + { "stop", G_FLAG_VERBOSE, NULL, + { + { 'f', "force", NULL, G_TYPE_NONE }, + G_OPT_SENTINEL + }, + "[-fv] name ..." + }, + G_CMD_SENTINEL +}; + +static int verbose = 0; + +static void +cache_main(struct gctl_req *req, unsigned flags) +{ + const char *name; + + if ((flags & G_FLAG_VERBOSE) != 0) + verbose = 1; + + name = gctl_get_ascii(req, "verb"); + if (name == NULL) { + gctl_error(req, "No '%s' argument.", "verb"); + return; + } + if (strcmp(name, "label") == 0) + cache_label(req); + else if (strcmp(name, "clear") == 0) + cache_clear(req); + else if (strcmp(name, "dump") == 0) + cache_dump(req); + else + gctl_error(req, "Unknown command: %s.", name); +} + +static void +cache_label(struct gctl_req *req) +{ + struct g_cache_metadata md; + u_char sector[512]; + const char *name; + int error, nargs; + intmax_t val; + + nargs = gctl_get_int(req, "nargs"); + if (nargs != 2) { + gctl_error(req, "Invalid number of arguments."); + return; + } + + strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic)); + md.md_version = G_CACHE_VERSION; + name = gctl_get_ascii(req, "arg0"); + strlcpy(md.md_name, name, sizeof(md.md_name)); + val = gctl_get_intmax(req, "blocksize"); + md.md_bsize = val; + val = gctl_get_intmax(req, "size"); + md.md_size = val; + + name = gctl_get_ascii(req, "arg1"); + md.md_provsize = g_get_mediasize(name); + if (md.md_provsize == 0) { + fprintf(stderr, "Can't get mediasize of %s: %s.\n", + name, strerror(errno)); + gctl_error(req, "Not fully done."); + return; + } + cache_metadata_encode(&md, sector); + error = g_metadata_store(name, sector, sizeof(sector)); + if (error != 0) { + fprintf(stderr, "Can't store metadata on %s: %s.\n", + name, strerror(error)); + gctl_error(req, "Not fully done."); + return; + } + if (verbose) + printf("Metadata value stored on %s.\n", name); +} + +static void +cache_clear(struct gctl_req *req) +{ + const char *name; + int error, i, nargs; + + nargs = gctl_get_int(req, "nargs"); + if (nargs < 1) { + gctl_error(req, "Too few arguments."); + return; + } + + for (i = 0; i < nargs; i++) { + name = gctl_get_ascii(req, "arg%d", i); + error = g_metadata_clear(name, G_CACHE_MAGIC); + if (error != 0) { + fprintf(stderr, "Can't clear metadata on %s: %s.\n", + name, strerror(error)); + gctl_error(req, "Not fully done."); + continue; + } + if (verbose) + printf("Metadata cleared on %s.\n", name); + } +} + +static void +cache_metadata_dump(const struct g_cache_metadata *md) +{ + + printf(" Magic string: %s\n", md->md_magic); + printf(" Metadata version: %u\n", (u_int)md->md_version); + printf(" Device name: %s\n", md->md_name); + printf(" Block size: %u\n", (u_int)md->md_bsize); + printf(" Cache size: %u\n", (u_int)md->md_size); + printf(" Provider size: %ju\n", (uintmax_t)md->md_provsize); +} + +static void +cache_dump(struct gctl_req *req) +{ + struct g_cache_metadata md, tmpmd; + const char *name; + int error, i, nargs; + + nargs = gctl_get_int(req, "nargs"); + if (nargs < 1) { + gctl_error(req, "Too few arguments."); + return; + } + + for (i = 0; i < nargs; i++) { + name = gctl_get_ascii(req, "arg%d", i); + error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), + G_CACHE_MAGIC); + if (error != 0) { + fprintf(stderr, "Can't read metadata from %s: %s.\n", + name, strerror(error)); + gctl_error(req, "Not fully done."); + continue; + } + cache_metadata_decode((u_char *)&tmpmd, &md); + printf("Metadata on %s:\n", name); + cache_metadata_dump(&md); + printf("\n"); + } +} diff --git a/sys/geom/cache/g_cache.c b/sys/geom/cache/g_cache.c new file mode 100644 index 000000000000..60b0254e1a73 --- /dev/null +++ b/sys/geom/cache/g_cache.c @@ -0,0 +1,1037 @@ +/*- + * Copyright (c) 2006 Ruslan Ermilov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static MALLOC_DEFINE(M_GCACHE, "gcache_data", "GEOM_CACHE Data"); + +SYSCTL_DECL(_kern_geom); +SYSCTL_NODE(_kern_geom, OID_AUTO, cache, CTLFLAG_RW, 0, "GEOM_CACHE stuff"); +static u_int g_cache_debug = 0; +SYSCTL_UINT(_kern_geom_cache, OID_AUTO, debug, CTLFLAG_RW, &g_cache_debug, 0, + "Debug level"); +static u_int g_cache_enable = 1; +SYSCTL_UINT(_kern_geom_cache, OID_AUTO, enable, CTLFLAG_RW, &g_cache_enable, 0, + ""); +static u_int g_cache_timeout = 10; +SYSCTL_UINT(_kern_geom_cache, OID_AUTO, timeout, CTLFLAG_RW, &g_cache_timeout, + 0, ""); +static u_int g_cache_idletime = 5; +SYSCTL_UINT(_kern_geom_cache, OID_AUTO, idletime, CTLFLAG_RW, &g_cache_idletime, + 0, ""); +static u_int g_cache_used_lo = 5; +static u_int g_cache_used_hi = 20; +static int +sysctl_handle_pct(SYSCTL_HANDLER_ARGS) +{ + u_int val = *(u_int *)arg1; + int error; + + error = sysctl_handle_int(oidp, &val, sizeof(val), req); + if (error || !req->newptr) + return (error); + if (val < 0 || val > 100) + return (EINVAL); + if ((arg1 == &g_cache_used_lo && val > g_cache_used_hi) || + (arg1 == &g_cache_used_hi && g_cache_used_lo > val)) + return (EINVAL); + *(u_int *)arg1 = val; + return (0); +} +SYSCTL_PROC(_kern_geom_cache, OID_AUTO, used_lo, CTLTYPE_UINT|CTLFLAG_RW, + &g_cache_used_lo, 0, sysctl_handle_pct, "IU", ""); +SYSCTL_PROC(_kern_geom_cache, OID_AUTO, used_hi, CTLTYPE_UINT|CTLFLAG_RW, + &g_cache_used_hi, 0, sysctl_handle_pct, "IU", ""); + + +static int g_cache_destroy(struct g_cache_softc *sc, boolean_t force); +static g_ctl_destroy_geom_t g_cache_destroy_geom; + +static g_taste_t g_cache_taste; +static g_ctl_req_t g_cache_config; +static g_dumpconf_t g_cache_dumpconf; + +struct g_class g_cache_class = { + .name = G_CACHE_CLASS_NAME, + .version = G_VERSION, + .ctlreq = g_cache_config, + .taste = g_cache_taste, + .destroy_geom = g_cache_destroy_geom +}; + +#define OFF2BNO(off, sc) ((off) >> (sc)->sc_bshift) +#define BNO2OFF(bno, sc) ((bno) << (sc)->sc_bshift) + + +static struct g_cache_desc * +g_cache_alloc(struct g_cache_softc *sc) +{ + struct g_cache_desc *dp; + + mtx_assert(&sc->sc_mtx, MA_OWNED); + + if (!TAILQ_EMPTY(&sc->sc_usedlist)) { + dp = TAILQ_FIRST(&sc->sc_usedlist); + TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used); + sc->sc_nused--; + dp->d_flags = 0; + LIST_REMOVE(dp, d_next); + return (dp); + } + if (sc->sc_nent > sc->sc_maxent) { + sc->sc_cachefull++; + return (NULL); + } + dp = malloc(sizeof(*dp), M_GCACHE, M_NOWAIT | M_ZERO); + if (dp == NULL) + return (NULL); + dp->d_data = uma_zalloc(sc->sc_zone, M_NOWAIT); + if (dp->d_data == NULL) { + free(dp, M_GCACHE); + return (NULL); + } + sc->sc_nent++; + return (dp); +} + +static void +g_cache_free(struct g_cache_softc *sc, struct g_cache_desc *dp) +{ + + mtx_assert(&sc->sc_mtx, MA_OWNED); + + uma_zfree(sc->sc_zone, dp->d_data); + free(dp, M_GCACHE); + sc->sc_nent--; +} + +static void +g_cache_free_used(struct g_cache_softc *sc) +{ + struct g_cache_desc *dp; + u_int n; + + mtx_assert(&sc->sc_mtx, MA_OWNED); + + n = g_cache_used_lo * sc->sc_maxent / 100; + while (sc->sc_nused > n) { + KASSERT(!TAILQ_EMPTY(&sc->sc_usedlist), ("used list empty")); + dp = TAILQ_FIRST(&sc->sc_usedlist); + TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used); + sc->sc_nused--; + LIST_REMOVE(dp, d_next); + g_cache_free(sc, dp); + } +} + +static void +g_cache_deliver(struct g_cache_softc *sc, struct bio *bp, + struct g_cache_desc *dp, int error) +{ + off_t off1, off, len; + + mtx_assert(&sc->sc_mtx, MA_OWNED); + KASSERT(OFF2BNO(bp->bio_offset, sc) <= dp->d_bno, ("wrong entry")); + KASSERT(OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc) >= + dp->d_bno, ("wrong entry")); + + off1 = BNO2OFF(dp->d_bno, sc); + off = MAX(bp->bio_offset, off1); + len = MIN(bp->bio_offset + bp->bio_length, off1 + sc->sc_bsize) - off; + + if (bp->bio_error == 0) + bp->bio_error = error; + if (bp->bio_error == 0) { + bcopy(dp->d_data + (off - off1), + bp->bio_data + (off - bp->bio_offset), len); + } + bp->bio_completed += len; + KASSERT(bp->bio_completed <= bp->bio_length, ("extra data")); + if (bp->bio_completed == bp->bio_length) { + if (bp->bio_error != 0) + bp->bio_completed = 0; + g_io_deliver(bp, bp->bio_error); + } + + if (dp->d_flags & D_FLAG_USED) { + TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used); + TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used); + } else if (OFF2BNO(off + len, sc) > dp->d_bno) { + TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used); + sc->sc_nused++; + dp->d_flags |= D_FLAG_USED; + } + dp->d_atime = time_uptime; +} + +static void +g_cache_done(struct bio *bp) +{ + struct g_cache_softc *sc; + struct g_cache_desc *dp; + struct bio *bp2, *tmpbp; + + sc = bp->bio_from->geom->softc; + KASSERT(G_CACHE_DESC1(bp) == sc, ("corrupt bio_caller in g_cache_done()")); + dp = G_CACHE_DESC2(bp); + mtx_lock(&sc->sc_mtx); + bp2 = dp->d_biolist; + while (bp2 != NULL) { + KASSERT(G_CACHE_NEXT_BIO1(bp2) == sc, ("corrupt bio_driver in g_cache_done()")); + tmpbp = G_CACHE_NEXT_BIO2(bp2); + g_cache_deliver(sc, bp2, dp, bp->bio_error); + bp2 = tmpbp; + } + dp->d_biolist = NULL; + if (dp->d_flags & D_FLAG_INVALID) { + sc->sc_invalid--; + g_cache_free(sc, dp); + } else if (bp->bio_error) { + LIST_REMOVE(dp, d_next); + if (dp->d_flags & D_FLAG_USED) { + TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used); + sc->sc_nused--; + } + g_cache_free(sc, dp); + } + mtx_unlock(&sc->sc_mtx); + g_destroy_bio(bp); +} + +static struct g_cache_desc * +g_cache_lookup(struct g_cache_softc *sc, off_t bno) +{ + struct g_cache_desc *dp; + + mtx_assert(&sc->sc_mtx, MA_OWNED); + + LIST_FOREACH(dp, &sc->sc_desclist[G_CACHE_BUCKET(bno)], d_next) + if (dp->d_bno == bno) + return (dp); + return (NULL); +} + +static int +g_cache_read(struct g_cache_softc *sc, struct bio *bp) +{ + struct bio *cbp; + struct g_cache_desc *dp; + + mtx_lock(&sc->sc_mtx); + dp = g_cache_lookup(sc, + OFF2BNO(bp->bio_offset + bp->bio_completed, sc)); + if (dp != NULL) { + /* Add to waiters list or deliver. */ + sc->sc_cachehits++; + if (dp->d_biolist != NULL) { + G_CACHE_NEXT_BIO1(bp) = sc; + G_CACHE_NEXT_BIO2(bp) = dp->d_biolist; + dp->d_biolist = bp; + } else + g_cache_deliver(sc, bp, dp, 0); + mtx_unlock(&sc->sc_mtx); + return (0); + } + + /* Cache miss. Allocate entry and schedule bio. */ + sc->sc_cachemisses++; + dp = g_cache_alloc(sc); + if (dp == NULL) { + mtx_unlock(&sc->sc_mtx); + return (ENOMEM); + } + cbp = g_clone_bio(bp); + if (cbp == NULL) { + g_cache_free(sc, dp); + mtx_unlock(&sc->sc_mtx); + return (ENOMEM); + } + + dp->d_bno = OFF2BNO(bp->bio_offset + bp->bio_completed, sc); + G_CACHE_NEXT_BIO1(bp) = sc; + G_CACHE_NEXT_BIO2(bp) = NULL; + dp->d_biolist = bp; + LIST_INSERT_HEAD(&sc->sc_desclist[G_CACHE_BUCKET(dp->d_bno)], + dp, d_next); + mtx_unlock(&sc->sc_mtx); + + G_CACHE_DESC1(cbp) = sc; + G_CACHE_DESC2(cbp) = dp; + cbp->bio_done = g_cache_done; + cbp->bio_offset = BNO2OFF(dp->d_bno, sc); + cbp->bio_data = dp->d_data; + cbp->bio_length = sc->sc_bsize; + g_io_request(cbp, LIST_FIRST(&bp->bio_to->geom->consumer)); + return (0); +} + +static void +g_cache_invalidate(struct g_cache_softc *sc, struct bio *bp) +{ + struct g_cache_desc *dp; + off_t bno, lim; + + mtx_lock(&sc->sc_mtx); + bno = OFF2BNO(bp->bio_offset, sc); + lim = OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc); + do { + if ((dp = g_cache_lookup(sc, bno)) != NULL) { + LIST_REMOVE(dp, d_next); + if (dp->d_flags & D_FLAG_USED) { + TAILQ_REMOVE(&sc->sc_usedlist, dp, d_used); + sc->sc_nused--; + } + if (dp->d_biolist == NULL) + g_cache_free(sc, dp); + else { + dp->d_flags = D_FLAG_INVALID; + sc->sc_invalid++; + } + } + bno++; + } while (bno <= lim); + mtx_unlock(&sc->sc_mtx); +} + +static void +g_cache_start(struct bio *bp) +{ + struct g_cache_softc *sc; + struct g_geom *gp; + struct g_cache_desc *dp; + struct bio *cbp; + + gp = bp->bio_to->geom; + sc = gp->softc; + G_CACHE_LOGREQ(bp, "Request received."); + switch (bp->bio_cmd) { + case BIO_READ: + sc->sc_reads++; + sc->sc_readbytes += bp->bio_length; + if (!g_cache_enable) + break; + if (bp->bio_offset + bp->bio_length > sc->sc_tail) + break; + if (OFF2BNO(bp->bio_offset, sc) == + OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) { + sc->sc_cachereads++; + sc->sc_cachereadbytes += bp->bio_length; + if (g_cache_read(sc, bp) == 0) + return; + sc->sc_cachereads--; + sc->sc_cachereadbytes -= bp->bio_length; + break; + } else if (OFF2BNO(bp->bio_offset, sc) + 1 == + OFF2BNO(bp->bio_offset + bp->bio_length - 1, sc)) { + mtx_lock(&sc->sc_mtx); + dp = g_cache_lookup(sc, OFF2BNO(bp->bio_offset, sc)); + if (dp == NULL || dp->d_biolist != NULL) { + mtx_unlock(&sc->sc_mtx); + break; + } + sc->sc_cachereads++; + sc->sc_cachereadbytes += bp->bio_length; + g_cache_deliver(sc, bp, dp, 0); + mtx_unlock(&sc->sc_mtx); + if (g_cache_read(sc, bp) == 0) + return; + sc->sc_cachereads--; + sc->sc_cachereadbytes -= bp->bio_length; + break; + } + break; + case BIO_WRITE: + sc->sc_writes++; + sc->sc_wrotebytes += bp->bio_length; + g_cache_invalidate(sc, bp); + break; + } + cbp = g_clone_bio(bp); + if (cbp == NULL) { + g_io_deliver(bp, ENOMEM); + return; + } + cbp->bio_done = g_std_done; + G_CACHE_LOGREQ(cbp, "Sending request."); + g_io_request(cbp, LIST_FIRST(&gp->consumer)); +} + +static void +g_cache_go(void *arg) +{ + struct g_cache_softc *sc = arg; + struct g_cache_desc *dp; + int i; + + mtx_assert(&sc->sc_mtx, MA_OWNED); + + /* Forcibly mark idle ready entries as used. */ + for (i = 0; i < G_CACHE_BUCKETS; i++) { + LIST_FOREACH(dp, &sc->sc_desclist[i], d_next) { + if (dp->d_flags & D_FLAG_USED || + dp->d_biolist != NULL || + time_uptime - dp->d_atime < g_cache_idletime) + continue; + TAILQ_INSERT_TAIL(&sc->sc_usedlist, dp, d_used); + sc->sc_nused++; + dp->d_flags |= D_FLAG_USED; + } + } + + /* Keep the number of used entries low. */ + if (sc->sc_nused > g_cache_used_hi * sc->sc_maxent / 100) + g_cache_free_used(sc); + + callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc); +} + +static int +g_cache_access(struct g_provider *pp, int dr, int dw, int de) +{ + struct g_geom *gp; + struct g_consumer *cp; + int error; + + gp = pp->geom; + cp = LIST_FIRST(&gp->consumer); + error = g_access(cp, dr, dw, de); + + return (error); +} + +static void +g_cache_orphan(struct g_consumer *cp) +{ + + g_topology_assert(); + g_cache_destroy(cp->geom->softc, 1); +} + +static struct g_cache_softc * +g_cache_find_device(struct g_class *mp, const char *name) +{ + struct g_geom *gp; + + LIST_FOREACH(gp, &mp->geom, geom) { + if (strcmp(gp->name, name) == 0) + return (gp->softc); + } + return (NULL); +} + +static struct g_geom * +g_cache_create(struct g_class *mp, struct g_provider *pp, + const struct g_cache_metadata *md, u_int type) +{ + struct g_cache_softc *sc; + struct g_geom *gp; + struct g_provider *newpp; + struct g_consumer *cp; + u_int bshift; + int i; + + g_topology_assert(); + + gp = NULL; + newpp = NULL; + cp = NULL; + + G_CACHE_DEBUG(1, "Creating device %s.", md->md_name); + + /* Cache size is minimum 100. */ + if (md->md_size < 100) { + G_CACHE_DEBUG(0, "Invalid size for device %s.", md->md_name); + return (NULL); + } + + /* Block size restrictions. */ + bshift = ffs(md->md_bsize) - 1; + if (md->md_bsize == 0 || md->md_bsize > MAXPHYS || + md->md_bsize != 1 << bshift || + (md->md_bsize % pp->sectorsize) != 0) { + G_CACHE_DEBUG(0, "Invalid blocksize for provider %s.", pp->name); + return (NULL); + } + + /* Check for duplicate unit. */ + if (g_cache_find_device(mp, (const char *)&md->md_name) != NULL) { + G_CACHE_DEBUG(0, "Provider %s already exists.", md->md_name); + return (NULL); + } + + gp = g_new_geomf(mp, md->md_name); + if (gp == NULL) { + G_CACHE_DEBUG(0, "Cannot create geom %s.", md->md_name); + return (NULL); + } + gp->softc = NULL; /* for a moment */ + + sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); + sc->sc_type = type; + sc->sc_bshift = bshift; + sc->sc_bsize = 1 << bshift; + sc->sc_zone = uma_zcreate("gcache", sc->sc_bsize, NULL, NULL, NULL, NULL, + UMA_ALIGN_PTR, 0); + mtx_init(&sc->sc_mtx, "GEOM CACHE mutex", NULL, MTX_DEF); + for (i = 0; i < G_CACHE_BUCKETS; i++) + LIST_INIT(&sc->sc_desclist[i]); + TAILQ_INIT(&sc->sc_usedlist); + sc->sc_maxent = md->md_size; + callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0); + gp->softc = sc; + sc->sc_geom = gp; + gp->start = g_cache_start; + gp->orphan = g_cache_orphan; + gp->access = g_cache_access; + gp->dumpconf = g_cache_dumpconf; + + newpp = g_new_providerf(gp, "cache/%s", gp->name); + if (newpp == NULL) { + G_CACHE_DEBUG(0, "Cannot create provider cache/%s.", gp->name); + goto fail; + } + newpp->sectorsize = pp->sectorsize; + newpp->mediasize = pp->mediasize; + if (type == G_CACHE_TYPE_AUTOMATIC) + newpp->mediasize -= pp->sectorsize; + sc->sc_tail = BNO2OFF(OFF2BNO(newpp->mediasize, sc), sc); + + cp = g_new_consumer(gp); + if (cp == NULL) { + G_CACHE_DEBUG(0, "Cannot create consumer for %s.", gp->name); + goto fail; + } + if (g_attach(cp, pp) != 0) { + G_CACHE_DEBUG(0, "Cannot attach to provider %s.", pp->name); + goto fail; + } + + g_error_provider(newpp, 0); + G_CACHE_DEBUG(0, "Device %s created.", gp->name); + callout_reset(&sc->sc_callout, g_cache_timeout * hz, g_cache_go, sc); + return (gp); +fail: + if (cp != NULL) { + if (cp->provider != NULL) + g_detach(cp); + g_destroy_consumer(cp); + } + if (newpp != NULL) + g_destroy_provider(newpp); + if (gp != NULL) { + if (gp->softc != NULL) { + mtx_destroy(&sc->sc_mtx); + g_free(gp->softc); + } + g_destroy_geom(gp); + } + return (NULL); +} + +static int +g_cache_destroy(struct g_cache_softc *sc, boolean_t force) +{ + struct g_geom *gp; + struct g_provider *pp; + struct g_cache_desc *dp, *dp2; + int i; + + g_topology_assert(); + if (sc == NULL) + return (ENXIO); + gp = sc->sc_geom; + pp = LIST_FIRST(&gp->provider); + if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { + if (force) { + G_CACHE_DEBUG(0, "Device %s is still open, so it " + "can't be definitely removed.", pp->name); + } else { + G_CACHE_DEBUG(1, "Device %s is still open (r%dw%de%d).", + pp->name, pp->acr, pp->acw, pp->ace); + return (EBUSY); + } + } else { + G_CACHE_DEBUG(0, "Device %s removed.", gp->name); + } + callout_drain(&sc->sc_callout); + mtx_lock(&sc->sc_mtx); + for (i = 0; i < G_CACHE_BUCKETS; i++) { + dp = LIST_FIRST(&sc->sc_desclist[i]); + while (dp != NULL) { + dp2 = LIST_NEXT(dp, d_next); + g_cache_free(sc, dp); + dp = dp2; + } + } + mtx_unlock(&sc->sc_mtx); + mtx_destroy(&sc->sc_mtx); + uma_zdestroy(sc->sc_zone); + g_free(sc); + gp->softc = NULL; + g_wither_geom(gp, ENXIO); + + return (0); +} + +static int +g_cache_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) +{ + + return (g_cache_destroy(gp->softc, 0)); +} + +static int +g_cache_read_metadata(struct g_consumer *cp, struct g_cache_metadata *md) +{ + struct g_provider *pp; + u_char *buf; + int error; + + g_topology_assert(); + + error = g_access(cp, 1, 0, 0); + if (error != 0) + return (error); + pp = cp->provider; + g_topology_unlock(); + buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, + &error); + g_topology_lock(); + g_access(cp, -1, 0, 0); + if (buf == NULL) + return (error); + + /* Decode metadata. */ + cache_metadata_decode(buf, md); + g_free(buf); + + return (0); +} + +static int +g_cache_write_metadata(struct g_consumer *cp, struct g_cache_metadata *md) +{ + struct g_provider *pp; + u_char *buf; + int error; + + g_topology_assert(); + + error = g_access(cp, 0, 1, 0); + if (error != 0) + return (error); + pp = cp->provider; + buf = malloc((size_t)pp->sectorsize, M_GCACHE, M_WAITOK | M_ZERO); + cache_metadata_encode(md, buf); + g_topology_unlock(); + error = g_write_data(cp, pp->mediasize - pp->sectorsize, buf, pp->sectorsize); + g_topology_lock(); + g_access(cp, 0, -1, 0); + free(buf, M_GCACHE); + + return (error); +} + +static struct g_geom * +g_cache_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) +{ + struct g_cache_metadata md; + struct g_consumer *cp; + struct g_geom *gp; + int error; + + g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); + g_topology_assert(); + + G_CACHE_DEBUG(3, "Tasting %s.", pp->name); + + gp = g_new_geomf(mp, "cache:taste"); + gp->start = g_cache_start; + gp->orphan = g_cache_orphan; + gp->access = g_cache_access; + cp = g_new_consumer(gp); + g_attach(cp, pp); + error = g_cache_read_metadata(cp, &md); + g_detach(cp); + g_destroy_consumer(cp); + g_destroy_geom(gp); + if (error != 0) + return (NULL); + + if (strcmp(md.md_magic, G_CACHE_MAGIC) != 0) + return (NULL); + if (md.md_version > G_CACHE_VERSION) { + printf("geom_cache.ko module is too old to handle %s.\n", + pp->name); + return (NULL); + } + if (md.md_provsize != pp->mediasize) + return (NULL); + + gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_AUTOMATIC); + if (gp == NULL) { + G_CACHE_DEBUG(0, "Can't create %s.", md.md_name); + return (NULL); + } + return (gp); +} + +static void +g_cache_ctl_create(struct gctl_req *req, struct g_class *mp) +{ + struct g_cache_metadata md; + struct g_provider *pp; + struct g_geom *gp; + intmax_t *bsize, *size; + const char *name; + int *nargs; + + g_topology_assert(); + + nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); + if (nargs == NULL) { + gctl_error(req, "No '%s' argument", "nargs"); + return; + } + if (*nargs != 2) { + gctl_error(req, "Invalid number of arguments."); + return; + } + + strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic)); + md.md_version = G_CACHE_VERSION; + name = gctl_get_asciiparam(req, "arg0"); + if (name == NULL) { + gctl_error(req, "No 'arg0' argument"); + return; + } + strlcpy(md.md_name, name, sizeof(md.md_name)); + + size = gctl_get_paraml(req, "size", sizeof(*size)); + if (size == NULL) { + gctl_error(req, "No '%s' argument", "size"); + return; + } + if ((u_int)*size < 100) { + gctl_error(req, "Invalid '%s' argument", "size"); + return; + } + md.md_size = (u_int)*size; + + bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize)); + if (bsize == NULL) { + gctl_error(req, "No '%s' argument", "blocksize"); + return; + } + if (*bsize < 0) { + gctl_error(req, "Invalid '%s' argument", "blocksize"); + return; + } + md.md_bsize = (u_int)*bsize; + + /* This field is not important here. */ + md.md_provsize = 0; + + name = gctl_get_asciiparam(req, "arg1"); + if (name == NULL) { + gctl_error(req, "No 'arg1' argument"); + return; + } + if (strncmp(name, "/dev/", strlen("/dev/")) == 0) + name += strlen("/dev/"); + pp = g_provider_by_name(name); + if (pp == NULL) { + G_CACHE_DEBUG(1, "Provider %s is invalid.", name); + gctl_error(req, "Provider %s is invalid.", name); + return; + } + gp = g_cache_create(mp, pp, &md, G_CACHE_TYPE_MANUAL); + if (gp == NULL) { + gctl_error(req, "Can't create %s.", md.md_name); + return; + } +} + +static void +g_cache_ctl_configure(struct gctl_req *req, struct g_class *mp) +{ + struct g_cache_metadata md; + struct g_cache_softc *sc; + struct g_consumer *cp; + intmax_t *bsize, *size; + const char *name; + int error, *nargs; + + g_topology_assert(); + + nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); + if (nargs == NULL) { + gctl_error(req, "No '%s' argument", "nargs"); + return; + } + if (*nargs != 1) { + gctl_error(req, "Missing device."); + return; + } + + name = gctl_get_asciiparam(req, "arg0"); + if (name == NULL) { + gctl_error(req, "No 'arg0' argument"); + return; + } + sc = g_cache_find_device(mp, name); + if (sc == NULL) { + G_CACHE_DEBUG(1, "Device %s is invalid.", name); + gctl_error(req, "Device %s is invalid.", name); + return; + } + + size = gctl_get_paraml(req, "size", sizeof(*size)); + if (size == NULL) { + gctl_error(req, "No '%s' argument", "size"); + return; + } + if ((u_int)*size != 0 && (u_int)*size < 100) { + gctl_error(req, "Invalid '%s' argument", "size"); + return; + } + if ((u_int)*size != 0) + sc->sc_maxent = (u_int)*size; + + bsize = gctl_get_paraml(req, "blocksize", sizeof(*bsize)); + if (bsize == NULL) { + gctl_error(req, "No '%s' argument", "blocksize"); + return; + } + if (*bsize < 0) { + gctl_error(req, "Invalid '%s' argument", "blocksize"); + return; + } + + if (sc->sc_type != G_CACHE_TYPE_AUTOMATIC) + return; + + strlcpy(md.md_name, name, sizeof(md.md_name)); + strlcpy(md.md_magic, G_CACHE_MAGIC, sizeof(md.md_magic)); + md.md_version = G_CACHE_VERSION; + if ((u_int)*size != 0) + md.md_size = (u_int)*size; + else + md.md_size = sc->sc_maxent; + if ((u_int)*bsize != 0) + md.md_bsize = (u_int)*bsize; + else + md.md_bsize = sc->sc_bsize; + cp = LIST_FIRST(&sc->sc_geom->consumer); + md.md_provsize = cp->provider->mediasize; + error = g_cache_write_metadata(cp, &md); + if (error == 0) + G_CACHE_DEBUG(2, "Metadata on %s updated.", cp->provider->name); + else + G_CACHE_DEBUG(0, "Cannot update metadata on %s (error=%d).", + cp->provider->name, error); +} + +static void +g_cache_ctl_destroy(struct gctl_req *req, struct g_class *mp) +{ + int *nargs, *force, error, i; + struct g_cache_softc *sc; + const char *name; + char param[16]; + + g_topology_assert(); + + nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); + if (nargs == NULL) { + gctl_error(req, "No '%s' argument", "nargs"); + return; + } + if (*nargs <= 0) { + gctl_error(req, "Missing device(s)."); + return; + } + force = gctl_get_paraml(req, "force", sizeof(*force)); + if (force == NULL) { + gctl_error(req, "No 'force' argument"); + return; + } + + for (i = 0; i < *nargs; i++) { + snprintf(param, sizeof(param), "arg%d", i); + name = gctl_get_asciiparam(req, param); + if (name == NULL) { + gctl_error(req, "No 'arg%d' argument", i); + return; + } + sc = g_cache_find_device(mp, name); + if (sc == NULL) { + G_CACHE_DEBUG(1, "Device %s is invalid.", name); + gctl_error(req, "Device %s is invalid.", name); + return; + } + error = g_cache_destroy(sc, *force); + if (error != 0) { + gctl_error(req, "Cannot destroy device %s (error=%d).", + sc->sc_name, error); + return; + } + } +} + +static void +g_cache_ctl_reset(struct gctl_req *req, struct g_class *mp) +{ + struct g_cache_softc *sc; + const char *name; + char param[16]; + int i, *nargs; + + g_topology_assert(); + + nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); + if (nargs == NULL) { + gctl_error(req, "No '%s' argument", "nargs"); + return; + } + if (*nargs <= 0) { + gctl_error(req, "Missing device(s)."); + return; + } + + for (i = 0; i < *nargs; i++) { + snprintf(param, sizeof(param), "arg%d", i); + name = gctl_get_asciiparam(req, param); + if (name == NULL) { + gctl_error(req, "No 'arg%d' argument", i); + return; + } + sc = g_cache_find_device(mp, name); + if (sc == NULL) { + G_CACHE_DEBUG(1, "Device %s is invalid.", name); + gctl_error(req, "Device %s is invalid.", name); + return; + } + sc->sc_reads = 0; + sc->sc_readbytes = 0; + sc->sc_cachereads = 0; + sc->sc_cachereadbytes = 0; + sc->sc_cachehits = 0; + sc->sc_cachemisses = 0; + sc->sc_cachefull = 0; + sc->sc_writes = 0; + sc->sc_wrotebytes = 0; + } +} + +static void +g_cache_config(struct gctl_req *req, struct g_class *mp, const char *verb) +{ + uint32_t *version; + + g_topology_assert(); + + version = gctl_get_paraml(req, "version", sizeof(*version)); + if (version == NULL) { + gctl_error(req, "No '%s' argument.", "version"); + return; + } + if (*version != G_CACHE_VERSION) { + gctl_error(req, "Userland and kernel parts are out of sync."); + return; + } + + if (strcmp(verb, "create") == 0) { + g_cache_ctl_create(req, mp); + return; + } else if (strcmp(verb, "configure") == 0) { + g_cache_ctl_configure(req, mp); + return; + } else if (strcmp(verb, "destroy") == 0 || + strcmp(verb, "stop") == 0) { + g_cache_ctl_destroy(req, mp); + return; + } else if (strcmp(verb, "reset") == 0) { + g_cache_ctl_reset(req, mp); + return; + } + + gctl_error(req, "Unknown verb."); +} + +static void +g_cache_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, + struct g_consumer *cp, struct g_provider *pp) +{ + struct g_cache_softc *sc; + + if (pp != NULL || cp != NULL) + return; + sc = gp->softc; + sbuf_printf(sb, "%s%u\n", indent, sc->sc_maxent); + sbuf_printf(sb, "%s%u\n", indent, sc->sc_bsize); + sbuf_printf(sb, "%s%ju\n", indent, + (uintmax_t)sc->sc_tail); + sbuf_printf(sb, "%s%u\n", indent, sc->sc_nent); + sbuf_printf(sb, "%s%u\n", indent, + sc->sc_nused); + sbuf_printf(sb, "%s%u\n", indent, + sc->sc_invalid); + sbuf_printf(sb, "%s%ju\n", indent, sc->sc_reads); + sbuf_printf(sb, "%s%ju\n", indent, + sc->sc_readbytes); + sbuf_printf(sb, "%s%ju\n", indent, + sc->sc_cachereads); + sbuf_printf(sb, "%s%ju\n", indent, + sc->sc_cachereadbytes); + sbuf_printf(sb, "%s%ju\n", indent, + sc->sc_cachehits); + sbuf_printf(sb, "%s%ju\n", indent, + sc->sc_cachemisses); + sbuf_printf(sb, "%s%ju\n", indent, + sc->sc_cachefull); + sbuf_printf(sb, "%s%ju\n", indent, sc->sc_writes); + sbuf_printf(sb, "%s%ju\n", indent, + sc->sc_wrotebytes); +} + +DECLARE_GEOM_CLASS(g_cache_class, g_cache); diff --git a/sys/geom/cache/g_cache.h b/sys/geom/cache/g_cache.h new file mode 100644 index 000000000000..4164abf7cefd --- /dev/null +++ b/sys/geom/cache/g_cache.h @@ -0,0 +1,146 @@ +/*- + * Copyright (c) 2006 Ruslan Ermilov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _G_CACHE_H_ +#define _G_CACHE_H_ + +#include + +#define G_CACHE_CLASS_NAME "CACHE" +#define G_CACHE_MAGIC "GEOM::CACHE" +#define G_CACHE_VERSION 1 + +#ifdef _KERNEL +#define G_CACHE_TYPE_MANUAL 0 +#define G_CACHE_TYPE_AUTOMATIC 1 + +#define G_CACHE_DEBUG(lvl, ...) do { \ + if (g_cache_debug >= (lvl)) { \ + printf("GEOM_CACHE"); \ + if (g_cache_debug > 0) \ + printf("[%u]", lvl); \ + printf(": "); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } \ +} while (0) +#define G_CACHE_LOGREQ(bp, ...) do { \ + if (g_cache_debug >= 2) { \ + printf("GEOM_CACHE[2]: "); \ + printf(__VA_ARGS__); \ + printf(" "); \ + g_print_bio(bp); \ + printf("\n"); \ + } \ +} while (0) + +#define G_CACHE_BUCKETS (1 << 3) +#define G_CACHE_BUCKET(bno) ((bno) & (G_CACHE_BUCKETS - 1)) + +struct g_cache_softc { + struct g_geom *sc_geom; + int sc_type; + u_int sc_bshift; + u_int sc_bsize; + off_t sc_tail; + struct mtx sc_mtx; + struct callout sc_callout; + LIST_HEAD(, g_cache_desc) sc_desclist[G_CACHE_BUCKETS]; + TAILQ_HEAD(, g_cache_desc) sc_usedlist; + uma_zone_t sc_zone; + + u_int sc_maxent; /* max entries */ + u_int sc_nent; /* allocated entries */ + u_int sc_nused; /* re-useable entries */ + u_int sc_invalid; /* invalid entries */ + + uintmax_t sc_reads; /* #reads */ + uintmax_t sc_readbytes; /* bytes read */ + uintmax_t sc_cachereads; /* #reads from cache */ + uintmax_t sc_cachereadbytes; /* bytes read from cache */ + uintmax_t sc_cachehits; /* cache hits */ + uintmax_t sc_cachemisses; /* cache misses */ + uintmax_t sc_cachefull; /* #times a cache was full */ + uintmax_t sc_writes; /* #writes */ + uintmax_t sc_wrotebytes; /* bytes written */ +}; +#define sc_name sc_geom->name + +struct g_cache_desc { + off_t d_bno; /* block number */ + caddr_t d_data; /* data area */ + struct bio *d_biolist; /* waiters */ + time_t d_atime; /* access time */ + int d_flags; /* flags */ +#define D_FLAG_USED (1 << 0) /* can be reused */ +#define D_FLAG_INVALID (1 << 1) /* invalid */ + LIST_ENTRY(g_cache_desc) d_next; /* list */ + TAILQ_ENTRY(g_cache_desc) d_used; /* used list */ +}; + +#define G_CACHE_NEXT_BIO1(bp) (bp)->bio_driver1 +#define G_CACHE_NEXT_BIO2(bp) (bp)->bio_driver2 +#define G_CACHE_DESC1(bp) (bp)->bio_caller1 +#define G_CACHE_DESC2(bp) (bp)->bio_caller2 + +#endif /* _KERNEL */ + +struct g_cache_metadata { + char md_magic[16]; /* Magic value. */ + uint32_t md_version; /* Version number. */ + char md_name[16]; /* Cache value. */ + uint32_t md_bsize; /* Cache block size. */ + uint32_t md_size; /* Cache size. */ + uint64_t md_provsize; /* Provider's size. */ +}; + +static __inline void +cache_metadata_encode(const struct g_cache_metadata *md, u_char *data) +{ + + bcopy(md->md_magic, data, sizeof(md->md_magic)); + le32enc(data + 16, md->md_version); + bcopy(md->md_name, data + 20, sizeof(md->md_name)); + le32enc(data + 36, md->md_bsize); + le32enc(data + 40, md->md_size); + le64enc(data + 44, md->md_provsize); +} + +static __inline void +cache_metadata_decode(const u_char *data, struct g_cache_metadata *md) +{ + + bcopy(data, md->md_magic, sizeof(md->md_magic)); + md->md_version = le32dec(data + 16); + bcopy(data + 20, md->md_name, sizeof(md->md_name)); + md->md_bsize = le32dec(data + 36); + md->md_size = le16dec(data + 40); + md->md_provsize = le64dec(data + 44); +} + +#endif /* _G_CACHE_H_ */ diff --git a/sys/modules/geom/Makefile b/sys/modules/geom/Makefile index cf99e377f7c8..d5091057f527 100644 --- a/sys/modules/geom/Makefile +++ b/sys/modules/geom/Makefile @@ -3,6 +3,7 @@ SUBDIR= geom_apple \ geom_bde \ geom_bsd \ + geom_cache \ geom_ccd \ geom_concat \ geom_eli \ diff --git a/sys/modules/geom/geom_cache/Makefile b/sys/modules/geom/geom_cache/Makefile new file mode 100644 index 000000000000..7ccbe98fdac9 --- /dev/null +++ b/sys/modules/geom/geom_cache/Makefile @@ -0,0 +1,8 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/../../../geom/cache + +KMOD= geom_cache +SRCS= g_cache.c + +.include