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>
102 #include <machine/bus.h>
106 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
107 #define MD_EXITING 0x20000 /* Worker thread is exiting. */
110 #define MD_NSECT (10000 * 2)
113 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
114 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
117 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0,
118 "Enable md(4) debug messages");
119 static int md_malloc_wait;
120 SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0,
121 "Allow malloc to wait for memory allocations");
123 #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE)
124 #define MD_ROOT_FSTYPE "ufs"
127 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
129 * Preloaded image gets put here.
130 * Applications that patch the object with the image can determine
131 * the size looking at the start and end markers (strings),
132 * so we want them contiguous.
135 u_char start[MD_ROOT_SIZE*1024];
138 .start = "MFS Filesystem goes here",
139 .end = "MFS Filesystem had better STOP here",
143 static g_init_t g_md_init;
144 static g_fini_t g_md_fini;
145 static g_start_t g_md_start;
146 static g_access_t g_md_access;
147 static void g_md_dumpconf(struct sbuf *sb, const char *indent,
148 struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
150 static struct cdev *status_dev = 0;
151 static struct sx md_sx;
152 static struct unrhdr *md_uh;
154 static d_ioctl_t mdctlioctl;
156 static struct cdevsw mdctl_cdevsw = {
157 .d_version = D_VERSION,
158 .d_ioctl = mdctlioctl,
162 struct g_class g_md_class = {
164 .version = G_VERSION,
168 .access = g_md_access,
169 .dumpconf = g_md_dumpconf,
172 DECLARE_GEOM_CLASS(g_md_class, g_md);
175 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
177 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
178 #define NMASK (NINDIR-1)
181 static int md_vnode_pbuf_freecnt;
192 LIST_ENTRY(md_s) list;
193 struct bio_queue_head bio_queue;
194 struct mtx queue_mtx;
207 struct g_provider *pp;
208 int (*start)(struct md_s *sc, struct bio *bp);
209 struct devstat *devstat;
211 /* MD_MALLOC related fields */
215 /* MD_PRELOAD related fields */
219 /* MD_VNODE related fields */
224 /* MD_SWAP related fields */
228 static struct indir *
229 new_indir(u_int shift)
233 ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
237 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
238 M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
239 if (ip->array == NULL) {
249 del_indir(struct indir *ip)
252 free(ip->array, M_MDSECT);
257 destroy_indir(struct md_s *sc, struct indir *ip)
261 for (i = 0; i < NINDIR; i++) {
265 destroy_indir(sc, (struct indir*)(ip->array[i]));
266 else if (ip->array[i] > 255)
267 uma_zfree(sc->uma, (void *)(ip->array[i]));
273 * This function does the math and allocates the top level "indir" structure
274 * for a device of "size" sectors.
277 static struct indir *
278 dimension(off_t size)
286 while (rcnt > NINDIR) {
292 * XXX: the top layer is probably not fully populated, so we allocate
293 * too much space for ip->array in here.
295 ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
296 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
297 M_MDSECT, M_WAITOK | M_ZERO);
299 ip->shift = layer * nshift;
304 * Read a given sector
308 s_read(struct indir *ip, off_t offset)
315 printf("s_read(%jd)\n", (intmax_t)offset);
317 for (cip = ip; cip != NULL;) {
319 idx = (offset >> cip->shift) & NMASK;
320 up = cip->array[idx];
321 cip = (struct indir *)up;
324 idx = offset & NMASK;
325 return (cip->array[idx]);
331 * Write a given sector, prune the tree if the value is 0
335 s_write(struct indir *ip, off_t offset, uintptr_t ptr)
337 struct indir *cip, *lip[10];
342 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
349 idx = (offset >> cip->shift) & NMASK;
350 up = cip->array[idx];
352 cip = (struct indir *)up;
355 /* Allocate branch */
357 (uintptr_t)new_indir(cip->shift - nshift);
358 if (cip->array[idx] == 0)
361 up = cip->array[idx];
362 cip = (struct indir *)up;
366 idx = offset & NMASK;
367 up = cip->array[idx];
370 cip->array[idx] = ptr;
375 if (cip->used != 0 || li == 1)
378 while (cip->used == 0 && cip != ip) {
380 idx = (offset >> lip[li]->shift) & NMASK;
381 up = lip[li]->array[idx];
382 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
384 lip[li]->array[idx] = 0;
393 g_md_access(struct g_provider *pp, int r, int w, int e)
397 sc = pp->geom->softc;
399 if (r <= 0 && w <= 0 && e <= 0)
406 if ((sc->flags & MD_READONLY) != 0 && w > 0)
408 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
410 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
417 g_md_start(struct bio *bp)
421 sc = bp->bio_to->geom->softc;
422 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) {
423 mtx_lock(&sc->stat_mtx);
424 devstat_start_transaction_bio(sc->devstat, bp);
425 mtx_unlock(&sc->stat_mtx);
427 mtx_lock(&sc->queue_mtx);
428 bioq_disksort(&sc->bio_queue, bp);
429 mtx_unlock(&sc->queue_mtx);
433 #define MD_MALLOC_MOVE_ZERO 1
434 #define MD_MALLOC_MOVE_FILL 2
435 #define MD_MALLOC_MOVE_READ 3
436 #define MD_MALLOC_MOVE_WRITE 4
437 #define MD_MALLOC_MOVE_CMP 5
440 md_malloc_move_ma(vm_page_t **mp, int *ma_offs, unsigned sectorsize,
441 void *ptr, u_char fill, int op)
448 int error, i, ma_offs1, sz, first_read;
453 /* if (op == MD_MALLOC_MOVE_CMP) { gcc */
461 for (n = sectorsize; n != 0; n -= sz) {
462 sz = imin(PAGE_SIZE - *ma_offs, n);
467 sf = sf_buf_alloc(m, SFB_CPUPRIVATE |
468 (md_malloc_wait ? 0 : SFB_NOWAIT));
474 p = (char *)sf_buf_kva(sf) + *ma_offs;
476 case MD_MALLOC_MOVE_ZERO:
479 case MD_MALLOC_MOVE_FILL:
482 case MD_MALLOC_MOVE_READ:
484 cpu_flush_dcache(p, sz);
486 case MD_MALLOC_MOVE_WRITE:
489 case MD_MALLOC_MOVE_CMP:
490 for (i = 0; i < sz; i++, p++) {
495 } else if (*p != first) {
502 KASSERT(0, ("md_malloc_move_ma unknown op %d\n", op));
508 *ma_offs %= PAGE_SIZE;
511 ptr = (char *)ptr + sz;
517 if (op == MD_MALLOC_MOVE_CMP && error != 0) {
525 md_malloc_move_vlist(bus_dma_segment_t **pvlist, int *pma_offs,
526 unsigned len, void *ptr, u_char fill, int op)
528 bus_dma_segment_t *vlist;
529 uint8_t *p, *end, first;
531 int ma_offs, seg_len;
537 for (; len != 0; len -= seg_len) {
538 seg_len = imin(vlist->ds_len - ma_offs, len);
539 p = (uint8_t *)(uintptr_t)vlist->ds_addr + ma_offs;
541 case MD_MALLOC_MOVE_ZERO:
544 case MD_MALLOC_MOVE_FILL:
545 memset(p, fill, seg_len);
547 case MD_MALLOC_MOVE_READ:
548 bcopy(ptr, p, seg_len);
549 cpu_flush_dcache(p, seg_len);
551 case MD_MALLOC_MOVE_WRITE:
552 bcopy(p, ptr, seg_len);
554 case MD_MALLOC_MOVE_CMP:
557 /* Confirm all following bytes match the first */
564 KASSERT(0, ("md_malloc_move_vlist unknown op %d\n", op));
569 if (ma_offs == vlist->ds_len) {
573 ptr = (uint8_t *)ptr + seg_len;
582 mdstart_malloc(struct md_s *sc, struct bio *bp)
586 bus_dma_segment_t *vlist;
587 int i, error, error1, ma_offs, notmapped;
588 off_t secno, nsec, uc;
591 switch (bp->bio_cmd) {
600 notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0;
601 vlist = (bp->bio_flags & BIO_VLIST) != 0 ?
602 (bus_dma_segment_t *)bp->bio_data : NULL;
605 ma_offs = bp->bio_ma_offset;
607 KASSERT(vlist == NULL, ("vlists cannot be unmapped"));
608 } else if (vlist != NULL) {
609 ma_offs = bp->bio_ma_offset;
615 nsec = bp->bio_length / sc->sectorsize;
616 secno = bp->bio_offset / sc->sectorsize;
619 osp = s_read(sc->indir, secno);
620 if (bp->bio_cmd == BIO_DELETE) {
622 error = s_write(sc->indir, secno, 0);
623 } else if (bp->bio_cmd == BIO_READ) {
626 error = md_malloc_move_ma(&m, &ma_offs,
627 sc->sectorsize, NULL, 0,
628 MD_MALLOC_MOVE_ZERO);
629 } else if (vlist != NULL) {
630 error = md_malloc_move_vlist(&vlist,
631 &ma_offs, sc->sectorsize, NULL, 0,
632 MD_MALLOC_MOVE_ZERO);
634 bzero(dst, sc->sectorsize);
635 } else if (osp <= 255) {
637 error = md_malloc_move_ma(&m, &ma_offs,
638 sc->sectorsize, NULL, osp,
639 MD_MALLOC_MOVE_FILL);
640 } else if (vlist != NULL) {
641 error = md_malloc_move_vlist(&vlist,
642 &ma_offs, sc->sectorsize, NULL, osp,
643 MD_MALLOC_MOVE_FILL);
645 memset(dst, osp, sc->sectorsize);
648 error = md_malloc_move_ma(&m, &ma_offs,
649 sc->sectorsize, (void *)osp, 0,
650 MD_MALLOC_MOVE_READ);
651 } else if (vlist != NULL) {
652 error = md_malloc_move_vlist(&vlist,
653 &ma_offs, sc->sectorsize,
655 MD_MALLOC_MOVE_READ);
657 bcopy((void *)osp, dst, sc->sectorsize);
658 cpu_flush_dcache(dst, sc->sectorsize);
662 } else if (bp->bio_cmd == BIO_WRITE) {
663 if (sc->flags & MD_COMPRESS) {
665 error1 = md_malloc_move_ma(&m, &ma_offs,
666 sc->sectorsize, &uc, 0,
668 i = error1 == 0 ? sc->sectorsize : 0;
669 } else if (vlist != NULL) {
670 error1 = md_malloc_move_vlist(&vlist,
671 &ma_offs, sc->sectorsize, &uc, 0,
673 i = error1 == 0 ? sc->sectorsize : 0;
676 for (i = 1; i < sc->sectorsize; i++) {
685 if (i == sc->sectorsize) {
687 error = s_write(sc->indir, secno, uc);
690 sp = (uintptr_t)uma_zalloc(sc->uma,
691 md_malloc_wait ? M_WAITOK :
698 error = md_malloc_move_ma(&m,
699 &ma_offs, sc->sectorsize,
701 MD_MALLOC_MOVE_WRITE);
702 } else if (vlist != NULL) {
703 error = md_malloc_move_vlist(
705 sc->sectorsize, (void *)sp,
706 0, MD_MALLOC_MOVE_WRITE);
708 bcopy(dst, (void *)sp,
711 error = s_write(sc->indir, secno, sp);
714 error = md_malloc_move_ma(&m,
715 &ma_offs, sc->sectorsize,
717 MD_MALLOC_MOVE_WRITE);
718 } else if (vlist != NULL) {
719 error = md_malloc_move_vlist(
721 sc->sectorsize, (void *)osp,
722 0, MD_MALLOC_MOVE_WRITE);
724 bcopy(dst, (void *)osp,
734 uma_zfree(sc->uma, (void*)osp);
738 if (!notmapped && vlist == NULL)
739 dst += sc->sectorsize;
746 mdcopyto_vlist(void *src, bus_dma_segment_t *vlist, off_t offset, off_t len)
750 while (offset >= vlist->ds_len) {
751 offset -= vlist->ds_len;
756 seg_len = omin(len, vlist->ds_len - offset);
757 bcopy(src, (void *)(uintptr_t)(vlist->ds_addr + offset),
760 src = (uint8_t *)src + seg_len;
767 mdcopyfrom_vlist(bus_dma_segment_t *vlist, off_t offset, void *dst, off_t len)
771 while (offset >= vlist->ds_len) {
772 offset -= vlist->ds_len;
777 seg_len = omin(len, vlist->ds_len - offset);
778 bcopy((void *)(uintptr_t)(vlist->ds_addr + offset), dst,
781 dst = (uint8_t *)dst + seg_len;
788 mdstart_preload(struct md_s *sc, struct bio *bp)
792 p = sc->pl_ptr + bp->bio_offset;
793 switch (bp->bio_cmd) {
795 if ((bp->bio_flags & BIO_VLIST) != 0) {
796 mdcopyto_vlist(p, (bus_dma_segment_t *)bp->bio_data,
797 bp->bio_ma_offset, bp->bio_length);
799 bcopy(p, bp->bio_data, bp->bio_length);
801 cpu_flush_dcache(bp->bio_data, bp->bio_length);
804 if ((bp->bio_flags & BIO_VLIST) != 0) {
805 mdcopyfrom_vlist((bus_dma_segment_t *)bp->bio_data,
806 bp->bio_ma_offset, p, bp->bio_length);
808 bcopy(bp->bio_data, p, bp->bio_length);
817 mdstart_vnode(struct md_s *sc, struct bio *bp)
826 bus_dma_segment_t *vlist;
828 off_t iolen, len, zerosize;
831 switch (bp->bio_cmd) {
833 auio.uio_rw = UIO_READ;
837 auio.uio_rw = UIO_WRITE;
849 ma_offs = bp->bio_ma_offset;
850 len = bp->bio_length;
855 * If an error occurs, we set BIO_ERROR but we do not set
856 * B_INVAL because (for a write anyway), the buffer is
860 if (bp->bio_cmd == BIO_FLUSH) {
861 (void) vn_start_write(vp, &mp, V_WAIT);
862 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
863 error = VOP_FSYNC(vp, MNT_WAIT, td);
865 vn_finished_write(mp);
869 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
870 auio.uio_resid = bp->bio_length;
871 auio.uio_segflg = UIO_SYSSPACE;
874 if (bp->bio_cmd == BIO_DELETE) {
876 * Emulate BIO_DELETE by writing zeros.
878 zerosize = ZERO_REGION_SIZE -
879 (ZERO_REGION_SIZE % sc->sectorsize);
880 auio.uio_iovcnt = howmany(bp->bio_length, zerosize);
881 piov = malloc(sizeof(*piov) * auio.uio_iovcnt, M_MD, M_WAITOK);
884 piov->iov_base = __DECONST(void *, zero_region);
887 piov->iov_len = zerosize;
888 len -= piov->iov_len;
892 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
893 piov = malloc(sizeof(*piov) * bp->bio_ma_n, M_MD, M_WAITOK);
895 vlist = (bus_dma_segment_t *)bp->bio_data;
897 piov->iov_base = (void *)(uintptr_t)(vlist->ds_addr +
899 piov->iov_len = vlist->ds_len - ma_offs;
900 if (piov->iov_len > len)
902 len -= piov->iov_len;
907 auio.uio_iovcnt = piov - auio.uio_iov;
909 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
910 pb = getpbuf(&md_vnode_pbuf_freecnt);
913 npages = atop(min(MAXPHYS, round_page(len + (ma_offs &
915 iolen = min(ptoa(npages) - (ma_offs & PAGE_MASK), len);
916 KASSERT(iolen > 0, ("zero iolen"));
917 pmap_qenter((vm_offset_t)pb->b_data,
918 &bp->bio_ma[atop(ma_offs)], npages);
919 aiov.iov_base = (void *)((vm_offset_t)pb->b_data +
920 (ma_offs & PAGE_MASK));
921 aiov.iov_len = iolen;
922 auio.uio_iov = &aiov;
924 auio.uio_resid = iolen;
926 aiov.iov_base = bp->bio_data;
927 aiov.iov_len = bp->bio_length;
928 auio.uio_iov = &aiov;
932 * When reading set IO_DIRECT to try to avoid double-caching
933 * the data. When writing IO_DIRECT is not optimal.
935 if (auio.uio_rw == UIO_READ) {
936 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
937 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
940 (void) vn_start_write(vp, &mp, V_WAIT);
941 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
942 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
945 vn_finished_write(mp);
949 pmap_qremove((vm_offset_t)pb->b_data, npages);
952 bp->bio_resid -= iolen;
957 relpbuf(pb, &md_vnode_pbuf_freecnt);
962 bp->bio_resid = auio.uio_resid;
967 mdstart_swap(struct md_s *sc, struct bio *bp)
971 vm_pindex_t i, lastp;
972 bus_dma_segment_t *vlist;
973 int rv, ma_offs, offs, len, lastend;
975 switch (bp->bio_cmd) {
985 ma_offs = (bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0 ?
986 bp->bio_ma_offset : 0;
987 vlist = (bp->bio_flags & BIO_VLIST) != 0 ?
988 (bus_dma_segment_t *)bp->bio_data : NULL;
991 * offs is the offset at which to start operating on the
992 * next (ie, first) page. lastp is the last page on
993 * which we're going to operate. lastend is the ending
994 * position within that last page (ie, PAGE_SIZE if
995 * we're operating on complete aligned pages).
997 offs = bp->bio_offset % PAGE_SIZE;
998 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
999 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
1002 VM_OBJECT_WLOCK(sc->object);
1003 vm_object_pip_add(sc->object, 1);
1004 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
1005 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
1006 m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM);
1007 if (bp->bio_cmd == BIO_READ) {
1008 if (m->valid == VM_PAGE_BITS_ALL)
1011 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
1012 if (rv == VM_PAGER_ERROR) {
1015 } else if (rv == VM_PAGER_FAIL) {
1017 * Pager does not have the page. Zero
1018 * the allocated page, and mark it as
1019 * valid. Do not set dirty, the page
1020 * can be recreated if thrown out.
1023 m->valid = VM_PAGE_BITS_ALL;
1025 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
1026 pmap_copy_pages(&m, offs, bp->bio_ma,
1028 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
1029 physcopyout_vlist(VM_PAGE_TO_PHYS(m) + offs,
1030 vlist, ma_offs, len);
1031 cpu_flush_dcache(p, len);
1033 physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len);
1034 cpu_flush_dcache(p, len);
1036 } else if (bp->bio_cmd == BIO_WRITE) {
1037 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
1038 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
1041 if (rv == VM_PAGER_ERROR) {
1045 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
1046 pmap_copy_pages(bp->bio_ma, ma_offs, &m,
1048 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
1049 physcopyin_vlist(vlist, ma_offs,
1050 VM_PAGE_TO_PHYS(m) + offs, len);
1052 physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len);
1054 m->valid = VM_PAGE_BITS_ALL;
1055 } else if (bp->bio_cmd == BIO_DELETE) {
1056 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
1057 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
1060 if (rv == VM_PAGER_ERROR) {
1064 if (len != PAGE_SIZE) {
1065 pmap_zero_page_area(m, offs, len);
1066 vm_page_clear_dirty(m, offs, len);
1067 m->valid = VM_PAGE_BITS_ALL;
1069 vm_pager_page_unswapped(m);
1073 if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
1076 vm_page_activate(m);
1078 if (bp->bio_cmd == BIO_WRITE) {
1080 vm_pager_page_unswapped(m);
1083 /* Actions on further pages start at offset 0 */
1084 p += PAGE_SIZE - offs;
1088 vm_object_pip_subtract(sc->object, 1);
1089 VM_OBJECT_WUNLOCK(sc->object);
1090 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
1094 mdstart_null(struct md_s *sc, struct bio *bp)
1097 switch (bp->bio_cmd) {
1099 bzero(bp->bio_data, bp->bio_length);
1100 cpu_flush_dcache(bp->bio_data, bp->bio_length);
1110 md_kthread(void *arg)
1117 thread_lock(curthread);
1118 sched_prio(curthread, PRIBIO);
1119 thread_unlock(curthread);
1120 if (sc->type == MD_VNODE)
1121 curthread->td_pflags |= TDP_NORUNNINGBUF;
1124 mtx_lock(&sc->queue_mtx);
1125 if (sc->flags & MD_SHUTDOWN) {
1126 sc->flags |= MD_EXITING;
1127 mtx_unlock(&sc->queue_mtx);
1130 bp = bioq_takefirst(&sc->bio_queue);
1132 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
1135 mtx_unlock(&sc->queue_mtx);
1136 if (bp->bio_cmd == BIO_GETATTR) {
1137 if ((sc->fwsectors && sc->fwheads &&
1138 (g_handleattr_int(bp, "GEOM::fwsectors",
1140 g_handleattr_int(bp, "GEOM::fwheads",
1142 g_handleattr_int(bp, "GEOM::candelete", 1))
1147 error = sc->start(sc, bp);
1151 bp->bio_completed = bp->bio_length;
1152 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
1153 devstat_end_transaction_bio(sc->devstat, bp);
1154 g_io_deliver(bp, error);
1159 static struct md_s *
1164 LIST_FOREACH(sc, &md_softc_list, list) {
1165 if (sc->unit == unit)
1171 static struct md_s *
1172 mdnew(int unit, int *errp, enum md_types type)
1179 unit = alloc_unr(md_uh);
1181 unit = alloc_unr_specific(md_uh, unit);
1188 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
1190 bioq_init(&sc->bio_queue);
1191 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
1192 mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF);
1194 sprintf(sc->name, "md%d", unit);
1195 LIST_INSERT_HEAD(&md_softc_list, sc, list);
1196 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
1199 LIST_REMOVE(sc, list);
1200 mtx_destroy(&sc->stat_mtx);
1201 mtx_destroy(&sc->queue_mtx);
1202 free_unr(md_uh, sc->unit);
1209 mdinit(struct md_s *sc)
1212 struct g_provider *pp;
1215 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
1217 pp = g_new_providerf(gp, "md%d", sc->unit);
1218 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
1219 pp->mediasize = sc->mediasize;
1220 pp->sectorsize = sc->sectorsize;
1225 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1233 g_error_provider(pp, 0);
1234 g_topology_unlock();
1235 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
1236 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
1240 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
1247 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
1249 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
1251 /* Compression doesn't make sense if we have reserved space */
1252 if (mdio->md_options & MD_RESERVE)
1253 mdio->md_options &= ~MD_COMPRESS;
1254 if (mdio->md_fwsectors != 0)
1255 sc->fwsectors = mdio->md_fwsectors;
1256 if (mdio->md_fwheads != 0)
1257 sc->fwheads = mdio->md_fwheads;
1258 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
1259 sc->indir = dimension(sc->mediasize / sc->sectorsize);
1260 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
1262 if (mdio->md_options & MD_RESERVE) {
1265 nsectors = sc->mediasize / sc->sectorsize;
1266 for (u = 0; u < nsectors; u++) {
1267 sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
1268 M_WAITOK : M_NOWAIT) | M_ZERO);
1270 error = s_write(sc->indir, u, sp);
1282 mdsetcred(struct md_s *sc, struct ucred *cred)
1288 * Set credits in our softc
1293 sc->cred = crhold(cred);
1296 * Horrible kludge to establish credentials for NFS XXX.
1303 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
1304 bzero(&auio, sizeof(auio));
1306 aiov.iov_base = tmpbuf;
1307 aiov.iov_len = sc->sectorsize;
1308 auio.uio_iov = &aiov;
1309 auio.uio_iovcnt = 1;
1310 auio.uio_offset = 0;
1311 auio.uio_rw = UIO_READ;
1312 auio.uio_segflg = UIO_SYSSPACE;
1313 auio.uio_resid = aiov.iov_len;
1314 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1315 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
1316 VOP_UNLOCK(sc->vnode, 0);
1317 free(tmpbuf, M_TEMP);
1323 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1326 struct nameidata nd;
1331 * Kernel-originated requests must have the filename appended
1332 * to the mdio structure to protect against malicious software.
1334 fname = mdio->md_file;
1335 if ((void *)fname != (void *)(mdio + 1)) {
1336 error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
1340 strlcpy(sc->file, fname, sizeof(sc->file));
1343 * If the user specified that this is a read only device, don't
1344 * set the FWRITE mask before trying to open the backing store.
1346 flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
1347 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
1348 error = vn_open(&nd, &flags, 0, NULL);
1351 NDFREE(&nd, NDF_ONLY_PNBUF);
1352 if (nd.ni_vp->v_type != VREG) {
1356 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
1359 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
1360 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
1361 if (nd.ni_vp->v_iflag & VI_DOOMED) {
1362 /* Forced unmount. */
1367 nd.ni_vp->v_vflag |= VV_MD;
1368 VOP_UNLOCK(nd.ni_vp, 0);
1370 if (mdio->md_fwsectors != 0)
1371 sc->fwsectors = mdio->md_fwsectors;
1372 if (mdio->md_fwheads != 0)
1373 sc->fwheads = mdio->md_fwheads;
1374 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
1375 if (!(flags & FWRITE))
1376 sc->flags |= MD_READONLY;
1377 sc->vnode = nd.ni_vp;
1379 error = mdsetcred(sc, td->td_ucred);
1382 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1383 nd.ni_vp->v_vflag &= ~VV_MD;
1388 VOP_UNLOCK(nd.ni_vp, 0);
1389 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
1394 mddestroy(struct md_s *sc, struct thread *td)
1398 sc->gp->softc = NULL;
1400 g_wither_geom(sc->gp, ENXIO);
1401 g_topology_unlock();
1406 devstat_remove_entry(sc->devstat);
1409 mtx_lock(&sc->queue_mtx);
1410 sc->flags |= MD_SHUTDOWN;
1412 while (!(sc->flags & MD_EXITING))
1413 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1414 mtx_unlock(&sc->queue_mtx);
1415 mtx_destroy(&sc->stat_mtx);
1416 mtx_destroy(&sc->queue_mtx);
1417 if (sc->vnode != NULL) {
1418 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1419 sc->vnode->v_vflag &= ~VV_MD;
1420 VOP_UNLOCK(sc->vnode, 0);
1421 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1422 FREAD : (FREAD|FWRITE), sc->cred, td);
1424 if (sc->cred != NULL)
1426 if (sc->object != NULL)
1427 vm_object_deallocate(sc->object);
1429 destroy_indir(sc, sc->indir);
1431 uma_zdestroy(sc->uma);
1433 LIST_REMOVE(sc, list);
1434 free_unr(md_uh, sc->unit);
1440 mdresize(struct md_s *sc, struct md_ioctl *mdio)
1443 vm_pindex_t oldpages, newpages;
1450 if (mdio->md_mediasize <= 0 ||
1451 (mdio->md_mediasize % PAGE_SIZE) != 0)
1453 oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1454 newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1455 if (newpages < oldpages) {
1456 VM_OBJECT_WLOCK(sc->object);
1457 vm_object_page_remove(sc->object, newpages, 0, 0);
1458 swap_pager_freespace(sc->object, newpages,
1459 oldpages - newpages);
1460 swap_release_by_cred(IDX_TO_OFF(oldpages -
1461 newpages), sc->cred);
1462 sc->object->charge = IDX_TO_OFF(newpages);
1463 sc->object->size = newpages;
1464 VM_OBJECT_WUNLOCK(sc->object);
1465 } else if (newpages > oldpages) {
1466 res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1467 oldpages), sc->cred);
1470 if ((mdio->md_options & MD_RESERVE) ||
1471 (sc->flags & MD_RESERVE)) {
1472 error = swap_pager_reserve(sc->object,
1473 oldpages, newpages - oldpages);
1475 swap_release_by_cred(
1476 IDX_TO_OFF(newpages - oldpages),
1481 VM_OBJECT_WLOCK(sc->object);
1482 sc->object->charge = IDX_TO_OFF(newpages);
1483 sc->object->size = newpages;
1484 VM_OBJECT_WUNLOCK(sc->object);
1488 return (EOPNOTSUPP);
1491 sc->mediasize = mdio->md_mediasize;
1493 g_resize_provider(sc->pp, sc->mediasize);
1494 g_topology_unlock();
1499 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1505 * Range check. Disallow negative sizes or any size less then the
1506 * size of a page. Then round to a page.
1508 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1512 * Allocate an OBJT_SWAP object.
1514 * Note the truncation.
1517 npage = mdio->md_mediasize / PAGE_SIZE;
1518 if (mdio->md_fwsectors != 0)
1519 sc->fwsectors = mdio->md_fwsectors;
1520 if (mdio->md_fwheads != 0)
1521 sc->fwheads = mdio->md_fwheads;
1522 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1523 VM_PROT_DEFAULT, 0, td->td_ucred);
1524 if (sc->object == NULL)
1526 sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
1527 if (mdio->md_options & MD_RESERVE) {
1528 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1533 error = mdsetcred(sc, td->td_ucred);
1536 vm_object_deallocate(sc->object);
1543 mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1547 * Range check. Disallow negative sizes or any size less then the
1548 * size of a page. Then round to a page.
1550 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1557 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1559 struct md_ioctl *mdio;
1565 printf("mdctlioctl(%s %lx %p %x %p)\n",
1566 devtoname(dev), cmd, addr, flags, td);
1568 mdio = (struct md_ioctl *)addr;
1569 if (mdio->md_version != MDIOVERSION)
1573 * We assert the version number in the individual ioctl
1574 * handlers instead of out here because (a) it is possible we
1575 * may add another ioctl in the future which doesn't read an
1576 * mdio, and (b) the correct return value for an unknown ioctl
1577 * is ENOIOCTL, not EINVAL.
1582 switch (mdio->md_type) {
1592 if (mdio->md_sectorsize == 0)
1593 sectsize = DEV_BSIZE;
1595 sectsize = mdio->md_sectorsize;
1596 if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
1598 if (mdio->md_options & MD_AUTOUNIT)
1599 sc = mdnew(-1, &error, mdio->md_type);
1601 if (mdio->md_unit > INT_MAX)
1603 sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1607 if (mdio->md_options & MD_AUTOUNIT)
1608 mdio->md_unit = sc->unit;
1609 sc->mediasize = mdio->md_mediasize;
1610 sc->sectorsize = sectsize;
1614 sc->start = mdstart_malloc;
1615 error = mdcreate_malloc(sc, mdio);
1619 * We disallow attaching preloaded memory disks via
1620 * ioctl. Preloaded memory disks are automatically
1621 * attached in g_md_init().
1626 sc->start = mdstart_vnode;
1627 error = mdcreate_vnode(sc, mdio, td);
1630 sc->start = mdstart_swap;
1631 error = mdcreate_swap(sc, mdio, td);
1634 sc->start = mdstart_null;
1635 error = mdcreate_null(sc, mdio, td);
1643 /* Prune off any residual fractional sector */
1644 i = sc->mediasize % sc->sectorsize;
1650 if (mdio->md_mediasize != 0 ||
1651 (mdio->md_options & ~MD_FORCE) != 0)
1654 sc = mdfind(mdio->md_unit);
1657 if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1658 !(mdio->md_options & MD_FORCE))
1660 return (mddestroy(sc, td));
1662 if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1665 sc = mdfind(mdio->md_unit);
1668 if (mdio->md_mediasize < sc->sectorsize)
1670 if (mdio->md_mediasize < sc->mediasize &&
1671 !(sc->flags & MD_FORCE) &&
1672 !(mdio->md_options & MD_FORCE))
1674 return (mdresize(sc, mdio));
1676 sc = mdfind(mdio->md_unit);
1679 mdio->md_type = sc->type;
1680 mdio->md_options = sc->flags;
1681 mdio->md_mediasize = sc->mediasize;
1682 mdio->md_sectorsize = sc->sectorsize;
1683 if (sc->type == MD_VNODE)
1684 error = copyout(sc->file, mdio->md_file,
1685 strlen(sc->file) + 1);
1689 LIST_FOREACH(sc, &md_softc_list, list) {
1690 if (i == MDNPAD - 1)
1691 mdio->md_pad[i] = -1;
1693 mdio->md_pad[i++] = sc->unit;
1695 mdio->md_pad[0] = i - 1;
1703 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1708 error = xmdctlioctl(dev, cmd, addr, flags, td);
1714 md_preloaded(u_char *image, size_t length, const char *name)
1719 sc = mdnew(-1, &error, MD_PRELOAD);
1722 sc->mediasize = length;
1723 sc->sectorsize = DEV_BSIZE;
1725 sc->pl_len = length;
1726 sc->start = mdstart_preload;
1729 rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
1733 printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
1734 MD_NAME, sc->unit, name, length, image);
1739 g_md_init(struct g_class *mp __unused)
1742 u_char *ptr, *name, *type;
1746 /* figure out log2(NINDIR) */
1747 for (i = NINDIR, nshift = -1; i; nshift++)
1751 sx_init(&md_sx, "MD config lock");
1752 g_topology_unlock();
1753 md_uh = new_unrhdr(0, INT_MAX, NULL);
1756 md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL);
1759 /* XXX: are preload_* static or do they need Giant ? */
1760 while ((mod = preload_search_next_name(mod)) != NULL) {
1761 name = (char *)preload_search_info(mod, MODINFO_NAME);
1764 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1767 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1769 ptr = preload_fetch_addr(mod);
1770 len = preload_fetch_size(mod);
1771 if (ptr != NULL && len != 0) {
1773 md_preloaded(ptr, len, name);
1777 md_vnode_pbuf_freecnt = nswbuf / 10;
1778 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
1784 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1785 struct g_consumer *cp __unused, struct g_provider *pp)
1816 if (indent == NULL) {
1817 sbuf_printf(sb, " u %d", mp->unit);
1818 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1819 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1820 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1821 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1822 sbuf_printf(sb, " t %s", type);
1823 if (mp->type == MD_VNODE && mp->vnode != NULL)
1824 sbuf_printf(sb, " file %s", mp->file);
1826 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1828 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1829 indent, (uintmax_t) mp->sectorsize);
1830 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1831 indent, (uintmax_t) mp->fwheads);
1832 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1833 indent, (uintmax_t) mp->fwsectors);
1834 sbuf_printf(sb, "%s<length>%ju</length>\n",
1835 indent, (uintmax_t) mp->mediasize);
1836 sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
1837 (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
1838 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
1839 (mp->flags & MD_READONLY) == 0 ? "read-write":
1841 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1843 if (mp->type == MD_VNODE && mp->vnode != NULL) {
1844 sbuf_printf(sb, "%s<file>", indent);
1845 g_conf_printf_escaped(sb, "%s", mp->file);
1846 sbuf_printf(sb, "</file>\n");
1853 g_md_fini(struct g_class *mp __unused)
1857 if (status_dev != NULL)
1858 destroy_dev(status_dev);
1859 delete_unrhdr(md_uh);