From a350b3637d5559c60f1a6a9b05f045ac5572beff Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 23 Feb 2009 10:07:51 +0000 Subject: [PATCH] Code to create, map and ident the storage silo git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3806 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/stevedore.c | 1 + varnish-cache/bin/varnishd/stevedore.h | 1 + .../bin/varnishd/storage_persistent.c | 137 +++++++++++++++++- varnish-cache/include/persistent.h | 2 + 4 files changed, 139 insertions(+), 2 deletions(-) diff --git a/varnish-cache/bin/varnishd/stevedore.c b/varnish-cache/bin/varnishd/stevedore.c index 4cf46d06..6e19b5df 100644 --- a/varnish-cache/bin/varnishd/stevedore.c +++ b/varnish-cache/bin/varnishd/stevedore.c @@ -128,6 +128,7 @@ STV_open(void) const struct choice STV_choice[] = { { "file", &smf_stevedore }, { "malloc", &sma_stevedore }, + { "persistent", &smp_stevedore }, #ifdef HAVE_LIBUMEM { "umem", &smu_stevedore }, #endif diff --git a/varnish-cache/bin/varnishd/stevedore.h b/varnish-cache/bin/varnishd/stevedore.h index c1c192d3..ed052a70 100644 --- a/varnish-cache/bin/varnishd/stevedore.h +++ b/varnish-cache/bin/varnishd/stevedore.h @@ -71,6 +71,7 @@ void SMS_Init(void); extern struct stevedore sma_stevedore; extern struct stevedore smf_stevedore; +extern struct stevedore smp_stevedore; #ifdef HAVE_LIBUMEM extern struct stevedore smu_stevedore; #endif diff --git a/varnish-cache/bin/varnishd/storage_persistent.c b/varnish-cache/bin/varnishd/storage_persistent.c index b039ec8b..af4aea0f 100644 --- a/varnish-cache/bin/varnishd/storage_persistent.c +++ b/varnish-cache/bin/varnishd/storage_persistent.c @@ -32,25 +32,158 @@ #include "config.h" +#include +#include #include +#include +#include #include +#include #include "cache.h" #include "stevedore.h" +#include "vsha256.h" #include "persistent.h" +struct smp_sc { + unsigned magic; +#define SMP_SC_MAGIC 0x7b73af0a + + int fd; + const char *filename; + off_t mediasize; + unsigned granularity; + + uint8_t *ptr; +}; + +/*--------------------------------------------------------------------*/ + +static void +smp_make_sign(const void *ptr, off_t len, uint8_t *dest) +{ + struct SHA256Context c; + + SHA256_Init(&c); + SHA256_Update(&c, ptr, len); + SHA256_Final(dest, &c); +} + +/*--------------------------------------------------------------------*/ + +static int +smp_check_sign(const void *ptr, off_t len, const void *sign) +{ + struct SHA256Context c; + unsigned char nsign[32]; + + SHA256_Init(&c); + SHA256_Update(&c, ptr, len); + SHA256_Final(nsign, &c); + return(memcmp(sign, nsign, sizeof nsign)); +} + +/*--------------------------------------------------------------------*/ + +static void +smp_newsilo(struct smp_sc *sc) +{ + struct smp_ident *si; + + assert(strlen(SMP_IDENT_STRING) < sizeof si->ident); + si = (void*)sc->ptr; + memset(si, 0, sizeof *si); + strcpy(si->ident, SMP_IDENT_STRING); + si->byte_order = 0x12345678; + si->size = sizeof *si; + si->major_version = 1; + si->minor_version = 1; + si->mediasize = sc->mediasize; + si->granularity = sc->granularity; + + smp_make_sign(si, sizeof *si, sc->ptr + sizeof *si); +} + +/*--------------------------------------------------------------------*/ + +static int +smp_valid_ident(struct smp_sc *sc) +{ + struct smp_ident *si; + + assert(strlen(SMP_IDENT_STRING) < sizeof si->ident); + si = (void*)sc->ptr; + if (strcmp(si->ident, SMP_IDENT_STRING)) + return (1); + if (si->byte_order != 0x12345678) + return (2); + if (si->size != sizeof *si) + return (3); + if (smp_check_sign(si, sizeof *si, sc->ptr + sizeof *si)) + return (4); + if (si->major_version != 1) + return (5); + if (si->minor_version != 1) + return (6); + if (si->mediasize != sc->mediasize) + return (7); + if (si->granularity != sc->granularity) + return (8); + return (0); +} + +/*--------------------------------------------------------------------*/ + static void smp_init(struct stevedore *parent, int ac, char * const *av) { + struct smp_sc *sc; + int i; (void)parent; - (void)ac; - (void)av; + + AZ(av[ac]); assert(sizeof(struct smp_ident) == SMP_IDENT_SIZE); assert(sizeof(struct smp_object) == SMP_OBJECT_SIZE); + + /* Allocate softc */ + ALLOC_OBJ(sc, SMP_SC_MAGIC); + XXXAN(sc); + sc->fd = -1; + + /* Argument processing */ + if (ac != 2) + ARGV_ERR("(-spersistent) wrong number of arguments\n"); + + i = STV_GetFile(av[0], &sc->fd, &sc->filename, "-spersistent"); + if (i == 2) + ARGV_ERR("(-spersistent) need filename (not directory)\n"); + + sc->granularity = getpagesize(); + sc->mediasize = STV_FileSize(sc->fd, av[1], &sc->granularity, + "-spersistent"); + + AZ(ftruncate(sc->fd, sc->mediasize)); + + sc->ptr = mmap(NULL, sc->mediasize, PROT_READ|PROT_WRITE, + MAP_NOCORE | MAP_NOSYNC | MAP_SHARED, sc->fd, 0); + + if (sc->ptr == MAP_FAILED) + ARGV_ERR("(-spersistent) failed to mmap (%s)\n", + strerror(errno)); + + fprintf(stderr, "i = %d ms = %jd g = %u\n", + i, (intmax_t)sc->mediasize, sc->granularity); + + fprintf(stderr, "Silo: %d\n", smp_valid_ident(sc)); + smp_newsilo(sc); + fprintf(stderr, "Silo: %d\n", smp_valid_ident(sc)); + exit (2); } +/*--------------------------------------------------------------------*/ + struct stevedore smp_stevedore = { .magic = STEVEDORE_MAGIC, .name = "persistent", diff --git a/varnish-cache/include/persistent.h b/varnish-cache/include/persistent.h index 3d92ff91..6997a1d9 100644 --- a/varnish-cache/include/persistent.h +++ b/varnish-cache/include/persistent.h @@ -81,6 +81,8 @@ struct smp_ident { #define SMP_IDENT_SIZE (32 + 4 + 4 + 4 + 4 + 8 + 4 ) +#define SMP_IDENT_STRING "Varnish Persistent Storage Silo" + /* * A segment descriptor. */ -- 2.39.5