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