xref: /dragonfly/sys/dev/raid/vinum/vinum.c (revision 685c703c)
1 /*-
2  * Copyright (c) 1997, 1998
3  *	Nan Yang Computer Services Limited.  All rights reserved.
4  *
5  *  Written by Greg Lehey
6  *
7  *  This software is distributed under the so-called ``Berkeley
8  *  License'':
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by Nan Yang Computer
21  *      Services Limited.
22  * 4. Neither the name of the Company nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * This software is provided ``as is'', and any express or implied
27  * warranties, including, but not limited to, the implied warranties of
28  * merchantability and fitness for a particular purpose are disclaimed.
29  * In no event shall the company or contributors be liable for any
30  * direct, indirect, incidental, special, exemplary, or consequential
31  * damages (including, but not limited to, procurement of substitute
32  * goods or services; loss of use, data, or profits; or business
33  * interruption) however caused and on any theory of liability, whether
34  * in contract, strict liability, or tort (including negligence or
35  * otherwise) arising in any way out of the use of this software, even if
36  * advised of the possibility of such damage.
37  *
38  * $Id: vinum.c,v 1.33 2001/01/09 06:19:15 grog Exp grog $
39  * $FreeBSD: src/sys/dev/vinum/vinum.c,v 1.38.2.3 2003/01/07 12:14:16 joerg Exp $
40  * $DragonFly: src/sys/dev/raid/vinum/vinum.c,v 1.14 2006/07/28 02:17:38 dillon Exp $
41  */
42 
43 #define STATIC static					    /* nothing while we're testing XXX */
44 
45 #include "vinumhdr.h"
46 #include <sys/sysproto.h>				    /* for sync(2) */
47 #include <sys/devicestat.h>
48 #ifdef VINUMDEBUG
49 #include <sys/reboot.h>
50 int debug = 0;
51 extern int total_malloced;
52 extern int malloccount;
53 extern struct mc malloced[];
54 #endif
55 #include "request.h"
56 
57 struct dev_ops vinum_ops =
58 {
59 	{ "vinum", VINUM_CDEV_MAJOR, D_DISK },
60 	.d_open =	vinumopen,
61 	.d_close =	vinumclose,
62 	.d_read =	physread,
63 	.d_write =	physwrite,
64 	.d_ioctl =	vinumioctl,
65 	.d_poll =	vinumpoll,
66 	.d_strategy =	vinumstrategy,
67 	.d_dump =	vinumdump,
68 };
69 
70 /* Called by main() during pseudo-device attachment. */
71 STATIC void vinumattach(void *);
72 
73 STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
74 
75 struct _vinum_conf vinum_conf;				    /* configuration information */
76 
77 /*
78  * Called by main() during pseudo-device attachment.  All we need
79  * to do is allocate enough space for devices to be configured later, and
80  * add devsw entries.
81  */
82 void
83 vinumattach(void *dummy)
84 {
85     char *cp, *cp1, *cp2, **drives;
86     int i, rv;
87     struct volume *vol;
88 
89     /* modload should prevent multiple loads, so this is worth a panic */
90     if ((vinum_conf.flags & VF_LOADED) != 0)
91 	panic("vinum: already loaded");
92 
93     log(LOG_INFO, "vinum: loaded\n");
94     vinum_conf.flags |= VF_LOADED;			    /* we're loaded now */
95 
96     daemonq = NULL;					    /* initialize daemon's work queue */
97     dqend = NULL;
98 
99     dev_ops_add(&vinum_ops, 0, 0);			    /* add the ops entry */
100 
101     vinum_conf.physbufs = nswbuf / 2 + 1;		    /* maximum amount of physical bufs */
102 
103     /* allocate space: drives... */
104     DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
105     CHECKALLOC(DRIVE, "vinum: no memory\n");
106     bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
107     vinum_conf.drives_allocated = INITIAL_DRIVES;	    /* number of drive slots allocated */
108     vinum_conf.drives_used = 0;				    /* and number in use */
109 
110     /* volumes, ... */
111     VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
112     CHECKALLOC(VOL, "vinum: no memory\n");
113     bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
114     vinum_conf.volumes_allocated = INITIAL_VOLUMES;	    /* number of volume slots allocated */
115     vinum_conf.volumes_used = 0;			    /* and number in use */
116 
117     /* plexes, ... */
118     PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
119     CHECKALLOC(PLEX, "vinum: no memory\n");
120     bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
121     vinum_conf.plexes_allocated = INITIAL_PLEXES;	    /* number of plex slots allocated */
122     vinum_conf.plexes_used = 0;				    /* and number in use */
123 
124     /* and subdisks */
125     SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
126     CHECKALLOC(SD, "vinum: no memory\n");
127     bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
128     vinum_conf.subdisks_allocated = INITIAL_SUBDISKS;	    /* number of sd slots allocated */
129     vinum_conf.subdisks_used = 0;			    /* and number in use */
130 
131     /*
132      * See if the loader has passed us a disk to
133      * read the initial configuration from.
134      */
135     if ((cp = getenv("vinum.drives")) != NULL) {
136 	for (cp1 = cp, i = 0, drives = 0; *cp1 != '\0'; i++) {
137 	    cp2 = cp1;
138 	    while (*cp1 != '\0' && *cp1 != ',' && *cp1 != ' ')
139 		cp1++;
140 	    if (*cp1 != '\0')
141 		*cp1++ = '\0';
142 	    drives = realloc(drives, (unsigned long)((i + 1) * sizeof(char *)),
143 			     M_TEMP, M_WAITOK);
144 	    drives[i] = cp2;
145 	}
146 	if (i == 0)
147 	    goto bailout;
148 	rv = vinum_scandisk(drives, i);
149 	if (rv)
150 	    log(LOG_NOTICE, "vinum_scandisk() returned %d", rv);
151     bailout:
152 	free(drives, M_TEMP);
153     }
154     if ((cp = getenv("vinum.root")) != NULL) {
155 	for (i = 0; i < vinum_conf.volumes_used; i++) {
156 	    vol = &vinum_conf.volume[i];
157 	    if ((vol->state == volume_up)
158 		&& (strcmp (vol->name, cp) == 0)
159 	    ) {
160 		rootdev = make_dev(&vinum_ops, i, UID_ROOT, GID_OPERATOR,
161 				0640, "vinum");
162 		log(LOG_INFO, "vinum: using volume %s for root device\n", cp);
163 		break;
164 	    }
165 	}
166     }
167 }
168 
169 /*
170  * Check if we have anything open.  If confopen is != 0,
171  * that goes for the super device as well, otherwise
172  * only for volumes.
173  *
174  * Return 0 if not inactive, 1 if inactive.
175  */
176 int
177 vinum_inactive(int confopen)
178 {
179     int i;
180     int can_do = 1;					    /* assume we can do it */
181 
182     if (confopen && (vinum_conf.flags & VF_OPEN))	    /* open by vinum(8)? */
183 	return 0;					    /* can't do it while we're open */
184     lock_config();
185     for (i = 0; i < vinum_conf.volumes_allocated; i++) {
186 	if ((VOL[i].state > volume_down)
187 	    && (VOL[i].flags & VF_OPEN)) {		    /* volume is open */
188 	    can_do = 0;
189 	    break;
190 	}
191     }
192     unlock_config();
193     return can_do;
194 }
195 
196 /*
197  * Free all structures.
198  * If cleardrive is 0, save the configuration; otherwise
199  * remove the configuration from the drive.
200  *
201  * Before coming here, ensure that no volumes are open.
202  */
203 void
204 free_vinum(int cleardrive)
205 {
206     int i;
207     int drives_allocated = vinum_conf.drives_allocated;
208 
209     if (DRIVE != NULL) {
210 	if (cleardrive) {				    /* remove the vinum config */
211 	    for (i = 0; i < drives_allocated; i++)
212 		remove_drive(i);			    /* remove the drive */
213 	} else {					    /* keep the config */
214 	    for (i = 0; i < drives_allocated; i++)
215 		free_drive(&DRIVE[i]);			    /* close files and things */
216 	}
217 	Free(DRIVE);
218     }
219     while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
220 	== (VF_STOPPING | VF_DAEMONOPEN)) {		    /* at least one daemon open, we're stopping */
221 	queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
222 	tsleep(&vinumclose, 0, "vstop", 1);		    /* and wait for it */
223     }
224     if (SD != NULL)
225 	Free(SD);
226     if (PLEX != NULL) {
227 	for (i = 0; i < vinum_conf.plexes_allocated; i++) {
228 	    struct plex *plex = &vinum_conf.plex[i];
229 
230 	    if (plex->state != plex_unallocated) {	    /* we have real data there */
231 		if (plex->sdnos)
232 		    Free(plex->sdnos);
233 	    }
234 	}
235 	Free(PLEX);
236     }
237     if (VOL != NULL)
238 	Free(VOL);
239     bzero(&vinum_conf, sizeof(vinum_conf));
240 }
241 
242 STATIC int
243 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
244 {
245     switch (type) {
246     case MOD_LOAD:
247 	vinumattach(NULL);
248 	return 0;					    /* OK */
249     case MOD_UNLOAD:
250 	if (!vinum_inactive(1))				    /* is anything open? */
251 	    return EBUSY;				    /* yes, we can't do it */
252 	vinum_conf.flags |= VF_STOPPING;		    /* note that we want to stop */
253 	sys_sync(NULL);			    /* write out buffers */
254 	free_vinum(0);					    /* clean up */
255 #ifdef VINUMDEBUG
256 	if (total_malloced) {
257 	    int i;
258 #ifdef INVARIANTS
259 	    int *poke;
260 #endif
261 
262 	    for (i = 0; i < malloccount; i++) {
263 		if (debug & DEBUG_WARNINGS)		    /* want to hear about them */
264 		    log(LOG_WARNING,
265 			"vinum: exiting with %d bytes malloced from %s:%d\n",
266 			malloced[i].size,
267 			malloced[i].file,
268 			malloced[i].line);
269 #ifdef INVARIANTS
270 		poke = &((int *) malloced[i].address)
271 		    [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
272 		if (*poke == 0xdeadc0de)		    /* already freed */
273 		    log(LOG_ERR,
274 			"vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
275 			malloced[i].address,
276 			malloced[i].file,
277 			malloced[i].line);
278 #endif
279 		Free(malloced[i].address);
280 	    }
281 	}
282 #endif
283 	dev_ops_remove(&vinum_ops, 0, 0);
284 	log(LOG_INFO, "vinum: unloaded\n");		    /* tell the world */
285 	return 0;
286     default:
287 	break;
288     }
289     return 0;
290 }
291 
292 moduledata_t vinum_mod =
293 {
294     "vinum",
295     (modeventhand_t) vinum_modevent,
296     0
297 };
298 DECLARE_MODULE(vinum, vinum_mod, SI_SUB_RAID, SI_ORDER_MIDDLE);
299 
300 /* ARGSUSED */
301 /* Open a vinum object */
302 int
303 vinumopen(struct dev_open_args *ap)
304 {
305     dev_t dev = ap->a_head.a_dev;
306     int error;
307     unsigned int index;
308     struct volume *vol;
309     struct plex *plex;
310     struct sd *sd;
311     int devminor;					    /* minor number */
312 
313     devminor = minor(dev);
314     error = 0;
315     /* First, decide what we're looking at */
316     switch (DEVTYPE(dev)) {
317     case VINUM_VOLUME_TYPE:
318 	index = Volno(dev);
319 	if (index >= vinum_conf.volumes_allocated)
320 	    return ENXIO;				    /* no such device */
321 	vol = &VOL[index];
322 
323 	switch (vol->state) {
324 	case volume_unallocated:
325 	case volume_uninit:
326 	    return ENXIO;
327 
328 	case volume_up:
329 	    vol->flags |= VF_OPEN;			    /* note we're open */
330 	    return 0;
331 
332 	case volume_down:
333 	    return EIO;
334 
335 	default:
336 	    return EINVAL;
337 	}
338 
339     case VINUM_PLEX_TYPE:
340 	if (Volno(dev) >= vinum_conf.volumes_allocated)
341 	    return ENXIO;
342 	/* FALLTHROUGH */
343 
344     case VINUM_RAWPLEX_TYPE:
345 	index = Plexno(dev);				    /* get plex index in vinum_conf */
346 	if (index >= vinum_conf.plexes_allocated)
347 	    return ENXIO;				    /* no such device */
348 	plex = &PLEX[index];
349 
350 	switch (plex->state) {
351 	case plex_referenced:
352 	case plex_unallocated:
353 	    return EINVAL;
354 
355 	default:
356 	    plex->flags |= VF_OPEN;			    /* note we're open */
357 	    return 0;
358 	}
359 
360     case VINUM_SD_TYPE:
361 	if ((Volno(dev) >= vinum_conf.volumes_allocated)    /* no such volume */
362 	||(Plexno(dev) >= vinum_conf.plexes_allocated))	    /* or no such plex */
363 	    return ENXIO;				    /* no such device */
364 
365 	/* FALLTHROUGH */
366 
367     case VINUM_RAWSD_TYPE:
368 	index = Sdno(dev);				    /* get the subdisk number */
369 	if ((index >= vinum_conf.subdisks_allocated)	    /* not a valid SD entry */
370 	||(SD[index].state < sd_init))			    /* or SD is not real */
371 	    return ENXIO;				    /* no such device */
372 	sd = &SD[index];
373 
374 	/*
375 	 * Opening a subdisk is always a special operation, so we
376 	 * ignore the state as long as it represents a real subdisk
377 	 */
378 	switch (sd->state) {
379 	case sd_unallocated:
380 	case sd_uninit:
381 	    return EINVAL;
382 
383 	default:
384 	    sd->flags |= VF_OPEN;			    /* note we're open */
385 	    return 0;
386 	}
387 
388     case VINUM_SUPERDEV_TYPE:
389 	error = suser_cred(ap->a_cred, 0);		    /* are we root? */
390 	if (error == 0) {				    /* yes, can do */
391 	    if (devminor == VINUM_DAEMON_DEV)		    /* daemon device */
392 		vinum_conf.flags |= VF_DAEMONOPEN;	    /* we're open */
393 	    else if (devminor == VINUM_SUPERDEV)
394 		vinum_conf.flags |= VF_OPEN;		    /* we're open */
395 	    else
396 		error = ENODEV;				    /* nothing, maybe a debug mismatch */
397 	}
398 	return error;
399 
400 	/* Vinum drives are disks.  We already have a disk
401 	 * driver, so don't handle them here */
402     case VINUM_DRIVE_TYPE:
403     default:
404 	return ENODEV;					    /* don't know what to do with these */
405     }
406 }
407 
408 /* ARGSUSED */
409 int
410 vinumclose(struct dev_close_args *ap)
411 {
412     dev_t dev = ap->a_head.a_dev;
413     unsigned int index;
414     struct volume *vol;
415     int devminor;
416 
417     devminor = minor(dev);
418     index = Volno(dev);
419     /* First, decide what we're looking at */
420     switch (DEVTYPE(dev)) {
421     case VINUM_VOLUME_TYPE:
422 	if (index >= vinum_conf.volumes_allocated)
423 	    return ENXIO;				    /* no such device */
424 	vol = &VOL[index];
425 
426 	switch (vol->state) {
427 	case volume_unallocated:
428 	case volume_uninit:
429 	    return ENXIO;
430 
431 	case volume_up:
432 	    vol->flags &= ~VF_OPEN;			    /* reset our flags */
433 	    return 0;
434 
435 	case volume_down:
436 	    return EIO;
437 
438 	default:
439 	    return EINVAL;
440 	}
441 
442     case VINUM_PLEX_TYPE:
443 	if (Volno(dev) >= vinum_conf.volumes_allocated)
444 	    return ENXIO;
445 	/* FALLTHROUGH */
446 
447     case VINUM_RAWPLEX_TYPE:
448 	index = Plexno(dev);				    /* get plex index in vinum_conf */
449 	if (index >= vinum_conf.plexes_allocated)
450 	    return ENXIO;				    /* no such device */
451 	PLEX[index].flags &= ~VF_OPEN;			    /* reset our flags */
452 	return 0;
453 
454     case VINUM_SD_TYPE:
455 	if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
456 	    (Plexno(dev) >= vinum_conf.plexes_allocated))   /* or no such plex */
457 	    return ENXIO;				    /* no such device */
458 	/* FALLTHROUGH */
459 
460     case VINUM_RAWSD_TYPE:
461 	index = Sdno(dev);				    /* get the subdisk number */
462 	if (index >= vinum_conf.subdisks_allocated)
463 	    return ENXIO;				    /* no such device */
464 	SD[index].flags &= ~VF_OPEN;			    /* reset our flags */
465 	return 0;
466 
467     case VINUM_SUPERDEV_TYPE:
468 	/*
469 	 * don't worry about whether we're root:
470 	 * nobody else would get this far.
471 	 */
472 	if (devminor == VINUM_SUPERDEV)			    /* normal superdev */
473 	    vinum_conf.flags &= ~VF_OPEN;		    /* no longer open */
474 	else if (devminor == VINUM_DAEMON_DEV) {	    /* the daemon device */
475 	    vinum_conf.flags &= ~VF_DAEMONOPEN;		    /* no longer open */
476 	    if (vinum_conf.flags & VF_STOPPING)		    /* we're stopping, */
477 		wakeup(&vinumclose);			    /* we can continue stopping now */
478 	}
479 	return 0;
480 
481     case VINUM_DRIVE_TYPE:
482     default:
483 	return ENODEV;					    /* don't know what to do with these */
484     }
485 }
486 
487 /* size routine */
488 int
489 vinumsize(struct dev_psize_args *ap)
490 {
491     dev_t dev = ap->a_head.a_dev;
492     struct volume *vol;
493 
494     vol = &VOL[Volno(dev)];
495 
496     if (vol->state == volume_up) {
497 	ap->a_result = vol->size;
498 	return(0);
499     } else {
500 	return(ENXIO);
501     }
502 }
503 
504 int
505 vinumdump(struct dev_dump_args *ap)
506 {
507     /* Not implemented. */
508     return ENXIO;
509 }
510 
511 int
512 vinumpoll(struct dev_poll_args *ap)
513 {
514     ap->a_events = seltrue(ap->a_head.a_dev, ap->a_events);
515     return(0);
516 }
517 
518 /* Local Variables: */
519 /* fill-column: 50 */
520 /* End: */
521