xref: /freebsd/sys/dev/md/md.c (revision c11e094d96120a2e0e726ed9705ae0ec08db49b6)
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  *
11  */
12 
13 /*
14  * The following functions are based in the vn(4) driver: mdstart_swap(),
15  * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
16  * and as such under the following copyright:
17  *
18  * Copyright (c) 1988 University of Utah.
19  * Copyright (c) 1990, 1993
20  *	The Regents of the University of California.  All rights reserved.
21  *
22  * This code is derived from software contributed to Berkeley by
23  * the Systems Programming Group of the University of Utah Computer
24  * Science Department.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *	This product includes software developed by the University of
37  *	California, Berkeley and its contributors.
38  * 4. Neither the name of the University nor the names of its contributors
39  *    may be used to endorse or promote products derived from this software
40  *    without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  * from: Utah Hdr: vn.c 1.13 94/04/02
55  *
56  *	from: @(#)vn.c	8.6 (Berkeley) 4/1/94
57  * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
58  */
59 
60 #include "opt_md.h"
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/bio.h>
65 #include <sys/conf.h>
66 #include <sys/devicestat.h>
67 #include <sys/disk.h>
68 #include <sys/fcntl.h>
69 #include <sys/kernel.h>
70 #include <sys/linker.h>
71 #include <sys/lock.h>
72 #include <sys/malloc.h>
73 #include <sys/mdioctl.h>
74 #include <sys/mutex.h>
75 #include <sys/namei.h>
76 #include <sys/proc.h>
77 #include <sys/queue.h>
78 #include <sys/sysctl.h>
79 #include <sys/vnode.h>
80 
81 #include <machine/atomic.h>
82 
83 #include <vm/vm.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pager.h>
87 #include <vm/swap_pager.h>
88 
89 #define MD_MODVER 1
90 
91 #ifndef MD_NSECT
92 #define MD_NSECT (10000 * 2)
93 #endif
94 
95 static MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
96 static MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
97 
98 static int md_debug;
99 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
100 
101 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
102 /* Image gets put here: */
103 static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
104 static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
105 #endif
106 
107 static int	mdrootready;
108 static int	mdunits;
109 static dev_t	status_dev = 0;
110 
111 
112 #define CDEV_MAJOR	95
113 
114 static d_strategy_t mdstrategy;
115 static d_open_t mdopen;
116 static d_close_t mdclose;
117 static d_ioctl_t mdioctl, mdctlioctl;
118 
119 static struct cdevsw md_cdevsw = {
120         /* open */      mdopen,
121         /* close */     mdclose,
122         /* read */      physread,
123         /* write */     physwrite,
124         /* ioctl */     mdioctl,
125         /* poll */      nopoll,
126         /* mmap */      nommap,
127         /* strategy */  mdstrategy,
128         /* name */      MD_NAME,
129         /* maj */       CDEV_MAJOR,
130         /* dump */      nodump,
131         /* psize */     nopsize,
132         /* flags */     D_DISK | D_CANFREE | D_MEMDISK,
133 };
134 
135 static struct cdevsw mdctl_cdevsw = {
136         /* open */      nullopen,
137         /* close */     nullclose,
138         /* read */      noread,
139         /* write */     nowrite,
140         /* ioctl */     mdctlioctl,
141         /* poll */      nopoll,
142         /* mmap */      nommap,
143         /* strategy */  nostrategy,
144         /* name */      MD_NAME,
145         /* maj */       CDEV_MAJOR
146 };
147 
148 static struct cdevsw mddisk_cdevsw;
149 
150 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list);
151 
152 struct md_s {
153 	int unit;
154 	LIST_ENTRY(md_s) list;
155 	struct devstat stats;
156 	struct bio_queue_head bio_queue;
157 	struct disk disk;
158 	dev_t dev;
159 	int busy;
160 	enum md_types type;
161 	unsigned nsect;
162 	unsigned opencount;
163 	unsigned secsize;
164 	unsigned flags;
165 
166 	/* MD_MALLOC related fields */
167 	u_char **secp;
168 
169 	/* MD_PRELOAD related fields */
170 	u_char *pl_ptr;
171 	unsigned pl_len;
172 
173 	/* MD_VNODE related fields */
174 	struct vnode *vnode;
175 	struct ucred *cred;
176 
177 	/* MD_SWAP related fields */
178 	vm_object_t object;
179 };
180 
181 static int
182 mdopen(dev_t dev, int flag, int fmt, struct thread *td)
183 {
184 	struct md_s *sc;
185 	struct disklabel *dl;
186 
187 	if (md_debug)
188 		printf("mdopen(%s %x %x %p)\n",
189 			devtoname(dev), flag, fmt, td->td_proc);
190 
191 	sc = dev->si_drv1;
192 
193 	dl = &sc->disk.d_label;
194 	bzero(dl, sizeof(*dl));
195 	dl->d_secsize = sc->secsize;
196 	dl->d_nsectors = sc->nsect > 63 ? 63 : sc->nsect;
197 	dl->d_ntracks = 1;
198 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
199 	dl->d_secperunit = sc->nsect;
200 	dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
201 	sc->opencount++;
202 	return (0);
203 }
204 
205 static int
206 mdclose(dev_t dev, int flags, int fmt, struct thread *td)
207 {
208 	struct md_s *sc = dev->si_drv1;
209 
210 	sc->opencount--;
211 	return (0);
212 }
213 
214 static int
215 mdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
216 {
217 
218 	if (md_debug)
219 		printf("mdioctl(%s %lx %p %x %p)\n",
220 			devtoname(dev), cmd, addr, flags, td);
221 
222 	return (ENOIOCTL);
223 }
224 
225 static int
226 mdstart_malloc(struct md_s *sc, struct bio *bp)
227 {
228 	int i;
229 	devstat_trans_flags dop;
230 	u_char *secp, **secpp, *dst;
231 	unsigned secno, nsec, secval, uc;
232 
233 	if (bp->bio_cmd == BIO_DELETE)
234 		dop = DEVSTAT_NO_DATA;
235 	else if (bp->bio_cmd == BIO_READ)
236 		dop = DEVSTAT_READ;
237 	else
238 		dop = DEVSTAT_WRITE;
239 
240 	nsec = bp->bio_bcount / sc->secsize;
241 	secno = bp->bio_pblkno;
242 	dst = bp->bio_data;
243 	while (nsec--) {
244 		secpp = &sc->secp[secno];
245 		if ((uintptr_t)*secpp > 255) {
246 			secp = *secpp;
247 			secval = 0;
248 		} else {
249 			secp = NULL;
250 			secval = (uintptr_t) *secpp;
251 		}
252 
253 		if (md_debug > 2)
254 			printf("%x %p %p %d\n",
255 			    bp->bio_flags, secpp, secp, secval);
256 
257 		if (bp->bio_cmd == BIO_DELETE) {
258 			if (!(sc->flags & MD_RESERVE) && secp != NULL) {
259 				FREE(secp, M_MDSECT);
260 				*secpp = 0;
261 			}
262 		} else if (bp->bio_cmd == BIO_READ) {
263 			if (secp != NULL) {
264 				bcopy(secp, dst, sc->secsize);
265 			} else if (secval) {
266 				for (i = 0; i < sc->secsize; i++)
267 					dst[i] = secval;
268 			} else {
269 				bzero(dst, sc->secsize);
270 			}
271 		} else {
272 			if (sc->flags & MD_COMPRESS) {
273 				uc = dst[0];
274 				for (i = 1; i < sc->secsize; i++)
275 					if (dst[i] != uc)
276 						break;
277 			} else {
278 				i = 0;
279 				uc = 0;
280 			}
281 			if (i == sc->secsize) {
282 				if (secp)
283 					FREE(secp, M_MDSECT);
284 				*secpp = (u_char *)(uintptr_t)uc;
285 			} else {
286 				if (secp == NULL)
287 					MALLOC(secp, u_char *, sc->secsize, M_MDSECT, M_WAITOK);
288 				bcopy(dst, secp, sc->secsize);
289 				*secpp = secp;
290 			}
291 		}
292 		secno++;
293 		dst += sc->secsize;
294 	}
295 	bp->bio_resid = 0;
296 	return (0);
297 }
298 
299 
300 static int
301 mdstart_preload(struct md_s *sc, struct bio *bp)
302 {
303 	devstat_trans_flags dop;
304 
305 	if (bp->bio_cmd == BIO_DELETE) {
306 		dop = DEVSTAT_NO_DATA;
307 	} else if (bp->bio_cmd == BIO_READ) {
308 		dop = DEVSTAT_READ;
309 		bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount);
310 	} else {
311 		dop = DEVSTAT_WRITE;
312 		bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount);
313 	}
314 	bp->bio_resid = 0;
315 	return (0);
316 }
317 
318 static int
319 mdstart_vnode(struct md_s *sc, struct bio *bp)
320 {
321 	int error;
322 	struct uio auio;
323 	struct iovec aiov;
324 	struct mount *mp;
325 
326 	/*
327 	 * VNODE I/O
328 	 *
329 	 * If an error occurs, we set BIO_ERROR but we do not set
330 	 * B_INVAL because (for a write anyway), the buffer is
331 	 * still valid.
332 	 */
333 
334 	bzero(&auio, sizeof(auio));
335 
336 	aiov.iov_base = bp->bio_data;
337 	aiov.iov_len = bp->bio_bcount;
338 	auio.uio_iov = &aiov;
339 	auio.uio_iovcnt = 1;
340 	auio.uio_offset = (vm_ooffset_t)bp->bio_pblkno * sc->secsize;
341 	auio.uio_segflg = UIO_SYSSPACE;
342 	if(bp->bio_cmd == BIO_READ)
343 		auio.uio_rw = UIO_READ;
344 	else
345 		auio.uio_rw = UIO_WRITE;
346 	auio.uio_resid = bp->bio_bcount;
347 	auio.uio_td = curthread;
348 	/*
349 	 * When reading set IO_DIRECT to try to avoid double-caching
350 	 * the data.  When writing IO_DIRECT is not optimal, but we
351 	 * must set IO_NOWDRAIN to avoid a wdrain deadlock.
352 	 */
353 	if (bp->bio_cmd == BIO_READ) {
354 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
355 		error = VOP_READ(sc->vnode, &auio, IO_DIRECT, sc->cred);
356 	} else {
357 		(void) vn_start_write(sc->vnode, &mp, V_WAIT);
358 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
359 		error = VOP_WRITE(sc->vnode, &auio, IO_NOWDRAIN, sc->cred);
360 		vn_finished_write(mp);
361 	}
362 	VOP_UNLOCK(sc->vnode, 0, curthread);
363 	bp->bio_resid = auio.uio_resid;
364 	return (error);
365 }
366 
367 static int
368 mdstart_swap(struct md_s *sc, struct bio *bp)
369 {
370 
371 	if ((bp->bio_cmd == BIO_DELETE) && (sc->flags & MD_RESERVE))
372 		biodone(bp);
373 	else
374 		vm_pager_strategy(sc->object, bp);
375 	return (-1);
376 }
377 
378 static void
379 mdstrategy(struct bio *bp)
380 {
381 	struct md_s *sc;
382 	int error;
383 
384 	if (md_debug > 1)
385 		printf("mdstrategy(%p) %s %x, %lld, %ld, %p)\n",
386 		    (void *)bp, devtoname(bp->bio_dev), bp->bio_flags,
387 		    (long long)bp->bio_blkno, bp->bio_bcount / DEV_BSIZE,
388 		    (void *)bp->bio_data);
389 
390 	sc = bp->bio_dev->si_drv1;
391 
392 	/* XXX: LOCK(sc->lock) */
393 	bioqdisksort(&sc->bio_queue, bp);
394 	/* XXX: UNLOCK(sc->lock) */
395 
396 	if (atomic_cmpset_int(&sc->busy, 0, 1) == 0)
397 		return;
398 
399 	for (;;) {
400 		/* XXX: LOCK(unique unit numbers) */
401 		bp = bioq_first(&sc->bio_queue);
402 		if (bp)
403 			bioq_remove(&sc->bio_queue, bp);
404 		/* XXX: UNLOCK(unique unit numbers) */
405 		if (!bp)
406 			break;
407 
408 
409 		switch (sc->type) {
410 		case MD_MALLOC:
411 			devstat_start_transaction(&sc->stats);
412 			error = mdstart_malloc(sc, bp);
413 			break;
414 		case MD_PRELOAD:
415 			devstat_start_transaction(&sc->stats);
416 			error = mdstart_preload(sc, bp);
417 			break;
418 		case MD_VNODE:
419 			devstat_start_transaction(&sc->stats);
420 			error = mdstart_vnode(sc, bp);
421 			break;
422 		case MD_SWAP:
423 			error = mdstart_swap(sc, bp);
424 			break;
425 		default:
426 			panic("Impossible md(type)");
427 			break;
428 		}
429 
430 		if (error != -1)
431 			biofinish(bp, &sc->stats, error);
432 	}
433 	sc->busy = 0;
434 }
435 
436 static struct md_s *
437 mdfind(int unit)
438 {
439 	struct md_s *sc;
440 
441 	/* XXX: LOCK(unique unit numbers) */
442 	LIST_FOREACH(sc, &md_softc_list, list) {
443 		if (sc->unit == unit)
444 			break;
445 	}
446 	/* XXX: UNLOCK(unique unit numbers) */
447 	return (sc);
448 }
449 
450 static struct md_s *
451 mdnew(int unit)
452 {
453 	struct md_s *sc;
454 	int max = -1;
455 
456 	/* XXX: LOCK(unique unit numbers) */
457 	LIST_FOREACH(sc, &md_softc_list, list) {
458 		if (sc->unit == unit) {
459 			/* XXX: UNLOCK(unique unit numbers) */
460 			return (NULL);
461 		}
462 		if (sc->unit > max)
463 			max = sc->unit;
464 	}
465 	if (unit == -1)
466 		unit = max + 1;
467 	if (unit > DKMAXUNIT)
468 		return (NULL);
469 	MALLOC(sc, struct md_s *, sizeof(*sc), M_MD, M_WAITOK | M_ZERO);
470 	sc->unit = unit;
471 	LIST_INSERT_HEAD(&md_softc_list, sc, list);
472 	/* XXX: UNLOCK(unique unit numbers) */
473 	return (sc);
474 }
475 
476 static void
477 mdinit(struct md_s *sc)
478 {
479 
480 	bioq_init(&sc->bio_queue);
481 	devstat_add_entry(&sc->stats, MD_NAME, sc->unit, sc->secsize,
482 		DEVSTAT_NO_ORDERED_TAGS,
483 		DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
484 		DEVSTAT_PRIORITY_OTHER);
485 	sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw, &mddisk_cdevsw);
486 	sc->dev->si_drv1 = sc;
487 }
488 
489 /*
490  * XXX: we should check that the range they feed us is mapped.
491  * XXX: we should implement read-only.
492  */
493 
494 static int
495 mdcreate_preload(struct md_ioctl *mdio)
496 {
497 	struct md_s *sc;
498 
499 	if (mdio->md_size == 0)
500 		return (EINVAL);
501 	if (mdio->md_options & ~(MD_AUTOUNIT))
502 		return (EINVAL);
503 	if (mdio->md_options & MD_AUTOUNIT) {
504 		sc = mdnew(-1);
505 		if (sc == NULL)
506 			return (ENOMEM);
507 		mdio->md_unit = sc->unit;
508 	} else {
509 		sc = mdnew(mdio->md_unit);
510 		if (sc == NULL)
511 			return (EBUSY);
512 	}
513 	sc->type = MD_PRELOAD;
514 	sc->secsize = DEV_BSIZE;
515 	sc->nsect = mdio->md_size;
516 	sc->flags = mdio->md_options & MD_FORCE;
517 	/* Cast to pointer size, then to pointer to avoid warning */
518 	sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
519 	sc->pl_len = (mdio->md_size << DEV_BSHIFT);
520 	mdinit(sc);
521 	return (0);
522 }
523 
524 
525 static int
526 mdcreate_malloc(struct md_ioctl *mdio)
527 {
528 	struct md_s *sc;
529 	unsigned u;
530 
531 	if (mdio->md_size == 0)
532 		return (EINVAL);
533 	if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
534 		return (EINVAL);
535 	/* Compression doesn't make sense if we have reserved space */
536 	if (mdio->md_options & MD_RESERVE)
537 		mdio->md_options &= ~MD_COMPRESS;
538 	if (mdio->md_options & MD_AUTOUNIT) {
539 		sc = mdnew(-1);
540 		if (sc == NULL)
541 			return (ENOMEM);
542 		mdio->md_unit = sc->unit;
543 	} else {
544 		sc = mdnew(mdio->md_unit);
545 		if (sc == NULL)
546 			return (EBUSY);
547 	}
548 	sc->type = MD_MALLOC;
549 	sc->secsize = DEV_BSIZE;
550 	sc->nsect = mdio->md_size;
551 	sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
552 	MALLOC(sc->secp, u_char **, sc->nsect * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
553 	if (mdio->md_options & MD_RESERVE) {
554 		for (u = 0; u < sc->nsect; u++)
555 			MALLOC(sc->secp[u], u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK | M_ZERO);
556 	}
557 	printf("%s%d: Malloc disk\n", MD_NAME, sc->unit);
558 	mdinit(sc);
559 	return (0);
560 }
561 
562 
563 static int
564 mdsetcred(struct md_s *sc, struct ucred *cred)
565 {
566 	char *tmpbuf;
567 	int error = 0;
568 
569 	/*
570 	 * Set credits in our softc
571 	 */
572 
573 	if (sc->cred)
574 		crfree(sc->cred);
575 	sc->cred = crhold(cred);
576 
577 	/*
578 	 * Horrible kludge to establish credentials for NFS  XXX.
579 	 */
580 
581 	if (sc->vnode) {
582 		struct uio auio;
583 		struct iovec aiov;
584 
585 		tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK);
586 		bzero(&auio, sizeof(auio));
587 
588 		aiov.iov_base = tmpbuf;
589 		aiov.iov_len = sc->secsize;
590 		auio.uio_iov = &aiov;
591 		auio.uio_iovcnt = 1;
592 		auio.uio_offset = 0;
593 		auio.uio_rw = UIO_READ;
594 		auio.uio_segflg = UIO_SYSSPACE;
595 		auio.uio_resid = aiov.iov_len;
596 		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
597 		error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
598 		VOP_UNLOCK(sc->vnode, 0, curthread);
599 		free(tmpbuf, M_TEMP);
600 	}
601 	return (error);
602 }
603 
604 static int
605 mdcreate_vnode(struct md_ioctl *mdio, struct thread *td)
606 {
607 	struct md_s *sc;
608 	struct vattr vattr;
609 	struct nameidata nd;
610 	int error, flags;
611 
612 	flags = FREAD|FWRITE;
613 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
614 	error = vn_open(&nd, &flags, 0);
615 	if (error) {
616 		if (error != EACCES && error != EPERM && error != EROFS)
617 			return (error);
618 		flags &= ~FWRITE;
619 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, td);
620 		error = vn_open(&nd, &flags, 0);
621 		if (error)
622 			return (error);
623 	}
624 	NDFREE(&nd, NDF_ONLY_PNBUF);
625 	if (nd.ni_vp->v_type != VREG ||
626 	    (error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred, td))) {
627 		VOP_UNLOCK(nd.ni_vp, 0, td);
628 		(void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
629 		return (error ? error : EINVAL);
630 	}
631 	VOP_UNLOCK(nd.ni_vp, 0, td);
632 
633 	if (mdio->md_options & MD_AUTOUNIT) {
634 		sc = mdnew(-1);
635 		mdio->md_unit = sc->unit;
636 	} else {
637 		sc = mdnew(mdio->md_unit);
638 	}
639 	if (sc == NULL) {
640 		(void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
641 		return (EBUSY);
642 	}
643 
644 	sc->type = MD_VNODE;
645 	sc->flags = mdio->md_options & MD_FORCE;
646 	if (!(flags & FWRITE))
647 		sc->flags |= MD_READONLY;
648 	sc->secsize = DEV_BSIZE;
649 	sc->vnode = nd.ni_vp;
650 
651 	/*
652 	 * If the size is specified, override the file attributes.
653 	 */
654 	if (mdio->md_size)
655 		sc->nsect = mdio->md_size;
656 	else
657 		sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */
658 	if (sc->nsect == 0) {
659 		(void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
660 		return (EINVAL);
661 	}
662 	error = mdsetcred(sc, td->td_ucred);
663 	if (error) {
664 		(void) vn_close(nd.ni_vp, flags, td->td_ucred, td);
665 		return (error);
666 	}
667 	mdinit(sc);
668 	return (0);
669 }
670 
671 static int
672 mddestroy(struct md_s *sc, struct thread *td)
673 {
674 	unsigned u;
675 
676 	GIANT_REQUIRED;
677 
678 	if (sc->dev != NULL) {
679 		devstat_remove_entry(&sc->stats);
680 		disk_destroy(sc->dev);
681 	}
682 	if (sc->vnode != NULL)
683 		(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
684 		    FREAD : (FREAD|FWRITE), sc->cred, td);
685 	if (sc->cred != NULL)
686 		crfree(sc->cred);
687 	if (sc->object != NULL) {
688 		vm_pager_deallocate(sc->object);
689 	}
690 	if (sc->secp != NULL) {
691 		for (u = 0; u < sc->nsect; u++)
692 			if ((uintptr_t)sc->secp[u] > 255)
693 				FREE(sc->secp[u], M_MDSECT);
694 		FREE(sc->secp, M_MD);
695 	}
696 
697 	/* XXX: LOCK(unique unit numbers) */
698 	LIST_REMOVE(sc, list);
699 	/* XXX: UNLOCK(unique unit numbers) */
700 	FREE(sc, M_MD);
701 	return (0);
702 }
703 
704 static int
705 mdcreate_swap(struct md_ioctl *mdio, struct thread *td)
706 {
707 	int error;
708 	struct md_s *sc;
709 
710 	GIANT_REQUIRED;
711 
712 	if (mdio->md_options & MD_AUTOUNIT) {
713 		sc = mdnew(-1);
714 		mdio->md_unit = sc->unit;
715 	} else {
716 		sc = mdnew(mdio->md_unit);
717 	}
718 	if (sc == NULL)
719 		return (EBUSY);
720 
721 	sc->type = MD_SWAP;
722 
723 	/*
724 	 * Range check.  Disallow negative sizes or any size less then the
725 	 * size of a page.  Then round to a page.
726 	 */
727 
728 	if (mdio->md_size == 0) {
729 		mddestroy(sc, td);
730 		return (EDOM);
731 	}
732 
733 	/*
734 	 * Allocate an OBJT_SWAP object.
735 	 *
736 	 * sc_secsize is PAGE_SIZE'd
737 	 *
738 	 * mdio->size is in DEV_BSIZE'd chunks.
739 	 * Note the truncation.
740 	 */
741 
742 	sc->secsize = PAGE_SIZE;
743 	sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE);
744 	sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0);
745 	sc->flags = mdio->md_options & MD_FORCE;
746 	if (mdio->md_options & MD_RESERVE) {
747 		if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) {
748 			vm_pager_deallocate(sc->object);
749 			sc->object = NULL;
750 			mddestroy(sc, td);
751 			return (EDOM);
752 		}
753 	}
754 	error = mdsetcred(sc, td->td_ucred);
755 	if (error)
756 		mddestroy(sc, td);
757 	else
758 		mdinit(sc);
759 	return (error);
760 }
761 
762 static int
763 mddetach(int unit, struct thread *td)
764 {
765 	struct md_s *sc;
766 
767 	sc = mdfind(unit);
768 	if (sc == NULL)
769 		return (ENOENT);
770 	if (sc->opencount != 0 && !(sc->flags & MD_FORCE))
771 		return (EBUSY);
772 	switch(sc->type) {
773 	case MD_VNODE:
774 	case MD_SWAP:
775 	case MD_MALLOC:
776 	case MD_PRELOAD:
777 		return (mddestroy(sc, td));
778 	default:
779 		return (EOPNOTSUPP);
780 	}
781 }
782 
783 static int
784 mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
785 {
786 	struct md_ioctl *mdio;
787 	struct md_s *sc;
788 
789 	if (md_debug)
790 		printf("mdctlioctl(%s %lx %p %x %p)\n",
791 			devtoname(dev), cmd, addr, flags, td);
792 
793 	/*
794 	 * We assert the version number in the individual ioctl
795 	 * handlers instead of out here because (a) it is possible we
796 	 * may add another ioctl in the future which doesn't read an
797 	 * mdio, and (b) the correct return value for an unknown ioctl
798 	 * is ENOIOCTL, not EINVAL.
799 	 */
800 	mdio = (struct md_ioctl *)addr;
801 	switch (cmd) {
802 	case MDIOCATTACH:
803 		if (mdio->md_version != MDIOVERSION)
804 			return (EINVAL);
805 		switch (mdio->md_type) {
806 		case MD_MALLOC:
807 			return (mdcreate_malloc(mdio));
808 		case MD_PRELOAD:
809 			return (mdcreate_preload(mdio));
810 		case MD_VNODE:
811 			return (mdcreate_vnode(mdio, td));
812 		case MD_SWAP:
813 			return (mdcreate_swap(mdio, td));
814 		default:
815 			return (EINVAL);
816 		}
817 	case MDIOCDETACH:
818 		if (mdio->md_version != MDIOVERSION)
819 			return (EINVAL);
820 		if (mdio->md_file != NULL || mdio->md_size != 0 ||
821 		    mdio->md_options != 0)
822 			return (EINVAL);
823 		return (mddetach(mdio->md_unit, td));
824 	case MDIOCQUERY:
825 		if (mdio->md_version != MDIOVERSION)
826 			return (EINVAL);
827 		sc = mdfind(mdio->md_unit);
828 		if (sc == NULL)
829 			return (ENOENT);
830 		mdio->md_type = sc->type;
831 		mdio->md_options = sc->flags;
832 		switch (sc->type) {
833 		case MD_MALLOC:
834 			mdio->md_size = sc->nsect;
835 			break;
836 		case MD_PRELOAD:
837 			mdio->md_size = sc->nsect;
838 			(u_char *)(uintptr_t)mdio->md_base = sc->pl_ptr;
839 			break;
840 		case MD_SWAP:
841 			mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE);
842 			break;
843 		case MD_VNODE:
844 			mdio->md_size = sc->nsect;
845 			/* XXX fill this in */
846 			mdio->md_file = NULL;
847 			break;
848 		}
849 		return (0);
850 	default:
851 		return (ENOIOCTL);
852 	};
853 	return (ENOIOCTL);
854 }
855 
856 static void
857 md_preloaded(u_char *image, unsigned length)
858 {
859 	struct md_s *sc;
860 
861 	sc = mdnew(-1);
862 	if (sc == NULL)
863 		return;
864 	sc->type = MD_PRELOAD;
865 	sc->secsize = DEV_BSIZE;
866 	sc->nsect = length / DEV_BSIZE;
867 	sc->pl_ptr = image;
868 	sc->pl_len = length;
869 	if (sc->unit == 0)
870 		mdrootready = 1;
871 	mdinit(sc);
872 }
873 
874 static void
875 md_drvinit(void *unused)
876 {
877 
878 	caddr_t mod;
879 	caddr_t c;
880 	u_char *ptr, *name, *type;
881 	unsigned len;
882 
883 #ifdef MD_ROOT_SIZE
884 	md_preloaded(mfs_root, MD_ROOT_SIZE*1024);
885 #endif
886 	mod = NULL;
887 	while ((mod = preload_search_next_name(mod)) != NULL) {
888 		name = (char *)preload_search_info(mod, MODINFO_NAME);
889 		type = (char *)preload_search_info(mod, MODINFO_TYPE);
890 		if (name == NULL)
891 			continue;
892 		if (type == NULL)
893 			continue;
894 		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
895 			continue;
896 		c = preload_search_info(mod, MODINFO_ADDR);
897 		ptr = *(u_char **)c;
898 		c = preload_search_info(mod, MODINFO_SIZE);
899 		len = *(unsigned *)c;
900 		printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
901 		    MD_NAME, mdunits, name, len, ptr);
902 		md_preloaded(ptr, len);
903 	}
904 	status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL,
905 	    0600, MDCTL_NAME);
906 }
907 
908 static int
909 md_modevent(module_t mod, int type, void *data)
910 {
911 	int error;
912 	struct md_s *sc;
913 
914 	switch (type) {
915 	case MOD_LOAD:
916 		md_drvinit(NULL);
917 		break;
918 	case MOD_UNLOAD:
919 		LIST_FOREACH(sc, &md_softc_list, list) {
920 			error = mddetach(sc->unit, curthread);
921 			if (error != 0)
922 				return (error);
923 		}
924 		if (status_dev)
925 			destroy_dev(status_dev);
926 		status_dev = 0;
927 		break;
928 	default:
929 		break;
930 	}
931 	return (0);
932 }
933 
934 static moduledata_t md_mod = {
935 	MD_NAME,
936 	md_modevent,
937 	NULL
938 };
939 DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR);
940 MODULE_VERSION(md, MD_MODVER);
941 
942 
943 #ifdef MD_ROOT
944 static void
945 md_takeroot(void *junk)
946 {
947 	if (mdrootready)
948 		rootdevnames[0] = "ufs:/dev/md0c";
949 }
950 
951 SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
952 #endif
953