| | 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 |
1827 |
vsm_diag(struct vsm *vd, const char *fmt, ...) |
| 161 |
|
{ |
| 162 |
|
va_list ap; |
| 163 |
|
|
| 164 |
1827 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 165 |
1827 |
AN(fmt); |
| 166 |
|
|
| 167 |
1827 |
if (vd->diag == NULL) |
| 168 |
1826 |
vd->diag = VSB_new_auto(); |
| 169 |
1827 |
AN(vd->diag); |
| 170 |
1827 |
VSB_clear(vd->diag); |
| 171 |
1827 |
va_start(ap, fmt); |
| 172 |
1827 |
VSB_vprintf(vd->diag, fmt, ap); |
| 173 |
1827 |
va_end(ap); |
| 174 |
1827 |
AZ(VSB_finish(vd->diag)); |
| 175 |
1827 |
return (-1); |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
/*--------------------------------------------------------------------*/ |
| 179 |
|
|
| 180 |
|
static int |
| 181 |
266167 |
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 |
266167 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 189 |
|
|
| 190 |
266167 |
if (vg->s != NULL) |
| 191 |
4040 |
return (0); |
| 192 |
|
|
| 193 |
262127 |
ps = getpagesize(); |
| 194 |
|
|
| 195 |
262127 |
of = strtoul(vg->av[2], NULL, 10); |
| 196 |
262127 |
off = RDN2(of, ps); |
| 197 |
|
|
| 198 |
262127 |
if (vg->flags & VSM_FLAG_CLUSTER) |
| 199 |
1732 |
assert(of == 0); |
| 200 |
262127 |
assert(vg->cluster == NULL); |
| 201 |
|
|
| 202 |
262127 |
sz = strtoul(vg->av[3], NULL, 10); |
| 203 |
262127 |
assert(sz > 0); |
| 204 |
262127 |
assert(of >= off); |
| 205 |
262127 |
len = RUP2((of - off) + sz, ps); |
| 206 |
|
|
| 207 |
262127 |
vsb = VSB_new_auto(); |
| 208 |
262127 |
AN(vsb); |
| 209 |
262127 |
VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]); |
| 210 |
262127 |
AZ(VSB_finish(vsb)); |
| 211 |
|
|
| 212 |
262127 |
fd = open(VSB_data(vsb), O_RDONLY); // XXX: openat |
| 213 |
262127 |
if (fd < 0) { |
| 214 |
22 |
VSB_destroy(&vsb); |
| 215 |
22 |
return (vsm_diag(vd, "Could not open segment")); |
| 216 |
|
} |
| 217 |
|
|
| 218 |
524210 |
s = (void*)mmap(NULL, len, |
| 219 |
|
PROT_READ, |
| 220 |
|
MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED, |
| 221 |
262105 |
fd, (off_t)off); |
| 222 |
|
|
| 223 |
262105 |
VSB_destroy(&vsb); |
| 224 |
|
|
| 225 |
262105 |
closefd(&fd); |
| 226 |
262105 |
if (s == MAP_FAILED) |
| 227 |
0 |
return (vsm_diag(vd, "Could not mmap segment")); |
| 228 |
|
|
| 229 |
262105 |
vg->s = s; |
| 230 |
262105 |
vg->b = (char*)(vg->s) + of - off; |
| 231 |
262105 |
vg->e = (char *)vg->b + sz; |
| 232 |
262105 |
vg->sz = len; |
| 233 |
|
|
| 234 |
262105 |
return (0); |
| 235 |
266167 |
} |
| 236 |
|
|
| 237 |
|
static void |
| 238 |
233305 |
vsm_unmapseg(struct vsm_seg *vg) |
| 239 |
|
{ |
| 240 |
|
|
| 241 |
233305 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 242 |
|
|
| 243 |
233305 |
AN(vg->b); |
| 244 |
233305 |
AN(vg->e); |
| 245 |
233305 |
AZ(munmap(vg->s, vg->sz)); |
| 246 |
233305 |
vg->s = vg->b = vg->e = NULL; |
| 247 |
233305 |
vg->sz = 0; |
| 248 |
233305 |
} |
| 249 |
|
|
| 250 |
|
/*--------------------------------------------------------------------*/ |
| 251 |
|
|
| 252 |
|
static void |
| 253 |
3377424 |
vsm_delseg(struct vsm_seg *vg, int refsok) |
| 254 |
|
{ |
| 255 |
|
|
| 256 |
3377424 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 257 |
|
|
| 258 |
3377424 |
if (vg->set->vg == vg) { |
| 259 |
202488 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 260 |
202488 |
vg->set->vg = VTAILQ_NEXT(vg, list); |
| 261 |
202488 |
} |
| 262 |
|
|
| 263 |
3377424 |
if (refsok && vg->refs) { |
| 264 |
5789 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 265 |
5789 |
vg->flags |= VSM_FLAG_STALE; |
| 266 |
5789 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
| 267 |
5789 |
VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list); |
| 268 |
5789 |
return; |
| 269 |
|
} |
| 270 |
|
|
| 271 |
3371635 |
if (vg->s != NULL) |
| 272 |
0 |
vsm_unmapseg(vg); |
| 273 |
|
|
| 274 |
3371635 |
if (vg->flags & VSM_FLAG_CLUSTER) { |
| 275 |
78880 |
vg->flags &= ~VSM_FLAG_CLUSTER; |
| 276 |
78880 |
VTAILQ_REMOVE(&vg->set->clusters, vg, clist); |
| 277 |
78880 |
} |
| 278 |
|
|
| 279 |
3371635 |
if (vg->flags & VSM_FLAG_STALE) |
| 280 |
5789 |
VTAILQ_REMOVE(&vg->set->stale, vg, list); |
| 281 |
|
else |
| 282 |
3365846 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
| 283 |
3371635 |
VAV_Free(vg->av); |
| 284 |
3371635 |
FREE_OBJ(vg); |
| 285 |
3377424 |
} |
| 286 |
|
|
| 287 |
|
/*--------------------------------------------------------------------*/ |
| 288 |
|
|
| 289 |
|
static struct vsm_set * |
| 290 |
184464 |
vsm_newset(const char *dirname) |
| 291 |
|
{ |
| 292 |
|
struct vsm_set *vs; |
| 293 |
|
|
| 294 |
184464 |
ALLOC_OBJ(vs, VSM_SET_MAGIC); |
| 295 |
184464 |
AN(vs); |
| 296 |
184464 |
VTAILQ_INIT(&vs->segs); |
| 297 |
184464 |
VTAILQ_INIT(&vs->stale); |
| 298 |
184464 |
VTAILQ_INIT(&vs->clusters); |
| 299 |
184464 |
vs->dname = dirname; |
| 300 |
184464 |
vs->dfd = vs->fd = -1; |
| 301 |
184464 |
vs->vlu = VLU_New(vsm_vlu_func, vs, 0); |
| 302 |
184464 |
AN(vs->vlu); |
| 303 |
184464 |
if (getenv("VSM_NOPID") != NULL) |
| 304 |
0 |
vs->couldkill = -1; |
| 305 |
184464 |
return (vs); |
| 306 |
|
} |
| 307 |
|
|
| 308 |
|
static void |
| 309 |
182224 |
vsm_delset(struct vsm_set **p) |
| 310 |
|
{ |
| 311 |
|
struct vsm_set *vs; |
| 312 |
|
struct vsm_seg *vg; |
| 313 |
|
|
| 314 |
182224 |
TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC); |
| 315 |
|
|
| 316 |
182224 |
if (vs->fd >= 0) |
| 317 |
111950 |
closefd(&vs->fd); |
| 318 |
182224 |
if (vs->dfd >= 0) |
| 319 |
150112 |
closefd(&vs->dfd); |
| 320 |
182224 |
while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) { |
| 321 |
0 |
AN(vg->flags & VSM_FLAG_STALE); |
| 322 |
0 |
vsm_delseg(vg, 0); |
| 323 |
|
} |
| 324 |
3364145 |
while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) { |
| 325 |
3181921 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 326 |
3181921 |
vsm_delseg(vg, 0); |
| 327 |
|
} |
| 328 |
182224 |
assert(VTAILQ_EMPTY(&vs->clusters)); |
| 329 |
182224 |
VLU_Destroy(&vs->vlu); |
| 330 |
182224 |
FREE_OBJ(vs); |
| 331 |
182224 |
} |
| 332 |
|
|
| 333 |
|
static void |
| 334 |
417714 |
vsm_wash_set(const struct vsm_set *vs, int all) |
| 335 |
|
{ |
| 336 |
|
struct vsm_seg *vg, *vg2; |
| 337 |
|
|
| 338 |
3735847 |
VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) { |
| 339 |
3318133 |
if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0) |
| 340 |
112832 |
vsm_delseg(vg, 1); |
| 341 |
3318133 |
} |
| 342 |
417714 |
} |
| 343 |
|
|
| 344 |
|
/*--------------------------------------------------------------------*/ |
| 345 |
|
|
| 346 |
|
struct vsm * |
| 347 |
92232 |
VSM_New(void) |
| 348 |
|
{ |
| 349 |
|
struct vsm *vd; |
| 350 |
|
|
| 351 |
92232 |
ALLOC_OBJ(vd, VSM_MAGIC); |
| 352 |
92232 |
AN(vd); |
| 353 |
|
|
| 354 |
92232 |
vd->mgt = vsm_newset(VSM_MGT_DIRNAME); |
| 355 |
92232 |
vd->mgt->flag_running = VSM_MGT_RUNNING; |
| 356 |
92232 |
vd->mgt->flag_changed = VSM_MGT_CHANGED; |
| 357 |
92232 |
vd->mgt->flag_restarted = VSM_MGT_RESTARTED; |
| 358 |
|
|
| 359 |
92232 |
vd->child = vsm_newset(VSM_CHILD_DIRNAME); |
| 360 |
92232 |
vd->child->flag_running = VSM_WRK_RUNNING; |
| 361 |
92232 |
vd->child->flag_changed = VSM_WRK_CHANGED; |
| 362 |
92232 |
vd->child->flag_restarted = VSM_WRK_RESTARTED; |
| 363 |
|
|
| 364 |
92232 |
vd->mgt->vsm = vd; |
| 365 |
92232 |
vd->child->vsm = vd; |
| 366 |
92232 |
vd->wdfd = -1; |
| 367 |
92232 |
vd->patience = 5; |
| 368 |
92232 |
return (vd); |
| 369 |
|
} |
| 370 |
|
|
| 371 |
|
/*--------------------------------------------------------------------*/ |
| 372 |
|
|
| 373 |
|
int |
| 374 |
93992 |
VSM_Arg(struct vsm *vd, char flag, const char *arg) |
| 375 |
|
{ |
| 376 |
93992 |
char *p = NULL; |
| 377 |
|
|
| 378 |
93992 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 379 |
|
|
| 380 |
93992 |
if (arg == NULL) |
| 381 |
1640 |
return (1); |
| 382 |
92352 |
switch (flag) { |
| 383 |
|
case 't': |
| 384 |
520 |
if (!strcasecmp(arg, "off")) { |
| 385 |
0 |
vd->patience = -1; |
| 386 |
0 |
} else { |
| 387 |
520 |
vd->patience = strtod(arg, &p); |
| 388 |
840 |
if ((p != NULL && *p != '\0') || |
| 389 |
320 |
!isfinite(vd->patience) || vd->patience < 0) |
| 390 |
560 |
return (vsm_diag(vd, |
| 391 |
280 |
"-t: Invalid argument: %s", arg)); |
| 392 |
|
} |
| 393 |
240 |
break; |
| 394 |
|
case 'n': |
| 395 |
91832 |
if (vd->wdname != NULL) |
| 396 |
0 |
free(vd->wdname); |
| 397 |
91832 |
vd->wdname = VIN_n_Arg(arg); |
| 398 |
91832 |
if (vd->wdname == NULL) |
| 399 |
0 |
return (vsm_diag(vd, "Invalid instance name: %s", |
| 400 |
0 |
strerror(errno))); |
| 401 |
91832 |
break; |
| 402 |
|
default: |
| 403 |
0 |
return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag)); |
| 404 |
|
} |
| 405 |
92072 |
return (1); |
| 406 |
93992 |
} |
| 407 |
|
|
| 408 |
|
/*--------------------------------------------------------------------*/ |
| 409 |
|
|
| 410 |
|
void |
| 411 |
91112 |
VSM_Destroy(struct vsm **vdp) |
| 412 |
|
{ |
| 413 |
|
struct vsm *vd; |
| 414 |
|
|
| 415 |
91112 |
TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC); |
| 416 |
|
|
| 417 |
91112 |
VSM_ResetError(vd); |
| 418 |
91112 |
REPLACE(vd->wdname, NULL); |
| 419 |
91112 |
if (vd->diag != NULL) |
| 420 |
0 |
VSB_destroy(&vd->diag); |
| 421 |
91112 |
if (vd->wdfd >= 0) |
| 422 |
91032 |
closefd(&vd->wdfd); |
| 423 |
91112 |
vsm_delset(&vd->mgt); |
| 424 |
91112 |
vsm_delset(&vd->child); |
| 425 |
91112 |
FREE_OBJ(vd); |
| 426 |
91112 |
} |
| 427 |
|
|
| 428 |
|
/*--------------------------------------------------------------------*/ |
| 429 |
|
|
| 430 |
|
const char * |
| 431 |
462 |
VSM_Error(const struct vsm *vd) |
| 432 |
|
{ |
| 433 |
|
|
| 434 |
462 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 435 |
|
|
| 436 |
462 |
if (vd->diag == NULL) |
| 437 |
0 |
return ("No VSM error"); |
| 438 |
|
else |
| 439 |
462 |
return (VSB_data(vd->diag)); |
| 440 |
462 |
} |
| 441 |
|
|
| 442 |
|
/*--------------------------------------------------------------------*/ |
| 443 |
|
|
| 444 |
|
void |
| 445 |
184229 |
VSM_ResetError(struct vsm *vd) |
| 446 |
|
{ |
| 447 |
|
|
| 448 |
184229 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 449 |
|
|
| 450 |
184229 |
if (vd->diag == NULL) |
| 451 |
182763 |
return; |
| 452 |
1466 |
VSB_destroy(&vd->diag); |
| 453 |
184229 |
} |
| 454 |
|
|
| 455 |
|
/*-------------------------------------------------------------------- |
| 456 |
|
*/ |
| 457 |
|
|
| 458 |
|
static int |
| 459 |
691344 |
vsm_cmp_av(char * const *a1, char * const *a2) |
| 460 |
|
{ |
| 461 |
|
|
| 462 |
1010336 |
while (1) { |
| 463 |
1010336 |
if (*a1 == NULL && *a2 == NULL) |
| 464 |
78915 |
return (0); |
| 465 |
931421 |
if (*a1 == NULL || *a2 == NULL) |
| 466 |
4 |
return (1); |
| 467 |
931417 |
if (strcmp(*a1, *a2)) |
| 468 |
612425 |
return (1); |
| 469 |
318992 |
a1++; |
| 470 |
318992 |
a2++; |
| 471 |
|
} |
| 472 |
691340 |
} |
| 473 |
|
|
| 474 |
|
static struct vsm_seg * |
| 475 |
84794 |
vsm_findcluster(const struct vsm_set *vs, const char *cnam) |
| 476 |
|
{ |
| 477 |
|
struct vsm_seg *vg; |
| 478 |
84794 |
AN(vs); |
| 479 |
84794 |
AN(cnam); |
| 480 |
120501 |
VTAILQ_FOREACH(vg, &vs->clusters, clist) { |
| 481 |
120501 |
AN(vg->av[1]); |
| 482 |
120501 |
if (!strcmp(cnam, vg->av[1])) |
| 483 |
84794 |
return (vg); |
| 484 |
35707 |
} |
| 485 |
0 |
return (NULL); |
| 486 |
84794 |
} |
| 487 |
|
|
| 488 |
|
static unsigned |
| 489 |
3023509 |
vsm_running(struct vsm_set *vs, pid_t pid) |
| 490 |
|
{ |
| 491 |
|
|
| 492 |
3023509 |
AN(vs); |
| 493 |
|
|
| 494 |
3023509 |
if (pid == 0) |
| 495 |
0 |
return (0); |
| 496 |
|
|
| 497 |
3023509 |
if (kill(pid, 0) == 0) { |
| 498 |
2938241 |
vs->couldkill = 1; |
| 499 |
2938241 |
return (1); |
| 500 |
|
} |
| 501 |
85268 |
if (errno == EPERM) /* a process exists, assume running */ |
| 502 |
0 |
return (1); |
| 503 |
85268 |
assert(errno != EINVAL); |
| 504 |
85268 |
return (0); |
| 505 |
3023509 |
} |
| 506 |
|
|
| 507 |
|
static int |
| 508 |
153904 |
vsm_vlu_hash(struct vsm_set *vs, const char *line) |
| 509 |
|
{ |
| 510 |
|
int i; |
| 511 |
|
uintmax_t id1, id2; |
| 512 |
|
|
| 513 |
153904 |
i = sscanf(line, "# %ju %ju", &id1, &id2); |
| 514 |
153904 |
if (i != 2) { |
| 515 |
0 |
vs->retval |= vs->flag_restarted; |
| 516 |
0 |
return (0); |
| 517 |
|
} |
| 518 |
153904 |
if (vs->couldkill >= 0 && vsm_running(vs, id1)) { |
| 519 |
|
/* nothing to do */ |
| 520 |
153904 |
} else if (vs->couldkill > 0 && errno == ESRCH) { |
| 521 |
0 |
vs->retval |= vs->flag_restarted | VSM_MGT_CHANGED; |
| 522 |
0 |
return (0); |
| 523 |
|
} |
| 524 |
153904 |
vs->retval |= VSM_MGT_RUNNING; |
| 525 |
153904 |
if (id1 != vs->id1 || id2 != vs->id2) { |
| 526 |
153832 |
vs->retval |= vs->flag_restarted; |
| 527 |
153832 |
vs->id1 = id1; |
| 528 |
153832 |
vs->id2 = id2; |
| 529 |
153832 |
} |
| 530 |
153904 |
return (0); |
| 531 |
153904 |
} |
| 532 |
|
|
| 533 |
|
static int |
| 534 |
3410908 |
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 |
3410908 |
av = VAV_Parse(line + 1, &ac, 0); |
| 541 |
|
|
| 542 |
3410908 |
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 |
3410908 |
vg = vs->vg; |
| 550 |
3410908 |
CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC); |
| 551 |
3410908 |
if (vg != NULL) |
| 552 |
2032 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 553 |
3410908 |
while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1])) |
| 554 |
0 |
vg = VTAILQ_NEXT(vg, list); |
| 555 |
3410908 |
if (vg != NULL) { |
| 556 |
|
/* entry compared equal, so it survives */ |
| 557 |
2032 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 558 |
2032 |
VAV_Free(av); |
| 559 |
2032 |
vg->flags |= VSM_FLAG_MARKSCAN; |
| 560 |
2032 |
vs->vg = VTAILQ_NEXT(vg, list); |
| 561 |
2032 |
} else { |
| 562 |
3408876 |
ALLOC_OBJ(vg, VSM_SEG_MAGIC); |
| 563 |
3408876 |
AN(vg); |
| 564 |
3408876 |
vg->av = av; |
| 565 |
3408876 |
vg->set = vs; |
| 566 |
3408876 |
vg->flags = VSM_FLAG_MARKSCAN; |
| 567 |
3408876 |
vg->serial = vd->serial; |
| 568 |
|
|
| 569 |
3408876 |
VTAILQ_INSERT_TAIL(&vs->segs, vg, list); |
| 570 |
3408876 |
if (ac == 4) { |
| 571 |
79520 |
vg->flags |= VSM_FLAG_CLUSTER; |
| 572 |
79520 |
VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist); |
| 573 |
3408876 |
} else if (*vg->av[2] != '0') { |
| 574 |
82720 |
vg->cluster = vsm_findcluster(vs, vg->av[1]); |
| 575 |
82720 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
| 576 |
82720 |
} |
| 577 |
3408876 |
vs->retval |= vs->flag_changed; |
| 578 |
|
} |
| 579 |
3410908 |
return (0); |
| 580 |
3410908 |
} |
| 581 |
|
|
| 582 |
|
static int |
| 583 |
76883 |
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 |
76883 |
av = VAV_Parse(line + 1, &ac, 0); |
| 590 |
|
|
| 591 |
76883 |
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 |
76883 |
if (*av[2] != '0') |
| 600 |
2074 |
vg = vsm_findcluster(vs, av[1]); |
| 601 |
|
else |
| 602 |
74809 |
vg = VTAILQ_FIRST(&vs->segs); |
| 603 |
|
|
| 604 |
689308 |
for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) { |
| 605 |
689308 |
if (!vsm_cmp_av(&vg->av[1], &av[1])) { |
| 606 |
76883 |
vs->retval |= vs->flag_changed; |
| 607 |
76883 |
vsm_delseg(vg, 1); |
| 608 |
76883 |
break; |
| 609 |
|
} |
| 610 |
612425 |
} |
| 611 |
76883 |
AN(vg); |
| 612 |
76883 |
VAV_Free(av); |
| 613 |
76883 |
return (0); |
| 614 |
76883 |
} |
| 615 |
|
|
| 616 |
|
static int v_matchproto_(vlu_f) |
| 617 |
3641694 |
vsm_vlu_func(void *priv, const char *line) |
| 618 |
|
{ |
| 619 |
|
struct vsm *vd; |
| 620 |
|
struct vsm_set *vs; |
| 621 |
3641694 |
int i = 0; |
| 622 |
|
|
| 623 |
3641694 |
CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC); |
| 624 |
3641694 |
vd = vs->vsm; |
| 625 |
3641694 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 626 |
3641694 |
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 |
3641694 |
vd->serial = VSM_PRIV_LOW(vd->serial + 1); |
| 632 |
|
|
| 633 |
3641694 |
switch (line[0]) { |
| 634 |
|
case '#': |
| 635 |
153904 |
i = vsm_vlu_hash(vs, line); |
| 636 |
268723 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
| 637 |
114819 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
| 638 |
153904 |
if (!(vs->retval & vs->flag_restarted)) |
| 639 |
72 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
| 640 |
153904 |
break; |
| 641 |
|
case '+': |
| 642 |
3410908 |
i = vsm_vlu_plus(vd, vs, line); |
| 643 |
3410908 |
break; |
| 644 |
|
case '-': |
| 645 |
76882 |
i = vsm_vlu_minus(vd, vs, line); |
| 646 |
76882 |
break; |
| 647 |
|
default: |
| 648 |
0 |
break; |
| 649 |
|
} |
| 650 |
3641694 |
return (i); |
| 651 |
|
} |
| 652 |
|
|
| 653 |
|
static void |
| 654 |
2869587 |
vsm_readlines(struct vsm_set *vs) |
| 655 |
|
{ |
| 656 |
|
int i; |
| 657 |
|
|
| 658 |
2869587 |
do { |
| 659 |
3242181 |
assert(vs->fd >= 0); |
| 660 |
3242181 |
i = VLU_Fd(vs->vlu, vs->fd); |
| 661 |
3242181 |
} while (!i); |
| 662 |
2869587 |
assert(i == -2); |
| 663 |
2869587 |
} |
| 664 |
|
|
| 665 |
|
static unsigned |
| 666 |
3205696 |
vsm_refresh_set(struct vsm *vd, struct vsm_set *vs) |
| 667 |
|
{ |
| 668 |
3205696 |
unsigned restarted = 0; |
| 669 |
|
struct stat st; |
| 670 |
|
|
| 671 |
3205696 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 672 |
3205696 |
CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC); |
| 673 |
3205696 |
vs->retval = 0; |
| 674 |
5993686 |
if (vs->dfd >= 0 && ( |
| 675 |
2790415 |
fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) || |
| 676 |
2790447 |
st.st_ino != vs->dst.st_ino || |
| 677 |
2787994 |
st.st_dev != vs->dst.st_dev || |
| 678 |
2787991 |
st.st_mode != vs->dst.st_mode || |
| 679 |
2787990 |
st.st_nlink == 0)) { |
| 680 |
2554 |
closefd(&vs->dfd); |
| 681 |
2440 |
restarted = vs->flag_restarted; |
| 682 |
2440 |
} |
| 683 |
|
|
| 684 |
3205654 |
if (vs->dfd < 0) { |
| 685 |
417642 |
if (vs->fd >= 0) |
| 686 |
2434 |
closefd(&vs->fd); |
| 687 |
417642 |
vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY); |
| 688 |
417642 |
} |
| 689 |
|
|
| 690 |
3205654 |
if (vs->dfd < 0) { |
| 691 |
263810 |
vs->id1 = vs->id2 = 0; |
| 692 |
263810 |
vsm_wash_set(vs, 1); |
| 693 |
263810 |
return (vs->retval | restarted); |
| 694 |
|
} |
| 695 |
|
|
| 696 |
2941844 |
AZ(fstat(vs->dfd, &vs->dst)); |
| 697 |
|
|
| 698 |
5657563 |
if (vs->fd >= 0 && ( |
| 699 |
2753942 |
fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) || |
| 700 |
2715800 |
st.st_ino != vs->fst.st_ino || |
| 701 |
2715727 |
st.st_dev != vs->fst.st_dev || |
| 702 |
2715728 |
st.st_mode != vs->fst.st_mode || |
| 703 |
2715721 |
st.st_size < vs->fst.st_size || |
| 704 |
2715719 |
st.st_nlink < 1)) { |
| 705 |
38250 |
closefd(&vs->fd); |
| 706 |
38240 |
vs->retval |= vs->flag_changed; |
| 707 |
38240 |
} |
| 708 |
|
|
| 709 |
2941838 |
if (vs->fd >= 0) { |
| 710 |
2715712 |
vs->vg = NULL; |
| 711 |
2715712 |
vsm_readlines(vs); |
| 712 |
2715712 |
} else { |
| 713 |
703499 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
| 714 |
477373 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
| 715 |
226126 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
| 716 |
226126 |
vs->fd = openat(vs->dfd, "_.index", O_RDONLY); |
| 717 |
226126 |
if (vs->fd < 0) |
| 718 |
72222 |
return (vs->retval | restarted); |
| 719 |
153904 |
VLU_Reset(vs->vlu); |
| 720 |
153904 |
AZ(fstat(vs->fd, &vs->fst)); |
| 721 |
153904 |
vsm_readlines(vs); |
| 722 |
153904 |
vsm_wash_set(vs, 0); |
| 723 |
|
} |
| 724 |
|
|
| 725 |
2869616 |
vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR); |
| 726 |
|
|
| 727 |
2869616 |
if (vs->couldkill < 0 || vsm_running(vs, vs->id1)) |
| 728 |
2784474 |
vs->retval |= vs->flag_running; |
| 729 |
2869620 |
return (vs->retval); |
| 730 |
3205652 |
} |
| 731 |
|
|
| 732 |
|
/*--------------------------------------------------------------------*/ |
| 733 |
|
|
| 734 |
|
unsigned |
| 735 |
1639168 |
VSM_Status(struct vsm *vd) |
| 736 |
|
{ |
| 737 |
1639168 |
unsigned retval = 0; |
| 738 |
|
struct stat st; |
| 739 |
|
|
| 740 |
1639168 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 741 |
|
|
| 742 |
|
/* See if the -n workdir changed */ |
| 743 |
1639168 |
if (vd->wdfd >= 0) { |
| 744 |
1546133 |
AZ(fstat(vd->wdfd, &st)); |
| 745 |
3092252 |
if (st.st_ino != vd->wdst.st_ino || |
| 746 |
1546120 |
st.st_dev != vd->wdst.st_dev || |
| 747 |
1546118 |
st.st_mode != vd->wdst.st_mode || |
| 748 |
1546119 |
st.st_nlink == 0) { |
| 749 |
26 |
closefd(&vd->wdfd); |
| 750 |
0 |
vsm_wash_set(vd->mgt, 1); |
| 751 |
0 |
vsm_wash_set(vd->child, 1); |
| 752 |
0 |
} |
| 753 |
1546115 |
} |
| 754 |
|
|
| 755 |
|
/* Open workdir */ |
| 756 |
1639150 |
if (vd->wdfd < 0) { |
| 757 |
93037 |
retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED; |
| 758 |
93037 |
retval |= VSM_WRK_RESTARTED | VSM_WRK_CHANGED; |
| 759 |
93037 |
vd->wdfd = open(vd->wdname, O_RDONLY); |
| 760 |
93037 |
if (vd->wdfd < 0) |
| 761 |
1365 |
(void)vsm_diag(vd, |
| 762 |
|
"VSM_Status: Cannot open workdir"); |
| 763 |
|
else |
| 764 |
91672 |
AZ(fstat(vd->wdfd, &vd->wdst)); |
| 765 |
93037 |
} |
| 766 |
|
|
| 767 |
1639150 |
if (vd->wdfd >= 0) { |
| 768 |
1637803 |
retval |= vsm_refresh_set(vd, vd->mgt); |
| 769 |
1637803 |
if (vd->mgt->couldkill > 0 && (retval & VSM_MGT_RESTARTED)) |
| 770 |
91672 |
vd->mgt->couldkill = 0; |
| 771 |
1637803 |
if (retval & VSM_MGT_RUNNING) |
| 772 |
1567850 |
retval |= vsm_refresh_set(vd, vd->child); |
| 773 |
1637799 |
if (vd->child->couldkill > 0 && (retval & VSM_WRK_RESTARTED)) |
| 774 |
62053 |
vd->child->couldkill = 0; |
| 775 |
1637799 |
} |
| 776 |
1639146 |
return (retval); |
| 777 |
|
} |
| 778 |
|
|
| 779 |
|
/*--------------------------------------------------------------------*/ |
| 780 |
|
|
| 781 |
|
int |
| 782 |
91832 |
VSM_Attach(struct vsm *vd, int progress) |
| 783 |
|
{ |
| 784 |
|
const char *def; |
| 785 |
|
double t0; |
| 786 |
|
unsigned u; |
| 787 |
91832 |
int i, n = 0; |
| 788 |
|
|
| 789 |
91832 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 790 |
|
|
| 791 |
91832 |
if (vd->patience < 0) |
| 792 |
0 |
t0 = DBL_MAX; |
| 793 |
|
else |
| 794 |
91832 |
t0 = VTIM_mono() + vd->patience; |
| 795 |
|
|
| 796 |
91832 |
if (vd->wdname == NULL) { |
| 797 |
280 |
def = getenv("VARNISH_DEFAULT_N"); |
| 798 |
280 |
if (def == NULL) |
| 799 |
0 |
def = ""; /* Use default (hostname) */ |
| 800 |
280 |
i = VSM_Arg(vd, 'n', def); |
| 801 |
280 |
if (i < 0) |
| 802 |
0 |
return (i); |
| 803 |
280 |
AN(vd->wdname); |
| 804 |
280 |
} |
| 805 |
|
|
| 806 |
91832 |
AZ(vd->attached); |
| 807 |
93157 |
while (!VSIG_int && !VSIG_term) { |
| 808 |
93117 |
u = VSM_Status(vd); |
| 809 |
93117 |
VSM_ResetError(vd); |
| 810 |
93117 |
if (u & VSM_MGT_RUNNING) { |
| 811 |
91672 |
if (progress >= 0 && n > 4) |
| 812 |
0 |
(void)write(progress, "\n", 1); |
| 813 |
91672 |
vd->attached = 1; |
| 814 |
91672 |
return (0); |
| 815 |
|
} |
| 816 |
1445 |
if (t0 < VTIM_mono()) { |
| 817 |
120 |
if (progress >= 0 && n > 4) |
| 818 |
40 |
(void)write(progress, "\n", 1); |
| 819 |
120 |
return (vsm_diag(vd, |
| 820 |
|
"Could not get hold of varnishd, is it running?")); |
| 821 |
|
} |
| 822 |
1325 |
if (progress >= 0 && !(++n % 4)) |
| 823 |
281 |
(void)write(progress, ".", 1); |
| 824 |
1325 |
VTIM_sleep(.25); |
| 825 |
|
} |
| 826 |
40 |
return (vsm_diag(vd, "Attach interrupted")); |
| 827 |
91832 |
} |
| 828 |
|
|
| 829 |
|
/*--------------------------------------------------------------------*/ |
| 830 |
|
|
| 831 |
|
static struct vsm_seg * |
| 832 |
249850 |
vsm_set_findseg(const struct vsm_set *vs, uintptr_t serial) |
| 833 |
|
{ |
| 834 |
|
struct vsm_seg *vg; |
| 835 |
|
|
| 836 |
3446428 |
VTAILQ_FOREACH(vg, &vs->segs, list) { |
| 837 |
3294747 |
if (vg->serial == serial) |
| 838 |
98169 |
return (vg); |
| 839 |
3196578 |
} |
| 840 |
299081 |
VTAILQ_FOREACH(vg, &vs->stale, list) { |
| 841 |
153109 |
if (vg->serial == serial) |
| 842 |
5709 |
return (vg); |
| 843 |
147400 |
} |
| 844 |
145972 |
return (NULL); |
| 845 |
249850 |
} |
| 846 |
|
|
| 847 |
|
static struct vsm_seg * |
| 848 |
8653568 |
vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf) |
| 849 |
|
{ |
| 850 |
|
struct vsm_seg *vg; |
| 851 |
|
uint64_t x; |
| 852 |
|
|
| 853 |
8653568 |
x = VSM_PRIV_HIGH(vf->priv); |
| 854 |
8653568 |
if (x == vd->serial) { |
| 855 |
8526978 |
vg = (struct vsm_seg *)vf->priv2; |
| 856 |
8526978 |
if (!VALID_OBJ(vg, VSM_SEG_MAGIC) || |
| 857 |
8526978 |
vg->serial != VSM_PRIV_LOW(vf->priv)) |
| 858 |
0 |
WRONG("Corrupt fantom"); |
| 859 |
8526978 |
return (vg); |
| 860 |
|
} |
| 861 |
|
|
| 862 |
126590 |
x = VSM_PRIV_LOW(vf->priv); |
| 863 |
126590 |
vg = vsm_set_findseg(vd->mgt, x); |
| 864 |
126590 |
if (vg == NULL) |
| 865 |
123260 |
vg = vsm_set_findseg(vd->child, x); |
| 866 |
126590 |
if (vg == NULL) |
| 867 |
22712 |
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 |
103878 |
((struct vsm_fantom *)TRUST_ME(vf))->priv = |
| 872 |
103878 |
VSM_PRIV_MERGE(vg->serial, vd->serial); |
| 873 |
103878 |
return (vg); |
| 874 |
8653568 |
} |
| 875 |
|
|
| 876 |
|
/*--------------------------------------------------------------------*/ |
| 877 |
|
|
| 878 |
|
void |
| 879 |
357548 |
VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf) |
| 880 |
|
{ |
| 881 |
|
|
| 882 |
357548 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 883 |
357548 |
AN(vf); |
| 884 |
|
|
| 885 |
357548 |
AN(vd->attached); |
| 886 |
357548 |
memset(vf, 0, sizeof *vf); |
| 887 |
357548 |
} |
| 888 |
|
|
| 889 |
|
int |
| 890 |
4934296 |
VSM__itern(struct vsm *vd, struct vsm_fantom *vf) |
| 891 |
|
{ |
| 892 |
|
struct vsm_seg *vg; |
| 893 |
|
|
| 894 |
4934296 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 895 |
4934296 |
AN(vd->attached); |
| 896 |
4934296 |
AN(vf); |
| 897 |
|
|
| 898 |
4934296 |
if (vf->priv == 0) { |
| 899 |
357549 |
vg = VTAILQ_FIRST(&vd->mgt->segs); |
| 900 |
357549 |
if (vg == NULL) |
| 901 |
7 |
return (0); |
| 902 |
357542 |
} else { |
| 903 |
4576747 |
vg = vsm_findseg(vd, vf); |
| 904 |
4576747 |
if (vg == NULL) |
| 905 |
0 |
return (vsm_diag(vd, "VSM_FOREACH: inconsistency")); |
| 906 |
4644378 |
while (1) { |
| 907 |
4644378 |
if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL) |
| 908 |
353737 |
vg = VTAILQ_FIRST(&vd->child->segs); |
| 909 |
|
else |
| 910 |
4290641 |
vg = VTAILQ_NEXT(vg, list); |
| 911 |
4644378 |
if (vg == NULL) |
| 912 |
243486 |
return (0); |
| 913 |
4400892 |
if (!(vg->flags & VSM_FLAG_CLUSTER)) |
| 914 |
4333261 |
break; |
| 915 |
|
} |
| 916 |
|
} |
| 917 |
4690803 |
memset(vf, 0, sizeof *vf); |
| 918 |
4690803 |
vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial); |
| 919 |
4690803 |
vf->priv2 = (uintptr_t)vg; |
| 920 |
4690803 |
vf->category = vg->av[4]; |
| 921 |
4690803 |
vf->ident = vg->av[5]; |
| 922 |
4690803 |
AN(vf->category); |
| 923 |
4690803 |
return (1); |
| 924 |
4934296 |
} |
| 925 |
|
|
| 926 |
|
/*--------------------------------------------------------------------*/ |
| 927 |
|
|
| 928 |
|
int |
| 929 |
266167 |
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 |
266167 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 936 |
266167 |
AN(vd->attached); |
| 937 |
266167 |
AN(vf); |
| 938 |
266167 |
vg = vsm_findseg(vd, vf); |
| 939 |
266167 |
if (vg == NULL) |
| 940 |
0 |
return (vsm_diag(vd, "VSM_Map: bad fantom")); |
| 941 |
|
|
| 942 |
266167 |
assert(vg->serial == VSM_PRIV_LOW(vf->priv)); |
| 943 |
266167 |
assert(vg->av[4] == vf->category); |
| 944 |
266167 |
assert(vg->av[5] == vf->ident); |
| 945 |
|
|
| 946 |
266167 |
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 |
266167 |
assert(vg->refs == 0); |
| 956 |
|
|
| 957 |
266167 |
vgc = vg->cluster; |
| 958 |
|
|
| 959 |
266167 |
if (vgc == NULL) { |
| 960 |
260395 |
r = vsm_mapseg(vd, vg); |
| 961 |
260395 |
if (r) |
| 962 |
22 |
return (r); |
| 963 |
260373 |
vf->b = vg->b; |
| 964 |
260373 |
vf->e = vg->e; |
| 965 |
|
|
| 966 |
260373 |
vg->refs++; |
| 967 |
|
|
| 968 |
260373 |
return (0); |
| 969 |
|
} |
| 970 |
|
|
| 971 |
5772 |
CHECK_OBJ_NOTNULL(vgc, VSM_SEG_MAGIC); |
| 972 |
5772 |
assert(vgc->flags & VSM_FLAG_CLUSTER); |
| 973 |
5772 |
assert(vg->s == NULL); |
| 974 |
5772 |
assert(vg->sz == 0); |
| 975 |
|
|
| 976 |
5772 |
r = vsm_mapseg(vd, vgc); |
| 977 |
5772 |
if (r) |
| 978 |
0 |
return (r); |
| 979 |
5772 |
vgc->refs++; |
| 980 |
|
|
| 981 |
5772 |
of = strtoul(vg->av[2], NULL, 10); |
| 982 |
5772 |
sz = strtoul(vg->av[3], NULL, 10); |
| 983 |
5772 |
assert(sz > 0); |
| 984 |
|
|
| 985 |
5772 |
assert(vgc->sz >= of + sz); |
| 986 |
5772 |
assert(vgc->s == vgc->b); |
| 987 |
5772 |
vg->b = (char *)vgc->b + of; |
| 988 |
5772 |
vg->e = (char *)vg->b + sz; |
| 989 |
|
|
| 990 |
5772 |
vf->b = vg->b; |
| 991 |
5772 |
vf->e = vg->e; |
| 992 |
|
|
| 993 |
5772 |
vg->refs++; |
| 994 |
|
|
| 995 |
5772 |
return (0); |
| 996 |
266167 |
} |
| 997 |
|
|
| 998 |
|
/*--------------------------------------------------------------------*/ |
| 999 |
|
|
| 1000 |
|
int |
| 1001 |
233385 |
VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf) |
| 1002 |
|
{ |
| 1003 |
|
struct vsm_seg *vg; |
| 1004 |
|
|
| 1005 |
233385 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1006 |
233385 |
AN(vd->attached); |
| 1007 |
233385 |
AN(vf); |
| 1008 |
233385 |
AN(vf->b); |
| 1009 |
233385 |
vg = vsm_findseg(vd, vf); |
| 1010 |
233385 |
if (vg == NULL) |
| 1011 |
0 |
return (vsm_diag(vd, "VSM_Unmap: bad fantom")); |
| 1012 |
233385 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 1013 |
233385 |
assert(vg->refs > 0); |
| 1014 |
233385 |
vg->refs--; |
| 1015 |
233385 |
vf->b = NULL; |
| 1016 |
233385 |
vf->e = NULL; |
| 1017 |
233385 |
if (vg->refs > 0) |
| 1018 |
0 |
return (0); |
| 1019 |
|
|
| 1020 |
233385 |
if (vg->cluster) { |
| 1021 |
1172 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
| 1022 |
1172 |
assert(vg->s == NULL); |
| 1023 |
1172 |
assert(vg->sz == 0); |
| 1024 |
1172 |
assert(vg->cluster->refs > 0); |
| 1025 |
1172 |
if (--vg->cluster->refs == 0) { |
| 1026 |
1092 |
vsm_unmapseg(vg->cluster); |
| 1027 |
1092 |
if (vg->cluster->flags & VSM_FLAG_STALE) { |
| 1028 |
80 |
AN(vg->flags & VSM_FLAG_STALE); |
| 1029 |
80 |
vsm_delseg(vg->cluster, 0); |
| 1030 |
80 |
} |
| 1031 |
1092 |
} |
| 1032 |
1172 |
vg->b = vg->e = NULL; |
| 1033 |
1172 |
} else { |
| 1034 |
232213 |
vsm_unmapseg(vg); |
| 1035 |
|
} |
| 1036 |
233385 |
if (vg->flags & VSM_FLAG_STALE) |
| 1037 |
5709 |
vsm_delseg(vg, 0); |
| 1038 |
233385 |
return (0); |
| 1039 |
233385 |
} |
| 1040 |
|
|
| 1041 |
|
/*--------------------------------------------------------------------*/ |
| 1042 |
|
|
| 1043 |
|
const struct vsm_valid * |
| 1044 |
3577482 |
VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf) |
| 1045 |
|
{ |
| 1046 |
|
struct vsm_seg *vg; |
| 1047 |
|
|
| 1048 |
3577482 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1049 |
3577482 |
AN(vf); |
| 1050 |
3577482 |
vg = vsm_findseg(vd, vf); |
| 1051 |
3577482 |
if (vg == NULL || vg->flags & VSM_FLAG_STALE) |
| 1052 |
28933 |
return (VSM_invalid); |
| 1053 |
3548549 |
return (VSM_valid); |
| 1054 |
3577482 |
} |
| 1055 |
|
|
| 1056 |
|
/*--------------------------------------------------------------------*/ |
| 1057 |
|
|
| 1058 |
|
int |
| 1059 |
300509 |
VSM_Get(struct vsm *vd, struct vsm_fantom *vf, |
| 1060 |
|
const char *category, const char *ident) |
| 1061 |
|
{ |
| 1062 |
|
|
| 1063 |
300509 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1064 |
300509 |
AN(vd->attached); |
| 1065 |
2135932 |
VSM_FOREACH(vf, vd) { |
| 1066 |
1945677 |
if (strcmp(vf->category, category)) |
| 1067 |
1835423 |
continue; |
| 1068 |
110254 |
if (ident != NULL && strcmp(vf->ident, ident)) |
| 1069 |
0 |
continue; |
| 1070 |
110254 |
return (1); |
| 1071 |
|
} |
| 1072 |
190255 |
memset(vf, 0, sizeof *vf); |
| 1073 |
190255 |
return (0); |
| 1074 |
300509 |
} |
| 1075 |
|
|
| 1076 |
|
/*--------------------------------------------------------------------*/ |
| 1077 |
|
|
| 1078 |
|
char * |
| 1079 |
3800 |
VSM_Dup(struct vsm *vd, const char *category, const char *ident) |
| 1080 |
|
{ |
| 1081 |
|
struct vsm_fantom vf; |
| 1082 |
3800 |
char *p = NULL; |
| 1083 |
|
|
| 1084 |
3800 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1085 |
3800 |
AN(vd->attached); |
| 1086 |
15280 |
VSM_FOREACH(&vf, vd) { |
| 1087 |
15280 |
if (strcmp(vf.category, category)) |
| 1088 |
6560 |
continue; |
| 1089 |
8720 |
if (ident != NULL && strcmp(vf.ident, ident)) |
| 1090 |
4920 |
continue; |
| 1091 |
3800 |
AZ(VSM_Map(vd, &vf)); |
| 1092 |
3800 |
AN(vf.b); |
| 1093 |
3800 |
AN(vf.e); |
| 1094 |
3800 |
p = malloc((char*)vf.e - (char*)vf.b); |
| 1095 |
3800 |
AN(p); |
| 1096 |
3800 |
memcpy(p, vf.b, (char*)vf.e - (char*)vf.b); |
| 1097 |
3800 |
AZ(VSM_Unmap(vd, &vf)); |
| 1098 |
3800 |
break; |
| 1099 |
|
} |
| 1100 |
3800 |
return (p); |
| 1101 |
|
} |