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 md_swap_page_free(vm_page_t m)
977 mdstart_swap(struct md_s *sc, struct bio *bp)
981 vm_pindex_t i, lastp;
982 bus_dma_segment_t *vlist;
983 int rv, ma_offs, offs, len, lastend;
985 switch (bp->bio_cmd) {
995 ma_offs = (bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0 ?
996 bp->bio_ma_offset : 0;
997 vlist = (bp->bio_flags & BIO_VLIST) != 0 ?
998 (bus_dma_segment_t *)bp->bio_data : NULL;
1001 * offs is the offset at which to start operating on the
1002 * next (ie, first) page. lastp is the last page on
1003 * which we're going to operate. lastend is the ending
1004 * position within that last page (ie, PAGE_SIZE if
1005 * we're operating on complete aligned pages).
1007 offs = bp->bio_offset % PAGE_SIZE;
1008 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
1009 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
1012 VM_OBJECT_WLOCK(sc->object);
1013 vm_object_pip_add(sc->object, 1);
1014 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
1015 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
1016 m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM);
1017 if (bp->bio_cmd == BIO_READ) {
1018 if (m->valid == VM_PAGE_BITS_ALL)
1021 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
1022 if (rv == VM_PAGER_ERROR) {
1025 } else if (rv == VM_PAGER_FAIL) {
1027 * Pager does not have the page. Zero
1028 * the allocated page, and mark it as
1029 * valid. Do not set dirty, the page
1030 * can be recreated if thrown out.
1033 m->valid = VM_PAGE_BITS_ALL;
1035 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
1036 pmap_copy_pages(&m, offs, bp->bio_ma,
1038 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
1039 physcopyout_vlist(VM_PAGE_TO_PHYS(m) + offs,
1040 vlist, ma_offs, len);
1041 cpu_flush_dcache(p, len);
1043 physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len);
1044 cpu_flush_dcache(p, len);
1046 } else if (bp->bio_cmd == BIO_WRITE) {
1047 if (len == PAGE_SIZE || m->valid == VM_PAGE_BITS_ALL)
1050 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
1051 if (rv == VM_PAGER_ERROR) {
1054 } else if (rv == VM_PAGER_FAIL)
1057 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
1058 pmap_copy_pages(bp->bio_ma, ma_offs, &m,
1060 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
1061 physcopyin_vlist(vlist, ma_offs,
1062 VM_PAGE_TO_PHYS(m) + offs, len);
1064 physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len);
1067 m->valid = VM_PAGE_BITS_ALL;
1069 vm_pager_page_unswapped(m);
1070 } else if (bp->bio_cmd == BIO_DELETE) {
1071 if (len == PAGE_SIZE || m->valid == VM_PAGE_BITS_ALL)
1074 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
1075 if (rv == VM_PAGER_ERROR) {
1078 } else if (rv == VM_PAGER_FAIL) {
1079 md_swap_page_free(m);
1082 /* Page is valid. */
1083 if (len != PAGE_SIZE) {
1084 pmap_zero_page_area(m, offs, len);
1087 vm_pager_page_unswapped(m);
1088 if (len == PAGE_SIZE) {
1089 md_swap_page_free(m);
1097 vm_page_activate(m);
1101 /* Actions on further pages start at offset 0 */
1102 p += PAGE_SIZE - offs;
1106 vm_object_pip_subtract(sc->object, 1);
1107 VM_OBJECT_WUNLOCK(sc->object);
1108 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
1112 mdstart_null(struct md_s *sc, struct bio *bp)
1115 switch (bp->bio_cmd) {
1117 bzero(bp->bio_data, bp->bio_length);
1118 cpu_flush_dcache(bp->bio_data, bp->bio_length);
1128 md_kthread(void *arg)
1135 thread_lock(curthread);
1136 sched_prio(curthread, PRIBIO);
1137 thread_unlock(curthread);
1138 if (sc->type == MD_VNODE)
1139 curthread->td_pflags |= TDP_NORUNNINGBUF;
1142 mtx_lock(&sc->queue_mtx);
1143 if (sc->flags & MD_SHUTDOWN) {
1144 sc->flags |= MD_EXITING;
1145 mtx_unlock(&sc->queue_mtx);
1148 bp = bioq_takefirst(&sc->bio_queue);
1150 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
1153 mtx_unlock(&sc->queue_mtx);
1154 if (bp->bio_cmd == BIO_GETATTR) {
1155 if ((sc->fwsectors && sc->fwheads &&
1156 (g_handleattr_int(bp, "GEOM::fwsectors",
1158 g_handleattr_int(bp, "GEOM::fwheads",
1160 g_handleattr_int(bp, "GEOM::candelete", 1))
1165 error = sc->start(sc, bp);
1169 bp->bio_completed = bp->bio_length;
1170 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
1171 devstat_end_transaction_bio(sc->devstat, bp);
1172 g_io_deliver(bp, error);
1177 static struct md_s *
1182 LIST_FOREACH(sc, &md_softc_list, list) {
1183 if (sc->unit == unit)
1189 static struct md_s *
1190 mdnew(int unit, int *errp, enum md_types type)
1197 unit = alloc_unr(md_uh);
1199 unit = alloc_unr_specific(md_uh, unit);
1206 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
1208 bioq_init(&sc->bio_queue);
1209 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
1210 mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF);
1212 sprintf(sc->name, "md%d", unit);
1213 LIST_INSERT_HEAD(&md_softc_list, sc, list);
1214 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
1217 LIST_REMOVE(sc, list);
1218 mtx_destroy(&sc->stat_mtx);
1219 mtx_destroy(&sc->queue_mtx);
1220 free_unr(md_uh, sc->unit);
1227 mdinit(struct md_s *sc)
1230 struct g_provider *pp;
1233 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
1235 pp = g_new_providerf(gp, "md%d", sc->unit);
1236 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
1237 pp->mediasize = sc->mediasize;
1238 pp->sectorsize = sc->sectorsize;
1243 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1251 g_error_provider(pp, 0);
1252 g_topology_unlock();
1253 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
1254 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
1258 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
1265 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
1267 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
1269 /* Compression doesn't make sense if we have reserved space */
1270 if (mdio->md_options & MD_RESERVE)
1271 mdio->md_options &= ~MD_COMPRESS;
1272 if (mdio->md_fwsectors != 0)
1273 sc->fwsectors = mdio->md_fwsectors;
1274 if (mdio->md_fwheads != 0)
1275 sc->fwheads = mdio->md_fwheads;
1276 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
1277 sc->indir = dimension(sc->mediasize / sc->sectorsize);
1278 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
1280 if (mdio->md_options & MD_RESERVE) {
1283 nsectors = sc->mediasize / sc->sectorsize;
1284 for (u = 0; u < nsectors; u++) {
1285 sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
1286 M_WAITOK : M_NOWAIT) | M_ZERO);
1288 error = s_write(sc->indir, u, sp);
1300 mdsetcred(struct md_s *sc, struct ucred *cred)
1306 * Set credits in our softc
1311 sc->cred = crhold(cred);
1314 * Horrible kludge to establish credentials for NFS XXX.
1321 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
1322 bzero(&auio, sizeof(auio));
1324 aiov.iov_base = tmpbuf;
1325 aiov.iov_len = sc->sectorsize;
1326 auio.uio_iov = &aiov;
1327 auio.uio_iovcnt = 1;
1328 auio.uio_offset = 0;
1329 auio.uio_rw = UIO_READ;
1330 auio.uio_segflg = UIO_SYSSPACE;
1331 auio.uio_resid = aiov.iov_len;
1332 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1333 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
1334 VOP_UNLOCK(sc->vnode, 0);
1335 free(tmpbuf, M_TEMP);
1341 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1344 struct nameidata nd;
1349 * Kernel-originated requests must have the filename appended
1350 * to the mdio structure to protect against malicious software.
1352 fname = mdio->md_file;
1353 if ((void *)fname != (void *)(mdio + 1)) {
1354 error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
1358 strlcpy(sc->file, fname, sizeof(sc->file));
1361 * If the user specified that this is a read only device, don't
1362 * set the FWRITE mask before trying to open the backing store.
1364 flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
1365 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
1366 error = vn_open(&nd, &flags, 0, NULL);
1369 NDFREE(&nd, NDF_ONLY_PNBUF);
1370 if (nd.ni_vp->v_type != VREG) {
1374 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
1377 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
1378 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
1379 if (nd.ni_vp->v_iflag & VI_DOOMED) {
1380 /* Forced unmount. */
1385 nd.ni_vp->v_vflag |= VV_MD;
1386 VOP_UNLOCK(nd.ni_vp, 0);
1388 if (mdio->md_fwsectors != 0)
1389 sc->fwsectors = mdio->md_fwsectors;
1390 if (mdio->md_fwheads != 0)
1391 sc->fwheads = mdio->md_fwheads;
1392 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
1393 if (!(flags & FWRITE))
1394 sc->flags |= MD_READONLY;
1395 sc->vnode = nd.ni_vp;
1397 error = mdsetcred(sc, td->td_ucred);
1400 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1401 nd.ni_vp->v_vflag &= ~VV_MD;
1406 VOP_UNLOCK(nd.ni_vp, 0);
1407 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
1412 mddestroy(struct md_s *sc, struct thread *td)
1416 sc->gp->softc = NULL;
1418 g_wither_geom(sc->gp, ENXIO);
1419 g_topology_unlock();
1424 devstat_remove_entry(sc->devstat);
1427 mtx_lock(&sc->queue_mtx);
1428 sc->flags |= MD_SHUTDOWN;
1430 while (!(sc->flags & MD_EXITING))
1431 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1432 mtx_unlock(&sc->queue_mtx);
1433 mtx_destroy(&sc->stat_mtx);
1434 mtx_destroy(&sc->queue_mtx);
1435 if (sc->vnode != NULL) {
1436 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1437 sc->vnode->v_vflag &= ~VV_MD;
1438 VOP_UNLOCK(sc->vnode, 0);
1439 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1440 FREAD : (FREAD|FWRITE), sc->cred, td);
1442 if (sc->cred != NULL)
1444 if (sc->object != NULL)
1445 vm_object_deallocate(sc->object);
1447 destroy_indir(sc, sc->indir);
1449 uma_zdestroy(sc->uma);
1451 LIST_REMOVE(sc, list);
1452 free_unr(md_uh, sc->unit);
1458 mdresize(struct md_s *sc, struct md_ioctl *mdio)
1461 vm_pindex_t oldpages, newpages;
1468 if (mdio->md_mediasize <= 0 ||
1469 (mdio->md_mediasize % PAGE_SIZE) != 0)
1471 oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1472 newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1473 if (newpages < oldpages) {
1474 VM_OBJECT_WLOCK(sc->object);
1475 vm_object_page_remove(sc->object, newpages, 0, 0);
1476 swap_pager_freespace(sc->object, newpages,
1477 oldpages - newpages);
1478 swap_release_by_cred(IDX_TO_OFF(oldpages -
1479 newpages), sc->cred);
1480 sc->object->charge = IDX_TO_OFF(newpages);
1481 sc->object->size = newpages;
1482 VM_OBJECT_WUNLOCK(sc->object);
1483 } else if (newpages > oldpages) {
1484 res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1485 oldpages), sc->cred);
1488 if ((mdio->md_options & MD_RESERVE) ||
1489 (sc->flags & MD_RESERVE)) {
1490 error = swap_pager_reserve(sc->object,
1491 oldpages, newpages - oldpages);
1493 swap_release_by_cred(
1494 IDX_TO_OFF(newpages - oldpages),
1499 VM_OBJECT_WLOCK(sc->object);
1500 sc->object->charge = IDX_TO_OFF(newpages);
1501 sc->object->size = newpages;
1502 VM_OBJECT_WUNLOCK(sc->object);
1506 return (EOPNOTSUPP);
1509 sc->mediasize = mdio->md_mediasize;
1511 g_resize_provider(sc->pp, sc->mediasize);
1512 g_topology_unlock();
1517 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1523 * Range check. Disallow negative sizes or any size less then the
1524 * size of a page. Then round to a page.
1526 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1530 * Allocate an OBJT_SWAP object.
1532 * Note the truncation.
1535 npage = mdio->md_mediasize / PAGE_SIZE;
1536 if (mdio->md_fwsectors != 0)
1537 sc->fwsectors = mdio->md_fwsectors;
1538 if (mdio->md_fwheads != 0)
1539 sc->fwheads = mdio->md_fwheads;
1540 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1541 VM_PROT_DEFAULT, 0, td->td_ucred);
1542 if (sc->object == NULL)
1544 sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
1545 if (mdio->md_options & MD_RESERVE) {
1546 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1551 error = mdsetcred(sc, td->td_ucred);
1554 vm_object_deallocate(sc->object);
1561 mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1565 * Range check. Disallow negative sizes or any size less then the
1566 * size of a page. Then round to a page.
1568 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1575 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1577 struct md_ioctl *mdio;
1583 printf("mdctlioctl(%s %lx %p %x %p)\n",
1584 devtoname(dev), cmd, addr, flags, td);
1586 mdio = (struct md_ioctl *)addr;
1587 if (mdio->md_version != MDIOVERSION)
1591 * We assert the version number in the individual ioctl
1592 * handlers instead of out here because (a) it is possible we
1593 * may add another ioctl in the future which doesn't read an
1594 * mdio, and (b) the correct return value for an unknown ioctl
1595 * is ENOIOCTL, not EINVAL.
1600 switch (mdio->md_type) {
1610 if (mdio->md_sectorsize == 0)
1611 sectsize = DEV_BSIZE;
1613 sectsize = mdio->md_sectorsize;
1614 if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
1616 if (mdio->md_options & MD_AUTOUNIT)
1617 sc = mdnew(-1, &error, mdio->md_type);
1619 if (mdio->md_unit > INT_MAX)
1621 sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1625 if (mdio->md_options & MD_AUTOUNIT)
1626 mdio->md_unit = sc->unit;
1627 sc->mediasize = mdio->md_mediasize;
1628 sc->sectorsize = sectsize;
1632 sc->start = mdstart_malloc;
1633 error = mdcreate_malloc(sc, mdio);
1637 * We disallow attaching preloaded memory disks via
1638 * ioctl. Preloaded memory disks are automatically
1639 * attached in g_md_init().
1644 sc->start = mdstart_vnode;
1645 error = mdcreate_vnode(sc, mdio, td);
1648 sc->start = mdstart_swap;
1649 error = mdcreate_swap(sc, mdio, td);
1652 sc->start = mdstart_null;
1653 error = mdcreate_null(sc, mdio, td);
1661 /* Prune off any residual fractional sector */
1662 i = sc->mediasize % sc->sectorsize;
1668 if (mdio->md_mediasize != 0 ||
1669 (mdio->md_options & ~MD_FORCE) != 0)
1672 sc = mdfind(mdio->md_unit);
1675 if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1676 !(mdio->md_options & MD_FORCE))
1678 return (mddestroy(sc, td));
1680 if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1683 sc = mdfind(mdio->md_unit);
1686 if (mdio->md_mediasize < sc->sectorsize)
1688 if (mdio->md_mediasize < sc->mediasize &&
1689 !(sc->flags & MD_FORCE) &&
1690 !(mdio->md_options & MD_FORCE))
1692 return (mdresize(sc, mdio));
1694 sc = mdfind(mdio->md_unit);
1697 mdio->md_type = sc->type;
1698 mdio->md_options = sc->flags;
1699 mdio->md_mediasize = sc->mediasize;
1700 mdio->md_sectorsize = sc->sectorsize;
1701 if (sc->type == MD_VNODE)
1702 error = copyout(sc->file, mdio->md_file,
1703 strlen(sc->file) + 1);
1707 LIST_FOREACH(sc, &md_softc_list, list) {
1708 if (i == MDNPAD - 1)
1709 mdio->md_pad[i] = -1;
1711 mdio->md_pad[i++] = sc->unit;
1713 mdio->md_pad[0] = i - 1;
1721 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1726 error = xmdctlioctl(dev, cmd, addr, flags, td);
1732 md_preloaded(u_char *image, size_t length, const char *name)
1737 sc = mdnew(-1, &error, MD_PRELOAD);
1740 sc->mediasize = length;
1741 sc->sectorsize = DEV_BSIZE;
1743 sc->pl_len = length;
1744 sc->start = mdstart_preload;
1747 rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
1751 printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
1752 MD_NAME, sc->unit, name, length, image);
1757 g_md_init(struct g_class *mp __unused)
1760 u_char *ptr, *name, *type;
1764 /* figure out log2(NINDIR) */
1765 for (i = NINDIR, nshift = -1; i; nshift++)
1769 sx_init(&md_sx, "MD config lock");
1770 g_topology_unlock();
1771 md_uh = new_unrhdr(0, INT_MAX, NULL);
1774 md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL);
1777 /* XXX: are preload_* static or do they need Giant ? */
1778 while ((mod = preload_search_next_name(mod)) != NULL) {
1779 name = (char *)preload_search_info(mod, MODINFO_NAME);
1782 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1785 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1787 ptr = preload_fetch_addr(mod);
1788 len = preload_fetch_size(mod);
1789 if (ptr != NULL && len != 0) {
1791 md_preloaded(ptr, len, name);
1795 md_vnode_pbuf_freecnt = nswbuf / 10;
1796 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
1802 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1803 struct g_consumer *cp __unused, struct g_provider *pp)
1834 if (indent == NULL) {
1835 sbuf_printf(sb, " u %d", mp->unit);
1836 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1837 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1838 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1839 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1840 sbuf_printf(sb, " t %s", type);
1841 if (mp->type == MD_VNODE && mp->vnode != NULL)
1842 sbuf_printf(sb, " file %s", mp->file);
1844 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1846 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1847 indent, (uintmax_t) mp->sectorsize);
1848 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1849 indent, (uintmax_t) mp->fwheads);
1850 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1851 indent, (uintmax_t) mp->fwsectors);
1852 sbuf_printf(sb, "%s<length>%ju</length>\n",
1853 indent, (uintmax_t) mp->mediasize);
1854 sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
1855 (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
1856 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
1857 (mp->flags & MD_READONLY) == 0 ? "read-write":
1859 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1861 if (mp->type == MD_VNODE && mp->vnode != NULL) {
1862 sbuf_printf(sb, "%s<file>", indent);
1863 g_conf_printf_escaped(sb, "%s", mp->file);
1864 sbuf_printf(sb, "</file>\n");
1871 g_md_fini(struct g_class *mp __unused)
1875 if (status_dev != NULL)
1876 destroy_dev(status_dev);
1877 delete_unrhdr(md_uh);