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/devicestat.h>
64 #include <sys/fcntl.h>
65 #include <sys/kernel.h>
66 #include <sys/kthread.h>
67 #include <sys/limits.h>
68 #include <sys/linker.h>
70 #include <sys/malloc.h>
71 #include <sys/mdioctl.h>
72 #include <sys/mount.h>
73 #include <sys/mutex.h>
75 #include <sys/namei.h>
77 #include <sys/queue.h>
78 #include <sys/sched.h>
79 #include <sys/sf_buf.h>
80 #include <sys/sysctl.h>
81 #include <sys/vnode.h>
83 #include <geom/geom.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pager.h>
89 #include <vm/swap_pager.h>
94 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
95 #define MD_EXITING 0x20000 /* Worker thread is exiting. */
98 #define MD_NSECT (10000 * 2)
101 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
102 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
105 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
107 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
109 * Preloaded image gets put here.
110 * Applications that patch the object with the image can determine
111 * the size looking at the start and end markers (strings),
112 * so we want them contiguous.
115 u_char start[MD_ROOT_SIZE*1024];
118 .start = "MFS Filesystem goes here",
119 .end = "MFS Filesystem had better STOP here",
123 static g_init_t g_md_init;
124 static g_fini_t g_md_fini;
125 static g_start_t g_md_start;
126 static g_access_t g_md_access;
127 static void g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
128 struct g_consumer *cp __unused, struct g_provider *pp);
131 static struct cdev *status_dev = 0;
132 static struct sx md_sx;
134 static d_ioctl_t mdctlioctl;
136 static struct cdevsw mdctl_cdevsw = {
137 .d_version = D_VERSION,
138 .d_ioctl = mdctlioctl,
142 struct g_class g_md_class = {
144 .version = G_VERSION,
148 .access = g_md_access,
149 .dumpconf = g_md_dumpconf,
152 DECLARE_GEOM_CLASS(g_md_class, g_md);
155 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
157 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
158 #define NMASK (NINDIR-1)
170 LIST_ENTRY(md_s) list;
171 struct bio_queue_head bio_queue;
172 struct mtx queue_mtx;
184 struct g_provider *pp;
185 int (*start)(struct md_s *sc, struct bio *bp);
186 struct devstat *devstat;
188 /* MD_MALLOC related fields */
192 /* MD_PRELOAD related fields */
196 /* MD_VNODE related fields */
201 /* MD_SWAP related fields */
205 static struct indir *
206 new_indir(u_int shift)
210 ip = malloc(sizeof *ip, M_MD, M_NOWAIT | M_ZERO);
213 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
214 M_MDSECT, M_NOWAIT | M_ZERO);
215 if (ip->array == NULL) {
225 del_indir(struct indir *ip)
228 free(ip->array, M_MDSECT);
233 destroy_indir(struct md_s *sc, struct indir *ip)
237 for (i = 0; i < NINDIR; i++) {
241 destroy_indir(sc, (struct indir*)(ip->array[i]));
242 else if (ip->array[i] > 255)
243 uma_zfree(sc->uma, (void *)(ip->array[i]));
249 * This function does the math and allocates the top level "indir" structure
250 * for a device of "size" sectors.
253 static struct indir *
254 dimension(off_t size)
262 while (rcnt > NINDIR) {
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;
375 if (r <= 0 && w <= 0 && e <= 0)
382 if ((sc->flags & MD_READONLY) != 0 && w > 0)
384 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
386 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
393 g_md_start(struct bio *bp)
397 sc = bp->bio_to->geom->softc;
398 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
399 devstat_start_transaction_bio(sc->devstat, bp);
400 mtx_lock(&sc->queue_mtx);
401 bioq_disksort(&sc->bio_queue, bp);
402 mtx_unlock(&sc->queue_mtx);
407 mdstart_malloc(struct md_s *sc, struct bio *bp)
411 off_t secno, nsec, uc;
414 switch (bp->bio_cmd) {
423 nsec = bp->bio_length / sc->sectorsize;
424 secno = bp->bio_offset / sc->sectorsize;
428 osp = s_read(sc->indir, secno);
429 if (bp->bio_cmd == BIO_DELETE) {
431 error = s_write(sc->indir, secno, 0);
432 } else if (bp->bio_cmd == BIO_READ) {
434 bzero(dst, sc->sectorsize);
436 memset(dst, osp, sc->sectorsize);
438 bcopy((void *)osp, dst, sc->sectorsize);
439 cpu_flush_dcache(dst, sc->sectorsize);
442 } else if (bp->bio_cmd == BIO_WRITE) {
443 if (sc->flags & MD_COMPRESS) {
445 for (i = 1; i < sc->sectorsize; i++)
452 if (i == sc->sectorsize) {
454 error = s_write(sc->indir, secno, uc);
457 sp = (uintptr_t)uma_zalloc(sc->uma,
463 bcopy(dst, (void *)sp, sc->sectorsize);
464 error = s_write(sc->indir, secno, sp);
466 bcopy(dst, (void *)osp, sc->sectorsize);
474 uma_zfree(sc->uma, (void*)osp);
478 dst += sc->sectorsize;
485 mdstart_preload(struct md_s *sc, struct bio *bp)
488 switch (bp->bio_cmd) {
490 bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
492 cpu_flush_dcache(bp->bio_data, bp->bio_length);
495 bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
504 mdstart_vnode(struct md_s *sc, struct bio *bp)
506 int error, vfslocked;
513 switch (bp->bio_cmd) {
528 * If an error occurs, we set BIO_ERROR but we do not set
529 * B_INVAL because (for a write anyway), the buffer is
533 if (bp->bio_cmd == BIO_FLUSH) {
534 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
535 (void) vn_start_write(vp, &mp, V_WAIT);
536 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
537 error = VOP_FSYNC(vp, MNT_WAIT, td);
539 vn_finished_write(mp);
540 VFS_UNLOCK_GIANT(vfslocked);
544 bzero(&auio, sizeof(auio));
546 aiov.iov_base = bp->bio_data;
547 aiov.iov_len = bp->bio_length;
548 auio.uio_iov = &aiov;
550 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
551 auio.uio_segflg = UIO_SYSSPACE;
552 if (bp->bio_cmd == BIO_READ)
553 auio.uio_rw = UIO_READ;
554 else if (bp->bio_cmd == BIO_WRITE)
555 auio.uio_rw = UIO_WRITE;
557 panic("wrong BIO_OP in mdstart_vnode");
558 auio.uio_resid = bp->bio_length;
561 * When reading set IO_DIRECT to try to avoid double-caching
562 * the data. When writing IO_DIRECT is not optimal.
564 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
565 if (bp->bio_cmd == BIO_READ) {
566 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
567 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
570 (void) vn_start_write(vp, &mp, V_WAIT);
571 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
572 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
575 vn_finished_write(mp);
577 VFS_UNLOCK_GIANT(vfslocked);
578 bp->bio_resid = auio.uio_resid;
583 mdstart_swap(struct md_s *sc, struct bio *bp)
586 int rv, offs, len, lastend;
587 vm_pindex_t i, lastp;
591 switch (bp->bio_cmd) {
603 * offs is the offset at which to start operating on the
604 * next (ie, first) page. lastp is the last page on
605 * which we're going to operate. lastend is the ending
606 * position within that last page (ie, PAGE_SIZE if
607 * we're operating on complete aligned pages).
609 offs = bp->bio_offset % PAGE_SIZE;
610 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
611 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
614 VM_OBJECT_LOCK(sc->object);
615 vm_object_pip_add(sc->object, 1);
616 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
617 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
619 m = vm_page_grab(sc->object, i,
620 VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
621 VM_OBJECT_UNLOCK(sc->object);
623 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
624 VM_OBJECT_LOCK(sc->object);
625 if (bp->bio_cmd == BIO_READ) {
626 if (m->valid != VM_PAGE_BITS_ALL)
627 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
628 if (rv == VM_PAGER_ERROR) {
634 bcopy((void *)(sf_buf_kva(sf) + offs), p, len);
635 cpu_flush_dcache(p, len);
636 } else if (bp->bio_cmd == BIO_WRITE) {
637 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
638 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
639 if (rv == VM_PAGER_ERROR) {
645 bcopy(p, (void *)(sf_buf_kva(sf) + offs), len);
646 m->valid = VM_PAGE_BITS_ALL;
648 } else if (bp->bio_cmd == BIO_DELETE) {
649 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
650 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
651 if (rv == VM_PAGER_ERROR) {
657 bzero((void *)(sf_buf_kva(sf) + offs), len);
659 m->valid = VM_PAGE_BITS_ALL;
665 vm_page_lock_queues();
667 if (bp->bio_cmd == BIO_WRITE)
669 vm_page_unlock_queues();
671 /* Actions on further pages start at offset 0 */
672 p += PAGE_SIZE - offs;
675 if (bootverbose || bp->bio_offset / PAGE_SIZE < 17)
676 printf("wire_count %d busy %d flags %x hold_count %d act_count %d queue %d valid %d dirty %d @ %d\n",
677 m->wire_count, m->busy,
678 m->flags, m->hold_count, m->act_count, m->queue, m->valid, m->dirty, i);
681 vm_object_pip_subtract(sc->object, 1);
682 vm_object_set_writeable_dirty(sc->object);
683 VM_OBJECT_UNLOCK(sc->object);
684 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
688 md_kthread(void *arg)
695 thread_lock(curthread);
696 sched_prio(curthread, PRIBIO);
697 thread_unlock(curthread);
698 if (sc->type == MD_VNODE)
699 curthread->td_pflags |= TDP_NORUNNINGBUF;
702 mtx_lock(&sc->queue_mtx);
703 if (sc->flags & MD_SHUTDOWN) {
704 sc->flags |= MD_EXITING;
705 mtx_unlock(&sc->queue_mtx);
708 bp = bioq_takefirst(&sc->bio_queue);
710 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
713 mtx_unlock(&sc->queue_mtx);
714 if (bp->bio_cmd == BIO_GETATTR) {
715 if (sc->fwsectors && sc->fwheads &&
716 (g_handleattr_int(bp, "GEOM::fwsectors",
718 g_handleattr_int(bp, "GEOM::fwheads",
724 error = sc->start(sc, bp);
728 bp->bio_completed = bp->bio_length;
729 g_io_deliver(bp, error);
730 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
731 devstat_end_transaction_bio(sc->devstat, bp);
741 LIST_FOREACH(sc, &md_softc_list, list) {
742 if (sc->unit == unit)
749 mdnew(int unit, int *errp, enum md_types type)
751 struct md_s *sc, *sc2;
755 LIST_FOREACH(sc2, &md_softc_list, list) {
756 if (unit == sc2->unit) {
760 if (unit == -1 && sc2->unit > max)
765 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
767 bioq_init(&sc->bio_queue);
768 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
770 sprintf(sc->name, "md%d", unit);
771 LIST_INSERT_HEAD(&md_softc_list, sc, list);
772 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
775 LIST_REMOVE(sc, list);
776 mtx_destroy(&sc->queue_mtx);
783 mdinit(struct md_s *sc)
787 struct g_provider *pp;
790 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
792 pp = g_new_providerf(gp, "md%d", sc->unit);
793 pp->mediasize = sc->mediasize;
794 pp->sectorsize = sc->sectorsize;
797 g_error_provider(pp, 0);
799 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
800 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
804 * XXX: we should check that the range they feed us is mapped.
805 * XXX: we should implement read-only.
809 mdcreate_preload(struct md_s *sc, struct md_ioctl *mdio)
812 if (mdio->md_options & ~(MD_AUTOUNIT | MD_FORCE))
814 if (mdio->md_base == 0)
816 sc->flags = mdio->md_options & MD_FORCE;
817 /* Cast to pointer size, then to pointer to avoid warning */
818 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
819 sc->pl_len = (size_t)sc->mediasize;
825 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
832 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
834 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
836 /* Compression doesn't make sense if we have reserved space */
837 if (mdio->md_options & MD_RESERVE)
838 mdio->md_options &= ~MD_COMPRESS;
839 if (mdio->md_fwsectors != 0)
840 sc->fwsectors = mdio->md_fwsectors;
841 if (mdio->md_fwheads != 0)
842 sc->fwheads = mdio->md_fwheads;
843 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
844 sc->indir = dimension(sc->mediasize / sc->sectorsize);
845 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
847 if (mdio->md_options & MD_RESERVE) {
850 nsectors = sc->mediasize / sc->sectorsize;
851 for (u = 0; u < nsectors; u++) {
852 sp = (uintptr_t)uma_zalloc(sc->uma, M_NOWAIT | M_ZERO);
854 error = s_write(sc->indir, u, sp);
866 mdsetcred(struct md_s *sc, struct ucred *cred)
872 * Set credits in our softc
877 sc->cred = crhold(cred);
880 * Horrible kludge to establish credentials for NFS XXX.
887 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
888 bzero(&auio, sizeof(auio));
890 aiov.iov_base = tmpbuf;
891 aiov.iov_len = sc->sectorsize;
892 auio.uio_iov = &aiov;
895 auio.uio_rw = UIO_READ;
896 auio.uio_segflg = UIO_SYSSPACE;
897 auio.uio_resid = aiov.iov_len;
898 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
899 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
900 VOP_UNLOCK(sc->vnode, 0);
901 free(tmpbuf, M_TEMP);
907 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
911 int error, flags, vfslocked;
913 error = copyinstr(mdio->md_file, sc->file, sizeof(sc->file), NULL);
916 flags = FREAD|FWRITE;
918 * If the user specified that this is a read only device, unset the
919 * FWRITE mask before trying to open the backing store.
921 if ((mdio->md_options & MD_READONLY) != 0)
923 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, sc->file, td);
924 error = vn_open(&nd, &flags, 0, NULL);
927 vfslocked = NDHASGIANT(&nd);
928 NDFREE(&nd, NDF_ONLY_PNBUF);
929 if (nd.ni_vp->v_type != VREG) {
933 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
936 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
937 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
938 if (nd.ni_vp->v_iflag & VI_DOOMED) {
939 /* Forced unmount. */
944 nd.ni_vp->v_vflag |= VV_MD;
945 VOP_UNLOCK(nd.ni_vp, 0);
947 if (mdio->md_fwsectors != 0)
948 sc->fwsectors = mdio->md_fwsectors;
949 if (mdio->md_fwheads != 0)
950 sc->fwheads = mdio->md_fwheads;
951 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
952 if (!(flags & FWRITE))
953 sc->flags |= MD_READONLY;
954 sc->vnode = nd.ni_vp;
956 error = mdsetcred(sc, td->td_ucred);
959 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
960 nd.ni_vp->v_vflag &= ~VV_MD;
963 VFS_UNLOCK_GIANT(vfslocked);
966 VOP_UNLOCK(nd.ni_vp, 0);
967 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
968 VFS_UNLOCK_GIANT(vfslocked);
973 mddestroy(struct md_s *sc, struct thread *td)
978 sc->gp->softc = NULL;
980 g_wither_geom(sc->gp, ENXIO);
986 devstat_remove_entry(sc->devstat);
989 mtx_lock(&sc->queue_mtx);
990 sc->flags |= MD_SHUTDOWN;
992 while (!(sc->flags & MD_EXITING))
993 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
994 mtx_unlock(&sc->queue_mtx);
995 mtx_destroy(&sc->queue_mtx);
996 if (sc->vnode != NULL) {
997 vfslocked = VFS_LOCK_GIANT(sc->vnode->v_mount);
998 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
999 sc->vnode->v_vflag &= ~VV_MD;
1000 VOP_UNLOCK(sc->vnode, 0);
1001 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1002 FREAD : (FREAD|FWRITE), sc->cred, td);
1003 VFS_UNLOCK_GIANT(vfslocked);
1005 if (sc->cred != NULL)
1007 if (sc->object != NULL)
1008 vm_object_deallocate(sc->object);
1010 destroy_indir(sc, sc->indir);
1012 uma_zdestroy(sc->uma);
1014 LIST_REMOVE(sc, list);
1020 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1026 * Range check. Disallow negative sizes or any size less then the
1027 * size of a page. Then round to a page.
1029 if (sc->mediasize == 0 || (sc->mediasize % PAGE_SIZE) != 0)
1033 * Allocate an OBJT_SWAP object.
1035 * Note the truncation.
1038 npage = mdio->md_mediasize / PAGE_SIZE;
1039 if (mdio->md_fwsectors != 0)
1040 sc->fwsectors = mdio->md_fwsectors;
1041 if (mdio->md_fwheads != 0)
1042 sc->fwheads = mdio->md_fwheads;
1043 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1044 VM_PROT_DEFAULT, 0, td->td_ucred);
1045 if (sc->object == NULL)
1047 sc->flags = mdio->md_options & MD_FORCE;
1048 if (mdio->md_options & MD_RESERVE) {
1049 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1054 error = mdsetcred(sc, td->td_ucred);
1057 vm_object_deallocate(sc->object);
1065 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1067 struct md_ioctl *mdio;
1072 printf("mdctlioctl(%s %lx %p %x %p)\n",
1073 devtoname(dev), cmd, addr, flags, td);
1075 mdio = (struct md_ioctl *)addr;
1076 if (mdio->md_version != MDIOVERSION)
1080 * We assert the version number in the individual ioctl
1081 * handlers instead of out here because (a) it is possible we
1082 * may add another ioctl in the future which doesn't read an
1083 * mdio, and (b) the correct return value for an unknown ioctl
1084 * is ENOIOCTL, not EINVAL.
1089 switch (mdio->md_type) {
1098 if (mdio->md_options & MD_AUTOUNIT)
1099 sc = mdnew(-1, &error, mdio->md_type);
1101 sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1104 if (mdio->md_options & MD_AUTOUNIT)
1105 mdio->md_unit = sc->unit;
1106 sc->mediasize = mdio->md_mediasize;
1107 if (mdio->md_sectorsize == 0)
1108 sc->sectorsize = DEV_BSIZE;
1110 sc->sectorsize = mdio->md_sectorsize;
1114 sc->start = mdstart_malloc;
1115 error = mdcreate_malloc(sc, mdio);
1118 sc->start = mdstart_preload;
1119 error = mdcreate_preload(sc, mdio);
1122 sc->start = mdstart_vnode;
1123 error = mdcreate_vnode(sc, mdio, td);
1126 sc->start = mdstart_swap;
1127 error = mdcreate_swap(sc, mdio, td);
1135 /* Prune off any residual fractional sector */
1136 i = sc->mediasize % sc->sectorsize;
1142 if (mdio->md_mediasize != 0 ||
1143 (mdio->md_options & ~MD_FORCE) != 0)
1146 sc = mdfind(mdio->md_unit);
1149 if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1150 !(mdio->md_options & MD_FORCE))
1152 return (mddestroy(sc, td));
1154 sc = mdfind(mdio->md_unit);
1157 mdio->md_type = sc->type;
1158 mdio->md_options = sc->flags;
1159 mdio->md_mediasize = sc->mediasize;
1160 mdio->md_sectorsize = sc->sectorsize;
1161 if (sc->type == MD_VNODE)
1162 error = copyout(sc->file, mdio->md_file,
1163 strlen(sc->file) + 1);
1167 LIST_FOREACH(sc, &md_softc_list, list) {
1168 if (i == MDNPAD - 1)
1169 mdio->md_pad[i] = -1;
1171 mdio->md_pad[i++] = sc->unit;
1173 mdio->md_pad[0] = i - 1;
1181 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1186 error = xmdctlioctl(dev, cmd, addr, flags, td);
1192 md_preloaded(u_char *image, size_t length)
1197 sc = mdnew(-1, &error, MD_PRELOAD);
1200 sc->mediasize = length;
1201 sc->sectorsize = DEV_BSIZE;
1203 sc->pl_len = length;
1204 sc->start = mdstart_preload;
1207 rootdevnames[0] = "ufs:/dev/md0";
1213 g_md_init(struct g_class *mp __unused)
1218 u_char *ptr, *name, *type;
1222 /* figure out log2(NINDIR) */
1223 for (i = NINDIR, nshift = -1; i; nshift++)
1227 sx_init(&md_sx, "MD config lock");
1228 g_topology_unlock();
1231 md_preloaded(mfs_root.start, sizeof(mfs_root.start));
1234 /* XXX: are preload_* static or do they need Giant ? */
1235 while ((mod = preload_search_next_name(mod)) != NULL) {
1236 name = (char *)preload_search_info(mod, MODINFO_NAME);
1239 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1242 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1244 c = preload_search_info(mod, MODINFO_ADDR);
1245 ptr = *(u_char **)c;
1246 c = preload_search_info(mod, MODINFO_SIZE);
1248 printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
1249 MD_NAME, mdunits, name, len, ptr);
1251 md_preloaded(ptr, len);
1254 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
1260 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1261 struct g_consumer *cp __unused, struct g_provider *pp)
1289 if (indent == NULL) {
1290 sbuf_printf(sb, " u %d", mp->unit);
1291 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1292 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1293 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1294 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1295 sbuf_printf(sb, " t %s", type);
1296 if (mp->type == MD_VNODE && mp->vnode != NULL)
1297 sbuf_printf(sb, " file %s", mp->file);
1299 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1301 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1302 indent, (uintmax_t) mp->sectorsize);
1303 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1304 indent, (uintmax_t) mp->fwheads);
1305 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1306 indent, (uintmax_t) mp->fwsectors);
1307 sbuf_printf(sb, "%s<length>%ju</length>\n",
1308 indent, (uintmax_t) mp->mediasize);
1309 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1311 if (mp->type == MD_VNODE && mp->vnode != NULL)
1312 sbuf_printf(sb, "%s<file>%s</file>\n",
1319 g_md_fini(struct g_class *mp __unused)
1323 if (status_dev != NULL)
1324 destroy_dev(status_dev);