md.c revision 1.62.2.2 1 /* $NetBSD: md.c,v 1.62.2.2 2010/11/18 16:09:46 uebayasi 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * This implements a general-purpose memory-disk.
30 * See md.h for notes on the config types.
31 *
32 * Note that this driver provides the same functionality
33 * as the MFS filesystem hack, but this is better because
34 * you can use this for any filesystem type you'd like!
35 *
36 * Credit for most of the kmem ramdisk code goes to:
37 * Leo Weppelman (atari) and Phil Nelson (pc532)
38 * Credit for the ideas behind the "user space memory" code goes
39 * to the authors of the MFS implementation.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.62.2.2 2010/11/18 16:09:46 uebayasi Exp $");
44
45 #include "opt_md.h"
46 #include "opt_xip.h"
47
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/systm.h>
52 #include <sys/buf.h>
53 #include <sys/bufq.h>
54 #include <sys/device.h>
55 #include <sys/disk.h>
56 #include <sys/stat.h>
57 #include <sys/proc.h>
58 #include <sys/conf.h>
59 #include <sys/disklabel.h>
60
61 #include <uvm/uvm_extern.h>
62
63 #include <dev/md.h>
64
65 /*
66 * The user-space functionality is included by default.
67 * Use `options MEMORY_DISK_SERVER=0' to turn it off.
68 */
69 #ifndef MEMORY_DISK_SERVER
70 #error MEMORY_DISK_SERVER should be defined by opt_md.h
71 #endif /* MEMORY_DISK_SERVER */
72
73 /*
74 * We should use the raw partition for ioctl.
75 */
76 #define MD_UNIT(unit) DISKUNIT(unit)
77
78 /* autoconfig stuff... */
79
80 struct md_softc {
81 device_t sc_dev; /* Self. */
82 struct disk sc_dkdev; /* hook for generic disk handling */
83 struct md_conf sc_md;
84 struct bufq_state *sc_buflist;
85 };
86 /* shorthand for fields in sc_md: */
87 #define sc_addr sc_md.md_addr
88 #define sc_size sc_md.md_size
89 #define sc_type sc_md.md_type
90
91 void mdattach(int);
92
93 static void md_attach(device_t, device_t, void *);
94 static int md_detach(device_t, int);
95
96 static dev_type_open(mdopen);
97 static dev_type_close(mdclose);
98 static dev_type_read(mdread);
99 static dev_type_write(mdwrite);
100 static dev_type_ioctl(mdioctl);
101 static dev_type_strategy(mdstrategy);
102 static dev_type_size(mdsize);
103 static dev_type_mmap(mdmmap);
104
105 const struct bdevsw md_bdevsw = {
106 mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
107 };
108
109 const struct cdevsw md_cdevsw = {
110 mdopen, mdclose, mdread, mdwrite, mdioctl,
111 nostop, notty, nopoll, mdmmap, nokqfilter, D_DISK
112 };
113
114 static struct dkdriver mddkdriver = { mdstrategy, NULL };
115
116 extern struct cfdriver md_cd;
117 CFATTACH_DECL3_NEW(md, sizeof(struct md_softc),
118 0, md_attach, md_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
119
120 static kmutex_t md_device_lock; /* Protect unit creation / deletion. */
121 extern size_t md_root_size;
122
123 static void md_set_disklabel(struct md_softc *);
124
125 /*
126 * This is called if we are configured as a pseudo-device
127 */
128 void
129 mdattach(int n)
130 {
131
132 mutex_init(&md_device_lock, MUTEX_DEFAULT, IPL_NONE);
133 if (config_cfattach_attach(md_cd.cd_name, &md_ca)) {
134 aprint_error("%s: cfattach_attach failed\n", md_cd.cd_name);
135 return;
136 }
137 }
138
139 static void
140 md_attach(device_t parent, device_t self, void *aux)
141 {
142 struct md_softc *sc = device_private(self);
143
144 sc->sc_dev = self;
145 bufq_alloc(&sc->sc_buflist, "fcfs", 0);
146
147 /* XXX - Could accept aux info here to set the config. */
148 #ifdef MEMORY_DISK_HOOKS
149 /*
150 * This external function might setup a pre-loaded disk.
151 * All it would need to do is setup the md_conf struct.
152 * See sys/dev/md_root.c for an example.
153 */
154 md_attach_hook(device_unit(self), &sc->sc_md);
155 #endif
156
157 /*
158 * Initialize and attach the disk structure.
159 */
160 disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
161 disk_attach(&sc->sc_dkdev);
162
163 if (sc->sc_type != MD_UNCONFIGURED)
164 md_set_disklabel(sc);
165
166 if (!pmf_device_register(self, NULL, NULL))
167 aprint_error_dev(self, "couldn't establish power handler\n");
168 }
169
170 static int
171 md_detach(device_t self, int flags)
172 {
173 struct md_softc *sc = device_private(self);
174 int rc;
175
176 rc = 0;
177 mutex_enter(&sc->sc_dkdev.dk_openlock);
178 if (sc->sc_dkdev.dk_openmask == 0)
179 ; /* nothing to do */
180 else if ((flags & DETACH_FORCE) == 0)
181 rc = EBUSY;
182 mutex_exit(&sc->sc_dkdev.dk_openlock);
183
184 if (rc != 0)
185 return rc;
186
187 pmf_device_deregister(self);
188 disk_detach(&sc->sc_dkdev);
189 disk_destroy(&sc->sc_dkdev);
190 bufq_free(sc->sc_buflist);
191 return 0;
192 }
193
194 /*
195 * operational routines:
196 * open, close, read, write, strategy,
197 * ioctl, dump, size
198 */
199
200 #if MEMORY_DISK_SERVER
201 static int md_server_loop(struct md_softc *sc);
202 static int md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
203 struct lwp *l);
204 #endif /* MEMORY_DISK_SERVER */
205 static int md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
206 struct lwp *l);
207
208 static int
209 mdsize(dev_t dev)
210 {
211 struct md_softc *sc;
212
213 sc = device_lookup_private(&md_cd, MD_UNIT(dev));
214 if (sc == NULL)
215 return 0;
216
217 if (sc->sc_type == MD_UNCONFIGURED)
218 return 0;
219
220 return (sc->sc_size >> DEV_BSHIFT);
221 }
222
223 static int
224 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
225 {
226 int unit;
227 int part = DISKPART(dev);
228 int pmask = 1 << part;
229 cfdata_t cf;
230 struct md_softc *sc;
231 struct disk *dk;
232
233 mutex_enter(&md_device_lock);
234 unit = MD_UNIT(dev);
235 sc = device_lookup_private(&md_cd, unit);
236 if (sc == NULL) {
237 if (part != RAW_PART) {
238 mutex_exit(&md_device_lock);
239 return ENXIO;
240 }
241 cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
242 cf->cf_name = md_cd.cd_name;
243 cf->cf_atname = md_cd.cd_name;
244 cf->cf_unit = unit;
245 cf->cf_fstate = FSTATE_STAR;
246 sc = device_private(config_attach_pseudo(cf));
247 if (sc == NULL) {
248 mutex_exit(&md_device_lock);
249 return ENOMEM;
250 }
251 }
252
253 dk = &sc->sc_dkdev;
254
255 /*
256 * The raw partition is used for ioctl to configure.
257 */
258 if (part == RAW_PART)
259 goto ok;
260
261 #ifdef MEMORY_DISK_HOOKS
262 /* Call the open hook to allow loading the device. */
263 md_open_hook(unit, &sc->sc_md);
264 #endif
265
266 /*
267 * This is a normal, "slave" device, so
268 * enforce initialized.
269 */
270 if (sc->sc_type == MD_UNCONFIGURED) {
271 mutex_exit(&md_device_lock);
272 return ENXIO;
273 }
274
275 ok:
276 /* XXX duplicates code in dk_open(). Call dk_open(), instead? */
277 mutex_enter(&dk->dk_openlock);
278 /* Mark our unit as open. */
279 switch (fmt) {
280 case S_IFCHR:
281 dk->dk_copenmask |= pmask;
282 break;
283 case S_IFBLK:
284 dk->dk_bopenmask |= pmask;
285 break;
286 }
287
288 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
289
290 mutex_exit(&dk->dk_openlock);
291 mutex_exit(&md_device_lock);
292 return 0;
293 }
294
295 static int
296 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
297 {
298 int part = DISKPART(dev);
299 int pmask = 1 << part;
300 int error;
301 cfdata_t cf;
302 struct md_softc *sc;
303 struct disk *dk;
304
305 sc = device_lookup_private(&md_cd, MD_UNIT(dev));
306 if (sc == NULL)
307 return ENXIO;
308
309 dk = &sc->sc_dkdev;
310
311 mutex_enter(&dk->dk_openlock);
312
313 switch (fmt) {
314 case S_IFCHR:
315 dk->dk_copenmask &= ~pmask;
316 break;
317 case S_IFBLK:
318 dk->dk_bopenmask &= ~pmask;
319 break;
320 }
321 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
322
323 mutex_exit(&dk->dk_openlock);
324
325 mutex_enter(&md_device_lock);
326 cf = device_cfdata(sc->sc_dev);
327 error = config_detach(sc->sc_dev, DETACH_QUIET);
328 if (! error)
329 free(cf, M_DEVBUF);
330 mutex_exit(&md_device_lock);
331 return error;
332 }
333
334 static int
335 mdread(dev_t dev, struct uio *uio, int flags)
336 {
337 struct md_softc *sc;
338
339 sc = device_lookup_private(&md_cd, MD_UNIT(dev));
340
341 if (sc == NULL || sc->sc_type == MD_UNCONFIGURED)
342 return ENXIO;
343
344 return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
345 }
346
347 static int
348 mdwrite(dev_t dev, struct uio *uio, int flags)
349 {
350 struct md_softc *sc;
351
352 sc = device_lookup_private(&md_cd, MD_UNIT(dev));
353
354 if (sc == NULL || sc->sc_type == MD_UNCONFIGURED)
355 return ENXIO;
356
357 return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
358 }
359
360 /*
361 * Handle I/O requests, either directly, or
362 * by passing them to the server process.
363 */
364 static void
365 mdstrategy(struct buf *bp)
366 {
367 struct md_softc *sc;
368 void * addr;
369 size_t off, xfer;
370 bool is_read;
371
372 sc = device_lookup_private(&md_cd, MD_UNIT(bp->b_dev));
373
374 if (sc == NULL || sc->sc_type == MD_UNCONFIGURED) {
375 bp->b_error = ENXIO;
376 goto done;
377 }
378
379 switch (sc->sc_type) {
380 #if MEMORY_DISK_SERVER
381 case MD_UMEM_SERVER:
382 /* Just add this job to the server's queue. */
383 bufq_put(sc->sc_buflist, bp);
384 wakeup((void *)sc);
385 /* see md_server_loop() */
386 /* no biodone in this case */
387 return;
388 #endif /* MEMORY_DISK_SERVER */
389
390 case MD_KMEM_FIXED:
391 case MD_KMEM_ALLOCATED:
392 /* These are in kernel space. Access directly. */
393 is_read = ((bp->b_flags & B_READ) == B_READ);
394 bp->b_resid = bp->b_bcount;
395 off = (bp->b_blkno << DEV_BSHIFT);
396 if (off >= sc->sc_size) {
397 if (is_read)
398 break; /* EOF */
399 goto set_eio;
400 }
401 xfer = bp->b_resid;
402 if (xfer > (sc->sc_size - off))
403 xfer = (sc->sc_size - off);
404 addr = (char *)sc->sc_addr + off;
405 disk_busy(&sc->sc_dkdev);
406 if (is_read)
407 memcpy(bp->b_data, addr, xfer);
408 else
409 memcpy(addr, bp->b_data, xfer);
410 disk_unbusy(&sc->sc_dkdev, xfer, is_read);
411 bp->b_resid -= xfer;
412 break;
413
414 default:
415 bp->b_resid = bp->b_bcount;
416 set_eio:
417 bp->b_error = EIO;
418 break;
419 }
420 done:
421 biodone(bp);
422 }
423
424 static int
425 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
426 {
427 struct md_softc *sc;
428 struct md_conf *umd;
429 struct disklabel *lp;
430 struct partinfo *pp;
431
432 if ((sc = device_lookup_private(&md_cd, MD_UNIT(dev))) == NULL)
433 return ENXIO;
434
435 if (sc->sc_type != MD_UNCONFIGURED) {
436 switch (cmd) {
437 case DIOCGDINFO:
438 lp = (struct disklabel *)data;
439 *lp = *sc->sc_dkdev.dk_label;
440 return 0;
441
442 case DIOCGPART:
443 pp = (struct partinfo *)data;
444 pp->disklab = sc->sc_dkdev.dk_label;
445 pp->part =
446 &sc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
447 return 0;
448
449 #if defined(XIP) && !defined(XIP_CDEV_MMAP)
450 case DIOCGPHYSSEG:
451 {
452 int error = 0;
453
454 if (sc->sc_md.md_phys == NULL)
455 error = EINVAL;
456 else
457 *(void **)data = sc->sc_md.md_phys;
458 return error;
459 }
460 #endif
461 }
462 }
463
464 /* If this is not the raw partition, punt! */
465 if (DISKPART(dev) != RAW_PART)
466 return ENOTTY;
467
468 umd = (struct md_conf *)data;
469 switch (cmd) {
470 case MD_GETCONF:
471 *umd = sc->sc_md;
472 return 0;
473
474 case MD_SETCONF:
475 /* Can only set it once. */
476 if (sc->sc_type != MD_UNCONFIGURED)
477 break;
478 switch (umd->md_type) {
479 case MD_KMEM_ALLOCATED:
480 return md_ioctl_kalloc(sc, umd, l);
481 #if MEMORY_DISK_SERVER
482 case MD_UMEM_SERVER:
483 return md_ioctl_server(sc, umd, l);
484 #endif /* MEMORY_DISK_SERVER */
485 default:
486 break;
487 }
488 break;
489 }
490 return EINVAL;
491 }
492
493 paddr_t
494 mdmmap(dev_t dev, off_t off, int flags)
495 {
496
497 #if defined(MEMORY_DISK_HOOKS) && defined(XIP)
498 return md_mmap_hook(dev, off, flags);
499 #else
500 return -1;
501 #endif
502 }
503
504 static void
505 md_set_disklabel(struct md_softc *sc)
506 {
507 struct disklabel *lp = sc->sc_dkdev.dk_label;
508 struct partition *pp;
509
510 memset(lp, 0, sizeof(*lp));
511
512 lp->d_secsize = DEV_BSIZE;
513 lp->d_secperunit = sc->sc_size / DEV_BSIZE;
514 if (lp->d_secperunit >= (32*64)) {
515 lp->d_nsectors = 32;
516 lp->d_ntracks = 64;
517 lp->d_ncylinders = lp->d_secperunit / (32*64);
518 } else {
519 lp->d_nsectors = 1;
520 lp->d_ntracks = 1;
521 lp->d_ncylinders = lp->d_secperunit;
522 }
523 lp->d_secpercyl = lp->d_ntracks*lp->d_nsectors;
524
525 strncpy(lp->d_typename, md_cd.cd_name, sizeof(lp->d_typename));
526 lp->d_type = DTYPE_UNKNOWN;
527 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
528 lp->d_rpm = 3600;
529 lp->d_interleave = 1;
530 lp->d_flags = 0;
531
532 pp = &lp->d_partitions[0];
533 pp->p_offset = 0;
534 pp->p_size = lp->d_secperunit;
535 pp->p_fstype = FS_BSDFFS;
536
537 pp = &lp->d_partitions[RAW_PART];
538 pp->p_offset = 0;
539 pp->p_size = lp->d_secperunit;
540 pp->p_fstype = FS_UNUSED;
541
542 lp->d_npartitions = RAW_PART+1;
543 lp->d_magic = DISKMAGIC;
544 lp->d_magic2 = DISKMAGIC;
545 lp->d_checksum = dkcksum(lp);
546 }
547
548 /*
549 * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
550 * Just allocate some kernel memory and return.
551 */
552 static int
553 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
554 struct lwp *l)
555 {
556 vaddr_t addr;
557 vsize_t size;
558
559 /* Sanity check the size. */
560 size = umd->md_size;
561 addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
562 if (!addr)
563 return ENOMEM;
564
565 /* This unit is now configured. */
566 sc->sc_addr = (void *)addr; /* kernel space */
567 sc->sc_size = (size_t)size;
568 sc->sc_type = MD_KMEM_ALLOCATED;
569 md_set_disklabel(sc);
570 return 0;
571 }
572
573 #if MEMORY_DISK_SERVER
574
575 /*
576 * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
577 * Set config, then become the I/O server for this unit.
578 */
579 static int
580 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
581 struct lwp *l)
582 {
583 vaddr_t end;
584 int error;
585
586 /* Sanity check addr, size. */
587 end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
588
589 if ((end >= VM_MAXUSER_ADDRESS) ||
590 (end < ((vaddr_t) umd->md_addr)) )
591 return EINVAL;
592
593 /* This unit is now configured. */
594 sc->sc_addr = umd->md_addr; /* user space */
595 sc->sc_size = umd->md_size;
596 sc->sc_type = MD_UMEM_SERVER;
597 md_set_disklabel(sc);
598
599 /* Become the server daemon */
600 error = md_server_loop(sc);
601
602 /* This server is now going away! */
603 sc->sc_type = MD_UNCONFIGURED;
604 sc->sc_addr = 0;
605 sc->sc_size = 0;
606
607 return (error);
608 }
609
610 static int md_sleep_pri = PWAIT | PCATCH;
611
612 static int
613 md_server_loop(struct md_softc *sc)
614 {
615 struct buf *bp;
616 void *addr; /* user space address */
617 size_t off; /* offset into "device" */
618 size_t xfer; /* amount to transfer */
619 int error;
620 bool is_read;
621
622 for (;;) {
623 /* Wait for some work to arrive. */
624 while ((bp = bufq_get(sc->sc_buflist)) == NULL) {
625 error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
626 if (error)
627 return error;
628 }
629
630 /* Do the transfer to/from user space. */
631 error = 0;
632 is_read = ((bp->b_flags & B_READ) == B_READ);
633 bp->b_resid = bp->b_bcount;
634 off = (bp->b_blkno << DEV_BSHIFT);
635 if (off >= sc->sc_size) {
636 if (is_read)
637 goto done; /* EOF (not an error) */
638 error = EIO;
639 goto done;
640 }
641 xfer = bp->b_resid;
642 if (xfer > (sc->sc_size - off))
643 xfer = (sc->sc_size - off);
644 addr = (char *)sc->sc_addr + off;
645 disk_busy(&sc->sc_dkdev);
646 if (is_read)
647 error = copyin(addr, bp->b_data, xfer);
648 else
649 error = copyout(bp->b_data, addr, xfer);
650 disk_unbusy(&sc->sc_dkdev, (error ? 0 : xfer), is_read);
651 if (!error)
652 bp->b_resid -= xfer;
653
654 done:
655 if (error) {
656 bp->b_error = error;
657 }
658 biodone(bp);
659 }
660 }
661 #endif /* MEMORY_DISK_SERVER */
662