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