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