md.c revision 1.55 1 1.55 cegger /* $NetBSD: md.c,v 1.55 2008/06/13 20:05:06 cegger Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1995 Gordon W. Ross, Leo Weppelman.
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.1 gwr * 3. The name of the author may not be used to endorse or promote products
16 1.1 gwr * derived from this software without specific prior written permission.
17 1.1 gwr * 4. All advertising materials mentioning features or use of this software
18 1.1 gwr * must display the following acknowledgement:
19 1.1 gwr * This product includes software developed by
20 1.1 gwr * Gordon W. Ross and Leo Weppelman.
21 1.1 gwr *
22 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 gwr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 gwr */
33 1.1 gwr
34 1.1 gwr /*
35 1.11 pk * This implements a general-purpose memory-disk.
36 1.13 jeremy * See md.h for notes on the config types.
37 1.1 gwr *
38 1.1 gwr * Note that this driver provides the same functionality
39 1.1 gwr * as the MFS filesystem hack, but this is better because
40 1.1 gwr * you can use this for any filesystem type you'd like!
41 1.1 gwr *
42 1.1 gwr * Credit for most of the kmem ramdisk code goes to:
43 1.1 gwr * Leo Weppelman (atari) and Phil Nelson (pc532)
44 1.11 pk * Credit for the ideas behind the "user space memory" code goes
45 1.1 gwr * to the authors of the MFS implementation.
46 1.1 gwr */
47 1.27 lukem
48 1.27 lukem #include <sys/cdefs.h>
49 1.55 cegger __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.55 2008/06/13 20:05:06 cegger Exp $");
50 1.16 mrg
51 1.19 jonathan #include "opt_md.h"
52 1.1 gwr
53 1.1 gwr #include <sys/param.h>
54 1.7 gwr #include <sys/kernel.h>
55 1.7 gwr #include <sys/malloc.h>
56 1.1 gwr #include <sys/systm.h>
57 1.1 gwr #include <sys/buf.h>
58 1.39 yamt #include <sys/bufq.h>
59 1.1 gwr #include <sys/device.h>
60 1.4 thorpej #include <sys/disk.h>
61 1.8 leo #include <sys/proc.h>
62 1.8 leo #include <sys/conf.h>
63 1.14 leo #include <sys/disklabel.h>
64 1.23 mrg
65 1.23 mrg #include <uvm/uvm_extern.h>
66 1.1 gwr
67 1.11 pk #include <dev/md.h>
68 1.1 gwr
69 1.1 gwr /*
70 1.49 dsl * The user-space functionality is included by default.
71 1.11 pk * Use `options MEMORY_DISK_SERVER=0' to turn it off.
72 1.1 gwr */
73 1.11 pk #ifndef MEMORY_DISK_SERVER
74 1.49 dsl #error MEMORY_DISK_SERVER should be defined by opt_md.h
75 1.33 atatat #endif /* MEMORY_DISK_SERVER */
76 1.1 gwr
77 1.1 gwr /*
78 1.21 tsutsui * We should use the raw partition for ioctl.
79 1.1 gwr */
80 1.21 tsutsui #define MD_UNIT(unit) DISKUNIT(unit)
81 1.1 gwr
82 1.1 gwr /* autoconfig stuff... */
83 1.1 gwr
84 1.11 pk struct md_softc {
85 1.54 cegger device_t sc_dev; /* REQUIRED first entry */
86 1.4 thorpej struct disk sc_dkdev; /* hook for generic disk handling */
87 1.11 pk struct md_conf sc_md;
88 1.42 yamt struct bufq_state *sc_buflist;
89 1.1 gwr };
90 1.11 pk /* shorthand for fields in sc_md: */
91 1.11 pk #define sc_addr sc_md.md_addr
92 1.11 pk #define sc_size sc_md.md_size
93 1.11 pk #define sc_type sc_md.md_type
94 1.1 gwr
95 1.38 thorpej void mdattach(int);
96 1.6 thorpej
97 1.54 cegger static void md_attach(device_t, device_t, void *);
98 1.38 thorpej
99 1.38 thorpej static dev_type_open(mdopen);
100 1.38 thorpej static dev_type_close(mdclose);
101 1.38 thorpej static dev_type_read(mdread);
102 1.38 thorpej static dev_type_write(mdwrite);
103 1.38 thorpej static dev_type_ioctl(mdioctl);
104 1.38 thorpej static dev_type_strategy(mdstrategy);
105 1.38 thorpej static dev_type_size(mdsize);
106 1.31 gehenna
107 1.31 gehenna const struct bdevsw md_bdevsw = {
108 1.31 gehenna mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
109 1.31 gehenna };
110 1.31 gehenna
111 1.31 gehenna const struct cdevsw md_cdevsw = {
112 1.31 gehenna mdopen, mdclose, mdread, mdwrite, mdioctl,
113 1.32 jdolecek nostop, notty, nopoll, nommap, nokqfilter, D_DISK
114 1.31 gehenna };
115 1.31 gehenna
116 1.45 christos static struct dkdriver mddkdriver = { mdstrategy, NULL };
117 1.4 thorpej
118 1.53 drochner extern struct cfdriver md_cd;
119 1.54 cegger CFATTACH_DECL_NEW(md, sizeof(struct md_softc),
120 1.53 drochner 0, md_attach, 0, NULL);
121 1.4 thorpej
122 1.7 gwr /*
123 1.7 gwr * This is called if we are configured as a pseudo-device
124 1.7 gwr */
125 1.7 gwr void
126 1.38 thorpej mdattach(int n)
127 1.1 gwr {
128 1.7 gwr int i;
129 1.54 cegger cfdata_t cf;
130 1.7 gwr
131 1.53 drochner if (config_cfattach_attach("md", &md_ca)) {
132 1.53 drochner printf("md: cfattach_attach failed\n");
133 1.7 gwr return;
134 1.7 gwr }
135 1.7 gwr
136 1.7 gwr /* XXX: Are we supposed to provide a default? */
137 1.7 gwr if (n <= 1)
138 1.7 gwr n = 1;
139 1.7 gwr
140 1.7 gwr /* Attach as if by autoconfig. */
141 1.7 gwr for (i = 0; i < n; i++) {
142 1.53 drochner cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
143 1.53 drochner cf->cf_name = "md";
144 1.53 drochner cf->cf_atname = "md";
145 1.53 drochner cf->cf_unit = i;
146 1.53 drochner cf->cf_fstate = FSTATE_NOTFOUND;
147 1.54 cegger (void)config_attach_pseudo(cf);
148 1.7 gwr }
149 1.1 gwr }
150 1.1 gwr
151 1.1 gwr static void
152 1.54 cegger md_attach(device_t parent, device_t self,
153 1.47 christos void *aux)
154 1.1 gwr {
155 1.54 cegger struct md_softc *sc = device_private(self);
156 1.1 gwr
157 1.54 cegger sc->sc_dev = self;
158 1.42 yamt bufq_alloc(&sc->sc_buflist, "fcfs", 0);
159 1.22 thorpej
160 1.1 gwr /* XXX - Could accept aux info here to set the config. */
161 1.11 pk #ifdef MEMORY_DISK_HOOKS
162 1.1 gwr /*
163 1.1 gwr * This external function might setup a pre-loaded disk.
164 1.11 pk * All it would need to do is setup the md_conf struct.
165 1.25 tsutsui * See sys/dev/md_root.c for an example.
166 1.1 gwr */
167 1.54 cegger md_attach_hook(device_unit(self), &sc->sc_md);
168 1.1 gwr #endif
169 1.4 thorpej
170 1.4 thorpej /*
171 1.4 thorpej * Initialize and attach the disk structure.
172 1.4 thorpej */
173 1.54 cegger disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
174 1.4 thorpej disk_attach(&sc->sc_dkdev);
175 1.55 cegger
176 1.55 cegger if (!pmf_device_register(self, NULL, NULL))
177 1.55 cegger aprint_error_dev(self, "couldn't establish power handler\n");
178 1.1 gwr }
179 1.1 gwr
180 1.1 gwr /*
181 1.1 gwr * operational routines:
182 1.1 gwr * open, close, read, write, strategy,
183 1.1 gwr * ioctl, dump, size
184 1.1 gwr */
185 1.1 gwr
186 1.11 pk #if MEMORY_DISK_SERVER
187 1.38 thorpej static int md_server_loop(struct md_softc *sc);
188 1.38 thorpej static int md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
189 1.43 christos struct lwp *l);
190 1.33 atatat #endif /* MEMORY_DISK_SERVER */
191 1.38 thorpej static int md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
192 1.43 christos struct lwp *l);
193 1.1 gwr
194 1.38 thorpej static int
195 1.21 tsutsui mdsize(dev_t dev)
196 1.1 gwr {
197 1.11 pk struct md_softc *sc;
198 1.1 gwr
199 1.54 cegger sc = device_lookup_private(&md_cd, MD_UNIT(dev));
200 1.1 gwr if (sc == NULL)
201 1.1 gwr return 0;
202 1.1 gwr
203 1.11 pk if (sc->sc_type == MD_UNCONFIGURED)
204 1.1 gwr return 0;
205 1.1 gwr
206 1.1 gwr return (sc->sc_size >> DEV_BSHIFT);
207 1.1 gwr }
208 1.1 gwr
209 1.38 thorpej static int
210 1.47 christos mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
211 1.1 gwr {
212 1.21 tsutsui int unit;
213 1.11 pk struct md_softc *sc;
214 1.1 gwr
215 1.21 tsutsui unit = MD_UNIT(dev);
216 1.53 drochner sc = device_lookup_private(&md_cd, unit);
217 1.1 gwr if (sc == NULL)
218 1.1 gwr return ENXIO;
219 1.1 gwr
220 1.1 gwr /*
221 1.21 tsutsui * The raw partition is used for ioctl to configure.
222 1.1 gwr */
223 1.21 tsutsui if (DISKPART(dev) == RAW_PART)
224 1.1 gwr return 0;
225 1.1 gwr
226 1.11 pk #ifdef MEMORY_DISK_HOOKS
227 1.1 gwr /* Call the open hook to allow loading the device. */
228 1.11 pk md_open_hook(unit, &sc->sc_md);
229 1.1 gwr #endif
230 1.1 gwr
231 1.1 gwr /*
232 1.1 gwr * This is a normal, "slave" device, so
233 1.21 tsutsui * enforce initialized.
234 1.1 gwr */
235 1.11 pk if (sc->sc_type == MD_UNCONFIGURED)
236 1.1 gwr return ENXIO;
237 1.1 gwr
238 1.1 gwr return 0;
239 1.1 gwr }
240 1.1 gwr
241 1.38 thorpej static int
242 1.47 christos mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
243 1.1 gwr {
244 1.1 gwr
245 1.1 gwr return 0;
246 1.1 gwr }
247 1.1 gwr
248 1.38 thorpej static int
249 1.47 christos mdread(dev_t dev, struct uio *uio, int flags)
250 1.1 gwr {
251 1.21 tsutsui struct md_softc *sc;
252 1.21 tsutsui
253 1.54 cegger sc = device_lookup_private(&md_cd, MD_UNIT(dev));
254 1.21 tsutsui
255 1.21 tsutsui if (sc->sc_type == MD_UNCONFIGURED)
256 1.21 tsutsui return ENXIO;
257 1.21 tsutsui
258 1.11 pk return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
259 1.1 gwr }
260 1.1 gwr
261 1.38 thorpej static int
262 1.47 christos mdwrite(dev_t dev, struct uio *uio, int flags)
263 1.1 gwr {
264 1.21 tsutsui struct md_softc *sc;
265 1.21 tsutsui
266 1.54 cegger sc = device_lookup_private(&md_cd, MD_UNIT(dev));
267 1.21 tsutsui
268 1.21 tsutsui if (sc->sc_type == MD_UNCONFIGURED)
269 1.21 tsutsui return ENXIO;
270 1.21 tsutsui
271 1.11 pk return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
272 1.1 gwr }
273 1.1 gwr
274 1.1 gwr /*
275 1.1 gwr * Handle I/O requests, either directly, or
276 1.1 gwr * by passing them to the server process.
277 1.1 gwr */
278 1.38 thorpej static void
279 1.38 thorpej mdstrategy(struct buf *bp)
280 1.1 gwr {
281 1.11 pk struct md_softc *sc;
282 1.48 christos void * addr;
283 1.21 tsutsui size_t off, xfer;
284 1.1 gwr
285 1.54 cegger sc = device_lookup_private(&md_cd, MD_UNIT(bp->b_dev));
286 1.1 gwr
287 1.21 tsutsui if (sc->sc_type == MD_UNCONFIGURED) {
288 1.21 tsutsui bp->b_error = ENXIO;
289 1.21 tsutsui goto done;
290 1.21 tsutsui }
291 1.21 tsutsui
292 1.1 gwr switch (sc->sc_type) {
293 1.11 pk #if MEMORY_DISK_SERVER
294 1.11 pk case MD_UMEM_SERVER:
295 1.1 gwr /* Just add this job to the server's queue. */
296 1.42 yamt BUFQ_PUT(sc->sc_buflist, bp);
297 1.48 christos wakeup((void *)sc);
298 1.29 hannken /* see md_server_loop() */
299 1.1 gwr /* no biodone in this case */
300 1.1 gwr return;
301 1.11 pk #endif /* MEMORY_DISK_SERVER */
302 1.1 gwr
303 1.11 pk case MD_KMEM_FIXED:
304 1.11 pk case MD_KMEM_ALLOCATED:
305 1.1 gwr /* These are in kernel space. Access directly. */
306 1.1 gwr bp->b_resid = bp->b_bcount;
307 1.1 gwr off = (bp->b_blkno << DEV_BSHIFT);
308 1.1 gwr if (off >= sc->sc_size) {
309 1.1 gwr if (bp->b_flags & B_READ)
310 1.1 gwr break; /* EOF */
311 1.1 gwr goto set_eio;
312 1.1 gwr }
313 1.1 gwr xfer = bp->b_resid;
314 1.1 gwr if (xfer > (sc->sc_size - off))
315 1.1 gwr xfer = (sc->sc_size - off);
316 1.48 christos addr = (char *)sc->sc_addr + off;
317 1.1 gwr if (bp->b_flags & B_READ)
318 1.26 thorpej memcpy(bp->b_data, addr, xfer);
319 1.1 gwr else
320 1.26 thorpej memcpy(addr, bp->b_data, xfer);
321 1.1 gwr bp->b_resid -= xfer;
322 1.1 gwr break;
323 1.1 gwr
324 1.1 gwr default:
325 1.1 gwr bp->b_resid = bp->b_bcount;
326 1.1 gwr set_eio:
327 1.1 gwr bp->b_error = EIO;
328 1.1 gwr break;
329 1.1 gwr }
330 1.21 tsutsui done:
331 1.1 gwr biodone(bp);
332 1.1 gwr }
333 1.1 gwr
334 1.38 thorpej static int
335 1.48 christos mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
336 1.1 gwr {
337 1.21 tsutsui struct md_softc *sc;
338 1.21 tsutsui struct md_conf *umd;
339 1.1 gwr
340 1.54 cegger sc = device_lookup_private(&md_cd, MD_UNIT(dev));
341 1.1 gwr
342 1.21 tsutsui /* If this is not the raw partition, punt! */
343 1.21 tsutsui if (DISKPART(dev) != RAW_PART)
344 1.1 gwr return ENOTTY;
345 1.1 gwr
346 1.11 pk umd = (struct md_conf *)data;
347 1.1 gwr switch (cmd) {
348 1.11 pk case MD_GETCONF:
349 1.11 pk *umd = sc->sc_md;
350 1.1 gwr return 0;
351 1.1 gwr
352 1.11 pk case MD_SETCONF:
353 1.1 gwr /* Can only set it once. */
354 1.11 pk if (sc->sc_type != MD_UNCONFIGURED)
355 1.1 gwr break;
356 1.11 pk switch (umd->md_type) {
357 1.11 pk case MD_KMEM_ALLOCATED:
358 1.43 christos return md_ioctl_kalloc(sc, umd, l);
359 1.11 pk #if MEMORY_DISK_SERVER
360 1.11 pk case MD_UMEM_SERVER:
361 1.43 christos return md_ioctl_server(sc, umd, l);
362 1.33 atatat #endif /* MEMORY_DISK_SERVER */
363 1.1 gwr default:
364 1.1 gwr break;
365 1.1 gwr }
366 1.1 gwr break;
367 1.1 gwr }
368 1.1 gwr return EINVAL;
369 1.1 gwr }
370 1.1 gwr
371 1.1 gwr /*
372 1.11 pk * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
373 1.1 gwr * Just allocate some kernel memory and return.
374 1.1 gwr */
375 1.8 leo static int
376 1.46 christos md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
377 1.47 christos struct lwp *l)
378 1.1 gwr {
379 1.17 eeh vaddr_t addr;
380 1.21 tsutsui vsize_t size;
381 1.1 gwr
382 1.1 gwr /* Sanity check the size. */
383 1.11 pk size = umd->md_size;
384 1.41 yamt addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
385 1.1 gwr if (!addr)
386 1.1 gwr return ENOMEM;
387 1.1 gwr
388 1.1 gwr /* This unit is now configured. */
389 1.48 christos sc->sc_addr = (void *)addr; /* kernel space */
390 1.1 gwr sc->sc_size = (size_t)size;
391 1.11 pk sc->sc_type = MD_KMEM_ALLOCATED;
392 1.1 gwr return 0;
393 1.40 perry }
394 1.1 gwr
395 1.11 pk #if MEMORY_DISK_SERVER
396 1.1 gwr
397 1.1 gwr /*
398 1.11 pk * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
399 1.1 gwr * Set config, then become the I/O server for this unit.
400 1.1 gwr */
401 1.8 leo static int
402 1.46 christos md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
403 1.47 christos struct lwp *l)
404 1.1 gwr {
405 1.17 eeh vaddr_t end;
406 1.1 gwr int error;
407 1.1 gwr
408 1.1 gwr /* Sanity check addr, size. */
409 1.48 christos end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
410 1.1 gwr
411 1.1 gwr if ((end >= VM_MAXUSER_ADDRESS) ||
412 1.17 eeh (end < ((vaddr_t) umd->md_addr)) )
413 1.1 gwr return EINVAL;
414 1.1 gwr
415 1.1 gwr /* This unit is now configured. */
416 1.11 pk sc->sc_addr = umd->md_addr; /* user space */
417 1.11 pk sc->sc_size = umd->md_size;
418 1.11 pk sc->sc_type = MD_UMEM_SERVER;
419 1.1 gwr
420 1.1 gwr /* Become the server daemon */
421 1.11 pk error = md_server_loop(sc);
422 1.1 gwr
423 1.1 gwr /* This server is now going away! */
424 1.11 pk sc->sc_type = MD_UNCONFIGURED;
425 1.1 gwr sc->sc_addr = 0;
426 1.1 gwr sc->sc_size = 0;
427 1.1 gwr
428 1.1 gwr return (error);
429 1.40 perry }
430 1.1 gwr
431 1.38 thorpej static int md_sleep_pri = PWAIT | PCATCH;
432 1.1 gwr
433 1.1 gwr static int
434 1.38 thorpej md_server_loop(struct md_softc *sc)
435 1.1 gwr {
436 1.1 gwr struct buf *bp;
437 1.48 christos void *addr; /* user space address */
438 1.21 tsutsui size_t off; /* offset into "device" */
439 1.21 tsutsui size_t xfer; /* amount to transfer */
440 1.1 gwr int error;
441 1.1 gwr
442 1.1 gwr for (;;) {
443 1.1 gwr /* Wait for some work to arrive. */
444 1.42 yamt while ((bp = BUFQ_GET(sc->sc_buflist)) == NULL) {
445 1.48 christos error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
446 1.1 gwr if (error)
447 1.1 gwr return error;
448 1.1 gwr }
449 1.1 gwr
450 1.1 gwr /* Do the transfer to/from user space. */
451 1.1 gwr error = 0;
452 1.1 gwr bp->b_resid = bp->b_bcount;
453 1.1 gwr off = (bp->b_blkno << DEV_BSHIFT);
454 1.1 gwr if (off >= sc->sc_size) {
455 1.1 gwr if (bp->b_flags & B_READ)
456 1.1 gwr goto done; /* EOF (not an error) */
457 1.1 gwr error = EIO;
458 1.1 gwr goto done;
459 1.1 gwr }
460 1.1 gwr xfer = bp->b_resid;
461 1.1 gwr if (xfer > (sc->sc_size - off))
462 1.1 gwr xfer = (sc->sc_size - off);
463 1.48 christos addr = (char *)sc->sc_addr + off;
464 1.1 gwr if (bp->b_flags & B_READ)
465 1.1 gwr error = copyin(addr, bp->b_data, xfer);
466 1.1 gwr else
467 1.1 gwr error = copyout(bp->b_data, addr, xfer);
468 1.1 gwr if (!error)
469 1.1 gwr bp->b_resid -= xfer;
470 1.1 gwr
471 1.1 gwr done:
472 1.1 gwr if (error) {
473 1.1 gwr bp->b_error = error;
474 1.1 gwr }
475 1.1 gwr biodone(bp);
476 1.1 gwr }
477 1.1 gwr }
478 1.11 pk #endif /* MEMORY_DISK_SERVER */
479