md.c revision 1.53 1 /* $NetBSD: md.c,v 1.53 2008/06/11 10:38:44 drochner 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.53 2008/06/11 10:38:44 drochner 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_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, NULL };
117
118 extern struct cfdriver md_cd;
119 CFATTACH_DECL(md, sizeof(struct md_softc),
120 0, md_attach, 0, NULL);
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 struct cfdata *cf;
131
132 if (config_cfattach_attach("md", &md_ca)) {
133 printf("md: cfattach_attach failed\n");
134 return;
135 }
136
137 /* XXX: Are we supposed to provide a default? */
138 if (n <= 1)
139 n = 1;
140
141 /* Attach as if by autoconfig. */
142 for (i = 0; i < n; i++) {
143 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
144 cf->cf_name = "md";
145 cf->cf_atname = "md";
146 cf->cf_unit = i;
147 cf->cf_fstate = FSTATE_NOTFOUND;
148 sc = (struct md_softc *)config_attach_pseudo(cf);
149 }
150 }
151
152 static void
153 md_attach(struct device *parent, struct device *self,
154 void *aux)
155 {
156 struct md_softc *sc = (struct md_softc *)self;
157
158 bufq_alloc(&sc->sc_buflist, "fcfs", 0);
159
160 /* XXX - Could accept aux info here to set the config. */
161 #ifdef MEMORY_DISK_HOOKS
162 /*
163 * This external function might setup a pre-loaded disk.
164 * All it would need to do is setup the md_conf struct.
165 * See sys/dev/md_root.c for an example.
166 */
167 md_attach_hook(device_unit(&sc->sc_dev), &sc->sc_md);
168 #endif
169
170 /*
171 * Initialize and attach the disk structure.
172 */
173 disk_init(&sc->sc_dkdev, device_xname(&sc->sc_dev), &mddkdriver);
174 disk_attach(&sc->sc_dkdev);
175 }
176
177 /*
178 * operational routines:
179 * open, close, read, write, strategy,
180 * ioctl, dump, size
181 */
182
183 #if MEMORY_DISK_SERVER
184 static int md_server_loop(struct md_softc *sc);
185 static int md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
186 struct lwp *l);
187 #endif /* MEMORY_DISK_SERVER */
188 static int md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
189 struct lwp *l);
190
191 static int
192 mdsize(dev_t dev)
193 {
194 int unit;
195 struct md_softc *sc;
196
197 unit = MD_UNIT(dev);
198 sc = device_lookup_private(&md_cd, unit);
199 if (sc == NULL)
200 return 0;
201
202 if (sc->sc_type == MD_UNCONFIGURED)
203 return 0;
204
205 return (sc->sc_size >> DEV_BSHIFT);
206 }
207
208 static int
209 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
210 {
211 int unit;
212 struct md_softc *sc;
213
214 unit = MD_UNIT(dev);
215 sc = device_lookup_private(&md_cd, unit);
216 if (sc == NULL)
217 return ENXIO;
218
219 /*
220 * The raw partition is used for ioctl to configure.
221 */
222 if (DISKPART(dev) == RAW_PART)
223 return 0;
224
225 #ifdef MEMORY_DISK_HOOKS
226 /* Call the open hook to allow loading the device. */
227 md_open_hook(unit, &sc->sc_md);
228 #endif
229
230 /*
231 * This is a normal, "slave" device, so
232 * enforce initialized.
233 */
234 if (sc->sc_type == MD_UNCONFIGURED)
235 return ENXIO;
236
237 return 0;
238 }
239
240 static int
241 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
242 {
243
244 return 0;
245 }
246
247 static int
248 mdread(dev_t dev, struct uio *uio, int flags)
249 {
250 int unit;
251 struct md_softc *sc;
252
253 unit = MD_UNIT(dev);
254
255 sc = device_lookup_private(&md_cd, unit);
256
257 if (sc->sc_type == MD_UNCONFIGURED)
258 return ENXIO;
259
260 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
261 }
262
263 static int
264 mdwrite(dev_t dev, struct uio *uio, int flags)
265 {
266 int unit;
267 struct md_softc *sc;
268
269 unit = MD_UNIT(dev);
270
271 sc = device_lookup_private(&md_cd, unit);
272
273 if (sc->sc_type == MD_UNCONFIGURED)
274 return ENXIO;
275
276 return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
277 }
278
279 /*
280 * Handle I/O requests, either directly, or
281 * by passing them to the server process.
282 */
283 static void
284 mdstrategy(struct buf *bp)
285 {
286 int unit;
287 struct md_softc *sc;
288 void * addr;
289 size_t off, xfer;
290
291 unit = MD_UNIT(bp->b_dev);
292 sc = device_lookup_private(&md_cd, unit);
293
294 if (sc->sc_type == MD_UNCONFIGURED) {
295 bp->b_error = ENXIO;
296 goto done;
297 }
298
299 switch (sc->sc_type) {
300 #if MEMORY_DISK_SERVER
301 case MD_UMEM_SERVER:
302 /* Just add this job to the server's queue. */
303 BUFQ_PUT(sc->sc_buflist, bp);
304 wakeup((void *)sc);
305 /* see md_server_loop() */
306 /* no biodone in this case */
307 return;
308 #endif /* MEMORY_DISK_SERVER */
309
310 case MD_KMEM_FIXED:
311 case MD_KMEM_ALLOCATED:
312 /* These are in kernel space. Access directly. */
313 bp->b_resid = bp->b_bcount;
314 off = (bp->b_blkno << DEV_BSHIFT);
315 if (off >= sc->sc_size) {
316 if (bp->b_flags & B_READ)
317 break; /* EOF */
318 goto set_eio;
319 }
320 xfer = bp->b_resid;
321 if (xfer > (sc->sc_size - off))
322 xfer = (sc->sc_size - off);
323 addr = (char *)sc->sc_addr + off;
324 if (bp->b_flags & B_READ)
325 memcpy(bp->b_data, addr, xfer);
326 else
327 memcpy(addr, bp->b_data, xfer);
328 bp->b_resid -= xfer;
329 break;
330
331 default:
332 bp->b_resid = bp->b_bcount;
333 set_eio:
334 bp->b_error = EIO;
335 break;
336 }
337 done:
338 biodone(bp);
339 }
340
341 static int
342 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
343 {
344 int unit;
345 struct md_softc *sc;
346 struct md_conf *umd;
347
348 unit = MD_UNIT(dev);
349 sc = device_lookup_private(&md_cd, unit);
350
351 /* If this is not the raw partition, punt! */
352 if (DISKPART(dev) != RAW_PART)
353 return ENOTTY;
354
355 umd = (struct md_conf *)data;
356 switch (cmd) {
357 case MD_GETCONF:
358 *umd = sc->sc_md;
359 return 0;
360
361 case MD_SETCONF:
362 /* Can only set it once. */
363 if (sc->sc_type != MD_UNCONFIGURED)
364 break;
365 switch (umd->md_type) {
366 case MD_KMEM_ALLOCATED:
367 return md_ioctl_kalloc(sc, umd, l);
368 #if MEMORY_DISK_SERVER
369 case MD_UMEM_SERVER:
370 return md_ioctl_server(sc, umd, l);
371 #endif /* MEMORY_DISK_SERVER */
372 default:
373 break;
374 }
375 break;
376 }
377 return EINVAL;
378 }
379
380 /*
381 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
382 * Just allocate some kernel memory and return.
383 */
384 static int
385 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
386 struct lwp *l)
387 {
388 vaddr_t addr;
389 vsize_t size;
390
391 /* Sanity check the size. */
392 size = umd->md_size;
393 addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
394 if (!addr)
395 return ENOMEM;
396
397 /* This unit is now configured. */
398 sc->sc_addr = (void *)addr; /* kernel space */
399 sc->sc_size = (size_t)size;
400 sc->sc_type = MD_KMEM_ALLOCATED;
401 return 0;
402 }
403
404 #if MEMORY_DISK_SERVER
405
406 /*
407 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
408 * Set config, then become the I/O server for this unit.
409 */
410 static int
411 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
412 struct lwp *l)
413 {
414 vaddr_t end;
415 int error;
416
417 /* Sanity check addr, size. */
418 end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
419
420 if ((end >= VM_MAXUSER_ADDRESS) ||
421 (end < ((vaddr_t) umd->md_addr)) )
422 return EINVAL;
423
424 /* This unit is now configured. */
425 sc->sc_addr = umd->md_addr; /* user space */
426 sc->sc_size = umd->md_size;
427 sc->sc_type = MD_UMEM_SERVER;
428
429 /* Become the server daemon */
430 error = md_server_loop(sc);
431
432 /* This server is now going away! */
433 sc->sc_type = MD_UNCONFIGURED;
434 sc->sc_addr = 0;
435 sc->sc_size = 0;
436
437 return (error);
438 }
439
440 static int md_sleep_pri = PWAIT | PCATCH;
441
442 static int
443 md_server_loop(struct md_softc *sc)
444 {
445 struct buf *bp;
446 void *addr; /* user space address */
447 size_t off; /* offset into "device" */
448 size_t xfer; /* amount to transfer */
449 int error;
450
451 for (;;) {
452 /* Wait for some work to arrive. */
453 while ((bp = BUFQ_GET(sc->sc_buflist)) == NULL) {
454 error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
455 if (error)
456 return error;
457 }
458
459 /* Do the transfer to/from user space. */
460 error = 0;
461 bp->b_resid = bp->b_bcount;
462 off = (bp->b_blkno << DEV_BSHIFT);
463 if (off >= sc->sc_size) {
464 if (bp->b_flags & B_READ)
465 goto done; /* EOF (not an error) */
466 error = EIO;
467 goto done;
468 }
469 xfer = bp->b_resid;
470 if (xfer > (sc->sc_size - off))
471 xfer = (sc->sc_size - off);
472 addr = (char *)sc->sc_addr + off;
473 if (bp->b_flags & B_READ)
474 error = copyin(addr, bp->b_data, xfer);
475 else
476 error = copyout(bp->b_data, addr, xfer);
477 if (!error)
478 bp->b_resid -= xfer;
479
480 done:
481 if (error) {
482 bp->b_error = error;
483 }
484 biodone(bp);
485 }
486 }
487 #endif /* MEMORY_DISK_SERVER */
488