| | 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 |
7426 |
hsh_newobjhead(void) |
97 |
|
{ |
98 |
|
struct objhead *oh; |
99 |
|
|
100 |
7426 |
ALLOC_OBJ(oh, OBJHEAD_MAGIC); |
101 |
7426 |
XXXAN(oh); |
102 |
7426 |
oh->refcnt = 1; |
103 |
7426 |
VTAILQ_INIT(&oh->objcs); |
104 |
7426 |
VTAILQ_INIT(&oh->waitinglist); |
105 |
7426 |
Lck_New(&oh->mtx, lck_objhdr); |
106 |
7426 |
return (oh); |
107 |
|
} |
108 |
|
|
109 |
|
/*---------------------------------------------------------------------*/ |
110 |
|
/* Precreate an objhead and object for later use */ |
111 |
|
static void |
112 |
7761 |
hsh_prealloc(struct worker *wrk) |
113 |
|
{ |
114 |
|
|
115 |
7761 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
116 |
|
|
117 |
7761 |
if (wrk->wpriv->nobjcore == NULL) |
118 |
5308 |
wrk->wpriv->nobjcore = ObjNew(wrk); |
119 |
7761 |
CHECK_OBJ_NOTNULL(wrk->wpriv->nobjcore, OBJCORE_MAGIC); |
120 |
|
|
121 |
7761 |
if (wrk->wpriv->nobjhead == NULL) { |
122 |
4640 |
wrk->wpriv->nobjhead = hsh_newobjhead(); |
123 |
4640 |
wrk->stats->n_objecthead++; |
124 |
4640 |
} |
125 |
7761 |
CHECK_OBJ_NOTNULL(wrk->wpriv->nobjhead, OBJHEAD_MAGIC); |
126 |
|
|
127 |
7761 |
if (hash->prep != NULL) |
128 |
7743 |
hash->prep(wrk); |
129 |
7761 |
} |
130 |
|
|
131 |
|
/*---------------------------------------------------------------------*/ |
132 |
|
|
133 |
|
struct objcore * |
134 |
4188 |
HSH_Private(const struct worker *wrk) |
135 |
|
{ |
136 |
|
struct objcore *oc; |
137 |
|
|
138 |
4188 |
CHECK_OBJ_NOTNULL(private_oh, OBJHEAD_MAGIC); |
139 |
|
|
140 |
4188 |
oc = ObjNew(wrk); |
141 |
4188 |
AN(oc); |
142 |
4188 |
oc->refcnt = 1; |
143 |
4188 |
oc->objhead = private_oh; |
144 |
4188 |
oc->flags |= OC_F_PRIVATE; |
145 |
4188 |
Lck_Lock(&private_oh->mtx); |
146 |
4188 |
VTAILQ_INSERT_TAIL(&private_oh->objcs, oc, hsh_list); |
147 |
4188 |
private_oh->refcnt++; |
148 |
4188 |
Lck_Unlock(&private_oh->mtx); |
149 |
4188 |
return (oc); |
150 |
|
} |
151 |
|
|
152 |
|
/*---------------------------------------------------------------------*/ |
153 |
|
|
154 |
|
void |
155 |
2821 |
HSH_Cleanup(const struct worker *wrk) |
156 |
|
{ |
157 |
|
|
158 |
2821 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
159 |
2821 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
160 |
2821 |
if (wrk->wpriv->nobjcore != NULL) |
161 |
5 |
ObjDestroy(wrk, &wrk->wpriv->nobjcore); |
162 |
|
|
163 |
2821 |
if (wrk->wpriv->nobjhead != NULL) { |
164 |
5 |
CHECK_OBJ(wrk->wpriv->nobjhead, OBJHEAD_MAGIC); |
165 |
5 |
Lck_Delete(&wrk->wpriv->nobjhead->mtx); |
166 |
5 |
FREE_OBJ(wrk->wpriv->nobjhead); |
167 |
5 |
wrk->stats->n_objecthead--; |
168 |
5 |
} |
169 |
2821 |
if (wrk->wpriv->nhashpriv != NULL) { |
170 |
|
/* XXX: If needed, add slinger method for this */ |
171 |
6 |
free(wrk->wpriv->nhashpriv); |
172 |
6 |
wrk->wpriv->nhashpriv = NULL; |
173 |
6 |
} |
174 |
2821 |
} |
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 |
43501 |
HSH_AddString(struct req *req, void *ctx, const char *str) |
192 |
|
{ |
193 |
|
|
194 |
43501 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
195 |
43501 |
AN(ctx); |
196 |
43501 |
if (str != NULL) { |
197 |
21752 |
VSHA256_Update(ctx, str, strlen(str)); |
198 |
21752 |
VSLbs(req->vsl, SLT_Hash, TOSTRAND(str)); |
199 |
21752 |
} else |
200 |
21749 |
VSHA256_Update(ctx, &str, 1); |
201 |
43501 |
} |
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 |
54 |
hsh_testmagic(void *result) |
256 |
|
{ |
257 |
|
size_t i, j; |
258 |
|
static size_t nused = 0; |
259 |
|
|
260 |
270 |
for (i = 0; i < nused; i++) |
261 |
243 |
if (!memcmp(hsh_magiclist[i].was, result, VSHA256_LEN)) |
262 |
27 |
break; |
263 |
54 |
if (i == nused && i < HSH_NMAGIC) |
264 |
27 |
memcpy(hsh_magiclist[nused++].was, result, VSHA256_LEN); |
265 |
54 |
if (i == nused) |
266 |
0 |
return; |
267 |
54 |
assert(i < HSH_NMAGIC); |
268 |
54 |
fprintf(stderr, "HASHMAGIC: <"); |
269 |
1782 |
for (j = 0; j < VSHA256_LEN; j++) |
270 |
1728 |
fprintf(stderr, "%02x", ((unsigned char*)result)[j]); |
271 |
54 |
fprintf(stderr, "> -> <"); |
272 |
54 |
memcpy(result, hsh_magiclist[i].now, VSHA256_LEN); |
273 |
1782 |
for (j = 0; j < VSHA256_LEN; j++) |
274 |
1728 |
fprintf(stderr, "%02x", ((unsigned char*)result)[j]); |
275 |
54 |
fprintf(stderr, ">\n"); |
276 |
54 |
} |
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 |
51 |
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 |
51 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
292 |
51 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
293 |
51 |
AN(digest); |
294 |
51 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
295 |
51 |
AN(ban); |
296 |
51 |
AN(oc->flags & OC_F_BUSY); |
297 |
51 |
AZ(oc->flags & OC_F_PRIVATE); |
298 |
51 |
assert(oc->refcnt == 1); |
299 |
51 |
INIT_OBJ(&rush, RUSH_MAGIC); |
300 |
|
|
301 |
51 |
hsh_prealloc(wrk); |
302 |
|
|
303 |
51 |
AN(wrk->wpriv->nobjhead); |
304 |
51 |
oh = hash->lookup(wrk, digest, &wrk->wpriv->nobjhead); |
305 |
51 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
306 |
51 |
Lck_AssertHeld(&oh->mtx); |
307 |
51 |
assert(oh->refcnt > 0); |
308 |
|
|
309 |
|
/* Mark object busy and insert (precreated) objcore in |
310 |
|
objecthead. The new object inherits our objhead reference. */ |
311 |
51 |
oc->objhead = oh; |
312 |
51 |
VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list); |
313 |
51 |
EXP_RefNewObjcore(oc); |
314 |
51 |
Lck_Unlock(&oh->mtx); |
315 |
|
|
316 |
51 |
BAN_RefBan(oc, ban); |
317 |
51 |
AN(oc->ban); |
318 |
|
|
319 |
|
/* Move the object first in the oh list, unbusy it and run the |
320 |
|
waitinglist if necessary */ |
321 |
51 |
Lck_Lock(&oh->mtx); |
322 |
51 |
VTAILQ_REMOVE(&oh->objcs, oc, hsh_list); |
323 |
51 |
VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list); |
324 |
51 |
oc->flags &= ~OC_F_BUSY; |
325 |
51 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) |
326 |
0 |
hsh_rush1(wrk, oh, &rush, HSH_RUSH_POLICY); |
327 |
51 |
Lck_Unlock(&oh->mtx); |
328 |
51 |
hsh_rush2(wrk, &rush); |
329 |
|
|
330 |
51 |
EXP_Insert(wrk, oc); |
331 |
51 |
} |
332 |
|
|
333 |
|
/*--------------------------------------------------------------------- |
334 |
|
*/ |
335 |
|
|
336 |
|
static struct objcore * |
337 |
4472 |
hsh_insert_busyobj(const struct worker *wrk, struct objhead *oh) |
338 |
|
{ |
339 |
|
struct objcore *oc; |
340 |
|
|
341 |
4472 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
342 |
4472 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
343 |
4472 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
344 |
4472 |
Lck_AssertHeld(&oh->mtx); |
345 |
|
|
346 |
4472 |
oc = wrk->wpriv->nobjcore; |
347 |
4472 |
wrk->wpriv->nobjcore = NULL; |
348 |
4472 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
349 |
|
|
350 |
4472 |
AN(oc->flags & OC_F_BUSY); |
351 |
4472 |
oc->refcnt = 1; /* Owned by busyobj */ |
352 |
4472 |
oc->objhead = oh; |
353 |
4472 |
VTAILQ_INSERT_TAIL(&oh->objcs, oc, hsh_list); |
354 |
4472 |
return (oc); |
355 |
|
} |
356 |
|
|
357 |
|
/*--------------------------------------------------------------------- |
358 |
|
*/ |
359 |
|
|
360 |
|
enum lookup_e |
361 |
7710 |
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 |
7710 |
unsigned xid = 0; |
373 |
|
unsigned ban_checks; |
374 |
|
unsigned ban_any_variant; |
375 |
7710 |
float dttl = 0.0; |
376 |
|
|
377 |
7710 |
AN(ocp); |
378 |
7710 |
*ocp = NULL; |
379 |
7710 |
AN(bocp); |
380 |
7710 |
*bocp = NULL; |
381 |
|
|
382 |
7710 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
383 |
7710 |
wrk = req->wrk; |
384 |
7710 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
385 |
7710 |
CHECK_OBJ_NOTNULL(wrk->wpriv, WORKER_PRIV_MAGIC); |
386 |
7710 |
CHECK_OBJ_NOTNULL(req->http, HTTP_MAGIC); |
387 |
7710 |
CHECK_OBJ_ORNULL(req->vcf, VCF_MAGIC); |
388 |
7710 |
AN(hash); |
389 |
|
|
390 |
7710 |
hsh_prealloc(wrk); |
391 |
7710 |
if (DO_DEBUG(DBG_HASHEDGE)) |
392 |
54 |
hsh_testmagic(req->digest); |
393 |
|
|
394 |
7710 |
if (req->hash_objhead != NULL) { |
395 |
|
/* |
396 |
|
* This req came off the waiting list, and brings an |
397 |
|
* oh refcnt with it. |
398 |
|
*/ |
399 |
145 |
CHECK_OBJ_NOTNULL(req->hash_objhead, OBJHEAD_MAGIC); |
400 |
145 |
oh = req->hash_objhead; |
401 |
145 |
Lck_Lock(&oh->mtx); |
402 |
145 |
req->hash_objhead = NULL; |
403 |
145 |
} else { |
404 |
7565 |
AN(wrk->wpriv->nobjhead); |
405 |
7565 |
oh = hash->lookup(wrk, req->digest, &wrk->wpriv->nobjhead); |
406 |
|
} |
407 |
|
|
408 |
7710 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
409 |
7710 |
Lck_AssertHeld(&oh->mtx); |
410 |
|
|
411 |
7710 |
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 |
42 |
*bocp = hsh_insert_busyobj(wrk, oh); |
415 |
|
/* NB: no deref of objhead, new object inherits reference */ |
416 |
42 |
Lck_Unlock(&oh->mtx); |
417 |
42 |
return (HSH_MISS); |
418 |
|
} |
419 |
|
|
420 |
7668 |
assert(oh->refcnt > 0); |
421 |
7668 |
busy_found = 0; |
422 |
7668 |
exp_oc = NULL; |
423 |
7668 |
exp_t_origin = 0.0; |
424 |
7668 |
ban_checks = 0; |
425 |
7668 |
ban_any_variant = cache_param->ban_any_variant; |
426 |
16374 |
VTAILQ_FOREACH(oc, &oh->objcs, hsh_list) { |
427 |
|
/* Must be at least our own ref + the objcore we examine */ |
428 |
11896 |
assert(oh->refcnt > 1); |
429 |
11896 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
430 |
11896 |
assert(oc->objhead == oh); |
431 |
11896 |
assert(oc->refcnt > 0); |
432 |
|
|
433 |
11896 |
if (oc->flags & OC_F_DYING) |
434 |
0 |
continue; |
435 |
11896 |
if (oc->flags & OC_F_FAILED) |
436 |
0 |
continue; |
437 |
|
|
438 |
11896 |
CHECK_OBJ_ORNULL(oc->boc, BOC_MAGIC); |
439 |
11896 |
if (oc->flags & OC_F_BUSY) { |
440 |
162 |
if (req->hash_ignore_busy) |
441 |
3 |
continue; |
442 |
|
|
443 |
169 |
if (oc->boc && oc->boc->vary != NULL && |
444 |
10 |
!req->hash_ignore_vary && |
445 |
10 |
!VRY_Match(req, oc->boc->vary)) { |
446 |
3 |
wrk->strangelove++; |
447 |
3 |
continue; |
448 |
|
} |
449 |
|
|
450 |
156 |
busy_found = 1; |
451 |
156 |
continue; |
452 |
|
} |
453 |
|
|
454 |
11734 |
if (oc->ttl <= 0.) |
455 |
138 |
continue; |
456 |
|
|
457 |
11596 |
if (ban_checks++ < ban_any_variant |
458 |
11596 |
&& BAN_CheckObject(wrk, oc, req)) { |
459 |
96 |
oc->flags |= OC_F_DYING; |
460 |
96 |
EXP_Remove(oc, NULL); |
461 |
96 |
continue; |
462 |
|
} |
463 |
|
|
464 |
11500 |
if (!req->hash_ignore_vary && ObjHasAttr(wrk, oc, OA_VARY)) { |
465 |
8348 |
vary = ObjGetAttr(wrk, oc, OA_VARY, NULL); |
466 |
8348 |
AN(vary); |
467 |
8348 |
if (!VRY_Match(req, vary)) { |
468 |
7889 |
wrk->strangelove++; |
469 |
7889 |
continue; |
470 |
|
} |
471 |
459 |
} |
472 |
|
|
473 |
3611 |
if (ban_checks >= ban_any_variant |
474 |
3611 |
&& BAN_CheckObject(wrk, oc, req)) { |
475 |
6 |
oc->flags |= OC_F_DYING; |
476 |
6 |
EXP_Remove(oc, NULL); |
477 |
6 |
continue; |
478 |
|
} |
479 |
|
|
480 |
3605 |
if (req->vcf != NULL) { |
481 |
24 |
vr = req->vcf->func(req, &oc, &exp_oc, 0); |
482 |
24 |
if (vr == VCF_CONTINUE) |
483 |
12 |
continue; |
484 |
12 |
if (vr == VCF_MISS) { |
485 |
9 |
oc = NULL; |
486 |
9 |
break; |
487 |
|
} |
488 |
3 |
if (vr == VCF_HIT) |
489 |
3 |
break; |
490 |
0 |
assert(vr == VCF_DEFAULT); |
491 |
0 |
} |
492 |
|
|
493 |
3581 |
if (EXP_Ttl(req, oc) > req->t_req) { |
494 |
3178 |
assert(oh->refcnt > 1); |
495 |
3178 |
assert(oc->objhead == oh); |
496 |
3178 |
break; |
497 |
|
} |
498 |
|
|
499 |
403 |
if (EXP_Ttl(NULL, oc) <= req->t_req && /* ignore req.ttl */ |
500 |
393 |
oc->t_origin > exp_t_origin) { |
501 |
|
/* record the newest object */ |
502 |
390 |
exp_oc = oc; |
503 |
390 |
exp_t_origin = oc->t_origin; |
504 |
390 |
assert(oh->refcnt > 1); |
505 |
390 |
assert(exp_oc->objhead == oh); |
506 |
390 |
} |
507 |
403 |
} |
508 |
|
|
509 |
7668 |
if (req->vcf != NULL) |
510 |
18 |
(void)req->vcf->func(req, &oc, &exp_oc, 1); |
511 |
|
|
512 |
7668 |
if (oc != NULL && oc->flags & OC_F_HFP) { |
513 |
33 |
xid = VXID(ObjGetXID(wrk, oc)); |
514 |
33 |
dttl = EXP_Dttl(req, oc); |
515 |
33 |
AN(hsh_deref_objhead_unlock(wrk, &oh, HSH_RUSH_POLICY)); |
516 |
33 |
wrk->stats->cache_hitpass++; |
517 |
33 |
VSLb(req->vsl, SLT_HitPass, "%u %.6f", xid, dttl); |
518 |
33 |
return (HSH_HITPASS); |
519 |
|
} |
520 |
|
|
521 |
7635 |
if (oc != NULL) { |
522 |
3151 |
*ocp = oc; |
523 |
3151 |
oc->refcnt++; |
524 |
3151 |
if (oc->flags & OC_F_HFM) { |
525 |
99 |
xid = VXID(ObjGetXID(wrk, oc)); |
526 |
99 |
dttl = EXP_Dttl(req, oc); |
527 |
99 |
*bocp = hsh_insert_busyobj(wrk, oh); |
528 |
99 |
Lck_Unlock(&oh->mtx); |
529 |
99 |
wrk->stats->cache_hitmiss++; |
530 |
99 |
VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl); |
531 |
99 |
return (HSH_HITMISS); |
532 |
|
} |
533 |
3052 |
oc->hits++; |
534 |
3052 |
boc_progress = oc->boc == NULL ? -1 : oc->boc->fetched_so_far; |
535 |
3052 |
AN(hsh_deref_objhead_unlock(wrk, &oh, HSH_RUSH_POLICY)); |
536 |
3052 |
Req_LogHit(wrk, req, oc, boc_progress); |
537 |
3052 |
return (HSH_HIT); |
538 |
|
} |
539 |
|
|
540 |
4484 |
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 |
12 |
xid = VXID(ObjGetXID(wrk, exp_oc)); |
548 |
12 |
dttl = EXP_Dttl(req, exp_oc); |
549 |
12 |
*bocp = hsh_insert_busyobj(wrk, oh); |
550 |
12 |
Lck_Unlock(&oh->mtx); |
551 |
12 |
wrk->stats->cache_hitmiss++; |
552 |
12 |
VSLb(req->vsl, SLT_HitMiss, "%u %.6f", xid, dttl); |
553 |
12 |
return (HSH_HITMISS); |
554 |
|
} |
555 |
|
|
556 |
4472 |
if (exp_oc != NULL && exp_oc->boc != NULL) |
557 |
12 |
boc_progress = exp_oc->boc->fetched_so_far; |
558 |
|
else |
559 |
4460 |
boc_progress = -1; |
560 |
|
|
561 |
4472 |
if (!busy_found) { |
562 |
4319 |
*bocp = hsh_insert_busyobj(wrk, oh); |
563 |
|
|
564 |
4319 |
if (exp_oc != NULL) { |
565 |
360 |
exp_oc->refcnt++; |
566 |
360 |
*ocp = exp_oc; |
567 |
360 |
if (EXP_Ttl_grace(req, exp_oc) >= req->t_req) { |
568 |
276 |
exp_oc->hits++; |
569 |
276 |
Lck_Unlock(&oh->mtx); |
570 |
276 |
Req_LogHit(wrk, req, exp_oc, boc_progress); |
571 |
276 |
return (HSH_GRACE); |
572 |
|
} |
573 |
84 |
} |
574 |
4043 |
Lck_Unlock(&oh->mtx); |
575 |
4043 |
return (HSH_MISS); |
576 |
|
} |
577 |
|
|
578 |
153 |
AN(busy_found); |
579 |
153 |
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 |
9 |
exp_oc->refcnt++; |
582 |
9 |
*ocp = exp_oc; |
583 |
9 |
exp_oc->hits++; |
584 |
9 |
AN(hsh_deref_objhead_unlock(wrk, &oh, 0)); |
585 |
9 |
Req_LogHit(wrk, req, exp_oc, boc_progress); |
586 |
9 |
return (HSH_GRACE); |
587 |
|
} |
588 |
|
|
589 |
|
/* There are one or more busy objects, wait for them */ |
590 |
144 |
VTAILQ_INSERT_TAIL(&oh->waitinglist, req, w_list); |
591 |
|
|
592 |
144 |
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 |
144 |
req->hash_objhead = oh; |
600 |
144 |
req->wrk = NULL; |
601 |
144 |
req->waitinglist = 1; |
602 |
|
|
603 |
144 |
if (DO_DEBUG(DBG_WAITINGLIST)) |
604 |
42 |
VSLb(req->vsl, SLT_Debug, "on waiting list <%p>", oh); |
605 |
|
|
606 |
144 |
Lck_Unlock(&oh->mtx); |
607 |
|
|
608 |
144 |
wrk->stats->busy_sleep++; |
609 |
144 |
return (HSH_BUSY); |
610 |
7710 |
} |
611 |
|
|
612 |
|
/*--------------------------------------------------------------------- |
613 |
|
* Pick the req's we are going to rush from the waiting list |
614 |
|
*/ |
615 |
|
|
616 |
|
static void |
617 |
113 |
hsh_rush1(const struct worker *wrk, struct objhead *oh, struct rush *r, int max) |
618 |
|
{ |
619 |
|
int i; |
620 |
|
struct req *req; |
621 |
|
|
622 |
113 |
if (max == 0) |
623 |
5 |
return; |
624 |
108 |
if (max == HSH_RUSH_POLICY) |
625 |
108 |
max = cache_param->rush_exponent; |
626 |
108 |
assert(max > 0); |
627 |
|
|
628 |
108 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
629 |
108 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
630 |
108 |
CHECK_OBJ_NOTNULL(r, RUSH_MAGIC); |
631 |
108 |
VTAILQ_INIT(&r->reqs); |
632 |
108 |
Lck_AssertHeld(&oh->mtx); |
633 |
252 |
for (i = 0; i < max; i++) { |
634 |
228 |
req = VTAILQ_FIRST(&oh->waitinglist); |
635 |
228 |
if (req == NULL) |
636 |
84 |
break; |
637 |
144 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
638 |
144 |
wrk->stats->busy_wakeup++; |
639 |
144 |
AZ(req->wrk); |
640 |
144 |
VTAILQ_REMOVE(&oh->waitinglist, req, w_list); |
641 |
144 |
VTAILQ_INSERT_TAIL(&r->reqs, req, w_list); |
642 |
144 |
req->waitinglist = 0; |
643 |
144 |
} |
644 |
113 |
} |
645 |
|
|
646 |
|
/*--------------------------------------------------------------------- |
647 |
|
* Rush req's that came from waiting list. |
648 |
|
*/ |
649 |
|
|
650 |
|
static void |
651 |
31916 |
hsh_rush2(struct worker *wrk, struct rush *r) |
652 |
|
{ |
653 |
|
struct req *req; |
654 |
|
|
655 |
31916 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
656 |
31916 |
CHECK_OBJ_NOTNULL(r, RUSH_MAGIC); |
657 |
|
|
658 |
32060 |
while (!VTAILQ_EMPTY(&r->reqs)) { |
659 |
144 |
req = VTAILQ_FIRST(&r->reqs); |
660 |
144 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
661 |
144 |
VTAILQ_REMOVE(&r->reqs, req, w_list); |
662 |
144 |
DSL(DBG_WAITINGLIST, req->vsl->wid, "off waiting list"); |
663 |
144 |
if (req->transport->reembark != NULL) { |
664 |
|
// For ESI includes |
665 |
4 |
req->transport->reembark(wrk, req); |
666 |
4 |
} 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 |
140 |
AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH)); |
674 |
|
} |
675 |
|
} |
676 |
31916 |
} |
677 |
|
|
678 |
|
/*--------------------------------------------------------------------- |
679 |
|
* Purge an entire objhead |
680 |
|
*/ |
681 |
|
|
682 |
|
unsigned |
683 |
63 |
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 |
63 |
unsigned i, j, n, n_max, total = 0; |
688 |
|
int is_purge; |
689 |
|
|
690 |
63 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
691 |
63 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
692 |
|
|
693 |
63 |
is_purge = (ttl == 0 && grace == 0 && keep == 0); |
694 |
63 |
n_max = WS_ReserveLumps(wrk->aws, sizeof *ocp); |
695 |
63 |
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 |
63 |
ocp = WS_Reservation(wrk->aws); |
703 |
63 |
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 |
63 |
Lck_Lock(&oh->mtx); |
715 |
63 |
oc = VTAILQ_FIRST(&oh->objcs); |
716 |
63 |
n = 0; |
717 |
66 |
while (1) { |
718 |
390 |
for (; n < n_max && oc != NULL; oc = VTAILQ_NEXT(oc, hsh_list)) |
719 |
|
{ |
720 |
324 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
721 |
324 |
assert(oc->objhead == oh); |
722 |
324 |
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 |
51 |
continue; |
729 |
|
} |
730 |
273 |
if (oc->flags & OC_F_DYING) |
731 |
0 |
continue; |
732 |
273 |
if (is_purge) |
733 |
243 |
oc->flags |= OC_F_DYING; |
734 |
273 |
oc->refcnt++; |
735 |
273 |
ocp[n++] = oc; |
736 |
273 |
} |
737 |
|
|
738 |
66 |
Lck_Unlock(&oh->mtx); |
739 |
|
|
740 |
66 |
if (n == 0) { |
741 |
|
/* No eligible objcores found. We are finished. */ |
742 |
12 |
break; |
743 |
|
} |
744 |
|
|
745 |
54 |
j = n; |
746 |
54 |
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 |
3 |
j--; |
754 |
3 |
assert(j >= 1); /* True because n_max >= 2 */ |
755 |
3 |
} |
756 |
327 |
for (i = 0; i < j; i++) { |
757 |
273 |
CHECK_OBJ_NOTNULL(ocp[i], OBJCORE_MAGIC); |
758 |
273 |
if (is_purge) |
759 |
243 |
EXP_Remove(ocp[i], NULL); |
760 |
|
else |
761 |
30 |
EXP_Reduce(ocp[i], ttl_now, ttl, grace, keep); |
762 |
273 |
(void)HSH_DerefObjCore(wrk, &ocp[i], 0); |
763 |
273 |
AZ(ocp[i]); |
764 |
273 |
total++; |
765 |
273 |
} |
766 |
|
|
767 |
54 |
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 |
51 |
break; |
772 |
|
} |
773 |
|
|
774 |
3 |
Lck_Lock(&oh->mtx); |
775 |
|
|
776 |
|
/* Move the bookmark first and continue scanning the |
777 |
|
* objcores */ |
778 |
3 |
CHECK_OBJ_NOTNULL(ocp[j], OBJCORE_MAGIC); |
779 |
3 |
ocp[0] = ocp[j]; |
780 |
3 |
n = 1; |
781 |
3 |
oc = VTAILQ_NEXT(ocp[0], hsh_list); |
782 |
3 |
CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC); |
783 |
|
} |
784 |
|
|
785 |
63 |
WS_Release(wrk->aws, 0); |
786 |
63 |
if (is_purge) |
787 |
33 |
Pool_PurgeStat(total); |
788 |
63 |
return (total); |
789 |
|
} |
790 |
|
|
791 |
|
/*--------------------------------------------------------------------- |
792 |
|
* Fail an objcore |
793 |
|
*/ |
794 |
|
|
795 |
|
void |
796 |
195 |
HSH_Fail(struct objcore *oc) |
797 |
|
{ |
798 |
|
struct objhead *oh; |
799 |
|
|
800 |
195 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
801 |
195 |
oh = oc->objhead; |
802 |
195 |
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 |
195 |
assert((oc->flags & OC_F_BUSY) || (oc->stobj->stevedore != NULL)); |
810 |
|
|
811 |
195 |
Lck_Lock(&oh->mtx); |
812 |
195 |
oc->flags |= OC_F_FAILED; |
813 |
195 |
Lck_Unlock(&oh->mtx); |
814 |
195 |
} |
815 |
|
|
816 |
|
/*--------------------------------------------------------------------- |
817 |
|
* Mark a fetch we will not need as cancelled |
818 |
|
*/ |
819 |
|
|
820 |
|
static void |
821 |
1047 |
hsh_cancel(struct objcore *oc) |
822 |
|
{ |
823 |
|
struct objhead *oh; |
824 |
|
|
825 |
1047 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
826 |
1047 |
oh = oc->objhead; |
827 |
1047 |
CHECK_OBJ(oh, OBJHEAD_MAGIC); |
828 |
|
|
829 |
1047 |
Lck_Lock(&oh->mtx); |
830 |
1047 |
oc->flags |= OC_F_CANCEL; |
831 |
1047 |
Lck_Unlock(&oh->mtx); |
832 |
1047 |
} |
833 |
|
|
834 |
|
/*--------------------------------------------------------------------- |
835 |
|
* Cancel a fetch when the client does not need it any more |
836 |
|
*/ |
837 |
|
|
838 |
|
void |
839 |
11140 |
HSH_Cancel(struct worker *wrk, struct objcore *oc, struct boc *boc) |
840 |
|
{ |
841 |
11140 |
struct boc *bocref = NULL; |
842 |
|
|
843 |
11140 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
844 |
|
|
845 |
11140 |
if ((oc->flags & OC_F_TRANSIENT) == 0) |
846 |
6985 |
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 |
4155 |
if (boc == NULL) |
853 |
3116 |
bocref = boc = HSH_RefBoc(oc); |
854 |
|
|
855 |
4155 |
CHECK_OBJ_ORNULL(boc, BOC_MAGIC); |
856 |
|
|
857 |
4155 |
if (oc->flags & OC_F_HFP) |
858 |
65 |
AN(oc->flags & OC_F_HFM); |
859 |
|
|
860 |
4155 |
if (boc != NULL) { |
861 |
1047 |
hsh_cancel(oc); |
862 |
1047 |
ObjWaitState(oc, BOS_FINISHED); |
863 |
1047 |
} |
864 |
|
|
865 |
4155 |
if (bocref != NULL) |
866 |
9 |
HSH_DerefBoc(wrk, oc); |
867 |
|
|
868 |
4155 |
ObjSlim(wrk, oc); |
869 |
11140 |
} |
870 |
|
|
871 |
|
/*--------------------------------------------------------------------- |
872 |
|
* Unbusy an objcore when the object is completely fetched. |
873 |
|
*/ |
874 |
|
|
875 |
|
void |
876 |
6552 |
HSH_Unbusy(struct worker *wrk, struct objcore *oc) |
877 |
|
{ |
878 |
|
struct objhead *oh; |
879 |
|
struct rush rush; |
880 |
|
|
881 |
6552 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
882 |
6552 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
883 |
6552 |
CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC); |
884 |
|
|
885 |
6552 |
oh = oc->objhead; |
886 |
6552 |
CHECK_OBJ(oh, OBJHEAD_MAGIC); |
887 |
6552 |
INIT_OBJ(&rush, RUSH_MAGIC); |
888 |
|
|
889 |
6552 |
AN(oc->stobj->stevedore); |
890 |
6552 |
AN(oc->flags & OC_F_BUSY); |
891 |
6552 |
assert(oh->refcnt > 0); |
892 |
6552 |
assert(oc->refcnt > 0); |
893 |
|
|
894 |
6552 |
if (!(oc->flags & OC_F_PRIVATE)) { |
895 |
4236 |
BAN_NewObjCore(oc); |
896 |
4236 |
AN(oc->ban); |
897 |
4236 |
} |
898 |
|
|
899 |
|
/* XXX: pretouch neighbors on oh->objcs to prevent page-on under mtx */ |
900 |
6552 |
Lck_Lock(&oh->mtx); |
901 |
6552 |
assert(oh->refcnt > 0); |
902 |
6552 |
assert(oc->refcnt > 0); |
903 |
6552 |
if (!(oc->flags & OC_F_PRIVATE)) |
904 |
4236 |
EXP_RefNewObjcore(oc); /* Takes a ref for expiry */ |
905 |
|
/* XXX: strictly speaking, we should sort in Date: order. */ |
906 |
6552 |
VTAILQ_REMOVE(&oh->objcs, oc, hsh_list); |
907 |
6552 |
VTAILQ_INSERT_HEAD(&oh->objcs, oc, hsh_list); |
908 |
6552 |
oc->flags &= ~OC_F_BUSY; |
909 |
6552 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) { |
910 |
85 |
assert(oh->refcnt > 1); |
911 |
85 |
hsh_rush1(wrk, oh, &rush, HSH_RUSH_POLICY); |
912 |
85 |
} |
913 |
6552 |
Lck_Unlock(&oh->mtx); |
914 |
6552 |
EXP_Insert(wrk, oc); /* Does nothing unless EXP_RefNewObjcore was |
915 |
|
* called */ |
916 |
6552 |
hsh_rush2(wrk, &rush); |
917 |
6552 |
} |
918 |
|
|
919 |
|
/*==================================================================== |
920 |
|
* HSH_Kill() |
921 |
|
* |
922 |
|
* It's dead Jim, kick it... |
923 |
|
*/ |
924 |
|
|
925 |
|
void |
926 |
521 |
HSH_Kill(struct objcore *oc) |
927 |
|
{ |
928 |
|
|
929 |
521 |
HSH_Replace(oc, NULL); |
930 |
521 |
} |
931 |
|
|
932 |
|
void |
933 |
737 |
HSH_Replace(struct objcore *oc, const struct objcore *new_oc) |
934 |
|
{ |
935 |
|
|
936 |
737 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
937 |
737 |
CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC); |
938 |
737 |
if (new_oc != NULL) { |
939 |
216 |
CHECK_OBJ(new_oc, OBJCORE_MAGIC); |
940 |
216 |
assert(oc->objhead == new_oc->objhead); |
941 |
216 |
} |
942 |
|
|
943 |
737 |
Lck_Lock(&oc->objhead->mtx); |
944 |
737 |
oc->flags |= OC_F_DYING; |
945 |
737 |
Lck_Unlock(&oc->objhead->mtx); |
946 |
737 |
EXP_Remove(oc, new_oc); |
947 |
737 |
} |
948 |
|
|
949 |
|
/*==================================================================== |
950 |
|
* HSH_Snipe() |
951 |
|
* |
952 |
|
* If objcore is idle, gain a ref and mark it dead. |
953 |
|
*/ |
954 |
|
|
955 |
|
int |
956 |
33 |
HSH_Snipe(const struct worker *wrk, struct objcore *oc) |
957 |
|
{ |
958 |
33 |
int retval = 0; |
959 |
|
|
960 |
33 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
961 |
33 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
962 |
33 |
CHECK_OBJ_NOTNULL(oc->objhead, OBJHEAD_MAGIC); |
963 |
|
|
964 |
33 |
if (oc->refcnt == 1 && !Lck_Trylock(&oc->objhead->mtx)) { |
965 |
33 |
if (oc->refcnt == 1 && !(oc->flags & OC_F_DYING)) { |
966 |
33 |
oc->flags |= OC_F_DYING; |
967 |
33 |
oc->refcnt++; |
968 |
33 |
retval = 1; |
969 |
33 |
} |
970 |
33 |
Lck_Unlock(&oc->objhead->mtx); |
971 |
33 |
} |
972 |
33 |
if (retval) |
973 |
33 |
EXP_Remove(oc, NULL); |
974 |
33 |
return (retval); |
975 |
|
} |
976 |
|
|
977 |
|
|
978 |
|
/*--------------------------------------------------------------------- |
979 |
|
* Gain a reference on an objcore |
980 |
|
*/ |
981 |
|
|
982 |
|
void |
983 |
7209 |
HSH_Ref(struct objcore *oc) |
984 |
|
{ |
985 |
|
struct objhead *oh; |
986 |
|
|
987 |
7209 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
988 |
7209 |
oh = oc->objhead; |
989 |
7209 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
990 |
7209 |
Lck_Lock(&oh->mtx); |
991 |
7209 |
assert(oc->refcnt > 0); |
992 |
7209 |
oc->refcnt++; |
993 |
7209 |
Lck_Unlock(&oh->mtx); |
994 |
7209 |
} |
995 |
|
|
996 |
|
/*--------------------------------------------------------------------- |
997 |
|
* Gain a reference on the busyobj, if the objcore has one |
998 |
|
*/ |
999 |
|
|
1000 |
|
struct boc * |
1001 |
28008 |
HSH_RefBoc(const struct objcore *oc) |
1002 |
|
{ |
1003 |
|
struct objhead *oh; |
1004 |
|
struct boc *boc; |
1005 |
|
|
1006 |
28008 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
1007 |
28008 |
oh = oc->objhead; |
1008 |
28008 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
1009 |
28008 |
if (oc->boc == NULL) |
1010 |
16373 |
return (NULL); |
1011 |
11635 |
Lck_Lock(&oh->mtx); |
1012 |
11635 |
assert(oc->refcnt > 0); |
1013 |
11635 |
boc = oc->boc; |
1014 |
11635 |
CHECK_OBJ_ORNULL(boc, BOC_MAGIC); |
1015 |
11635 |
if (boc != NULL) { |
1016 |
11632 |
assert(boc->refcount > 0); |
1017 |
11632 |
if (boc->state < BOS_FINISHED) |
1018 |
11543 |
boc->refcount++; |
1019 |
|
else |
1020 |
89 |
boc = NULL; |
1021 |
11632 |
} |
1022 |
11635 |
Lck_Unlock(&oh->mtx); |
1023 |
11635 |
return (boc); |
1024 |
28008 |
} |
1025 |
|
|
1026 |
|
void |
1027 |
20150 |
HSH_DerefBoc(struct worker *wrk, struct objcore *oc) |
1028 |
|
{ |
1029 |
|
struct boc *boc; |
1030 |
|
unsigned r; |
1031 |
|
|
1032 |
20150 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1033 |
20150 |
CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC); |
1034 |
20150 |
boc = oc->boc; |
1035 |
20150 |
CHECK_OBJ_NOTNULL(boc, BOC_MAGIC); |
1036 |
20150 |
Lck_Lock(&oc->objhead->mtx); |
1037 |
20150 |
assert(oc->refcnt > 0); |
1038 |
20150 |
assert(boc->refcount > 0); |
1039 |
20150 |
r = --boc->refcount; |
1040 |
20150 |
if (r == 0) |
1041 |
8610 |
oc->boc = NULL; |
1042 |
20150 |
Lck_Unlock(&oc->objhead->mtx); |
1043 |
20150 |
if (r == 0) |
1044 |
8610 |
ObjBocDone(wrk, oc, &boc); |
1045 |
20150 |
} |
1046 |
|
|
1047 |
|
/*-------------------------------------------------------------------- |
1048 |
|
* Dereference objcore |
1049 |
|
* |
1050 |
|
* Returns zero if target was destroyed. |
1051 |
|
*/ |
1052 |
|
|
1053 |
|
int |
1054 |
20890 |
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 |
20890 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1062 |
20890 |
TAKE_OBJ_NOTNULL(oc, ocp, OBJCORE_MAGIC); |
1063 |
20890 |
assert(oc->refcnt > 0); |
1064 |
20890 |
INIT_OBJ(&rush, RUSH_MAGIC); |
1065 |
|
|
1066 |
20890 |
oh = oc->objhead; |
1067 |
20890 |
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC); |
1068 |
|
|
1069 |
20890 |
Lck_Lock(&oh->mtx); |
1070 |
20890 |
assert(oh->refcnt > 0); |
1071 |
20890 |
r = --oc->refcnt; |
1072 |
20890 |
if (!r) |
1073 |
5520 |
VTAILQ_REMOVE(&oh->objcs, oc, hsh_list); |
1074 |
20890 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) { |
1075 |
11 |
assert(oh->refcnt > 1); |
1076 |
11 |
hsh_rush1(wrk, oh, &rush, rushmax); |
1077 |
11 |
} |
1078 |
20890 |
Lck_Unlock(&oh->mtx); |
1079 |
20890 |
hsh_rush2(wrk, &rush); |
1080 |
20890 |
if (r != 0) |
1081 |
15370 |
return (r); |
1082 |
|
|
1083 |
5520 |
AZ(oc->exp_flags); |
1084 |
|
|
1085 |
5520 |
BAN_DestroyObj(oc); |
1086 |
5520 |
AZ(oc->ban); |
1087 |
|
|
1088 |
5520 |
if (oc->stobj->stevedore != NULL) |
1089 |
5263 |
ObjFreeObj(wrk, oc); |
1090 |
5520 |
ObjDestroy(wrk, &oc); |
1091 |
|
|
1092 |
|
/* Drop our ref on the objhead */ |
1093 |
5520 |
assert(oh->refcnt > 0); |
1094 |
5520 |
(void)hsh_deref_objhead(wrk, &oh); |
1095 |
5520 |
return (0); |
1096 |
20890 |
} |
1097 |
|
|
1098 |
|
static int |
1099 |
8614 |
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 |
8614 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1106 |
8614 |
TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC); |
1107 |
|
|
1108 |
8614 |
Lck_AssertHeld(&oh->mtx); |
1109 |
|
|
1110 |
8614 |
if (oh == private_oh) { |
1111 |
4188 |
assert(VTAILQ_EMPTY(&oh->waitinglist)); |
1112 |
4188 |
assert(oh->refcnt > 1); |
1113 |
4188 |
oh->refcnt--; |
1114 |
4188 |
Lck_Unlock(&oh->mtx); |
1115 |
4188 |
return (1); |
1116 |
|
} |
1117 |
|
|
1118 |
4426 |
INIT_OBJ(&rush, RUSH_MAGIC); |
1119 |
4426 |
if (!VTAILQ_EMPTY(&oh->waitinglist)) { |
1120 |
17 |
assert(oh->refcnt > 1); |
1121 |
17 |
hsh_rush1(wrk, oh, &rush, max); |
1122 |
17 |
} |
1123 |
|
|
1124 |
4426 |
if (oh->refcnt == 1) |
1125 |
527 |
assert(VTAILQ_EMPTY(&oh->waitinglist)); |
1126 |
|
|
1127 |
4426 |
assert(oh->refcnt > 0); |
1128 |
4426 |
r = hash->deref(wrk, oh); /* Unlocks oh->mtx */ |
1129 |
4426 |
hsh_rush2(wrk, &rush); |
1130 |
4426 |
return (r); |
1131 |
8614 |
} |
1132 |
|
|
1133 |
|
static int |
1134 |
5520 |
hsh_deref_objhead(struct worker *wrk, struct objhead **poh) |
1135 |
|
{ |
1136 |
|
struct objhead *oh; |
1137 |
|
|
1138 |
5520 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
1139 |
5520 |
TAKE_OBJ_NOTNULL(oh, poh, OBJHEAD_MAGIC); |
1140 |
|
|
1141 |
5520 |
Lck_Lock(&oh->mtx); |
1142 |
5520 |
return (hsh_deref_objhead_unlock(wrk, &oh, 0)); |
1143 |
|
} |
1144 |
|
|
1145 |
|
void |
1146 |
2786 |
HSH_Init(const struct hash_slinger *slinger) |
1147 |
|
{ |
1148 |
|
|
1149 |
2786 |
assert(DIGEST_LEN == VSHA256_LEN); /* avoid #include pollution */ |
1150 |
2786 |
hash = slinger; |
1151 |
2786 |
if (hash->start != NULL) |
1152 |
2786 |
hash->start(); |
1153 |
2786 |
private_oh = hsh_newobjhead(); |
1154 |
2786 |
private_oh->refcnt = 1; |
1155 |
2786 |
} |