xref: /titanic_44/usr/src/cmd/fs.d/preenlib.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*7c478bd9Sstevel@tonic-gate /*
24*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1992,1996, by Sun Microsystems, Inc.
25*7c478bd9Sstevel@tonic-gate  * All rights reserved.
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * common routines for parallelization (used by both fsck and quotacheck)
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
33*7c478bd9Sstevel@tonic-gate #include <dlfcn.h>
34*7c478bd9Sstevel@tonic-gate #include <macros.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * data structures for parallelization
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate static struct driver {
44*7c478bd9Sstevel@tonic-gate 	char 	*name;			/* driver name (from DKIOCINFO) */
45*7c478bd9Sstevel@tonic-gate 	uint_t	mapsize;		/* size of `busymap' */
46*7c478bd9Sstevel@tonic-gate 	uint_t	*busymap;		/* bitmask of active units */
47*7c478bd9Sstevel@tonic-gate 	int	(*choosefunc)();	/* driver specific chooser */
48*7c478bd9Sstevel@tonic-gate 	void	*data;			/* driver private data */
49*7c478bd9Sstevel@tonic-gate };
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static struct onedev {
52*7c478bd9Sstevel@tonic-gate 	int	drvid;			/* index in driver array */
53*7c478bd9Sstevel@tonic-gate 	uint_t	mapsize;		/* size of `unitmap' */
54*7c478bd9Sstevel@tonic-gate 	uint_t	*unitmap;		/* unit #'s (from DKIOCINFO) */
55*7c478bd9Sstevel@tonic-gate 	struct onedev *nxtdev;
56*7c478bd9Sstevel@tonic-gate };
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static struct rawdev {
59*7c478bd9Sstevel@tonic-gate 	char	*devname;		/* name passed to preen_addev */
60*7c478bd9Sstevel@tonic-gate 	struct	onedev *alldevs;	/* info about each component device */
61*7c478bd9Sstevel@tonic-gate 	struct rawdev *nxtrd;		/* next entry in list */
62*7c478bd9Sstevel@tonic-gate };
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static int debug = 0;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * defines used in building shared object names
68*7c478bd9Sstevel@tonic-gate  */
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /* the directory where we find shared objects */
71*7c478bd9Sstevel@tonic-gate #define	OBJECT_DIRECTORY	"/usr/lib/drv"
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /* a shared object name is OBJECT_PREFIX || driver_name */
74*7c478bd9Sstevel@tonic-gate #define	OBJECT_PREFIX		"preen_"
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate /* the version of the driver interface we support */
77*7c478bd9Sstevel@tonic-gate #define	OBJECT_VERSION		1
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /* the "build" entry point for a driver specific object is named this */
80*7c478bd9Sstevel@tonic-gate #define	BUILD_ENTRY		preen_build_devs
81*7c478bd9Sstevel@tonic-gate #define	BUILD_NAME		"preen_build_devs"
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #define	DRIVER_ALLOC	10
84*7c478bd9Sstevel@tonic-gate static int ndrivers, ndalloc;
85*7c478bd9Sstevel@tonic-gate static struct driver *dlist;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate static struct rawdev *unchecked, *active, *get_runnable();
88*7c478bd9Sstevel@tonic-gate static struct onedev *alloc_dev();
89*7c478bd9Sstevel@tonic-gate static int chooseone();
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #define	WORDSIZE	(NBBY * sizeof (uint_t))
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate void 	preen_addunit(void *, char *, int (*)(), void *, uint_t);
94*7c478bd9Sstevel@tonic-gate int 	preen_subdev(char *, struct dk_cinfo *, void *);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static int 	alloc_driver(char *, int (*)(), void *);
97*7c478bd9Sstevel@tonic-gate static void 	addunit(struct onedev *, uint_t);
98*7c478bd9Sstevel@tonic-gate static void	makebusy(struct onedev *);
99*7c478bd9Sstevel@tonic-gate static void	notbusy(struct rawdev *);
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * add the given device to the list of devices to be checked
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate preen_addev(char *devnm)
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate 	struct rawdev *rdp;
107*7c478bd9Sstevel@tonic-gate 	int fd;
108*7c478bd9Sstevel@tonic-gate 	struct dk_cinfo dki;
109*7c478bd9Sstevel@tonic-gate 	extern char *strdup();
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if ((fd = open64(devnm, O_RDONLY)) == -1) {
112*7c478bd9Sstevel@tonic-gate 		perror(devnm);
113*7c478bd9Sstevel@tonic-gate 		return (-1);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &dki) == -1) {
116*7c478bd9Sstevel@tonic-gate 		perror("DKIOCINFO");
117*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "device: `%s'\n", devnm);
118*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
119*7c478bd9Sstevel@tonic-gate 		return (-1);
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
122*7c478bd9Sstevel@tonic-gate 	if ((rdp = (struct rawdev *)malloc(sizeof (struct rawdev))) == NULL) {
123*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "out of memory in preenlib\n");
124*7c478bd9Sstevel@tonic-gate 		return (-1);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 	if ((rdp->devname = strdup(devnm)) == NULL) {
127*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "out of memory in preenlib\n");
128*7c478bd9Sstevel@tonic-gate 		return (-1);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 	rdp->alldevs = NULL;
131*7c478bd9Sstevel@tonic-gate 	rdp->nxtrd = NULL;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (preen_subdev(devnm, &dki, (void *)rdp)) {
134*7c478bd9Sstevel@tonic-gate 		preen_addunit(rdp, dki.dki_dname, NULL, NULL, dki.dki_unit);
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	rdp->nxtrd = unchecked;
138*7c478bd9Sstevel@tonic-gate 	unchecked = rdp;
139*7c478bd9Sstevel@tonic-gate 	return (0);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate preen_subdev(char *name, struct dk_cinfo *dkiop, void *dp)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	char modname[255];
145*7c478bd9Sstevel@tonic-gate 	void *dlhandle;
146*7c478bd9Sstevel@tonic-gate 	int (*fptr)();
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	(void) sprintf(modname, "%s/%s%s.so.%d",
149*7c478bd9Sstevel@tonic-gate 	    OBJECT_DIRECTORY, OBJECT_PREFIX, dkiop->dki_dname, OBJECT_VERSION);
150*7c478bd9Sstevel@tonic-gate 	dlhandle = dlopen(modname, RTLD_LAZY);
151*7c478bd9Sstevel@tonic-gate 	if (dlhandle == NULL) {
152*7c478bd9Sstevel@tonic-gate 		if (debug)
153*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "preen_subdev: %s\n", dlerror());
154*7c478bd9Sstevel@tonic-gate 		return (1);
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 	fptr = (int (*)())dlsym(dlhandle, BUILD_NAME);
157*7c478bd9Sstevel@tonic-gate 	if (fptr == NULL) {
158*7c478bd9Sstevel@tonic-gate 		if (debug)
159*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "preen_subdev: %s\n", dlerror());
160*7c478bd9Sstevel@tonic-gate 		return (1);
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 	(*fptr)(name, dkiop, dp);
163*7c478bd9Sstevel@tonic-gate 	return (0);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate  * select a device from the "unchecked" list, and add it to the
168*7c478bd9Sstevel@tonic-gate  * active list.
169*7c478bd9Sstevel@tonic-gate  */
170*7c478bd9Sstevel@tonic-gate preen_getdev(char *devnm)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	struct rawdev *rdp;
173*7c478bd9Sstevel@tonic-gate 	register struct onedev *dp;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	if (unchecked == NULL)
176*7c478bd9Sstevel@tonic-gate 		return (0);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	rdp = get_runnable(&unchecked);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (rdp) {
181*7c478bd9Sstevel@tonic-gate 		for (dp = rdp->alldevs; dp; dp = dp->nxtdev) {
182*7c478bd9Sstevel@tonic-gate 			makebusy(dp);
183*7c478bd9Sstevel@tonic-gate 		}
184*7c478bd9Sstevel@tonic-gate 		rdp->nxtrd = active;
185*7c478bd9Sstevel@tonic-gate 		active = rdp;
186*7c478bd9Sstevel@tonic-gate 		(void) strcpy(devnm, rdp->devname);
187*7c478bd9Sstevel@tonic-gate 		return (1);
188*7c478bd9Sstevel@tonic-gate 	} else {
189*7c478bd9Sstevel@tonic-gate 		return (2);
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate preen_releasedev(char *name)
194*7c478bd9Sstevel@tonic-gate {
195*7c478bd9Sstevel@tonic-gate 	register struct rawdev *dp, *ldp;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	for (ldp = NULL, dp = active; dp != NULL; ldp = dp, dp = dp->nxtrd) {
198*7c478bd9Sstevel@tonic-gate 		if (strcmp(dp->devname, name) == 0)
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 	if (dp == NULL)
203*7c478bd9Sstevel@tonic-gate 		return (-1);
204*7c478bd9Sstevel@tonic-gate 	if (ldp != NULL) {
205*7c478bd9Sstevel@tonic-gate 		ldp->nxtrd = dp->nxtrd;
206*7c478bd9Sstevel@tonic-gate 	} else {
207*7c478bd9Sstevel@tonic-gate 		active = dp->nxtrd;
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	notbusy(dp);
211*7c478bd9Sstevel@tonic-gate 	/*
212*7c478bd9Sstevel@tonic-gate 	 * free(dp->devname);
213*7c478bd9Sstevel@tonic-gate 	 * free(dp);
214*7c478bd9Sstevel@tonic-gate 	 */
215*7c478bd9Sstevel@tonic-gate 	return (0);
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate static
219*7c478bd9Sstevel@tonic-gate struct rawdev *
220*7c478bd9Sstevel@tonic-gate get_runnable(struct rawdev **devlist)
221*7c478bd9Sstevel@tonic-gate {
222*7c478bd9Sstevel@tonic-gate 	register struct rawdev *last, *rdp;
223*7c478bd9Sstevel@tonic-gate 	register struct onedev *devp;
224*7c478bd9Sstevel@tonic-gate 	register struct driver *drvp;
225*7c478bd9Sstevel@tonic-gate 	int rc = 1;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	for (last = NULL, rdp = *devlist; rdp; last = rdp, rdp = rdp->nxtrd) {
228*7c478bd9Sstevel@tonic-gate 		for (devp = rdp->alldevs; devp != NULL; devp = devp->nxtdev) {
229*7c478bd9Sstevel@tonic-gate 			drvp = &dlist[devp->drvid];
230*7c478bd9Sstevel@tonic-gate 			rc = (*drvp->choosefunc)(devp->mapsize, devp->unitmap,
231*7c478bd9Sstevel@tonic-gate 			    drvp->mapsize, drvp->busymap);
232*7c478bd9Sstevel@tonic-gate 			if (rc != 0)
233*7c478bd9Sstevel@tonic-gate 				break;
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 		if (rc == 0)
236*7c478bd9Sstevel@tonic-gate 			break;
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	/*
240*7c478bd9Sstevel@tonic-gate 	 * remove from list...
241*7c478bd9Sstevel@tonic-gate 	 */
242*7c478bd9Sstevel@tonic-gate 	if (rdp) {
243*7c478bd9Sstevel@tonic-gate 		if (last) {
244*7c478bd9Sstevel@tonic-gate 			last->nxtrd = rdp->nxtrd;
245*7c478bd9Sstevel@tonic-gate 		} else {
246*7c478bd9Sstevel@tonic-gate 			*devlist = rdp->nxtrd;
247*7c478bd9Sstevel@tonic-gate 		}
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	return (rdp);
251*7c478bd9Sstevel@tonic-gate }
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate /*
254*7c478bd9Sstevel@tonic-gate  * add the given driver/unit reference to the `rawdev' structure identified
255*7c478bd9Sstevel@tonic-gate  * by `cookie'
256*7c478bd9Sstevel@tonic-gate  * If a new `driver' structure needs to be created, associate the given
257*7c478bd9Sstevel@tonic-gate  * choosing function and driver private data with it.
258*7c478bd9Sstevel@tonic-gate  */
259*7c478bd9Sstevel@tonic-gate void
260*7c478bd9Sstevel@tonic-gate preen_addunit(
261*7c478bd9Sstevel@tonic-gate 	void    *cookie,
262*7c478bd9Sstevel@tonic-gate 	char	*dname,		/* driver name */
263*7c478bd9Sstevel@tonic-gate 	int	(*cf)(),	/* candidate choosing function */
264*7c478bd9Sstevel@tonic-gate 	void	*datap,		/* driver private data */
265*7c478bd9Sstevel@tonic-gate 	uint_t	unit)		/* unit number */
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate 	register int drvid;
268*7c478bd9Sstevel@tonic-gate 	register struct driver *dp;
269*7c478bd9Sstevel@tonic-gate 	register struct onedev *devp;
270*7c478bd9Sstevel@tonic-gate 	struct rawdev *rdp = (struct rawdev *)cookie;
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	/*
273*7c478bd9Sstevel@tonic-gate 	 * locate the driver struct
274*7c478bd9Sstevel@tonic-gate 	 */
275*7c478bd9Sstevel@tonic-gate 	dp = NULL;
276*7c478bd9Sstevel@tonic-gate 	for (drvid = 0; drvid < ndrivers; drvid++) {
277*7c478bd9Sstevel@tonic-gate 		if (strcmp(dlist[drvid].name, dname) == 0) {
278*7c478bd9Sstevel@tonic-gate 			dp = &dlist[drvid];
279*7c478bd9Sstevel@tonic-gate 			break;
280*7c478bd9Sstevel@tonic-gate 		}
281*7c478bd9Sstevel@tonic-gate 	}
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
284*7c478bd9Sstevel@tonic-gate 		/*
285*7c478bd9Sstevel@tonic-gate 		 * driver struct doesn't exist yet -- create one
286*7c478bd9Sstevel@tonic-gate 		 */
287*7c478bd9Sstevel@tonic-gate 		if (cf == NULL)
288*7c478bd9Sstevel@tonic-gate 			cf = chooseone;
289*7c478bd9Sstevel@tonic-gate 		drvid = alloc_driver(dname, cf, datap);
290*7c478bd9Sstevel@tonic-gate 		dp = &dlist[drvid];
291*7c478bd9Sstevel@tonic-gate 	}
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	for (devp = rdp->alldevs; devp != NULL; devp = devp->nxtdev) {
294*7c478bd9Sstevel@tonic-gate 		/*
295*7c478bd9Sstevel@tonic-gate 		 * see if this device already references the given driver
296*7c478bd9Sstevel@tonic-gate 		 */
297*7c478bd9Sstevel@tonic-gate 		if (devp->drvid == drvid)
298*7c478bd9Sstevel@tonic-gate 			break;
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if (devp == NULL) {
302*7c478bd9Sstevel@tonic-gate 		/*
303*7c478bd9Sstevel@tonic-gate 		 * allocate a new `struct onedev' and chain it in
304*7c478bd9Sstevel@tonic-gate 		 * rdp->alldevs...
305*7c478bd9Sstevel@tonic-gate 		 */
306*7c478bd9Sstevel@tonic-gate 		devp = alloc_dev(drvid);
307*7c478bd9Sstevel@tonic-gate 		devp->nxtdev = rdp->alldevs;
308*7c478bd9Sstevel@tonic-gate 		rdp->alldevs = devp;
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	/*
312*7c478bd9Sstevel@tonic-gate 	 * add `unit' to the unitmap in devp
313*7c478bd9Sstevel@tonic-gate 	 */
314*7c478bd9Sstevel@tonic-gate 	addunit(devp, unit);
315*7c478bd9Sstevel@tonic-gate }
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate static
318*7c478bd9Sstevel@tonic-gate int
319*7c478bd9Sstevel@tonic-gate alloc_driver(char *name, int (*cf)(), void *datap)
320*7c478bd9Sstevel@tonic-gate {
321*7c478bd9Sstevel@tonic-gate 	register struct driver *dp;
322*7c478bd9Sstevel@tonic-gate 	extern char *strdup();
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	if (ndrivers == ndalloc) {
325*7c478bd9Sstevel@tonic-gate 		dlist = ndalloc ?
326*7c478bd9Sstevel@tonic-gate 		    (struct driver *)
327*7c478bd9Sstevel@tonic-gate 		    realloc(dlist, sizeof (struct driver) * DRIVER_ALLOC) :
328*7c478bd9Sstevel@tonic-gate 		    (struct driver *)
329*7c478bd9Sstevel@tonic-gate 		    malloc(sizeof (struct driver) * DRIVER_ALLOC);
330*7c478bd9Sstevel@tonic-gate 		if (dlist == NULL) {
331*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "out of memory in preenlib\n");
332*7c478bd9Sstevel@tonic-gate 			exit(1);
333*7c478bd9Sstevel@tonic-gate 		}
334*7c478bd9Sstevel@tonic-gate 		ndalloc += DRIVER_ALLOC;
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	dp = &dlist[ndrivers];
338*7c478bd9Sstevel@tonic-gate 	dp->name = strdup(name);
339*7c478bd9Sstevel@tonic-gate 	if (dp->name == NULL) {
340*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "out of memory in preenlib\n");
341*7c478bd9Sstevel@tonic-gate 		exit(1);
342*7c478bd9Sstevel@tonic-gate 	}
343*7c478bd9Sstevel@tonic-gate 	dp->choosefunc = cf;
344*7c478bd9Sstevel@tonic-gate 	dp->data = datap;
345*7c478bd9Sstevel@tonic-gate 	dp->mapsize = 0;
346*7c478bd9Sstevel@tonic-gate 	dp->busymap = NULL;
347*7c478bd9Sstevel@tonic-gate 	return (ndrivers++);
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate static
351*7c478bd9Sstevel@tonic-gate struct onedev *
352*7c478bd9Sstevel@tonic-gate alloc_dev(int did)
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	register struct onedev *devp;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	devp = (struct onedev *)malloc(sizeof (struct onedev));
357*7c478bd9Sstevel@tonic-gate 	if (devp == NULL) {
358*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "out of memory in preenlib\n");
359*7c478bd9Sstevel@tonic-gate 		exit(1);
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 	devp->drvid = did;
362*7c478bd9Sstevel@tonic-gate 	devp->mapsize = 0;
363*7c478bd9Sstevel@tonic-gate 	devp->unitmap = NULL;
364*7c478bd9Sstevel@tonic-gate 	devp->nxtdev = NULL;
365*7c478bd9Sstevel@tonic-gate 	return (devp);
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate static
369*7c478bd9Sstevel@tonic-gate void
370*7c478bd9Sstevel@tonic-gate addunit(struct onedev *devp, uint_t unit)
371*7c478bd9Sstevel@tonic-gate {
372*7c478bd9Sstevel@tonic-gate 	uint_t newsize;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	newsize = howmany(unit+1, WORDSIZE);
375*7c478bd9Sstevel@tonic-gate 	if (devp->mapsize < newsize) {
376*7c478bd9Sstevel@tonic-gate 		devp->unitmap = devp->mapsize ?
377*7c478bd9Sstevel@tonic-gate 		    (uint_t *)realloc(devp->unitmap,
378*7c478bd9Sstevel@tonic-gate 		    newsize * sizeof (uint_t)) :
379*7c478bd9Sstevel@tonic-gate 		    (uint_t *)malloc(newsize * sizeof (uint_t));
380*7c478bd9Sstevel@tonic-gate 		if (devp->unitmap == NULL) {
381*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "out of memory in preenlib\n");
382*7c478bd9Sstevel@tonic-gate 			exit(1);
383*7c478bd9Sstevel@tonic-gate 		}
384*7c478bd9Sstevel@tonic-gate 		(void) memset((char *)&devp->unitmap[devp->mapsize], 0,
385*7c478bd9Sstevel@tonic-gate 		    (uint_t)((newsize - devp->mapsize) * sizeof (uint_t)));
386*7c478bd9Sstevel@tonic-gate 		devp->mapsize = newsize;
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 	devp->unitmap[unit / WORDSIZE] |= (1 << (unit % WORDSIZE));
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate static
392*7c478bd9Sstevel@tonic-gate chooseone(int devmapsize, ulong_t *devmap, int drvmapsize, ulong_t *drvmap)
393*7c478bd9Sstevel@tonic-gate {
394*7c478bd9Sstevel@tonic-gate 	register int i;
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < min(devmapsize, drvmapsize); i++) {
397*7c478bd9Sstevel@tonic-gate 		if (devmap[i] & drvmap[i])
398*7c478bd9Sstevel@tonic-gate 			return (1);
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate 	return (0);
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate /*
404*7c478bd9Sstevel@tonic-gate  * mark the given driver/unit pair as busy.  This is called from
405*7c478bd9Sstevel@tonic-gate  * preen_getdev.
406*7c478bd9Sstevel@tonic-gate  */
407*7c478bd9Sstevel@tonic-gate static
408*7c478bd9Sstevel@tonic-gate void
409*7c478bd9Sstevel@tonic-gate makebusy(struct onedev *dev)
410*7c478bd9Sstevel@tonic-gate {
411*7c478bd9Sstevel@tonic-gate 	struct driver *drvp = &dlist[dev->drvid];
412*7c478bd9Sstevel@tonic-gate 	int newsize = dev->mapsize;
413*7c478bd9Sstevel@tonic-gate 	register int i;
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	if (drvp->mapsize < newsize) {
416*7c478bd9Sstevel@tonic-gate 		drvp->busymap = drvp->mapsize ?
417*7c478bd9Sstevel@tonic-gate 		    (uint_t *)realloc(drvp->busymap,
418*7c478bd9Sstevel@tonic-gate 		    newsize * sizeof (uint_t)) :
419*7c478bd9Sstevel@tonic-gate 		    (uint_t *)malloc(newsize * sizeof (uint_t));
420*7c478bd9Sstevel@tonic-gate 		if (drvp->busymap == NULL) {
421*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "out of memory in preenlib\n");
422*7c478bd9Sstevel@tonic-gate 			exit(1);
423*7c478bd9Sstevel@tonic-gate 		}
424*7c478bd9Sstevel@tonic-gate 		(void) memset((char *)&drvp->busymap[drvp->mapsize], 0,
425*7c478bd9Sstevel@tonic-gate 		    (uint_t)((newsize - drvp->mapsize) * sizeof (uint_t)));
426*7c478bd9Sstevel@tonic-gate 		drvp->mapsize = newsize;
427*7c478bd9Sstevel@tonic-gate 	}
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < newsize; i++)
430*7c478bd9Sstevel@tonic-gate 		drvp->busymap[i] |= dev->unitmap[i];
431*7c478bd9Sstevel@tonic-gate }
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate /*
434*7c478bd9Sstevel@tonic-gate  * make each device in the given `rawdev' un-busy.
435*7c478bd9Sstevel@tonic-gate  * Called from preen_releasedev
436*7c478bd9Sstevel@tonic-gate  */
437*7c478bd9Sstevel@tonic-gate static
438*7c478bd9Sstevel@tonic-gate void
439*7c478bd9Sstevel@tonic-gate notbusy(struct rawdev *rd)
440*7c478bd9Sstevel@tonic-gate {
441*7c478bd9Sstevel@tonic-gate 	struct onedev *devp;
442*7c478bd9Sstevel@tonic-gate 	struct driver *drvp;
443*7c478bd9Sstevel@tonic-gate 	register int i;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	for (devp = rd->alldevs; devp; devp = devp->nxtdev) {
446*7c478bd9Sstevel@tonic-gate 		drvp = &dlist[devp->drvid];
447*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < devp->mapsize; i++)
448*7c478bd9Sstevel@tonic-gate 			drvp->busymap[i] &= ~(devp->unitmap[i]);
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate }
451