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