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 2706547
hsh_initobjhead(struct objhead *oh)
99
{
100
101 2706547
        XXXAN(oh);
102 2706547
        INIT_OBJ(oh, OBJHEAD_MAGIC);
103 2706547
        oh->refcnt = 1;
104 2706547
        oh->waitinglist_gen = 1;
105 2706547
        VTAILQ_INIT(&oh->objcs);
106 2706547
        VTAILQ_INIT(&oh->waitinglist);
107 2706547
        Lck_New(&oh->mtx, lck_objhdr);
108 2706547
}
109
110
static struct objhead *
111 33395
hsh_newobjhead(void)
112
{
113 33395
        struct objhead *oh = malloc(sizeof *oh);
114 33395
        hsh_initobjhead(oh);
115 33395
        return (oh);
116
}
117
118
/*---------------------------------------------------------------------*/
119
/* Precreate an objhead and object for later use */
120
static void
121 56920
hsh_prealloc(struct worker *wrk)
122
{
123
124 56920
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
125
126 56920
        if (wrk->wpriv->nobjcore == NULL)
127 38179
                wrk->wpriv->nobjcore = ObjNew(wrk);
128 56920
        CHECK_OBJ_NOTNULL(wrk->wpriv->nobjcore, OBJCORE_MAGIC);
129
130 56920
        if (wrk->wpriv->nobjhead == NULL) {
131 33395
                wrk->wpriv->nobjhead = hsh_newobjhead();
132 33395
                wrk->stats->n_objecthead++;
133 33395
        }
134 56920
        CHECK_OBJ_NOTNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
135
136 56920
        if (hash->prep != NULL)
137 56794
                hash->prep(wrk);
138 56920
}
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 37201
fib(uint64_t n, uint8_t bits)
145
{
146 37201
        const uint64_t gr = 11400714819323198485LLU;
147
        uint64_t r;
148
149 37201
        r = n * gr;
150 37201
        r >>= (sizeof(gr) * 8) - bits;
151 37201
        assert(r < (size_t)1 << bits);
152 37201
        return ((size_t)r);
153
}
154
155
struct objcore *
156 37207
HSH_Private(const struct worker *wrk)
157
{
158
        struct objcore *oc;
159
        struct objhead *oh;
160
161 37207
        oh = &private_ohs[fib((uintptr_t)wrk, PRIVATE_OH_EXP)];
162 37207
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
163
164 37207
        oc = ObjNew(wrk);
165 37207
        AN(oc);
166 37207
        oc->refcnt = 1;
167 37207
        oc->objhead = oh;
168 37207
        oc->flags |= OC_F_PRIVATE;
169 37207
        Lck_Lock(&oh->mtx);
170 37207
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
171 37207
        oh->refcnt++;
172 37207
        Lck_Unlock(&oh->mtx);
173 37207
        return (oc);
174
}
175
176
/*---------------------------------------------------------------------*/
177
178
void
179 473705
HSH_Cleanup(const struct worker *wrk)
180
{
181
182 473705
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
183 473705
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
184 473705
        if (wrk->wpriv->nobjcore != NULL)
185 5857
                ObjDestroy(wrk, &wrk->wpriv->nobjcore);
186
187 473705
        if (wrk->wpriv->nobjhead != NULL) {
188 8054
                CHECK_OBJ(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
189 8054
                Lck_Delete(&wrk->wpriv->nobjhead->mtx);
190 8054
                FREE_OBJ(wrk->wpriv->nobjhead);
191 8054
                wrk->stats->n_objecthead--;
192 8054
        }
193 473705
        if (wrk->wpriv->nhashpriv != NULL) {
194
                /* XXX: If needed, add slinger method for this */
195 16179
                free(wrk->wpriv->nhashpriv);
196 16179
                wrk->wpriv->nhashpriv = NULL;
197 16179
        }
198 473705
}
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 339635
HSH_AddString(struct req *req, void *ctx, const char *str)
216
{
217
218 339635
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
219 339635
        AN(ctx);
220 339635
        if (str != NULL) {
221 169811
                VSHA256_Update(ctx, str, vstrlen(str));
222 169811
                VSLbs(req->vsl, SLT_Hash, TOSTRAND(str));
223 169811
        } else
224 169824
                VSHA256_Update(ctx, &str, 1);
225 339635
}
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 378
hsh_testmagic(void *result)
280
{
281
        size_t i, j;
282
        static size_t nused = 0;
283
284 1890
        for (i = 0; i < nused; i++)
285 1701
                if (!vmemcmp(hsh_magiclist[i].was, result, VSHA256_LEN))
286 189
                        break;
287 378
        if (i == nused && i < HSH_NMAGIC)
288 189
                vmemcpy(hsh_magiclist[nused++].was, result, VSHA256_LEN);
289 378
        if (i == nused)
290 0
                return;
291 378
        assert(i < HSH_NMAGIC);
292 378
        fprintf(stderr, "HASHMAGIC: <");
293 12474
        for (j = 0; j < VSHA256_LEN; j++)
294 12096
                fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
295 378
        fprintf(stderr, "> -> <");
296 378
        vmemcpy(result, hsh_magiclist[i].now, VSHA256_LEN);
297 12474
        for (j = 0; j < VSHA256_LEN; j++)
298 12096
                fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
299 378
        fprintf(stderr, ">\n");
300 378
}
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 336
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 336
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
316 336
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
317 336
        AN(digest);
318 336
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
319 336
        AN(ban);
320 336
        AZ(oc->flags & (OC_F_BUSY | OC_F_PRIVATE));
321 336
        assert(oc->refcnt == 1);
322 336
        INIT_OBJ(&rush, RUSH_MAGIC);
323
324 336
        hsh_prealloc(wrk);
325
326 336
        AN(wrk->wpriv->nobjhead);
327 336
        oh = hash->lookup(wrk, digest, &wrk->wpriv->nobjhead);
328 336
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
329 336
        Lck_AssertHeld(&oh->mtx);
330 336
        assert(oh->refcnt > 0);
331
332
        /* Mark object busy and insert (precreated) objcore in
333
           objecthead. The new object inherits our objhead reference. */
334 336
        oc->objhead = oh;
335 336
        oc->flags |= OC_F_BUSY;
336 336
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
337 336
        EXP_RefNewObjcore(oc);
338 336
        Lck_Unlock(&oh->mtx);
339
340 336
        BAN_RefBan(oc, ban);
341 336
        AN(oc->ban);
342
343
        /* Move the object first in the oh list, unbusy it and run the
344
           waitinglist if necessary */
345 336
        Lck_Lock(&oh->mtx);
346 336
        oc->flags &= ~OC_F_BUSY;
347 336
        VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
348 336
        VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list);
349 336
        if (!VTAILQ_EMPTY(&oh->waitinglist))
350 0
                hsh_rush1(wrk, oc, &rush);
351 336
        Lck_Unlock(&oh->mtx);
352 336
        hsh_rush2(wrk, &rush);
353
354 336
        EXP_Insert(wrk, oc);
355 336
}
356
357
/*---------------------------------------------------------------------
358
 */
359
360
static struct objcore *
361 32033
hsh_insert_busyobj(const struct worker *wrk, struct objhead *oh)
362
{
363
        struct objcore *oc;
364
365 32033
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
366 32033
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
367 32033
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
368 32033
        Lck_AssertHeld(&oh->mtx);
369
370 32033
        oc = wrk->wpriv->nobjcore;
371 32033
        wrk->wpriv->nobjcore = NULL;
372 32033
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
373
374 32033
        AZ(oc->flags & OC_F_BUSY);
375 32033
        oc->flags |= OC_F_BUSY;
376 32033
        oc->refcnt = 1;         /* Owned by busyobj */
377 32033
        oc->objhead = oh;
378 32033
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
379 32033
        return (oc);
380
}
381
382
/*---------------------------------------------------------------------
383
 */
384
385
static int
386 83085
hsh_vry_match(const struct req *req, struct objcore *oc, const uint8_t *vary)
387
{
388
389 83085
        if (req->hash_ignore_vary)
390 21
                return (1);
391 83064
        if (vary == NULL) {
392 83024
                if (! ObjHasAttr(req->wrk, oc, OA_VARY))
393 23762
                        return (1);
394 59262
                vary = ObjGetAttr(req->wrk, oc, OA_VARY, NULL);
395 59262
                AN(vary);
396 59262
        }
397 59302
        return (VRY_Match(req, vary));
398 83085
}
399
400
static unsigned
401 1283
hsh_rush_match(const struct req *req)
402
{
403
        struct objcore *oc;
404
405 1283
        oc = req->objcore;
406 1283
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
407 1283
        assert(oc->refcnt > 0);
408
409 1283
        AZ(oc->flags & (OC_F_BUSY | OC_F_PRIVATE));
410 1283
        if (oc->flags & (OC_F_WITHDRAWN|OC_F_HFM|OC_F_HFP|OC_F_CANCEL|
411
            OC_F_FAILED))
412 250
                return (0);
413
414 1033
        if (req->vcf != NULL) /* NB: must operate under oh lock. */
415 0
                return (0);
416
417 1033
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
418
419 1033
        return (hsh_vry_match(req, oc, NULL));
420 1283
}
421
422
/*---------------------------------------------------------------------
423
 */
424
425
enum lookup_e
426 56587
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 56587
        unsigned xid = 0;
437
        unsigned ban_checks;
438
        unsigned ban_any_variant;
439 56587
        float dttl = 0.0;
440
441 56587
        AN(ocp);
442 56587
        *ocp = NULL;
443 56587
        AN(bocp);
444 56587
        *bocp = NULL;
445
446 56587
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
447 56587
        wrk = req->wrk;
448 56587
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
449 56587
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
450 56587
        CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC);
451 56587
        CHECK_OBJ_ORNULL(req->objcore, OBJCORE_MAGIC);
452 56587
        CHECK_OBJ_ORNULL(req->vcf, VCF_MAGIC);
453 56587
        AN(hash);
454
455 56587
        hsh_prealloc(wrk);
456 56587
        if (DO_DEBUG(DBG_HASHEDGE))
457 378
                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 56587
        if (req->objcore != NULL && hsh_rush_match(req)) {
466 991
                TAKE_OBJ_NOTNULL(oc, &req->objcore, OBJCORE_MAGIC);
467 991
                *ocp = oc;
468 991
                oh = oc->objhead;
469 991
                Lck_Lock(&oh->mtx);
470 991
                oc->hits++;
471 991
                boc_progress = oc->boc == NULL ? -1 : oc->boc->fetched_so_far;
472 991
                AN(hsh_deref_objhead_unlock(wrk, &oh, oc));
473 991
                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 991
                return (HSH_HIT);
480
        }
481
482 55596
        if (req->objcore != NULL) {
483 297
                oh = req->objcore->objhead;
484 297
                (void)HSH_DerefObjCore(wrk, &req->objcore);
485 297
                Lck_Lock(&oh->mtx);
486 297
        } else {
487 55299
                AN(wrk->wpriv->nobjhead);
488 55299
                oh = hash->lookup(wrk, req->digest, &wrk->wpriv->nobjhead);
489
        }
490
491 55596
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
492 55596
        Lck_AssertHeld(&oh->mtx);
493
494 55596
        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 357
                *bocp = hsh_insert_busyobj(wrk, oh);
498
                /* NB: no deref of objhead, new object inherits reference */
499 357
                Lck_Unlock(&oh->mtx);
500 357
                return (HSH_MISS);
501
        }
502
503 55239
        assert(oh->refcnt > 0);
504 55239
        busy_found = 0;
505 55239
        exp_oc = NULL;
506 55239
        exp_t_origin = 0.0;
507 55239
        ban_checks = 0;
508 55239
        ban_any_variant = cache_param->ban_any_variant;
509 116718
        VTAILQ_FOREACH(oc, &oh->objcs, hsh_list) {
510
                /* Must be at least our own ref + the objcore we examine */
511 84409
                assert(oh->refcnt > 1);
512 84409
                CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
513 84409
                assert(oc->objhead == oh);
514 84409
                assert(oc->refcnt > 0);
515
516 84409
                if (oc->flags & OC_F_DYING)
517 0
                        continue;
518 84409
                if (oc->flags & OC_F_FAILED)
519 0
                        continue;
520
521 84409
                CHECK_OBJ_ORNULL(oc->boc, BOC_MAGIC);
522 84409
                if (oc->flags & OC_F_BUSY) {
523 1410
                        if (req->hash_ignore_busy)
524 21
                                continue;
525
526 1389
                        if (oc->boc && oc->boc->vary != NULL &&
527 40
                            !hsh_vry_match(req, oc, oc->boc->vary)) {
528 21
                                wrk->strangelove++;
529 21
                                continue;
530
                        }
531
532 1368
                        busy_found = 1;
533 1368
                        continue;
534
                }
535
536 82999
                if (oc->ttl <= 0.)
537 987
                        continue;
538
539 82012
                if (ban_checks++ < ban_any_variant
540 82012
                    && BAN_CheckObject(wrk, oc, req)) {
541 0
                        oc->flags |= OC_F_DYING;
542 0
                        EXP_Remove(oc, NULL);
543 0
                        continue;
544
                }
545
546 82012
                if (!hsh_vry_match(req, oc, NULL)) {
547 55146
                        wrk->strangelove++;
548 55146
                        continue;
549
                }
550
551 26866
                if (ban_checks >= ban_any_variant
552 26866
                    && BAN_CheckObject(wrk, oc, req)) {
553 714
                        oc->flags |= OC_F_DYING;
554 714
                        EXP_Remove(oc, NULL);
555 714
                        continue;
556
                }
557
558 26152
                if (req->vcf != NULL) {
559 168
                        vr = req->vcf->func(req, &oc, &exp_oc, 0);
560 168
                        if (vr == VCF_CONTINUE)
561 84
                                continue;
562 84
                        if (vr == VCF_MISS) {
563 63
                                oc = NULL;
564 63
                                break;
565
                        }
566 21
                        if (vr == VCF_HIT)
567 21
                                break;
568 0
                        assert(vr == VCF_DEFAULT);
569 0
                }
570
571 25984
                if (EXP_Ttl(req, oc) > req->t_req) {
572 22846
                        assert(oh->refcnt > 1);
573 22846
                        assert(oc->objhead == oh);
574 22846
                        break;
575
                }
576
577 3138
                if (EXP_Ttl(NULL, oc) <= req->t_req && /* ignore req.max_age */
578 3024
                    oc->t_origin > exp_t_origin) {
579
                        /* record the newest object */
580 3024
                        exp_oc = oc;
581 3024
                        exp_t_origin = oc->t_origin;
582 3024
                        assert(oh->refcnt > 1);
583 3024
                        assert(exp_oc->objhead == oh);
584 3024
                }
585 3138
        }
586
587 55239
        if (req->vcf != NULL)
588 126
                (void)req->vcf->func(req, &oc, &exp_oc, 1);
589
590 55239
        if (oc != NULL && oc->flags & OC_F_HFP) {
591 273
                xid = VXID(ObjGetXID(wrk, oc));
592 273
                dttl = EXP_Dttl(req, oc);
593 273
                AN(hsh_deref_objhead_unlock(wrk, &oh, oc));
594 273
                wrk->stats->cache_hitpass++;
595 273
                VSLb(req->vsl, SLT_HitPass, "%u %.6f", xid, dttl);
596 273
                return (HSH_HITPASS);
597
        }
598
599 54966
        if (oc != NULL) {
600 22615
                *ocp = oc;
601 22615
                oc->refcnt++;
602 22615
                if (oc->flags & OC_F_HFM) {
603 672
                        xid = VXID(ObjGetXID(wrk, oc));
604 672
                        dttl = EXP_Dttl(req, oc);
605 672
                        *bocp = hsh_insert_busyobj(wrk, oh);
606 672
                        Lck_Unlock(&oh->mtx);
607 672
                        wrk->stats->cache_hitmiss++;
608 672
                        VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl);
609 672
                        return (HSH_HITMISS);
610
                }
611 21943
                oc->hits++;
612 21943
                boc_progress = oc->boc == NULL ? -1 : oc->boc->fetched_so_far;
613 21943
                AN(hsh_deref_objhead_unlock(wrk, &oh, oc));
614 21943
                Req_LogHit(wrk, req, oc, boc_progress);
615 21943
                return (HSH_HIT);
616
        }
617
618 32351
        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 84
                xid = VXID(ObjGetXID(wrk, exp_oc));
626 84
                dttl = EXP_Dttl(req, exp_oc);
627 84
                *bocp = hsh_insert_busyobj(wrk, oh);
628 84
                Lck_Unlock(&oh->mtx);
629 84
                wrk->stats->cache_hitmiss++;
630 84
                VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl);
631 84
                return (HSH_HITMISS);
632
        }
633
634 32267
        if (exp_oc != NULL && exp_oc->boc != NULL)
635 105
                boc_progress = exp_oc->boc->fetched_so_far;
636
        else
637 32162
                boc_progress = -1;
638
639 32267
        if (!busy_found) {
640 30920
                *bocp = hsh_insert_busyobj(wrk, oh);
641
642 30920
                if (exp_oc != NULL) {
643 2835
                        exp_oc->refcnt++;
644 2835
                        *ocp = exp_oc;
645 2835
                        if (EXP_Ttl_grace(req, exp_oc) >= req->t_req) {
646 1995
                                exp_oc->hits++;
647 1995
                                Lck_Unlock(&oh->mtx);
648 1995
                                Req_LogHit(wrk, req, exp_oc, boc_progress);
649 1995
                                return (HSH_GRACE);
650
                        }
651 840
                }
652 28925
                Lck_Unlock(&oh->mtx);
653 28925
                return (HSH_MISS);
654
        }
655
656 1347
        AN(busy_found);
657 1347
        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 63
                exp_oc->refcnt++;
660 63
                *ocp = exp_oc;
661 63
                exp_oc->hits++;
662 63
                AN(hsh_deref_objhead_unlock(wrk, &oh, NULL));
663 63
                Req_LogHit(wrk, req, exp_oc, boc_progress);
664 63
                return (HSH_GRACE);
665
        }
666
667
        /* There are one or more busy objects, wait for them */
668 1284
        VTAILQ_INSERT_TAIL(&oh->waitinglist, req, w_list);
669
670 1284
        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 1284
        assert(oh->refcnt > 1);
678
679 1284
        req->wrk = NULL;
680 1284
        req->waitinglist_gen = oh->waitinglist_gen;
681
682 1284
        if (DO_DEBUG(DBG_WAITINGLIST))
683 462
                VSLb(req->vsl, SLT_Debug, "on waiting list <%p>", oh);
684
685 1284
        Lck_Unlock(&oh->mtx);
686
687 1284
        wrk->stats->busy_sleep++;
688 1284
        return (HSH_BUSY);
689 56587
}
690
691
/*---------------------------------------------------------------------
692
 * Pick the req's we are going to rush from the waiting list
693
 */
694
695
static void
696 2595
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 2595
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
703 2595
        CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
704 2595
        CHECK_OBJ_NOTNULL(r, RUSH_MAGIC);
705 2595
        VTAILQ_INIT(&r->reqs);
706
707 2595
        if (oc == NULL)
708 21
                return;
709
710 2574
        oh = oc->objhead;
711 2574
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
712 2574
        Lck_AssertHeld(&oh->mtx);
713
714 2574
        AZ(oc->flags & (OC_F_BUSY | OC_F_PRIVATE));
715 2574
        max = cache_param->rush_exponent;
716 2574
        if (oc->flags & (OC_F_WITHDRAWN|OC_F_FAILED))
717 1646
                max = 1;
718 2574
        assert(max > 0);
719
720 2574
        if (oc->waitinglist_gen == 0) {
721 2427
                oc->waitinglist_gen = oh->waitinglist_gen;
722 2427
                oh->waitinglist_gen++;
723 2427
        }
724
725 3858
        for (i = 0; i < max; i++) {
726 3606
                req = VTAILQ_FIRST(&oh->waitinglist);
727 3606
                if (req == NULL)
728 2322
                        break;
729
730 1284
                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 1284
                if (req->waitinglist_gen > oc->waitinglist_gen)
745 0
                        break;
746
747 1284
                AZ(req->wrk);
748 1284
                VTAILQ_REMOVE(&oh->waitinglist, req, w_list);
749 1284
                VTAILQ_INSERT_TAIL(&r->reqs, req, w_list);
750 1284
                req->objcore = oc;
751 1284
                oc->refcnt++;
752 1284
                wrk->stats->busy_wakeup++;
753 1284
        }
754 2595
}
755
756
/*---------------------------------------------------------------------
757
 * Rush req's that came from waiting list.
758
 */
759
760
static void
761 66495
hsh_rush2(struct worker *wrk, struct rush *r)
762
{
763
        struct req *req;
764
765 66495
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
766 66495
        CHECK_OBJ_NOTNULL(r, RUSH_MAGIC);
767
768 67779
        while (!VTAILQ_EMPTY(&r->reqs)) {
769 1284
                req = VTAILQ_FIRST(&r->reqs);
770 1284
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
771 1284
                VTAILQ_REMOVE(&r->reqs, req, w_list);
772 1284
                DSL(DBG_WAITINGLIST, req->vsl->wid, "off waiting list");
773 1284
                if (req->transport->reembark != NULL) {
774
                        // For ESI includes
775 23
                        req->transport->reembark(wrk, req);
776 23
                } 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 1261
                        AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH));
784
                }
785
        }
786 66495
}
787
788
/*---------------------------------------------------------------------
789
 * Purge an entire objhead
790
 */
791
792
unsigned
793 504
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 504
        unsigned i, j, n, n_max, total = 0;
798
        int is_purge;
799
800 504
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
801 504
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
802
803 504
        is_purge = (ttl == 0 && grace == 0 && keep == 0);
804 504
        n_max = WS_ReserveLumps(wrk->aws, sizeof *ocp);
805 504
        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 504
                ocp = WS_Reservation(wrk->aws);
813 504
        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 504
        Lck_Lock(&oh->mtx);
825 504
        oc = VTAILQ_FIRST(&oh->objcs);
826 504
        n = 0;
827 525
        while (1) {
828 2982
                for (; n < n_max && oc != NULL; oc = VTAILQ_NEXT(oc, hsh_list))
829
                {
830 2457
                        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
831 2457
                        assert(oc->objhead == oh);
832 2457
                        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 483
                                continue;
839
                        }
840 1974
                        if (oc->flags & OC_F_DYING)
841 0
                                continue;
842 1974
                        if (is_purge)
843 1764
                                oc->flags |= OC_F_DYING;
844 1974
                        oc->refcnt++;
845 1974
                        ocp[n++] = oc;
846 1974
                }
847
848 525
                Lck_Unlock(&oh->mtx);
849
850 525
                if (n == 0) {
851
                        /* No eligible objcores found. We are finished. */
852 84
                        break;
853
                }
854
855 441
                j = n;
856 441
                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 21
                        j--;
864 21
                        assert(j >= 1); /* True because n_max >= 2 */
865 21
                }
866 2415
                for (i = 0; i < j; i++) {
867 1974
                        CHECK_OBJ_NOTNULL(ocp[i], OBJCORE_MAGIC);
868 1974
                        if (is_purge)
869 1764
                                EXP_Remove(ocp[i], NULL);
870
                        else
871 210
                                EXP_Reduce(ocp[i], ttl_now, ttl, grace, keep);
872 1974
                        (void)HSH_DerefObjCore(wrk, &ocp[i]);
873 1974
                        AZ(ocp[i]);
874 1974
                        total++;
875 1974
                }
876
877 441
                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 420
                        break;
882
                }
883
884 21
                Lck_Lock(&oh->mtx);
885
886
                /* Move the bookmark first and continue scanning the
887
                 * objcores */
888 21
                CHECK_OBJ_NOTNULL(ocp[j], OBJCORE_MAGIC);
889 21
                ocp[0] = ocp[j];
890 21
                n = 1;
891 21
                oc = VTAILQ_NEXT(ocp[0], hsh_list);
892 21
                CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
893
        }
894
895 504
        WS_Release(wrk->aws, 0);
896 504
        if (is_purge)
897 294
                Pool_PurgeStat(total);
898 504
        return (total);
899
}
900
901
/*---------------------------------------------------------------------
902
 * Fail an objcore
903
 */
904
905
void
906 1470
HSH_Fail(struct worker *wrk, struct objcore *oc)
907
{
908
        struct objhead *oh;
909
        struct rush rush;
910
911 1470
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
912 1470
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
913 1470
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
914 1470
        oh = oc->objhead;
915 1470
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
916 1470
        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 1470
        if (oc->boc->state < BOS_STREAM)
929 1092
                assert(oc->flags & (OC_F_BUSY|OC_F_PRIVATE));
930
        else
931 378
                assert(oc->stobj->stevedore != NULL);
932
933 1470
        Lck_Lock(&oh->mtx);
934 1470
        oc->flags |= OC_F_FAILED;
935 1470
        if (oc->flags & OC_F_BUSY) {
936 966
                oc->flags &= ~OC_F_BUSY;
937 966
                hsh_rush1(wrk, oc, &rush);
938 966
        }
939 1470
        Lck_Unlock(&oh->mtx);
940 1470
        hsh_rush2(wrk, &rush);
941 1470
}
942
943
/*---------------------------------------------------------------------
944
 * Mark a fetch we will not need as cancelled
945
 */
946
947
static void
948 7952
hsh_cancel(struct objcore *oc)
949
{
950
        struct objhead *oh;
951
952 7952
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
953 7952
        oh = oc->objhead;
954 7952
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
955
956 7952
        Lck_Lock(&oh->mtx);
957 7952
        oc->flags |= OC_F_CANCEL;
958 7952
        Lck_Unlock(&oh->mtx);
959 7952
}
960
961
/*---------------------------------------------------------------------
962
 * Cancel a fetch when the client does not need it any more
963
 */
964
965
void
966 85576
HSH_Cancel(struct worker *wrk, struct objcore *oc, struct boc *boc)
967
{
968 85576
        struct boc *bocref = NULL;
969
970 85576
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
971
972 85576
        if ((oc->flags & OC_F_TRANSIENT) == 0)
973 51563
                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 34013
        if (boc == NULL)
980 26120
                bocref = boc = HSH_RefBoc(oc);
981
982 34013
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
983
984 34013
        if (oc->flags & OC_F_HFP)
985 630
                AN(oc->flags & OC_F_HFM);
986
987 34013
        if (boc != NULL) {
988 7952
                hsh_cancel(oc);
989 7952
                (void)ObjWaitState(oc, BOS_FINISHED);
990 7952
        }
991
992 34013
        if (bocref != NULL)
993 59
                HSH_DerefBoc(wrk, oc);
994
995 34013
        ObjSlim(wrk, oc);
996 85576
}
997
998
/*---------------------------------------------------------------------
999
 * Withdraw an objcore that will not proceed with a fetch.
1000
 */
1001
1002
void
1003 680
HSH_Withdraw(struct worker *wrk, struct objcore **ocp)
1004
{
1005
        struct objhead *oh;
1006
        struct objcore *oc;
1007
        struct rush rush;
1008
1009 680
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1010 680
        TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC);
1011 680
        INIT_OBJ(&rush, RUSH_MAGIC);
1012
1013 680
        oh = oc->objhead;
1014 680
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
1015
1016 680
        Lck_Lock(&oh->mtx);
1017 680
        AZ(oc->stobj->stevedore);
1018 680
        AN(oc->flags & OC_F_BUSY);
1019 680
        assert(oc->refcnt == 1);
1020 680
        assert(oh->refcnt > 0);
1021 680
        oc->flags &= ~OC_F_BUSY;
1022 680
        oc->flags |= OC_F_WITHDRAWN;
1023 680
        hsh_rush1(wrk, oc, &rush); /* grabs up to 1 oc ref */
1024 680
        assert(hsh_deref_objcore_unlock(wrk, &oc) <= 1);
1025
1026 680
        hsh_rush2(wrk, &rush);
1027 680
}
1028
1029
/*---------------------------------------------------------------------
1030
 * Unbusy an objcore when the object is completely fetched.
1031
 */
1032
1033
void
1034 50063
HSH_Unbusy(struct worker *wrk, struct objcore *oc)
1035
{
1036
        struct objhead *oh;
1037
        struct rush rush;
1038
1039 50063
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1040 50063
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1041 50063
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
1042
1043 50063
        oh = oc->objhead;
1044 50063
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
1045
1046 50063
        AN(oc->stobj->stevedore);
1047 50063
        assert(oh->refcnt > 0);
1048 50063
        assert(oc->refcnt > 0);
1049
1050 50063
        if (oc->flags & OC_F_PRIVATE) {
1051 19697
                AZ(oc->flags & OC_F_BUSY);
1052 19697
                return;
1053
        }
1054
1055 30366
        AN(oc->flags & OC_F_BUSY);
1056 30366
        INIT_OBJ(&rush, RUSH_MAGIC);
1057
1058 30366
        BAN_NewObjCore(oc);
1059 30366
        AN(oc->ban);
1060
1061
        /* XXX: pretouch neighbors on oh->objcs to prevent page-on under mtx */
1062 30366
        Lck_Lock(&oh->mtx);
1063 30366
        assert(oh->refcnt > 0);
1064 30366
        assert(oc->refcnt > 0);
1065 30366
        EXP_RefNewObjcore(oc); /* Takes a ref for expiry */
1066
        /* XXX: strictly speaking, we should sort in Date: order. */
1067 30366
        VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
1068 30366
        VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list);
1069 30366
        oc->flags &= ~OC_F_BUSY;
1070 30366
        if (!VTAILQ_EMPTY(&oh->waitinglist)) {
1071 772
                assert(oh->refcnt > 1);
1072 772
                hsh_rush1(wrk, oc, &rush);
1073 772
        }
1074 30366
        Lck_Unlock(&oh->mtx);
1075 30366
        EXP_Insert(wrk, oc);
1076 30366
        hsh_rush2(wrk, &rush);
1077 50063
}
1078
1079
/*====================================================================
1080
 * HSH_Kill()
1081
 *
1082
 * It's dead Jim, kick it...
1083
 */
1084
1085
void
1086 5607
HSH_Kill(struct objcore *oc)
1087
{
1088
1089 5607
        HSH_Replace(oc, NULL);
1090 5607
}
1091
1092
void
1093 7287
HSH_Replace(struct objcore *oc, const struct objcore *new_oc)
1094
{
1095
1096 7287
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1097 7287
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
1098 7287
        if (new_oc != NULL) {
1099 1680
                CHECK_OBJ(new_oc, OBJCORE_MAGIC);
1100 1680
                assert(oc->objhead == new_oc->objhead);
1101 1680
        }
1102
1103 7287
        Lck_Lock(&oc->objhead->mtx);
1104 7287
        oc->flags |= OC_F_DYING;
1105 7287
        Lck_Unlock(&oc->objhead->mtx);
1106 7287
        EXP_Remove(oc, new_oc);
1107 7287
}
1108
1109
/*====================================================================
1110
 * HSH_Snipe()
1111
 *
1112
 * If objcore is idle, gain a ref and mark it dead.
1113
 */
1114
1115
int
1116 231
HSH_Snipe(const struct worker *wrk, struct objcore *oc)
1117
{
1118 231
        int retval = 0;
1119
1120 231
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1121 231
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1122 231
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
1123
1124 231
        if (oc->refcnt == 1 && !Lck_Trylock(&oc->objhead->mtx)) {
1125 231
                if (oc->refcnt == 1 && !(oc->flags & OC_F_DYING)) {
1126 231
                        oc->flags |= OC_F_DYING;
1127 231
                        oc->refcnt++;
1128 231
                        retval = 1;
1129 231
                }
1130 231
                Lck_Unlock(&oc->objhead->mtx);
1131 231
        }
1132 231
        if (retval)
1133 231
                EXP_Remove(oc, NULL);
1134 231
        return (retval);
1135
}
1136
1137
1138
/*---------------------------------------------------------------------
1139
 * Gain a reference on an objcore
1140
 */
1141
1142
void
1143 55124
HSH_Ref(struct objcore *oc)
1144
{
1145
        struct objhead *oh;
1146
1147 55124
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1148 55124
        oh = oc->objhead;
1149 55124
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1150 55124
        Lck_Lock(&oh->mtx);
1151 55124
        assert(oc->refcnt > 0);
1152 55124
        oc->refcnt++;
1153 55124
        Lck_Unlock(&oh->mtx);
1154 55124
}
1155
1156
/*---------------------------------------------------------------------
1157
 * Gain a reference on the busyobj, if the objcore has one
1158
 */
1159
1160
struct boc *
1161 207134
HSH_RefBoc(const struct objcore *oc)
1162
{
1163
        struct objhead *oh;
1164
        struct boc *boc;
1165
1166 207134
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1167 207134
        oh = oc->objhead;
1168 207134
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1169 207134
        if (oc->boc == NULL)
1170 118656
                return (NULL);
1171 88478
        Lck_Lock(&oh->mtx);
1172 88478
        assert(oc->refcnt > 0);
1173 88478
        boc = oc->boc;
1174 88478
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
1175 88478
        if (boc != NULL) {
1176 88468
                assert(boc->refcount > 0);
1177 88468
                if (boc->state < BOS_FINISHED)
1178 87857
                        boc->refcount++;
1179
                else
1180 611
                        boc = NULL;
1181 88468
        }
1182 88478
        Lck_Unlock(&oh->mtx);
1183 88478
        return (boc);
1184 207128
}
1185
1186
void
1187 156684
HSH_DerefBoc(struct worker *wrk, struct objcore *oc)
1188
{
1189
        struct boc *boc;
1190
        unsigned r;
1191
1192 156684
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1193 156684
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1194 156684
        boc = oc->boc;
1195 156684
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1196 156684
        Lck_Lock(&oc->objhead->mtx);
1197 156684
        assert(oc->refcnt > 0);
1198 156684
        assert(boc->refcount > 0);
1199 156684
        r = --boc->refcount;
1200 156684
        if (r == 0)
1201 68850
                oc->boc = NULL;
1202 156684
        Lck_Unlock(&oc->objhead->mtx);
1203 156684
        if (r == 0)
1204 68852
                ObjBocDone(wrk, oc, &boc);
1205 156684
}
1206
1207
/*--------------------------------------------------------------------
1208
 * Dereference objcore
1209
 *
1210
 * Returns zero if target was destroyed.
1211
 */
1212
1213
int
1214 162792
HSH_DerefObjCore(struct worker *wrk, struct objcore **ocp)
1215
{
1216
        struct objcore *oc;
1217
        struct objhead *oh;
1218
1219 162792
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1220 162792
        TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC);
1221 162792
        assert(oc->refcnt > 0);
1222
1223 162792
        oh = oc->objhead;
1224 162792
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1225
1226 162792
        Lck_Lock(&oh->mtx);
1227 162792
        return (hsh_deref_objcore_unlock(wrk, &oc));
1228
}
1229
1230
static int
1231 163540
hsh_deref_objcore_unlock(struct worker *wrk, struct objcore **ocp)
1232
{
1233
        struct objcore *oc;
1234
        struct objhead *oh;
1235
        int r;
1236
1237 163540
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1238 163540
        TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC);
1239 163540
        assert(oc->refcnt > 0);
1240
1241 163540
        oh = oc->objhead;
1242 163540
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1243
1244 163540
        Lck_AssertHeld(&oh->mtx);
1245 163540
        assert(oh->refcnt > 0);
1246 163540
        r = --oc->refcnt;
1247 163540
        if (!r)
1248 47576
                VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
1249 163540
        Lck_Unlock(&oh->mtx);
1250 163540
        if (r != 0)
1251 115962
                return (r);
1252
1253 47578
        AZ(oc->flags & OC_F_BUSY);
1254 47578
        AZ(oc->exp_flags);
1255
1256 47578
        BAN_DestroyObj(oc);
1257 47578
        AZ(oc->ban);
1258
1259 47578
        if (oc->stobj->stevedore != NULL)
1260 45743
                ObjFreeObj(wrk, oc);
1261 47578
        ObjDestroy(wrk, &oc);
1262
1263
        /* Drop our ref on the objhead */
1264 47578
        assert(oh->refcnt > 0);
1265 47578
        (void)hsh_deref_objhead(wrk, &oh);
1266 47578
        return (0);
1267 163540
}
1268
1269
static int
1270 70849
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 70849
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1278 70849
        TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC);
1279
1280 70849
        Lck_AssertHeld(&oh->mtx);
1281
1282 70849
        if (oh >= private_ohs && oh < private_ohs + vcountof(private_ohs)) {
1283 37207
                assert(VTAILQ_EMPTY(&oh->waitinglist));
1284 37207
                assert(oh->refcnt > 1);
1285 37207
                oh->refcnt--;
1286 37207
                Lck_Unlock(&oh->mtx);
1287 37207
                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 33642
        INIT_OBJ(&rush, RUSH_MAGIC);
1298 33642
        if (!VTAILQ_EMPTY(&oh->waitinglist)) {
1299 177
                assert(oh->refcnt > 1);
1300 177
                hsh_rush1(wrk, oc, &rush);
1301 177
        }
1302
1303 33642
        if (oh->refcnt == 1)
1304 4381
                assert(VTAILQ_EMPTY(&oh->waitinglist));
1305
1306 33642
        assert(oh->refcnt > 0);
1307 33642
        r = hash->deref(wrk, oh); /* Unlocks oh->mtx */
1308 33642
        hsh_rush2(wrk, &rush);
1309 33642
        return (r);
1310 70849
}
1311
1312
static int
1313 47577
hsh_deref_objhead(struct worker *wrk, struct objhead **poh)
1314
{
1315
        struct objhead *oh;
1316
1317 47577
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1318 47577
        TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC);
1319
1320 47577
        Lck_Lock(&oh->mtx);
1321 47577
        return (hsh_deref_objhead_unlock(wrk, &oh, NULL));
1322
}
1323
1324
void
1325 20884
HSH_Init(const struct hash_slinger *slinger)
1326
{
1327
1328 20884
        assert(DIGEST_LEN == VSHA256_LEN);      /* avoid #include pollution */
1329 20884
        hash = slinger;
1330 20884
        if (hash->start != NULL)
1331 20884
                hash->start();
1332 2694036
        for (struct objhead *oh = private_ohs;
1333 2694036
            oh < private_ohs + vcountof(private_ohs);
1334 2673152
            oh++) {
1335 2673152
                hsh_initobjhead(oh);
1336 2673152
                assert(oh->refcnt == 1);
1337 2673152
        }
1338 20884
}