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 110400
#include "cache/cache_varnishd.h"
36 90037
37 21156
#include "cache/cache_obj.h"
38 14527
#include "cache/cache_objhead.h"
39 90034
40
#include "storage/storage.h"
41
#include "storage/storage_simple.h"
42
43
#include "vtim.h"
44
45 122000
/* Flags for allocating memory in sml_stv_alloc */
46 237008
#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 160142
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 139078
sml_stv_alloc(const struct stevedore *stv, ssize_t size, int flags)
61
{
62
        struct storage *st;
63
64 139078
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
65 139078
        AN(stv->sml_alloc);
66
67 139078
        if (!(flags & LESS_MEM_ALLOCED_IS_OK)) {
68 25046
                if (size > cache_param->fetch_maxchunksize)
69 0
                        return (NULL);
70
                else
71 25046
                        return (stv->sml_alloc(stv, size));
72
        }
73
74 114032
        if (size > cache_param->fetch_maxchunksize)
75 0
                size = cache_param->fetch_maxchunksize;
76
77 114032
        assert(size <= UINT_MAX);       /* field limit in struct storage */
78
79 119432
        for (;;) {
80
                /* try to allocate from it */
81 119432
                assert(size > 0);
82 119432
                st = stv->sml_alloc(stv, size);
83 119432
                if (st != NULL)
84 113593
                        break;
85
86 5839
                if (size <= cache_param->fetch_chunksize)
87 439
                        break;
88
89 5400
                size /= 2;
90
        }
91 114032
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
92 114032
        return (st);
93 139078
}
94
95
static void
96 179021
sml_stv_free(const struct stevedore *stv, struct storage *st)
97
{
98
99 179021
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
100 179021
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
101 179021
        if (stv->sml_free != NULL)
102 179024
                stv->sml_free(st);
103 179027
}
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 115319
SML_MkObject(const struct stevedore *stv, struct objcore *oc, void *ptr)
115
{
116
        struct object *o;
117
118 115319
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
119 115319
        AN(stv->methods);
120 115319
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
121
122 115319
        assert(PAOK(ptr));
123
124 115319
        o = ptr;
125 115319
        INIT_OBJ(o, OBJECT_MAGIC);
126
127 115319
        VTAILQ_INIT(&o->list);
128
129 115319
        oc->stobj->stevedore = stv;
130 115319
        oc->stobj->priv = o;
131 115319
        oc->stobj->priv2 = 0;
132 115319
        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 114879
SML_allocobj(struct worker *wrk, const struct stevedore *stv,
142
    struct objcore *oc, unsigned wsl)
143
{
144
        struct object *o;
145 114879
        struct storage *st = NULL;
146
        unsigned ltot;
147
148 114879
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
149 114879
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
150 114879
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
151
152 114879
        AN(stv->sml_alloc);
153
154 114879
        ltot = sizeof(*o) + PRNDUP(wsl);
155
156 114879
        do {
157 114999
                st = stv->sml_alloc(stv, ltot);
158 114999
                if (st != NULL && st->space < ltot) {
159 0
                        stv->sml_free(st);
160 0
                        st = NULL;
161 0
                }
162 114999
        } while (st == NULL && LRU_NukeOne(wrk, stv->lru));
163 114879
        if (st == NULL)
164 440
                return (0);
165
166 114439
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
167 114439
        o = SML_MkObject(stv, oc, st->ptr);
168 114439
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
169 114439
        st->len = sizeof(*o);
170 114439
        o->objstore = st;
171 114439
        return (1);
172 114879
}
173
174
void * v_matchproto_(storage_allocbuf_t)
175 11760
SML_AllocBuf(struct worker *wrk, const struct stevedore *stv, size_t size,
176
    uintptr_t *ppriv)
177
{
178
        struct storage *st;
179
180 11760
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
181 11760
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
182 11760
        AN(ppriv);
183
184 11760
        if (size > UINT_MAX)
185 0
                return (NULL);
186 11760
        st = objallocwithnuke(wrk, stv, size, 0);
187 11760
        if (st == NULL)
188 0
                return (NULL);
189 11760
        assert(st->space >= size);
190 11760
        st->flags = STORAGE_F_BUFFER;
191 11760
        st->len = size;
192 11760
        *ppriv = (uintptr_t)st;
193 11760
        return (st->ptr);
194 11760
}
195
196
void v_matchproto_(storage_freebuf_t)
197 11760
SML_FreeBuf(struct worker *wrk, const struct stevedore *stv, uintptr_t priv)
198
{
199
        struct storage *st;
200
201 11760
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
202 11760
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
203
204 11760
        CAST_OBJ_NOTNULL(st, (void *)priv, STORAGE_MAGIC);
205 11760
        assert(st->flags == STORAGE_F_BUFFER);
206 11760
        sml_stv_free(stv, st);
207 11760
}
208
209
/*---------------------------------------------------------------------
210
 */
211
212
static struct object *
213 6228042
sml_getobj(struct worker *wrk, struct objcore *oc)
214
{
215
        const struct stevedore *stv;
216
        struct object *o;
217
218 6228042
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
219 6228042
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
220 6228042
        stv = oc->stobj->stevedore;
221 6228042
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
222 6228042
        if (stv->sml_getobj != NULL)
223 11720
                return (stv->sml_getobj(wrk, oc));
224 6216322
        if (oc->stobj->priv == NULL)
225 0
                return (NULL);
226 6216322
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
227 6216322
        return (o);
228 6228042
}
229
230
static void v_matchproto_(objslim_f)
231 129062
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 129062
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
238
239 129062
        stv = oc->stobj->stevedore;
240 129062
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
241 129062
        o = sml_getobj(wrk, oc);
242 129062
        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 193705
        VTAILQ_FOREACH_SAFE(st, &o->list, list, stn) {
254 64643
                CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
255 64643
                VTAILQ_REMOVE(&o->list, st, list);
256 64643
                sml_stv_free(stv, st);
257 64643
        }
258
}
259
260
static void
261 115957
sml_bocfini(const struct stevedore *stv, struct boc *boc)
262
{
263
        struct storage *st;
264
265 115957
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
266 115957
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
267
268 115957
        if (boc->stevedore_priv == NULL ||
269 76448
            boc->stevedore_priv == trim_once)
270 107748
                return;
271
272
        /* Free any leftovers from Trim */
273 8209
        TAKE_OBJ_NOTNULL(st, &boc->stevedore_priv, STORAGE_MAGIC);
274 8209
        sml_stv_free(stv, st);
275 115957
}
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 72548
sml_objfree(struct worker *wrk, struct objcore *oc)
284
{
285
        const struct stevedore *stv;
286
        struct storage *st;
287
        struct object *o;
288
289 72548
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
290 72548
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
291 72548
        stv = oc->stobj->stevedore;
292 72548
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
293 72548
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
294
295 72548
        sml_slim(wrk, oc);
296 72548
        st = o->objstore;
297 72548
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
298 72548
        FINI_OBJ(o);
299
300 72548
        if (oc->boc != NULL)
301 840
                sml_bocfini(stv, oc->boc);
302 71708
        else if (stv->lru != NULL)
303 71703
                LRU_Remove(oc);
304
305 72546
        sml_stv_free(stv, st);
306
307 72546
        memset(oc->stobj, 0, sizeof oc->stobj);
308
309 72546
        wrk->stats->n_object--;
310 72546
}
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 76073
sml_ai_viov_fill(struct viov *viov, struct storage *st)
336
{
337 76073
        viov->iov.iov_base = TRUST_ME(st->ptr);
338 76073
        viov->iov.iov_len = st->len;
339 76073
        viov->lease = ptr2lease(st);
340 76073
        VAI_ASSERT_LEASE(viov->lease);
341 76073
}
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 640
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 640
        int r = 0;
381
382 640
        (void) wrk;
383 640
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
384 640
        stv = hdl->stv;
385 640
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
386
387 1278
        VSCARAB_FOREACH(vio, scarab)
388 638
                if (vio->iov.iov_len > UINT_MAX)
389 0
                        return (-EINVAL);
390
391 1280
        VSCARAB_FOREACH(vio, scarab) {
392 640
                st = objallocwithnuke(wrk, stv, vio->iov.iov_len, 0);
393 640
                if (st == NULL)
394 0
                        break;
395 640
                assert(st->space >= vio->iov.iov_len);
396 640
                st->flags = STORAGE_F_BUFFER;
397 640
                st->len = st->space;
398
399 640
                sml_ai_viov_fill(vio, st);
400 640
                r++;
401 640
        }
402 640
        if (r == 0) {
403 0
                sml_ai_later(wrk, hdl);
404 0
                r = -EAGAIN;
405 0
        }
406 640
        return (r);
407 640
}
408
409
static int
410 134851
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 134851
        int r = 0;
416
417 134851
        (void) wrk;
418 134851
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
419 134851
        VSCARAB_CHECK_NOTNULL(scarab);
420
421 134851
        AZ(hdl->st_off);
422 134851
        st = hdl->st;
423 210283
        while (st != NULL && (viov = VSCARAB_GET(scarab)) != NULL) {
424 75432
                CHECK_OBJ(st, STORAGE_MAGIC);
425 75432
                sml_ai_viov_fill(viov, st);
426 75432
                r++;
427 75432
                st = VTAILQ_PREV(st, storagehead, list);
428
        }
429 134851
        hdl->st = st;
430 134851
        if (st == NULL)
431 134211
                scarab->flags |= VSCARAB_F_END;
432 134851
        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 135857
sml_ai_lease_boc(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
449
{
450 135857
        enum boc_state_e state = BOS_INVALID;
451
        struct storage *next;
452
        struct sml_hdl *hdl;
453
        struct viov *viov;
454 135857
        int r = 0;
455
456 135857
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
457 135857
        VSCARAB_CHECK_NOTNULL(scarab);
458 135857
        assert(hdl->boc == hdl->oc->boc);
459
460 135857
        if (hdl->avail == hdl->returned) {
461 269678
                hdl->avail = ObjVAIGetExtend(wrk, hdl->oc, hdl->returned,
462 134839
                    &state, &hdl->qe);
463 134839
                assert(state >= BOS_STREAM);
464 134839
                if (state == BOS_FAILED) {
465 639
                        hdl->last = NULL;
466 639
                        return (-EPIPE);
467
                }
468 134200
                else if (state == BOS_FINISHED)
469 30043
                        (void)0;
470 104157
                else if (hdl->avail == hdl->returned) {
471
                        // ObjVAIGetExtend() has scheduled a notification
472 53429
                        if (hdl->boc->transit_buffer > 0)
473 12081
                                return (-ENOBUFS);
474
                        else
475 41348
                                return (-EAGAIN);
476
                }
477
                else
478 50728
                        assert(state < BOS_FINISHED);
479 80771
        }
480 81789
        Lck_Lock(&hdl->boc->mtx);
481 81789
        if (hdl->st == NULL && hdl->last != NULL)
482 34976
                hdl->st = VTAILQ_PREV(hdl->last, storagehead, list);
483 81789
        if (hdl->last != NULL && state < BOS_FINISHED) {
484 6515
                viov = VSCARAB_GET(scarab);
485 6515
                AN(viov);
486 6515
                viov->iov.iov_base = null_iov;
487 6515
                viov->iov.iov_len = 0;
488 6515
                viov->lease = ptr2lease(hdl->last);
489 6515
                r++;
490 6515
        }
491 81789
        if (hdl->last != NULL)
492 34977
                hdl->last = NULL;
493 81789
        if (hdl->st == NULL && hdl->returned == 0)
494 30905
                hdl->st = VTAILQ_LAST(&hdl->obj->list, storagehead);
495 81789
        if (hdl->st == NULL)
496 28395
                assert(hdl->avail == hdl->returned);
497
498 140936
        while (hdl->avail > hdl->returned && (viov = VSCARAB_GET(scarab)) != NULL) {
499 59147
                CHECK_OBJ_NOTNULL(hdl->st, STORAGE_MAGIC); // ObjVAIGetExtend ensures
500 59147
                assert(hdl->boc == hdl->oc->boc);
501 59147
                assert(hdl->st_off <= hdl->st->space);
502 59147
                size_t av = hdl->avail - hdl->returned;
503 59147
                size_t l = hdl->st->space - hdl->st_off;
504 59147
                AN(l);
505 59147
                if (l > av)
506 7877
                        l = av;
507 59147
                viov->iov.iov_base = TRUST_ME(hdl->st->ptr + hdl->st_off);
508 59147
                viov->iov.iov_len = l;
509 59147
                if (hdl->st_off + l == hdl->st->space) {
510 51270
                        next = VTAILQ_PREV(hdl->st, storagehead, list);
511 51270
                        AZ(hdl->last);
512 51270
                        if (next == NULL) {
513 35382
                                hdl->last = hdl->st;
514 35382
                                viov->lease = VAI_LEASE_NORET;
515 35382
                        }
516
                        else {
517 15888
                                CHECK_OBJ(next, STORAGE_MAGIC);
518 15888
                                viov->lease = ptr2lease(hdl->st);
519
                        }
520
#ifdef VAI_DBG
521
                        if (wrk->vsl)
522
                                VSLb(wrk->vsl, SLT_Debug, "off %zu + l %zu == space st %p next st %p stvprv %p",
523
                                    hdl->st_off, l, hdl->st, next, hdl->boc->stevedore_priv);
524
#endif
525 51270
                        hdl->st_off = 0;
526 51270
                        hdl->st = next;
527 51270
                }
528
                else {
529 7877
                        viov->lease = VAI_LEASE_NORET;
530 7877
                        hdl->st_off += l;
531
                }
532 59147
                hdl->returned += l;
533 59147
                VAI_ASSERT_LEASE(viov->lease);
534 59147
                r++;
535
        }
536
537 81789
        Lck_Unlock(&hdl->boc->mtx);
538 81789
        if (state != BOS_FINISHED && hdl->avail == hdl->returned) {
539 103472
                hdl->avail = ObjVAIGetExtend(wrk, hdl->oc, hdl->returned,
540 51736
                    &state, &hdl->qe);
541 51736
        }
542 81789
        if (state == BOS_FINISHED && hdl->avail == hdl->returned)
543 30047
                scarab->flags |= VSCARAB_F_END;
544 81789
        return (r);
545 135857
}
546
547
// return only buffers, used if object is not streaming
548
static void v_matchproto_(vai_return_f)
549 93571
sml_ai_return_buffers(struct worker *wrk, vai_hdl vhdl, struct vscaret *scaret)
550
{
551
        struct storage *st;
552
        struct sml_hdl *hdl;
553
        uint64_t *p;
554
555 93571
        (void) wrk;
556 93571
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
557
558 195685
        VSCARET_FOREACH(p, scaret) {
559 102114
                if (*p == VAI_LEASE_NORET)
560 24388
                        continue;
561 77726
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
562 77726
                if ((st->flags & STORAGE_F_BUFFER) == 0)
563 77086
                        continue;
564 640
                sml_stv_free(hdl->stv, st);
565 640
        }
566 93571
        VSCARET_INIT(scaret, scaret->capacity);
567 93571
}
568
569
// generic return for buffers and object leases, used when streaming
570
static void v_matchproto_(vai_return_f)
571 27256
sml_ai_return(struct worker *wrk, vai_hdl vhdl, struct vscaret *scaret)
572
{
573
        struct storage *st;
574
        struct sml_hdl *hdl;
575
        uint64_t *p;
576
577 27256
        (void) wrk;
578 27256
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
579 27256
        VSCARET_CHECK_NOTNULL(scaret);
580 27256
        if (scaret->used == 0)
581 0
                return;
582
583
        // callback is only registered if needed
584 27256
        assert(hdl->boc != NULL && (hdl->oc->flags & OC_F_TRANSIENT) != 0);
585
586
        // filter noret and last
587 27256
        VSCARET_LOCAL(todo, scaret->used);
588 68164
        VSCARET_FOREACH(p, scaret) {
589 40908
                if (*p == VAI_LEASE_NORET)
590 20160
                        continue;
591 20748
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
592 20748
                VSCARET_ADD(todo, *p);
593 20748
        }
594 27256
        VSCARET_INIT(scaret, scaret->capacity);
595
596 27256
        Lck_Lock(&hdl->boc->mtx);
597 48004
        VSCARET_FOREACH(p, todo) {
598 20748
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
599 20748
                if ((st->flags & STORAGE_F_BUFFER) != 0)
600 0
                        continue;
601 20748
                VTAILQ_REMOVE(&hdl->obj->list, st, list);
602 20748
                if (st == hdl->boc->stevedore_priv)
603 0
                        hdl->boc->stevedore_priv = trim_once;
604 20748
        }
605 27256
        Lck_Unlock(&hdl->boc->mtx);
606
607 48005
        VSCARET_FOREACH(p, todo) {
608 20749
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
609
#ifdef VAI_DBG
610
                if (wrk->vsl != NULL)
611
                        VSLb(wrk->vsl, SLT_Debug, "ret %p", st);
612
#endif
613 20749
                sml_stv_free(hdl->stv, st);
614 20749
        }
615 27256
}
616
617
static void v_matchproto_(vai_fini_f)
618 99476
sml_ai_fini(struct worker *wrk, vai_hdl *vai_hdlp)
619
{
620
        struct sml_hdl *hdl;
621
622 99476
        AN(vai_hdlp);
623 99476
        CAST_VAI_HDL_NOTNULL(hdl, *vai_hdlp, SML_HDL_MAGIC);
624 99476
        *vai_hdlp = NULL;
625
626 99476
        if (hdl->boc != NULL) {
627 31078
                ObjVAICancel(wrk, hdl->boc, &hdl->qe);
628 31078
                HSH_DerefBoc(wrk, hdl->oc);
629 31078
                hdl->boc = NULL;
630 31078
        }
631
632 99476
        if (hdl->ws != NULL)
633 1280
                WS_Release(hdl->ws, 0);
634
        else
635 98196
                free(hdl);
636 99476
}
637
638
static vai_hdl v_matchproto_(vai_init_f)
639 99472
sml_ai_init(struct worker *wrk, struct objcore *oc, struct ws *ws,
640
    vai_notify_cb *notify, void *notify_priv)
641
{
642
        struct sml_hdl *hdl;
643 99472
        const size_t sz = sizeof *hdl;
644
645 99472
        if (ws != NULL && WS_ReserveSize(ws, (unsigned)sz))
646 1280
                hdl = WS_Reservation(ws);
647
        else {
648 98192
                hdl = malloc(sz);
649 98192
                ws = NULL;
650
        }
651
652 99472
        AN(hdl);
653 99472
        INIT_VAI_HDL(hdl, SML_HDL_MAGIC);
654 99472
        hdl->preamble.vai_lease = sml_ai_lease_simple;
655 99472
        hdl->preamble.vai_buffer = sml_ai_buffer;
656 99472
        hdl->preamble.vai_return = sml_ai_return_buffers;
657 99472
        hdl->preamble.vai_fini = sml_ai_fini;
658 99472
        hdl->ws = ws;
659
660 99472
        hdl->oc = oc;
661 99472
        hdl->obj = sml_getobj(wrk, oc);
662 99472
        CHECK_OBJ_NOTNULL(hdl->obj, OBJECT_MAGIC);
663 99472
        hdl->stv = oc->stobj->stevedore;
664 99472
        CHECK_OBJ_NOTNULL(hdl->stv, STEVEDORE_MAGIC);
665
666
667 99472
        hdl->qe.magic = VAI_Q_MAGIC;
668 99472
        hdl->qe.cb = notify;
669 99472
        hdl->qe.hdl = hdl;
670 99472
        hdl->qe.priv = notify_priv;
671
672 99472
        hdl->boc = HSH_RefBoc(oc);
673 99472
        if (hdl->boc == NULL) {
674 68393
                hdl->st = VTAILQ_LAST(&hdl->obj->list, storagehead);
675 68393
                CHECK_OBJ_ORNULL(hdl->st, STORAGE_MAGIC);
676 68393
                return (hdl);
677
        }
678
        /* we only initialize notifications if we have a boc, so
679
         * any wrong attempt triggers magic checks.
680
         */
681 31079
        hdl->preamble.vai_lease = sml_ai_lease_boc;
682 31079
        if ((hdl->oc->flags & OC_F_TRANSIENT) != 0)
683 13714
                hdl->preamble.vai_return = sml_ai_return;
684 31079
        return (hdl);
685 99472
}
686
687
/*
688
 * trivial notification to allow the iterator to simply block
689
 */
690
struct sml_notify {
691
        unsigned                magic;
692
#define SML_NOTIFY_MAGIC        0x4589af31
693
        unsigned                hasmore;
694
        pthread_mutex_t         mtx;
695
        pthread_cond_t          cond;
696
};
697
698
static void
699 98192
sml_notify_init(struct sml_notify *sn)
700
{
701
702 98192
        INIT_OBJ(sn, SML_NOTIFY_MAGIC);
703 98192
        AZ(pthread_mutex_init(&sn->mtx, NULL));
704 98192
        AZ(pthread_cond_init(&sn->cond, NULL));
705 98192
}
706
707
static void
708 98184
sml_notify_fini(struct sml_notify *sn)
709
{
710
711 98184
        CHECK_OBJ_NOTNULL(sn, SML_NOTIFY_MAGIC);
712 98184
        AZ(pthread_mutex_destroy(&sn->mtx));
713 98184
        AZ(pthread_cond_destroy(&sn->cond));
714 98184
}
715
716
static void v_matchproto_(vai_notify_cb)
717 53586
sml_notify(vai_hdl hdl, void *priv)
718
{
719
        struct sml_notify *sn;
720
721 53586
        (void) hdl;
722 53586
        CAST_OBJ_NOTNULL(sn, priv, SML_NOTIFY_MAGIC);
723 53586
        AZ(pthread_mutex_lock(&sn->mtx));
724 53586
        sn->hasmore = 1;
725 53586
        AZ(pthread_cond_signal(&sn->cond));
726 53586
        AZ(pthread_mutex_unlock(&sn->mtx));
727
728 53586
}
729
730
static void
731 52607
sml_notify_wait(struct sml_notify *sn)
732
{
733
734 52607
        CHECK_OBJ_NOTNULL(sn, SML_NOTIFY_MAGIC);
735 52607
        AZ(pthread_mutex_lock(&sn->mtx));
736 90233
        while (sn->hasmore == 0)
737 37626
                AZ(pthread_cond_wait(&sn->cond, &sn->mtx));
738 52607
        AN(sn->hasmore);
739 52607
        sn->hasmore = 0;
740 52607
        AZ(pthread_mutex_unlock(&sn->mtx));
741 52607
}
742
743
static int v_matchproto_(objiterator_f)
744 98198
sml_iterator(struct worker *wrk, struct objcore *oc,
745
    void *priv, objiterate_f *func, int final)
746
{
747
        struct sml_notify sn;
748
        struct viov *vio, *last;
749
        unsigned u, uu;
750
        vai_hdl hdl;
751
        int nn, r, r2, islast;
752
753 98198
        VSCARAB_LOCAL(scarab, 16);
754 98198
        VSCARET_LOCAL(scaret, 16);
755
756 98198
        (void) final; // phase out?
757 98198
        sml_notify_init(&sn);
758 98198
        hdl = ObjVAIinit(wrk, oc, NULL, sml_notify, &sn);
759 98198
        AN(hdl);
760
761 98198
        r = u = 0;
762
763 98198
        do {
764 216958
                do {
765 268566
                        nn = ObjVAIlease(wrk, hdl, scarab);
766 268566
                        if (nn <= 0 || scarab->flags & VSCARAB_F_END)
767 216919
                                break;
768 51647
                } while (scarab->used < scarab->capacity);
769
770
                /*
771
                 * nn is the wait/return action or 0
772
                 * nn tells us if to flush
773
                 */
774 216958
                uu = u;
775 216958
                last = VSCARAB_LAST(scarab);
776 354596
                VSCARAB_FOREACH(vio, scarab) {
777 139770
                        islast = vio == last;
778 139770
                        AZ(u & OBJ_ITER_END);
779 139770
                        if (islast && scarab->flags & VSCARAB_F_END)
780 67519
                                u |= OBJ_ITER_END;
781
782
                        // flush if it is the scarab's last IOV and we will block next
783
                        // or if we need space in the return leases array
784 139770
                        uu = u;
785 139770
                        if ((islast && nn < 0) || scaret->used == scaret->capacity - 1)
786 49925
                                uu |= OBJ_ITER_FLUSH;
787
788
                        // null iov with the only purpose to return the resume ptr lease
789
                        // exception needed because assert(len > 0) in VDP_bytes()
790 139770
                        if (vio->iov.iov_base == null_iov)
791 6515
                                r = 0;
792
                        else
793 133255
                                r = func(priv, uu, vio->iov.iov_base, vio->iov.iov_len);
794 139770
                        if (r != 0)
795 2132
                                break;
796
797
                        // sufficient space ensured by capacity check above
798 137638
                        VSCARET_ADD(scaret, vio->lease);
799
800
#ifdef VAI_DBG
801
                        if (wrk->vsl)
802
                                VSLb(wrk->vsl, SLT_Debug, "len %zu scaret %u uu %u",
803
                                    vio->iov.iov_len, scaret->used, uu);
804
#endif
805
806
                        // whenever we have flushed, return leases
807 137638
                        if ((uu & OBJ_ITER_FLUSH) && scaret->used > 0)
808 49246
                                ObjVAIreturn(wrk, hdl, scaret);
809 137638
                }
810
811
                // return leases which we did not use if error (break)
812 219145
                VSCARAB_FOREACH_RESUME(vio, scarab) {
813 2187
                        if (scaret->used == scaret->capacity)
814 0
                                ObjVAIreturn(wrk, hdl, scaret);
815 2187
                        VSCARET_ADD(scaret, vio->lease);
816 2187
                }
817
818
                // we have now completed the scarab
819 216958
                VSCARAB_INIT(scarab, scarab->capacity);
820
821
#ifdef VAI_DBG
822
                if (wrk->vsl)
823
                        VSLb(wrk->vsl, SLT_Debug, "r %d nn %d uu %u",
824
                            r, nn, uu);
825
#endif
826
827
                // flush before blocking if we did not already
828 216958
                if (r == 0 && (nn == -ENOBUFS || nn == -EAGAIN) &&
829 214847
                    (uu & OBJ_ITER_FLUSH) == 0) {
830 3408
                        r = func(priv, OBJ_ITER_FLUSH, NULL, 0);
831 3408
                        if (scaret->used > 0)
832 0
                                ObjVAIreturn(wrk, hdl, scaret);
833 3408
                }
834
835 216958
                if (r == 0 && (nn == -ENOBUFS || nn == -EAGAIN)) {
836 52611
                        assert(scaret->used <= 1);
837 52611
                        sml_notify_wait(&sn);
838 52611
                }
839 164347
                else if (r == 0 && nn < 0)
840 639
                        r = -1;
841 216958
        } while (nn != 0 && r == 0);
842
843 98198
        if ((u & OBJ_ITER_END) == 0) {
844 30676
                r2 = func(priv, OBJ_ITER_END, NULL, 0);
845 30676
                if (r == 0)
846 29288
                        r = r2;
847 30676
        }
848
849 98198
        if (scaret->used > 0)
850 68269
                ObjVAIreturn(wrk, hdl, scaret);
851
852 98198
        ObjVAIfini(wrk, &hdl);
853 98198
        sml_notify_fini(&sn);
854
855 98198
        return (r);
856
}
857
858
/*--------------------------------------------------------------------
859
 */
860
861
static struct storage *
862 130872
objallocwithnuke(struct worker *wrk, const struct stevedore *stv, ssize_t size,
863
    int flags)
864
{
865 130872
        struct storage *st = NULL;
866
867 130872
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
868 130872
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
869
870 130872
        if (size > cache_param->fetch_maxchunksize) {
871 80
                if (!(flags & LESS_MEM_ALLOCED_IS_OK))
872 0
                        return (NULL);
873 80
                size = cache_param->fetch_maxchunksize;
874 80
        }
875
876 130872
        assert(size <= UINT_MAX);       /* field limit in struct storage */
877
878 130872
        do {
879
                /* try to allocate from it */
880 131192
                st = sml_stv_alloc(stv, size, flags);
881 131192
                if (st != NULL)
882 130673
                        break;
883
884
                /* no luck; try to free some space and keep trying */
885 519
                if (stv->lru == NULL)
886 0
                        break;
887 519
        } while (LRU_NukeOne(wrk, stv->lru));
888
889 130872
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
890 130872
        return (st);
891 130872
}
892
893
static int v_matchproto_(objgetspace_f)
894 2322889
sml_getspace(struct worker *wrk, struct objcore *oc, ssize_t *sz,
895
    uint8_t **ptr)
896
{
897
        struct object *o;
898
        struct storage *st;
899
900 2322889
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
901 2322889
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
902 2322889
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
903 2322889
        AN(sz);
904 2322889
        AN(ptr);
905 2322889
        if (*sz == 0)
906 2177244
                *sz = cache_param->fetch_chunksize;
907 2322889
        assert(*sz > 0);
908 2322889
        if (oc->boc->transit_buffer > 0)
909 19791
                *sz = vmin_t(ssize_t, *sz, oc->boc->transit_buffer);
910
911 2322889
        o = sml_getobj(wrk, oc);
912 2322889
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
913 2322889
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
914
915 2322889
        st = VTAILQ_FIRST(&o->list);
916 2322889
        if (st != NULL && st->len < st->space) {
917 2209100
                *sz = st->space - st->len;
918 2209100
                *ptr = st->ptr + st->len;
919 2209100
                assert (*sz > 0);
920 2209100
                return (1);
921
        }
922
923 113789
        st = objallocwithnuke(wrk, oc->stobj->stevedore, *sz,
924
            LESS_MEM_ALLOCED_IS_OK);
925 113789
        if (st == NULL)
926 199
                return (0);
927
928 113590
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
929 113590
        Lck_Lock(&oc->boc->mtx);
930 113590
        VTAILQ_INSERT_HEAD(&o->list, st, list);
931 113590
        Lck_Unlock(&oc->boc->mtx);
932
933 113590
        *sz = st->space - st->len;
934 113590
        assert (*sz > 0);
935 113590
        *ptr = st->ptr + st->len;
936 113590
        return (1);
937 2322889
}
938
939
static void v_matchproto_(objextend_f)
940 2264478
sml_extend(struct worker *wrk, struct objcore *oc, ssize_t l)
941
{
942
        struct object *o;
943
        struct storage *st;
944
945 2264478
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
946 2264478
        assert(l > 0);
947
948 2264478
        o = sml_getobj(wrk, oc);
949 2264478
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
950 2264478
        st = VTAILQ_FIRST(&o->list);
951 2264478
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
952 2264478
        assert(st->len + l <= st->space);
953 2264478
        st->len += l;
954 2264478
}
955
956
static void v_matchproto_(objtrimstore_f)
957 76487
sml_trimstore(struct worker *wrk, struct objcore *oc)
958
{
959
        const struct stevedore *stv;
960
        struct storage *st, *st1;
961
        struct object *o;
962
963 76487
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
964 76487
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
965 76487
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
966
967 76487
        stv = oc->stobj->stevedore;
968 76487
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
969
970 76487
        if (oc->boc->stevedore_priv != NULL)
971 0
                WRONG("sml_trimstore already called");
972 76487
        oc->boc->stevedore_priv = trim_once;
973
974 76487
        if (stv->sml_free == NULL)
975 0
                return;
976
977 76487
        o = sml_getobj(wrk, oc);
978 76487
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
979 76487
        st = VTAILQ_FIRST(&o->list);
980
981 76487
        if (st == NULL)
982 0
                return;
983
984 76487
        if (st->len == 0) {
985 320
                Lck_Lock(&oc->boc->mtx);
986 320
                VTAILQ_REMOVE(&o->list, st, list);
987 320
                Lck_Unlock(&oc->boc->mtx);
988
                /* sml_bocdone frees this */
989 320
                oc->boc->stevedore_priv = st;
990 320
                return;
991
        }
992
993 76167
        if (st->space - st->len < 512)
994 68279
                return;
995
996 7888
        st1 = sml_stv_alloc(stv, st->len, 0);
997 7888
        if (st1 == NULL)
998 0
                return;
999 7888
        assert(st1->space >= st->len);
1000
1001 7888
        memcpy(st1->ptr, st->ptr, st->len);
1002 7888
        st1->len = st->len;
1003 7888
        Lck_Lock(&oc->boc->mtx);
1004 7888
        VTAILQ_REMOVE(&o->list, st, list);
1005 7888
        VTAILQ_INSERT_HEAD(&o->list, st1, list);
1006 7888
        Lck_Unlock(&oc->boc->mtx);
1007
        /* sml_bocdone frees this */
1008 7888
        oc->boc->stevedore_priv = st;
1009 76487
}
1010
1011
static void v_matchproto_(objbocdone_f)
1012 115116
sml_bocdone(struct worker *wrk, struct objcore *oc, struct boc *boc)
1013
{
1014
        const struct stevedore *stv;
1015
1016 115116
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1017 115116
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1018 115116
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1019 115116
        stv = oc->stobj->stevedore;
1020 115116
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
1021
1022 115116
        sml_bocfini(stv, boc);
1023
1024 115116
        if (stv->lru != NULL) {
1025 113556
                if (isnan(wrk->lastused))
1026 0
                        wrk->lastused = VTIM_real();
1027 113556
                LRU_Add(oc, wrk->lastused);     // approx timestamp is OK
1028 113556
        }
1029 115116
}
1030
1031
static const void * v_matchproto_(objgetattr_f)
1032 906775
sml_getattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
1033
   ssize_t *len)
1034
{
1035
        struct object *o;
1036
        ssize_t dummy;
1037
1038 906775
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1039
1040 906775
        if (len == NULL)
1041 555161
                len = &dummy;
1042 906775
        o = sml_getobj(wrk, oc);
1043 906775
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
1044
1045 906775
        switch (attr) {
1046
                /* Fixed size attributes */
1047
#define OBJ_FIXATTR(U, l, s)                                            \
1048
        case OA_##U:                                                    \
1049
                *len = sizeof o->fa_##l;                                \
1050
                return (o->fa_##l);
1051
#include "tbl/obj_attr.h"
1052
1053
                /* Variable size attributes */
1054
#define OBJ_VARATTR(U, l)                                               \
1055
        case OA_##U:                                                    \
1056
                if (o->va_##l == NULL)                                  \
1057
                        return (NULL);                                  \
1058
                *len = o->va_##l##_len;                                 \
1059
                return (o->va_##l);
1060
#include "tbl/obj_attr.h"
1061
1062
                /* Auxiliary attributes */
1063
#define OBJ_AUXATTR(U, l)                                               \
1064
        case OA_##U:                                                    \
1065
                if (o->aa_##l == NULL)                                  \
1066
                        return (NULL);                                  \
1067
                CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);            \
1068
                *len = o->aa_##l->len;                                  \
1069
                return (o->aa_##l->ptr);
1070
#include "tbl/obj_attr.h"
1071
1072
        default:
1073
                break;
1074
        }
1075 0
        WRONG("Unsupported OBJ_ATTR");
1076 906775
}
1077
1078
static void * v_matchproto_(objsetattr_f)
1079 429473
sml_setattr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
1080
    ssize_t len, const void *ptr)
1081
{
1082
        struct object *o;
1083 429473
        void *retval = NULL;
1084
        struct storage *st;
1085
1086 429473
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1087
1088 429473
        o = sml_getobj(wrk, oc);
1089 429473
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
1090 429473
        st = o->objstore;
1091
1092 429473
        switch (attr) {
1093
                /* Fixed size attributes */
1094
#define OBJ_FIXATTR(U, l, s)                                            \
1095
        case OA_##U:                                                    \
1096
                assert(len == sizeof o->fa_##l);                        \
1097
                retval = o->fa_##l;                                     \
1098
                break;
1099
#include "tbl/obj_attr.h"
1100
1101
                /* Variable size attributes */
1102
#define OBJ_VARATTR(U, l)                                               \
1103
        case OA_##U:                                                    \
1104
                if (o->va_##l##_len > 0) {                              \
1105
                        AN(o->va_##l);                                  \
1106
                        assert(len == o->va_##l##_len);                 \
1107
                        retval = o->va_##l;                             \
1108
                } else if (len > 0) {                                   \
1109
                        assert(len <= UINT_MAX);                        \
1110
                        assert(st->len + len <= st->space);             \
1111
                        o->va_##l = st->ptr + st->len;                  \
1112
                        st->len += len;                                 \
1113
                        o->va_##l##_len = len;                          \
1114
                        retval = o->va_##l;                             \
1115
                }                                                       \
1116
                break;
1117
#include "tbl/obj_attr.h"
1118
1119
                /* Auxiliary attributes */
1120
#define OBJ_AUXATTR(U, l)                                               \
1121
        case OA_##U:                                                    \
1122
                if (o->aa_##l != NULL) {                                \
1123
                        CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);    \
1124
                        assert(len == o->aa_##l->len);                  \
1125
                        retval = o->aa_##l->ptr;                        \
1126
                        break;                                          \
1127
                }                                                       \
1128
                if (len == 0)                                           \
1129
                        break;                                          \
1130
                o->aa_##l = objallocwithnuke(wrk, oc->stobj->stevedore, \
1131
                    len, 0);                                            \
1132
                if (o->aa_##l == NULL)                                  \
1133
                        break;                                          \
1134
                CHECK_OBJ_NOTNULL(o->aa_##l, STORAGE_MAGIC);            \
1135
                assert(len <= o->aa_##l->space);                        \
1136
                o->aa_##l->len = len;                                   \
1137
                retval = o->aa_##l->ptr;                                \
1138
                break;
1139
#include "tbl/obj_attr.h"
1140
1141
        default:
1142 0
                WRONG("Unsupported OBJ_ATTR");
1143
                break;
1144
        }
1145
1146 429473
        if (retval != NULL && ptr != NULL)
1147 15680
                memcpy(retval, ptr, len);
1148 429477
        return (retval);
1149
}
1150
1151
const struct obj_methods SML_methods = {
1152
        .objfree        = sml_objfree,
1153
        .objiterator    = sml_iterator,
1154
        .objgetspace    = sml_getspace,
1155
        .objextend      = sml_extend,
1156
        .objtrimstore   = sml_trimstore,
1157
        .objbocdone     = sml_bocdone,
1158
        .objslim        = sml_slim,
1159
        .objgetattr     = sml_getattr,
1160
        .objsetattr     = sml_setattr,
1161
        .objtouch       = LRU_Touch,
1162
        .vai_init       = sml_ai_init
1163
};
1164
1165
static void
1166 160
sml_panic_st(struct vsb *vsb, const char *hd, const struct storage *st)
1167
{
1168 320
        VSB_printf(vsb, "%s = %p {priv=%p, ptr=%p, len=%u, space=%u},\n",
1169 160
            hd, st, st->priv, st->ptr, st->len, st->space);
1170 160
}
1171
1172
void
1173 80
SML_panic(struct vsb *vsb, const struct objcore *oc)
1174
{
1175
        struct object *o;
1176
        struct storage *st;
1177
1178 80
        VSB_printf(vsb, "Simple = %p,\n", oc->stobj->priv);
1179 80
        if (oc->stobj->priv == NULL)
1180 0
                return;
1181 80
        o = oc->stobj->priv;
1182 80
        PAN_CheckMagic(vsb, o, OBJECT_MAGIC);
1183 80
        sml_panic_st(vsb, "Obj", o->objstore);
1184
1185
#define OBJ_FIXATTR(U, l, sz) \
1186
        VSB_printf(vsb, "%s = ", #U); \
1187
        VSB_quote(vsb, (const void*)o->fa_##l, sz, VSB_QUOTE_HEX); \
1188
        VSB_printf(vsb, ",\n");
1189
1190
#define OBJ_VARATTR(U, l) \
1191
        VSB_printf(vsb, "%s = {len=%u, ptr=%p},\n", \
1192
            #U, o->va_##l##_len, o->va_##l);
1193
1194
#define OBJ_AUXATTR(U, l)                                               \
1195
        do {                                                            \
1196
                if (o->aa_##l != NULL) sml_panic_st(vsb, #U, o->aa_##l);\
1197
        } while(0);
1198
1199
#include "tbl/obj_attr.h"
1200
1201 160
        VTAILQ_FOREACH(st, &o->list, list) {
1202 80
                sml_panic_st(vsb, "Body", st);
1203 80
        }
1204
}