Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / raid / vinum / vinum.c
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.2 2003/06/17 04:28:33 dillon Exp $
41  */
42
43 #define STATIC static                                       /* nothing while we're testing XXX */
44
45 #include <dev/vinum/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 <dev/vinum/request.h>
56
57 STATIC struct cdevsw vinum_cdevsw =
58 {
59     vinumopen, vinumclose, physread, physwrite,
60     vinumioctl, seltrue, nommap, vinumstrategy,
61     "vinum", VINUM_CDEV_MAJOR, vinumdump, vinumsize,
62     D_DISK, VINUM_BDEV_MAJOR
63 };
64
65 /* Called by main() during pseudo-device attachment. */
66 STATIC void vinumattach(void *);
67
68 STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
69
70 struct _vinum_conf vinum_conf;                              /* configuration information */
71
72 /*
73  * Called by main() during pseudo-device attachment.  All we need
74  * to do is allocate enough space for devices to be configured later, and
75  * add devsw entries.
76  */
77 void
78 vinumattach(void *dummy)
79 {
80     char *cp, *cp1, *cp2, **drives;
81     int i, rv;
82     struct volume *vol;
83
84     /* modload should prevent multiple loads, so this is worth a panic */
85     if ((vinum_conf.flags & VF_LOADED) != 0)
86         panic("vinum: already loaded");
87
88     log(LOG_INFO, "vinum: loaded\n");
89     vinum_conf.flags |= VF_LOADED;                          /* we're loaded now */
90
91     daemonq = NULL;                                         /* initialize daemon's work queue */
92     dqend = NULL;
93
94     cdevsw_add(&vinum_cdevsw);                              /* add the cdevsw entry */
95
96     /* allocate space: drives... */
97     DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
98     CHECKALLOC(DRIVE, "vinum: no memory\n");
99     bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
100     vinum_conf.drives_allocated = INITIAL_DRIVES;           /* number of drive slots allocated */
101     vinum_conf.drives_used = 0;                             /* and number in use */
102
103     /* volumes, ... */
104     VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
105     CHECKALLOC(VOL, "vinum: no memory\n");
106     bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
107     vinum_conf.volumes_allocated = INITIAL_VOLUMES;         /* number of volume slots allocated */
108     vinum_conf.volumes_used = 0;                            /* and number in use */
109
110     /* plexes, ... */
111     PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
112     CHECKALLOC(PLEX, "vinum: no memory\n");
113     bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
114     vinum_conf.plexes_allocated = INITIAL_PLEXES;           /* number of plex slots allocated */
115     vinum_conf.plexes_used = 0;                             /* and number in use */
116
117     /* and subdisks */
118     SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
119     CHECKALLOC(SD, "vinum: no memory\n");
120     bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
121     vinum_conf.subdisks_allocated = INITIAL_SUBDISKS;       /* number of sd slots allocated */
122     vinum_conf.subdisks_used = 0;                           /* and number in use */
123
124     /*
125      * See if the loader has passed us a disk to
126      * read the initial configuration from.
127      */
128     if ((cp = getenv("vinum.drives")) != NULL) {
129         for (cp1 = cp, i = 0, drives = 0; *cp1 != '\0'; i++) {
130             cp2 = cp1;
131             while (*cp1 != '\0' && *cp1 != ',' && *cp1 != ' ')
132                 cp1++;
133             if (*cp1 != '\0')
134                 *cp1++ = '\0';
135             drives = realloc(drives, (unsigned long)((i + 1) * sizeof(char *)),
136                              M_TEMP, M_WAITOK);
137             drives[i] = cp2;
138         }
139         if (i == 0)
140             goto bailout;
141         rv = vinum_scandisk(drives, i);
142         if (rv)
143             log(LOG_NOTICE, "vinum_scandisk() returned %d", rv);
144     bailout:
145         free(drives, M_TEMP);
146     }
147     if ((cp = getenv("vinum.root")) != NULL) {
148         for (i = 0; i < vinum_conf.volumes_used; i++) {
149             vol = &vinum_conf.volume[i];
150             if ((vol->state == volume_up)
151                 && (strcmp (vol->name, cp) == 0) ) {
152                 rootdev = makedev(VINUM_CDEV_MAJOR, i); 
153                 log(LOG_INFO, "vinum: using volume %s for root device\n", cp);
154                 break;
155             }
156         }
157     }
158 }
159
160 /*
161  * Check if we have anything open.  If confopen is != 0,
162  * that goes for the super device as well, otherwise
163  * only for volumes.
164  *
165  * Return 0 if not inactive, 1 if inactive.
166  */
167 int
168 vinum_inactive(int confopen)
169 {
170     int i;
171     int can_do = 1;                                         /* assume we can do it */
172
173     if (confopen && (vinum_conf.flags & VF_OPEN))           /* open by vinum(8)? */
174         return 0;                                           /* can't do it while we're open */
175     lock_config();
176     for (i = 0; i < vinum_conf.volumes_allocated; i++) {
177         if ((VOL[i].state > volume_down)
178             && (VOL[i].flags & VF_OPEN)) {                  /* volume is open */
179             can_do = 0;
180             break;
181         }
182     }
183     unlock_config();
184     return can_do;
185 }
186
187 /*
188  * Free all structures.
189  * If cleardrive is 0, save the configuration; otherwise
190  * remove the configuration from the drive.
191  *
192  * Before coming here, ensure that no volumes are open.
193  */
194 void
195 free_vinum(int cleardrive)
196 {
197     int i;
198     int drives_allocated = vinum_conf.drives_allocated;
199
200     if (DRIVE != NULL) {
201         if (cleardrive) {                                   /* remove the vinum config */
202             for (i = 0; i < drives_allocated; i++)
203                 remove_drive(i);                            /* remove the drive */
204         } else {                                            /* keep the config */
205             for (i = 0; i < drives_allocated; i++)
206                 free_drive(&DRIVE[i]);                      /* close files and things */
207         }
208         Free(DRIVE);
209     }
210     while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
211         == (VF_STOPPING | VF_DAEMONOPEN)) {                 /* at least one daemon open, we're stopping */
212         queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
213         tsleep(&vinumclose, PUSER, "vstop", 1);             /* and wait for it */
214     }
215     if (SD != NULL)
216         Free(SD);
217     if (PLEX != NULL) {
218         for (i = 0; i < vinum_conf.plexes_allocated; i++) {
219             struct plex *plex = &vinum_conf.plex[i];
220
221             if (plex->state != plex_unallocated) {          /* we have real data there */
222                 if (plex->sdnos)
223                     Free(plex->sdnos);
224             }
225         }
226         Free(PLEX);
227     }
228     if (VOL != NULL)
229         Free(VOL);
230     bzero(&vinum_conf, sizeof(vinum_conf));
231 }
232
233 STATIC int
234 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
235 {
236     struct sync_args dummyarg =
237     {0};
238
239     switch (type) {
240     case MOD_LOAD:
241         vinumattach(NULL);
242         return 0;                                           /* OK */
243     case MOD_UNLOAD:
244         if (!vinum_inactive(1))                             /* is anything open? */
245             return EBUSY;                                   /* yes, we can't do it */
246         vinum_conf.flags |= VF_STOPPING;                    /* note that we want to stop */
247         sync(curproc, &dummyarg);                           /* write out buffers */
248         free_vinum(0);                                      /* clean up */
249 #ifdef VINUMDEBUG
250         if (total_malloced) {
251             int i;
252 #ifdef INVARIANTS
253             int *poke;
254 #endif
255
256             for (i = 0; i < malloccount; i++) {
257                 if (debug & DEBUG_WARNINGS)                 /* want to hear about them */
258                     log(LOG_WARNING,
259                         "vinum: exiting with %d bytes malloced from %s:%d\n",
260                         malloced[i].size,
261                         malloced[i].file,
262                         malloced[i].line);
263 #ifdef INVARIANTS
264                 poke = &((int *) malloced[i].address)
265                     [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
266                 if (*poke == 0xdeadc0de)                    /* already freed */
267                     log(LOG_ERR,
268                         "vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
269                         malloced[i].address,
270                         malloced[i].file,
271                         malloced[i].line);
272 #endif
273                 Free(malloced[i].address);
274             }
275         }
276 #endif
277         cdevsw_remove(&vinum_cdevsw);
278         log(LOG_INFO, "vinum: unloaded\n");                 /* tell the world */
279         return 0;
280     default:
281         break;
282     }
283     return 0;
284 }
285
286 moduledata_t vinum_mod =
287 {
288     "vinum",
289     (modeventhand_t) vinum_modevent,
290     0
291 };
292 DECLARE_MODULE(vinum, vinum_mod, SI_SUB_VINUM, SI_ORDER_MIDDLE);
293
294 /* ARGSUSED */
295 /* Open a vinum object */
296 int
297 vinumopen(dev_t dev,
298     int flags,
299     int fmt,
300     struct proc *p)
301 {
302     int error;
303     unsigned int index;
304     struct volume *vol;
305     struct plex *plex;
306     struct sd *sd;
307     int devminor;                                           /* minor number */
308
309     devminor = minor(dev);
310     error = 0;
311     /* First, decide what we're looking at */
312     switch (DEVTYPE(dev)) {
313     case VINUM_VOLUME_TYPE:
314         index = Volno(dev);
315         if (index >= vinum_conf.volumes_allocated)
316             return ENXIO;                                   /* no such device */
317         vol = &VOL[index];
318
319         switch (vol->state) {
320         case volume_unallocated:
321         case volume_uninit:
322             return ENXIO;
323
324         case volume_up:
325             vol->flags |= VF_OPEN;                          /* note we're open */
326             return 0;
327
328         case volume_down:
329             return EIO;
330
331         default:
332             return EINVAL;
333         }
334
335     case VINUM_PLEX_TYPE:
336         if (Volno(dev) >= vinum_conf.volumes_allocated)
337             return ENXIO;
338         /* FALLTHROUGH */
339
340     case VINUM_RAWPLEX_TYPE:
341         index = Plexno(dev);                                /* get plex index in vinum_conf */
342         if (index >= vinum_conf.plexes_allocated)
343             return ENXIO;                                   /* no such device */
344         plex = &PLEX[index];
345
346         switch (plex->state) {
347         case plex_referenced:
348         case plex_unallocated:
349             return EINVAL;
350
351         default:
352             plex->flags |= VF_OPEN;                         /* note we're open */
353             return 0;
354         }
355
356     case VINUM_SD_TYPE:
357         if ((Volno(dev) >= vinum_conf.volumes_allocated)    /* no such volume */
358         ||(Plexno(dev) >= vinum_conf.plexes_allocated))     /* or no such plex */
359             return ENXIO;                                   /* no such device */
360
361         /* FALLTHROUGH */
362
363     case VINUM_RAWSD_TYPE:
364         index = Sdno(dev);                                  /* get the subdisk number */
365         if ((index >= vinum_conf.subdisks_allocated)        /* not a valid SD entry */
366         ||(SD[index].state < sd_init))                      /* or SD is not real */
367             return ENXIO;                                   /* no such device */
368         sd = &SD[index];
369
370         /*
371          * Opening a subdisk is always a special operation, so we
372          * ignore the state as long as it represents a real subdisk
373          */
374         switch (sd->state) {
375         case sd_unallocated:
376         case sd_uninit:
377             return EINVAL;
378
379         default:
380             sd->flags |= VF_OPEN;                           /* note we're open */
381             return 0;
382         }
383
384     case VINUM_SUPERDEV_TYPE:
385         error = suser(p);                                   /* are we root? */
386         if (error == 0) {                                   /* yes, can do */
387             if (devminor == VINUM_DAEMON_DEV)               /* daemon device */
388                 vinum_conf.flags |= VF_DAEMONOPEN;          /* we're open */
389             else if (devminor == VINUM_SUPERDEV)
390                 vinum_conf.flags |= VF_OPEN;                /* we're open */
391             else
392                 error = ENODEV;                             /* nothing, maybe a debug mismatch */
393         }
394         return error;
395
396         /* Vinum drives are disks.  We already have a disk
397          * driver, so don't handle them here */
398     case VINUM_DRIVE_TYPE:
399     default:
400         return ENODEV;                                      /* don't know what to do with these */
401     }
402 }
403
404 /* ARGSUSED */
405 int
406 vinumclose(dev_t dev,
407     int flags,
408     int fmt,
409     struct proc *p)
410 {
411     unsigned int index;
412     struct volume *vol;
413     int devminor;
414
415     devminor = minor(dev);
416     index = Volno(dev);
417     /* First, decide what we're looking at */
418     switch (DEVTYPE(dev)) {
419     case VINUM_VOLUME_TYPE:
420         if (index >= vinum_conf.volumes_allocated)
421             return ENXIO;                                   /* no such device */
422         vol = &VOL[index];
423
424         switch (vol->state) {
425         case volume_unallocated:
426         case volume_uninit:
427             return ENXIO;
428
429         case volume_up:
430             vol->flags &= ~VF_OPEN;                         /* reset our flags */
431             return 0;
432
433         case volume_down:
434             return EIO;
435
436         default:
437             return EINVAL;
438         }
439
440     case VINUM_PLEX_TYPE:
441         if (Volno(dev) >= vinum_conf.volumes_allocated)
442             return ENXIO;
443         /* FALLTHROUGH */
444
445     case VINUM_RAWPLEX_TYPE:
446         index = Plexno(dev);                                /* get plex index in vinum_conf */
447         if (index >= vinum_conf.plexes_allocated)
448             return ENXIO;                                   /* no such device */
449         PLEX[index].flags &= ~VF_OPEN;                      /* reset our flags */
450         return 0;
451
452     case VINUM_SD_TYPE:
453         if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
454             (Plexno(dev) >= vinum_conf.plexes_allocated))   /* or no such plex */
455             return ENXIO;                                   /* no such device */
456         /* FALLTHROUGH */
457
458     case VINUM_RAWSD_TYPE:
459         index = Sdno(dev);                                  /* get the subdisk number */
460         if (index >= vinum_conf.subdisks_allocated)
461             return ENXIO;                                   /* no such device */
462         SD[index].flags &= ~VF_OPEN;                        /* reset our flags */
463         return 0;
464
465     case VINUM_SUPERDEV_TYPE:
466         /*
467          * don't worry about whether we're root:
468          * nobody else would get this far.
469          */
470         if (devminor == VINUM_SUPERDEV)                     /* normal superdev */
471             vinum_conf.flags &= ~VF_OPEN;                   /* no longer open */
472         else if (devminor == VINUM_DAEMON_DEV) {            /* the daemon device */
473             vinum_conf.flags &= ~VF_DAEMONOPEN;             /* no longer open */
474             if (vinum_conf.flags & VF_STOPPING)             /* we're stopping, */
475                 wakeup(&vinumclose);                        /* we can continue stopping now */
476         }
477         return 0;
478
479     case VINUM_DRIVE_TYPE:
480     default:
481         return ENODEV;                                      /* don't know what to do with these */
482     }
483 }
484
485 /* size routine */
486 int
487 vinumsize(dev_t dev)
488 {
489     struct volume *vol;
490     int size;
491
492     vol = &VOL[Volno(dev)];
493
494     if (vol->state == volume_up)
495         size = vol->size;
496     else
497         return 0;                                           /* err on the size of conservatism */
498
499     return size;
500 }
501
502 int
503 vinumdump(dev_t dev)
504 {
505     /* Not implemented. */
506     return ENXIO;
507 }
508
509 /* Local Variables: */
510 /* fill-column: 50 */
511 /* End: */