varnish-cache/bin/varnishd/storage/storage_simple.c
0
/*-
1
 * Copyright (c) 2007-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include "cache/cache_varnishd.h"
34
35 106719
#include "cache/cache_obj.h"
36 86719
#include "cache/cache_objhead.h"
37 20557
38 14245
#include "storage/storage.h"
39 86716
#include "storage/storage_simple.h"
40
41
#include "vtim.h"
42
43
/* Flags for allocating memory in sml_stv_alloc */
44
#define LESS_MEM_ALLOCED_IS_OK  1
45 119265
46 222141
// marker pointer for sml_trimstore
47
static void *trim_once = &trim_once;
48
49
/*-------------------------------------------------------------------*/
50
51
static struct storage *
52 156540
objallocwithnuke(struct worker *, const struct stevedore *, ssize_t size,
53
    int flags);
54
55
static struct storage *
56 124065
sml_stv_alloc(const struct stevedore *stv, ssize_t size, int flags)
57
{
58
        struct storage *st;
59
60 124065
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
61 124065
        AN(stv->sml_alloc);
62
63 124065
        if (!(flags & LESS_MEM_ALLOCED_IS_OK)) {
64 13406
                if (size > cache_param->fetch_maxchunksize)
65 0
                        return (NULL);
66
                else
67 13406
                        return (stv->sml_alloc(stv, size));
68
        }
69
70 110659
        if (size > cache_param->fetch_maxchunksize)
71 0
                size = cache_param->fetch_maxchunksize;
72
73 110659
        assert(size <= UINT_MAX);       /* field limit in struct storage */
74
75 116059
        for (;;) {
76
                /* try to allocate from it */
77 116059
                assert(size > 0);
78 116059
                st = stv->sml_alloc(stv, size);
79 116059
                if (st != NULL)
80 110219
                        break;
81
82 5840
                if (size <= cache_param->fetch_chunksize)
83 440
                        break;
84
85 5400
                size /= 2;
86
        }
87 110659
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
88 110659
        return (st);
89 124065
}
90
91
static void
92 162484
sml_stv_free(const struct stevedore *stv, struct storage *st)
93
{
94
95 162484
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
96 162484
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
97 162484
        if (stv->sml_free != NULL)
98 162482
                stv->sml_free(st);
99 162486
}
100
101
/*--------------------------------------------------------------------
102
 * This function is called by stevedores ->allocobj() method, which
103
 * very often will be SML_allocobj() below, to convert a slab
104
 * of storage into object which the stevedore can then register in its
105
 * internal state, before returning it to STV_NewObject().
106
 * As you probably guessed: All this for persistence.
107
 */
108
109
struct object *
110 111240
SML_MkObject(const struct stevedore *stv, struct objcore *oc, void *ptr)
111
{
112
        struct object *o;
113
114 111240
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
115 111240
        AN(stv->methods);
116 111240
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
117
118 111240
        assert(PAOK(ptr));
119
120 111240
        o = ptr;
121 111240
        INIT_OBJ(o, OBJECT_MAGIC);
122
123 111240
        VTAILQ_INIT(&o->list);
124
125 111240
        oc->stobj->stevedore = stv;
126 111240
        oc->stobj->priv = o;
127 111240
        oc->stobj->priv2 = 0;
128 111240
        return (o);
129
}
130
131
/*--------------------------------------------------------------------
132
 * This is the default ->allocobj() which all stevedores who do not
133
 * implement persistent storage can rely on.
134
 */
135
136
int v_matchproto_(storage_allocobj_f)
137 110797
SML_allocobj(struct worker *wrk, const struct stevedore *stv,
138
    struct objcore *oc, unsigned wsl)
139
{
140
        struct object *o;
141 110797
        struct storage *st = NULL;
142
        unsigned ltot;
143
144 110797
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
145 110797
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
146 110797
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
147
148 110797
        AN(stv->sml_alloc);
149
150 110797
        ltot = sizeof(*o) + PRNDUP(wsl);
151
152 110797
        do {
153 110920
                st = stv->sml_alloc(stv, ltot);
154 110920
                if (st != NULL && st->space < ltot) {
155 0
                        stv->sml_free(st);
156 0
                        st = NULL;
157 0
                }
158 110920
        } while (st == NULL && LRU_NukeOne(wrk, stv->lru));
159 110797
        if (st == NULL)
160 440
                return (0);
161
162 110357
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
163 110357
        o = SML_MkObject(stv, oc, st->ptr);
164 110357
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
165 110357
        st->len = sizeof(*o);
166 110357
        o->objstore = st;
167 110357
        return (1);
168 110797
}
169
170
void * v_matchproto_(storage_allocbuf_t)
171 880
SML_AllocBuf(struct worker *wrk, const struct stevedore *stv, size_t size,
172
    uintptr_t *ppriv)
173
{
174
        struct storage *st;
175
176 880
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
177 880
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
178 880
        AN(ppriv);
179
180 880
        if (size > UINT_MAX)
181 0
                return (NULL);
182 880
        st = objallocwithnuke(wrk, stv, size, 0);
183 880
        if (st == NULL)
184 0
                return (NULL);
185 880
        assert(st->space >= size);
186 880
        st->len = size;
187 880
        *ppriv = (uintptr_t)st;
188 880
        return (st->ptr);
189 880
}
190
191
void v_matchproto_(storage_freebuf_t)
192 880
SML_FreeBuf(struct worker *wrk, const struct stevedore *stv, uintptr_t priv)
193
{
194
        struct storage *st;
195
196 880
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
197 880
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
198
199 880
        CAST_OBJ_NOTNULL(st, (void *)priv, STORAGE_MAGIC);
200 880
        sml_stv_free(stv, st);
201 880
}
202
203
/*---------------------------------------------------------------------
204
 */
205
206
static struct object *
207 6146184
sml_getobj(struct worker *wrk, struct objcore *oc)
208
{
209
        const struct stevedore *stv;
210
        struct object *o;
211
212 6146184
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
213 6146184
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
214 6146184
        stv = oc->stobj->stevedore;
215 6146184
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
216 6146184
        if (stv->sml_getobj != NULL)
217 11600
                return (stv->sml_getobj(wrk, oc));
218 6134584
        if (oc->stobj->priv == NULL)
219 0
                return (NULL);
220 6134584
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
221 6134584
        return (o);
222 6146184
}
223
224
static void v_matchproto_(objslim_f)
225 125460
sml_slim(struct worker *wrk, struct objcore *oc)
226
{
227
        const struct stevedore *stv;
228
        struct object *o;
229
        struct storage *st, *stn;
230
231 125460
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
232
233 125460
        stv = oc->stobj->stevedore;
234 125460
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
235 125460
        o = sml_getobj(wrk, oc);
236 125460
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
237
238
#define OBJ_AUXATTR(U, l)                                       \
239
        do {                                                    \
240
                if (o->aa_##l != NULL) {                        \
241
                        sml_stv_free(stv, o->aa_##l);           \
242
                        o->aa_##l = NULL;                       \
243
                }                                               \
244
        } while (0);
245
#include "tbl/obj_attr.h"
246
247 158214
        VTAILQ_FOREACH_SAFE(st, &o->list, list, stn) {
248 32754
                CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
249 32754
                VTAILQ_REMOVE(&o->list, st, list);
250 32754
                sml_stv_free(stv, st);
251 32754
        }
252
}
253
254
static void
255 111918
sml_bocfini(const struct stevedore *stv, struct boc *boc)
256
{
257
        struct storage *st;
258
259 111918
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
260 111918
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
261
262 111918
        if (boc->stevedore_priv == NULL ||
263 74126
            boc->stevedore_priv == trim_once)
264 103832
                return;
265
266
        /* Free any leftovers from Trim */
267 8086
        TAKE_OBJ_NOTNULL(st, &boc->stevedore_priv, STORAGE_MAGIC);
268 8086
        sml_stv_free(stv, st);
269 111918
}
270
271
/*
272
 * called in two cases:
273
 * - oc->boc == NULL: cache object on LRU freed
274
 * - oc->boc != NULL: cache object replaced for backend error
275
 */
276
static void v_matchproto_(objfree_f)
277 70262
sml_objfree(struct worker *wrk, struct objcore *oc)
278
{
279
        const struct stevedore *stv;
280
        struct storage *st;
281
        struct object *o;
282
283 70262
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
284 70262
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
285 70262
        stv = oc->stobj->stevedore;
286 70262
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
287 70262
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
288
289 70262
        sml_slim(wrk, oc);
290 70262
        st = o->objstore;
291 70262
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
292 70262
        FINI_OBJ(o);
293
294 70262
        if (oc->boc != NULL)
295 840
                sml_bocfini(stv, oc->boc);
296 69422
        else if (stv->lru != NULL)
297 69420
                LRU_Remove(oc);
298
299 70262
        sml_stv_free(stv, st);
300
301 70262
        memset(oc->stobj, 0, sizeof oc->stobj);
302
303 70262
        wrk->stats->n_object--;
304 70262
}
305
306
static int v_matchproto_(objiterator_f)
307 93707
sml_iterator(struct worker *wrk, struct objcore *oc,
308
    void *priv, objiterate_f *func, int final)
309
{
310
        struct boc *boc;
311
        enum boc_state_e state;
312
        struct object *obj;
313
        struct storage *st;
314 93707
        struct storage *checkpoint = NULL;
315
        const struct stevedore *stv;
316 93707
        ssize_t checkpoint_len = 0;
317 93707
        ssize_t len = 0;
318 93707
        int ret = 0, ret2;
319
        ssize_t ol;
320
        ssize_t nl;
321
        ssize_t sl;
322
        void *p;
323
        ssize_t l;
324
        unsigned u;
325
326 93707
        obj = sml_getobj(wrk, oc);
327 93707
        CHECK_OBJ_NOTNULL(obj, OBJECT_MAGIC);
328 93707
        stv = oc->stobj->stevedore;
329 93707
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
330
331 93707
        boc = HSH_RefBoc(oc);
332
333 93707
        if (boc == NULL) {
334 133956
                VTAILQ_FOREACH_REVERSE_SAFE(
335
                    st, &obj->list, storagehead, list, checkpoint) {
336
337 71078
                        u = 0;
338 71078
                        if (VTAILQ_PREV(st, storagehead, list) == NULL)
339 64076
                                u |= OBJ_ITER_END;
340 71078
                        if (final)
341 30161
                                u |= OBJ_ITER_FLUSH;
342 71078
                        if (ret == 0 && st->len > 0)
343 71076
                                ret = func(priv, u, st->ptr, st->len);
344 71078
                        if (final) {
345 30161
                                VTAILQ_REMOVE(&obj->list, st, list);
346 30161
                                sml_stv_free(stv, st);
347 71078
                        } else if (ret)
348 1240
                                break;
349 69838
                }
350 64118
                return (ret);
351
        }
352
353 29589
        p = NULL;
354 29589
        l = 0;
355
356 29589
        u = 0;
357 29589
        if (boc->fetched_so_far == 0) {
358 3366
                ret = func(priv, OBJ_ITER_FLUSH, NULL, 0);
359 3366
                if (ret)
360 0
                        return (ret);
361 3366
        }
362 86348
        while (1) {
363 86348
                ol = len;
364 86348
                nl = ObjWaitExtend(wrk, oc, ol, &state);
365 86348
                if (state == BOS_FAILED) {
366 560
                        ret = -1;
367 560
                        break;
368
                }
369 85788
                if (nl == ol) {
370 28324
                        assert(state == BOS_FINISHED);
371 28324
                        break;
372
                }
373 57464
                assert(nl > ol);
374 57464
                Lck_Lock(&boc->mtx);
375 57464
                AZ(VTAILQ_EMPTY(&obj->list));
376 57464
                if (checkpoint == NULL) {
377 35055
                        st = VTAILQ_LAST(&obj->list, storagehead);
378 35055
                        sl = 0;
379 35055
                } else {
380 22409
                        st = checkpoint;
381 22409
                        sl = checkpoint_len;
382 22409
                        ol -= checkpoint_len;
383
                }
384 79227
                while (st != NULL) {
385 79231
                        if (st->len > ol) {
386 57468
                                p = st->ptr + ol;
387 57468
                                l = st->len - ol;
388 57468
                                len += l;
389 57468
                                break;
390
                        }
391 21763
                        ol -= st->len;
392 21763
                        assert(ol >= 0);
393 21763
                        nl -= st->len;
394 21763
                        assert(nl > 0);
395 21763
                        sl += st->len;
396 21763
                        st = VTAILQ_PREV(st, storagehead, list);
397 21763
                        if (final && checkpoint != NULL) {
398 19864
                                if (checkpoint == boc->stevedore_priv)
399 0
                                        boc->stevedore_priv = trim_once;
400
                                else
401 19864
                                        VTAILQ_REMOVE(&obj->list, checkpoint, list);
402 19864
                                sml_stv_free(stv, checkpoint);
403 19864
                        }
404 21763
                        checkpoint = st;
405 21763
                        checkpoint_len = sl;
406
                }
407 57474
                CHECK_OBJ_NOTNULL(obj, OBJECT_MAGIC);
408 57474
                CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
409 57474
                st = VTAILQ_PREV(st, storagehead, list);
410 57474
                if (st != NULL && st->len == 0)
411 10532
                        st = NULL;
412 57474
                Lck_Unlock(&boc->mtx);
413 57474
                assert(l > 0 || state == BOS_FINISHED);
414 57474
                u = 0;
415 57474
                if (st == NULL || final)
416 56782
                        u |= OBJ_ITER_FLUSH;
417 57474
                if (st == NULL && state == BOS_FINISHED)
418 310
                        u |= OBJ_ITER_END;
419 57474
                ret = func(priv, u, p, l);
420 57474
                if (ret)
421 715
                        break;
422
        }
423 29599
        HSH_DerefBoc(wrk, oc);
424 29599
        if ((u & OBJ_ITER_END) == 0) {
425 29287
                ret2 = func(priv, OBJ_ITER_END, NULL, 0);
426 29287
                if (ret == 0)
427 28012
                        ret = ret2;
428 29287
        }
429 29599
        return (ret);
430 93717
}
431
432
/*--------------------------------------------------------------------
433
 */
434
435
static struct storage *
436 115979
objallocwithnuke(struct worker *wrk, const struct stevedore *stv, ssize_t size,
437
    int flags)
438
{
439 115979
        struct storage *st = NULL;
440
441 115979
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
442 115979
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
443
444 115979
        if (size > cache_param->fetch_maxchunksize) {
445 80
                if (!(flags & LESS_MEM_ALLOCED_IS_OK))
446 0
                        return (NULL);
447 80
                size = cache_param->fetch_maxchunksize;
448 80
        }
449
450 115979
        assert(size <= UINT_MAX);       /* field limit in struct storage */
451
452 115979
        do {
453
                /* try to allocate from it */
454 116299
                st = sml_stv_alloc(stv, size, flags);
455 116299
                if (st != NULL)
456 115779
                        break;
457
458
                /* no luck; try to free some space and keep trying */
459 520
                if (stv->lru == NULL)
460 0
                        break;
461 520
        } while (LRU_NukeOne(wrk, stv->lru));
462
463 115979
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
464 115979
        return (st);
465 115979
}
466
467
static int v_matchproto_(objgetspace_f)
468 2321128
sml_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz,
469
    uint8_t **ptr)
470
{
471
        struct object *o;
472
        struct storage *st;
473
474 2321128
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
475 2321128
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
476 2321128
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
477 2321128
        AN(sz);
478 2321128
        AN(ptr);
479 2321128
        if (*sz == 0)
480 2176920
                *sz = cache_param->fetch_chunksize;
481 2321128
        assert(*sz > 0);
482 2321128
        if (oc->boc->transit_buffer > 0)
483 19259
                *sz = vmin_t(ssize_t, *sz, oc->boc->transit_buffer);
484
485 2321128
        o = sml_getobj(wrk, oc);
486 2321128
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
487 2321128
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
488
489 2321128
        st = VTAILQ_FIRST(&o->list);
490 2321128
        if (st != NULL && st->len < st->space) {
491 2210711
                *sz = st->space - st->len;
492 2210711
                *ptr = st->ptr + st->len;
493 2210711
                assert (*sz > 0);
494 2210711
                return (1);
495
        }
496
497 110417
        st = objallocwithnuke(wrk, oc->stobj->stevedore, *sz,
498
            LESS_MEM_ALLOCED_IS_OK);
499 110417
        if (st == NULL)
500 200
                return (0);
501
502 110217
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
503 110217
        Lck_Lock(&oc->boc->mtx);
504 110217
        VTAILQ_INSERT_HEAD(&o->list, st, list);
505 110217
        Lck_Unlock(&oc->boc->mtx);
506
507 110217
        *sz = st->space - st->len;
508 110217
        assert (*sz > 0);
509 110217
        *ptr = st->ptr + st->len;
510 110217
        return (1);
511 2321128
}
512
513
static void v_matchproto_(objextend_f)
514 2262924
sml_extend(struct worker *wrk, struct objcore *oc, ssize_t l)
515
{
516
        struct object *o;
517
        struct storage *st;
518
519 2262924
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
520 2262924
        assert(l > 0);
521
522 2262924
        o = sml_getobj(wrk, oc);
523 2262924
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
524 2262924
        st = VTAILQ_FIRST(&o->list);
525 2262924
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
526 2262924
        assert(st->len + l <= st->space);
527 2262924
        st->len += l;
528 2262924
}
529
530
static void v_matchproto_(objtrimstore_f)
531 74124
sml_trimstore(struct worker *wrk, struct objcore *oc)
532
{
533
        const struct stevedore *stv;
534
        struct storage *st, *st1;
535
        struct object *o;
536
537 74124
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
538 74124
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
539 74124
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
540
541 74124
        stv = oc->stobj->stevedore;
542 74124
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
543
544 74124
        if (oc->boc->stevedore_priv != NULL)
545 0
                WRONG("sml_trimstore already called");
546 74124
        oc->boc->stevedore_priv = trim_once;
547
548 74124
        if (stv->sml_free == NULL)
549 0
                return;
550
551 74124
        o = sml_getobj(wrk, oc);
552 74124
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
553 74124
        st = VTAILQ_FIRST(&o->list);
554
555 74124
        if (st == NULL)
556 0
                return;
557
558 74124
        if (st->len == 0) {
559 320
                Lck_Lock(&oc->boc->mtx);
560 320
                VTAILQ_REMOVE(&o->list, st, list);
561 320
                Lck_Unlock(&oc->boc->mtx);
562
                /* sml_bocdone frees this */
563 320
                oc->boc->stevedore_priv = st;
564 320
                return;
565
        }
566
567 73804
        if (st->space - st->len < 512)
568 66039
                return;
569
570 7765
        st1 = sml_stv_alloc(stv, st->len, 0);
571 7765
        if (st1 == NULL)
572 0
                return;
573 7765
        assert(st1->space >= st->len);
574
575 7765
        memcpy(st1->ptr, st->ptr, st->len);
576 7765
        st1->len = st->len;
577 7765
        Lck_Lock(&oc->boc->mtx);
578 7765
        VTAILQ_REMOVE(&o->list, st, list);
579 7765
        VTAILQ_INSERT_HEAD(&o->list, st1, list);
580 7765
        Lck_Unlock(&oc->boc->mtx);
581
        /* sml_bocdone frees this */
582 7765
        oc->boc->stevedore_priv = st;
583 74124
}
584
585
static void v_matchproto_(objbocdone_f)
586 111079
sml_bocdone(struct worker *wrk, struct objcore *oc, struct boc *boc)
587
{
588
        const struct stevedore *stv;
589
590 111079
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
591 111079
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
592 111079
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
593 111079
        stv = oc->stobj->stevedore;
594 111079
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
595
596 111079
        sml_bocfini(stv, boc);
597
598 111079
        if (stv->lru != NULL) {
599 109517
                if (isnan(wrk->lastused))
600 0
                        wrk->lastused = VTIM_real();
601 109517
                LRU_Add(oc, wrk->lastused);     // approx timestamp is OK
602 109517
        }
603 111079
}
604
605
static const void * v_matchproto_(objgetattr_f)
606 855504
sml_getattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
607
   ssize_t *len)
608
{
609
        struct object *o;
610
        ssize_t dummy;
611
612 855504
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
613
614 855504
        if (len == NULL)
615 526095
                len = &dummy;
616 855504
        o = sml_getobj(wrk, oc);
617 855504
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
618
619 855504
        switch (attr) {
620
                /* Fixed size attributes */
621
#define OBJ_FIXATTR(U, l, s)                                            \
622
        case OA_##U:                                                    \
623
                *len = sizeof o->fa_##l;                                \
624
                return (o->fa_##l);
625
#include "tbl/obj_attr.h"
626
627
                /* Variable size attributes */
628
#define OBJ_VARATTR(U, l)                                               \
629
        case OA_##U:                                                    \
630
                if (o->va_##l == NULL)                                  \
631
                        return (NULL);                                  \
632
                *len = o->va_##l##_len;                                 \
633
                return (o->va_##l);
634
#include "tbl/obj_attr.h"
635
636
                /* Auxiliary attributes */
637
#define OBJ_AUXATTR(U, l)                                               \
638
        case OA_##U:                                                    \
639
                if (o->aa_##l == NULL)                                  \
640
                        return (NULL);                                  \
641
                CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);            \
642
                *len = o->aa_##l->len;                                  \
643
                return (o->aa_##l->ptr);
644
#include "tbl/obj_attr.h"
645
646
        default:
647
                break;
648
        }
649 0
        WRONG("Unsupported OBJ_ATTR");
650 855504
}
651
652
static void * v_matchproto_(objsetattr_f)
653 414515
sml_setattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
654
    ssize_t len, const void *ptr)
655
{
656
        struct object *o;
657 414515
        void *retval = NULL;
658
        struct storage *st;
659
660 414515
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
661
662 414515
        o = sml_getobj(wrk, oc);
663 414515
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
664 414515
        st = o->objstore;
665
666 414515
        switch (attr) {
667
                /* Fixed size attributes */
668
#define OBJ_FIXATTR(U, l, s)                                            \
669
        case OA_##U:                                                    \
670
                assert(len == sizeof o->fa_##l);                        \
671
                retval = o->fa_##l;                                     \
672
                break;
673
#include "tbl/obj_attr.h"
674
675
                /* Variable size attributes */
676
#define OBJ_VARATTR(U, l)                                               \
677
        case OA_##U:                                                    \
678
                if (o->va_##l##_len > 0) {                              \
679
                        AN(o->va_##l);                                  \
680
                        assert(len == o->va_##l##_len);                 \
681
                        retval = o->va_##l;                             \
682
                } else if (len > 0) {                                   \
683
                        assert(len <= UINT_MAX);                        \
684
                        assert(st->len + len <= st->space);             \
685
                        o->va_##l = st->ptr + st->len;                  \
686
                        st->len += len;                                 \
687
                        o->va_##l##_len = len;                          \
688
                        retval = o->va_##l;                             \
689
                }                                                       \
690
                break;
691
#include "tbl/obj_attr.h"
692
693
                /* Auxiliary attributes */
694
#define OBJ_AUXATTR(U, l)                                               \
695
        case OA_##U:                                                    \
696
                if (o->aa_##l != NULL) {                                \
697
                        CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);    \
698
                        assert(len == o->aa_##l->len);                  \
699
                        retval = o->aa_##l->ptr;                        \
700
                        break;                                          \
701
                }                                                       \
702
                if (len == 0)                                           \
703
                        break;                                          \
704
                o->aa_##l = objallocwithnuke(wrk, oc->stobj->stevedore, \
705
                    len, 0);                                            \
706
                if (o->aa_##l == NULL)                                  \
707
                        break;                                          \
708
                CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);            \
709
                assert(len <= o->aa_##l->space);                        \
710
                o->aa_##l->len = len;                                   \
711
                retval = o->aa_##l->ptr;                                \
712
                break;
713
#include "tbl/obj_attr.h"
714
715
        default:
716 0
                WRONG("Unsupported OBJ_ATTR");
717
                break;
718
        }
719
720 414515
        if (retval != NULL && ptr != NULL)
721 14760
                memcpy(retval, ptr, len);
722 414517
        return (retval);
723
}
724
725
const struct obj_methods SML_methods = {
726
        .objfree        = sml_objfree,
727
        .objiterator    = sml_iterator,
728
        .objgetspace    = sml_getspace,
729
        .objextend      = sml_extend,
730
        .objtrimstore   = sml_trimstore,
731
        .objbocdone     = sml_bocdone,
732
        .objslim        = sml_slim,
733
        .objgetattr     = sml_getattr,
734
        .objsetattr     = sml_setattr,
735
        .objtouch       = LRU_Touch,
736
};
737
738
static void
739 120
sml_panic_st(struct vsb *vsb, const char *hd, const struct storage *st)
740
{
741 240
        VSB_printf(vsb, "%s = %p {priv=%p, ptr=%p, len=%u, space=%u},\n",
742 120
            hd, st, st->priv, st->ptr, st->len, st->space);
743 120
}
744
745
void
746 80
SML_panic(struct vsb *vsb, const struct objcore *oc)
747
{
748
        struct object *o;
749
        struct storage *st;
750
751 80
        VSB_printf(vsb, "Simple = %p,\n", oc->stobj->priv);
752 80
        if (oc->stobj->priv == NULL)
753 0
                return;
754 80
        o = oc->stobj->priv;
755 80
        PAN_CheckMagic(vsb, o, OBJECT_MAGIC);
756 80
        sml_panic_st(vsb, "Obj", o->objstore);
757
758
#define OBJ_FIXATTR(U, l, sz) \
759
        VSB_printf(vsb, "%s = ", #U); \
760
        VSB_quote(vsb, (const void*)o->fa_##l, sz, VSB_QUOTE_HEX); \
761
        VSB_printf(vsb, ",\n");
762
763
#define OBJ_VARATTR(U, l) \
764
        VSB_printf(vsb, "%s = {len=%u, ptr=%p},\n", \
765
            #U, o->va_##l##_len, o->va_##l);
766
767
#define OBJ_AUXATTR(U, l)                                               \
768
        do {                                                            \
769
                if (o->aa_##l != NULL) sml_panic_st(vsb, #U, o->aa_##l);\
770
        } while(0);
771
772
#include "tbl/obj_attr.h"
773
774 120
        VTAILQ_FOREACH(st, &o->list, list) {
775 40
                sml_panic_st(vsb, "Body", st);
776 40
        }
777
}