md.c revision 76365
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: head/sys/dev/md/md.c 76365 2001-05-08 09:09:32Z phk $
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_mfs.h"		/* We have adopted some tasks from MFS */
61#include "opt_md.h"
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/bio.h>
66#include <sys/conf.h>
67#include <sys/devicestat.h>
68#include <sys/disk.h>
69#include <sys/fcntl.h>
70#include <sys/kernel.h>
71#include <sys/linker.h>
72#include <sys/lock.h>
73#include <sys/malloc.h>
74#include <sys/mdioctl.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/vm_zone.h>
88#include <vm/swap_pager.h>
89
90#define MD_MODVER 1
91
92#ifndef MD_NSECT
93#define MD_NSECT (10000 * 2)
94#endif
95
96MALLOC_DEFINE(M_MD, "MD disk", "Memory Disk");
97MALLOC_DEFINE(M_MDSECT, "MD sectors", "Memory Disk Sectors");
98
99static int md_debug;
100SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
101
102#if defined(MFS_ROOT) && !defined(MD_ROOT)
103#define MD_ROOT MFS_ROOT
104#warning "option MFS_ROOT has been superceeded by MD_ROOT"
105#endif
106
107#if defined(MFS_ROOT_SIZE) && !defined(MD_ROOT_SIZE)
108#define MD_ROOT_SIZE MFS_ROOT_SIZE
109#warning "option MFS_ROOT_SIZE has been superceeded by MD_ROOT_SIZE"
110#endif
111
112#if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
113/* Image gets put here: */
114static u_char mfs_root[MD_ROOT_SIZE*1024] = "MFS Filesystem goes here";
115static u_char end_mfs_root[] __unused = "MFS Filesystem had better STOP here";
116#endif
117
118static int	mdrootready;
119static int	mdunits;
120static dev_t	status_dev = 0;
121
122
123#define CDEV_MAJOR	95
124
125static d_strategy_t mdstrategy;
126static d_open_t mdopen;
127static d_ioctl_t mdioctl, mdctlioctl;
128
129static struct cdevsw md_cdevsw = {
130        /* open */      mdopen,
131        /* close */     nullclose,
132        /* read */      physread,
133        /* write */     physwrite,
134        /* ioctl */     mdioctl,
135        /* poll */      nopoll,
136        /* mmap */      nommap,
137        /* strategy */  mdstrategy,
138        /* name */      MD_NAME,
139        /* maj */       CDEV_MAJOR,
140        /* dump */      nodump,
141        /* psize */     nopsize,
142        /* flags */     D_DISK | D_CANFREE | D_MEMDISK,
143};
144
145static struct cdevsw mdctl_cdevsw = {
146        /* open */      nullopen,
147        /* close */     nullclose,
148        /* read */      noread,
149        /* write */     nowrite,
150        /* ioctl */     mdctlioctl,
151        /* poll */      nopoll,
152        /* mmap */      nommap,
153        /* strategy */  nostrategy,
154        /* name */      MD_NAME,
155        /* maj */       CDEV_MAJOR
156};
157
158static struct cdevsw mddisk_cdevsw;
159
160static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list);
161
162struct md_s {
163	int unit;
164	LIST_ENTRY(md_s) list;
165	struct devstat stats;
166	struct bio_queue_head bio_queue;
167	struct disk disk;
168	dev_t dev;
169	int busy;
170	enum md_types type;
171	unsigned nsect;
172	unsigned secsize;
173	unsigned flags;
174
175	/* MD_MALLOC related fields */
176	u_char **secp;
177
178	/* MD_PRELOAD related fields */
179	u_char *pl_ptr;
180	unsigned pl_len;
181
182	/* MD_VNODE related fields */
183	struct vnode *vnode;
184	struct ucred *cred;
185
186	/* MD_OBJET related fields */
187	vm_object_t object;
188};
189
190static int
191mdopen(dev_t dev, int flag, int fmt, struct proc *p)
192{
193	struct md_s *sc;
194	struct disklabel *dl;
195
196	if (md_debug)
197		printf("mdopen(%s %x %x %p)\n",
198			devtoname(dev), flag, fmt, p);
199
200	sc = dev->si_drv1;
201
202	dl = &sc->disk.d_label;
203	bzero(dl, sizeof(*dl));
204	dl->d_secsize = sc->secsize;
205	dl->d_nsectors = sc->nsect > 63 ? 63 : sc->nsect;
206	dl->d_ntracks = 1;
207	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
208	dl->d_secperunit = sc->nsect;
209	dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
210	return (0);
211}
212
213static int
214mdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
215{
216
217	if (md_debug)
218		printf("mdioctl(%s %lx %p %x %p)\n",
219			devtoname(dev), cmd, addr, flags, p);
220
221	return (ENOIOCTL);
222}
223
224static void
225mdstart_malloc(struct md_s *sc)
226{
227	int i;
228	struct bio *bp;
229	devstat_trans_flags dop;
230	u_char *secp, **secpp, *dst;
231	unsigned secno, nsec, secval, uc;
232
233	for (;;) {
234		/* XXX: LOCK(unique unit numbers) */
235		bp = bioq_first(&sc->bio_queue);
236		if (bp)
237			bioq_remove(&sc->bio_queue, bp);
238		/* XXX: UNLOCK(unique unit numbers) */
239		if (!bp)
240			break;
241
242		devstat_start_transaction(&sc->stats);
243
244		if (bp->bio_cmd == BIO_DELETE)
245			dop = DEVSTAT_NO_DATA;
246		else if (bp->bio_cmd == BIO_READ)
247			dop = DEVSTAT_READ;
248		else
249			dop = DEVSTAT_WRITE;
250
251		nsec = bp->bio_bcount / sc->secsize;
252		secno = bp->bio_pblkno;
253		dst = bp->bio_data;
254		while (nsec--) {
255			secpp = &sc->secp[secno];
256			if ((uintptr_t)*secpp > 255) {
257				secp = *secpp;
258				secval = 0;
259			} else {
260				secp = NULL;
261				secval = (uintptr_t) *secpp;
262			}
263
264			if (md_debug > 2)
265				printf("%x %p %p %d\n",
266				    bp->bio_flags, secpp, secp, secval);
267
268			if (bp->bio_cmd == BIO_DELETE) {
269				if (!(sc->flags & MD_RESERVE) && secp != NULL) {
270					FREE(secp, M_MDSECT);
271					*secpp = 0;
272				}
273			} else if (bp->bio_cmd == BIO_READ) {
274				if (secp != NULL) {
275					bcopy(secp, dst, sc->secsize);
276				} else if (secval) {
277					for (i = 0; i < sc->secsize; i++)
278						dst[i] = secval;
279				} else {
280					bzero(dst, sc->secsize);
281				}
282			} else {
283				if (sc->flags & MD_COMPRESS) {
284					uc = dst[0];
285					for (i = 1; i < sc->secsize; i++)
286						if (dst[i] != uc)
287							break;
288				} else {
289					i = 0;
290					uc = 0;
291				}
292				if (i == sc->secsize) {
293					if (secp)
294						FREE(secp, M_MDSECT);
295					*secpp = (u_char *)(uintptr_t)uc;
296				} else {
297					if (secp == NULL)
298						MALLOC(secp, u_char *, sc->secsize, M_MDSECT, M_WAITOK);
299					bcopy(dst, secp, sc->secsize);
300					*secpp = secp;
301				}
302			}
303			secno++;
304			dst += sc->secsize;
305		}
306		bp->bio_resid = 0;
307		biofinish(bp, &sc->stats, 0);
308	}
309	return;
310}
311
312
313static void
314mdstart_preload(struct md_s *sc)
315{
316	struct bio *bp;
317	devstat_trans_flags dop;
318
319	for (;;) {
320		/* XXX: LOCK(unique unit numbers) */
321		bp = bioq_first(&sc->bio_queue);
322		if (bp)
323			bioq_remove(&sc->bio_queue, bp);
324		/* XXX: UNLOCK(unique unit numbers) */
325		if (!bp)
326			break;
327
328		devstat_start_transaction(&sc->stats);
329
330		if (bp->bio_cmd == BIO_DELETE) {
331			dop = DEVSTAT_NO_DATA;
332		} else if (bp->bio_cmd == BIO_READ) {
333			dop = DEVSTAT_READ;
334			bcopy(sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_data, bp->bio_bcount);
335		} else {
336			dop = DEVSTAT_WRITE;
337			bcopy(bp->bio_data, sc->pl_ptr + (bp->bio_pblkno << DEV_BSHIFT), bp->bio_bcount);
338		}
339		bp->bio_resid = 0;
340		biofinish(bp, &sc->stats, 0);
341	}
342	return;
343}
344
345static void
346mdstart_vnode(struct md_s *sc)
347{
348	int error;
349	struct bio *bp;
350	struct uio auio;
351	struct iovec aiov;
352	struct mount *mp;
353
354	/*
355	 * VNODE I/O
356	 *
357	 * If an error occurs, we set BIO_ERROR but we do not set
358	 * B_INVAL because (for a write anyway), the buffer is
359	 * still valid.
360	 */
361
362	for (;;) {
363		/* XXX: LOCK(unique unit numbers) */
364		bp = bioq_first(&sc->bio_queue);
365		if (bp)
366			bioq_remove(&sc->bio_queue, bp);
367		/* XXX: UNLOCK(unique unit numbers) */
368		if (!bp)
369			break;
370
371		devstat_start_transaction(&sc->stats);
372
373		bzero(&auio, sizeof(auio));
374
375		aiov.iov_base = bp->bio_data;
376		aiov.iov_len = bp->bio_bcount;
377		auio.uio_iov = &aiov;
378		auio.uio_iovcnt = 1;
379		auio.uio_offset = (vm_ooffset_t)bp->bio_pblkno * sc->secsize;
380		auio.uio_segflg = UIO_SYSSPACE;
381		if(bp->bio_cmd == BIO_READ)
382			auio.uio_rw = UIO_READ;
383		else
384			auio.uio_rw = UIO_WRITE;
385		auio.uio_resid = bp->bio_bcount;
386		auio.uio_procp = curproc;
387		if (VOP_ISLOCKED(sc->vnode, NULL))
388			vprint("unexpected md driver lock", sc->vnode);
389		if (bp->bio_cmd == BIO_READ) {
390			vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
391			error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
392		} else {
393			(void) vn_start_write(sc->vnode, &mp, V_WAIT);
394			vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
395			error = VOP_WRITE(sc->vnode, &auio, 0, sc->cred);
396			vn_finished_write(mp);
397		}
398		VOP_UNLOCK(sc->vnode, 0, curproc);
399		bp->bio_resid = auio.uio_resid;
400		biofinish(bp, &sc->stats, error);
401	}
402	return;
403}
404
405static void
406mdstart_swap(struct md_s *sc)
407{
408	struct bio *bp;
409
410	for (;;) {
411		/* XXX: LOCK(unique unit numbers) */
412		bp = bioq_first(&sc->bio_queue);
413		if (bp)
414			bioq_remove(&sc->bio_queue, bp);
415		/* XXX: UNLOCK(unique unit numbers) */
416		if (!bp)
417			break;
418
419#if 0
420		devstat_start_transaction(&sc->stats);
421#endif
422
423		if ((bp->bio_cmd == BIO_DELETE) && (sc->flags & MD_RESERVE))
424			biodone(bp);
425		else
426			vm_pager_strategy(sc->object, bp);
427
428#if 0
429		devstat_end_transaction_bio(&sc->stats, bp);
430#endif
431	}
432	return;
433}
434
435static void
436mdstrategy(struct bio *bp)
437{
438	struct md_s *sc;
439
440	if (md_debug > 1)
441		printf("mdstrategy(%p) %s %x, %d, %ld, %p)\n",
442		    bp, devtoname(bp->bio_dev), bp->bio_flags, bp->bio_blkno,
443		    bp->bio_bcount / DEV_BSIZE, bp->bio_data);
444
445	sc = bp->bio_dev->si_drv1;
446
447	/* XXX: LOCK(sc->lock) */
448	bioqdisksort(&sc->bio_queue, bp);
449	/* XXX: UNLOCK(sc->lock) */
450
451	if (atomic_cmpset_int(&sc->busy, 0, 1) == 0)
452		return;
453
454	switch (sc->type) {
455	case MD_MALLOC:
456		mdstart_malloc(sc);
457		break;
458	case MD_PRELOAD:
459		mdstart_preload(sc);
460		break;
461	case MD_VNODE:
462		mdstart_vnode(sc);
463		break;
464	case MD_SWAP:
465		mdstart_swap(sc);
466		break;
467	default:
468		panic("Impossible md(type)");
469		break;
470	}
471	sc->busy = 0;
472}
473
474static struct md_s *
475mdfind(int unit)
476{
477	struct md_s *sc;
478
479	/* XXX: LOCK(unique unit numbers) */
480	LIST_FOREACH(sc, &md_softc_list, list) {
481		if (sc->unit == unit)
482			break;
483	}
484	/* XXX: UNLOCK(unique unit numbers) */
485	return (sc);
486}
487
488static struct md_s *
489mdnew(int unit)
490{
491	struct md_s *sc;
492	int max = -1;
493
494	/* XXX: LOCK(unique unit numbers) */
495	LIST_FOREACH(sc, &md_softc_list, list) {
496		if (sc->unit == unit) {
497			/* XXX: UNLOCK(unique unit numbers) */
498			return (NULL);
499		}
500		if (sc->unit > max)
501			max = sc->unit;
502	}
503	if (unit == -1)
504		unit = max + 1;
505	if (unit > DKMAXUNIT)
506		return (NULL);
507	MALLOC(sc, struct md_s *,sizeof(*sc), M_MD, M_WAITOK | M_ZERO);
508	sc->unit = unit;
509	LIST_INSERT_HEAD(&md_softc_list, sc, list);
510	/* XXX: UNLOCK(unique unit numbers) */
511	return (sc);
512}
513
514static void
515mdinit(struct md_s *sc)
516{
517
518	bioq_init(&sc->bio_queue);
519	devstat_add_entry(&sc->stats, MD_NAME, sc->unit, sc->secsize,
520		DEVSTAT_NO_ORDERED_TAGS,
521		DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
522		DEVSTAT_PRIORITY_OTHER);
523	sc->dev = disk_create(sc->unit, &sc->disk, 0, &md_cdevsw, &mddisk_cdevsw);
524	sc->dev->si_drv1 = sc;
525}
526
527/*
528 * XXX: we should check that the range they feed us is mapped.
529 * XXX: we should implement read-only.
530 */
531
532static int
533mdcreate_preload(struct md_ioctl *mdio)
534{
535	struct md_s *sc;
536
537	if (mdio->md_size == 0)
538		return(EINVAL);
539	if (mdio->md_options & ~(MD_AUTOUNIT))
540		return(EINVAL);
541	if (mdio->md_options & MD_AUTOUNIT) {
542		sc = mdnew(-1);
543		if (sc == NULL)
544			return (ENOMEM);
545		mdio->md_unit = sc->unit;
546	} else {
547		sc = mdnew(mdio->md_unit);
548		if (sc == NULL)
549			return (EBUSY);
550	}
551	sc->type = MD_PRELOAD;
552	sc->secsize = DEV_BSIZE;
553	sc->nsect = mdio->md_size;
554	/* Cast to pointer size, then to pointer to avoid warning */
555	sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
556	sc->pl_len = (mdio->md_size << DEV_BSHIFT);
557	mdinit(sc);
558	return (0);
559}
560
561
562static int
563mdcreate_malloc(struct md_ioctl *mdio)
564{
565	struct md_s *sc;
566	unsigned u;
567
568	if (mdio->md_size == 0)
569		return(EINVAL);
570	if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
571		return(EINVAL);
572	/* Compression doesn't make sense if we have reserved space */
573	if (mdio->md_options & MD_RESERVE)
574		mdio->md_options &= ~MD_COMPRESS;
575	if (mdio->md_options & MD_AUTOUNIT) {
576		sc = mdnew(-1);
577		if (sc == NULL)
578			return (ENOMEM);
579		mdio->md_unit = sc->unit;
580	} else {
581		sc = mdnew(mdio->md_unit);
582		if (sc == NULL)
583			return (EBUSY);
584	}
585	sc->type = MD_MALLOC;
586	sc->secsize = DEV_BSIZE;
587	sc->nsect = mdio->md_size;
588	sc->flags = mdio->md_options & MD_COMPRESS;
589	MALLOC(sc->secp, u_char **, sc->nsect * sizeof(u_char *), M_MD, M_WAITOK | M_ZERO);
590	if (mdio->md_options & MD_RESERVE) {
591		for (u = 0; u < sc->nsect; u++)
592			MALLOC(sc->secp[u], u_char *, DEV_BSIZE, M_MDSECT, M_WAITOK | M_ZERO);
593	}
594	printf("%s%d: Malloc disk\n", MD_NAME, sc->unit);
595	mdinit(sc);
596	return (0);
597}
598
599
600static int
601mdsetcred(struct md_s *sc, struct ucred *cred)
602{
603	char *tmpbuf;
604	int error = 0;
605
606	/*
607	 * Set credits in our softc
608	 */
609
610	if (sc->cred)
611		crfree(sc->cred);
612	sc->cred = crdup(cred);
613
614	/*
615	 * Horrible kludge to establish credentials for NFS  XXX.
616	 */
617
618	if (sc->vnode) {
619		struct uio auio;
620		struct iovec aiov;
621
622		tmpbuf = malloc(sc->secsize, M_TEMP, M_WAITOK);
623		bzero(&auio, sizeof(auio));
624
625		aiov.iov_base = tmpbuf;
626		aiov.iov_len = sc->secsize;
627		auio.uio_iov = &aiov;
628		auio.uio_iovcnt = 1;
629		auio.uio_offset = 0;
630		auio.uio_rw = UIO_READ;
631		auio.uio_segflg = UIO_SYSSPACE;
632		auio.uio_resid = aiov.iov_len;
633		vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curproc);
634		error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
635		VOP_UNLOCK(sc->vnode, 0, curproc);
636		free(tmpbuf, M_TEMP);
637	}
638	return (error);
639}
640
641static int
642mdcreate_vnode(struct md_ioctl *mdio, struct proc *p)
643{
644	struct md_s *sc;
645	struct vattr vattr;
646	struct nameidata nd;
647	int error, flags;
648
649	if (mdio->md_options & MD_AUTOUNIT) {
650		sc = mdnew(-1);
651		mdio->md_unit = sc->unit;
652	} else {
653		sc = mdnew(mdio->md_unit);
654	}
655	if (sc == NULL)
656		return (EBUSY);
657
658	sc->type = MD_VNODE;
659
660	flags = FREAD|FWRITE;
661	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, p);
662	error = vn_open(&nd, &flags, 0);
663	if (error) {
664		if (error != EACCES && error != EPERM && error != EROFS)
665			return (error);
666		flags &= ~FWRITE;
667		sc->flags |= MD_READONLY;
668		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, mdio->md_file, p);
669		error = vn_open(&nd, &flags, 0);
670		if (error)
671			return (error);
672	}
673	NDFREE(&nd, NDF_ONLY_PNBUF);
674	if (nd.ni_vp->v_type != VREG ||
675	    (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p))) {
676		VOP_UNLOCK(nd.ni_vp, 0, p);
677		(void) vn_close(nd.ni_vp, flags, p->p_ucred, p);
678		return (error ? error : EINVAL);
679	}
680	VOP_UNLOCK(nd.ni_vp, 0, p);
681	sc->secsize = DEV_BSIZE;
682	sc->vnode = nd.ni_vp;
683
684	/*
685	 * If the size is specified, override the file attributes.
686	 */
687	if (mdio->md_size)
688		sc->nsect = mdio->md_size;
689	else
690		sc->nsect = vattr.va_size / sc->secsize; /* XXX: round up ? */
691	error = mdsetcred(sc, p->p_ucred);
692	if (error) {
693		(void) vn_close(nd.ni_vp, flags, p->p_ucred, p);
694		return(error);
695	}
696	mdinit(sc);
697	return (0);
698}
699
700static int
701mddestroy(struct md_s *sc, struct md_ioctl *mdio, struct proc *p)
702{
703	unsigned u;
704
705	if (sc->dev != NULL) {
706		devstat_remove_entry(&sc->stats);
707		disk_destroy(sc->dev);
708	}
709	if (sc->vnode != NULL)
710		(void)vn_close(sc->vnode, sc->flags & MD_READONLY ?  FREAD : (FREAD|FWRITE), sc->cred, p);
711	if (sc->cred != NULL)
712		crfree(sc->cred);
713	if (sc->object != NULL)
714		vm_pager_deallocate(sc->object);
715	if (sc->secp != NULL) {
716		for (u = 0; u < sc->nsect; u++)
717			if ((uintptr_t)sc->secp[u] > 255)
718				FREE(sc->secp[u], M_MDSECT);
719		FREE(sc->secp, M_MD);
720	}
721
722	/* XXX: LOCK(unique unit numbers) */
723	LIST_REMOVE(sc, list);
724	/* XXX: UNLOCK(unique unit numbers) */
725	FREE(sc, M_MD);
726	return (0);
727}
728
729static int
730mdcreate_swap(struct md_ioctl *mdio, struct proc *p)
731{
732	int error;
733	struct md_s *sc;
734
735	if (mdio->md_options & MD_AUTOUNIT) {
736		sc = mdnew(-1);
737		mdio->md_unit = sc->unit;
738	} else {
739		sc = mdnew(mdio->md_unit);
740	}
741	if (sc == NULL)
742		return (EBUSY);
743
744	sc->type = MD_SWAP;
745
746	/*
747	 * Range check.  Disallow negative sizes or any size less then the
748	 * size of a page.  Then round to a page.
749	 */
750
751	if (mdio->md_size == 0) {
752		mddestroy(sc, mdio, p);
753		return(EDOM);
754	}
755
756	/*
757	 * Allocate an OBJT_SWAP object.
758	 *
759	 * sc_secsize is PAGE_SIZE'd
760	 *
761	 * mdio->size is in DEV_BSIZE'd chunks.
762	 * Note the truncation.
763	 */
764
765	sc->secsize = PAGE_SIZE;
766	sc->nsect = mdio->md_size / (PAGE_SIZE / DEV_BSIZE);
767	sc->object = vm_pager_allocate(OBJT_SWAP, NULL, sc->secsize * (vm_offset_t)sc->nsect, VM_PROT_DEFAULT, 0);
768	if (mdio->md_options & MD_RESERVE) {
769		if (swap_pager_reserve(sc->object, 0, sc->nsect) < 0) {
770			vm_pager_deallocate(sc->object);
771			sc->object = NULL;
772			mddestroy(sc, mdio, p);
773			return(EDOM);
774		}
775	}
776	error = mdsetcred(sc, p->p_ucred);
777	if (error)
778		mddestroy(sc, mdio, p);
779	else
780		mdinit(sc);
781	return(error);
782}
783
784static int
785mdctlioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
786{
787	struct md_ioctl *mdio;
788	struct md_s *sc;
789
790	if (md_debug)
791		printf("mdctlioctl(%s %lx %p %x %p)\n",
792			devtoname(dev), cmd, addr, flags, p);
793
794	mdio = (struct md_ioctl *)addr;
795	switch (cmd) {
796	case MDIOCATTACH:
797		switch (mdio->md_type) {
798		case MD_MALLOC:
799			return(mdcreate_malloc(mdio));
800		case MD_PRELOAD:
801			return(mdcreate_preload(mdio));
802		case MD_VNODE:
803			return(mdcreate_vnode(mdio, p));
804		case MD_SWAP:
805			return(mdcreate_swap(mdio, p));
806		default:
807			return (EINVAL);
808		}
809	case MDIOCDETACH:
810		if (mdio->md_file != NULL)
811			return(EINVAL);
812		if (mdio->md_size != 0)
813			return(EINVAL);
814		if (mdio->md_options != 0)
815			return(EINVAL);
816		sc = mdfind(mdio->md_unit);
817		if (sc == NULL)
818			return (ENOENT);
819		switch(sc->type) {
820		case MD_VNODE:
821		case MD_SWAP:
822		case MD_MALLOC:
823		case MD_PRELOAD:
824			return(mddestroy(sc, mdio, p));
825		default:
826			return (EOPNOTSUPP);
827		}
828	case MDIOCQUERY:
829		sc = mdfind(mdio->md_unit);
830		if (sc == NULL)
831			return (ENOENT);
832		mdio->md_type = sc->type;
833		mdio->md_options = sc->flags;
834		switch (sc->type) {
835		case MD_MALLOC:
836			mdio->md_size = sc->nsect;
837			break;
838		case MD_PRELOAD:
839			mdio->md_size = sc->nsect;
840			(u_char *)(uintptr_t)mdio->md_base = sc->pl_ptr;
841			break;
842		case MD_SWAP:
843			mdio->md_size = sc->nsect * (PAGE_SIZE / DEV_BSIZE);
844			break;
845		case MD_VNODE:
846			mdio->md_size = sc->nsect;
847			/* XXX fill this in */
848			mdio->md_file = NULL;
849			break;
850		}
851		return (0);
852	default:
853		return (ENOIOCTL);
854	};
855	return (ENOIOCTL);
856}
857
858static void
859md_preloaded(u_char *image, unsigned length)
860{
861	struct md_s *sc;
862
863	sc = mdnew(-1);
864	if (sc == NULL)
865		return;
866	sc->type = MD_PRELOAD;
867	sc->secsize = DEV_BSIZE;
868	sc->nsect = length / DEV_BSIZE;
869	sc->pl_ptr = image;
870	sc->pl_len = length;
871	if (sc->unit == 0)
872		mdrootready = 1;
873	mdinit(sc);
874}
875
876static void
877md_drvinit(void *unused)
878{
879
880	caddr_t mod;
881	caddr_t c;
882	u_char *ptr, *name, *type;
883	unsigned len;
884
885#ifdef MD_ROOT_SIZE
886	md_preloaded(mfs_root, MD_ROOT_SIZE*1024);
887#endif
888	mod = NULL;
889	while ((mod = preload_search_next_name(mod)) != NULL) {
890		name = (char *)preload_search_info(mod, MODINFO_NAME);
891		type = (char *)preload_search_info(mod, MODINFO_TYPE);
892		if (name == NULL)
893			continue;
894		if (type == NULL)
895			continue;
896		if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
897			continue;
898		c = preload_search_info(mod, MODINFO_ADDR);
899		ptr = *(u_char **)c;
900		c = preload_search_info(mod, MODINFO_SIZE);
901		len = *(unsigned *)c;
902		printf("md%d: Preloaded image <%s> %d bytes at %p\n",
903		   mdunits, name, len, ptr);
904		md_preloaded(ptr, len);
905	}
906	status_dev = make_dev(&mdctl_cdevsw, 0xffff00ff, UID_ROOT, GID_WHEEL, 0600, "mdctl");
907}
908
909static int
910md_modevent(module_t mod, int type, void *data)
911{
912        switch (type) {
913        case MOD_LOAD:
914		md_drvinit(NULL);
915                break;
916        case MOD_UNLOAD:
917		if (!LIST_EMPTY(&md_softc_list))
918			return EBUSY;
919                if (status_dev)
920                        destroy_dev(status_dev);
921                status_dev = 0;
922                break;
923        default:
924                break;
925        }
926        return 0;
927}
928
929static moduledata_t md_mod = {
930        "md",
931        md_modevent,
932        NULL
933};
934DECLARE_MODULE(md, md_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR);
935MODULE_VERSION(md, MD_MODVER);
936
937
938#ifdef MD_ROOT
939static void
940md_takeroot(void *junk)
941{
942	if (mdrootready)
943		rootdevnames[0] = "ufs:/dev/md0c";
944}
945
946SYSINIT(md_root, SI_SUB_MOUNT_ROOT, SI_ORDER_FIRST, md_takeroot, NULL);
947#endif
948
949