1 /* $NetBSD: sem.c,v 1.82 2017/11/27 00:25:46 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratories. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)sem.c 8.1 (Berkeley) 6/6/93 41 */ 42 43 #if HAVE_NBTOOL_CONFIG_H 44 #include "nbtool_config.h" 45 #endif 46 47 #include <sys/cdefs.h> 48 __RCSID("$NetBSD: sem.c,v 1.82 2017/11/27 00:25:46 christos Exp $"); 49 50 #include <sys/param.h> 51 #include <ctype.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <util.h> 56 #include "defs.h" 57 #include "sem.h" 58 59 /* 60 * config semantics. 61 */ 62 63 #define NAMESIZE 100 /* local name buffers */ 64 65 const char *s_ifnet; /* magic attribute */ 66 const char *s_qmark; 67 const char *s_none; 68 69 static struct hashtab *cfhashtab; /* for config lookup */ 70 struct hashtab *devitab; /* etc */ 71 struct attr allattr; 72 size_t nattrs; 73 74 static struct attr errattr; 75 static struct devbase errdev; 76 static struct deva errdeva; 77 78 static int has_errobj(struct attrlist *, struct attr *); 79 static struct nvlist *addtoattr(struct nvlist *, struct devbase *); 80 static int resolve(struct nvlist **, const char *, const char *, 81 struct nvlist *, int); 82 static struct pspec *getpspec(struct attr *, struct devbase *, int, int); 83 static struct devi *newdevi(const char *, int, struct devbase *d); 84 static struct devi *getdevi(const char *); 85 static void remove_devi(struct devi *); 86 static const char *concat(const char *, int); 87 static char *extend(char *, const char *); 88 static int split(const char *, size_t, char *, size_t, int *); 89 static void selectbase(struct devbase *, struct deva *); 90 static const char **fixloc(const char *, struct attr *, struct loclist *); 91 static const char *makedevstr(devmajor_t, devminor_t); 92 static const char *major2name(devmajor_t); 93 static devmajor_t dev2major(struct devbase *); 94 95 extern const char *yyfile; 96 extern int vflag; 97 98 #define V_ATTRIBUTE 0 99 #define V_DEVICE 1 100 struct vtype { 101 int type; 102 struct attr *attr; 103 void *value; 104 }; 105 106 static struct nvlist * 107 makedevstack(struct devbase *d) 108 { 109 struct devi *firsti, *i; 110 struct nvlist *stack = NULL; 111 112 for (firsti = d->d_ihead; firsti != NULL; firsti = firsti->i_bsame) 113 for (i = firsti; i != NULL; i = i->i_alias) 114 stack = newnv(NULL, NULL, i, 0, stack); 115 return stack; 116 } 117 118 static void 119 devcleanup(struct nvlist *stack) 120 { 121 struct nvlist *nv; 122 for (nv = stack; nv != NULL; nv = nv->nv_next) 123 remove_devi(nv->nv_ptr); 124 nvfreel(stack); 125 } 126 127 static void * 128 addvalue(int type, struct attr *a, void *value) 129 { 130 struct vtype *vt = emalloc(sizeof(*vt)); 131 vt->type = type; 132 vt->attr = a; 133 vt->value = value; 134 return vt; 135 } 136 137 void 138 initsem(void) 139 { 140 141 attrtab = ht_new(); 142 attrdeptab = ht_new(); 143 144 allattr.a_name = "netbsd"; 145 TAILQ_INIT(&allattr.a_files); 146 (void)ht_insert(attrtab, allattr.a_name, &allattr); 147 selectattr(&allattr); 148 149 errattr.a_name = "<internal>"; 150 151 TAILQ_INIT(&allbases); 152 153 TAILQ_INIT(&alldevas); 154 155 TAILQ_INIT(&allpspecs); 156 157 cfhashtab = ht_new(); 158 TAILQ_INIT(&allcf); 159 160 TAILQ_INIT(&alldevi); 161 errdev.d_name = "<internal>"; 162 163 TAILQ_INIT(&allpseudo); 164 165 TAILQ_INIT(&alldevms); 166 167 s_ifnet = intern("ifnet"); 168 s_qmark = intern("?"); 169 s_none = intern("none"); 170 } 171 172 /* Name of include file just ended (set in scan.l) */ 173 extern const char *lastfile; 174 175 static struct attr * 176 finddep(struct attr *a, const char *name) 177 { 178 struct attrlist *al; 179 180 for (al = a->a_deps; al != NULL; al = al->al_next) { 181 struct attr *this = al->al_this; 182 if (strcmp(this->a_name, name) == 0) 183 return this; 184 } 185 return NULL; 186 } 187 188 static void 189 mergedeps(const char *dname, const char *name) 190 { 191 struct attr *a, *newa; 192 193 CFGDBG(4, "merging attr `%s' to devbase `%s'", name, dname); 194 a = refattr(dname); 195 if (finddep(a, name) == NULL) { 196 newa = refattr(name); 197 a->a_deps = attrlist_cons(a->a_deps, newa); 198 CFGDBG(3, "attr `%s' merged to attr `%s'", newa->a_name, 199 a->a_name); 200 } 201 } 202 203 static void 204 fixdev(struct devbase *dev) 205 { 206 struct attrlist *al; 207 struct attr *devattr, *a; 208 209 devattr = refattr(dev->d_name); 210 if (devattr->a_devclass) 211 panic("%s: dev %s is devclass!", __func__, devattr->a_name); 212 213 CFGDBG(4, "fixing devbase `%s'", dev->d_name); 214 for (al = dev->d_attrs; al != NULL; al = al->al_next) { 215 a = al->al_this; 216 CFGDBG(4, "fixing devbase `%s' attr `%s'", dev->d_name, 217 a->a_name); 218 if (a->a_iattr) { 219 a->a_refs = addtoattr(a->a_refs, dev); 220 CFGDBG(3, "device `%s' has iattr `%s'", dev->d_name, 221 a->a_name); 222 } else if (a->a_devclass != NULL) { 223 if (dev->d_classattr != NULL && dev->d_classattr != a) { 224 cfgwarn("device `%s' has multiple classes " 225 "(`%s' and `%s')", 226 dev->d_name, dev->d_classattr->a_name, 227 a->a_name); 228 } 229 if (dev->d_classattr == NULL) { 230 dev->d_classattr = a; 231 CFGDBG(3, "device `%s' is devclass `%s'", 232 dev->d_name, a->a_name); 233 } 234 } else { 235 if (strcmp(dev->d_name, a->a_name) != 0) { 236 mergedeps(dev->d_name, a->a_name); 237 } 238 } 239 } 240 } 241 242 void 243 enddefs(void) 244 { 245 struct devbase *dev; 246 247 yyfile = "enddefs"; 248 249 TAILQ_FOREACH(dev, &allbases, d_next) { 250 if (!dev->d_isdef) { 251 (void)fprintf(stderr, 252 "%s: device `%s' used but not defined\n", 253 lastfile, dev->d_name); 254 errors++; 255 continue; 256 } 257 fixdev(dev); 258 } 259 if (errors) { 260 (void)fprintf(stderr, "*** Stop.\n"); 261 exit(1); 262 } 263 } 264 265 void 266 setdefmaxusers(int min, int def, int max) 267 { 268 269 if (min < 1 || min > def || def > max) 270 cfgerror("maxusers must have 1 <= min (%d) <= default (%d) " 271 "<= max (%d)", min, def, max); 272 else { 273 minmaxusers = min; 274 defmaxusers = def; 275 maxmaxusers = max; 276 } 277 } 278 279 void 280 setmaxusers(int n) 281 { 282 283 if (maxusers == n) { 284 cfgerror("duplicate maxusers parameter"); 285 return; 286 } 287 if (vflag && maxusers != 0) 288 cfgwarn("warning: maxusers already defined"); 289 maxusers = n; 290 if (n < minmaxusers) { 291 cfgerror("warning: minimum of %d maxusers assumed", 292 minmaxusers); 293 errors--; /* take it away */ 294 maxusers = minmaxusers; 295 } else if (n > maxmaxusers) { 296 cfgerror("warning: maxusers (%d) > %d", n, maxmaxusers); 297 errors--; 298 } 299 } 300 301 void 302 setident(const char *i) 303 { 304 305 if (i) 306 ident = intern(i); 307 else 308 ident = NULL; 309 } 310 311 /* 312 * Define an attribute, optionally with an interface (a locator list) 313 * and a set of attribute-dependencies. 314 * 315 * Attribute dependencies MAY NOT be interface attributes. 316 * 317 * Since an empty locator list is logically different from "no interface", 318 * all locator lists include a dummy head node, which we discard here. 319 */ 320 int 321 defattr0(const char *name, struct loclist *locs, struct attrlist *deps, 322 int devclass) 323 { 324 325 if (locs != NULL) 326 return defiattr(name, locs, deps, devclass); 327 else if (devclass) 328 return defdevclass(name, locs, deps, devclass); 329 else 330 return defattr(name, locs, deps, devclass); 331 } 332 333 int 334 defattr(const char *name, struct loclist *locs, struct attrlist *deps, 335 int devclass) 336 { 337 struct attr *a, *dep; 338 struct attrlist *al; 339 340 if (getrefattr(name, &a)) { 341 cfgerror("attribute `%s' already defined", name); 342 loclist_destroy(locs); 343 return (1); 344 } 345 if (a == NULL) 346 a = mkattr(name); 347 348 /* 349 * If this attribute depends on any others, make sure none of 350 * the dependencies are interface attributes. 351 */ 352 for (al = deps; al != NULL; al = al->al_next) { 353 dep = al->al_this; 354 if (dep->a_iattr) { 355 cfgerror("`%s' dependency `%s' is an interface " 356 "attribute", name, dep->a_name); 357 return (1); 358 } 359 (void)ht_insert2(attrdeptab, name, dep->a_name, 360 addvalue(V_ATTRIBUTE, a, dep)); 361 CFGDBG(2, "attr `%s' depends on attr `%s'", name, dep->a_name); 362 } 363 364 365 a->a_deps = deps; 366 expandattr(a, NULL); 367 CFGDBG(3, "attr `%s' defined", a->a_name); 368 369 return (0); 370 } 371 372 struct attr * 373 mkattr(const char *name) 374 { 375 struct attr *a; 376 377 a = ecalloc(1, sizeof *a); 378 if (ht_insert(attrtab, name, a)) { 379 free(a); 380 return NULL; 381 } 382 a->a_name = name; 383 TAILQ_INIT(&a->a_files); 384 CFGDBG(3, "attr `%s' allocated", name); 385 386 return a; 387 } 388 389 /* "interface attribute" initialization */ 390 int 391 defiattr(const char *name, struct loclist *locs, struct attrlist *deps, 392 int devclass) 393 { 394 struct attr *a; 395 int len; 396 struct loclist *ll; 397 398 if (devclass) 399 panic("%s: %s has both locators and devclass", __func__, name); 400 401 if (defattr(name, locs, deps, devclass) != 0) 402 return (1); 403 404 a = getattr(name); 405 a->a_iattr = 1; 406 /* unwrap */ 407 a->a_locs = locs->ll_next; 408 locs->ll_next = NULL; 409 loclist_destroy(locs); 410 len = 0; 411 for (ll = a->a_locs; ll != NULL; ll = ll->ll_next) 412 len++; 413 a->a_loclen = len; 414 if (deps) 415 CFGDBG(2, "attr `%s' iface with deps", a->a_name); 416 return (0); 417 } 418 419 /* "device class" initialization */ 420 int 421 defdevclass(const char *name, struct loclist *locs, struct attrlist *deps, 422 int devclass) 423 { 424 struct attr *a; 425 char classenum[256], *cp; 426 int errored = 0; 427 428 if (deps) 429 panic("%s: %s has both dependencies and devclass", __func__, 430 name); 431 432 if (defattr(name, locs, deps, devclass) != 0) 433 return (1); 434 435 a = getattr(name); 436 (void)snprintf(classenum, sizeof(classenum), "DV_%s", name); 437 for (cp = classenum + 3; *cp; cp++) { 438 if (!errored && (!isalnum((unsigned char)*cp) || 439 (isalpha((unsigned char)*cp) 440 && !islower((unsigned char)*cp)))) { 441 cfgerror("device class names must be " 442 "lower-case alphanumeric characters"); 443 errored = 1; 444 } 445 *cp = (char)toupper((unsigned char)*cp); 446 } 447 a->a_devclass = intern(classenum); 448 449 return (0); 450 } 451 452 /* 453 * Return true if the given `error object' is embedded in the given 454 * pointer list. 455 */ 456 static int 457 has_errobj(struct attrlist *al, struct attr *obj) 458 { 459 460 for (; al != NULL; al = al->al_next) 461 if (al->al_this == obj) 462 return (1); 463 return (0); 464 } 465 466 /* 467 * Return true if the given attribute is embedded in the given 468 * pointer list. 469 */ 470 int 471 has_attr(struct attrlist *al, const char *attr) 472 { 473 struct attr *a; 474 475 if ((a = getattr(attr)) == NULL) 476 return (0); 477 478 for (; al != NULL; al = al->al_next) 479 if (al->al_this == a) 480 return (1); 481 return (0); 482 } 483 484 /* 485 * Add a device base to a list in an attribute (actually, to any list). 486 * Note that this does not check for duplicates, and does reverse the 487 * list order, but no one cares anyway. 488 */ 489 static struct nvlist * 490 addtoattr(struct nvlist *l, struct devbase *dev) 491 { 492 struct nvlist *n; 493 494 n = newnv(NULL, NULL, dev, 0, l); 495 return (n); 496 } 497 498 /* 499 * Define a device. This may (or may not) also define an interface 500 * attribute and/or refer to existing attributes. 501 */ 502 void 503 defdev(struct devbase *dev, struct loclist *loclist, struct attrlist *attrs, 504 int ispseudo) 505 { 506 struct loclist *ll; 507 struct attrlist *al; 508 509 if (dev == &errdev) 510 goto bad; 511 if (dev->d_isdef) { 512 cfgerror("redefinition of `%s'", dev->d_name); 513 goto bad; 514 } 515 516 dev->d_isdef = 1; 517 if (has_errobj(attrs, &errattr)) 518 goto bad; 519 520 /* 521 * Handle implicit attribute definition from locator list. Do 522 * this before scanning the `at' list so that we can have, e.g.: 523 * device foo at other, foo { slot = -1 } 524 * (where you can plug in a foo-bus extender to a foo-bus). 525 */ 526 if (loclist != NULL) { 527 ll = loclist; 528 loclist = NULL; /* defattr disposes of them for us */ 529 if (defiattr(dev->d_name, ll, NULL, 0)) 530 goto bad; 531 attrs = attrlist_cons(attrs, getattr(dev->d_name)); 532 /* This used to be stored but was never used */ 533 /* attrs->al_name = dev->d_name; */ 534 } 535 536 /* 537 * Pseudo-devices can have children. Consider them as 538 * attaching at root. 539 */ 540 if (ispseudo) { 541 for (al = attrs; al != NULL; al = al->al_next) 542 if (al->al_this->a_iattr) 543 break; 544 if (al != NULL) { 545 if (ispseudo < 2) { 546 if (version >= 20080610) 547 cfgerror("interface attribute on " 548 "non-device pseudo `%s'", dev->d_name); 549 else { 550 ispseudo = 2; 551 } 552 } 553 ht_insert(devroottab, dev->d_name, dev); 554 } 555 } 556 557 /* Committed! Set up fields. */ 558 dev->d_ispseudo = ispseudo; 559 dev->d_attrs = attrs; 560 dev->d_classattr = NULL; /* for now */ 561 CFGDBG(3, "dev `%s' defined", dev->d_name); 562 563 /* 564 * Implicit attribute definition for device. 565 */ 566 refattr(dev->d_name); 567 568 /* 569 * For each interface attribute this device refers to, add this 570 * device to its reference list. This makes, e.g., finding all 571 * "scsi"s easier. 572 * 573 * While looking through the attributes, set up the device 574 * class if any are devclass attributes (and error out if the 575 * device has two classes). 576 */ 577 for (al = attrs; al != NULL; al = al->al_next) { 578 /* 579 * Implicit attribute definition for device dependencies. 580 */ 581 refattr(al->al_this->a_name); 582 (void)ht_insert2(attrdeptab, dev->d_name, al->al_this->a_name, 583 addvalue(V_DEVICE, al->al_this, dev)); 584 CFGDBG(2, "device `%s' depends on attr `%s'", dev->d_name, 585 al->al_this->a_name); 586 } 587 return; 588 bad: 589 loclist_destroy(loclist); 590 attrlist_destroyall(attrs); 591 } 592 593 /* 594 * Look up a devbase. Also makes sure it is a reasonable name, 595 * i.e., does not end in a digit or contain special characters. 596 */ 597 struct devbase * 598 getdevbase(const char *name) 599 { 600 const u_char *p; 601 struct devbase *dev; 602 603 p = (const u_char *)name; 604 if (!isalpha(*p)) 605 goto badname; 606 while (*++p) { 607 if (!isalnum(*p) && *p != '_') 608 goto badname; 609 } 610 if (isdigit(*--p)) { 611 badname: 612 cfgerror("bad device base name `%s'", name); 613 return (&errdev); 614 } 615 dev = ht_lookup(devbasetab, name); 616 if (dev == NULL) { 617 dev = ecalloc(1, sizeof *dev); 618 dev->d_name = name; 619 dev->d_isdef = 0; 620 dev->d_major = NODEVMAJOR; 621 dev->d_attrs = NULL; 622 dev->d_ihead = NULL; 623 dev->d_ipp = &dev->d_ihead; 624 dev->d_ahead = NULL; 625 dev->d_app = &dev->d_ahead; 626 dev->d_umax = 0; 627 TAILQ_INSERT_TAIL(&allbases, dev, d_next); 628 if (ht_insert(devbasetab, name, dev)) 629 panic("%s: Can't insert %s", __func__, name); 630 CFGDBG(3, "devbase defined `%s'", dev->d_name); 631 } 632 return (dev); 633 } 634 635 /* 636 * Define some of a device's allowable parent attachments. 637 * There may be a list of (plain) attributes. 638 */ 639 void 640 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist, 641 struct attrlist *attrs) 642 { 643 struct nvlist *nv; 644 struct attrlist *al; 645 struct attr *a; 646 647 if (dev == &errdev) 648 goto bad; 649 if (deva == NULL) 650 deva = getdevattach(dev->d_name); 651 if (deva == &errdeva) 652 goto bad; 653 if (!dev->d_isdef) { 654 cfgerror("attaching undefined device `%s'", dev->d_name); 655 goto bad; 656 } 657 if (deva->d_isdef) { 658 cfgerror("redefinition of `%s'", deva->d_name); 659 goto bad; 660 } 661 if (dev->d_ispseudo) { 662 cfgerror("pseudo-devices can't attach"); 663 goto bad; 664 } 665 666 deva->d_isdef = 1; 667 if (has_errobj(attrs, &errattr)) 668 goto bad; 669 for (al = attrs; al != NULL; al = al->al_next) { 670 a = al->al_this; 671 if (a == &errattr) 672 continue; /* already complained */ 673 if (a->a_iattr || a->a_devclass != NULL) 674 cfgerror("`%s' is not a plain attribute", a->a_name); 675 } 676 677 /* Committed! Set up fields. */ 678 deva->d_attrs = attrs; 679 deva->d_atlist = atlist; 680 deva->d_devbase = dev; 681 CFGDBG(3, "deva `%s' defined", deva->d_name); 682 683 /* 684 * Implicit attribute definition for device attachment. 685 */ 686 refattr(deva->d_name); 687 688 /* 689 * Turn the `at' list into interface attributes (map each 690 * nv_name to an attribute, or to NULL for root), and add 691 * this device to those attributes, so that children can 692 * be listed at this particular device if they are supported 693 * by that attribute. 694 */ 695 for (nv = atlist; nv != NULL; nv = nv->nv_next) { 696 if (nv->nv_name == NULL) 697 nv->nv_ptr = a = NULL; /* at root */ 698 else 699 nv->nv_ptr = a = getattr(nv->nv_name); 700 if (a == &errattr) 701 continue; /* already complained */ 702 703 #if 0 704 /* 705 * Make sure that an attachment spec doesn't 706 * already say how to attach to this attribute. 707 */ 708 for (struct deva *da = dev->d_ahead; da; da = da->d_bsame) 709 if (onlist(da->d_atlist, a)) 710 cfgerror("attach at `%s' already done by `%s'", 711 a ? a->a_name : "root", da->d_name); 712 #endif 713 714 if (a == NULL) { 715 ht_insert(devroottab, dev->d_name, dev); 716 continue; /* at root; don't add */ 717 } 718 if (!a->a_iattr) 719 cfgerror("%s cannot be at plain attribute `%s'", 720 dev->d_name, a->a_name); 721 else 722 a->a_devs = addtoattr(a->a_devs, dev); 723 } 724 725 /* attach to parent */ 726 *dev->d_app = deva; 727 dev->d_app = &deva->d_bsame; 728 return; 729 bad: 730 nvfreel(atlist); 731 attrlist_destroyall(attrs); 732 } 733 734 /* 735 * Look up a device attachment. Also makes sure it is a reasonable 736 * name, i.e., does not contain digits or special characters. 737 */ 738 struct deva * 739 getdevattach(const char *name) 740 { 741 const u_char *p; 742 struct deva *deva; 743 744 p = (const u_char *)name; 745 if (!isalpha(*p)) 746 goto badname; 747 while (*++p) { 748 if (!isalnum(*p) && *p != '_') 749 goto badname; 750 } 751 if (isdigit(*--p)) { 752 badname: 753 cfgerror("bad device attachment name `%s'", name); 754 return (&errdeva); 755 } 756 deva = ht_lookup(devatab, name); 757 if (deva == NULL) { 758 deva = ecalloc(1, sizeof *deva); 759 deva->d_name = name; 760 deva->d_bsame = NULL; 761 deva->d_isdef = 0; 762 deva->d_devbase = NULL; 763 deva->d_atlist = NULL; 764 deva->d_attrs = NULL; 765 deva->d_ihead = NULL; 766 deva->d_ipp = &deva->d_ihead; 767 TAILQ_INSERT_TAIL(&alldevas, deva, d_next); 768 if (ht_insert(devatab, name, deva)) 769 panic("%s: Can't insert %s", __func__, name); 770 } 771 return (deva); 772 } 773 774 /* 775 * Look up an attribute. 776 */ 777 struct attr * 778 getattr(const char *name) 779 { 780 struct attr *a; 781 782 if ((a = ht_lookup(attrtab, name)) == NULL) { 783 cfgerror("undefined attribute `%s'", name); 784 a = &errattr; 785 } 786 return (a); 787 } 788 789 /* 790 * Implicit attribute definition. 791 */ 792 struct attr * 793 refattr(const char *name) 794 { 795 struct attr *a; 796 797 if ((a = ht_lookup(attrtab, name)) == NULL) 798 a = mkattr(name); 799 return a; 800 } 801 802 int 803 getrefattr(const char *name, struct attr **ra) 804 { 805 struct attr *a; 806 807 a = ht_lookup(attrtab, name); 808 if (a == NULL) { 809 *ra = NULL; 810 return (0); 811 } 812 /* 813 * Check if the existing attr is only referenced, not really defined. 814 */ 815 if (a->a_deps == NULL && 816 a->a_iattr == 0 && 817 a->a_devclass == 0) { 818 *ra = a; 819 return (0); 820 } 821 return (1); 822 } 823 824 /* 825 * Recursively expand an attribute and its dependencies, checking for 826 * cycles, and invoking a callback for each attribute found. 827 */ 828 void 829 expandattr(struct attr *a, void (*callback)(struct attr *)) 830 { 831 struct attrlist *al; 832 struct attr *dep; 833 834 if (a->a_expanding) { 835 cfgerror("circular dependency on attribute `%s'", a->a_name); 836 return; 837 } 838 839 a->a_expanding = 1; 840 841 /* First expand all of this attribute's dependencies. */ 842 for (al = a->a_deps; al != NULL; al = al->al_next) { 843 dep = al->al_this; 844 expandattr(dep, callback); 845 } 846 847 /* ...and now invoke the callback for ourself. */ 848 if (callback != NULL) 849 (*callback)(a); 850 851 a->a_expanding = 0; 852 } 853 854 /* 855 * Set the major device number for a device, so that it can be used 856 * as a root/dumps "on" device in a configuration. 857 */ 858 void 859 setmajor(struct devbase *d, devmajor_t n) 860 { 861 862 if (d != &errdev && d->d_major != NODEVMAJOR) 863 cfgerror("device `%s' is already major %d", 864 d->d_name, d->d_major); 865 else 866 d->d_major = n; 867 } 868 869 const char * 870 major2name(devmajor_t maj) 871 { 872 struct devbase *dev; 873 struct devm *dm; 874 875 if (!do_devsw) { 876 TAILQ_FOREACH(dev, &allbases, d_next) { 877 if (dev->d_major == maj) 878 return (dev->d_name); 879 } 880 } else { 881 TAILQ_FOREACH(dm, &alldevms, dm_next) { 882 if (dm->dm_bmajor == maj) 883 return (dm->dm_name); 884 } 885 } 886 return (NULL); 887 } 888 889 devmajor_t 890 dev2major(struct devbase *dev) 891 { 892 struct devm *dm; 893 894 if (!do_devsw) 895 return (dev->d_major); 896 897 TAILQ_FOREACH(dm, &alldevms, dm_next) { 898 if (strcmp(dm->dm_name, dev->d_name) == 0) 899 return (dm->dm_bmajor); 900 } 901 return (NODEVMAJOR); 902 } 903 904 /* 905 * Make a string description of the device at maj/min. 906 */ 907 static const char * 908 makedevstr(devmajor_t maj, devminor_t min) 909 { 910 const char *devicename; 911 char buf[32]; 912 913 devicename = major2name(maj); 914 if (devicename == NULL) 915 (void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min); 916 else 917 (void)snprintf(buf, sizeof(buf), "%s%d%c", devicename, 918 min / maxpartitions, (min % maxpartitions) + 'a'); 919 920 return (intern(buf)); 921 } 922 923 /* 924 * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a'). 925 * Handle the case where the device number is given but there is no 926 * corresponding name, and map NULL to the default. 927 */ 928 static int 929 resolve(struct nvlist **nvp, const char *name, const char *what, 930 struct nvlist *dflt, int part) 931 { 932 struct nvlist *nv; 933 struct devbase *dev; 934 const char *cp; 935 devmajor_t maj; 936 devminor_t min; 937 size_t i, l; 938 int unit; 939 char buf[NAMESIZE]; 940 941 if ((part -= 'a') >= maxpartitions || part < 0) 942 panic("%s: Bad partition %c", __func__, part); 943 if ((nv = *nvp) == NULL) { 944 dev_t d = NODEV; 945 /* 946 * Apply default. Easiest to do this by number. 947 * Make sure to retain NODEVness, if this is dflt's disposition. 948 */ 949 if ((dev_t)dflt->nv_num != NODEV) { 950 maj = major(dflt->nv_num); 951 min = ((minor(dflt->nv_num) / maxpartitions) * 952 maxpartitions) + part; 953 d = makedev(maj, min); 954 cp = makedevstr(maj, min); 955 } else 956 cp = NULL; 957 *nvp = nv = newnv(NULL, cp, NULL, (long long)d, NULL); 958 } 959 if ((dev_t)nv->nv_num != NODEV) { 960 /* 961 * By the numbers. Find the appropriate major number 962 * to make a name. 963 */ 964 maj = major(nv->nv_num); 965 min = minor(nv->nv_num); 966 nv->nv_str = makedevstr(maj, min); 967 return (0); 968 } 969 970 if (nv->nv_str == NULL || nv->nv_str == s_qmark) 971 /* 972 * Wildcarded or unspecified; leave it as NODEV. 973 */ 974 return (0); 975 976 if (nv->nv_ptr != NULL && strcmp(nv->nv_ptr, "spec") == 0) 977 /* 978 * spec string, interpreted by kernel 979 */ 980 return (0); 981 982 /* 983 * The normal case: things like "ra2b". Check for partition 984 * suffix, remove it if there, and split into name ("ra") and 985 * unit (2). 986 */ 987 l = i = strlen(nv->nv_str); 988 cp = &nv->nv_str[l]; 989 if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions && 990 isdigit((unsigned char)cp[-1])) { 991 l--; 992 part = *cp - 'a'; 993 } 994 cp = nv->nv_str; 995 if (split(cp, l, buf, sizeof buf, &unit)) { 996 cfgerror("%s: invalid %s device name `%s'", name, what, cp); 997 return (1); 998 } 999 dev = ht_lookup(devbasetab, intern(buf)); 1000 if (dev == NULL) { 1001 cfgerror("%s: device `%s' does not exist", name, buf); 1002 return (1); 1003 } 1004 1005 /* 1006 * Check for the magic network interface attribute, and 1007 * don't bother making a device number. 1008 */ 1009 if (has_attr(dev->d_attrs, s_ifnet)) { 1010 nv->nv_num = (long long)NODEV; 1011 nv->nv_ifunit = unit; /* XXX XXX XXX */ 1012 } else { 1013 maj = dev2major(dev); 1014 if (maj == NODEVMAJOR) { 1015 cfgerror("%s: can't make %s device from `%s'", 1016 name, what, nv->nv_str); 1017 return (1); 1018 } 1019 nv->nv_num = (long long)makedev(maj, unit * maxpartitions + part); 1020 } 1021 1022 nv->nv_name = dev->d_name; 1023 return (0); 1024 } 1025 1026 /* 1027 * Add a completed configuration to the list. 1028 */ 1029 void 1030 addconf(struct config *cf0) 1031 { 1032 struct config *cf; 1033 const char *name; 1034 1035 name = cf0->cf_name; 1036 cf = ecalloc(1, sizeof *cf); 1037 if (ht_insert(cfhashtab, name, cf)) { 1038 cfgerror("configuration `%s' already defined", name); 1039 free(cf); 1040 goto bad; 1041 } 1042 *cf = *cf0; 1043 1044 /* 1045 * Resolve the root device. 1046 */ 1047 if (cf->cf_root == NULL) { 1048 cfgerror("%s: no root device specified", name); 1049 goto bad; 1050 } 1051 if (cf->cf_root && cf->cf_root->nv_str != s_qmark) { 1052 struct nvlist *nv; 1053 nv = cf->cf_root; 1054 if (resolve(&cf->cf_root, name, "root", nv, 'a')) 1055 goto bad; 1056 } 1057 1058 /* 1059 * Resolve the dump device. 1060 */ 1061 if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) { 1062 /* 1063 * Wildcarded dump device is equivalent to unspecified. 1064 */ 1065 cf->cf_dump = NULL; 1066 } else if (cf->cf_dump->nv_str == s_none) { 1067 /* 1068 * Operator has requested that no dump device should be 1069 * configured; do nothing. 1070 */ 1071 } else { 1072 if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b')) 1073 goto bad; 1074 } 1075 1076 /* Wildcarded fstype is `unspecified'. */ 1077 if (cf->cf_fstype == s_qmark) 1078 cf->cf_fstype = NULL; 1079 1080 TAILQ_INSERT_TAIL(&allcf, cf, cf_next); 1081 return; 1082 bad: 1083 nvfreel(cf0->cf_root); 1084 nvfreel(cf0->cf_dump); 1085 } 1086 1087 void 1088 setconf(struct nvlist **npp, const char *what, struct nvlist *v) 1089 { 1090 1091 if (*npp != NULL) { 1092 cfgerror("duplicate %s specification", what); 1093 nvfreel(v); 1094 } else 1095 *npp = v; 1096 } 1097 1098 void 1099 delconf(const char *name, int nowarn) 1100 { 1101 struct config *cf; 1102 1103 CFGDBG(5, "deselecting config `%s'", name); 1104 if (ht_lookup(cfhashtab, name) == NULL) { 1105 if (!nowarn) 1106 cfgerror("configuration `%s' undefined", name); 1107 return; 1108 } 1109 (void)ht_remove(cfhashtab, name); 1110 1111 TAILQ_FOREACH(cf, &allcf, cf_next) 1112 if (!strcmp(cf->cf_name, name)) 1113 break; 1114 if (cf == NULL) 1115 panic("%s: lost configuration for %s", __func__, name); 1116 1117 TAILQ_REMOVE(&allcf, cf, cf_next); 1118 } 1119 1120 void 1121 setfstype(const char **fstp, const char *v) 1122 { 1123 1124 if (*fstp != NULL) { 1125 cfgerror("multiple fstype specifications"); 1126 return; 1127 } 1128 1129 if (v != s_qmark && OPT_FSOPT(v)) { 1130 cfgerror("\"%s\" is not a configured file system", v); 1131 return; 1132 } 1133 1134 *fstp = v; 1135 } 1136 1137 static struct devi * 1138 newdevi(const char *name, int unit, struct devbase *d) 1139 { 1140 struct devi *i; 1141 1142 i = ecalloc(1, sizeof *i); 1143 i->i_name = name; 1144 i->i_unit = unit; 1145 i->i_base = d; 1146 i->i_bsame = NULL; 1147 i->i_asame = NULL; 1148 i->i_alias = NULL; 1149 i->i_at = NULL; 1150 i->i_pspec = NULL; 1151 i->i_atdeva = NULL; 1152 i->i_locs = NULL; 1153 i->i_cfflags = 0; 1154 i->i_lineno = currentline(); 1155 i->i_srcfile = yyfile; 1156 i->i_active = DEVI_ORPHAN; /* Proper analysis comes later */ 1157 i->i_level = devilevel; 1158 i->i_pseudoroot = 0; 1159 if (unit >= d->d_umax) 1160 d->d_umax = unit + 1; 1161 return (i); 1162 } 1163 1164 static struct attr * 1165 finddevattr(const char *name, const char *at, struct devbase *ib, 1166 struct devbase **ab, int *atunit) 1167 { 1168 const char *cp; 1169 char atbuf[NAMESIZE]; 1170 struct attrlist *al; 1171 struct attr *attr; 1172 1173 if (at == NULL) { 1174 *ab = NULL; 1175 *atunit = -1; 1176 return &errattr; /* a convenient "empty" attr */ 1177 } 1178 if (split(at, strlen(at), atbuf, sizeof atbuf, atunit)) { 1179 cfgerror("invalid attachment name `%s'", at); 1180 /* (void)getdevi(name); -- ??? */ 1181 return NULL; 1182 } 1183 1184 /* 1185 * Devices can attach to two types of things: Attributes, 1186 * and other devices (which have the appropriate attributes 1187 * to allow attachment). 1188 * 1189 * (1) If we're attached to an attribute, then we don't need 1190 * look at the parent base device to see what attributes 1191 * it has, and make sure that we can attach to them. 1192 * 1193 * (2) If we're attached to a real device (i.e. named in 1194 * the config file), we want to remember that so that 1195 * at cross-check time, if the device we're attached to 1196 * is missing but other devices which also provide the 1197 * attribute are present, we don't get a false "OK." 1198 * 1199 * (3) If the thing we're attached to is an attribute 1200 * but is actually named in the config file, we still 1201 * have to remember its devbase. 1202 */ 1203 cp = intern(atbuf); 1204 1205 /* Figure out parent's devbase, to satisfy case (3). */ 1206 *ab = ht_lookup(devbasetab, cp); 1207 1208 /* Find out if it's an attribute. */ 1209 attr = ht_lookup(attrtab, cp); 1210 1211 /* Make sure we're _really_ attached to the attr. Case (1). */ 1212 if (attr != NULL && onlist(attr->a_devs, ib)) 1213 return attr; 1214 1215 /* 1216 * Else a real device, and not just an attribute. Case (2). 1217 * 1218 * Have to work a bit harder to see whether we have 1219 * something like "tg0 at esp0" (where esp is merely 1220 * not an attribute) or "tg0 at nonesuch0" (where 1221 * nonesuch is not even a device). 1222 */ 1223 if (*ab == NULL) { 1224 cfgerror("%s at %s: `%s' unknown", name, at, atbuf); 1225 return NULL; 1226 } 1227 1228 /* 1229 * See if the named parent carries an attribute 1230 * that allows it to supervise device ib. 1231 */ 1232 for (al = (*ab)->d_attrs; al != NULL; al = al->al_next) { 1233 attr = al->al_this; 1234 if (onlist(attr->a_devs, ib)) 1235 return attr; 1236 } 1237 cfgerror("`%s' cannot attach to `%s'", ib->d_name, atbuf); 1238 return NULL; 1239 } 1240 1241 /* 1242 * Add the named device as attaching to the named attribute (or perhaps 1243 * another device instead) plus unit number. 1244 */ 1245 void 1246 adddev(const char *name, const char *at, struct loclist *loclist, int flags) 1247 { 1248 struct devi *i; /* the new instance */ 1249 struct pspec *p; /* and its pspec */ 1250 struct attr *attr; /* attribute that allows attach */ 1251 struct devbase *ib; /* i->i_base */ 1252 struct devbase *ab; /* not NULL => at another dev */ 1253 struct deva *iba; /* devbase attachment used */ 1254 struct deva *lastiba; 1255 int atunit, first; 1256 1257 lastiba = NULL; 1258 if ((i = getdevi(name)) == NULL) 1259 goto bad; 1260 ib = i->i_base; 1261 attr = finddevattr(name, at, ib, &ab, &atunit); 1262 if (attr == NULL) { 1263 i->i_active = DEVI_BROKEN; 1264 goto bad; 1265 } 1266 1267 for (lastiba = ib->d_ahead; lastiba; lastiba = iba->d_bsame) { 1268 for (iba = lastiba; iba != NULL; iba = iba->d_bsame) 1269 if (onlist(iba->d_atlist, 1270 attr == &errattr ? NULL : attr)) 1271 break; 1272 1273 first = lastiba == ib->d_ahead; 1274 if (iba == NULL) { 1275 if (!first) 1276 goto bad; 1277 if (attr != &errattr) { 1278 panic("%s: can't figure out attachment", 1279 __func__); 1280 } else { 1281 cfgerror("`%s' cannot attach to the root", 1282 ib->d_name); 1283 i->i_active = DEVI_BROKEN; 1284 } 1285 } 1286 // get a new one if it is not the first time 1287 if (!first && (i = getdevi(name)) == NULL) 1288 goto bad; 1289 1290 if (attr != &errattr) { 1291 /* 1292 * Find the parent spec. If a matching one has not 1293 * yet been created, create one. 1294 * 1295 * XXX: This creates multiple pspecs that look the 1296 * same in the config file and could be merged. 1297 */ 1298 p = getpspec(attr, ab, atunit, first); 1299 p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs); 1300 } else 1301 p = NULL; 1302 1303 if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) { 1304 i->i_active = DEVI_BROKEN; 1305 goto bad; 1306 } 1307 i->i_at = at; 1308 i->i_pspec = p; 1309 i->i_atdeva = iba; 1310 i->i_cfflags = flags; 1311 CFGDBG(3, "devi `%s' at '%s' added", i->i_name, iba->d_name); 1312 1313 *iba->d_ipp = i; 1314 iba->d_ipp = &i->i_asame; 1315 } 1316 1317 /* all done, fall into ... */ 1318 bad: 1319 loclist_destroy(loclist); 1320 return; 1321 } 1322 1323 void 1324 deldevi(const char *name, const char *at, int nowarn) 1325 { 1326 struct devi *firsti, *i; 1327 struct devbase *d; 1328 int unit; 1329 char base[NAMESIZE]; 1330 1331 CFGDBG(5, "deselecting devi `%s'", name); 1332 if (split(name, strlen(name), base, sizeof base, &unit)) { 1333 if (!nowarn) { 1334 cfgerror("invalid device name `%s'", name); 1335 return; 1336 } 1337 } 1338 d = ht_lookup(devbasetab, intern(base)); 1339 if (d == NULL) { 1340 if (!nowarn) 1341 cfgerror("%s: unknown device `%s'", name, base); 1342 return; 1343 } 1344 if (d->d_ispseudo) { 1345 cfgerror("%s: %s is a pseudo-device", name, base); 1346 return; 1347 } 1348 if ((firsti = ht_lookup(devitab, name)) == NULL) { 1349 cfgerror("`%s' not defined", name); 1350 return; 1351 } 1352 if (at == NULL && firsti->i_at == NULL) { 1353 /* 'at root' */ 1354 remove_devi(firsti); 1355 return; 1356 } else if (at != NULL) 1357 for (i = firsti; i != NULL; i = i->i_alias) 1358 if (i->i_active != DEVI_BROKEN && 1359 strcmp(at, i->i_at) == 0) { 1360 remove_devi(i); 1361 return; 1362 } 1363 cfgerror("`%s' at `%s' not found", name, at ? at : "root"); 1364 } 1365 1366 static void 1367 remove_pspec(struct devi *i) 1368 { 1369 struct pspec *p = i->i_pspec; 1370 struct nvlist *nv, *onv; 1371 1372 if (p == NULL) 1373 return; 1374 1375 /* Double-linked nvlist anyone? */ 1376 for (nv = p->p_devs; nv->nv_next != NULL; nv = nv->nv_next) { 1377 if (nv->nv_next && nv->nv_next->nv_ptr == i) { 1378 onv = nv->nv_next; 1379 nv->nv_next = onv->nv_next; 1380 nvfree(onv); 1381 break; 1382 } 1383 if (nv->nv_ptr == i) { 1384 /* nv is p->p_devs in that case */ 1385 p->p_devs = nv->nv_next; 1386 nvfree(nv); 1387 break; 1388 } 1389 } 1390 if (p->p_devs == NULL) 1391 TAILQ_REMOVE(&allpspecs, p, p_list); 1392 } 1393 1394 static void 1395 remove_devi(struct devi *i) 1396 { 1397 struct devbase *d = i->i_base; 1398 struct devi *f, *j, **ppi; 1399 struct deva *iba; 1400 1401 CFGDBG(5, "removing devi `%s'", i->i_name); 1402 f = ht_lookup(devitab, i->i_name); 1403 if (f == NULL) 1404 panic("%s: instance %s disappeared from devitab", __func__, 1405 i->i_name); 1406 1407 if (i->i_active == DEVI_BROKEN) { 1408 cfgerror("not removing broken instance `%s'", i->i_name); 1409 return; 1410 } 1411 1412 /* 1413 * We have the device instance, i. 1414 * We have to: 1415 * - delete the alias 1416 * 1417 * If the devi was an alias of an already listed devi, all is 1418 * good we don't have to do more. 1419 * If it was the first alias, we have to replace i's entry in 1420 * d's list by its first alias. 1421 * If it was the only entry, we must remove i's entry from d's 1422 * list. 1423 */ 1424 if (i != f) { 1425 for (j = f; j->i_alias != i; j = j->i_alias) 1426 continue; 1427 j->i_alias = i->i_alias; 1428 } else { 1429 if (i->i_alias == NULL) { 1430 /* No alias, must unlink the entry from devitab */ 1431 ht_remove(devitab, i->i_name); 1432 j = i->i_bsame; 1433 } else { 1434 /* Or have the first alias replace i in d's list */ 1435 i->i_alias->i_bsame = i->i_bsame; 1436 j = i->i_alias; 1437 if (i == f) 1438 ht_replace(devitab, i->i_name, i->i_alias); 1439 } 1440 1441 /* 1442 * - remove/replace the instance from the devbase's list 1443 * 1444 * A double-linked list would make this much easier. Oh, well, 1445 * what is done is done. 1446 */ 1447 for (ppi = &d->d_ihead; 1448 *ppi != NULL && *ppi != i && (*ppi)->i_bsame != i; 1449 ppi = &(*ppi)->i_bsame) 1450 continue; 1451 if (*ppi == NULL) 1452 panic("%s: dev (%s) doesn't list the devi (%s at %s)", 1453 __func__, d->d_name, i->i_name, i->i_at); 1454 f = *ppi; 1455 if (f == i) 1456 /* That implies d->d_ihead == i */ 1457 *ppi = j; 1458 else 1459 (*ppi)->i_bsame = j; 1460 if (d->d_ipp == &i->i_bsame) { 1461 if (i->i_alias == NULL) { 1462 if (f == i) 1463 d->d_ipp = &d->d_ihead; 1464 else 1465 d->d_ipp = &f->i_bsame; 1466 } else 1467 d->d_ipp = &i->i_alias->i_bsame; 1468 } 1469 } 1470 /* 1471 * - delete the attachment instance 1472 */ 1473 iba = i->i_atdeva; 1474 for (ppi = &iba->d_ihead; 1475 *ppi != NULL && *ppi != i && (*ppi)->i_asame != i; 1476 ppi = &(*ppi)->i_asame) 1477 continue; 1478 if (*ppi == NULL) 1479 panic("%s: deva (%s) doesn't list the devi (%s)", __func__, 1480 iba->d_name, i->i_name); 1481 f = *ppi; 1482 if (f == i) 1483 /* That implies iba->d_ihead == i */ 1484 *ppi = i->i_asame; 1485 else 1486 (*ppi)->i_asame = i->i_asame; 1487 if (iba->d_ipp == &i->i_asame) { 1488 if (f == i) 1489 iba->d_ipp = &iba->d_ihead; 1490 else 1491 iba->d_ipp = &f->i_asame; 1492 } 1493 /* 1494 * - delete the pspec 1495 */ 1496 remove_pspec(i); 1497 1498 /* 1499 * - delete the alldevi entry 1500 */ 1501 TAILQ_REMOVE(&alldevi, i, i_next); 1502 ndevi--; 1503 /* 1504 * Put it in deaddevitab 1505 * 1506 * Each time a devi is removed, devilevel is increased so that later on 1507 * it is possible to tell if an instance was added before or after the 1508 * removal of its parent. 1509 * 1510 * For active instances, i_level contains the number of devi removed so 1511 * far, and for dead devis, it contains its index. 1512 */ 1513 i->i_level = devilevel++; 1514 i->i_alias = NULL; 1515 f = ht_lookup(deaddevitab, i->i_name); 1516 if (f == NULL) { 1517 if (ht_insert(deaddevitab, i->i_name, i)) 1518 panic("%s: can't add %s to deaddevitab", __func__, 1519 i->i_name); 1520 } else { 1521 for (j = f; j->i_alias != NULL; j = j->i_alias) 1522 continue; 1523 j->i_alias = i; 1524 } 1525 /* 1526 * - reconstruct d->d_umax 1527 */ 1528 d->d_umax = 0; 1529 for (i = d->d_ihead; i != NULL; i = i->i_bsame) 1530 if (i->i_unit >= d->d_umax) 1531 d->d_umax = i->i_unit + 1; 1532 } 1533 1534 void 1535 deldeva(const char *at, int nowarn) 1536 { 1537 int unit; 1538 const char *cp; 1539 struct devbase *d, *ad; 1540 struct devi *i, *j; 1541 struct attr *a; 1542 struct pspec *p; 1543 struct nvlist *nv, *stack = NULL; 1544 1545 if (at == NULL) { 1546 TAILQ_FOREACH(i, &alldevi, i_next) 1547 if (i->i_at == NULL) 1548 stack = newnv(NULL, NULL, i, 0, stack); 1549 } else { 1550 size_t l; 1551 1552 CFGDBG(5, "deselecting deva `%s'", at); 1553 if (at[0] == '\0') 1554 goto out; 1555 1556 l = strlen(at) - 1; 1557 if (at[l] == '?' || isdigit((unsigned char)at[l])) { 1558 char base[NAMESIZE]; 1559 1560 if (split(at, l+1, base, sizeof base, &unit)) { 1561 out: 1562 cfgerror("invalid attachment name `%s'", at); 1563 return; 1564 } 1565 cp = intern(base); 1566 } else { 1567 cp = intern(at); 1568 unit = STAR; 1569 } 1570 1571 ad = ht_lookup(devbasetab, cp); 1572 a = ht_lookup(attrtab, cp); 1573 if (a == NULL) { 1574 cfgerror("unknown attachment attribute or device `%s'", 1575 cp); 1576 return; 1577 } 1578 if (!a->a_iattr) { 1579 cfgerror("plain attribute `%s' cannot have children", 1580 a->a_name); 1581 return; 1582 } 1583 1584 /* 1585 * remove_devi() makes changes to the devbase's list and the 1586 * alias list, * so the actual deletion of the instances must 1587 * be delayed. 1588 */ 1589 for (nv = a->a_devs; nv != NULL; nv = nv->nv_next) { 1590 d = nv->nv_ptr; 1591 for (i = d->d_ihead; i != NULL; i = i->i_bsame) 1592 for (j = i; j != NULL; j = j->i_alias) { 1593 /* Ignore devices at root */ 1594 if (j->i_at == NULL) 1595 continue; 1596 p = j->i_pspec; 1597 /* 1598 * There are three cases: 1599 * 1600 * 1. unit is not STAR. Consider 'at' 1601 * to be explicit, even if it 1602 * references an interface 1603 * attribute. 1604 * 1605 * 2. unit is STAR and 'at' references 1606 * a real device. Look for pspec 1607 * that have a matching p_atdev 1608 * field. 1609 * 1610 * 3. unit is STAR and 'at' references 1611 * an interface attribute. Look 1612 * for pspec that have a matching 1613 * p_iattr field. 1614 */ 1615 if ((unit != STAR && /* Case */ 1616 !strcmp(j->i_at, at)) || /* 1 */ 1617 (unit == STAR && 1618 ((ad != NULL && /* Case */ 1619 p->p_atdev == ad) || /* 2 */ 1620 (ad == NULL && /* Case */ 1621 p->p_iattr == a)))) /* 3 */ 1622 stack = newnv(NULL, NULL, j, 0, 1623 stack); 1624 } 1625 } 1626 } 1627 1628 devcleanup(stack); 1629 } 1630 1631 void 1632 deldev(const char *name, int nowarn) 1633 { 1634 size_t l; 1635 struct devi *firsti, *i; 1636 struct nvlist *stack = NULL; 1637 1638 CFGDBG(5, "deselecting dev `%s'", name); 1639 if (name[0] == '\0') 1640 goto out; 1641 1642 l = strlen(name) - 1; 1643 if (name[l] == '*' || isdigit((unsigned char)name[l])) { 1644 /* `no mydev0' or `no mydev*' */ 1645 firsti = ht_lookup(devitab, name); 1646 if (firsti == NULL) { 1647 out: 1648 if (!nowarn) 1649 cfgerror("unknown instance %s", name); 1650 return; 1651 } 1652 for (i = firsti; i != NULL; i = i->i_alias) 1653 stack = newnv(NULL, NULL, i, 0, stack); 1654 } else { 1655 struct devbase *d = ht_lookup(devbasetab, name); 1656 1657 if (d == NULL) { 1658 cfgerror("unknown device %s", name); 1659 return; 1660 } 1661 if (d->d_ispseudo) { 1662 cfgerror("%s is a pseudo-device; " 1663 "use \"no pseudo-device %s\" instead", name, 1664 name); 1665 return; 1666 } 1667 stack = makedevstack(d); 1668 } 1669 1670 devcleanup(stack); 1671 } 1672 1673 /* 1674 * Insert given device "name" into devroottab. In case "name" 1675 * designates a pure interface attribute, create a fake device 1676 * instance for the attribute and insert that into the roottab 1677 * (this scheme avoids mucking around with the orphanage analysis). 1678 */ 1679 void 1680 addpseudoroot(const char *name) 1681 { 1682 char buf[NAMESIZE]; 1683 int unit; 1684 struct attr *attr; 1685 struct devi *i; 1686 struct deva *iba; 1687 struct devbase *ib; 1688 1689 if (split(name, strlen(name), buf, sizeof(buf), &unit)) { 1690 cfgerror("invalid pseudo-root name `%s'", name); 1691 return; 1692 } 1693 1694 /* 1695 * Prefer device because devices with locators define an 1696 * implicit interface attribute. However, if a device is 1697 * not available, try to attach to the interface attribute. 1698 * This makes sure adddev() doesn't get confused when we 1699 * are really attaching to a device (alternatively we maybe 1700 * could specify a non-NULL atlist to defdevattach() below). 1701 */ 1702 ib = ht_lookup(devbasetab, intern(buf)); 1703 if (ib == NULL) { 1704 struct devbase *fakedev; 1705 char fakename[NAMESIZE]; 1706 1707 attr = ht_lookup(attrtab, intern(buf)); 1708 if (!(attr && attr->a_iattr)) { 1709 cfgerror("pseudo-root `%s' not available", name); 1710 return; 1711 } 1712 1713 /* 1714 * here we cheat a bit: create a fake devbase with the 1715 * interface attribute and instantiate it. quick, cheap, 1716 * dirty & bad for you, much like the stuff in the fridge. 1717 * and, it works, since the pseudoroot device is not included 1718 * in ioconf, just used by config to make sure we start from 1719 * the right place. 1720 */ 1721 snprintf(fakename, sizeof(fakename), "%s_devattrs", buf); 1722 fakedev = getdevbase(intern(fakename)); 1723 fakedev->d_isdef = 1; 1724 fakedev->d_ispseudo = 0; 1725 fakedev->d_attrs = attrlist_cons(NULL, attr); 1726 defdevattach(NULL, fakedev, NULL, NULL); 1727 1728 if (unit == STAR) 1729 snprintf(buf, sizeof(buf), "%s*", fakename); 1730 else 1731 snprintf(buf, sizeof(buf), "%s%d", fakename, unit); 1732 name = buf; 1733 } 1734 1735 /* ok, everything should be set up, so instantiate a fake device */ 1736 i = getdevi(name); 1737 if (i == NULL) 1738 panic("%s: device `%s' expected to be present", __func__, 1739 name); 1740 ib = i->i_base; 1741 iba = ib->d_ahead; 1742 1743 i->i_atdeva = iba; 1744 i->i_cfflags = 0; 1745 i->i_locs = fixloc(name, &errattr, NULL); 1746 i->i_pseudoroot = 1; 1747 i->i_active = DEVI_ORPHAN; /* set active by kill_orphans() */ 1748 1749 *iba->d_ipp = i; 1750 iba->d_ipp = &i->i_asame; 1751 1752 ht_insert(devroottab, ib->d_name, ib); 1753 } 1754 1755 static void 1756 deldevbase(struct devbase *d) 1757 { 1758 struct devi *i; 1759 const char *name = d->d_name; 1760 1761 if (!d->d_ispseudo) { 1762 devcleanup(makedevstack(d)); 1763 return; 1764 } 1765 1766 if ((i = ht_lookup(devitab, name)) == NULL) 1767 return; 1768 1769 d->d_umax = 0; /* clear neads-count entries */ 1770 d->d_ihead = NULL; /* make sure it won't be considered active */ 1771 TAILQ_REMOVE(&allpseudo, i, i_next); 1772 if (ht_remove(devitab, name)) 1773 panic("%s: Can't remove %s from devitab", __func__, name); 1774 if (ht_insert(deaddevitab, name, i)) 1775 panic("%s: Can't add %s to deaddevitab", __func__, name); 1776 } 1777 1778 void 1779 addpseudo(const char *name, int number) 1780 { 1781 struct devbase *d; 1782 struct devi *i; 1783 1784 d = ht_lookup(devbasetab, name); 1785 if (d == NULL) { 1786 cfgerror("undefined pseudo-device %s", name); 1787 return; 1788 } 1789 if (!d->d_ispseudo) { 1790 cfgerror("%s is a real device, not a pseudo-device", name); 1791 return; 1792 } 1793 if (ht_lookup(devitab, name) != NULL) { 1794 cfgerror("`%s' already defined", name); 1795 return; 1796 } 1797 i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */ 1798 if (ht_insert(devitab, name, i)) 1799 panic("%s: %s", __func__, name); 1800 /* Useful to retrieve the instance from the devbase */ 1801 d->d_ihead = i; 1802 i->i_active = DEVI_ACTIVE; 1803 TAILQ_INSERT_TAIL(&allpseudo, i, i_next); 1804 } 1805 1806 void 1807 delpseudo(const char *name, int nowarn) 1808 { 1809 struct devbase *d; 1810 1811 CFGDBG(5, "deselecting pseudo `%s'", name); 1812 d = ht_lookup(devbasetab, name); 1813 if (d == NULL) { 1814 if (!nowarn) 1815 cfgerror("undefined pseudo-device %s", name); 1816 return; 1817 } 1818 if (!d->d_ispseudo) { 1819 cfgerror("%s is a real device, not a pseudo-device", name); 1820 return; 1821 } 1822 deldevbase(d); 1823 } 1824 1825 void 1826 adddevm(const char *name, devmajor_t cmajor, devmajor_t bmajor, 1827 struct condexpr *cond, struct nvlist *nv_nodes) 1828 { 1829 struct devm *dm; 1830 1831 if (cmajor != NODEVMAJOR && (cmajor < 0 || cmajor >= 4096)) { 1832 cfgerror("character major %d is invalid", cmajor); 1833 condexpr_destroy(cond); 1834 nvfreel(nv_nodes); 1835 return; 1836 } 1837 1838 if (bmajor != NODEVMAJOR && (bmajor < 0 || bmajor >= 4096)) { 1839 cfgerror("block major %d is invalid", bmajor); 1840 condexpr_destroy(cond); 1841 nvfreel(nv_nodes); 1842 return; 1843 } 1844 if (cmajor == NODEVMAJOR && bmajor == NODEVMAJOR) { 1845 cfgerror("both character/block majors are not specified"); 1846 condexpr_destroy(cond); 1847 nvfreel(nv_nodes); 1848 return; 1849 } 1850 1851 dm = ecalloc(1, sizeof(*dm)); 1852 dm->dm_srcfile = yyfile; 1853 dm->dm_srcline = currentline(); 1854 dm->dm_name = name; 1855 dm->dm_cmajor = cmajor; 1856 dm->dm_bmajor = bmajor; 1857 dm->dm_opts = cond; 1858 dm->dm_devnodes = nv_nodes; 1859 1860 TAILQ_INSERT_TAIL(&alldevms, dm, dm_next); 1861 1862 maxcdevm = MAX(maxcdevm, dm->dm_cmajor); 1863 maxbdevm = MAX(maxbdevm, dm->dm_bmajor); 1864 } 1865 1866 int 1867 fixdevis(void) 1868 { 1869 const char *msg; 1870 struct devi *i; 1871 struct pspec *p; 1872 int error = 0; 1873 1874 TAILQ_FOREACH(i, &alldevi, i_next) { 1875 CFGDBG(3, "fixing devis `%s'", i->i_name); 1876 if (i->i_active == DEVI_ACTIVE) 1877 selectbase(i->i_base, i->i_atdeva); 1878 else if (i->i_active == DEVI_ORPHAN) { 1879 /* 1880 * At this point, we can't have instances for which 1881 * i_at or i_pspec are NULL. 1882 */ 1883 ++error; 1884 p = i->i_pspec; 1885 msg = p == NULL ? "no parent" : 1886 (p->p_atunit == WILD ? "nothing matching" : "no"); 1887 cfgxerror(i->i_srcfile, i->i_lineno, 1888 "`%s at %s' is orphaned (%s `%s' found)", 1889 i->i_name, i->i_at, msg, i->i_at); 1890 } else if (vflag && i->i_active == DEVI_IGNORED) 1891 cfgxwarn(i->i_srcfile, i->i_lineno, "ignoring " 1892 "explicitly orphaned instance `%s at %s'", 1893 i->i_name, i->i_at); 1894 } 1895 1896 if (error) 1897 return error; 1898 1899 TAILQ_FOREACH(i, &allpseudo, i_next) 1900 if (i->i_active == DEVI_ACTIVE) 1901 selectbase(i->i_base, NULL); 1902 return 0; 1903 } 1904 1905 /* 1906 * Look up a parent spec, creating a new one if it does not exist. 1907 */ 1908 static struct pspec * 1909 getpspec(struct attr *attr, struct devbase *ab, int atunit, int first) 1910 { 1911 struct pspec *p; 1912 int inst = npspecs; 1913 int ref = 1; 1914 1915 TAILQ_FOREACH(p, &allpspecs, p_list) { 1916 if (p->p_iattr == attr && p->p_atdev == ab && 1917 p->p_atunit == atunit) { 1918 p->p_ref++; 1919 if (first) 1920 return p; 1921 else { 1922 inst = p->p_inst; 1923 ref = p->p_ref; 1924 } 1925 1926 } 1927 } 1928 1929 p = ecalloc(1, sizeof(*p)); 1930 1931 p->p_iattr = attr; 1932 p->p_atdev = ab; 1933 p->p_atunit = atunit; 1934 p->p_inst = inst; 1935 if (inst == npspecs) 1936 npspecs++; 1937 p->p_active = 0; 1938 p->p_ref = ref; 1939 1940 TAILQ_INSERT_TAIL(&allpspecs, p, p_list); 1941 1942 return (p); 1943 } 1944 1945 /* 1946 * Define a new instance of a specific device. 1947 */ 1948 static struct devi * 1949 getdevi(const char *name) 1950 { 1951 struct devi *i, *firsti; 1952 struct devbase *d; 1953 int unit; 1954 char base[NAMESIZE]; 1955 1956 if (split(name, strlen(name), base, sizeof base, &unit)) { 1957 cfgerror("invalid device name `%s'", name); 1958 return (NULL); 1959 } 1960 d = ht_lookup(devbasetab, intern(base)); 1961 if (d == NULL) { 1962 cfgerror("%s: unknown device `%s'", name, base); 1963 return (NULL); 1964 } 1965 if (d->d_ispseudo) { 1966 cfgerror("%s: %s is a pseudo-device", name, base); 1967 return (NULL); 1968 } 1969 firsti = ht_lookup(devitab, name); 1970 i = newdevi(name, unit, d); 1971 if (firsti == NULL) { 1972 if (ht_insert(devitab, name, i)) 1973 panic("%s: %s", __func__, name); 1974 *d->d_ipp = i; 1975 d->d_ipp = &i->i_bsame; 1976 } else { 1977 while (firsti->i_alias) 1978 firsti = firsti->i_alias; 1979 firsti->i_alias = i; 1980 } 1981 TAILQ_INSERT_TAIL(&alldevi, i, i_next); 1982 ndevi++; 1983 return (i); 1984 } 1985 1986 static const char * 1987 concat(const char *name, int c) 1988 { 1989 size_t len; 1990 char buf[NAMESIZE]; 1991 1992 len = strlen(name); 1993 if (len + 2 > sizeof(buf)) { 1994 cfgerror("device name `%s%c' too long", name, c); 1995 len = sizeof(buf) - 2; 1996 } 1997 memmove(buf, name, len); 1998 buf[len] = (char)c; 1999 buf[len + 1] = '\0'; 2000 return (intern(buf)); 2001 } 2002 2003 const char * 2004 starref(const char *name) 2005 { 2006 2007 return (concat(name, '*')); 2008 } 2009 2010 const char * 2011 wildref(const char *name) 2012 { 2013 2014 return (concat(name, '?')); 2015 } 2016 2017 /* 2018 * Split a name like "foo0" into base name (foo) and unit number (0). 2019 * Return 0 on success. To make this useful for names like "foo0a", 2020 * the length of the "foo0" part is one of the arguments. 2021 */ 2022 static int 2023 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit) 2024 { 2025 const char *cp; 2026 int c; 2027 size_t l; 2028 2029 l = nlen; 2030 if (l < 2 || l >= bsize || isdigit((unsigned char)*name)) 2031 return (1); 2032 c = (u_char)name[--l]; 2033 if (!isdigit(c)) { 2034 if (c == '*') 2035 *aunit = STAR; 2036 else if (c == '?') 2037 *aunit = WILD; 2038 else 2039 return (1); 2040 } else { 2041 cp = &name[l]; 2042 while (isdigit((unsigned char)cp[-1])) 2043 l--, cp--; 2044 *aunit = atoi(cp); 2045 } 2046 memmove(base, name, l); 2047 base[l] = 0; 2048 return (0); 2049 } 2050 2051 void 2052 addattr(const char *name) 2053 { 2054 struct attr *a; 2055 2056 a = refattr(name); 2057 selectattr(a); 2058 } 2059 2060 void 2061 delattr(const char *name, int nowarn) 2062 { 2063 struct attr *a; 2064 2065 a = refattr(name); 2066 deselectattr(a); 2067 } 2068 2069 void 2070 selectattr(struct attr *a) 2071 { 2072 struct attrlist *al; 2073 struct attr *dep; 2074 2075 CFGDBG(5, "selecting attr `%s'", a->a_name); 2076 for (al = a->a_deps; al != NULL; al = al->al_next) { 2077 dep = al->al_this; 2078 selectattr(dep); 2079 } 2080 if (ht_insert(selecttab, a->a_name, __UNCONST(a->a_name)) == 0) 2081 nattrs++; 2082 CFGDBG(3, "attr selected `%s'", a->a_name); 2083 } 2084 2085 static int 2086 deselectattrcb2(const char *name1, const char *name2, void *v, void *arg) 2087 { 2088 struct attr *a = arg; 2089 const char *name = a->a_name; 2090 struct vtype *vt = v; 2091 2092 if (strcmp(name, name2) == 0) { 2093 delattr(name1, 0); 2094 return 0; 2095 } 2096 2097 if (!vt->attr->a_deselected) 2098 return 0; 2099 2100 switch (vt->type) { 2101 case V_ATTRIBUTE: 2102 #ifdef notyet 2103 // XXX: Loops 2104 deselectattr(vt->value); 2105 #endif 2106 break; 2107 case V_DEVICE: 2108 CFGDBG(5, "removing device `%s' with attr `%s' because attr `%s'" 2109 " is deselected", name1, name2, name); 2110 deldevbase(vt->value); 2111 break; 2112 default: 2113 abort(); 2114 } 2115 return 0; 2116 } 2117 2118 void 2119 deselectattr(struct attr *a) 2120 { 2121 CFGDBG(5, "deselecting attr `%s'", a->a_name); 2122 a->a_deselected = 1; 2123 ht_enumerate2(attrdeptab, deselectattrcb2, a); 2124 if (ht_remove(selecttab, a->a_name) == 0) 2125 nattrs--; 2126 CFGDBG(3, "attr deselected `%s'", a->a_name); 2127 } 2128 2129 static int 2130 dumpattrdepcb2(const char *name1, const char *name2, void *v, void *arg) 2131 { 2132 2133 CFGDBG(3, "attr `%s' depends on attr `%s'", name1, name2); 2134 return 0; 2135 } 2136 2137 void 2138 dependattrs(void) 2139 { 2140 2141 ht_enumerate2(attrdeptab, dumpattrdepcb2, NULL); 2142 } 2143 2144 /* 2145 * We have an instance of the base foo, so select it and all its 2146 * attributes for "optional foo". 2147 */ 2148 static void 2149 selectbase(struct devbase *d, struct deva *da) 2150 { 2151 struct attr *a; 2152 struct attrlist *al; 2153 2154 (void)ht_insert(selecttab, d->d_name, __UNCONST(d->d_name)); 2155 CFGDBG(3, "devbase selected `%s'", d->d_name); 2156 CFGDBG(5, "selecting dependencies of devbase `%s'", d->d_name); 2157 for (al = d->d_attrs; al != NULL; al = al->al_next) { 2158 a = al->al_this; 2159 expandattr(a, selectattr); 2160 } 2161 2162 struct attr *devattr; 2163 devattr = refattr(d->d_name); 2164 expandattr(devattr, selectattr); 2165 2166 if (da != NULL) { 2167 (void)ht_insert(selecttab, da->d_name, __UNCONST(da->d_name)); 2168 CFGDBG(3, "devattr selected `%s'", da->d_name); 2169 for (al = da->d_attrs; al != NULL; al = al->al_next) { 2170 a = al->al_this; 2171 expandattr(a, selectattr); 2172 } 2173 } 2174 2175 fixdev(d); 2176 } 2177 2178 /* 2179 * Is the given pointer on the given list of pointers? 2180 */ 2181 int 2182 onlist(struct nvlist *nv, void *ptr) 2183 { 2184 for (; nv != NULL; nv = nv->nv_next) 2185 if (nv->nv_ptr == ptr) 2186 return (1); 2187 return (0); 2188 } 2189 2190 static char * 2191 extend(char *p, const char *name) 2192 { 2193 size_t l; 2194 2195 l = strlen(name); 2196 memmove(p, name, l); 2197 p += l; 2198 *p++ = ','; 2199 *p++ = ' '; 2200 return (p); 2201 } 2202 2203 /* 2204 * Check that we got all required locators, and default any that are 2205 * given as "?" and have defaults. Return 0 on success. 2206 */ 2207 static const char ** 2208 fixloc(const char *name, struct attr *attr, struct loclist *got) 2209 { 2210 struct loclist *m, *n; 2211 int ord; 2212 const char **lp; 2213 int nmissing, nextra, nnodefault; 2214 char *mp, *ep, *ndp; 2215 char missing[1000], extra[1000], nodefault[1000]; 2216 static const char *nullvec[1]; 2217 2218 /* 2219 * Look for all required locators, and number the given ones 2220 * according to the required order. While we are numbering, 2221 * set default values for defaulted locators. 2222 */ 2223 if (attr->a_loclen == 0) /* e.g., "at root" */ 2224 lp = nullvec; 2225 else 2226 lp = emalloc((size_t)(attr->a_loclen + 1) * sizeof(const char *)); 2227 for (n = got; n != NULL; n = n->ll_next) 2228 n->ll_num = -1; 2229 nmissing = 0; 2230 mp = missing; 2231 /* yes, this is O(mn), but m and n should be small */ 2232 for (ord = 0, m = attr->a_locs; m != NULL; m = m->ll_next, ord++) { 2233 for (n = got; n != NULL; n = n->ll_next) { 2234 if (n->ll_name == m->ll_name) { 2235 n->ll_num = ord; 2236 break; 2237 } 2238 } 2239 if (n == NULL && m->ll_num == 0) { 2240 nmissing++; 2241 mp = extend(mp, m->ll_name); 2242 } 2243 lp[ord] = m->ll_string; 2244 } 2245 if (ord != attr->a_loclen) 2246 panic("%s: bad length", __func__); 2247 lp[ord] = NULL; 2248 nextra = 0; 2249 ep = extra; 2250 nnodefault = 0; 2251 ndp = nodefault; 2252 for (n = got; n != NULL; n = n->ll_next) { 2253 if (n->ll_num >= 0) { 2254 if (n->ll_string != NULL) 2255 lp[n->ll_num] = n->ll_string; 2256 else if (lp[n->ll_num] == NULL) { 2257 nnodefault++; 2258 ndp = extend(ndp, n->ll_name); 2259 } 2260 } else { 2261 nextra++; 2262 ep = extend(ep, n->ll_name); 2263 } 2264 } 2265 if (nextra) { 2266 ep[-2] = 0; /* kill ", " */ 2267 cfgerror("%s: extraneous locator%s: %s", 2268 name, nextra > 1 ? "s" : "", extra); 2269 } 2270 if (nmissing) { 2271 mp[-2] = 0; 2272 cfgerror("%s: must specify %s", name, missing); 2273 } 2274 if (nnodefault) { 2275 ndp[-2] = 0; 2276 cfgerror("%s: cannot wildcard %s", name, nodefault); 2277 } 2278 if (nmissing || nnodefault) { 2279 free(lp); 2280 lp = NULL; 2281 } 2282 return (lp); 2283 } 2284 2285 void 2286 setversion(int newver) 2287 { 2288 if (newver > CONFIG_VERSION) 2289 cfgerror("your sources require a newer version of config(1) " 2290 "-- please rebuild it."); 2291 else if (newver < CONFIG_MINVERSION) 2292 cfgerror("your sources are out of date -- please update."); 2293 else 2294 version = newver; 2295 } 2296