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 1306328
obj_getmethods(const struct objcore *oc)
94
{
95
96 1306328
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
97 1306328
        CHECK_OBJ_NOTNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
98 1306328
        AN(oc->stobj->stevedore->methods);
99 1306328
        return (oc->stobj->stevedore->methods);
100
}
101
102
static struct boc *
103 26182
obj_newboc(void)
104
{
105
        struct boc *boc;
106
107 26182
        ALLOC_OBJ(boc, BOC_MAGIC);
108 26182
        AN(boc);
109 26182
        Lck_New(&boc->mtx, lck_busyobj);
110 26182
        PTOK(pthread_cond_init(&boc->cond, NULL));
111 26182
        boc->refcount = 1;
112 26182
        return (boc);
113
}
114
115
static void
116 23870
obj_deleteboc(struct boc **p)
117
{
118
        struct boc *boc;
119
120 23870
        TAKE_OBJ_NOTNULL(boc, p, BOC_MAGIC);
121 23870
        Lck_Delete(&boc->mtx);
122 23870
        PTOK(pthread_cond_destroy(&boc->cond));
123 23870
        free(boc->vary);
124 23870
        FREE_OBJ(boc);
125 23870
}
126
127
/*====================================================================
128
 * ObjNew()
129
 *
130
 */
131
132
struct objcore *
133 26182
ObjNew(const struct worker *wrk)
134
{
135
        struct objcore *oc;
136
137 26182
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
138
139 26182
        ALLOC_OBJ(oc, OBJCORE_MAGIC);
140 26182
        AN(oc);
141 26182
        wrk->stats->n_objectcore++;
142 26182
        oc->last_lru = NAN;
143 26182
        oc->boc = obj_newboc();
144
145 26182
        return (oc);
146
}
147
148
/*====================================================================
149
 * ObjDestroy()
150
 *
151
 */
152
153
void
154 15204
ObjDestroy(const struct worker *wrk, struct objcore **p)
155
{
156
        struct objcore *oc;
157
158 15204
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
159 15204
        TAKE_OBJ_NOTNULL(oc, p, OBJCORE_MAGIC);
160 15204
        if (oc->boc != NULL)
161 302
                obj_deleteboc(&oc->boc);
162 15204
        FREE_OBJ(oc);
163 15204
        wrk->stats->n_objectcore--;
164 15204
}
165
166
/*====================================================================
167
 * ObjIterate()
168
 *
169
 */
170
171
int
172 19726
ObjIterate(struct worker *wrk, struct objcore *oc,
173
    void *priv, objiterate_f *func, int final)
174
{
175 19726
        const struct obj_methods *om = obj_getmethods(oc);
176
177 19726
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
178 19726
        AN(func);
179 19726
        AN(om->objiterator);
180 19726
        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 19983
ObjVAIinit(struct worker *wrk, struct objcore *oc, struct ws *ws,
251
    vai_notify_cb *cb, void *cb_priv)
252
{
253 19983
        const struct obj_methods *om = obj_getmethods(oc);
254
255 19983
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
256
257 19983
        if (om->vai_init == NULL)
258 0
                return (NULL);
259 19983
        return (om->vai_init(wrk, oc, ws, cb, cb_priv));
260 19983
}
261
262
int
263 54353
ObjVAIlease(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
264
{
265 54353
        struct vai_hdl_preamble *vaip = vhdl;
266
267 54353
        AN(vaip);
268 54353
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
269 54353
        AN(vaip->vai_lease);
270 54353
        return (vaip->vai_lease(wrk, vhdl, scarab));
271
}
272
273
int
274 128
ObjVAIbuffer(struct worker *wrk, vai_hdl vhdl, struct vscarab *scarab)
275
{
276 128
        struct vai_hdl_preamble *vaip = vhdl;
277
278 128
        AN(vaip);
279 128
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
280 128
        AN(vaip->vai_buffer);
281 128
        return (vaip->vai_buffer(wrk, vhdl, scarab));
282
}
283
284
void
285 24249
ObjVAIreturn(struct worker *wrk, vai_hdl vhdl, struct vscaret *scaret)
286
{
287 24249
        struct vai_hdl_preamble *vaip = vhdl;
288
289 24249
        AN(vaip);
290 24249
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
291 24249
        AN(vaip->vai_return);
292 24249
        vaip->vai_return(wrk, vhdl, scaret);
293 24249
}
294
295
void
296 19982
ObjVAIfini(struct worker *wrk, vai_hdl *vhdlp)
297
{
298 19982
        AN(vhdlp);
299 19982
        struct vai_hdl_preamble *vaip = *vhdlp;
300
301 19982
        AN(vaip);
302 19982
        assert(vaip->magic2 == VAI_HDL_PREAMBLE_MAGIC2);
303 19982
        AN(vaip->vai_lease);
304 19982
        vaip->vai_fini(wrk, vhdlp);
305 19982
}
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 464596
ObjGetSpace(struct worker *wrk, struct objcore *oc, ssize_t *sz, uint8_t **ptr)
319
{
320 464596
        const struct obj_methods *om = obj_getmethods(oc);
321
322 464596
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
323 464596
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
324 464596
        AN(sz);
325 464596
        AN(ptr);
326 464596
        assert(*sz >= 0);
327
328 464596
        AN(om->objgetspace);
329 464596
        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 452871
obj_extend_condwait(const struct objcore *oc)
344
{
345
346 452871
        if (oc->boc->transit_buffer == 0)
347 448925
                return;
348
349 3946
        assert(oc->flags & OC_F_TRANSIENT);
350 4285
        while (!(oc->flags & OC_F_CANCEL) && oc->boc->fetched_so_far >
351 4269
            oc->boc->delivered_so_far + oc->boc->transit_buffer)
352 339
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
353 452871
}
354
355
// notify of an extension of the boc or state change
356
357
static void
358 478777
obj_boc_notify(struct boc *boc)
359
{
360
        struct vai_qe *qe, *next;
361
362 478777
        PTOK(pthread_cond_broadcast(&boc->cond));
363 478777
        qe = VSLIST_FIRST(&boc->vai_q_head);
364 478777
        VSLIST_FIRST(&boc->vai_q_head) = NULL;
365 489548
        while (qe != NULL) {
366 10771
                CHECK_OBJ(qe, VAI_Q_MAGIC);
367 10771
                AN(qe->flags & VAI_QF_INQUEUE);
368 10771
                qe->flags &= ~VAI_QF_INQUEUE;
369 10771
                next = VSLIST_NEXT(qe, list);
370 10771
                VSLIST_NEXT(qe, list) = NULL;
371 10771
                qe->cb(qe->hdl, qe->priv);
372 10771
                qe = next;
373
        }
374 478777
}
375
376
void
377 458369
ObjExtend(struct worker *wrk, struct objcore *oc, ssize_t l, int final)
378
{
379 458369
        const struct obj_methods *om = obj_getmethods(oc);
380
381 458369
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
382 458369
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
383 458369
        AN(om->objextend);
384 458369
        assert(l >= 0);
385
386 458369
        if (l > 0) {
387 452872
                Lck_Lock(&oc->boc->mtx);
388 452872
                obj_extend_condwait(oc);
389 452872
                om->objextend(wrk, oc, l);
390 452872
                oc->boc->fetched_so_far += l;
391 452872
                obj_boc_notify(oc->boc);
392 452872
                Lck_Unlock(&oc->boc->mtx);
393
394 452872
                if (oc->boc->transit_buffer > 0)
395 3946
                        wrk->stats->transit_buffered += l;
396 448926
                else if (oc->flags & OC_F_TRANSIENT)
397 10838
                        wrk->stats->transit_stored += l;
398 452872
        }
399
400 458369
        assert(oc->boc->state < BOS_FINISHED);
401 458369
        if (final && om->objtrimstore != NULL)
402 15383
                om->objtrimstore(wrk, oc);
403 458369
}
404
405
/*====================================================================
406
 */
407
408
static inline void
409 37480
objSignalFetchLocked(const struct objcore *oc, uint64_t l)
410
{
411 37480
        if (oc->boc->transit_buffer > 0) {
412 7709
                assert(oc->flags & OC_F_TRANSIENT);
413
                /* Signal the new client position */
414 7709
                oc->boc->delivered_so_far = l;
415 7709
                PTOK(pthread_cond_signal(&oc->boc->cond));
416 7709
        }
417 37480
}
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 37480
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 37480
        (void) wrk;
454 37480
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
455 37480
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
456 37480
        CHECK_OBJ_NOTNULL(qe, VAI_Q_MAGIC);
457 37480
        Lck_Lock(&oc->boc->mtx);
458 37480
        rv = oc->boc->fetched_so_far;
459 37480
        assert(l <= rv || oc->boc->state == BOS_FAILED);
460 37480
        state = oc->boc->state;
461 37480
        objSignalFetchLocked(oc, l);
462 37480
        if (l == rv && state < BOS_FINISHED &&
463 20911
            (qe->flags & VAI_QF_INQUEUE) == 0) {
464 10848
                qe->flags |= VAI_QF_INQUEUE;
465 10848
                VSLIST_INSERT_HEAD(&oc->boc->vai_q_head, qe, list);
466 10848
        }
467 37480
        Lck_Unlock(&oc->boc->mtx);
468 37480
        if (statep != NULL)
469 37491
                *statep = state;
470 37506
        return (rv);
471
}
472
473
void
474 6271
ObjVAICancel(struct worker *wrk, struct boc *boc, struct vai_qe *qe)
475
{
476
477 6271
        (void) wrk;
478 6271
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
479 6271
        CHECK_OBJ_NOTNULL(qe, VAI_Q_MAGIC);
480
481 6271
        Lck_Lock(&boc->mtx);
482
        // inefficient, but should be rare
483 6271
        if ((qe->flags & VAI_QF_INQUEUE) != 0)
484 78
                VSLIST_REMOVE(&boc->vai_q_head, qe, vai_qe, list);
485 6271
        qe->flags = 0;
486 6271
        Lck_Unlock(&boc->mtx);
487 6271
}
488
489
/*====================================================================
490
 */
491
492
void
493 43382
ObjSetState(struct worker *wrk, struct objcore *oc, enum boc_state_e next,
494
    unsigned broadcast)
495
{
496
        const struct obj_methods *om;
497
498 43382
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
499 43382
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
500 43382
        assert(next > oc->boc->state);
501
502 43382
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
503 43382
        assert(next != BOS_FINISHED || (oc->oa_present & (1 << OA_LEN)));
504
505 43382
        if (oc->stobj->stevedore != NULL) {
506 24822
                om = oc->stobj->stevedore->methods;
507 24822
                if (om->objsetstate != NULL)
508 0
                        om->objsetstate(wrk, oc, next);
509 24822
        }
510
511 43382
        if (next == BOS_FAILED)
512 544
                HSH_Fail(wrk, oc);
513 42838
        else if (oc->boc->state < BOS_STREAM && next >= BOS_STREAM)
514 17928
                HSH_Unbusy(wrk, oc);
515
516 43382
        Lck_Lock(&oc->boc->mtx);
517 43382
        oc->boc->state = next;
518 43382
        if (broadcast)
519 25908
                obj_boc_notify(oc->boc);
520 43382
        Lck_Unlock(&oc->boc->mtx);
521 43382
}
522
523
/*====================================================================
524
 */
525
526
enum boc_state_e
527 21346
ObjWaitState(const struct objcore *oc, enum boc_state_e want)
528
{
529
        enum boc_state_e got;
530
531 21346
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
532 21346
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
533
534 21346
        Lck_Lock(&oc->boc->mtx);
535
        /* wake up obj_extend_condwait() */
536 21346
        if (oc->flags & OC_F_CANCEL)
537 2875
                PTOK(pthread_cond_signal(&oc->boc->cond));
538 251592
        while (1) {
539 251592
                if (oc->boc->state >= want)
540 21346
                        break;
541 230246
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx);
542
        }
543 21346
        got = oc->boc->state;
544 21346
        Lck_Unlock(&oc->boc->mtx);
545
546 21346
        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 43725
ObjGetLen(struct worker *wrk, struct objcore *oc)
557
{
558
        uint64_t len;
559
560 43725
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
561
562 43725
        AZ(ObjGetU64(wrk, oc, OA_LEN, &len));
563 43725
        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 11391
ObjSlim(struct worker *wrk, struct objcore *oc)
575
{
576 11391
        const struct obj_methods *om = obj_getmethods(oc);
577
578 11391
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
579
580 11391
        if (om->objslim != NULL)
581 11391
                om->objslim(wrk, oc);
582 11391
}
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 23568
ObjBocDone(struct worker *wrk, struct objcore *oc, struct boc **boc)
591
{
592
        const struct obj_methods *m;
593
594 23568
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
595 23568
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
596 23568
        AN(boc);
597 23568
        CHECK_OBJ_NOTNULL(*boc, BOC_MAGIC);
598 23568
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
599 23568
        if (oc->stobj->stevedore != NULL) {
600 23144
                m = obj_getmethods(oc);
601 23144
                if (m->objbocdone != NULL)
602 23144
                        m->objbocdone(wrk, oc, *boc);
603 23144
        }
604 23568
        obj_deleteboc(boc);
605 23568
}
606
607
/*====================================================================
608
 */
609
void
610 14646
ObjFreeObj(struct worker *wrk, struct objcore *oc)
611
{
612 14646
        const struct obj_methods *m = obj_getmethods(oc);
613
614 14646
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
615
616 14646
        AN(m->objfree);
617 14646
        m->objfree(wrk, oc);
618 14646
        AZ(oc->stobj->stevedore);
619 14646
}
620
621
/*====================================================================
622
 * ObjHasAttr()
623
 *
624
 * Check if object has this attribute
625
 */
626
627
int
628 63254
ObjHasAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr)
629
{
630
631 63254
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
632 63254
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
633
634 63254
        if (oc->oa_present)
635 63086
                return (oc->oa_present & (1 << attr));
636
637
        /* resurrected persistent objects don't have oa_present set */
638 168
        return (ObjGetAttr(wrk, oc, attr, NULL) != NULL ? 1 : 0);
639 63254
}
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 181781
ObjGetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
653
   ssize_t *len)
654
{
655 181781
        const struct obj_methods *om = obj_getmethods(oc);
656
657 181781
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
658
659 181781
        AN(om->objgetattr);
660 181781
        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 86281
ObjSetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
687
    ssize_t len, const void *ptr)
688
{
689 86281
        const struct obj_methods *om = obj_getmethods(oc);
690
        void *r;
691
692 86281
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
693 86281
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
694
695 86281
        AN(om->objsetattr);
696 86281
        assert((int)attr < 16);
697 86281
        r = om->objsetattr(wrk, oc, attr, len, ptr);
698 86281
        if (r)
699 86282
                oc->oa_present |= (1 << attr);
700 86291
        return (r);
701
}
702
703
/*====================================================================
704
 * ObjTouch()
705
 */
706
707
void
708 26526
ObjTouch(struct worker *wrk, struct objcore *oc, vtim_real now)
709
{
710 26526
        const struct obj_methods *om = obj_getmethods(oc);
711
712 26526
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
713 26526
        if (om->objtouch != NULL)
714 26524
                om->objtouch(wrk, oc, now);
715 26526
}
716
717
/*====================================================================
718
 * Utility functions which work on top of the previous ones
719
 */
720
721
int
722 488
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 488
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
730 488
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
731 488
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
732 488
        CHECK_OBJ_NOTNULL(ocs, OBJCORE_MAGIC);
733
734 488
        vps = ObjGetAttr(wrk, ocs, attr, &l);
735
        // XXX: later we want to have zero-length OA's too
736 488
        if (vps == NULL || l <= 0)
737 0
                return (-1);
738 488
        vpd = ObjSetAttr(wrk, oc, attr, l, vps);
739 488
        if (vpd == NULL)
740 0
                return (-1);
741 488
        return (0);
742 488
}
743
744
int
745 18104
ObjSetXID(struct worker *wrk, struct objcore *oc, vxid_t xid)
746
{
747
        uint64_t u;
748
749 18104
        u = VXID(xid);
750 18104
        AZ(ObjSetU64(wrk, oc, OA_VXID, u));
751 18104
        return (0);
752
}
753
754
755
vxid_t
756 23652
ObjGetXID(struct worker *wrk, struct objcore *oc)
757
{
758
        vxid_t u;
759
760 23652
        AZ(ObjGetU64(wrk, oc, OA_VXID, &u.vxid));
761 23652
        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 18104
ObjSetDouble(struct worker *wrk, struct objcore *oc, enum obj_attr a, double t)
774
{
775
        void *vp;
776
        uint64_t u;
777
778 18104
        assert(sizeof t == sizeof u);
779 18104
        memcpy(&u, &t, sizeof u);
780 18104
        vp = ObjSetAttr(wrk, oc, a, sizeof u, NULL);
781 18104
        if (vp == NULL)
782 0
                return (-1);
783 18104
        vbe64enc(vp, u);
784 18104
        return (0);
785 18104
}
786
787
int
788 16
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 16
        assert(sizeof *d == sizeof u);
795 16
        vp = ObjGetAttr(wrk, oc, a, &l);
796 16
        if (vp == NULL)
797 0
                return (-1);
798 16
        if (d != NULL) {
799 16
                assert(l == sizeof u);
800 16
                u = vbe64dec(vp);
801 16
                memcpy(d, &u, sizeof *d);
802 16
        }
803 16
        return (0);
804 16
}
805
806
/*--------------------------------------------------------------------
807
 */
808
809
int
810 40286
ObjSetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t t)
811
{
812
        void *vp;
813
814 40286
        vp = ObjSetAttr(wrk, oc, a, sizeof t, NULL);
815 40286
        if (vp == NULL)
816 0
                return (-1);
817 40286
        vbe64enc(vp, t);
818 40286
        return (0);
819 40286
}
820
821
int
822 67403
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 67403
        vp = ObjGetAttr(wrk, oc, a, &l);
828 67403
        if (vp == NULL || l != sizeof *d)
829 2
                return (-1);
830 67403
        if (d != NULL)
831 67402
                *d = vbe64dec(vp);
832 67403
        return (0);
833 67401
}
834
835
/*--------------------------------------------------------------------
836
 */
837
838
int
839 59111
ObjCheckFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of)
840
{
841
        const uint8_t *fp;
842
843 59111
        fp = ObjGetAttr(wrk, oc, OA_FLAGS, NULL);
844 59111
        AN(fp);
845 59111
        return ((*fp) & of);
846
}
847
848
void
849 3990
ObjSetFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of, int val)
850
{
851
        uint8_t *fp;
852
853 3990
        fp = ObjSetAttr(wrk, oc, OA_FLAGS, 1, NULL);
854 3990
        AN(fp);
855 3990
        if (val)
856 3974
                (*fp) |= of;
857
        else
858 16
                (*fp) &= ~of;
859 3990
}
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 312
ObjSubscribeEvents(obj_event_f *func, void *priv, unsigned mask)
886
{
887
        struct oev_entry *oev;
888
889 312
        AN(func);
890 312
        AZ(mask & ~OEV_MASK);
891
892 312
        ALLOC_OBJ(oev, OEV_MAGIC);
893 312
        AN(oev);
894 312
        oev->func = func;
895 312
        oev->priv = priv;
896 312
        oev->mask = mask;
897 312
        PTOK(pthread_rwlock_wrlock(&oev_rwl));
898 312
        VTAILQ_INSERT_TAIL(&oev_list, oev, list);
899 312
        oev_mask |= mask;
900 312
        PTOK(pthread_rwlock_unlock(&oev_rwl));
901 312
        return ((uintptr_t)oev);
902
}
903
904
void
905 8
ObjUnsubscribeEvents(uintptr_t *handle)
906
{
907 8
        struct oev_entry *oev, *oev2 = NULL;
908 8
        unsigned newmask = 0;
909
910 8
        AN(handle);
911 8
        AN(*handle);
912 8
        PTOK(pthread_rwlock_wrlock(&oev_rwl));
913 16
        VTAILQ_FOREACH(oev, &oev_list, list) {
914 8
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
915 8
                if ((uintptr_t)oev == *handle)
916 8
                        oev2 = oev;
917
                else
918 0
                        newmask |= oev->mask;
919 8
        }
920 8
        AN(oev2);
921 8
        VTAILQ_REMOVE(&oev_list, oev2, list);
922 8
        oev_mask = newmask;
923 8
        AZ(newmask & ~OEV_MASK);
924 8
        PTOK(pthread_rwlock_unlock(&oev_rwl));
925 8
        FREE_OBJ(oev2);
926 8
        *handle = 0;
927 8
}
928
929
void
930 26886
ObjSendEvent(struct worker *wrk, struct objcore *oc, unsigned event)
931
{
932
        struct oev_entry *oev;
933
934 26886
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
935 26886
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
936 26886
        AN(event & OEV_MASK);
937 26886
        AZ(event & ~OEV_MASK);
938 26886
        if (!(event & oev_mask))
939 26174
                return;
940
941 712
        PTOK(pthread_rwlock_rdlock(&oev_rwl));
942 1552
        VTAILQ_FOREACH(oev, &oev_list, list) {
943 840
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
944 840
                if (event & oev->mask)
945 840
                        oev->func(wrk, oev->priv, oc, event);
946 840
        }
947 712
        PTOK(pthread_rwlock_unlock(&oev_rwl));
948
949 26886
}
950
951
void
952 7631
ObjInit(void)
953
{
954 7631
        VTAILQ_INIT(&oev_list);
955 7631
        PTOK(pthread_rwlock_init(&oev_rwl, NULL));
956 7631
}