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