xref: /netbsd-src/sys/dev/md.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: md.c,v 1.57 2009/01/13 13:35:52 yamt 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.57 2009/01/13 13:35:52 yamt 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/bufq.h>
59 #include <sys/device.h>
60 #include <sys/disk.h>
61 #include <sys/proc.h>
62 #include <sys/conf.h>
63 #include <sys/disklabel.h>
64 
65 #include <uvm/uvm_extern.h>
66 
67 #include <dev/md.h>
68 
69 /*
70  * The user-space functionality is included by default.
71  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
72  */
73 #ifndef MEMORY_DISK_SERVER
74 #error MEMORY_DISK_SERVER should be defined by opt_md.h
75 #endif	/* MEMORY_DISK_SERVER */
76 
77 /*
78  * We should use the raw partition for ioctl.
79  */
80 #define MD_UNIT(unit)	DISKUNIT(unit)
81 
82 /* autoconfig stuff... */
83 
84 struct md_softc {
85 	struct disk sc_dkdev;	/* hook for generic disk handling */
86 	struct md_conf sc_md;
87 	struct bufq_state *sc_buflist;
88 };
89 /* shorthand for fields in sc_md: */
90 #define sc_addr sc_md.md_addr
91 #define sc_size sc_md.md_size
92 #define sc_type sc_md.md_type
93 
94 void	mdattach(int);
95 
96 static void	md_attach(device_t, device_t, void *);
97 
98 static dev_type_open(mdopen);
99 static dev_type_close(mdclose);
100 static dev_type_read(mdread);
101 static dev_type_write(mdwrite);
102 static dev_type_ioctl(mdioctl);
103 static dev_type_strategy(mdstrategy);
104 static dev_type_size(mdsize);
105 
106 const struct bdevsw md_bdevsw = {
107 	mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
108 };
109 
110 const struct cdevsw md_cdevsw = {
111 	mdopen, mdclose, mdread, mdwrite, mdioctl,
112 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
113 };
114 
115 static struct dkdriver mddkdriver = { mdstrategy, NULL };
116 
117 extern struct cfdriver md_cd;
118 CFATTACH_DECL_NEW(md, sizeof(struct md_softc),
119 	0, md_attach, 0, NULL);
120 
121 /*
122  * This is called if we are configured as a pseudo-device
123  */
124 void
125 mdattach(int n)
126 {
127 	int i;
128 	cfdata_t cf;
129 
130 	if (config_cfattach_attach("md", &md_ca)) {
131 		printf("md: cfattach_attach failed\n");
132 		return;
133 	}
134 
135 	/* XXX:  Are we supposed to provide a default? */
136 	if (n <= 1)
137 		n = 1;
138 
139 	/* Attach as if by autoconfig. */
140 	for (i = 0; i < n; i++) {
141 		cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
142 		cf->cf_name = "md";
143 		cf->cf_atname = "md";
144 		cf->cf_unit = i;
145 		cf->cf_fstate = FSTATE_NOTFOUND;
146 		(void)config_attach_pseudo(cf);
147 	}
148 }
149 
150 static void
151 md_attach(device_t parent, device_t self,
152     void *aux)
153 {
154 	struct md_softc *sc = device_private(self);
155 
156 	bufq_alloc(&sc->sc_buflist, "fcfs", 0);
157 
158 	/* XXX - Could accept aux info here to set the config. */
159 #ifdef	MEMORY_DISK_HOOKS
160 	/*
161 	 * This external function might setup a pre-loaded disk.
162 	 * All it would need to do is setup the md_conf struct.
163 	 * See sys/dev/md_root.c for an example.
164 	 */
165 	md_attach_hook(device_unit(self), &sc->sc_md);
166 #endif
167 
168 	/*
169 	 * Initialize and attach the disk structure.
170 	 */
171 	disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
172 	disk_attach(&sc->sc_dkdev);
173 
174 	if (!pmf_device_register(self, NULL, NULL))
175 		aprint_error_dev(self, "couldn't establish power handler\n");
176 }
177 
178 /*
179  * operational routines:
180  * open, close, read, write, strategy,
181  * ioctl, dump, size
182  */
183 
184 #if MEMORY_DISK_SERVER
185 static int	md_server_loop(struct md_softc *sc);
186 static int	md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
187 		    struct lwp *l);
188 #endif	/* MEMORY_DISK_SERVER */
189 static int	md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
190 		    struct lwp *l);
191 
192 static int
193 mdsize(dev_t dev)
194 {
195 	struct md_softc *sc;
196 
197 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
198 	if (sc == NULL)
199 		return 0;
200 
201 	if (sc->sc_type == MD_UNCONFIGURED)
202 		return 0;
203 
204 	return (sc->sc_size >> DEV_BSHIFT);
205 }
206 
207 static int
208 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
209 {
210 	int unit;
211 	struct md_softc *sc;
212 
213 	unit = MD_UNIT(dev);
214 	sc = device_lookup_private(&md_cd, unit);
215 	if (sc == NULL)
216 		return ENXIO;
217 
218 	/*
219 	 * The raw partition is used for ioctl to configure.
220 	 */
221 	if (DISKPART(dev) == RAW_PART)
222 		return 0;
223 
224 #ifdef	MEMORY_DISK_HOOKS
225 	/* Call the open hook to allow loading the device. */
226 	md_open_hook(unit, &sc->sc_md);
227 #endif
228 
229 	/*
230 	 * This is a normal, "slave" device, so
231 	 * enforce initialized.
232 	 */
233 	if (sc->sc_type == MD_UNCONFIGURED)
234 		return ENXIO;
235 
236 	return 0;
237 }
238 
239 static int
240 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
241 {
242 
243 	return 0;
244 }
245 
246 static int
247 mdread(dev_t dev, struct uio *uio, int flags)
248 {
249 	struct md_softc *sc;
250 
251 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
252 
253 	if (sc->sc_type == MD_UNCONFIGURED)
254 		return ENXIO;
255 
256 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
257 }
258 
259 static int
260 mdwrite(dev_t dev, struct uio *uio, int flags)
261 {
262 	struct md_softc *sc;
263 
264 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
265 
266 	if (sc->sc_type == MD_UNCONFIGURED)
267 		return ENXIO;
268 
269 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
270 }
271 
272 /*
273  * Handle I/O requests, either directly, or
274  * by passing them to the server process.
275  */
276 static void
277 mdstrategy(struct buf *bp)
278 {
279 	struct md_softc	*sc;
280 	void *	addr;
281 	size_t off, xfer;
282 
283 	sc = device_lookup_private(&md_cd, MD_UNIT(bp->b_dev));
284 
285 	if (sc->sc_type == MD_UNCONFIGURED) {
286 		bp->b_error = ENXIO;
287 		goto done;
288 	}
289 
290 	switch (sc->sc_type) {
291 #if MEMORY_DISK_SERVER
292 	case MD_UMEM_SERVER:
293 		/* Just add this job to the server's queue. */
294 		bufq_put(sc->sc_buflist, bp);
295 		wakeup((void *)sc);
296 		/* see md_server_loop() */
297 		/* no biodone in this case */
298 		return;
299 #endif	/* MEMORY_DISK_SERVER */
300 
301 	case MD_KMEM_FIXED:
302 	case MD_KMEM_ALLOCATED:
303 		/* These are in kernel space.  Access directly. */
304 		bp->b_resid = bp->b_bcount;
305 		off = (bp->b_blkno << DEV_BSHIFT);
306 		if (off >= sc->sc_size) {
307 			if (bp->b_flags & B_READ)
308 				break;	/* EOF */
309 			goto set_eio;
310 		}
311 		xfer = bp->b_resid;
312 		if (xfer > (sc->sc_size - off))
313 			xfer = (sc->sc_size - off);
314 		addr = (char *)sc->sc_addr + off;
315 		if (bp->b_flags & B_READ)
316 			memcpy(bp->b_data, addr, xfer);
317 		else
318 			memcpy(addr, bp->b_data, xfer);
319 		bp->b_resid -= xfer;
320 		break;
321 
322 	default:
323 		bp->b_resid = bp->b_bcount;
324 	set_eio:
325 		bp->b_error = EIO;
326 		break;
327 	}
328  done:
329 	biodone(bp);
330 }
331 
332 static int
333 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
334 {
335 	struct md_softc *sc;
336 	struct md_conf *umd;
337 
338 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
339 
340 	/* If this is not the raw partition, punt! */
341 	if (DISKPART(dev) != RAW_PART)
342 		return ENOTTY;
343 
344 	umd = (struct md_conf *)data;
345 	switch (cmd) {
346 	case MD_GETCONF:
347 		*umd = sc->sc_md;
348 		return 0;
349 
350 	case MD_SETCONF:
351 		/* Can only set it once. */
352 		if (sc->sc_type != MD_UNCONFIGURED)
353 			break;
354 		switch (umd->md_type) {
355 		case MD_KMEM_ALLOCATED:
356 			return md_ioctl_kalloc(sc, umd, l);
357 #if MEMORY_DISK_SERVER
358 		case MD_UMEM_SERVER:
359 			return md_ioctl_server(sc, umd, l);
360 #endif	/* MEMORY_DISK_SERVER */
361 		default:
362 			break;
363 		}
364 		break;
365 	}
366 	return EINVAL;
367 }
368 
369 /*
370  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
371  * Just allocate some kernel memory and return.
372  */
373 static int
374 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
375     struct lwp *l)
376 {
377 	vaddr_t addr;
378 	vsize_t size;
379 
380 	/* Sanity check the size. */
381 	size = umd->md_size;
382 	addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
383 	if (!addr)
384 		return ENOMEM;
385 
386 	/* This unit is now configured. */
387 	sc->sc_addr = (void *)addr; 	/* kernel space */
388 	sc->sc_size = (size_t)size;
389 	sc->sc_type = MD_KMEM_ALLOCATED;
390 	return 0;
391 }
392 
393 #if MEMORY_DISK_SERVER
394 
395 /*
396  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
397  * Set config, then become the I/O server for this unit.
398  */
399 static int
400 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
401     struct lwp *l)
402 {
403 	vaddr_t end;
404 	int error;
405 
406 	/* Sanity check addr, size. */
407 	end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
408 
409 	if ((end >= VM_MAXUSER_ADDRESS) ||
410 		(end < ((vaddr_t) umd->md_addr)) )
411 		return EINVAL;
412 
413 	/* This unit is now configured. */
414 	sc->sc_addr = umd->md_addr; 	/* user space */
415 	sc->sc_size = umd->md_size;
416 	sc->sc_type = MD_UMEM_SERVER;
417 
418 	/* Become the server daemon */
419 	error = md_server_loop(sc);
420 
421 	/* This server is now going away! */
422 	sc->sc_type = MD_UNCONFIGURED;
423 	sc->sc_addr = 0;
424 	sc->sc_size = 0;
425 
426 	return (error);
427 }
428 
429 static int md_sleep_pri = PWAIT | PCATCH;
430 
431 static int
432 md_server_loop(struct md_softc *sc)
433 {
434 	struct buf *bp;
435 	void *addr;	/* user space address */
436 	size_t off;	/* offset into "device" */
437 	size_t xfer;	/* amount to transfer */
438 	int error;
439 
440 	for (;;) {
441 		/* Wait for some work to arrive. */
442 		while ((bp = bufq_get(sc->sc_buflist)) == NULL) {
443 			error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
444 			if (error)
445 				return error;
446 		}
447 
448 		/* Do the transfer to/from user space. */
449 		error = 0;
450 		bp->b_resid = bp->b_bcount;
451 		off = (bp->b_blkno << DEV_BSHIFT);
452 		if (off >= sc->sc_size) {
453 			if (bp->b_flags & B_READ)
454 				goto done;	/* EOF (not an error) */
455 			error = EIO;
456 			goto done;
457 		}
458 		xfer = bp->b_resid;
459 		if (xfer > (sc->sc_size - off))
460 			xfer = (sc->sc_size - off);
461 		addr = (char *)sc->sc_addr + off;
462 		if (bp->b_flags & B_READ)
463 			error = copyin(addr, bp->b_data, xfer);
464 		else
465 			error = copyout(bp->b_data, addr, xfer);
466 		if (!error)
467 			bp->b_resid -= xfer;
468 
469 	done:
470 		if (error) {
471 			bp->b_error = error;
472 		}
473 		biodone(bp);
474 	}
475 }
476 #endif	/* MEMORY_DISK_SERVER */
477