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