varnish-cache/bin/varnishd/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_varnishd.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
static struct objhead *private_oh;
76
77
static void hsh_rush1(const struct worker *, struct objhead *,
78
    struct rush *, int);
79
static void hsh_rush2(struct worker *, struct rush *);
80
static int hsh_deref_objhead(struct worker *wrk, struct objhead **poh);
81
static int hsh_deref_objhead_unlock(struct worker *wrk, struct objhead **poh,
82
    int);
83
84
/*---------------------------------------------------------------------*/
85
86
#define VCF_RETURN(x) const struct vcf_return VCF_##x[1] = { \
87
        { .name = #x, } \
88
};
89
90
VCF_RETURNS()
91
#undef VCF_RETURN
92
93
/*---------------------------------------------------------------------*/
94
95
static struct objhead *
96 2494
hsh_newobjhead(void)
97
{
98
        struct objhead *oh;
99
100 2494
        ALLOC_OBJ(oh, OBJHEAD_MAGIC);
101 2494
        XXXAN(oh);
102 2494
        oh->refcnt = 1;
103 2494
        VTAILQ_INIT(&oh->objcs);
104 2494
        VTAILQ_INIT(&oh->waitinglist);
105 2494
        Lck_New(&oh->mtx, lck_objhdr);
106 2494
        return (oh);
107
}
108
109
/*---------------------------------------------------------------------*/
110
/* Precreate an objhead and object for later use */
111
static void
112 2599
hsh_prealloc(struct worker *wrk)
113
{
114
115 2599
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
116
117 2599
        if (wrk->wpriv->nobjcore == NULL)
118 1781
                wrk->wpriv->nobjcore = ObjNew(wrk);
119 2599
        CHECK_OBJ_NOTNULL(wrk->wpriv->nobjcore, OBJCORE_MAGIC);
120
121 2599
        if (wrk->wpriv->nobjhead == NULL) {
122 1559
                wrk->wpriv->nobjhead = hsh_newobjhead();
123 1559
                wrk->stats->n_objecthead++;
124 1559
        }
125 2599
        CHECK_OBJ_NOTNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
126
127 2599
        if (hash->prep != NULL)
128 2593
                hash->prep(wrk);
129 2599
}
130
131
/*---------------------------------------------------------------------*/
132
133
struct objcore *
134 1410
HSH_Private(const struct worker *wrk)
135
{
136
        struct objcore *oc;
137
138 1410
        CHECK_OBJ_NOTNULL(private_oh, OBJHEAD_MAGIC);
139
140 1410
        oc = ObjNew(wrk);
141 1410
        AN(oc);
142 1410
        oc->refcnt = 1;
143 1410
        oc->objhead = private_oh;
144 1410
        oc->flags |= OC_F_PRIVATE;
145 1410
        Lck_Lock(&private_oh->mtx);
146 1410
        VTAILQ_INSERT_TAIL(&private_oh->objcs, oc, hsh_list);
147 1410
        private_oh->refcnt++;
148 1410
        Lck_Unlock(&private_oh->mtx);
149 1410
        return (oc);
150
}
151
152
/*---------------------------------------------------------------------*/
153
154
void
155 944
HSH_Cleanup(const struct worker *wrk)
156
{
157
158 944
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
159 944
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
160 944
        if (wrk->wpriv->nobjcore != NULL)
161 2
                ObjDestroy(wrk, &wrk->wpriv->nobjcore);
162
163 944
        if (wrk->wpriv->nobjhead != NULL) {
164 2
                CHECK_OBJ(wrk->wpriv->nobjhead, OBJHEAD_MAGIC);
165 2
                Lck_Delete(&wrk->wpriv->nobjhead->mtx);
166 2
                FREE_OBJ(wrk->wpriv->nobjhead);
167 2
                wrk->stats->n_objecthead--;
168 2
        }
169 944
        if (wrk->wpriv->nhashpriv != NULL) {
170
                /* XXX: If needed, add slinger method for this */
171 2
                free(wrk->wpriv->nhashpriv);
172 2
                wrk->wpriv->nhashpriv = NULL;
173 2
        }
174 944
}
175
176
void
177 0
HSH_DeleteObjHead(const struct worker *wrk, struct objhead *oh)
178
{
179 0
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
180 0
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
181
182 0
        AZ(oh->refcnt);
183 0
        assert(VTAILQ_EMPTY(&oh->objcs));
184 0
        assert(VTAILQ_EMPTY(&oh->waitinglist));
185 0
        Lck_Delete(&oh->mtx);
186 0
        wrk->stats->n_objecthead--;
187 0
        FREE_OBJ(oh);
188 0
}
189
190
void
191 14584
HSH_AddString(struct req *req, void *ctx, const char *str)
192
{
193
194 14584
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
195 14584
        AN(ctx);
196 14584
        if (str != NULL) {
197 7293
                VSHA256_Update(ctx, str, strlen(str));
198 7293
                VSLbs(req->vsl, SLT_Hash, TOSTRAND(str));
199 7293
        } else
200 7291
                VSHA256_Update(ctx, &str, 1);
201 14584
}
202
203
/*---------------------------------------------------------------------
204
 * This is a debugging hack to enable testing of boundary conditions
205
 * in the hash algorithm.
206
 * We trap the first 9 different digests and translate them to different
207
 * digests with edge bit conditions
208
 */
209
210
static struct hsh_magiclist {
211
        unsigned char was[VSHA256_LEN];
212
        unsigned char now[VSHA256_LEN];
213
} hsh_magiclist[] = {
214
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
218
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
219
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } },
222
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 } },
226
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
227
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
229
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40 } },
230
        { .now = {      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
231
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
233
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 } },
234
        { .now = {      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
238
        { .now = {      0x02, 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 = {      0x80, 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, 0x00 } },
246
        { .now = {      0x40, 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, 0x00 } },
250
};
251
252
#define HSH_NMAGIC (sizeof hsh_magiclist / sizeof hsh_magiclist[0])
253
254
static void
255 18
hsh_testmagic(void *result)
256
{
257
        size_t i, j;
258
        static size_t nused = 0;
259
260 90
        for (i = 0; i < nused; i++)
261 81
                if (!memcmp(hsh_magiclist[i].was, result, VSHA256_LEN))
262 9
                        break;
263 18
        if (i == nused && i < HSH_NMAGIC)
264 9
                memcpy(hsh_magiclist[nused++].was, result, VSHA256_LEN);
265 18
        if (i == nused)
266 0
                return;
267 18
        assert(i < HSH_NMAGIC);
268 18
        fprintf(stderr, "HASHMAGIC: <");
269 594
        for (j = 0; j < VSHA256_LEN; j++)
270 576
                fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
271 18
        fprintf(stderr, "> -> <");
272 18
        memcpy(result, hsh_magiclist[i].now, VSHA256_LEN);
273 594
        for (j = 0; j < VSHA256_LEN; j++)
274 576
                fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
275 18
        fprintf(stderr, ">\n");
276 18
}
277
278
/*---------------------------------------------------------------------
279
 * Insert an object which magically appears out of nowhere or, more likely,
280
 * comes off some persistent storage device.
281
 * Insert it with a reference held.
282
 */
283
284
void
285 17
HSH_Insert(struct worker *wrk, const void *digest, struct objcore *oc,
286
    struct ban *ban)
287
{
288
        struct objhead *oh;
289
        struct rush rush;
290
291 17
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
292 17
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
293 17
        AN(digest);
294 17
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
295 17
        AN(ban);
296 17
        AN(oc->flags & OC_F_BUSY);
297 17
        AZ(oc->flags & OC_F_PRIVATE);
298 17
        assert(oc->refcnt == 1);
299 17
        INIT_OBJ(&rush, RUSH_MAGIC);
300
301 17
        hsh_prealloc(wrk);
302
303 17
        AN(wrk->wpriv->nobjhead);
304 17
        oh = hash->lookup(wrk, digest, &wrk->wpriv->nobjhead);
305 17
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
306 17
        Lck_AssertHeld(&oh->mtx);
307 17
        assert(oh->refcnt > 0);
308
309
        /* Mark object busy and insert (precreated) objcore in
310
           objecthead. The new object inherits our objhead reference. */
311 17
        oc->objhead = oh;
312 17
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
313 17
        EXP_RefNewObjcore(oc);
314 17
        Lck_Unlock(&oh->mtx);
315
316 17
        BAN_RefBan(oc, ban);
317 17
        AN(oc->ban);
318
319
        /* Move the object first in the oh list, unbusy it and run the
320
           waitinglist if necessary */
321 17
        Lck_Lock(&oh->mtx);
322 17
        VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
323 17
        VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list);
324 17
        oc->flags &= ~OC_F_BUSY;
325 17
        if (!VTAILQ_EMPTY(&oh->waitinglist))
326 0
                hsh_rush1(wrk, oh, &rush, HSH_RUSH_POLICY);
327 17
        Lck_Unlock(&oh->mtx);
328 17
        hsh_rush2(wrk, &rush);
329
330 17
        EXP_Insert(wrk, oc);
331 17
}
332
333
/*---------------------------------------------------------------------
334
 */
335
336
static struct objcore *
337 1497
hsh_insert_busyobj(const struct worker *wrk, struct objhead *oh)
338
{
339
        struct objcore *oc;
340
341 1497
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
342 1497
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
343 1497
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
344 1497
        Lck_AssertHeld(&oh->mtx);
345
346 1497
        oc = wrk->wpriv->nobjcore;
347 1497
        wrk->wpriv->nobjcore = NULL;
348 1497
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
349
350 1497
        AN(oc->flags & OC_F_BUSY);
351 1497
        oc->refcnt = 1;         /* Owned by busyobj */
352 1497
        oc->objhead = oh;
353 1497
        VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list);
354 1497
        return (oc);
355
}
356
357
/*---------------------------------------------------------------------
358
 */
359
360
enum lookup_e
361 2582
HSH_Lookup(struct req *req, struct objcore **ocp, struct objcore **bocp)
362
{
363
        struct worker *wrk;
364
        struct objhead *oh;
365
        struct objcore *oc;
366
        struct objcore *exp_oc;
367
        const struct vcf_return *vr;
368
        vtim_real exp_t_origin;
369
        int busy_found;
370
        const uint8_t *vary;
371
        intmax_t boc_progress;
372 2582
        unsigned xid = 0;
373
        unsigned ban_checks;
374
        unsigned ban_any_variant;
375 2582
        float dttl = 0.0;
376
377 2582
        AN(ocp);
378 2582
        *ocp = NULL;
379 2582
        AN(bocp);
380 2582
        *bocp = NULL;
381
382 2582
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
383 2582
        wrk = req->wrk;
384 2582
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
385 2582
        CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC);
386 2582
        CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC);
387 2582
        CHECK_OBJ_ORNULL(req->vcf, VCF_MAGIC);
388 2582
        AN(hash);
389
390 2582
        hsh_prealloc(wrk);
391 2582
        if (DO_DEBUG(DBG_HASHEDGE))
392 18
                hsh_testmagic(req->digest);
393
394 2582
        if (req->hash_objhead != NULL) {
395
                /*
396
                 * This req came off the waiting list, and brings an
397
                 * oh refcnt with it.
398
                 */
399 48
                CHECK_OBJ_NOTNULL(req->hash_objhead, OBJHEAD_MAGIC);
400 48
                oh = req->hash_objhead;
401 48
                Lck_Lock(&oh->mtx);
402 48
                req->hash_objhead = NULL;
403 48
        } else {
404 2534
                AN(wrk->wpriv->nobjhead);
405 2534
                oh = hash->lookup(wrk, req->digest, &wrk->wpriv->nobjhead);
406
        }
407
408 2582
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
409 2582
        Lck_AssertHeld(&oh->mtx);
410
411 2582
        if (req->hash_always_miss) {
412
                /* XXX: should we do predictive Vary in this case ? */
413
                /* Insert new objcore in objecthead and release mutex */
414 14
                *bocp = hsh_insert_busyobj(wrk, oh);
415
                /* NB: no deref of objhead, new object inherits reference */
416 14
                Lck_Unlock(&oh->mtx);
417 14
                return (HSH_MISS);
418
        }
419
420 2568
        assert(oh->refcnt > 0);
421 2568
        busy_found = 0;
422 2568
        exp_oc = NULL;
423 2568
        exp_t_origin = 0.0;
424 2568
        ban_checks = 0;
425 2568
        ban_any_variant = cache_param->ban_any_variant;
426 5475
        VTAILQ_FOREACH(oc, &oh->objcs, hsh_list) {
427
                /* Must be at least our own ref + the objcore we examine */
428 3977
                assert(oh->refcnt > 1);
429 3977
                CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
430 3977
                assert(oc->objhead == oh);
431 3977
                assert(oc->refcnt > 0);
432
433 3977
                if (oc->flags & OC_F_DYING)
434 0
                        continue;
435 3977
                if (oc->flags & OC_F_FAILED)
436 0
                        continue;
437
438 3977
                CHECK_OBJ_ORNULL(oc->boc, BOC_MAGIC);
439 3977
                if (oc->flags & OC_F_BUSY) {
440 54
                        if (req->hash_ignore_busy)
441 1
                                continue;
442
443 57
                        if (oc->boc && oc->boc->vary != NULL &&
444 4
                            !req->hash_ignore_vary &&
445 4
                            !VRY_Match(req, oc->boc->vary)) {
446 1
                                wrk->strangelove++;
447 1
                                continue;
448
                        }
449
450 52
                        busy_found = 1;
451 52
                        continue;
452
                }
453
454 3923
                if (oc->ttl <= 0.)
455 46
                        continue;
456
457 3877
                if (ban_checks++ < ban_any_variant
458 3877
                    && BAN_CheckObject(wrk, oc, req)) {
459 32
                        oc->flags |= OC_F_DYING;
460 32
                        EXP_Remove(oc, NULL);
461 32
                        continue;
462
                }
463
464 3845
                if (!req->hash_ignore_vary && ObjHasAttr(wrk, oc, OA_VARY)) {
465 2786
                        vary = ObjGetAttr(wrk, oc, OA_VARY, NULL);
466 2786
                        AN(vary);
467 2786
                        if (!VRY_Match(req, vary)) {
468 2633
                                wrk->strangelove++;
469 2633
                                continue;
470
                        }
471 153
                }
472
473 1212
                if (ban_checks >= ban_any_variant
474 1212
                    && BAN_CheckObject(wrk, oc, req)) {
475 2
                        oc->flags |= OC_F_DYING;
476 2
                        EXP_Remove(oc, NULL);
477 2
                        continue;
478
                }
479
480 1210
                if (req->vcf != NULL) {
481 8
                        vr = req->vcf->func(req, &oc, &exp_oc, 0);
482 8
                        if (vr == VCF_CONTINUE)
483 4
                                continue;
484 4
                        if (vr == VCF_MISS) {
485 3
                                oc = NULL;
486 3
                                break;
487
                        }
488 1
                        if (vr == VCF_HIT)
489 1
                                break;
490 0
                        assert(vr == VCF_DEFAULT);
491 0
                }
492
493 1202
                if (EXP_Ttl(req, oc) > req->t_req) {
494 1066
                        assert(oh->refcnt > 1);
495 1066
                        assert(oc->objhead == oh);
496 1066
                        break;
497
                }
498
499 136
                if (EXP_Ttl(NULL, oc) <= req->t_req && /* ignore req.ttl */
500 131
                    oc->t_origin > exp_t_origin) {
501
                        /* record the newest object */
502 130
                        exp_oc = oc;
503 130
                        exp_t_origin = oc->t_origin;
504 130
                        assert(oh->refcnt > 1);
505 130
                        assert(exp_oc->objhead == oh);
506 130
                }
507 136
        }
508
509 2568
        if (req->vcf != NULL)
510 6
                (void)req->vcf->func(req, &oc, &exp_oc, 1);
511
512 2568
        if (oc != NULL && oc->flags & OC_F_HFP) {
513 11
                xid = VXID(ObjGetXID(wrk, oc));
514 11
                dttl = EXP_Dttl(req, oc);
515 11
                AN(hsh_deref_objhead_unlock(wrk, &oh, HSH_RUSH_POLICY));
516 11
                wrk->stats->cache_hitpass++;
517 11
                VSLb(req->vsl, SLT_HitPass, "%u %.6f", xid, dttl);
518 11
                return (HSH_HITPASS);
519
        }
520
521 2557
        if (oc != NULL) {
522 1057
                *ocp = oc;
523 1057
                oc->refcnt++;
524 1057
                if (oc->flags & OC_F_HFM) {
525 34
                        xid = VXID(ObjGetXID(wrk, oc));
526 34
                        dttl = EXP_Dttl(req, oc);
527 34
                        *bocp = hsh_insert_busyobj(wrk, oh);
528 34
                        Lck_Unlock(&oh->mtx);
529 34
                        wrk->stats->cache_hitmiss++;
530 34
                        VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl);
531 34
                        return (HSH_HITMISS);
532
                }
533 1023
                oc->hits++;
534 1023
                boc_progress = oc->boc == NULL ? -1 : oc->boc->fetched_so_far;
535 1023
                AN(hsh_deref_objhead_unlock(wrk, &oh, HSH_RUSH_POLICY));
536 1023
                Req_LogHit(wrk, req, oc, boc_progress);
537 1023
                return (HSH_HIT);
538
        }
539
540 1500
        if (exp_oc != NULL && exp_oc->flags & OC_F_HFM) {
541
                /*
542
                 * expired HFM ("grace/keep HFM")
543
                 *
544
                 * XXX should HFM objects actually have grace/keep ?
545
                 * XXX also:  why isn't *ocp = exp_oc ?
546
                 */
547 4
                xid = VXID(ObjGetXID(wrk, exp_oc));
548 4
                dttl = EXP_Dttl(req, exp_oc);
549 4
                *bocp = hsh_insert_busyobj(wrk, oh);
550 4
                Lck_Unlock(&oh->mtx);
551 4
                wrk->stats->cache_hitmiss++;
552 4
                VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl);
553 4
                return (HSH_HITMISS);
554
        }
555
556 1496
        if (exp_oc != NULL && exp_oc->boc != NULL)
557 4
                boc_progress = exp_oc->boc->fetched_so_far;
558
        else
559 1492
                boc_progress = -1;
560
561 1496
        if (!busy_found) {
562 1445
                *bocp = hsh_insert_busyobj(wrk, oh);
563
564 1445
                if (exp_oc != NULL) {
565 120
                        exp_oc->refcnt++;
566 120
                        *ocp = exp_oc;
567 120
                        if (EXP_Ttl_grace(req, exp_oc) >= req->t_req) {
568 92
                                exp_oc->hits++;
569 92
                                Lck_Unlock(&oh->mtx);
570 92
                                Req_LogHit(wrk, req, exp_oc, boc_progress);
571 92
                                return (HSH_GRACE);
572
                        }
573 28
                }
574 1353
                Lck_Unlock(&oh->mtx);
575 1353
                return (HSH_MISS);
576
        }
577
578 51
        AN(busy_found);
579 51
        if (exp_oc != NULL && EXP_Ttl_grace(req, exp_oc) >= req->t_req) {
580
                /* we do not wait on the busy object if in grace */
581 3
                exp_oc->refcnt++;
582 3
                *ocp = exp_oc;
583 3
                exp_oc->hits++;
584 3
                AN(hsh_deref_objhead_unlock(wrk, &oh, 0));
585 3
                Req_LogHit(wrk, req, exp_oc, boc_progress);
586 3
                return (HSH_GRACE);
587
        }
588
589
        /* There are one or more busy objects, wait for them */
590 48
        VTAILQ_INSERT_TAIL(&oh->waitinglist, req, w_list);
591
592 48
        AZ(req->hash_ignore_busy);
593
594
        /*
595
         * The objhead reference transfers to the sess, we get it
596
         * back when the sess comes off the waiting list and
597
         * calls us again
598
         */
599 48
        req->hash_objhead = oh;
600 48
        req->wrk = NULL;
601 48
        req->waitinglist = 1;
602
603 48
        if (DO_DEBUG(DBG_WAITINGLIST))
604 14
                VSLb(req->vsl, SLT_Debug, "on waiting list <%p>", oh);
605
606 48
        Lck_Unlock(&oh->mtx);
607
608 48
        wrk->stats->busy_sleep++;
609 48
        return (HSH_BUSY);
610 2582
}
611
612
/*---------------------------------------------------------------------
613
 * Pick the req's we are going to rush from the waiting list
614
 */
615
616
static void
617 39
hsh_rush1(const struct worker *wrk, struct objhead *oh, struct rush *r, int max)
618
{
619
        int i;
620
        struct req *req;
621
622 39
        if (max == 0)
623 2
                return;
624 37
        if (max == HSH_RUSH_POLICY)
625 37
                max = cache_param->rush_exponent;
626 37
        assert(max > 0);
627
628 37
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
629 37
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
630 37
        CHECK_OBJ_NOTNULL(r, RUSH_MAGIC);
631 37
        VTAILQ_INIT(&r->reqs);
632 37
        Lck_AssertHeld(&oh->mtx);
633 85
        for (i = 0; i < max; i++) {
634 77
                req = VTAILQ_FIRST(&oh->waitinglist);
635 77
                if (req == NULL)
636 29
                        break;
637 48
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
638 48
                wrk->stats->busy_wakeup++;
639 48
                AZ(req->wrk);
640 48
                VTAILQ_REMOVE(&oh->waitinglist, req, w_list);
641 48
                VTAILQ_INSERT_TAIL(&r->reqs, req, w_list);
642 48
                req->waitinglist = 0;
643 48
        }
644 39
}
645
646
/*---------------------------------------------------------------------
647
 * Rush req's that came from waiting list.
648
 */
649
650
static void
651 10712
hsh_rush2(struct worker *wrk, struct rush *r)
652
{
653
        struct req *req;
654
655 10712
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
656 10712
        CHECK_OBJ_NOTNULL(r, RUSH_MAGIC);
657
658 10760
        while (!VTAILQ_EMPTY(&r->reqs)) {
659 48
                req = VTAILQ_FIRST(&r->reqs);
660 48
                CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
661 48
                VTAILQ_REMOVE(&r->reqs, req, w_list);
662 48
                DSL(DBG_WAITINGLIST, req->vsl->wid, "off waiting list");
663 48
                if (req->transport->reembark != NULL) {
664
                        // For ESI includes
665 1
                        req->transport->reembark(wrk, req);
666 1
                } else {
667
                        /*
668
                         * We ignore the queue limits which apply to new
669
                         * requests because if we fail to reschedule there
670
                         * may be vmod_privs to cleanup and we need a proper
671
                         * workerthread for that.
672
                         */
673 47
                        AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH));
674
                }
675
        }
676 10712
}
677
678
/*---------------------------------------------------------------------
679
 * Purge an entire objhead
680
 */
681
682
unsigned
683 21
HSH_Purge(struct worker *wrk, struct objhead *oh, vtim_real ttl_now,
684
    vtim_dur ttl, vtim_dur grace, vtim_dur keep)
685
{
686
        struct objcore *oc, *oc_nows[2], **ocp;
687 21
        unsigned i, j, n, n_max, total = 0;
688
        int is_purge;
689
690 21
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
691 21
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
692
693 21
        is_purge = (ttl == 0 && grace == 0 && keep == 0);
694 21
        n_max = WS_ReserveLumps(wrk->aws, sizeof *ocp);
695 21
        if (n_max < 2) {
696
                /* No space on the workspace. Give it a stack buffer of 2
697
                 * elements, which is the minimum for the algorithm
698
                 * below. */
699 0
                ocp = oc_nows;
700 0
                n_max = 2;
701 0
        } else
702 21
                ocp = WS_Reservation(wrk->aws);
703 21
        AN(ocp);
704
705
        /* Note: This algorithm uses OC references in the list as
706
         * bookmarks, in order to know how far into the list we were when
707
         * releasing the mutex partway through and want to resume
708
         * again. This relies on the list not being reordered while we are
709
         * not holding the mutex. The only place where that happens is in
710
         * HSH_Unbusy(), where an OC_F_BUSY OC is moved first in the
711
         * list. This does not cause problems because we skip OC_F_BUSY
712
         * OCs. */
713
714 21
        Lck_Lock(&oh->mtx);
715 21
        oc = VTAILQ_FIRST(&oh->objcs);
716 21
        n = 0;
717 22
        while (1) {
718 130
                for (; n < n_max && oc != NULL; oc = VTAILQ_NEXT(oc, hsh_list))
719
                {
720 108
                        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
721 108
                        assert(oc->objhead == oh);
722 108
                        if (oc->flags & OC_F_BUSY) {
723
                                /* We cannot purge busy objects here, because
724
                                 * their owners have special rights to them,
725
                                 * and may nuke them without concern for the
726
                                 * refcount, which by definition always must
727
                                 * be one, so they don't check. */
728 17
                                continue;
729
                        }
730 91
                        if (oc->flags & OC_F_DYING)
731 0
                                continue;
732 91
                        if (is_purge)
733 81
                                oc->flags |= OC_F_DYING;
734 91
                        oc->refcnt++;
735 91
                        ocp[n++] = oc;
736 91
                }
737
738 22
                Lck_Unlock(&oh->mtx);
739
740 22
                if (n == 0) {
741
                        /* No eligible objcores found. We are finished. */
742 4
                        break;
743
                }
744
745 18
                j = n;
746 18
                if (oc != NULL) {
747
                        /* There are more objects on the objhead that we
748
                         * have not yet looked at, but no more space on
749
                         * the objcore reference list. Do not process the
750
                         * last one, it will be used as the bookmark into
751
                         * the objcore list for the next iteration of the
752
                         * outer loop. */
753 1
                        j--;
754 1
                        assert(j >= 1); /* True because n_max >= 2 */
755 1
                }
756 109
                for (i = 0; i < j; i++) {
757 91
                        CHECK_OBJ_NOTNULL(ocp[i], OBJCORE_MAGIC);
758 91
                        if (is_purge)
759 81
                                EXP_Remove(ocp[i], NULL);
760
                        else
761 10
                                EXP_Reduce(ocp[i], ttl_now, ttl, grace, keep);
762 91
                        (void)HSH_DerefObjCore(wrk, &ocp[i], 0);
763 91
                        AZ(ocp[i]);
764 91
                        total++;
765 91
                }
766
767 18
                if (j == n) {
768
                        /* No bookmark set, that means we got to the end
769
                         * of the objcore list in the previous run and are
770
                         * finished. */
771 17
                        break;
772
                }
773
774 1
                Lck_Lock(&oh->mtx);
775
776
                /* Move the bookmark first and continue scanning the
777
                 * objcores */
778 1
                CHECK_OBJ_NOTNULL(ocp[j], OBJCORE_MAGIC);
779 1
                ocp[0] = ocp[j];
780 1
                n = 1;
781 1
                oc = VTAILQ_NEXT(ocp[0], hsh_list);
782 1
                CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC);
783
        }
784
785 21
        WS_Release(wrk->aws, 0);
786 21
        if (is_purge)
787 11
                Pool_PurgeStat(total);
788 21
        return (total);
789
}
790
791
/*---------------------------------------------------------------------
792
 * Fail an objcore
793
 */
794
795
void
796 67
HSH_Fail(struct objcore *oc)
797
{
798
        struct objhead *oh;
799
800 67
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
801 67
        oh = oc->objhead;
802 67
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
803
804
        /*
805
         * We have to have either a busy bit, so that HSH_Lookup
806
         * will not consider this oc, or an object hung of the oc
807
         * so that it can consider it.
808
         */
809 67
        assert((oc->flags & OC_F_BUSY) || (oc->stobj->stevedore != NULL));
810
811 67
        Lck_Lock(&oh->mtx);
812 67
        oc->flags |= OC_F_FAILED;
813 67
        Lck_Unlock(&oh->mtx);
814 67
}
815
816
/*---------------------------------------------------------------------
817
 * Mark a fetch we will not need as cancelled
818
 */
819
820
static void
821 352
hsh_cancel(struct objcore *oc)
822
{
823
        struct objhead *oh;
824
825 352
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
826 352
        oh = oc->objhead;
827 352
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
828
829 352
        Lck_Lock(&oh->mtx);
830 352
        oc->flags |= OC_F_CANCEL;
831 352
        Lck_Unlock(&oh->mtx);
832 352
}
833
834
/*---------------------------------------------------------------------
835
 * Cancel a fetch when the client does not need it any more
836
 */
837
838
void
839 3735
HSH_Cancel(struct worker *wrk, struct objcore *oc, struct boc *boc)
840
{
841 3735
        struct boc *bocref = NULL;
842
843 3735
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
844
845 3735
        if ((oc->flags & OC_F_TRANSIENT) == 0)
846 2337
                return;
847
848
        /*
849
         * NB: we use two distinct variables to only release the reference if
850
         * we had to acquire one. The caller-provided boc is optional.
851
         */
852 1398
        if (boc == NULL)
853 1049
                bocref = boc = HSH_RefBoc(oc);
854
855 1398
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
856
857 1398
        if (oc->flags & OC_F_HFP)
858 22
                AN(oc->flags & OC_F_HFM);
859
860 1398
        if (boc != NULL) {
861 352
                hsh_cancel(oc);
862 352
                ObjWaitState(oc, BOS_FINISHED);
863 352
        }
864
865 1398
        if (bocref != NULL)
866 3
                HSH_DerefBoc(wrk, oc);
867
868 1398
        ObjSlim(wrk, oc);
869 3735
}
870
871
/*---------------------------------------------------------------------
872
 * Unbusy an objcore when the object is completely fetched.
873
 */
874
875
void
876 2197
HSH_Unbusy(struct worker *wrk, struct objcore *oc)
877
{
878
        struct objhead *oh;
879
        struct rush rush;
880
881 2197
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
882 2197
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
883 2197
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
884
885 2197
        oh = oc->objhead;
886 2197
        CHECK_OBJ(oh, OBJHEAD_MAGIC);
887 2197
        INIT_OBJ(&rush, RUSH_MAGIC);
888
889 2197
        AN(oc->stobj->stevedore);
890 2197
        AN(oc->flags & OC_F_BUSY);
891 2197
        assert(oh->refcnt > 0);
892 2197
        assert(oc->refcnt > 0);
893
894 2197
        if (!(oc->flags & OC_F_PRIVATE)) {
895 1419
                BAN_NewObjCore(oc);
896 1419
                AN(oc->ban);
897 1419
        }
898
899
        /* XXX: pretouch neighbors on oh->objcs to prevent page-on under mtx */
900 2197
        Lck_Lock(&oh->mtx);
901 2197
        assert(oh->refcnt > 0);
902 2197
        assert(oc->refcnt > 0);
903 2197
        if (!(oc->flags & OC_F_PRIVATE))
904 1419
                EXP_RefNewObjcore(oc); /* Takes a ref for expiry */
905
        /* XXX: strictly speaking, we should sort in Date: order. */
906 2197
        VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
907 2197
        VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list);
908 2197
        oc->flags &= ~OC_F_BUSY;
909 2197
        if (!VTAILQ_EMPTY(&oh->waitinglist)) {
910 27
                assert(oh->refcnt > 1);
911 27
                hsh_rush1(wrk, oh, &rush, HSH_RUSH_POLICY);
912 27
        }
913 2197
        Lck_Unlock(&oh->mtx);
914 2197
        EXP_Insert(wrk, oc); /* Does nothing unless EXP_RefNewObjcore was
915
                              * called */
916 2197
        hsh_rush2(wrk, &rush);
917 2197
}
918
919
/*====================================================================
920
 * HSH_Kill()
921
 *
922
 * It's dead Jim, kick it...
923
 */
924
925
void
926 178
HSH_Kill(struct objcore *oc)
927
{
928
929 178
        HSH_Replace(oc, NULL);
930 178
}
931
932
void
933 251
HSH_Replace(struct objcore *oc, const struct objcore *new_oc)
934
{
935
936 251
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
937 251
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
938 251
        if (new_oc != NULL) {
939 73
                CHECK_OBJ(new_oc, OBJCORE_MAGIC);
940 73
                assert(oc->objhead == new_oc->objhead);
941 73
        }
942
943 251
        Lck_Lock(&oc->objhead->mtx);
944 251
        oc->flags |= OC_F_DYING;
945 251
        Lck_Unlock(&oc->objhead->mtx);
946 251
        EXP_Remove(oc, new_oc);
947 251
}
948
949
/*====================================================================
950
 * HSH_Snipe()
951
 *
952
 * If objcore is idle, gain a ref and mark it dead.
953
 */
954
955
int
956 11
HSH_Snipe(const struct worker *wrk, struct objcore *oc)
957
{
958 11
        int retval = 0;
959
960 11
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
961 11
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
962 11
        CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC);
963
964 11
        if (oc->refcnt == 1 && !Lck_Trylock(&oc->objhead->mtx)) {
965 11
                if (oc->refcnt == 1 && !(oc->flags & OC_F_DYING)) {
966 11
                        oc->flags |= OC_F_DYING;
967 11
                        oc->refcnt++;
968 11
                        retval = 1;
969 11
                }
970 11
                Lck_Unlock(&oc->objhead->mtx);
971 11
        }
972 11
        if (retval)
973 11
                EXP_Remove(oc, NULL);
974 11
        return (retval);
975
}
976
977
978
/*---------------------------------------------------------------------
979
 * Gain a reference on an objcore
980
 */
981
982
void
983 2418
HSH_Ref(struct objcore *oc)
984
{
985
        struct objhead *oh;
986
987 2418
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
988 2418
        oh = oc->objhead;
989 2418
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
990 2418
        Lck_Lock(&oh->mtx);
991 2418
        assert(oc->refcnt > 0);
992 2418
        oc->refcnt++;
993 2418
        Lck_Unlock(&oh->mtx);
994 2418
}
995
996
/*---------------------------------------------------------------------
997
 * Gain a reference on the busyobj, if the objcore has one
998
 */
999
1000
struct boc *
1001 9393
HSH_RefBoc(const struct objcore *oc)
1002
{
1003
        struct objhead *oh;
1004
        struct boc *boc;
1005
1006 9393
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1007 9393
        oh = oc->objhead;
1008 9393
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1009 9393
        if (oc->boc == NULL)
1010 5495
                return (NULL);
1011 3898
        Lck_Lock(&oh->mtx);
1012 3898
        assert(oc->refcnt > 0);
1013 3898
        boc = oc->boc;
1014 3898
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
1015 3898
        if (boc != NULL) {
1016 3893
                assert(boc->refcount > 0);
1017 3893
                if (boc->state < BOS_FINISHED)
1018 3867
                        boc->refcount++;
1019
                else
1020 26
                        boc = NULL;
1021 3893
        }
1022 3898
        Lck_Unlock(&oh->mtx);
1023 3898
        return (boc);
1024 9393
}
1025
1026
void
1027 6757
HSH_DerefBoc(struct worker *wrk, struct objcore *oc)
1028
{
1029
        struct boc *boc;
1030
        unsigned r;
1031
1032 6757
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1033 6757
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
1034 6757
        boc = oc->boc;
1035 6757
        CHECK_OBJ_NOTNULL(boc, BOC_MAGIC);
1036 6757
        Lck_Lock(&oc->objhead->mtx);
1037 6757
        assert(oc->refcnt > 0);
1038 6757
        assert(boc->refcount > 0);
1039 6757
        r = --boc->refcount;
1040 6757
        if (r == 0)
1041 2891
                oc->boc = NULL;
1042 6757
        Lck_Unlock(&oc->objhead->mtx);
1043 6757
        if (r == 0)
1044 2891
                ObjBocDone(wrk, oc, &boc);
1045 6757
}
1046
1047
/*--------------------------------------------------------------------
1048
 * Dereference objcore
1049
 *
1050
 * Returns zero if target was destroyed.
1051
 */
1052
1053
int
1054 7013
HSH_DerefObjCore(struct worker *wrk, struct objcore **ocp, int rushmax)
1055
{
1056
        struct objcore *oc;
1057
        struct objhead *oh;
1058
        struct rush rush;
1059
        int r;
1060
1061 7013
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1062 7013
        TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC);
1063 7013
        assert(oc->refcnt > 0);
1064 7013
        INIT_OBJ(&rush, RUSH_MAGIC);
1065
1066 7013
        oh = oc->objhead;
1067 7013
        CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
1068
1069 7013
        Lck_Lock(&oh->mtx);
1070 7013
        assert(oh->refcnt > 0);
1071 7013
        r = --oc->refcnt;
1072 7013
        if (!r)
1073 1858
                VTAILQ_REMOVE(&oh->objcs, oc, hsh_list);
1074 7013
        if (!VTAILQ_EMPTY(&oh->waitinglist)) {
1075 6
                assert(oh->refcnt > 1);
1076 6
                hsh_rush1(wrk, oh, &rush, rushmax);
1077 6
        }
1078 7013
        Lck_Unlock(&oh->mtx);
1079 7013
        hsh_rush2(wrk, &rush);
1080 7013
        if (r != 0)
1081 5155
                return (r);
1082
1083 1858
        AZ(oc->exp_flags);
1084
1085 1858
        BAN_DestroyObj(oc);
1086 1858
        AZ(oc->ban);
1087
1088 1858
        if (oc->stobj->stevedore != NULL)
1089 1773
                ObjFreeObj(wrk, oc);
1090 1858
        ObjDestroy(wrk, &oc);
1091
1092
        /* Drop our ref on the objhead */
1093 1858
        assert(oh->refcnt > 0);
1094 1858
        (void)hsh_deref_objhead(wrk, &oh);
1095 1858
        return (0);
1096 7013
}
1097
1098
static int
1099 2895
hsh_deref_objhead_unlock(struct worker *wrk, struct objhead **poh, int max)
1100
{
1101
        struct objhead *oh;
1102
        struct rush rush;
1103
        int r;
1104
1105 2895
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1106 2895
        TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC);
1107
1108 2895
        Lck_AssertHeld(&oh->mtx);
1109
1110 2895
        if (oh == private_oh) {
1111 1410
                assert(VTAILQ_EMPTY(&oh->waitinglist));
1112 1410
                assert(oh->refcnt > 1);
1113 1410
                oh->refcnt--;
1114 1410
                Lck_Unlock(&oh->mtx);
1115 1410
                return (1);
1116
        }
1117
1118 1485
        INIT_OBJ(&rush, RUSH_MAGIC);
1119 1485
        if (!VTAILQ_EMPTY(&oh->waitinglist)) {
1120 6
                assert(oh->refcnt > 1);
1121 6
                hsh_rush1(wrk, oh, &rush, max);
1122 6
        }
1123
1124 1485
        if (oh->refcnt == 1)
1125 178
                assert(VTAILQ_EMPTY(&oh->waitinglist));
1126
1127 1485
        assert(oh->refcnt > 0);
1128 1485
        r = hash->deref(wrk, oh); /* Unlocks oh->mtx */
1129 1485
        hsh_rush2(wrk, &rush);
1130 1485
        return (r);
1131 2895
}
1132
1133
static int
1134 1858
hsh_deref_objhead(struct worker *wrk, struct objhead **poh)
1135
{
1136
        struct objhead *oh;
1137
1138 1858
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
1139 1858
        TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC);
1140
1141 1858
        Lck_Lock(&oh->mtx);
1142 1858
        return (hsh_deref_objhead_unlock(wrk, &oh, 0));
1143
}
1144
1145
void
1146 935
HSH_Init(const struct hash_slinger *slinger)
1147
{
1148
1149 935
        assert(DIGEST_LEN == VSHA256_LEN);      /* avoid #include pollution */
1150 935
        hash = slinger;
1151 935
        if (hash->start != NULL)
1152 935
                hash->start();
1153 935
        private_oh = hsh_newobjhead();
1154 935
        private_oh->refcnt = 1;
1155 935
}