source: bin/varnishd/storage/storage_persistent.h @ 219aa0

Revision 219aa0, 6.1 KB checked in by Poul-Henning Kamp <phk@…>, 2 years ago (diff)

Eliminate struct sess from HSH_Insert()

  • Property mode set to 100644
Line 
1/*-
2 * Copyright (c) 2008-2011 Varnish Software AS
3 * All rights reserved.
4 *
5 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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 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
26 * SUCH DAMAGE.
27 *
28 * Persistent storage method
29 *
30 * XXX: Before we start the client or maybe after it stops, we should give the
31 * XXX: stevedores a chance to examine their storage for consistency.
32 *
33 * XXX: Do we ever free the LRU-lists ?
34 */
35
36#define ASSERT_SILO_THREAD(sc) \
37    do {assert(pthread_self() == (sc)->thread);} while (0)
38
39#define OC_F_NEEDFIXUP OC_F_PRIV
40
41/*
42 * Context for a signature.
43 *
44 * A signature is a sequence of bytes in the silo, signed by a SHA256 hash
45 * which follows the bytes.
46 *
47 * The context structure allows us to append to a signature without
48 * recalculating the entire SHA256 hash.
49 */
50
51struct smp_signctx {
52        struct smp_sign         *ss;
53        struct SHA256Context    ctx;
54        uint32_t                unique;
55        const char              *id;
56};
57
58struct smp_sc;
59
60/* XXX: name confusion with on-media version ? */
61struct smp_seg {
62        unsigned                magic;
63#define SMP_SEG_MAGIC           0x45c61895
64
65        struct smp_sc           *sc;
66        struct lru              *lru;
67
68        VTAILQ_ENTRY(smp_seg)   list;           /* on smp_sc.smp_segments */
69
70        struct smp_segptr       p;
71
72        unsigned                flags;
73#define SMP_SEG_MUSTLOAD        (1 << 0)
74#define SMP_SEG_LOADED          (1 << 1)
75
76        uint32_t                nobj;           /* Number of objects */
77        uint32_t                nalloc;         /* Allocations */
78        uint32_t                nfixed;         /* How many fixed objects */
79
80        /* Only for open segment */
81        struct smp_object       *objs;          /* objdesc array */
82        struct smp_signctx      ctx[1];
83};
84
85VTAILQ_HEAD(smp_seghead, smp_seg);
86
87struct smp_sc {
88        unsigned                magic;
89#define SMP_SC_MAGIC            0x7b73af0a
90        struct stevedore        *parent;
91
92        unsigned                flags;
93#define SMP_SC_LOADED           (1 << 0)
94
95        const struct stevedore  *stevedore;
96        int                     fd;
97        const char              *filename;
98        off_t                   mediasize;
99        uintptr_t               align;
100        uint32_t                granularity;
101        uint32_t                unique;
102
103        uint8_t                 *base;
104
105        struct smp_ident        *ident;
106
107        struct smp_seghead      segments;
108        struct smp_seg          *cur_seg;
109        uint64_t                next_bot;       /* next alloc address bottom */
110        uint64_t                next_top;       /* next alloc address top */
111
112        uint64_t                free_offset;
113
114        pthread_t               thread;
115
116        VTAILQ_ENTRY(smp_sc)    list;
117
118        struct smp_signctx      idn;
119        struct smp_signctx      ban1;
120        struct smp_signctx      ban2;
121        struct smp_signctx      seg1;
122        struct smp_signctx      seg2;
123
124        struct ban              *tailban;
125
126        struct lock             mtx;
127
128        /* Cleaner metrics */
129
130        unsigned                min_nseg;
131        unsigned                aim_nseg;
132        unsigned                max_nseg;
133
134        uint64_t                min_segl;
135        uint64_t                aim_segl;
136        uint64_t                max_segl;
137
138        uint64_t                free_reserve;
139};
140
141/*--------------------------------------------------------------------*/
142
143/* Pointer round up/down & assert */
144#define PRNDN(sc, x)    ((void*)RDN2((uintptr_t)(x), sc->align))
145#define PRNUP(sc, x)    ((void*)RUP2((uintptr_t)(x), sc->align))
146#define PASSERTALIGN(sc, x)     assert(PRNDN(sc, x) == (x))
147
148/* Integer round up/down & assert */
149#define IRNDN(sc, x)    RDN2(x, sc->align)
150#define IRNUP(sc, x)    RUP2(x, sc->align)
151#define IASSERTALIGN(sc, x)     assert(IRNDN(sc, x) == (x))
152
153/*--------------------------------------------------------------------*/
154
155#define ASSERT_PTR_IN_SILO(sc, ptr) \
156        assert((const void*)(ptr) >= (const void*)((sc)->base) && \
157            (const void*)(ptr) < (const void *)((sc)->base + (sc)->mediasize))
158
159/*--------------------------------------------------------------------*/
160
161#define SIGN_DATA(ctx)  ((void *)((ctx)->ss + 1))
162#define SIGN_END(ctx)   ((void *)((int8_t *)SIGN_DATA(ctx) + (ctx)->ss->length))
163
164/* storage_persistent_mgt.c */
165
166void smp_mgt_init(struct stevedore *parent, int ac, char * const *av);
167
168/* storage_persistent_silo.c */
169
170void smp_load_seg(struct worker *, const struct smp_sc *sc, struct smp_seg *sg);
171void smp_new_seg(struct smp_sc *sc);
172void smp_close_seg(struct smp_sc *sc, struct smp_seg *sg);
173void smp_init_oc(struct objcore *oc, struct smp_seg *sg, unsigned objidx);
174void smp_save_segs(struct smp_sc *sc);
175
176/* storage_persistent_subr.c */
177
178void smp_def_sign(const struct smp_sc *sc, struct smp_signctx *ctx,
179    uint64_t off, const char *id);
180int smp_chk_sign(struct smp_signctx *ctx);
181void smp_append_sign(struct smp_signctx *ctx, const void *ptr, uint32_t len);
182void smp_reset_sign(struct smp_signctx *ctx);
183void smp_sync_sign(const struct smp_signctx *ctx);
184void smp_newsilo(struct smp_sc *sc);
185int smp_valid_silo(struct smp_sc *sc);
186
187/*--------------------------------------------------------------------
188 * Caculate payload of some stuff
189 */
190
191static inline uint64_t
192smp_stuff_len(const struct smp_sc *sc, unsigned stuff)
193{
194        uint64_t l;
195
196        assert(stuff < SMP_END_STUFF);
197        l = sc->ident->stuff[stuff + 1] - sc->ident->stuff[stuff];
198        l -= SMP_SIGN_SPACE;
199        return (l);
200}
201
202static inline uint64_t
203smp_segend(const struct smp_seg *sg)
204{
205
206        return (sg->p.offset + sg->p.length);
207}
208
209static inline uint64_t
210smp_spaceleft(const struct smp_sc *sc, const struct smp_seg *sg)
211{
212
213        IASSERTALIGN(sc, sc->next_bot);
214        assert(sc->next_bot <= sc->next_top - IRNUP(sc, SMP_SIGN_SPACE));
215        assert(sc->next_bot >= sg->p.offset);
216        assert(sc->next_top < sg->p.offset + sg->p.length);
217        return ((sc->next_top - sc->next_bot) - IRNUP(sc, SMP_SIGN_SPACE));
218}
Note: See TracBrowser for help on using the repository browser.