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