/* * Add a device base to a list in an attribute (actually, to any list). * Note that this does not check for duplicates, and does reverse the * list order, but no one cares anyway. */ static struct nvlist * addtoattr(struct nvlist *l, struct devbase *dev) { struct nvlist *n; n = newnv(NULL, NULL, dev, 0, l); return (n); }
/* * Called from fixfiles when eval'ing a selection expression for a * file that will generate a .h with flags. We will need the flat list. */ static int fixfsel(const char *name, void *context) { struct nvlist ***p = context; struct nvlist *nv; int sel; sel = ht_lookup(selecttab, name) != NULL; nv = newnv(name, NULL, NULL, sel, NULL); **p = nv; *p = &nv->nv_next; return (sel); }
/* * Define a device. This may (or may not) also define an interface * attribute and/or refer to existing attributes. */ void defdev(struct devbase *dev, int ispseudo, struct nvlist *loclist, struct nvlist *attrs) { struct nvlist *nv; struct attr *a; if (dev == &errdev) goto bad; if (dev->d_isdef) { error("redefinition of `%s'", dev->d_name); goto bad; } dev->d_isdef = 1; if (has_errobj(attrs, &errattr)) goto bad; /* * Handle implicit attribute definition from locator list. Do * this before scanning the `at' list so that we can have, e.g.: * device foo at other, foo { slot = -1 } * (where you can plug in a foo-bus extender to a foo-bus). */ if (loclist != NULL) { nv = loclist; loclist = NULL; /* defattr disposes of them for us */ if (defattr(dev->d_name, nv)) goto bad; attrs = newnv(dev->d_name, NULL, getattr(dev->d_name), 0, attrs); } /* Committed! Set up fields. */ dev->d_ispseudo = ispseudo; dev->d_attrs = attrs; /* * For each interface attribute this device refers to, add this * device to its reference list. This makes, e.g., finding all * "scsi"s easier. */ for (nv = attrs; nv != NULL; nv = nv->nv_next) { a = nv->nv_ptr; if (a->a_iattr) a->a_refs = addtoattr(a->a_refs, dev); } return; bad: nvfreel(loclist); nvfreel(attrs); }
/* * Called when evaluating a needs-count expression. Make sure the * atom is a countable device. The expression succeeds iff there * is at least one of them (note that while `xx*' will not always * set xx's d_umax > 0, you cannot mix '*' and needs-count). The * mkheaders() routine wants a flattened, in-order list of the * atoms for `#define name value' lines, so we build that as we * are called to eval each atom. */ static int fixcount(const char *name, void *context) { struct nvlist ***p = context; struct devbase *dev; struct nvlist *nv; dev = ht_lookup(devbasetab, name); if (dev == NULL) /* cannot occur here; we checked earlier */ panic("fixcount(%s)", name); nv = newnv(name, NULL, NULL, dev->d_umax, NULL); **p = nv; *p = &nv->nv_next; (void)ht_insert(needcnttab, name, nv); return (dev->d_umax != 0); }
/* * Add a name=value pair to an option list. The value may be NULL. */ static int do_option(struct hashtab *ht, struct nvlist ***nppp, const char *name, const char *value, const char *type) { struct nvlist *nv; /* assume it will work */ nv = newnv(name, value, NULL, 0, NULL); if (ht_insert(ht, name, nv) == 0) { **nppp = nv; *nppp = &nv->nv_next; return (0); } /* oops, already got that option */ nvfree(nv); if ((nv = ht_lookup(ht, name)) == NULL) panic("do_option"); if (nv->nv_str != NULL) error("already have %s `%s=%s'", type, name, nv->nv_str); else error("already have %s `%s'", type, name); return (1); }
/* * Map things like "ra0b" => makedev(major("ra"), 0*maxpartitions + 'b'-'a'). * Handle the case where the device number is given but there is no * corresponding name, and map NULL to the default. */ static int resolve(struct nvlist **nvp, const char *name, const char *what, struct nvlist *dflt, int part) { struct nvlist *nv; struct devbase *dev; const char *cp; int maj, min, l; int unit; char buf[NAMESIZE]; part -= 'a'; if ((part >= maxpartitions) || (part < 0)) panic("resolve"); if ((nv = *nvp) == NULL) { dev_t d = NODEV; /* * Apply default. Easiest to do this by number. * Make sure to retain NODEVness, if this is dflt's disposition. */ if (dflt->nv_int != NODEV) { maj = major(dflt->nv_int); min = (minor(dflt->nv_int) / maxpartitions) + part; d = makedev(maj, min); } *nvp = nv = newnv(NULL, NULL, NULL, d, NULL); } if (nv->nv_int != NODEV) { /* * By the numbers. Find the appropriate major number * to make a name. */ maj = major(nv->nv_int); min = minor(nv->nv_int); for (dev = allbases; dev != NULL; dev = dev->d_next) if (dev->d_major == maj) break; if (dev == NULL) (void)snprintf(buf, sizeof buf, "<%d/%d>", maj, min); else (void)snprintf(buf, sizeof buf, "%s%d%c", dev->d_name, min / maxpartitions, (min % maxpartitions) + 'a'); nv->nv_str = intern(buf); return (0); } if (nv->nv_str == NULL || nv->nv_str == s_nfs) /* * NFS spec. Leave as NODEV. */ return (0); /* * The normal case: things like "ra2b". Check for partition * suffix, remove it if there, and split into name ("ra") and * unit (2). */ l = strlen(nv->nv_str); cp = &nv->nv_str[l]; if (l > 1 && *--cp >= 'a' && *cp <= 'a'+maxpartitions && isdigit((unsigned char)cp[-1])) { l--; part = *cp - 'a'; } cp = nv->nv_str; if (split(cp, l, buf, sizeof buf, &unit)) { error("%s: invalid %s device name `%s'", name, what, cp); return (1); } dev = ht_lookup(devbasetab, intern(buf)); if (dev == NULL || dev->d_major == NODEV) { error("%s: can't make %s device from `%s'", name, what, nv->nv_str); return (1); } nv->nv_name = dev->d_name; nv->nv_int = makedev(dev->d_major, unit * maxpartitions + part); return (0); }