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>
92 #include <machine/vmparam.h>
96 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
97 #define MD_EXITING 0x20000 /* Worker thread is exiting. */
100 #define MD_NSECT (10000 * 2)
103 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
104 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
107 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, "");
108 static int md_malloc_wait;
109 SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0, "");
111 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
113 * Preloaded image gets put here.
114 * Applications that patch the object with the image can determine
115 * the size looking at the start and end markers (strings),
116 * so we want them contiguous.
119 u_char start[MD_ROOT_SIZE*1024];
122 .start = "MFS Filesystem goes here",
123 .end = "MFS Filesystem had better STOP here",
127 static g_init_t g_md_init;
128 static g_fini_t g_md_fini;
129 static g_start_t g_md_start;
130 static g_access_t g_md_access;
131 static void g_md_dumpconf(struct sbuf *sb, const char *indent,
132 struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
135 static struct cdev *status_dev = 0;
136 static struct sx md_sx;
137 static struct unrhdr *md_uh;
139 static d_ioctl_t mdctlioctl;
141 static struct cdevsw mdctl_cdevsw = {
142 .d_version = D_VERSION,
143 .d_ioctl = mdctlioctl,
147 struct g_class g_md_class = {
149 .version = G_VERSION,
153 .access = g_md_access,
154 .dumpconf = g_md_dumpconf,
157 DECLARE_GEOM_CLASS(g_md_class, g_md);
160 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
162 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
163 #define NMASK (NINDIR-1)
175 LIST_ENTRY(md_s) list;
176 struct bio_queue_head bio_queue;
177 struct mtx queue_mtx;
189 struct g_provider *pp;
190 int (*start)(struct md_s *sc, struct bio *bp);
191 struct devstat *devstat;
193 /* MD_MALLOC related fields */
197 /* MD_PRELOAD related fields */
201 /* MD_VNODE related fields */
206 /* MD_SWAP related fields */
210 static struct indir *
211 new_indir(u_int shift)
215 ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
219 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
220 M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
221 if (ip->array == NULL) {
231 del_indir(struct indir *ip)
234 free(ip->array, M_MDSECT);
239 destroy_indir(struct md_s *sc, struct indir *ip)
243 for (i = 0; i < NINDIR; i++) {
247 destroy_indir(sc, (struct indir*)(ip->array[i]));
248 else if (ip->array[i] > 255)
249 uma_zfree(sc->uma, (void *)(ip->array[i]));
255 * This function does the math and allocates the top level "indir" structure
256 * for a device of "size" sectors.
259 static struct indir *
260 dimension(off_t size)
268 while (rcnt > NINDIR) {
274 * XXX: the top layer is probably not fully populated, so we allocate
275 * too much space for ip->array in here.
277 ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
278 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
279 M_MDSECT, M_WAITOK | M_ZERO);
281 ip->shift = layer * nshift;
286 * Read a given sector
290 s_read(struct indir *ip, off_t offset)
297 printf("s_read(%jd)\n", (intmax_t)offset);
299 for (cip = ip; cip != NULL;) {
301 idx = (offset >> cip->shift) & NMASK;
302 up = cip->array[idx];
303 cip = (struct indir *)up;
306 idx = offset & NMASK;
307 return (cip->array[idx]);
313 * Write a given sector, prune the tree if the value is 0
317 s_write(struct indir *ip, off_t offset, uintptr_t ptr)
319 struct indir *cip, *lip[10];
324 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
331 idx = (offset >> cip->shift) & NMASK;
332 up = cip->array[idx];
334 cip = (struct indir *)up;
337 /* Allocate branch */
339 (uintptr_t)new_indir(cip->shift - nshift);
340 if (cip->array[idx] == 0)
343 up = cip->array[idx];
344 cip = (struct indir *)up;
348 idx = offset & NMASK;
349 up = cip->array[idx];
352 cip->array[idx] = ptr;
357 if (cip->used != 0 || li == 1)
360 while (cip->used == 0 && cip != ip) {
362 idx = (offset >> lip[li]->shift) & NMASK;
363 up = lip[li]->array[idx];
364 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
366 lip[li]->array[idx] = 0;
375 g_md_access(struct g_provider *pp, int r, int w, int e)
379 sc = pp->geom->softc;
381 if (r <= 0 && w <= 0 && e <= 0)
388 if ((sc->flags & MD_READONLY) != 0 && w > 0)
390 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
392 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
399 g_md_start(struct bio *bp)
403 sc = bp->bio_to->geom->softc;
404 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
405 devstat_start_transaction_bio(sc->devstat, bp);
406 mtx_lock(&sc->queue_mtx);
407 bioq_disksort(&sc->bio_queue, bp);
408 mtx_unlock(&sc->queue_mtx);
413 mdstart_malloc(struct md_s *sc, struct bio *bp)
417 off_t secno, nsec, uc;
420 switch (bp->bio_cmd) {
429 nsec = bp->bio_length / sc->sectorsize;
430 secno = bp->bio_offset / sc->sectorsize;
434 osp = s_read(sc->indir, secno);
435 if (bp->bio_cmd == BIO_DELETE) {
437 error = s_write(sc->indir, secno, 0);
438 } else if (bp->bio_cmd == BIO_READ) {
440 bzero(dst, sc->sectorsize);
442 memset(dst, osp, sc->sectorsize);
444 bcopy((void *)osp, dst, sc->sectorsize);
445 cpu_flush_dcache(dst, sc->sectorsize);
448 } else if (bp->bio_cmd == BIO_WRITE) {
449 if (sc->flags & MD_COMPRESS) {
451 for (i = 1; i < sc->sectorsize; i++)
458 if (i == sc->sectorsize) {
460 error = s_write(sc->indir, secno, uc);
463 sp = (uintptr_t)uma_zalloc(sc->uma,
464 md_malloc_wait ? M_WAITOK :
470 bcopy(dst, (void *)sp, sc->sectorsize);
471 error = s_write(sc->indir, secno, sp);
473 bcopy(dst, (void *)osp, sc->sectorsize);
481 uma_zfree(sc->uma, (void*)osp);
485 dst += sc->sectorsize;
492 mdstart_preload(struct md_s *sc, struct bio *bp)
495 switch (bp->bio_cmd) {
497 bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
499 cpu_flush_dcache(bp->bio_data, bp->bio_length);
502 bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
511 mdstart_vnode(struct md_s *sc, struct bio *bp)
513 int error, vfslocked;
521 switch (bp->bio_cmd) {
537 * If an error occurs, we set BIO_ERROR but we do not set
538 * B_INVAL because (for a write anyway), the buffer is
542 if (bp->bio_cmd == BIO_FLUSH) {
543 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
544 (void) vn_start_write(vp, &mp, V_WAIT);
545 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
546 error = VOP_FSYNC(vp, MNT_WAIT, td);
548 vn_finished_write(mp);
549 VFS_UNLOCK_GIANT(vfslocked);
553 bzero(&auio, sizeof(auio));
556 * Special case for BIO_DELETE. On the surface, this is very
557 * similar to BIO_WRITE, except that we write from our own
558 * fixed-length buffer, so we have to loop. The net result is
559 * that the two cases end up having very little in common.
561 if (bp->bio_cmd == BIO_DELETE) {
562 zerosize = ZERO_REGION_SIZE -
563 (ZERO_REGION_SIZE % sc->sectorsize);
564 auio.uio_iov = &aiov;
566 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
567 auio.uio_segflg = UIO_SYSSPACE;
568 auio.uio_rw = UIO_WRITE;
570 end = bp->bio_offset + bp->bio_length;
571 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
572 (void) vn_start_write(vp, &mp, V_WAIT);
573 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
575 while (auio.uio_offset < end) {
576 aiov.iov_base = __DECONST(void *, zero_region);
577 aiov.iov_len = end - auio.uio_offset;
578 if (aiov.iov_len > zerosize)
579 aiov.iov_len = zerosize;
580 auio.uio_resid = aiov.iov_len;
581 error = VOP_WRITE(vp, &auio,
582 sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred);
587 vn_finished_write(mp);
588 bp->bio_resid = end - auio.uio_offset;
589 VFS_UNLOCK_GIANT(vfslocked);
593 aiov.iov_base = bp->bio_data;
594 aiov.iov_len = bp->bio_length;
595 auio.uio_iov = &aiov;
597 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
598 auio.uio_segflg = UIO_SYSSPACE;
599 if (bp->bio_cmd == BIO_READ)
600 auio.uio_rw = UIO_READ;
601 else if (bp->bio_cmd == BIO_WRITE)
602 auio.uio_rw = UIO_WRITE;
604 panic("wrong BIO_OP in mdstart_vnode");
605 auio.uio_resid = bp->bio_length;
608 * When reading set IO_DIRECT to try to avoid double-caching
609 * the data. When writing IO_DIRECT is not optimal.
611 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
612 if (bp->bio_cmd == BIO_READ) {
613 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
614 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
617 (void) vn_start_write(vp, &mp, V_WAIT);
618 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
619 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
622 vn_finished_write(mp);
624 VFS_UNLOCK_GIANT(vfslocked);
625 bp->bio_resid = auio.uio_resid;
630 mdstart_swap(struct md_s *sc, struct bio *bp)
633 int rv, offs, len, lastend;
634 vm_pindex_t i, lastp;
638 switch (bp->bio_cmd) {
650 * offs is the offset at which to start operating on the
651 * next (ie, first) page. lastp is the last page on
652 * which we're going to operate. lastend is the ending
653 * position within that last page (ie, PAGE_SIZE if
654 * we're operating on complete aligned pages).
656 offs = bp->bio_offset % PAGE_SIZE;
657 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
658 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
661 VM_OBJECT_LOCK(sc->object);
662 vm_object_pip_add(sc->object, 1);
663 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
664 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
666 m = vm_page_grab(sc->object, i,
667 VM_ALLOC_NORMAL|VM_ALLOC_RETRY);
668 VM_OBJECT_UNLOCK(sc->object);
670 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
671 VM_OBJECT_LOCK(sc->object);
672 if (bp->bio_cmd == BIO_READ) {
673 if (m->valid != VM_PAGE_BITS_ALL)
674 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
675 if (rv == VM_PAGER_ERROR) {
681 bcopy((void *)(sf_buf_kva(sf) + offs), p, len);
682 cpu_flush_dcache(p, len);
683 } else if (bp->bio_cmd == BIO_WRITE) {
684 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
685 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
686 if (rv == VM_PAGER_ERROR) {
692 bcopy(p, (void *)(sf_buf_kva(sf) + offs), len);
693 m->valid = VM_PAGE_BITS_ALL;
694 } else if (bp->bio_cmd == BIO_DELETE) {
695 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
696 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
697 if (rv == VM_PAGER_ERROR) {
703 if (len != PAGE_SIZE) {
704 bzero((void *)(sf_buf_kva(sf) + offs), len);
705 vm_page_clear_dirty(m, offs, len);
706 m->valid = VM_PAGE_BITS_ALL;
708 vm_pager_page_unswapped(m);
713 vm_page_lock_queues();
714 if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
718 if (bp->bio_cmd == BIO_WRITE)
720 vm_page_unlock_queues();
722 /* Actions on further pages start at offset 0 */
723 p += PAGE_SIZE - offs;
726 vm_object_pip_subtract(sc->object, 1);
727 vm_object_set_writeable_dirty(sc->object);
728 VM_OBJECT_UNLOCK(sc->object);
729 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
733 md_kthread(void *arg)
740 thread_lock(curthread);
741 sched_prio(curthread, PRIBIO);
742 thread_unlock(curthread);
743 if (sc->type == MD_VNODE)
744 curthread->td_pflags |= TDP_NORUNNINGBUF;
747 mtx_lock(&sc->queue_mtx);
748 if (sc->flags & MD_SHUTDOWN) {
749 sc->flags |= MD_EXITING;
750 mtx_unlock(&sc->queue_mtx);
753 bp = bioq_takefirst(&sc->bio_queue);
755 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
758 mtx_unlock(&sc->queue_mtx);
759 if (bp->bio_cmd == BIO_GETATTR) {
760 if ((sc->fwsectors && sc->fwheads &&
761 (g_handleattr_int(bp, "GEOM::fwsectors",
763 g_handleattr_int(bp, "GEOM::fwheads",
765 g_handleattr_int(bp, "GEOM::candelete", 1))
770 error = sc->start(sc, bp);
774 bp->bio_completed = bp->bio_length;
775 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
776 devstat_end_transaction_bio(sc->devstat, bp);
777 g_io_deliver(bp, error);
787 LIST_FOREACH(sc, &md_softc_list, list) {
788 if (sc->unit == unit)
795 mdnew(int unit, int *errp, enum md_types type)
802 unit = alloc_unr(md_uh);
804 unit = alloc_unr_specific(md_uh, unit);
811 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
813 bioq_init(&sc->bio_queue);
814 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
816 sprintf(sc->name, "md%d", unit);
817 LIST_INSERT_HEAD(&md_softc_list, sc, list);
818 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
821 LIST_REMOVE(sc, list);
822 mtx_destroy(&sc->queue_mtx);
823 free_unr(md_uh, sc->unit);
830 mdinit(struct md_s *sc)
833 struct g_provider *pp;
836 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
838 pp = g_new_providerf(gp, "md%d", sc->unit);
839 pp->mediasize = sc->mediasize;
840 pp->sectorsize = sc->sectorsize;
843 g_error_provider(pp, 0);
845 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
846 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
850 * XXX: we should check that the range they feed us is mapped.
851 * XXX: we should implement read-only.
855 mdcreate_preload(struct md_s *sc, struct md_ioctl *mdio)
858 if (mdio->md_options & ~(MD_AUTOUNIT | MD_FORCE))
860 if (mdio->md_base == 0)
862 sc->flags = mdio->md_options & MD_FORCE;
863 /* Cast to pointer size, then to pointer to avoid warning */
864 sc->pl_ptr = (u_char *)(uintptr_t)mdio->md_base;
865 sc->pl_len = (size_t)sc->mediasize;
871 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
878 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
880 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
882 /* Compression doesn't make sense if we have reserved space */
883 if (mdio->md_options & MD_RESERVE)
884 mdio->md_options &= ~MD_COMPRESS;
885 if (mdio->md_fwsectors != 0)
886 sc->fwsectors = mdio->md_fwsectors;
887 if (mdio->md_fwheads != 0)
888 sc->fwheads = mdio->md_fwheads;
889 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
890 sc->indir = dimension(sc->mediasize / sc->sectorsize);
891 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
893 if (mdio->md_options & MD_RESERVE) {
896 nsectors = sc->mediasize / sc->sectorsize;
897 for (u = 0; u < nsectors; u++) {
898 sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
899 M_WAITOK : M_NOWAIT) | M_ZERO);
901 error = s_write(sc->indir, u, sp);
913 mdsetcred(struct md_s *sc, struct ucred *cred)
919 * Set credits in our softc
924 sc->cred = crhold(cred);
927 * Horrible kludge to establish credentials for NFS XXX.
934 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
935 bzero(&auio, sizeof(auio));
937 aiov.iov_base = tmpbuf;
938 aiov.iov_len = sc->sectorsize;
939 auio.uio_iov = &aiov;
942 auio.uio_rw = UIO_READ;
943 auio.uio_segflg = UIO_SYSSPACE;
944 auio.uio_resid = aiov.iov_len;
945 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
946 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
947 VOP_UNLOCK(sc->vnode, 0);
948 free(tmpbuf, M_TEMP);
954 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
958 int error, flags, vfslocked;
960 error = copyinstr(mdio->md_file, sc->file, sizeof(sc->file), NULL);
963 flags = FREAD|FWRITE;
965 * If the user specified that this is a read only device, unset the
966 * FWRITE mask before trying to open the backing store.
968 if ((mdio->md_options & MD_READONLY) != 0)
970 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, sc->file, td);
971 error = vn_open(&nd, &flags, 0, NULL);
974 vfslocked = NDHASGIANT(&nd);
975 NDFREE(&nd, NDF_ONLY_PNBUF);
976 if (nd.ni_vp->v_type != VREG) {
980 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
983 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
984 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
985 if (nd.ni_vp->v_iflag & VI_DOOMED) {
986 /* Forced unmount. */
991 nd.ni_vp->v_vflag |= VV_MD;
992 VOP_UNLOCK(nd.ni_vp, 0);
994 if (mdio->md_fwsectors != 0)
995 sc->fwsectors = mdio->md_fwsectors;
996 if (mdio->md_fwheads != 0)
997 sc->fwheads = mdio->md_fwheads;
998 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
999 if (!(flags & FWRITE))
1000 sc->flags |= MD_READONLY;
1001 sc->vnode = nd.ni_vp;
1003 error = mdsetcred(sc, td->td_ucred);
1006 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1007 nd.ni_vp->v_vflag &= ~VV_MD;
1010 VFS_UNLOCK_GIANT(vfslocked);
1013 VOP_UNLOCK(nd.ni_vp, 0);
1014 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
1015 VFS_UNLOCK_GIANT(vfslocked);
1020 mddestroy(struct md_s *sc, struct thread *td)
1025 sc->gp->softc = NULL;
1027 g_wither_geom(sc->gp, ENXIO);
1028 g_topology_unlock();
1033 devstat_remove_entry(sc->devstat);
1036 mtx_lock(&sc->queue_mtx);
1037 sc->flags |= MD_SHUTDOWN;
1039 while (!(sc->flags & MD_EXITING))
1040 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1041 mtx_unlock(&sc->queue_mtx);
1042 mtx_destroy(&sc->queue_mtx);
1043 if (sc->vnode != NULL) {
1044 vfslocked = VFS_LOCK_GIANT(sc->vnode->v_mount);
1045 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1046 sc->vnode->v_vflag &= ~VV_MD;
1047 VOP_UNLOCK(sc->vnode, 0);
1048 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1049 FREAD : (FREAD|FWRITE), sc->cred, td);
1050 VFS_UNLOCK_GIANT(vfslocked);
1052 if (sc->cred != NULL)
1054 if (sc->object != NULL)
1055 vm_object_deallocate(sc->object);
1057 destroy_indir(sc, sc->indir);
1059 uma_zdestroy(sc->uma);
1061 LIST_REMOVE(sc, list);
1062 free_unr(md_uh, sc->unit);
1068 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1074 * Range check. Disallow negative sizes or any size less then the
1075 * size of a page. Then round to a page.
1077 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1081 * Allocate an OBJT_SWAP object.
1083 * Note the truncation.
1086 npage = mdio->md_mediasize / PAGE_SIZE;
1087 if (mdio->md_fwsectors != 0)
1088 sc->fwsectors = mdio->md_fwsectors;
1089 if (mdio->md_fwheads != 0)
1090 sc->fwheads = mdio->md_fwheads;
1091 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1092 VM_PROT_DEFAULT, 0, td->td_ucred);
1093 if (sc->object == NULL)
1095 sc->flags = mdio->md_options & MD_FORCE;
1096 if (mdio->md_options & MD_RESERVE) {
1097 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1102 error = mdsetcred(sc, td->td_ucred);
1105 vm_object_deallocate(sc->object);
1113 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1115 struct md_ioctl *mdio;
1121 printf("mdctlioctl(%s %lx %p %x %p)\n",
1122 devtoname(dev), cmd, addr, flags, td);
1124 mdio = (struct md_ioctl *)addr;
1125 if (mdio->md_version != MDIOVERSION)
1129 * We assert the version number in the individual ioctl
1130 * handlers instead of out here because (a) it is possible we
1131 * may add another ioctl in the future which doesn't read an
1132 * mdio, and (b) the correct return value for an unknown ioctl
1133 * is ENOIOCTL, not EINVAL.
1138 switch (mdio->md_type) {
1147 if (mdio->md_sectorsize == 0)
1148 sectsize = DEV_BSIZE;
1150 sectsize = mdio->md_sectorsize;
1151 if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
1153 if (mdio->md_options & MD_AUTOUNIT)
1154 sc = mdnew(-1, &error, mdio->md_type);
1156 if (mdio->md_unit > INT_MAX)
1158 sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1162 if (mdio->md_options & MD_AUTOUNIT)
1163 mdio->md_unit = sc->unit;
1164 sc->mediasize = mdio->md_mediasize;
1165 sc->sectorsize = sectsize;
1169 sc->start = mdstart_malloc;
1170 error = mdcreate_malloc(sc, mdio);
1173 sc->start = mdstart_preload;
1174 error = mdcreate_preload(sc, mdio);
1177 sc->start = mdstart_vnode;
1178 error = mdcreate_vnode(sc, mdio, td);
1181 sc->start = mdstart_swap;
1182 error = mdcreate_swap(sc, mdio, td);
1190 /* Prune off any residual fractional sector */
1191 i = sc->mediasize % sc->sectorsize;
1197 if (mdio->md_mediasize != 0 ||
1198 (mdio->md_options & ~MD_FORCE) != 0)
1201 sc = mdfind(mdio->md_unit);
1204 if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1205 !(mdio->md_options & MD_FORCE))
1207 return (mddestroy(sc, td));
1209 sc = mdfind(mdio->md_unit);
1212 mdio->md_type = sc->type;
1213 mdio->md_options = sc->flags;
1214 mdio->md_mediasize = sc->mediasize;
1215 mdio->md_sectorsize = sc->sectorsize;
1216 if (sc->type == MD_VNODE)
1217 error = copyout(sc->file, mdio->md_file,
1218 strlen(sc->file) + 1);
1222 LIST_FOREACH(sc, &md_softc_list, list) {
1223 if (i == MDNPAD - 1)
1224 mdio->md_pad[i] = -1;
1226 mdio->md_pad[i++] = sc->unit;
1228 mdio->md_pad[0] = i - 1;
1236 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1241 error = xmdctlioctl(dev, cmd, addr, flags, td);
1247 md_preloaded(u_char *image, size_t length)
1252 sc = mdnew(-1, &error, MD_PRELOAD);
1255 sc->mediasize = length;
1256 sc->sectorsize = DEV_BSIZE;
1258 sc->pl_len = length;
1259 sc->start = mdstart_preload;
1262 rootdevnames[0] = "ufs:/dev/md0";
1268 g_md_init(struct g_class *mp __unused)
1272 u_char *ptr, *name, *type;
1276 /* figure out log2(NINDIR) */
1277 for (i = NINDIR, nshift = -1; i; nshift++)
1281 sx_init(&md_sx, "MD config lock");
1282 g_topology_unlock();
1283 md_uh = new_unrhdr(0, INT_MAX, NULL);
1286 md_preloaded(mfs_root.start, sizeof(mfs_root.start));
1289 /* XXX: are preload_* static or do they need Giant ? */
1290 while ((mod = preload_search_next_name(mod)) != NULL) {
1291 name = (char *)preload_search_info(mod, MODINFO_NAME);
1294 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1297 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1299 c = preload_search_info(mod, MODINFO_ADDR);
1300 ptr = *(u_char **)c;
1301 c = preload_search_info(mod, MODINFO_SIZE);
1303 printf("%s%d: Preloaded image <%s> %d bytes at %p\n",
1304 MD_NAME, mdunits, name, len, ptr);
1306 md_preloaded(ptr, len);
1309 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
1315 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1316 struct g_consumer *cp __unused, struct g_provider *pp)
1344 if (indent == NULL) {
1345 sbuf_printf(sb, " u %d", mp->unit);
1346 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1347 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1348 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1349 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1350 sbuf_printf(sb, " t %s", type);
1351 if (mp->type == MD_VNODE && mp->vnode != NULL)
1352 sbuf_printf(sb, " file %s", mp->file);
1354 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1356 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1357 indent, (uintmax_t) mp->sectorsize);
1358 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1359 indent, (uintmax_t) mp->fwheads);
1360 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1361 indent, (uintmax_t) mp->fwsectors);
1362 sbuf_printf(sb, "%s<length>%ju</length>\n",
1363 indent, (uintmax_t) mp->mediasize);
1364 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1366 if (mp->type == MD_VNODE && mp->vnode != NULL)
1367 sbuf_printf(sb, "%s<file>%s</file>\n",
1374 g_md_fini(struct g_class *mp __unused)
1378 if (status_dev != NULL)
1379 destroy_dev(status_dev);
1380 delete_unrhdr(md_uh);