Code Coverage: vinyl-cache/bin/vinyld/cache/cache_obj.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2013-2016 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
 * Lifetime of an objcore:
30
 *      phase 0 - nonexistent
31
 *      phase 1 - created, but no stevedore associated
32
 *      phase 2 - stevedore associated, being filled out
33
 *      phase 3 - stable, no changes happening
34
 *      phase 4 - unavailable, being dismantled
35
 *      phase 5 - stevedore disassociated
36
 *      phase 6 - nonexistent
37
 *
38
 * 0->1 ObjNew()        creates objcore
39
 *
40
 * 1->2 STV_NewObject() associates a stevedore
41
 *
42
 * 2    ObjSetState()   sets state
43
 * 2    ObjWaitState()  waits for particular state
44
 *                      INVALID->REQ_DONE->STREAM->FINISHED->FAILED
45
 *
46
 * 2    ObjGetSpace()   allocates space
47
 * 2    ObjExtend()     commits content
48
 * 2    ObjWaitExtend() waits for content - used to implement ObjIterate())
49
 *
50
 * 2    ObjSetAttr()
51
 * 2      ObjCopyAttr()
52
 * 2      ObjSetFlag()
53
 * 2      ObjSetDouble()
54
 * 2      ObjSetU32()
55
 * 2      ObjSetU64()
56
 *
57
 * 2->3 ObjBocDone()    Boc removed from OC, clean it up
58
 *
59
 * 23   ObjHasAttr()
60
 * 23   ObjGetAttr()
61
 * 23     ObjCheckFlag()
62
 * 23     ObjGetDouble()
63
 * 23     ObjGetU32()
64
 * 23     ObjGetU64()
65
 * 23     ObjGetLen()
66
 * 23     ObjGetXID()
67
 *
68
 * 23   ObjIterate()    ... over body
69
 *
70
 * 23   ObjTouch()      Signal to LRU(-like) facilities
71
 *
72
 * 3->4 HSH_Snipe()     kill if not in use
73
 * 3->4 HSH_Kill()      make unavailable
74
 *
75
 * 234  ObjSlim()       Release body storage (but retain attribute storage)
76
 *
77
 * 4->5 ObjFreeObj()    disassociates stevedore
78
 *
79
 * 5->6 FREE_OBJ()      ...in HSH_DerefObjCore()
80
 */
81
82
#include "config.h"
83
84
#include <stdlib.h>
85
86
#include "cache_int.h"
87
#include "cache_obj.h"
88
#include "cache_objhead.h"
89
#include "vend.h"
90
#include "storage/storage.h"
91
92
static const struct obj_methods *
93
obj_getmethods(const struct objcore *oc)
94 234085
{
95
96 234085
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
97 234085
        CHECK_OBJ_NOTNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
98 234085
        AN(oc->stobj->stevedore->methods);
99 234085
        return (oc->stobj->stevedore->methods);
100 234085
}
101
102
static struct boc *
103
obj_newboc(void)
104 7250
{
105 7250
        struct boc *boc;
106
107 7250
        ALLOC_OBJ(boc, BOC_MAGIC);
108 7250
        AN(boc);
109 7250
        Lck_New(&boc->mtx, lck_busyobj);
110 7250
        PTOK(pthread_cond_init(&boc->cond, NULL));
111 7250
        boc->refcount = 1;
112 7250
        return (boc);
113 7250
}
114
115
static void
116
obj_deleteboc(struct boc **p)
117 7234
{
118 7234
        struct boc *boc;
119
120 7234
        TAKE_OBJ_NOTNULL(boc, p, BOC_MAGIC);
121 7234
        Lck_Delete(&boc->mtx);
122 7234
        PTOK(pthread_cond_destroy(&boc->cond));
123 7234
        free(boc->vary);
124 7234
        FREE_OBJ(boc);
125 7234
}
126
127
/*====================================================================
128
 * ObjNew()
129
 *
130
 */
131
132
struct objcore *
133
ObjNew(const struct worker *wrk)
134 7248
{
135 7248
        struct objcore *oc;
136
137 7248
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
138
139 7248
        ALLOC_OBJ(oc, OBJCORE_MAGIC);
140 7248
        AN(oc);
141 7248
        wrk->stats->n_objectcore++;
142 7248
        oc->last_lru = NAN;
143 7248
        oc->boc = obj_newboc();
144
145 7248
        return (oc);
146 7248
}
147
148
/*====================================================================
149
 * ObjDestroy()
150
 *
151
 */
152
153
void
154
ObjDestroy(const struct worker *wrk, struct objcore **p)
155 6158
{
156 6158
        struct objcore *oc;
157
158 6158
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
159 6158
        TAKE_OBJ_NOTNULL(oc, p, OBJCORE_MAGIC);
160 6158
        if (oc->boc != NULL)
161 338
                obj_deleteboc(&oc->boc);
162 6158
        FREE_OBJ(oc);
163 6158
        wrk->stats->n_objectcore--;
164 6158
}
165
166
/*====================================================================
167
 * ObjIterate()
168
 *
169
 */
170
171
int
172
ObjIterate(struct worker *wrk, struct objcore *oc,
173
    void *priv, objiterate_f *func, int final)
174 7096
{
175 7096
        const struct obj_methods *om = obj_getmethods(oc);
176
177 7096
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
178 7096
        AN(func);
179 7096
        AN(om->objiterator);
180 7096
        return (om->objiterator(wrk, oc, priv, func, final));
181 7096
}
182
183
/*====================================================================
184
 * ObjVAI...(): Asynchronous Iteration
185
 *
186
 *
187
 * ObjVAIinit() returns an opaque handle, or NULL if not supported
188
 *
189
 *      A VAI handle must not be used concurrently
190
 *
191
 *      the vai_notify_cb(priv) will be called asynchronously by the storage
192
 *      engine when a -EAGAIN / -ENOBUFS condition is over and ObjVAIlease()
193
 *      can be called again.
194
 *
195
 *      Note:
196
 *      - the callback gets executed by an arbitrary thread
197
 *      - WITH the boc mtx held
198
 *      so it should never block and only do minimal work
199
 *
200
 * ObjVAIlease() fills the vscarab with leases. returns:
201
 *
202
 *      -EAGAIN:  nothing available at the moment, storage will notify, no use to
203
 *                call again until notification
204
 *      -ENOBUFS: caller needs to return leases, storage will notify
205
 *      -EPIPE:   BOS_FAILED for busy object
206
 *      -(errno): other problem, fatal
207
 *
208
 *      >= 0:     number of viovs added (== scarab->capacity - scarab->used)
209
 *
210
 *      struct vscarab:
211
 *
212
 *      the leases can be used by the caller until returned with
213
 *      ObjVAIreturn(). The storage guarantees that the lease member is a
214
 *      multiple of 8 (that is, the lower three bits are zero). These can be
215
 *      used by the caller between lease and return, but must be cleared to
216
 *      zero before returning.
217
 *
218
 * ObjVAIbuffer() allocates temporary buffers, returns:
219
 *
220
 *      -EAGAIN:  allocation can not be fulfilled immediately, storage will notify,
221
 *                no use to call again until notification
222
 *      -EINVAL:  size larger than UINT_MAX requested
223
 *      -(errno): other problem, fatal
224
 *      n:        n > 0, number of viovs filled
225
 *
226
 *      The struct vscarab is used on the way in and out: On the way in, the
227
 *      iov.iov_len members contain the sizes the caller requests, all other
228
 *      members of the struct viovs are expected to be zero initialized.
229
 *
230
 *      The maximum size to be requested is UINT_MAX.
231
 *
232
 *      ObjVAIbuffer() may return sizes larger than requested. The returned n
233
 *      might be smaller than requested.
234
 *
235
 * ObjVAIreturn() returns leases collected in a struct vscaret
236
 *
237
 *      it must be called with a vscaret, which holds an array of lease values
238
 *      received via ObjVAIlease() or ObjVAIbuffer() when the caller can
239
 *      guarantee that they are no longer accessed.
240
 *
241
 *      ObjVAIreturn() may retain leases in the vscaret if the implementation
242
 *      still requires them, iow, the vscaret might not be empty upon return.
243
 *
244
 * ObjVAInotify() notify to resume delivery from a vmod
245
 *
246
 *      Not only a storage engine can encounter a "no data ready at the moment"
247
 *      condition, but also a filter: A filter's .io_lease() function might
248
 *      return -EAGAIN, but then it needs a way to notify when more data is
249
 *      available. This is ObjVAInotify().
250
 *
251
 * ObjVAIfini() finalized iteration
252
 *
253
 *      it must be called when iteration is done, irrespective of error status
254
 */
255
256
vai_hdl
257
ObjVAIinit(struct worker *wrk, struct objcore *oc, struct ws *ws,
258
    vai_notify_cb *cb, void *cb_priv)
259 7128
{
260 7128
        const struct obj_methods *om = obj_getmethods(oc);
261
262 7128
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
263
264 7128
        if (om->vai_init == NULL)
265 0
                return (NULL);
266 7128
        return (om->vai_init(wrk, oc, ws, cb, cb_priv));
267 7128
}
268
269
int
270
ObjVAIlease(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
271 17737
{
272 17737
        struct vai_hdl_preamble *vaip = vhdl;
273
274 17737
        AN(vaip);
275 17737
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
276 17737
        AN(vaip->vai_lease);
277 17737
        return (vaip->vai_lease(wrk, vhdl, scarab));
278 17737
}
279
280
int
281
ObjVAIbuffer(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
282 16
{
283 16
        struct vai_hdl_preamble *vaip = vhdl;
284 16
        int r;
285
286 16
        AN(vaip);
287 16
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
288 16
        AN(vaip->vai_buffer);
289 16
        r = vaip->vai_buffer(wrk, vhdl, scarab);
290
        // returning no reason for failure is a bug
291 16
        assert(r != 0);
292 16
        return (r);
293 16
}
294
295
void
296
ObjVAIreturn(struct worker *wrk, vai_hdl vhdl, struct vscaret *scaret)
297 8331
{
298 8331
        struct vai_hdl_preamble *vaip = vhdl;
299
300 8331
        AN(vaip);
301 8331
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
302 8331
        AN(vaip->vai_return);
303 8331
        vaip->vai_return(wrk, vhdl, scaret);
304 8331
}
305
306
void
307
ObjVAInotify(struct worker *wrk, vai_hdl vhdl)
308 128
{
309 128
        struct vai_hdl_preamble *vaip = vhdl;
310
311 128
        AN(vaip);
312 128
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
313 128
        AN(vaip->vai_notify);
314 128
        vaip->vai_notify(wrk, vhdl);
315 128
}
316
317
void
318
ObjVAIfini(struct worker *wrk, vai_hdl *vhdlp)
319 7127
{
320 7127
        AN(vhdlp);
321 7127
        struct vai_hdl_preamble *vaip = *vhdlp;
322
323 7127
        AN(vaip);
324 7127
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
325 7127
        AN(vaip->vai_lease);
326 7127
        vaip->vai_fini(wrk, vhdlp);
327 7127
}
328
329
/*====================================================================
330
 * ObjGetSpace()
331
 *
332
 * This function returns a pointer and length of free space.  If there
333
 * is no free space, some will be added first.
334
 *
335
 * The "sz" argument is an input hint of how much space is desired.
336
 * 0 means "unknown", return some default size (maybe fetch_chunksize)
337
 */
338
339
int
340
ObjGetSpace(struct worker *wrk, struct objcore *oc, ssize_t *sz, uint8_t **ptr)
341 70471
{
342 70471
        const struct obj_methods *om = obj_getmethods(oc);
343
344 70471
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
345 70471
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
346 70471
        AN(sz);
347 70471
        AN(ptr);
348 70471
        assert(*sz >= 0);
349
350 70471
        AN(om->objgetspace);
351 70471
        return (om->objgetspace(wrk, oc, sz, ptr));
352 70471
}
353
354
/*====================================================================
355
 * ObjExtend()
356
 *
357
 * This function extends the used part of the object a number of bytes
358
 * into the last space returned by ObjGetSpace()
359
 *
360
 * The final flag must be set on the last call, and it will release any
361
 * surplus space allocated.
362
 */
363
364
static void
365
obj_extend_condwait(const struct objcore *oc)
366 64384
{
367
368 64384
        if (oc->boc->transit_buffer == 0)
369 63928
                return;
370
371 456
        assert(oc->flags & OC_F_TRANSIENT);
372 496
        while (!(oc->flags & OC_F_CANCEL) && oc->boc->fetched_so_far >
373 494
            oc->boc->delivered_so_far + oc->boc->transit_buffer)
374 40
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
375 456
}
376
377
// notify of an extension of the boc or state change
378
379
static void
380
obj_boc_notify(struct boc *boc)
381 68517
{
382 68517
        struct vai_qe *qe, *next;
383
384 68517
        PTOK(pthread_cond_broadcast(&boc->cond));
385 68520
        qe = VSLIST_FIRST(&boc->vai_q_head);
386 68520
        VSLIST_FIRST(&boc->vai_q_head) = NULL;
387 70753
        while (qe != NULL) {
388 2233
                CHECK_OBJ(qe, VAI_Q_MAGIC);
389 2233
                AN(qe->flags & VAI_QF_INQUEUE);
390 2233
                qe->flags &= ~VAI_QF_INQUEUE;
391 2233
                next = VSLIST_NEXT(qe, list);
392 2233
                VSLIST_NEXT(qe, list) = NULL;
393 2233
                qe->cb(qe->hdl, qe->priv);
394 2233
                qe = next;
395 2233
        }
396 68520
}
397
398
void
399
ObjExtend(struct worker *wrk, struct objcore *oc, ssize_t l, int final)
400 65124
{
401 65124
        const struct obj_methods *om = obj_getmethods(oc);
402
403 65124
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
404 65124
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
405 65124
        AN(om->objextend);
406 65124
        assert(l >= 0);
407
408 65124
        if (l > 0) {
409 64393
                Lck_Lock(&oc->boc->mtx);
410 64393
                obj_extend_condwait(oc);
411 64393
                om->objextend(wrk, oc, l);
412 64393
                oc->boc->fetched_so_far += l;
413 64393
                obj_boc_notify(oc->boc);
414 64393
                Lck_Unlock(&oc->boc->mtx);
415
416 64393
                if (oc->boc->transit_buffer > 0)
417 455
                        wrk->stats->transit_buffered += l;
418 63938
                else if (oc->flags & OC_F_TRANSIENT)
419 9194
                        wrk->stats->transit_stored += l;
420 64393
        }
421
422 65124
        assert(oc->boc->state < BOS_FINISHED);
423 65124
        if (final && om->objtrimstore != NULL)
424 2016
                om->objtrimstore(wrk, oc);
425 65124
}
426
427
/*====================================================================
428
 */
429
430
static inline void
431
objSignalFetchLocked(const struct objcore *oc, uint64_t l)
432 7611
{
433 7611
        if (oc->boc->transit_buffer > 0) {
434 856
                assert(oc->flags & OC_F_TRANSIENT);
435
                /* Signal the new client position */
436 856
                oc->boc->delivered_so_far = l;
437 856
                PTOK(pthread_cond_signal(&oc->boc->cond));
438 856
        }
439 7611
}
440
441
uint64_t
442
ObjWaitExtend(const struct worker *wrk, const struct objcore *oc, uint64_t l,
443
    enum boc_state_e *statep)
444 0
{
445 0
        enum boc_state_e state;
446 0
        uint64_t rv;
447
448 0
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
449 0
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
450 0
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
451 0
        Lck_Lock(&oc->boc->mtx);
452 0
        while (1) {
453 0
                rv = oc->boc->fetched_so_far;
454 0
                assert(l <= rv || oc->boc->state == BOS_FAILED);
455 0
                state = oc->boc->state;
456 0
                objSignalFetchLocked(oc, l);
457 0
                if (rv > l || state >= BOS_FINISHED)
458 0
                        break;
459 0
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
460 0
        }
461 0
        Lck_Unlock(&oc->boc->mtx);
462 0
        if (statep != NULL)
463 0
                *statep = state;
464 0
        return (rv);
465 0
}
466
467
// get a new extension _or_ register a notification
468
uint64_t
469
ObjVAIGetExtend(struct worker *wrk, const struct objcore *oc, uint64_t l,
470
    enum boc_state_e *statep, struct vai_qe *qe)
471 7613
{
472 7613
        enum boc_state_e state;
473 7613
        uint64_t rv;
474
475 7613
        (void) wrk;
476 7613
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
477 7613
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
478 7613
        CHECK_OBJ_NOTNULL(qe, VAI_Q_MAGIC);
479 7613
        Lck_Lock(&oc->boc->mtx);
480 7613
        rv = oc->boc->fetched_so_far;
481 7613
        assert(l <= rv || oc->boc->state == BOS_FAILED);
482 7613
        state = oc->boc->state;
483 7613
        objSignalFetchLocked(oc, l);
484 7613
        if (l == rv && state < BOS_FINISHED &&
485 7613
            (qe->flags & VAI_QF_INQUEUE) == 0) {
486 2243
                qe->flags |= VAI_QF_INQUEUE;
487 2243
                VSLIST_INSERT_HEAD(&oc->boc->vai_q_head, qe, list);
488 2243
        }
489 7613
        Lck_Unlock(&oc->boc->mtx);
490 7613
        if (statep != NULL)
491 7615
                *statep = state;
492 7613
        return (rv);
493 7613
}
494
495
void
496
ObjVAICancel(struct worker *wrk, struct boc *boc, struct vai_qe *qe)
497 962
{
498
499 962
        (void) wrk;
500 962
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
501 962
        CHECK_OBJ_NOTNULL(qe, VAI_Q_MAGIC);
502
503 962
        Lck_Lock(&boc->mtx);
504
        // inefficient, but should be rare
505 962
        if ((qe->flags & VAI_QF_INQUEUE) != 0)
506 10
                VSLIST_REMOVE(&boc->vai_q_head, qe, vai_qe, list);
507 962
        qe->flags = 0;
508 962
        Lck_Unlock(&boc->mtx);
509 962
}
510
511
/*====================================================================
512
 */
513
514
void
515
ObjSetState(struct worker *wrk, struct objcore *oc, enum boc_state_e next,
516
    unsigned broadcast)
517 6992
{
518 6992
        const struct obj_methods *om;
519
520 6992
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
521 6992
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
522 6992
        assert(next > oc->boc->state);
523
524 6992
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
525 6992
        assert(next != BOS_FINISHED || (oc->oa_present & (1 << OA_LEN)));
526
527 6992
        if (oc->stobj->stevedore != NULL) {
528 3994
                om = oc->stobj->stevedore->methods;
529 3994
                if (om->objsetstate != NULL)
530 0
                        om->objsetstate(wrk, oc, next);
531 3994
        }
532
533 6992
        if (next == BOS_FAILED)
534 74
                HSH_Fail(wrk, oc);
535 6918
        else if (oc->boc->state < BOS_STREAM && next >= BOS_STREAM)
536 2917
                HSH_Unbusy(wrk, oc);
537
538 6992
        Lck_Lock(&oc->boc->mtx);
539 6992
        oc->boc->state = next;
540 6992
        if (broadcast)
541 4133
                obj_boc_notify(oc->boc);
542 6992
        Lck_Unlock(&oc->boc->mtx);
543 6992
}
544
545
/*====================================================================
546
 */
547
548
enum boc_state_e
549
ObjWaitState(const struct objcore *oc, enum boc_state_e want)
550 3554
{
551 3554
        enum boc_state_e got;
552
553 3554
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
554 3554
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
555
556 3554
        Lck_Lock(&oc->boc->mtx);
557
        /* wake up obj_extend_condwait() */
558 3554
        if (oc->flags & OC_F_CANCEL)
559 3554
                PTOK(pthread_cond_signal(&oc->boc->cond));
560 35439
        while (1) {
561 35438
                if (oc->boc->state >= want)
562 3553
                        break;
563 31885
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
564 31885
        }
565 3554
        got = oc->boc->state;
566 3554
        Lck_Unlock(&oc->boc->mtx);
567
568 3554
        return (got);
569 3554
}
570
571
/*====================================================================
572
 * ObjGetlen()
573
 *
574
 * This is a separate function because it may need locking
575
 */
576
577
uint64_t
578
ObjGetLen(struct worker *wrk, struct objcore *oc)
579 14778
{
580 14778
        uint64_t len;
581
582 14778
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
583
584 14778
        AZ(ObjGetU64(wrk, oc, OA_LEN, &len));
585 14778
        return (len);
586 14778
}
587
588
/*====================================================================
589
 * ObjSlim()
590
 *
591
 * Free the whatever storage can be freed, without freeing the actual
592
 * object yet.
593
 */
594
595
void
596
ObjSlim(struct worker *wrk, struct objcore *oc)
597 5205
{
598 5205
        const struct obj_methods *om = obj_getmethods(oc);
599
600 5205
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
601
602 5205
        if (om->objslim != NULL)
603 3106
                om->objslim(wrk, oc);
604 5205
}
605
606
/*====================================================================
607
 * Called when the boc used to populate the objcore is going away.
608
 * Useful for releasing any leftovers from Trim.
609
 */
610
611
void
612
ObjBocDone(struct worker *wrk, struct objcore *oc, struct boc **boc)
613 6896
{
614 6896
        const struct obj_methods *m;
615
616 6896
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
617 6896
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
618 6896
        AN(boc);
619 6896
        CHECK_OBJ_NOTNULL(*boc, BOC_MAGIC);
620 6896
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
621 6896
        if (oc->stobj->stevedore != NULL) {
622 6841
                m = obj_getmethods(oc);
623 6841
                if (m->objbocdone != NULL)
624 4736
                        m->objbocdone(wrk, oc, *boc);
625 6841
        }
626 6896
        obj_deleteboc(boc);
627 6896
}
628
629
/*====================================================================
630
 */
631
void
632
ObjFreeObj(struct worker *wrk, struct objcore *oc)
633 5790
{
634 5790
        const struct obj_methods *m = obj_getmethods(oc);
635
636 5790
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
637
638 5790
        AN(m->objfree);
639 5790
        m->objfree(wrk, oc);
640 5790
        AZ(oc->stobj->stevedore);
641 5790
}
642
643
/*====================================================================
644
 * ObjHasAttr()
645
 *
646
 * Check if object has this attribute
647
 */
648
649
int
650
ObjHasAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr)
651 12979
{
652
653 12979
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
654 12979
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
655
656 12979
        if (oc->oa_present)
657 10861
                return (oc->oa_present & (1 << attr));
658
659
        /* resurrected persistent objects don't have oa_present set */
660 2118
        return (ObjGetAttr(wrk, oc, attr, NULL) != NULL ? 1 : 0);
661 12979
}
662
663
/*====================================================================
664
 * ObjGetAttr()
665
 *
666
 * Get an attribute of the object.
667
 *
668
 * Returns NULL on unset or zero length attributes and len set to
669
 * zero. Returns Non-NULL otherwise and len is updated with the attributes
670
 * length.
671
 */
672
673
const void *
674
ObjGetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
675
   ssize_t *len)
676 45919
{
677 45919
        const struct obj_methods *om = obj_getmethods(oc);
678
679 45919
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
680
681 45919
        AN(om->objgetattr);
682 45919
        return (om->objgetattr(wrk, oc, attr, len));
683 45919
}
684
685
/*====================================================================
686
 * ObjSetAttr()
687
 *
688
 * Setting fixed size attributes always succeeds.
689
 *
690
 * Setting a variable size attribute asserts if the combined size of the
691
 * variable attributes exceeds the total variable attribute space set at
692
 * object creation. If there is space it always succeeds.
693
 *
694
 * Setting an auxiliary attribute can fail.
695
 *
696
 * Resetting any variable asserts if the new length does not match the
697
 * previous length exactly.
698
 *
699
 * If ptr is Non-NULL, it points to the new content which is copied into
700
 * the attribute.  Otherwise the caller will have to do the copying.
701
 *
702
 * Return value is non-NULL on success and NULL on failure. If ptr was
703
 * non-NULL, it is an error to use the returned pointer to set the
704
 * attribute data, it is only a success indicator in that case.
705
 */
706
707
void *
708
ObjSetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
709
    ssize_t len, const void *ptr)
710 15676
{
711 15676
        const struct obj_methods *om = obj_getmethods(oc);
712 15676
        void *r;
713
714 15676
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
715 15676
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
716
717 15676
        AN(om->objsetattr);
718 15676
        assert((int)attr < 16);
719 15676
        r = om->objsetattr(wrk, oc, attr, len, ptr);
720 15676
        if (r)
721 15675
                oc->oa_present |= (1 << attr);
722 15676
        return (r);
723 15676
}
724
725
/*====================================================================
726
 * ObjTouch()
727
 */
728
729
void
730
ObjTouch(struct worker *wrk, struct objcore *oc, vtim_real now)
731 4996
{
732 4996
        const struct obj_methods *om = obj_getmethods(oc);
733
734 4996
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
735 4996
        if (om->objtouch != NULL)
736 4996
                om->objtouch(wrk, oc, now);
737 4996
}
738
739
/*====================================================================
740
 * Utility functions which work on top of the previous ones
741
 */
742
743
int
744
ObjCopyAttr(struct worker *wrk, struct objcore *oc, struct objcore *ocs,
745
    enum obj_attr attr)
746 63
{
747 63
        const void *vps;
748 63
        void *vpd;
749 63
        ssize_t l;
750
751 63
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
752 63
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
753 63
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
754 63
        CHECK_OBJ_NOTNULL(ocs, OBJCORE_MAGIC);
755
756 63
        vps = ObjGetAttr(wrk, ocs, attr, &l);
757
        // XXX: later we want to have zero-length OA's too
758 63
        if (vps == NULL || l <= 0)
759 0
                return (-1);
760 63
        vpd = ObjSetAttr(wrk, oc, attr, l, vps);
761 63
        if (vpd == NULL)
762 0
                return (-1);
763 63
        return (0);
764 63
}
765
766
int
767
ObjSetXID(struct worker *wrk, struct objcore *oc, vxid_t xid)
768 2943
{
769 2943
        uint64_t u;
770
771 2943
        u = VXID(xid);
772 2943
        AZ(ObjSetU64(wrk, oc, OA_VXID, u));
773 2943
        return (0);
774 2943
}
775
776
777
vxid_t
778
ObjGetXID(struct worker *wrk, struct objcore *oc)
779 5031
{
780 5031
        vxid_t u;
781
782 5031
        AZ(ObjGetU64(wrk, oc, OA_VXID, &u.vxid));
783 5031
        return (u);
784 5031
}
785
786
/*--------------------------------------------------------------------
787
 * There is no well-defined byteorder for IEEE-754 double and the
788
 * correct solution (frexp(3) and manual encoding) is more work
789
 * than our (weak) goal of being endian-agnostic requires at this point.
790
 * We give it a shot by memcpy'ing doubles over a uint64_t and then
791
 * BE encode that.
792
 */
793
794
int
795
ObjSetDouble(struct worker *wrk, struct objcore *oc, enum obj_attr a, double t)
796 2943
{
797 2943
        void *vp;
798 2943
        uint64_t u;
799
800 2943
        assert(sizeof t == sizeof u);
801 2943
        vmemcpy(&u, &t, sizeof u);
802 2943
        vp = ObjSetAttr(wrk, oc, a, sizeof u, NULL);
803 2943
        if (vp == NULL)
804 0
                return (-1);
805 2943
        vbe64enc(vp, u);
806 2943
        return (0);
807 2943
}
808
809
int
810
ObjGetDouble(struct worker *wrk, struct objcore *oc, enum obj_attr a, double *d)
811 2
{
812 2
        const void *vp;
813 2
        uint64_t u;
814 2
        ssize_t l;
815
816 2
        assert(sizeof *d == sizeof u);
817 2
        vp = ObjGetAttr(wrk, oc, a, &l);
818 2
        if (vp == NULL)
819 0
                return (-1);
820 2
        if (d != NULL) {
821 2
                assert(l == sizeof u);
822 2
                u = vbe64dec(vp);
823 2
                vmemcpy(d, &u, sizeof *d);
824 2
        }
825 2
        return (0);
826 2
}
827
828
/*--------------------------------------------------------------------
829
 */
830
831
int
832
ObjSetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t t)
833 7417
{
834 7417
        void *vp;
835
836 7417
        vp = ObjSetAttr(wrk, oc, a, sizeof t, NULL);
837 7417
        if (vp == NULL)
838 0
                return (-1);
839 7417
        vbe64enc(vp, t);
840 7417
        return (0);
841 7417
}
842
843
int
844
ObjGetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t *d)
845 19808
{
846 19808
        const void *vp;
847 19808
        ssize_t l;
848
849 19808
        vp = ObjGetAttr(wrk, oc, a, &l);
850 19808
        if (vp == NULL || l != sizeof *d)
851 0
                return (-1);
852 19808
        if (d != NULL)
853 19805
                *d = vbe64dec(vp);
854 19808
        return (0);
855 19808
}
856
857
/*--------------------------------------------------------------------
858
 */
859
860
int
861
ObjCheckFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of)
862 14207
{
863 14207
        const uint8_t *fp;
864
865 14207
        fp = ObjGetAttr(wrk, oc, OA_FLAGS, NULL);
866 14207
        AN(fp);
867 14207
        return ((*fp) & of);
868 14207
}
869
870
void
871
ObjSetFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of, int val)
872 1009
{
873 1009
        uint8_t *fp;
874
875 1009
        fp = ObjSetAttr(wrk, oc, OA_FLAGS, 1, NULL);
876 1009
        AN(fp);
877 1009
        if (val)
878 1007
                (*fp) |= of;
879 2
        else
880 2
                (*fp) &= ~of;
881 1009
}
882
883
/*====================================================================
884
 * Object event subscription mechanism.
885
 *
886
 * XXX: it is extremely unclear what the locking circumstances are here.
887
 */
888
889
struct oev_entry {
890
        unsigned                        magic;
891
#define OEV_MAGIC                       0xb0b7c5a1
892
        unsigned                        mask;
893
        obj_event_f                     *func;
894
        void                            *priv;
895
        VTAILQ_ENTRY(oev_entry)         list;
896
};
897
898
static VTAILQ_HEAD(,oev_entry)          oev_list;
899
static pthread_rwlock_t                 oev_rwl;
900
static unsigned                         oev_mask;
901
902
/*
903
 * NB: ObjSubscribeEvents() is not atomic:
904
 * oev_mask is checked optimistically in ObjSendEvent()
905
 */
906
uintptr_t
907
ObjSubscribeEvents(obj_event_f *func, void *priv, unsigned mask)
908 37
{
909 37
        struct oev_entry *oev;
910
911 37
        AN(func);
912 37
        AZ(mask & ~OEV_MASK);
913
914 37
        ALLOC_OBJ(oev, OEV_MAGIC);
915 37
        AN(oev);
916 37
        oev->func = func;
917 37
        oev->priv = priv;
918 37
        oev->mask = mask;
919 37
        PTOK(pthread_rwlock_wrlock(&oev_rwl));
920 37
        VTAILQ_INSERT_TAIL(&oev_list, oev, list);
921 37
        oev_mask |= mask;
922 37
        PTOK(pthread_rwlock_unlock(&oev_rwl));
923 37
        return ((uintptr_t)oev);
924 37
}
925
926
void
927
ObjUnsubscribeEvents(uintptr_t *handle)
928 1
{
929 1
        struct oev_entry *oev, *oev2 = NULL;
930 1
        unsigned newmask = 0;
931
932 1
        AN(handle);
933 1
        AN(*handle);
934 1
        PTOK(pthread_rwlock_wrlock(&oev_rwl));
935 1
        VTAILQ_FOREACH(oev, &oev_list, list) {
936 1
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
937 1
                if ((uintptr_t)oev == *handle)
938 1
                        oev2 = oev;
939 0
                else
940 0
                        newmask |= oev->mask;
941 1
        }
942 1
        AN(oev2);
943 1
        VTAILQ_REMOVE(&oev_list, oev2, list);
944 1
        oev_mask = newmask;
945 1
        AZ(newmask & ~OEV_MASK);
946 1
        PTOK(pthread_rwlock_unlock(&oev_rwl));
947 1
        FREE_OBJ(oev2);
948 1
        *handle = 0;
949 1
}
950
951
void
952
ObjSendEvent(struct worker *wrk, struct objcore *oc, unsigned event)
953 3468
{
954 3468
        struct oev_entry *oev;
955
956 3468
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
957 3468
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
958 3468
        AN(event & OEV_MASK);
959 3468
        AZ(event & ~OEV_MASK);
960 3468
        if (!(event & oev_mask))
961 3388
                return;
962
963 80
        PTOK(pthread_rwlock_rdlock(&oev_rwl));
964 96
        VTAILQ_FOREACH(oev, &oev_list, list) {
965 96
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
966 96
                if (event & oev->mask)
967 96
                        oev->func(wrk, oev->priv, oc, event);
968 96
        }
969 80
        PTOK(pthread_rwlock_unlock(&oev_rwl));
970
971 80
}
972
973
void
974
ObjInit(void)
975 1026
{
976 1026
        VTAILQ_INIT(&oev_list);
977
        PTOK(pthread_rwlock_init(&oev_rwl, NULL));
978 1026
}