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