md.c revision 1.21 1 /* $NetBSD: md.c,v 1.21 2000/01/21 12:14:53 tsutsui 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
61 #include <vm/vm.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_extern.h>
64
65 #include <dev/md.h>
66
67 /*
68 * By default, include the user-space functionality.
69 * Use `options MEMORY_DISK_SERVER=0' to turn it off.
70 */
71 #ifndef MEMORY_DISK_SERVER
72 #define MEMORY_DISK_SERVER 1
73 #endif
74
75 /*
76 * We should use the raw partition for ioctl.
77 */
78 #define MD_MAX_UNITS 0x10
79 #define MD_UNIT(unit) DISKUNIT(unit)
80
81 /* autoconfig stuff... */
82
83 struct md_softc {
84 struct device sc_dev; /* REQUIRED first entry */
85 struct disk sc_dkdev; /* hook for generic disk handling */
86 struct md_conf sc_md;
87 struct buf *sc_buflist;
88 };
89 /* shorthand for fields in sc_md: */
90 #define sc_addr sc_md.md_addr
91 #define sc_size sc_md.md_size
92 #define sc_type sc_md.md_type
93
94 void mdattach __P((int));
95 static void md_attach __P((struct device *, struct device *, void *));
96
97 void mdstrategy __P((struct buf *bp));
98 struct dkdriver mddkdriver = { mdstrategy };
99
100 static int ramdisk_ndevs;
101 static void *ramdisk_devs[MD_MAX_UNITS];
102
103 /*
104 * This is called if we are configured as a pseudo-device
105 */
106 void
107 mdattach(n)
108 int n;
109 {
110 struct md_softc *sc;
111 int i;
112
113 #ifdef DIAGNOSTIC
114 if (ramdisk_ndevs) {
115 printf("ramdisk: multiple attach calls?\n");
116 return;
117 }
118 #endif
119
120 /* XXX: Are we supposed to provide a default? */
121 if (n <= 1)
122 n = 1;
123 if (n > MD_MAX_UNITS)
124 n = MD_MAX_UNITS;
125 ramdisk_ndevs = n;
126
127 /* Attach as if by autoconfig. */
128 for (i = 0; i < n; i++) {
129
130 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
131 if (!sc) {
132 printf("ramdisk: malloc for attach failed!\n");
133 return;
134 }
135 bzero((caddr_t)sc, sizeof(*sc));
136 ramdisk_devs[i] = sc;
137 sc->sc_dev.dv_unit = i;
138 sprintf(sc->sc_dev.dv_xname, "md%d", i);
139 md_attach(NULL, &sc->sc_dev, NULL);
140 }
141 }
142
143 static void
144 md_attach(parent, self, aux)
145 struct device *parent, *self;
146 void *aux;
147 {
148 struct md_softc *sc = (struct md_softc *)self;
149
150 /* XXX - Could accept aux info here to set the config. */
151 #ifdef MEMORY_DISK_HOOKS
152 /*
153 * This external function might setup a pre-loaded disk.
154 * All it would need to do is setup the md_conf struct.
155 * See sys/arch/sun3/dev/md_root.c for an example.
156 */
157 md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
158 #endif
159
160 /*
161 * Initialize and attach the disk structure.
162 */
163 sc->sc_dkdev.dk_driver = &mddkdriver;
164 sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
165 disk_attach(&sc->sc_dkdev);
166 }
167
168 /*
169 * operational routines:
170 * open, close, read, write, strategy,
171 * ioctl, dump, size
172 */
173
174 #if MEMORY_DISK_SERVER
175 static int md_server_loop __P((struct md_softc *sc));
176 static int md_ioctl_server __P((struct md_softc *sc,
177 struct md_conf *umd, struct proc *proc));
178 #endif
179 static int md_ioctl_kalloc __P((struct md_softc *sc,
180 struct md_conf *umd, struct proc *proc));
181
182 dev_type_open(mdopen);
183 dev_type_close(mdclose);
184 dev_type_read(mdread);
185 dev_type_write(mdwrite);
186 dev_type_ioctl(mdioctl);
187 dev_type_size(mdsize);
188 dev_type_dump(mddump);
189
190 int
191 mddump(dev, blkno, va, size)
192 dev_t dev;
193 daddr_t blkno;
194 caddr_t va;
195 size_t size;
196 {
197 return ENODEV;
198 }
199
200 int
201 mdsize(dev_t dev)
202 {
203 int unit;
204 struct md_softc *sc;
205
206 unit = MD_UNIT(dev);
207 if (unit >= ramdisk_ndevs)
208 return 0;
209 sc = ramdisk_devs[unit];
210 if (sc == NULL)
211 return 0;
212
213 if (sc->sc_type == MD_UNCONFIGURED)
214 return 0;
215
216 return (sc->sc_size >> DEV_BSHIFT);
217 }
218
219 int
220 mdopen(dev, flag, fmt, proc)
221 dev_t dev;
222 int flag, fmt;
223 struct proc *proc;
224 {
225 int unit;
226 struct md_softc *sc;
227
228 unit = MD_UNIT(dev);
229 if (unit >= ramdisk_ndevs)
230 return ENXIO;
231 sc = ramdisk_devs[unit];
232 if (sc == NULL)
233 return ENXIO;
234
235 /*
236 * The raw partition is used for ioctl to configure.
237 */
238 if (DISKPART(dev) == RAW_PART)
239 return 0;
240
241 #ifdef MEMORY_DISK_HOOKS
242 /* Call the open hook to allow loading the device. */
243 md_open_hook(unit, &sc->sc_md);
244 #endif
245
246 /*
247 * This is a normal, "slave" device, so
248 * enforce initialized.
249 */
250 if (sc->sc_type == MD_UNCONFIGURED)
251 return ENXIO;
252
253 return 0;
254 }
255
256 int
257 mdclose(dev, flag, fmt, proc)
258 dev_t dev;
259 int flag, fmt;
260 struct proc *proc;
261 {
262 int unit;
263
264 unit = MD_UNIT(dev);
265
266 if (unit >= ramdisk_ndevs)
267 return ENXIO;
268
269 return 0;
270 }
271
272 int
273 mdread(dev, uio, flags)
274 dev_t dev;
275 struct uio *uio;
276 int flags;
277 {
278 int unit;
279 struct md_softc *sc;
280
281 unit = MD_UNIT(dev);
282
283 if (unit >= ramdisk_ndevs)
284 return ENXIO;
285
286 sc = ramdisk_devs[unit];
287
288 if (sc->sc_type == MD_UNCONFIGURED)
289 return ENXIO;
290
291 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
292 }
293
294 int
295 mdwrite(dev, uio, flags)
296 dev_t dev;
297 struct uio *uio;
298 int flags;
299 {
300 int unit;
301 struct md_softc *sc;
302
303 unit = MD_UNIT(dev);
304
305 if (unit >= ramdisk_ndevs)
306 return ENXIO;
307
308 sc = ramdisk_devs[unit];
309
310 if (sc->sc_type == MD_UNCONFIGURED)
311 return ENXIO;
312
313 return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
314 }
315
316 /*
317 * Handle I/O requests, either directly, or
318 * by passing them to the server process.
319 */
320 void
321 mdstrategy(bp)
322 struct buf *bp;
323 {
324 int unit;
325 struct md_softc *sc;
326 caddr_t addr;
327 size_t off, xfer;
328
329 unit = MD_UNIT(bp->b_dev);
330 sc = ramdisk_devs[unit];
331
332 if (sc->sc_type == MD_UNCONFIGURED) {
333 bp->b_error = ENXIO;
334 bp->b_flags |= B_ERROR;
335 goto done;
336 }
337
338 switch (sc->sc_type) {
339 #if MEMORY_DISK_SERVER
340 case MD_UMEM_SERVER:
341 /* Just add this job to the server's queue. */
342 bp->b_actf = sc->sc_buflist;
343 sc->sc_buflist = bp;
344 if (bp->b_actf == NULL) {
345 /* server queue was empty. */
346 wakeup((caddr_t)sc);
347 /* see md_server_loop() */
348 }
349 /* no biodone in this case */
350 return;
351 #endif /* MEMORY_DISK_SERVER */
352
353 case MD_KMEM_FIXED:
354 case MD_KMEM_ALLOCATED:
355 /* These are in kernel space. Access directly. */
356 bp->b_resid = bp->b_bcount;
357 off = (bp->b_blkno << DEV_BSHIFT);
358 if (off >= sc->sc_size) {
359 if (bp->b_flags & B_READ)
360 break; /* EOF */
361 goto set_eio;
362 }
363 xfer = bp->b_resid;
364 if (xfer > (sc->sc_size - off))
365 xfer = (sc->sc_size - off);
366 addr = sc->sc_addr + off;
367 if (bp->b_flags & B_READ)
368 bcopy(addr, bp->b_data, xfer);
369 else
370 bcopy(bp->b_data, addr, xfer);
371 bp->b_resid -= xfer;
372 break;
373
374 default:
375 bp->b_resid = bp->b_bcount;
376 set_eio:
377 bp->b_error = EIO;
378 bp->b_flags |= B_ERROR;
379 break;
380 }
381 done:
382 biodone(bp);
383 }
384
385 int
386 mdioctl(dev, cmd, data, flag, proc)
387 dev_t dev;
388 u_long cmd;
389 int flag;
390 caddr_t data;
391 struct proc *proc;
392 {
393 int unit;
394 struct md_softc *sc;
395 struct md_conf *umd;
396
397 unit = MD_UNIT(dev);
398 sc = ramdisk_devs[unit];
399
400 /* If this is not the raw partition, punt! */
401 if (DISKPART(dev) != RAW_PART)
402 return ENOTTY;
403
404 umd = (struct md_conf *)data;
405 switch (cmd) {
406 case MD_GETCONF:
407 *umd = sc->sc_md;
408 return 0;
409
410 case MD_SETCONF:
411 /* Can only set it once. */
412 if (sc->sc_type != MD_UNCONFIGURED)
413 break;
414 switch (umd->md_type) {
415 case MD_KMEM_ALLOCATED:
416 return md_ioctl_kalloc(sc, umd, proc);
417 #if MEMORY_DISK_SERVER
418 case MD_UMEM_SERVER:
419 return md_ioctl_server(sc, umd, proc);
420 #endif
421 default:
422 break;
423 }
424 break;
425 }
426 return EINVAL;
427 }
428
429 /*
430 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
431 * Just allocate some kernel memory and return.
432 */
433 static int
434 md_ioctl_kalloc(sc, umd, proc)
435 struct md_softc *sc;
436 struct md_conf *umd;
437 struct proc *proc;
438 {
439 vaddr_t addr;
440 vsize_t size;
441
442 /* Sanity check the size. */
443 size = umd->md_size;
444 addr = uvm_km_zalloc(kernel_map, size);
445 if (!addr)
446 return ENOMEM;
447
448 /* This unit is now configured. */
449 sc->sc_addr = (caddr_t)addr; /* kernel space */
450 sc->sc_size = (size_t)size;
451 sc->sc_type = MD_KMEM_ALLOCATED;
452 return 0;
453 }
454
455 #if MEMORY_DISK_SERVER
456
457 /*
458 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
459 * Set config, then become the I/O server for this unit.
460 */
461 static int
462 md_ioctl_server(sc, umd, proc)
463 struct md_softc *sc;
464 struct md_conf *umd;
465 struct proc *proc;
466 {
467 vaddr_t end;
468 int error;
469
470 /* Sanity check addr, size. */
471 end = (vaddr_t) (umd->md_addr + umd->md_size);
472
473 if ((end >= VM_MAXUSER_ADDRESS) ||
474 (end < ((vaddr_t) umd->md_addr)) )
475 return EINVAL;
476
477 /* This unit is now configured. */
478 sc->sc_addr = umd->md_addr; /* user space */
479 sc->sc_size = umd->md_size;
480 sc->sc_type = MD_UMEM_SERVER;
481
482 /* Become the server daemon */
483 error = md_server_loop(sc);
484
485 /* This server is now going away! */
486 sc->sc_type = MD_UNCONFIGURED;
487 sc->sc_addr = 0;
488 sc->sc_size = 0;
489
490 return (error);
491 }
492
493 int md_sleep_pri = PWAIT | PCATCH;
494
495 static int
496 md_server_loop(sc)
497 struct md_softc *sc;
498 {
499 struct buf *bp;
500 caddr_t addr; /* user space address */
501 size_t off; /* offset into "device" */
502 size_t xfer; /* amount to transfer */
503 int error;
504
505 for (;;) {
506 /* Wait for some work to arrive. */
507 while (sc->sc_buflist == NULL) {
508 error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
509 if (error)
510 return error;
511 }
512
513 /* Unlink buf from head of list. */
514 bp = sc->sc_buflist;
515 sc->sc_buflist = bp->b_actf;
516 bp->b_actf = NULL;
517
518 /* Do the transfer to/from user space. */
519 error = 0;
520 bp->b_resid = bp->b_bcount;
521 off = (bp->b_blkno << DEV_BSHIFT);
522 if (off >= sc->sc_size) {
523 if (bp->b_flags & B_READ)
524 goto done; /* EOF (not an error) */
525 error = EIO;
526 goto done;
527 }
528 xfer = bp->b_resid;
529 if (xfer > (sc->sc_size - off))
530 xfer = (sc->sc_size - off);
531 addr = sc->sc_addr + off;
532 if (bp->b_flags & B_READ)
533 error = copyin(addr, bp->b_data, xfer);
534 else
535 error = copyout(bp->b_data, addr, xfer);
536 if (!error)
537 bp->b_resid -= xfer;
538
539 done:
540 if (error) {
541 bp->b_error = error;
542 bp->b_flags |= B_ERROR;
543 }
544 biodone(bp);
545 }
546 }
547 #endif /* MEMORY_DISK_SERVER */
548