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 <stdlib.h>
34
35 2724
#include "cache/cache_varnishd.h"
36 2218
37 515
#include "cache/cache_obj.h"
38 357
#include "cache/cache_objhead.h"
39 2218
40
#include "storage/storage.h"
41
#include "storage/storage_simple.h"
42
43
#include "vtim.h"
44
45 3045
/* Flags for allocating memory in sml_stv_alloc */
46 5769
#define LESS_MEM_ALLOCED_IS_OK  1
47
48
// marker pointer for sml_trimstore
49
static void *trim_once = &trim_once;
50
// for delayed return of hdl->last resume pointer
51
static void *null_iov = &null_iov;
52 3976
53
/*-------------------------------------------------------------------*/
54
55
static struct storage *
56
objallocwithnuke(struct worker *, const struct stevedore *, ssize_t size,
57
    int flags);
58
59
static struct storage *
60 3449
sml_stv_alloc(const struct stevedore *stv, ssize_t size, int flags)
61
{
62
        struct storage *st;
63
64 3449
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
65 3449
        AN(stv->sml_alloc);
66
67 3449
        if (!(flags & LESS_MEM_ALLOCED_IS_OK)) {
68 625
                if (size > cache_param->fetch_maxchunksize)
69 0
                        return (NULL);
70
                else
71 625
                        return (stv->sml_alloc(stv, size));
72
        }
73
74 2824
        if (size > cache_param->fetch_maxchunksize)
75 0
                size = cache_param->fetch_maxchunksize;
76
77 2824
        assert(size <= UINT_MAX);       /* field limit in struct storage */
78
79 2959
        for (;;) {
80
                /* try to allocate from it */
81 2959
                assert(size > 0);
82 2959
                st = stv->sml_alloc(stv, size);
83 2959
                if (st != NULL)
84 2813
                        break;
85
86 146
                if (size <= cache_param->fetch_chunksize)
87 11
                        break;
88
89 135
                size /= 2;
90
        }
91 2824
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
92 2824
        return (st);
93 3449
}
94
95
static void
96 4434
sml_stv_free(const struct stevedore *stv, struct storage *st)
97
{
98
99 4434
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
100 4434
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
101 4434
        if (stv->sml_free != NULL)
102 4434
                stv->sml_free(st);
103 4434
}
104
105
/*--------------------------------------------------------------------
106
 * This function is called by stevedores ->allocobj() method, which
107
 * very often will be SML_allocobj() below, to convert a slab
108
 * of storage into object which the stevedore can then register in its
109
 * internal state, before returning it to STV_NewObject().
110
 * As you probably guessed: All this for persistence.
111
 */
112
113
struct object *
114 2845
SML_MkObject(const struct stevedore *stv, struct objcore *oc, void *ptr)
115
{
116
        struct object *o;
117
118 2845
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
119 2845
        AN(stv->methods);
120 2845
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
121
122 2845
        assert(PAOK(ptr));
123
124 2845
        o = ptr;
125 2845
        INIT_OBJ(o, OBJECT_MAGIC);
126
127 2845
        VTAILQ_INIT(&o->list);
128
129 2845
        oc->stobj->stevedore = stv;
130 2845
        oc->stobj->priv = o;
131 2845
        oc->stobj->priv2 = 0;
132 2845
        return (o);
133
}
134
135
/*--------------------------------------------------------------------
136
 * This is the default ->allocobj() which all stevedores who do not
137
 * implement persistent storage can rely on.
138
 */
139
140
int v_matchproto_(storage_allocobj_f)
141 2834
SML_allocobj(struct worker *wrk, const struct stevedore *stv,
142
    struct objcore *oc, unsigned wsl)
143
{
144
        struct object *o;
145 2834
        struct storage *st = NULL;
146
        unsigned ltot;
147
148 2834
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
149 2834
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
150 2834
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
151
152 2834
        AN(stv->sml_alloc);
153
154 2834
        ltot = sizeof(*o) + PRNDUP(wsl);
155
156 2834
        do {
157 2837
                st = stv->sml_alloc(stv, ltot);
158 2837
                if (st != NULL && st->space < ltot) {
159 0
                        stv->sml_free(st);
160 0
                        st = NULL;
161 0
                }
162 2837
        } while (st == NULL && LRU_NukeOne(wrk, stv->lru));
163 2834
        if (st == NULL)
164 11
                return (0);
165
166 2823
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
167 2823
        o = SML_MkObject(stv, oc, st->ptr);
168 2823
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
169 2823
        st->len = sizeof(*o);
170 2823
        o->objstore = st;
171 2823
        return (1);
172 2834
}
173
174
void * v_matchproto_(storage_allocbuf_t)
175 294
SML_AllocBuf(struct worker *wrk, const struct stevedore *stv, size_t size,
176
    uintptr_t *ppriv)
177
{
178
        struct storage *st;
179
180 294
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
181 294
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
182 294
        AN(ppriv);
183
184 294
        if (size > UINT_MAX)
185 0
                return (NULL);
186 294
        st = objallocwithnuke(wrk, stv, size, 0);
187 294
        if (st == NULL)
188 0
                return (NULL);
189 294
        assert(st->space >= size);
190 294
        st->flags = STORAGE_F_BUFFER;
191 294
        st->len = size;
192 294
        *ppriv = (uintptr_t)st;
193 294
        return (st->ptr);
194 294
}
195
196
void v_matchproto_(storage_freebuf_t)
197 294
SML_FreeBuf(struct worker *wrk, const struct stevedore *stv, uintptr_t priv)
198
{
199
        struct storage *st;
200
201 294
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
202 294
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
203
204 294
        CAST_OBJ_NOTNULL(st, (void *)priv, STORAGE_MAGIC);
205 294
        assert(st->flags == STORAGE_F_BUFFER);
206 294
        sml_stv_free(stv, st);
207 294
}
208
209
/*---------------------------------------------------------------------
210
 */
211
212
static struct object *
213 155087
sml_getobj(struct worker *wrk, struct objcore *oc)
214
{
215
        const struct stevedore *stv;
216
        struct object *o;
217
218 155087
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
219 155087
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
220 155087
        stv = oc->stobj->stevedore;
221 155087
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
222 155087
        if (stv->sml_getobj != NULL)
223 293
                return (stv->sml_getobj(wrk, oc));
224 154794
        if (oc->stobj->priv == NULL)
225 0
                return (NULL);
226 154794
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
227 154794
        return (o);
228 155087
}
229
230
static void v_matchproto_(objslim_f)
231 3199
sml_slim(struct worker *wrk, struct objcore *oc)
232
{
233
        const struct stevedore *stv;
234
        struct object *o;
235
        struct storage *st, *stn;
236
237 3199
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
238
239 3199
        stv = oc->stobj->stevedore;
240 3199
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
241 3199
        o = sml_getobj(wrk, oc);
242 3199
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
243
244
#define OBJ_AUXATTR(U, l)                                       \
245
        do {                                                    \
246
                if (o->aa_##l != NULL) {                        \
247
                        sml_stv_free(stv, o->aa_##l);           \
248
                        o->aa_##l = NULL;                       \
249
                }                                               \
250
        } while (0);
251
#include "tbl/obj_attr.h"
252
253 4796
        VTAILQ_FOREACH_SAFE(st, &o->list, list, stn) {
254 1597
                CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
255 1597
                VTAILQ_REMOVE(&o->list, st, list);
256 1597
                sml_stv_free(stv, st);
257 1597
        }
258
}
259
260
static void
261 2861
sml_bocfini(const struct stevedore *stv, struct boc *boc)
262
{
263
        struct storage *st;
264
265 2861
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
266 2861
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
267
268 2861
        if (boc->stevedore_priv == NULL ||
269 1885
            boc->stevedore_priv == trim_once)
270 2657
                return;
271
272
        /* Free any leftovers from Trim */
273 204
        TAKE_OBJ_NOTNULL(st, &boc->stevedore_priv, STORAGE_MAGIC);
274 204
        sml_stv_free(stv, st);
275 2861
}
276
277
/*
278
 * called in two cases:
279
 * - oc->boc == NULL: cache object on LRU freed
280
 * - oc->boc != NULL: cache object replaced for backend error
281
 */
282
static void v_matchproto_(objfree_f)
283 1792
sml_objfree(struct worker *wrk, struct objcore *oc)
284
{
285
        const struct stevedore *stv;
286
        struct storage *st;
287
        struct object *o;
288
289 1792
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
290 1792
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
291 1792
        stv = oc->stobj->stevedore;
292 1792
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
293 1792
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
294
295 1792
        sml_slim(wrk, oc);
296 1792
        st = o->objstore;
297 1792
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
298 1792
        FINI_OBJ(o);
299
300 1792
        if (oc->boc != NULL)
301 21
                sml_bocfini(stv, oc->boc);
302 1771
        else if (stv->lru != NULL)
303 1771
                LRU_Remove(oc);
304
305 1792
        sml_stv_free(stv, st);
306
307 1792
        memset(oc->stobj, 0, sizeof oc->stobj);
308
309 1792
        wrk->stats->n_object--;
310 1792
}
311
312
// kept for reviewers - XXX remove later
313
#undef VAI_DBG
314
315
struct sml_hdl {
316
        struct vai_hdl_preamble preamble;
317
#define SML_HDL_MAGIC           0x37dfd996
318
        struct vai_qe           qe;
319
        struct pool_task        task;   // unfortunate
320
        struct ws               *ws;    // NULL is malloc()
321
        struct objcore          *oc;
322
        struct object           *obj;
323
        const struct stevedore  *stv;
324
        struct boc              *boc;
325
326
        struct storage          *st;    // updated by _lease()
327
328
        // only for _lease_boc()
329
        uint64_t                st_off; // already returned fragment of current st
330
        uint64_t                avail, returned;
331
        struct storage          *last;  // to resume, held back by _return()
332
};
333
334
static inline void
335 1881
sml_ai_viov_fill(struct viov *viov, struct storage *st)
336
{
337 1881
        viov->iov.iov_base = TRUST_ME(st->ptr);
338 1881
        viov->iov.iov_len = st->len;
339 1881
        viov->lease = ptr2lease(st);
340 1881
        VAI_ASSERT_LEASE(viov->lease);
341 1881
}
342
343
// sml has no mechanism to notify "I got free space again now"
344
// (we could add that, but because storage.h is used in mgt, a first attempt
345
//  looks at least like this would cause some include spill for vai_q_head or
346
//  something similar)
347
//
348
// So anyway, to get ahead we just implement a pretty stupid "call the notify
349
// some time later" on a thread
350
static void
351 0
sml_ai_later_task(struct worker *wrk, void *priv)
352
{
353
        struct sml_hdl *hdl;
354 0
        const vtim_dur dur = 0.0042;
355
356 0
        (void)wrk;
357 0
        VTIM_sleep(dur);
358 0
        CAST_VAI_HDL_NOTNULL(hdl, priv, SML_HDL_MAGIC);
359 0
        memset(&hdl->task, 0, sizeof hdl->task);
360 0
        hdl->qe.cb(hdl, hdl->qe.priv);
361 0
}
362
static void
363 0
sml_ai_later(struct worker *wrk, struct sml_hdl *hdl)
364
{
365 0
        AZ(hdl->task.func);
366 0
        AZ(hdl->task.priv);
367 0
        hdl->task.func = sml_ai_later_task;
368 0
        hdl->task.priv = hdl;
369 0
        AZ(Pool_Task(wrk->pool, &hdl->task, TASK_QUEUE_BG));
370 0
}
371
372
373
static int
374 16
sml_ai_buffer(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
375
{
376
        const struct stevedore *stv;
377
        struct sml_hdl *hdl;
378
        struct storage *st;
379
        struct viov *vio;
380 16
        int r = 0;
381
382 16
        (void) wrk;
383 16
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
384 16
        stv = hdl->stv;
385 16
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
386
387 32
        VSCARAB_FOREACH(vio, scarab)
388 16
                if (vio->iov.iov_len > UINT_MAX)
389 0
                        return (-EINVAL);
390
391 32
        VSCARAB_FOREACH(vio, scarab) {
392 16
                st = objallocwithnuke(wrk, stv, vio->iov.iov_len, 0);
393 16
                if (st == NULL)
394 0
                        break;
395 16
                assert(st->space >= vio->iov.iov_len);
396 16
                st->flags = STORAGE_F_BUFFER;
397 16
                st->len = st->space;
398
399 16
                sml_ai_viov_fill(vio, st);
400 16
                r++;
401 16
        }
402 16
        if (r == 0) {
403 0
                sml_ai_later(wrk, hdl);
404 0
                r = -EAGAIN;
405 0
        }
406 16
        return (r);
407 16
}
408
409
static int
410 3334
sml_ai_lease_simple(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
411
{
412
        struct storage *st;
413
        struct sml_hdl *hdl;
414
        struct viov *viov;
415 3334
        int r = 0;
416
417 3334
        (void) wrk;
418 3334
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
419 3334
        VSCARAB_CHECK_NOTNULL(scarab);
420
421 3334
        AZ(hdl->st_off);
422 3334
        st = hdl->st;
423 5199
        while (st != NULL && (viov = VSCARAB_GET(scarab)) != NULL) {
424 1865
                CHECK_OBJ(st, STORAGE_MAGIC);
425 1865
                sml_ai_viov_fill(viov, st);
426 1865
                r++;
427 1865
                st = VTAILQ_PREV(st, storagehead, list);
428
        }
429 3334
        hdl->st = st;
430 3334
        if (st == NULL)
431 3318
                scarab->flags |= VSCARAB_F_END;
432 3334
        return (r);
433
}
434
435
/*
436
 * on leases while streaming (with a boc):
437
 *
438
 * SML uses the lease return facility to implement the "free behind" for
439
 * OC_F_TRANSIENT objects. When streaming, we also return leases on
440
 * fragments of sts, but we must only "free behind" when we are done with the
441
 * last fragment.
442
 *
443
 * So we use a magic lease to signal "this is only a fragment", which we ignore
444
 * on returns
445
 */
446
447
static int
448 3302
sml_ai_lease_boc(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
449
{
450 3302
        enum boc_state_e state = BOS_INVALID;
451
        struct storage *next;
452
        struct sml_hdl *hdl;
453
        struct viov *viov;
454 3302
        int r = 0;
455
456 3302
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
457 3302
        VSCARAB_CHECK_NOTNULL(scarab);
458 3302
        assert(hdl->boc == hdl->oc->boc);
459
460 3302
        if (hdl->avail == hdl->returned) {
461 6568
                hdl->avail = ObjVAIGetExtend(wrk, hdl->oc, hdl->returned,
462 3284
                    &state, &hdl->qe);
463 3284
                assert(state >= BOS_STREAM);
464 3284
                if (state == BOS_FAILED) {
465 15
                        hdl->last = NULL;
466 15
                        return (-EPIPE);
467
                }
468 3269
                else if (state == BOS_FINISHED)
469 735
                        (void)0;
470 2534
                else if (hdl->avail == hdl->returned) {
471
                        // ObjVAIGetExtend() has scheduled a notification
472 1302
                        if (hdl->boc->transit_buffer > 0)
473 293
                                return (-ENOBUFS);
474
                        else
475 1009
                                return (-EAGAIN);
476
                }
477
                else
478 1232
                        assert(state < BOS_FINISHED);
479 1967
        }
480 1985
        Lck_Lock(&hdl->boc->mtx);
481 1985
        if (hdl->st == NULL && hdl->last != NULL)
482 853
                hdl->st = VTAILQ_PREV(hdl->last, storagehead, list);
483 1985
        if (hdl->last != NULL && state < BOS_FINISHED) {
484 165
                viov = VSCARAB_GET(scarab);
485 165
                AN(viov);
486 165
                viov->iov.iov_base = null_iov;
487 165
                viov->iov.iov_len = 0;
488 165
                viov->lease = ptr2lease(hdl->last);
489 165
                r++;
490 165
        }
491 1985
        if (hdl->last != NULL)
492 853
                hdl->last = NULL;
493 1985
        if (hdl->st == NULL) {
494 1441
                assert(hdl->returned == 0 || hdl->avail == hdl->returned);
495 1441
                hdl->st = VTAILQ_LAST(&hdl->obj->list, storagehead);
496 1441
        }
497 1985
        if (hdl->st == NULL)
498 1
                assert(hdl->avail == hdl->returned);
499
500 3439
        while (hdl->avail > hdl->returned && (viov = VSCARAB_GET(scarab)) != NULL) {
501 1454
                CHECK_OBJ_NOTNULL(hdl->st, STORAGE_MAGIC); // ObjVAIGetExtend ensures
502 1454
                assert(hdl->boc == hdl->oc->boc);
503 1454
                assert(hdl->st_off <= hdl->st->space);
504 1454
                size_t av = hdl->avail - hdl->returned;
505 1454
                size_t l = hdl->st->space - hdl->st_off;
506 1454
                AN(l);
507 1454
                if (l > av)
508 198
                        l = av;
509 1454
                viov->iov.iov_base = TRUST_ME(hdl->st->ptr + hdl->st_off);
510 1454
                viov->iov.iov_len = l;
511 1454
                if (hdl->st_off + l == hdl->st->space) {
512 1256
                        next = VTAILQ_PREV(hdl->st, storagehead, list);
513 1256
                        AZ(hdl->last);
514 1256
                        if (next == NULL) {
515 860
                                hdl->last = hdl->st;
516 860
                                viov->lease = VAI_LEASE_NORET;
517 860
                        }
518
                        else {
519 396
                                CHECK_OBJ(next, STORAGE_MAGIC);
520 396
                                viov->lease = ptr2lease(hdl->st);
521
                        }
522
#ifdef VAI_DBG
523
                        if (wrk->vsl)
524
                                VSLb(wrk->vsl, SLT_Debug, "off %zu + l %zu == space st %p next st %p stvprv %p",
525
                                    hdl->st_off, l, hdl->st, next, hdl->boc->stevedore_priv);
526
#endif
527 1256
                        hdl->st_off = 0;
528 1256
                        hdl->st = next;
529 1256
                }
530
                else {
531 198
                        viov->lease = VAI_LEASE_NORET;
532 198
                        hdl->st_off += l;
533
                }
534 1454
                hdl->returned += l;
535 1454
                VAI_ASSERT_LEASE(viov->lease);
536 1454
                r++;
537
        }
538
539 1985
        Lck_Unlock(&hdl->boc->mtx);
540 1985
        if (state != BOS_FINISHED && hdl->avail == hdl->returned) {
541 2500
                hdl->avail = ObjVAIGetExtend(wrk, hdl->oc, hdl->returned,
542 1250
                    &state, &hdl->qe);
543 1250
        }
544 1985
        if (state == BOS_FINISHED && hdl->avail == hdl->returned)
545 736
                scarab->flags |= VSCARAB_F_END;
546 1985
        return (r);
547 3302
}
548
549
// return only buffers, used if object is not streaming
550
static void v_matchproto_(vai_return_f)
551 2297
sml_ai_return_buffers(struct worker *wrk, vai_hdl vhdl, struct vscaret *scaret)
552
{
553
        struct storage *st;
554
        struct sml_hdl *hdl;
555
        uint64_t *p;
556
557 2297
        (void) wrk;
558 2297
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
559
560 4804
        VSCARET_FOREACH(p, scaret) {
561 2507
                if (*p == VAI_LEASE_NORET)
562 586
                        continue;
563 1921
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
564 1921
                if ((st->flags & STORAGE_F_BUFFER) == 0)
565 1905
                        continue;
566 16
                sml_stv_free(hdl->stv, st);
567 16
        }
568 2297
        VSCARET_INIT(scaret, scaret->capacity);
569 2297
}
570
571
// generic return for buffers and object leases, used when streaming
572
static void v_matchproto_(vai_return_f)
573 669
sml_ai_return(struct worker *wrk, vai_hdl vhdl, struct vscaret *scaret)
574
{
575
        struct storage *st;
576
        struct sml_hdl *hdl;
577
        uint64_t *p;
578
579 669
        (void) wrk;
580 669
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
581 669
        VSCARET_CHECK_NOTNULL(scaret);
582 669
        if (scaret->used == 0)
583 0
                return;
584
585
        // callback is only registered if needed
586 669
        assert(hdl->boc != NULL && (hdl->oc->flags & OC_F_TRANSIENT) != 0);
587
588
        // filter noret and last
589 669
        VSCARET_LOCAL(todo, scaret->used);
590 1690
        VSCARET_FOREACH(p, scaret) {
591 1021
                if (*p == VAI_LEASE_NORET)
592 502
                        continue;
593 519
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
594 519
                VSCARET_ADD(todo, *p);
595 519
        }
596 669
        VSCARET_INIT(scaret, scaret->capacity);
597
598 669
        Lck_Lock(&hdl->boc->mtx);
599 1188
        VSCARET_FOREACH(p, todo) {
600 519
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
601 519
                if ((st->flags & STORAGE_F_BUFFER) != 0)
602 0
                        continue;
603 519
                VTAILQ_REMOVE(&hdl->obj->list, st, list);
604 519
                if (st == hdl->boc->stevedore_priv)
605 0
                        hdl->boc->stevedore_priv = trim_once;
606 519
        }
607 669
        Lck_Unlock(&hdl->boc->mtx);
608
609 1188
        VSCARET_FOREACH(p, todo) {
610 519
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
611
#ifdef VAI_DBG
612
                if (wrk->vsl != NULL)
613
                        VSLb(wrk->vsl, SLT_Debug, "ret %p", st);
614
#endif
615 519
                sml_stv_free(hdl->stv, st);
616 519
        }
617 669
}
618
619
static void v_matchproto_(vai_fini_f)
620 2450
sml_ai_fini(struct worker *wrk, vai_hdl *vai_hdlp)
621
{
622
        struct sml_hdl *hdl;
623
624 2450
        AN(vai_hdlp);
625 2450
        CAST_VAI_HDL_NOTNULL(hdl, *vai_hdlp, SML_HDL_MAGIC);
626 2450
        *vai_hdlp = NULL;
627
628 2450
        if (hdl->boc != NULL) {
629 759
                ObjVAICancel(wrk, hdl->boc, &hdl->qe);
630 759
                HSH_DerefBoc(wrk, hdl->oc);
631 759
                hdl->boc = NULL;
632 759
        }
633
634 2450
        if (hdl->ws != NULL)
635 32
                WS_Release(hdl->ws, 0);
636
        else
637 2418
                free(hdl);
638 2450
}
639
640
static vai_hdl v_matchproto_(vai_init_f)
641 2449
sml_ai_init(struct worker *wrk, struct objcore *oc, struct ws *ws,
642
    vai_notify_cb *notify, void *notify_priv)
643
{
644
        struct sml_hdl *hdl;
645 2449
        const size_t sz = sizeof *hdl;
646
647 2449
        if (ws != NULL && WS_ReserveSize(ws, (unsigned)sz))
648 32
                hdl = WS_Reservation(ws);
649
        else {
650 2417
                hdl = malloc(sz);
651 2417
                ws = NULL;
652
        }
653
654 2449
        AN(hdl);
655 2449
        INIT_VAI_HDL(hdl, SML_HDL_MAGIC);
656 2449
        hdl->preamble.vai_lease = sml_ai_lease_simple;
657 2449
        hdl->preamble.vai_buffer = sml_ai_buffer;
658 2449
        hdl->preamble.vai_return = sml_ai_return_buffers;
659 2449
        hdl->preamble.vai_fini = sml_ai_fini;
660 2449
        hdl->ws = ws;
661
662 2449
        hdl->oc = oc;
663 2449
        hdl->obj = sml_getobj(wrk, oc);
664 2449
        CHECK_OBJ_NOTNULL(hdl->obj, OBJECT_MAGIC);
665 2449
        hdl->stv = oc->stobj->stevedore;
666 2449
        CHECK_OBJ_NOTNULL(hdl->stv, STEVEDORE_MAGIC);
667
668
669 2449
        hdl->qe.magic = VAI_Q_MAGIC;
670 2449
        hdl->qe.cb = notify;
671 2449
        hdl->qe.hdl = hdl;
672 2449
        hdl->qe.priv = notify_priv;
673
674 2449
        hdl->boc = HSH_RefBoc(oc);
675 2449
        if (hdl->boc == NULL) {
676 1691
                hdl->st = VTAILQ_LAST(&hdl->obj->list, storagehead);
677 1691
                CHECK_OBJ_ORNULL(hdl->st, STORAGE_MAGIC);
678 1691
                return (hdl);
679
        }
680
        /* we only initialize notifications if we have a boc, so
681
         * any wrong attempt triggers magic checks.
682
         */
683 758
        hdl->preamble.vai_lease = sml_ai_lease_boc;
684 758
        if ((hdl->oc->flags & OC_F_TRANSIENT) != 0)
685 341
                hdl->preamble.vai_return = sml_ai_return;
686 758
        return (hdl);
687 2449
}
688
689
/*
690
 * trivial notification to allow the iterator to simply block
691
 */
692
struct sml_notify {
693
        unsigned                magic;
694
#define SML_NOTIFY_MAGIC        0x4589af31
695
        unsigned                hasmore;
696
        pthread_mutex_t         mtx;
697
        pthread_cond_t          cond;
698
};
699
700
static void
701 2417
sml_notify_init(struct sml_notify *sn)
702
{
703
704 2417
        INIT_OBJ(sn, SML_NOTIFY_MAGIC);
705 2417
        AZ(pthread_mutex_init(&sn->mtx, NULL));
706 2417
        AZ(pthread_cond_init(&sn->cond, NULL));
707 2417
}
708
709
static void
710 2416
sml_notify_fini(struct sml_notify *sn)
711
{
712
713 2416
        CHECK_OBJ_NOTNULL(sn, SML_NOTIFY_MAGIC);
714 2416
        AZ(pthread_mutex_destroy(&sn->mtx));
715 2416
        AZ(pthread_cond_destroy(&sn->cond));
716 2416
}
717
718
static void v_matchproto_(vai_notify_cb)
719 1305
sml_notify(vai_hdl hdl, void *priv)
720
{
721
        struct sml_notify *sn;
722
723 1305
        (void) hdl;
724 1305
        CAST_OBJ_NOTNULL(sn, priv, SML_NOTIFY_MAGIC);
725 1305
        AZ(pthread_mutex_lock(&sn->mtx));
726 1305
        sn->hasmore = 1;
727 1305
        AZ(pthread_cond_signal(&sn->cond));
728 1305
        AZ(pthread_mutex_unlock(&sn->mtx));
729
730 1305
}
731
732
static void
733 1282
sml_notify_wait(struct sml_notify *sn)
734
{
735
736 1282
        CHECK_OBJ_NOTNULL(sn, SML_NOTIFY_MAGIC);
737 1282
        AZ(pthread_mutex_lock(&sn->mtx));
738 2205
        while (sn->hasmore == 0)
739 923
                AZ(pthread_cond_wait(&sn->cond, &sn->mtx));
740 1282
        AN(sn->hasmore);
741 1282
        sn->hasmore = 0;
742 1282
        AZ(pthread_mutex_unlock(&sn->mtx));
743 1282
}
744
745
static int v_matchproto_(objiterator_f)
746 2418
sml_iterator(struct worker *wrk, struct objcore *oc,
747
    void *priv, objiterate_f *func, int final)
748
{
749
        struct sml_notify sn;
750
        struct viov *vio, *last;
751
        unsigned u, uu;
752
        vai_hdl hdl;
753
        int nn, r, r2, islast;
754
755 2418
        VSCARAB_LOCAL(scarab, 16);
756 2418
        VSCARET_LOCAL(scaret, 16);
757
758 2418
        (void) final; // phase out?
759 2418
        sml_notify_init(&sn);
760 2418
        hdl = ObjVAIinit(wrk, oc, NULL, sml_notify, &sn);
761 2418
        AN(hdl);
762
763 2418
        r = u = 0;
764
765 2418
        do {
766 5337
                do {
767 6584
                        nn = ObjVAIlease(wrk, hdl, scarab);
768 6584
                        if (nn <= 0 || scarab->flags & VSCARAB_F_END)
769 5336
                                break;
770 1248
                } while (scarab->used < scarab->capacity);
771
772
                /*
773
                 * nn is the wait/return action or 0
774
                 * nn tells us if to flush
775
                 */
776 5337
                uu = u;
777 5337
                last = VSCARAB_LAST(scarab);
778 8736
                VSCARAB_FOREACH(vio, scarab) {
779 3451
                        islast = vio == last;
780 3451
                        AZ(u & OBJ_ITER_END);
781 3451
                        if (islast && scarab->flags & VSCARAB_F_END)
782 1671
                                u |= OBJ_ITER_END;
783
784
                        // flush if it is the scarab's last IOV and we will block next
785
                        // or if we need space in the return leases array
786 3451
                        uu = u;
787 3451
                        if ((islast && nn < 0) || scaret->used == scaret->capacity - 1)
788 1212
                                uu |= OBJ_ITER_FLUSH;
789
790
                        // null iov with the only purpose to return the resume ptr lease
791
                        // exception needed because assert(len > 0) in VDP_bytes()
792 3451
                        if (vio->iov.iov_base == null_iov)
793 165
                                r = 0;
794
                        else
795 3286
                                r = func(priv, uu, vio->iov.iov_base, vio->iov.iov_len);
796 3451
                        if (r != 0)
797 52
                                break;
798
799
                        // sufficient space ensured by capacity check above
800 3399
                        VSCARET_ADD(scaret, vio->lease);
801
802
#ifdef VAI_DBG
803
                        if (wrk->vsl)
804
                                VSLb(wrk->vsl, SLT_Debug, "len %zu scaret %u uu %u",
805
                                    vio->iov.iov_len, scaret->used, uu);
806
#endif
807
808
                        // whenever we have flushed, return leases
809 3399
                        if ((uu & OBJ_ITER_FLUSH) && scaret->used > 0)
810 1195
                                ObjVAIreturn(wrk, hdl, scaret);
811 3399
                }
812
813
                // return leases which we did not use if error (break)
814 5389
                VSCARAB_FOREACH_RESUME(vio, scarab) {
815 52
                        if (scaret->used == scaret->capacity)
816 0
                                ObjVAIreturn(wrk, hdl, scaret);
817 52
                        VSCARET_ADD(scaret, vio->lease);
818 52
                }
819
820
                // we have now completed the scarab
821 5337
                VSCARAB_INIT(scarab, scarab->capacity);
822
823
#ifdef VAI_DBG
824
                if (wrk->vsl)
825
                        VSLb(wrk->vsl, SLT_Debug, "r %d nn %d uu %u",
826
                            r, nn, uu);
827
#endif
828
829
                // flush before blocking if we did not already
830 5337
                if (r == 0 && (nn == -ENOBUFS || nn == -EAGAIN) &&
831 5286
                    (uu & OBJ_ITER_FLUSH) == 0) {
832 87
                        r = func(priv, OBJ_ITER_FLUSH, NULL, 0);
833 87
                        if (scaret->used > 0)
834 0
                                ObjVAIreturn(wrk, hdl, scaret);
835 87
                }
836
837 5337
                if (r == 0 && (nn == -ENOBUFS || nn == -EAGAIN)) {
838 1281
                        assert(scaret->used <= 1);
839 1281
                        sml_notify_wait(&sn);
840 1281
                }
841 4056
                else if (r == 0 && nn < 0)
842 15
                        r = -1;
843 5337
        } while (nn != 0 && r == 0);
844
845 2418
        if ((u & OBJ_ITER_END) == 0) {
846 747
                r2 = func(priv, OBJ_ITER_END, NULL, 0);
847 747
                if (r == 0)
848 714
                        r = r2;
849 747
        }
850
851 2418
        if (scaret->used > 0)
852 1689
                ObjVAIreturn(wrk, hdl, scaret);
853
854 2418
        ObjVAIfini(wrk, &hdl);
855 2418
        sml_notify_fini(&sn);
856
857 2418
        return (r);
858
}
859
860
/*--------------------------------------------------------------------
861
 */
862
863
static struct storage *
864 3245
objallocwithnuke(struct worker *wrk, const struct stevedore *stv, ssize_t size,
865
    int flags)
866
{
867 3245
        struct storage *st = NULL;
868
869 3245
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
870 3245
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
871
872 3245
        if (size > cache_param->fetch_maxchunksize) {
873 2
                if (!(flags & LESS_MEM_ALLOCED_IS_OK))
874 0
                        return (NULL);
875 2
                size = cache_param->fetch_maxchunksize;
876 2
        }
877
878 3245
        assert(size <= UINT_MAX);       /* field limit in struct storage */
879
880 3245
        do {
881
                /* try to allocate from it */
882 3253
                st = sml_stv_alloc(stv, size, flags);
883 3253
                if (st != NULL)
884 3240
                        break;
885
886
                /* no luck; try to free some space and keep trying */
887 13
                if (stv->lru == NULL)
888 0
                        break;
889 13
        } while (LRU_NukeOne(wrk, stv->lru));
890
891 3245
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
892 3245
        return (st);
893 3245
}
894
895
static int v_matchproto_(objgetspace_f)
896 58076
sml_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz,
897
    uint8_t **ptr)
898
{
899
        struct object *o;
900
        struct storage *st;
901
902 58076
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
903 58076
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
904 58076
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
905 58076
        AN(sz);
906 58076
        AN(ptr);
907 58076
        if (*sz == 0)
908 54466
                *sz = cache_param->fetch_chunksize;
909 58076
        assert(*sz > 0);
910 58076
        if (oc->boc->transit_buffer > 0)
911 498
                *sz = vmin_t(ssize_t, *sz, oc->boc->transit_buffer);
912
913 58076
        o = sml_getobj(wrk, oc);
914 58076
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
915 58076
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
916
917 58076
        st = VTAILQ_FIRST(&o->list);
918 58076
        if (st != NULL && st->len < st->space) {
919 55259
                *sz = st->space - st->len;
920 55259
                *ptr = st->ptr + st->len;
921 55259
                assert (*sz > 0);
922 55259
                return (1);
923
        }
924
925 2817
        st = objallocwithnuke(wrk, oc->stobj->stevedore, *sz,
926
            LESS_MEM_ALLOCED_IS_OK);
927 2817
        if (st == NULL)
928 5
                return (0);
929
930 2812
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
931 2812
        Lck_Lock(&oc->boc->mtx);
932 2812
        VTAILQ_INSERT_HEAD(&o->list, st, list);
933 2812
        Lck_Unlock(&oc->boc->mtx);
934
935 2812
        *sz = st->space - st->len;
936 2812
        assert (*sz > 0);
937 2812
        *ptr = st->ptr + st->len;
938 2812
        return (1);
939 58076
}
940
941
static void v_matchproto_(objextend_f)
942 56604
sml_extend(struct worker *wrk, struct objcore *oc, ssize_t l)
943
{
944
        struct object *o;
945
        struct storage *st;
946
947 56604
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
948 56604
        assert(l > 0);
949
950 56604
        o = sml_getobj(wrk, oc);
951 56604
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
952 56604
        st = VTAILQ_FIRST(&o->list);
953 56604
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
954 56604
        assert(st->len + l <= st->space);
955 56604
        st->len += l;
956 56604
}
957
958
static void v_matchproto_(objtrimstore_f)
959 1886
sml_trimstore(struct worker *wrk, struct objcore *oc)
960
{
961
        const struct stevedore *stv;
962
        struct storage *st, *st1;
963
        struct object *o;
964
965 1886
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
966 1886
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
967 1886
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
968
969 1886
        stv = oc->stobj->stevedore;
970 1886
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
971
972 1886
        if (oc->boc->stevedore_priv != NULL)
973 0
                WRONG("sml_trimstore already called");
974 1886
        oc->boc->stevedore_priv = trim_once;
975
976 1886
        if (stv->sml_free == NULL)
977 0
                return;
978
979 1886
        o = sml_getobj(wrk, oc);
980 1886
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
981 1886
        st = VTAILQ_FIRST(&o->list);
982
983 1886
        if (st == NULL)
984 0
                return;
985
986 1886
        if (st->len == 0) {
987 8
                Lck_Lock(&oc->boc->mtx);
988 8
                VTAILQ_REMOVE(&o->list, st, list);
989 8
                Lck_Unlock(&oc->boc->mtx);
990
                /* sml_bocdone frees this */
991 8
                oc->boc->stevedore_priv = st;
992 8
                return;
993
        }
994
995 1878
        if (st->space - st->len < 512)
996 1682
                return;
997
998 196
        st1 = sml_stv_alloc(stv, st->len, 0);
999 196
        if (st1 == NULL)
1000 0
                return;
1001 196
        assert(st1->space >= st->len);
1002
1003 196
        memcpy(st1->ptr, st->ptr, st->len);
1004 196
        st1->len = st->len;
1005 196
        Lck_Lock(&oc->boc->mtx);
1006 196
        VTAILQ_REMOVE(&o->list, st, list);
1007 196
        VTAILQ_INSERT_HEAD(&o->list, st1, list);
1008 196
        Lck_Unlock(&oc->boc->mtx);
1009
        /* sml_bocdone frees this */
1010 196
        oc->boc->stevedore_priv = st;
1011 1886
}
1012
1013
static void v_matchproto_(objbocdone_f)
1014 2840
sml_bocdone(struct worker *wrk, struct objcore *oc, struct boc *boc)
1015
{
1016
        const struct stevedore *stv;
1017
1018 2840
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1019 2840
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1020 2840
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1021 2840
        stv = oc->stobj->stevedore;
1022 2840
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
1023
1024 2840
        sml_bocfini(stv, boc);
1025
1026 2840
        if (stv->lru != NULL) {
1027 2801
                if (isnan(wrk->lastused))
1028 0
                        wrk->lastused = VTIM_real();
1029 2801
                LRU_Add(oc, wrk->lastused);     // approx timestamp is OK
1030 2801
        }
1031 2840
}
1032
1033
static const void * v_matchproto_(objgetattr_f)
1034 22319
sml_getattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
1035
   ssize_t *len)
1036
{
1037
        struct object *o;
1038
        ssize_t dummy;
1039
1040 22319
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1041
1042 22319
        if (len == NULL)
1043 13676
                len = &dummy;
1044 22319
        o = sml_getobj(wrk, oc);
1045 22319
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
1046
1047 22319
        switch (attr) {
1048
                /* Fixed size attributes */
1049
#define OBJ_FIXATTR(U, l, s)                                            \
1050
        case OA_##U:                                                    \
1051
                *len = sizeof o->fa_##l;                                \
1052
                return (o->fa_##l);
1053
#include "tbl/obj_attr.h"
1054
1055
                /* Variable size attributes */
1056
#define OBJ_VARATTR(U, l)                                               \
1057
        case OA_##U:                                                    \
1058
                if (o->va_##l == NULL)                                  \
1059
                        return (NULL);                                  \
1060
                *len = o->va_##l##_len;                                 \
1061
                return (o->va_##l);
1062
#include "tbl/obj_attr.h"
1063
1064
                /* Auxiliary attributes */
1065
#define OBJ_AUXATTR(U, l)                                               \
1066
        case OA_##U:                                                    \
1067
                if (o->aa_##l == NULL)                                  \
1068
                        return (NULL);                                  \
1069
                CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);            \
1070
                *len = o->aa_##l->len;                                  \
1071
                return (o->aa_##l->ptr);
1072
#include "tbl/obj_attr.h"
1073
1074
        default:
1075
                break;
1076
        }
1077 0
        WRONG("Unsupported OBJ_ATTR");
1078 22319
}
1079
1080
static void * v_matchproto_(objsetattr_f)
1081 10579
sml_setattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
1082
    ssize_t len, const void *ptr)
1083
{
1084
        struct object *o;
1085 10579
        void *retval = NULL;
1086
        struct storage *st;
1087
1088 10579
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1089
1090 10579
        o = sml_getobj(wrk, oc);
1091 10579
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
1092 10579
        st = o->objstore;
1093
1094 10579
        switch (attr) {
1095
                /* Fixed size attributes */
1096
#define OBJ_FIXATTR(U, l, s)                                            \
1097
        case OA_##U:                                                    \
1098
                assert(len == sizeof o->fa_##l);                        \
1099
                retval = o->fa_##l;                                     \
1100
                break;
1101
#include "tbl/obj_attr.h"
1102
1103
                /* Variable size attributes */
1104
#define OBJ_VARATTR(U, l)                                               \
1105
        case OA_##U:                                                    \
1106
                if (o->va_##l##_len > 0) {                              \
1107
                        AN(o->va_##l);                                  \
1108
                        assert(len == o->va_##l##_len);                 \
1109
                        retval = o->va_##l;                             \
1110
                } else if (len > 0) {                                   \
1111
                        assert(len <= UINT_MAX);                        \
1112
                        assert(st->len + len <= st->space);             \
1113
                        o->va_##l = st->ptr + st->len;                  \
1114
                        st->len += len;                                 \
1115
                        o->va_##l##_len = len;                          \
1116
                        retval = o->va_##l;                             \
1117
                }                                                       \
1118
                break;
1119
#include "tbl/obj_attr.h"
1120
1121
                /* Auxiliary attributes */
1122
#define OBJ_AUXATTR(U, l)                                               \
1123
        case OA_##U:                                                    \
1124
                if (o->aa_##l != NULL) {                                \
1125
                        CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);    \
1126
                        assert(len == o->aa_##l->len);                  \
1127
                        retval = o->aa_##l->ptr;                        \
1128
                        break;                                          \
1129
                }                                                       \
1130
                if (len == 0)                                           \
1131
                        break;                                          \
1132
                o->aa_##l = objallocwithnuke(wrk, oc->stobj->stevedore, \
1133
                    len, 0);                                            \
1134
                if (o->aa_##l == NULL)                                  \
1135
                        break;                                          \
1136
                CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);            \
1137
                assert(len <= o->aa_##l->space);                        \
1138
                o->aa_##l->len = len;                                   \
1139
                retval = o->aa_##l->ptr;                                \
1140
                break;
1141
#include "tbl/obj_attr.h"
1142
1143
        default:
1144 0
                WRONG("Unsupported OBJ_ATTR");
1145
                break;
1146
        }
1147
1148 10579
        if (retval != NULL && ptr != NULL)
1149 377
                memcpy(retval, ptr, len);
1150 10579
        return (retval);
1151
}
1152
1153
const struct obj_methods SML_methods = {
1154
        .objfree        = sml_objfree,
1155
        .objiterator    = sml_iterator,
1156
        .objgetspace    = sml_getspace,
1157
        .objextend      = sml_extend,
1158
        .objtrimstore   = sml_trimstore,
1159
        .objbocdone     = sml_bocdone,
1160
        .objslim        = sml_slim,
1161
        .objgetattr     = sml_getattr,
1162
        .objsetattr     = sml_setattr,
1163
        .objtouch       = LRU_Touch,
1164
        .vai_init       = sml_ai_init
1165
};
1166
1167
static void
1168 4
sml_panic_st(struct vsb *vsb, const char *hd, const struct storage *st)
1169
{
1170 8
        VSB_printf(vsb, "%s = %p {priv=%p, ptr=%p, len=%u, space=%u},\n",
1171 4
            hd, st, st->priv, st->ptr, st->len, st->space);
1172 4
}
1173
1174
void
1175 2
SML_panic(struct vsb *vsb, const struct objcore *oc)
1176
{
1177
        struct object *o;
1178
        struct storage *st;
1179
1180 2
        VSB_printf(vsb, "Simple = %p,\n", oc->stobj->priv);
1181 2
        if (oc->stobj->priv == NULL)
1182 0
                return;
1183 2
        o = oc->stobj->priv;
1184 2
        PAN_CheckMagic(vsb, o, OBJECT_MAGIC);
1185 2
        sml_panic_st(vsb, "Obj", o->objstore);
1186
1187
#define OBJ_FIXATTR(U, l, sz) \
1188
        VSB_printf(vsb, "%s = ", #U); \
1189
        VSB_quote(vsb, (const void*)o->fa_##l, sz, VSB_QUOTE_HEX); \
1190
        VSB_printf(vsb, ",\n");
1191
1192
#define OBJ_VARATTR(U, l) \
1193
        VSB_printf(vsb, "%s = {len=%u, ptr=%p},\n", \
1194
            #U, o->va_##l##_len, o->va_##l);
1195
1196
#define OBJ_AUXATTR(U, l)                                               \
1197
        do {                                                            \
1198
                if (o->aa_##l != NULL) sml_panic_st(vsb, #U, o->aa_##l);\
1199
        } while(0);
1200
1201
#include "tbl/obj_attr.h"
1202
1203 4
        VTAILQ_FOREACH(st, &o->list, list) {
1204 2
                sml_panic_st(vsb, "Body", st);
1205 2
        }
1206
}