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