8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fs.d / preenlib.c
blobcb474b261d32ff87964bb125434ef5c35b436f35
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
22 #pragma ident "%Z%%M% %I% %E% SMI"
24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 * common routines for parallelization (used by both fsck and quotacheck)
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <dlfcn.h>
34 #include <macros.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <sys/mntent.h>
38 #include <sys/dkio.h>
41 * data structures for parallelization
43 struct driver {
44 char *name; /* driver name (from DKIOCINFO) */
45 uint_t mapsize; /* size of `busymap' */
46 uint_t *busymap; /* bitmask of active units */
47 int (*choosefunc)(); /* driver specific chooser */
48 void *data; /* driver private data */
51 struct onedev {
52 int drvid; /* index in driver array */
53 uint_t mapsize; /* size of `unitmap' */
54 uint_t *unitmap; /* unit #'s (from DKIOCINFO) */
55 struct onedev *nxtdev;
58 struct rawdev {
59 char *devname; /* name passed to preen_addev */
60 struct onedev *alldevs; /* info about each component device */
61 struct rawdev *nxtrd; /* next entry in list */
64 static int debug = 0;
67 * defines used in building shared object names
70 /* the directory where we find shared objects */
71 #define OBJECT_DIRECTORY "/usr/lib/drv"
73 /* a shared object name is OBJECT_PREFIX || driver_name */
74 #define OBJECT_PREFIX "preen_"
76 /* the version of the driver interface we support */
77 #define OBJECT_VERSION 1
79 /* the "build" entry point for a driver specific object is named this */
80 #define BUILD_ENTRY preen_build_devs
81 #define BUILD_NAME "preen_build_devs"
83 #define DRIVER_ALLOC 10
84 static int ndrivers, ndalloc;
85 static struct driver *dlist;
87 static struct rawdev *unchecked, *active, *get_runnable();
88 static struct onedev *alloc_dev();
89 static int chooseone();
91 #define WORDSIZE (NBBY * sizeof (uint_t))
93 void preen_addunit(void *, char *, int (*)(), void *, uint_t);
94 int preen_subdev(char *, struct dk_cinfo *, void *);
96 static int alloc_driver(char *, int (*)(), void *);
97 static void addunit(struct onedev *, uint_t);
98 static void makebusy(struct onedev *);
99 static void notbusy(struct rawdev *);
102 * add the given device to the list of devices to be checked
105 preen_addev(char *devnm)
107 struct rawdev *rdp;
108 int fd;
109 struct dk_cinfo dki;
110 extern char *strdup();
112 if ((fd = open64(devnm, O_RDONLY)) == -1) {
113 perror(devnm);
114 return (-1);
116 if (ioctl(fd, DKIOCINFO, &dki) == -1) {
117 perror("DKIOCINFO");
118 fprintf(stderr, "device: `%s'\n", devnm);
119 (void) close(fd);
120 return (-1);
122 (void) close(fd);
123 if ((rdp = (struct rawdev *)malloc(sizeof (struct rawdev))) == NULL) {
124 (void) fprintf(stderr, "out of memory in preenlib\n");
125 return (-1);
127 if ((rdp->devname = strdup(devnm)) == NULL) {
128 (void) fprintf(stderr, "out of memory in preenlib\n");
129 return (-1);
131 rdp->alldevs = NULL;
132 rdp->nxtrd = NULL;
134 if (preen_subdev(devnm, &dki, (void *)rdp)) {
135 preen_addunit(rdp, dki.dki_dname, NULL, NULL, dki.dki_unit);
138 rdp->nxtrd = unchecked;
139 unchecked = rdp;
140 return (0);
144 preen_subdev(char *name, struct dk_cinfo *dkiop, void *dp)
146 char modname[255];
147 void *dlhandle;
148 int (*fptr)();
150 (void) sprintf(modname, "%s/%s%s.so.%d",
151 OBJECT_DIRECTORY, OBJECT_PREFIX, dkiop->dki_dname, OBJECT_VERSION);
152 dlhandle = dlopen(modname, RTLD_LAZY);
153 if (dlhandle == NULL) {
154 if (debug)
155 (void) fprintf(stderr, "preen_subdev: %s\n", dlerror());
156 return (1);
158 fptr = (int (*)())dlsym(dlhandle, BUILD_NAME);
159 if (fptr == NULL) {
160 if (debug)
161 (void) fprintf(stderr, "preen_subdev: %s\n", dlerror());
162 return (1);
164 (*fptr)(name, dkiop, dp);
165 return (0);
169 * select a device from the "unchecked" list, and add it to the
170 * active list.
173 preen_getdev(char *devnm)
175 struct rawdev *rdp;
176 struct onedev *dp;
178 if (unchecked == NULL)
179 return (0);
181 rdp = get_runnable(&unchecked);
183 if (rdp) {
184 for (dp = rdp->alldevs; dp; dp = dp->nxtdev) {
185 makebusy(dp);
187 rdp->nxtrd = active;
188 active = rdp;
189 (void) strcpy(devnm, rdp->devname);
190 return (1);
191 } else {
192 return (2);
197 preen_releasedev(char *name)
199 struct rawdev *dp, *ldp;
201 for (ldp = NULL, dp = active; dp != NULL; ldp = dp, dp = dp->nxtrd) {
202 if (strcmp(dp->devname, name) == 0)
203 break;
206 if (dp == NULL)
207 return (-1);
208 if (ldp != NULL) {
209 ldp->nxtrd = dp->nxtrd;
210 } else {
211 active = dp->nxtrd;
214 notbusy(dp);
216 * free(dp->devname);
217 * free(dp);
219 return (0);
222 static
223 struct rawdev *
224 get_runnable(struct rawdev **devlist)
226 struct rawdev *last, *rdp;
227 struct onedev *devp;
228 struct driver *drvp;
229 int rc = 1;
231 for (last = NULL, rdp = *devlist; rdp; last = rdp, rdp = rdp->nxtrd) {
232 for (devp = rdp->alldevs; devp != NULL; devp = devp->nxtdev) {
233 drvp = &dlist[devp->drvid];
234 rc = (*drvp->choosefunc)(devp->mapsize, devp->unitmap,
235 drvp->mapsize, drvp->busymap);
236 if (rc != 0)
237 break;
239 if (rc == 0)
240 break;
244 * remove from list...
246 if (rdp) {
247 if (last) {
248 last->nxtrd = rdp->nxtrd;
249 } else {
250 *devlist = rdp->nxtrd;
254 return (rdp);
258 * add the given driver/unit reference to the `rawdev' structure identified
259 * by `cookie'
260 * If a new `driver' structure needs to be created, associate the given
261 * choosing function and driver private data with it.
263 void
264 preen_addunit(
265 void *cookie,
266 char *dname, /* driver name */
267 int (*cf)(), /* candidate choosing function */
268 void *datap, /* driver private data */
269 uint_t unit) /* unit number */
271 int drvid;
272 struct driver *dp;
273 struct onedev *devp;
274 struct rawdev *rdp = (struct rawdev *)cookie;
277 * locate the driver struct
279 dp = NULL;
280 for (drvid = 0; drvid < ndrivers; drvid++) {
281 if (strcmp(dlist[drvid].name, dname) == 0) {
282 dp = &dlist[drvid];
283 break;
287 if (dp == NULL) {
289 * driver struct doesn't exist yet -- create one
291 if (cf == NULL)
292 cf = chooseone;
293 drvid = alloc_driver(dname, cf, datap);
294 dp = &dlist[drvid];
297 for (devp = rdp->alldevs; devp != NULL; devp = devp->nxtdev) {
299 * see if this device already references the given driver
301 if (devp->drvid == drvid)
302 break;
305 if (devp == NULL) {
307 * allocate a new `struct onedev' and chain it in
308 * rdp->alldevs...
310 devp = alloc_dev(drvid);
311 devp->nxtdev = rdp->alldevs;
312 rdp->alldevs = devp;
316 * add `unit' to the unitmap in devp
318 addunit(devp, unit);
321 static
323 alloc_driver(char *name, int (*cf)(), void *datap)
325 struct driver *dp;
326 extern char *strdup();
328 if (ndrivers == ndalloc) {
329 dlist = ndalloc ?
330 (struct driver *)
331 realloc(dlist, sizeof (struct driver) * DRIVER_ALLOC) :
332 (struct driver *)
333 malloc(sizeof (struct driver) * DRIVER_ALLOC);
334 if (dlist == NULL) {
335 (void) fprintf(stderr, "out of memory in preenlib\n");
336 exit(1);
338 ndalloc += DRIVER_ALLOC;
341 dp = &dlist[ndrivers];
342 dp->name = strdup(name);
343 if (dp->name == NULL) {
344 (void) fprintf(stderr, "out of memory in preenlib\n");
345 exit(1);
347 dp->choosefunc = cf;
348 dp->data = datap;
349 dp->mapsize = 0;
350 dp->busymap = NULL;
351 return (ndrivers++);
354 static
355 struct onedev *
356 alloc_dev(int did)
358 struct onedev *devp;
360 devp = (struct onedev *)malloc(sizeof (struct onedev));
361 if (devp == NULL) {
362 (void) fprintf(stderr, "out of memory in preenlib\n");
363 exit(1);
365 devp->drvid = did;
366 devp->mapsize = 0;
367 devp->unitmap = NULL;
368 devp->nxtdev = NULL;
369 return (devp);
372 static
373 void
374 addunit(struct onedev *devp, uint_t unit)
376 uint_t newsize;
378 newsize = howmany(unit+1, WORDSIZE);
379 if (devp->mapsize < newsize) {
380 devp->unitmap = devp->mapsize ?
381 (uint_t *)realloc(devp->unitmap,
382 newsize * sizeof (uint_t)) :
383 (uint_t *)malloc(newsize * sizeof (uint_t));
384 if (devp->unitmap == NULL) {
385 (void) fprintf(stderr, "out of memory in preenlib\n");
386 exit(1);
388 (void) memset((char *)&devp->unitmap[devp->mapsize], 0,
389 (uint_t)((newsize - devp->mapsize) * sizeof (uint_t)));
390 devp->mapsize = newsize;
392 devp->unitmap[unit / WORDSIZE] |= (1 << (unit % WORDSIZE));
395 static int
396 chooseone(int devmapsize, ulong_t *devmap, int drvmapsize, ulong_t *drvmap)
398 int i;
400 for (i = 0; i < min(devmapsize, drvmapsize); i++) {
401 if (devmap[i] & drvmap[i])
402 return (1);
404 return (0);
408 * mark the given driver/unit pair as busy. This is called from
409 * preen_getdev.
411 static
412 void
413 makebusy(struct onedev *dev)
415 struct driver *drvp = &dlist[dev->drvid];
416 int newsize = dev->mapsize;
417 int i;
419 if (drvp->mapsize < newsize) {
420 drvp->busymap = drvp->mapsize ?
421 (uint_t *)realloc(drvp->busymap,
422 newsize * sizeof (uint_t)) :
423 (uint_t *)malloc(newsize * sizeof (uint_t));
424 if (drvp->busymap == NULL) {
425 (void) fprintf(stderr, "out of memory in preenlib\n");
426 exit(1);
428 (void) memset((char *)&drvp->busymap[drvp->mapsize], 0,
429 (uint_t)((newsize - drvp->mapsize) * sizeof (uint_t)));
430 drvp->mapsize = newsize;
433 for (i = 0; i < newsize; i++)
434 drvp->busymap[i] |= dev->unitmap[i];
438 * make each device in the given `rawdev' un-busy.
439 * Called from preen_releasedev
441 static
442 void
443 notbusy(struct rawdev *rd)
445 struct onedev *devp;
446 struct driver *drvp;
447 int i;
449 for (devp = rd->alldevs; devp; devp = devp->nxtdev) {
450 drvp = &dlist[devp->drvid];
451 for (i = 0; i < devp->mapsize; i++)
452 drvp->busymap[i] &= ~(devp->unitmap[i]);