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