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