| | 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 |
|
|
137 |
|
struct vsm { |
138 |
|
unsigned magic; |
139 |
|
#define VSM_MAGIC 0x6e3bd69b |
140 |
|
|
141 |
|
struct vsb *diag; |
142 |
|
uint64_t serial; |
143 |
|
|
144 |
|
int wdfd; |
145 |
|
struct stat wdst; |
146 |
|
char *wdname; |
147 |
|
|
148 |
|
struct vsm_set *mgt; |
149 |
|
struct vsm_set *child; |
150 |
|
|
151 |
|
int attached; |
152 |
|
double patience; |
153 |
|
}; |
154 |
|
|
155 |
|
/*--------------------------------------------------------------------*/ |
156 |
|
|
157 |
|
static int |
158 |
1583 |
vsm_diag(struct vsm *vd, const char *fmt, ...) |
159 |
|
{ |
160 |
|
va_list ap; |
161 |
|
|
162 |
1583 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
163 |
1583 |
AN(fmt); |
164 |
|
|
165 |
1583 |
if (vd->diag == NULL) |
166 |
1583 |
vd->diag = VSB_new_auto(); |
167 |
1583 |
AN(vd->diag); |
168 |
1583 |
VSB_clear(vd->diag); |
169 |
1583 |
va_start(ap, fmt); |
170 |
1583 |
VSB_vprintf(vd->diag, fmt, ap); |
171 |
1583 |
va_end(ap); |
172 |
1583 |
AZ(VSB_finish(vd->diag)); |
173 |
1583 |
return (-1); |
174 |
|
} |
175 |
|
|
176 |
|
/*--------------------------------------------------------------------*/ |
177 |
|
|
178 |
|
static int |
179 |
204542 |
vsm_mapseg(struct vsm *vd, struct vsm_seg *vg) |
180 |
|
{ |
181 |
|
size_t of, off, sz, ps, len; |
182 |
|
struct vsb *vsb; |
183 |
|
int fd; |
184 |
|
|
185 |
204542 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
186 |
|
|
187 |
204542 |
if (vg->s != NULL) |
188 |
4000 |
return (0); |
189 |
|
|
190 |
200542 |
ps = getpagesize(); |
191 |
|
|
192 |
200542 |
of = strtoul(vg->av[2], NULL, 10); |
193 |
200542 |
off = RDN2(of, ps); |
194 |
|
|
195 |
200542 |
if (vg->flags & VSM_FLAG_CLUSTER) |
196 |
3001 |
assert(of == 0); |
197 |
200542 |
assert(vg->cluster == NULL); |
198 |
|
|
199 |
200542 |
sz = strtoul(vg->av[3], NULL, 10); |
200 |
200542 |
assert(sz > 0); |
201 |
200542 |
assert(of >= off); |
202 |
200542 |
len = RUP2((of - off) + sz, ps); |
203 |
|
|
204 |
200542 |
vsb = VSB_new_auto(); |
205 |
200542 |
AN(vsb); |
206 |
200542 |
VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]); |
207 |
200542 |
AZ(VSB_finish(vsb)); |
208 |
|
|
209 |
200542 |
fd = open(VSB_data(vsb), O_RDONLY); // XXX: openat |
210 |
200542 |
if (fd < 0) { |
211 |
0 |
VSB_destroy(&vsb); |
212 |
0 |
return (vsm_diag(vd, "Could not open segment")); |
213 |
|
} |
214 |
|
|
215 |
401084 |
vg->s = (void*)mmap(NULL, len, |
216 |
|
PROT_READ, |
217 |
|
MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED, |
218 |
200542 |
fd, (off_t)off); |
219 |
|
|
220 |
200542 |
VSB_destroy(&vsb); |
221 |
|
|
222 |
200542 |
closefd(&fd); |
223 |
200542 |
if (vg->s == MAP_FAILED) |
224 |
0 |
return (vsm_diag(vd, "Could not mmap segment")); |
225 |
|
|
226 |
200542 |
vg->b = (char*)(vg->s) + of - off; |
227 |
200542 |
vg->e = (char *)vg->b + sz; |
228 |
200542 |
vg->sz = len; |
229 |
|
|
230 |
200542 |
return (0); |
231 |
204542 |
} |
232 |
|
|
233 |
|
static void |
234 |
177742 |
vsm_unmapseg(struct vsm_seg *vg) |
235 |
|
{ |
236 |
|
|
237 |
177742 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
238 |
|
|
239 |
177742 |
AN(vg->b); |
240 |
177742 |
AN(vg->e); |
241 |
177742 |
AZ(munmap(vg->s, vg->sz)); |
242 |
177742 |
vg->s = vg->b = vg->e = NULL; |
243 |
177742 |
vg->sz = 0; |
244 |
177742 |
} |
245 |
|
|
246 |
|
/*--------------------------------------------------------------------*/ |
247 |
|
|
248 |
|
static void |
249 |
2701958 |
vsm_delseg(struct vsm_seg *vg, int refsok) |
250 |
|
{ |
251 |
|
|
252 |
2701958 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
253 |
|
|
254 |
2701958 |
if (vg->set->vg == vg) { |
255 |
285631 |
AZ(vg->flags & VSM_FLAG_STALE); |
256 |
285631 |
vg->set->vg = VTAILQ_NEXT(vg, list); |
257 |
285631 |
} |
258 |
|
|
259 |
2701958 |
if (refsok && vg->refs) { |
260 |
5912 |
AZ(vg->flags & VSM_FLAG_STALE); |
261 |
5912 |
vg->flags |= VSM_FLAG_STALE; |
262 |
5912 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
263 |
5912 |
VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list); |
264 |
5912 |
return; |
265 |
|
} |
266 |
|
|
267 |
2696046 |
if (vg->s != NULL) |
268 |
0 |
vsm_unmapseg(vg); |
269 |
|
|
270 |
2696046 |
if (vg->flags & VSM_FLAG_CLUSTER) { |
271 |
71240 |
vg->flags &= ~VSM_FLAG_CLUSTER; |
272 |
71240 |
VTAILQ_REMOVE(&vg->set->clusters, vg, clist); |
273 |
71240 |
} |
274 |
|
|
275 |
2696046 |
if (vg->flags & VSM_FLAG_STALE) |
276 |
5912 |
VTAILQ_REMOVE(&vg->set->stale, vg, list); |
277 |
|
else |
278 |
2690134 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
279 |
2696046 |
VAV_Free(vg->av); |
280 |
2696046 |
FREE_OBJ(vg); |
281 |
2701958 |
} |
282 |
|
|
283 |
|
/*--------------------------------------------------------------------*/ |
284 |
|
|
285 |
|
static struct vsm_set * |
286 |
170960 |
vsm_newset(const char *dirname) |
287 |
|
{ |
288 |
|
struct vsm_set *vs; |
289 |
|
|
290 |
170960 |
ALLOC_OBJ(vs, VSM_SET_MAGIC); |
291 |
170960 |
AN(vs); |
292 |
170960 |
VTAILQ_INIT(&vs->segs); |
293 |
170960 |
VTAILQ_INIT(&vs->stale); |
294 |
170960 |
VTAILQ_INIT(&vs->clusters); |
295 |
170960 |
vs->dname = dirname; |
296 |
170960 |
vs->dfd = vs->fd = -1; |
297 |
170960 |
vs->vlu = VLU_New(vsm_vlu_func, vs, 0); |
298 |
170960 |
AN(vs->vlu); |
299 |
170960 |
return (vs); |
300 |
|
} |
301 |
|
|
302 |
|
static void |
303 |
168880 |
vsm_delset(struct vsm_set **p) |
304 |
|
{ |
305 |
|
struct vsm_set *vs; |
306 |
|
struct vsm_seg *vg; |
307 |
|
|
308 |
168880 |
TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC); |
309 |
|
|
310 |
168880 |
if (vs->fd >= 0) |
311 |
102646 |
closefd(&vs->fd); |
312 |
168880 |
if (vs->dfd >= 0) |
313 |
138560 |
closefd(&vs->dfd); |
314 |
168880 |
while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) { |
315 |
0 |
AN(vg->flags & VSM_FLAG_STALE); |
316 |
0 |
vsm_delseg(vg, 0); |
317 |
|
} |
318 |
2723140 |
while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) { |
319 |
2554260 |
AZ(vg->flags & VSM_FLAG_STALE); |
320 |
2554260 |
vsm_delseg(vg, 0); |
321 |
|
} |
322 |
168880 |
assert(VTAILQ_EMPTY(&vs->clusters)); |
323 |
168880 |
VLU_Destroy(&vs->vlu); |
324 |
168880 |
FREE_OBJ(vs); |
325 |
168880 |
} |
326 |
|
|
327 |
|
static void |
328 |
364904 |
vsm_wash_set(const struct vsm_set *vs, int all) |
329 |
|
{ |
330 |
|
struct vsm_seg *vg, *vg2; |
331 |
|
|
332 |
3044660 |
VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) { |
333 |
2679756 |
if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0) |
334 |
79985 |
vsm_delseg(vg, 1); |
335 |
2679756 |
} |
336 |
364904 |
} |
337 |
|
|
338 |
|
/*--------------------------------------------------------------------*/ |
339 |
|
|
340 |
|
struct vsm * |
341 |
85480 |
VSM_New(void) |
342 |
|
{ |
343 |
|
struct vsm *vd; |
344 |
|
|
345 |
85480 |
ALLOC_OBJ(vd, VSM_MAGIC); |
346 |
85480 |
AN(vd); |
347 |
|
|
348 |
85480 |
vd->mgt = vsm_newset(VSM_MGT_DIRNAME); |
349 |
85480 |
vd->mgt->flag_running = VSM_MGT_RUNNING; |
350 |
85480 |
vd->mgt->flag_changed = VSM_MGT_CHANGED; |
351 |
85480 |
vd->mgt->flag_restarted = VSM_MGT_RESTARTED; |
352 |
|
|
353 |
85480 |
vd->child = vsm_newset(VSM_CHILD_DIRNAME); |
354 |
85480 |
vd->child->flag_running = VSM_WRK_RUNNING; |
355 |
85480 |
vd->child->flag_changed = VSM_WRK_CHANGED; |
356 |
85480 |
vd->child->flag_restarted = VSM_WRK_RESTARTED; |
357 |
|
|
358 |
85480 |
vd->mgt->vsm = vd; |
359 |
85480 |
vd->child->vsm = vd; |
360 |
85480 |
vd->wdfd = -1; |
361 |
85480 |
vd->patience = 5; |
362 |
85480 |
return (vd); |
363 |
|
} |
364 |
|
|
365 |
|
/*--------------------------------------------------------------------*/ |
366 |
|
|
367 |
|
int |
368 |
87080 |
VSM_Arg(struct vsm *vd, char flag, const char *arg) |
369 |
|
{ |
370 |
87080 |
char *p = NULL; |
371 |
|
|
372 |
87080 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
373 |
|
|
374 |
87080 |
if (arg == NULL) |
375 |
1520 |
return (1); |
376 |
85560 |
switch (flag) { |
377 |
|
case 't': |
378 |
480 |
if (!strcasecmp(arg, "off")) { |
379 |
0 |
vd->patience = -1; |
380 |
0 |
} else { |
381 |
480 |
vd->patience = strtod(arg, &p); |
382 |
760 |
if ((p != NULL && *p != '\0') || |
383 |
280 |
!isfinite(vd->patience) || vd->patience < 0) |
384 |
560 |
return (vsm_diag(vd, |
385 |
280 |
"-t: Invalid argument: %s", arg)); |
386 |
|
} |
387 |
200 |
break; |
388 |
|
case 'n': |
389 |
85080 |
if (vd->wdname != NULL) |
390 |
0 |
free(vd->wdname); |
391 |
85080 |
vd->wdname = VIN_n_Arg(arg); |
392 |
85080 |
if (vd->wdname == NULL) |
393 |
0 |
return (vsm_diag(vd, "Invalid instance name: %s", |
394 |
0 |
strerror(errno))); |
395 |
85080 |
break; |
396 |
|
default: |
397 |
0 |
return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag)); |
398 |
|
} |
399 |
85280 |
return (1); |
400 |
87080 |
} |
401 |
|
|
402 |
|
/*--------------------------------------------------------------------*/ |
403 |
|
|
404 |
|
void |
405 |
84440 |
VSM_Destroy(struct vsm **vdp) |
406 |
|
{ |
407 |
|
struct vsm *vd; |
408 |
|
|
409 |
84440 |
TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC); |
410 |
|
|
411 |
84440 |
VSM_ResetError(vd); |
412 |
84440 |
REPLACE(vd->wdname, NULL); |
413 |
84440 |
if (vd->diag != NULL) |
414 |
0 |
VSB_destroy(&vd->diag); |
415 |
84440 |
if (vd->wdfd >= 0) |
416 |
84360 |
closefd(&vd->wdfd); |
417 |
84440 |
vsm_delset(&vd->mgt); |
418 |
84440 |
vsm_delset(&vd->child); |
419 |
84440 |
FREE_OBJ(vd); |
420 |
84440 |
} |
421 |
|
|
422 |
|
/*--------------------------------------------------------------------*/ |
423 |
|
|
424 |
|
const char * |
425 |
400 |
VSM_Error(const struct vsm *vd) |
426 |
|
{ |
427 |
|
|
428 |
400 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
429 |
|
|
430 |
400 |
if (vd->diag == NULL) |
431 |
0 |
return ("No VSM error"); |
432 |
|
else |
433 |
400 |
return (VSB_data(vd->diag)); |
434 |
400 |
} |
435 |
|
|
436 |
|
/*--------------------------------------------------------------------*/ |
437 |
|
|
438 |
|
void |
439 |
170663 |
VSM_ResetError(struct vsm *vd) |
440 |
|
{ |
441 |
|
|
442 |
170663 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
443 |
|
|
444 |
170663 |
if (vd->diag == NULL) |
445 |
169400 |
return; |
446 |
1263 |
VSB_destroy(&vd->diag); |
447 |
170663 |
} |
448 |
|
|
449 |
|
/*-------------------------------------------------------------------- |
450 |
|
*/ |
451 |
|
|
452 |
|
static int |
453 |
4826286 |
vsm_cmp_av(char * const *a1, char * const *a2) |
454 |
|
{ |
455 |
|
|
456 |
5089346 |
while (1) { |
457 |
5089346 |
if (*a1 == NULL && *a2 == NULL) |
458 |
63631 |
return (0); |
459 |
5025715 |
if (*a1 == NULL || *a2 == NULL) |
460 |
0 |
return (1); |
461 |
5025715 |
if (strcmp(*a1, *a2)) |
462 |
4762655 |
return (1); |
463 |
263060 |
a1++; |
464 |
263060 |
a2++; |
465 |
|
} |
466 |
4826286 |
} |
467 |
|
|
468 |
|
static struct vsm_seg * |
469 |
78400 |
vsm_findcluster(const struct vsm_set *vs, const char *cnam) |
470 |
|
{ |
471 |
|
struct vsm_seg *vg; |
472 |
78400 |
AN(vs); |
473 |
78400 |
AN(cnam); |
474 |
109219 |
VTAILQ_FOREACH(vg, &vs->clusters, clist) { |
475 |
109219 |
AN(vg->av[1]); |
476 |
109219 |
if (!strcmp(cnam, vg->av[1])) |
477 |
78400 |
return (vg); |
478 |
30819 |
} |
479 |
0 |
return (NULL); |
480 |
78400 |
} |
481 |
|
|
482 |
|
static int |
483 |
141863 |
vsm_vlu_hash(struct vsm *vd, struct vsm_set *vs, const char *line) |
484 |
|
{ |
485 |
|
int i; |
486 |
|
uintmax_t id1, id2; |
487 |
|
|
488 |
141863 |
(void)vd; |
489 |
|
|
490 |
141863 |
i = sscanf(line, "# %ju %ju", &id1, &id2); |
491 |
141863 |
if (i != 2) { |
492 |
0 |
vs->retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED; |
493 |
0 |
return (0); |
494 |
|
} |
495 |
141863 |
vs->retval |= VSM_MGT_RUNNING; |
496 |
141863 |
if (id1 != vs->id1 || id2 != vs->id2) { |
497 |
141798 |
vs->retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED; |
498 |
141798 |
vs->id1 = id1; |
499 |
141798 |
vs->id2 = id2; |
500 |
141798 |
} |
501 |
141863 |
return (0); |
502 |
141863 |
} |
503 |
|
|
504 |
|
static int |
505 |
2728836 |
vsm_vlu_plus(struct vsm *vd, struct vsm_set *vs, const char *line) |
506 |
|
{ |
507 |
|
char **av; |
508 |
|
int ac; |
509 |
|
struct vsm_seg *vg; |
510 |
|
|
511 |
2728836 |
av = VAV_Parse(line + 1, &ac, 0); |
512 |
|
|
513 |
2728836 |
if (av[0] != NULL || ac < 4 || ac > 6) { |
514 |
0 |
(void)(vsm_diag(vd, "vsm_vlu_plus: bad index (%d/%s)", |
515 |
0 |
ac, av[0])); |
516 |
0 |
VAV_Free(av); |
517 |
0 |
return (-1); |
518 |
|
} |
519 |
|
|
520 |
2728836 |
vg = vs->vg; |
521 |
2728836 |
CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC); |
522 |
2728836 |
if (vg != NULL) |
523 |
76318 |
AZ(vg->flags & VSM_FLAG_STALE); |
524 |
6999934 |
while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1])) |
525 |
4271098 |
vg = VTAILQ_NEXT(vg, list); |
526 |
2728836 |
if (vg != NULL) { |
527 |
|
/* entry compared equal, so it survives */ |
528 |
1830 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
529 |
1830 |
VAV_Free(av); |
530 |
1830 |
vg->flags |= VSM_FLAG_MARKSCAN; |
531 |
1830 |
vs->vg = VTAILQ_NEXT(vg, list); |
532 |
1830 |
} else { |
533 |
2727006 |
ALLOC_OBJ(vg, VSM_SEG_MAGIC); |
534 |
2727006 |
AN(vg); |
535 |
2727006 |
vg->av = av; |
536 |
2727006 |
vg->set = vs; |
537 |
2727006 |
vg->flags = VSM_FLAG_MARKSCAN; |
538 |
2727006 |
vg->serial = vd->serial; |
539 |
|
|
540 |
2727006 |
VTAILQ_INSERT_TAIL(&vs->segs, vg, list); |
541 |
2727006 |
if (ac == 4) { |
542 |
71840 |
vg->flags |= VSM_FLAG_CLUSTER; |
543 |
71840 |
VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist); |
544 |
2727006 |
} else if (*vg->av[2] != '0') { |
545 |
76400 |
vg->cluster = vsm_findcluster(vs, vg->av[1]); |
546 |
76400 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
547 |
76400 |
} |
548 |
|
} |
549 |
2728836 |
return (0); |
550 |
2728836 |
} |
551 |
|
|
552 |
|
static int |
553 |
61801 |
vsm_vlu_minus(struct vsm *vd, const struct vsm_set *vs, const char *line) |
554 |
|
{ |
555 |
|
char **av; |
556 |
|
int ac; |
557 |
|
struct vsm_seg *vg; |
558 |
|
|
559 |
61801 |
av = VAV_Parse(line + 1, &ac, 0); |
560 |
|
|
561 |
61801 |
if (av[0] != NULL || ac < 4 || ac > 6) { |
562 |
0 |
(void)(vsm_diag(vd, "vsm_vlu_minus: bad index (%d/%s)", |
563 |
0 |
ac, av[0])); |
564 |
0 |
VAV_Free(av); |
565 |
0 |
return (-1); |
566 |
|
} |
567 |
|
|
568 |
|
/* Clustered segments cannot come before their cluster */ |
569 |
61801 |
if (*av[2] != '0') |
570 |
2000 |
vg = vsm_findcluster(vs, av[1]); |
571 |
|
else |
572 |
59801 |
vg = VTAILQ_FIRST(&vs->segs); |
573 |
|
|
574 |
553358 |
for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) { |
575 |
553358 |
if (!vsm_cmp_av(&vg->av[1], &av[1])) { |
576 |
61801 |
vsm_delseg(vg, 1); |
577 |
61801 |
break; |
578 |
|
} |
579 |
491557 |
} |
580 |
61801 |
AN(vg); |
581 |
61801 |
VAV_Free(av); |
582 |
61801 |
return (0); |
583 |
61801 |
} |
584 |
|
|
585 |
|
static int v_matchproto_(vlu_f) |
586 |
2932500 |
vsm_vlu_func(void *priv, const char *line) |
587 |
|
{ |
588 |
|
struct vsm *vd; |
589 |
|
struct vsm_set *vs; |
590 |
2932500 |
int i = 0; |
591 |
|
|
592 |
2932500 |
CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC); |
593 |
2932500 |
vd = vs->vsm; |
594 |
2932500 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
595 |
2932500 |
AN(line); |
596 |
|
|
597 |
|
/* Up the serial counter. This wraps at UINTPTR_MAX/2 |
598 |
|
* because thats the highest value we can store in struct |
599 |
|
* vsm_fantom. */ |
600 |
2932500 |
vd->serial = VSM_PRIV_LOW(vd->serial + 1); |
601 |
|
|
602 |
2932500 |
switch (line[0]) { |
603 |
|
case '#': |
604 |
141863 |
i = vsm_vlu_hash(vd, vs, line); |
605 |
223564 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
606 |
81701 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
607 |
141863 |
if (!(vs->retval & vs->flag_restarted)) |
608 |
56903 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
609 |
141863 |
break; |
610 |
|
case '+': |
611 |
2728836 |
i = vsm_vlu_plus(vd, vs, line); |
612 |
2728836 |
break; |
613 |
|
case '-': |
614 |
61801 |
i = vsm_vlu_minus(vd, vs, line); |
615 |
61801 |
break; |
616 |
|
default: |
617 |
0 |
break; |
618 |
|
} |
619 |
2932500 |
return (i); |
620 |
|
} |
621 |
|
|
622 |
|
static void |
623 |
2000305 |
vsm_readlines(struct vsm_set *vs) |
624 |
|
{ |
625 |
|
int i; |
626 |
|
|
627 |
2000305 |
do { |
628 |
2284148 |
assert(vs->fd >= 0); |
629 |
2284148 |
i = VLU_Fd(vs->vlu, vs->fd); |
630 |
2284148 |
} while (!i); |
631 |
2000305 |
assert(i == -2); |
632 |
2000305 |
} |
633 |
|
|
634 |
|
static unsigned |
635 |
2261257 |
vsm_refresh_set(struct vsm *vd, struct vsm_set *vs) |
636 |
|
{ |
637 |
|
struct stat st; |
638 |
|
|
639 |
2261257 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
640 |
2261257 |
CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC); |
641 |
2261257 |
vs->retval = 0; |
642 |
4157676 |
if (vs->dfd >= 0 && ( |
643 |
1898440 |
fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) || |
644 |
1898454 |
st.st_ino != vs->dst.st_ino || |
645 |
1896420 |
st.st_dev != vs->dst.st_dev || |
646 |
1896419 |
st.st_mode != vs->dst.st_mode || |
647 |
1896419 |
st.st_nlink == 0)) { |
648 |
2074 |
closefd(&vs->dfd); |
649 |
2040 |
} |
650 |
|
|
651 |
2261257 |
if (vs->dfd < 0) { |
652 |
364839 |
if (vs->fd >= 0) |
653 |
2038 |
closefd(&vs->fd); |
654 |
364839 |
vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY); |
655 |
364839 |
} |
656 |
|
|
657 |
2261257 |
if (vs->dfd < 0) { |
658 |
223039 |
vs->id1 = vs->id2 = 0; |
659 |
223039 |
vsm_wash_set(vs, 1); |
660 |
223039 |
return (vs->retval | vs->flag_restarted); |
661 |
|
} |
662 |
|
|
663 |
2038218 |
AZ(fstat(vs->dfd, &vs->dst)); |
664 |
|
|
665 |
3896668 |
if (vs->fd >= 0 && ( |
666 |
1894425 |
fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) || |
667 |
1858518 |
st.st_ino != vs->fst.st_ino || |
668 |
1858450 |
st.st_dev != vs->fst.st_dev || |
669 |
1858450 |
st.st_mode != vs->fst.st_mode || |
670 |
1858450 |
st.st_size < vs->fst.st_size || |
671 |
1858450 |
st.st_nlink < 1)) { |
672 |
35983 |
closefd(&vs->fd); |
673 |
35981 |
} |
674 |
|
|
675 |
2038216 |
if (vs->fd >= 0) { |
676 |
1858450 |
vs->vg = NULL; |
677 |
1858450 |
vsm_readlines(vs); |
678 |
1858450 |
} else { |
679 |
460475 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
680 |
280709 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
681 |
179766 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
682 |
179766 |
vs->fd = openat(vs->dfd, "_.index", O_RDONLY); |
683 |
179766 |
if (vs->fd < 0) |
684 |
37901 |
return (vs->retval | vs->flag_restarted); |
685 |
141865 |
VLU_Reset(vs->vlu); |
686 |
141865 |
AZ(fstat(vs->fd, &vs->fst)); |
687 |
141865 |
vsm_readlines(vs); |
688 |
141865 |
vsm_wash_set(vs, 0); |
689 |
|
} |
690 |
|
|
691 |
2000315 |
vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR); |
692 |
|
|
693 |
2000315 |
vs->retval |= vs->flag_running; |
694 |
2000315 |
return (vs->retval); |
695 |
2261255 |
} |
696 |
|
|
697 |
|
/*--------------------------------------------------------------------*/ |
698 |
|
|
699 |
|
unsigned |
700 |
1149807 |
VSM_Status(struct vsm *vd) |
701 |
|
{ |
702 |
1149807 |
unsigned retval = 0; |
703 |
|
struct stat st; |
704 |
|
|
705 |
1149807 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
706 |
|
|
707 |
|
/* See if the -n workdir changed */ |
708 |
1149807 |
if (vd->wdfd >= 0) { |
709 |
1063663 |
AZ(fstat(vd->wdfd, &st)); |
710 |
2127325 |
if (st.st_ino != vd->wdst.st_ino || |
711 |
1063662 |
st.st_dev != vd->wdst.st_dev || |
712 |
1063662 |
st.st_mode != vd->wdst.st_mode || |
713 |
1063662 |
st.st_nlink == 0) { |
714 |
2 |
closefd(&vd->wdfd); |
715 |
0 |
vsm_wash_set(vd->mgt, 1); |
716 |
0 |
vsm_wash_set(vd->child, 1); |
717 |
0 |
} |
718 |
1063663 |
} |
719 |
|
|
720 |
|
/* Open workdir */ |
721 |
1149807 |
if (vd->wdfd < 0) { |
722 |
86143 |
retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED; |
723 |
86143 |
retval |= VSM_WRK_RESTARTED | VSM_MGT_CHANGED; |
724 |
86143 |
vd->wdfd = open(vd->wdname, O_RDONLY); |
725 |
86143 |
if (vd->wdfd < 0) |
726 |
1183 |
(void)vsm_diag(vd, |
727 |
|
"VSM_Status: Cannot open workdir"); |
728 |
|
else |
729 |
84960 |
AZ(fstat(vd->wdfd, &vd->wdst)); |
730 |
86143 |
} |
731 |
|
|
732 |
1149807 |
if (vd->wdfd >= 0) { |
733 |
1148631 |
retval |= vsm_refresh_set(vd, vd->mgt); |
734 |
1148631 |
if (retval & VSM_MGT_RUNNING) |
735 |
1112627 |
retval |= vsm_refresh_set(vd, vd->child); |
736 |
1148631 |
} |
737 |
1149807 |
return (retval); |
738 |
|
} |
739 |
|
|
740 |
|
/*--------------------------------------------------------------------*/ |
741 |
|
|
742 |
|
int |
743 |
85080 |
VSM_Attach(struct vsm *vd, int progress) |
744 |
|
{ |
745 |
|
double t0; |
746 |
|
unsigned u; |
747 |
85080 |
int i, n = 0; |
748 |
|
|
749 |
85080 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
750 |
|
|
751 |
85080 |
if (vd->patience < 0) |
752 |
0 |
t0 = DBL_MAX; |
753 |
|
else |
754 |
85080 |
t0 = VTIM_mono() + vd->patience; |
755 |
|
|
756 |
85080 |
if (vd->wdname == NULL) { |
757 |
|
/* Use default (hostname) */ |
758 |
0 |
i = VSM_Arg(vd, 'n', ""); |
759 |
0 |
if (i < 0) |
760 |
0 |
return (i); |
761 |
0 |
AN(vd->wdname); |
762 |
0 |
} |
763 |
|
|
764 |
85080 |
AZ(vd->attached); |
765 |
86263 |
while (!VSIG_int && !VSIG_term) { |
766 |
86223 |
u = VSM_Status(vd); |
767 |
86223 |
VSM_ResetError(vd); |
768 |
86223 |
if (u & VSM_MGT_RUNNING) { |
769 |
84960 |
if (progress >= 0 && n > 4) |
770 |
0 |
(void)write(progress, "\n", 1); |
771 |
84960 |
vd->attached = 1; |
772 |
84960 |
return (0); |
773 |
|
} |
774 |
1263 |
if (t0 < VTIM_mono()) { |
775 |
80 |
if (progress >= 0 && n > 4) |
776 |
40 |
(void)write(progress, "\n", 1); |
777 |
80 |
return (vsm_diag(vd, |
778 |
|
"Could not get hold of varnishd, is it running?")); |
779 |
|
} |
780 |
1183 |
if (progress >= 0 && !(++n % 4)) |
781 |
260 |
(void)write(progress, ".", 1); |
782 |
1183 |
VTIM_sleep(.25); |
783 |
|
} |
784 |
40 |
return (vsm_diag(vd, "Attach interrupted")); |
785 |
85080 |
} |
786 |
|
|
787 |
|
/*--------------------------------------------------------------------*/ |
788 |
|
|
789 |
|
static struct vsm_seg * |
790 |
4776190 |
vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf) |
791 |
|
{ |
792 |
|
struct vsm_set *vs; |
793 |
|
struct vsm_seg *vg; |
794 |
|
uint64_t x; |
795 |
|
|
796 |
4776190 |
x = VSM_PRIV_HIGH(vf->priv); |
797 |
4776190 |
if (x == vd->serial) { |
798 |
4723795 |
vg = (struct vsm_seg *)vf->priv2; |
799 |
4723795 |
if (!VALID_OBJ(vg, VSM_SEG_MAGIC) || |
800 |
4723795 |
vg->serial != VSM_PRIV_LOW(vf->priv)) |
801 |
0 |
WRONG("Corrupt fantom"); |
802 |
4723795 |
return (vg); |
803 |
|
} |
804 |
|
|
805 |
52395 |
x = VSM_PRIV_LOW(vf->priv); |
806 |
52395 |
vs = vd->mgt; |
807 |
352230 |
VTAILQ_FOREACH(vg, &vs->segs, list) |
808 |
302475 |
if (vg->serial == x) |
809 |
2640 |
break; |
810 |
52395 |
if (vg == NULL) { |
811 |
49755 |
VTAILQ_FOREACH(vg, &vs->stale, list) |
812 |
0 |
if (vg->serial == x) |
813 |
0 |
break; |
814 |
49755 |
} |
815 |
52395 |
vs = vd->child; |
816 |
52395 |
if (vg == NULL) { |
817 |
435492 |
VTAILQ_FOREACH(vg, &vs->segs, list) |
818 |
429582 |
if (vg->serial == x) |
819 |
43845 |
break; |
820 |
49755 |
} |
821 |
52395 |
if (vg == NULL) { |
822 |
5910 |
VTAILQ_FOREACH(vg, &vs->stale, list) |
823 |
5910 |
if (vg->serial == x) |
824 |
5910 |
break; |
825 |
5910 |
} |
826 |
52395 |
if (vg == NULL) |
827 |
0 |
return (NULL); |
828 |
|
|
829 |
|
/* Update the fantom with the new priv so that lookups will be |
830 |
|
* fast on the next call. Note that this casts away the const. */ |
831 |
52395 |
((struct vsm_fantom *)TRUST_ME(vf))->priv = |
832 |
52395 |
VSM_PRIV_MERGE(vg->serial, vd->serial); |
833 |
52395 |
return (vg); |
834 |
4776190 |
} |
835 |
|
|
836 |
|
/*--------------------------------------------------------------------*/ |
837 |
|
|
838 |
|
void |
839 |
252889 |
VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf) |
840 |
|
{ |
841 |
|
|
842 |
252889 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
843 |
252889 |
AN(vf); |
844 |
|
|
845 |
252889 |
AN(vd->attached); |
846 |
252889 |
memset(vf, 0, sizeof *vf); |
847 |
252889 |
} |
848 |
|
|
849 |
|
int |
850 |
2639719 |
VSM__itern(struct vsm *vd, struct vsm_fantom *vf) |
851 |
|
{ |
852 |
|
struct vsm_seg *vg; |
853 |
|
|
854 |
2639719 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
855 |
2639719 |
AN(vd->attached); |
856 |
2639719 |
AN(vf); |
857 |
|
|
858 |
2639719 |
if (vf->priv == 0) { |
859 |
252889 |
vg = VTAILQ_FIRST(&vd->mgt->segs); |
860 |
252889 |
if (vg == NULL) |
861 |
0 |
return (0); |
862 |
252889 |
} else { |
863 |
2386830 |
vg = vsm_findseg(vd, vf); |
864 |
2386830 |
if (vg == NULL) |
865 |
0 |
return (vsm_diag(vd, "VSM_FOREACH: inconsistency")); |
866 |
2410653 |
while (1) { |
867 |
2410653 |
if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL) |
868 |
249089 |
vg = VTAILQ_FIRST(&vd->child->segs); |
869 |
|
else |
870 |
2161564 |
vg = VTAILQ_NEXT(vg, list); |
871 |
2410653 |
if (vg == NULL) |
872 |
167165 |
return (0); |
873 |
2243488 |
if (!(vg->flags & VSM_FLAG_CLUSTER)) |
874 |
2219665 |
break; |
875 |
|
} |
876 |
|
} |
877 |
2472554 |
memset(vf, 0, sizeof *vf); |
878 |
2472554 |
vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial); |
879 |
2472554 |
vf->priv2 = (uintptr_t)vg; |
880 |
2472554 |
vf->category = vg->av[4]; |
881 |
2472554 |
vf->ident = vg->av[5]; |
882 |
2472554 |
AN(vf->category); |
883 |
2472554 |
return (1); |
884 |
2639719 |
} |
885 |
|
|
886 |
|
/*--------------------------------------------------------------------*/ |
887 |
|
|
888 |
|
int |
889 |
204542 |
VSM_Map(struct vsm *vd, struct vsm_fantom *vf) |
890 |
|
{ |
891 |
|
struct vsm_seg *vg, *vgc; |
892 |
|
size_t of, sz; |
893 |
|
int r; |
894 |
|
|
895 |
204542 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
896 |
204542 |
AN(vd->attached); |
897 |
204542 |
AN(vf); |
898 |
204542 |
vg = vsm_findseg(vd, vf); |
899 |
204542 |
if (vg == NULL) |
900 |
0 |
return (vsm_diag(vd, "VSM_Map: bad fantom")); |
901 |
|
|
902 |
204542 |
assert(vg->serial == VSM_PRIV_LOW(vf->priv)); |
903 |
204542 |
assert(vg->av[4] == vf->category); |
904 |
204542 |
assert(vg->av[5] == vf->ident); |
905 |
|
|
906 |
204542 |
if (vg->b != NULL) { |
907 |
0 |
assert(vg->refs > 0); |
908 |
0 |
AN(vg->e); |
909 |
0 |
vf->b = vg->b; |
910 |
0 |
vf->e = vg->e; |
911 |
0 |
vg->refs++; |
912 |
0 |
return (0); |
913 |
|
} |
914 |
|
|
915 |
204542 |
assert(vg->refs == 0); |
916 |
|
|
917 |
204542 |
vgc = vg->cluster; |
918 |
|
|
919 |
204542 |
if (vgc == NULL) { |
920 |
197541 |
r = vsm_mapseg(vd, vg); |
921 |
197541 |
if (r) |
922 |
0 |
return (r); |
923 |
197541 |
vf->b = vg->b; |
924 |
197541 |
vf->e = vg->e; |
925 |
|
|
926 |
197541 |
vg->refs++; |
927 |
|
|
928 |
197541 |
return (0); |
929 |
|
} |
930 |
|
|
931 |
7001 |
CHECK_OBJ_NOTNULL(vgc, VSM_SEG_MAGIC); |
932 |
7001 |
assert(vgc->flags & VSM_FLAG_CLUSTER); |
933 |
7001 |
assert(vg->s == NULL); |
934 |
7001 |
assert(vg->sz == 0); |
935 |
|
|
936 |
7001 |
r = vsm_mapseg(vd, vgc); |
937 |
7001 |
if (r) |
938 |
0 |
return (r); |
939 |
7001 |
vgc->refs++; |
940 |
|
|
941 |
7001 |
of = strtoul(vg->av[2], NULL, 10); |
942 |
7001 |
sz = strtoul(vg->av[3], NULL, 10); |
943 |
7001 |
assert(sz > 0); |
944 |
|
|
945 |
7001 |
assert(vgc->sz >= of + sz); |
946 |
7001 |
assert(vgc->s == vgc->b); |
947 |
7001 |
vg->b = (char *)vgc->b + of; |
948 |
7001 |
vg->e = (char *)vg->b + sz; |
949 |
|
|
950 |
7001 |
vf->b = vg->b; |
951 |
7001 |
vf->e = vg->e; |
952 |
|
|
953 |
7001 |
vg->refs++; |
954 |
|
|
955 |
7001 |
return (0); |
956 |
204542 |
} |
957 |
|
|
958 |
|
/*--------------------------------------------------------------------*/ |
959 |
|
|
960 |
|
int |
961 |
177782 |
VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf) |
962 |
|
{ |
963 |
|
struct vsm_seg *vg; |
964 |
|
|
965 |
177782 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
966 |
177782 |
AN(vd->attached); |
967 |
177782 |
AN(vf); |
968 |
177782 |
AN(vf->b); |
969 |
177782 |
vg = vsm_findseg(vd, vf); |
970 |
177782 |
if (vg == NULL) |
971 |
0 |
return (vsm_diag(vd, "VSM_Unmap: bad fantom")); |
972 |
177782 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
973 |
177782 |
assert(vg->refs > 0); |
974 |
177782 |
vg->refs--; |
975 |
177782 |
vf->b = NULL; |
976 |
177782 |
vf->e = NULL; |
977 |
177782 |
if (vg->refs > 0) |
978 |
0 |
return (0); |
979 |
|
|
980 |
177782 |
if (vg->cluster) { |
981 |
2441 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
982 |
2441 |
assert(vg->s == NULL); |
983 |
2441 |
assert(vg->sz == 0); |
984 |
2441 |
assert(vg->cluster->refs > 0); |
985 |
2441 |
if (--vg->cluster->refs == 0) { |
986 |
2401 |
vsm_unmapseg(vg->cluster); |
987 |
2401 |
if (vg->cluster->flags & VSM_FLAG_STALE) { |
988 |
0 |
AN(vg->flags & VSM_FLAG_STALE); |
989 |
0 |
vsm_delseg(vg->cluster, 0); |
990 |
0 |
} |
991 |
2401 |
} |
992 |
2441 |
vg->b = vg->e = NULL; |
993 |
2441 |
} else { |
994 |
175341 |
vsm_unmapseg(vg); |
995 |
|
} |
996 |
177782 |
if (vg->flags & VSM_FLAG_STALE) |
997 |
5912 |
vsm_delseg(vg, 0); |
998 |
177782 |
return (0); |
999 |
177782 |
} |
1000 |
|
|
1001 |
|
/*--------------------------------------------------------------------*/ |
1002 |
|
|
1003 |
|
const struct vsm_valid * |
1004 |
2007083 |
VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf) |
1005 |
|
{ |
1006 |
|
struct vsm_seg *vg; |
1007 |
|
|
1008 |
2007083 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
1009 |
2007083 |
AN(vf); |
1010 |
2007083 |
vg = vsm_findseg(vd, vf); |
1011 |
2007083 |
if (vg == NULL || vg->flags & VSM_FLAG_STALE) |
1012 |
2177 |
return (VSM_invalid); |
1013 |
2004908 |
return (VSM_valid); |
1014 |
2007085 |
} |
1015 |
|
|
1016 |
|
/*--------------------------------------------------------------------*/ |
1017 |
|
|
1018 |
|
int |
1019 |
202910 |
VSM_Get(struct vsm *vd, struct vsm_fantom *vf, |
1020 |
|
const char *category, const char *ident) |
1021 |
|
{ |
1022 |
|
|
1023 |
202910 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
1024 |
202910 |
AN(vd->attached); |
1025 |
1416278 |
VSM_FOREACH(vf, vd) { |
1026 |
1262138 |
if (strcmp(vf->category, category)) |
1027 |
1213368 |
continue; |
1028 |
48770 |
if (ident != NULL && strcmp(vf->ident, ident)) |
1029 |
0 |
continue; |
1030 |
48770 |
return (1); |
1031 |
|
} |
1032 |
154140 |
memset(vf, 0, sizeof *vf); |
1033 |
154140 |
return (0); |
1034 |
202910 |
} |
1035 |
|
|
1036 |
|
/*--------------------------------------------------------------------*/ |
1037 |
|
|
1038 |
|
char * |
1039 |
3600 |
VSM_Dup(struct vsm *vd, const char *category, const char *ident) |
1040 |
|
{ |
1041 |
|
struct vsm_fantom vf; |
1042 |
3600 |
char *p = NULL; |
1043 |
|
|
1044 |
3600 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
1045 |
3600 |
AN(vd->attached); |
1046 |
14520 |
VSM_FOREACH(&vf, vd) { |
1047 |
14520 |
if (strcmp(vf.category, category)) |
1048 |
6240 |
continue; |
1049 |
8280 |
if (ident != NULL && strcmp(vf.ident, ident)) |
1050 |
4680 |
continue; |
1051 |
3600 |
AZ(VSM_Map(vd, &vf)); |
1052 |
3600 |
AN(vf.b); |
1053 |
3600 |
AN(vf.e); |
1054 |
3600 |
p = malloc((char*)vf.e - (char*)vf.b); |
1055 |
3600 |
AN(p); |
1056 |
3600 |
memcpy(p, vf.b, (char*)vf.e - (char*)vf.b); |
1057 |
3600 |
AZ(VSM_Unmap(vd, &vf)); |
1058 |
3600 |
break; |
1059 |
|
} |
1060 |
3600 |
return (p); |
1061 |
|
} |