FreeBSD/Linux Kernel Cross Reference
sys/dev/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.44 2003/05/23 00:50:55 grog Exp grog $
39 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #define STATIC static /* nothing while we're testing */
45
46 #include <dev/vinum/vinumhdr.h>
47 #include <sys/sysproto.h> /* for sync(2) */
48 #ifdef VINUMDEBUG
49 #include <sys/reboot.h>
50 int debug = 0; /* debug flags */
51 extern int total_malloced;
52 extern int malloccount;
53 extern struct mc malloced[];
54 #endif
55 #include <dev/vinum/request.h>
56
57 struct cdevsw vinum_cdevsw = {
58 .d_version = D_VERSION,
59 .d_open = vinumopen,
60 .d_close = vinumclose,
61 .d_read = physread,
62 .d_write = physwrite,
63 .d_ioctl = vinumioctl,
64 .d_strategy = vinumstrategy,
65 .d_name = "vinum",
66 .d_flags = D_DISK | D_NEEDGIANT
67 };
68
69 /* Called by main() during pseudo-device attachment. */
70 void vinumattach(void *);
71 STATIC int vinum_modevent(module_t mod, modeventtype_t type, void *unused);
72 STATIC void vinum_clone(void *arg, char *name, int namelen, struct cdev ** dev);
73
74 struct _vinum_conf vinum_conf; /* configuration information */
75
76 struct cdev *vinum_daemon_dev;
77 struct cdev *vinum_super_dev;
78
79 static eventhandler_tag dev_clone_tag;
80
81 /*
82 * Mutexes for plex synchronization. Ideally each plex
83 * should have its own mutex, but the fact that the plex
84 * struct can move makes that very complicated. Instead,
85 * have plexes use share these mutexes based on modulo plex
86 * number.
87 */
88 struct mtx plexmutex[PLEXMUTEXES];
89
90 /*
91 * Called by main() during pseudo-device attachment. All we need
92 * to do is allocate enough space for devices to be configured later, and
93 * add devsw entries.
94 */
95 void
96 vinumattach(void *dummy)
97 {
98 char *envp;
99 int i;
100 #define MUTEXNAMELEN 16
101 char mutexname[MUTEXNAMELEN];
102 #if PLEXMUTEXES > 10000
103 #error Increase size of MUTEXNAMELEN
104 #endif
105 /* modload should prevent multiple loads, so this is worth a panic */
106 if ((vinum_conf.flags & VF_LOADED) != 0)
107 panic("vinum: already loaded");
108
109 log(LOG_INFO, "vinum: loaded\n");
110 #ifdef VINUMDEBUG
111 vinum_conf.flags |= VF_LOADED | VF_HASDEBUG; /* we're loaded now, and we support debug */
112 #else
113 vinum_conf.flags |= VF_LOADED; /* we're loaded now */
114 #endif
115
116 daemonq = NULL; /* initialize daemon's work queue */
117 dqend = NULL;
118
119 vinum_daemon_dev = make_dev(&vinum_cdevsw,
120 VINUM_DAEMON_MINOR,
121 UID_ROOT,
122 GID_WHEEL,
123 S_IRUSR | S_IWUSR,
124 "vinum/controld");
125 vinum_super_dev = make_dev(&vinum_cdevsw,
126 VINUM_SUPERDEV_MINOR,
127 UID_ROOT,
128 GID_WHEEL,
129 S_IRUSR | S_IWUSR,
130 "vinum/control");
131
132 vinum_conf.version = VINUMVERSION; /* note what version we are */
133
134 /* allocate space: drives... */
135 DRIVE = (struct drive *) Malloc(sizeof(struct drive) * INITIAL_DRIVES);
136 CHECKALLOC(DRIVE, "vinum: no memory\n");
137 bzero(DRIVE, sizeof(struct drive) * INITIAL_DRIVES);
138 vinum_conf.drives_allocated = INITIAL_DRIVES; /* number of drive slots allocated */
139 vinum_conf.drives_used = 0; /* and number in use */
140
141 /* volumes, ... */
142 VOL = (struct volume *) Malloc(sizeof(struct volume) * INITIAL_VOLUMES);
143 CHECKALLOC(VOL, "vinum: no memory\n");
144 bzero(VOL, sizeof(struct volume) * INITIAL_VOLUMES);
145 vinum_conf.volumes_allocated = INITIAL_VOLUMES; /* number of volume slots allocated */
146 vinum_conf.volumes_used = 0; /* and number in use */
147
148 /* plexes, ... */
149 PLEX = (struct plex *) Malloc(sizeof(struct plex) * INITIAL_PLEXES);
150 CHECKALLOC(PLEX, "vinum: no memory\n");
151 bzero(PLEX, sizeof(struct plex) * INITIAL_PLEXES);
152 vinum_conf.plexes_allocated = INITIAL_PLEXES; /* number of plex slots allocated */
153 vinum_conf.plexes_used = 0; /* and number in use */
154
155 for (i = 0; i < PLEXMUTEXES; i++) {
156 snprintf(mutexname, MUTEXNAMELEN, "vinumplex%d", i);
157 mtx_init(&plexmutex[i], mutexname, "plex", MTX_DEF);
158 }
159
160 /* and subdisks */
161 SD = (struct sd *) Malloc(sizeof(struct sd) * INITIAL_SUBDISKS);
162 CHECKALLOC(SD, "vinum: no memory\n");
163 bzero(SD, sizeof(struct sd) * INITIAL_SUBDISKS);
164 vinum_conf.subdisks_allocated = INITIAL_SUBDISKS; /* number of sd slots allocated */
165 vinum_conf.subdisks_used = 0; /* and number in use */
166 dev_clone_tag = EVENTHANDLER_REGISTER(dev_clone, vinum_clone, 0, 1000);
167
168 /*
169 * See if the loader has passed us any of the autostart
170 * options.
171 */
172 envp = NULL;
173 if ((envp = getenv("vinum.autostart")) != NULL) { /* start all drives now */
174 vinum_scandisk(NULL);
175 freeenv(envp);
176 } else if ((envp = getenv("vinum.drives")) != NULL) {
177 vinum_scandisk(envp);
178 freeenv(envp);
179 }
180 }
181
182 /*
183 * Check if we have anything open. If confopen is != 0,
184 * that goes for the super device as well, otherwise
185 * only for volumes.
186 *
187 * Return 0 if not inactive, 1 if inactive.
188 */
189 int
190 vinum_inactive(int confopen)
191 {
192 int i;
193 int can_do = 1; /* assume we can do it */
194
195 if (confopen && (vinum_conf.flags & VF_OPEN)) /* open by vinum(8)? */
196 return 0; /* can't do it while we're open */
197 lock_config();
198 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
199 if ((VOL[i].state > volume_down)
200 && (VOL[i].flags & VF_OPEN)) { /* volume is open */
201 can_do = 0;
202 break;
203 }
204 }
205 unlock_config();
206 return can_do;
207 }
208
209 /*
210 * Free all structures.
211 * If cleardrive is 0, save the configuration; otherwise
212 * remove the configuration from the drive.
213 *
214 * Before coming here, ensure that no volumes are open.
215 */
216 void
217 free_vinum(int cleardrive)
218 {
219 int i;
220 int drives_allocated = vinum_conf.drives_allocated;
221
222 while ((vinum_conf.flags & (VF_STOPPING | VF_DAEMONOPEN))
223 == (VF_STOPPING | VF_DAEMONOPEN)) { /* at least one daemon open, we're stopping */
224 queue_daemon_request(daemonrq_return, (union daemoninfo) 0); /* stop the daemon */
225 tsleep(&vinumclose, PUSER, "vstop", 1); /* and wait for it */
226 }
227 if (DRIVE != NULL) {
228 if (cleardrive) { /* remove the vinum config */
229 for (i = 0; i < drives_allocated; i++)
230 remove_drive(i); /* remove the drive */
231 } else { /* keep the config */
232 for (i = 0; i < drives_allocated; i++)
233 free_drive(&DRIVE[i]); /* close files and things */
234 }
235 Free(DRIVE);
236 }
237 if (SD != NULL) {
238 for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
239 struct sd *sd = &SD[i];
240
241 if (sd->state != sd_unallocated)
242 free_sd(i);
243 }
244 Free(SD);
245 }
246 if (PLEX != NULL) {
247 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
248 struct plex *plex = &PLEX[i];
249
250 if (plex->state != plex_unallocated) /* we have real data there */
251 free_plex(i);
252 }
253 Free(PLEX);
254 }
255 if (VOL != NULL) {
256 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
257 struct volume *volume = &VOL[i];
258
259 if (volume->state != volume_unallocated)
260 free_volume(i);
261 }
262 Free(VOL);
263 }
264 bzero(&vinum_conf, sizeof(vinum_conf));
265 vinum_conf.version = VINUMVERSION; /* reinstate version number */
266 }
267
268 STATIC int
269 vinum_modevent(module_t mod, modeventtype_t type, void *unused)
270 {
271 struct sync_args dummyarg =
272 {0};
273 int i;
274
275 switch (type) {
276 case MOD_LOAD:
277 vinumattach(NULL);
278 return 0; /* OK */
279 case MOD_UNLOAD:
280 if (!vinum_inactive(1)) /* is anything open? */
281 return EBUSY; /* yes, we can't do it */
282 vinum_conf.flags |= VF_STOPPING; /* note that we want to stop */
283 sync(curthread, &dummyarg); /* write out buffers */
284 free_vinum(0); /* clean up */
285 #ifdef VINUMDEBUG
286 if (total_malloced) {
287 int i;
288 #ifdef INVARIANTS
289 int *poke;
290 #endif
291
292 for (i = 0; i < malloccount; i++) {
293 if (debug & DEBUG_WARNINGS) /* want to hear about them */
294 log(LOG_WARNING,
295 "vinum: exiting with %d bytes malloced from %s:%d\n",
296 malloced[i].size,
297 malloced[i].file,
298 malloced[i].line);
299 #ifdef INVARIANTS
300 poke = &((int *) malloced[i].address)
301 [malloced[i].size / (2 * sizeof(int))]; /* middle of the area */
302 if (*poke == 0xdeadc0de) /* already freed */
303 log(LOG_ERR,
304 "vinum: exiting with malloc table inconsistency at %p from %s:%d\n",
305 malloced[i].address,
306 malloced[i].file,
307 malloced[i].line);
308 #endif
309 Free(malloced[i].address);
310 }
311 }
312 #endif
313 destroy_dev(vinum_daemon_dev); /* daemon device */
314 destroy_dev(vinum_super_dev);
315 for (i = 0; i < PLEXMUTEXES; i++)
316 mtx_destroy(&plexmutex[i]);
317 log(LOG_INFO, "vinum: unloaded\n"); /* tell the world */
318 EVENTHANDLER_DEREGISTER(dev_clone, dev_clone_tag);
319 return 0;
320 default:
321 return EOPNOTSUPP;
322 break;
323 }
324 return 0;
325 }
326
327 static moduledata_t vinum_mod =
328 {
329 "vinum",
330 (modeventhand_t) vinum_modevent,
331 0
332 };
333 DECLARE_MODULE(vinum, vinum_mod, SI_SUB_RAID, SI_ORDER_MIDDLE);
334
335 /* ARGSUSED */
336 /* Open a vinum object */
337 int
338 vinumopen(struct cdev *dev,
339 int flags,
340 int fmt,
341 struct thread *td)
342 {
343 int error;
344 unsigned int index;
345 struct volume *vol;
346 struct plex *plex;
347 struct sd *sd;
348 int devminor; /* minor number */
349
350 devminor = minor(dev);
351 error = 0;
352 /* First, decide what we're looking at */
353 switch (DEVTYPE(dev)) {
354 case VINUM_VOLUME_TYPE:
355 /*
356 * The super device and daemon device are the last two
357 * volume numbers, so check for them first.
358 */
359 if ((devminor == VINUM_DAEMON_MINOR) /* daemon device */
360 ||(devminor == VINUM_SUPERDEV_MINOR)) { /* or normal super device */
361 error = suser(td); /* are we root? */
362
363 if (error == 0) { /* yes, can do */
364 if (devminor == VINUM_DAEMON_MINOR) /* daemon device */
365 vinum_conf.flags |= VF_DAEMONOPEN; /* we're open */
366 else /* superdev */
367 vinum_conf.flags |= VF_OPEN; /* we're open */
368 }
369 return error;
370 }
371 /* Must be a real volume. Check. */
372 index = Volno(dev);
373 if (index >= vinum_conf.volumes_allocated)
374 return ENXIO; /* no such device */
375 vol = &VOL[index];
376
377 switch (vol->state) {
378 case volume_unallocated:
379 case volume_uninit:
380 return ENXIO;
381
382 case volume_up:
383 vol->flags |= VF_OPEN; /* note we're open */
384 return 0;
385
386 case volume_down:
387 return EIO;
388
389 default:
390 return EINVAL;
391 }
392
393 case VINUM_PLEX_TYPE:
394 index = Plexno(dev); /* get plex index in vinum_conf */
395 if (index >= vinum_conf.plexes_allocated)
396 return ENXIO; /* no such device */
397 plex = &PLEX[index];
398
399 switch (plex->state) {
400 case plex_unallocated:
401 return ENXIO;
402
403 case plex_referenced:
404 return EINVAL;
405
406 default:
407 plex->flags |= VF_OPEN; /* note we're open */
408 return 0;
409 }
410
411 case VINUM_SD_TYPE:
412 case VINUM_SD2_TYPE:
413 index = Sdno(dev); /* get the subdisk number */
414 if (index >= vinum_conf.subdisks_allocated) /* not a valid SD entry */
415 return ENXIO; /* no such device */
416 sd = &SD[index];
417
418 /*
419 * Opening a subdisk is always a special operation, so
420 * we ignore the state as long as it represents a real
421 * subdisk.
422 */
423 switch (sd->state) {
424 case sd_unallocated:
425 return ENXIO;
426
427 case sd_uninit:
428 case sd_referenced:
429 return EINVAL;
430
431 default:
432 sd->flags |= VF_OPEN; /* note we're open */
433 return 0;
434 }
435 }
436 return 0; /* to keep the compiler happy */
437 }
438
439 /* ARGSUSED */
440 int
441 vinumclose(struct cdev *dev,
442 int flags,
443 int fmt,
444 struct thread *td)
445 {
446 unsigned int index;
447 struct volume *vol;
448 int devminor;
449
450 devminor = minor(dev);
451 /* First, decide what we're looking at */
452 switch (DEVTYPE(dev)) {
453 case VINUM_VOLUME_TYPE:
454 /*
455 * The super device and daemon device are the last two
456 * volume numbers, so check for them first.
457 */
458 if ((devminor == VINUM_DAEMON_MINOR) /* daemon device */
459 ||(devminor == VINUM_SUPERDEV_MINOR)) { /* or normal super device */
460 /*
461 * don't worry about whether we're root:
462 * nobody else would get this far.
463 */
464 if (devminor == VINUM_SUPERDEV_MINOR) /* normal superdev */
465 vinum_conf.flags &= ~VF_OPEN; /* no longer open */
466 else { /* the daemon device */
467 vinum_conf.flags &= ~VF_DAEMONOPEN; /* no longer open */
468 if (vinum_conf.flags & VF_STOPPING) /* we're trying to stop, */
469 wakeup(&vinumclose); /* we can continue now */
470 }
471 return 0;
472 }
473 /* Real volume */
474 index = Volno(dev);
475 if (index >= vinum_conf.volumes_allocated)
476 return ENXIO; /* no such device */
477 vol = &VOL[index];
478
479 switch (vol->state) {
480 case volume_unallocated:
481 case volume_uninit:
482 return ENXIO;
483
484 case volume_up:
485 vol->flags &= ~VF_OPEN; /* reset our flags */
486 return 0;
487
488 case volume_down:
489 return EIO;
490
491 default:
492 return EINVAL;
493 }
494
495 case VINUM_PLEX_TYPE:
496 if (Volno(dev) >= vinum_conf.volumes_allocated)
497 return ENXIO;
498 index = Plexno (dev);
499 if (index >= vinum_conf.plexes_allocated) /* no such plex */
500 return ENXIO;
501 PLEX [index].flags &= ~VF_OPEN; /* no longer open */
502 return 0;
503
504 case VINUM_SD_TYPE:
505 if ((Volno(dev) >= vinum_conf.volumes_allocated) || /* no such volume */
506 (Plexno(dev) >= vinum_conf.plexes_allocated)) /* or no such plex */
507 return ENXIO; /* no such device */
508 index = Sdno (dev);
509 if (index >= vinum_conf.subdisks_allocated) /* no such sd */
510 return ENXIO;
511 SD [index].flags &= ~VF_OPEN; /* no longer open */
512 return 0;
513
514
515 default:
516 return ENODEV; /* don't know what to do with these */
517 }
518 }
519
520 void
521 vinum_clone(void *arg, char *name, int namelen, struct cdev ** dev)
522 {
523 struct volume *vol;
524 int i;
525
526 if (*dev != NULL)
527 return;
528 if (strncmp(name, "vinum/", sizeof("vinum/") - 1) != 0)
529 return;
530
531 name += sizeof("vinum/") - 1;
532 if ((i = find_volume(name, 0)) == -1)
533 return;
534
535 vol = &VOL[i];
536 *dev = vol->dev;
537 }
538
539
540 /* Local Variables: */
541 /* fill-column: 60 */
542 /* End: */
Cache object: 807699c2b0fae66c48086d1bd21be262
|