| | varnish-cache/lib/libvarnishapi/vsm.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 |
|
* Author: Martin Blix Grydeland <martin@varnish-software.com> |
7 |
|
* |
8 |
|
* SPDX-License-Identifier: BSD-2-Clause |
9 |
|
* |
10 |
|
* Redistribution and use in source and binary forms, with or without |
11 |
|
* modification, are permitted provided that the following conditions |
12 |
|
* are met: |
13 |
|
* 1. Redistributions of source code must retain the above copyright |
14 |
|
* notice, this list of conditions and the following disclaimer. |
15 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
16 |
|
* notice, this list of conditions and the following disclaimer in the |
17 |
|
* documentation and/or other materials provided with the distribution. |
18 |
|
* |
19 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
20 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
23 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 |
|
* SUCH DAMAGE. |
30 |
|
*/ |
31 |
|
|
32 |
|
#include "config.h" |
33 |
|
|
34 |
|
#include <sys/mman.h> |
35 |
|
#include <sys/stat.h> |
36 |
|
|
37 |
|
#include <fcntl.h> |
38 |
|
#include <float.h> |
39 |
|
#include <math.h> |
40 |
|
#include <stdarg.h> |
41 |
|
#include <stdint.h> |
42 |
|
#include <stdio.h> |
43 |
|
#include <stdlib.h> |
44 |
|
#include <string.h> |
45 |
|
#include <unistd.h> |
46 |
|
|
47 |
|
#include "vdef.h" |
48 |
|
#include "vas.h" |
49 |
|
#include "miniobj.h" |
50 |
|
|
51 |
|
#include "vav.h" |
52 |
|
#include "vin.h" |
53 |
|
#include "vlu.h" |
54 |
|
#include "vsb.h" |
55 |
|
#include "vsm_priv.h" |
56 |
|
#include "vqueue.h" |
57 |
|
#include "vtim.h" |
58 |
|
|
59 |
|
#include "vapi/vsig.h" |
60 |
|
#include "vapi/vsm.h" |
61 |
|
|
62 |
|
#ifndef MAP_HASSEMAPHORE |
63 |
|
# define MAP_HASSEMAPHORE 0 /* XXX Linux */ |
64 |
|
#endif |
65 |
|
|
66 |
|
#ifndef MAP_NOSYNC |
67 |
|
# define MAP_NOSYNC 0 /* XXX Linux */ |
68 |
|
#endif |
69 |
|
|
70 |
|
const struct vsm_valid VSM_invalid[1] = {{"invalid"}}; |
71 |
|
const struct vsm_valid VSM_valid[1] = {{"valid"}}; |
72 |
|
|
73 |
|
static vlu_f vsm_vlu_func; |
74 |
|
|
75 |
|
#define VSM_PRIV_SHIFT \ |
76 |
|
(sizeof (uint64_t) * 4) |
77 |
|
#define VSM_PRIV_MASK \ |
78 |
|
((1ULL << VSM_PRIV_SHIFT) - 1) |
79 |
|
#define VSM_PRIV_LOW(u) \ |
80 |
|
((uint64_t)(u) & VSM_PRIV_MASK) |
81 |
|
#define VSM_PRIV_HIGH(u) \ |
82 |
|
(((uint64_t)(u) >> VSM_PRIV_SHIFT) & VSM_PRIV_MASK) |
83 |
|
#define VSM_PRIV_MERGE(low, high) \ |
84 |
|
(VSM_PRIV_LOW(low) | (VSM_PRIV_LOW(high) << VSM_PRIV_SHIFT)) |
85 |
|
|
86 |
|
/*--------------------------------------------------------------------*/ |
87 |
|
|
88 |
|
struct vsm_set; |
89 |
|
|
90 |
|
struct vsm_seg { |
91 |
|
unsigned magic; |
92 |
|
#define VSM_SEG_MAGIC 0xeb6c6dfd |
93 |
|
unsigned flags; |
94 |
|
#define VSM_FLAG_MARKSCAN (1U<<1) |
95 |
|
#define VSM_FLAG_STALE (1U<<2) |
96 |
|
#define VSM_FLAG_CLUSTER (1U<<3) |
97 |
|
VTAILQ_ENTRY(vsm_seg) list; |
98 |
|
VTAILQ_ENTRY(vsm_seg) clist; |
99 |
|
struct vsm_set *set; |
100 |
|
struct vsm_seg *cluster; |
101 |
|
char **av; |
102 |
|
int refs; |
103 |
|
void *s; |
104 |
|
size_t sz; |
105 |
|
void *b; |
106 |
|
void *e; |
107 |
|
uint64_t serial; |
108 |
|
}; |
109 |
|
|
110 |
|
struct vsm_set { |
111 |
|
unsigned magic; |
112 |
|
#define VSM_SET_MAGIC 0xdee401b8 |
113 |
|
const char *dname; |
114 |
|
struct vsm *vsm; |
115 |
|
VTAILQ_HEAD(,vsm_seg) segs; |
116 |
|
VTAILQ_HEAD(,vsm_seg) stale; |
117 |
|
VTAILQ_HEAD(,vsm_seg) clusters; |
118 |
|
|
119 |
|
int dfd; |
120 |
|
struct stat dst; |
121 |
|
|
122 |
|
int fd; |
123 |
|
struct stat fst; |
124 |
|
|
125 |
|
uintmax_t id1, id2; |
126 |
|
|
127 |
|
// _.index reading state |
128 |
|
struct vlu *vlu; |
129 |
|
unsigned retval; |
130 |
|
struct vsm_seg *vg; |
131 |
|
|
132 |
|
unsigned flag_running; |
133 |
|
unsigned flag_changed; |
134 |
|
unsigned flag_restarted; |
135 |
|
|
136 |
|
int couldkill; |
137 |
|
}; |
138 |
|
|
139 |
|
struct vsm { |
140 |
|
unsigned magic; |
141 |
|
#define VSM_MAGIC 0x6e3bd69b |
142 |
|
|
143 |
|
struct vsb *diag; |
144 |
|
uint64_t serial; |
145 |
|
|
146 |
|
int wdfd; |
147 |
|
struct stat wdst; |
148 |
|
char *wdname; |
149 |
|
|
150 |
|
struct vsm_set *mgt; |
151 |
|
struct vsm_set *child; |
152 |
|
|
153 |
|
int attached; |
154 |
|
double patience; |
155 |
|
}; |
156 |
|
|
157 |
|
/*--------------------------------------------------------------------*/ |
158 |
|
|
159 |
|
static int |
160 |
141 |
vsm_diag(struct vsm *vd, const char *fmt, ...) |
161 |
|
{ |
162 |
|
va_list ap; |
163 |
|
|
164 |
141 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
165 |
141 |
AN(fmt); |
166 |
|
|
167 |
141 |
if (vd->diag == NULL) |
168 |
141 |
vd->diag = VSB_new_auto(); |
169 |
141 |
AN(vd->diag); |
170 |
141 |
VSB_clear(vd->diag); |
171 |
141 |
va_start(ap, fmt); |
172 |
141 |
VSB_vprintf(vd->diag, fmt, ap); |
173 |
141 |
va_end(ap); |
174 |
141 |
AZ(VSB_finish(vd->diag)); |
175 |
141 |
return (-1); |
176 |
|
} |
177 |
|
|
178 |
|
/*--------------------------------------------------------------------*/ |
179 |
|
|
180 |
|
static int |
181 |
19225 |
vsm_mapseg(struct vsm *vd, struct vsm_seg *vg) |
182 |
|
{ |
183 |
|
size_t of, off, sz, ps, len; |
184 |
|
struct vsb *vsb; |
185 |
|
void *s; |
186 |
|
int fd; |
187 |
|
|
188 |
19225 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
189 |
|
|
190 |
19225 |
if (vg->s != NULL) |
191 |
303 |
return (0); |
192 |
|
|
193 |
18922 |
ps = getpagesize(); |
194 |
|
|
195 |
18922 |
of = strtoul(vg->av[2], NULL, 10); |
196 |
18922 |
off = RDN2(of, ps); |
197 |
|
|
198 |
18922 |
if (vg->flags & VSM_FLAG_CLUSTER) |
199 |
128 |
assert(of == 0); |
200 |
18922 |
assert(vg->cluster == NULL); |
201 |
|
|
202 |
18922 |
sz = strtoul(vg->av[3], NULL, 10); |
203 |
18922 |
assert(sz > 0); |
204 |
18922 |
assert(of >= off); |
205 |
18922 |
len = RUP2((of - off) + sz, ps); |
206 |
|
|
207 |
18922 |
vsb = VSB_new_auto(); |
208 |
18922 |
AN(vsb); |
209 |
18922 |
VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]); |
210 |
18922 |
AZ(VSB_finish(vsb)); |
211 |
|
|
212 |
18922 |
fd = open(VSB_data(vsb), O_RDONLY); // XXX: openat |
213 |
18922 |
if (fd < 0) { |
214 |
6 |
VSB_destroy(&vsb); |
215 |
6 |
return (vsm_diag(vd, "Could not open segment")); |
216 |
|
} |
217 |
|
|
218 |
37832 |
s = (void*)mmap(NULL, len, |
219 |
|
PROT_READ, |
220 |
|
MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED, |
221 |
18916 |
fd, (off_t)off); |
222 |
|
|
223 |
18916 |
VSB_destroy(&vsb); |
224 |
|
|
225 |
18916 |
closefd(&fd); |
226 |
18916 |
if (s == MAP_FAILED) |
227 |
0 |
return (vsm_diag(vd, "Could not mmap segment")); |
228 |
|
|
229 |
18916 |
vg->s = s; |
230 |
18916 |
vg->b = (char*)(vg->s) + of - off; |
231 |
18916 |
vg->e = (char *)vg->b + sz; |
232 |
18916 |
vg->sz = len; |
233 |
|
|
234 |
18916 |
return (0); |
235 |
19225 |
} |
236 |
|
|
237 |
|
static void |
238 |
16900 |
vsm_unmapseg(struct vsm_seg *vg) |
239 |
|
{ |
240 |
|
|
241 |
16900 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
242 |
|
|
243 |
16900 |
AN(vg->b); |
244 |
16900 |
AN(vg->e); |
245 |
16900 |
AZ(munmap(vg->s, vg->sz)); |
246 |
16900 |
vg->s = vg->b = vg->e = NULL; |
247 |
16900 |
vg->sz = 0; |
248 |
16900 |
} |
249 |
|
|
250 |
|
/*--------------------------------------------------------------------*/ |
251 |
|
|
252 |
|
static void |
253 |
231430 |
vsm_delseg(struct vsm_seg *vg, int refsok) |
254 |
|
{ |
255 |
|
|
256 |
231430 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
257 |
|
|
258 |
231430 |
if (vg->set->vg == vg) { |
259 |
14454 |
AZ(vg->flags & VSM_FLAG_STALE); |
260 |
14454 |
vg->set->vg = VTAILQ_NEXT(vg, list); |
261 |
14454 |
} |
262 |
|
|
263 |
231430 |
if (refsok && vg->refs) { |
264 |
405 |
AZ(vg->flags & VSM_FLAG_STALE); |
265 |
405 |
vg->flags |= VSM_FLAG_STALE; |
266 |
405 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
267 |
405 |
VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list); |
268 |
405 |
return; |
269 |
|
} |
270 |
|
|
271 |
231025 |
if (vg->s != NULL) |
272 |
0 |
vsm_unmapseg(vg); |
273 |
|
|
274 |
231025 |
if (vg->flags & VSM_FLAG_CLUSTER) { |
275 |
5700 |
vg->flags &= ~VSM_FLAG_CLUSTER; |
276 |
5700 |
VTAILQ_REMOVE(&vg->set->clusters, vg, clist); |
277 |
5700 |
} |
278 |
|
|
279 |
231025 |
if (vg->flags & VSM_FLAG_STALE) |
280 |
405 |
VTAILQ_REMOVE(&vg->set->stale, vg, list); |
281 |
|
else |
282 |
230620 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
283 |
231025 |
VAV_Free(vg->av); |
284 |
231025 |
FREE_OBJ(vg); |
285 |
231430 |
} |
286 |
|
|
287 |
|
/*--------------------------------------------------------------------*/ |
288 |
|
|
289 |
|
static struct vsm_set * |
290 |
13392 |
vsm_newset(const char *dirname) |
291 |
|
{ |
292 |
|
struct vsm_set *vs; |
293 |
|
|
294 |
13392 |
ALLOC_OBJ(vs, VSM_SET_MAGIC); |
295 |
13392 |
AN(vs); |
296 |
13392 |
VTAILQ_INIT(&vs->segs); |
297 |
13392 |
VTAILQ_INIT(&vs->stale); |
298 |
13392 |
VTAILQ_INIT(&vs->clusters); |
299 |
13392 |
vs->dname = dirname; |
300 |
13392 |
vs->dfd = vs->fd = -1; |
301 |
13392 |
vs->vlu = VLU_New(vsm_vlu_func, vs, 0); |
302 |
13392 |
AN(vs->vlu); |
303 |
13392 |
if (getenv("VSM_NOPID") != NULL) |
304 |
0 |
vs->couldkill = -1; |
305 |
13392 |
return (vs); |
306 |
|
} |
307 |
|
|
308 |
|
static void |
309 |
13224 |
vsm_delset(struct vsm_set **p) |
310 |
|
{ |
311 |
|
struct vsm_set *vs; |
312 |
|
struct vsm_seg *vg; |
313 |
|
|
314 |
13224 |
TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC); |
315 |
|
|
316 |
13224 |
if (vs->fd >= 0) |
317 |
8109 |
closefd(&vs->fd); |
318 |
13224 |
if (vs->dfd >= 0) |
319 |
10884 |
closefd(&vs->dfd); |
320 |
13224 |
while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) { |
321 |
0 |
AN(vg->flags & VSM_FLAG_STALE); |
322 |
0 |
vsm_delseg(vg, 0); |
323 |
|
} |
324 |
230655 |
while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) { |
325 |
217431 |
AZ(vg->flags & VSM_FLAG_STALE); |
326 |
217431 |
vsm_delseg(vg, 0); |
327 |
|
} |
328 |
13224 |
assert(VTAILQ_EMPTY(&vs->clusters)); |
329 |
13224 |
VLU_Destroy(&vs->vlu); |
330 |
13224 |
FREE_OBJ(vs); |
331 |
13224 |
} |
332 |
|
|
333 |
|
static void |
334 |
29278 |
vsm_wash_set(const struct vsm_set *vs, int all) |
335 |
|
{ |
336 |
|
struct vsm_seg *vg, *vg2; |
337 |
|
|
338 |
249804 |
VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) { |
339 |
220526 |
if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0) |
340 |
7785 |
vsm_delseg(vg, 1); |
341 |
220526 |
} |
342 |
29278 |
} |
343 |
|
|
344 |
|
/*--------------------------------------------------------------------*/ |
345 |
|
|
346 |
|
struct vsm * |
347 |
6696 |
VSM_New(void) |
348 |
|
{ |
349 |
|
struct vsm *vd; |
350 |
|
|
351 |
6696 |
ALLOC_OBJ(vd, VSM_MAGIC); |
352 |
6696 |
AN(vd); |
353 |
|
|
354 |
6696 |
vd->mgt = vsm_newset(VSM_MGT_DIRNAME); |
355 |
6696 |
vd->mgt->flag_running = VSM_MGT_RUNNING; |
356 |
6696 |
vd->mgt->flag_changed = VSM_MGT_CHANGED; |
357 |
6696 |
vd->mgt->flag_restarted = VSM_MGT_RESTARTED; |
358 |
|
|
359 |
6696 |
vd->child = vsm_newset(VSM_CHILD_DIRNAME); |
360 |
6696 |
vd->child->flag_running = VSM_WRK_RUNNING; |
361 |
6696 |
vd->child->flag_changed = VSM_WRK_CHANGED; |
362 |
6696 |
vd->child->flag_restarted = VSM_WRK_RESTARTED; |
363 |
|
|
364 |
6696 |
vd->mgt->vsm = vd; |
365 |
6696 |
vd->child->vsm = vd; |
366 |
6696 |
vd->wdfd = -1; |
367 |
6696 |
vd->patience = 5; |
368 |
6696 |
return (vd); |
369 |
|
} |
370 |
|
|
371 |
|
/*--------------------------------------------------------------------*/ |
372 |
|
|
373 |
|
int |
374 |
6828 |
VSM_Arg(struct vsm *vd, char flag, const char *arg) |
375 |
|
{ |
376 |
6828 |
char *p = NULL; |
377 |
|
|
378 |
6828 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
379 |
|
|
380 |
6828 |
if (arg == NULL) |
381 |
123 |
return (1); |
382 |
6705 |
switch (flag) { |
383 |
|
case 't': |
384 |
39 |
if (!strcasecmp(arg, "off")) { |
385 |
0 |
vd->patience = -1; |
386 |
0 |
} else { |
387 |
39 |
vd->patience = strtod(arg, &p); |
388 |
63 |
if ((p != NULL && *p != '\0') || |
389 |
24 |
!isfinite(vd->patience) || vd->patience < 0) |
390 |
42 |
return (vsm_diag(vd, |
391 |
21 |
"-t: Invalid argument: %s", arg)); |
392 |
|
} |
393 |
18 |
break; |
394 |
|
case 'n': |
395 |
6666 |
if (vd->wdname != NULL) |
396 |
0 |
free(vd->wdname); |
397 |
6666 |
vd->wdname = VIN_n_Arg(arg); |
398 |
6666 |
if (vd->wdname == NULL) |
399 |
0 |
return (vsm_diag(vd, "Invalid instance name: %s", |
400 |
0 |
strerror(errno))); |
401 |
6666 |
break; |
402 |
|
default: |
403 |
0 |
return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag)); |
404 |
|
} |
405 |
6684 |
return (1); |
406 |
6828 |
} |
407 |
|
|
408 |
|
/*--------------------------------------------------------------------*/ |
409 |
|
|
410 |
|
void |
411 |
6612 |
VSM_Destroy(struct vsm **vdp) |
412 |
|
{ |
413 |
|
struct vsm *vd; |
414 |
|
|
415 |
6612 |
TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC); |
416 |
|
|
417 |
6612 |
VSM_ResetError(vd); |
418 |
6612 |
REPLACE(vd->wdname, NULL); |
419 |
6612 |
if (vd->diag != NULL) |
420 |
0 |
VSB_destroy(&vd->diag); |
421 |
6612 |
if (vd->wdfd >= 0) |
422 |
6606 |
closefd(&vd->wdfd); |
423 |
6612 |
vsm_delset(&vd->mgt); |
424 |
6612 |
vsm_delset(&vd->child); |
425 |
6612 |
FREE_OBJ(vd); |
426 |
6612 |
} |
427 |
|
|
428 |
|
/*--------------------------------------------------------------------*/ |
429 |
|
|
430 |
|
const char * |
431 |
39 |
VSM_Error(const struct vsm *vd) |
432 |
|
{ |
433 |
|
|
434 |
39 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
435 |
|
|
436 |
39 |
if (vd->diag == NULL) |
437 |
0 |
return ("No VSM error"); |
438 |
|
else |
439 |
39 |
return (VSB_data(vd->diag)); |
440 |
39 |
} |
441 |
|
|
442 |
|
/*--------------------------------------------------------------------*/ |
443 |
|
|
444 |
|
void |
445 |
13374 |
VSM_ResetError(struct vsm *vd) |
446 |
|
{ |
447 |
|
|
448 |
13374 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
449 |
|
|
450 |
13374 |
if (vd->diag == NULL) |
451 |
13260 |
return; |
452 |
114 |
VSB_destroy(&vd->diag); |
453 |
13374 |
} |
454 |
|
|
455 |
|
/*-------------------------------------------------------------------- |
456 |
|
*/ |
457 |
|
|
458 |
|
static int |
459 |
50915 |
vsm_cmp_av(char * const *a1, char * const *a2) |
460 |
|
{ |
461 |
|
|
462 |
75127 |
while (1) { |
463 |
75127 |
if (*a1 == NULL && *a2 == NULL) |
464 |
5959 |
return (0); |
465 |
69168 |
if (*a1 == NULL || *a2 == NULL) |
466 |
0 |
return (1); |
467 |
69168 |
if (strcmp(*a1, *a2)) |
468 |
44956 |
return (1); |
469 |
24212 |
a1++; |
470 |
24212 |
a2++; |
471 |
|
} |
472 |
50915 |
} |
473 |
|
|
474 |
|
static struct vsm_seg * |
475 |
6178 |
vsm_findcluster(const struct vsm_set *vs, const char *cnam) |
476 |
|
{ |
477 |
|
struct vsm_seg *vg; |
478 |
6178 |
AN(vs); |
479 |
6178 |
AN(cnam); |
480 |
8748 |
VTAILQ_FOREACH(vg, &vs->clusters, clist) { |
481 |
8748 |
AN(vg->av[1]); |
482 |
8748 |
if (!strcmp(cnam, vg->av[1])) |
483 |
6178 |
return (vg); |
484 |
2570 |
} |
485 |
0 |
return (NULL); |
486 |
6178 |
} |
487 |
|
|
488 |
|
static unsigned |
489 |
183123 |
vsm_running(struct vsm_set *vs, pid_t pid) |
490 |
|
{ |
491 |
|
|
492 |
183123 |
AN(vs); |
493 |
|
|
494 |
183123 |
if (pid == 0) |
495 |
0 |
return (0); |
496 |
|
|
497 |
183123 |
if (kill(pid, 0) == 0) { |
498 |
176123 |
vs->couldkill = 1; |
499 |
176123 |
return (1); |
500 |
|
} |
501 |
7000 |
if (errno == EPERM) /* a process exists, assume running */ |
502 |
0 |
return (1); |
503 |
7000 |
assert(errno != EINVAL); |
504 |
7000 |
return (0); |
505 |
183123 |
} |
506 |
|
|
507 |
|
static int |
508 |
11166 |
vsm_vlu_hash(struct vsm_set *vs, const char *line) |
509 |
|
{ |
510 |
|
int i; |
511 |
|
uintmax_t id1, id2; |
512 |
|
|
513 |
11166 |
i = sscanf(line, "# %ju %ju", &id1, &id2); |
514 |
11166 |
if (i != 2) { |
515 |
0 |
vs->retval |= vs->flag_restarted; |
516 |
0 |
return (0); |
517 |
|
} |
518 |
11166 |
if (vs->couldkill >= 0 && vsm_running(vs, id1)) { |
519 |
|
/* nothing to do */ |
520 |
11166 |
} else if (vs->couldkill > 0 && errno == ESRCH) { |
521 |
0 |
vs->retval |= vs->flag_restarted | VSM_MGT_CHANGED; |
522 |
0 |
return (0); |
523 |
|
} |
524 |
11166 |
vs->retval |= VSM_MGT_RUNNING; |
525 |
11166 |
if (id1 != vs->id1 || id2 != vs->id2) { |
526 |
11160 |
vs->retval |= vs->flag_restarted; |
527 |
11160 |
vs->id1 = id1; |
528 |
11160 |
vs->id2 = id2; |
529 |
11160 |
} |
530 |
11166 |
return (0); |
531 |
11166 |
} |
532 |
|
|
533 |
|
static int |
534 |
233824 |
vsm_vlu_plus(struct vsm *vd, struct vsm_set *vs, const char *line) |
535 |
|
{ |
536 |
|
char **av; |
537 |
|
int ac; |
538 |
|
struct vsm_seg *vg; |
539 |
|
|
540 |
233824 |
av = VAV_Parse(line + 1, &ac, 0); |
541 |
|
|
542 |
233824 |
if (av[0] != NULL || ac < 4 || ac > 6) { |
543 |
0 |
(void)(vsm_diag(vd, "vsm_vlu_plus: bad index (%d/%s)", |
544 |
0 |
ac, av[0])); |
545 |
0 |
VAV_Free(av); |
546 |
0 |
return (-1); |
547 |
|
} |
548 |
|
|
549 |
233824 |
vg = vs->vg; |
550 |
233824 |
CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC); |
551 |
233824 |
if (vg != NULL) |
552 |
150 |
AZ(vg->flags & VSM_FLAG_STALE); |
553 |
233881 |
while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1])) |
554 |
57 |
vg = VTAILQ_NEXT(vg, list); |
555 |
233824 |
if (vg != NULL) { |
556 |
|
/* entry compared equal, so it survives */ |
557 |
150 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
558 |
150 |
VAV_Free(av); |
559 |
150 |
vg->flags |= VSM_FLAG_MARKSCAN; |
560 |
150 |
vs->vg = VTAILQ_NEXT(vg, list); |
561 |
150 |
} else { |
562 |
233674 |
ALLOC_OBJ(vg, VSM_SEG_MAGIC); |
563 |
233674 |
AN(vg); |
564 |
233674 |
vg->av = av; |
565 |
233674 |
vg->set = vs; |
566 |
233674 |
vg->flags = VSM_FLAG_MARKSCAN; |
567 |
233674 |
vg->serial = vd->serial; |
568 |
|
|
569 |
233674 |
VTAILQ_INSERT_TAIL(&vs->segs, vg, list); |
570 |
233674 |
if (ac == 4) { |
571 |
5748 |
vg->flags |= VSM_FLAG_CLUSTER; |
572 |
5748 |
VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist); |
573 |
233674 |
} else if (*vg->av[2] != '0') { |
574 |
6021 |
vg->cluster = vsm_findcluster(vs, vg->av[1]); |
575 |
6021 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
576 |
6021 |
} |
577 |
233674 |
vs->retval |= vs->flag_changed; |
578 |
|
} |
579 |
233824 |
return (0); |
580 |
233824 |
} |
581 |
|
|
582 |
|
static int |
583 |
5809 |
vsm_vlu_minus(struct vsm *vd, struct vsm_set *vs, const char *line) |
584 |
|
{ |
585 |
|
char **av; |
586 |
|
int ac; |
587 |
|
struct vsm_seg *vg; |
588 |
|
|
589 |
5809 |
av = VAV_Parse(line + 1, &ac, 0); |
590 |
|
|
591 |
5809 |
if (av[0] != NULL || ac < 4 || ac > 6) { |
592 |
0 |
(void)(vsm_diag(vd, "vsm_vlu_minus: bad index (%d/%s)", |
593 |
0 |
ac, av[0])); |
594 |
0 |
VAV_Free(av); |
595 |
0 |
return (-1); |
596 |
|
} |
597 |
|
|
598 |
|
/* Clustered segments cannot come before their cluster */ |
599 |
5809 |
if (*av[2] != '0') |
600 |
157 |
vg = vsm_findcluster(vs, av[1]); |
601 |
|
else |
602 |
5652 |
vg = VTAILQ_FIRST(&vs->segs); |
603 |
|
|
604 |
50708 |
for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) { |
605 |
50708 |
if (!vsm_cmp_av(&vg->av[1], &av[1])) { |
606 |
5809 |
vs->retval |= vs->flag_changed; |
607 |
5809 |
vsm_delseg(vg, 1); |
608 |
5809 |
break; |
609 |
|
} |
610 |
44899 |
} |
611 |
5809 |
AN(vg); |
612 |
5809 |
VAV_Free(av); |
613 |
5809 |
return (0); |
614 |
5809 |
} |
615 |
|
|
616 |
|
static int v_matchproto_(vlu_f) |
617 |
250799 |
vsm_vlu_func(void *priv, const char *line) |
618 |
|
{ |
619 |
|
struct vsm *vd; |
620 |
|
struct vsm_set *vs; |
621 |
250799 |
int i = 0; |
622 |
|
|
623 |
250799 |
CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC); |
624 |
250799 |
vd = vs->vsm; |
625 |
250799 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
626 |
250799 |
AN(line); |
627 |
|
|
628 |
|
/* Up the serial counter. This wraps at UINTPTR_MAX/2 |
629 |
|
* because thats the highest value we can store in struct |
630 |
|
* vsm_fantom. */ |
631 |
250799 |
vd->serial = VSM_PRIV_LOW(vd->serial + 1); |
632 |
|
|
633 |
250799 |
switch (line[0]) { |
634 |
|
case '#': |
635 |
11166 |
i = vsm_vlu_hash(vs, line); |
636 |
19101 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
637 |
7935 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
638 |
11166 |
if (!(vs->retval & vs->flag_restarted)) |
639 |
6 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
640 |
11166 |
break; |
641 |
|
case '+': |
642 |
233824 |
i = vsm_vlu_plus(vd, vs, line); |
643 |
233824 |
break; |
644 |
|
case '-': |
645 |
5809 |
i = vsm_vlu_minus(vd, vs, line); |
646 |
5809 |
break; |
647 |
|
default: |
648 |
0 |
break; |
649 |
|
} |
650 |
250799 |
return (i); |
651 |
|
} |
652 |
|
|
653 |
|
static void |
654 |
171957 |
vsm_readlines(struct vsm_set *vs) |
655 |
|
{ |
656 |
|
int i; |
657 |
|
|
658 |
171957 |
do { |
659 |
196144 |
assert(vs->fd >= 0); |
660 |
196144 |
i = VLU_Fd(vs->vlu, vs->fd); |
661 |
196144 |
} while (!i); |
662 |
171957 |
assert(i == -2); |
663 |
171957 |
} |
664 |
|
|
665 |
|
static unsigned |
666 |
195489 |
vsm_refresh_set(struct vsm *vd, struct vsm_set *vs) |
667 |
|
{ |
668 |
195489 |
unsigned restarted = 0; |
669 |
|
struct stat st; |
670 |
|
|
671 |
195489 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
672 |
195489 |
CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC); |
673 |
195489 |
vs->retval = 0; |
674 |
361700 |
if (vs->dfd >= 0 && ( |
675 |
166388 |
fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) || |
676 |
166391 |
st.st_ino != vs->dst.st_ino || |
677 |
166210 |
st.st_dev != vs->dst.st_dev || |
678 |
166211 |
st.st_mode != vs->dst.st_mode || |
679 |
166211 |
st.st_nlink == 0)) { |
680 |
190 |
closefd(&vs->dfd); |
681 |
180 |
restarted = vs->flag_restarted; |
682 |
180 |
} |
683 |
|
|
684 |
195487 |
if (vs->dfd < 0) { |
685 |
29272 |
if (vs->fd >= 0) |
686 |
180 |
closefd(&vs->fd); |
687 |
29272 |
vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY); |
688 |
29272 |
} |
689 |
|
|
690 |
195487 |
if (vs->dfd < 0) { |
691 |
18112 |
vs->id1 = vs->id2 = 0; |
692 |
18112 |
vsm_wash_set(vs, 1); |
693 |
18112 |
return (vs->retval | restarted); |
694 |
|
} |
695 |
|
|
696 |
177375 |
AZ(fstat(vs->dfd, &vs->dst)); |
697 |
|
|
698 |
338168 |
if (vs->fd >= 0 && ( |
699 |
163574 |
fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) || |
700 |
160800 |
st.st_ino != vs->fst.st_ino || |
701 |
160794 |
st.st_dev != vs->fst.st_dev || |
702 |
160793 |
st.st_mode != vs->fst.st_mode || |
703 |
160792 |
st.st_size < vs->fst.st_size || |
704 |
160793 |
st.st_nlink < 1)) { |
705 |
2787 |
closefd(&vs->fd); |
706 |
2781 |
vs->retval |= vs->flag_changed; |
707 |
2781 |
} |
708 |
|
|
709 |
177371 |
if (vs->fd >= 0) { |
710 |
160792 |
vs->vg = NULL; |
711 |
160792 |
vsm_readlines(vs); |
712 |
160792 |
} else { |
713 |
51230 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
714 |
34651 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
715 |
16579 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
716 |
16579 |
vs->fd = openat(vs->dfd, "_.index", O_RDONLY); |
717 |
16579 |
if (vs->fd < 0) |
718 |
5413 |
return (vs->retval | restarted); |
719 |
11166 |
VLU_Reset(vs->vlu); |
720 |
11166 |
AZ(fstat(vs->fd, &vs->fst)); |
721 |
11166 |
vsm_readlines(vs); |
722 |
11166 |
vsm_wash_set(vs, 0); |
723 |
|
} |
724 |
|
|
725 |
171958 |
vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR); |
726 |
|
|
727 |
171958 |
if (vs->couldkill < 0 || vsm_running(vs, vs->id1)) |
728 |
164967 |
vs->retval |= vs->flag_running; |
729 |
171960 |
return (vs->retval); |
730 |
195485 |
} |
731 |
|
|
732 |
|
/*--------------------------------------------------------------------*/ |
733 |
|
|
734 |
|
unsigned |
735 |
100434 |
VSM_Status(struct vsm *vd) |
736 |
|
{ |
737 |
100434 |
unsigned retval = 0; |
738 |
|
struct stat st; |
739 |
|
|
740 |
100434 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
741 |
|
|
742 |
|
/* See if the -n workdir changed */ |
743 |
100434 |
if (vd->wdfd >= 0) { |
744 |
93677 |
AZ(fstat(vd->wdfd, &st)); |
745 |
187354 |
if (st.st_ino != vd->wdst.st_ino || |
746 |
93677 |
st.st_dev != vd->wdst.st_dev || |
747 |
93677 |
st.st_mode != vd->wdst.st_mode || |
748 |
93677 |
st.st_nlink == 0) { |
749 |
0 |
closefd(&vd->wdfd); |
750 |
0 |
vsm_wash_set(vd->mgt, 1); |
751 |
0 |
vsm_wash_set(vd->child, 1); |
752 |
0 |
} |
753 |
93677 |
} |
754 |
|
|
755 |
|
/* Open workdir */ |
756 |
100434 |
if (vd->wdfd < 0) { |
757 |
6756 |
retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED; |
758 |
6756 |
retval |= VSM_WRK_RESTARTED | VSM_WRK_CHANGED; |
759 |
6756 |
vd->wdfd = open(vd->wdname, O_RDONLY); |
760 |
6756 |
if (vd->wdfd < 0) |
761 |
102 |
(void)vsm_diag(vd, |
762 |
|
"VSM_Status: Cannot open workdir"); |
763 |
|
else |
764 |
6654 |
AZ(fstat(vd->wdfd, &vd->wdst)); |
765 |
6756 |
} |
766 |
|
|
767 |
100434 |
if (vd->wdfd >= 0) { |
768 |
100334 |
retval |= vsm_refresh_set(vd, vd->mgt); |
769 |
100334 |
if (vd->mgt->couldkill > 0 && (retval & VSM_MGT_RESTARTED)) |
770 |
6654 |
vd->mgt->couldkill = 0; |
771 |
100334 |
if (retval & VSM_MGT_RUNNING) |
772 |
95149 |
retval |= vsm_refresh_set(vd, vd->child); |
773 |
100334 |
if (vd->child->couldkill > 0 && (retval & VSM_WRK_RESTARTED)) |
774 |
4500 |
vd->child->couldkill = 0; |
775 |
100334 |
} |
776 |
100434 |
return (retval); |
777 |
|
} |
778 |
|
|
779 |
|
/*--------------------------------------------------------------------*/ |
780 |
|
|
781 |
|
int |
782 |
6666 |
VSM_Attach(struct vsm *vd, int progress) |
783 |
|
{ |
784 |
|
const char *def; |
785 |
|
double t0; |
786 |
|
unsigned u; |
787 |
6666 |
int i, n = 0; |
788 |
|
|
789 |
6666 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
790 |
|
|
791 |
6666 |
if (vd->patience < 0) |
792 |
0 |
t0 = DBL_MAX; |
793 |
|
else |
794 |
6666 |
t0 = VTIM_mono() + vd->patience; |
795 |
|
|
796 |
6666 |
if (vd->wdname == NULL) { |
797 |
21 |
def = getenv("VARNISH_DEFAULT_N"); |
798 |
21 |
if (def == NULL) |
799 |
0 |
def = ""; /* Use default (hostname) */ |
800 |
21 |
i = VSM_Arg(vd, 'n', def); |
801 |
21 |
if (i < 0) |
802 |
0 |
return (i); |
803 |
21 |
AN(vd->wdname); |
804 |
21 |
} |
805 |
|
|
806 |
6666 |
AZ(vd->attached); |
807 |
6765 |
while (!VSIG_int && !VSIG_term) { |
808 |
6762 |
u = VSM_Status(vd); |
809 |
6762 |
VSM_ResetError(vd); |
810 |
6762 |
if (u & VSM_MGT_RUNNING) { |
811 |
6654 |
if (progress >= 0 && n > 4) |
812 |
0 |
(void)write(progress, "\n", 1); |
813 |
6654 |
vd->attached = 1; |
814 |
6654 |
return (0); |
815 |
|
} |
816 |
108 |
if (t0 < VTIM_mono()) { |
817 |
9 |
if (progress >= 0 && n > 4) |
818 |
3 |
(void)write(progress, "\n", 1); |
819 |
9 |
return (vsm_diag(vd, |
820 |
|
"Could not get hold of varnishd, is it running?")); |
821 |
|
} |
822 |
99 |
if (progress >= 0 && !(++n % 4)) |
823 |
21 |
(void)write(progress, ".", 1); |
824 |
99 |
VTIM_sleep(.25); |
825 |
|
} |
826 |
3 |
return (vsm_diag(vd, "Attach interrupted")); |
827 |
6666 |
} |
828 |
|
|
829 |
|
/*--------------------------------------------------------------------*/ |
830 |
|
|
831 |
|
static struct vsm_seg * |
832 |
18190 |
vsm_set_findseg(const struct vsm_set *vs, uintptr_t serial) |
833 |
|
{ |
834 |
|
struct vsm_seg *vg; |
835 |
|
|
836 |
229237 |
VTAILQ_FOREACH(vg, &vs->segs, list) { |
837 |
218275 |
if (vg->serial == serial) |
838 |
7228 |
return (vg); |
839 |
211047 |
} |
840 |
20694 |
VTAILQ_FOREACH(vg, &vs->stale, list) { |
841 |
10131 |
if (vg->serial == serial) |
842 |
399 |
return (vg); |
843 |
9732 |
} |
844 |
10563 |
return (NULL); |
845 |
18190 |
} |
846 |
|
|
847 |
|
static struct vsm_seg * |
848 |
586539 |
vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf) |
849 |
|
{ |
850 |
|
struct vsm_seg *vg; |
851 |
|
uint64_t x; |
852 |
|
|
853 |
586539 |
x = VSM_PRIV_HIGH(vf->priv); |
854 |
586539 |
if (x == vd->serial) { |
855 |
577325 |
vg = (struct vsm_seg *)vf->priv2; |
856 |
577325 |
if (!VALID_OBJ(vg, VSM_SEG_MAGIC) || |
857 |
577325 |
vg->serial != VSM_PRIV_LOW(vf->priv)) |
858 |
0 |
WRONG("Corrupt fantom"); |
859 |
577325 |
return (vg); |
860 |
|
} |
861 |
|
|
862 |
9214 |
x = VSM_PRIV_LOW(vf->priv); |
863 |
9214 |
vg = vsm_set_findseg(vd->mgt, x); |
864 |
9214 |
if (vg == NULL) |
865 |
8976 |
vg = vsm_set_findseg(vd->child, x); |
866 |
9214 |
if (vg == NULL) |
867 |
1587 |
return (NULL); |
868 |
|
|
869 |
|
/* Update the fantom with the new priv so that lookups will be |
870 |
|
* fast on the next call. Note that this casts away the const. */ |
871 |
7627 |
((struct vsm_fantom *)TRUST_ME(vf))->priv = |
872 |
7627 |
VSM_PRIV_MERGE(vg->serial, vd->serial); |
873 |
7627 |
return (vg); |
874 |
586539 |
} |
875 |
|
|
876 |
|
/*--------------------------------------------------------------------*/ |
877 |
|
|
878 |
|
void |
879 |
25424 |
VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf) |
880 |
|
{ |
881 |
|
|
882 |
25424 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
883 |
25424 |
AN(vf); |
884 |
|
|
885 |
25424 |
AN(vd->attached); |
886 |
25424 |
memset(vf, 0, sizeof *vf); |
887 |
25424 |
} |
888 |
|
|
889 |
|
int |
890 |
344053 |
VSM__itern(struct vsm *vd, struct vsm_fantom *vf) |
891 |
|
{ |
892 |
|
struct vsm_seg *vg; |
893 |
|
|
894 |
344053 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
895 |
344053 |
AN(vd->attached); |
896 |
344053 |
AN(vf); |
897 |
|
|
898 |
344053 |
if (vf->priv == 0) { |
899 |
25424 |
vg = VTAILQ_FIRST(&vd->mgt->segs); |
900 |
25424 |
if (vg == NULL) |
901 |
0 |
return (0); |
902 |
25424 |
} else { |
903 |
318629 |
vg = vsm_findseg(vd, vf); |
904 |
318629 |
if (vg == NULL) |
905 |
0 |
return (vsm_diag(vd, "VSM_FOREACH: inconsistency")); |
906 |
323537 |
while (1) { |
907 |
323537 |
if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL) |
908 |
25139 |
vg = VTAILQ_FIRST(&vd->child->segs); |
909 |
|
else |
910 |
298398 |
vg = VTAILQ_NEXT(vg, list); |
911 |
323537 |
if (vg == NULL) |
912 |
16692 |
return (0); |
913 |
306845 |
if (!(vg->flags & VSM_FLAG_CLUSTER)) |
914 |
301937 |
break; |
915 |
|
} |
916 |
|
} |
917 |
327361 |
memset(vf, 0, sizeof *vf); |
918 |
327361 |
vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial); |
919 |
327361 |
vf->priv2 = (uintptr_t)vg; |
920 |
327361 |
vf->category = vg->av[4]; |
921 |
327361 |
vf->ident = vg->av[5]; |
922 |
327361 |
AN(vf->category); |
923 |
327361 |
return (1); |
924 |
344053 |
} |
925 |
|
|
926 |
|
/*--------------------------------------------------------------------*/ |
927 |
|
|
928 |
|
int |
929 |
19225 |
VSM_Map(struct vsm *vd, struct vsm_fantom *vf) |
930 |
|
{ |
931 |
|
struct vsm_seg *vg, *vgc; |
932 |
|
size_t of, sz; |
933 |
|
int r; |
934 |
|
|
935 |
19225 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
936 |
19225 |
AN(vd->attached); |
937 |
19225 |
AN(vf); |
938 |
19225 |
vg = vsm_findseg(vd, vf); |
939 |
19225 |
if (vg == NULL) |
940 |
0 |
return (vsm_diag(vd, "VSM_Map: bad fantom")); |
941 |
|
|
942 |
19225 |
assert(vg->serial == VSM_PRIV_LOW(vf->priv)); |
943 |
19225 |
assert(vg->av[4] == vf->category); |
944 |
19225 |
assert(vg->av[5] == vf->ident); |
945 |
|
|
946 |
19225 |
if (vg->b != NULL) { |
947 |
0 |
assert(vg->refs > 0); |
948 |
0 |
AN(vg->e); |
949 |
0 |
vf->b = vg->b; |
950 |
0 |
vf->e = vg->e; |
951 |
0 |
vg->refs++; |
952 |
0 |
return (0); |
953 |
|
} |
954 |
|
|
955 |
19225 |
assert(vg->refs == 0); |
956 |
|
|
957 |
19225 |
vgc = vg->cluster; |
958 |
|
|
959 |
19225 |
if (vgc == NULL) { |
960 |
18794 |
r = vsm_mapseg(vd, vg); |
961 |
18794 |
if (r) |
962 |
6 |
return (r); |
963 |
18788 |
vf->b = vg->b; |
964 |
18788 |
vf->e = vg->e; |
965 |
|
|
966 |
18788 |
vg->refs++; |
967 |
|
|
968 |
18788 |
return (0); |
969 |
|
} |
970 |
|
|
971 |
431 |
CHECK_OBJ_NOTNULL(vgc, VSM_SEG_MAGIC); |
972 |
431 |
assert(vgc->flags & VSM_FLAG_CLUSTER); |
973 |
431 |
assert(vg->s == NULL); |
974 |
431 |
assert(vg->sz == 0); |
975 |
|
|
976 |
431 |
r = vsm_mapseg(vd, vgc); |
977 |
431 |
if (r) |
978 |
0 |
return (r); |
979 |
431 |
vgc->refs++; |
980 |
|
|
981 |
431 |
of = strtoul(vg->av[2], NULL, 10); |
982 |
431 |
sz = strtoul(vg->av[3], NULL, 10); |
983 |
431 |
assert(sz > 0); |
984 |
|
|
985 |
431 |
assert(vgc->sz >= of + sz); |
986 |
431 |
assert(vgc->s == vgc->b); |
987 |
431 |
vg->b = (char *)vgc->b + of; |
988 |
431 |
vg->e = (char *)vg->b + sz; |
989 |
|
|
990 |
431 |
vf->b = vg->b; |
991 |
431 |
vf->e = vg->e; |
992 |
|
|
993 |
431 |
vg->refs++; |
994 |
|
|
995 |
431 |
return (0); |
996 |
19225 |
} |
997 |
|
|
998 |
|
/*--------------------------------------------------------------------*/ |
999 |
|
|
1000 |
|
int |
1001 |
16906 |
VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf) |
1002 |
|
{ |
1003 |
|
struct vsm_seg *vg; |
1004 |
|
|
1005 |
16906 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
1006 |
16906 |
AN(vd->attached); |
1007 |
16906 |
AN(vf); |
1008 |
16906 |
AN(vf->b); |
1009 |
16906 |
vg = vsm_findseg(vd, vf); |
1010 |
16906 |
if (vg == NULL) |
1011 |
0 |
return (vsm_diag(vd, "VSM_Unmap: bad fantom")); |
1012 |
16906 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
1013 |
16906 |
assert(vg->refs > 0); |
1014 |
16906 |
vg->refs--; |
1015 |
16906 |
vf->b = NULL; |
1016 |
16906 |
vf->e = NULL; |
1017 |
16906 |
if (vg->refs > 0) |
1018 |
0 |
return (0); |
1019 |
|
|
1020 |
16906 |
if (vg->cluster) { |
1021 |
86 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
1022 |
86 |
assert(vg->s == NULL); |
1023 |
86 |
assert(vg->sz == 0); |
1024 |
86 |
assert(vg->cluster->refs > 0); |
1025 |
86 |
if (--vg->cluster->refs == 0) { |
1026 |
80 |
vsm_unmapseg(vg->cluster); |
1027 |
80 |
if (vg->cluster->flags & VSM_FLAG_STALE) { |
1028 |
6 |
AN(vg->flags & VSM_FLAG_STALE); |
1029 |
6 |
vsm_delseg(vg->cluster, 0); |
1030 |
6 |
} |
1031 |
80 |
} |
1032 |
86 |
vg->b = vg->e = NULL; |
1033 |
86 |
} else { |
1034 |
16820 |
vsm_unmapseg(vg); |
1035 |
|
} |
1036 |
16906 |
if (vg->flags & VSM_FLAG_STALE) |
1037 |
399 |
vsm_delseg(vg, 0); |
1038 |
16906 |
return (0); |
1039 |
16906 |
} |
1040 |
|
|
1041 |
|
/*--------------------------------------------------------------------*/ |
1042 |
|
|
1043 |
|
const struct vsm_valid * |
1044 |
231771 |
VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf) |
1045 |
|
{ |
1046 |
|
struct vsm_seg *vg; |
1047 |
|
|
1048 |
231771 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
1049 |
231771 |
AN(vf); |
1050 |
231771 |
vg = vsm_findseg(vd, vf); |
1051 |
231771 |
if (vg == NULL || vg->flags & VSM_FLAG_STALE) |
1052 |
2009 |
return (VSM_invalid); |
1053 |
229762 |
return (VSM_valid); |
1054 |
231771 |
} |
1055 |
|
|
1056 |
|
/*--------------------------------------------------------------------*/ |
1057 |
|
|
1058 |
|
int |
1059 |
21289 |
VSM_Get(struct vsm *vd, struct vsm_fantom *vf, |
1060 |
|
const char *category, const char *ident) |
1061 |
|
{ |
1062 |
|
|
1063 |
21289 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
1064 |
21289 |
AN(vd->attached); |
1065 |
152958 |
VSM_FOREACH(vf, vd) { |
1066 |
140116 |
if (strcmp(vf->category, category)) |
1067 |
131669 |
continue; |
1068 |
8447 |
if (ident != NULL && strcmp(vf->ident, ident)) |
1069 |
0 |
continue; |
1070 |
8447 |
return (1); |
1071 |
|
} |
1072 |
12842 |
memset(vf, 0, sizeof *vf); |
1073 |
12842 |
return (0); |
1074 |
21289 |
} |
1075 |
|
|
1076 |
|
/*--------------------------------------------------------------------*/ |
1077 |
|
|
1078 |
|
char * |
1079 |
285 |
VSM_Dup(struct vsm *vd, const char *category, const char *ident) |
1080 |
|
{ |
1081 |
|
struct vsm_fantom vf; |
1082 |
285 |
char *p = NULL; |
1083 |
|
|
1084 |
285 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
1085 |
285 |
AN(vd->attached); |
1086 |
1146 |
VSM_FOREACH(&vf, vd) { |
1087 |
1146 |
if (strcmp(vf.category, category)) |
1088 |
492 |
continue; |
1089 |
654 |
if (ident != NULL && strcmp(vf.ident, ident)) |
1090 |
369 |
continue; |
1091 |
285 |
AZ(VSM_Map(vd, &vf)); |
1092 |
285 |
AN(vf.b); |
1093 |
285 |
AN(vf.e); |
1094 |
285 |
p = malloc((char*)vf.e - (char*)vf.b); |
1095 |
285 |
AN(p); |
1096 |
285 |
memcpy(p, vf.b, (char*)vf.e - (char*)vf.b); |
1097 |
285 |
AZ(VSM_Unmap(vd, &vf)); |
1098 |
285 |
break; |
1099 |
|
} |
1100 |
285 |
return (p); |
1101 |
|
} |