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