vinyl-cache/bin/vinyld/cache/cache_hash.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * This is the central hash-table code, it relies on a chosen hash
31
 * implementation only for the actual hashing, all the housekeeping
32
 * happens here.
33
 *
34
 * We have two kinds of structures, objecthead and object.  An objecthead
35
 * corresponds to a given (Host:, URL) tuple, and the objects hung from
36
 * the objecthead may represent various variations (ie: Vary: header,
37
 * different TTL etc) instances of that web-entity.
38
 *
39
 * Each objecthead has a mutex which locks both its own fields, the
40
 * list of objects and fields in the objects.
41
 *
42
 * The hash implementation must supply a reference count facility on
43
 * the objecthead, and return with a reference held after a lookup.
44
 *
45
 * Lookups in the hash implementation returns with a ref held and each
46
 * object hung from the objhead holds a ref as well.
47
 *
48
 * Objects have refcounts which are locked by the objecthead mutex.
49
 *
50
 * New objects are always marked busy, and they can go from busy to
51
 * not busy only once.
52
 */
53
54
#include "config.h"
55
56
#include <stdio.h>
57
#include <stdlib.h>
58
59
#include "cache_int.h"
60
61
#include "cache/cache_objhead.h"
62
#include "cache/cache_transport.h"
63
64
#include "hash/hash_slinger.h"
65
66
#include "vsha256.h"
67
68
struct rush {
69
        unsigned                magic;
70
#define RUSH_MAGIC              0xa1af5f01
71
        VTAILQ_HEAD(,req)       reqs;
72
};
73
74
static const struct hash_slinger *hash;
75
#define PRIVATE_OH_EXP 7
76
static struct objhead private_ohs[1 << PRIVATE_OH_EXP];
77
78
static void hsh_rush1(const struct worker *, struct objcore *,
79
    struct rush *);
80
static void hsh_rush2(struct worker *, struct rush *);
81
static int hsh_deref_objhead(struct worker *wrk, struct objhead **poh);
82
static int hsh_deref_objhead_unlock(struct worker *wrk, struct objhead **poh,
83
    struct objcore *oc);
84
static int hsh_deref_objcore_unlock(struct worker *, struct objcore **);
85
86
/*---------------------------------------------------------------------*/
87
88
#define VCF_RETURN(x) const struct vcf_return VCF_##x[1] = { \
89
        { .name = #x, } \
90
};
91
92
VCF_RETURNS()
93
#undef VCF_RETURN
94
95
/*---------------------------------------------------------------------*/
96
97
static void
98 504728
hsh_initobjhead(struct objhead *oh)
99
{
100
101 504728
        XXXAN(oh);
102 504728
        INIT_OBJ(oh, OBJHEAD_MAGIC);
103 504728
        oh->refcnt = 1;
104 504728
        oh->waitinglist_gen = 1;
105 504728
        VTAILQ_INIT(&oh->objcs);
106 504728
        VTAILQ_INIT(&oh->waitinglist);
107 504728
        Lck_New(&oh->mtx, lck_objhdr);
108 504728
}
109
110
static struct objhead *
111 6552
hsh_newobjhead(void)
112
{
113 6552
        struct objhead *oh = malloc(sizeof *oh);
114 6552
        hsh_initobjhead(oh);
115 6552
        return (oh);
116
}
117
118
/*---------------------------------------------------------------------*/
119
/* Precreate an objhead and object for later use */
120
static void
121 11027
hsh_prealloc(struct worker *wrk)
122
{
123
124 11027
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
125
126 11027
        if (wrk->wpriv->nobjcore == NULL)
127 7465
                wrk->wpriv->nobjcore = ObjNew(wrk);
128 11027
        CHECK_OBJ_NOTNULL(wrk->wpriv->nobjcore, OBJCORE_MAGIC);
129
130 11027
        if (wrk->wpriv->nobjhead == NULL) {
131 6552
                wrk->wpriv->nobjhead = hsh_newobjhead();
132 6552
                wrk->stats->n_objecthead++;
133 6552
        }
134 11027
        CHECK_OBJ_NOTNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
135
136 11027
        if (hash->prep != NULL)
137 11003
                hash->prep(wrk);
138 11027
}
139
140
/*---------------------------------------------------------------------*/
141
142
// https://probablydance.com/2018/06/16/fibonacci-hashing-the-optimization-that-the-world-forgot-or-a-better-alternative-to-integer-modulo/
143
static inline size_t
144 5912
fib(uint64_t n, uint8_t bits)
145
{
146 5912
        const uint64_t gr = 11400714819323198485LLU;
147
        uint64_t r;
148
149 5912
        r = n * gr;
150 5912
        r >>= (sizeof(gr) * 8) - bits;
151 5912
        assert(r < (size_t)1 << bits);
152 5912
        return ((size_t)r);
153
}
154
155
struct objcore *
156 5912
HSH_Private(const struct worker *wrk)
157
{
158
        struct objcore *oc;
159
        struct objhead *oh;
160
161 5912
        oh = &private_ohs[fib((uintptr_t)wrk, PRIVATE_OH_EXP)];
162 5912
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
163
164 5912
        oc = ObjNew(wrk);
165 5912
        AN(oc);
166 5912
        oc->refcnt = 1;
167 5912
        oc->objhead = oh;
168 5912
        oc->flags |= OC_F_PRIVATE;
169 5912
        Lck_Lock(&oh->mtx);
170 5912
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
171 5912
        oh->refcnt++;
172 5912
        Lck_Unlock(&oh->mtx);
173 5912
        return (oc);
174
}
175
176
/*---------------------------------------------------------------------*/
177
178
void
179 39199
HSH_Cleanup(const struct worker *wrk)
180
{
181
182 39199
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
183 39199
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
184 39199
        if (wrk->wpriv->nobjcore != NULL)
185 548
                ObjDestroy(wrk, &wrk->wpriv->nobjcore);
186
187 39199
        if (wrk->wpriv->nobjhead != NULL) {
188 761
                CHECK_OBJ(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
189 761
                Lck_Delete(&wrk->wpriv->nobjhead->mtx);
190 761
                FREE_OBJ(wrk->wpriv->nobjhead);
191 761
                wrk->stats->n_objecthead--;
192 761
        }
193 39199
        if (wrk->wpriv->nhashpriv != NULL) {
194
                /* XXX: If needed, add slinger method for this */
195 1734
                free(wrk->wpriv->nhashpriv);
196 1734
                wrk->wpriv->nhashpriv = NULL;
197 1734
        }
198 39199
}
199
200
void
201 0
HSH_DeleteObjHead(const struct worker *wrk, struct objhead *oh)
202
{
203 0
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
204 0
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
205
206 0
        AZ(oh->refcnt);
207 0
        assert(VTAILQ_EMPTY(&oh->objcs));
208 0
        assert(VTAILQ_EMPTY(&oh->waitinglist));
209 0
        Lck_Delete(&oh->mtx);
210 0
        wrk->stats->n_objecthead--;
211 0
        FREE_OBJ(oh);
212 0
}
213
214
void
215 61076
HSH_AddString(struct req *req, void *ctx, const char *str)
216
{
217
218 61076
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
219 61076
        AN(ctx);
220 61076
        if (str != NULL) {
221 30534
                VSHA256_Update(ctx, str, vstrlen(str));
222 30534
                VSLbs(req->vsl, SLT_Hash, TOSTRAND(str));
223 30534
        } else
224 30542
                VSHA256_Update(ctx, &str, 1);
225 61076
}
226
227
/*---------------------------------------------------------------------
228
 * This is a debugging hack to enable testing of boundary conditions
229
 * in the hash algorithm.
230
 * We trap the first 9 different digests and translate them to different
231
 * digests with edge bit conditions
232
 */
233
234
static struct hsh_magiclist {
235
        unsigned char was[VSHA256_LEN];
236
        unsigned char now[VSHA256_LEN];
237
} hsh_magiclist[] = {
238
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
242
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
246
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
247
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
249
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } },
250
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40 } },
254
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
255
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
256
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 } },
258
        { .now = {      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
259
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
260
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
261
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
262
        { .now = {      0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
263
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
264
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
265
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
266
        { .now = {      0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
268
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
270
        { .now = {      0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
271
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
272
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
274
};
275
276
#define HSH_NMAGIC vcountof(hsh_magiclist)
277
278
static void
279 72
hsh_testmagic(void *result)
280
{
281
        size_t i, j;
282
        static size_t nused = 0;
283
284 360
        for (i = 0; i < nused; i++)
285 324
                if (!memcmp(hsh_magiclist[i].was, result, VSHA256_LEN))
286 36
                        break;
287 72
        if (i == nused && i < HSH_NMAGIC)
288 36
                memcpy(hsh_magiclist[nused++].was, result, VSHA256_LEN);
289 72
        if (i == nused)
290 0
                return;
291 72
        assert(i < HSH_NMAGIC);
292 72
        fprintf(stderr, "HASHMAGIC: <");
293 2376
        for (j = 0; j < VSHA256_LEN; j++)
294 2304
                fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
295 72
        fprintf(stderr, "> -> <");
296 72
        memcpy(result, hsh_magiclist[i].now, VSHA256_LEN);
297 2376
        for (j = 0; j < VSHA256_LEN; j++)
298 2304
                fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
299 72
        fprintf(stderr, ">\n");
300 72
}
301
302
/*---------------------------------------------------------------------
303
 * Insert an object which magically appears out of nowhere or, more likely,
304
 * comes off some persistent storage device.
305
 * Insert it with a reference held.
306
 */
307
308
void
309 68
HSH_Insert(struct worker *wrk, const void *digest, struct objcore *oc,
310
    struct ban *ban)
311
{
312
        struct objhead *oh;
313
        struct rush rush;
314
315 68
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
316 68
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
317 68
        AN(digest);
318 68
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
319 68
        AN(ban);
320 68
        AZ(oc->flags & (OC_F_BUSY | OC_F_PRIVATE));
321 68
        assert(oc->refcnt == 1);
322 68
        INIT_OBJ(&rush, RUSH_MAGIC);
323
324 68
        hsh_prealloc(wrk);
325
326 68
        AN(wrk->wpriv->nobjhead);
327 68
        oh = hash->lookup(wrk, digest, &wrk->wpriv->nobjhead);
328 68
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
329 68
        Lck_AssertHeld(&oh->mtx);
330 68
        assert(oh->refcnt > 0);
331
332
        /* Mark object busy and insert (precreated) objcore in
333
           objecthead. The new object inherits our objhead reference. */
334 68
        oc->objhead = oh;
335 68
        oc->flags |= OC_F_BUSY;
336 68
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
337 68
        EXP_RefNewObjcore(oc);
338 68
        Lck_Unlock(&oh->mtx);
339
340 68
        BAN_RefBan(oc, ban);
341 68
        AN(oc->ban);
342
343
        /* Move the object first in the oh list, unbusy it and run the
344
           waitinglist if necessary */
345 68
        Lck_Lock(&oh->mtx);
346 68
        oc->flags &= ~OC_F_BUSY;
347 68
        VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
348 68
        VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list);
349 68
        if (!VTAILQ_EMPTY(&oh->waitinglist))
350 0
                hsh_rush1(wrk, oc, &rush);
351 68
        Lck_Unlock(&oh->mtx);
352 68
        hsh_rush2(wrk, &rush);
353
354 68
        EXP_Insert(wrk, oc);
355 68
}
356
357
/*---------------------------------------------------------------------
358
 */
359
360
static struct objcore *
361 6293
hsh_insert_busyobj(const struct worker *wrk, struct objhead *oh)
362
{
363
        struct objcore *oc;
364
365 6293
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
366 6293
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
367 6293
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
368 6293
        Lck_AssertHeld(&oh->mtx);
369
370 6293
        oc = wrk->wpriv->nobjcore;
371 6293
        wrk->wpriv->nobjcore = NULL;
372 6293
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
373
374 6293
        AZ(oc->flags & OC_F_BUSY);
375 6293
        oc->flags |= OC_F_BUSY;
376 6293
        oc->refcnt = 1;         /* Owned by busyobj */
377 6293
        oc->objhead = oh;
378 6293
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
379 6293
        return (oc);
380
}
381
382
/*---------------------------------------------------------------------
383
 */
384
385
static int
386 15813
hsh_vry_match(const struct req *req, struct objcore *oc, const uint8_t *vary)
387
{
388
389 15813
        if (req->hash_ignore_vary)
390 4
                return (1);
391 15809
        if (vary == NULL) {
392 15803
                if (! ObjHasAttr(req->wrk, oc, OA_VARY))
393 4515
                        return (1);
394 11288
                vary = ObjGetAttr(req->wrk, oc, OA_VARY, NULL);
395 11288
                AN(vary);
396 11288
        }
397 11294
        return (VRY_Match(req, vary));
398 15813
}
399
400
static unsigned
401 244
hsh_rush_match(const struct req *req)
402
{
403
        struct objcore *oc;
404
405 244
        oc = req->objcore;
406 244
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
407 244
        assert(oc->refcnt > 0);
408
409 244
        AZ(oc->flags & (OC_F_BUSY | OC_F_PRIVATE));
410 244
        if (oc->flags & (OC_F_WITHDRAWN|OC_F_HFM|OC_F_HFP|OC_F_CANCEL|
411
            OC_F_FAILED))
412 48
                return (0);
413
414 196
        if (req->vcf != NULL) /* NB: must operate under oh lock. */
415 0
                return (0);
416
417 196
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
418
419 196
        return (hsh_vry_match(req, oc, NULL));
420 244
}
421
422
/*---------------------------------------------------------------------
423
 */
424
425
enum lookup_e
426 10959
HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp)
427
{
428
        struct worker *wrk;
429
        struct objhead *oh;
430
        struct objcore *oc;
431
        struct objcore *exp_oc;
432
        const struct vcf_return *vr;
433
        vtim_real exp_t_origin;
434
        int busy_found;
435
        intmax_t boc_progress;
436 10959
        unsigned xid = 0;
437
        unsigned ban_checks;
438
        unsigned ban_any_variant;
439 10959
        float dttl = 0.0;
440
441 10959
        AN(ocp);
442 10959
        *ocp = NULL;
443 10959
        AN(bocp);
444 10959
        *bocp = NULL;
445
446 10959
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
447 10959
        wrk = req->wrk;
448 10959
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
449 10959
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
450 10959
        CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC);
451 10959
        CHECK_OBJ_ORNULL(req->objcore, OBJCORE_MAGIC);
452 10959
        CHECK_OBJ_ORNULL(req->vcf, VCF_MAGIC);
453 10959
        AN(hash);
454
455 10959
        hsh_prealloc(wrk);
456 10959
        if (DO_DEBUG(DBG_HASHEDGE))
457 72
                hsh_testmagic(req->digest);
458
459
        /*
460
         * When a req rushes off the waiting list, it brings an implicit
461
         * oh refcnt acquired at disembark time and an oc ref (with its
462
         * own distinct oh ref) acquired during rush hour.
463
         */
464
465 10959
        if (req->objcore != NULL && hsh_rush_match(req)) {
466 188
                TAKE_OBJ_NOTNULL(oc, &req->objcore, OBJCORE_MAGIC);
467 188
                *ocp = oc;
468 188
                oh = oc->objhead;
469 188
                Lck_Lock(&oh->mtx);
470 188
                oc->hits++;
471 188
                boc_progress = oc->boc == NULL ? -1 : oc->boc->fetched_so_far;
472 188
                AN(hsh_deref_objhead_unlock(wrk, &oh, oc));
473 188
                Req_LogHit(wrk, req, oc, boc_progress);
474
                /* NB: since this hit comes from the waiting list instead of
475
                 * a regular lookup, grace is not considered. The object is
476
                 * fresh in the context of the waiting list, even expired: it
477
                 * was successfully just [re]validated by a fetch task.
478
                 */
479 188
                return (HSH_HIT);
480
        }
481
482 10771
        if (req->objcore != NULL) {
483 56
                oh = req->objcore->objhead;
484 56
                (void)HSH_DerefObjCore(wrk, &req->objcore);
485 56
                Lck_Lock(&oh->mtx);
486 56
        } else {
487 10715
                AN(wrk->wpriv->nobjhead);
488 10715
                oh = hash->lookup(wrk, req->digest, &wrk->wpriv->nobjhead);
489
        }
490
491 10771
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
492 10771
        Lck_AssertHeld(&oh->mtx);
493
494 10771
        if (req->hash_always_miss) {
495
                /* XXX: should we do predictive Vary in this case ? */
496
                /* Insert new objcore in objecthead and release mutex */
497 68
                *bocp = hsh_insert_busyobj(wrk, oh);
498
                /* NB: no deref of objhead, new object inherits reference */
499 68
                Lck_Unlock(&oh->mtx);
500 68
                return (HSH_MISS);
501
        }
502
503 10703
        assert(oh->refcnt > 0);
504 10703
        busy_found = 0;
505 10703
        exp_oc = NULL;
506 10703
        exp_t_origin = 0.0;
507 10703
        ban_checks = 0;
508 10703
        ban_any_variant = cache_param->ban_any_variant;
509 22412
        VTAILQ_FOREACH(oc, &oh->objcs, hsh_list) {
510
                /* Must be at least our own ref + the objcore we examine */
511 16067
                assert(oh->refcnt > 1);
512 16067
                CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
513 16067
                assert(oc->objhead == oh);
514 16067
                assert(oc->refcnt > 0);
515
516 16067
                if (oc->flags & OC_F_DYING)
517 0
                        continue;
518 16067
                if (oc->flags & OC_F_FAILED)
519 0
                        continue;
520
521 16067
                CHECK_OBJ_ORNULL(oc->boc, BOC_MAGIC);
522 16067
                if (oc->flags & OC_F_BUSY) {
523 268
                        if (req->hash_ignore_busy)
524 4
                                continue;
525
526 264
                        if (oc->boc && oc->boc->vary != NULL &&
527 6
                            !hsh_vry_match(req, oc, oc->boc->vary)) {
528 4
                                wrk->strangelove++;
529 4
                                continue;
530
                        }
531
532 260
                        busy_found = 1;
533 260
                        continue;
534
                }
535
536 15799
                if (oc->ttl <= 0.)
537 188
                        continue;
538
539 15611
                if (ban_checks++ < ban_any_variant
540 15611
                    && BAN_CheckObject(wrk, oc, req)) {
541 0
                        oc->flags |= OC_F_DYING;
542 0
                        EXP_Remove(oc, NULL);
543 0
                        continue;
544
                }
545
546 15611
                if (!hsh_vry_match(req, oc, NULL)) {
547 10504
                        wrk->strangelove++;
548 10504
                        continue;
549
                }
550
551 5107
                if (ban_checks >= ban_any_variant
552 5107
                    && BAN_CheckObject(wrk, oc, req)) {
553 136
                        oc->flags |= OC_F_DYING;
554 136
                        EXP_Remove(oc, NULL);
555 136
                        continue;
556
                }
557
558 4971
                if (req->vcf != NULL) {
559 32
                        vr = req->vcf->func(req, &oc, &exp_oc, 0);
560 32
                        if (vr == VCF_CONTINUE)
561 16
                                continue;
562 16
                        if (vr == VCF_MISS) {
563 12
                                oc = NULL;
564 12
                                break;
565
                        }
566 4
                        if (vr == VCF_HIT)
567 4
                                break;
568 0
                        assert(vr == VCF_DEFAULT);
569 0
                }
570
571 4939
                if (EXP_Ttl(req, oc) > req->t_req) {
572 4342
                        assert(oh->refcnt > 1);
573 4342
                        assert(oc->objhead == oh);
574 4342
                        break;
575
                }
576
577 597
                if (EXP_Ttl(NULL, oc) <= req->t_req && /* ignore req.max_age */
578 576
                    oc->t_origin > exp_t_origin) {
579
                        /* record the newest object */
580 576
                        exp_oc = oc;
581 576
                        exp_t_origin = oc->t_origin;
582 576
                        assert(oh->refcnt > 1);
583 576
                        assert(exp_oc->objhead == oh);
584 576
                }
585 597
        }
586
587 10703
        if (req->vcf != NULL)
588 24
                (void)req->vcf->func(req, &oc, &exp_oc, 1);
589
590 10703
        if (oc != NULL && oc->flags & OC_F_HFP) {
591 52
                xid = VXID(ObjGetXID(wrk, oc));
592 52
                dttl = EXP_Dttl(req, oc);
593 52
                AN(hsh_deref_objhead_unlock(wrk, &oh, oc));
594 52
                wrk->stats->cache_hitpass++;
595 52
                VSLb(req->vsl, SLT_HitPass, "%u %.6f", xid, dttl);
596 52
                return (HSH_HITPASS);
597
        }
598
599 10651
        if (oc != NULL) {
600 4298
                *ocp = oc;
601 4298
                oc->refcnt++;
602 4298
                if (oc->flags & OC_F_HFM) {
603 128
                        xid = VXID(ObjGetXID(wrk, oc));
604 128
                        dttl = EXP_Dttl(req, oc);
605 128
                        *bocp = hsh_insert_busyobj(wrk, oh);
606 128
                        Lck_Unlock(&oh->mtx);
607 128
                        wrk->stats->cache_hitmiss++;
608 128
                        VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl);
609 128
                        return (HSH_HITMISS);
610
                }
611 4170
                oc->hits++;
612 4170
                boc_progress = oc->boc == NULL ? -1 : oc->boc->fetched_so_far;
613 4170
                AN(hsh_deref_objhead_unlock(wrk, &oh, oc));
614 4170
                Req_LogHit(wrk, req, oc, boc_progress);
615 4170
                return (HSH_HIT);
616
        }
617
618 6353
        if (exp_oc != NULL && exp_oc->flags & OC_F_HFM) {
619
                /*
620
                 * expired HFM ("grace/keep HFM")
621
                 *
622
                 * XXX should HFM objects actually have grace/keep ?
623
                 * XXX also:  why isn't *ocp = exp_oc ?
624
                 */
625 16
                xid = VXID(ObjGetXID(wrk, exp_oc));
626 16
                dttl = EXP_Dttl(req, exp_oc);
627 16
                *bocp = hsh_insert_busyobj(wrk, oh);
628 16
                Lck_Unlock(&oh->mtx);
629 16
                wrk->stats->cache_hitmiss++;
630 16
                VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl);
631 16
                return (HSH_HITMISS);
632
        }
633
634 6337
        if (exp_oc != NULL && exp_oc->boc != NULL)
635 20
                boc_progress = exp_oc->boc->fetched_so_far;
636
        else
637 6317
                boc_progress = -1;
638
639 6337
        if (!busy_found) {
640 6081
                *bocp = hsh_insert_busyobj(wrk, oh);
641
642 6081
                if (exp_oc != NULL) {
643 540
                        exp_oc->refcnt++;
644 540
                        *ocp = exp_oc;
645 540
                        if (EXP_Ttl_grace(req, exp_oc) >= req->t_req) {
646 380
                                exp_oc->hits++;
647 380
                                Lck_Unlock(&oh->mtx);
648 380
                                Req_LogHit(wrk, req, exp_oc, boc_progress);
649 380
                                return (HSH_GRACE);
650
                        }
651 160
                }
652 5701
                Lck_Unlock(&oh->mtx);
653 5701
                return (HSH_MISS);
654
        }
655
656 256
        AN(busy_found);
657 256
        if (exp_oc != NULL && EXP_Ttl_grace(req, exp_oc) >= req->t_req) {
658
                /* we do not wait on the busy object if in grace */
659 12
                exp_oc->refcnt++;
660 12
                *ocp = exp_oc;
661 12
                exp_oc->hits++;
662 12
                AN(hsh_deref_objhead_unlock(wrk, &oh, NULL));
663 12
                Req_LogHit(wrk, req, exp_oc, boc_progress);
664 12
                return (HSH_GRACE);
665
        }
666
667
        /* There are one or more busy objects, wait for them */
668 244
        VTAILQ_INSERT_TAIL(&oh->waitinglist, req, w_list);
669
670 244
        AZ(req->hash_ignore_busy);
671
672
        /*
673
         * The objhead reference is held by req while it is parked on the
674
         * waiting list. The oh pointer is taken back from the objcore that
675
         * triggers a rush of req off the waiting list.
676
         */
677 244
        assert(oh->refcnt > 1);
678
679 244
        req->wrk = NULL;
680 244
        req->waitinglist_gen = oh->waitinglist_gen;
681
682 244
        if (DO_DEBUG(DBG_WAITINGLIST))
683 88
                VSLb(req->vsl, SLT_Debug, "on waiting list <%p>", oh);
684
685 244
        Lck_Unlock(&oh->mtx);
686
687 244
        wrk->stats->busy_sleep++;
688 244
        return (HSH_BUSY);
689 10959
}
690
691
/*---------------------------------------------------------------------
692
 * Pick the req's we are going to rush from the waiting list
693
 */
694
695
static void
696 489
hsh_rush1(const struct worker *wrk, struct objcore *oc, struct rush *r)
697
{
698
        struct objhead *oh;
699
        struct req *req;
700
        int i, max;
701
702 489
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
703 489
        CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
704 489
        CHECK_OBJ_NOTNULL(r, RUSH_MAGIC);
705 489
        VTAILQ_INIT(&r->reqs);
706
707 489
        if (oc == NULL)
708 4
                return;
709
710 485
        oh = oc->objhead;
711 485
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
712 485
        Lck_AssertHeld(&oh->mtx);
713
714 485
        AZ(oc->flags & (OC_F_BUSY | OC_F_PRIVATE));
715 485
        max = cache_param->rush_exponent;
716 485
        if (oc->flags & (OC_F_WITHDRAWN|OC_F_FAILED))
717 309
                max = 1;
718 485
        assert(max > 0);
719
720 485
        if (oc->waitinglist_gen == 0) {
721 457
                oc->waitinglist_gen = oh->waitinglist_gen;
722 457
                oh->waitinglist_gen++;
723 457
        }
724
725 729
        for (i = 0; i < max; i++) {
726 681
                req = VTAILQ_FIRST(&oh->waitinglist);
727 681
                if (req == NULL)
728 437
                        break;
729
730 244
                CHECK_OBJ(req, REQ_MAGIC);
731
732
                /* NB: The waiting list is naturally sorted by generation.
733
                 *
734
                 * Because of the exponential nature of the rush, it is
735
                 * possible that new requests enter the waiting list before
736
                 * the rush for this oc completes. Because the OC_F_BUSY flag
737
                 * was cleared before the beginning of the rush, requests
738
                 * from a newer generation already got a chance to evaluate
739
                 * oc during a lookup and it didn't match their criteria.
740
                 *
741
                 * Therefore there's no point propagating the exponential
742
                 * rush of this oc when we see a newer generation.
743
                 */
744 244
                if (req->waitinglist_gen > oc->waitinglist_gen)
745 0
                        break;
746
747 244
                AZ(req->wrk);
748 244
                VTAILQ_REMOVE(&oh->waitinglist, req, w_list);
749 244
                VTAILQ_INSERT_TAIL(&r->reqs, req, w_list);
750 244
                req->objcore = oc;
751 244
                oc->refcnt++;
752 244
                wrk->stats->busy_wakeup++;
753 244
        }
754 489
}
755
756
/*---------------------------------------------------------------------
757
 * Rush req's that came from waiting list.
758
 */
759
760
static void
761 12845
hsh_rush2(struct worker *wrk, struct rush *r)
762
{
763
        struct req *req;
764
765 12845
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
766 12845
        CHECK_OBJ_NOTNULL(r, RUSH_MAGIC);
767
768 13089
        while (!VTAILQ_EMPTY(&r->reqs)) {
769 244
                req = VTAILQ_FIRST(&r->reqs);
770 244
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
771 244
                VTAILQ_REMOVE(&r->reqs, req, w_list);
772 244
                DSL(DBG_WAITINGLIST, req->vsl->wid, "off waiting list");
773 244
                if (req->transport->reembark != NULL) {
774
                        // For ESI includes
775 4
                        req->transport->reembark(wrk, req);
776 4
                } else {
777
                        /*
778
                         * We ignore the queue limits which apply to new
779
                         * requests because if we fail to reschedule there
780
                         * may be vmod_privs to cleanup and we need a proper
781
                         * workerthread for that.
782
                         */
783 240
                        AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH));
784
                }
785
        }
786 12845
}
787
788
/*---------------------------------------------------------------------
789
 * Purge an entire objhead
790
 */
791
792
unsigned
793 96
HSH_Purge(struct worker *wrk, struct objhead *oh, vtim_real ttl_now,
794
    vtim_dur ttl, vtim_dur grace, vtim_dur keep)
795
{
796
        struct objcore *oc, *oc_nows[2], **ocp;
797 96
        unsigned i, j, n, n_max, total = 0;
798
        int is_purge;
799
800 96
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
801 96
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
802
803 96
        is_purge = (ttl == 0 && grace == 0 && keep == 0);
804 96
        n_max = WS_ReserveLumps(wrk->aws, sizeof *ocp);
805 96
        if (n_max < 2) {
806
                /* No space on the workspace. Give it a stack buffer of 2
807
                 * elements, which is the minimum for the algorithm
808
                 * below. */
809 0
                ocp = oc_nows;
810 0
                n_max = 2;
811 0
        } else
812 96
                ocp = WS_Reservation(wrk->aws);
813 96
        AN(ocp);
814
815
        /* Note: This algorithm uses OC references in the list as
816
         * bookmarks, in order to know how far into the list we were when
817
         * releasing the mutex partway through and want to resume
818
         * again. This relies on the list not being reordered while we are
819
         * not holding the mutex. The only place where that happens is in
820
         * HSH_Unbusy(), where an OC_F_BUSY OC is moved first in the
821
         * list. This does not cause problems because we skip OC_F_BUSY
822
         * OCs. */
823
824 96
        Lck_Lock(&oh->mtx);
825 96
        oc = VTAILQ_FIRST(&oh->objcs);
826 96
        n = 0;
827 100
        while (1) {
828 568
                for (; n < n_max && oc != NULL; oc = VTAILQ_NEXT(oc, hsh_list))
829
                {
830 468
                        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
831 468
                        assert(oc->objhead == oh);
832 468
                        if (oc->flags & OC_F_BUSY) {
833
                                /* We cannot purge busy objects here, because
834
                                 * their owners have special rights to them,
835
                                 * and may nuke them without concern for the
836
                                 * refcount, which by definition always must
837
                                 * be one, so they don't check. */
838 92
                                continue;
839
                        }
840 376
                        if (oc->flags & OC_F_DYING)
841 0
                                continue;
842 376
                        if (is_purge)
843 336
                                oc->flags |= OC_F_DYING;
844 376
                        oc->refcnt++;
845 376
                        ocp[n++] = oc;
846 376
                }
847
848 100
                Lck_Unlock(&oh->mtx);
849
850 100
                if (n == 0) {
851
                        /* No eligible objcores found. We are finished. */
852 16
                        break;
853
                }
854
855 84
                j = n;
856 84
                if (oc != NULL) {
857
                        /* There are more objects on the objhead that we
858
                         * have not yet looked at, but no more space on
859
                         * the objcore reference list. Do not process the
860
                         * last one, it will be used as the bookmark into
861
                         * the objcore list for the next iteration of the
862
                         * outer loop. */
863 4
                        j--;
864 4
                        assert(j >= 1); /* True because n_max >= 2 */
865 4
                }
866 460
                for (i = 0; i < j; i++) {
867 376
                        CHECK_OBJ_NOTNULL(ocp[i], OBJCORE_MAGIC);
868 376
                        if (is_purge)
869 336
                                EXP_Remove(ocp[i], NULL);
870
                        else
871 40
                                EXP_Reduce(ocp[i], ttl_now, ttl, grace, keep);
872 376
                        (void)HSH_DerefObjCore(wrk, &ocp[i]);
873 376
                        AZ(ocp[i]);
874 376
                        total++;
875 376
                }
876
877 84
                if (j == n) {
878
                        /* No bookmark set, that means we got to the end
879
                         * of the objcore list in the previous run and are
880
                         * finished. */
881 80
                        break;
882
                }
883
884 4
                Lck_Lock(&oh->mtx);
885
886
                /* Move the bookmark first and continue scanning the
887
                 * objcores */
888 4
                CHECK_OBJ_NOTNULL(ocp[j], OBJCORE_MAGIC);
889 4
                ocp[0] = ocp[j];
890 4
                n = 1;
891 4
                oc = VTAILQ_NEXT(ocp[0], hsh_list);
892 4
                CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
893
        }
894
895 96
        WS_Release(wrk->aws, 0);
896 96
        if (is_purge)
897 56
                Pool_PurgeStat(total);
898 96
        return (total);
899
}
900
901
/*---------------------------------------------------------------------
902
 * Fail an objcore
903
 */
904
905
void
906 276
HSH_Fail(struct worker *wrk, struct objcore *oc)
907
{
908
        struct objhead *oh;
909
        struct rush rush;
910
911 276
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
912 276
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
913 276
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
914 276
        oh = oc->objhead;
915 276
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
916 276
        INIT_OBJ(&rush, RUSH_MAGIC);
917
918
        /*
919
         * We either failed before the end of vcl_backend_response
920
         * and a cache miss has the busy bit, so that HSH_Lookup()
921
         * will not consider this oc, or an object hung off the oc
922
         * so that it can consider it.
923
         *
924
         * We can only fail an ongoing fetch in a backend context
925
         * so we can safely check the BOC state as it won't change
926
         * under our feet.
927
         */
928 276
        if (oc->boc->state < BOS_STREAM)
929 204
                assert(oc->flags & (OC_F_BUSY|OC_F_PRIVATE));
930
        else
931 72
                assert(oc->stobj->stevedore != NULL);
932
933 276
        Lck_Lock(&oh->mtx);
934 276
        oc->flags |= OC_F_FAILED;
935 276
        if (oc->flags & OC_F_BUSY) {
936 180
                oc->flags &= ~OC_F_BUSY;
937 180
                hsh_rush1(wrk, oc, &rush);
938 180
        }
939 276
        Lck_Unlock(&oh->mtx);
940 276
        hsh_rush2(wrk, &rush);
941 276
}
942
943
/*---------------------------------------------------------------------
944
 * Mark a fetch we will not need as cancelled
945
 */
946
947
static void
948 1501
hsh_cancel(struct objcore *oc)
949
{
950
        struct objhead *oh;
951
952 1501
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
953 1501
        oh = oc->objhead;
954 1501
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
955
956 1501
        Lck_Lock(&oh->mtx);
957 1501
        oc->flags |= OC_F_CANCEL;
958 1501
        Lck_Unlock(&oh->mtx);
959 1501
}
960
961
/*---------------------------------------------------------------------
962
 * Cancel a fetch when the client does not need it any more
963
 */
964
965
void
966 15832
HSH_Cancel(struct worker *wrk, struct objcore *oc, struct boc *boc)
967
{
968 15832
        struct boc *bocref = NULL;
969
970 15832
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
971
972 15832
        if ((oc->flags & OC_F_TRANSIENT) == 0)
973 10008
                return;
974
975
        /*
976
         * NB: we use two distinct variables to only release the reference if
977
         * we had to acquire one. The caller-provided boc is optional.
978
         */
979 5824
        if (boc == NULL)
980 4335
                bocref = boc = HSH_RefBoc(oc);
981
982 5824
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
983
984 5824
        if (oc->flags & OC_F_HFP)
985 120
                AN(oc->flags & OC_F_HFM);
986
987 5824
        if (boc != NULL) {
988 1501
                hsh_cancel(oc);
989 1501
                (void)ObjWaitState(oc, BOS_FINISHED);
990 1501
        }
991
992 5824
        if (bocref != NULL)
993 12
                HSH_DerefBoc(wrk, oc);
994
995 5824
        ObjSlim(wrk, oc);
996 15832
}
997
998
/*---------------------------------------------------------------------
999
 * Withdraw an objcore that will not proceed with a fetch.
1000
 */
1001
1002
void
1003 129
HSH_Withdraw(struct worker *wrk, struct objcore **ocp)
1004
{
1005
        struct objhead *oh;
1006
        struct objcore *oc;
1007
        struct rush rush;
1008
1009 129
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1010 129
        TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC);
1011 129
        INIT_OBJ(&rush, RUSH_MAGIC);
1012
1013 129
        oh = oc->objhead;
1014 129
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
1015
1016 129
        Lck_Lock(&oh->mtx);
1017 129
        AZ(oc->stobj->stevedore);
1018 129
        AN(oc->flags & OC_F_BUSY);
1019 129
        assert(oc->refcnt == 1);
1020 129
        assert(oh->refcnt > 0);
1021 129
        oc->flags &= ~OC_F_BUSY;
1022 129
        oc->flags |= OC_F_WITHDRAWN;
1023 129
        hsh_rush1(wrk, oc, &rush); /* grabs up to 1 oc ref */
1024 129
        assert(hsh_deref_objcore_unlock(wrk, &oc) <= 1);
1025
1026 129
        hsh_rush2(wrk, &rush);
1027 129
}
1028
1029
/*---------------------------------------------------------------------
1030
 * Unbusy an objcore when the object is completely fetched.
1031
 */
1032
1033
void
1034 9224
HSH_Unbusy(struct worker *wrk, struct objcore *oc)
1035
{
1036
        struct objhead *oh;
1037
        struct rush rush;
1038
1039 9224
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1040 9224
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1041 9224
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
1042
1043 9224
        oh = oc->objhead;
1044 9224
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
1045
1046 9224
        AN(oc->stobj->stevedore);
1047 9224
        assert(oh->refcnt > 0);
1048 9224
        assert(oc->refcnt > 0);
1049
1050 9224
        if (oc->flags & OC_F_PRIVATE) {
1051 3244
                AZ(oc->flags & OC_F_BUSY);
1052 3244
                return;
1053
        }
1054
1055 5980
        AN(oc->flags & OC_F_BUSY);
1056 5980
        INIT_OBJ(&rush, RUSH_MAGIC);
1057
1058 5980
        BAN_NewObjCore(oc);
1059 5980
        AN(oc->ban);
1060
1061
        /* XXX: pretouch neighbors on oh->objcs to prevent page-on under mtx */
1062 5980
        Lck_Lock(&oh->mtx);
1063 5980
        assert(oh->refcnt > 0);
1064 5980
        assert(oc->refcnt > 0);
1065 5980
        EXP_RefNewObjcore(oc); /* Takes a ref for expiry */
1066
        /* XXX: strictly speaking, we should sort in Date: order. */
1067 5980
        VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
1068 5980
        VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list);
1069 5980
        oc->flags &= ~OC_F_BUSY;
1070 5980
        if (!VTAILQ_EMPTY(&oh->waitinglist)) {
1071 147
                assert(oh->refcnt > 1);
1072 147
                hsh_rush1(wrk, oc, &rush);
1073 147
        }
1074 5980
        Lck_Unlock(&oh->mtx);
1075 5980
        EXP_Insert(wrk, oc);
1076 5980
        hsh_rush2(wrk, &rush);
1077 9224
}
1078
1079
/*====================================================================
1080
 * HSH_Kill()
1081
 *
1082
 * It's dead Jim, kick it...
1083
 */
1084
1085
void
1086 1065
HSH_Kill(struct objcore *oc)
1087
{
1088
1089 1065
        HSH_Replace(oc, NULL);
1090 1065
}
1091
1092
void
1093 1385
HSH_Replace(struct objcore *oc, const struct objcore *new_oc)
1094
{
1095
1096 1385
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1097 1385
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
1098 1385
        if (new_oc != NULL) {
1099 320
                CHECK_OBJ(new_oc, OBJCORE_MAGIC);
1100 320
                assert(oc->objhead == new_oc->objhead);
1101 320
        }
1102
1103 1385
        Lck_Lock(&oc->objhead->mtx);
1104 1385
        oc->flags |= OC_F_DYING;
1105 1385
        Lck_Unlock(&oc->objhead->mtx);
1106 1385
        EXP_Remove(oc, new_oc);
1107 1385
}
1108
1109
/*====================================================================
1110
 * HSH_Snipe()
1111
 *
1112
 * If objcore is idle, gain a ref and mark it dead.
1113
 */
1114
1115
int
1116 44
HSH_Snipe(const struct worker *wrk, struct objcore *oc)
1117
{
1118 44
        int retval = 0;
1119
1120 44
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1121 44
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1122 44
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
1123
1124 44
        if (oc->refcnt == 1 && !Lck_Trylock(&oc->objhead->mtx)) {
1125 44
                if (oc->refcnt == 1 && !(oc->flags & OC_F_DYING)) {
1126 44
                        oc->flags |= OC_F_DYING;
1127 44
                        oc->refcnt++;
1128 44
                        retval = 1;
1129 44
                }
1130 44
                Lck_Unlock(&oc->objhead->mtx);
1131 44
        }
1132 44
        if (retval)
1133 44
                EXP_Remove(oc, NULL);
1134 44
        return (retval);
1135
}
1136
1137
1138
/*---------------------------------------------------------------------
1139
 * Gain a reference on an objcore
1140
 */
1141
1142
void
1143 10184
HSH_Ref(struct objcore *oc)
1144
{
1145
        struct objhead *oh;
1146
1147 10184
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1148 10184
        oh = oc->objhead;
1149 10184
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1150 10184
        Lck_Lock(&oh->mtx);
1151 10184
        assert(oc->refcnt > 0);
1152 10184
        oc->refcnt++;
1153 10184
        Lck_Unlock(&oh->mtx);
1154 10184
}
1155
1156
/*---------------------------------------------------------------------
1157
 * Gain a reference on the busyobj, if the objcore has one
1158
 */
1159
1160
struct boc *
1161 37785
HSH_RefBoc(const struct objcore *oc)
1162
{
1163
        struct objhead *oh;
1164
        struct boc *boc;
1165
1166 37785
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1167 37785
        oh = oc->objhead;
1168 37785
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1169 37785
        if (oc->boc == NULL)
1170 21317
                return (NULL);
1171 16468
        Lck_Lock(&oh->mtx);
1172 16468
        assert(oc->refcnt > 0);
1173 16468
        boc = oc->boc;
1174 16468
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
1175 16468
        if (boc != NULL) {
1176 16466
                assert(boc->refcount > 0);
1177 16466
                if (boc->state < BOS_FINISHED)
1178 16381
                        boc->refcount++;
1179
                else
1180 85
                        boc = NULL;
1181 16466
        }
1182 16468
        Lck_Unlock(&oh->mtx);
1183 16468
        return (boc);
1184 37783
}
1185
1186
void
1187 28509
HSH_DerefBoc(struct worker *wrk, struct objcore *oc)
1188
{
1189
        struct boc *boc;
1190
        unsigned r;
1191
1192 28509
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1193 28509
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1194 28509
        boc = oc->boc;
1195 28509
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1196 28509
        Lck_Lock(&oc->objhead->mtx);
1197 28509
        assert(oc->refcnt > 0);
1198 28509
        assert(boc->refcount > 0);
1199 28509
        r = --boc->refcount;
1200 28509
        if (r == 0)
1201 12134
                oc->boc = NULL;
1202 28509
        Lck_Unlock(&oc->objhead->mtx);
1203 28509
        if (r == 0)
1204 12135
                ObjBocDone(wrk, oc, &boc);
1205 28509
}
1206
1207
/*--------------------------------------------------------------------
1208
 * Dereference objcore
1209
 *
1210
 * Returns zero if target was destroyed.
1211
 */
1212
1213
int
1214 29703
HSH_DerefObjCore(struct worker *wrk, struct objcore **ocp)
1215
{
1216
        struct objcore *oc;
1217
        struct objhead *oh;
1218
1219 29703
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1220 29703
        TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC);
1221 29703
        assert(oc->refcnt > 0);
1222
1223 29703
        oh = oc->objhead;
1224 29703
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1225
1226 29703
        Lck_Lock(&oh->mtx);
1227 29703
        return (hsh_deref_objcore_unlock(wrk, &oc));
1228
}
1229
1230
static int
1231 29844
hsh_deref_objcore_unlock(struct worker *wrk, struct objcore **ocp)
1232
{
1233
        struct objcore *oc;
1234
        struct objhead *oh;
1235
        int r;
1236
1237 29844
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1238 29844
        TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC);
1239 29844
        assert(oc->refcnt > 0);
1240
1241 29844
        oh = oc->objhead;
1242 29844
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1243
1244 29844
        Lck_AssertHeld(&oh->mtx);
1245 29844
        assert(oh->refcnt > 0);
1246 29844
        r = --oc->refcnt;
1247 29844
        if (!r)
1248 7882
                VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
1249 29844
        Lck_Unlock(&oh->mtx);
1250 29844
        if (r != 0)
1251 21962
                return (r);
1252
1253 7882
        AZ(oc->flags & OC_F_BUSY);
1254 7882
        AZ(oc->exp_flags);
1255
1256 7882
        BAN_DestroyObj(oc);
1257 7882
        AZ(oc->ban);
1258
1259 7882
        if (oc->stobj->stevedore != NULL)
1260 7540
                ObjFreeObj(wrk, oc);
1261 7882
        ObjDestroy(wrk, &oc);
1262
1263
        /* Drop our ref on the objhead */
1264 7882
        assert(oh->refcnt > 0);
1265 7882
        (void)hsh_deref_objhead(wrk, &oh);
1266 7882
        return (0);
1267 29844
}
1268
1269
static int
1270 12304
hsh_deref_objhead_unlock(struct worker *wrk, struct objhead **poh,
1271
    struct objcore *oc)
1272
{
1273
        struct objhead *oh;
1274
        struct rush rush;
1275
        int r;
1276
1277 12304
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1278 12304
        TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC);
1279
1280 12304
        Lck_AssertHeld(&oh->mtx);
1281
1282 12304
        if (oh >= private_ohs && oh < private_ohs + vcountof(private_ohs)) {
1283 5912
                assert(VTAILQ_EMPTY(&oh->waitinglist));
1284 5912
                assert(oh->refcnt > 1);
1285 5912
                oh->refcnt--;
1286 5912
                Lck_Unlock(&oh->mtx);
1287 5912
                return (1);
1288
        }
1289
1290
        //lint --e{661}
1291
        //lint -specific(-e661)
1292
        //
1293
        // because of the static array, flexelint thinks that all ohs were from
1294
        // the static array :( the above suppression applies to the remainder of
1295
        // this function body and specific walks involving this function
1296
1297 6392
        INIT_OBJ(&rush, RUSH_MAGIC);
1298 6392
        if (!VTAILQ_EMPTY(&oh->waitinglist)) {
1299 33
                assert(oh->refcnt > 1);
1300 33
                hsh_rush1(wrk, oc, &rush);
1301 33
        }
1302
1303 6392
        if (oh->refcnt == 1)
1304 830
                assert(VTAILQ_EMPTY(&oh->waitinglist));
1305
1306 6392
        assert(oh->refcnt > 0);
1307 6392
        r = hash->deref(wrk, oh); /* Unlocks oh->mtx */
1308 6392
        hsh_rush2(wrk, &rush);
1309 6392
        return (r);
1310 12304
}
1311
1312
static int
1313 7881
hsh_deref_objhead(struct worker *wrk, struct objhead **poh)
1314
{
1315
        struct objhead *oh;
1316
1317 7881
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1318 7881
        TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC);
1319
1320 7881
        Lck_Lock(&oh->mtx);
1321 7881
        return (hsh_deref_objhead_unlock(wrk, &oh, NULL));
1322
}
1323
1324
void
1325 3892
HSH_Init(const struct hash_slinger *slinger)
1326
{
1327
1328 3892
        assert(DIGEST_LEN == VSHA256_LEN);      /* avoid #include pollution */
1329 3892
        hash = slinger;
1330 3892
        if (hash->start != NULL)
1331 3892
                hash->start();
1332 502068
        for (struct objhead *oh = private_ohs;
1333 502068
            oh < private_ohs + vcountof(private_ohs);
1334 498176
            oh++) {
1335 498176
                hsh_initobjhead(oh);
1336 498176
                assert(oh->refcnt == 1);
1337 498176
        }
1338 3892
}