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