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>
94 #include <vm/vm_param.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_pager.h>
98 #include <vm/swap_pager.h>
103 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
104 #define MD_EXITING 0x20000 /* Worker thread is exiting. */
107 #define MD_NSECT (10000 * 2)
110 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
111 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
114 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0,
115 "Enable md(4) debug messages");
116 static int md_malloc_wait;
117 SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0,
118 "Allow malloc to wait for memory allocations");
120 #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE)
121 #define MD_ROOT_FSTYPE "ufs"
124 #if defined(MD_ROOT) && defined(MD_ROOT_SIZE)
126 * Preloaded image gets put here.
127 * Applications that patch the object with the image can determine
128 * the size looking at the start and end markers (strings),
129 * so we want them contiguous.
132 u_char start[MD_ROOT_SIZE*1024];
135 .start = "MFS Filesystem goes here",
136 .end = "MFS Filesystem had better STOP here",
140 static g_init_t g_md_init;
141 static g_fini_t g_md_fini;
142 static g_start_t g_md_start;
143 static g_access_t g_md_access;
144 static void g_md_dumpconf(struct sbuf *sb, const char *indent,
145 struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
147 static struct cdev *status_dev = 0;
148 static struct sx md_sx;
149 static struct unrhdr *md_uh;
151 static d_ioctl_t mdctlioctl;
153 static struct cdevsw mdctl_cdevsw = {
154 .d_version = D_VERSION,
155 .d_ioctl = mdctlioctl,
159 struct g_class g_md_class = {
161 .version = G_VERSION,
165 .access = g_md_access,
166 .dumpconf = g_md_dumpconf,
169 DECLARE_GEOM_CLASS(g_md_class, g_md);
172 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
174 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
175 #define NMASK (NINDIR-1)
178 static int md_vnode_pbuf_freecnt;
189 LIST_ENTRY(md_s) list;
190 struct bio_queue_head bio_queue;
191 struct mtx queue_mtx;
203 struct g_provider *pp;
204 int (*start)(struct md_s *sc, struct bio *bp);
205 struct devstat *devstat;
207 /* MD_MALLOC related fields */
211 /* MD_PRELOAD related fields */
215 /* MD_VNODE related fields */
220 /* MD_SWAP related fields */
224 static struct indir *
225 new_indir(u_int shift)
229 ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
233 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
234 M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
235 if (ip->array == NULL) {
245 del_indir(struct indir *ip)
248 free(ip->array, M_MDSECT);
253 destroy_indir(struct md_s *sc, struct indir *ip)
257 for (i = 0; i < NINDIR; i++) {
261 destroy_indir(sc, (struct indir*)(ip->array[i]));
262 else if (ip->array[i] > 255)
263 uma_zfree(sc->uma, (void *)(ip->array[i]));
269 * This function does the math and allocates the top level "indir" structure
270 * for a device of "size" sectors.
273 static struct indir *
274 dimension(off_t size)
282 while (rcnt > NINDIR) {
288 * XXX: the top layer is probably not fully populated, so we allocate
289 * too much space for ip->array in here.
291 ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
292 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
293 M_MDSECT, M_WAITOK | M_ZERO);
295 ip->shift = layer * nshift;
300 * Read a given sector
304 s_read(struct indir *ip, off_t offset)
311 printf("s_read(%jd)\n", (intmax_t)offset);
313 for (cip = ip; cip != NULL;) {
315 idx = (offset >> cip->shift) & NMASK;
316 up = cip->array[idx];
317 cip = (struct indir *)up;
320 idx = offset & NMASK;
321 return (cip->array[idx]);
327 * Write a given sector, prune the tree if the value is 0
331 s_write(struct indir *ip, off_t offset, uintptr_t ptr)
333 struct indir *cip, *lip[10];
338 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
345 idx = (offset >> cip->shift) & NMASK;
346 up = cip->array[idx];
348 cip = (struct indir *)up;
351 /* Allocate branch */
353 (uintptr_t)new_indir(cip->shift - nshift);
354 if (cip->array[idx] == 0)
357 up = cip->array[idx];
358 cip = (struct indir *)up;
362 idx = offset & NMASK;
363 up = cip->array[idx];
366 cip->array[idx] = ptr;
371 if (cip->used != 0 || li == 1)
374 while (cip->used == 0 && cip != ip) {
376 idx = (offset >> lip[li]->shift) & NMASK;
377 up = lip[li]->array[idx];
378 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
380 lip[li]->array[idx] = 0;
389 g_md_access(struct g_provider *pp, int r, int w, int e)
393 sc = pp->geom->softc;
395 if (r <= 0 && w <= 0 && e <= 0)
402 if ((sc->flags & MD_READONLY) != 0 && w > 0)
404 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
406 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
413 g_md_start(struct bio *bp)
417 sc = bp->bio_to->geom->softc;
418 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
419 devstat_start_transaction_bio(sc->devstat, bp);
420 mtx_lock(&sc->queue_mtx);
421 bioq_disksort(&sc->bio_queue, bp);
422 mtx_unlock(&sc->queue_mtx);
426 #define MD_MALLOC_MOVE_ZERO 1
427 #define MD_MALLOC_MOVE_FILL 2
428 #define MD_MALLOC_MOVE_READ 3
429 #define MD_MALLOC_MOVE_WRITE 4
430 #define MD_MALLOC_MOVE_CMP 5
433 md_malloc_move(vm_page_t **mp, int *ma_offs, unsigned sectorsize,
434 void *ptr, u_char fill, int op)
441 int error, i, ma_offs1, sz, first_read;
446 /* if (op == MD_MALLOC_MOVE_CMP) { gcc */
454 for (n = sectorsize; n != 0; n -= sz) {
455 sz = imin(PAGE_SIZE - *ma_offs, n);
460 sf = sf_buf_alloc(m, SFB_CPUPRIVATE |
461 (md_malloc_wait ? 0 : SFB_NOWAIT));
467 p = (char *)sf_buf_kva(sf) + *ma_offs;
469 case MD_MALLOC_MOVE_ZERO:
472 case MD_MALLOC_MOVE_FILL:
475 case MD_MALLOC_MOVE_READ:
477 cpu_flush_dcache(p, sz);
479 case MD_MALLOC_MOVE_WRITE:
482 case MD_MALLOC_MOVE_CMP:
483 for (i = 0; i < sz; i++, p++) {
488 } else if (*p != first) {
495 KASSERT(0, ("md_malloc_move unknown op %d\n", op));
501 *ma_offs %= PAGE_SIZE;
504 ptr = (char *)ptr + sz;
510 if (op == MD_MALLOC_MOVE_CMP && error != 0) {
518 mdstart_malloc(struct md_s *sc, struct bio *bp)
522 int i, error, error1, ma_offs, notmapped;
523 off_t secno, nsec, uc;
526 switch (bp->bio_cmd) {
535 notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0;
538 ma_offs = bp->bio_ma_offset;
544 nsec = bp->bio_length / sc->sectorsize;
545 secno = bp->bio_offset / sc->sectorsize;
548 osp = s_read(sc->indir, secno);
549 if (bp->bio_cmd == BIO_DELETE) {
551 error = s_write(sc->indir, secno, 0);
552 } else if (bp->bio_cmd == BIO_READ) {
555 error = md_malloc_move(&m, &ma_offs,
556 sc->sectorsize, NULL, 0,
557 MD_MALLOC_MOVE_ZERO);
559 bzero(dst, sc->sectorsize);
560 } else if (osp <= 255) {
562 error = md_malloc_move(&m, &ma_offs,
563 sc->sectorsize, NULL, osp,
564 MD_MALLOC_MOVE_FILL);
566 memset(dst, osp, sc->sectorsize);
569 error = md_malloc_move(&m, &ma_offs,
570 sc->sectorsize, (void *)osp, 0,
571 MD_MALLOC_MOVE_READ);
573 bcopy((void *)osp, dst, sc->sectorsize);
574 cpu_flush_dcache(dst, sc->sectorsize);
578 } else if (bp->bio_cmd == BIO_WRITE) {
579 if (sc->flags & MD_COMPRESS) {
581 error1 = md_malloc_move(&m, &ma_offs,
582 sc->sectorsize, &uc, 0,
584 i = error1 == 0 ? sc->sectorsize : 0;
587 for (i = 1; i < sc->sectorsize; i++) {
596 if (i == sc->sectorsize) {
598 error = s_write(sc->indir, secno, uc);
601 sp = (uintptr_t)uma_zalloc(sc->uma,
602 md_malloc_wait ? M_WAITOK :
609 error = md_malloc_move(&m,
610 &ma_offs, sc->sectorsize,
612 MD_MALLOC_MOVE_WRITE);
614 bcopy(dst, (void *)sp,
617 error = s_write(sc->indir, secno, sp);
620 error = md_malloc_move(&m,
621 &ma_offs, sc->sectorsize,
623 MD_MALLOC_MOVE_WRITE);
625 bcopy(dst, (void *)osp,
635 uma_zfree(sc->uma, (void*)osp);
640 dst += sc->sectorsize;
647 mdstart_preload(struct md_s *sc, struct bio *bp)
650 switch (bp->bio_cmd) {
652 bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data,
654 cpu_flush_dcache(bp->bio_data, bp->bio_length);
657 bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset,
666 mdstart_vnode(struct md_s *sc, struct bio *bp)
677 switch (bp->bio_cmd) {
693 * If an error occurs, we set BIO_ERROR but we do not set
694 * B_INVAL because (for a write anyway), the buffer is
698 if (bp->bio_cmd == BIO_FLUSH) {
699 (void) vn_start_write(vp, &mp, V_WAIT);
700 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
701 error = VOP_FSYNC(vp, MNT_WAIT, td);
703 vn_finished_write(mp);
707 bzero(&auio, sizeof(auio));
710 * Special case for BIO_DELETE. On the surface, this is very
711 * similar to BIO_WRITE, except that we write from our own
712 * fixed-length buffer, so we have to loop. The net result is
713 * that the two cases end up having very little in common.
715 if (bp->bio_cmd == BIO_DELETE) {
716 zerosize = ZERO_REGION_SIZE -
717 (ZERO_REGION_SIZE % sc->sectorsize);
718 auio.uio_iov = &aiov;
720 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
721 auio.uio_segflg = UIO_SYSSPACE;
722 auio.uio_rw = UIO_WRITE;
724 end = bp->bio_offset + bp->bio_length;
725 (void) vn_start_write(vp, &mp, V_WAIT);
726 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
728 while (auio.uio_offset < end) {
729 aiov.iov_base = __DECONST(void *, zero_region);
730 aiov.iov_len = end - auio.uio_offset;
731 if (aiov.iov_len > zerosize)
732 aiov.iov_len = zerosize;
733 auio.uio_resid = aiov.iov_len;
734 error = VOP_WRITE(vp, &auio,
735 sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred);
740 vn_finished_write(mp);
741 bp->bio_resid = end - auio.uio_offset;
745 KASSERT(bp->bio_length <= MAXPHYS, ("bio_length %jd",
746 (uintmax_t)bp->bio_length));
747 if ((bp->bio_flags & BIO_UNMAPPED) == 0) {
749 aiov.iov_base = bp->bio_data;
751 pb = getpbuf(&md_vnode_pbuf_freecnt);
752 pmap_qenter((vm_offset_t)pb->b_data, bp->bio_ma, bp->bio_ma_n);
753 aiov.iov_base = (void *)((vm_offset_t)pb->b_data +
756 aiov.iov_len = bp->bio_length;
757 auio.uio_iov = &aiov;
759 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
760 auio.uio_segflg = UIO_SYSSPACE;
761 if (bp->bio_cmd == BIO_READ)
762 auio.uio_rw = UIO_READ;
763 else if (bp->bio_cmd == BIO_WRITE)
764 auio.uio_rw = UIO_WRITE;
766 panic("wrong BIO_OP in mdstart_vnode");
767 auio.uio_resid = bp->bio_length;
770 * When reading set IO_DIRECT to try to avoid double-caching
771 * the data. When writing IO_DIRECT is not optimal.
773 if (bp->bio_cmd == BIO_READ) {
774 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
775 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
778 (void) vn_start_write(vp, &mp, V_WAIT);
779 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
780 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
783 vn_finished_write(mp);
785 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
786 pmap_qremove((vm_offset_t)pb->b_data, bp->bio_ma_n);
787 relpbuf(pb, &md_vnode_pbuf_freecnt);
789 bp->bio_resid = auio.uio_resid;
794 mdstart_swap(struct md_s *sc, struct bio *bp)
798 vm_pindex_t i, lastp;
799 int rv, ma_offs, offs, len, lastend;
801 switch (bp->bio_cmd) {
811 ma_offs = (bp->bio_flags & BIO_UNMAPPED) == 0 ? 0 : bp->bio_ma_offset;
814 * offs is the offset at which to start operating on the
815 * next (ie, first) page. lastp is the last page on
816 * which we're going to operate. lastend is the ending
817 * position within that last page (ie, PAGE_SIZE if
818 * we're operating on complete aligned pages).
820 offs = bp->bio_offset % PAGE_SIZE;
821 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
822 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
825 VM_OBJECT_WLOCK(sc->object);
826 vm_object_pip_add(sc->object, 1);
827 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
828 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
829 m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM);
830 if (bp->bio_cmd == BIO_READ) {
831 if (m->valid == VM_PAGE_BITS_ALL)
834 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
835 if (rv == VM_PAGER_ERROR) {
838 } else if (rv == VM_PAGER_FAIL) {
840 * Pager does not have the page. Zero
841 * the allocated page, and mark it as
842 * valid. Do not set dirty, the page
843 * can be recreated if thrown out.
846 m->valid = VM_PAGE_BITS_ALL;
848 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
849 pmap_copy_pages(&m, offs, bp->bio_ma,
852 physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len);
853 cpu_flush_dcache(p, len);
855 } else if (bp->bio_cmd == BIO_WRITE) {
856 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
857 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
860 if (rv == VM_PAGER_ERROR) {
864 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
865 pmap_copy_pages(bp->bio_ma, ma_offs, &m,
868 physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len);
870 m->valid = VM_PAGE_BITS_ALL;
871 } else if (bp->bio_cmd == BIO_DELETE) {
872 if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL)
873 rv = vm_pager_get_pages(sc->object, &m, 1, 0);
876 if (rv == VM_PAGER_ERROR) {
880 if (len != PAGE_SIZE) {
881 pmap_zero_page_area(m, offs, len);
882 vm_page_clear_dirty(m, offs, len);
883 m->valid = VM_PAGE_BITS_ALL;
885 vm_pager_page_unswapped(m);
889 if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE)
894 if (bp->bio_cmd == BIO_WRITE)
897 /* Actions on further pages start at offset 0 */
898 p += PAGE_SIZE - offs;
902 vm_object_pip_subtract(sc->object, 1);
903 VM_OBJECT_WUNLOCK(sc->object);
904 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
908 md_kthread(void *arg)
915 thread_lock(curthread);
916 sched_prio(curthread, PRIBIO);
917 thread_unlock(curthread);
918 if (sc->type == MD_VNODE)
919 curthread->td_pflags |= TDP_NORUNNINGBUF;
922 mtx_lock(&sc->queue_mtx);
923 if (sc->flags & MD_SHUTDOWN) {
924 sc->flags |= MD_EXITING;
925 mtx_unlock(&sc->queue_mtx);
928 bp = bioq_takefirst(&sc->bio_queue);
930 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
933 mtx_unlock(&sc->queue_mtx);
934 if (bp->bio_cmd == BIO_GETATTR) {
935 if ((sc->fwsectors && sc->fwheads &&
936 (g_handleattr_int(bp, "GEOM::fwsectors",
938 g_handleattr_int(bp, "GEOM::fwheads",
940 g_handleattr_int(bp, "GEOM::candelete", 1))
945 error = sc->start(sc, bp);
949 bp->bio_completed = bp->bio_length;
950 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE))
951 devstat_end_transaction_bio(sc->devstat, bp);
952 g_io_deliver(bp, error);
962 LIST_FOREACH(sc, &md_softc_list, list) {
963 if (sc->unit == unit)
970 mdnew(int unit, int *errp, enum md_types type)
977 unit = alloc_unr(md_uh);
979 unit = alloc_unr_specific(md_uh, unit);
986 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
988 bioq_init(&sc->bio_queue);
989 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
991 sprintf(sc->name, "md%d", unit);
992 LIST_INSERT_HEAD(&md_softc_list, sc, list);
993 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
996 LIST_REMOVE(sc, list);
997 mtx_destroy(&sc->queue_mtx);
998 free_unr(md_uh, sc->unit);
1005 mdinit(struct md_s *sc)
1008 struct g_provider *pp;
1011 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
1013 pp = g_new_providerf(gp, "md%d", sc->unit);
1014 pp->mediasize = sc->mediasize;
1015 pp->sectorsize = sc->sectorsize;
1020 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1027 g_error_provider(pp, 0);
1028 g_topology_unlock();
1029 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
1030 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
1034 mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio)
1041 if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
1043 if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize))
1045 /* Compression doesn't make sense if we have reserved space */
1046 if (mdio->md_options & MD_RESERVE)
1047 mdio->md_options &= ~MD_COMPRESS;
1048 if (mdio->md_fwsectors != 0)
1049 sc->fwsectors = mdio->md_fwsectors;
1050 if (mdio->md_fwheads != 0)
1051 sc->fwheads = mdio->md_fwheads;
1052 sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE);
1053 sc->indir = dimension(sc->mediasize / sc->sectorsize);
1054 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
1056 if (mdio->md_options & MD_RESERVE) {
1059 nsectors = sc->mediasize / sc->sectorsize;
1060 for (u = 0; u < nsectors; u++) {
1061 sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
1062 M_WAITOK : M_NOWAIT) | M_ZERO);
1064 error = s_write(sc->indir, u, sp);
1076 mdsetcred(struct md_s *sc, struct ucred *cred)
1082 * Set credits in our softc
1087 sc->cred = crhold(cred);
1090 * Horrible kludge to establish credentials for NFS XXX.
1097 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
1098 bzero(&auio, sizeof(auio));
1100 aiov.iov_base = tmpbuf;
1101 aiov.iov_len = sc->sectorsize;
1102 auio.uio_iov = &aiov;
1103 auio.uio_iovcnt = 1;
1104 auio.uio_offset = 0;
1105 auio.uio_rw = UIO_READ;
1106 auio.uio_segflg = UIO_SYSSPACE;
1107 auio.uio_resid = aiov.iov_len;
1108 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1109 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
1110 VOP_UNLOCK(sc->vnode, 0);
1111 free(tmpbuf, M_TEMP);
1117 mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1120 struct nameidata nd;
1125 * Kernel-originated requests must have the filename appended
1126 * to the mdio structure to protect against malicious software.
1128 fname = mdio->md_file;
1129 if ((void *)fname != (void *)(mdio + 1)) {
1130 error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
1134 strlcpy(sc->file, fname, sizeof(sc->file));
1137 * If the user specified that this is a read only device, don't
1138 * set the FWRITE mask before trying to open the backing store.
1140 flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE);
1141 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
1142 error = vn_open(&nd, &flags, 0, NULL);
1145 NDFREE(&nd, NDF_ONLY_PNBUF);
1146 if (nd.ni_vp->v_type != VREG) {
1150 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
1153 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
1154 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
1155 if (nd.ni_vp->v_iflag & VI_DOOMED) {
1156 /* Forced unmount. */
1161 nd.ni_vp->v_vflag |= VV_MD;
1162 VOP_UNLOCK(nd.ni_vp, 0);
1164 if (mdio->md_fwsectors != 0)
1165 sc->fwsectors = mdio->md_fwsectors;
1166 if (mdio->md_fwheads != 0)
1167 sc->fwheads = mdio->md_fwheads;
1168 sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC);
1169 if (!(flags & FWRITE))
1170 sc->flags |= MD_READONLY;
1171 sc->vnode = nd.ni_vp;
1173 error = mdsetcred(sc, td->td_ucred);
1176 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1177 nd.ni_vp->v_vflag &= ~VV_MD;
1182 VOP_UNLOCK(nd.ni_vp, 0);
1183 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
1188 mddestroy(struct md_s *sc, struct thread *td)
1192 sc->gp->softc = NULL;
1194 g_wither_geom(sc->gp, ENXIO);
1195 g_topology_unlock();
1200 devstat_remove_entry(sc->devstat);
1203 mtx_lock(&sc->queue_mtx);
1204 sc->flags |= MD_SHUTDOWN;
1206 while (!(sc->flags & MD_EXITING))
1207 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1208 mtx_unlock(&sc->queue_mtx);
1209 mtx_destroy(&sc->queue_mtx);
1210 if (sc->vnode != NULL) {
1211 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1212 sc->vnode->v_vflag &= ~VV_MD;
1213 VOP_UNLOCK(sc->vnode, 0);
1214 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1215 FREAD : (FREAD|FWRITE), sc->cred, td);
1217 if (sc->cred != NULL)
1219 if (sc->object != NULL)
1220 vm_object_deallocate(sc->object);
1222 destroy_indir(sc, sc->indir);
1224 uma_zdestroy(sc->uma);
1226 LIST_REMOVE(sc, list);
1227 free_unr(md_uh, sc->unit);
1233 mdresize(struct md_s *sc, struct md_ioctl *mdio)
1236 vm_pindex_t oldpages, newpages;
1242 if (mdio->md_mediasize <= 0 ||
1243 (mdio->md_mediasize % PAGE_SIZE) != 0)
1245 oldpages = OFF_TO_IDX(round_page(sc->mediasize));
1246 newpages = OFF_TO_IDX(round_page(mdio->md_mediasize));
1247 if (newpages < oldpages) {
1248 VM_OBJECT_WLOCK(sc->object);
1249 vm_object_page_remove(sc->object, newpages, 0, 0);
1250 swap_pager_freespace(sc->object, newpages,
1251 oldpages - newpages);
1252 swap_release_by_cred(IDX_TO_OFF(oldpages -
1253 newpages), sc->cred);
1254 sc->object->charge = IDX_TO_OFF(newpages);
1255 sc->object->size = newpages;
1256 VM_OBJECT_WUNLOCK(sc->object);
1257 } else if (newpages > oldpages) {
1258 res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1259 oldpages), sc->cred);
1262 if ((mdio->md_options & MD_RESERVE) ||
1263 (sc->flags & MD_RESERVE)) {
1264 error = swap_pager_reserve(sc->object,
1265 oldpages, newpages - oldpages);
1267 swap_release_by_cred(
1268 IDX_TO_OFF(newpages - oldpages),
1273 VM_OBJECT_WLOCK(sc->object);
1274 sc->object->charge = IDX_TO_OFF(newpages);
1275 sc->object->size = newpages;
1276 VM_OBJECT_WUNLOCK(sc->object);
1280 return (EOPNOTSUPP);
1283 sc->mediasize = mdio->md_mediasize;
1285 g_resize_provider(sc->pp, sc->mediasize);
1286 g_topology_unlock();
1291 mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td)
1297 * Range check. Disallow negative sizes or any size less then the
1298 * size of a page. Then round to a page.
1300 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1304 * Allocate an OBJT_SWAP object.
1306 * Note the truncation.
1309 npage = mdio->md_mediasize / PAGE_SIZE;
1310 if (mdio->md_fwsectors != 0)
1311 sc->fwsectors = mdio->md_fwsectors;
1312 if (mdio->md_fwheads != 0)
1313 sc->fwheads = mdio->md_fwheads;
1314 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1315 VM_PROT_DEFAULT, 0, td->td_ucred);
1316 if (sc->object == NULL)
1318 sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE);
1319 if (mdio->md_options & MD_RESERVE) {
1320 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1325 error = mdsetcred(sc, td->td_ucred);
1328 vm_object_deallocate(sc->object);
1336 xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1338 struct md_ioctl *mdio;
1344 printf("mdctlioctl(%s %lx %p %x %p)\n",
1345 devtoname(dev), cmd, addr, flags, td);
1347 mdio = (struct md_ioctl *)addr;
1348 if (mdio->md_version != MDIOVERSION)
1352 * We assert the version number in the individual ioctl
1353 * handlers instead of out here because (a) it is possible we
1354 * may add another ioctl in the future which doesn't read an
1355 * mdio, and (b) the correct return value for an unknown ioctl
1356 * is ENOIOCTL, not EINVAL.
1361 switch (mdio->md_type) {
1370 if (mdio->md_sectorsize == 0)
1371 sectsize = DEV_BSIZE;
1373 sectsize = mdio->md_sectorsize;
1374 if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize)
1376 if (mdio->md_options & MD_AUTOUNIT)
1377 sc = mdnew(-1, &error, mdio->md_type);
1379 if (mdio->md_unit > INT_MAX)
1381 sc = mdnew(mdio->md_unit, &error, mdio->md_type);
1385 if (mdio->md_options & MD_AUTOUNIT)
1386 mdio->md_unit = sc->unit;
1387 sc->mediasize = mdio->md_mediasize;
1388 sc->sectorsize = sectsize;
1392 sc->start = mdstart_malloc;
1393 error = mdcreate_malloc(sc, mdio);
1397 * We disallow attaching preloaded memory disks via
1398 * ioctl. Preloaded memory disks are automatically
1399 * attached in g_md_init().
1404 sc->start = mdstart_vnode;
1405 error = mdcreate_vnode(sc, mdio, td);
1408 sc->start = mdstart_swap;
1409 error = mdcreate_swap(sc, mdio, td);
1417 /* Prune off any residual fractional sector */
1418 i = sc->mediasize % sc->sectorsize;
1424 if (mdio->md_mediasize != 0 ||
1425 (mdio->md_options & ~MD_FORCE) != 0)
1428 sc = mdfind(mdio->md_unit);
1431 if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1432 !(mdio->md_options & MD_FORCE))
1434 return (mddestroy(sc, td));
1436 if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1439 sc = mdfind(mdio->md_unit);
1442 if (mdio->md_mediasize < sc->sectorsize)
1444 if (mdio->md_mediasize < sc->mediasize &&
1445 !(sc->flags & MD_FORCE) &&
1446 !(mdio->md_options & MD_FORCE))
1448 return (mdresize(sc, mdio));
1450 sc = mdfind(mdio->md_unit);
1453 mdio->md_type = sc->type;
1454 mdio->md_options = sc->flags;
1455 mdio->md_mediasize = sc->mediasize;
1456 mdio->md_sectorsize = sc->sectorsize;
1457 if (sc->type == MD_VNODE)
1458 error = copyout(sc->file, mdio->md_file,
1459 strlen(sc->file) + 1);
1463 LIST_FOREACH(sc, &md_softc_list, list) {
1464 if (i == MDNPAD - 1)
1465 mdio->md_pad[i] = -1;
1467 mdio->md_pad[i++] = sc->unit;
1469 mdio->md_pad[0] = i - 1;
1477 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
1482 error = xmdctlioctl(dev, cmd, addr, flags, td);
1488 md_preloaded(u_char *image, size_t length, const char *name)
1493 sc = mdnew(-1, &error, MD_PRELOAD);
1496 sc->mediasize = length;
1497 sc->sectorsize = DEV_BSIZE;
1499 sc->pl_len = length;
1500 sc->start = mdstart_preload;
1503 rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
1507 printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
1508 MD_NAME, sc->unit, name, length, image);
1513 g_md_init(struct g_class *mp __unused)
1516 u_char *ptr, *name, *type;
1520 /* figure out log2(NINDIR) */
1521 for (i = NINDIR, nshift = -1; i; nshift++)
1525 sx_init(&md_sx, "MD config lock");
1526 g_topology_unlock();
1527 md_uh = new_unrhdr(0, INT_MAX, NULL);
1530 md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL);
1533 /* XXX: are preload_* static or do they need Giant ? */
1534 while ((mod = preload_search_next_name(mod)) != NULL) {
1535 name = (char *)preload_search_info(mod, MODINFO_NAME);
1538 type = (char *)preload_search_info(mod, MODINFO_TYPE);
1541 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
1543 ptr = preload_fetch_addr(mod);
1544 len = preload_fetch_size(mod);
1545 if (ptr != NULL && len != 0) {
1547 md_preloaded(ptr, len, name);
1551 md_vnode_pbuf_freecnt = nswbuf / 10;
1552 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
1558 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1559 struct g_consumer *cp __unused, struct g_provider *pp)
1587 if (indent == NULL) {
1588 sbuf_printf(sb, " u %d", mp->unit);
1589 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
1590 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
1591 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
1592 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
1593 sbuf_printf(sb, " t %s", type);
1594 if (mp->type == MD_VNODE && mp->vnode != NULL)
1595 sbuf_printf(sb, " file %s", mp->file);
1597 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
1599 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
1600 indent, (uintmax_t) mp->sectorsize);
1601 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
1602 indent, (uintmax_t) mp->fwheads);
1603 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
1604 indent, (uintmax_t) mp->fwsectors);
1605 sbuf_printf(sb, "%s<length>%ju</length>\n",
1606 indent, (uintmax_t) mp->mediasize);
1607 sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
1608 (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
1609 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
1610 (mp->flags & MD_READONLY) == 0 ? "read-write":
1612 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
1614 if (mp->type == MD_VNODE && mp->vnode != NULL)
1615 sbuf_printf(sb, "%s<file>%s</file>\n",
1622 g_md_fini(struct g_class *mp __unused)
1626 if (status_dev != NULL)
1627 destroy_dev(status_dev);
1628 delete_unrhdr(md_uh);