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 * ----------------------------------------------------------------------------
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:
18 * Copyright (c) 1988 University of Utah.
19 * Copyright (c) 1990, 1993
20 * The Regents of the University of California. All rights reserved.
22 * This code is derived from software contributed to Berkeley by
23 * the Systems Programming Group of the University of Utah Computer
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * from: Utah Hdr: vn.c 1.13 94/04/02
52 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
53 * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
59 #include <sys/param.h>
60 #include <sys/systm.h>
63 #include <sys/fcntl.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
66 #include <sys/linker.h>
68 #include <sys/malloc.h>
69 #include <sys/mdioctl.h>
70 #include <sys/mount.h>
71 #include <sys/mutex.h>
73 #include <sys/namei.h>
75 #include <sys/queue.h>
76 #include <sys/sched.h>
77 #include <sys/sf_buf.h>
78 #include <sys/sysctl.h>
79 #include <sys/vnode.h>
81 #include <geom/geom.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>
92 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
93 #define MD_EXITING 0x20000 /* Worker thread is exiting. */
96 #define MD_NSECT (10000 * 2)
99 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
100 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
103 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
105 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
107 * Preloaded image gets put here.
108 * Applications that patch the object with the image can determine
109 * the size looking at the start and end markers (strings),
110 * so we want them contiguous.
113 u_char start[MD_ROOT_SIZE*1024];
116 .start = "MFS Filesystem goes here",
117 .end = "MFS Filesystem had better STOP here",
121 static g_init_t g_md_init;
122 static g_fini_t g_md_fini;
123 static g_start_t g_md_start;
124 static g_access_t g_md_access;
125 static void g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
126 struct g_consumer *cp __unused, struct g_provider *pp);
129 static struct cdev *status_dev = 0;
130 static struct sx md_sx;
132 static d_ioctl_t mdctlioctl;
134 static struct cdevsw mdctl_cdevsw = {
135 .d_version = D_VERSION,
136 .d_ioctl = mdctlioctl,
140 struct g_class g_md_class = {
142 .version = G_VERSION,
146 .access = g_md_access,
147 .dumpconf = g_md_dumpconf,
150 DECLARE_GEOM_CLASS(g_md_class, g_md);
153 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(&md_softc_list);
155 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
156 #define NMASK (NINDIR-1)
168 LIST_ENTRY(md_s) list;
169 struct bio_queue_head bio_queue;
170 struct mtx queue_mtx;
182 struct g_provider *pp;
183 int (*start)(struct md_s *sc, struct bio *bp);
185 /* MD_MALLOC related fields */
189 /* MD_PRELOAD related fields */
193 /* MD_VNODE related fields */
198 /* MD_SWAP related fields */
202 static struct indir *
203 new_indir(u_int shift)
207 ip = malloc(sizeof *ip, M_MD, M_NOWAIT | M_ZERO);
210 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
211 M_MDSECT, M_NOWAIT | M_ZERO);
212 if (ip->array == NULL) {
222 del_indir(struct indir *ip)
225 free(ip->array, M_MDSECT);
230 destroy_indir(struct md_s *sc, struct indir *ip)
234 for (i = 0; i < NINDIR; i++) {
238 destroy_indir(sc, (struct indir*)(ip->array[i]));
239 else if (ip->array[i] > 255)
240 uma_zfree(sc->uma, (void *)(ip->array[i]));
246 * This function does the math and allocates the top level "indir" structure
247 * for a device of "size" sectors.
250 static struct indir *
251 dimension(off_t size)
259 while (rcnt > NINDIR) {
263 /* figure out log2(NINDIR) */
264 for (i = NINDIR, nshift = -1; i; nshift++)
268 * XXX: the top layer is probably not fully populated, so we allocate
269 * too much space for ip->array in here.
271 ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
272 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
273 M_MDSECT, M_WAITOK | M_ZERO);
275 ip->shift = layer * nshift;
280 * Read a given sector
284 s_read(struct indir *ip, off_t offset)
291 printf("s_read(%jd)\n", (intmax_t)offset);
293 for (cip = ip; cip != NULL;) {
295 idx = (offset >> cip->shift) & NMASK;
296 up = cip->array[idx];
297 cip = (struct indir *)up;
300 idx = offset & NMASK;
301 return (cip->array[idx]);
307 * Write a given sector, prune the tree if the value is 0
311 s_write(struct indir *ip, off_t offset, uintptr_t ptr)
313 struct indir *cip, *lip[10];
318 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
325 idx = (offset >> cip->shift) & NMASK;
326 up = cip->array[idx];
328 cip = (struct indir *)up;
331 /* Allocate branch */
333 (uintptr_t)new_indir(cip->shift - nshift);
334 if (cip->array[idx] == 0)
337 up = cip->array[idx];
338 cip = (struct indir *)up;
342 idx = offset & NMASK;
343 up = cip->array[idx];
346 cip->array[idx] = ptr;
351 if (cip->used != 0 || li == 1)
354 while (cip->used == 0 && cip != ip) {
356 idx = (offset >> lip[li]->shift) & NMASK;
357 up = lip[li]->array[idx];
358 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
360 lip[li]->array[idx] = 0;
369 g_md_access(struct g_provider *pp, int r, int w, int e)
373 sc = pp->geom->softc;
379 if ((sc->flags & MD_READONLY) != 0 && w > 0)
381 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
383 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
390 g_md_start(struct bio *bp)
394 sc = bp->bio_to->geom->softc;
395 mtx_lock(&sc->queue_mtx);
396 bioq_disksort(&sc->bio_queue, bp);
397 mtx_unlock(&sc->queue_mtx);
402 mdstart_malloc(struct md_s *sc, struct bio *bp)
406 off_t secno, nsec, uc;
409 switch (bp->bio_cmd) {
418 nsec = bp->bio_length / sc->sectorsize;
419 secno = bp->bio_offset / sc->sectorsize;
423 osp = s_read(sc->indir, secno);
424 if (bp->bio_cmd == BIO_DELETE) {
426 error = s_write(sc->indir, secno, 0);
427 } else if (bp->bio_cmd == BIO_READ) {
429 bzero(dst, sc->sectorsize);
431 for (i = 0; i < sc->sectorsize; i++)
434 bcopy((void *)osp, dst, sc->sectorsize);
436 } else if (bp->bio_cmd == BIO_WRITE) {
437 if (sc->flags & MD_COMPRESS) {
439 for (i = 1; i < sc->sectorsize; i++)
446 if (i == sc->sectorsize) {
448 error = s_write(sc->indir, secno, uc);
451 sp = (uintptr_t)uma_zalloc(sc->uma,
457 bcopy(dst, (void *)sp, sc->sectorsize);
458 error = s_write(sc->indir, secno, sp);
460 bcopy(dst, (void *)osp, sc->sectorsize);
468 uma_zfree(sc->uma, (void*)osp);
472 dst += sc->sectorsize;
479 mdstart_preload(struct md_s *sc, struct bio *bp)
482 switch (bp->bio_cmd) {
484 bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
488 bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
497 mdstart_vnode(struct md_s *sc, struct bio *bp)
499 int error, vfslocked;
506 switch (bp->bio_cmd) {
521 * If an error occurs, we set BIO_ERROR but we do not set
522 * B_INVAL because (for a write anyway), the buffer is
526 if (bp->bio_cmd == BIO_FLUSH) {
527 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
528 (void) vn_start_write(vp, &mp, V_WAIT);
529 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
530 error = VOP_FSYNC(vp, MNT_WAIT, td);
531 VOP_UNLOCK(vp, 0, td);
532 vn_finished_write(mp);
533 VFS_UNLOCK_GIANT(vfslocked);
537 bzero(&auio, sizeof(auio));
539 aiov.iov_base = bp->bio_data;
540 aiov.iov_len = bp->bio_length;
541 auio.uio_iov = &aiov;
543 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
544 auio.uio_segflg = UIO_SYSSPACE;
545 if (bp->bio_cmd == BIO_READ)
546 auio.uio_rw = UIO_READ;
547 else if (bp->bio_cmd == BIO_WRITE)
548 auio.uio_rw = UIO_WRITE;
550 panic("wrong BIO_OP in mdstart_vnode");
551 auio.uio_resid = bp->bio_length;
554 * When reading set IO_DIRECT to try to avoid double-caching
555 * the data. When writing IO_DIRECT is not optimal.
557 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
558 if (bp->bio_cmd == BIO_READ) {
559 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
560 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
561 VOP_UNLOCK(vp, 0, td);
563 (void) vn_start_write(vp, &mp, V_WAIT);
564 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
565 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
567 VOP_UNLOCK(vp, 0, td);
568 vn_finished_write(mp);
570 VFS_UNLOCK_GIANT(vfslocked);
571 bp->bio_resid = auio.uio_resid;
576 mdstart_swap(struct md_s *sc, struct bio *bp)
579 int rv, offs, len, lastend;
580 vm_pindex_t i, lastp;
584 switch (bp->bio_cmd) {
596 * offs is the offset at which to start operating on the
597 * next (ie, first) page. lastp is the last page on
598 * which we're going to operate. lastend is the ending
599 * position within that last page (ie, PAGE_SIZE if
600 * we're operating on complete aligned pages).
602 offs = bp->bio_offset % PAGE_SIZE;
603 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
604 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
607 VM_OBJECT_LOCK(sc->object);
608 vm_object_pip_add(sc->object, 1);
609 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
610 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
612 m = vm_page_grab(sc->object, i,
613 VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
614 VM_OBJECT_UNLOCK(sc->object);
616 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
617 VM_OBJECT_LOCK(sc->object);
618 if (bp->bio_cmd == BIO_READ) {
619 if (m->valid != VM_PAGE_BITS_ALL)
620 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
621 if (rv == VM_PAGER_ERROR) {
624 vm_page_lock_queues();
626 vm_page_unlock_queues();
629 bcopy((void *)(sf_buf_kva(sf) + offs), p, len);
630 } else if (bp->bio_cmd == BIO_WRITE) {
631 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
632 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
633 if (rv == VM_PAGER_ERROR) {
636 vm_page_lock_queues();
638 vm_page_unlock_queues();
641 bcopy(p, (void *)(sf_buf_kva(sf) + offs), len);
642 m->valid = VM_PAGE_BITS_ALL;
644 } else if (bp->bio_cmd == BIO_DELETE) {
645 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
646 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
647 if (rv == VM_PAGER_ERROR) {
650 vm_page_lock_queues();
652 vm_page_unlock_queues();
655 bzero((void *)(sf_buf_kva(sf) + offs), len);
657 m->valid = VM_PAGE_BITS_ALL;
662 vm_page_lock_queues();
665 if (bp->bio_cmd == BIO_WRITE)
667 vm_page_unlock_queues();
669 /* Actions on further pages start at offset 0 */
670 p += PAGE_SIZE - offs;
673 if (bootverbose || bp->bio_offset / PAGE_SIZE < 17)
674 printf("wire_count %d busy %d flags %x hold_count %d act_count %d queue %d valid %d dirty %d @ %d\n",
675 m->wire_count, m->busy,
676 m->flags, m->hold_count, m->act_count, m->queue, m->valid, m->dirty, i);
679 vm_object_pip_subtract(sc->object, 1);
680 vm_object_set_writeable_dirty(sc->object);
681 VM_OBJECT_UNLOCK(sc->object);
682 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
686 md_kthread(void *arg)
693 thread_lock(curthread);
694 sched_prio(curthread, PRIBIO);
695 thread_unlock(curthread);
696 if (sc->type == MD_VNODE)
697 curthread->td_pflags |= TDP_NORUNNINGBUF;
700 mtx_lock(&sc->queue_mtx);
701 if (sc->flags & MD_SHUTDOWN) {
702 sc->flags |= MD_EXITING;
703 mtx_unlock(&sc->queue_mtx);
706 bp = bioq_takefirst(&sc->bio_queue);
708 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
711 mtx_unlock(&sc->queue_mtx);
712 if (bp->bio_cmd == BIO_GETATTR) {
713 if (sc->fwsectors && sc->fwheads &&
714 (g_handleattr_int(bp, "GEOM::fwsectors",
716 g_handleattr_int(bp, "GEOM::fwheads",
722 error = sc->start(sc, bp);
726 bp->bio_completed = bp->bio_length;
727 g_io_deliver(bp, error);
737 LIST_FOREACH(sc, &md_softc_list, list) {
738 if (sc->unit == unit)
745 mdnew(int unit, int *errp, enum md_types type)
747 struct md_s *sc, *sc2;
751 LIST_FOREACH(sc2, &md_softc_list, list) {
752 if (unit == sc2->unit) {
756 if (unit == -1 && sc2->unit > max)
761 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
763 bioq_init(&sc->bio_queue);
764 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
766 sprintf(sc->name, "md%d", unit);
767 LIST_INSERT_HEAD(&md_softc_list, sc, list);
768 error = kthread_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
771 LIST_REMOVE(sc, list);
772 mtx_destroy(&sc->queue_mtx);
779 mdinit(struct md_s *sc)
783 struct g_provider *pp;
786 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
788 pp = g_new_providerf(gp, "md%d", sc->unit);
789 pp->mediasize = sc->mediasize;
790 pp->sectorsize = sc->sectorsize;
793 g_error_provider(pp, 0);
798 * XXX: we should check that the range they feed us is mapped.
799 * XXX: we should implement read-only.
803 mdcreate_preload(struct md_s *sc, struct md_ioctl *mdio)
806 if (mdio->md_options & ~(MD_AUTOUNIT | MD_FORCE))
808 sc->flags = mdio->md_options & MD_FORCE;
809 /* Cast to pointer size, then to pointer to avoid warning */
810 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
811 sc->pl_len = (size_t)sc->mediasize;
817 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
824 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
826 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
828 /* Compression doesn't make sense if we have reserved space */
829 if (mdio->md_options & MD_RESERVE)
830 mdio->md_options &= ~MD_COMPRESS;
831 if (mdio->md_fwsectors != 0)
832 sc->fwsectors = mdio->md_fwsectors;
833 if (mdio->md_fwheads != 0)
834 sc->fwheads = mdio->md_fwheads;
835 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
836 sc->indir = dimension(sc->mediasize / sc->sectorsize);
837 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
839 if (mdio->md_options & MD_RESERVE) {
842 nsectors = sc->mediasize / sc->sectorsize;
843 for (u = 0; u < nsectors; u++) {
844 sp = (uintptr_t)uma_zalloc(sc->uma, M_NOWAIT | M_ZERO);
846 error = s_write(sc->indir, u, sp);
858 mdsetcred(struct md_s *sc, struct ucred *cred)
864 * Set credits in our softc
869 sc->cred = crhold(cred);
872 * Horrible kludge to establish credentials for NFS XXX.
879 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
880 bzero(&auio, sizeof(auio));
882 aiov.iov_base = tmpbuf;
883 aiov.iov_len = sc->sectorsize;
884 auio.uio_iov = &aiov;
887 auio.uio_rw = UIO_READ;
888 auio.uio_segflg = UIO_SYSSPACE;
889 auio.uio_resid = aiov.iov_len;
890 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
891 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
892 VOP_UNLOCK(sc->vnode, 0, curthread);
893 free(tmpbuf, M_TEMP);
899 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
903 int error, flags, vfslocked;
905 error = copyinstr(mdio->md_file, sc->file, sizeof(sc->file), NULL);
908 flags = FREAD|FWRITE;
910 * If the user specified that this is a read only device, unset the
911 * FWRITE mask before trying to open the backing store.
913 if ((mdio->md_options & MD_READONLY) != 0)
915 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, sc->file, td);
916 error = vn_open(&nd, &flags, 0, NULL);
919 vfslocked = NDHASGIANT(&nd);
920 NDFREE(&nd, NDF_ONLY_PNBUF);
921 if (nd.ni_vp->v_type != VREG ||
922 (error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred, td))) {
923 VOP_UNLOCK(nd.ni_vp, 0, td);
924 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
925 VFS_UNLOCK_GIANT(vfslocked);
926 return (error ? error : EINVAL);
928 nd.ni_vp->v_vflag |= VV_MD;
929 VOP_UNLOCK(nd.ni_vp, 0, td);
931 if (mdio->md_fwsectors != 0)
932 sc->fwsectors = mdio->md_fwsectors;
933 if (mdio->md_fwheads != 0)
934 sc->fwheads = mdio->md_fwheads;
935 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
936 if (!(flags & FWRITE))
937 sc->flags |= MD_READONLY;
938 sc->vnode = nd.ni_vp;
940 error = mdsetcred(sc, td->td_ucred);
943 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY, td);
944 nd.ni_vp->v_vflag &= ~VV_MD;
945 VOP_UNLOCK(nd.ni_vp, 0, td);
946 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
947 VFS_UNLOCK_GIANT(vfslocked);
950 VFS_UNLOCK_GIANT(vfslocked);
955 mddestroy(struct md_s *sc, struct thread *td)
960 sc->gp->softc = NULL;
962 g_wither_geom(sc->gp, ENXIO);
967 mtx_lock(&sc->queue_mtx);
968 sc->flags |= MD_SHUTDOWN;
970 while (!(sc->flags & MD_EXITING))
971 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
972 mtx_unlock(&sc->queue_mtx);
973 mtx_destroy(&sc->queue_mtx);
974 if (sc->vnode != NULL) {
975 vfslocked = VFS_LOCK_GIANT(sc->vnode->v_mount);
976 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY, td);
977 sc->vnode->v_vflag &= ~VV_MD;
978 VOP_UNLOCK(sc->vnode, 0, td);
979 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
980 FREAD : (FREAD|FWRITE), sc->cred, td);
981 VFS_UNLOCK_GIANT(vfslocked);
983 if (sc->cred != NULL)
985 if (sc->object != NULL)
986 vm_object_deallocate(sc->object);
988 destroy_indir(sc, sc->indir);
990 uma_zdestroy(sc->uma);
992 LIST_REMOVE(sc, list);
998 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1004 * Range check. Disallow negative sizes or any size less then the
1005 * size of a page. Then round to a page.
1007 if (sc->mediasize == 0 || (sc->mediasize % PAGE_SIZE) != 0)
1011 * Allocate an OBJT_SWAP object.
1013 * Note the truncation.
1016 npage = mdio->md_mediasize / PAGE_SIZE;
1017 if (mdio->md_fwsectors != 0)
1018 sc->fwsectors = mdio->md_fwsectors;
1019 if (mdio->md_fwheads != 0)
1020 sc->fwheads = mdio->md_fwheads;
1021 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1022 VM_PROT_DEFAULT, 0);
1023 if (sc->object == NULL)
1025 sc->flags = mdio->md_options & MD_FORCE;
1026 if (mdio->md_options & MD_RESERVE) {
1027 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1028 vm_object_deallocate(sc->object);
1033 error = mdsetcred(sc, td->td_ucred);
1035 vm_object_deallocate(sc->object);
1043 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1045 struct md_ioctl *mdio;
1050 printf("mdctlioctl(%s %lx %p %x %p)\n",
1051 devtoname(dev), cmd, addr, flags, td);
1053 mdio = (struct md_ioctl *)addr;
1054 if (mdio->md_version != MDIOVERSION)
1058 * We assert the version number in the individual ioctl
1059 * handlers instead of out here because (a) it is possible we
1060 * may add another ioctl in the future which doesn't read an
1061 * mdio, and (b) the correct return value for an unknown ioctl
1062 * is ENOIOCTL, not EINVAL.
1067 switch (mdio->md_type) {
1076 if (mdio->md_options & MD_AUTOUNIT)
1077 sc = mdnew(-1, &error, mdio->md_type);
1079 sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1082 if (mdio->md_options & MD_AUTOUNIT)
1083 mdio->md_unit = sc->unit;
1084 sc->mediasize = mdio->md_mediasize;
1085 if (mdio->md_sectorsize == 0)
1086 sc->sectorsize = DEV_BSIZE;
1088 sc->sectorsize = mdio->md_sectorsize;
1092 sc->start = mdstart_malloc;
1093 error = mdcreate_malloc(sc, mdio);
1096 sc->start = mdstart_preload;
1097 error = mdcreate_preload(sc, mdio);
1100 sc->start = mdstart_vnode;
1101 error = mdcreate_vnode(sc, mdio, td);
1104 sc->start = mdstart_swap;
1105 error = mdcreate_swap(sc, mdio, td);
1113 /* Prune off any residual fractional sector */
1114 i = sc->mediasize % sc->sectorsize;
1120 if (mdio->md_mediasize != 0 || mdio->md_options != 0)
1123 sc = mdfind(mdio->md_unit);
1126 if (sc->opencount != 0 && !(sc->flags & MD_FORCE))
1128 return (mddestroy(sc, td));
1130 sc = mdfind(mdio->md_unit);
1133 mdio->md_type = sc->type;
1134 mdio->md_options = sc->flags;
1135 mdio->md_mediasize = sc->mediasize;
1136 mdio->md_sectorsize = sc->sectorsize;
1137 if (sc->type == MD_VNODE)
1138 error = copyout(sc->file, mdio->md_file,
1139 strlen(sc->file) + 1);
1143 LIST_FOREACH(sc, &md_softc_list, list) {
1144 if (i == MDNPAD - 1)
1145 mdio->md_pad[i] = -1;
1147 mdio->md_pad[i++] = sc->unit;
1149 mdio->md_pad[0] = i - 1;
1157 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1162 error = xmdctlioctl(dev, cmd, addr, flags, td);
1168 md_preloaded(u_char *image, size_t length)
1173 sc = mdnew(-1, &error, MD_PRELOAD);
1176 sc->mediasize = length;
1177 sc->sectorsize = DEV_BSIZE;
1179 sc->pl_len = length;
1180 sc->start = mdstart_preload;
1183 rootdevnames[0] = "ufs:/dev/md0";
1189 g_md_init(struct g_class *mp __unused)
1194 u_char *ptr, *name, *type;
1198 sx_init(&md_sx, "MD config lock");
1199 g_topology_unlock();
1202 md_preloaded(mfs_root.start, sizeof(mfs_root.start));
1205 /* XXX: are preload_* static or do they need Giant ? */
1206 while ((mod = preload_search_next_name(mod)) != NULL) {
1207 name = (char *)preload_search_info(mod, MODINFO_NAME);
1210 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1213 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1215 c = preload_search_info(mod, MODINFO_ADDR);
1216 ptr = *(u_char **)c;
1217 c = preload_search_info(mod, MODINFO_SIZE);
1219 printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
1220 MD_NAME, mdunits, name, len, ptr);
1222 md_preloaded(ptr, len);
1225 status_dev = make_dev(&mdctl_cdevsw, MAXMINOR, UID_ROOT, GID_WHEEL,
1231 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1232 struct g_consumer *cp __unused, struct g_provider *pp)
1260 if (indent == NULL) {
1261 sbuf_printf(sb, " u %d", mp->unit);
1262 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1263 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1264 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1265 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1266 sbuf_printf(sb, " t %s", type);
1267 if (mp->type == MD_VNODE && mp->vnode != NULL)
1268 sbuf_printf(sb, " file %s", mp->file);
1270 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1272 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1273 indent, (uintmax_t) mp->sectorsize);
1274 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1275 indent, (uintmax_t) mp->fwheads);
1276 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1277 indent, (uintmax_t) mp->fwsectors);
1278 sbuf_printf(sb, "%s<length>%ju</length>\n",
1279 indent, (uintmax_t) mp->mediasize);
1280 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1282 if (mp->type == MD_VNODE && mp->vnode != NULL)
1283 sbuf_printf(sb, "%s<file>%s</file>\n",
1290 g_md_fini(struct g_class *mp __unused)
1294 if (status_dev != NULL)
1295 destroy_dev(status_dev);