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