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