xref: /netbsd/sys/dev/md.c (revision bf9ec67e)
1 /*	$NetBSD: md.c,v 1.28 2002/01/13 19:28:07 tsutsui 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.28 2002/01/13 19:28:07 tsutsui 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/device.h>
59 #include <sys/disk.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  * By default, include the user-space functionality.
70  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
71  */
72 #ifndef MEMORY_DISK_SERVER
73 #define	MEMORY_DISK_SERVER 1
74 #endif
75 
76 /*
77  * We should use the raw partition for ioctl.
78  */
79 #define MD_MAX_UNITS	0x10
80 #define MD_UNIT(unit)	DISKUNIT(unit)
81 
82 /* autoconfig stuff... */
83 
84 struct md_softc {
85 	struct device sc_dev;	/* REQUIRED first entry */
86 	struct disk sc_dkdev;	/* hook for generic disk handling */
87 	struct md_conf sc_md;
88 	struct buf_queue 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 __P((int));
96 static void md_attach __P((struct device *, struct device *, void *));
97 
98 void mdstrategy __P((struct buf *bp));
99 struct dkdriver mddkdriver = { mdstrategy };
100 
101 static int   ramdisk_ndevs;
102 static void *ramdisk_devs[MD_MAX_UNITS];
103 
104 /*
105  * This is called if we are configured as a pseudo-device
106  */
107 void
108 mdattach(n)
109 	int n;
110 {
111 	struct md_softc *sc;
112 	int i;
113 
114 #ifdef	DIAGNOSTIC
115 	if (ramdisk_ndevs) {
116 		printf("ramdisk: multiple attach calls?\n");
117 		return;
118 	}
119 #endif
120 
121 	/* XXX:  Are we supposed to provide a default? */
122 	if (n <= 1)
123 		n = 1;
124 	if (n > MD_MAX_UNITS)
125 		n = MD_MAX_UNITS;
126 	ramdisk_ndevs = n;
127 
128 	/* Attach as if by autoconfig. */
129 	for (i = 0; i < n; i++) {
130 
131 		sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO);
132 		if (!sc) {
133 			printf("ramdisk: malloc for attach failed!\n");
134 			return;
135 		}
136 		ramdisk_devs[i] = sc;
137 		sc->sc_dev.dv_unit = i;
138 		sprintf(sc->sc_dev.dv_xname, "md%d", i);
139 		md_attach(NULL, &sc->sc_dev, NULL);
140 	}
141 }
142 
143 static void
144 md_attach(parent, self, aux)
145 	struct device	*parent, *self;
146 	void		*aux;
147 {
148 	struct md_softc *sc = (struct md_softc *)self;
149 
150 	BUFQ_INIT(&sc->sc_buflist);
151 
152 	/* XXX - Could accept aux info here to set the config. */
153 #ifdef	MEMORY_DISK_HOOKS
154 	/*
155 	 * This external function might setup a pre-loaded disk.
156 	 * All it would need to do is setup the md_conf struct.
157 	 * See sys/dev/md_root.c for an example.
158 	 */
159 	md_attach_hook(sc->sc_dev.dv_unit, &sc->sc_md);
160 #endif
161 
162 	/*
163 	 * Initialize and attach the disk structure.
164 	 */
165 	sc->sc_dkdev.dk_driver = &mddkdriver;
166 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
167 	disk_attach(&sc->sc_dkdev);
168 }
169 
170 /*
171  * operational routines:
172  * open, close, read, write, strategy,
173  * ioctl, dump, size
174  */
175 
176 #if MEMORY_DISK_SERVER
177 static int md_server_loop __P((struct md_softc *sc));
178 static int md_ioctl_server __P((struct md_softc *sc,
179 		struct md_conf *umd, struct proc *proc));
180 #endif
181 static int md_ioctl_kalloc __P((struct md_softc *sc,
182 		struct md_conf *umd, struct proc *proc));
183 
184 dev_type_open(mdopen);
185 dev_type_close(mdclose);
186 dev_type_read(mdread);
187 dev_type_write(mdwrite);
188 dev_type_ioctl(mdioctl);
189 dev_type_size(mdsize);
190 dev_type_dump(mddump);
191 
192 int
193 mddump(dev, blkno, va, size)
194 	dev_t dev;
195 	daddr_t blkno;
196 	caddr_t va;
197 	size_t size;
198 {
199 	return ENODEV;
200 }
201 
202 int
203 mdsize(dev_t dev)
204 {
205 	int unit;
206 	struct md_softc *sc;
207 
208 	unit = MD_UNIT(dev);
209 	if (unit >= ramdisk_ndevs)
210 		return 0;
211 	sc = ramdisk_devs[unit];
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 int
222 mdopen(dev, flag, fmt, proc)
223 	dev_t dev;
224 	int flag, fmt;
225 	struct proc *proc;
226 {
227 	int unit;
228 	struct md_softc *sc;
229 
230 	unit = MD_UNIT(dev);
231 	if (unit >= ramdisk_ndevs)
232 		return ENXIO;
233 	sc = ramdisk_devs[unit];
234 	if (sc == NULL)
235 		return ENXIO;
236 
237 	/*
238 	 * The raw partition is used for ioctl to configure.
239 	 */
240 	if (DISKPART(dev) == RAW_PART)
241 		return 0;
242 
243 #ifdef	MEMORY_DISK_HOOKS
244 	/* Call the open hook to allow loading the device. */
245 	md_open_hook(unit, &sc->sc_md);
246 #endif
247 
248 	/*
249 	 * This is a normal, "slave" device, so
250 	 * enforce initialized.
251 	 */
252 	if (sc->sc_type == MD_UNCONFIGURED)
253 		return ENXIO;
254 
255 	return 0;
256 }
257 
258 int
259 mdclose(dev, flag, fmt, proc)
260 	dev_t dev;
261 	int flag, fmt;
262 	struct proc *proc;
263 {
264 	int unit;
265 
266 	unit = MD_UNIT(dev);
267 
268 	if (unit >= ramdisk_ndevs)
269 		return ENXIO;
270 
271 	return 0;
272 }
273 
274 int
275 mdread(dev, uio, flags)
276 	dev_t dev;
277 	struct uio *uio;
278 	int flags;
279 {
280 	int unit;
281 	struct md_softc *sc;
282 
283 	unit = MD_UNIT(dev);
284 
285 	if (unit >= ramdisk_ndevs)
286 		return ENXIO;
287 
288 	sc = ramdisk_devs[unit];
289 
290 	if (sc->sc_type == MD_UNCONFIGURED)
291 		return ENXIO;
292 
293 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
294 }
295 
296 int
297 mdwrite(dev, uio, flags)
298 	dev_t dev;
299 	struct uio *uio;
300 	int flags;
301 {
302 	int unit;
303 	struct md_softc *sc;
304 
305 	unit = MD_UNIT(dev);
306 
307 	if (unit >= ramdisk_ndevs)
308 		return ENXIO;
309 
310 	sc = ramdisk_devs[unit];
311 
312 	if (sc->sc_type == MD_UNCONFIGURED)
313 		return ENXIO;
314 
315 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
316 }
317 
318 /*
319  * Handle I/O requests, either directly, or
320  * by passing them to the server process.
321  */
322 void
323 mdstrategy(bp)
324 	struct buf *bp;
325 {
326 	int unit;
327 	struct md_softc	*sc;
328 	caddr_t	addr;
329 	size_t off, xfer;
330 
331 	unit = MD_UNIT(bp->b_dev);
332 	sc = ramdisk_devs[unit];
333 
334 	if (sc->sc_type == MD_UNCONFIGURED) {
335 		bp->b_error = ENXIO;
336 		bp->b_flags |= B_ERROR;
337 		goto done;
338 	}
339 
340 	switch (sc->sc_type) {
341 #if MEMORY_DISK_SERVER
342 	case MD_UMEM_SERVER:
343 		/* Just add this job to the server's queue. */
344 		BUFQ_INSERT_TAIL(&sc->sc_buflist, bp);
345 		if (BUFQ_FIRST(&sc->sc_buflist) == bp) {
346 			/* server queue was empty. */
347 			wakeup((caddr_t)sc);
348 			/* see md_server_loop() */
349 		}
350 		/* no biodone in this case */
351 		return;
352 #endif	/* MEMORY_DISK_SERVER */
353 
354 	case MD_KMEM_FIXED:
355 	case MD_KMEM_ALLOCATED:
356 		/* These are in kernel space.  Access directly. */
357 		bp->b_resid = bp->b_bcount;
358 		off = (bp->b_blkno << DEV_BSHIFT);
359 		if (off >= sc->sc_size) {
360 			if (bp->b_flags & B_READ)
361 				break;	/* EOF */
362 			goto set_eio;
363 		}
364 		xfer = bp->b_resid;
365 		if (xfer > (sc->sc_size - off))
366 			xfer = (sc->sc_size - off);
367 		addr = sc->sc_addr + off;
368 		if (bp->b_flags & B_READ)
369 			memcpy(bp->b_data, addr, xfer);
370 		else
371 			memcpy(addr, bp->b_data, xfer);
372 		bp->b_resid -= xfer;
373 		break;
374 
375 	default:
376 		bp->b_resid = bp->b_bcount;
377 	set_eio:
378 		bp->b_error = EIO;
379 		bp->b_flags |= B_ERROR;
380 		break;
381 	}
382  done:
383 	biodone(bp);
384 }
385 
386 int
387 mdioctl(dev, cmd, data, flag, proc)
388 	dev_t dev;
389 	u_long cmd;
390 	int flag;
391 	caddr_t data;
392 	struct proc *proc;
393 {
394 	int unit;
395 	struct md_softc *sc;
396 	struct md_conf *umd;
397 
398 	unit = MD_UNIT(dev);
399 	sc = ramdisk_devs[unit];
400 
401 	/* If this is not the raw partition, punt! */
402 	if (DISKPART(dev) != RAW_PART)
403 		return ENOTTY;
404 
405 	umd = (struct md_conf *)data;
406 	switch (cmd) {
407 	case MD_GETCONF:
408 		*umd = sc->sc_md;
409 		return 0;
410 
411 	case MD_SETCONF:
412 		/* Can only set it once. */
413 		if (sc->sc_type != MD_UNCONFIGURED)
414 			break;
415 		switch (umd->md_type) {
416 		case MD_KMEM_ALLOCATED:
417 			return md_ioctl_kalloc(sc, umd, proc);
418 #if MEMORY_DISK_SERVER
419 		case MD_UMEM_SERVER:
420 			return md_ioctl_server(sc, umd, proc);
421 #endif
422 		default:
423 			break;
424 		}
425 		break;
426 	}
427 	return EINVAL;
428 }
429 
430 /*
431  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
432  * Just allocate some kernel memory and return.
433  */
434 static int
435 md_ioctl_kalloc(sc, umd, proc)
436 	struct md_softc *sc;
437 	struct md_conf *umd;
438 	struct proc *proc;
439 {
440 	vaddr_t addr;
441 	vsize_t size;
442 
443 	/* Sanity check the size. */
444 	size = umd->md_size;
445 	addr = uvm_km_zalloc(kernel_map, size);
446 	if (!addr)
447 		return ENOMEM;
448 
449 	/* This unit is now configured. */
450 	sc->sc_addr = (caddr_t)addr; 	/* kernel space */
451 	sc->sc_size = (size_t)size;
452 	sc->sc_type = MD_KMEM_ALLOCATED;
453 	return 0;
454 }
455 
456 #if MEMORY_DISK_SERVER
457 
458 /*
459  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
460  * Set config, then become the I/O server for this unit.
461  */
462 static int
463 md_ioctl_server(sc, umd, proc)
464 	struct md_softc *sc;
465 	struct md_conf *umd;
466 	struct proc *proc;
467 {
468 	vaddr_t end;
469 	int error;
470 
471 	/* Sanity check addr, size. */
472 	end = (vaddr_t) (umd->md_addr + umd->md_size);
473 
474 	if ((end >= VM_MAXUSER_ADDRESS) ||
475 		(end < ((vaddr_t) umd->md_addr)) )
476 		return EINVAL;
477 
478 	/* This unit is now configured. */
479 	sc->sc_addr = umd->md_addr; 	/* user space */
480 	sc->sc_size = umd->md_size;
481 	sc->sc_type = MD_UMEM_SERVER;
482 
483 	/* Become the server daemon */
484 	error = md_server_loop(sc);
485 
486 	/* This server is now going away! */
487 	sc->sc_type = MD_UNCONFIGURED;
488 	sc->sc_addr = 0;
489 	sc->sc_size = 0;
490 
491 	return (error);
492 }
493 
494 int md_sleep_pri = PWAIT | PCATCH;
495 
496 static int
497 md_server_loop(sc)
498 	struct md_softc *sc;
499 {
500 	struct buf *bp;
501 	caddr_t addr;	/* user space address */
502 	size_t off;	/* offset into "device" */
503 	size_t xfer;	/* amount to transfer */
504 	int error;
505 
506 	for (;;) {
507 		/* Wait for some work to arrive. */
508 		while ((bp = BUFQ_FIRST(&sc->sc_buflist)) == NULL) {
509 			error = tsleep((caddr_t)sc, md_sleep_pri, "md_idle", 0);
510 			if (error)
511 				return error;
512 		}
513 
514 		/* Unlink buf from head of list. */
515 		BUFQ_REMOVE(&sc->sc_buflist, bp);
516 
517 		/* Do the transfer to/from user space. */
518 		error = 0;
519 		bp->b_resid = bp->b_bcount;
520 		off = (bp->b_blkno << DEV_BSHIFT);
521 		if (off >= sc->sc_size) {
522 			if (bp->b_flags & B_READ)
523 				goto done;	/* EOF (not an error) */
524 			error = EIO;
525 			goto done;
526 		}
527 		xfer = bp->b_resid;
528 		if (xfer > (sc->sc_size - off))
529 			xfer = (sc->sc_size - off);
530 		addr = sc->sc_addr + off;
531 		if (bp->b_flags & B_READ)
532 			error = copyin(addr, bp->b_data, xfer);
533 		else
534 			error = copyout(bp->b_data, addr, xfer);
535 		if (!error)
536 			bp->b_resid -= xfer;
537 
538 	done:
539 		if (error) {
540 			bp->b_error = error;
541 			bp->b_flags |= B_ERROR;
542 		}
543 		biodone(bp);
544 	}
545 }
546 #endif	/* MEMORY_DISK_SERVER */
547