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