md.c revision 1.20.14.1 1 /* $NetBSD: md.c,v 1.20.14.1 1999/12/21 23:19:52 wrstuden Exp $ */
2
3 /*
4 * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 * 4. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by
20 * Gordon W. Ross and Leo Weppelman.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * This implements a general-purpose memory-disk.
36 * See md.h for notes on the config types.
37 *
38 * Note that this driver provides the same functionality
39 * as the MFS filesystem hack, but this is better because
40 * you can use this for any filesystem type you'd like!
41 *
42 * Credit for most of the kmem ramdisk code goes to:
43 * Leo Weppelman (atari) and Phil Nelson (pc532)
44 * Credit for the ideas behind the "user space memory" code goes
45 * to the authors of the MFS implementation.
46 */
47
48 #include "opt_md.h"
49
50 #include <sys/param.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/systm.h>
54 #include <sys/buf.h>
55 #include <sys/device.h>
56 #include <sys/disk.h>
57 #include <sys/proc.h>
58 #include <sys/conf.h>
59 #include <sys/disklabel.h>
60 #include <sys/dkio.h>
61
62 #include <vm/vm.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_extern.h>
65
66 #include <dev/md.h>
67
68 /*
69 * By default, include the user-space functionality.
70 * Use `options MEMORY_DISK_SERVER=0' to turn it off.
71 */
72 #ifndef MEMORY_DISK_SERVER
73 #define MEMORY_DISK_SERVER 1
74 #endif
75
76 /*
77 * XXX: the "control" unit is (base unit + 16).
78 * We should just use the cdev as the "control", but
79 * that interferes with the security stuff preventing
80 * simulatneous use of raw and block devices.
81 *
82 * XXX Assumption: 16 memory-disks are enough!
83 */
84 #define MD_MAX_UNITS 0x10
85 #define MD_IS_CTRL(unit) (unit & 0x10)
86 #define MD_UNIT(unit) (unit & 0xF)
87
88 /* autoconfig stuff... */
89
90 struct md_softc {
91 struct device sc_dev; /* REQUIRED first entry */
92 struct disk sc_dkdev; /* hook for generic disk handling */
93 struct md_conf sc_md;
94 struct buf *sc_buflist;
95 int sc_flags;
96 };
97 /* shorthand for fields in sc_md: */
98 #define sc_addr sc_md.md_addr
99 #define sc_size sc_md.md_size
100 #define sc_type sc_md.md_type
101 /* flags */
102 #define MD_ISOPEN 0x01
103 #define MD_SERVED 0x02
104
105 void mdattach __P((int));
106 static void md_attach __P((struct device *, struct device *, void *));
107
108 void mdstrategy __P((struct buf *bp));
109 struct dkdriver mddkdriver = { mdstrategy };
110
111 static int ramdisk_ndevs;
112 static void *ramdisk_devs[MD_MAX_UNITS];
113
114 /*
115 * This is called if we are configured as a pseudo-device
116 */
117 void
118 mdattach(n)
119 int n;
120 {
121 struct md_softc *sc;
122 int i;
123
124 #ifdef DIAGNOSTIC
125 if (ramdisk_ndevs) {
126 printf("ramdisk: multiple attach calls?\n");
127 return;
128 }
129 #endif
130
131 /* XXX: Are we supposed to provide a default? */
132 if (n <= 1)
133 n = 1;
134 if (n > MD_MAX_UNITS)
135 n = MD_MAX_UNITS;
136 ramdisk_ndevs = n;
137
138 /* Attach as if by autoconfig. */
139 for (i = 0; i < n; i++) {
140
141 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
142 if (!sc) {
143 printf("ramdisk: malloc for attach failed!\n");
144 return;
145 }
146 bzero((caddr_t)sc, sizeof(*sc));
147 ramdisk_devs[i] = sc;
148 sc->sc_dev.dv_unit = i;
149 sprintf(sc->sc_dev.dv_xname, "md%d", i);
150 md_attach(NULL, &sc->sc_dev, NULL);
151 }
152 }
153
154 static void
155 md_attach(parent, self, aux)
156 struct device *parent, *self;
157 void *aux;
158 {
159 struct md_softc *sc = (struct md_softc *)self;
160
161 /* XXX - Could accept aux info here to set the config. */
162 #ifdef MEMORY_DISK_HOOKS
163 /*
164 * This external function might setup a pre-loaded disk.
165 * All it would need to do is setup the md_conf struct.
166 * See sys/arch/sun3/dev/md_root.c for an example.
167 */
168 md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
169 #endif
170
171 /*
172 * Initialize and attach the disk structure.
173 */
174 sc->sc_dkdev.dk_driver = &mddkdriver;
175 sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
176 sc->sc_dkdev.dk_byteshift = DEF_BSHIFT;
177 disk_attach(&sc->sc_dkdev);
178 }
179
180 /*
181 * operational routines:
182 * open, close, read, write, strategy,
183 * ioctl, dump, size
184 */
185
186 #if MEMORY_DISK_SERVER
187 static int md_server_loop __P((struct md_softc *sc));
188 static int md_ioctl_server __P((struct md_softc *sc,
189 struct md_conf *umd, struct proc *proc));
190 #endif
191 static int md_ioctl_kalloc __P((struct md_softc *sc,
192 struct md_conf *umd, struct proc *proc));
193
194 dev_type_open(mdopen);
195 dev_type_close(mdclose);
196 dev_type_read(mdread);
197 dev_type_write(mdwrite);
198 dev_type_ioctl(mdioctl);
199 dev_type_size(mdsize);
200 dev_type_dump(mddump);
201
202 int mddump(dev, blkno, va, size)
203 dev_t dev;
204 daddr_t blkno;
205 caddr_t va;
206 size_t size;
207 {
208 return ENODEV;
209 }
210
211 int mdsize(dev_t dev)
212 {
213 int unit;
214 struct md_softc *sc;
215
216 /* Disallow control units. */
217 unit = DISKUNIT(dev);
218 if (unit >= ramdisk_ndevs)
219 return 0;
220 sc = ramdisk_devs[unit];
221 if (sc == NULL)
222 return 0;
223
224 if (sc->sc_type == MD_UNCONFIGURED)
225 return 0;
226
227 return (sc->sc_size >> DEF_BSHIFT);
228 }
229
230 int
231 mdopen(dev, flag, fmt, proc)
232 dev_t dev;
233 int flag, fmt;
234 struct proc *proc;
235 {
236 int md, unit;
237 struct md_softc *sc;
238
239 md = DISKUNIT(dev);
240 unit = MD_UNIT(md);
241 if (unit >= ramdisk_ndevs)
242 return ENXIO;
243 sc = ramdisk_devs[unit];
244 if (sc == NULL)
245 return ENXIO;
246
247 /*
248 * The control device is not exclusive, and can
249 * open uninitialized units (so you can setconf).
250 */
251 if (MD_IS_CTRL(md))
252 return 0;
253
254 #ifdef MEMORY_DISK_HOOKS
255 /* Call the open hook to allow loading the device. */
256 md_open_hook(unit, &sc->sc_md);
257 #endif
258
259 /*
260 * This is a normal, "slave" device, so
261 * enforce initialized, exclusive open.
262 */
263 if (sc->sc_type == MD_UNCONFIGURED)
264 return ENXIO;
265 if (sc->sc_flags & MD_ISOPEN)
266 return EBUSY;
267
268 return 0;
269 }
270
271 int
272 mdclose(dev, flag, fmt, proc)
273 dev_t dev;
274 int flag, fmt;
275 struct proc *proc;
276 {
277 int md, unit;
278 struct md_softc *sc;
279
280 md = DISKUNIT(dev);
281 unit = MD_UNIT(md);
282 sc = ramdisk_devs[unit];
283
284 if (MD_IS_CTRL(md))
285 return 0;
286
287 /* Normal device. */
288 sc->sc_flags = 0;
289
290 return 0;
291 }
292
293 int
294 mdread(dev, uio, flags)
295 dev_t dev;
296 struct uio *uio;
297 int flags;
298 {
299 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio,
300 DEF_BSHIFT));
301 }
302
303 int
304 mdwrite(dev, uio, flags)
305 dev_t dev;
306 struct uio *uio;
307 int flags;
308 {
309 return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio,
310 DEF_BSHIFT));
311 }
312
313 /*
314 * Handle I/O requests, either directly, or
315 * by passing them to the server process.
316 */
317 void
318 mdstrategy(bp)
319 struct buf *bp;
320 {
321 int md, unit;
322 struct md_softc *sc;
323 caddr_t addr;
324 size_t off, xfer;
325
326 md = DISKUNIT(bp->b_dev);
327 unit = MD_UNIT(md);
328 sc = ramdisk_devs[unit];
329
330 switch (sc->sc_type) {
331 #if MEMORY_DISK_SERVER
332 case MD_UMEM_SERVER:
333 /* Just add this job to the server's queue. */
334 bp->b_actf = sc->sc_buflist;
335 sc->sc_buflist = bp;
336 if (bp->b_actf == NULL) {
337 /* server queue was empty. */
338 wakeup((caddr_t)sc);
339 /* see md_server_loop() */
340 }
341 /* no biodone in this case */
342 return;
343 #endif /* MEMORY_DISK_SERVER */
344
345 case MD_KMEM_FIXED:
346 case MD_KMEM_ALLOCATED:
347 /* These are in kernel space. Access directly. */
348 bp->b_resid = bp->b_bcount;
349 off = (bp->b_blkno << sc->sc_dkdev.dk_byteshift);
350 if (off >= sc->sc_size) {
351 if (bp->b_flags & B_READ)
352 break; /* EOF */
353 goto set_eio;
354 }
355 xfer = bp->b_resid;
356 if (xfer > (sc->sc_size - off))
357 xfer = (sc->sc_size - off);
358 addr = sc->sc_addr + off;
359 if (bp->b_flags & B_READ)
360 bcopy(addr, bp->b_data, xfer);
361 else
362 bcopy(bp->b_data, addr, xfer);
363 bp->b_resid -= xfer;
364 break;
365
366 default:
367 bp->b_resid = bp->b_bcount;
368 set_eio:
369 bp->b_error = EIO;
370 bp->b_flags |= B_ERROR;
371 break;
372 }
373 biodone(bp);
374 }
375
376 int
377 mdioctl(dev, cmd, data, flag, proc)
378 dev_t dev;
379 u_long cmd;
380 int flag;
381 caddr_t data;
382 struct proc *proc;
383 {
384 int md, unit;
385 struct md_softc *sc;
386 struct md_conf *umd;
387
388 md = DISKUNIT(dev);
389 unit = MD_UNIT(md);
390 sc = ramdisk_devs[unit];
391
392 /* If this is not the control device, punt! */
393 if (MD_IS_CTRL(md) == 0)
394 return ENOTTY;
395
396 umd = (struct md_conf *)data;
397 switch (cmd) {
398 case MD_GETCONF:
399 *umd = sc->sc_md;
400 return 0;
401
402 case DIOCGBSHIFT:
403 *(int *)data = DEF_BSHIFT;
404 return 0;
405
406 case MD_SETCONF:
407 /* Can only set it once. */
408 if (sc->sc_type != MD_UNCONFIGURED)
409 break;
410 switch (umd->md_type) {
411 case MD_KMEM_ALLOCATED:
412 return md_ioctl_kalloc(sc, umd, proc);
413 #if MEMORY_DISK_SERVER
414 case MD_UMEM_SERVER:
415 return md_ioctl_server(sc, umd, proc);
416 #endif
417 default:
418 break;
419 }
420 break;
421 }
422 return EINVAL;
423 }
424
425 /*
426 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
427 * Just allocate some kernel memory and return.
428 */
429 static int
430 md_ioctl_kalloc(sc, umd, proc)
431 struct md_softc *sc;
432 struct md_conf *umd;
433 struct proc *proc;
434 {
435 vaddr_t addr;
436 vsize_t size;
437
438 /* Sanity check the size. */
439 size = umd->md_size;
440 addr = uvm_km_zalloc(kernel_map, size);
441 if (!addr)
442 return ENOMEM;
443
444 /* This unit is now configured. */
445 sc->sc_addr = (caddr_t)addr; /* kernel space */
446 sc->sc_size = (size_t)size;
447 sc->sc_type = MD_KMEM_ALLOCATED;
448 return 0;
449 }
450
451 #if MEMORY_DISK_SERVER
452
453 /*
454 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
455 * Set config, then become the I/O server for this unit.
456 */
457 static int
458 md_ioctl_server(sc, umd, proc)
459 struct md_softc *sc;
460 struct md_conf *umd;
461 struct proc *proc;
462 {
463 vaddr_t end;
464 int error;
465
466 /* Sanity check addr, size. */
467 end = (vaddr_t) (umd->md_addr + umd->md_size);
468
469 if ((end >= VM_MAXUSER_ADDRESS) ||
470 (end < ((vaddr_t) umd->md_addr)) )
471 return EINVAL;
472
473 /* This unit is now configured. */
474 sc->sc_addr = umd->md_addr; /* user space */
475 sc->sc_size = umd->md_size;
476 sc->sc_type = MD_UMEM_SERVER;
477
478 /* Become the server daemon */
479 error = md_server_loop(sc);
480
481 /* This server is now going away! */
482 sc->sc_type = MD_UNCONFIGURED;
483 sc->sc_addr = 0;
484 sc->sc_size = 0;
485
486 return (error);
487 }
488
489 int md_sleep_pri = PWAIT | PCATCH;
490
491 static int
492 md_server_loop(sc)
493 struct md_softc *sc;
494 {
495 struct buf *bp;
496 caddr_t addr; /* user space address */
497 size_t off; /* offset into "device" */
498 size_t xfer; /* amount to transfer */
499 int error;
500
501 for (;;) {
502 /* Wait for some work to arrive. */
503 while (sc->sc_buflist == NULL) {
504 error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
505 if (error)
506 return error;
507 }
508
509 /* Unlink buf from head of list. */
510 bp = sc->sc_buflist;
511 sc->sc_buflist = bp->b_actf;
512 bp->b_actf = NULL;
513
514 /* Do the transfer to/from user space. */
515 error = 0;
516 bp->b_resid = bp->b_bcount;
517 off = (bp->b_blkno << sc->sc_dkdev.dk_byteshift);
518 if (off >= sc->sc_size) {
519 if (bp->b_flags & B_READ)
520 goto done; /* EOF (not an error) */
521 error = EIO;
522 goto done;
523 }
524 xfer = bp->b_resid;
525 if (xfer > (sc->sc_size - off))
526 xfer = (sc->sc_size - off);
527 addr = sc->sc_addr + off;
528 if (bp->b_flags & B_READ)
529 error = copyin(addr, bp->b_data, xfer);
530 else
531 error = copyout(bp->b_data, addr, xfer);
532 if (!error)
533 bp->b_resid -= xfer;
534
535 done:
536 if (error) {
537 bp->b_error = error;
538 bp->b_flags |= B_ERROR;
539 }
540 biodone(bp);
541 }
542 }
543 #endif /* MEMORY_DISK_SERVER */
544