1098ca2bdSWarner Losh /*- 200a6a3c6SPoul-Henning Kamp * ---------------------------------------------------------------------------- 300a6a3c6SPoul-Henning Kamp * "THE BEER-WARE LICENSE" (Revision 42): 400a6a3c6SPoul-Henning Kamp * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 500a6a3c6SPoul-Henning Kamp * can do whatever you want with this stuff. If we meet some day, and you think 600a6a3c6SPoul-Henning Kamp * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 700a6a3c6SPoul-Henning Kamp * ---------------------------------------------------------------------------- 800a6a3c6SPoul-Henning Kamp * 900a6a3c6SPoul-Henning Kamp * $FreeBSD$ 1000a6a3c6SPoul-Henning Kamp * 1100a6a3c6SPoul-Henning Kamp */ 1200a6a3c6SPoul-Henning Kamp 13098ca2bdSWarner Losh /*- 14637f671aSPoul-Henning Kamp * The following functions are based in the vn(4) driver: mdstart_swap(), 15637f671aSPoul-Henning Kamp * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(), 16637f671aSPoul-Henning Kamp * and as such under the following copyright: 17637f671aSPoul-Henning Kamp * 18637f671aSPoul-Henning Kamp * Copyright (c) 1988 University of Utah. 19637f671aSPoul-Henning Kamp * Copyright (c) 1990, 1993 20637f671aSPoul-Henning Kamp * The Regents of the University of California. All rights reserved. 2159ec9023SKonstantin Belousov * Copyright (c) 2013 The FreeBSD Foundation 2259ec9023SKonstantin Belousov * All rights reserved. 23637f671aSPoul-Henning Kamp * 24ed010cdfSWarner Losh * This code is derived from software contributed to Berkeley by 25ed010cdfSWarner Losh * the Systems Programming Group of the University of Utah Computer 26ed010cdfSWarner Losh * Science Department. 27ed010cdfSWarner Losh * 2859ec9023SKonstantin Belousov * Portions of this software were developed by Konstantin Belousov 2959ec9023SKonstantin Belousov * under sponsorship from the FreeBSD Foundation. 3059ec9023SKonstantin Belousov * 31637f671aSPoul-Henning Kamp * Redistribution and use in source and binary forms, with or without 32637f671aSPoul-Henning Kamp * modification, are permitted provided that the following conditions 33637f671aSPoul-Henning Kamp * are met: 34637f671aSPoul-Henning Kamp * 1. Redistributions of source code must retain the above copyright 35637f671aSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer. 36637f671aSPoul-Henning Kamp * 2. Redistributions in binary form must reproduce the above copyright 37637f671aSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer in the 38637f671aSPoul-Henning Kamp * documentation and/or other materials provided with the distribution. 39637f671aSPoul-Henning Kamp * 4. Neither the name of the University nor the names of its contributors 40637f671aSPoul-Henning Kamp * may be used to endorse or promote products derived from this software 41637f671aSPoul-Henning Kamp * without specific prior written permission. 42637f671aSPoul-Henning Kamp * 43637f671aSPoul-Henning Kamp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 44637f671aSPoul-Henning Kamp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45637f671aSPoul-Henning Kamp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46637f671aSPoul-Henning Kamp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 47637f671aSPoul-Henning Kamp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48637f671aSPoul-Henning Kamp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49637f671aSPoul-Henning Kamp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50637f671aSPoul-Henning Kamp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51637f671aSPoul-Henning Kamp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52637f671aSPoul-Henning Kamp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53637f671aSPoul-Henning Kamp * SUCH DAMAGE. 54637f671aSPoul-Henning Kamp * 55637f671aSPoul-Henning Kamp * from: Utah Hdr: vn.c 1.13 94/04/02 56637f671aSPoul-Henning Kamp * 57637f671aSPoul-Henning Kamp * from: @(#)vn.c 8.6 (Berkeley) 4/1/94 58637f671aSPoul-Henning Kamp * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03 59637f671aSPoul-Henning Kamp */ 60637f671aSPoul-Henning Kamp 616f4f00f1SPoul-Henning Kamp #include "opt_geom.h" 623f54a085SPoul-Henning Kamp #include "opt_md.h" 6371e4fff8SPoul-Henning Kamp 6400a6a3c6SPoul-Henning Kamp #include <sys/param.h> 6500a6a3c6SPoul-Henning Kamp #include <sys/systm.h> 669626b608SPoul-Henning Kamp #include <sys/bio.h> 6759ec9023SKonstantin Belousov #include <sys/buf.h> 6800a6a3c6SPoul-Henning Kamp #include <sys/conf.h> 69a03be42dSMaxim Sobolev #include <sys/devicestat.h> 708f8def9eSPoul-Henning Kamp #include <sys/fcntl.h> 71fb919e4dSMark Murray #include <sys/kernel.h> 725c97ca54SIan Dowse #include <sys/kthread.h> 7306d425f9SEd Schouten #include <sys/limits.h> 74fb919e4dSMark Murray #include <sys/linker.h> 75fb919e4dSMark Murray #include <sys/lock.h> 76fb919e4dSMark Murray #include <sys/malloc.h> 77fb919e4dSMark Murray #include <sys/mdioctl.h> 78a08d2e7fSJohn Baldwin #include <sys/mount.h> 799dceb26bSJohn Baldwin #include <sys/mutex.h> 809b00ca19SPoul-Henning Kamp #include <sys/sx.h> 81fb919e4dSMark Murray #include <sys/namei.h> 828f8def9eSPoul-Henning Kamp #include <sys/proc.h> 83fb919e4dSMark Murray #include <sys/queue.h> 8489f6b863SAttilio Rao #include <sys/rwlock.h> 85657bd8b1SAndrey V. Elsukov #include <sys/sbuf.h> 8663710c4dSJohn Baldwin #include <sys/sched.h> 877cd53fddSAlan Cox #include <sys/sf_buf.h> 88fb919e4dSMark Murray #include <sys/sysctl.h> 89fb919e4dSMark Murray #include <sys/vnode.h> 90fb919e4dSMark Murray 916f4f00f1SPoul-Henning Kamp #include <geom/geom.h> 92ec170744SAndrey V. Elsukov #include <geom/geom_int.h> 936f4f00f1SPoul-Henning Kamp 948f8def9eSPoul-Henning Kamp #include <vm/vm.h> 951c771f92SKonstantin Belousov #include <vm/vm_param.h> 968f8def9eSPoul-Henning Kamp #include <vm/vm_object.h> 978f8def9eSPoul-Henning Kamp #include <vm/vm_page.h> 988f8def9eSPoul-Henning Kamp #include <vm/vm_pager.h> 998f8def9eSPoul-Henning Kamp #include <vm/swap_pager.h> 100f43b2bacSPoul-Henning Kamp #include <vm/uma.h> 1013f54a085SPoul-Henning Kamp 102a9934668SKenneth D. Merry #include <machine/bus.h> 103a9934668SKenneth D. Merry 10457e9624eSPoul-Henning Kamp #define MD_MODVER 1 10557e9624eSPoul-Henning Kamp 1065c97ca54SIan Dowse #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */ 107a08d2e7fSJohn Baldwin #define MD_EXITING 0x20000 /* Worker thread is exiting. */ 1085c97ca54SIan Dowse 109f2744793SSheldon Hearn #ifndef MD_NSECT 110f2744793SSheldon Hearn #define MD_NSECT (10000 * 2) 11133edfabeSPoul-Henning Kamp #endif 11233edfabeSPoul-Henning Kamp 1135bb84bc8SRobert Watson static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk"); 1145bb84bc8SRobert Watson static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors"); 11500a6a3c6SPoul-Henning Kamp 11671e4fff8SPoul-Henning Kamp static int md_debug; 1173eb9ab52SEitan Adler SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, 1183eb9ab52SEitan Adler "Enable md(4) debug messages"); 119c44d423eSKonstantin Belousov static int md_malloc_wait; 1203eb9ab52SEitan Adler SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0, 1213eb9ab52SEitan Adler "Allow malloc to wait for memory allocations"); 12200a6a3c6SPoul-Henning Kamp 12322ff74b2SMarcel Moolenaar #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE) 12422ff74b2SMarcel Moolenaar #define MD_ROOT_FSTYPE "ufs" 12522ff74b2SMarcel Moolenaar #endif 12622ff74b2SMarcel Moolenaar 127cc787e3dSMarcel Moolenaar #if defined(MD_ROOT) 128de64f22aSLuigi Rizzo /* 129de64f22aSLuigi Rizzo * Preloaded image gets put here. 130cc787e3dSMarcel Moolenaar */ 131cc787e3dSMarcel Moolenaar #if defined(MD_ROOT_SIZE) 132cc787e3dSMarcel Moolenaar /* 133de64f22aSLuigi Rizzo * Applications that patch the object with the image can determine 134de64f22aSLuigi Rizzo * the size looking at the start and end markers (strings), 135de64f22aSLuigi Rizzo * so we want them contiguous. 136de64f22aSLuigi Rizzo */ 137de64f22aSLuigi Rizzo static struct { 138de64f22aSLuigi Rizzo u_char start[MD_ROOT_SIZE*1024]; 139de64f22aSLuigi Rizzo u_char end[128]; 140de64f22aSLuigi Rizzo } mfs_root = { 141de64f22aSLuigi Rizzo .start = "MFS Filesystem goes here", 142de64f22aSLuigi Rizzo .end = "MFS Filesystem had better STOP here", 143de64f22aSLuigi Rizzo }; 144cc787e3dSMarcel Moolenaar const int mfs_root_size = sizeof(mfs_root.start); 145cc787e3dSMarcel Moolenaar #else 146cc787e3dSMarcel Moolenaar extern volatile u_char __weak_symbol mfs_root; 147cc787e3dSMarcel Moolenaar extern volatile u_char __weak_symbol mfs_root_end; 148cc787e3dSMarcel Moolenaar __GLOBL(mfs_root); 149cc787e3dSMarcel Moolenaar __GLOBL(mfs_root_end); 150cc787e3dSMarcel Moolenaar #define mfs_root_size ((uintptr_t)(&mfs_root_end - &mfs_root)) 151cc787e3dSMarcel Moolenaar #endif 15271e4fff8SPoul-Henning Kamp #endif 15371e4fff8SPoul-Henning Kamp 15419945697SPoul-Henning Kamp static g_init_t g_md_init; 15519945697SPoul-Henning Kamp static g_fini_t g_md_fini; 15619945697SPoul-Henning Kamp static g_start_t g_md_start; 15719945697SPoul-Henning Kamp static g_access_t g_md_access; 158b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent, 159b42f40b8SJaakko Heinonen struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp); 1600eb14309SPoul-Henning Kamp 16189c9c53dSPoul-Henning Kamp static struct cdev *status_dev = 0; 1629b00ca19SPoul-Henning Kamp static struct sx md_sx; 163f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh; 16457e9624eSPoul-Henning Kamp 165a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl; 1668f8def9eSPoul-Henning Kamp 1678f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = { 168dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 1697ac40f5fSPoul-Henning Kamp .d_ioctl = mdctlioctl, 1707ac40f5fSPoul-Henning Kamp .d_name = MD_NAME, 17100a6a3c6SPoul-Henning Kamp }; 17200a6a3c6SPoul-Henning Kamp 17319945697SPoul-Henning Kamp struct g_class g_md_class = { 17419945697SPoul-Henning Kamp .name = "MD", 1755721c9c7SPoul-Henning Kamp .version = G_VERSION, 17619945697SPoul-Henning Kamp .init = g_md_init, 17719945697SPoul-Henning Kamp .fini = g_md_fini, 17819945697SPoul-Henning Kamp .start = g_md_start, 17919945697SPoul-Henning Kamp .access = g_md_access, 180c27a8954SWojciech A. Koszek .dumpconf = g_md_dumpconf, 18119945697SPoul-Henning Kamp }; 18219945697SPoul-Henning Kamp 18319945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md); 18419945697SPoul-Henning Kamp 1850cfaeeeeSPoul-Henning Kamp 18613e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list); 1873f54a085SPoul-Henning Kamp 188c6517568SPoul-Henning Kamp #define NINDIR (PAGE_SIZE / sizeof(uintptr_t)) 189c6517568SPoul-Henning Kamp #define NMASK (NINDIR-1) 190c6517568SPoul-Henning Kamp static int nshift; 191c6517568SPoul-Henning Kamp 19259ec9023SKonstantin Belousov static int md_vnode_pbuf_freecnt; 19359ec9023SKonstantin Belousov 194c6517568SPoul-Henning Kamp struct indir { 195c6517568SPoul-Henning Kamp uintptr_t *array; 1968b149b51SJohn Baldwin u_int total; 1978b149b51SJohn Baldwin u_int used; 1988b149b51SJohn Baldwin u_int shift; 199c6517568SPoul-Henning Kamp }; 200c6517568SPoul-Henning Kamp 20100a6a3c6SPoul-Henning Kamp struct md_s { 20200a6a3c6SPoul-Henning Kamp int unit; 2033f54a085SPoul-Henning Kamp LIST_ENTRY(md_s) list; 2048177437dSPoul-Henning Kamp struct bio_queue_head bio_queue; 2050f8500a5SPoul-Henning Kamp struct mtx queue_mtx; 20640ea77a0SAlexander Motin struct mtx stat_mtx; 20789c9c53dSPoul-Henning Kamp struct cdev *dev; 2088f8def9eSPoul-Henning Kamp enum md_types type; 209b830359bSPawel Jakub Dawidek off_t mediasize; 210b830359bSPawel Jakub Dawidek unsigned sectorsize; 211fe603109SMaxim Sobolev unsigned opencount; 2124e8bfe14SPoul-Henning Kamp unsigned fwheads; 2134e8bfe14SPoul-Henning Kamp unsigned fwsectors; 2148f8def9eSPoul-Henning Kamp unsigned flags; 215f43b2bacSPoul-Henning Kamp char name[20]; 2165c97ca54SIan Dowse struct proc *procp; 2176f4f00f1SPoul-Henning Kamp struct g_geom *gp; 2186f4f00f1SPoul-Henning Kamp struct g_provider *pp; 2199b00ca19SPoul-Henning Kamp int (*start)(struct md_s *sc, struct bio *bp); 220a03be42dSMaxim Sobolev struct devstat *devstat; 22195f1a897SPoul-Henning Kamp 22295f1a897SPoul-Henning Kamp /* MD_MALLOC related fields */ 223c6517568SPoul-Henning Kamp struct indir *indir; 224f43b2bacSPoul-Henning Kamp uma_zone_t uma; 22500a6a3c6SPoul-Henning Kamp 22695f1a897SPoul-Henning Kamp /* MD_PRELOAD related fields */ 22795f1a897SPoul-Henning Kamp u_char *pl_ptr; 228b830359bSPawel Jakub Dawidek size_t pl_len; 22900a6a3c6SPoul-Henning Kamp 2308f8def9eSPoul-Henning Kamp /* MD_VNODE related fields */ 2318f8def9eSPoul-Henning Kamp struct vnode *vnode; 23261a6eb62SPawel Jakub Dawidek char file[PATH_MAX]; 2338f8def9eSPoul-Henning Kamp struct ucred *cred; 2348f8def9eSPoul-Henning Kamp 235e0cebb40SDima Dorfman /* MD_SWAP related fields */ 2368f8def9eSPoul-Henning Kamp vm_object_t object; 2378f8def9eSPoul-Henning Kamp }; 23800a6a3c6SPoul-Henning Kamp 239c6517568SPoul-Henning Kamp static struct indir * 2408b149b51SJohn Baldwin new_indir(u_int shift) 241c6517568SPoul-Henning Kamp { 242c6517568SPoul-Henning Kamp struct indir *ip; 243c6517568SPoul-Henning Kamp 244c44d423eSKonstantin Belousov ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT) 245c44d423eSKonstantin Belousov | M_ZERO); 246c6517568SPoul-Henning Kamp if (ip == NULL) 247c6517568SPoul-Henning Kamp return (NULL); 248c6517568SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 249c44d423eSKonstantin Belousov M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO); 250c6517568SPoul-Henning Kamp if (ip->array == NULL) { 251c6517568SPoul-Henning Kamp free(ip, M_MD); 252c6517568SPoul-Henning Kamp return (NULL); 253c6517568SPoul-Henning Kamp } 254c6517568SPoul-Henning Kamp ip->total = NINDIR; 255c6517568SPoul-Henning Kamp ip->shift = shift; 256c6517568SPoul-Henning Kamp return (ip); 257c6517568SPoul-Henning Kamp } 258c6517568SPoul-Henning Kamp 259c6517568SPoul-Henning Kamp static void 260c6517568SPoul-Henning Kamp del_indir(struct indir *ip) 261c6517568SPoul-Henning Kamp { 262c6517568SPoul-Henning Kamp 263f43b2bacSPoul-Henning Kamp free(ip->array, M_MDSECT); 264c6517568SPoul-Henning Kamp free(ip, M_MD); 265c6517568SPoul-Henning Kamp } 266c6517568SPoul-Henning Kamp 267f43b2bacSPoul-Henning Kamp static void 268f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip) 269f43b2bacSPoul-Henning Kamp { 270f43b2bacSPoul-Henning Kamp int i; 271f43b2bacSPoul-Henning Kamp 272f43b2bacSPoul-Henning Kamp for (i = 0; i < NINDIR; i++) { 273f43b2bacSPoul-Henning Kamp if (!ip->array[i]) 274f43b2bacSPoul-Henning Kamp continue; 275f43b2bacSPoul-Henning Kamp if (ip->shift) 276f43b2bacSPoul-Henning Kamp destroy_indir(sc, (struct indir*)(ip->array[i])); 277f43b2bacSPoul-Henning Kamp else if (ip->array[i] > 255) 278f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void *)(ip->array[i])); 279f43b2bacSPoul-Henning Kamp } 280f43b2bacSPoul-Henning Kamp del_indir(ip); 281f43b2bacSPoul-Henning Kamp } 282f43b2bacSPoul-Henning Kamp 283c6517568SPoul-Henning Kamp /* 2846c3cd0e2SMaxim Konovalov * This function does the math and allocates the top level "indir" structure 285c6517568SPoul-Henning Kamp * for a device of "size" sectors. 286c6517568SPoul-Henning Kamp */ 287c6517568SPoul-Henning Kamp 288c6517568SPoul-Henning Kamp static struct indir * 289c6517568SPoul-Henning Kamp dimension(off_t size) 290c6517568SPoul-Henning Kamp { 291c6517568SPoul-Henning Kamp off_t rcnt; 292c6517568SPoul-Henning Kamp struct indir *ip; 293d12fc952SKonstantin Belousov int layer; 294c6517568SPoul-Henning Kamp 295c6517568SPoul-Henning Kamp rcnt = size; 296c6517568SPoul-Henning Kamp layer = 0; 297c6517568SPoul-Henning Kamp while (rcnt > NINDIR) { 298c6517568SPoul-Henning Kamp rcnt /= NINDIR; 299c6517568SPoul-Henning Kamp layer++; 300c6517568SPoul-Henning Kamp } 301c6517568SPoul-Henning Kamp 302c6517568SPoul-Henning Kamp /* 303c6517568SPoul-Henning Kamp * XXX: the top layer is probably not fully populated, so we allocate 30483e13864SPoul-Henning Kamp * too much space for ip->array in here. 305c6517568SPoul-Henning Kamp */ 30683e13864SPoul-Henning Kamp ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO); 30783e13864SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 30883e13864SPoul-Henning Kamp M_MDSECT, M_WAITOK | M_ZERO); 30983e13864SPoul-Henning Kamp ip->total = NINDIR; 31083e13864SPoul-Henning Kamp ip->shift = layer * nshift; 311c6517568SPoul-Henning Kamp return (ip); 312c6517568SPoul-Henning Kamp } 313c6517568SPoul-Henning Kamp 314c6517568SPoul-Henning Kamp /* 315c6517568SPoul-Henning Kamp * Read a given sector 316c6517568SPoul-Henning Kamp */ 317c6517568SPoul-Henning Kamp 318c6517568SPoul-Henning Kamp static uintptr_t 319c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset) 320c6517568SPoul-Henning Kamp { 321c6517568SPoul-Henning Kamp struct indir *cip; 322c6517568SPoul-Henning Kamp int idx; 323c6517568SPoul-Henning Kamp uintptr_t up; 324c6517568SPoul-Henning Kamp 325c6517568SPoul-Henning Kamp if (md_debug > 1) 3266569d6f3SMaxime Henrion printf("s_read(%jd)\n", (intmax_t)offset); 327c6517568SPoul-Henning Kamp up = 0; 328c6517568SPoul-Henning Kamp for (cip = ip; cip != NULL;) { 329c6517568SPoul-Henning Kamp if (cip->shift) { 330c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 331c6517568SPoul-Henning Kamp up = cip->array[idx]; 332c6517568SPoul-Henning Kamp cip = (struct indir *)up; 333c6517568SPoul-Henning Kamp continue; 334c6517568SPoul-Henning Kamp } 335c6517568SPoul-Henning Kamp idx = offset & NMASK; 336c6517568SPoul-Henning Kamp return (cip->array[idx]); 337c6517568SPoul-Henning Kamp } 338c6517568SPoul-Henning Kamp return (0); 339c6517568SPoul-Henning Kamp } 340c6517568SPoul-Henning Kamp 341c6517568SPoul-Henning Kamp /* 342c6517568SPoul-Henning Kamp * Write a given sector, prune the tree if the value is 0 343c6517568SPoul-Henning Kamp */ 344c6517568SPoul-Henning Kamp 345c6517568SPoul-Henning Kamp static int 346fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr) 347c6517568SPoul-Henning Kamp { 348c6517568SPoul-Henning Kamp struct indir *cip, *lip[10]; 349c6517568SPoul-Henning Kamp int idx, li; 350c6517568SPoul-Henning Kamp uintptr_t up; 351c6517568SPoul-Henning Kamp 352c6517568SPoul-Henning Kamp if (md_debug > 1) 3536569d6f3SMaxime Henrion printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr); 354c6517568SPoul-Henning Kamp up = 0; 355c6517568SPoul-Henning Kamp li = 0; 356c6517568SPoul-Henning Kamp cip = ip; 357c6517568SPoul-Henning Kamp for (;;) { 358c6517568SPoul-Henning Kamp lip[li++] = cip; 359c6517568SPoul-Henning Kamp if (cip->shift) { 360c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 361c6517568SPoul-Henning Kamp up = cip->array[idx]; 362c6517568SPoul-Henning Kamp if (up != 0) { 363c6517568SPoul-Henning Kamp cip = (struct indir *)up; 364c6517568SPoul-Henning Kamp continue; 365c6517568SPoul-Henning Kamp } 366c6517568SPoul-Henning Kamp /* Allocate branch */ 367c6517568SPoul-Henning Kamp cip->array[idx] = 368c6517568SPoul-Henning Kamp (uintptr_t)new_indir(cip->shift - nshift); 369c6517568SPoul-Henning Kamp if (cip->array[idx] == 0) 370975b628fSPoul-Henning Kamp return (ENOSPC); 371c6517568SPoul-Henning Kamp cip->used++; 372c6517568SPoul-Henning Kamp up = cip->array[idx]; 373c6517568SPoul-Henning Kamp cip = (struct indir *)up; 374c6517568SPoul-Henning Kamp continue; 375c6517568SPoul-Henning Kamp } 376c6517568SPoul-Henning Kamp /* leafnode */ 377c6517568SPoul-Henning Kamp idx = offset & NMASK; 378c6517568SPoul-Henning Kamp up = cip->array[idx]; 379c6517568SPoul-Henning Kamp if (up != 0) 380c6517568SPoul-Henning Kamp cip->used--; 381c6517568SPoul-Henning Kamp cip->array[idx] = ptr; 382c6517568SPoul-Henning Kamp if (ptr != 0) 383c6517568SPoul-Henning Kamp cip->used++; 384c6517568SPoul-Henning Kamp break; 385c6517568SPoul-Henning Kamp } 386c6517568SPoul-Henning Kamp if (cip->used != 0 || li == 1) 387c6517568SPoul-Henning Kamp return (0); 388c6517568SPoul-Henning Kamp li--; 389c6517568SPoul-Henning Kamp while (cip->used == 0 && cip != ip) { 390c6517568SPoul-Henning Kamp li--; 391c6517568SPoul-Henning Kamp idx = (offset >> lip[li]->shift) & NMASK; 392c6517568SPoul-Henning Kamp up = lip[li]->array[idx]; 393c6517568SPoul-Henning Kamp KASSERT(up == (uintptr_t)cip, ("md screwed up")); 394c6517568SPoul-Henning Kamp del_indir(cip); 3954a6a94d8SArchie Cobbs lip[li]->array[idx] = 0; 396c6517568SPoul-Henning Kamp lip[li]->used--; 397c6517568SPoul-Henning Kamp cip = lip[li]; 398c6517568SPoul-Henning Kamp } 399c6517568SPoul-Henning Kamp return (0); 400c6517568SPoul-Henning Kamp } 401c6517568SPoul-Henning Kamp 4026f4f00f1SPoul-Henning Kamp 4036f4f00f1SPoul-Henning Kamp static int 4046f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e) 4056f4f00f1SPoul-Henning Kamp { 4066f4f00f1SPoul-Henning Kamp struct md_s *sc; 4076f4f00f1SPoul-Henning Kamp 4086f4f00f1SPoul-Henning Kamp sc = pp->geom->softc; 40941c8b468SEdward Tomasz Napierala if (sc == NULL) { 41041c8b468SEdward Tomasz Napierala if (r <= 0 && w <= 0 && e <= 0) 41141c8b468SEdward Tomasz Napierala return (0); 4126b60a2cdSPoul-Henning Kamp return (ENXIO); 41341c8b468SEdward Tomasz Napierala } 4146f4f00f1SPoul-Henning Kamp r += pp->acr; 4156f4f00f1SPoul-Henning Kamp w += pp->acw; 4166f4f00f1SPoul-Henning Kamp e += pp->ace; 41786776891SChristian S.J. Peron if ((sc->flags & MD_READONLY) != 0 && w > 0) 41886776891SChristian S.J. Peron return (EROFS); 4196f4f00f1SPoul-Henning Kamp if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 4206f4f00f1SPoul-Henning Kamp sc->opencount = 1; 4216f4f00f1SPoul-Henning Kamp } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 4226f4f00f1SPoul-Henning Kamp sc->opencount = 0; 4236f4f00f1SPoul-Henning Kamp } 4246f4f00f1SPoul-Henning Kamp return (0); 4256f4f00f1SPoul-Henning Kamp } 4266f4f00f1SPoul-Henning Kamp 4276f4f00f1SPoul-Henning Kamp static void 4286f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp) 4296f4f00f1SPoul-Henning Kamp { 4306f4f00f1SPoul-Henning Kamp struct md_s *sc; 4316f4f00f1SPoul-Henning Kamp 4326f4f00f1SPoul-Henning Kamp sc = bp->bio_to->geom->softc; 43340ea77a0SAlexander Motin if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) { 43440ea77a0SAlexander Motin mtx_lock(&sc->stat_mtx); 435a03be42dSMaxim Sobolev devstat_start_transaction_bio(sc->devstat, bp); 43640ea77a0SAlexander Motin mtx_unlock(&sc->stat_mtx); 43740ea77a0SAlexander Motin } 4380f8500a5SPoul-Henning Kamp mtx_lock(&sc->queue_mtx); 439891619a6SPoul-Henning Kamp bioq_disksort(&sc->bio_queue, bp); 4404b07ede4SPawel Jakub Dawidek mtx_unlock(&sc->queue_mtx); 441e4cdd0d4SPawel Jakub Dawidek wakeup(sc); 4426f4f00f1SPoul-Henning Kamp } 4436f4f00f1SPoul-Henning Kamp 44459ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_ZERO 1 44559ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_FILL 2 44659ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_READ 3 44759ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_WRITE 4 44859ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_CMP 5 44959ec9023SKonstantin Belousov 45059ec9023SKonstantin Belousov static int 451a9934668SKenneth D. Merry md_malloc_move_ma(vm_page_t **mp, int *ma_offs, unsigned sectorsize, 45259ec9023SKonstantin Belousov void *ptr, u_char fill, int op) 45359ec9023SKonstantin Belousov { 45459ec9023SKonstantin Belousov struct sf_buf *sf; 45559ec9023SKonstantin Belousov vm_page_t m, *mp1; 45659ec9023SKonstantin Belousov char *p, first; 45759ec9023SKonstantin Belousov off_t *uc; 45859ec9023SKonstantin Belousov unsigned n; 45959ec9023SKonstantin Belousov int error, i, ma_offs1, sz, first_read; 46059ec9023SKonstantin Belousov 46159ec9023SKonstantin Belousov m = NULL; 46259ec9023SKonstantin Belousov error = 0; 46359ec9023SKonstantin Belousov sf = NULL; 46459ec9023SKonstantin Belousov /* if (op == MD_MALLOC_MOVE_CMP) { gcc */ 46559ec9023SKonstantin Belousov first = 0; 46659ec9023SKonstantin Belousov first_read = 0; 46759ec9023SKonstantin Belousov uc = ptr; 46859ec9023SKonstantin Belousov mp1 = *mp; 46959ec9023SKonstantin Belousov ma_offs1 = *ma_offs; 47059ec9023SKonstantin Belousov /* } */ 47159ec9023SKonstantin Belousov sched_pin(); 47259ec9023SKonstantin Belousov for (n = sectorsize; n != 0; n -= sz) { 47359ec9023SKonstantin Belousov sz = imin(PAGE_SIZE - *ma_offs, n); 47459ec9023SKonstantin Belousov if (m != **mp) { 47559ec9023SKonstantin Belousov if (sf != NULL) 47659ec9023SKonstantin Belousov sf_buf_free(sf); 47759ec9023SKonstantin Belousov m = **mp; 47859ec9023SKonstantin Belousov sf = sf_buf_alloc(m, SFB_CPUPRIVATE | 47959ec9023SKonstantin Belousov (md_malloc_wait ? 0 : SFB_NOWAIT)); 48059ec9023SKonstantin Belousov if (sf == NULL) { 48159ec9023SKonstantin Belousov error = ENOMEM; 48259ec9023SKonstantin Belousov break; 48359ec9023SKonstantin Belousov } 48459ec9023SKonstantin Belousov } 48559ec9023SKonstantin Belousov p = (char *)sf_buf_kva(sf) + *ma_offs; 48659ec9023SKonstantin Belousov switch (op) { 48759ec9023SKonstantin Belousov case MD_MALLOC_MOVE_ZERO: 48859ec9023SKonstantin Belousov bzero(p, sz); 48959ec9023SKonstantin Belousov break; 49059ec9023SKonstantin Belousov case MD_MALLOC_MOVE_FILL: 49159ec9023SKonstantin Belousov memset(p, fill, sz); 49259ec9023SKonstantin Belousov break; 49359ec9023SKonstantin Belousov case MD_MALLOC_MOVE_READ: 49459ec9023SKonstantin Belousov bcopy(ptr, p, sz); 49559ec9023SKonstantin Belousov cpu_flush_dcache(p, sz); 49659ec9023SKonstantin Belousov break; 49759ec9023SKonstantin Belousov case MD_MALLOC_MOVE_WRITE: 49859ec9023SKonstantin Belousov bcopy(p, ptr, sz); 49959ec9023SKonstantin Belousov break; 50059ec9023SKonstantin Belousov case MD_MALLOC_MOVE_CMP: 50159ec9023SKonstantin Belousov for (i = 0; i < sz; i++, p++) { 50259ec9023SKonstantin Belousov if (!first_read) { 50359ec9023SKonstantin Belousov *uc = (u_char)*p; 50459ec9023SKonstantin Belousov first = *p; 50559ec9023SKonstantin Belousov first_read = 1; 50659ec9023SKonstantin Belousov } else if (*p != first) { 50759ec9023SKonstantin Belousov error = EDOOFUS; 50859ec9023SKonstantin Belousov break; 50959ec9023SKonstantin Belousov } 51059ec9023SKonstantin Belousov } 51159ec9023SKonstantin Belousov break; 51259ec9023SKonstantin Belousov default: 513a9934668SKenneth D. Merry KASSERT(0, ("md_malloc_move_ma unknown op %d\n", op)); 51459ec9023SKonstantin Belousov break; 51559ec9023SKonstantin Belousov } 51659ec9023SKonstantin Belousov if (error != 0) 51759ec9023SKonstantin Belousov break; 51859ec9023SKonstantin Belousov *ma_offs += sz; 51959ec9023SKonstantin Belousov *ma_offs %= PAGE_SIZE; 52059ec9023SKonstantin Belousov if (*ma_offs == 0) 52159ec9023SKonstantin Belousov (*mp)++; 52259ec9023SKonstantin Belousov ptr = (char *)ptr + sz; 52359ec9023SKonstantin Belousov } 52459ec9023SKonstantin Belousov 52559ec9023SKonstantin Belousov if (sf != NULL) 52659ec9023SKonstantin Belousov sf_buf_free(sf); 52759ec9023SKonstantin Belousov sched_unpin(); 52859ec9023SKonstantin Belousov if (op == MD_MALLOC_MOVE_CMP && error != 0) { 52959ec9023SKonstantin Belousov *mp = mp1; 53059ec9023SKonstantin Belousov *ma_offs = ma_offs1; 53159ec9023SKonstantin Belousov } 53259ec9023SKonstantin Belousov return (error); 53359ec9023SKonstantin Belousov } 53459ec9023SKonstantin Belousov 535b4a4f93cSPoul-Henning Kamp static int 536a9934668SKenneth D. Merry md_malloc_move_vlist(bus_dma_segment_t **pvlist, int *pma_offs, 537a9934668SKenneth D. Merry unsigned len, void *ptr, u_char fill, int op) 538a9934668SKenneth D. Merry { 539a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 540a9934668SKenneth D. Merry uint8_t *p, *end, first; 541a9934668SKenneth D. Merry off_t *uc; 542a9934668SKenneth D. Merry int ma_offs, seg_len; 543a9934668SKenneth D. Merry 544a9934668SKenneth D. Merry vlist = *pvlist; 545a9934668SKenneth D. Merry ma_offs = *pma_offs; 546a9934668SKenneth D. Merry uc = ptr; 547a9934668SKenneth D. Merry 548a9934668SKenneth D. Merry for (; len != 0; len -= seg_len) { 549a9934668SKenneth D. Merry seg_len = imin(vlist->ds_len - ma_offs, len); 550a9934668SKenneth D. Merry p = (uint8_t *)(uintptr_t)vlist->ds_addr + ma_offs; 551a9934668SKenneth D. Merry switch (op) { 552a9934668SKenneth D. Merry case MD_MALLOC_MOVE_ZERO: 553a9934668SKenneth D. Merry bzero(p, seg_len); 554a9934668SKenneth D. Merry break; 555a9934668SKenneth D. Merry case MD_MALLOC_MOVE_FILL: 556a9934668SKenneth D. Merry memset(p, fill, seg_len); 557a9934668SKenneth D. Merry break; 558a9934668SKenneth D. Merry case MD_MALLOC_MOVE_READ: 559a9934668SKenneth D. Merry bcopy(ptr, p, seg_len); 560a9934668SKenneth D. Merry cpu_flush_dcache(p, seg_len); 561a9934668SKenneth D. Merry break; 562a9934668SKenneth D. Merry case MD_MALLOC_MOVE_WRITE: 563a9934668SKenneth D. Merry bcopy(p, ptr, seg_len); 564a9934668SKenneth D. Merry break; 565a9934668SKenneth D. Merry case MD_MALLOC_MOVE_CMP: 566a9934668SKenneth D. Merry end = p + seg_len; 567a9934668SKenneth D. Merry first = *uc = *p; 568a9934668SKenneth D. Merry /* Confirm all following bytes match the first */ 569a9934668SKenneth D. Merry while (++p < end) { 570a9934668SKenneth D. Merry if (*p != first) 571a9934668SKenneth D. Merry return (EDOOFUS); 572a9934668SKenneth D. Merry } 573a9934668SKenneth D. Merry break; 574a9934668SKenneth D. Merry default: 575a9934668SKenneth D. Merry KASSERT(0, ("md_malloc_move_vlist unknown op %d\n", op)); 576a9934668SKenneth D. Merry break; 577a9934668SKenneth D. Merry } 578a9934668SKenneth D. Merry 579a9934668SKenneth D. Merry ma_offs += seg_len; 580a9934668SKenneth D. Merry if (ma_offs == vlist->ds_len) { 581a9934668SKenneth D. Merry ma_offs = 0; 582a9934668SKenneth D. Merry vlist++; 583a9934668SKenneth D. Merry } 584a9934668SKenneth D. Merry ptr = (uint8_t *)ptr + seg_len; 585a9934668SKenneth D. Merry } 586a9934668SKenneth D. Merry *pvlist = vlist; 587a9934668SKenneth D. Merry *pma_offs = ma_offs; 588a9934668SKenneth D. Merry 589a9934668SKenneth D. Merry return (0); 590a9934668SKenneth D. Merry } 591a9934668SKenneth D. Merry 592a9934668SKenneth D. Merry static int 593b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp) 59400a6a3c6SPoul-Henning Kamp { 595c6517568SPoul-Henning Kamp u_char *dst; 59659ec9023SKonstantin Belousov vm_page_t *m; 597a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 59859ec9023SKonstantin Belousov int i, error, error1, ma_offs, notmapped; 599b830359bSPawel Jakub Dawidek off_t secno, nsec, uc; 600c6517568SPoul-Henning Kamp uintptr_t sp, osp; 60100a6a3c6SPoul-Henning Kamp 6025541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 6035541f25eSPawel Jakub Dawidek case BIO_READ: 6045541f25eSPawel Jakub Dawidek case BIO_WRITE: 6055541f25eSPawel Jakub Dawidek case BIO_DELETE: 6065541f25eSPawel Jakub Dawidek break; 6075541f25eSPawel Jakub Dawidek default: 6085541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 6095541f25eSPawel Jakub Dawidek } 6105541f25eSPawel Jakub Dawidek 61159ec9023SKonstantin Belousov notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0; 612a9934668SKenneth D. Merry vlist = (bp->bio_flags & BIO_VLIST) != 0 ? 613a9934668SKenneth D. Merry (bus_dma_segment_t *)bp->bio_data : NULL; 61459ec9023SKonstantin Belousov if (notmapped) { 61559ec9023SKonstantin Belousov m = bp->bio_ma; 61659ec9023SKonstantin Belousov ma_offs = bp->bio_ma_offset; 61759ec9023SKonstantin Belousov dst = NULL; 618a9934668SKenneth D. Merry KASSERT(vlist == NULL, ("vlists cannot be unmapped")); 619a9934668SKenneth D. Merry } else if (vlist != NULL) { 620a9934668SKenneth D. Merry ma_offs = bp->bio_ma_offset; 621a9934668SKenneth D. Merry dst = NULL; 62259ec9023SKonstantin Belousov } else { 62359ec9023SKonstantin Belousov dst = bp->bio_data; 62459ec9023SKonstantin Belousov } 62559ec9023SKonstantin Belousov 626b830359bSPawel Jakub Dawidek nsec = bp->bio_length / sc->sectorsize; 627b830359bSPawel Jakub Dawidek secno = bp->bio_offset / sc->sectorsize; 628c6517568SPoul-Henning Kamp error = 0; 62900a6a3c6SPoul-Henning Kamp while (nsec--) { 630fde2a2e4SPoul-Henning Kamp osp = s_read(sc->indir, secno); 6318177437dSPoul-Henning Kamp if (bp->bio_cmd == BIO_DELETE) { 632fde2a2e4SPoul-Henning Kamp if (osp != 0) 633fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, 0); 6348177437dSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_READ) { 63559ec9023SKonstantin Belousov if (osp == 0) { 63659ec9023SKonstantin Belousov if (notmapped) { 637a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, &ma_offs, 63859ec9023SKonstantin Belousov sc->sectorsize, NULL, 0, 63959ec9023SKonstantin Belousov MD_MALLOC_MOVE_ZERO); 640a9934668SKenneth D. Merry } else if (vlist != NULL) { 641a9934668SKenneth D. Merry error = md_malloc_move_vlist(&vlist, 642a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, NULL, 0, 643a9934668SKenneth D. Merry MD_MALLOC_MOVE_ZERO); 64459ec9023SKonstantin Belousov } else 645b830359bSPawel Jakub Dawidek bzero(dst, sc->sectorsize); 64659ec9023SKonstantin Belousov } else if (osp <= 255) { 64759ec9023SKonstantin Belousov if (notmapped) { 648a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, &ma_offs, 64959ec9023SKonstantin Belousov sc->sectorsize, NULL, osp, 65059ec9023SKonstantin Belousov MD_MALLOC_MOVE_FILL); 651a9934668SKenneth D. Merry } else if (vlist != NULL) { 652a9934668SKenneth D. Merry error = md_malloc_move_vlist(&vlist, 653a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, NULL, osp, 654a9934668SKenneth D. Merry MD_MALLOC_MOVE_FILL); 65559ec9023SKonstantin Belousov } else 656dbb95048SMarcel Moolenaar memset(dst, osp, sc->sectorsize); 65759ec9023SKonstantin Belousov } else { 65859ec9023SKonstantin Belousov if (notmapped) { 659a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, &ma_offs, 66059ec9023SKonstantin Belousov sc->sectorsize, (void *)osp, 0, 66159ec9023SKonstantin Belousov MD_MALLOC_MOVE_READ); 662a9934668SKenneth D. Merry } else if (vlist != NULL) { 663a9934668SKenneth D. Merry error = md_malloc_move_vlist(&vlist, 664a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, 665a9934668SKenneth D. Merry (void *)osp, 0, 666a9934668SKenneth D. Merry MD_MALLOC_MOVE_READ); 66759ec9023SKonstantin Belousov } else { 668b830359bSPawel Jakub Dawidek bcopy((void *)osp, dst, sc->sectorsize); 669dbb95048SMarcel Moolenaar cpu_flush_dcache(dst, sc->sectorsize); 670dbb95048SMarcel Moolenaar } 67159ec9023SKonstantin Belousov } 672fde2a2e4SPoul-Henning Kamp osp = 0; 673c6517568SPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 6748f8def9eSPoul-Henning Kamp if (sc->flags & MD_COMPRESS) { 67559ec9023SKonstantin Belousov if (notmapped) { 676a9934668SKenneth D. Merry error1 = md_malloc_move_ma(&m, &ma_offs, 67759ec9023SKonstantin Belousov sc->sectorsize, &uc, 0, 67859ec9023SKonstantin Belousov MD_MALLOC_MOVE_CMP); 67959ec9023SKonstantin Belousov i = error1 == 0 ? sc->sectorsize : 0; 680a9934668SKenneth D. Merry } else if (vlist != NULL) { 681a9934668SKenneth D. Merry error1 = md_malloc_move_vlist(&vlist, 682a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, &uc, 0, 683a9934668SKenneth D. Merry MD_MALLOC_MOVE_CMP); 684a9934668SKenneth D. Merry i = error1 == 0 ? sc->sectorsize : 0; 68559ec9023SKonstantin Belousov } else { 68600a6a3c6SPoul-Henning Kamp uc = dst[0]; 68759ec9023SKonstantin Belousov for (i = 1; i < sc->sectorsize; i++) { 68800a6a3c6SPoul-Henning Kamp if (dst[i] != uc) 68900a6a3c6SPoul-Henning Kamp break; 69059ec9023SKonstantin Belousov } 69159ec9023SKonstantin Belousov } 6928f8def9eSPoul-Henning Kamp } else { 6938f8def9eSPoul-Henning Kamp i = 0; 6948f8def9eSPoul-Henning Kamp uc = 0; 6958f8def9eSPoul-Henning Kamp } 696b830359bSPawel Jakub Dawidek if (i == sc->sectorsize) { 697fde2a2e4SPoul-Henning Kamp if (osp != uc) 698fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, uc); 69900a6a3c6SPoul-Henning Kamp } else { 700fde2a2e4SPoul-Henning Kamp if (osp <= 255) { 701b830359bSPawel Jakub Dawidek sp = (uintptr_t)uma_zalloc(sc->uma, 702c44d423eSKonstantin Belousov md_malloc_wait ? M_WAITOK : 703b830359bSPawel Jakub Dawidek M_NOWAIT); 704c6517568SPoul-Henning Kamp if (sp == 0) { 705fde2a2e4SPoul-Henning Kamp error = ENOSPC; 706fde2a2e4SPoul-Henning Kamp break; 707fde2a2e4SPoul-Henning Kamp } 70859ec9023SKonstantin Belousov if (notmapped) { 709a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, 71059ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 71159ec9023SKonstantin Belousov (void *)sp, 0, 71259ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 713a9934668SKenneth D. Merry } else if (vlist != NULL) { 714a9934668SKenneth D. Merry error = md_malloc_move_vlist( 715a9934668SKenneth D. Merry &vlist, &ma_offs, 716a9934668SKenneth D. Merry sc->sectorsize, (void *)sp, 717a9934668SKenneth D. Merry 0, MD_MALLOC_MOVE_WRITE); 71859ec9023SKonstantin Belousov } else { 71959ec9023SKonstantin Belousov bcopy(dst, (void *)sp, 72059ec9023SKonstantin Belousov sc->sectorsize); 72159ec9023SKonstantin Belousov } 722fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, sp); 723c6517568SPoul-Henning Kamp } else { 72459ec9023SKonstantin Belousov if (notmapped) { 725a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, 72659ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 72759ec9023SKonstantin Belousov (void *)osp, 0, 72859ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 729a9934668SKenneth D. Merry } else if (vlist != NULL) { 730a9934668SKenneth D. Merry error = md_malloc_move_vlist( 731a9934668SKenneth D. Merry &vlist, &ma_offs, 732a9934668SKenneth D. Merry sc->sectorsize, (void *)osp, 733a9934668SKenneth D. Merry 0, MD_MALLOC_MOVE_WRITE); 73459ec9023SKonstantin Belousov } else { 73559ec9023SKonstantin Belousov bcopy(dst, (void *)osp, 73659ec9023SKonstantin Belousov sc->sectorsize); 73759ec9023SKonstantin Belousov } 738fde2a2e4SPoul-Henning Kamp osp = 0; 73900a6a3c6SPoul-Henning Kamp } 74000a6a3c6SPoul-Henning Kamp } 741c6517568SPoul-Henning Kamp } else { 742c6517568SPoul-Henning Kamp error = EOPNOTSUPP; 743c6517568SPoul-Henning Kamp } 744c6517568SPoul-Henning Kamp if (osp > 255) 745f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void*)osp); 746e3ed29a7SPawel Jakub Dawidek if (error != 0) 747c6517568SPoul-Henning Kamp break; 74800a6a3c6SPoul-Henning Kamp secno++; 749a9934668SKenneth D. Merry if (!notmapped && vlist == NULL) 750b830359bSPawel Jakub Dawidek dst += sc->sectorsize; 75100a6a3c6SPoul-Henning Kamp } 7528177437dSPoul-Henning Kamp bp->bio_resid = 0; 753c6517568SPoul-Henning Kamp return (error); 75400a6a3c6SPoul-Henning Kamp } 75500a6a3c6SPoul-Henning Kamp 756a9934668SKenneth D. Merry static void 757a9934668SKenneth D. Merry mdcopyto_vlist(void *src, bus_dma_segment_t *vlist, off_t offset, off_t len) 758a9934668SKenneth D. Merry { 759a9934668SKenneth D. Merry off_t seg_len; 760a9934668SKenneth D. Merry 761a9934668SKenneth D. Merry while (offset >= vlist->ds_len) { 762a9934668SKenneth D. Merry offset -= vlist->ds_len; 763a9934668SKenneth D. Merry vlist++; 764a9934668SKenneth D. Merry } 765a9934668SKenneth D. Merry 766a9934668SKenneth D. Merry while (len != 0) { 767a9934668SKenneth D. Merry seg_len = omin(len, vlist->ds_len - offset); 768a9934668SKenneth D. Merry bcopy(src, (void *)(uintptr_t)(vlist->ds_addr + offset), 769a9934668SKenneth D. Merry seg_len); 770a9934668SKenneth D. Merry offset = 0; 771a9934668SKenneth D. Merry src = (uint8_t *)src + seg_len; 772a9934668SKenneth D. Merry len -= seg_len; 773a9934668SKenneth D. Merry vlist++; 774a9934668SKenneth D. Merry } 775a9934668SKenneth D. Merry } 776a9934668SKenneth D. Merry 777a9934668SKenneth D. Merry static void 778a9934668SKenneth D. Merry mdcopyfrom_vlist(bus_dma_segment_t *vlist, off_t offset, void *dst, off_t len) 779a9934668SKenneth D. Merry { 780a9934668SKenneth D. Merry off_t seg_len; 781a9934668SKenneth D. Merry 782a9934668SKenneth D. Merry while (offset >= vlist->ds_len) { 783a9934668SKenneth D. Merry offset -= vlist->ds_len; 784a9934668SKenneth D. Merry vlist++; 785a9934668SKenneth D. Merry } 786a9934668SKenneth D. Merry 787a9934668SKenneth D. Merry while (len != 0) { 788a9934668SKenneth D. Merry seg_len = omin(len, vlist->ds_len - offset); 789a9934668SKenneth D. Merry bcopy((void *)(uintptr_t)(vlist->ds_addr + offset), dst, 790a9934668SKenneth D. Merry seg_len); 791a9934668SKenneth D. Merry offset = 0; 792a9934668SKenneth D. Merry dst = (uint8_t *)dst + seg_len; 793a9934668SKenneth D. Merry len -= seg_len; 794a9934668SKenneth D. Merry vlist++; 795a9934668SKenneth D. Merry } 796a9934668SKenneth D. Merry } 797a9934668SKenneth D. Merry 798b4a4f93cSPoul-Henning Kamp static int 799b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp) 80071e4fff8SPoul-Henning Kamp { 801a9934668SKenneth D. Merry uint8_t *p; 80271e4fff8SPoul-Henning Kamp 803a9934668SKenneth D. Merry p = sc->pl_ptr + bp->bio_offset; 804a8a58d03SPawel Jakub Dawidek switch (bp->bio_cmd) { 805a8a58d03SPawel Jakub Dawidek case BIO_READ: 806a9934668SKenneth D. Merry if ((bp->bio_flags & BIO_VLIST) != 0) { 807a9934668SKenneth D. Merry mdcopyto_vlist(p, (bus_dma_segment_t *)bp->bio_data, 808a9934668SKenneth D. Merry bp->bio_ma_offset, bp->bio_length); 809a9934668SKenneth D. Merry } else { 810a9934668SKenneth D. Merry bcopy(p, bp->bio_data, bp->bio_length); 811a9934668SKenneth D. Merry } 812dbb95048SMarcel Moolenaar cpu_flush_dcache(bp->bio_data, bp->bio_length); 813a8a58d03SPawel Jakub Dawidek break; 814a8a58d03SPawel Jakub Dawidek case BIO_WRITE: 815a9934668SKenneth D. Merry if ((bp->bio_flags & BIO_VLIST) != 0) { 816a9934668SKenneth D. Merry mdcopyfrom_vlist((bus_dma_segment_t *)bp->bio_data, 817a9934668SKenneth D. Merry bp->bio_ma_offset, p, bp->bio_length); 818a9934668SKenneth D. Merry } else { 819a9934668SKenneth D. Merry bcopy(bp->bio_data, p, bp->bio_length); 820a9934668SKenneth D. Merry } 821a8a58d03SPawel Jakub Dawidek break; 82271e4fff8SPoul-Henning Kamp } 8238177437dSPoul-Henning Kamp bp->bio_resid = 0; 824b4a4f93cSPoul-Henning Kamp return (0); 82571e4fff8SPoul-Henning Kamp } 82671e4fff8SPoul-Henning Kamp 827b4a4f93cSPoul-Henning Kamp static int 828b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp) 8298f8def9eSPoul-Henning Kamp { 8305050aa86SKonstantin Belousov int error; 8318f8def9eSPoul-Henning Kamp struct uio auio; 8328f8def9eSPoul-Henning Kamp struct iovec aiov; 833a9934668SKenneth D. Merry struct iovec *piov; 8348f8def9eSPoul-Henning Kamp struct mount *mp; 8355541f25eSPawel Jakub Dawidek struct vnode *vp; 83659ec9023SKonstantin Belousov struct buf *pb; 837a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 8385541f25eSPawel Jakub Dawidek struct thread *td; 839*d5f998baSKonstantin Belousov off_t iolen, len, zerosize; 840*d5f998baSKonstantin Belousov int ma_offs, npages; 8415541f25eSPawel Jakub Dawidek 8425541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 8435541f25eSPawel Jakub Dawidek case BIO_READ: 844a9934668SKenneth D. Merry auio.uio_rw = UIO_READ; 845a9934668SKenneth D. Merry break; 8465541f25eSPawel Jakub Dawidek case BIO_WRITE: 8470abd21bdSDag-Erling Smørgrav case BIO_DELETE: 848a9934668SKenneth D. Merry auio.uio_rw = UIO_WRITE; 849a9934668SKenneth D. Merry break; 8505541f25eSPawel Jakub Dawidek case BIO_FLUSH: 8515541f25eSPawel Jakub Dawidek break; 8525541f25eSPawel Jakub Dawidek default: 8535541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 8545541f25eSPawel Jakub Dawidek } 8555541f25eSPawel Jakub Dawidek 8565541f25eSPawel Jakub Dawidek td = curthread; 8575541f25eSPawel Jakub Dawidek vp = sc->vnode; 858a9934668SKenneth D. Merry pb = NULL; 859a9934668SKenneth D. Merry piov = NULL; 860a9934668SKenneth D. Merry ma_offs = bp->bio_ma_offset; 861*d5f998baSKonstantin Belousov len = bp->bio_length; 8628f8def9eSPoul-Henning Kamp 8638f8def9eSPoul-Henning Kamp /* 8648f8def9eSPoul-Henning Kamp * VNODE I/O 8658f8def9eSPoul-Henning Kamp * 8668f8def9eSPoul-Henning Kamp * If an error occurs, we set BIO_ERROR but we do not set 8678f8def9eSPoul-Henning Kamp * B_INVAL because (for a write anyway), the buffer is 8688f8def9eSPoul-Henning Kamp * still valid. 8698f8def9eSPoul-Henning Kamp */ 8708f8def9eSPoul-Henning Kamp 8715541f25eSPawel Jakub Dawidek if (bp->bio_cmd == BIO_FLUSH) { 8725541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 873cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 8745541f25eSPawel Jakub Dawidek error = VOP_FSYNC(vp, MNT_WAIT, td); 87522db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 8765541f25eSPawel Jakub Dawidek vn_finished_write(mp); 8775541f25eSPawel Jakub Dawidek return (error); 8785541f25eSPawel Jakub Dawidek } 8795541f25eSPawel Jakub Dawidek 880a9934668SKenneth D. Merry auio.uio_offset = (vm_ooffset_t)bp->bio_offset; 881a9934668SKenneth D. Merry auio.uio_resid = bp->bio_length; 882a9934668SKenneth D. Merry auio.uio_segflg = UIO_SYSSPACE; 883a9934668SKenneth D. Merry auio.uio_td = td; 8848f8def9eSPoul-Henning Kamp 8850abd21bdSDag-Erling Smørgrav if (bp->bio_cmd == BIO_DELETE) { 886a9934668SKenneth D. Merry /* 887a9934668SKenneth D. Merry * Emulate BIO_DELETE by writing zeros. 888a9934668SKenneth D. Merry */ 88989cb2a19SMatthew D Fleming zerosize = ZERO_REGION_SIZE - 89089cb2a19SMatthew D Fleming (ZERO_REGION_SIZE % sc->sectorsize); 891a9934668SKenneth D. Merry auio.uio_iovcnt = howmany(bp->bio_length, zerosize); 892a9934668SKenneth D. Merry piov = malloc(sizeof(*piov) * auio.uio_iovcnt, M_MD, M_WAITOK); 893a9934668SKenneth D. Merry auio.uio_iov = piov; 894a9934668SKenneth D. Merry while (len > 0) { 895a9934668SKenneth D. Merry piov->iov_base = __DECONST(void *, zero_region); 896a9934668SKenneth D. Merry piov->iov_len = len; 897a9934668SKenneth D. Merry if (len > zerosize) 898a9934668SKenneth D. Merry piov->iov_len = zerosize; 899a9934668SKenneth D. Merry len -= piov->iov_len; 900a9934668SKenneth D. Merry piov++; 9010abd21bdSDag-Erling Smørgrav } 902a9934668SKenneth D. Merry piov = auio.uio_iov; 903a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_VLIST) != 0) { 904a9934668SKenneth D. Merry piov = malloc(sizeof(*piov) * bp->bio_ma_n, M_MD, M_WAITOK); 905a9934668SKenneth D. Merry auio.uio_iov = piov; 906a9934668SKenneth D. Merry vlist = (bus_dma_segment_t *)bp->bio_data; 907a9934668SKenneth D. Merry while (len > 0) { 908a9934668SKenneth D. Merry piov->iov_base = (void *)(uintptr_t)(vlist->ds_addr + 909a9934668SKenneth D. Merry ma_offs); 910a9934668SKenneth D. Merry piov->iov_len = vlist->ds_len - ma_offs; 911a9934668SKenneth D. Merry if (piov->iov_len > len) 912a9934668SKenneth D. Merry piov->iov_len = len; 913a9934668SKenneth D. Merry len -= piov->iov_len; 914a9934668SKenneth D. Merry ma_offs = 0; 915a9934668SKenneth D. Merry vlist++; 916a9934668SKenneth D. Merry piov++; 9170abd21bdSDag-Erling Smørgrav } 918a9934668SKenneth D. Merry auio.uio_iovcnt = piov - auio.uio_iov; 919a9934668SKenneth D. Merry piov = auio.uio_iov; 920a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 92159ec9023SKonstantin Belousov pb = getpbuf(&md_vnode_pbuf_freecnt); 922*d5f998baSKonstantin Belousov bp->bio_resid = len; 923*d5f998baSKonstantin Belousov unmapped_step: 924*d5f998baSKonstantin Belousov npages = atop(min(MAXPHYS, round_page(len + (ma_offs & 925*d5f998baSKonstantin Belousov PAGE_MASK)))); 926*d5f998baSKonstantin Belousov iolen = min(ptoa(npages) - (ma_offs & PAGE_MASK), len); 927*d5f998baSKonstantin Belousov KASSERT(iolen > 0, ("zero iolen")); 928*d5f998baSKonstantin Belousov pmap_qenter((vm_offset_t)pb->b_data, 929*d5f998baSKonstantin Belousov &bp->bio_ma[atop(ma_offs)], npages); 930*d5f998baSKonstantin Belousov aiov.iov_base = (void *)((vm_offset_t)pb->b_data + 931*d5f998baSKonstantin Belousov (ma_offs & PAGE_MASK)); 932*d5f998baSKonstantin Belousov aiov.iov_len = iolen; 9338f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 9348f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 935*d5f998baSKonstantin Belousov auio.uio_resid = iolen; 936a9934668SKenneth D. Merry } else { 937a9934668SKenneth D. Merry aiov.iov_base = bp->bio_data; 938a9934668SKenneth D. Merry aiov.iov_len = bp->bio_length; 939a9934668SKenneth D. Merry auio.uio_iov = &aiov; 940a9934668SKenneth D. Merry auio.uio_iovcnt = 1; 941a9934668SKenneth D. Merry } 9427e76bb56SMatthew Dillon /* 9437e76bb56SMatthew Dillon * When reading set IO_DIRECT to try to avoid double-caching 94417a13919SPoul-Henning Kamp * the data. When writing IO_DIRECT is not optimal. 9457e76bb56SMatthew Dillon */ 946a9934668SKenneth D. Merry if (auio.uio_rw == UIO_READ) { 947cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 9485541f25eSPawel Jakub Dawidek error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred); 94922db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 9508f8def9eSPoul-Henning Kamp } else { 9515541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 952cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 9535541f25eSPawel Jakub Dawidek error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC, 9545541f25eSPawel Jakub Dawidek sc->cred); 95522db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 9568f8def9eSPoul-Henning Kamp vn_finished_write(mp); 9578f8def9eSPoul-Henning Kamp } 958a9934668SKenneth D. Merry 959*d5f998baSKonstantin Belousov if (pb != NULL) { 960*d5f998baSKonstantin Belousov pmap_qremove((vm_offset_t)pb->b_data, npages); 961*d5f998baSKonstantin Belousov if (error == 0) { 962*d5f998baSKonstantin Belousov len -= iolen; 963*d5f998baSKonstantin Belousov bp->bio_resid -= iolen; 964*d5f998baSKonstantin Belousov ma_offs += iolen; 965*d5f998baSKonstantin Belousov if (len > 0) 966*d5f998baSKonstantin Belousov goto unmapped_step; 967*d5f998baSKonstantin Belousov } 96859ec9023SKonstantin Belousov relpbuf(pb, &md_vnode_pbuf_freecnt); 96959ec9023SKonstantin Belousov } 970a9934668SKenneth D. Merry 971a9934668SKenneth D. Merry free(piov, M_MD); 972*d5f998baSKonstantin Belousov if (pb == NULL) 9738f8def9eSPoul-Henning Kamp bp->bio_resid = auio.uio_resid; 974b4a4f93cSPoul-Henning Kamp return (error); 9758f8def9eSPoul-Henning Kamp } 9768f8def9eSPoul-Henning Kamp 977b4a4f93cSPoul-Henning Kamp static int 978b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp) 9798f8def9eSPoul-Henning Kamp { 9808e28326aSPoul-Henning Kamp vm_page_t m; 9818e28326aSPoul-Henning Kamp u_char *p; 98259ec9023SKonstantin Belousov vm_pindex_t i, lastp; 983a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 98459ec9023SKonstantin Belousov int rv, ma_offs, offs, len, lastend; 9858f8def9eSPoul-Henning Kamp 9865541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 9875541f25eSPawel Jakub Dawidek case BIO_READ: 9885541f25eSPawel Jakub Dawidek case BIO_WRITE: 9895541f25eSPawel Jakub Dawidek case BIO_DELETE: 9905541f25eSPawel Jakub Dawidek break; 9915541f25eSPawel Jakub Dawidek default: 9925541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 9935541f25eSPawel Jakub Dawidek } 9945541f25eSPawel Jakub Dawidek 9958e28326aSPoul-Henning Kamp p = bp->bio_data; 996a9934668SKenneth D. Merry ma_offs = (bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0 ? 997a9934668SKenneth D. Merry bp->bio_ma_offset : 0; 998a9934668SKenneth D. Merry vlist = (bp->bio_flags & BIO_VLIST) != 0 ? 999a9934668SKenneth D. Merry (bus_dma_segment_t *)bp->bio_data : NULL; 1000e07113d6SColin Percival 1001e07113d6SColin Percival /* 10026c3cd0e2SMaxim Konovalov * offs is the offset at which to start operating on the 1003e07113d6SColin Percival * next (ie, first) page. lastp is the last page on 1004e07113d6SColin Percival * which we're going to operate. lastend is the ending 1005e07113d6SColin Percival * position within that last page (ie, PAGE_SIZE if 1006e07113d6SColin Percival * we're operating on complete aligned pages). 1007e07113d6SColin Percival */ 1008e07113d6SColin Percival offs = bp->bio_offset % PAGE_SIZE; 1009e07113d6SColin Percival lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE; 1010e07113d6SColin Percival lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1; 1011e07113d6SColin Percival 1012812851b6SBrian Feldman rv = VM_PAGER_OK; 101389f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 10148e28326aSPoul-Henning Kamp vm_object_pip_add(sc->object, 1); 1015e07113d6SColin Percival for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) { 1016e07113d6SColin Percival len = ((i == lastp) ? lastend : PAGE_SIZE) - offs; 10171a42d14aSKonstantin Belousov m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM); 10188e28326aSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 1019537cc627SKonstantin Belousov if (m->valid == VM_PAGE_BITS_ALL) 1020537cc627SKonstantin Belousov rv = VM_PAGER_OK; 1021537cc627SKonstantin Belousov else 102207be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 1023812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 1024c7aebda8SAttilio Rao vm_page_xunbusy(m); 1025812851b6SBrian Feldman break; 1026e9f581baSKonstantin Belousov } else if (rv == VM_PAGER_FAIL) { 1027e9f581baSKonstantin Belousov /* 1028e9f581baSKonstantin Belousov * Pager does not have the page. Zero 1029e9f581baSKonstantin Belousov * the allocated page, and mark it as 1030e9f581baSKonstantin Belousov * valid. Do not set dirty, the page 1031e9f581baSKonstantin Belousov * can be recreated if thrown out. 1032e9f581baSKonstantin Belousov */ 103359ec9023SKonstantin Belousov pmap_zero_page(m); 1034e9f581baSKonstantin Belousov m->valid = VM_PAGE_BITS_ALL; 1035812851b6SBrian Feldman } 103659ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 103759ec9023SKonstantin Belousov pmap_copy_pages(&m, offs, bp->bio_ma, 103859ec9023SKonstantin Belousov ma_offs, len); 1039a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_VLIST) != 0) { 1040a9934668SKenneth D. Merry physcopyout_vlist(VM_PAGE_TO_PHYS(m) + offs, 1041a9934668SKenneth D. Merry vlist, ma_offs, len); 1042a9934668SKenneth D. Merry cpu_flush_dcache(p, len); 104359ec9023SKonstantin Belousov } else { 104459ec9023SKonstantin Belousov physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len); 1045dbb95048SMarcel Moolenaar cpu_flush_dcache(p, len); 104659ec9023SKonstantin Belousov } 10478e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 104807be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 104907be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 1050537cc627SKonstantin Belousov else 1051537cc627SKonstantin Belousov rv = VM_PAGER_OK; 1052812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 1053c7aebda8SAttilio Rao vm_page_xunbusy(m); 1054812851b6SBrian Feldman break; 1055812851b6SBrian Feldman } 105659ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 105759ec9023SKonstantin Belousov pmap_copy_pages(bp->bio_ma, ma_offs, &m, 105859ec9023SKonstantin Belousov offs, len); 1059a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_VLIST) != 0) { 1060a9934668SKenneth D. Merry physcopyin_vlist(vlist, ma_offs, 1061a9934668SKenneth D. Merry VM_PAGE_TO_PHYS(m) + offs, len); 106259ec9023SKonstantin Belousov } else { 106359ec9023SKonstantin Belousov physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len); 106459ec9023SKonstantin Belousov } 10658e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 10668e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_DELETE) { 106707be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 106807be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 1069537cc627SKonstantin Belousov else 1070537cc627SKonstantin Belousov rv = VM_PAGER_OK; 1071812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 1072c7aebda8SAttilio Rao vm_page_xunbusy(m); 1073812851b6SBrian Feldman break; 1074812851b6SBrian Feldman } 10754a13a769SKonstantin Belousov if (len != PAGE_SIZE) { 107659ec9023SKonstantin Belousov pmap_zero_page_area(m, offs, len); 10774a13a769SKonstantin Belousov vm_page_clear_dirty(m, offs, len); 10788e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 10794a13a769SKonstantin Belousov } else 10804a13a769SKonstantin Belousov vm_pager_page_unswapped(m); 10818e28326aSPoul-Henning Kamp } 1082c7aebda8SAttilio Rao vm_page_xunbusy(m); 1083fc0c3802SKonstantin Belousov vm_page_lock(m); 10844a13a769SKonstantin Belousov if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE) 10854a13a769SKonstantin Belousov vm_page_free(m); 10864a13a769SKonstantin Belousov else 10878e28326aSPoul-Henning Kamp vm_page_activate(m); 1088ecd5dd95SAlan Cox vm_page_unlock(m); 10895d9b4508SKonstantin Belousov if (bp->bio_cmd == BIO_WRITE) { 10908e28326aSPoul-Henning Kamp vm_page_dirty(m); 10915d9b4508SKonstantin Belousov vm_pager_page_unswapped(m); 10925d9b4508SKonstantin Belousov } 1093e07113d6SColin Percival 1094e07113d6SColin Percival /* Actions on further pages start at offset 0 */ 1095e07113d6SColin Percival p += PAGE_SIZE - offs; 1096e07113d6SColin Percival offs = 0; 109759ec9023SKonstantin Belousov ma_offs += len; 10988e28326aSPoul-Henning Kamp } 10990d8243ccSAttilio Rao vm_object_pip_wakeup(sc->object); 110089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1101812851b6SBrian Feldman return (rv != VM_PAGER_ERROR ? 0 : ENOSPC); 11028e28326aSPoul-Henning Kamp } 11038f8def9eSPoul-Henning Kamp 11040efd9bfdSEdward Tomasz Napierala static int 11050efd9bfdSEdward Tomasz Napierala mdstart_null(struct md_s *sc, struct bio *bp) 11060efd9bfdSEdward Tomasz Napierala { 11070efd9bfdSEdward Tomasz Napierala 11080efd9bfdSEdward Tomasz Napierala switch (bp->bio_cmd) { 11090efd9bfdSEdward Tomasz Napierala case BIO_READ: 11100efd9bfdSEdward Tomasz Napierala bzero(bp->bio_data, bp->bio_length); 11110efd9bfdSEdward Tomasz Napierala cpu_flush_dcache(bp->bio_data, bp->bio_length); 11120efd9bfdSEdward Tomasz Napierala break; 11130efd9bfdSEdward Tomasz Napierala case BIO_WRITE: 11140efd9bfdSEdward Tomasz Napierala break; 11150efd9bfdSEdward Tomasz Napierala } 11160efd9bfdSEdward Tomasz Napierala bp->bio_resid = 0; 11170efd9bfdSEdward Tomasz Napierala return (0); 11180efd9bfdSEdward Tomasz Napierala } 11190efd9bfdSEdward Tomasz Napierala 11208f8def9eSPoul-Henning Kamp static void 11215c97ca54SIan Dowse md_kthread(void *arg) 11225c97ca54SIan Dowse { 11235c97ca54SIan Dowse struct md_s *sc; 11245c97ca54SIan Dowse struct bio *bp; 1125a08d2e7fSJohn Baldwin int error; 11265c97ca54SIan Dowse 11275c97ca54SIan Dowse sc = arg; 1128982d11f8SJeff Roberson thread_lock(curthread); 112963710c4dSJohn Baldwin sched_prio(curthread, PRIBIO); 1130982d11f8SJeff Roberson thread_unlock(curthread); 11313b7b5496SKonstantin Belousov if (sc->type == MD_VNODE) 11323b7b5496SKonstantin Belousov curthread->td_pflags |= TDP_NORUNNINGBUF; 11335c97ca54SIan Dowse 1134b4a4f93cSPoul-Henning Kamp for (;;) { 1135a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 11365c97ca54SIan Dowse if (sc->flags & MD_SHUTDOWN) { 1137a08d2e7fSJohn Baldwin sc->flags |= MD_EXITING; 1138a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 11393745c395SJulian Elischer kproc_exit(0); 11405c97ca54SIan Dowse } 11419b00ca19SPoul-Henning Kamp bp = bioq_takefirst(&sc->bio_queue); 11429b00ca19SPoul-Henning Kamp if (!bp) { 11430f8500a5SPoul-Henning Kamp msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0); 11445c97ca54SIan Dowse continue; 11455c97ca54SIan Dowse } 11460f8500a5SPoul-Henning Kamp mtx_unlock(&sc->queue_mtx); 11474e8bfe14SPoul-Henning Kamp if (bp->bio_cmd == BIO_GETATTR) { 1148d91e813cSKonstantin Belousov if ((sc->fwsectors && sc->fwheads && 11494e8bfe14SPoul-Henning Kamp (g_handleattr_int(bp, "GEOM::fwsectors", 11504e8bfe14SPoul-Henning Kamp sc->fwsectors) || 11514e8bfe14SPoul-Henning Kamp g_handleattr_int(bp, "GEOM::fwheads", 1152d91e813cSKonstantin Belousov sc->fwheads))) || 1153d91e813cSKonstantin Belousov g_handleattr_int(bp, "GEOM::candelete", 1)) 11544e8bfe14SPoul-Henning Kamp error = -1; 11554e8bfe14SPoul-Henning Kamp else 11564e8bfe14SPoul-Henning Kamp error = EOPNOTSUPP; 11574e8bfe14SPoul-Henning Kamp } else { 11589b00ca19SPoul-Henning Kamp error = sc->start(sc, bp); 11594e8bfe14SPoul-Henning Kamp } 1160b4a4f93cSPoul-Henning Kamp 11616f4f00f1SPoul-Henning Kamp if (error != -1) { 11626f4f00f1SPoul-Henning Kamp bp->bio_completed = bp->bio_length; 1163a03be42dSMaxim Sobolev if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) 1164a03be42dSMaxim Sobolev devstat_end_transaction_bio(sc->devstat, bp); 116596410b95SKonstantin Belousov g_io_deliver(bp, error); 1166b4a4f93cSPoul-Henning Kamp } 11678f8def9eSPoul-Henning Kamp } 116826d48b40SPoul-Henning Kamp } 11698f8def9eSPoul-Henning Kamp 11708f8def9eSPoul-Henning Kamp static struct md_s * 11718f8def9eSPoul-Henning Kamp mdfind(int unit) 11728f8def9eSPoul-Henning Kamp { 11738f8def9eSPoul-Henning Kamp struct md_s *sc; 11748f8def9eSPoul-Henning Kamp 11753f54a085SPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 11763f54a085SPoul-Henning Kamp if (sc->unit == unit) 11778f8def9eSPoul-Henning Kamp break; 11788f8def9eSPoul-Henning Kamp } 11798f8def9eSPoul-Henning Kamp return (sc); 11808f8def9eSPoul-Henning Kamp } 11818f8def9eSPoul-Henning Kamp 11828f8def9eSPoul-Henning Kamp static struct md_s * 1183947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type) 11848f8def9eSPoul-Henning Kamp { 1185f4e7c5a8SJaakko Heinonen struct md_s *sc; 1186f4e7c5a8SJaakko Heinonen int error; 11878f8def9eSPoul-Henning Kamp 11889b00ca19SPoul-Henning Kamp *errp = 0; 1189f4e7c5a8SJaakko Heinonen if (unit == -1) 1190f4e7c5a8SJaakko Heinonen unit = alloc_unr(md_uh); 1191f4e7c5a8SJaakko Heinonen else 1192f4e7c5a8SJaakko Heinonen unit = alloc_unr_specific(md_uh, unit); 1193f4e7c5a8SJaakko Heinonen 1194f4e7c5a8SJaakko Heinonen if (unit == -1) { 11957ee3c044SPawel Jakub Dawidek *errp = EBUSY; 11963f54a085SPoul-Henning Kamp return (NULL); 11973f54a085SPoul-Henning Kamp } 1198f4e7c5a8SJaakko Heinonen 11999b00ca19SPoul-Henning Kamp sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO); 1200947fc8deSPoul-Henning Kamp sc->type = type; 12019b00ca19SPoul-Henning Kamp bioq_init(&sc->bio_queue); 12029b00ca19SPoul-Henning Kamp mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF); 120340ea77a0SAlexander Motin mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF); 12043f54a085SPoul-Henning Kamp sc->unit = unit; 1205f43b2bacSPoul-Henning Kamp sprintf(sc->name, "md%d", unit); 12067ee3c044SPawel Jakub Dawidek LIST_INSERT_HEAD(&md_softc_list, sc, list); 12073745c395SJulian Elischer error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name); 12089b00ca19SPoul-Henning Kamp if (error == 0) 12099b00ca19SPoul-Henning Kamp return (sc); 12107ee3c044SPawel Jakub Dawidek LIST_REMOVE(sc, list); 121140ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 12127ee3c044SPawel Jakub Dawidek mtx_destroy(&sc->queue_mtx); 1213f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 12145c97ca54SIan Dowse free(sc, M_MD); 12157ee3c044SPawel Jakub Dawidek *errp = error; 12165c97ca54SIan Dowse return (NULL); 12175c97ca54SIan Dowse } 12188f8def9eSPoul-Henning Kamp 12198f8def9eSPoul-Henning Kamp static void 12208f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc) 12218f8def9eSPoul-Henning Kamp { 12226f4f00f1SPoul-Henning Kamp struct g_geom *gp; 12236f4f00f1SPoul-Henning Kamp struct g_provider *pp; 12246f4f00f1SPoul-Henning Kamp 12256f4f00f1SPoul-Henning Kamp g_topology_lock(); 12266f4f00f1SPoul-Henning Kamp gp = g_new_geomf(&g_md_class, "md%d", sc->unit); 12276f4f00f1SPoul-Henning Kamp gp->softc = sc; 12286f4f00f1SPoul-Henning Kamp pp = g_new_providerf(gp, "md%d", sc->unit); 122940ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 1230b830359bSPawel Jakub Dawidek pp->mediasize = sc->mediasize; 1231b830359bSPawel Jakub Dawidek pp->sectorsize = sc->sectorsize; 12321ef76554SKonstantin Belousov switch (sc->type) { 12331ef76554SKonstantin Belousov case MD_MALLOC: 12341ef76554SKonstantin Belousov case MD_VNODE: 12351ef76554SKonstantin Belousov case MD_SWAP: 123659ec9023SKonstantin Belousov pp->flags |= G_PF_ACCEPT_UNMAPPED; 12371ef76554SKonstantin Belousov break; 12381ef76554SKonstantin Belousov case MD_PRELOAD: 12390efd9bfdSEdward Tomasz Napierala case MD_NULL: 12401ef76554SKonstantin Belousov break; 12411ef76554SKonstantin Belousov } 12426f4f00f1SPoul-Henning Kamp sc->gp = gp; 12436f4f00f1SPoul-Henning Kamp sc->pp = pp; 12446f4f00f1SPoul-Henning Kamp g_error_provider(pp, 0); 12456f4f00f1SPoul-Henning Kamp g_topology_unlock(); 1246a03be42dSMaxim Sobolev sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize, 1247a03be42dSMaxim Sobolev DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 12486f4f00f1SPoul-Henning Kamp } 124971e4fff8SPoul-Henning Kamp 12508f8def9eSPoul-Henning Kamp static int 1251b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio) 125295f1a897SPoul-Henning Kamp { 1253c6517568SPoul-Henning Kamp uintptr_t sp; 1254c6517568SPoul-Henning Kamp int error; 1255b830359bSPawel Jakub Dawidek off_t u; 125695f1a897SPoul-Henning Kamp 1257c6517568SPoul-Henning Kamp error = 0; 12588f8def9eSPoul-Henning Kamp if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 12598f8def9eSPoul-Henning Kamp return (EINVAL); 1260b830359bSPawel Jakub Dawidek if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize)) 1261ebe789d6SPoul-Henning Kamp return (EINVAL); 12628f8def9eSPoul-Henning Kamp /* Compression doesn't make sense if we have reserved space */ 12638f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) 12648f8def9eSPoul-Henning Kamp mdio->md_options &= ~MD_COMPRESS; 12654e8bfe14SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 12664e8bfe14SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 12674e8bfe14SPoul-Henning Kamp if (mdio->md_fwheads != 0) 12684e8bfe14SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 126926a0ee75SDima Dorfman sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE); 1270b830359bSPawel Jakub Dawidek sc->indir = dimension(sc->mediasize / sc->sectorsize); 1271b830359bSPawel Jakub Dawidek sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL, 1272b830359bSPawel Jakub Dawidek 0x1ff, 0); 127396b6a55fSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1274b830359bSPawel Jakub Dawidek off_t nsectors; 1275b830359bSPawel Jakub Dawidek 1276b830359bSPawel Jakub Dawidek nsectors = sc->mediasize / sc->sectorsize; 1277b830359bSPawel Jakub Dawidek for (u = 0; u < nsectors; u++) { 1278007777f1SKonstantin Belousov sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ? 1279007777f1SKonstantin Belousov M_WAITOK : M_NOWAIT) | M_ZERO); 1280c6517568SPoul-Henning Kamp if (sp != 0) 1281fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, u, sp); 1282c6517568SPoul-Henning Kamp else 1283c6517568SPoul-Henning Kamp error = ENOMEM; 1284b830359bSPawel Jakub Dawidek if (error != 0) 1285c6517568SPoul-Henning Kamp break; 12868f8def9eSPoul-Henning Kamp } 1287c6517568SPoul-Henning Kamp } 1288c6517568SPoul-Henning Kamp return (error); 128900a6a3c6SPoul-Henning Kamp } 129000a6a3c6SPoul-Henning Kamp 12913f54a085SPoul-Henning Kamp 12928f8def9eSPoul-Henning Kamp static int 12938f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred) 12948f8def9eSPoul-Henning Kamp { 12958f8def9eSPoul-Henning Kamp char *tmpbuf; 12968f8def9eSPoul-Henning Kamp int error = 0; 12978f8def9eSPoul-Henning Kamp 12983f54a085SPoul-Henning Kamp /* 12998f8def9eSPoul-Henning Kamp * Set credits in our softc 13003f54a085SPoul-Henning Kamp */ 13018f8def9eSPoul-Henning Kamp 13028f8def9eSPoul-Henning Kamp if (sc->cred) 13038f8def9eSPoul-Henning Kamp crfree(sc->cred); 1304bd78ceceSJohn Baldwin sc->cred = crhold(cred); 13058f8def9eSPoul-Henning Kamp 13068f8def9eSPoul-Henning Kamp /* 13078f8def9eSPoul-Henning Kamp * Horrible kludge to establish credentials for NFS XXX. 13088f8def9eSPoul-Henning Kamp */ 13098f8def9eSPoul-Henning Kamp 13108f8def9eSPoul-Henning Kamp if (sc->vnode) { 13118f8def9eSPoul-Henning Kamp struct uio auio; 13128f8def9eSPoul-Henning Kamp struct iovec aiov; 13138f8def9eSPoul-Henning Kamp 1314b830359bSPawel Jakub Dawidek tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK); 13158f8def9eSPoul-Henning Kamp bzero(&auio, sizeof(auio)); 13168f8def9eSPoul-Henning Kamp 13178f8def9eSPoul-Henning Kamp aiov.iov_base = tmpbuf; 1318b830359bSPawel Jakub Dawidek aiov.iov_len = sc->sectorsize; 13198f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 13208f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 13218f8def9eSPoul-Henning Kamp auio.uio_offset = 0; 13228f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_READ; 13238f8def9eSPoul-Henning Kamp auio.uio_segflg = UIO_SYSSPACE; 13248f8def9eSPoul-Henning Kamp auio.uio_resid = aiov.iov_len; 1325cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 13268f8def9eSPoul-Henning Kamp error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 132722db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 13288f8def9eSPoul-Henning Kamp free(tmpbuf, M_TEMP); 13298f8def9eSPoul-Henning Kamp } 13308f8def9eSPoul-Henning Kamp return (error); 13318f8def9eSPoul-Henning Kamp } 13328f8def9eSPoul-Henning Kamp 13338f8def9eSPoul-Henning Kamp static int 1334b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 13358f8def9eSPoul-Henning Kamp { 13368f8def9eSPoul-Henning Kamp struct vattr vattr; 13378f8def9eSPoul-Henning Kamp struct nameidata nd; 13383d5c947dSMarcel Moolenaar char *fname; 13395050aa86SKonstantin Belousov int error, flags; 13408f8def9eSPoul-Henning Kamp 13413d5c947dSMarcel Moolenaar /* 13423d5c947dSMarcel Moolenaar * Kernel-originated requests must have the filename appended 13433d5c947dSMarcel Moolenaar * to the mdio structure to protect against malicious software. 13443d5c947dSMarcel Moolenaar */ 13453d5c947dSMarcel Moolenaar fname = mdio->md_file; 13463d5c947dSMarcel Moolenaar if ((void *)fname != (void *)(mdio + 1)) { 13473d5c947dSMarcel Moolenaar error = copyinstr(fname, sc->file, sizeof(sc->file), NULL); 134888b5b78dSPawel Jakub Dawidek if (error != 0) 134988b5b78dSPawel Jakub Dawidek return (error); 13503d5c947dSMarcel Moolenaar } else 13513d5c947dSMarcel Moolenaar strlcpy(sc->file, fname, sizeof(sc->file)); 13523d5c947dSMarcel Moolenaar 135386776891SChristian S.J. Peron /* 13543d5c947dSMarcel Moolenaar * If the user specified that this is a read only device, don't 13553d5c947dSMarcel Moolenaar * set the FWRITE mask before trying to open the backing store. 135686776891SChristian S.J. Peron */ 13573d5c947dSMarcel Moolenaar flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE); 13585050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td); 13599e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 1360e3ed29a7SPawel Jakub Dawidek if (error != 0) 136152c6716fSPawel Jakub Dawidek return (error); 1362b322d85dSPawel Jakub Dawidek NDFREE(&nd, NDF_ONLY_PNBUF); 136333fc3625SJohn Baldwin if (nd.ni_vp->v_type != VREG) { 136433fc3625SJohn Baldwin error = EINVAL; 136533fc3625SJohn Baldwin goto bad; 136633fc3625SJohn Baldwin } 136733fc3625SJohn Baldwin error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred); 136833fc3625SJohn Baldwin if (error != 0) 136933fc3625SJohn Baldwin goto bad; 137033fc3625SJohn Baldwin if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) { 137133fc3625SJohn Baldwin vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY); 137233fc3625SJohn Baldwin if (nd.ni_vp->v_iflag & VI_DOOMED) { 137333fc3625SJohn Baldwin /* Forced unmount. */ 137433fc3625SJohn Baldwin error = EBADF; 137533fc3625SJohn Baldwin goto bad; 137633fc3625SJohn Baldwin } 13778f8def9eSPoul-Henning Kamp } 13783b7b5496SKonstantin Belousov nd.ni_vp->v_vflag |= VV_MD; 137922db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 13809589c256SPoul-Henning Kamp 1381d5a929dcSPoul-Henning Kamp if (mdio->md_fwsectors != 0) 1382d5a929dcSPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 1383d5a929dcSPoul-Henning Kamp if (mdio->md_fwheads != 0) 1384d5a929dcSPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 13857a6b2b64SPoul-Henning Kamp sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC); 13869589c256SPoul-Henning Kamp if (!(flags & FWRITE)) 13879589c256SPoul-Henning Kamp sc->flags |= MD_READONLY; 13888f8def9eSPoul-Henning Kamp sc->vnode = nd.ni_vp; 13898f8def9eSPoul-Henning Kamp 1390a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 1391b830359bSPawel Jakub Dawidek if (error != 0) { 13923cf74e53SPhilip Paeps sc->vnode = NULL; 1393cb05b60aSAttilio Rao vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY); 13943b7b5496SKonstantin Belousov nd.ni_vp->v_vflag &= ~VV_MD; 139533fc3625SJohn Baldwin goto bad; 139633fc3625SJohn Baldwin } 139733fc3625SJohn Baldwin return (0); 139833fc3625SJohn Baldwin bad: 139922db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 140052c6716fSPawel Jakub Dawidek (void)vn_close(nd.ni_vp, flags, td->td_ucred, td); 14018f8def9eSPoul-Henning Kamp return (error); 14028f8def9eSPoul-Henning Kamp } 14038f8def9eSPoul-Henning Kamp 14048f8def9eSPoul-Henning Kamp static int 1405b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td) 14068f8def9eSPoul-Henning Kamp { 14070cddd8f0SMatthew Dillon 14086f4f00f1SPoul-Henning Kamp if (sc->gp) { 14096f4f00f1SPoul-Henning Kamp sc->gp->softc = NULL; 14109b00ca19SPoul-Henning Kamp g_topology_lock(); 14119b00ca19SPoul-Henning Kamp g_wither_geom(sc->gp, ENXIO); 14129b00ca19SPoul-Henning Kamp g_topology_unlock(); 14136b60a2cdSPoul-Henning Kamp sc->gp = NULL; 14146b60a2cdSPoul-Henning Kamp sc->pp = NULL; 14151f4ee1aaSPoul-Henning Kamp } 1416a03be42dSMaxim Sobolev if (sc->devstat) { 1417a03be42dSMaxim Sobolev devstat_remove_entry(sc->devstat); 1418a03be42dSMaxim Sobolev sc->devstat = NULL; 1419a03be42dSMaxim Sobolev } 1420a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 14215c97ca54SIan Dowse sc->flags |= MD_SHUTDOWN; 14225c97ca54SIan Dowse wakeup(sc); 1423a08d2e7fSJohn Baldwin while (!(sc->flags & MD_EXITING)) 1424a08d2e7fSJohn Baldwin msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10); 1425a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 142640ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 14279fbea3e3SPoul-Henning Kamp mtx_destroy(&sc->queue_mtx); 14289b00ca19SPoul-Henning Kamp if (sc->vnode != NULL) { 1429cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 14303b7b5496SKonstantin Belousov sc->vnode->v_vflag &= ~VV_MD; 143122db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 14329d4b5945SMaxim Sobolev (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? 1433b40ce416SJulian Elischer FREAD : (FREAD|FWRITE), sc->cred, td); 14349b00ca19SPoul-Henning Kamp } 14358f8def9eSPoul-Henning Kamp if (sc->cred != NULL) 14368f8def9eSPoul-Henning Kamp crfree(sc->cred); 14371db17c6dSPawel Jakub Dawidek if (sc->object != NULL) 1438f820bc50SAlan Cox vm_object_deallocate(sc->object); 1439f43b2bacSPoul-Henning Kamp if (sc->indir) 1440f43b2bacSPoul-Henning Kamp destroy_indir(sc, sc->indir); 1441f43b2bacSPoul-Henning Kamp if (sc->uma) 1442f43b2bacSPoul-Henning Kamp uma_zdestroy(sc->uma); 14431f4ee1aaSPoul-Henning Kamp 14441f4ee1aaSPoul-Henning Kamp LIST_REMOVE(sc, list); 1445f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 1446c6517568SPoul-Henning Kamp free(sc, M_MD); 14478f8def9eSPoul-Henning Kamp return (0); 14488f8def9eSPoul-Henning Kamp } 14498f8def9eSPoul-Henning Kamp 14508f8def9eSPoul-Henning Kamp static int 1451dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio) 1452dc604f0cSEdward Tomasz Napierala { 1453dc604f0cSEdward Tomasz Napierala int error, res; 1454dc604f0cSEdward Tomasz Napierala vm_pindex_t oldpages, newpages; 1455dc604f0cSEdward Tomasz Napierala 1456dc604f0cSEdward Tomasz Napierala switch (sc->type) { 1457dc604f0cSEdward Tomasz Napierala case MD_VNODE: 14580efd9bfdSEdward Tomasz Napierala case MD_NULL: 1459dc604f0cSEdward Tomasz Napierala break; 1460dc604f0cSEdward Tomasz Napierala case MD_SWAP: 14618cb51643SJaakko Heinonen if (mdio->md_mediasize <= 0 || 1462dc604f0cSEdward Tomasz Napierala (mdio->md_mediasize % PAGE_SIZE) != 0) 1463dc604f0cSEdward Tomasz Napierala return (EDOM); 1464dc604f0cSEdward Tomasz Napierala oldpages = OFF_TO_IDX(round_page(sc->mediasize)); 1465dc604f0cSEdward Tomasz Napierala newpages = OFF_TO_IDX(round_page(mdio->md_mediasize)); 1466dc604f0cSEdward Tomasz Napierala if (newpages < oldpages) { 146789f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1468dc604f0cSEdward Tomasz Napierala vm_object_page_remove(sc->object, newpages, 0, 0); 1469dc604f0cSEdward Tomasz Napierala swap_pager_freespace(sc->object, newpages, 1470dc604f0cSEdward Tomasz Napierala oldpages - newpages); 1471dc604f0cSEdward Tomasz Napierala swap_release_by_cred(IDX_TO_OFF(oldpages - 1472dc604f0cSEdward Tomasz Napierala newpages), sc->cred); 1473dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1474dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 147589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1476dc604f0cSEdward Tomasz Napierala } else if (newpages > oldpages) { 1477dc604f0cSEdward Tomasz Napierala res = swap_reserve_by_cred(IDX_TO_OFF(newpages - 1478dc604f0cSEdward Tomasz Napierala oldpages), sc->cred); 1479dc604f0cSEdward Tomasz Napierala if (!res) 1480dc604f0cSEdward Tomasz Napierala return (ENOMEM); 1481dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & MD_RESERVE) || 1482dc604f0cSEdward Tomasz Napierala (sc->flags & MD_RESERVE)) { 1483dc604f0cSEdward Tomasz Napierala error = swap_pager_reserve(sc->object, 1484dc604f0cSEdward Tomasz Napierala oldpages, newpages - oldpages); 1485dc604f0cSEdward Tomasz Napierala if (error < 0) { 1486dc604f0cSEdward Tomasz Napierala swap_release_by_cred( 1487dc604f0cSEdward Tomasz Napierala IDX_TO_OFF(newpages - oldpages), 1488dc604f0cSEdward Tomasz Napierala sc->cred); 1489dc604f0cSEdward Tomasz Napierala return (EDOM); 1490dc604f0cSEdward Tomasz Napierala } 1491dc604f0cSEdward Tomasz Napierala } 149289f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1493dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1494dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 149589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1496dc604f0cSEdward Tomasz Napierala } 1497dc604f0cSEdward Tomasz Napierala break; 1498dc604f0cSEdward Tomasz Napierala default: 1499dc604f0cSEdward Tomasz Napierala return (EOPNOTSUPP); 1500dc604f0cSEdward Tomasz Napierala } 1501dc604f0cSEdward Tomasz Napierala 1502dc604f0cSEdward Tomasz Napierala sc->mediasize = mdio->md_mediasize; 1503dc604f0cSEdward Tomasz Napierala g_topology_lock(); 1504dc604f0cSEdward Tomasz Napierala g_resize_provider(sc->pp, sc->mediasize); 1505dc604f0cSEdward Tomasz Napierala g_topology_unlock(); 1506dc604f0cSEdward Tomasz Napierala return (0); 1507dc604f0cSEdward Tomasz Napierala } 1508dc604f0cSEdward Tomasz Napierala 1509dc604f0cSEdward Tomasz Napierala static int 1510b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 15118f8def9eSPoul-Henning Kamp { 1512fcd57fbeSPawel Jakub Dawidek vm_ooffset_t npage; 1513fcd57fbeSPawel Jakub Dawidek int error; 15148f8def9eSPoul-Henning Kamp 15158f8def9eSPoul-Henning Kamp /* 1516bc2308d4SEdward Tomasz Napierala * Range check. Disallow negative sizes and sizes not being 1517bc2308d4SEdward Tomasz Napierala * multiple of page size. 15188f8def9eSPoul-Henning Kamp */ 15198cb51643SJaakko Heinonen if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 15208f8def9eSPoul-Henning Kamp return (EDOM); 15218f8def9eSPoul-Henning Kamp 15228f8def9eSPoul-Henning Kamp /* 15238f8def9eSPoul-Henning Kamp * Allocate an OBJT_SWAP object. 15248f8def9eSPoul-Henning Kamp * 15258f8def9eSPoul-Henning Kamp * Note the truncation. 15268f8def9eSPoul-Henning Kamp */ 15278f8def9eSPoul-Henning Kamp 1528b830359bSPawel Jakub Dawidek npage = mdio->md_mediasize / PAGE_SIZE; 15299ed40643SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 15309ed40643SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 15319ed40643SPoul-Henning Kamp if (mdio->md_fwheads != 0) 15329ed40643SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 1533fcd57fbeSPawel Jakub Dawidek sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage, 15343364c323SKonstantin Belousov VM_PROT_DEFAULT, 0, td->td_ucred); 1535812851b6SBrian Feldman if (sc->object == NULL) 1536812851b6SBrian Feldman return (ENOMEM); 1537dc604f0cSEdward Tomasz Napierala sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE); 15388f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1539fcd57fbeSPawel Jakub Dawidek if (swap_pager_reserve(sc->object, 0, npage) < 0) { 15403364c323SKonstantin Belousov error = EDOM; 15413364c323SKonstantin Belousov goto finish; 15428f8def9eSPoul-Henning Kamp } 15438f8def9eSPoul-Henning Kamp } 1544a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 15453364c323SKonstantin Belousov finish: 1546e3ed29a7SPawel Jakub Dawidek if (error != 0) { 15472eafd8b1SPawel Jakub Dawidek vm_object_deallocate(sc->object); 15482eafd8b1SPawel Jakub Dawidek sc->object = NULL; 15498f8def9eSPoul-Henning Kamp } 1550b830359bSPawel Jakub Dawidek return (error); 1551b3b3d1b7SPoul-Henning Kamp } 15528f8def9eSPoul-Henning Kamp 15530efd9bfdSEdward Tomasz Napierala static int 15540efd9bfdSEdward Tomasz Napierala mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 15550efd9bfdSEdward Tomasz Napierala { 15560efd9bfdSEdward Tomasz Napierala 15570efd9bfdSEdward Tomasz Napierala /* 1558bc2308d4SEdward Tomasz Napierala * Range check. Disallow negative sizes and sizes not being 1559bc2308d4SEdward Tomasz Napierala * multiple of page size. 15600efd9bfdSEdward Tomasz Napierala */ 15610efd9bfdSEdward Tomasz Napierala if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 15620efd9bfdSEdward Tomasz Napierala return (EDOM); 15630efd9bfdSEdward Tomasz Napierala 15640efd9bfdSEdward Tomasz Napierala return (0); 15650efd9bfdSEdward Tomasz Napierala } 15669d4b5945SMaxim Sobolev 15679d4b5945SMaxim Sobolev static int 15689b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 15698f8def9eSPoul-Henning Kamp { 15708f8def9eSPoul-Henning Kamp struct md_ioctl *mdio; 15718f8def9eSPoul-Henning Kamp struct md_s *sc; 1572b830359bSPawel Jakub Dawidek int error, i; 15738cb51643SJaakko Heinonen unsigned sectsize; 15748f8def9eSPoul-Henning Kamp 15758f8def9eSPoul-Henning Kamp if (md_debug) 15768f8def9eSPoul-Henning Kamp printf("mdctlioctl(%s %lx %p %x %p)\n", 1577b40ce416SJulian Elischer devtoname(dev), cmd, addr, flags, td); 15788f8def9eSPoul-Henning Kamp 15799b00ca19SPoul-Henning Kamp mdio = (struct md_ioctl *)addr; 15809b00ca19SPoul-Henning Kamp if (mdio->md_version != MDIOVERSION) 15819b00ca19SPoul-Henning Kamp return (EINVAL); 15829b00ca19SPoul-Henning Kamp 158353d745bcSDima Dorfman /* 158453d745bcSDima Dorfman * We assert the version number in the individual ioctl 158553d745bcSDima Dorfman * handlers instead of out here because (a) it is possible we 158653d745bcSDima Dorfman * may add another ioctl in the future which doesn't read an 158753d745bcSDima Dorfman * mdio, and (b) the correct return value for an unknown ioctl 158853d745bcSDima Dorfman * is ENOIOCTL, not EINVAL. 158953d745bcSDima Dorfman */ 15909b00ca19SPoul-Henning Kamp error = 0; 15918f8def9eSPoul-Henning Kamp switch (cmd) { 15928f8def9eSPoul-Henning Kamp case MDIOCATTACH: 15938f8def9eSPoul-Henning Kamp switch (mdio->md_type) { 15948f8def9eSPoul-Henning Kamp case MD_MALLOC: 15958f8def9eSPoul-Henning Kamp case MD_PRELOAD: 15968f8def9eSPoul-Henning Kamp case MD_VNODE: 15978f8def9eSPoul-Henning Kamp case MD_SWAP: 15980efd9bfdSEdward Tomasz Napierala case MD_NULL: 1599b830359bSPawel Jakub Dawidek break; 16008f8def9eSPoul-Henning Kamp default: 16018f8def9eSPoul-Henning Kamp return (EINVAL); 16028f8def9eSPoul-Henning Kamp } 16038cb51643SJaakko Heinonen if (mdio->md_sectorsize == 0) 16048cb51643SJaakko Heinonen sectsize = DEV_BSIZE; 16058cb51643SJaakko Heinonen else 16068cb51643SJaakko Heinonen sectsize = mdio->md_sectorsize; 16078cb51643SJaakko Heinonen if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize) 16088cb51643SJaakko Heinonen return (EINVAL); 16097ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 1610947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, mdio->md_type); 1611f4e7c5a8SJaakko Heinonen else { 1612f4e7c5a8SJaakko Heinonen if (mdio->md_unit > INT_MAX) 1613f4e7c5a8SJaakko Heinonen return (EINVAL); 1614947fc8deSPoul-Henning Kamp sc = mdnew(mdio->md_unit, &error, mdio->md_type); 1615f4e7c5a8SJaakko Heinonen } 1616b830359bSPawel Jakub Dawidek if (sc == NULL) 16177ee3c044SPawel Jakub Dawidek return (error); 16187ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 16197ee3c044SPawel Jakub Dawidek mdio->md_unit = sc->unit; 1620b830359bSPawel Jakub Dawidek sc->mediasize = mdio->md_mediasize; 16218cb51643SJaakko Heinonen sc->sectorsize = sectsize; 1622b830359bSPawel Jakub Dawidek error = EDOOFUS; 1623b830359bSPawel Jakub Dawidek switch (sc->type) { 1624b830359bSPawel Jakub Dawidek case MD_MALLOC: 16259b00ca19SPoul-Henning Kamp sc->start = mdstart_malloc; 1626b830359bSPawel Jakub Dawidek error = mdcreate_malloc(sc, mdio); 1627b830359bSPawel Jakub Dawidek break; 1628b830359bSPawel Jakub Dawidek case MD_PRELOAD: 1629734e78dfSJaakko Heinonen /* 1630734e78dfSJaakko Heinonen * We disallow attaching preloaded memory disks via 1631734e78dfSJaakko Heinonen * ioctl. Preloaded memory disks are automatically 1632734e78dfSJaakko Heinonen * attached in g_md_init(). 1633734e78dfSJaakko Heinonen */ 1634734e78dfSJaakko Heinonen error = EOPNOTSUPP; 1635b830359bSPawel Jakub Dawidek break; 1636b830359bSPawel Jakub Dawidek case MD_VNODE: 16379b00ca19SPoul-Henning Kamp sc->start = mdstart_vnode; 1638b830359bSPawel Jakub Dawidek error = mdcreate_vnode(sc, mdio, td); 1639b830359bSPawel Jakub Dawidek break; 1640b830359bSPawel Jakub Dawidek case MD_SWAP: 16419b00ca19SPoul-Henning Kamp sc->start = mdstart_swap; 1642b830359bSPawel Jakub Dawidek error = mdcreate_swap(sc, mdio, td); 1643b830359bSPawel Jakub Dawidek break; 16440efd9bfdSEdward Tomasz Napierala case MD_NULL: 16450efd9bfdSEdward Tomasz Napierala sc->start = mdstart_null; 16460efd9bfdSEdward Tomasz Napierala error = mdcreate_null(sc, mdio, td); 16470efd9bfdSEdward Tomasz Napierala break; 1648b830359bSPawel Jakub Dawidek } 1649b830359bSPawel Jakub Dawidek if (error != 0) { 1650b830359bSPawel Jakub Dawidek mddestroy(sc, td); 1651b830359bSPawel Jakub Dawidek return (error); 1652b830359bSPawel Jakub Dawidek } 16539b00ca19SPoul-Henning Kamp 16549b00ca19SPoul-Henning Kamp /* Prune off any residual fractional sector */ 16559b00ca19SPoul-Henning Kamp i = sc->mediasize % sc->sectorsize; 16569b00ca19SPoul-Henning Kamp sc->mediasize -= i; 16579b00ca19SPoul-Henning Kamp 1658b830359bSPawel Jakub Dawidek mdinit(sc); 1659b830359bSPawel Jakub Dawidek return (0); 16608f8def9eSPoul-Henning Kamp case MDIOCDETACH: 1661a9ebb311SEdward Tomasz Napierala if (mdio->md_mediasize != 0 || 1662a9ebb311SEdward Tomasz Napierala (mdio->md_options & ~MD_FORCE) != 0) 16638f8def9eSPoul-Henning Kamp return (EINVAL); 16649b00ca19SPoul-Henning Kamp 16659b00ca19SPoul-Henning Kamp sc = mdfind(mdio->md_unit); 16669b00ca19SPoul-Henning Kamp if (sc == NULL) 16679b00ca19SPoul-Henning Kamp return (ENOENT); 1668a9ebb311SEdward Tomasz Napierala if (sc->opencount != 0 && !(sc->flags & MD_FORCE) && 1669a9ebb311SEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 16709b00ca19SPoul-Henning Kamp return (EBUSY); 16719b00ca19SPoul-Henning Kamp return (mddestroy(sc, td)); 1672dc604f0cSEdward Tomasz Napierala case MDIOCRESIZE: 1673dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0) 1674dc604f0cSEdward Tomasz Napierala return (EINVAL); 1675dc604f0cSEdward Tomasz Napierala 1676dc604f0cSEdward Tomasz Napierala sc = mdfind(mdio->md_unit); 1677dc604f0cSEdward Tomasz Napierala if (sc == NULL) 1678dc604f0cSEdward Tomasz Napierala return (ENOENT); 16798cb51643SJaakko Heinonen if (mdio->md_mediasize < sc->sectorsize) 16808cb51643SJaakko Heinonen return (EINVAL); 1681dc604f0cSEdward Tomasz Napierala if (mdio->md_mediasize < sc->mediasize && 1682dc604f0cSEdward Tomasz Napierala !(sc->flags & MD_FORCE) && 1683dc604f0cSEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 1684dc604f0cSEdward Tomasz Napierala return (EBUSY); 1685dc604f0cSEdward Tomasz Napierala return (mdresize(sc, mdio)); 1686174b5e9aSPoul-Henning Kamp case MDIOCQUERY: 1687174b5e9aSPoul-Henning Kamp sc = mdfind(mdio->md_unit); 1688174b5e9aSPoul-Henning Kamp if (sc == NULL) 1689174b5e9aSPoul-Henning Kamp return (ENOENT); 1690174b5e9aSPoul-Henning Kamp mdio->md_type = sc->type; 1691174b5e9aSPoul-Henning Kamp mdio->md_options = sc->flags; 1692b830359bSPawel Jakub Dawidek mdio->md_mediasize = sc->mediasize; 1693b830359bSPawel Jakub Dawidek mdio->md_sectorsize = sc->sectorsize; 16949b00ca19SPoul-Henning Kamp if (sc->type == MD_VNODE) 169588b5b78dSPawel Jakub Dawidek error = copyout(sc->file, mdio->md_file, 169688b5b78dSPawel Jakub Dawidek strlen(sc->file) + 1); 169788b5b78dSPawel Jakub Dawidek return (error); 169816bcbe8cSPoul-Henning Kamp case MDIOCLIST: 169916bcbe8cSPoul-Henning Kamp i = 1; 170016bcbe8cSPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 170116bcbe8cSPoul-Henning Kamp if (i == MDNPAD - 1) 170216bcbe8cSPoul-Henning Kamp mdio->md_pad[i] = -1; 170316bcbe8cSPoul-Henning Kamp else 170416bcbe8cSPoul-Henning Kamp mdio->md_pad[i++] = sc->unit; 170516bcbe8cSPoul-Henning Kamp } 170616bcbe8cSPoul-Henning Kamp mdio->md_pad[0] = i - 1; 170716bcbe8cSPoul-Henning Kamp return (0); 17088f8def9eSPoul-Henning Kamp default: 17098f8def9eSPoul-Henning Kamp return (ENOIOCTL); 17108f8def9eSPoul-Henning Kamp }; 17119b00ca19SPoul-Henning Kamp } 17129b00ca19SPoul-Henning Kamp 17139b00ca19SPoul-Henning Kamp static int 17149b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 17159b00ca19SPoul-Henning Kamp { 17169b00ca19SPoul-Henning Kamp int error; 17179b00ca19SPoul-Henning Kamp 17189b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 17199b00ca19SPoul-Henning Kamp error = xmdctlioctl(dev, cmd, addr, flags, td); 17209b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 17219b00ca19SPoul-Henning Kamp return (error); 17223f54a085SPoul-Henning Kamp } 17233f54a085SPoul-Henning Kamp 172400a6a3c6SPoul-Henning Kamp static void 1725341b240dSJaakko Heinonen md_preloaded(u_char *image, size_t length, const char *name) 1726637f671aSPoul-Henning Kamp { 1727637f671aSPoul-Henning Kamp struct md_s *sc; 17289b00ca19SPoul-Henning Kamp int error; 1729637f671aSPoul-Henning Kamp 1730947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, MD_PRELOAD); 1731637f671aSPoul-Henning Kamp if (sc == NULL) 1732637f671aSPoul-Henning Kamp return; 1733b830359bSPawel Jakub Dawidek sc->mediasize = length; 1734b830359bSPawel Jakub Dawidek sc->sectorsize = DEV_BSIZE; 1735637f671aSPoul-Henning Kamp sc->pl_ptr = image; 1736637f671aSPoul-Henning Kamp sc->pl_len = length; 17379b00ca19SPoul-Henning Kamp sc->start = mdstart_preload; 17385d4ca75eSLuigi Rizzo #ifdef MD_ROOT 1739637f671aSPoul-Henning Kamp if (sc->unit == 0) 174022ff74b2SMarcel Moolenaar rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0"; 17415d4ca75eSLuigi Rizzo #endif 1742637f671aSPoul-Henning Kamp mdinit(sc); 1743341b240dSJaakko Heinonen if (name != NULL) { 1744341b240dSJaakko Heinonen printf("%s%d: Preloaded image <%s> %zd bytes at %p\n", 1745341b240dSJaakko Heinonen MD_NAME, sc->unit, name, length, image); 1746cc787e3dSMarcel Moolenaar } else { 1747c68ea8a6SMarcel Moolenaar printf("%s%d: Embedded image %zd bytes at %p\n", 1748cc787e3dSMarcel Moolenaar MD_NAME, sc->unit, length, image); 1749341b240dSJaakko Heinonen } 1750637f671aSPoul-Henning Kamp } 1751637f671aSPoul-Henning Kamp 1752637f671aSPoul-Henning Kamp static void 175319945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused) 175400a6a3c6SPoul-Henning Kamp { 175595f1a897SPoul-Henning Kamp caddr_t mod; 175695f1a897SPoul-Henning Kamp u_char *ptr, *name, *type; 175795f1a897SPoul-Henning Kamp unsigned len; 1758d12fc952SKonstantin Belousov int i; 1759d12fc952SKonstantin Belousov 1760d12fc952SKonstantin Belousov /* figure out log2(NINDIR) */ 1761d12fc952SKonstantin Belousov for (i = NINDIR, nshift = -1; i; nshift++) 1762d12fc952SKonstantin Belousov i >>= 1; 176395f1a897SPoul-Henning Kamp 17640a937206SPoul-Henning Kamp mod = NULL; 17659b00ca19SPoul-Henning Kamp sx_init(&md_sx, "MD config lock"); 17660a937206SPoul-Henning Kamp g_topology_unlock(); 1767f4e7c5a8SJaakko Heinonen md_uh = new_unrhdr(0, INT_MAX, NULL); 1768cc787e3dSMarcel Moolenaar #ifdef MD_ROOT 1769cc787e3dSMarcel Moolenaar if (mfs_root_size != 0) { 17709b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1771cc787e3dSMarcel Moolenaar md_preloaded(__DEVOLATILE(u_char *, &mfs_root), mfs_root_size, 1772cc787e3dSMarcel Moolenaar NULL); 17739b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 1774cc787e3dSMarcel Moolenaar } 177571e4fff8SPoul-Henning Kamp #endif 17769b00ca19SPoul-Henning Kamp /* XXX: are preload_* static or do they need Giant ? */ 177795f1a897SPoul-Henning Kamp while ((mod = preload_search_next_name(mod)) != NULL) { 177895f1a897SPoul-Henning Kamp name = (char *)preload_search_info(mod, MODINFO_NAME); 177995f1a897SPoul-Henning Kamp if (name == NULL) 178095f1a897SPoul-Henning Kamp continue; 17819b00ca19SPoul-Henning Kamp type = (char *)preload_search_info(mod, MODINFO_TYPE); 178295f1a897SPoul-Henning Kamp if (type == NULL) 178395f1a897SPoul-Henning Kamp continue; 178471e4fff8SPoul-Henning Kamp if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 178595f1a897SPoul-Henning Kamp continue; 17868d5ac6c3SMarcel Moolenaar ptr = preload_fetch_addr(mod); 17878d5ac6c3SMarcel Moolenaar len = preload_fetch_size(mod); 17888d5ac6c3SMarcel Moolenaar if (ptr != NULL && len != 0) { 17899b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1790341b240dSJaakko Heinonen md_preloaded(ptr, len, name); 17919b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 179295f1a897SPoul-Henning Kamp } 17938d5ac6c3SMarcel Moolenaar } 179459ec9023SKonstantin Belousov md_vnode_pbuf_freecnt = nswbuf / 10; 179506d425f9SEd Schouten status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL, 179610b0e058SDima Dorfman 0600, MDCTL_NAME); 17970eb14309SPoul-Henning Kamp g_topology_lock(); 179800a6a3c6SPoul-Henning Kamp } 179900a6a3c6SPoul-Henning Kamp 180019945697SPoul-Henning Kamp static void 1801c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1802c27a8954SWojciech A. Koszek struct g_consumer *cp __unused, struct g_provider *pp) 1803c27a8954SWojciech A. Koszek { 1804c27a8954SWojciech A. Koszek struct md_s *mp; 1805c27a8954SWojciech A. Koszek char *type; 1806c27a8954SWojciech A. Koszek 1807c27a8954SWojciech A. Koszek mp = gp->softc; 1808c27a8954SWojciech A. Koszek if (mp == NULL) 1809c27a8954SWojciech A. Koszek return; 1810c27a8954SWojciech A. Koszek 1811c27a8954SWojciech A. Koszek switch (mp->type) { 1812c27a8954SWojciech A. Koszek case MD_MALLOC: 1813c27a8954SWojciech A. Koszek type = "malloc"; 1814c27a8954SWojciech A. Koszek break; 1815c27a8954SWojciech A. Koszek case MD_PRELOAD: 1816c27a8954SWojciech A. Koszek type = "preload"; 1817c27a8954SWojciech A. Koszek break; 1818c27a8954SWojciech A. Koszek case MD_VNODE: 1819c27a8954SWojciech A. Koszek type = "vnode"; 1820c27a8954SWojciech A. Koszek break; 1821c27a8954SWojciech A. Koszek case MD_SWAP: 1822c27a8954SWojciech A. Koszek type = "swap"; 1823c27a8954SWojciech A. Koszek break; 18240efd9bfdSEdward Tomasz Napierala case MD_NULL: 18250efd9bfdSEdward Tomasz Napierala type = "null"; 18260efd9bfdSEdward Tomasz Napierala break; 1827c27a8954SWojciech A. Koszek default: 1828c27a8954SWojciech A. Koszek type = "unknown"; 1829c27a8954SWojciech A. Koszek break; 1830c27a8954SWojciech A. Koszek } 1831c27a8954SWojciech A. Koszek 1832c27a8954SWojciech A. Koszek if (pp != NULL) { 1833c27a8954SWojciech A. Koszek if (indent == NULL) { 1834c27a8954SWojciech A. Koszek sbuf_printf(sb, " u %d", mp->unit); 1835c27a8954SWojciech A. Koszek sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize); 1836c27a8954SWojciech A. Koszek sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads); 1837c27a8954SWojciech A. Koszek sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors); 1838c27a8954SWojciech A. Koszek sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize); 1839c27a8954SWojciech A. Koszek sbuf_printf(sb, " t %s", type); 1840c27a8954SWojciech A. Koszek if (mp->type == MD_VNODE && mp->vnode != NULL) 1841c27a8954SWojciech A. Koszek sbuf_printf(sb, " file %s", mp->file); 1842c27a8954SWojciech A. Koszek } else { 1843c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, 1844c27a8954SWojciech A. Koszek mp->unit); 1845c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n", 1846c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->sectorsize); 1847c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n", 1848c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwheads); 1849c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n", 1850c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwsectors); 1851c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<length>%ju</length>\n", 1852c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->mediasize); 18531f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<compression>%s</compression>\n", indent, 18541f192809SAndrey V. Elsukov (mp->flags & MD_COMPRESS) == 0 ? "off": "on"); 18551f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<access>%s</access>\n", indent, 18561f192809SAndrey V. Elsukov (mp->flags & MD_READONLY) == 0 ? "read-write": 18571f192809SAndrey V. Elsukov "read-only"); 1858c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<type>%s</type>\n", indent, 1859c27a8954SWojciech A. Koszek type); 1860ec170744SAndrey V. Elsukov if (mp->type == MD_VNODE && mp->vnode != NULL) { 1861ec170744SAndrey V. Elsukov sbuf_printf(sb, "%s<file>", indent); 1862ec170744SAndrey V. Elsukov g_conf_printf_escaped(sb, "%s", mp->file); 1863ec170744SAndrey V. Elsukov sbuf_printf(sb, "</file>\n"); 1864ec170744SAndrey V. Elsukov } 1865c27a8954SWojciech A. Koszek } 1866c27a8954SWojciech A. Koszek } 1867c27a8954SWojciech A. Koszek } 1868c27a8954SWojciech A. Koszek 1869c27a8954SWojciech A. Koszek static void 187019945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused) 187157e9624eSPoul-Henning Kamp { 18729d4b5945SMaxim Sobolev 18739b00ca19SPoul-Henning Kamp sx_destroy(&md_sx); 187419945697SPoul-Henning Kamp if (status_dev != NULL) 187557e9624eSPoul-Henning Kamp destroy_dev(status_dev); 1876f4e7c5a8SJaakko Heinonen delete_unrhdr(md_uh); 187757e9624eSPoul-Henning Kamp } 1878