Fix a minor bug that prevented compilation.
[dragonfly.git] / sys / dev / raid / vinum / vinum.c
CommitLineData
984263bc
MD
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 $
e1c944c6 40 * $DragonFly: src/sys/dev/raid/vinum/vinum.c,v 1.13 2006/07/16 22:39:42 dillon Exp $
984263bc
MD
41 */
42
43#define STATIC static /* nothing while we're testing XXX */
44
1f2de5d4 45#include "vinumhdr.h"
984263bc
MD
46#include <sys/sysproto.h> /* for sync(2) */
47#include <sys/devicestat.h>
48#ifdef VINUMDEBUG
49#include <sys/reboot.h>
50int debug = 0;
51extern int total_malloced;
52extern int malloccount;
53extern struct mc malloced[];
54#endif
1f2de5d4 55#include "request.h"
984263bc 56
e4c9c0c8 57struct cdevsw vinum_cdevsw =
984263bc 58{
fabb8ceb
MD
59 /* name */ "vinum",
60 /* cmaj */ VINUM_CDEV_MAJOR,
61 /* flags */ D_DISK,
62 /* port */ NULL,
455fcd7e 63 /* clone */ NULL,
fabb8ceb 64
984263bc
MD
65 vinumopen, vinumclose, physread, physwrite,
66 vinumioctl, seltrue, nommap, vinumstrategy,
fabb8ceb 67 vinumdump, vinumsize,
984263bc
MD
68};
69
70/* Called by main() during pseudo-device attachment. */
71STATIC void vinumattach(void *);
72
73STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
74
75struct _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 */
82void
83vinumattach(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
e4c9c0c8 99 cdevsw_add(&vinum_cdevsw, 0, 0); /* add the cdevsw entry */
984263bc 100
e1c944c6
MD
101 vinum_conf.physbufs = nswbuf / 2 + 1; /* maximum amount of physical bufs */
102
984263bc
MD
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)
e4c9c0c8
MD
158 && (strcmp (vol->name, cp) == 0)
159 ) {
160 rootdev = make_dev(&vinum_cdevsw, i, UID_ROOT, GID_OPERATOR,
161 0640, "vinum");
984263bc
MD
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 */
176int
177vinum_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 */
203void
204free_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 */
377d4740 222 tsleep(&vinumclose, 0, "vstop", 1); /* and wait for it */
984263bc
MD
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
242STATIC int
243vinum_modevent(module_t mod, modeventtype_t type, void *unused)
244{
984263bc
MD
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 */
753fd850 253 sys_sync(NULL); /* write out buffers */
984263bc
MD
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
e4c9c0c8 283 cdevsw_remove(&vinum_cdevsw, 0, 0);
984263bc
MD
284 log(LOG_INFO, "vinum: unloaded\n"); /* tell the world */
285 return 0;
286 default:
287 break;
288 }
289 return 0;
290}
291
292moduledata_t vinum_mod =
293{
294 "vinum",
295 (modeventhand_t) vinum_modevent,
296 0
297};
5dbaf12c 298DECLARE_MODULE(vinum, vinum_mod, SI_SUB_RAID, SI_ORDER_MIDDLE);
984263bc
MD
299
300/* ARGSUSED */
301/* Open a vinum object */
302int
41c20dac 303vinumopen(dev_t dev, int flags, int fmt, d_thread_t *td)
984263bc
MD
304{
305 int error;
306 unsigned int index;
307 struct volume *vol;
308 struct plex *plex;
309 struct sd *sd;
310 int devminor; /* minor number */
311
312 devminor = minor(dev);
313 error = 0;
314 /* First, decide what we're looking at */
315 switch (DEVTYPE(dev)) {
316 case VINUM_VOLUME_TYPE:
317 index = Volno(dev);
318 if (index >= vinum_conf.volumes_allocated)
319 return ENXIO; /* no such device */
320 vol = &VOL[index];
321
322 switch (vol->state) {
323 case volume_unallocated:
324 case volume_uninit:
325 return ENXIO;
326
327 case volume_up:
328 vol->flags |= VF_OPEN; /* note we're open */
329 return 0;
330
331 case volume_down:
332 return EIO;
333
334 default:
335 return EINVAL;
336 }
337
338 case VINUM_PLEX_TYPE:
339 if (Volno(dev) >= vinum_conf.volumes_allocated)
340 return ENXIO;
341 /* FALLTHROUGH */
342
343 case VINUM_RAWPLEX_TYPE:
344 index = Plexno(dev); /* get plex index in vinum_conf */
345 if (index >= vinum_conf.plexes_allocated)
346 return ENXIO; /* no such device */
347 plex = &PLEX[index];
348
349 switch (plex->state) {
350 case plex_referenced:
351 case plex_unallocated:
352 return EINVAL;
353
354 default:
355 plex->flags |= VF_OPEN; /* note we're open */
356 return 0;
357 }
358
359 case VINUM_SD_TYPE:
360 if ((Volno(dev) >= vinum_conf.volumes_allocated) /* no such volume */
361 ||(Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
362 return ENXIO; /* no such device */
363
364 /* FALLTHROUGH */
365
366 case VINUM_RAWSD_TYPE:
367 index = Sdno(dev); /* get the subdisk number */
368 if ((index >= vinum_conf.subdisks_allocated) /* not a valid SD entry */
369 ||(SD[index].state < sd_init)) /* or SD is not real */
370 return ENXIO; /* no such device */
371 sd = &SD[index];
372
373 /*
374 * Opening a subdisk is always a special operation, so we
375 * ignore the state as long as it represents a real subdisk
376 */
377 switch (sd->state) {
378 case sd_unallocated:
379 case sd_uninit:
380 return EINVAL;
381
382 default:
383 sd->flags |= VF_OPEN; /* note we're open */
384 return 0;
385 }
386
387 case VINUM_SUPERDEV_TYPE:
dadab5e9 388 error = suser(td); /* are we root? */
984263bc
MD
389 if (error == 0) { /* yes, can do */
390 if (devminor == VINUM_DAEMON_DEV) /* daemon device */
391 vinum_conf.flags |= VF_DAEMONOPEN; /* we're open */
392 else if (devminor == VINUM_SUPERDEV)
393 vinum_conf.flags |= VF_OPEN; /* we're open */
394 else
395 error = ENODEV; /* nothing, maybe a debug mismatch */
396 }
397 return error;
398
399 /* Vinum drives are disks. We already have a disk
400 * driver, so don't handle them here */
401 case VINUM_DRIVE_TYPE:
402 default:
403 return ENODEV; /* don't know what to do with these */
404 }
405}
406
407/* ARGSUSED */
408int
41c20dac 409vinumclose(dev_t dev, int flags, int fmt, d_thread_t *td)
984263bc
MD
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 */
486int
487vinumsize(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
502int
e4c9c0c8 503vinumdump(dev_t dev, u_int count, u_int blkno, u_int secsize)
984263bc
MD
504{
505 /* Not implemented. */
506 return ENXIO;
507}
508
509/* Local Variables: */
510/* fill-column: 50 */
511/* End: */