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