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.
21 * Copyright (c) 2013 The FreeBSD Foundation
22 * All rights reserved.
24 * This code is derived from software contributed to Berkeley by
25 * the Systems Programming Group of the University of Utah Computer
28 * Portions of this software were developed by Konstantin Belousov
29 * under sponsorship from the FreeBSD Foundation.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * from: Utah Hdr: vn.c 1.13 94/04/02
57 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
58 * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
64 #include <sys/param.h>
65 #include <sys/systm.h>
69 #include <sys/devicestat.h>
70 #include <sys/fcntl.h>
71 #include <sys/kernel.h>
72 #include <sys/kthread.h>
73 #include <sys/limits.h>
74 #include <sys/linker.h>
76 #include <sys/malloc.h>
77 #include <sys/mdioctl.h>
78 #include <sys/mount.h>
79 #include <sys/mutex.h>
81 #include <sys/namei.h>
83 #include <sys/queue.h>
84 #include <sys/rwlock.h>
86 #include <sys/sched.h>
87 #include <sys/sf_buf.h>
88 #include <sys/sysctl.h>
89 #include <sys/vnode.h>
91 #include <geom/geom.h>
92 #include <geom/geom_int.h>
95 #include <vm/vm_param.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pager.h>
99 #include <vm/swap_pager.h>
104 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
105 #define MD_EXITING 0x20000 /* Worker thread is exiting. */
108 #define MD_NSECT (10000 * 2)
111 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
112 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
115 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0,
116 "Enable md(4) debug messages");
117 static int md_malloc_wait;
118 SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0,
119 "Allow malloc to wait for memory allocations");
121 #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE)
122 #define MD_ROOT_FSTYPE "ufs"
125 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
127 * Preloaded image gets put here.
128 * Applications that patch the object with the image can determine
129 * the size looking at the start and end markers (strings),
130 * so we want them contiguous.
133 u_char start[MD_ROOT_SIZE*1024];
136 .start = "MFS Filesystem goes here",
137 .end = "MFS Filesystem had better STOP here",
141 static g_init_t g_md_init;
142 static g_fini_t g_md_fini;
143 static g_start_t g_md_start;
144 static g_access_t g_md_access;
145 static void g_md_dumpconf(struct sbuf *sb, const char *indent,
146 struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
148 static struct cdev *status_dev = 0;
149 static struct sx md_sx;
150 static struct unrhdr *md_uh;
152 static d_ioctl_t mdctlioctl;
154 static struct cdevsw mdctl_cdevsw = {
155 .d_version = D_VERSION,
156 .d_ioctl = mdctlioctl,
160 struct g_class g_md_class = {
162 .version = G_VERSION,
166 .access = g_md_access,
167 .dumpconf = g_md_dumpconf,
170 DECLARE_GEOM_CLASS(g_md_class, g_md);
173 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
175 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
176 #define NMASK (NINDIR-1)
179 static int md_vnode_pbuf_freecnt;
190 LIST_ENTRY(md_s) list;
191 struct bio_queue_head bio_queue;
192 struct mtx queue_mtx;
205 struct g_provider *pp;
206 int (*start)(struct md_s *sc, struct bio *bp);
207 struct devstat *devstat;
209 /* MD_MALLOC related fields */
213 /* MD_PRELOAD related fields */
217 /* MD_VNODE related fields */
222 /* MD_SWAP related fields */
226 static struct indir *
227 new_indir(u_int shift)
231 ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
235 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
236 M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
237 if (ip->array == NULL) {
247 del_indir(struct indir *ip)
250 free(ip->array, M_MDSECT);
255 destroy_indir(struct md_s *sc, struct indir *ip)
259 for (i = 0; i < NINDIR; i++) {
263 destroy_indir(sc, (struct indir*)(ip->array[i]));
264 else if (ip->array[i] > 255)
265 uma_zfree(sc->uma, (void *)(ip->array[i]));
271 * This function does the math and allocates the top level "indir" structure
272 * for a device of "size" sectors.
275 static struct indir *
276 dimension(off_t size)
284 while (rcnt > NINDIR) {
290 * XXX: the top layer is probably not fully populated, so we allocate
291 * too much space for ip->array in here.
293 ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
294 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
295 M_MDSECT, M_WAITOK | M_ZERO);
297 ip->shift = layer * nshift;
302 * Read a given sector
306 s_read(struct indir *ip, off_t offset)
313 printf("s_read(%jd)\n", (intmax_t)offset);
315 for (cip = ip; cip != NULL;) {
317 idx = (offset >> cip->shift) & NMASK;
318 up = cip->array[idx];
319 cip = (struct indir *)up;
322 idx = offset & NMASK;
323 return (cip->array[idx]);
329 * Write a given sector, prune the tree if the value is 0
333 s_write(struct indir *ip, off_t offset, uintptr_t ptr)
335 struct indir *cip, *lip[10];
340 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
347 idx = (offset >> cip->shift) & NMASK;
348 up = cip->array[idx];
350 cip = (struct indir *)up;
353 /* Allocate branch */
355 (uintptr_t)new_indir(cip->shift - nshift);
356 if (cip->array[idx] == 0)
359 up = cip->array[idx];
360 cip = (struct indir *)up;
364 idx = offset & NMASK;
365 up = cip->array[idx];
368 cip->array[idx] = ptr;
373 if (cip->used != 0 || li == 1)
376 while (cip->used == 0 && cip != ip) {
378 idx = (offset >> lip[li]->shift) & NMASK;
379 up = lip[li]->array[idx];
380 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
382 lip[li]->array[idx] = 0;
391 g_md_access(struct g_provider *pp, int r, int w, int e)
395 sc = pp->geom->softc;
397 if (r <= 0 && w <= 0 && e <= 0)
404 if ((sc->flags & MD_READONLY) != 0 && w > 0)
406 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
408 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
415 g_md_start(struct bio *bp)
419 sc = bp->bio_to->geom->softc;
420 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) {
421 mtx_lock(&sc->stat_mtx);
422 devstat_start_transaction_bio(sc->devstat, bp);
423 mtx_unlock(&sc->stat_mtx);
425 mtx_lock(&sc->queue_mtx);
426 bioq_disksort(&sc->bio_queue, bp);
427 mtx_unlock(&sc->queue_mtx);
431 #define MD_MALLOC_MOVE_ZERO 1
432 #define MD_MALLOC_MOVE_FILL 2
433 #define MD_MALLOC_MOVE_READ 3
434 #define MD_MALLOC_MOVE_WRITE 4
435 #define MD_MALLOC_MOVE_CMP 5
438 md_malloc_move(vm_page_t **mp, int *ma_offs, unsigned sectorsize,
439 void *ptr, u_char fill, int op)
446 int error, i, ma_offs1, sz, first_read;
451 /* if (op == MD_MALLOC_MOVE_CMP) { gcc */
459 for (n = sectorsize; n != 0; n -= sz) {
460 sz = imin(PAGE_SIZE - *ma_offs, n);
465 sf = sf_buf_alloc(m, SFB_CPUPRIVATE |
466 (md_malloc_wait ? 0 : SFB_NOWAIT));
472 p = (char *)sf_buf_kva(sf) + *ma_offs;
474 case MD_MALLOC_MOVE_ZERO:
477 case MD_MALLOC_MOVE_FILL:
480 case MD_MALLOC_MOVE_READ:
482 cpu_flush_dcache(p, sz);
484 case MD_MALLOC_MOVE_WRITE:
487 case MD_MALLOC_MOVE_CMP:
488 for (i = 0; i < sz; i++, p++) {
493 } else if (*p != first) {
500 KASSERT(0, ("md_malloc_move unknown op %d\n", op));
506 *ma_offs %= PAGE_SIZE;
509 ptr = (char *)ptr + sz;
515 if (op == MD_MALLOC_MOVE_CMP && error != 0) {
523 mdstart_malloc(struct md_s *sc, struct bio *bp)
527 int i, error, error1, ma_offs, notmapped;
528 off_t secno, nsec, uc;
531 switch (bp->bio_cmd) {
540 notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0;
543 ma_offs = bp->bio_ma_offset;
549 nsec = bp->bio_length / sc->sectorsize;
550 secno = bp->bio_offset / sc->sectorsize;
553 osp = s_read(sc->indir, secno);
554 if (bp->bio_cmd == BIO_DELETE) {
556 error = s_write(sc->indir, secno, 0);
557 } else if (bp->bio_cmd == BIO_READ) {
560 error = md_malloc_move(&m, &ma_offs,
561 sc->sectorsize, NULL, 0,
562 MD_MALLOC_MOVE_ZERO);
564 bzero(dst, sc->sectorsize);
565 } else if (osp <= 255) {
567 error = md_malloc_move(&m, &ma_offs,
568 sc->sectorsize, NULL, osp,
569 MD_MALLOC_MOVE_FILL);
571 memset(dst, osp, sc->sectorsize);
574 error = md_malloc_move(&m, &ma_offs,
575 sc->sectorsize, (void *)osp, 0,
576 MD_MALLOC_MOVE_READ);
578 bcopy((void *)osp, dst, sc->sectorsize);
579 cpu_flush_dcache(dst, sc->sectorsize);
583 } else if (bp->bio_cmd == BIO_WRITE) {
584 if (sc->flags & MD_COMPRESS) {
586 error1 = md_malloc_move(&m, &ma_offs,
587 sc->sectorsize, &uc, 0,
589 i = error1 == 0 ? sc->sectorsize : 0;
592 for (i = 1; i < sc->sectorsize; i++) {
601 if (i == sc->sectorsize) {
603 error = s_write(sc->indir, secno, uc);
606 sp = (uintptr_t)uma_zalloc(sc->uma,
607 md_malloc_wait ? M_WAITOK :
614 error = md_malloc_move(&m,
615 &ma_offs, sc->sectorsize,
617 MD_MALLOC_MOVE_WRITE);
619 bcopy(dst, (void *)sp,
622 error = s_write(sc->indir, secno, sp);
625 error = md_malloc_move(&m,
626 &ma_offs, sc->sectorsize,
628 MD_MALLOC_MOVE_WRITE);
630 bcopy(dst, (void *)osp,
640 uma_zfree(sc->uma, (void*)osp);
645 dst += sc->sectorsize;
652 mdstart_preload(struct md_s *sc, struct bio *bp)
655 switch (bp->bio_cmd) {
657 bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
659 cpu_flush_dcache(bp->bio_data, bp->bio_length);
662 bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
671 mdstart_vnode(struct md_s *sc, struct bio *bp)
682 switch (bp->bio_cmd) {
698 * If an error occurs, we set BIO_ERROR but we do not set
699 * B_INVAL because (for a write anyway), the buffer is
703 if (bp->bio_cmd == BIO_FLUSH) {
704 (void) vn_start_write(vp, &mp, V_WAIT);
705 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
706 error = VOP_FSYNC(vp, MNT_WAIT, td);
708 vn_finished_write(mp);
712 bzero(&auio, sizeof(auio));
715 * Special case for BIO_DELETE. On the surface, this is very
716 * similar to BIO_WRITE, except that we write from our own
717 * fixed-length buffer, so we have to loop. The net result is
718 * that the two cases end up having very little in common.
720 if (bp->bio_cmd == BIO_DELETE) {
721 zerosize = ZERO_REGION_SIZE -
722 (ZERO_REGION_SIZE % sc->sectorsize);
723 auio.uio_iov = &aiov;
725 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
726 auio.uio_segflg = UIO_SYSSPACE;
727 auio.uio_rw = UIO_WRITE;
729 end = bp->bio_offset + bp->bio_length;
730 (void) vn_start_write(vp, &mp, V_WAIT);
731 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
733 while (auio.uio_offset < end) {
734 aiov.iov_base = __DECONST(void *, zero_region);
735 aiov.iov_len = end - auio.uio_offset;
736 if (aiov.iov_len > zerosize)
737 aiov.iov_len = zerosize;
738 auio.uio_resid = aiov.iov_len;
739 error = VOP_WRITE(vp, &auio,
740 sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred);
745 vn_finished_write(mp);
746 bp->bio_resid = end - auio.uio_offset;
750 KASSERT(bp->bio_length <= MAXPHYS, ("bio_length %jd",
751 (uintmax_t)bp->bio_length));
752 if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
754 aiov.iov_base = bp->bio_data;
756 pb = getpbuf(&md_vnode_pbuf_freecnt);
757 pmap_qenter((vm_offset_t)pb->b_data, bp->bio_ma, bp->bio_ma_n);
758 aiov.iov_base = (void *)((vm_offset_t)pb->b_data +
761 aiov.iov_len = bp->bio_length;
762 auio.uio_iov = &aiov;
764 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
765 auio.uio_segflg = UIO_SYSSPACE;
766 if (bp->bio_cmd == BIO_READ)
767 auio.uio_rw = UIO_READ;
768 else if (bp->bio_cmd == BIO_WRITE)
769 auio.uio_rw = UIO_WRITE;
771 panic("wrong BIO_OP in mdstart_vnode");
772 auio.uio_resid = bp->bio_length;
775 * When reading set IO_DIRECT to try to avoid double-caching
776 * the data. When writing IO_DIRECT is not optimal.
778 if (bp->bio_cmd == BIO_READ) {
779 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
780 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
783 (void) vn_start_write(vp, &mp, V_WAIT);
784 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
785 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
788 vn_finished_write(mp);
790 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
791 pmap_qremove((vm_offset_t)pb->b_data, bp->bio_ma_n);
792 relpbuf(pb, &md_vnode_pbuf_freecnt);
794 bp->bio_resid = auio.uio_resid;
799 mdstart_swap(struct md_s *sc, struct bio *bp)
803 vm_pindex_t i, lastp;
804 int rv, ma_offs, offs, len, lastend;
806 switch (bp->bio_cmd) {
816 ma_offs = (bp->bio_flags & BIO_UNMAPPED) == 0 ? 0 : bp->bio_ma_offset;
819 * offs is the offset at which to start operating on the
820 * next (ie, first) page. lastp is the last page on
821 * which we're going to operate. lastend is the ending
822 * position within that last page (ie, PAGE_SIZE if
823 * we're operating on complete aligned pages).
825 offs = bp->bio_offset % PAGE_SIZE;
826 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
827 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
830 VM_OBJECT_WLOCK(sc->object);
831 vm_object_pip_add(sc->object, 1);
832 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
833 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
834 m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM);
835 if (bp->bio_cmd == BIO_READ) {
836 if (m->valid == VM_PAGE_BITS_ALL)
839 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
840 if (rv == VM_PAGER_ERROR) {
843 } else if (rv == VM_PAGER_FAIL) {
845 * Pager does not have the page. Zero
846 * the allocated page, and mark it as
847 * valid. Do not set dirty, the page
848 * can be recreated if thrown out.
851 m->valid = VM_PAGE_BITS_ALL;
853 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
854 pmap_copy_pages(&m, offs, bp->bio_ma,
857 physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len);
858 cpu_flush_dcache(p, len);
860 } else if (bp->bio_cmd == BIO_WRITE) {
861 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
862 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
865 if (rv == VM_PAGER_ERROR) {
869 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
870 pmap_copy_pages(bp->bio_ma, ma_offs, &m,
873 physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len);
875 m->valid = VM_PAGE_BITS_ALL;
876 } else if (bp->bio_cmd == BIO_DELETE) {
877 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
878 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
881 if (rv == VM_PAGER_ERROR) {
885 if (len != PAGE_SIZE) {
886 pmap_zero_page_area(m, offs, len);
887 vm_page_clear_dirty(m, offs, len);
888 m->valid = VM_PAGE_BITS_ALL;
890 vm_pager_page_unswapped(m);
894 if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
899 if (bp->bio_cmd == BIO_WRITE) {
901 vm_pager_page_unswapped(m);
904 /* Actions on further pages start at offset 0 */
905 p += PAGE_SIZE - offs;
909 vm_object_pip_subtract(sc->object, 1);
910 VM_OBJECT_WUNLOCK(sc->object);
911 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
915 mdstart_null(struct md_s *sc, struct bio *bp)
918 switch (bp->bio_cmd) {
920 bzero(bp->bio_data, bp->bio_length);
921 cpu_flush_dcache(bp->bio_data, bp->bio_length);
931 md_kthread(void *arg)
938 thread_lock(curthread);
939 sched_prio(curthread, PRIBIO);
940 thread_unlock(curthread);
941 if (sc->type == MD_VNODE)
942 curthread->td_pflags |= TDP_NORUNNINGBUF;
945 mtx_lock(&sc->queue_mtx);
946 if (sc->flags & MD_SHUTDOWN) {
947 sc->flags |= MD_EXITING;
948 mtx_unlock(&sc->queue_mtx);
951 bp = bioq_takefirst(&sc->bio_queue);
953 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
956 mtx_unlock(&sc->queue_mtx);
957 if (bp->bio_cmd == BIO_GETATTR) {
958 if ((sc->fwsectors && sc->fwheads &&
959 (g_handleattr_int(bp, "GEOM::fwsectors",
961 g_handleattr_int(bp, "GEOM::fwheads",
963 g_handleattr_int(bp, "GEOM::candelete", 1))
968 error = sc->start(sc, bp);
972 bp->bio_completed = bp->bio_length;
973 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
974 devstat_end_transaction_bio(sc->devstat, bp);
975 g_io_deliver(bp, error);
985 LIST_FOREACH(sc, &md_softc_list, list) {
986 if (sc->unit == unit)
993 mdnew(int unit, int *errp, enum md_types type)
1000 unit = alloc_unr(md_uh);
1002 unit = alloc_unr_specific(md_uh, unit);
1009 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
1011 bioq_init(&sc->bio_queue);
1012 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
1013 mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF);
1015 sprintf(sc->name, "md%d", unit);
1016 LIST_INSERT_HEAD(&md_softc_list, sc, list);
1017 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
1020 LIST_REMOVE(sc, list);
1021 mtx_destroy(&sc->stat_mtx);
1022 mtx_destroy(&sc->queue_mtx);
1023 free_unr(md_uh, sc->unit);
1030 mdinit(struct md_s *sc)
1033 struct g_provider *pp;
1036 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
1038 pp = g_new_providerf(gp, "md%d", sc->unit);
1039 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
1040 pp->mediasize = sc->mediasize;
1041 pp->sectorsize = sc->sectorsize;
1046 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1054 g_error_provider(pp, 0);
1055 g_topology_unlock();
1056 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
1057 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
1061 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
1068 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
1070 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
1072 /* Compression doesn't make sense if we have reserved space */
1073 if (mdio->md_options & MD_RESERVE)
1074 mdio->md_options &= ~MD_COMPRESS;
1075 if (mdio->md_fwsectors != 0)
1076 sc->fwsectors = mdio->md_fwsectors;
1077 if (mdio->md_fwheads != 0)
1078 sc->fwheads = mdio->md_fwheads;
1079 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
1080 sc->indir = dimension(sc->mediasize / sc->sectorsize);
1081 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
1083 if (mdio->md_options & MD_RESERVE) {
1086 nsectors = sc->mediasize / sc->sectorsize;
1087 for (u = 0; u < nsectors; u++) {
1088 sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
1089 M_WAITOK : M_NOWAIT) | M_ZERO);
1091 error = s_write(sc->indir, u, sp);
1103 mdsetcred(struct md_s *sc, struct ucred *cred)
1109 * Set credits in our softc
1114 sc->cred = crhold(cred);
1117 * Horrible kludge to establish credentials for NFS XXX.
1124 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
1125 bzero(&auio, sizeof(auio));
1127 aiov.iov_base = tmpbuf;
1128 aiov.iov_len = sc->sectorsize;
1129 auio.uio_iov = &aiov;
1130 auio.uio_iovcnt = 1;
1131 auio.uio_offset = 0;
1132 auio.uio_rw = UIO_READ;
1133 auio.uio_segflg = UIO_SYSSPACE;
1134 auio.uio_resid = aiov.iov_len;
1135 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1136 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
1137 VOP_UNLOCK(sc->vnode, 0);
1138 free(tmpbuf, M_TEMP);
1144 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1147 struct nameidata nd;
1152 * Kernel-originated requests must have the filename appended
1153 * to the mdio structure to protect against malicious software.
1155 fname = mdio->md_file;
1156 if ((void *)fname != (void *)(mdio + 1)) {
1157 error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
1161 strlcpy(sc->file, fname, sizeof(sc->file));
1164 * If the user specified that this is a read only device, don't
1165 * set the FWRITE mask before trying to open the backing store.
1167 flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
1168 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
1169 error = vn_open(&nd, &flags, 0, NULL);
1172 NDFREE(&nd, NDF_ONLY_PNBUF);
1173 if (nd.ni_vp->v_type != VREG) {
1177 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
1180 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
1181 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
1182 if (nd.ni_vp->v_iflag & VI_DOOMED) {
1183 /* Forced unmount. */
1188 nd.ni_vp->v_vflag |= VV_MD;
1189 VOP_UNLOCK(nd.ni_vp, 0);
1191 if (mdio->md_fwsectors != 0)
1192 sc->fwsectors = mdio->md_fwsectors;
1193 if (mdio->md_fwheads != 0)
1194 sc->fwheads = mdio->md_fwheads;
1195 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
1196 if (!(flags & FWRITE))
1197 sc->flags |= MD_READONLY;
1198 sc->vnode = nd.ni_vp;
1200 error = mdsetcred(sc, td->td_ucred);
1203 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1204 nd.ni_vp->v_vflag &= ~VV_MD;
1209 VOP_UNLOCK(nd.ni_vp, 0);
1210 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
1215 mddestroy(struct md_s *sc, struct thread *td)
1219 sc->gp->softc = NULL;
1221 g_wither_geom(sc->gp, ENXIO);
1222 g_topology_unlock();
1227 devstat_remove_entry(sc->devstat);
1230 mtx_lock(&sc->queue_mtx);
1231 sc->flags |= MD_SHUTDOWN;
1233 while (!(sc->flags & MD_EXITING))
1234 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1235 mtx_unlock(&sc->queue_mtx);
1236 mtx_destroy(&sc->stat_mtx);
1237 mtx_destroy(&sc->queue_mtx);
1238 if (sc->vnode != NULL) {
1239 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1240 sc->vnode->v_vflag &= ~VV_MD;
1241 VOP_UNLOCK(sc->vnode, 0);
1242 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1243 FREAD : (FREAD|FWRITE), sc->cred, td);
1245 if (sc->cred != NULL)
1247 if (sc->object != NULL)
1248 vm_object_deallocate(sc->object);
1250 destroy_indir(sc, sc->indir);
1252 uma_zdestroy(sc->uma);
1254 LIST_REMOVE(sc, list);
1255 free_unr(md_uh, sc->unit);
1261 mdresize(struct md_s *sc, struct md_ioctl *mdio)
1264 vm_pindex_t oldpages, newpages;
1271 if (mdio->md_mediasize <= 0 ||
1272 (mdio->md_mediasize % PAGE_SIZE) != 0)
1274 oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1275 newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1276 if (newpages < oldpages) {
1277 VM_OBJECT_WLOCK(sc->object);
1278 vm_object_page_remove(sc->object, newpages, 0, 0);
1279 swap_pager_freespace(sc->object, newpages,
1280 oldpages - newpages);
1281 swap_release_by_cred(IDX_TO_OFF(oldpages -
1282 newpages), sc->cred);
1283 sc->object->charge = IDX_TO_OFF(newpages);
1284 sc->object->size = newpages;
1285 VM_OBJECT_WUNLOCK(sc->object);
1286 } else if (newpages > oldpages) {
1287 res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1288 oldpages), sc->cred);
1291 if ((mdio->md_options & MD_RESERVE) ||
1292 (sc->flags & MD_RESERVE)) {
1293 error = swap_pager_reserve(sc->object,
1294 oldpages, newpages - oldpages);
1296 swap_release_by_cred(
1297 IDX_TO_OFF(newpages - oldpages),
1302 VM_OBJECT_WLOCK(sc->object);
1303 sc->object->charge = IDX_TO_OFF(newpages);
1304 sc->object->size = newpages;
1305 VM_OBJECT_WUNLOCK(sc->object);
1309 return (EOPNOTSUPP);
1312 sc->mediasize = mdio->md_mediasize;
1314 g_resize_provider(sc->pp, sc->mediasize);
1315 g_topology_unlock();
1320 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1326 * Range check. Disallow negative sizes or any size less then the
1327 * size of a page. Then round to a page.
1329 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1333 * Allocate an OBJT_SWAP object.
1335 * Note the truncation.
1338 npage = mdio->md_mediasize / PAGE_SIZE;
1339 if (mdio->md_fwsectors != 0)
1340 sc->fwsectors = mdio->md_fwsectors;
1341 if (mdio->md_fwheads != 0)
1342 sc->fwheads = mdio->md_fwheads;
1343 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1344 VM_PROT_DEFAULT, 0, td->td_ucred);
1345 if (sc->object == NULL)
1347 sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
1348 if (mdio->md_options & MD_RESERVE) {
1349 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1354 error = mdsetcred(sc, td->td_ucred);
1357 vm_object_deallocate(sc->object);
1364 mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1368 * Range check. Disallow negative sizes or any size less then the
1369 * size of a page. Then round to a page.
1371 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1378 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1380 struct md_ioctl *mdio;
1386 printf("mdctlioctl(%s %lx %p %x %p)\n",
1387 devtoname(dev), cmd, addr, flags, td);
1389 mdio = (struct md_ioctl *)addr;
1390 if (mdio->md_version != MDIOVERSION)
1394 * We assert the version number in the individual ioctl
1395 * handlers instead of out here because (a) it is possible we
1396 * may add another ioctl in the future which doesn't read an
1397 * mdio, and (b) the correct return value for an unknown ioctl
1398 * is ENOIOCTL, not EINVAL.
1403 switch (mdio->md_type) {
1413 if (mdio->md_sectorsize == 0)
1414 sectsize = DEV_BSIZE;
1416 sectsize = mdio->md_sectorsize;
1417 if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
1419 if (mdio->md_options & MD_AUTOUNIT)
1420 sc = mdnew(-1, &error, mdio->md_type);
1422 if (mdio->md_unit > INT_MAX)
1424 sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1428 if (mdio->md_options & MD_AUTOUNIT)
1429 mdio->md_unit = sc->unit;
1430 sc->mediasize = mdio->md_mediasize;
1431 sc->sectorsize = sectsize;
1435 sc->start = mdstart_malloc;
1436 error = mdcreate_malloc(sc, mdio);
1440 * We disallow attaching preloaded memory disks via
1441 * ioctl. Preloaded memory disks are automatically
1442 * attached in g_md_init().
1447 sc->start = mdstart_vnode;
1448 error = mdcreate_vnode(sc, mdio, td);
1451 sc->start = mdstart_swap;
1452 error = mdcreate_swap(sc, mdio, td);
1455 sc->start = mdstart_null;
1456 error = mdcreate_null(sc, mdio, td);
1464 /* Prune off any residual fractional sector */
1465 i = sc->mediasize % sc->sectorsize;
1471 if (mdio->md_mediasize != 0 ||
1472 (mdio->md_options & ~MD_FORCE) != 0)
1475 sc = mdfind(mdio->md_unit);
1478 if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1479 !(mdio->md_options & MD_FORCE))
1481 return (mddestroy(sc, td));
1483 if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1486 sc = mdfind(mdio->md_unit);
1489 if (mdio->md_mediasize < sc->sectorsize)
1491 if (mdio->md_mediasize < sc->mediasize &&
1492 !(sc->flags & MD_FORCE) &&
1493 !(mdio->md_options & MD_FORCE))
1495 return (mdresize(sc, mdio));
1497 sc = mdfind(mdio->md_unit);
1500 mdio->md_type = sc->type;
1501 mdio->md_options = sc->flags;
1502 mdio->md_mediasize = sc->mediasize;
1503 mdio->md_sectorsize = sc->sectorsize;
1504 if (sc->type == MD_VNODE)
1505 error = copyout(sc->file, mdio->md_file,
1506 strlen(sc->file) + 1);
1510 LIST_FOREACH(sc, &md_softc_list, list) {
1511 if (i == MDNPAD - 1)
1512 mdio->md_pad[i] = -1;
1514 mdio->md_pad[i++] = sc->unit;
1516 mdio->md_pad[0] = i - 1;
1524 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1529 error = xmdctlioctl(dev, cmd, addr, flags, td);
1535 md_preloaded(u_char *image, size_t length, const char *name)
1540 sc = mdnew(-1, &error, MD_PRELOAD);
1543 sc->mediasize = length;
1544 sc->sectorsize = DEV_BSIZE;
1546 sc->pl_len = length;
1547 sc->start = mdstart_preload;
1550 rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
1554 printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
1555 MD_NAME, sc->unit, name, length, image);
1560 g_md_init(struct g_class *mp __unused)
1563 u_char *ptr, *name, *type;
1567 /* figure out log2(NINDIR) */
1568 for (i = NINDIR, nshift = -1; i; nshift++)
1572 sx_init(&md_sx, "MD config lock");
1573 g_topology_unlock();
1574 md_uh = new_unrhdr(0, INT_MAX, NULL);
1577 md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL);
1580 /* XXX: are preload_* static or do they need Giant ? */
1581 while ((mod = preload_search_next_name(mod)) != NULL) {
1582 name = (char *)preload_search_info(mod, MODINFO_NAME);
1585 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1588 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1590 ptr = preload_fetch_addr(mod);
1591 len = preload_fetch_size(mod);
1592 if (ptr != NULL && len != 0) {
1594 md_preloaded(ptr, len, name);
1598 md_vnode_pbuf_freecnt = nswbuf / 10;
1599 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
1605 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1606 struct g_consumer *cp __unused, struct g_provider *pp)
1637 if (indent == NULL) {
1638 sbuf_printf(sb, " u %d", mp->unit);
1639 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1640 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1641 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1642 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1643 sbuf_printf(sb, " t %s", type);
1644 if (mp->type == MD_VNODE && mp->vnode != NULL)
1645 sbuf_printf(sb, " file %s", mp->file);
1647 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1649 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1650 indent, (uintmax_t) mp->sectorsize);
1651 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1652 indent, (uintmax_t) mp->fwheads);
1653 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1654 indent, (uintmax_t) mp->fwsectors);
1655 sbuf_printf(sb, "%s<length>%ju</length>\n",
1656 indent, (uintmax_t) mp->mediasize);
1657 sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
1658 (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
1659 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
1660 (mp->flags & MD_READONLY) == 0 ? "read-write":
1662 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1664 if (mp->type == MD_VNODE && mp->vnode != NULL) {
1665 sbuf_printf(sb, "%s<file>", indent);
1666 g_conf_printf_escaped(sb, "%s", mp->file);
1667 sbuf_printf(sb, "</file>\n");
1674 g_md_fini(struct g_class *mp __unused)
1678 if (status_dev != NULL)
1679 destroy_dev(status_dev);
1680 delete_unrhdr(md_uh);