xref: /netbsd-src/sys/dev/md.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: md.c,v 1.62 2010/01/21 02:14:42 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * This implements a general-purpose memory-disk.
30  * See md.h for notes on the config types.
31  *
32  * Note that this driver provides the same functionality
33  * as the MFS filesystem hack, but this is better because
34  * you can use this for any filesystem type you'd like!
35  *
36  * Credit for most of the kmem ramdisk code goes to:
37  *   Leo Weppelman (atari) and Phil Nelson (pc532)
38  * Credit for the ideas behind the "user space memory" code goes
39  * to the authors of the MFS implementation.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: md.c,v 1.62 2010/01/21 02:14:42 dyoung Exp $");
44 
45 #include "opt_md.h"
46 #include "opt_tftproot.h"
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/systm.h>
52 #include <sys/buf.h>
53 #include <sys/bufq.h>
54 #include <sys/device.h>
55 #include <sys/disk.h>
56 #include <sys/stat.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  * The user-space functionality is included by default.
67  * Use  `options MEMORY_DISK_SERVER=0' to turn it off.
68  */
69 #ifndef MEMORY_DISK_SERVER
70 #error MEMORY_DISK_SERVER should be defined by opt_md.h
71 #endif	/* MEMORY_DISK_SERVER */
72 
73 /*
74  * We should use the raw partition for ioctl.
75  */
76 #define MD_UNIT(unit)	DISKUNIT(unit)
77 
78 /* autoconfig stuff... */
79 
80 struct md_softc {
81 	struct disk sc_dkdev;	/* hook for generic disk handling */
82 	struct md_conf sc_md;
83 	struct bufq_state *sc_buflist;
84 };
85 /* shorthand for fields in sc_md: */
86 #define sc_addr sc_md.md_addr
87 #define sc_size sc_md.md_size
88 #define sc_type sc_md.md_type
89 
90 void	mdattach(int);
91 
92 static void	md_attach(device_t, device_t, void *);
93 static int	md_detach(device_t, int);
94 
95 static dev_type_open(mdopen);
96 static dev_type_close(mdclose);
97 static dev_type_read(mdread);
98 static dev_type_write(mdwrite);
99 static dev_type_ioctl(mdioctl);
100 static dev_type_strategy(mdstrategy);
101 static dev_type_size(mdsize);
102 
103 const struct bdevsw md_bdevsw = {
104 	mdopen, mdclose, mdstrategy, mdioctl, nodump, mdsize, D_DISK
105 };
106 
107 const struct cdevsw md_cdevsw = {
108 	mdopen, mdclose, mdread, mdwrite, mdioctl,
109 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
110 };
111 
112 static struct dkdriver mddkdriver = { mdstrategy, NULL };
113 
114 extern struct cfdriver md_cd;
115 CFATTACH_DECL3_NEW(md, sizeof(struct md_softc),
116 	0, md_attach, md_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
117 
118 extern size_t md_root_size;
119 
120 /*
121  * This is called if we are configured as a pseudo-device
122  */
123 void
124 mdattach(int n)
125 {
126 	int i;
127 	cfdata_t cf;
128 
129 #ifdef TFTPROOT
130 	/*
131 	 * Attachement of md0 must be done after md_root_setconf(),
132 	 * because the RAMdisk is not loaded yet.
133 	 */
134 	if (md_root_size == 0)
135 		return;
136 #endif
137 	if (config_cfattach_attach("md", &md_ca)) {
138 		printf("md: cfattach_attach failed\n");
139 		return;
140 	}
141 
142 	/* XXX:  Are we supposed to provide a default? */
143 	if (n <= 1)
144 		n = 1;
145 
146 	/* Attach as if by autoconfig. */
147 	for (i = 0; i < n; i++) {
148 		cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
149 		cf->cf_name = "md";
150 		cf->cf_atname = "md";
151 		cf->cf_unit = i;
152 		cf->cf_fstate = FSTATE_NOTFOUND;
153 		(void)config_attach_pseudo(cf);
154 	}
155 }
156 
157 static void
158 md_attach(device_t parent, device_t self, void *aux)
159 {
160 	struct md_softc *sc = device_private(self);
161 
162 	bufq_alloc(&sc->sc_buflist, "fcfs", 0);
163 
164 	/* XXX - Could accept aux info here to set the config. */
165 #ifdef	MEMORY_DISK_HOOKS
166 	/*
167 	 * This external function might setup a pre-loaded disk.
168 	 * All it would need to do is setup the md_conf struct.
169 	 * See sys/dev/md_root.c for an example.
170 	 */
171 	md_attach_hook(device_unit(self), &sc->sc_md);
172 #endif
173 
174 	/*
175 	 * Initialize and attach the disk structure.
176 	 */
177 	disk_init(&sc->sc_dkdev, device_xname(self), &mddkdriver);
178 	disk_attach(&sc->sc_dkdev);
179 
180 	if (!pmf_device_register(self, NULL, NULL))
181 		aprint_error_dev(self, "couldn't establish power handler\n");
182 }
183 
184 static int
185 md_detach(device_t self, int flags)
186 {
187 	struct md_softc *sc = device_private(self);
188 	int rc;
189 
190 	rc = 0;
191 	mutex_enter(&sc->sc_dkdev.dk_openlock);
192 	if (sc->sc_dkdev.dk_openmask == 0)
193 		;	/* nothing to do */
194 	else if ((flags & DETACH_FORCE) == 0)
195 		rc = EBUSY;
196 	mutex_exit(&sc->sc_dkdev.dk_openlock);
197 
198 	if (rc != 0)
199 		return rc;
200 
201 	pmf_device_deregister(self);
202 	disk_detach(&sc->sc_dkdev);
203 	disk_destroy(&sc->sc_dkdev);
204 	bufq_free(sc->sc_buflist);
205 	return 0;
206 }
207 
208 /*
209  * operational routines:
210  * open, close, read, write, strategy,
211  * ioctl, dump, size
212  */
213 
214 #if MEMORY_DISK_SERVER
215 static int	md_server_loop(struct md_softc *sc);
216 static int	md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
217 		    struct lwp *l);
218 #endif	/* MEMORY_DISK_SERVER */
219 static int	md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
220 		    struct lwp *l);
221 
222 static int
223 mdsize(dev_t dev)
224 {
225 	struct md_softc *sc;
226 
227 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
228 	if (sc == NULL)
229 		return 0;
230 
231 	if (sc->sc_type == MD_UNCONFIGURED)
232 		return 0;
233 
234 	return (sc->sc_size >> DEV_BSHIFT);
235 }
236 
237 static int
238 mdopen(dev_t dev, int flag, int fmt, struct lwp *l)
239 {
240 	int unit;
241 	int part = DISKPART(dev);
242 	int pmask = 1 << part;
243 	struct md_softc *sc;
244 	struct disk *dk;
245 
246 	unit = MD_UNIT(dev);
247 	sc = device_lookup_private(&md_cd, unit);
248 	if (sc == NULL)
249 		return ENXIO;
250 
251 	dk = &sc->sc_dkdev;
252 
253 	/*
254 	 * The raw partition is used for ioctl to configure.
255 	 */
256 	if (part == RAW_PART)
257 		goto ok;
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 ok:
272 	/* XXX duplicates code in dk_open().  Call dk_open(), instead? */
273 	mutex_enter(&dk->dk_openlock);
274 	/* Mark our unit as open. */
275 	switch (fmt) {
276 	case S_IFCHR:
277 		dk->dk_copenmask |= pmask;
278 		break;
279 	case S_IFBLK:
280 		dk->dk_bopenmask |= pmask;
281 		break;
282 	}
283 
284 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
285 
286 	mutex_exit(&dk->dk_openlock);
287 	return 0;
288 }
289 
290 static int
291 mdclose(dev_t dev, int flag, int fmt, struct lwp *l)
292 {
293 	int part = DISKPART(dev);
294 	int pmask = 1 << part;
295 	struct md_softc *sc;
296 	struct disk *dk;
297 
298 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
299 	if (sc == NULL)
300 		return ENXIO;
301 
302 	dk = &sc->sc_dkdev;
303 
304 	mutex_enter(&dk->dk_openlock);
305 
306 	switch (fmt) {
307 	case S_IFCHR:
308 		dk->dk_copenmask &= ~pmask;
309 		break;
310 	case S_IFBLK:
311 		dk->dk_bopenmask &= ~pmask;
312 		break;
313 	}
314 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
315 
316 	mutex_exit(&dk->dk_openlock);
317 	return 0;
318 }
319 
320 static int
321 mdread(dev_t dev, struct uio *uio, int flags)
322 {
323 	struct md_softc *sc;
324 
325 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
326 
327 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED)
328 		return ENXIO;
329 
330 	return (physio(mdstrategy, NULL, dev, B_READ, minphys, uio));
331 }
332 
333 static int
334 mdwrite(dev_t dev, struct uio *uio, int flags)
335 {
336 	struct md_softc *sc;
337 
338 	sc = device_lookup_private(&md_cd, MD_UNIT(dev));
339 
340 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED)
341 		return ENXIO;
342 
343 	return (physio(mdstrategy, NULL, dev, B_WRITE, minphys, uio));
344 }
345 
346 /*
347  * Handle I/O requests, either directly, or
348  * by passing them to the server process.
349  */
350 static void
351 mdstrategy(struct buf *bp)
352 {
353 	struct md_softc	*sc;
354 	void *	addr;
355 	size_t off, xfer;
356 
357 	sc = device_lookup_private(&md_cd, MD_UNIT(bp->b_dev));
358 
359 	if (sc == NULL || sc->sc_type == MD_UNCONFIGURED) {
360 		bp->b_error = ENXIO;
361 		goto done;
362 	}
363 
364 	switch (sc->sc_type) {
365 #if MEMORY_DISK_SERVER
366 	case MD_UMEM_SERVER:
367 		/* Just add this job to the server's queue. */
368 		bufq_put(sc->sc_buflist, bp);
369 		wakeup((void *)sc);
370 		/* see md_server_loop() */
371 		/* no biodone in this case */
372 		return;
373 #endif	/* MEMORY_DISK_SERVER */
374 
375 	case MD_KMEM_FIXED:
376 	case MD_KMEM_ALLOCATED:
377 		/* These are in kernel space.  Access directly. */
378 		bp->b_resid = bp->b_bcount;
379 		off = (bp->b_blkno << DEV_BSHIFT);
380 		if (off >= sc->sc_size) {
381 			if (bp->b_flags & B_READ)
382 				break;	/* EOF */
383 			goto set_eio;
384 		}
385 		xfer = bp->b_resid;
386 		if (xfer > (sc->sc_size - off))
387 			xfer = (sc->sc_size - off);
388 		addr = (char *)sc->sc_addr + off;
389 		if (bp->b_flags & B_READ)
390 			memcpy(bp->b_data, addr, xfer);
391 		else
392 			memcpy(addr, bp->b_data, xfer);
393 		bp->b_resid -= xfer;
394 		break;
395 
396 	default:
397 		bp->b_resid = bp->b_bcount;
398 	set_eio:
399 		bp->b_error = EIO;
400 		break;
401 	}
402  done:
403 	biodone(bp);
404 }
405 
406 static int
407 mdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
408 {
409 	struct md_softc *sc;
410 	struct md_conf *umd;
411 
412 	if ((sc = device_lookup_private(&md_cd, MD_UNIT(dev))) == NULL)
413 		return ENXIO;
414 
415 	/* If this is not the raw partition, punt! */
416 	if (DISKPART(dev) != RAW_PART)
417 		return ENOTTY;
418 
419 	umd = (struct md_conf *)data;
420 	switch (cmd) {
421 	case MD_GETCONF:
422 		*umd = sc->sc_md;
423 		return 0;
424 
425 	case MD_SETCONF:
426 		/* Can only set it once. */
427 		if (sc->sc_type != MD_UNCONFIGURED)
428 			break;
429 		switch (umd->md_type) {
430 		case MD_KMEM_ALLOCATED:
431 			return md_ioctl_kalloc(sc, umd, l);
432 #if MEMORY_DISK_SERVER
433 		case MD_UMEM_SERVER:
434 			return md_ioctl_server(sc, umd, l);
435 #endif	/* MEMORY_DISK_SERVER */
436 		default:
437 			break;
438 		}
439 		break;
440 	}
441 	return EINVAL;
442 }
443 
444 /*
445  * Handle ioctl MD_SETCONF for (sc_type == MD_KMEM_ALLOCATED)
446  * Just allocate some kernel memory and return.
447  */
448 static int
449 md_ioctl_kalloc(struct md_softc *sc, struct md_conf *umd,
450     struct lwp *l)
451 {
452 	vaddr_t addr;
453 	vsize_t size;
454 
455 	/* Sanity check the size. */
456 	size = umd->md_size;
457 	addr = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_WIRED|UVM_KMF_ZERO);
458 	if (!addr)
459 		return ENOMEM;
460 
461 	/* This unit is now configured. */
462 	sc->sc_addr = (void *)addr; 	/* kernel space */
463 	sc->sc_size = (size_t)size;
464 	sc->sc_type = MD_KMEM_ALLOCATED;
465 	return 0;
466 }
467 
468 #if MEMORY_DISK_SERVER
469 
470 /*
471  * Handle ioctl MD_SETCONF for (sc_type == MD_UMEM_SERVER)
472  * Set config, then become the I/O server for this unit.
473  */
474 static int
475 md_ioctl_server(struct md_softc *sc, struct md_conf *umd,
476     struct lwp *l)
477 {
478 	vaddr_t end;
479 	int error;
480 
481 	/* Sanity check addr, size. */
482 	end = (vaddr_t) ((char *)umd->md_addr + umd->md_size);
483 
484 	if ((end >= VM_MAXUSER_ADDRESS) ||
485 		(end < ((vaddr_t) umd->md_addr)) )
486 		return EINVAL;
487 
488 	/* This unit is now configured. */
489 	sc->sc_addr = umd->md_addr; 	/* user space */
490 	sc->sc_size = umd->md_size;
491 	sc->sc_type = MD_UMEM_SERVER;
492 
493 	/* Become the server daemon */
494 	error = md_server_loop(sc);
495 
496 	/* This server is now going away! */
497 	sc->sc_type = MD_UNCONFIGURED;
498 	sc->sc_addr = 0;
499 	sc->sc_size = 0;
500 
501 	return (error);
502 }
503 
504 static int md_sleep_pri = PWAIT | PCATCH;
505 
506 static int
507 md_server_loop(struct md_softc *sc)
508 {
509 	struct buf *bp;
510 	void *addr;	/* user space address */
511 	size_t off;	/* offset into "device" */
512 	size_t xfer;	/* amount to transfer */
513 	int error;
514 
515 	for (;;) {
516 		/* Wait for some work to arrive. */
517 		while ((bp = bufq_get(sc->sc_buflist)) == NULL) {
518 			error = tsleep((void *)sc, md_sleep_pri, "md_idle", 0);
519 			if (error)
520 				return error;
521 		}
522 
523 		/* Do the transfer to/from user space. */
524 		error = 0;
525 		bp->b_resid = bp->b_bcount;
526 		off = (bp->b_blkno << DEV_BSHIFT);
527 		if (off >= sc->sc_size) {
528 			if (bp->b_flags & B_READ)
529 				goto done;	/* EOF (not an error) */
530 			error = EIO;
531 			goto done;
532 		}
533 		xfer = bp->b_resid;
534 		if (xfer > (sc->sc_size - off))
535 			xfer = (sc->sc_size - off);
536 		addr = (char *)sc->sc_addr + off;
537 		if (bp->b_flags & B_READ)
538 			error = copyin(addr, bp->b_data, xfer);
539 		else
540 			error = copyout(bp->b_data, addr, xfer);
541 		if (!error)
542 			bp->b_resid -= xfer;
543 
544 	done:
545 		if (error) {
546 			bp->b_error = error;
547 		}
548 		biodone(bp);
549 	}
550 }
551 #endif	/* MEMORY_DISK_SERVER */
552