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 38822
#include "cache/cache_varnishd.h"
36 31682
37 7403
#include "cache/cache_obj.h"
38 5085
#include "cache/cache_objhead.h"
39 31682
40
#include "storage/storage.h"
41
#include "storage/storage_simple.h"
42
43
#include "vtim.h"
44
45 42700
/* Flags for allocating memory in sml_stv_alloc */
46 83290
#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 56385
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 48858
sml_stv_alloc(const struct stevedore *stv, ssize_t size, int flags)
61
{
62
        struct storage *st;
63
64 48858
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
65 48858
        AN(stv->sml_alloc);
66
67 48858
        if (!(flags & LESS_MEM_ALLOCED_IS_OK)) {
68 8770
                if (size > cache_param->fetch_maxchunksize)
69 0
                        return (NULL);
70
                else
71 8770
                        return (stv->sml_alloc(stv, size));
72
        }
73
74 40088
        if (size > cache_param->fetch_maxchunksize)
75 0
                size = cache_param->fetch_maxchunksize;
76
77 40088
        assert(size <= UINT_MAX);       /* field limit in struct storage */
78
79 41978
        for (;;) {
80
                /* try to allocate from it */
81 41978
                assert(size > 0);
82 41978
                st = stv->sml_alloc(stv, size);
83 41978
                if (st != NULL)
84 39934
                        break;
85
86 2044
                if (size <= cache_param->fetch_chunksize)
87 154
                        break;
88
89 1890
                size /= 2;
90
        }
91 40088
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
92 40088
        return (st);
93 48858
}
94
95
static void
96 63019
sml_stv_free(const struct stevedore *stv, struct storage *st)
97
{
98
99 63019
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
100 63019
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
101 63019
        if (stv->sml_free != NULL)
102 63020
                stv->sml_free(st);
103 63021
}
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 40570
SML_MkObject(const struct stevedore *stv, struct objcore *oc, void *ptr)
115
{
116
        struct object *o;
117
118 40570
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
119 40570
        AN(stv->methods);
120 40570
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
121
122 40570
        assert(PAOK(ptr));
123
124 40570
        o = ptr;
125 40570
        INIT_OBJ(o, OBJECT_MAGIC);
126
127 40570
        VTAILQ_INIT(&o->list);
128
129 40570
        oc->stobj->stevedore = stv;
130 40570
        oc->stobj->priv = o;
131 40570
        oc->stobj->priv2 = 0;
132 40570
        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 40416
SML_allocobj(struct worker *wrk, const struct stevedore *stv,
142
    struct objcore *oc, unsigned wsl)
143
{
144
        struct object *o;
145 40416
        struct storage *st = NULL;
146
        unsigned ltot;
147
148 40416
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
149 40416
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
150 40416
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
151
152 40416
        AN(stv->sml_alloc);
153
154 40416
        ltot = sizeof(*o) + PRNDUP(wsl);
155
156 40416
        do {
157 40458
                st = stv->sml_alloc(stv, ltot);
158 40458
                if (st != NULL && st->space < ltot) {
159 0
                        stv->sml_free(st);
160 0
                        st = NULL;
161 0
                }
162 40458
        } while (st == NULL && LRU_NukeOne(wrk, stv->lru));
163 40416
        if (st == NULL)
164 154
                return (0);
165
166 40262
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
167 40262
        o = SML_MkObject(stv, oc, st->ptr);
168 40262
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
169 40262
        st->len = sizeof(*o);
170 40262
        o->objstore = st;
171 40262
        return (1);
172 40416
}
173
174
void * v_matchproto_(storage_allocbuf_t)
175 4116
SML_AllocBuf(struct worker *wrk, const struct stevedore *stv, size_t size,
176
    uintptr_t *ppriv)
177
{
178
        struct storage *st;
179
180 4116
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
181 4116
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
182 4116
        AN(ppriv);
183
184 4116
        if (size > UINT_MAX)
185 0
                return (NULL);
186 4116
        st = objallocwithnuke(wrk, stv, size, 0);
187 4116
        if (st == NULL)
188 0
                return (NULL);
189 4116
        assert(st->space >= size);
190 4116
        st->flags = STORAGE_F_BUFFER;
191 4116
        st->len = size;
192 4116
        *ppriv = (uintptr_t)st;
193 4116
        return (st->ptr);
194 4116
}
195
196
void v_matchproto_(storage_freebuf_t)
197 4116
SML_FreeBuf(struct worker *wrk, const struct stevedore *stv, uintptr_t priv)
198
{
199
        struct storage *st;
200
201 4116
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
202 4116
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
203
204 4116
        CAST_OBJ_NOTNULL(st, (void *)priv, STORAGE_MAGIC);
205 4116
        assert(st->flags == STORAGE_F_BUFFER);
206 4116
        sml_stv_free(stv, st);
207 4116
}
208
209
/*---------------------------------------------------------------------
210
 */
211
212
static struct object *
213 2183463
sml_getobj(struct worker *wrk, struct objcore *oc)
214
{
215
        const struct stevedore *stv;
216
        struct object *o;
217
218 2183463
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
219 2183463
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
220 2183463
        stv = oc->stobj->stevedore;
221 2183463
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
222 2183463
        if (stv->sml_getobj != NULL)
223 4102
                return (stv->sml_getobj(wrk, oc));
224 2179361
        if (oc->stobj->priv == NULL)
225 0
                return (NULL);
226 2179361
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
227 2179361
        return (o);
228 2183463
}
229
230
static void v_matchproto_(objslim_f)
231 45509
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 45509
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
238
239 45509
        stv = oc->stobj->stevedore;
240 45509
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
241 45509
        o = sml_getobj(wrk, oc);
242 45509
        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 68328
        VTAILQ_FOREACH_SAFE(st, &o->list, list, stn) {
254 22819
                CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
255 22819
                VTAILQ_REMOVE(&o->list, st, list);
256 22819
                sml_stv_free(stv, st);
257 22819
        }
258
}
259
260
static void
261 40792
sml_bocfini(const struct stevedore *stv, struct boc *boc)
262
{
263
        struct storage *st;
264
265 40792
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
266 40792
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
267
268 40792
        if (boc->stevedore_priv == NULL ||
269 26912
            boc->stevedore_priv == trim_once)
270 37916
                return;
271
272
        /* Free any leftovers from Trim */
273 2876
        TAKE_OBJ_NOTNULL(st, &boc->stevedore_priv, STORAGE_MAGIC);
274 2876
        sml_stv_free(stv, st);
275 40792
}
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 25573
sml_objfree(struct worker *wrk, struct objcore *oc)
284
{
285
        const struct stevedore *stv;
286
        struct storage *st;
287
        struct object *o;
288
289 25573
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
290 25573
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
291 25573
        stv = oc->stobj->stevedore;
292 25573
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
293 25573
        CAST_OBJ_NOTNULL(o, oc->stobj->priv, OBJECT_MAGIC);
294
295 25573
        sml_slim(wrk, oc);
296 25573
        st = o->objstore;
297 25573
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
298 25573
        FINI_OBJ(o);
299
300 25573
        if (oc->boc != NULL)
301 294
                sml_bocfini(stv, oc->boc);
302 25279
        else if (stv->lru != NULL)
303 25279
                LRU_Remove(oc);
304
305 25573
        sml_stv_free(stv, st);
306
307 25573
        memset(oc->stobj, 0, sizeof oc->stobj);
308
309 25573
        wrk->stats->n_object--;
310 25573
}
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 26683
sml_ai_viov_fill(struct viov *viov, struct storage *st)
336
{
337 26683
        viov->iov.iov_base = TRUST_ME(st->ptr);
338 26683
        viov->iov.iov_len = st->len;
339 26683
        viov->lease = ptr2lease(st);
340 26683
        VAI_ASSERT_LEASE(viov->lease);
341 26683
}
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 224
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 224
        int r = 0;
381
382 224
        (void) wrk;
383 224
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
384 224
        stv = hdl->stv;
385 224
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
386
387 447
        VSCARAB_FOREACH(vio, scarab)
388 223
                if (vio->iov.iov_len > UINT_MAX)
389 0
                        return (-EINVAL);
390
391 448
        VSCARAB_FOREACH(vio, scarab) {
392 224
                st = objallocwithnuke(wrk, stv, vio->iov.iov_len, 0);
393 224
                if (st == NULL)
394 0
                        break;
395 224
                assert(st->space >= vio->iov.iov_len);
396 224
                st->flags = STORAGE_F_BUFFER;
397 224
                st->len = st->space;
398
399 224
                sml_ai_viov_fill(vio, st);
400 224
                r++;
401 224
        }
402 224
        if (r == 0) {
403 0
                sml_ai_later(wrk, hdl);
404 0
                r = -EAGAIN;
405 0
        }
406 224
        return (r);
407 224
}
408
409
static int
410 47311
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 47311
        int r = 0;
416
417 47311
        (void) wrk;
418 47311
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
419 47311
        VSCARAB_CHECK_NOTNULL(scarab);
420
421 47311
        AZ(hdl->st_off);
422 47311
        st = hdl->st;
423 73769
        while (st != NULL && (viov = VSCARAB_GET(scarab)) != NULL) {
424 26458
                CHECK_OBJ(st, STORAGE_MAGIC);
425 26458
                sml_ai_viov_fill(viov, st);
426 26458
                r++;
427 26458
                st = VTAILQ_PREV(st, storagehead, list);
428
        }
429 47311
        hdl->st = st;
430 47311
        if (st == NULL)
431 47087
                scarab->flags |= VSCARAB_F_END;
432 47311
        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 47586
sml_ai_lease_boc(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
449
{
450 47586
        enum boc_state_e state = BOS_INVALID;
451
        struct storage *next;
452
        struct sml_hdl *hdl;
453
        struct viov *viov;
454 47586
        int r = 0;
455
456 47586
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
457 47586
        VSCARAB_CHECK_NOTNULL(scarab);
458 47586
        assert(hdl->boc == hdl->oc->boc);
459
460 47586
        if (hdl->avail == hdl->returned) {
461 94606
                hdl->avail = ObjVAIGetExtend(wrk, hdl->oc, hdl->returned,
462 47303
                    &state, &hdl->qe);
463 47303
                assert(state >= BOS_STREAM);
464 47303
                if (state == BOS_FAILED) {
465 224
                        hdl->last = NULL;
466 224
                        return (-EPIPE);
467
                }
468 47079
                else if (state == BOS_FINISHED)
469 10598
                        (void)0;
470 36481
                else if (hdl->avail == hdl->returned) {
471
                        // ObjVAIGetExtend() has scheduled a notification
472 18727
                        if (hdl->boc->transit_buffer > 0)
473 4176
                                return (-ENOBUFS);
474
                        else
475 14551
                                return (-EAGAIN);
476
                }
477
                else
478 17754
                        assert(state < BOS_FINISHED);
479 28352
        }
480 28635
        Lck_Lock(&hdl->boc->mtx);
481 28635
        if (hdl->st == NULL && hdl->last != NULL)
482 12280
                hdl->st = VTAILQ_PREV(hdl->last, storagehead, list);
483 28635
        if (hdl->last != NULL && state < BOS_FINISHED) {
484 2234
                viov = VSCARAB_GET(scarab);
485 2234
                AN(viov);
486 2234
                viov->iov.iov_base = null_iov;
487 2234
                viov->iov.iov_len = 0;
488 2234
                viov->lease = ptr2lease(hdl->last);
489 2234
                r++;
490 2234
        }
491 28635
        if (hdl->last != NULL)
492 12280
                hdl->last = NULL;
493 28635
        if (hdl->st == NULL && hdl->returned == 0)
494 10914
                hdl->st = VTAILQ_LAST(&hdl->obj->list, storagehead);
495 28635
        if (hdl->st == NULL)
496 10026
                assert(hdl->avail == hdl->returned);
497
498 49411
        while (hdl->avail > hdl->returned && (viov = VSCARAB_GET(scarab)) != NULL) {
499 20776
                CHECK_OBJ_NOTNULL(hdl->st, STORAGE_MAGIC); // ObjVAIGetExtend ensures
500 20776
                assert(hdl->boc == hdl->oc->boc);
501 20776
                assert(hdl->st_off <= hdl->st->space);
502 20776
                size_t av = hdl->avail - hdl->returned;
503 20776
                size_t l = hdl->st->space - hdl->st_off;
504 20776
                AN(l);
505 20776
                if (l > av)
506 2763
                        l = av;
507 20776
                viov->iov.iov_base = TRUST_ME(hdl->st->ptr + hdl->st_off);
508 20776
                viov->iov.iov_len = l;
509 20776
                if (hdl->st_off + l == hdl->st->space) {
510 18013
                        next = VTAILQ_PREV(hdl->st, storagehead, list);
511 18013
                        AZ(hdl->last);
512 18013
                        if (next == NULL) {
513 12422
                                hdl->last = hdl->st;
514 12422
                                viov->lease = VAI_LEASE_NORET;
515 12422
                        }
516
                        else {
517 5591
                                CHECK_OBJ(next, STORAGE_MAGIC);
518 5591
                                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 18013
                        hdl->st_off = 0;
526 18013
                        hdl->st = next;
527 18013
                }
528
                else {
529 2763
                        viov->lease = VAI_LEASE_NORET;
530 2763
                        hdl->st_off += l;
531
                }
532 20776
                hdl->returned += l;
533 20776
                VAI_ASSERT_LEASE(viov->lease);
534 20776
                r++;
535
        }
536
537 28635
        Lck_Unlock(&hdl->boc->mtx);
538 28635
        if (state != BOS_FINISHED && hdl->avail == hdl->returned) {
539 36076
                hdl->avail = ObjVAIGetExtend(wrk, hdl->oc, hdl->returned,
540 18038
                    &state, &hdl->qe);
541 18038
        }
542 28635
        if (state == BOS_FINISHED && hdl->avail == hdl->returned)
543 10600
                scarab->flags |= VSCARAB_F_END;
544 28635
        return (r);
545 47586
}
546
547
// return only buffers, used if object is not streaming
548
static void v_matchproto_(vai_return_f)
549 32777
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 32777
        (void) wrk;
556 32777
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
557
558 68562
        VSCARET_FOREACH(p, scaret) {
559 35785
                if (*p == VAI_LEASE_NORET)
560 8521
                        continue;
561 27264
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
562 27264
                if ((st->flags & STORAGE_F_BUFFER) == 0)
563 27040
                        continue;
564 224
                sml_stv_free(hdl->stv, st);
565 224
        }
566 32777
        VSCARET_INIT(scaret, scaret->capacity);
567 32777
}
568
569
// generic return for buffers and object leases, used when streaming
570
static void v_matchproto_(vai_return_f)
571 9575
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 9575
        (void) wrk;
578 9575
        CAST_VAI_HDL_NOTNULL(hdl, vhdl, SML_HDL_MAGIC);
579 9575
        VSCARET_CHECK_NOTNULL(scaret);
580 9575
        if (scaret->used == 0)
581 0
                return;
582
583
        // callback is only registered if needed
584 9575
        assert(hdl->boc != NULL && (hdl->oc->flags & OC_F_TRANSIENT) != 0);
585
586
        // filter noret and last
587 9575
        VSCARET_LOCAL(todo, scaret->used);
588 23931
        VSCARET_FOREACH(p, scaret) {
589 14356
                if (*p == VAI_LEASE_NORET)
590 7112
                        continue;
591 7244
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
592 7244
                VSCARET_ADD(todo, *p);
593 7244
        }
594 9575
        VSCARET_INIT(scaret, scaret->capacity);
595
596 9575
        Lck_Lock(&hdl->boc->mtx);
597 16819
        VSCARET_FOREACH(p, todo) {
598 7244
                CAST_OBJ_NOTNULL(st, lease2ptr(*p), STORAGE_MAGIC);
599 7244
                if ((st->flags & STORAGE_F_BUFFER) != 0)
600 0
                        continue;
601 7244
                VTAILQ_REMOVE(&hdl->obj->list, st, list);
602 7244
                if (st == hdl->boc->stevedore_priv)
603 0
                        hdl->boc->stevedore_priv = trim_once;
604 7244
        }
605 9575
        Lck_Unlock(&hdl->boc->mtx);
606
607 16820
        VSCARET_FOREACH(p, todo) {
608 7245
                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 7245
                sml_stv_free(hdl->stv, st);
614 7245
        }
615 9575
}
616
617
static void v_matchproto_(vai_fini_f)
618 34968
sml_ai_fini(struct worker *wrk, vai_hdl *vai_hdlp)
619
{
620
        struct sml_hdl *hdl;
621
622 34968
        AN(vai_hdlp);
623 34968
        CAST_VAI_HDL_NOTNULL(hdl, *vai_hdlp, SML_HDL_MAGIC);
624 34968
        *vai_hdlp = NULL;
625
626 34968
        if (hdl->boc != NULL) {
627 10973
                ObjVAICancel(wrk, hdl->boc, &hdl->qe);
628 10973
                HSH_DerefBoc(wrk, hdl->oc);
629 10973
                hdl->boc = NULL;
630 10973
        }
631
632 34968
        if (hdl->ws != NULL)
633 448
                WS_Release(hdl->ws, 0);
634
        else
635 34520
                free(hdl);
636 34968
}
637
638
static vai_hdl v_matchproto_(vai_init_f)
639 34971
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 34971
        const size_t sz = sizeof *hdl;
644
645 34971
        if (ws != NULL && WS_ReserveSize(ws, (unsigned)sz))
646 448
                hdl = WS_Reservation(ws);
647
        else {
648 34523
                hdl = malloc(sz);
649 34523
                ws = NULL;
650
        }
651
652 34971
        AN(hdl);
653 34971
        INIT_VAI_HDL(hdl, SML_HDL_MAGIC);
654 34971
        hdl->preamble.vai_lease = sml_ai_lease_simple;
655 34971
        hdl->preamble.vai_buffer = sml_ai_buffer;
656 34971
        hdl->preamble.vai_return = sml_ai_return_buffers;
657 34971
        hdl->preamble.vai_fini = sml_ai_fini;
658 34971
        hdl->ws = ws;
659
660 34971
        hdl->oc = oc;
661 34971
        hdl->obj = sml_getobj(wrk, oc);
662 34971
        CHECK_OBJ_NOTNULL(hdl->obj, OBJECT_MAGIC);
663 34971
        hdl->stv = oc->stobj->stevedore;
664 34971
        CHECK_OBJ_NOTNULL(hdl->stv, STEVEDORE_MAGIC);
665
666
667 34971
        hdl->qe.magic = VAI_Q_MAGIC;
668 34971
        hdl->qe.cb = notify;
669 34971
        hdl->qe.hdl = hdl;
670 34971
        hdl->qe.priv = notify_priv;
671
672 34971
        hdl->boc = HSH_RefBoc(oc);
673 34971
        if (hdl->boc == NULL) {
674 23994
                hdl->st = VTAILQ_LAST(&hdl->obj->list, storagehead);
675 23994
                CHECK_OBJ_ORNULL(hdl->st, STORAGE_MAGIC);
676 23994
                return (hdl);
677
        }
678
        /* we only initialize notifications if we have a boc, so
679
         * any wrong attempt triggers magic checks.
680
         */
681 10977
        hdl->preamble.vai_lease = sml_ai_lease_boc;
682 10977
        if ((hdl->oc->flags & OC_F_TRANSIENT) != 0)
683 4898
                hdl->preamble.vai_return = sml_ai_return;
684 10977
        return (hdl);
685 34971
}
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 34522
sml_notify_init(struct sml_notify *sn)
700
{
701
702 34522
        INIT_OBJ(sn, SML_NOTIFY_MAGIC);
703 34522
        AZ(pthread_mutex_init(&sn->mtx, NULL));
704 34522
        AZ(pthread_cond_init(&sn->cond, NULL));
705 34522
}
706
707
static void
708 34513
sml_notify_fini(struct sml_notify *sn)
709
{
710
711 34513
        CHECK_OBJ_NOTNULL(sn, SML_NOTIFY_MAGIC);
712 34513
        AZ(pthread_mutex_destroy(&sn->mtx));
713 34513
        AZ(pthread_cond_destroy(&sn->cond));
714 34513
}
715
716
static void v_matchproto_(vai_notify_cb)
717 18761
sml_notify(vai_hdl hdl, void *priv)
718
{
719
        struct sml_notify *sn;
720
721 18761
        (void) hdl;
722 18761
        CAST_OBJ_NOTNULL(sn, priv, SML_NOTIFY_MAGIC);
723 18761
        AZ(pthread_mutex_lock(&sn->mtx));
724 18761
        sn->hasmore = 1;
725 18761
        AZ(pthread_cond_signal(&sn->cond));
726 18761
        AZ(pthread_mutex_unlock(&sn->mtx));
727
728 18761
}
729
730
static void
731 18435
sml_notify_wait(struct sml_notify *sn)
732
{
733
734 18435
        CHECK_OBJ_NOTNULL(sn, SML_NOTIFY_MAGIC);
735 18435
        AZ(pthread_mutex_lock(&sn->mtx));
736 31606
        while (sn->hasmore == 0)
737 13171
                AZ(pthread_cond_wait(&sn->cond, &sn->mtx));
738 18435
        AN(sn->hasmore);
739 18435
        sn->hasmore = 0;
740 18435
        AZ(pthread_mutex_unlock(&sn->mtx));
741 18435
}
742
743
static int v_matchproto_(objiterator_f)
744 34523
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 34523
        VSCARAB_LOCAL(scarab, 16);
754 34523
        VSCARET_LOCAL(scaret, 16);
755
756 34523
        (void) final; // phase out?
757 34523
        sml_notify_init(&sn);
758 34523
        hdl = ObjVAIinit(wrk, oc, NULL, sml_notify, &sn);
759 34523
        AN(hdl);
760
761 34523
        r = u = 0;
762
763 34523
        do {
764 76161
                do {
765 94153
                        nn = ObjVAIlease(wrk, hdl, scarab);
766 94153
                        if (nn <= 0 || scarab->flags & VSCARAB_F_END)
767 76148
                                break;
768 18005
                } 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 76161
                uu = u;
775 76161
                last = VSCARAB_LAST(scarab);
776 124415
                VSCARAB_FOREACH(vio, scarab) {
777 49003
                        islast = vio == last;
778 49003
                        AZ(u & OBJ_ITER_END);
779 49003
                        if (islast && scarab->flags & VSCARAB_F_END)
780 23675
                                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 49003
                        uu = u;
785 49003
                        if ((islast && nn < 0) || scaret->used == scaret->capacity - 1)
786 17500
                                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 49003
                        if (vio->iov.iov_base == null_iov)
791 2234
                                r = 0;
792
                        else
793 46769
                                r = func(priv, uu, vio->iov.iov_base, vio->iov.iov_len);
794 49003
                        if (r != 0)
795 749
                                break;
796
797
                        // sufficient space ensured by capacity check above
798 48254
                        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 48254
                        if ((uu & OBJ_ITER_FLUSH) && scaret->used > 0)
808 17253
                                ObjVAIreturn(wrk, hdl, scaret);
809 48254
                }
810
811
                // return leases which we did not use if error (break)
812 76925
                VSCARAB_FOREACH_RESUME(vio, scarab) {
813 764
                        if (scaret->used == scaret->capacity)
814 0
                                ObjVAIreturn(wrk, hdl, scaret);
815 764
                        VSCARET_ADD(scaret, vio->lease);
816 764
                }
817
818
                // we have now completed the scarab
819 76161
                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 76161
                if (r == 0 && (nn == -ENOBUFS || nn == -EAGAIN) &&
829 75412
                    (uu & OBJ_ITER_FLUSH) == 0) {
830 1198
                        r = func(priv, OBJ_ITER_FLUSH, NULL, 0);
831 1198
                        if (scaret->used > 0)
832 0
                                ObjVAIreturn(wrk, hdl, scaret);
833 1198
                }
834
835 76161
                if (r == 0 && (nn == -ENOBUFS || nn == -EAGAIN)) {
836 18434
                        assert(scaret->used <= 1);
837 18434
                        sml_notify_wait(&sn);
838 18434
                }
839 57727
                else if (r == 0 && nn < 0)
840 224
                        r = -1;
841 76161
        } while (nn != 0 && r == 0);
842
843 34523
        if ((u & OBJ_ITER_END) == 0) {
844 10845
                r2 = func(priv, OBJ_ITER_END, NULL, 0);
845 10845
                if (r == 0)
846 10356
                        r = r2;
847 10845
        }
848
849 34523
        if (scaret->used > 0)
850 23941
                ObjVAIreturn(wrk, hdl, scaret);
851
852 34523
        ObjVAIfini(wrk, &hdl);
853 34523
        sml_notify_fini(&sn);
854
855 34523
        return (r);
856
}
857
858
/*--------------------------------------------------------------------
859
 */
860
861
static struct storage *
862 45982
objallocwithnuke(struct worker *wrk, const struct stevedore *stv, ssize_t size,
863
    int flags)
864
{
865 45982
        struct storage *st = NULL;
866
867 45982
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
868 45982
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
869
870 45982
        if (size > cache_param->fetch_maxchunksize) {
871 28
                if (!(flags & LESS_MEM_ALLOCED_IS_OK))
872 0
                        return (NULL);
873 28
                size = cache_param->fetch_maxchunksize;
874 28
        }
875
876 45982
        assert(size <= UINT_MAX);       /* field limit in struct storage */
877
878 45982
        do {
879
                /* try to allocate from it */
880 46094
                st = sml_stv_alloc(stv, size, flags);
881 46094
                if (st != NULL)
882 45912
                        break;
883
884
                /* no luck; try to free some space and keep trying */
885 182
                if (stv->lru == NULL)
886 0
                        break;
887 182
        } while (LRU_NukeOne(wrk, stv->lru));
888
889 45982
        CHECK_OBJ_ORNULL(st, STORAGE_MAGIC);
890 45982
        return (st);
891 45982
}
892
893
static int v_matchproto_(objgetspace_f)
894 813822
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 813822
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
901 813822
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
902 813822
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
903 813822
        AN(sz);
904 813822
        AN(ptr);
905 813822
        if (*sz == 0)
906 762491
                *sz = cache_param->fetch_chunksize;
907 813822
        assert(*sz > 0);
908 813822
        if (oc->boc->transit_buffer > 0)
909 6922
                *sz = vmin_t(ssize_t, *sz, oc->boc->transit_buffer);
910
911 813822
        o = sml_getobj(wrk, oc);
912 813822
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
913 813822
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
914
915 813822
        st = VTAILQ_FIRST(&o->list);
916 813822
        if (st != NULL && st->len < st->space) {
917 773819
                *sz = st->space - st->len;
918 773819
                *ptr = st->ptr + st->len;
919 773819
                assert (*sz > 0);
920 773819
                return (1);
921
        }
922
923 40003
        st = objallocwithnuke(wrk, oc->stobj->stevedore, *sz,
924
            LESS_MEM_ALLOCED_IS_OK);
925 40003
        if (st == NULL)
926 70
                return (0);
927
928 39933
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
929 39933
        Lck_Lock(&oc->boc->mtx);
930 39933
        VTAILQ_INSERT_HEAD(&o->list, st, list);
931 39933
        Lck_Unlock(&oc->boc->mtx);
932
933 39933
        *sz = st->space - st->len;
934 39933
        assert (*sz > 0);
935 39933
        *ptr = st->ptr + st->len;
936 39933
        return (1);
937 813822
}
938
939
static void v_matchproto_(objextend_f)
940 793327
sml_extend(struct worker *wrk, struct objcore *oc, ssize_t l)
941
{
942
        struct object *o;
943
        struct storage *st;
944
945 793327
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
946 793327
        assert(l > 0);
947
948 793327
        o = sml_getobj(wrk, oc);
949 793327
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
950 793327
        st = VTAILQ_FIRST(&o->list);
951 793327
        CHECK_OBJ_NOTNULL(st, STORAGE_MAGIC);
952 793327
        assert(st->len + l <= st->space);
953 793327
        st->len += l;
954 793327
}
955
956
static void v_matchproto_(objtrimstore_f)
957 26926
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 26926
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
964 26926
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
965 26926
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
966
967 26926
        stv = oc->stobj->stevedore;
968 26926
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
969
970 26926
        if (oc->boc->stevedore_priv != NULL)
971 0
                WRONG("sml_trimstore already called");
972 26926
        oc->boc->stevedore_priv = trim_once;
973
974 26926
        if (stv->sml_free == NULL)
975 0
                return;
976
977 26926
        o = sml_getobj(wrk, oc);
978 26926
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
979 26926
        st = VTAILQ_FIRST(&o->list);
980
981 26926
        if (st == NULL)
982 0
                return;
983
984 26926
        if (st->len == 0) {
985 112
                Lck_Lock(&oc->boc->mtx);
986 112
                VTAILQ_REMOVE(&o->list, st, list);
987 112
                Lck_Unlock(&oc->boc->mtx);
988
                /* sml_bocdone frees this */
989 112
                oc->boc->stevedore_priv = st;
990 112
                return;
991
        }
992
993 26814
        if (st->space - st->len < 512)
994 24050
                return;
995
996 2764
        st1 = sml_stv_alloc(stv, st->len, 0);
997 2764
        if (st1 == NULL)
998 0
                return;
999 2764
        assert(st1->space >= st->len);
1000
1001 2764
        memcpy(st1->ptr, st->ptr, st->len);
1002 2764
        st1->len = st->len;
1003 2764
        Lck_Lock(&oc->boc->mtx);
1004 2764
        VTAILQ_REMOVE(&o->list, st, list);
1005 2764
        VTAILQ_INSERT_HEAD(&o->list, st1, list);
1006 2764
        Lck_Unlock(&oc->boc->mtx);
1007
        /* sml_bocdone frees this */
1008 2764
        oc->boc->stevedore_priv = st;
1009 26926
}
1010
1011
static void v_matchproto_(objbocdone_f)
1012 40498
sml_bocdone(struct worker *wrk, struct objcore *oc, struct boc *boc)
1013
{
1014
        const struct stevedore *stv;
1015
1016 40498
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1017 40498
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1018 40498
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1019 40498
        stv = oc->stobj->stevedore;
1020 40498
        CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
1021
1022 40498
        sml_bocfini(stv, boc);
1023
1024 40498
        if (stv->lru != NULL) {
1025 39952
                if (isnan(wrk->lastused))
1026 0
                        wrk->lastused = VTIM_real();
1027 39952
                LRU_Add(oc, wrk->lastused);     // approx timestamp is OK
1028 39952
        }
1029 40498
}
1030
1031
static const void * v_matchproto_(objgetattr_f)
1032 318124
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 318124
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1039
1040 318124
        if (len == NULL)
1041 194816
                len = &dummy;
1042 318124
        o = sml_getobj(wrk, oc);
1043 318124
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
1044
1045 318124
        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 318124
}
1077
1078
static void * v_matchproto_(objsetattr_f)
1079 151003
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 151003
        void *retval = NULL;
1084
        struct storage *st;
1085
1086 151003
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1087
1088 151003
        o = sml_getobj(wrk, oc);
1089 151003
        CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
1090 151003
        st = o->objstore;
1091
1092 151003
        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 151003
        if (retval != NULL && ptr != NULL)
1147 5488
                memcpy(retval, ptr, len);
1148 151003
        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 56
sml_panic_st(struct vsb *vsb, const char *hd, const struct storage *st)
1167
{
1168 112
        VSB_printf(vsb, "%s = %p {priv=%p, ptr=%p, len=%u, space=%u},\n",
1169 56
            hd, st, st->priv, st->ptr, st->len, st->space);
1170 56
}
1171
1172
void
1173 28
SML_panic(struct vsb *vsb, const struct objcore *oc)
1174
{
1175
        struct object *o;
1176
        struct storage *st;
1177
1178 28
        VSB_printf(vsb, "Simple = %p,\n", oc->stobj->priv);
1179 28
        if (oc->stobj->priv == NULL)
1180 0
                return;
1181 28
        o = oc->stobj->priv;
1182 28
        PAN_CheckMagic(vsb, o, OBJECT_MAGIC);
1183 28
        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 56
        VTAILQ_FOREACH(st, &o->list, list) {
1202 28
                sml_panic_st(vsb, "Body", st);
1203 28
        }
1204
}