1 /* $NetBSD: sem.c,v 1.1 2005/06/05 18:19:53 thorpej 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 "defs.h" 53 #include "sem.h" 54 55 /* 56 * config semantics. 57 */ 58 59 #define NAMESIZE 100 /* local name buffers */ 60 61 const char *s_ifnet; /* magic attribute */ 62 const char *s_qmark; 63 const char *s_none; 64 65 static struct hashtab *cfhashtab; /* for config lookup */ 66 struct hashtab *devitab; /* etc */ 67 68 static struct attr errattr; 69 static struct devbase errdev; 70 static struct deva errdeva; 71 72 static int has_errobj(struct nvlist *, void *); 73 static struct nvlist *addtoattr(struct nvlist *, struct devbase *); 74 static int resolve(struct nvlist **, const char *, const char *, 75 struct nvlist *, int); 76 static struct pspec *getpspec(struct attr *, struct devbase *, int); 77 static struct devi *newdevi(const char *, int, struct devbase *d); 78 static struct devi *getdevi(const char *); 79 static const char *concat(const char *, int); 80 static char *extend(char *, const char *); 81 static int split(const char *, size_t, char *, size_t, int *); 82 static void selectbase(struct devbase *, struct deva *); 83 static int onlist(struct nvlist *, void *); 84 static const char **fixloc(const char *, struct attr *, struct nvlist *); 85 static const char *makedevstr(int, int); 86 static const char *major2name(int); 87 static int dev2major(struct devbase *); 88 89 extern const char *yyfile; 90 91 void 92 initsem(void) 93 { 94 95 attrtab = ht_new(); 96 errattr.a_name = "<internal>"; 97 98 TAILQ_INIT(&allbases); 99 100 TAILQ_INIT(&alldevas); 101 102 TAILQ_INIT(&allpspecs); 103 104 cfhashtab = ht_new(); 105 TAILQ_INIT(&allcf); 106 107 TAILQ_INIT(&alldevi); 108 errdev.d_name = "<internal>"; 109 110 TAILQ_INIT(&allpseudo); 111 112 TAILQ_INIT(&alldevms); 113 114 s_ifnet = intern("ifnet"); 115 s_qmark = intern("?"); 116 s_none = intern("none"); 117 } 118 119 /* Name of include file just ended (set in scan.l) */ 120 extern const char *lastfile; 121 122 void 123 enddefs(void) 124 { 125 struct devbase *dev; 126 127 TAILQ_FOREACH(dev, &allbases, d_next) { 128 if (!dev->d_isdef) { 129 (void)fprintf(stderr, 130 "%s: device `%s' used but not defined\n", 131 lastfile, dev->d_name); 132 errors++; 133 continue; 134 } 135 } 136 if (errors) { 137 (void)fprintf(stderr, "*** Stop.\n"); 138 exit(1); 139 } 140 } 141 142 void 143 setdefmaxusers(int min, int def, int max) 144 { 145 146 if (min < 1 || min > def || def > max) 147 error("maxusers must have 1 <= min (%d) <= default (%d) <= max (%d)", min, def, max); 148 else { 149 minmaxusers = min; 150 defmaxusers = def; 151 maxmaxusers = max; 152 } 153 } 154 155 void 156 setmaxusers(int n) 157 { 158 159 if (maxusers != 0) { 160 error("duplicate maxusers parameter"); 161 return; 162 } 163 maxusers = n; 164 if (n < minmaxusers) { 165 error("warning: minimum of %d maxusers assumed", minmaxusers); 166 errors--; /* take it away */ 167 maxusers = minmaxusers; 168 } else if (n > maxmaxusers) { 169 error("warning: maxusers (%d) > %d", n, maxmaxusers); 170 errors--; 171 } 172 } 173 174 void 175 setident(const char *i) 176 { 177 178 ident = intern(i); 179 } 180 181 /* 182 * Define an attribute, optionally with an interface (a locator list) 183 * and a set of attribute-dependencies. 184 * 185 * Attribute dependencies MAY NOT be interface attributes. 186 * 187 * Since an empty locator list is logically different from "no interface", 188 * all locator lists include a dummy head node, which we discard here. 189 */ 190 int 191 defattr(const char *name, struct nvlist *locs, struct nvlist *deps, 192 int devclass) 193 { 194 struct attr *a, *dep; 195 struct nvlist *nv; 196 int len; 197 198 if (locs != NULL && devclass) 199 panic("defattr(%s): locators and devclass", name); 200 201 if (deps != NULL && devclass) 202 panic("defattr(%s): dependencies and devclass", name); 203 204 /* 205 * If this attribute depends on any others, make sure none of 206 * the dependencies are interface attributes. 207 */ 208 for (nv = deps; nv != NULL; nv = nv->nv_next) { 209 dep = nv->nv_ptr; 210 if (dep->a_iattr) { 211 error("`%s' dependency `%s' is an interface attribute", 212 name, dep->a_name); 213 return (1); 214 } 215 } 216 217 a = ecalloc(1, sizeof *a); 218 if (ht_insert(attrtab, name, a)) { 219 free(a); 220 error("attribute `%s' already defined", name); 221 nvfreel(locs); 222 return (1); 223 } 224 225 a->a_name = name; 226 if (locs != NULL) { 227 a->a_iattr = 1; 228 a->a_locs = locs->nv_next; 229 nvfree(locs); 230 } else { 231 a->a_iattr = 0; 232 a->a_locs = NULL; 233 } 234 if (devclass) { 235 size_t l = strlen(name) + 4; 236 char *classenum = alloca(l), *cp; 237 int errored = 0; 238 239 strlcpy(classenum, "DV_", l); 240 strlcat(classenum, name, l); 241 for (cp = classenum + 3; *cp; cp++) { 242 if (!errored && 243 (!isalnum((unsigned char)*cp) || 244 (isalpha((unsigned char)*cp) && !islower((unsigned char)*cp)))) { 245 error("device class names must be lower-case alphanumeric characters"); 246 errored = 1; 247 } 248 *cp = toupper((unsigned char)*cp); 249 } 250 a->a_devclass = intern(classenum); 251 } else 252 a->a_devclass = NULL; 253 len = 0; 254 for (nv = a->a_locs; nv != NULL; nv = nv->nv_next) 255 len++; 256 a->a_loclen = len; 257 a->a_devs = NULL; 258 a->a_refs = NULL; 259 a->a_deps = deps; 260 a->a_expanding = 0; 261 262 /* Expand the attribute to check for cycles in the graph. */ 263 expandattr(a, NULL); 264 265 return (0); 266 } 267 268 /* 269 * Return true if the given `error object' is embedded in the given 270 * pointer list. 271 */ 272 static int 273 has_errobj(struct nvlist *nv, void *obj) 274 { 275 276 for (; nv != NULL; nv = nv->nv_next) 277 if (nv->nv_ptr == obj) 278 return (1); 279 return (0); 280 } 281 282 /* 283 * Return true if the given attribute is embedded in the given 284 * pointer list. 285 */ 286 int 287 has_attr(struct nvlist *nv, const char *attr) 288 { 289 struct attr *a; 290 291 if ((a = getattr(attr)) == NULL) 292 return (0); 293 294 for (; nv != NULL; nv = nv->nv_next) 295 if (nv->nv_ptr == a) 296 return (1); 297 return (0); 298 } 299 300 /* 301 * Add a device base to a list in an attribute (actually, to any list). 302 * Note that this does not check for duplicates, and does reverse the 303 * list order, but no one cares anyway. 304 */ 305 static struct nvlist * 306 addtoattr(struct nvlist *l, struct devbase *dev) 307 { 308 struct nvlist *n; 309 310 n = newnv(NULL, NULL, dev, 0, l); 311 return (n); 312 } 313 314 /* 315 * Define a device. This may (or may not) also define an interface 316 * attribute and/or refer to existing attributes. 317 */ 318 void 319 defdev(struct devbase *dev, struct nvlist *loclist, struct nvlist *attrs, 320 int ispseudo) 321 { 322 struct nvlist *nv; 323 struct attr *a; 324 325 if (dev == &errdev) 326 goto bad; 327 if (dev->d_isdef) { 328 error("redefinition of `%s'", dev->d_name); 329 goto bad; 330 } 331 332 dev->d_isdef = 1; 333 if (has_errobj(attrs, &errattr)) 334 goto bad; 335 336 /* 337 * Handle implicit attribute definition from locator list. Do 338 * this before scanning the `at' list so that we can have, e.g.: 339 * device foo at other, foo { slot = -1 } 340 * (where you can plug in a foo-bus extender to a foo-bus). 341 */ 342 if (loclist != NULL) { 343 nv = loclist; 344 loclist = NULL; /* defattr disposes of them for us */ 345 if (defattr(dev->d_name, nv, NULL, 0)) 346 goto bad; 347 attrs = newnv(dev->d_name, NULL, getattr(dev->d_name), 0, 348 attrs); 349 } 350 351 /* Committed! Set up fields. */ 352 dev->d_ispseudo = ispseudo; 353 dev->d_attrs = attrs; 354 dev->d_classattr = NULL; /* for now */ 355 356 /* 357 * For each interface attribute this device refers to, add this 358 * device to its reference list. This makes, e.g., finding all 359 * "scsi"s easier. 360 * 361 * While looking through the attributes, set up the device 362 * class if any are devclass attributes (and error out if the 363 * device has two classes). 364 */ 365 for (nv = attrs; nv != NULL; nv = nv->nv_next) { 366 a = nv->nv_ptr; 367 if (a->a_iattr) 368 a->a_refs = addtoattr(a->a_refs, dev); 369 if (a->a_devclass != NULL) { 370 if (dev->d_classattr != NULL) { 371 error("device `%s' has multiple classes (`%s' and `%s')", 372 dev->d_name, dev->d_classattr->a_name, 373 a->a_name); 374 } 375 dev->d_classattr = a; 376 } 377 } 378 return; 379 bad: 380 nvfreel(loclist); 381 nvfreel(attrs); 382 } 383 384 /* 385 * Look up a devbase. Also makes sure it is a reasonable name, 386 * i.e., does not end in a digit or contain special characters. 387 */ 388 struct devbase * 389 getdevbase(const char *name) 390 { 391 u_char *p; 392 struct devbase *dev; 393 394 p = (u_char *)name; 395 if (!isalpha(*p)) 396 goto badname; 397 while (*++p) { 398 if (!isalnum(*p) && *p != '_') 399 goto badname; 400 } 401 if (isdigit(*--p)) { 402 badname: 403 error("bad device base name `%s'", name); 404 return (&errdev); 405 } 406 dev = ht_lookup(devbasetab, name); 407 if (dev == NULL) { 408 dev = ecalloc(1, sizeof *dev); 409 dev->d_name = name; 410 dev->d_isdef = 0; 411 dev->d_major = NODEV; 412 dev->d_attrs = NULL; 413 dev->d_ihead = NULL; 414 dev->d_ipp = &dev->d_ihead; 415 dev->d_ahead = NULL; 416 dev->d_app = &dev->d_ahead; 417 dev->d_umax = 0; 418 TAILQ_INSERT_TAIL(&allbases, dev, d_next); 419 if (ht_insert(devbasetab, name, dev)) 420 panic("getdevbase(%s)", name); 421 } 422 return (dev); 423 } 424 425 /* 426 * Define some of a device's allowable parent attachments. 427 * There may be a list of (plain) attributes. 428 */ 429 void 430 defdevattach(struct deva *deva, struct devbase *dev, struct nvlist *atlist, 431 struct nvlist *attrs) 432 { 433 struct nvlist *nv; 434 struct attr *a; 435 struct deva *da; 436 437 if (dev == &errdev) 438 goto bad; 439 if (deva == NULL) 440 deva = getdevattach(dev->d_name); 441 if (deva == &errdeva) 442 goto bad; 443 if (!dev->d_isdef) { 444 error("attaching undefined device `%s'", dev->d_name); 445 goto bad; 446 } 447 if (deva->d_isdef) { 448 error("redefinition of `%s'", deva->d_name); 449 goto bad; 450 } 451 if (dev->d_ispseudo) { 452 error("pseudo-devices can't attach"); 453 goto bad; 454 } 455 456 deva->d_isdef = 1; 457 if (has_errobj(attrs, &errattr)) 458 goto bad; 459 for (nv = attrs; nv != NULL; nv = nv->nv_next) { 460 a = nv->nv_ptr; 461 if (a == &errattr) 462 continue; /* already complained */ 463 if (a->a_iattr || a->a_devclass != NULL) 464 error("`%s' is not a plain attribute", a->a_name); 465 } 466 467 /* Committed! Set up fields. */ 468 deva->d_attrs = attrs; 469 deva->d_atlist = atlist; 470 deva->d_devbase = dev; 471 472 /* 473 * Turn the `at' list into interface attributes (map each 474 * nv_name to an attribute, or to NULL for root), and add 475 * this device to those attributes, so that children can 476 * be listed at this particular device if they are supported 477 * by that attribute. 478 */ 479 for (nv = atlist; nv != NULL; nv = nv->nv_next) { 480 if (nv->nv_name == NULL) 481 nv->nv_ptr = a = NULL; /* at root */ 482 else 483 nv->nv_ptr = a = getattr(nv->nv_name); 484 if (a == &errattr) 485 continue; /* already complained */ 486 487 /* 488 * Make sure that an attachment spec doesn't 489 * already say how to attach to this attribute. 490 */ 491 for (da = dev->d_ahead; da != NULL; da = da->d_bsame) 492 if (onlist(da->d_atlist, a)) 493 error("attach at `%s' already done by `%s'", 494 a ? a->a_name : "root", da->d_name); 495 496 if (a == NULL) 497 continue; /* at root; don't add */ 498 if (!a->a_iattr) 499 error("%s cannot be at plain attribute `%s'", 500 dev->d_name, a->a_name); 501 else 502 a->a_devs = addtoattr(a->a_devs, dev); 503 } 504 505 /* attach to parent */ 506 *dev->d_app = deva; 507 dev->d_app = &deva->d_bsame; 508 return; 509 bad: 510 nvfreel(atlist); 511 nvfreel(attrs); 512 } 513 514 /* 515 * Look up a device attachment. Also makes sure it is a reasonable 516 * name, i.e., does not contain digits or special characters. 517 */ 518 struct deva * 519 getdevattach(const char *name) 520 { 521 u_char *p; 522 struct deva *deva; 523 524 p = (u_char *)name; 525 if (!isalpha(*p)) 526 goto badname; 527 while (*++p) { 528 if (!isalnum(*p) && *p != '_') 529 goto badname; 530 } 531 if (isdigit(*--p)) { 532 badname: 533 error("bad device attachment name `%s'", name); 534 return (&errdeva); 535 } 536 deva = ht_lookup(devatab, name); 537 if (deva == NULL) { 538 deva = ecalloc(1, sizeof *deva); 539 deva->d_name = name; 540 deva->d_bsame = NULL; 541 deva->d_isdef = 0; 542 deva->d_devbase = NULL; 543 deva->d_atlist = NULL; 544 deva->d_attrs = NULL; 545 deva->d_ihead = NULL; 546 deva->d_ipp = &deva->d_ihead; 547 TAILQ_INSERT_TAIL(&alldevas, deva, d_next); 548 if (ht_insert(devatab, name, deva)) 549 panic("getdeva(%s)", name); 550 } 551 return (deva); 552 } 553 554 /* 555 * Look up an attribute. 556 */ 557 struct attr * 558 getattr(const char *name) 559 { 560 struct attr *a; 561 562 if ((a = ht_lookup(attrtab, name)) == NULL) { 563 error("undefined attribute `%s'", name); 564 a = &errattr; 565 } 566 return (a); 567 } 568 569 /* 570 * Recursively expand an attribute and its dependencies, checking for 571 * cycles, and invoking a callback for each attribute found. 572 */ 573 void 574 expandattr(struct attr *a, void (*callback)(struct attr *)) 575 { 576 struct nvlist *nv; 577 struct attr *dep; 578 579 if (a->a_expanding) { 580 error("circular dependency on attribute `%s'", a->a_name); 581 return; 582 } 583 584 a->a_expanding = 1; 585 586 /* First expand all of this attribute's dependencies. */ 587 for (nv = a->a_deps; nv != NULL; nv = nv->nv_next) { 588 dep = nv->nv_ptr; 589 expandattr(dep, callback); 590 } 591 592 /* ...and now invoke the callback for ourself. */ 593 if (callback != NULL) 594 (*callback)(a); 595 596 a->a_expanding = 0; 597 } 598 599 /* 600 * Set the major device number for a device, so that it can be used 601 * as a root/dumps "on" device in a configuration. 602 */ 603 void 604 setmajor(struct devbase *d, int n) 605 { 606 607 if (d != &errdev && d->d_major != NODEV) 608 error("device `%s' is already major %d", 609 d->d_name, d->d_major); 610 else 611 d->d_major = n; 612 } 613 614 const char * 615 major2name(int maj) 616 { 617 struct devbase *dev; 618 struct devm *dm; 619 620 if (!do_devsw) { 621 TAILQ_FOREACH(dev, &allbases, d_next) { 622 if (dev->d_major == maj) 623 return (dev->d_name); 624 } 625 } else { 626 TAILQ_FOREACH(dm, &alldevms, dm_next) { 627 if (dm->dm_bmajor == maj) 628 return (dm->dm_name); 629 } 630 } 631 return (NULL); 632 } 633 634 int 635 dev2major(struct devbase *dev) 636 { 637 struct devm *dm; 638 639 if (!do_devsw) 640 return (dev->d_major); 641 642 TAILQ_FOREACH(dm, &alldevms, dm_next) { 643 if (strcmp(dm->dm_name, dev->d_name) == 0) 644 return (dm->dm_bmajor); 645 } 646 return (NODEV); 647 } 648 649 /* 650 * Make a string description of the device at maj/min. 651 */ 652 static const char * 653 makedevstr(int maj, int min) 654 { 655 const char *devname; 656 char buf[32]; 657 658 devname = major2name(maj); 659 if (devname == NULL) 660 (void)snprintf(buf, sizeof(buf), "<%d/%d>", maj, min); 661 else 662 (void)snprintf(buf, sizeof(buf), "%s%d%c", devname, 663 min / maxpartitions, (min % maxpartitions) + 'a'); 664 665 return (intern(buf)); 666 } 667 668 /* 669 * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a'). 670 * Handle the case where the device number is given but there is no 671 * corresponding name, and map NULL to the default. 672 */ 673 static int 674 resolve(struct nvlist **nvp, const char *name, const char *what, 675 struct nvlist *dflt, int part) 676 { 677 struct nvlist *nv; 678 struct devbase *dev; 679 const char *cp; 680 int maj, min, i, l; 681 int unit; 682 char buf[NAMESIZE]; 683 684 if ((u_int)(part -= 'a') >= maxpartitions) 685 panic("resolve"); 686 if ((nv = *nvp) == NULL) { 687 dev_t d = NODEV; 688 /* 689 * Apply default. Easiest to do this by number. 690 * Make sure to retain NODEVness, if this is dflt's disposition. 691 */ 692 if (dflt->nv_int != NODEV) { 693 maj = major(dflt->nv_int); 694 min = ((minor(dflt->nv_int) / maxpartitions) * 695 maxpartitions) + part; 696 d = makedev(maj, min); 697 cp = makedevstr(maj, min); 698 } else 699 cp = NULL; 700 *nvp = nv = newnv(NULL, cp, NULL, d, NULL); 701 } 702 if (nv->nv_int != NODEV) { 703 /* 704 * By the numbers. Find the appropriate major number 705 * to make a name. 706 */ 707 maj = major(nv->nv_int); 708 min = minor(nv->nv_int); 709 nv->nv_str = makedevstr(maj, min); 710 return (0); 711 } 712 713 if (nv->nv_str == NULL || nv->nv_str == s_qmark) 714 /* 715 * Wildcarded or unspecified; leave it as NODEV. 716 */ 717 return (0); 718 719 /* 720 * The normal case: things like "ra2b". Check for partition 721 * suffix, remove it if there, and split into name ("ra") and 722 * unit (2). 723 */ 724 l = i = strlen(nv->nv_str); 725 cp = &nv->nv_str[l]; 726 if (l > 1 && *--cp >= 'a' && *cp < 'a' + maxpartitions && 727 isdigit((unsigned char)cp[-1])) { 728 l--; 729 part = *cp - 'a'; 730 } 731 cp = nv->nv_str; 732 if (split(cp, l, buf, sizeof buf, &unit)) { 733 error("%s: invalid %s device name `%s'", name, what, cp); 734 return (1); 735 } 736 dev = ht_lookup(devbasetab, intern(buf)); 737 if (dev == NULL) { 738 error("%s: device `%s' does not exist", name, buf); 739 return (1); 740 } 741 742 /* 743 * Check for the magic network interface attribute, and 744 * don't bother making a device number. 745 */ 746 if (has_attr(dev->d_attrs, s_ifnet)) { 747 nv->nv_int = NODEV; 748 nv->nv_ifunit = unit; /* XXX XXX XXX */ 749 } else { 750 maj = dev2major(dev); 751 if (maj == NODEV) { 752 error("%s: can't make %s device from `%s'", 753 name, what, nv->nv_str); 754 return (1); 755 } 756 nv->nv_int = makedev(maj, unit * maxpartitions + part); 757 } 758 759 nv->nv_name = dev->d_name; 760 return (0); 761 } 762 763 /* 764 * Add a completed configuration to the list. 765 */ 766 void 767 addconf(struct config *cf0) 768 { 769 struct config *cf; 770 struct nvlist *nv; 771 const char *name; 772 773 name = cf0->cf_name; 774 cf = ecalloc(1, sizeof *cf); 775 if (ht_insert(cfhashtab, name, cf)) { 776 error("configuration `%s' already defined", name); 777 free(cf); 778 goto bad; 779 } 780 *cf = *cf0; 781 782 /* 783 * Resolve the root device. 784 */ 785 if (cf->cf_root->nv_str != s_qmark) { 786 nv = cf->cf_root; 787 if (nv == NULL) { 788 error("%s: no root device specified", name); 789 goto bad; 790 } 791 if (resolve(&cf->cf_root, name, "root", nv, 'a')) 792 goto bad; 793 } 794 795 /* 796 * Resolve the dump device. 797 */ 798 if (cf->cf_dump == NULL || cf->cf_dump->nv_str == s_qmark) { 799 /* 800 * Wildcarded dump device is equivalent to unspecified. 801 */ 802 cf->cf_dump = NULL; 803 } else if (cf->cf_dump->nv_str == s_none) { 804 /* 805 * Operator has requested that no dump device should be 806 * configured; do nothing. 807 */ 808 } else { 809 if (resolve(&cf->cf_dump, name, "dumps", cf->cf_dump, 'b')) 810 goto bad; 811 } 812 813 /* Wildcarded fstype is `unspecified'. */ 814 if (cf->cf_fstype == s_qmark) 815 cf->cf_fstype = NULL; 816 817 TAILQ_INSERT_TAIL(&allcf, cf, cf_next); 818 return; 819 bad: 820 nvfreel(cf0->cf_root); 821 nvfreel(cf0->cf_dump); 822 } 823 824 void 825 setconf(struct nvlist **npp, const char *what, struct nvlist *v) 826 { 827 828 if (*npp != NULL) { 829 error("duplicate %s specification", what); 830 nvfreel(v); 831 } else 832 *npp = v; 833 } 834 835 void 836 setfstype(const char **fstp, const char *v) 837 { 838 839 if (*fstp != NULL) { 840 error("multiple fstype specifications"); 841 return; 842 } 843 844 if (v != s_qmark && OPT_FSOPT(v)) { 845 error("\"%s\" is not a configured file system", v); 846 return; 847 } 848 849 *fstp = v; 850 } 851 852 static struct devi * 853 newdevi(const char *name, int unit, struct devbase *d) 854 { 855 struct devi *i; 856 857 i = ecalloc(1, sizeof *i); 858 i->i_name = name; 859 i->i_unit = unit; 860 i->i_base = d; 861 i->i_bsame = NULL; 862 i->i_asame = NULL; 863 i->i_alias = NULL; 864 i->i_at = NULL; 865 i->i_pspec = NULL; 866 i->i_atdeva = NULL; 867 i->i_locs = NULL; 868 i->i_cfflags = 0; 869 i->i_lineno = currentline(); 870 if (unit >= d->d_umax) 871 d->d_umax = unit + 1; 872 return (i); 873 } 874 875 /* 876 * Add the named device as attaching to the named attribute (or perhaps 877 * another device instead) plus unit number. 878 */ 879 void 880 adddev(const char *name, const char *at, struct nvlist *loclist, int flags) 881 { 882 struct devi *i; /* the new instance */ 883 struct pspec *p; /* and its pspec */ 884 struct attr *attr; /* attribute that allows attach */ 885 struct devbase *ib; /* i->i_base */ 886 struct devbase *ab; /* not NULL => at another dev */ 887 struct nvlist *nv; 888 struct deva *iba; /* devbase attachment used */ 889 const char *cp; 890 int atunit; 891 char atbuf[NAMESIZE]; 892 int hit; 893 894 ab = NULL; 895 iba = NULL; 896 if (at == NULL) { 897 /* "at root" */ 898 p = NULL; 899 if ((i = getdevi(name)) == NULL) 900 goto bad; 901 /* 902 * Must warn about i_unit > 0 later, after taking care of 903 * the STAR cases (we could do non-star's here but why 904 * bother?). Make sure this device can be at root. 905 */ 906 ib = i->i_base; 907 hit = 0; 908 for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 909 if (onlist(iba->d_atlist, NULL)) { 910 hit = 1; 911 break; 912 } 913 if (!hit) { 914 error("`%s' cannot attach to the root", ib->d_name); 915 goto bad; 916 } 917 attr = &errattr; /* a convenient "empty" attr */ 918 } else { 919 if (split(at, strlen(at), atbuf, sizeof atbuf, &atunit)) { 920 error("invalid attachment name `%s'", at); 921 /* (void)getdevi(name); -- ??? */ 922 goto bad; 923 } 924 if ((i = getdevi(name)) == NULL) 925 goto bad; 926 ib = i->i_base; 927 928 /* 929 * Devices can attach to two types of things: Attributes, 930 * and other devices (which have the appropriate attributes 931 * to allow attachment). 932 * 933 * (1) If we're attached to an attribute, then we don't need 934 * look at the parent base device to see what attributes 935 * it has, and make sure that we can attach to them. 936 * 937 * (2) If we're attached to a real device (i.e. named in 938 * the config file), we want to remember that so that 939 * at cross-check time, if the device we're attached to 940 * is missing but other devices which also provide the 941 * attribute are present, we don't get a false "OK." 942 * 943 * (3) If the thing we're attached to is an attribute 944 * but is actually named in the config file, we still 945 * have to remember its devbase. 946 */ 947 cp = intern(atbuf); 948 949 /* Figure out parent's devbase, to satisfy case (3). */ 950 ab = ht_lookup(devbasetab, cp); 951 952 /* Find out if it's an attribute. */ 953 attr = ht_lookup(attrtab, cp); 954 955 /* Make sure we're _really_ attached to the attr. Case (1). */ 956 if (attr != NULL && onlist(attr->a_devs, ib)) 957 goto findattachment; 958 959 /* 960 * Else a real device, and not just an attribute. Case (2). 961 * 962 * Have to work a bit harder to see whether we have 963 * something like "tg0 at esp0" (where esp is merely 964 * not an attribute) or "tg0 at nonesuch0" (where 965 * nonesuch is not even a device). 966 */ 967 if (ab == NULL) { 968 error("%s at %s: `%s' unknown", 969 name, at, atbuf); 970 goto bad; 971 } 972 973 /* 974 * See if the named parent carries an attribute 975 * that allows it to supervise device ib. 976 */ 977 for (nv = ab->d_attrs; nv != NULL; nv = nv->nv_next) { 978 attr = nv->nv_ptr; 979 if (onlist(attr->a_devs, ib)) 980 goto findattachment; 981 } 982 error("`%s' cannot attach to `%s'", ib->d_name, atbuf); 983 goto bad; 984 985 findattachment: 986 /* 987 * Find the parent spec. If a matching one has not yet been 988 * created, create one. 989 */ 990 p = getpspec(attr, ab, atunit); 991 p->p_devs = newnv(NULL, NULL, i, 0, p->p_devs); 992 993 /* find out which attachment it uses */ 994 hit = 0; 995 for (iba = ib->d_ahead; iba != NULL; iba = iba->d_bsame) 996 if (onlist(iba->d_atlist, attr)) { 997 hit = 1; 998 break; 999 } 1000 if (!hit) 1001 panic("adddev: can't figure out attachment"); 1002 } 1003 if ((i->i_locs = fixloc(name, attr, loclist)) == NULL) 1004 goto bad; 1005 i->i_at = at; 1006 i->i_pspec = p; 1007 i->i_atdeva = iba; 1008 i->i_cfflags = flags; 1009 1010 *iba->d_ipp = i; 1011 iba->d_ipp = &i->i_asame; 1012 1013 /* all done, fall into ... */ 1014 bad: 1015 nvfreel(loclist); 1016 return; 1017 } 1018 1019 void 1020 deldev(const char *name, const char *at) 1021 { 1022 struct devi *firsti, *i, *match; 1023 struct devbase *d; 1024 int unit; 1025 char base[NAMESIZE]; 1026 1027 if (split(name, strlen(name), base, sizeof base, &unit)) { 1028 error("invalid device name `%s'", name); 1029 return; 1030 } 1031 d = ht_lookup(devbasetab, intern(base)); 1032 if (d == NULL) { 1033 error("%s: unknown device `%s'", name, base); 1034 return; 1035 } 1036 if (d->d_ispseudo) { 1037 error("%s: %s is a pseudo-device", name, base); 1038 return; 1039 } 1040 if ((firsti = ht_lookup(devitab, name)) == NULL) { 1041 error("`%s' not defined", name); 1042 return; 1043 } 1044 match = NULL; 1045 if (strcmp(at, firsti->i_at) == 0) { 1046 match = firsti; 1047 } else { 1048 for (i = firsti; i->i_alias; i = i->i_alias) { 1049 if (strcmp(at, i->i_at) == 0) { 1050 match = i; 1051 break; 1052 } 1053 } 1054 } 1055 if (match == NULL) { 1056 error("`%s' at `%s' not found", name, at); 1057 return; 1058 } 1059 1060 // XXXLUKEM: actually implement... 1061 error("`%s' at `%s': device deletion not implemented", name, at); 1062 } 1063 1064 void 1065 addpseudo(const char *name, int number) 1066 { 1067 struct devbase *d; 1068 struct devi *i; 1069 1070 d = ht_lookup(devbasetab, name); 1071 if (d == NULL) { 1072 error("undefined pseudo-device %s", name); 1073 return; 1074 } 1075 if (!d->d_ispseudo) { 1076 error("%s is a real device, not a pseudo-device", name); 1077 return; 1078 } 1079 if (ht_lookup(devitab, name) != NULL) { 1080 error("`%s' already defined", name); 1081 return; 1082 } 1083 i = newdevi(name, number - 1, d); /* foo 16 => "foo0..foo15" */ 1084 if (ht_insert(devitab, name, i)) 1085 panic("addpseudo(%s)", name); 1086 TAILQ_INSERT_TAIL(&allpseudo, i, i_next); 1087 } 1088 1089 void 1090 delpseudo(const char *name) 1091 { 1092 struct devbase *d; 1093 struct devi *i; 1094 1095 d = ht_lookup(devbasetab, name); 1096 if (d == NULL) { 1097 error("undefined pseudo-device %s", name); 1098 return; 1099 } 1100 if (!d->d_ispseudo) { 1101 error("%s is a real device, not a pseudo-device", name); 1102 return; 1103 } 1104 if ((i = ht_lookup(devitab, name)) == NULL) { 1105 error("`%s' not defined", name); 1106 return; 1107 } 1108 d->d_umax = 0; /* clear neads-count entries */ 1109 TAILQ_REMOVE(&allpseudo, i, i_next); 1110 if (ht_remove(devitab, name)) 1111 panic("delpseudo(%s) - can't remove from devitab", name); 1112 } 1113 1114 void 1115 adddevm(const char *name, int cmajor, int bmajor, struct nvlist *options) 1116 { 1117 struct devm *dm; 1118 1119 if (cmajor < -1 || cmajor >= 4096) { 1120 error("character major %d is invalid", cmajor); 1121 nvfreel(options); 1122 return; 1123 } 1124 1125 if (bmajor < -1 || bmajor >= 4096) { 1126 error("block major %d is invalid", bmajor); 1127 nvfreel(options); 1128 return; 1129 } 1130 if (cmajor == -1 && bmajor == -1) { 1131 error("both character/block majors are not specified"); 1132 nvfreel(options); 1133 return; 1134 } 1135 1136 dm = ecalloc(1, sizeof(*dm)); 1137 dm->dm_srcfile = yyfile; 1138 dm->dm_srcline = currentline(); 1139 dm->dm_name = name; 1140 dm->dm_cmajor = cmajor; 1141 dm->dm_bmajor = bmajor; 1142 dm->dm_opts = options; 1143 1144 TAILQ_INSERT_TAIL(&alldevms, dm, dm_next); 1145 1146 maxcdevm = MAX(maxcdevm, dm->dm_cmajor); 1147 maxbdevm = MAX(maxbdevm, dm->dm_bmajor); 1148 } 1149 1150 void 1151 fixdevis(void) 1152 { 1153 struct devi *i; 1154 1155 TAILQ_FOREACH(i, &alldevi, i_next) 1156 selectbase(i->i_base, i->i_atdeva); 1157 1158 TAILQ_FOREACH(i, &allpseudo, i_next) 1159 selectbase(i->i_base, NULL); 1160 } 1161 1162 /* 1163 * Look up a parent spec, creating a new one if it does not exist. 1164 */ 1165 static struct pspec * 1166 getpspec(struct attr *attr, struct devbase *ab, int atunit) 1167 { 1168 struct pspec *p; 1169 1170 TAILQ_FOREACH(p, &allpspecs, p_list) { 1171 if (p->p_iattr == attr && 1172 p->p_atdev == ab && 1173 p->p_atunit == atunit) 1174 return (p); 1175 } 1176 1177 p = ecalloc(1, sizeof(*p)); 1178 1179 p->p_iattr = attr; 1180 p->p_atdev = ab; 1181 p->p_atunit = atunit; 1182 p->p_inst = npspecs++; 1183 1184 TAILQ_INSERT_TAIL(&allpspecs, p, p_list); 1185 1186 return (p); 1187 } 1188 1189 /* 1190 * Define a new instance of a specific device. 1191 */ 1192 static struct devi * 1193 getdevi(const char *name) 1194 { 1195 struct devi *i, *firsti; 1196 struct devbase *d; 1197 int unit; 1198 char base[NAMESIZE]; 1199 1200 if (split(name, strlen(name), base, sizeof base, &unit)) { 1201 error("invalid device name `%s'", name); 1202 return (NULL); 1203 } 1204 d = ht_lookup(devbasetab, intern(base)); 1205 if (d == NULL) { 1206 error("%s: unknown device `%s'", name, base); 1207 return (NULL); 1208 } 1209 if (d->d_ispseudo) { 1210 error("%s: %s is a pseudo-device", name, base); 1211 return (NULL); 1212 } 1213 firsti = ht_lookup(devitab, name); 1214 i = newdevi(name, unit, d); 1215 if (firsti == NULL) { 1216 if (ht_insert(devitab, name, i)) 1217 panic("getdevi(%s)", name); 1218 *d->d_ipp = i; 1219 d->d_ipp = &i->i_bsame; 1220 } else { 1221 while (firsti->i_alias) 1222 firsti = firsti->i_alias; 1223 firsti->i_alias = i; 1224 } 1225 TAILQ_INSERT_TAIL(&alldevi, i, i_next); 1226 ndevi++; 1227 return (i); 1228 } 1229 1230 static const char * 1231 concat(const char *name, int c) 1232 { 1233 int len; 1234 char buf[NAMESIZE]; 1235 1236 len = strlen(name); 1237 if (len + 2 > sizeof(buf)) { 1238 error("device name `%s%c' too long", name, c); 1239 len = sizeof(buf) - 2; 1240 } 1241 memmove(buf, name, len); 1242 buf[len] = c; 1243 buf[len + 1] = 0; 1244 return (intern(buf)); 1245 } 1246 1247 const char * 1248 starref(const char *name) 1249 { 1250 1251 return (concat(name, '*')); 1252 } 1253 1254 const char * 1255 wildref(const char *name) 1256 { 1257 1258 return (concat(name, '?')); 1259 } 1260 1261 /* 1262 * Split a name like "foo0" into base name (foo) and unit number (0). 1263 * Return 0 on success. To make this useful for names like "foo0a", 1264 * the length of the "foo0" part is one of the arguments. 1265 */ 1266 static int 1267 split(const char *name, size_t nlen, char *base, size_t bsize, int *aunit) 1268 { 1269 const char *cp; 1270 int c, l; 1271 1272 l = nlen; 1273 if (l < 2 || l >= bsize || isdigit((unsigned char)*name)) 1274 return (1); 1275 c = (u_char)name[--l]; 1276 if (!isdigit(c)) { 1277 if (c == '*') 1278 *aunit = STAR; 1279 else if (c == '?') 1280 *aunit = WILD; 1281 else 1282 return (1); 1283 } else { 1284 cp = &name[l]; 1285 while (isdigit((unsigned char)cp[-1])) 1286 l--, cp--; 1287 *aunit = atoi(cp); 1288 } 1289 memmove(base, name, l); 1290 base[l] = 0; 1291 return (0); 1292 } 1293 1294 void 1295 selectattr(struct attr *a) 1296 { 1297 1298 (void)ht_insert(selecttab, a->a_name, (char *)a->a_name); 1299 } 1300 1301 /* 1302 * We have an instance of the base foo, so select it and all its 1303 * attributes for "optional foo". 1304 */ 1305 static void 1306 selectbase(struct devbase *d, struct deva *da) 1307 { 1308 struct attr *a; 1309 struct nvlist *nv; 1310 1311 (void)ht_insert(selecttab, d->d_name, (char *)d->d_name); 1312 for (nv = d->d_attrs; nv != NULL; nv = nv->nv_next) { 1313 a = nv->nv_ptr; 1314 expandattr(a, selectattr); 1315 } 1316 if (da != NULL) { 1317 (void)ht_insert(selecttab, da->d_name, (char *)da->d_name); 1318 for (nv = da->d_attrs; nv != NULL; nv = nv->nv_next) { 1319 a = nv->nv_ptr; 1320 expandattr(a, selectattr); 1321 } 1322 } 1323 } 1324 1325 /* 1326 * Is the given pointer on the given list of pointers? 1327 */ 1328 static int 1329 onlist(struct nvlist *nv, void *ptr) 1330 { 1331 for (; nv != NULL; nv = nv->nv_next) 1332 if (nv->nv_ptr == ptr) 1333 return (1); 1334 return (0); 1335 } 1336 1337 static char * 1338 extend(char *p, const char *name) 1339 { 1340 int l; 1341 1342 l = strlen(name); 1343 memmove(p, name, l); 1344 p += l; 1345 *p++ = ','; 1346 *p++ = ' '; 1347 return (p); 1348 } 1349 1350 /* 1351 * Check that we got all required locators, and default any that are 1352 * given as "?" and have defaults. Return 0 on success. 1353 */ 1354 static const char ** 1355 fixloc(const char *name, struct attr *attr, struct nvlist *got) 1356 { 1357 struct nvlist *m, *n; 1358 int ord; 1359 const char **lp; 1360 int nmissing, nextra, nnodefault; 1361 char *mp, *ep, *ndp; 1362 char missing[1000], extra[1000], nodefault[1000]; 1363 static const char *nullvec[1]; 1364 1365 /* 1366 * Look for all required locators, and number the given ones 1367 * according to the required order. While we are numbering, 1368 * set default values for defaulted locators. 1369 */ 1370 if (attr->a_loclen == 0) /* e.g., "at root" */ 1371 lp = nullvec; 1372 else 1373 lp = emalloc((attr->a_loclen + 1) * sizeof(const char *)); 1374 for (n = got; n != NULL; n = n->nv_next) 1375 n->nv_int = -1; 1376 nmissing = 0; 1377 mp = missing; 1378 /* yes, this is O(mn), but m and n should be small */ 1379 for (ord = 0, m = attr->a_locs; m != NULL; m = m->nv_next, ord++) { 1380 for (n = got; n != NULL; n = n->nv_next) { 1381 if (n->nv_name == m->nv_name) { 1382 n->nv_int = ord; 1383 break; 1384 } 1385 } 1386 if (n == NULL && m->nv_int == 0) { 1387 nmissing++; 1388 mp = extend(mp, m->nv_name); 1389 } 1390 lp[ord] = m->nv_str; 1391 } 1392 if (ord != attr->a_loclen) 1393 panic("fixloc"); 1394 lp[ord] = NULL; 1395 nextra = 0; 1396 ep = extra; 1397 nnodefault = 0; 1398 ndp = nodefault; 1399 for (n = got; n != NULL; n = n->nv_next) { 1400 if (n->nv_int >= 0) { 1401 if (n->nv_str != NULL) 1402 lp[n->nv_int] = n->nv_str; 1403 else if (lp[n->nv_int] == NULL) { 1404 nnodefault++; 1405 ndp = extend(ndp, n->nv_name); 1406 } 1407 } else { 1408 nextra++; 1409 ep = extend(ep, n->nv_name); 1410 } 1411 } 1412 if (nextra) { 1413 ep[-2] = 0; /* kill ", " */ 1414 error("%s: extraneous locator%s: %s", 1415 name, nextra > 1 ? "s" : "", extra); 1416 } 1417 if (nmissing) { 1418 mp[-2] = 0; 1419 error("%s: must specify %s", name, missing); 1420 } 1421 if (nnodefault) { 1422 ndp[-2] = 0; 1423 error("%s: cannot wildcard %s", name, nodefault); 1424 } 1425 if (nmissing || nnodefault) { 1426 free(lp); 1427 lp = NULL; 1428 } 1429 return (lp); 1430 } 1431