| Code Coverage: vinyl-cache/lib/libvinylapi/vsm.c |
| |
| 82217aa4fc7094b3610340078afaf78cd95a8ec9 |
| |
| 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 "vapi/vsig.h" |
| 48 |
|
#include "vapi/vsm.h" |
| 49 |
|
|
| 50 |
|
#include "vdef.h" |
| 51 |
|
|
| 52 |
|
#include "miniobj.h" |
| 53 |
|
#include "vas.h" |
| 54 |
|
#include "vav.h" |
| 55 |
|
#include "vin.h" |
| 56 |
|
#include "vlu.h" |
| 57 |
|
#include "vqueue.h" |
| 58 |
|
#include "vsb.h" |
| 59 |
|
#include "vsm_priv.h" |
| 60 |
|
#include "vtim.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 |
1185496 |
(sizeof (uint64_t) * 4) |
| 77 |
|
#define VSM_PRIV_MASK \ |
| 78 |
825377 |
((1ULL << VSM_PRIV_SHIFT) - 1) |
| 79 |
|
#define VSM_PRIV_LOW(u) \ |
| 80 |
589689 |
((uint64_t)(u) & VSM_PRIV_MASK) |
| 81 |
|
#define VSM_PRIV_HIGH(u) \ |
| 82 |
235688 |
(((uint64_t)(u) >> VSM_PRIV_SHIFT) & VSM_PRIV_MASK) |
| 83 |
|
#define VSM_PRIV_MERGE(low, high) \ |
| 84 |
124431 |
(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 |
212715 |
#define VSM_FLAG_MARKSCAN (1U<<1) |
| 95 |
206207 |
#define VSM_FLAG_STALE (1U<<2) |
| 96 |
222079 |
#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 lineno; |
| 130 |
|
unsigned retval; |
| 131 |
|
struct vsm_seg *vg; |
| 132 |
|
|
| 133 |
|
unsigned flag_running; |
| 134 |
|
unsigned flag_changed; |
| 135 |
|
unsigned flag_restarted; |
| 136 |
|
|
| 137 |
|
int couldkill; |
| 138 |
|
}; |
| 139 |
|
|
| 140 |
|
struct vsm { |
| 141 |
|
unsigned magic; |
| 142 |
|
#define VSM_MAGIC 0x6e3bd69b |
| 143 |
|
|
| 144 |
|
struct vsb *diag; |
| 145 |
|
uint64_t serial; |
| 146 |
|
|
| 147 |
|
int wdfd; |
| 148 |
|
struct stat wdst; |
| 149 |
|
char *wdname; |
| 150 |
|
|
| 151 |
|
struct vsm_set *mgt; |
| 152 |
|
struct vsm_set *child; |
| 153 |
|
|
| 154 |
|
int attached; |
| 155 |
|
double patience; |
| 156 |
|
}; |
| 157 |
|
|
| 158 |
|
/*--------------------------------------------------------------------*/ |
| 159 |
|
|
| 160 |
|
static int |
| 161 |
|
vsm_diag(struct vsm *vd, const char *fmt, ...) |
| 162 |
51 |
{ |
| 163 |
51 |
va_list ap; |
| 164 |
|
|
| 165 |
51 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 166 |
51 |
AN(fmt); |
| 167 |
|
|
| 168 |
51 |
if (vd->diag == NULL) |
| 169 |
51 |
vd->diag = VSB_new_auto(); |
| 170 |
51 |
AN(vd->diag); |
| 171 |
51 |
VSB_clear(vd->diag); |
| 172 |
51 |
va_start(ap, fmt); |
| 173 |
51 |
VSB_vprintf(vd->diag, fmt, ap); |
| 174 |
51 |
va_end(ap); |
| 175 |
51 |
AZ(VSB_finish(vd->diag)); |
| 176 |
51 |
return (-1); |
| 177 |
51 |
} |
| 178 |
|
|
| 179 |
|
/*--------------------------------------------------------------------*/ |
| 180 |
|
|
| 181 |
|
static int |
| 182 |
|
vsm_mapseg(struct vsm *vd, struct vsm_seg *vg) |
| 183 |
6782 |
{ |
| 184 |
6782 |
size_t of, off, sz, ps, len; |
| 185 |
6782 |
struct vsb *vsb; |
| 186 |
6782 |
void *s; |
| 187 |
6782 |
int fd; |
| 188 |
|
|
| 189 |
6782 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 190 |
|
|
| 191 |
6782 |
if (vg->s != NULL) |
| 192 |
101 |
return (0); |
| 193 |
|
|
| 194 |
6681 |
ps = getpagesize(); |
| 195 |
|
|
| 196 |
6681 |
of = strtoul(vg->av[2], NULL, 10); |
| 197 |
6681 |
off = RDN2(of, ps); |
| 198 |
|
|
| 199 |
6681 |
if (vg->flags & VSM_FLAG_CLUSTER) |
| 200 |
39 |
assert(of == 0); |
| 201 |
6681 |
assert(vg->cluster == NULL); |
| 202 |
|
|
| 203 |
6681 |
sz = strtoul(vg->av[3], NULL, 10); |
| 204 |
6681 |
assert(sz > 0); |
| 205 |
6681 |
assert(of >= off); |
| 206 |
6681 |
len = RUP2((of - off) + sz, ps); |
| 207 |
|
|
| 208 |
6681 |
vsb = VSB_new_auto(); |
| 209 |
6681 |
AN(vsb); |
| 210 |
6681 |
VSB_printf(vsb, "%s/%s/%s", vd->wdname, vg->set->dname, vg->av[1]); |
| 211 |
6681 |
AZ(VSB_finish(vsb)); |
| 212 |
|
|
| 213 |
6681 |
fd = open(VSB_data(vsb), O_RDONLY); // XXX: openat |
| 214 |
6681 |
if (fd < 0) { |
| 215 |
1 |
VSB_destroy(&vsb); |
| 216 |
1 |
return (vsm_diag(vd, "Could not open segment")); |
| 217 |
1 |
} |
| 218 |
|
|
| 219 |
6680 |
s = (void*)mmap(NULL, len, |
| 220 |
6680 |
PROT_READ, |
| 221 |
6680 |
MAP_HASSEMAPHORE | MAP_NOSYNC | MAP_SHARED, |
| 222 |
6680 |
fd, (off_t)off); |
| 223 |
|
|
| 224 |
6680 |
VSB_destroy(&vsb); |
| 225 |
|
|
| 226 |
6680 |
closefd(&fd); |
| 227 |
6680 |
if (s == MAP_FAILED) |
| 228 |
0 |
return (vsm_diag(vd, "Could not mmap segment")); |
| 229 |
|
|
| 230 |
6680 |
vg->s = s; |
| 231 |
6680 |
vg->b = (char*)(vg->s) + of - off; |
| 232 |
6680 |
vg->e = (char *)vg->b + sz; |
| 233 |
6680 |
vg->sz = len; |
| 234 |
|
|
| 235 |
6680 |
return (0); |
| 236 |
6680 |
} |
| 237 |
|
|
| 238 |
|
static void |
| 239 |
|
vsm_unmapseg(struct vsm_seg *vg) |
| 240 |
5928 |
{ |
| 241 |
|
|
| 242 |
5928 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 243 |
|
|
| 244 |
5928 |
AN(vg->b); |
| 245 |
5928 |
AN(vg->e); |
| 246 |
5928 |
AZ(munmap(vg->s, vg->sz)); |
| 247 |
5928 |
vg->s = vg->b = vg->e = NULL; |
| 248 |
5928 |
vg->sz = 0; |
| 249 |
5928 |
} |
| 250 |
|
|
| 251 |
|
/*--------------------------------------------------------------------*/ |
| 252 |
|
|
| 253 |
|
static void |
| 254 |
|
vsm_delseg(struct vsm_seg *vg, int refsok) |
| 255 |
96049 |
{ |
| 256 |
|
|
| 257 |
96049 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 258 |
|
|
| 259 |
96049 |
if (vg->set->vg == vg) { |
| 260 |
5502 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 261 |
5502 |
vg->set->vg = VTAILQ_NEXT(vg, list); |
| 262 |
5502 |
} |
| 263 |
|
|
| 264 |
96049 |
if (refsok && vg->refs) { |
| 265 |
149 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 266 |
149 |
vg->flags |= VSM_FLAG_STALE; |
| 267 |
149 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
| 268 |
149 |
VTAILQ_INSERT_TAIL(&vg->set->stale, vg, list); |
| 269 |
149 |
return; |
| 270 |
149 |
} |
| 271 |
|
|
| 272 |
95900 |
if (vg->s != NULL) |
| 273 |
0 |
vsm_unmapseg(vg); |
| 274 |
|
|
| 275 |
95900 |
if (vg->flags & VSM_FLAG_CLUSTER) { |
| 276 |
2263 |
vg->flags &= ~VSM_FLAG_CLUSTER; |
| 277 |
2263 |
VTAILQ_REMOVE(&vg->set->clusters, vg, clist); |
| 278 |
2263 |
} |
| 279 |
|
|
| 280 |
95900 |
if (vg->flags & VSM_FLAG_STALE) |
| 281 |
149 |
VTAILQ_REMOVE(&vg->set->stale, vg, list); |
| 282 |
95751 |
else |
| 283 |
95751 |
VTAILQ_REMOVE(&vg->set->segs, vg, list); |
| 284 |
95900 |
VAV_Free(vg->av); |
| 285 |
95900 |
FREE_OBJ(vg); |
| 286 |
95900 |
} |
| 287 |
|
|
| 288 |
|
/*--------------------------------------------------------------------*/ |
| 289 |
|
|
| 290 |
|
static struct vsm_set * |
| 291 |
|
vsm_newset(const char *dirname) |
| 292 |
5034 |
{ |
| 293 |
5034 |
struct vsm_set *vs; |
| 294 |
|
|
| 295 |
5034 |
ALLOC_OBJ(vs, VSM_SET_MAGIC); |
| 296 |
5034 |
AN(vs); |
| 297 |
5034 |
VTAILQ_INIT(&vs->segs); |
| 298 |
5034 |
VTAILQ_INIT(&vs->stale); |
| 299 |
5034 |
VTAILQ_INIT(&vs->clusters); |
| 300 |
5034 |
vs->dname = dirname; |
| 301 |
5034 |
vs->dfd = vs->fd = -1; |
| 302 |
5034 |
vs->vlu = VLU_New(vsm_vlu_func, vs, 0); |
| 303 |
5034 |
AN(vs->vlu); |
| 304 |
5034 |
if (getenv("VSM_NOPID") != NULL) |
| 305 |
0 |
vs->couldkill = -1; |
| 306 |
5034 |
return (vs); |
| 307 |
5034 |
} |
| 308 |
|
|
| 309 |
|
static void |
| 310 |
|
vsm_delset(struct vsm_set **p) |
| 311 |
4970 |
{ |
| 312 |
4970 |
struct vsm_set *vs; |
| 313 |
4970 |
struct vsm_seg *vg; |
| 314 |
|
|
| 315 |
4970 |
TAKE_OBJ_NOTNULL(vs, p, VSM_SET_MAGIC); |
| 316 |
|
|
| 317 |
4970 |
if (vs->fd >= 0) |
| 318 |
3098 |
closefd(&vs->fd); |
| 319 |
4970 |
if (vs->dfd >= 0) |
| 320 |
4111 |
closefd(&vs->dfd); |
| 321 |
4970 |
while ((vg = VTAILQ_FIRST(&vs->stale)) != NULL) { |
| 322 |
0 |
AN(vg->flags & VSM_FLAG_STALE); |
| 323 |
0 |
vsm_delseg(vg, 0); |
| 324 |
0 |
} |
| 325 |
93888 |
while ((vg = VTAILQ_FIRST(&vs->segs)) != NULL) { |
| 326 |
88918 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 327 |
88918 |
vsm_delseg(vg, 0); |
| 328 |
88918 |
} |
| 329 |
4970 |
assert(VTAILQ_EMPTY(&vs->clusters)); |
| 330 |
4970 |
VLU_Destroy(&vs->vlu); |
| 331 |
4970 |
FREE_OBJ(vs); |
| 332 |
4970 |
} |
| 333 |
|
|
| 334 |
|
static void |
| 335 |
|
vsm_wash_set(const struct vsm_set *vs, int all) |
| 336 |
9734 |
{ |
| 337 |
9734 |
struct vsm_seg *vg, *vg2; |
| 338 |
|
|
| 339 |
96190 |
VTAILQ_FOREACH_SAFE(vg, &vs->segs, list, vg2) { |
| 340 |
96190 |
if (all || (vg->flags & VSM_FLAG_MARKSCAN) == 0) |
| 341 |
2994 |
vsm_delseg(vg, 1); |
| 342 |
96190 |
} |
| 343 |
9734 |
} |
| 344 |
|
|
| 345 |
|
/*--------------------------------------------------------------------*/ |
| 346 |
|
|
| 347 |
|
struct vsm * |
| 348 |
|
VSM_New(void) |
| 349 |
2517 |
{ |
| 350 |
2517 |
struct vsm *vd; |
| 351 |
|
|
| 352 |
2517 |
ALLOC_OBJ(vd, VSM_MAGIC); |
| 353 |
2517 |
AN(vd); |
| 354 |
|
|
| 355 |
2517 |
vd->mgt = vsm_newset(VSM_MGT_DIRNAME); |
| 356 |
2517 |
vd->mgt->flag_running = VSM_MGT_RUNNING; |
| 357 |
2517 |
vd->mgt->flag_changed = VSM_MGT_CHANGED; |
| 358 |
2517 |
vd->mgt->flag_restarted = VSM_MGT_RESTARTED; |
| 359 |
|
|
| 360 |
2517 |
vd->child = vsm_newset(VSM_CHILD_DIRNAME); |
| 361 |
2517 |
vd->child->flag_running = VSM_WRK_RUNNING; |
| 362 |
2517 |
vd->child->flag_changed = VSM_WRK_CHANGED; |
| 363 |
2517 |
vd->child->flag_restarted = VSM_WRK_RESTARTED; |
| 364 |
|
|
| 365 |
2517 |
vd->mgt->vsm = vd; |
| 366 |
2517 |
vd->child->vsm = vd; |
| 367 |
2517 |
vd->wdfd = -1; |
| 368 |
2517 |
vd->patience = 5; |
| 369 |
2517 |
return (vd); |
| 370 |
2517 |
} |
| 371 |
|
|
| 372 |
|
/*--------------------------------------------------------------------*/ |
| 373 |
|
|
| 374 |
|
int |
| 375 |
|
VSM_Arg(struct vsm *vd, char flag, const char *arg) |
| 376 |
2567 |
{ |
| 377 |
2567 |
char *p = NULL; |
| 378 |
|
|
| 379 |
2567 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 380 |
|
|
| 381 |
2567 |
if (arg == NULL) |
| 382 |
43 |
return (1); |
| 383 |
2524 |
switch (flag) { |
| 384 |
18 |
case 't': |
| 385 |
18 |
if (!strcasecmp(arg, "off")) { |
| 386 |
0 |
vd->patience = -1; |
| 387 |
18 |
} else { |
| 388 |
18 |
vd->patience = strtod(arg, &p); |
| 389 |
18 |
if ((p != NULL && *p != '\0') || |
| 390 |
18 |
!isfinite(vd->patience) || vd->patience < 0) |
| 391 |
8 |
return (vsm_diag(vd, |
| 392 |
8 |
"-t: Invalid argument: %s", arg)); |
| 393 |
18 |
} |
| 394 |
10 |
break; |
| 395 |
2506 |
case 'n': |
| 396 |
2506 |
if (vd->wdname != NULL) |
| 397 |
0 |
free(vd->wdname); |
| 398 |
2506 |
vd->wdname = VIN_n_Arg(arg); |
| 399 |
2506 |
if (vd->wdname == NULL) |
| 400 |
0 |
return (vsm_diag(vd, "Invalid instance name: %s", |
| 401 |
0 |
strerror(errno))); |
| 402 |
2506 |
break; |
| 403 |
2506 |
default: |
| 404 |
0 |
return (vsm_diag(vd, "Unknown VSM_Arg('%c')", flag)); |
| 405 |
2524 |
} |
| 406 |
2516 |
return (1); |
| 407 |
2524 |
} |
| 408 |
|
|
| 409 |
|
/*--------------------------------------------------------------------*/ |
| 410 |
|
|
| 411 |
|
void |
| 412 |
|
VSM_Destroy(struct vsm **vdp) |
| 413 |
2485 |
{ |
| 414 |
2485 |
struct vsm *vd; |
| 415 |
|
|
| 416 |
2485 |
TAKE_OBJ_NOTNULL(vd, vdp, VSM_MAGIC); |
| 417 |
|
|
| 418 |
2485 |
VSM_ResetError(vd); |
| 419 |
2485 |
REPLACE(vd->wdname, NULL); |
| 420 |
2485 |
if (vd->diag != NULL) |
| 421 |
0 |
VSB_destroy(&vd->diag); |
| 422 |
2485 |
if (vd->wdfd >= 0) |
| 423 |
2482 |
closefd(&vd->wdfd); |
| 424 |
2485 |
vsm_delset(&vd->mgt); |
| 425 |
2485 |
vsm_delset(&vd->child); |
| 426 |
2485 |
FREE_OBJ(vd); |
| 427 |
2485 |
} |
| 428 |
|
|
| 429 |
|
/*--------------------------------------------------------------------*/ |
| 430 |
|
|
| 431 |
|
const char * |
| 432 |
|
VSM_Error(const struct vsm *vd) |
| 433 |
13 |
{ |
| 434 |
|
|
| 435 |
13 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 436 |
|
|
| 437 |
13 |
if (vd->diag == NULL) |
| 438 |
0 |
return ("No VSM error"); |
| 439 |
13 |
else |
| 440 |
13 |
return (VSB_data(vd->diag)); |
| 441 |
13 |
} |
| 442 |
|
|
| 443 |
|
/*--------------------------------------------------------------------*/ |
| 444 |
|
|
| 445 |
|
void |
| 446 |
|
VSM_ResetError(struct vsm *vd) |
| 447 |
5019 |
{ |
| 448 |
|
|
| 449 |
5019 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 450 |
|
|
| 451 |
5019 |
if (vd->diag == NULL) |
| 452 |
4980 |
return; |
| 453 |
39 |
VSB_destroy(&vd->diag); |
| 454 |
39 |
} |
| 455 |
|
|
| 456 |
|
/*-------------------------------------------------------------------- |
| 457 |
|
*/ |
| 458 |
|
|
| 459 |
|
static int |
| 460 |
|
vsm_cmp_av(char * const *a1, char * const *a2) |
| 461 |
102525 |
{ |
| 462 |
|
|
| 463 |
120851 |
while (1) { |
| 464 |
120851 |
if (*a1 == NULL && *a2 == NULL) |
| 465 |
4042 |
return (0); |
| 466 |
116809 |
if (*a1 == NULL || *a2 == NULL) |
| 467 |
0 |
return (1); |
| 468 |
116809 |
if (vstrcmp(*a1, *a2)) |
| 469 |
98483 |
return (1); |
| 470 |
18326 |
a1++; |
| 471 |
18326 |
a2++; |
| 472 |
18326 |
} |
| 473 |
102525 |
} |
| 474 |
|
|
| 475 |
|
static struct vsm_seg * |
| 476 |
|
vsm_findcluster(const struct vsm_set *vs, const char *cnam) |
| 477 |
2186 |
{ |
| 478 |
2186 |
struct vsm_seg *vg; |
| 479 |
2186 |
AN(vs); |
| 480 |
2186 |
AN(cnam); |
| 481 |
3067 |
VTAILQ_FOREACH(vg, &vs->clusters, clist) { |
| 482 |
3067 |
AN(vg->av[1]); |
| 483 |
3067 |
if (!vstrcmp(cnam, vg->av[1])) |
| 484 |
2186 |
return (vg); |
| 485 |
3067 |
} |
| 486 |
0 |
return (NULL); |
| 487 |
2186 |
} |
| 488 |
|
|
| 489 |
|
static unsigned |
| 490 |
|
vsm_running(struct vsm_set *vs, pid_t pid) |
| 491 |
80764 |
{ |
| 492 |
|
|
| 493 |
80764 |
AN(vs); |
| 494 |
|
|
| 495 |
80764 |
if (pid == 0) |
| 496 |
0 |
return (0); |
| 497 |
|
|
| 498 |
80764 |
if (kill(pid, 0) == 0) { |
| 499 |
79154 |
vs->couldkill = 1; |
| 500 |
79154 |
return (1); |
| 501 |
79154 |
} |
| 502 |
1610 |
if (errno == EPERM) /* a process exists, assume running */ |
| 503 |
0 |
return (1); |
| 504 |
1610 |
assert(errno != EINVAL); |
| 505 |
1610 |
return (0); |
| 506 |
1610 |
} |
| 507 |
|
|
| 508 |
|
static int |
| 509 |
|
vsm_vlu_hash(struct vsm_set *vs, const char *line) |
| 510 |
4210 |
{ |
| 511 |
4210 |
int i; |
| 512 |
4210 |
uintmax_t id1, id2; |
| 513 |
|
|
| 514 |
4210 |
i = sscanf(line, "# %ju %ju", &id1, &id2); |
| 515 |
4210 |
if (i != 2) { |
| 516 |
0 |
vs->retval |= vs->flag_restarted; |
| 517 |
0 |
return (0); |
| 518 |
0 |
} |
| 519 |
4210 |
if (vs->couldkill >= 0 && vsm_running(vs, id1)) { |
| 520 |
|
/* nothing to do */ |
| 521 |
4207 |
} else if (vs->couldkill > 0 && errno == ESRCH) { |
| 522 |
0 |
vs->retval |= vs->flag_restarted | VSM_MGT_CHANGED; |
| 523 |
0 |
return (0); |
| 524 |
0 |
} |
| 525 |
4210 |
vs->retval |= VSM_MGT_RUNNING; |
| 526 |
4210 |
if (id1 != vs->id1 || id2 != vs->id2) { |
| 527 |
4208 |
vs->retval |= vs->flag_restarted; |
| 528 |
4208 |
vs->id1 = id1; |
| 529 |
4208 |
vs->id2 = id2; |
| 530 |
4208 |
} |
| 531 |
4210 |
return (0); |
| 532 |
4210 |
} |
| 533 |
|
|
| 534 |
|
static char ** |
| 535 |
|
vsm_parse_plus_minus_line(struct vsm *vd, const char *line, int *acp) |
| 536 |
100928 |
{ |
| 537 |
100928 |
char **av; |
| 538 |
100928 |
int ac; |
| 539 |
|
|
| 540 |
100928 |
AN(acp); |
| 541 |
100928 |
*acp = 0; |
| 542 |
|
|
| 543 |
100928 |
av = VAV_Parse(line + 1, &ac, 0); |
| 544 |
100928 |
AN(av); |
| 545 |
|
|
| 546 |
100928 |
if (av[0] != NULL) { |
| 547 |
1 |
(void)vsm_diag(vd, "%s", av[0]); |
| 548 |
1 |
VAV_Free(av); |
| 549 |
1 |
return (NULL); |
| 550 |
1 |
} |
| 551 |
100927 |
if (ac < 4 || ac > 6) { |
| 552 |
1 |
(void)vsm_diag(vd, "Wrong number of fields (%u)", ac-1); |
| 553 |
1 |
VAV_Free(av); |
| 554 |
1 |
return (NULL); |
| 555 |
1 |
} |
| 556 |
100926 |
*acp = ac; |
| 557 |
100926 |
return (av); |
| 558 |
100927 |
} |
| 559 |
|
|
| 560 |
|
static int |
| 561 |
|
vsm_vlu_plus(struct vsm *vd, struct vsm_set *vs, const char *line) |
| 562 |
96940 |
{ |
| 563 |
96940 |
char **av; |
| 564 |
96940 |
int ac; |
| 565 |
96940 |
struct vsm_seg *vg; |
| 566 |
|
|
| 567 |
96940 |
av = vsm_parse_plus_minus_line(vd, line, &ac); |
| 568 |
96940 |
if (av == NULL) |
| 569 |
2 |
return (-1); |
| 570 |
|
|
| 571 |
96938 |
vg = vs->vg; |
| 572 |
96938 |
CHECK_OBJ_ORNULL(vg, VSM_SEG_MAGIC); |
| 573 |
96938 |
if (vg != NULL) |
| 574 |
54 |
AZ(vg->flags & VSM_FLAG_STALE); |
| 575 |
96938 |
while (vg != NULL && vsm_cmp_av(&vg->av[1], &av[1])) |
| 576 |
0 |
vg = VTAILQ_NEXT(vg, list); |
| 577 |
96938 |
if (vg != NULL) { |
| 578 |
|
/* entry compared equal, so it survives */ |
| 579 |
54 |
CHECK_OBJ_NOTNULL(vg, VSM_SEG_MAGIC); |
| 580 |
54 |
VAV_Free(av); |
| 581 |
54 |
vg->flags |= VSM_FLAG_MARKSCAN; |
| 582 |
54 |
vs->vg = VTAILQ_NEXT(vg, list); |
| 583 |
96884 |
} else { |
| 584 |
96884 |
ALLOC_OBJ(vg, VSM_SEG_MAGIC); |
| 585 |
96884 |
AN(vg); |
| 586 |
96884 |
vg->av = av; |
| 587 |
96884 |
vg->set = vs; |
| 588 |
96884 |
vg->flags = VSM_FLAG_MARKSCAN; |
| 589 |
96884 |
vg->serial = vd->serial; |
| 590 |
|
|
| 591 |
96884 |
VTAILQ_INSERT_TAIL(&vs->segs, vg, list); |
| 592 |
96884 |
if (ac == 4) { |
| 593 |
2279 |
vg->flags |= VSM_FLAG_CLUSTER; |
| 594 |
2279 |
VTAILQ_INSERT_TAIL(&vs->clusters, vg, clist); |
| 595 |
94605 |
} else if (*vg->av[2] != '0') { |
| 596 |
2136 |
vg->cluster = vsm_findcluster(vs, vg->av[1]); |
| 597 |
2136 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
| 598 |
2136 |
} |
| 599 |
96884 |
vs->retval |= vs->flag_changed; |
| 600 |
96884 |
} |
| 601 |
96938 |
return (0); |
| 602 |
96938 |
} |
| 603 |
|
|
| 604 |
|
static int |
| 605 |
|
vsm_vlu_minus(struct vsm *vd, struct vsm_set *vs, const char *line) |
| 606 |
3988 |
{ |
| 607 |
3988 |
char **av; |
| 608 |
3988 |
int ac; |
| 609 |
3988 |
struct vsm_seg *vg; |
| 610 |
|
|
| 611 |
3988 |
av = vsm_parse_plus_minus_line(vd, line, &ac); |
| 612 |
3988 |
if (av == NULL) |
| 613 |
0 |
return (-1); |
| 614 |
|
|
| 615 |
|
/* Clustered segments cannot come before their cluster */ |
| 616 |
3988 |
if (*av[2] != '0') |
| 617 |
50 |
vg = vsm_findcluster(vs, av[1]); |
| 618 |
3938 |
else |
| 619 |
3938 |
vg = VTAILQ_FIRST(&vs->segs); |
| 620 |
|
|
| 621 |
102471 |
for (;vg != NULL; vg = VTAILQ_NEXT(vg, list)) { |
| 622 |
102471 |
if (!vsm_cmp_av(&vg->av[1], &av[1])) { |
| 623 |
3988 |
vs->retval |= vs->flag_changed; |
| 624 |
3988 |
vsm_delseg(vg, 1); |
| 625 |
3988 |
break; |
| 626 |
3988 |
} |
| 627 |
102471 |
} |
| 628 |
3988 |
AN(vg); |
| 629 |
3988 |
VAV_Free(av); |
| 630 |
3988 |
return (0); |
| 631 |
3988 |
} |
| 632 |
|
|
| 633 |
|
static int v_matchproto_(vlu_f) |
| 634 |
|
vsm_vlu_func(void *priv, const char *line) |
| 635 |
105139 |
{ |
| 636 |
105139 |
struct vsm *vd; |
| 637 |
105139 |
struct vsm_set *vs; |
| 638 |
105139 |
int i = 0; |
| 639 |
|
|
| 640 |
105139 |
CAST_OBJ_NOTNULL(vs, priv, VSM_SET_MAGIC); |
| 641 |
105139 |
vd = vs->vsm; |
| 642 |
105139 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 643 |
105139 |
AN(line); |
| 644 |
|
|
| 645 |
105139 |
vs->lineno++; |
| 646 |
|
|
| 647 |
|
/* Up the serial counter. This wraps at UINTPTR_MAX/2 |
| 648 |
|
* because thats the highest value we can store in struct |
| 649 |
|
* vsm_fantom. */ |
| 650 |
105139 |
vd->serial = VSM_PRIV_LOW(vd->serial + 1); |
| 651 |
|
|
| 652 |
105139 |
switch (line[0]) { |
| 653 |
4210 |
case '#': |
| 654 |
4210 |
i = vsm_vlu_hash(vs, line); |
| 655 |
4210 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
| 656 |
3048 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
| 657 |
4210 |
if (!(vs->retval & vs->flag_restarted)) |
| 658 |
2 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
| 659 |
4210 |
break; |
| 660 |
96940 |
case '+': |
| 661 |
96940 |
i = vsm_vlu_plus(vd, vs, line); |
| 662 |
96940 |
break; |
| 663 |
3988 |
case '-': |
| 664 |
3988 |
i = vsm_vlu_minus(vd, vs, line); |
| 665 |
3988 |
break; |
| 666 |
1 |
default: |
| 667 |
1 |
i = vsm_diag(vd, "Unknown ident"); |
| 668 |
1 |
break; |
| 669 |
105139 |
} |
| 670 |
105139 |
return (i); |
| 671 |
105139 |
} |
| 672 |
|
|
| 673 |
|
static void |
| 674 |
|
vsm_readlines(struct vsm_set *vs) |
| 675 |
76558 |
{ |
| 676 |
76558 |
struct vsm *vd; |
| 677 |
76558 |
int i; |
| 678 |
|
|
| 679 |
76558 |
vd = vs->vsm; |
| 680 |
76558 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 681 |
|
|
| 682 |
87656 |
do { |
| 683 |
87656 |
assert(vs->fd >= 0); |
| 684 |
87656 |
i = VLU_Fd(vs->vlu, vs->fd); |
| 685 |
87656 |
} while (!i); |
| 686 |
76558 |
if (i != -2) { |
| 687 |
|
/* The line handler left the reason in the diag */ |
| 688 |
4 |
if (vd->diag != NULL) { |
| 689 |
3 |
fprintf( |
| 690 |
3 |
stderr, |
| 691 |
3 |
"Malformed _.index line (%u): %s\n", |
| 692 |
3 |
vs->lineno, |
| 693 |
3 |
VSB_data(vd->diag) |
| 694 |
3 |
); |
| 695 |
3 |
} else { |
| 696 |
1 |
fprintf( |
| 697 |
1 |
stderr, |
| 698 |
1 |
"Could not read _.index:\nPossibly relevant: %s", |
| 699 |
1 |
strerror(errno) |
| 700 |
1 |
); |
| 701 |
1 |
} |
| 702 |
4 |
exit(2); |
| 703 |
4 |
} |
| 704 |
76558 |
} |
| 705 |
|
|
| 706 |
|
static unsigned |
| 707 |
|
vsm_refresh_set(struct vsm *vd, struct vsm_set *vs) |
| 708 |
83883 |
{ |
| 709 |
83883 |
unsigned restarted = 0; |
| 710 |
83883 |
struct stat st; |
| 711 |
|
|
| 712 |
83883 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 713 |
83883 |
CHECK_OBJ_NOTNULL(vs, VSM_SET_MAGIC); |
| 714 |
83883 |
vs->retval = 0; |
| 715 |
83883 |
if (vs->dfd >= 0 && ( |
| 716 |
74209 |
fstatat(vd->wdfd, vs->dname, &st, AT_SYMLINK_NOFOLLOW) || |
| 717 |
74209 |
st.st_ino != vs->dst.st_ino || |
| 718 |
74209 |
st.st_dev != vs->dst.st_dev || |
| 719 |
74209 |
st.st_mode != vs->dst.st_mode || |
| 720 |
74209 |
st.st_nlink == 0)) { |
| 721 |
64 |
closefd(&vs->dfd); |
| 722 |
64 |
restarted = vs->flag_restarted; |
| 723 |
64 |
} |
| 724 |
|
|
| 725 |
83883 |
if (vs->dfd < 0) { |
| 726 |
9738 |
if (vs->fd >= 0) |
| 727 |
64 |
closefd(&vs->fd); |
| 728 |
9738 |
vs->dfd = openat(vd->wdfd, vs->dname, O_RDONLY); |
| 729 |
9738 |
} |
| 730 |
|
|
| 731 |
83883 |
if (vs->dfd < 0) { |
| 732 |
5527 |
vs->id1 = vs->id2 = 0; |
| 733 |
5527 |
vsm_wash_set(vs, 1); |
| 734 |
5527 |
return (vs->retval | restarted); |
| 735 |
5527 |
} |
| 736 |
|
|
| 737 |
78356 |
AZ(fstat(vs->dfd, &vs->dst)); |
| 738 |
|
|
| 739 |
78356 |
if (vs->fd >= 0 && ( |
| 740 |
73360 |
fstatat(vs->dfd, "_.index", &st, AT_SYMLINK_NOFOLLOW) || |
| 741 |
73360 |
st.st_ino != vs->fst.st_ino || |
| 742 |
73360 |
st.st_dev != vs->fst.st_dev || |
| 743 |
73360 |
st.st_mode != vs->fst.st_mode || |
| 744 |
73360 |
st.st_size < vs->fst.st_size || |
| 745 |
73360 |
st.st_nlink < 1)) { |
| 746 |
1013 |
closefd(&vs->fd); |
| 747 |
1013 |
vs->retval |= vs->flag_changed; |
| 748 |
1013 |
} |
| 749 |
|
|
| 750 |
78356 |
if (vs->fd >= 0) { |
| 751 |
72347 |
vs->vg = NULL; |
| 752 |
72347 |
vsm_readlines(vs); |
| 753 |
72347 |
} else { |
| 754 |
6009 |
VTAILQ_FOREACH(vs->vg, &vs->segs, list) |
| 755 |
12329 |
vs->vg->flags &= ~VSM_FLAG_MARKSCAN; |
| 756 |
6009 |
vs->vg = VTAILQ_FIRST(&vs->segs); |
| 757 |
6009 |
vs->fd = openat(vs->dfd, "_.index", O_RDONLY); |
| 758 |
6009 |
if (vs->fd < 0) |
| 759 |
1798 |
return (vs->retval | restarted); |
| 760 |
4211 |
VLU_Reset(vs->vlu); |
| 761 |
4211 |
vs->lineno = 0; |
| 762 |
4211 |
AZ(fstat(vs->fd, &vs->fst)); |
| 763 |
4211 |
vsm_readlines(vs); |
| 764 |
4211 |
vsm_wash_set(vs, 0); |
| 765 |
4211 |
} |
| 766 |
|
|
| 767 |
76558 |
vs->fst.st_size = lseek(vs->fd, 0L, SEEK_CUR); |
| 768 |
|
|
| 769 |
76558 |
if (vs->couldkill < 0 || vsm_running(vs, vs->id1)) |
| 770 |
74947 |
vs->retval |= vs->flag_running; |
| 771 |
76558 |
return (vs->retval); |
| 772 |
78356 |
} |
| 773 |
|
|
| 774 |
|
/*--------------------------------------------------------------------*/ |
| 775 |
|
|
| 776 |
|
unsigned |
| 777 |
|
VSM_Status(struct vsm *vd) |
| 778 |
42860 |
{ |
| 779 |
42860 |
unsigned retval = 0; |
| 780 |
42860 |
struct stat st; |
| 781 |
|
|
| 782 |
42860 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 783 |
|
|
| 784 |
|
/* See if the -n workdir changed */ |
| 785 |
42860 |
if (vd->wdfd >= 0) { |
| 786 |
40323 |
AZ(fstat(vd->wdfd, &st)); |
| 787 |
40323 |
if (st.st_ino != vd->wdst.st_ino || |
| 788 |
40323 |
st.st_dev != vd->wdst.st_dev || |
| 789 |
40323 |
st.st_mode != vd->wdst.st_mode || |
| 790 |
40323 |
st.st_nlink == 0) { |
| 791 |
0 |
closefd(&vd->wdfd); |
| 792 |
0 |
vsm_wash_set(vd->mgt, 1); |
| 793 |
0 |
vsm_wash_set(vd->child, 1); |
| 794 |
0 |
} |
| 795 |
40323 |
} |
| 796 |
|
|
| 797 |
|
/* Open workdir */ |
| 798 |
42860 |
if (vd->wdfd < 0) { |
| 799 |
2537 |
retval |= VSM_MGT_RESTARTED | VSM_MGT_CHANGED; |
| 800 |
2537 |
retval |= VSM_WRK_RESTARTED | VSM_WRK_CHANGED; |
| 801 |
2537 |
vd->wdfd = open(vd->wdname, O_RDONLY); |
| 802 |
2537 |
if (vd->wdfd < 0) |
| 803 |
35 |
(void)vsm_diag(vd, |
| 804 |
35 |
"VSM_Status: Cannot open workdir"); |
| 805 |
2502 |
else |
| 806 |
2502 |
AZ(fstat(vd->wdfd, &vd->wdst)); |
| 807 |
2537 |
} |
| 808 |
|
|
| 809 |
42860 |
if (vd->wdfd >= 0) { |
| 810 |
42825 |
retval |= vsm_refresh_set(vd, vd->mgt); |
| 811 |
42825 |
if (vd->mgt->couldkill > 0 && (retval & VSM_MGT_RESTARTED)) |
| 812 |
2498 |
vd->mgt->couldkill = 0; |
| 813 |
42825 |
if (retval & VSM_MGT_RUNNING) |
| 814 |
41058 |
retval |= vsm_refresh_set(vd, vd->child); |
| 815 |
42825 |
if (vd->child->couldkill > 0 && (retval & VSM_WRK_RESTARTED)) |
| 816 |
1705 |
vd->child->couldkill = 0; |
| 817 |
42825 |
} |
| 818 |
42860 |
return (retval); |
| 819 |
42860 |
} |
| 820 |
|
|
| 821 |
|
/*--------------------------------------------------------------------*/ |
| 822 |
|
|
| 823 |
|
int |
| 824 |
|
VSM_Attach(struct vsm *vd, int progress) |
| 825 |
2506 |
{ |
| 826 |
2506 |
const char *def; |
| 827 |
2506 |
double t0; |
| 828 |
2506 |
unsigned u; |
| 829 |
2506 |
int i, n = 0; |
| 830 |
|
|
| 831 |
2506 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 832 |
|
|
| 833 |
2506 |
if (vd->patience < 0) |
| 834 |
0 |
t0 = DBL_MAX; |
| 835 |
2506 |
else |
| 836 |
2506 |
t0 = VTIM_mono() + vd->patience; |
| 837 |
|
|
| 838 |
2506 |
if (vd->wdname == NULL) { |
| 839 |
7 |
def = getenv("VINYL_DEFAULT_N"); |
| 840 |
7 |
if (def == NULL) |
| 841 |
0 |
def = ""; /* Use default (hostname) */ |
| 842 |
7 |
i = VSM_Arg(vd, 'n', def); |
| 843 |
7 |
if (i < 0) |
| 844 |
0 |
return (i); |
| 845 |
7 |
AN(vd->wdname); |
| 846 |
7 |
} |
| 847 |
|
|
| 848 |
2506 |
AZ(vd->attached); |
| 849 |
2543 |
while (!VSIG_int && !VSIG_term) { |
| 850 |
2538 |
u = VSM_Status(vd); |
| 851 |
2538 |
VSM_ResetError(vd); |
| 852 |
2538 |
if (u & VSM_MGT_RUNNING) { |
| 853 |
2498 |
if (progress >= 0 && n > 4) |
| 854 |
0 |
(void)write(progress, "\n", 1); |
| 855 |
2498 |
vd->attached = 1; |
| 856 |
2498 |
return (0); |
| 857 |
2498 |
} |
| 858 |
40 |
if (t0 < VTIM_mono()) { |
| 859 |
3 |
if (progress >= 0 && n > 4) |
| 860 |
1 |
(void)write(progress, "\n", 1); |
| 861 |
3 |
return (vsm_diag(vd, |
| 862 |
3 |
"Could not get hold of vinyld, is it running?")); |
| 863 |
3 |
} |
| 864 |
37 |
if (progress >= 0 && !(++n % 4)) |
| 865 |
8 |
(void)write(progress, ".", 1); |
| 866 |
37 |
VTIM_sleep(.25); |
| 867 |
37 |
} |
| 868 |
5 |
return (vsm_diag(vd, "Attach interrupted")); |
| 869 |
2506 |
} |
| 870 |
|
|
| 871 |
|
/*--------------------------------------------------------------------*/ |
| 872 |
|
|
| 873 |
|
static struct vsm_seg * |
| 874 |
|
vsm_set_findseg(const struct vsm_set *vs, uintptr_t serial) |
| 875 |
8314 |
{ |
| 876 |
8314 |
struct vsm_seg *vg; |
| 877 |
|
|
| 878 |
99487 |
VTAILQ_FOREACH(vg, &vs->segs, list) { |
| 879 |
99487 |
if (vg->serial == serial) |
| 880 |
3436 |
return (vg); |
| 881 |
99487 |
} |
| 882 |
4878 |
VTAILQ_FOREACH(vg, &vs->stale, list) { |
| 883 |
4105 |
if (vg->serial == serial) |
| 884 |
147 |
return (vg); |
| 885 |
4105 |
} |
| 886 |
4731 |
return (NULL); |
| 887 |
4878 |
} |
| 888 |
|
|
| 889 |
|
static struct vsm_seg * |
| 890 |
|
vsm_findseg(const struct vsm *vd, const struct vsm_fantom *vf) |
| 891 |
235688 |
{ |
| 892 |
235688 |
struct vsm_seg *vg; |
| 893 |
235688 |
uint64_t x; |
| 894 |
|
|
| 895 |
235688 |
x = VSM_PRIV_HIGH(vf->priv); |
| 896 |
235688 |
if (x == vd->serial) { |
| 897 |
231489 |
vg = (struct vsm_seg *)vf->priv2; |
| 898 |
231489 |
if (!VALID_OBJ(vg, VSM_SEG_MAGIC) || |
| 899 |
231489 |
vg->serial != VSM_PRIV_LOW(vf->priv)) |
| 900 |
0 |
WRONG("Corrupt fantom"); |
| 901 |
231489 |
return (vg); |
| 902 |
231489 |
} |
| 903 |
|
|
| 904 |
4199 |
x = VSM_PRIV_LOW(vf->priv); |
| 905 |
4199 |
vg = vsm_set_findseg(vd->mgt, x); |
| 906 |
4199 |
if (vg == NULL) |
| 907 |
4114 |
vg = vsm_set_findseg(vd->child, x); |
| 908 |
4199 |
if (vg == NULL) |
| 909 |
617 |
return (NULL); |
| 910 |
|
|
| 911 |
|
/* Update the fantom with the new priv so that lookups will be |
| 912 |
|
* fast on the next call. Note that this casts away the const. */ |
| 913 |
3582 |
((struct vsm_fantom *)TRUST_ME(vf))->priv = |
| 914 |
3582 |
VSM_PRIV_MERGE(vg->serial, vd->serial); |
| 915 |
3582 |
return (vg); |
| 916 |
4199 |
} |
| 917 |
|
|
| 918 |
|
/*--------------------------------------------------------------------*/ |
| 919 |
|
|
| 920 |
|
void |
| 921 |
|
VSM__iter0(const struct vsm *vd, struct vsm_fantom *vf) |
| 922 |
7722 |
{ |
| 923 |
|
|
| 924 |
7722 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 925 |
7722 |
AN(vf); |
| 926 |
|
|
| 927 |
7722 |
AN(vd->attached); |
| 928 |
7722 |
memset(vf, 0, sizeof *vf); |
| 929 |
7722 |
} |
| 930 |
|
|
| 931 |
|
int |
| 932 |
|
VSM__itern(struct vsm *vd, struct vsm_fantom *vf) |
| 933 |
125871 |
{ |
| 934 |
125871 |
struct vsm_seg *vg; |
| 935 |
|
|
| 936 |
125871 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 937 |
125871 |
AN(vd->attached); |
| 938 |
125871 |
AN(vf); |
| 939 |
|
|
| 940 |
125871 |
if (vf->priv == 0) { |
| 941 |
7722 |
vg = VTAILQ_FIRST(&vd->mgt->segs); |
| 942 |
7722 |
if (vg == NULL) |
| 943 |
0 |
return (0); |
| 944 |
118149 |
} else { |
| 945 |
118149 |
vg = vsm_findseg(vd, vf); |
| 946 |
118149 |
if (vg == NULL) |
| 947 |
0 |
return (vsm_diag(vd, "VSM_FOREACH: inconsistency")); |
| 948 |
119978 |
while (1) { |
| 949 |
119978 |
if (vg->set == vd->mgt && VTAILQ_NEXT(vg, list) == NULL) |
| 950 |
7625 |
vg = VTAILQ_FIRST(&vd->child->segs); |
| 951 |
112353 |
else |
| 952 |
112353 |
vg = VTAILQ_NEXT(vg, list); |
| 953 |
119978 |
if (vg == NULL) |
| 954 |
5022 |
return (0); |
| 955 |
114956 |
if (!(vg->flags & VSM_FLAG_CLUSTER)) |
| 956 |
113127 |
break; |
| 957 |
114956 |
} |
| 958 |
118149 |
} |
| 959 |
120849 |
memset(vf, 0, sizeof *vf); |
| 960 |
120849 |
vf->priv = VSM_PRIV_MERGE(vg->serial, vd->serial); |
| 961 |
120849 |
vf->priv2 = (uintptr_t)vg; |
| 962 |
120849 |
vf->category = vg->av[4]; |
| 963 |
120849 |
vf->ident = vg->av[5]; |
| 964 |
120849 |
AN(vf->category); |
| 965 |
120849 |
return (1); |
| 966 |
120849 |
} |
| 967 |
|
|
| 968 |
|
/*--------------------------------------------------------------------*/ |
| 969 |
|
|
| 970 |
|
int |
| 971 |
|
VSM_Map(struct vsm *vd, struct vsm_fantom *vf) |
| 972 |
6782 |
{ |
| 973 |
6782 |
struct vsm_seg *vg, *vgc; |
| 974 |
6782 |
size_t of, sz; |
| 975 |
6782 |
int r; |
| 976 |
|
|
| 977 |
6782 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 978 |
6782 |
AN(vd->attached); |
| 979 |
6782 |
AN(vf); |
| 980 |
6782 |
vg = vsm_findseg(vd, vf); |
| 981 |
6782 |
if (vg == NULL) |
| 982 |
0 |
return (vsm_diag(vd, "VSM_Map: bad fantom")); |
| 983 |
|
|
| 984 |
6782 |
assert(vg->serial == VSM_PRIV_LOW(vf->priv)); |
| 985 |
6782 |
assert(vg->av[4] == vf->category); |
| 986 |
6782 |
assert(vg->av[5] == vf->ident); |
| 987 |
|
|
| 988 |
6782 |
if (vg->b != NULL) { |
| 989 |
0 |
assert(vg->refs > 0); |
| 990 |
0 |
AN(vg->e); |
| 991 |
0 |
vf->b = vg->b; |
| 992 |
0 |
vf->e = vg->e; |
| 993 |
0 |
vg->refs++; |
| 994 |
0 |
return (0); |
| 995 |
0 |
} |
| 996 |
|
|
| 997 |
6782 |
assert(vg->refs == 0); |
| 998 |
|
|
| 999 |
6782 |
vgc = vg->cluster; |
| 1000 |
|
|
| 1001 |
6782 |
if (vgc == NULL) { |
| 1002 |
6642 |
r = vsm_mapseg(vd, vg); |
| 1003 |
6642 |
if (r) |
| 1004 |
1 |
return (r); |
| 1005 |
6641 |
vf->b = vg->b; |
| 1006 |
6641 |
vf->e = vg->e; |
| 1007 |
|
|
| 1008 |
6641 |
vg->refs++; |
| 1009 |
|
|
| 1010 |
6641 |
return (0); |
| 1011 |
6642 |
} |
| 1012 |
|
|
| 1013 |
140 |
CHECK_OBJ(vgc, VSM_SEG_MAGIC); |
| 1014 |
140 |
assert(vgc->flags & VSM_FLAG_CLUSTER); |
| 1015 |
140 |
assert(vg->s == NULL); |
| 1016 |
140 |
assert(vg->sz == 0); |
| 1017 |
|
|
| 1018 |
140 |
r = vsm_mapseg(vd, vgc); |
| 1019 |
140 |
if (r) |
| 1020 |
0 |
return (r); |
| 1021 |
140 |
vgc->refs++; |
| 1022 |
|
|
| 1023 |
140 |
of = strtoul(vg->av[2], NULL, 10); |
| 1024 |
140 |
sz = strtoul(vg->av[3], NULL, 10); |
| 1025 |
140 |
assert(sz > 0); |
| 1026 |
|
|
| 1027 |
140 |
assert(vgc->sz >= of + sz); |
| 1028 |
140 |
assert(vgc->s == vgc->b); |
| 1029 |
140 |
vg->b = (char *)vgc->b + of; |
| 1030 |
140 |
vg->e = (char *)vg->b + sz; |
| 1031 |
|
|
| 1032 |
140 |
vf->b = vg->b; |
| 1033 |
140 |
vf->e = vg->e; |
| 1034 |
|
|
| 1035 |
140 |
vg->refs++; |
| 1036 |
|
|
| 1037 |
140 |
return (0); |
| 1038 |
140 |
} |
| 1039 |
|
|
| 1040 |
|
/*--------------------------------------------------------------------*/ |
| 1041 |
|
|
| 1042 |
|
int |
| 1043 |
|
VSM_Unmap(struct vsm *vd, struct vsm_fantom *vf) |
| 1044 |
5930 |
{ |
| 1045 |
5930 |
struct vsm_seg *vg; |
| 1046 |
|
|
| 1047 |
5930 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1048 |
5930 |
AN(vd->attached); |
| 1049 |
5930 |
AN(vf); |
| 1050 |
5930 |
AN(vf->b); |
| 1051 |
5930 |
vg = vsm_findseg(vd, vf); |
| 1052 |
5930 |
if (vg == NULL) |
| 1053 |
0 |
return (vsm_diag(vd, "VSM_Unmap: bad fantom")); |
| 1054 |
5930 |
CHECK_OBJ(vg, VSM_SEG_MAGIC); |
| 1055 |
5930 |
assert(vg->refs > 0); |
| 1056 |
5930 |
vg->refs--; |
| 1057 |
5930 |
vf->b = NULL; |
| 1058 |
5930 |
vf->e = NULL; |
| 1059 |
5930 |
if (vg->refs > 0) |
| 1060 |
0 |
return (0); |
| 1061 |
|
|
| 1062 |
5930 |
if (vg->cluster) { |
| 1063 |
25 |
CHECK_OBJ_NOTNULL(vg->cluster, VSM_SEG_MAGIC); |
| 1064 |
25 |
assert(vg->s == NULL); |
| 1065 |
25 |
assert(vg->sz == 0); |
| 1066 |
25 |
assert(vg->cluster->refs > 0); |
| 1067 |
25 |
if (--vg->cluster->refs == 0) { |
| 1068 |
23 |
vsm_unmapseg(vg->cluster); |
| 1069 |
23 |
if (vg->cluster->flags & VSM_FLAG_STALE) { |
| 1070 |
2 |
AN(vg->flags & VSM_FLAG_STALE); |
| 1071 |
2 |
vsm_delseg(vg->cluster, 0); |
| 1072 |
2 |
} |
| 1073 |
23 |
} |
| 1074 |
25 |
vg->b = vg->e = NULL; |
| 1075 |
5905 |
} else { |
| 1076 |
5905 |
vsm_unmapseg(vg); |
| 1077 |
5905 |
} |
| 1078 |
5930 |
if (vg->flags & VSM_FLAG_STALE) |
| 1079 |
147 |
vsm_delseg(vg, 0); |
| 1080 |
5930 |
return (0); |
| 1081 |
5930 |
} |
| 1082 |
|
|
| 1083 |
|
/*--------------------------------------------------------------------*/ |
| 1084 |
|
|
| 1085 |
|
const struct vsm_valid * |
| 1086 |
|
VSM_StillValid(const struct vsm *vd, const struct vsm_fantom *vf) |
| 1087 |
104827 |
{ |
| 1088 |
104827 |
struct vsm_seg *vg; |
| 1089 |
|
|
| 1090 |
104827 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1091 |
104827 |
AN(vf); |
| 1092 |
104827 |
vg = vsm_findseg(vd, vf); |
| 1093 |
104827 |
if (vg == NULL || vg->flags & VSM_FLAG_STALE) |
| 1094 |
762 |
return (VSM_invalid); |
| 1095 |
104065 |
return (VSM_valid); |
| 1096 |
104827 |
} |
| 1097 |
|
|
| 1098 |
|
/*--------------------------------------------------------------------*/ |
| 1099 |
|
|
| 1100 |
|
int |
| 1101 |
|
VSM_Get(struct vsm *vd, struct vsm_fantom *vf, |
| 1102 |
|
const char *category, const char *ident) |
| 1103 |
6142 |
{ |
| 1104 |
|
|
| 1105 |
6142 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1106 |
6142 |
AN(vd->attached); |
| 1107 |
41515 |
VSM_FOREACH(vf, vd) { |
| 1108 |
41515 |
if (vstrcmp(vf->category, category)) |
| 1109 |
38912 |
continue; |
| 1110 |
2603 |
if (ident != NULL && vstrcmp(vf->ident, ident)) |
| 1111 |
0 |
continue; |
| 1112 |
2603 |
return (1); |
| 1113 |
2603 |
} |
| 1114 |
3539 |
memset(vf, 0, sizeof *vf); |
| 1115 |
3539 |
return (0); |
| 1116 |
6142 |
} |
| 1117 |
|
|
| 1118 |
|
/*--------------------------------------------------------------------*/ |
| 1119 |
|
|
| 1120 |
|
char * |
| 1121 |
|
VSM_Dup(struct vsm *vd, const char *category, const char *ident) |
| 1122 |
97 |
{ |
| 1123 |
97 |
struct vsm_fantom vf; |
| 1124 |
97 |
char *p = NULL; |
| 1125 |
|
|
| 1126 |
97 |
CHECK_OBJ_NOTNULL(vd, VSM_MAGIC); |
| 1127 |
97 |
AN(vd->attached); |
| 1128 |
391 |
VSM_FOREACH(&vf, vd) { |
| 1129 |
391 |
if (vstrcmp(vf.category, category)) |
| 1130 |
168 |
continue; |
| 1131 |
223 |
if (ident != NULL && vstrcmp(vf.ident, ident)) |
| 1132 |
126 |
continue; |
| 1133 |
97 |
AZ(VSM_Map(vd, &vf)); |
| 1134 |
97 |
AN(vf.b); |
| 1135 |
97 |
AN(vf.e); |
| 1136 |
97 |
p = malloc((char*)vf.e - (char*)vf.b); |
| 1137 |
97 |
AN(p); |
| 1138 |
97 |
vmemcpy(p, vf.b, (char *)vf.e - (char *)vf.b); |
| 1139 |
97 |
AZ(VSM_Unmap(vd, &vf)); |
| 1140 |
97 |
break; |
| 1141 |
97 |
} |
| 1142 |
97 |
return (p); |
| 1143 |
97 |
} |