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