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