2 * SPDX-License-Identifier: (Beerware AND BSD-3-Clause)
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
16 * The following functions are based on the vn(4) driver: mdstart_swap(),
17 * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(),
18 * and as such under the following copyright:
20 * Copyright (c) 1988 University of Utah.
21 * Copyright (c) 1990, 1993
22 * The Regents of the University of California. All rights reserved.
23 * Copyright (c) 2013 The FreeBSD Foundation
24 * All rights reserved.
26 * This code is derived from software contributed to Berkeley by
27 * the Systems Programming Group of the University of Utah Computer
30 * Portions of this software were developed by Konstantin Belousov
31 * under sponsorship from the FreeBSD Foundation.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * from: Utah Hdr: vn.c 1.13 94/04/02
59 * from: @(#)vn.c 8.6 (Berkeley) 4/1/94
60 * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03
63 #include "opt_rootdevname.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
72 #include <sys/devicestat.h>
73 #include <sys/fcntl.h>
74 #include <sys/kernel.h>
75 #include <sys/kthread.h>
76 #include <sys/limits.h>
77 #include <sys/linker.h>
79 #include <sys/malloc.h>
80 #include <sys/mdioctl.h>
81 #include <sys/mount.h>
82 #include <sys/mutex.h>
84 #include <sys/namei.h>
86 #include <sys/queue.h>
87 #include <sys/rwlock.h>
89 #include <sys/sched.h>
90 #include <sys/sf_buf.h>
91 #include <sys/sysctl.h>
93 #include <sys/vnode.h>
96 #include <geom/geom.h>
97 #include <geom/geom_int.h>
100 #include <vm/vm_param.h>
101 #include <vm/vm_object.h>
102 #include <vm/vm_page.h>
103 #include <vm/vm_pager.h>
104 #include <vm/swap_pager.h>
107 #include <machine/bus.h>
111 #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */
112 #define MD_EXITING 0x20000 /* Worker thread is exiting. */
113 #define MD_PROVIDERGONE 0x40000 /* Safe to free the softc */
116 #define MD_NSECT (10000 * 2)
120 unsigned md_unit; /* unit number */
121 enum md_types md_type; /* type of disk */
122 off_t md_mediasize; /* size of disk in bytes */
123 unsigned md_sectorsize; /* sectorsize */
124 unsigned md_options; /* options */
125 int md_fwheads; /* firmware heads */
126 int md_fwsectors; /* firmware sectors */
127 char *md_file; /* pathname of file to mount */
128 enum uio_seg md_file_seg; /* location of md_file */
129 char *md_label; /* label of the device (userspace) */
130 int *md_units; /* pointer to units array (kernel) */
131 size_t md_units_nitems; /* items in md_units array */
134 #ifdef COMPAT_FREEBSD32
138 enum md_types md_type;
141 unsigned md_sectorsize;
148 } __attribute__((__packed__));
149 CTASSERT((sizeof(struct md_ioctl32)) == 436);
151 #define MDIOCATTACH_32 _IOC_NEWTYPE(MDIOCATTACH, struct md_ioctl32)
152 #define MDIOCDETACH_32 _IOC_NEWTYPE(MDIOCDETACH, struct md_ioctl32)
153 #define MDIOCQUERY_32 _IOC_NEWTYPE(MDIOCQUERY, struct md_ioctl32)
154 #define MDIOCRESIZE_32 _IOC_NEWTYPE(MDIOCRESIZE, struct md_ioctl32)
155 #endif /* COMPAT_FREEBSD32 */
157 static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk");
158 static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors");
161 SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0,
162 "Enable md(4) debug messages");
163 static int md_malloc_wait;
164 SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0,
165 "Allow malloc to wait for memory allocations");
167 #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE)
168 #define MD_ROOT_FSTYPE "ufs"
173 * Preloaded image gets put here.
175 #if defined(MD_ROOT_SIZE)
177 * We put the mfs_root symbol into the oldmfs section of the kernel object file.
178 * Applications that patch the object with the image can determine
179 * the size looking at the oldmfs section size within the kernel.
181 u_char mfs_root[MD_ROOT_SIZE*1024] __attribute__ ((section ("oldmfs")));
182 const int mfs_root_size = sizeof(mfs_root);
183 #elif defined(MD_ROOT_MEM)
184 /* MD region already mapped in the memory */
188 extern volatile u_char __weak_symbol mfs_root;
189 extern volatile u_char __weak_symbol mfs_root_end;
191 __GLOBL(mfs_root_end);
192 #define mfs_root_size ((uintptr_t)(&mfs_root_end - &mfs_root))
196 static g_init_t g_md_init;
197 static g_fini_t g_md_fini;
198 static g_start_t g_md_start;
199 static g_access_t g_md_access;
200 static void g_md_dumpconf(struct sbuf *sb, const char *indent,
201 struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp);
202 static g_provgone_t g_md_providergone;
204 static struct cdev *status_dev = NULL;
205 static struct sx md_sx;
206 static struct unrhdr *md_uh;
208 static d_ioctl_t mdctlioctl;
210 static struct cdevsw mdctl_cdevsw = {
211 .d_version = D_VERSION,
212 .d_ioctl = mdctlioctl,
216 struct g_class g_md_class = {
218 .version = G_VERSION,
222 .access = g_md_access,
223 .dumpconf = g_md_dumpconf,
224 .providergone = g_md_providergone,
227 DECLARE_GEOM_CLASS(g_md_class, g_md);
229 static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list);
231 #define NINDIR (PAGE_SIZE / sizeof(uintptr_t))
232 #define NMASK (NINDIR-1)
235 static uma_zone_t md_pbuf_zone;
246 LIST_ENTRY(md_s) list;
247 struct bio_queue_head bio_queue;
248 struct mtx queue_mtx;
261 struct g_provider *pp;
262 int (*start)(struct md_s *sc, struct bio *bp);
263 struct devstat *devstat;
265 /* MD_MALLOC related fields */
269 /* MD_PRELOAD related fields */
273 /* MD_VNODE related fields */
276 char label[PATH_MAX];
279 /* MD_SWAP related fields */
283 static struct indir *
284 new_indir(u_int shift)
288 ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT)
292 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
293 M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO);
294 if (ip->array == NULL) {
304 del_indir(struct indir *ip)
307 free(ip->array, M_MDSECT);
312 destroy_indir(struct md_s *sc, struct indir *ip)
316 for (i = 0; i < NINDIR; i++) {
320 destroy_indir(sc, (struct indir*)(ip->array[i]));
321 else if (ip->array[i] > 255)
322 uma_zfree(sc->uma, (void *)(ip->array[i]));
328 * This function does the math and allocates the top level "indir" structure
329 * for a device of "size" sectors.
332 static struct indir *
333 dimension(off_t size)
341 while (rcnt > NINDIR) {
347 * XXX: the top layer is probably not fully populated, so we allocate
348 * too much space for ip->array in here.
350 ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO);
351 ip->array = malloc(sizeof(uintptr_t) * NINDIR,
352 M_MDSECT, M_WAITOK | M_ZERO);
354 ip->shift = layer * nshift;
359 * Read a given sector
363 s_read(struct indir *ip, off_t offset)
370 printf("s_read(%jd)\n", (intmax_t)offset);
372 for (cip = ip; cip != NULL;) {
374 idx = (offset >> cip->shift) & NMASK;
375 up = cip->array[idx];
376 cip = (struct indir *)up;
379 idx = offset & NMASK;
380 return (cip->array[idx]);
386 * Write a given sector, prune the tree if the value is 0
390 s_write(struct indir *ip, off_t offset, uintptr_t ptr)
392 struct indir *cip, *lip[10];
397 printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr);
404 idx = (offset >> cip->shift) & NMASK;
405 up = cip->array[idx];
407 cip = (struct indir *)up;
410 /* Allocate branch */
412 (uintptr_t)new_indir(cip->shift - nshift);
413 if (cip->array[idx] == 0)
416 up = cip->array[idx];
417 cip = (struct indir *)up;
421 idx = offset & NMASK;
422 up = cip->array[idx];
425 cip->array[idx] = ptr;
430 if (cip->used != 0 || li == 1)
433 while (cip->used == 0 && cip != ip) {
435 idx = (offset >> lip[li]->shift) & NMASK;
436 up = lip[li]->array[idx];
437 KASSERT(up == (uintptr_t)cip, ("md screwed up"));
439 lip[li]->array[idx] = 0;
447 g_md_access(struct g_provider *pp, int r, int w, int e)
451 sc = pp->geom->softc;
453 if (r <= 0 && w <= 0 && e <= 0)
460 if ((sc->flags & MD_READONLY) != 0 && w > 0)
462 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
464 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
471 g_md_start(struct bio *bp)
475 sc = bp->bio_to->geom->softc;
476 if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) {
477 devstat_start_transaction_bio(sc->devstat, bp);
479 mtx_lock(&sc->queue_mtx);
480 bioq_disksort(&sc->bio_queue, bp);
482 mtx_unlock(&sc->queue_mtx);
485 #define MD_MALLOC_MOVE_ZERO 1
486 #define MD_MALLOC_MOVE_FILL 2
487 #define MD_MALLOC_MOVE_READ 3
488 #define MD_MALLOC_MOVE_WRITE 4
489 #define MD_MALLOC_MOVE_CMP 5
492 md_malloc_move_ma(vm_page_t **mp, int *ma_offs, unsigned sectorsize,
493 void *ptr, u_char fill, int op)
500 int error, i, ma_offs1, sz, first_read;
505 /* if (op == MD_MALLOC_MOVE_CMP) { gcc */
513 for (n = sectorsize; n != 0; n -= sz) {
514 sz = imin(PAGE_SIZE - *ma_offs, n);
519 sf = sf_buf_alloc(m, SFB_CPUPRIVATE |
520 (md_malloc_wait ? 0 : SFB_NOWAIT));
526 p = (char *)sf_buf_kva(sf) + *ma_offs;
528 case MD_MALLOC_MOVE_ZERO:
531 case MD_MALLOC_MOVE_FILL:
534 case MD_MALLOC_MOVE_READ:
536 cpu_flush_dcache(p, sz);
538 case MD_MALLOC_MOVE_WRITE:
541 case MD_MALLOC_MOVE_CMP:
542 for (i = 0; i < sz; i++, p++) {
547 } else if (*p != first) {
554 KASSERT(0, ("md_malloc_move_ma unknown op %d\n", op));
560 *ma_offs %= PAGE_SIZE;
563 ptr = (char *)ptr + sz;
569 if (op == MD_MALLOC_MOVE_CMP && error != 0) {
577 md_malloc_move_vlist(bus_dma_segment_t **pvlist, int *pma_offs,
578 unsigned len, void *ptr, u_char fill, int op)
580 bus_dma_segment_t *vlist;
581 uint8_t *p, *end, first;
583 int ma_offs, seg_len;
589 for (; len != 0; len -= seg_len) {
590 seg_len = imin(vlist->ds_len - ma_offs, len);
591 p = (uint8_t *)(uintptr_t)vlist->ds_addr + ma_offs;
593 case MD_MALLOC_MOVE_ZERO:
596 case MD_MALLOC_MOVE_FILL:
597 memset(p, fill, seg_len);
599 case MD_MALLOC_MOVE_READ:
600 bcopy(ptr, p, seg_len);
601 cpu_flush_dcache(p, seg_len);
603 case MD_MALLOC_MOVE_WRITE:
604 bcopy(p, ptr, seg_len);
606 case MD_MALLOC_MOVE_CMP:
609 /* Confirm all following bytes match the first */
616 KASSERT(0, ("md_malloc_move_vlist unknown op %d\n", op));
621 if (ma_offs == vlist->ds_len) {
625 ptr = (uint8_t *)ptr + seg_len;
634 mdstart_malloc(struct md_s *sc, struct bio *bp)
638 bus_dma_segment_t *vlist;
639 int i, error, error1, ma_offs, notmapped;
640 off_t secno, nsec, uc;
643 switch (bp->bio_cmd) {
652 notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0;
653 vlist = (bp->bio_flags & BIO_VLIST) != 0 ?
654 (bus_dma_segment_t *)bp->bio_data : NULL;
657 ma_offs = bp->bio_ma_offset;
659 KASSERT(vlist == NULL, ("vlists cannot be unmapped"));
660 } else if (vlist != NULL) {
661 ma_offs = bp->bio_ma_offset;
667 nsec = bp->bio_length / sc->sectorsize;
668 secno = bp->bio_offset / sc->sectorsize;
671 osp = s_read(sc->indir, secno);
672 if (bp->bio_cmd == BIO_DELETE) {
674 error = s_write(sc->indir, secno, 0);
675 } else if (bp->bio_cmd == BIO_READ) {
678 error = md_malloc_move_ma(&m, &ma_offs,
679 sc->sectorsize, NULL, 0,
680 MD_MALLOC_MOVE_ZERO);
681 } else if (vlist != NULL) {
682 error = md_malloc_move_vlist(&vlist,
683 &ma_offs, sc->sectorsize, NULL, 0,
684 MD_MALLOC_MOVE_ZERO);
686 bzero(dst, sc->sectorsize);
687 } else if (osp <= 255) {
689 error = md_malloc_move_ma(&m, &ma_offs,
690 sc->sectorsize, NULL, osp,
691 MD_MALLOC_MOVE_FILL);
692 } else if (vlist != NULL) {
693 error = md_malloc_move_vlist(&vlist,
694 &ma_offs, sc->sectorsize, NULL, osp,
695 MD_MALLOC_MOVE_FILL);
697 memset(dst, osp, sc->sectorsize);
700 error = md_malloc_move_ma(&m, &ma_offs,
701 sc->sectorsize, (void *)osp, 0,
702 MD_MALLOC_MOVE_READ);
703 } else if (vlist != NULL) {
704 error = md_malloc_move_vlist(&vlist,
705 &ma_offs, sc->sectorsize,
707 MD_MALLOC_MOVE_READ);
709 bcopy((void *)osp, dst, sc->sectorsize);
710 cpu_flush_dcache(dst, sc->sectorsize);
714 } else if (bp->bio_cmd == BIO_WRITE) {
715 if (sc->flags & MD_COMPRESS) {
717 error1 = md_malloc_move_ma(&m, &ma_offs,
718 sc->sectorsize, &uc, 0,
720 i = error1 == 0 ? sc->sectorsize : 0;
721 } else if (vlist != NULL) {
722 error1 = md_malloc_move_vlist(&vlist,
723 &ma_offs, sc->sectorsize, &uc, 0,
725 i = error1 == 0 ? sc->sectorsize : 0;
728 for (i = 1; i < sc->sectorsize; i++) {
737 if (i == sc->sectorsize) {
739 error = s_write(sc->indir, secno, uc);
742 sp = (uintptr_t)uma_zalloc(sc->uma,
743 md_malloc_wait ? M_WAITOK :
750 error = md_malloc_move_ma(&m,
751 &ma_offs, sc->sectorsize,
753 MD_MALLOC_MOVE_WRITE);
754 } else if (vlist != NULL) {
755 error = md_malloc_move_vlist(
757 sc->sectorsize, (void *)sp,
758 0, MD_MALLOC_MOVE_WRITE);
760 bcopy(dst, (void *)sp,
763 error = s_write(sc->indir, secno, sp);
766 error = md_malloc_move_ma(&m,
767 &ma_offs, sc->sectorsize,
769 MD_MALLOC_MOVE_WRITE);
770 } else if (vlist != NULL) {
771 error = md_malloc_move_vlist(
773 sc->sectorsize, (void *)osp,
774 0, MD_MALLOC_MOVE_WRITE);
776 bcopy(dst, (void *)osp,
786 uma_zfree(sc->uma, (void*)osp);
790 if (!notmapped && vlist == NULL)
791 dst += sc->sectorsize;
798 mdcopyto_vlist(void *src, bus_dma_segment_t *vlist, off_t offset, off_t len)
802 while (offset >= vlist->ds_len) {
803 offset -= vlist->ds_len;
808 seg_len = omin(len, vlist->ds_len - offset);
809 bcopy(src, (void *)(uintptr_t)(vlist->ds_addr + offset),
812 src = (uint8_t *)src + seg_len;
819 mdcopyfrom_vlist(bus_dma_segment_t *vlist, off_t offset, void *dst, off_t len)
823 while (offset >= vlist->ds_len) {
824 offset -= vlist->ds_len;
829 seg_len = omin(len, vlist->ds_len - offset);
830 bcopy((void *)(uintptr_t)(vlist->ds_addr + offset), dst,
833 dst = (uint8_t *)dst + seg_len;
840 mdstart_preload(struct md_s *sc, struct bio *bp)
844 p = sc->pl_ptr + bp->bio_offset;
845 switch (bp->bio_cmd) {
847 if ((bp->bio_flags & BIO_VLIST) != 0) {
848 mdcopyto_vlist(p, (bus_dma_segment_t *)bp->bio_data,
849 bp->bio_ma_offset, bp->bio_length);
851 bcopy(p, bp->bio_data, bp->bio_length);
853 cpu_flush_dcache(bp->bio_data, bp->bio_length);
856 if ((bp->bio_flags & BIO_VLIST) != 0) {
857 mdcopyfrom_vlist((bus_dma_segment_t *)bp->bio_data,
858 bp->bio_ma_offset, p, bp->bio_length);
860 bcopy(bp->bio_data, p, bp->bio_length);
869 mdstart_vnode(struct md_s *sc, struct bio *bp)
878 bus_dma_segment_t *vlist;
880 off_t iolen, iostart, len, zerosize;
883 switch (bp->bio_cmd) {
885 auio.uio_rw = UIO_READ;
889 auio.uio_rw = UIO_WRITE;
901 ma_offs = bp->bio_ma_offset;
902 len = bp->bio_length;
907 * If an error occurs, we set BIO_ERROR but we do not set
908 * B_INVAL because (for a write anyway), the buffer is
912 if (bp->bio_cmd == BIO_FLUSH) {
913 (void) vn_start_write(vp, &mp, V_WAIT);
914 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
915 error = VOP_FSYNC(vp, MNT_WAIT, td);
917 vn_finished_write(mp);
921 auio.uio_offset = (vm_ooffset_t)bp->bio_offset;
922 auio.uio_resid = bp->bio_length;
923 auio.uio_segflg = UIO_SYSSPACE;
926 if (bp->bio_cmd == BIO_DELETE) {
928 * Emulate BIO_DELETE by writing zeros.
930 zerosize = ZERO_REGION_SIZE -
931 (ZERO_REGION_SIZE % sc->sectorsize);
932 auio.uio_iovcnt = howmany(bp->bio_length, zerosize);
933 piov = malloc(sizeof(*piov) * auio.uio_iovcnt, M_MD, M_WAITOK);
936 piov->iov_base = __DECONST(void *, zero_region);
939 piov->iov_len = zerosize;
940 len -= piov->iov_len;
944 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
945 piov = malloc(sizeof(*piov) * bp->bio_ma_n, M_MD, M_WAITOK);
947 vlist = (bus_dma_segment_t *)bp->bio_data;
949 piov->iov_base = (void *)(uintptr_t)(vlist->ds_addr +
951 piov->iov_len = vlist->ds_len - ma_offs;
952 if (piov->iov_len > len)
954 len -= piov->iov_len;
959 auio.uio_iovcnt = piov - auio.uio_iov;
961 } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
962 pb = uma_zalloc(md_pbuf_zone, M_WAITOK);
963 MPASS((pb->b_flags & B_MAXPHYS) != 0);
966 npages = atop(min(maxphys, round_page(len + (ma_offs &
968 iolen = min(ptoa(npages) - (ma_offs & PAGE_MASK), len);
969 KASSERT(iolen > 0, ("zero iolen"));
970 pmap_qenter((vm_offset_t)pb->b_data,
971 &bp->bio_ma[atop(ma_offs)], npages);
972 aiov.iov_base = (void *)((vm_offset_t)pb->b_data +
973 (ma_offs & PAGE_MASK));
974 aiov.iov_len = iolen;
975 auio.uio_iov = &aiov;
977 auio.uio_resid = iolen;
979 aiov.iov_base = bp->bio_data;
980 aiov.iov_len = bp->bio_length;
981 auio.uio_iov = &aiov;
984 iostart = auio.uio_offset;
985 if (auio.uio_rw == UIO_READ) {
986 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
987 error = VOP_READ(vp, &auio, 0, sc->cred);
990 (void) vn_start_write(vp, &mp, V_WAIT);
991 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
992 error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC,
995 vn_finished_write(mp);
997 sc->flags &= ~MD_VERIFY;
1000 /* When MD_CACHE is set, try to avoid double-caching the data. */
1001 if (error == 0 && (sc->flags & MD_CACHE) == 0)
1002 VOP_ADVISE(vp, iostart, auio.uio_offset - 1,
1003 POSIX_FADV_DONTNEED);
1006 pmap_qremove((vm_offset_t)pb->b_data, npages);
1009 bp->bio_resid -= iolen;
1014 uma_zfree(md_pbuf_zone, pb);
1019 bp->bio_resid = auio.uio_resid;
1024 mdstart_swap(struct md_s *sc, struct bio *bp)
1028 vm_pindex_t i, lastp;
1029 bus_dma_segment_t *vlist;
1030 int rv, ma_offs, offs, len, lastend;
1032 switch (bp->bio_cmd) {
1038 return (EOPNOTSUPP);
1042 ma_offs = (bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0 ?
1043 bp->bio_ma_offset : 0;
1044 vlist = (bp->bio_flags & BIO_VLIST) != 0 ?
1045 (bus_dma_segment_t *)bp->bio_data : NULL;
1048 * offs is the offset at which to start operating on the
1049 * next (ie, first) page. lastp is the last page on
1050 * which we're going to operate. lastend is the ending
1051 * position within that last page (ie, PAGE_SIZE if
1052 * we're operating on complete aligned pages).
1054 offs = bp->bio_offset % PAGE_SIZE;
1055 lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE;
1056 lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1;
1059 vm_object_pip_add(sc->object, 1);
1060 for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) {
1061 len = ((i == lastp) ? lastend : PAGE_SIZE) - offs;
1062 m = vm_page_grab_unlocked(sc->object, i, VM_ALLOC_SYSTEM);
1063 if (bp->bio_cmd == BIO_READ) {
1064 if (vm_page_all_valid(m))
1067 rv = vm_pager_get_pages(sc->object, &m, 1,
1069 if (rv == VM_PAGER_ERROR) {
1070 VM_OBJECT_WLOCK(sc->object);
1072 VM_OBJECT_WUNLOCK(sc->object);
1074 } else if (rv == VM_PAGER_FAIL) {
1076 * Pager does not have the page. Zero
1077 * the allocated page, and mark it as
1078 * valid. Do not set dirty, the page
1079 * can be recreated if thrown out.
1084 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
1085 pmap_copy_pages(&m, offs, bp->bio_ma,
1087 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
1088 physcopyout_vlist(VM_PAGE_TO_PHYS(m) + offs,
1089 vlist, ma_offs, len);
1090 cpu_flush_dcache(p, len);
1092 physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len);
1093 cpu_flush_dcache(p, len);
1095 } else if (bp->bio_cmd == BIO_WRITE) {
1096 if (len == PAGE_SIZE || vm_page_all_valid(m))
1099 rv = vm_pager_get_pages(sc->object, &m, 1,
1101 if (rv == VM_PAGER_ERROR) {
1102 VM_OBJECT_WLOCK(sc->object);
1104 VM_OBJECT_WUNLOCK(sc->object);
1106 } else if (rv == VM_PAGER_FAIL)
1109 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
1110 pmap_copy_pages(bp->bio_ma, ma_offs, &m,
1112 } else if ((bp->bio_flags & BIO_VLIST) != 0) {
1113 physcopyin_vlist(vlist, ma_offs,
1114 VM_PAGE_TO_PHYS(m) + offs, len);
1116 physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len);
1120 vm_page_set_dirty(m);
1121 } else if (bp->bio_cmd == BIO_DELETE) {
1122 if (len == PAGE_SIZE || vm_page_all_valid(m))
1125 rv = vm_pager_get_pages(sc->object, &m, 1,
1127 VM_OBJECT_WLOCK(sc->object);
1128 if (rv == VM_PAGER_ERROR) {
1130 VM_OBJECT_WUNLOCK(sc->object);
1132 } else if (rv == VM_PAGER_FAIL) {
1136 /* Page is valid. */
1137 if (len != PAGE_SIZE) {
1138 pmap_zero_page_area(m, offs, len);
1139 vm_page_set_dirty(m);
1141 vm_pager_page_unswapped(m);
1146 VM_OBJECT_WUNLOCK(sc->object);
1152 * The page may be deactivated prior to setting
1153 * PGA_REFERENCED, but in this case it will be
1154 * reactivated by the page daemon.
1156 if (vm_page_active(m))
1157 vm_page_reference(m);
1159 vm_page_activate(m);
1162 /* Actions on further pages start at offset 0 */
1163 p += PAGE_SIZE - offs;
1167 vm_object_pip_wakeup(sc->object);
1168 return (rv != VM_PAGER_ERROR ? 0 : ENOSPC);
1172 mdstart_null(struct md_s *sc, struct bio *bp)
1175 switch (bp->bio_cmd) {
1177 bzero(bp->bio_data, bp->bio_length);
1178 cpu_flush_dcache(bp->bio_data, bp->bio_length);
1188 md_kthread(void *arg)
1195 thread_lock(curthread);
1196 sched_prio(curthread, PRIBIO);
1197 thread_unlock(curthread);
1198 if (sc->type == MD_VNODE)
1199 curthread->td_pflags |= TDP_NORUNNINGBUF;
1202 mtx_lock(&sc->queue_mtx);
1203 if (sc->flags & MD_SHUTDOWN) {
1204 sc->flags |= MD_EXITING;
1205 mtx_unlock(&sc->queue_mtx);
1208 bp = bioq_takefirst(&sc->bio_queue);
1210 msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0);
1213 mtx_unlock(&sc->queue_mtx);
1214 if (bp->bio_cmd == BIO_GETATTR) {
1215 int isv = ((sc->flags & MD_VERIFY) != 0);
1217 if ((sc->fwsectors && sc->fwheads &&
1218 (g_handleattr_int(bp, "GEOM::fwsectors",
1220 g_handleattr_int(bp, "GEOM::fwheads",
1222 g_handleattr_int(bp, "GEOM::candelete", 1))
1224 else if (sc->ident[0] != '\0' &&
1225 g_handleattr_str(bp, "GEOM::ident", sc->ident))
1227 else if (g_handleattr_int(bp, "MNT::verified", isv))
1232 error = sc->start(sc, bp);
1235 if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
1237 * Devstat uses (bio_bcount, bio_resid) for
1238 * determining the length of the completed part of
1239 * the i/o. g_io_deliver() will translate from
1240 * bio_completed to that, but it also destroys the
1241 * bio so we must do our own translation.
1243 bp->bio_bcount = bp->bio_length;
1244 bp->bio_resid = (error == -1 ? bp->bio_bcount : 0);
1245 devstat_end_transaction_bio(sc->devstat, bp);
1248 bp->bio_completed = bp->bio_length;
1249 g_io_deliver(bp, error);
1254 static struct md_s *
1259 LIST_FOREACH(sc, &md_softc_list, list) {
1260 if (sc->unit == unit)
1266 static struct md_s *
1267 mdnew(int unit, int *errp, enum md_types type)
1274 unit = alloc_unr(md_uh);
1276 unit = alloc_unr_specific(md_uh, unit);
1283 sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO);
1285 bioq_init(&sc->bio_queue);
1286 mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF);
1288 sprintf(sc->name, "md%d", unit);
1289 LIST_INSERT_HEAD(&md_softc_list, sc, list);
1290 error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name);
1293 LIST_REMOVE(sc, list);
1294 mtx_destroy(&sc->queue_mtx);
1295 free_unr(md_uh, sc->unit);
1302 mdinit(struct md_s *sc)
1305 struct g_provider *pp;
1308 gp = g_new_geomf(&g_md_class, "md%d", sc->unit);
1310 pp = g_new_providerf(gp, "md%d", sc->unit);
1311 devstat_remove_entry(pp->stat);
1313 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
1314 pp->mediasize = sc->mediasize;
1315 pp->sectorsize = sc->sectorsize;
1320 pp->flags |= G_PF_ACCEPT_UNMAPPED;
1328 sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize,
1329 DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
1330 sc->devstat->id = pp;
1331 g_error_provider(pp, 0);
1332 g_topology_unlock();
1336 mdcreate_malloc(struct md_s *sc, struct md_req *mdr)
1343 if (mdr->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE))
1345 if (mdr->md_sectorsize != 0 && !powerof2(mdr->md_sectorsize))
1347 /* Compression doesn't make sense if we have reserved space */
1348 if (mdr->md_options & MD_RESERVE)
1349 mdr->md_options &= ~MD_COMPRESS;
1350 if (mdr->md_fwsectors != 0)
1351 sc->fwsectors = mdr->md_fwsectors;
1352 if (mdr->md_fwheads != 0)
1353 sc->fwheads = mdr->md_fwheads;
1354 sc->flags = mdr->md_options & (MD_COMPRESS | MD_FORCE);
1355 sc->indir = dimension(sc->mediasize / sc->sectorsize);
1356 sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL,
1358 if (mdr->md_options & MD_RESERVE) {
1361 nsectors = sc->mediasize / sc->sectorsize;
1362 for (u = 0; u < nsectors; u++) {
1363 sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ?
1364 M_WAITOK : M_NOWAIT) | M_ZERO);
1366 error = s_write(sc->indir, u, sp);
1377 mdsetcred(struct md_s *sc, struct ucred *cred)
1383 * Set credits in our softc
1388 sc->cred = crhold(cred);
1391 * Horrible kludge to establish credentials for NFS XXX.
1398 tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK);
1399 bzero(&auio, sizeof(auio));
1401 aiov.iov_base = tmpbuf;
1402 aiov.iov_len = sc->sectorsize;
1403 auio.uio_iov = &aiov;
1404 auio.uio_iovcnt = 1;
1405 auio.uio_offset = 0;
1406 auio.uio_rw = UIO_READ;
1407 auio.uio_segflg = UIO_SYSSPACE;
1408 auio.uio_resid = aiov.iov_len;
1409 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1410 error = VOP_READ(sc->vnode, &auio, 0, sc->cred);
1411 VOP_UNLOCK(sc->vnode);
1412 free(tmpbuf, M_TEMP);
1418 mdcreate_vnode(struct md_s *sc, struct md_req *mdr, struct thread *td)
1421 struct nameidata nd;
1425 fname = mdr->md_file;
1426 if (mdr->md_file_seg == UIO_USERSPACE) {
1427 error = copyinstr(fname, sc->file, sizeof(sc->file), NULL);
1430 } else if (mdr->md_file_seg == UIO_SYSSPACE)
1431 strlcpy(sc->file, fname, sizeof(sc->file));
1436 * If the user specified that this is a read only device, don't
1437 * set the FWRITE mask before trying to open the backing store.
1439 flags = FREAD | ((mdr->md_options & MD_READONLY) ? 0 : FWRITE) \
1440 | ((mdr->md_options & MD_VERIFY) ? O_VERIFY : 0);
1441 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td);
1442 error = vn_open(&nd, &flags, 0, NULL);
1445 NDFREE(&nd, NDF_ONLY_PNBUF);
1446 if (nd.ni_vp->v_type != VREG) {
1450 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
1453 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
1454 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
1455 if (VN_IS_DOOMED(nd.ni_vp)) {
1456 /* Forced unmount. */
1461 nd.ni_vp->v_vflag |= VV_MD;
1462 VOP_UNLOCK(nd.ni_vp);
1464 if (mdr->md_fwsectors != 0)
1465 sc->fwsectors = mdr->md_fwsectors;
1466 if (mdr->md_fwheads != 0)
1467 sc->fwheads = mdr->md_fwheads;
1468 snprintf(sc->ident, sizeof(sc->ident), "MD-DEV%ju-INO%ju",
1469 (uintmax_t)vattr.va_fsid, (uintmax_t)vattr.va_fileid);
1470 sc->flags = mdr->md_options & (MD_ASYNC | MD_CACHE | MD_FORCE |
1472 if (!(flags & FWRITE))
1473 sc->flags |= MD_READONLY;
1474 sc->vnode = nd.ni_vp;
1476 error = mdsetcred(sc, td->td_ucred);
1479 vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY);
1480 nd.ni_vp->v_vflag &= ~VV_MD;
1485 VOP_UNLOCK(nd.ni_vp);
1486 (void)vn_close(nd.ni_vp, flags, td->td_ucred, td);
1491 g_md_providergone(struct g_provider *pp)
1493 struct md_s *sc = pp->geom->softc;
1495 mtx_lock(&sc->queue_mtx);
1496 sc->flags |= MD_PROVIDERGONE;
1498 mtx_unlock(&sc->queue_mtx);
1502 mddestroy(struct md_s *sc, struct thread *td)
1507 g_wither_geom(sc->gp, ENXIO);
1508 g_topology_unlock();
1510 mtx_lock(&sc->queue_mtx);
1511 while (!(sc->flags & MD_PROVIDERGONE))
1512 msleep(&sc->flags, &sc->queue_mtx, PRIBIO, "mddestroy", 0);
1513 mtx_unlock(&sc->queue_mtx);
1516 devstat_remove_entry(sc->devstat);
1519 mtx_lock(&sc->queue_mtx);
1520 sc->flags |= MD_SHUTDOWN;
1522 while (!(sc->flags & MD_EXITING))
1523 msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10);
1524 mtx_unlock(&sc->queue_mtx);
1525 mtx_destroy(&sc->queue_mtx);
1526 if (sc->vnode != NULL) {
1527 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
1528 sc->vnode->v_vflag &= ~VV_MD;
1529 VOP_UNLOCK(sc->vnode);
1530 (void)vn_close(sc->vnode, sc->flags & MD_READONLY ?
1531 FREAD : (FREAD|FWRITE), sc->cred, td);
1533 if (sc->cred != NULL)
1535 if (sc->object != NULL)
1536 vm_object_deallocate(sc->object);
1538 destroy_indir(sc, sc->indir);
1540 uma_zdestroy(sc->uma);
1542 LIST_REMOVE(sc, list);
1543 free_unr(md_uh, sc->unit);
1549 mdresize(struct md_s *sc, struct md_req *mdr)
1552 vm_pindex_t oldpages, newpages;
1559 if (mdr->md_mediasize <= 0 ||
1560 (mdr->md_mediasize % PAGE_SIZE) != 0)
1562 oldpages = OFF_TO_IDX(sc->mediasize);
1563 newpages = OFF_TO_IDX(mdr->md_mediasize);
1564 if (newpages < oldpages) {
1565 VM_OBJECT_WLOCK(sc->object);
1566 vm_object_page_remove(sc->object, newpages, 0, 0);
1567 swap_release_by_cred(IDX_TO_OFF(oldpages -
1568 newpages), sc->cred);
1569 sc->object->charge = IDX_TO_OFF(newpages);
1570 sc->object->size = newpages;
1571 VM_OBJECT_WUNLOCK(sc->object);
1572 } else if (newpages > oldpages) {
1573 res = swap_reserve_by_cred(IDX_TO_OFF(newpages -
1574 oldpages), sc->cred);
1577 if ((mdr->md_options & MD_RESERVE) ||
1578 (sc->flags & MD_RESERVE)) {
1579 error = swap_pager_reserve(sc->object,
1580 oldpages, newpages - oldpages);
1582 swap_release_by_cred(
1583 IDX_TO_OFF(newpages - oldpages),
1588 VM_OBJECT_WLOCK(sc->object);
1589 sc->object->charge = IDX_TO_OFF(newpages);
1590 sc->object->size = newpages;
1591 VM_OBJECT_WUNLOCK(sc->object);
1595 return (EOPNOTSUPP);
1598 sc->mediasize = mdr->md_mediasize;
1600 g_resize_provider(sc->pp, sc->mediasize);
1601 g_topology_unlock();
1606 mdcreate_swap(struct md_s *sc, struct md_req *mdr, struct thread *td)
1612 * Range check. Disallow negative sizes and sizes not being
1613 * multiple of page size.
1615 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1619 * Allocate an OBJT_SWAP object.
1621 * Note the truncation.
1624 if ((mdr->md_options & MD_VERIFY) != 0)
1626 npage = mdr->md_mediasize / PAGE_SIZE;
1627 if (mdr->md_fwsectors != 0)
1628 sc->fwsectors = mdr->md_fwsectors;
1629 if (mdr->md_fwheads != 0)
1630 sc->fwheads = mdr->md_fwheads;
1631 sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage,
1632 VM_PROT_DEFAULT, 0, td->td_ucred);
1633 if (sc->object == NULL)
1635 sc->flags = mdr->md_options & (MD_FORCE | MD_RESERVE);
1636 if (mdr->md_options & MD_RESERVE) {
1637 if (swap_pager_reserve(sc->object, 0, npage) < 0) {
1642 error = mdsetcred(sc, td->td_ucred);
1645 vm_object_deallocate(sc->object);
1652 mdcreate_null(struct md_s *sc, struct md_req *mdr, struct thread *td)
1656 * Range check. Disallow negative sizes and sizes not being
1657 * multiple of page size.
1659 if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0)
1666 kern_mdattach_locked(struct thread *td, struct md_req *mdr)
1672 sx_assert(&md_sx, SA_XLOCKED);
1674 switch (mdr->md_type) {
1684 if (mdr->md_sectorsize == 0)
1685 sectsize = DEV_BSIZE;
1687 sectsize = mdr->md_sectorsize;
1688 if (sectsize > maxphys || mdr->md_mediasize < sectsize)
1690 if (mdr->md_options & MD_AUTOUNIT)
1691 sc = mdnew(-1, &error, mdr->md_type);
1693 if (mdr->md_unit > INT_MAX)
1695 sc = mdnew(mdr->md_unit, &error, mdr->md_type);
1699 if (mdr->md_label != NULL)
1700 error = copyinstr(mdr->md_label, sc->label,
1701 sizeof(sc->label), NULL);
1704 if (mdr->md_options & MD_AUTOUNIT)
1705 mdr->md_unit = sc->unit;
1706 sc->mediasize = mdr->md_mediasize;
1707 sc->sectorsize = sectsize;
1711 sc->start = mdstart_malloc;
1712 error = mdcreate_malloc(sc, mdr);
1716 * We disallow attaching preloaded memory disks via
1717 * ioctl. Preloaded memory disks are automatically
1718 * attached in g_md_init().
1723 sc->start = mdstart_vnode;
1724 error = mdcreate_vnode(sc, mdr, td);
1727 sc->start = mdstart_swap;
1728 error = mdcreate_swap(sc, mdr, td);
1731 sc->start = mdstart_null;
1732 error = mdcreate_null(sc, mdr, td);
1741 /* Prune off any residual fractional sector */
1742 i = sc->mediasize % sc->sectorsize;
1750 kern_mdattach(struct thread *td, struct md_req *mdr)
1755 error = kern_mdattach_locked(td, mdr);
1761 kern_mddetach_locked(struct thread *td, struct md_req *mdr)
1765 sx_assert(&md_sx, SA_XLOCKED);
1767 if (mdr->md_mediasize != 0 ||
1768 (mdr->md_options & ~MD_FORCE) != 0)
1771 sc = mdfind(mdr->md_unit);
1774 if (sc->opencount != 0 && !(sc->flags & MD_FORCE) &&
1775 !(mdr->md_options & MD_FORCE))
1777 return (mddestroy(sc, td));
1781 kern_mddetach(struct thread *td, struct md_req *mdr)
1786 error = kern_mddetach_locked(td, mdr);
1792 kern_mdresize_locked(struct md_req *mdr)
1796 sx_assert(&md_sx, SA_XLOCKED);
1798 if ((mdr->md_options & ~(MD_FORCE | MD_RESERVE)) != 0)
1801 sc = mdfind(mdr->md_unit);
1804 if (mdr->md_mediasize < sc->sectorsize)
1806 if (mdr->md_mediasize < sc->mediasize &&
1807 !(sc->flags & MD_FORCE) &&
1808 !(mdr->md_options & MD_FORCE))
1810 return (mdresize(sc, mdr));
1814 kern_mdresize(struct md_req *mdr)
1819 error = kern_mdresize_locked(mdr);
1825 kern_mdquery_locked(struct md_req *mdr)
1830 sx_assert(&md_sx, SA_XLOCKED);
1832 sc = mdfind(mdr->md_unit);
1835 mdr->md_type = sc->type;
1836 mdr->md_options = sc->flags;
1837 mdr->md_mediasize = sc->mediasize;
1838 mdr->md_sectorsize = sc->sectorsize;
1840 if (mdr->md_label != NULL) {
1841 error = copyout(sc->label, mdr->md_label,
1842 strlen(sc->label) + 1);
1846 if (sc->type == MD_VNODE ||
1847 (sc->type == MD_PRELOAD && mdr->md_file != NULL))
1848 error = copyout(sc->file, mdr->md_file,
1849 strlen(sc->file) + 1);
1854 kern_mdquery(struct md_req *mdr)
1859 error = kern_mdquery_locked(mdr);
1864 /* Copy members that are not userspace pointers. */
1865 #define MD_IOCTL2REQ(mdio, mdr) do { \
1866 (mdr)->md_unit = (mdio)->md_unit; \
1867 (mdr)->md_type = (mdio)->md_type; \
1868 (mdr)->md_mediasize = (mdio)->md_mediasize; \
1869 (mdr)->md_sectorsize = (mdio)->md_sectorsize; \
1870 (mdr)->md_options = (mdio)->md_options; \
1871 (mdr)->md_fwheads = (mdio)->md_fwheads; \
1872 (mdr)->md_fwsectors = (mdio)->md_fwsectors; \
1873 (mdr)->md_units = &(mdio)->md_pad[0]; \
1874 (mdr)->md_units_nitems = nitems((mdio)->md_pad); \
1877 /* Copy members that might have been updated */
1878 #define MD_REQ2IOCTL(mdr, mdio) do { \
1879 (mdio)->md_unit = (mdr)->md_unit; \
1880 (mdio)->md_type = (mdr)->md_type; \
1881 (mdio)->md_mediasize = (mdr)->md_mediasize; \
1882 (mdio)->md_sectorsize = (mdr)->md_sectorsize; \
1883 (mdio)->md_options = (mdr)->md_options; \
1884 (mdio)->md_fwheads = (mdr)->md_fwheads; \
1885 (mdio)->md_fwsectors = (mdr)->md_fwsectors; \
1889 mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
1896 printf("mdctlioctl(%s %lx %p %x %p)\n",
1897 devtoname(dev), cmd, addr, flags, td);
1899 bzero(&mdr, sizeof(mdr));
1905 struct md_ioctl *mdio = (struct md_ioctl *)addr;
1906 if (mdio->md_version != MDIOVERSION)
1908 MD_IOCTL2REQ(mdio, &mdr);
1909 mdr.md_file = mdio->md_file;
1910 mdr.md_file_seg = UIO_USERSPACE;
1911 /* If the file is adjacent to the md_ioctl it's in kernel. */
1912 if ((void *)mdio->md_file == (void *)(mdio + 1))
1913 mdr.md_file_seg = UIO_SYSSPACE;
1914 mdr.md_label = mdio->md_label;
1917 #ifdef COMPAT_FREEBSD32
1918 case MDIOCATTACH_32:
1919 case MDIOCDETACH_32:
1920 case MDIOCRESIZE_32:
1921 case MDIOCQUERY_32: {
1922 struct md_ioctl32 *mdio = (struct md_ioctl32 *)addr;
1923 if (mdio->md_version != MDIOVERSION)
1925 MD_IOCTL2REQ(mdio, &mdr);
1926 mdr.md_file = (void *)(uintptr_t)mdio->md_file;
1927 mdr.md_file_seg = UIO_USERSPACE;
1928 mdr.md_label = (void *)(uintptr_t)mdio->md_label;
1933 /* Fall through to handler switch. */
1940 #ifdef COMPAT_FREEBSD32
1941 case MDIOCATTACH_32:
1943 error = kern_mdattach(td, &mdr);
1946 #ifdef COMPAT_FREEBSD32
1947 case MDIOCDETACH_32:
1949 error = kern_mddetach(td, &mdr);
1952 #ifdef COMPAT_FREEBSD32
1953 case MDIOCRESIZE_32:
1955 error = kern_mdresize(&mdr);
1958 #ifdef COMPAT_FREEBSD32
1961 error = kern_mdquery(&mdr);
1970 struct md_ioctl *mdio = (struct md_ioctl *)addr;
1971 MD_REQ2IOCTL(&mdr, mdio);
1974 #ifdef COMPAT_FREEBSD32
1975 case MDIOCATTACH_32:
1976 case MDIOCQUERY_32: {
1977 struct md_ioctl32 *mdio = (struct md_ioctl32 *)addr;
1978 MD_REQ2IOCTL(&mdr, mdio);
1983 /* Other commands to not alter mdr. */
1991 md_preloaded(u_char *image, size_t length, const char *name)
1996 sc = mdnew(-1, &error, MD_PRELOAD);
1999 sc->mediasize = length;
2000 sc->sectorsize = DEV_BSIZE;
2002 sc->pl_len = length;
2003 sc->start = mdstart_preload;
2005 strlcpy(sc->file, name, sizeof(sc->file));
2007 if (sc->unit == 0) {
2009 rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0";
2011 #ifdef MD_ROOT_READONLY
2012 sc->flags |= MD_READONLY;
2018 printf("%s%d: Preloaded image <%s> %zd bytes at %p\n",
2019 MD_NAME, sc->unit, name, length, image);
2021 printf("%s%d: Embedded image %zd bytes at %p\n",
2022 MD_NAME, sc->unit, length, image);
2027 g_md_init(struct g_class *mp __unused)
2030 u_char *ptr, *name, *type;
2034 /* figure out log2(NINDIR) */
2035 for (i = NINDIR, nshift = -1; i; nshift++)
2039 sx_init(&md_sx, "MD config lock");
2040 g_topology_unlock();
2041 md_uh = new_unrhdr(0, INT_MAX, NULL);
2043 if (mfs_root_size != 0) {
2046 md_preloaded(mfs_root, mfs_root_size, NULL);
2048 md_preloaded(__DEVOLATILE(u_char *, &mfs_root), mfs_root_size,
2054 /* XXX: are preload_* static or do they need Giant ? */
2055 while ((mod = preload_search_next_name(mod)) != NULL) {
2056 name = (char *)preload_search_info(mod, MODINFO_NAME);
2059 type = (char *)preload_search_info(mod, MODINFO_TYPE);
2062 if (strcmp(type, "md_image") && strcmp(type, "mfs_root"))
2064 ptr = preload_fetch_addr(mod);
2065 len = preload_fetch_size(mod);
2066 if (ptr != NULL && len != 0) {
2068 md_preloaded(ptr, len, name);
2072 md_pbuf_zone = pbuf_zsecond_create("mdpbuf", nswbuf / 10);
2073 status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL,
2079 g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2080 struct g_consumer *cp __unused, struct g_provider *pp)
2111 if (indent == NULL) {
2112 sbuf_printf(sb, " u %d", mp->unit);
2113 sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize);
2114 sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads);
2115 sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors);
2116 sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize);
2117 sbuf_printf(sb, " t %s", type);
2118 if ((mp->type == MD_VNODE && mp->vnode != NULL) ||
2119 (mp->type == MD_PRELOAD && mp->file[0] != '\0'))
2120 sbuf_printf(sb, " file %s", mp->file);
2121 sbuf_printf(sb, " label %s", mp->label);
2123 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent,
2125 sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n",
2126 indent, (uintmax_t) mp->sectorsize);
2127 sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n",
2128 indent, (uintmax_t) mp->fwheads);
2129 sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n",
2130 indent, (uintmax_t) mp->fwsectors);
2131 if (mp->ident[0] != '\0') {
2132 sbuf_printf(sb, "%s<ident>", indent);
2133 g_conf_printf_escaped(sb, "%s", mp->ident);
2134 sbuf_printf(sb, "</ident>\n");
2136 sbuf_printf(sb, "%s<length>%ju</length>\n",
2137 indent, (uintmax_t) mp->mediasize);
2138 sbuf_printf(sb, "%s<compression>%s</compression>\n", indent,
2139 (mp->flags & MD_COMPRESS) == 0 ? "off": "on");
2140 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
2141 (mp->flags & MD_READONLY) == 0 ? "read-write":
2143 sbuf_printf(sb, "%s<type>%s</type>\n", indent,
2145 if ((mp->type == MD_VNODE && mp->vnode != NULL) ||
2146 (mp->type == MD_PRELOAD && mp->file[0] != '\0')) {
2147 sbuf_printf(sb, "%s<file>", indent);
2148 g_conf_printf_escaped(sb, "%s", mp->file);
2149 sbuf_printf(sb, "</file>\n");
2151 if (mp->type == MD_VNODE)
2152 sbuf_printf(sb, "%s<cache>%s</cache>\n", indent,
2153 (mp->flags & MD_CACHE) == 0 ? "off": "on");
2154 sbuf_printf(sb, "%s<label>", indent);
2155 g_conf_printf_escaped(sb, "%s", mp->label);
2156 sbuf_printf(sb, "</label>\n");
2162 g_md_fini(struct g_class *mp __unused)
2166 if (status_dev != NULL)
2167 destroy_dev(status_dev);
2168 uma_zdestroy(md_pbuf_zone);
2169 delete_unrhdr(md_uh);