varnish-cache/bin/varnishd/cache/cache_obj.c
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_varnishd.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 2610543
obj_getmethods(const struct objcore *oc)
94
{
95
96 2610543
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
97 2610543
        CHECK_OBJ_NOTNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
98 2610543
        AN(oc->stobj->stevedore->methods);
99 2610543
        return (oc->stobj->stevedore->methods);
100
}
101
102
static struct boc *
103 52076
obj_newboc(void)
104
{
105
        struct boc *boc;
106
107 52076
        ALLOC_OBJ(boc, BOC_MAGIC);
108 52074
        AN(boc);
109 52074
        Lck_New(&boc->mtx, lck_busyobj);
110 52074
        PTOK(pthread_cond_init(&boc->cond, NULL));
111 52074
        boc->refcount = 1;
112 52074
        return (boc);
113
}
114
115
static void
116 47478
obj_deleteboc(struct boc **p)
117
{
118
        struct boc *boc;
119
120 47478
        TAKE_OBJ_NOTNULL(boc, p, BOC_MAGIC);
121 47478
        Lck_Delete(&boc->mtx);
122 47478
        PTOK(pthread_cond_destroy(&boc->cond));
123 47478
        free(boc->vary);
124 47478
        FREE_OBJ(boc);
125 47478
}
126
127
/*====================================================================
128
 * ObjNew()
129
 *
130
 */
131
132
struct objcore *
133 52074
ObjNew(const struct worker *wrk)
134
{
135
        struct objcore *oc;
136
137 52074
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
138
139 52074
        ALLOC_OBJ(oc, OBJCORE_MAGIC);
140 52074
        AN(oc);
141 52074
        wrk->stats->n_objectcore++;
142 52074
        oc->last_lru = NAN;
143 52074
        oc->boc = obj_newboc();
144
145 52074
        return (oc);
146
}
147
148
/*====================================================================
149
 * ObjDestroy()
150
 *
151
 */
152
153
void
154 30179
ObjDestroy(const struct worker *wrk, struct objcore **p)
155
{
156
        struct objcore *oc;
157
158 30179
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
159 30179
        TAKE_OBJ_NOTNULL(oc, p, OBJCORE_MAGIC);
160 30179
        if (oc->boc != NULL)
161 584
                obj_deleteboc(&oc->boc);
162 30179
        FREE_OBJ(oc);
163 30179
        wrk->stats->n_objectcore--;
164 30179
}
165
166
/*====================================================================
167
 * ObjIterate()
168
 *
169
 */
170
171
int
172 39274
ObjIterate(struct worker *wrk, struct objcore *oc,
173
    void *priv, objiterate_f *func, int final)
174
{
175 39274
        const struct obj_methods *om = obj_getmethods(oc);
176
177 39274
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
178 39274
        AN(func);
179 39274
        AN(om->objiterator);
180 39274
        return (om->objiterator(wrk, oc, priv, func, final));
181
}
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
 * ObjVAIfini() finalized iteration
245
 *
246
 *      it must be called when iteration is done, irrespective of error status
247
 */
248
249
vai_hdl
250 39788
ObjVAIinit(struct worker *wrk, struct objcore *oc, struct ws *ws,
251
    vai_notify_cb *cb, void *cb_priv)
252
{
253 39788
        const struct obj_methods *om = obj_getmethods(oc);
254
255 39788
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
256
257 39788
        if (om->vai_init == NULL)
258 0
                return (NULL);
259 39788
        return (om->vai_init(wrk, oc, ws, cb, cb_priv));
260 39788
}
261
262
int
263 108191
ObjVAIlease(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
264
{
265 108191
        struct vai_hdl_preamble *vaip = vhdl;
266
267 108191
        AN(vaip);
268 108191
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
269 108191
        AN(vaip->vai_lease);
270 108191
        return (vaip->vai_lease(wrk, vhdl, scarab));
271
}
272
273
int
274 256
ObjVAIbuffer(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
275
{
276 256
        struct vai_hdl_preamble *vaip = vhdl;
277
278 256
        AN(vaip);
279 256
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
280 256
        AN(vaip->vai_buffer);
281 256
        return (vaip->vai_buffer(wrk, vhdl, scarab));
282
}
283
284
void
285 48286
ObjVAIreturn(struct worker *wrk, vai_hdl vhdl, struct vscaret *scaret)
286
{
287 48286
        struct vai_hdl_preamble *vaip = vhdl;
288
289 48286
        AN(vaip);
290 48286
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
291 48286
        AN(vaip->vai_return);
292 48286
        vaip->vai_return(wrk, vhdl, scaret);
293 48286
}
294
295
void
296 39789
ObjVAIfini(struct worker *wrk, vai_hdl *vhdlp)
297
{
298 39789
        AN(vhdlp);
299 39789
        struct vai_hdl_preamble *vaip = *vhdlp;
300
301 39789
        AN(vaip);
302 39789
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
303 39789
        AN(vaip->vai_lease);
304 39789
        vaip->vai_fini(wrk, vhdlp);
305 39789
}
306
307
/*====================================================================
308
 * ObjGetSpace()
309
 *
310
 * This function returns a pointer and length of free space.  If there
311
 * is no free space, some will be added first.
312
 *
313
 * The "sz" argument is an input hint of how much space is desired.
314
 * 0 means "unknown", return some default size (maybe fetch_chunksize)
315
 */
316
317
int
318 929519
ObjGetSpace(struct worker *wrk, struct objcore *oc, ssize_t *sz, uint8_t **ptr)
319
{
320 929519
        const struct obj_methods *om = obj_getmethods(oc);
321
322 929519
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
323 929519
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
324 929519
        AN(sz);
325 929519
        AN(ptr);
326 929519
        assert(*sz >= 0);
327
328 929519
        AN(om->objgetspace);
329 929519
        return (om->objgetspace(wrk, oc, sz, ptr));
330
}
331
332
/*====================================================================
333
 * ObjExtend()
334
 *
335
 * This function extends the used part of the object a number of bytes
336
 * into the last space returned by ObjGetSpace()
337
 *
338
 * The final flag must be set on the last call, and it will release any
339
 * surplus space allocated.
340
 */
341
342
static void
343 906133
obj_extend_condwait(const struct objcore *oc)
344
{
345
346 906133
        if (oc->boc->transit_buffer == 0)
347 898236
                return;
348
349 7897
        assert(oc->flags & OC_F_TRANSIENT);
350 8540
        while (!(oc->flags & OC_F_CANCEL) && oc->boc->fetched_so_far >
351 8508
            oc->boc->delivered_so_far + oc->boc->transit_buffer)
352 643
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
353 906133
}
354
355
// notify of an extension of the boc or state change
356
357
static void
358 957641
obj_boc_notify(struct boc *boc)
359
{
360
        struct vai_qe *qe, *next;
361
362 957641
        PTOK(pthread_cond_broadcast(&boc->cond));
363 957641
        qe = VSLIST_FIRST(&boc->vai_q_head);
364 957641
        VSLIST_FIRST(&boc->vai_q_head) = NULL;
365 979086
        while (qe != NULL) {
366 21445
                CHECK_OBJ(qe, VAI_Q_MAGIC);
367 21445
                AN(qe->flags & VAI_QF_INQUEUE);
368 21445
                qe->flags &= ~VAI_QF_INQUEUE;
369 21445
                next = VSLIST_NEXT(qe, list);
370 21445
                VSLIST_NEXT(qe, list) = NULL;
371 21445
                qe->cb(qe->hdl, qe->priv);
372 21445
                qe = next;
373
        }
374 957641
}
375
376
void
377 917114
ObjExtend(struct worker *wrk, struct objcore *oc, ssize_t l, int final)
378
{
379 917114
        const struct obj_methods *om = obj_getmethods(oc);
380
381 917114
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
382 917114
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
383 917114
        AN(om->objextend);
384 917114
        assert(l >= 0);
385
386 917114
        if (l > 0) {
387 906132
                Lck_Lock(&oc->boc->mtx);
388 906132
                obj_extend_condwait(oc);
389 906132
                om->objextend(wrk, oc, l);
390 906132
                oc->boc->fetched_so_far += l;
391 906132
                obj_boc_notify(oc->boc);
392 906132
                Lck_Unlock(&oc->boc->mtx);
393
394 906132
                if (oc->boc->transit_buffer > 0)
395 7897
                        wrk->stats->transit_buffered += l;
396 898235
                else if (oc->flags & OC_F_TRANSIENT)
397 21501
                        wrk->stats->transit_stored += l;
398 906132
        }
399
400 917114
        assert(oc->boc->state < BOS_FINISHED);
401 917114
        if (final && om->objtrimstore != NULL)
402 30591
                om->objtrimstore(wrk, oc);
403 917114
}
404
405
/*====================================================================
406
 */
407
408
static inline void
409 74494
objSignalFetchLocked(const struct objcore *oc, uint64_t l)
410
{
411 74494
        if (oc->boc->transit_buffer > 0) {
412 15216
                assert(oc->flags & OC_F_TRANSIENT);
413
                /* Signal the new client position */
414 15216
                oc->boc->delivered_so_far = l;
415 15216
                PTOK(pthread_cond_signal(&oc->boc->cond));
416 15216
        }
417 74494
}
418
419
uint64_t
420 0
ObjWaitExtend(const struct worker *wrk, const struct objcore *oc, uint64_t l,
421
    enum boc_state_e *statep)
422
{
423
        enum boc_state_e state;
424
        uint64_t rv;
425
426 0
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
427 0
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
428 0
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
429 0
        Lck_Lock(&oc->boc->mtx);
430 0
        while (1) {
431 0
                rv = oc->boc->fetched_so_far;
432 0
                assert(l <= rv || oc->boc->state == BOS_FAILED);
433 0
                state = oc->boc->state;
434 0
                objSignalFetchLocked(oc, l);
435 0
                if (rv > l || state >= BOS_FINISHED)
436 0
                        break;
437 0
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
438
        }
439 0
        Lck_Unlock(&oc->boc->mtx);
440 0
        if (statep != NULL)
441 0
                *statep = state;
442 0
        return (rv);
443
}
444
445
// get a new extension _or_ register a notification
446
uint64_t
447 74496
ObjVAIGetExtend(struct worker *wrk, const struct objcore *oc, uint64_t l,
448
    enum boc_state_e *statep, struct vai_qe *qe)
449
{
450
        enum boc_state_e state;
451
        uint64_t rv;
452
453 74496
        (void) wrk;
454 74496
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
455 74496
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
456 74496
        CHECK_OBJ_NOTNULL(qe, VAI_Q_MAGIC);
457 74496
        Lck_Lock(&oc->boc->mtx);
458 74496
        rv = oc->boc->fetched_so_far;
459 74496
        assert(l <= rv || oc->boc->state == BOS_FAILED);
460 74496
        state = oc->boc->state;
461 74496
        objSignalFetchLocked(oc, l);
462 74496
        if (l == rv && state < BOS_FINISHED &&
463 41597
            (qe->flags & VAI_QF_INQUEUE) == 0) {
464 21603
                qe->flags |= VAI_QF_INQUEUE;
465 21603
                VSLIST_INSERT_HEAD(&oc->boc->vai_q_head, qe, list);
466 21603
        }
467 74496
        Lck_Unlock(&oc->boc->mtx);
468 74496
        if (statep != NULL)
469 74482
                *statep = state;
470 74498
        return (rv);
471
}
472
473
void
474 12429
ObjVAICancel(struct worker *wrk, struct boc *boc, struct vai_qe *qe)
475
{
476
477 12429
        (void) wrk;
478 12429
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
479 12429
        CHECK_OBJ_NOTNULL(qe, VAI_Q_MAGIC);
480
481 12429
        Lck_Lock(&boc->mtx);
482
        // inefficient, but should be rare
483 12429
        if ((qe->flags & VAI_QF_INQUEUE) != 0)
484 160
                VSLIST_REMOVE(&boc->vai_q_head, qe, vai_qe, list);
485 12429
        qe->flags = 0;
486 12429
        Lck_Unlock(&boc->mtx);
487 12429
}
488
489
/*====================================================================
490
 */
491
492
void
493 86300
ObjSetState(struct worker *wrk, struct objcore *oc, enum boc_state_e next,
494
    unsigned broadcast)
495
{
496
        const struct obj_methods *om;
497
498 86300
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
499 86300
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
500 86300
        assert(next > oc->boc->state);
501
502 86300
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
503 86300
        assert(next != BOS_FINISHED || (oc->oa_present & (1 << OA_LEN)));
504
505 86300
        if (oc->stobj->stevedore != NULL) {
506 49344
                om = oc->stobj->stevedore->methods;
507 49344
                if (om->objsetstate != NULL)
508 0
                        om->objsetstate(wrk, oc, next);
509 49344
        }
510
511 86300
        if (next == BOS_FAILED)
512 1088
                HSH_Fail(wrk, oc);
513 85212
        else if (oc->boc->state < BOS_STREAM && next >= BOS_STREAM)
514 35664
                HSH_Unbusy(wrk, oc);
515
516 86300
        Lck_Lock(&oc->boc->mtx);
517 86300
        oc->boc->state = next;
518 86300
        if (broadcast)
519 51513
                obj_boc_notify(oc->boc);
520 86300
        Lck_Unlock(&oc->boc->mtx);
521 86300
}
522
523
/*====================================================================
524
 */
525
526
enum boc_state_e
527 42392
ObjWaitState(const struct objcore *oc, enum boc_state_e want)
528
{
529
        enum boc_state_e got;
530
531 42392
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
532 42392
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
533
534 42392
        Lck_Lock(&oc->boc->mtx);
535
        /* wake up obj_extend_condwait() */
536 42392
        if (oc->flags & OC_F_CANCEL)
537 5644
                PTOK(pthread_cond_signal(&oc->boc->cond));
538 473051
        while (1) {
539 473051
                if (oc->boc->state >= want)
540 42392
                        break;
541 430659
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
542
        }
543 42392
        got = oc->boc->state;
544 42392
        Lck_Unlock(&oc->boc->mtx);
545
546 42392
        return (got);
547
}
548
549
/*====================================================================
550
 * ObjGetlen()
551
 *
552
 * This is a separate function because it may need locking
553
 */
554
555
uint64_t
556 87165
ObjGetLen(struct worker *wrk, struct objcore *oc)
557
{
558
        uint64_t len;
559
560 87165
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
561
562 87165
        AZ(ObjGetU64(wrk, oc, OA_LEN, &len));
563 87165
        return (len);
564
}
565
566
/*====================================================================
567
 * ObjSlim()
568
 *
569
 * Free the whatever storage can be freed, without freeing the actual
570
 * object yet.
571
 */
572
573
void
574 22606
ObjSlim(struct worker *wrk, struct objcore *oc)
575
{
576 22606
        const struct obj_methods *om = obj_getmethods(oc);
577
578 22606
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
579
580 22606
        if (om->objslim != NULL)
581 22607
                om->objslim(wrk, oc);
582 22608
}
583
584
/*====================================================================
585
 * Called when the boc used to populate the objcore is going away.
586
 * Useful for releasing any leftovers from Trim.
587
 */
588
589
void
590 46893
ObjBocDone(struct worker *wrk, struct objcore *oc, struct boc **boc)
591
{
592
        const struct obj_methods *m;
593
594 46893
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
595 46893
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
596 46893
        AN(boc);
597 46893
        CHECK_OBJ_NOTNULL(*boc, BOC_MAGIC);
598 46893
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
599 46893
        if (oc->stobj->stevedore != NULL) {
600 46047
                m = obj_getmethods(oc);
601 46047
                if (m->objbocdone != NULL)
602 46044
                        m->objbocdone(wrk, oc, *boc);
603 46047
        }
604 46893
        obj_deleteboc(boc);
605 46893
}
606
607
/*====================================================================
608
 */
609
void
610 29084
ObjFreeObj(struct worker *wrk, struct objcore *oc)
611
{
612 29084
        const struct obj_methods *m = obj_getmethods(oc);
613
614 29084
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
615
616 29084
        AN(m->objfree);
617 29084
        m->objfree(wrk, oc);
618 29084
        AZ(oc->stobj->stevedore);
619 29084
}
620
621
/*====================================================================
622
 * ObjHasAttr()
623
 *
624
 * Check if object has this attribute
625
 */
626
627
int
628 126302
ObjHasAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr)
629
{
630
631 126302
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
632 126302
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
633
634 126302
        if (oc->oa_present)
635 125966
                return (oc->oa_present & (1 << attr));
636
637
        /* resurrected persistent objects don't have oa_present set */
638 336
        return (ObjGetAttr(wrk, oc, attr, NULL) != NULL ? 1 : 0);
639 126302
}
640
641
/*====================================================================
642
 * ObjGetAttr()
643
 *
644
 * Get an attribute of the object.
645
 *
646
 * Returns NULL on unset or zero length attributes and len set to
647
 * zero. Returns Non-NULL otherwise and len is updated with the attributes
648
 * length.
649
 */
650
651
const void *
652 362699
ObjGetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
653
   ssize_t *len)
654
{
655 362699
        const struct obj_methods *om = obj_getmethods(oc);
656
657 362699
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
658
659 362699
        AN(om->objgetattr);
660 362699
        return (om->objgetattr(wrk, oc, attr, len));
661
}
662
663
/*====================================================================
664
 * ObjSetAttr()
665
 *
666
 * Setting fixed size attributes always succeeds.
667
 *
668
 * Setting a variable size attribute asserts if the combined size of the
669
 * variable attributes exceeds the total variable attribute space set at
670
 * object creation. If there is space it always succeeds.
671
 *
672
 * Setting an auxiliary attribute can fail.
673
 *
674
 * Resetting any variable asserts if the new length does not match the
675
 * previous length exactly.
676
 *
677
 * If ptr is Non-NULL, it points to the new content which is copied into
678
 * the attribute.  Otherwise the caller will have to do the copying.
679
 *
680
 * Return value is non-NULL on success and NULL on failure. If ptr was
681
 * non-NULL, it is an error to use the returned pointer to set the
682
 * attribute data, it is only a success indicator in that case.
683
 */
684
685
void *
686 171787
ObjSetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
687
    ssize_t len, const void *ptr)
688
{
689 171787
        const struct obj_methods *om = obj_getmethods(oc);
690
        void *r;
691
692 171787
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
693 171787
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
694
695 171787
        AN(om->objsetattr);
696 171787
        assert((int)attr < 16);
697 171787
        r = om->objsetattr(wrk, oc, attr, len, ptr);
698 171787
        if (r)
699 171777
                oc->oa_present |= (1 << attr);
700 171791
        return (r);
701
}
702
703
/*====================================================================
704
 * ObjTouch()
705
 */
706
707
void
708 52858
ObjTouch(struct worker *wrk, struct objcore *oc, vtim_real now)
709
{
710 52858
        const struct obj_methods *om = obj_getmethods(oc);
711
712 52858
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
713 52858
        if (om->objtouch != NULL)
714 52858
                om->objtouch(wrk, oc, now);
715 52858
}
716
717
/*====================================================================
718
 * Utility functions which work on top of the previous ones
719
 */
720
721
int
722 976
ObjCopyAttr(struct worker *wrk, struct objcore *oc, struct objcore *ocs,
723
    enum obj_attr attr)
724
{
725
        const void *vps;
726
        void *vpd;
727
        ssize_t l;
728
729 976
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
730 976
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
731 976
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
732 976
        CHECK_OBJ_NOTNULL(ocs, OBJCORE_MAGIC);
733
734 976
        vps = ObjGetAttr(wrk, ocs, attr, &l);
735
        // XXX: later we want to have zero-length OA's too
736 976
        if (vps == NULL || l <= 0)
737 0
                return (-1);
738 976
        vpd = ObjSetAttr(wrk, oc, attr, l, vps);
739 976
        if (vpd == NULL)
740 0
                return (-1);
741 976
        return (0);
742 976
}
743
744
int
745 36014
ObjSetXID(struct worker *wrk, struct objcore *oc, vxid_t xid)
746
{
747
        uint64_t u;
748
749 36014
        u = VXID(xid);
750 36014
        AZ(ObjSetU64(wrk, oc, OA_VXID, u));
751 36014
        return (0);
752
}
753
754
755
vxid_t
756 47311
ObjGetXID(struct worker *wrk, struct objcore *oc)
757
{
758
        vxid_t u;
759
760 47311
        AZ(ObjGetU64(wrk, oc, OA_VXID, &u.vxid));
761 47311
        return (u);
762
}
763
764
/*--------------------------------------------------------------------
765
 * There is no well-defined byteorder for IEEE-754 double and the
766
 * correct solution (frexp(3) and manual encoding) is more work
767
 * than our (weak) goal of being endian-agnostic requires at this point.
768
 * We give it a shot by memcpy'ing doubles over a uint64_t and then
769
 * BE encode that.
770
 */
771
772
int
773 36013
ObjSetDouble(struct worker *wrk, struct objcore *oc, enum obj_attr a, double t)
774
{
775
        void *vp;
776
        uint64_t u;
777
778 36013
        assert(sizeof t == sizeof u);
779 36013
        memcpy(&u, &t, sizeof u);
780 36013
        vp = ObjSetAttr(wrk, oc, a, sizeof u, NULL);
781 36013
        if (vp == NULL)
782 0
                return (-1);
783 36013
        vbe64enc(vp, u);
784 36013
        return (0);
785 36013
}
786
787
int
788 32
ObjGetDouble(struct worker *wrk, struct objcore *oc, enum obj_attr a, double *d)
789
{
790
        const void *vp;
791
        uint64_t u;
792
        ssize_t l;
793
794 32
        assert(sizeof *d == sizeof u);
795 32
        vp = ObjGetAttr(wrk, oc, a, &l);
796 32
        if (vp == NULL)
797 0
                return (-1);
798 32
        if (d != NULL) {
799 32
                assert(l == sizeof u);
800 32
                u = vbe64dec(vp);
801 32
                memcpy(d, &u, sizeof *d);
802 32
        }
803 32
        return (0);
804 32
}
805
806
/*--------------------------------------------------------------------
807
 */
808
809
int
810 80173
ObjSetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t t)
811
{
812
        void *vp;
813
814 80173
        vp = ObjSetAttr(wrk, oc, a, sizeof t, NULL);
815 80173
        if (vp == NULL)
816 0
                return (-1);
817 80173
        vbe64enc(vp, t);
818 80173
        return (0);
819 80173
}
820
821
int
822 134525
ObjGetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t *d)
823
{
824
        const void *vp;
825
        ssize_t l;
826
827 134525
        vp = ObjGetAttr(wrk, oc, a, &l);
828 134525
        if (vp == NULL || l != sizeof *d)
829 12
                return (-1);
830 134523
        if (d != NULL)
831 134518
                *d = vbe64dec(vp);
832 134523
        return (0);
833 134513
}
834
835
/*--------------------------------------------------------------------
836
 */
837
838
int
839 117832
ObjCheckFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of)
840
{
841
        const uint8_t *fp;
842
843 117832
        fp = ObjGetAttr(wrk, oc, OA_FLAGS, NULL);
844 117832
        AN(fp);
845 117832
        return ((*fp) & of);
846
}
847
848
void
849 7982
ObjSetFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of, int val)
850
{
851
        uint8_t *fp;
852
853 7982
        fp = ObjSetAttr(wrk, oc, OA_FLAGS, 1, NULL);
854 7982
        AN(fp);
855 7982
        if (val)
856 7950
                (*fp) |= of;
857
        else
858 32
                (*fp) &= ~of;
859 7982
}
860
861
/*====================================================================
862
 * Object event subscription mechanism.
863
 *
864
 * XXX: it is extremely unclear what the locking circumstances are here.
865
 */
866
867
struct oev_entry {
868
        unsigned                        magic;
869
#define OEV_MAGIC                       0xb0b7c5a1
870
        unsigned                        mask;
871
        obj_event_f                     *func;
872
        void                            *priv;
873
        VTAILQ_ENTRY(oev_entry)         list;
874
};
875
876
static VTAILQ_HEAD(,oev_entry)          oev_list;
877
static pthread_rwlock_t                 oev_rwl;
878
static unsigned                         oev_mask;
879
880
/*
881
 * NB: ObjSubscribeEvents() is not atomic:
882
 * oev_mask is checked optimistically in ObjSendEvent()
883
 */
884
uintptr_t
885 624
ObjSubscribeEvents(obj_event_f *func, void *priv, unsigned mask)
886
{
887
        struct oev_entry *oev;
888
889 624
        AN(func);
890 624
        AZ(mask & ~OEV_MASK);
891
892 624
        ALLOC_OBJ(oev, OEV_MAGIC);
893 624
        AN(oev);
894 624
        oev->func = func;
895 624
        oev->priv = priv;
896 624
        oev->mask = mask;
897 624
        PTOK(pthread_rwlock_wrlock(&oev_rwl));
898 624
        VTAILQ_INSERT_TAIL(&oev_list, oev, list);
899 624
        oev_mask |= mask;
900 624
        PTOK(pthread_rwlock_unlock(&oev_rwl));
901 624
        return ((uintptr_t)oev);
902
}
903
904
void
905 16
ObjUnsubscribeEvents(uintptr_t *handle)
906
{
907 16
        struct oev_entry *oev, *oev2 = NULL;
908 16
        unsigned newmask = 0;
909
910 16
        AN(handle);
911 16
        AN(*handle);
912 16
        PTOK(pthread_rwlock_wrlock(&oev_rwl));
913 32
        VTAILQ_FOREACH(oev, &oev_list, list) {
914 16
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
915 16
                if ((uintptr_t)oev == *handle)
916 16
                        oev2 = oev;
917
                else
918 0
                        newmask |= oev->mask;
919 16
        }
920 16
        AN(oev2);
921 16
        VTAILQ_REMOVE(&oev_list, oev2, list);
922 16
        oev_mask = newmask;
923 16
        AZ(newmask & ~OEV_MASK);
924 16
        PTOK(pthread_rwlock_unlock(&oev_rwl));
925 16
        FREE_OBJ(oev2);
926 16
        *handle = 0;
927 16
}
928
929
void
930 53707
ObjSendEvent(struct worker *wrk, struct objcore *oc, unsigned event)
931
{
932
        struct oev_entry *oev;
933
934 53707
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
935 53707
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
936 53707
        AN(event & OEV_MASK);
937 53707
        AZ(event & ~OEV_MASK);
938 53707
        if (!(event & oev_mask))
939 52283
                return;
940
941 1424
        PTOK(pthread_rwlock_rdlock(&oev_rwl));
942 3104
        VTAILQ_FOREACH(oev, &oev_list, list) {
943 1680
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
944 1680
                if (event & oev->mask)
945 1680
                        oev->func(wrk, oev->priv, oc, event);
946 1680
        }
947 1424
        PTOK(pthread_rwlock_unlock(&oev_rwl));
948
949 53707
}
950
951
void
952 15216
ObjInit(void)
953
{
954 15216
        VTAILQ_INIT(&oev_list);
955 15216
        PTOK(pthread_rwlock_init(&oev_rwl, NULL));
956 15216
}