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