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