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