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