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. 39*fbbd9655SWarner Losh * 3. 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 61c3d1c73fSMaxim Sobolev #include "opt_rootdevname.h" 626f4f00f1SPoul-Henning Kamp #include "opt_geom.h" 633f54a085SPoul-Henning Kamp #include "opt_md.h" 6471e4fff8SPoul-Henning Kamp 6500a6a3c6SPoul-Henning Kamp #include <sys/param.h> 6600a6a3c6SPoul-Henning Kamp #include <sys/systm.h> 679626b608SPoul-Henning Kamp #include <sys/bio.h> 6859ec9023SKonstantin Belousov #include <sys/buf.h> 6900a6a3c6SPoul-Henning Kamp #include <sys/conf.h> 70a03be42dSMaxim Sobolev #include <sys/devicestat.h> 718f8def9eSPoul-Henning Kamp #include <sys/fcntl.h> 72fb919e4dSMark Murray #include <sys/kernel.h> 735c97ca54SIan Dowse #include <sys/kthread.h> 7406d425f9SEd Schouten #include <sys/limits.h> 75fb919e4dSMark Murray #include <sys/linker.h> 76fb919e4dSMark Murray #include <sys/lock.h> 77fb919e4dSMark Murray #include <sys/malloc.h> 78fb919e4dSMark Murray #include <sys/mdioctl.h> 79a08d2e7fSJohn Baldwin #include <sys/mount.h> 809dceb26bSJohn Baldwin #include <sys/mutex.h> 819b00ca19SPoul-Henning Kamp #include <sys/sx.h> 82fb919e4dSMark Murray #include <sys/namei.h> 838f8def9eSPoul-Henning Kamp #include <sys/proc.h> 84fb919e4dSMark Murray #include <sys/queue.h> 8589f6b863SAttilio Rao #include <sys/rwlock.h> 86657bd8b1SAndrey V. Elsukov #include <sys/sbuf.h> 8763710c4dSJohn Baldwin #include <sys/sched.h> 887cd53fddSAlan Cox #include <sys/sf_buf.h> 89fb919e4dSMark Murray #include <sys/sysctl.h> 90fb919e4dSMark Murray #include <sys/vnode.h> 91fb919e4dSMark Murray 926f4f00f1SPoul-Henning Kamp #include <geom/geom.h> 93ec170744SAndrey V. Elsukov #include <geom/geom_int.h> 946f4f00f1SPoul-Henning Kamp 958f8def9eSPoul-Henning Kamp #include <vm/vm.h> 961c771f92SKonstantin Belousov #include <vm/vm_param.h> 978f8def9eSPoul-Henning Kamp #include <vm/vm_object.h> 988f8def9eSPoul-Henning Kamp #include <vm/vm_page.h> 998f8def9eSPoul-Henning Kamp #include <vm/vm_pager.h> 1008f8def9eSPoul-Henning Kamp #include <vm/swap_pager.h> 101f43b2bacSPoul-Henning Kamp #include <vm/uma.h> 1023f54a085SPoul-Henning Kamp 103a9934668SKenneth D. Merry #include <machine/bus.h> 104a9934668SKenneth D. Merry 10557e9624eSPoul-Henning Kamp #define MD_MODVER 1 10657e9624eSPoul-Henning Kamp 1075c97ca54SIan Dowse #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */ 108a08d2e7fSJohn Baldwin #define MD_EXITING 0x20000 /* Worker thread is exiting. */ 1095c97ca54SIan Dowse 110f2744793SSheldon Hearn #ifndef MD_NSECT 111f2744793SSheldon Hearn #define MD_NSECT (10000 * 2) 11233edfabeSPoul-Henning Kamp #endif 11333edfabeSPoul-Henning Kamp 1145bb84bc8SRobert Watson static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk"); 1155bb84bc8SRobert Watson static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors"); 11600a6a3c6SPoul-Henning Kamp 11771e4fff8SPoul-Henning Kamp static int md_debug; 1183eb9ab52SEitan Adler SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, 1193eb9ab52SEitan Adler "Enable md(4) debug messages"); 120c44d423eSKonstantin Belousov static int md_malloc_wait; 1213eb9ab52SEitan Adler SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0, 1223eb9ab52SEitan Adler "Allow malloc to wait for memory allocations"); 12300a6a3c6SPoul-Henning Kamp 12422ff74b2SMarcel Moolenaar #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE) 12522ff74b2SMarcel Moolenaar #define MD_ROOT_FSTYPE "ufs" 12622ff74b2SMarcel Moolenaar #endif 12722ff74b2SMarcel Moolenaar 128cc787e3dSMarcel Moolenaar #if defined(MD_ROOT) 129de64f22aSLuigi Rizzo /* 130de64f22aSLuigi Rizzo * Preloaded image gets put here. 131cc787e3dSMarcel Moolenaar */ 132cc787e3dSMarcel Moolenaar #if defined(MD_ROOT_SIZE) 133cc787e3dSMarcel Moolenaar /* 134f4c1f0b9SAdrian Chadd * We put the mfs_root symbol into the oldmfs section of the kernel object file. 135de64f22aSLuigi Rizzo * Applications that patch the object with the image can determine 136f4c1f0b9SAdrian Chadd * the size looking at the oldmfs section size within the kernel. 137de64f22aSLuigi Rizzo */ 138f4c1f0b9SAdrian Chadd u_char mfs_root[MD_ROOT_SIZE*1024] __attribute__ ((section ("oldmfs"))); 139f4c1f0b9SAdrian Chadd const int mfs_root_size = sizeof(mfs_root); 140cc787e3dSMarcel Moolenaar #else 141cc787e3dSMarcel Moolenaar extern volatile u_char __weak_symbol mfs_root; 142cc787e3dSMarcel Moolenaar extern volatile u_char __weak_symbol mfs_root_end; 143cc787e3dSMarcel Moolenaar __GLOBL(mfs_root); 144cc787e3dSMarcel Moolenaar __GLOBL(mfs_root_end); 145cc787e3dSMarcel Moolenaar #define mfs_root_size ((uintptr_t)(&mfs_root_end - &mfs_root)) 146cc787e3dSMarcel Moolenaar #endif 14771e4fff8SPoul-Henning Kamp #endif 14871e4fff8SPoul-Henning Kamp 14919945697SPoul-Henning Kamp static g_init_t g_md_init; 15019945697SPoul-Henning Kamp static g_fini_t g_md_fini; 15119945697SPoul-Henning Kamp static g_start_t g_md_start; 15219945697SPoul-Henning Kamp static g_access_t g_md_access; 153b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent, 154b42f40b8SJaakko Heinonen struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp); 1550eb14309SPoul-Henning Kamp 1564d24901aSPedro F. Giffuni static struct cdev *status_dev = NULL; 1579b00ca19SPoul-Henning Kamp static struct sx md_sx; 158f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh; 15957e9624eSPoul-Henning Kamp 160a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl; 1618f8def9eSPoul-Henning Kamp 1628f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = { 163dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 1647ac40f5fSPoul-Henning Kamp .d_ioctl = mdctlioctl, 1657ac40f5fSPoul-Henning Kamp .d_name = MD_NAME, 16600a6a3c6SPoul-Henning Kamp }; 16700a6a3c6SPoul-Henning Kamp 16819945697SPoul-Henning Kamp struct g_class g_md_class = { 16919945697SPoul-Henning Kamp .name = "MD", 1705721c9c7SPoul-Henning Kamp .version = G_VERSION, 17119945697SPoul-Henning Kamp .init = g_md_init, 17219945697SPoul-Henning Kamp .fini = g_md_fini, 17319945697SPoul-Henning Kamp .start = g_md_start, 17419945697SPoul-Henning Kamp .access = g_md_access, 175c27a8954SWojciech A. Koszek .dumpconf = g_md_dumpconf, 17619945697SPoul-Henning Kamp }; 17719945697SPoul-Henning Kamp 17819945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md); 17919945697SPoul-Henning Kamp 1800cfaeeeeSPoul-Henning Kamp 18113e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list); 1823f54a085SPoul-Henning Kamp 183c6517568SPoul-Henning Kamp #define NINDIR (PAGE_SIZE / sizeof(uintptr_t)) 184c6517568SPoul-Henning Kamp #define NMASK (NINDIR-1) 185c6517568SPoul-Henning Kamp static int nshift; 186c6517568SPoul-Henning Kamp 18759ec9023SKonstantin Belousov static int md_vnode_pbuf_freecnt; 18859ec9023SKonstantin Belousov 189c6517568SPoul-Henning Kamp struct indir { 190c6517568SPoul-Henning Kamp uintptr_t *array; 1918b149b51SJohn Baldwin u_int total; 1928b149b51SJohn Baldwin u_int used; 1938b149b51SJohn Baldwin u_int shift; 194c6517568SPoul-Henning Kamp }; 195c6517568SPoul-Henning Kamp 19600a6a3c6SPoul-Henning Kamp struct md_s { 19700a6a3c6SPoul-Henning Kamp int unit; 1983f54a085SPoul-Henning Kamp LIST_ENTRY(md_s) list; 1998177437dSPoul-Henning Kamp struct bio_queue_head bio_queue; 2000f8500a5SPoul-Henning Kamp struct mtx queue_mtx; 20140ea77a0SAlexander Motin struct mtx stat_mtx; 20289c9c53dSPoul-Henning Kamp struct cdev *dev; 2038f8def9eSPoul-Henning Kamp enum md_types type; 204b830359bSPawel Jakub Dawidek off_t mediasize; 205b830359bSPawel Jakub Dawidek unsigned sectorsize; 206fe603109SMaxim Sobolev unsigned opencount; 2074e8bfe14SPoul-Henning Kamp unsigned fwheads; 2084e8bfe14SPoul-Henning Kamp unsigned fwsectors; 2098f8def9eSPoul-Henning Kamp unsigned flags; 210f43b2bacSPoul-Henning Kamp char name[20]; 2115c97ca54SIan Dowse struct proc *procp; 2126f4f00f1SPoul-Henning Kamp struct g_geom *gp; 2136f4f00f1SPoul-Henning Kamp struct g_provider *pp; 2149b00ca19SPoul-Henning Kamp int (*start)(struct md_s *sc, struct bio *bp); 215a03be42dSMaxim Sobolev struct devstat *devstat; 21695f1a897SPoul-Henning Kamp 21795f1a897SPoul-Henning Kamp /* MD_MALLOC related fields */ 218c6517568SPoul-Henning Kamp struct indir *indir; 219f43b2bacSPoul-Henning Kamp uma_zone_t uma; 22000a6a3c6SPoul-Henning Kamp 22195f1a897SPoul-Henning Kamp /* MD_PRELOAD related fields */ 22295f1a897SPoul-Henning Kamp u_char *pl_ptr; 223b830359bSPawel Jakub Dawidek size_t pl_len; 22400a6a3c6SPoul-Henning Kamp 2258f8def9eSPoul-Henning Kamp /* MD_VNODE related fields */ 2268f8def9eSPoul-Henning Kamp struct vnode *vnode; 22761a6eb62SPawel Jakub Dawidek char file[PATH_MAX]; 2288f8def9eSPoul-Henning Kamp struct ucred *cred; 2298f8def9eSPoul-Henning Kamp 230e0cebb40SDima Dorfman /* MD_SWAP related fields */ 2318f8def9eSPoul-Henning Kamp vm_object_t object; 2328f8def9eSPoul-Henning Kamp }; 23300a6a3c6SPoul-Henning Kamp 234c6517568SPoul-Henning Kamp static struct indir * 2358b149b51SJohn Baldwin new_indir(u_int shift) 236c6517568SPoul-Henning Kamp { 237c6517568SPoul-Henning Kamp struct indir *ip; 238c6517568SPoul-Henning Kamp 239c44d423eSKonstantin Belousov ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT) 240c44d423eSKonstantin Belousov | M_ZERO); 241c6517568SPoul-Henning Kamp if (ip == NULL) 242c6517568SPoul-Henning Kamp return (NULL); 243c6517568SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 244c44d423eSKonstantin Belousov M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO); 245c6517568SPoul-Henning Kamp if (ip->array == NULL) { 246c6517568SPoul-Henning Kamp free(ip, M_MD); 247c6517568SPoul-Henning Kamp return (NULL); 248c6517568SPoul-Henning Kamp } 249c6517568SPoul-Henning Kamp ip->total = NINDIR; 250c6517568SPoul-Henning Kamp ip->shift = shift; 251c6517568SPoul-Henning Kamp return (ip); 252c6517568SPoul-Henning Kamp } 253c6517568SPoul-Henning Kamp 254c6517568SPoul-Henning Kamp static void 255c6517568SPoul-Henning Kamp del_indir(struct indir *ip) 256c6517568SPoul-Henning Kamp { 257c6517568SPoul-Henning Kamp 258f43b2bacSPoul-Henning Kamp free(ip->array, M_MDSECT); 259c6517568SPoul-Henning Kamp free(ip, M_MD); 260c6517568SPoul-Henning Kamp } 261c6517568SPoul-Henning Kamp 262f43b2bacSPoul-Henning Kamp static void 263f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip) 264f43b2bacSPoul-Henning Kamp { 265f43b2bacSPoul-Henning Kamp int i; 266f43b2bacSPoul-Henning Kamp 267f43b2bacSPoul-Henning Kamp for (i = 0; i < NINDIR; i++) { 268f43b2bacSPoul-Henning Kamp if (!ip->array[i]) 269f43b2bacSPoul-Henning Kamp continue; 270f43b2bacSPoul-Henning Kamp if (ip->shift) 271f43b2bacSPoul-Henning Kamp destroy_indir(sc, (struct indir*)(ip->array[i])); 272f43b2bacSPoul-Henning Kamp else if (ip->array[i] > 255) 273f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void *)(ip->array[i])); 274f43b2bacSPoul-Henning Kamp } 275f43b2bacSPoul-Henning Kamp del_indir(ip); 276f43b2bacSPoul-Henning Kamp } 277f43b2bacSPoul-Henning Kamp 278c6517568SPoul-Henning Kamp /* 2796c3cd0e2SMaxim Konovalov * This function does the math and allocates the top level "indir" structure 280c6517568SPoul-Henning Kamp * for a device of "size" sectors. 281c6517568SPoul-Henning Kamp */ 282c6517568SPoul-Henning Kamp 283c6517568SPoul-Henning Kamp static struct indir * 284c6517568SPoul-Henning Kamp dimension(off_t size) 285c6517568SPoul-Henning Kamp { 286c6517568SPoul-Henning Kamp off_t rcnt; 287c6517568SPoul-Henning Kamp struct indir *ip; 288d12fc952SKonstantin Belousov int layer; 289c6517568SPoul-Henning Kamp 290c6517568SPoul-Henning Kamp rcnt = size; 291c6517568SPoul-Henning Kamp layer = 0; 292c6517568SPoul-Henning Kamp while (rcnt > NINDIR) { 293c6517568SPoul-Henning Kamp rcnt /= NINDIR; 294c6517568SPoul-Henning Kamp layer++; 295c6517568SPoul-Henning Kamp } 296c6517568SPoul-Henning Kamp 297c6517568SPoul-Henning Kamp /* 298c6517568SPoul-Henning Kamp * XXX: the top layer is probably not fully populated, so we allocate 29983e13864SPoul-Henning Kamp * too much space for ip->array in here. 300c6517568SPoul-Henning Kamp */ 30183e13864SPoul-Henning Kamp ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO); 30283e13864SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 30383e13864SPoul-Henning Kamp M_MDSECT, M_WAITOK | M_ZERO); 30483e13864SPoul-Henning Kamp ip->total = NINDIR; 30583e13864SPoul-Henning Kamp ip->shift = layer * nshift; 306c6517568SPoul-Henning Kamp return (ip); 307c6517568SPoul-Henning Kamp } 308c6517568SPoul-Henning Kamp 309c6517568SPoul-Henning Kamp /* 310c6517568SPoul-Henning Kamp * Read a given sector 311c6517568SPoul-Henning Kamp */ 312c6517568SPoul-Henning Kamp 313c6517568SPoul-Henning Kamp static uintptr_t 314c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset) 315c6517568SPoul-Henning Kamp { 316c6517568SPoul-Henning Kamp struct indir *cip; 317c6517568SPoul-Henning Kamp int idx; 318c6517568SPoul-Henning Kamp uintptr_t up; 319c6517568SPoul-Henning Kamp 320c6517568SPoul-Henning Kamp if (md_debug > 1) 3216569d6f3SMaxime Henrion printf("s_read(%jd)\n", (intmax_t)offset); 322c6517568SPoul-Henning Kamp up = 0; 323c6517568SPoul-Henning Kamp for (cip = ip; cip != NULL;) { 324c6517568SPoul-Henning Kamp if (cip->shift) { 325c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 326c6517568SPoul-Henning Kamp up = cip->array[idx]; 327c6517568SPoul-Henning Kamp cip = (struct indir *)up; 328c6517568SPoul-Henning Kamp continue; 329c6517568SPoul-Henning Kamp } 330c6517568SPoul-Henning Kamp idx = offset & NMASK; 331c6517568SPoul-Henning Kamp return (cip->array[idx]); 332c6517568SPoul-Henning Kamp } 333c6517568SPoul-Henning Kamp return (0); 334c6517568SPoul-Henning Kamp } 335c6517568SPoul-Henning Kamp 336c6517568SPoul-Henning Kamp /* 337c6517568SPoul-Henning Kamp * Write a given sector, prune the tree if the value is 0 338c6517568SPoul-Henning Kamp */ 339c6517568SPoul-Henning Kamp 340c6517568SPoul-Henning Kamp static int 341fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr) 342c6517568SPoul-Henning Kamp { 343c6517568SPoul-Henning Kamp struct indir *cip, *lip[10]; 344c6517568SPoul-Henning Kamp int idx, li; 345c6517568SPoul-Henning Kamp uintptr_t up; 346c6517568SPoul-Henning Kamp 347c6517568SPoul-Henning Kamp if (md_debug > 1) 3486569d6f3SMaxime Henrion printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr); 349c6517568SPoul-Henning Kamp up = 0; 350c6517568SPoul-Henning Kamp li = 0; 351c6517568SPoul-Henning Kamp cip = ip; 352c6517568SPoul-Henning Kamp for (;;) { 353c6517568SPoul-Henning Kamp lip[li++] = cip; 354c6517568SPoul-Henning Kamp if (cip->shift) { 355c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 356c6517568SPoul-Henning Kamp up = cip->array[idx]; 357c6517568SPoul-Henning Kamp if (up != 0) { 358c6517568SPoul-Henning Kamp cip = (struct indir *)up; 359c6517568SPoul-Henning Kamp continue; 360c6517568SPoul-Henning Kamp } 361c6517568SPoul-Henning Kamp /* Allocate branch */ 362c6517568SPoul-Henning Kamp cip->array[idx] = 363c6517568SPoul-Henning Kamp (uintptr_t)new_indir(cip->shift - nshift); 364c6517568SPoul-Henning Kamp if (cip->array[idx] == 0) 365975b628fSPoul-Henning Kamp return (ENOSPC); 366c6517568SPoul-Henning Kamp cip->used++; 367c6517568SPoul-Henning Kamp up = cip->array[idx]; 368c6517568SPoul-Henning Kamp cip = (struct indir *)up; 369c6517568SPoul-Henning Kamp continue; 370c6517568SPoul-Henning Kamp } 371c6517568SPoul-Henning Kamp /* leafnode */ 372c6517568SPoul-Henning Kamp idx = offset & NMASK; 373c6517568SPoul-Henning Kamp up = cip->array[idx]; 374c6517568SPoul-Henning Kamp if (up != 0) 375c6517568SPoul-Henning Kamp cip->used--; 376c6517568SPoul-Henning Kamp cip->array[idx] = ptr; 377c6517568SPoul-Henning Kamp if (ptr != 0) 378c6517568SPoul-Henning Kamp cip->used++; 379c6517568SPoul-Henning Kamp break; 380c6517568SPoul-Henning Kamp } 381c6517568SPoul-Henning Kamp if (cip->used != 0 || li == 1) 382c6517568SPoul-Henning Kamp return (0); 383c6517568SPoul-Henning Kamp li--; 384c6517568SPoul-Henning Kamp while (cip->used == 0 && cip != ip) { 385c6517568SPoul-Henning Kamp li--; 386c6517568SPoul-Henning Kamp idx = (offset >> lip[li]->shift) & NMASK; 387c6517568SPoul-Henning Kamp up = lip[li]->array[idx]; 388c6517568SPoul-Henning Kamp KASSERT(up == (uintptr_t)cip, ("md screwed up")); 389c6517568SPoul-Henning Kamp del_indir(cip); 3904a6a94d8SArchie Cobbs lip[li]->array[idx] = 0; 391c6517568SPoul-Henning Kamp lip[li]->used--; 392c6517568SPoul-Henning Kamp cip = lip[li]; 393c6517568SPoul-Henning Kamp } 394c6517568SPoul-Henning Kamp return (0); 395c6517568SPoul-Henning Kamp } 396c6517568SPoul-Henning Kamp 3976f4f00f1SPoul-Henning Kamp 3986f4f00f1SPoul-Henning Kamp static int 3996f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e) 4006f4f00f1SPoul-Henning Kamp { 4016f4f00f1SPoul-Henning Kamp struct md_s *sc; 4026f4f00f1SPoul-Henning Kamp 4036f4f00f1SPoul-Henning Kamp sc = pp->geom->softc; 40441c8b468SEdward Tomasz Napierala if (sc == NULL) { 40541c8b468SEdward Tomasz Napierala if (r <= 0 && w <= 0 && e <= 0) 40641c8b468SEdward Tomasz Napierala return (0); 4076b60a2cdSPoul-Henning Kamp return (ENXIO); 40841c8b468SEdward Tomasz Napierala } 4096f4f00f1SPoul-Henning Kamp r += pp->acr; 4106f4f00f1SPoul-Henning Kamp w += pp->acw; 4116f4f00f1SPoul-Henning Kamp e += pp->ace; 41286776891SChristian S.J. Peron if ((sc->flags & MD_READONLY) != 0 && w > 0) 41386776891SChristian S.J. Peron return (EROFS); 4146f4f00f1SPoul-Henning Kamp if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 4156f4f00f1SPoul-Henning Kamp sc->opencount = 1; 4166f4f00f1SPoul-Henning Kamp } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 4176f4f00f1SPoul-Henning Kamp sc->opencount = 0; 4186f4f00f1SPoul-Henning Kamp } 4196f4f00f1SPoul-Henning Kamp return (0); 4206f4f00f1SPoul-Henning Kamp } 4216f4f00f1SPoul-Henning Kamp 4226f4f00f1SPoul-Henning Kamp static void 4236f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp) 4246f4f00f1SPoul-Henning Kamp { 4256f4f00f1SPoul-Henning Kamp struct md_s *sc; 4266f4f00f1SPoul-Henning Kamp 4276f4f00f1SPoul-Henning Kamp sc = bp->bio_to->geom->softc; 42840ea77a0SAlexander Motin if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) { 42940ea77a0SAlexander Motin mtx_lock(&sc->stat_mtx); 430a03be42dSMaxim Sobolev devstat_start_transaction_bio(sc->devstat, bp); 43140ea77a0SAlexander Motin mtx_unlock(&sc->stat_mtx); 43240ea77a0SAlexander Motin } 4330f8500a5SPoul-Henning Kamp mtx_lock(&sc->queue_mtx); 434891619a6SPoul-Henning Kamp bioq_disksort(&sc->bio_queue, bp); 4354b07ede4SPawel Jakub Dawidek mtx_unlock(&sc->queue_mtx); 436e4cdd0d4SPawel Jakub Dawidek wakeup(sc); 4376f4f00f1SPoul-Henning Kamp } 4386f4f00f1SPoul-Henning Kamp 43959ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_ZERO 1 44059ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_FILL 2 44159ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_READ 3 44259ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_WRITE 4 44359ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_CMP 5 44459ec9023SKonstantin Belousov 44559ec9023SKonstantin Belousov static int 446a9934668SKenneth D. Merry md_malloc_move_ma(vm_page_t **mp, int *ma_offs, unsigned sectorsize, 44759ec9023SKonstantin Belousov void *ptr, u_char fill, int op) 44859ec9023SKonstantin Belousov { 44959ec9023SKonstantin Belousov struct sf_buf *sf; 45059ec9023SKonstantin Belousov vm_page_t m, *mp1; 45159ec9023SKonstantin Belousov char *p, first; 45259ec9023SKonstantin Belousov off_t *uc; 45359ec9023SKonstantin Belousov unsigned n; 45459ec9023SKonstantin Belousov int error, i, ma_offs1, sz, first_read; 45559ec9023SKonstantin Belousov 45659ec9023SKonstantin Belousov m = NULL; 45759ec9023SKonstantin Belousov error = 0; 45859ec9023SKonstantin Belousov sf = NULL; 45959ec9023SKonstantin Belousov /* if (op == MD_MALLOC_MOVE_CMP) { gcc */ 46059ec9023SKonstantin Belousov first = 0; 46159ec9023SKonstantin Belousov first_read = 0; 46259ec9023SKonstantin Belousov uc = ptr; 46359ec9023SKonstantin Belousov mp1 = *mp; 46459ec9023SKonstantin Belousov ma_offs1 = *ma_offs; 46559ec9023SKonstantin Belousov /* } */ 46659ec9023SKonstantin Belousov sched_pin(); 46759ec9023SKonstantin Belousov for (n = sectorsize; n != 0; n -= sz) { 46859ec9023SKonstantin Belousov sz = imin(PAGE_SIZE - *ma_offs, n); 46959ec9023SKonstantin Belousov if (m != **mp) { 47059ec9023SKonstantin Belousov if (sf != NULL) 47159ec9023SKonstantin Belousov sf_buf_free(sf); 47259ec9023SKonstantin Belousov m = **mp; 47359ec9023SKonstantin Belousov sf = sf_buf_alloc(m, SFB_CPUPRIVATE | 47459ec9023SKonstantin Belousov (md_malloc_wait ? 0 : SFB_NOWAIT)); 47559ec9023SKonstantin Belousov if (sf == NULL) { 47659ec9023SKonstantin Belousov error = ENOMEM; 47759ec9023SKonstantin Belousov break; 47859ec9023SKonstantin Belousov } 47959ec9023SKonstantin Belousov } 48059ec9023SKonstantin Belousov p = (char *)sf_buf_kva(sf) + *ma_offs; 48159ec9023SKonstantin Belousov switch (op) { 48259ec9023SKonstantin Belousov case MD_MALLOC_MOVE_ZERO: 48359ec9023SKonstantin Belousov bzero(p, sz); 48459ec9023SKonstantin Belousov break; 48559ec9023SKonstantin Belousov case MD_MALLOC_MOVE_FILL: 48659ec9023SKonstantin Belousov memset(p, fill, sz); 48759ec9023SKonstantin Belousov break; 48859ec9023SKonstantin Belousov case MD_MALLOC_MOVE_READ: 48959ec9023SKonstantin Belousov bcopy(ptr, p, sz); 49059ec9023SKonstantin Belousov cpu_flush_dcache(p, sz); 49159ec9023SKonstantin Belousov break; 49259ec9023SKonstantin Belousov case MD_MALLOC_MOVE_WRITE: 49359ec9023SKonstantin Belousov bcopy(p, ptr, sz); 49459ec9023SKonstantin Belousov break; 49559ec9023SKonstantin Belousov case MD_MALLOC_MOVE_CMP: 49659ec9023SKonstantin Belousov for (i = 0; i < sz; i++, p++) { 49759ec9023SKonstantin Belousov if (!first_read) { 49859ec9023SKonstantin Belousov *uc = (u_char)*p; 49959ec9023SKonstantin Belousov first = *p; 50059ec9023SKonstantin Belousov first_read = 1; 50159ec9023SKonstantin Belousov } else if (*p != first) { 50259ec9023SKonstantin Belousov error = EDOOFUS; 50359ec9023SKonstantin Belousov break; 50459ec9023SKonstantin Belousov } 50559ec9023SKonstantin Belousov } 50659ec9023SKonstantin Belousov break; 50759ec9023SKonstantin Belousov default: 508a9934668SKenneth D. Merry KASSERT(0, ("md_malloc_move_ma unknown op %d\n", op)); 50959ec9023SKonstantin Belousov break; 51059ec9023SKonstantin Belousov } 51159ec9023SKonstantin Belousov if (error != 0) 51259ec9023SKonstantin Belousov break; 51359ec9023SKonstantin Belousov *ma_offs += sz; 51459ec9023SKonstantin Belousov *ma_offs %= PAGE_SIZE; 51559ec9023SKonstantin Belousov if (*ma_offs == 0) 51659ec9023SKonstantin Belousov (*mp)++; 51759ec9023SKonstantin Belousov ptr = (char *)ptr + sz; 51859ec9023SKonstantin Belousov } 51959ec9023SKonstantin Belousov 52059ec9023SKonstantin Belousov if (sf != NULL) 52159ec9023SKonstantin Belousov sf_buf_free(sf); 52259ec9023SKonstantin Belousov sched_unpin(); 52359ec9023SKonstantin Belousov if (op == MD_MALLOC_MOVE_CMP && error != 0) { 52459ec9023SKonstantin Belousov *mp = mp1; 52559ec9023SKonstantin Belousov *ma_offs = ma_offs1; 52659ec9023SKonstantin Belousov } 52759ec9023SKonstantin Belousov return (error); 52859ec9023SKonstantin Belousov } 52959ec9023SKonstantin Belousov 530b4a4f93cSPoul-Henning Kamp static int 531a9934668SKenneth D. Merry md_malloc_move_vlist(bus_dma_segment_t **pvlist, int *pma_offs, 532a9934668SKenneth D. Merry unsigned len, void *ptr, u_char fill, int op) 533a9934668SKenneth D. Merry { 534a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 535a9934668SKenneth D. Merry uint8_t *p, *end, first; 536a9934668SKenneth D. Merry off_t *uc; 537a9934668SKenneth D. Merry int ma_offs, seg_len; 538a9934668SKenneth D. Merry 539a9934668SKenneth D. Merry vlist = *pvlist; 540a9934668SKenneth D. Merry ma_offs = *pma_offs; 541a9934668SKenneth D. Merry uc = ptr; 542a9934668SKenneth D. Merry 543a9934668SKenneth D. Merry for (; len != 0; len -= seg_len) { 544a9934668SKenneth D. Merry seg_len = imin(vlist->ds_len - ma_offs, len); 545a9934668SKenneth D. Merry p = (uint8_t *)(uintptr_t)vlist->ds_addr + ma_offs; 546a9934668SKenneth D. Merry switch (op) { 547a9934668SKenneth D. Merry case MD_MALLOC_MOVE_ZERO: 548a9934668SKenneth D. Merry bzero(p, seg_len); 549a9934668SKenneth D. Merry break; 550a9934668SKenneth D. Merry case MD_MALLOC_MOVE_FILL: 551a9934668SKenneth D. Merry memset(p, fill, seg_len); 552a9934668SKenneth D. Merry break; 553a9934668SKenneth D. Merry case MD_MALLOC_MOVE_READ: 554a9934668SKenneth D. Merry bcopy(ptr, p, seg_len); 555a9934668SKenneth D. Merry cpu_flush_dcache(p, seg_len); 556a9934668SKenneth D. Merry break; 557a9934668SKenneth D. Merry case MD_MALLOC_MOVE_WRITE: 558a9934668SKenneth D. Merry bcopy(p, ptr, seg_len); 559a9934668SKenneth D. Merry break; 560a9934668SKenneth D. Merry case MD_MALLOC_MOVE_CMP: 561a9934668SKenneth D. Merry end = p + seg_len; 562a9934668SKenneth D. Merry first = *uc = *p; 563a9934668SKenneth D. Merry /* Confirm all following bytes match the first */ 564a9934668SKenneth D. Merry while (++p < end) { 565a9934668SKenneth D. Merry if (*p != first) 566a9934668SKenneth D. Merry return (EDOOFUS); 567a9934668SKenneth D. Merry } 568a9934668SKenneth D. Merry break; 569a9934668SKenneth D. Merry default: 570a9934668SKenneth D. Merry KASSERT(0, ("md_malloc_move_vlist unknown op %d\n", op)); 571a9934668SKenneth D. Merry break; 572a9934668SKenneth D. Merry } 573a9934668SKenneth D. Merry 574a9934668SKenneth D. Merry ma_offs += seg_len; 575a9934668SKenneth D. Merry if (ma_offs == vlist->ds_len) { 576a9934668SKenneth D. Merry ma_offs = 0; 577a9934668SKenneth D. Merry vlist++; 578a9934668SKenneth D. Merry } 579a9934668SKenneth D. Merry ptr = (uint8_t *)ptr + seg_len; 580a9934668SKenneth D. Merry } 581a9934668SKenneth D. Merry *pvlist = vlist; 582a9934668SKenneth D. Merry *pma_offs = ma_offs; 583a9934668SKenneth D. Merry 584a9934668SKenneth D. Merry return (0); 585a9934668SKenneth D. Merry } 586a9934668SKenneth D. Merry 587a9934668SKenneth D. Merry static int 588b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp) 58900a6a3c6SPoul-Henning Kamp { 590c6517568SPoul-Henning Kamp u_char *dst; 59159ec9023SKonstantin Belousov vm_page_t *m; 592a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 59359ec9023SKonstantin Belousov int i, error, error1, ma_offs, notmapped; 594b830359bSPawel Jakub Dawidek off_t secno, nsec, uc; 595c6517568SPoul-Henning Kamp uintptr_t sp, osp; 59600a6a3c6SPoul-Henning Kamp 5975541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 5985541f25eSPawel Jakub Dawidek case BIO_READ: 5995541f25eSPawel Jakub Dawidek case BIO_WRITE: 6005541f25eSPawel Jakub Dawidek case BIO_DELETE: 6015541f25eSPawel Jakub Dawidek break; 6025541f25eSPawel Jakub Dawidek default: 6035541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 6045541f25eSPawel Jakub Dawidek } 6055541f25eSPawel Jakub Dawidek 60659ec9023SKonstantin Belousov notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0; 607a9934668SKenneth D. Merry vlist = (bp->bio_flags & BIO_VLIST) != 0 ? 608a9934668SKenneth D. Merry (bus_dma_segment_t *)bp->bio_data : NULL; 60959ec9023SKonstantin Belousov if (notmapped) { 61059ec9023SKonstantin Belousov m = bp->bio_ma; 61159ec9023SKonstantin Belousov ma_offs = bp->bio_ma_offset; 61259ec9023SKonstantin Belousov dst = NULL; 613a9934668SKenneth D. Merry KASSERT(vlist == NULL, ("vlists cannot be unmapped")); 614a9934668SKenneth D. Merry } else if (vlist != NULL) { 615a9934668SKenneth D. Merry ma_offs = bp->bio_ma_offset; 616a9934668SKenneth D. Merry dst = NULL; 61759ec9023SKonstantin Belousov } else { 61859ec9023SKonstantin Belousov dst = bp->bio_data; 61959ec9023SKonstantin Belousov } 62059ec9023SKonstantin Belousov 621b830359bSPawel Jakub Dawidek nsec = bp->bio_length / sc->sectorsize; 622b830359bSPawel Jakub Dawidek secno = bp->bio_offset / sc->sectorsize; 623c6517568SPoul-Henning Kamp error = 0; 62400a6a3c6SPoul-Henning Kamp while (nsec--) { 625fde2a2e4SPoul-Henning Kamp osp = s_read(sc->indir, secno); 6268177437dSPoul-Henning Kamp if (bp->bio_cmd == BIO_DELETE) { 627fde2a2e4SPoul-Henning Kamp if (osp != 0) 628fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, 0); 6298177437dSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_READ) { 63059ec9023SKonstantin Belousov if (osp == 0) { 63159ec9023SKonstantin Belousov if (notmapped) { 632a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, &ma_offs, 63359ec9023SKonstantin Belousov sc->sectorsize, NULL, 0, 63459ec9023SKonstantin Belousov MD_MALLOC_MOVE_ZERO); 635a9934668SKenneth D. Merry } else if (vlist != NULL) { 636a9934668SKenneth D. Merry error = md_malloc_move_vlist(&vlist, 637a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, NULL, 0, 638a9934668SKenneth D. Merry MD_MALLOC_MOVE_ZERO); 63959ec9023SKonstantin Belousov } else 640b830359bSPawel Jakub Dawidek bzero(dst, sc->sectorsize); 64159ec9023SKonstantin Belousov } else if (osp <= 255) { 64259ec9023SKonstantin Belousov if (notmapped) { 643a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, &ma_offs, 64459ec9023SKonstantin Belousov sc->sectorsize, NULL, osp, 64559ec9023SKonstantin Belousov MD_MALLOC_MOVE_FILL); 646a9934668SKenneth D. Merry } else if (vlist != NULL) { 647a9934668SKenneth D. Merry error = md_malloc_move_vlist(&vlist, 648a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, NULL, osp, 649a9934668SKenneth D. Merry MD_MALLOC_MOVE_FILL); 65059ec9023SKonstantin Belousov } else 651dbb95048SMarcel Moolenaar memset(dst, osp, sc->sectorsize); 65259ec9023SKonstantin Belousov } else { 65359ec9023SKonstantin Belousov if (notmapped) { 654a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, &ma_offs, 65559ec9023SKonstantin Belousov sc->sectorsize, (void *)osp, 0, 65659ec9023SKonstantin Belousov MD_MALLOC_MOVE_READ); 657a9934668SKenneth D. Merry } else if (vlist != NULL) { 658a9934668SKenneth D. Merry error = md_malloc_move_vlist(&vlist, 659a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, 660a9934668SKenneth D. Merry (void *)osp, 0, 661a9934668SKenneth D. Merry MD_MALLOC_MOVE_READ); 66259ec9023SKonstantin Belousov } else { 663b830359bSPawel Jakub Dawidek bcopy((void *)osp, dst, sc->sectorsize); 664dbb95048SMarcel Moolenaar cpu_flush_dcache(dst, sc->sectorsize); 665dbb95048SMarcel Moolenaar } 66659ec9023SKonstantin Belousov } 667fde2a2e4SPoul-Henning Kamp osp = 0; 668c6517568SPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 6698f8def9eSPoul-Henning Kamp if (sc->flags & MD_COMPRESS) { 67059ec9023SKonstantin Belousov if (notmapped) { 671a9934668SKenneth D. Merry error1 = md_malloc_move_ma(&m, &ma_offs, 67259ec9023SKonstantin Belousov sc->sectorsize, &uc, 0, 67359ec9023SKonstantin Belousov MD_MALLOC_MOVE_CMP); 67459ec9023SKonstantin Belousov i = error1 == 0 ? sc->sectorsize : 0; 675a9934668SKenneth D. Merry } else if (vlist != NULL) { 676a9934668SKenneth D. Merry error1 = md_malloc_move_vlist(&vlist, 677a9934668SKenneth D. Merry &ma_offs, sc->sectorsize, &uc, 0, 678a9934668SKenneth D. Merry MD_MALLOC_MOVE_CMP); 679a9934668SKenneth D. Merry i = error1 == 0 ? sc->sectorsize : 0; 68059ec9023SKonstantin Belousov } else { 68100a6a3c6SPoul-Henning Kamp uc = dst[0]; 68259ec9023SKonstantin Belousov for (i = 1; i < sc->sectorsize; i++) { 68300a6a3c6SPoul-Henning Kamp if (dst[i] != uc) 68400a6a3c6SPoul-Henning Kamp break; 68559ec9023SKonstantin Belousov } 68659ec9023SKonstantin Belousov } 6878f8def9eSPoul-Henning Kamp } else { 6888f8def9eSPoul-Henning Kamp i = 0; 6898f8def9eSPoul-Henning Kamp uc = 0; 6908f8def9eSPoul-Henning Kamp } 691b830359bSPawel Jakub Dawidek if (i == sc->sectorsize) { 692fde2a2e4SPoul-Henning Kamp if (osp != uc) 693fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, uc); 69400a6a3c6SPoul-Henning Kamp } else { 695fde2a2e4SPoul-Henning Kamp if (osp <= 255) { 696b830359bSPawel Jakub Dawidek sp = (uintptr_t)uma_zalloc(sc->uma, 697c44d423eSKonstantin Belousov md_malloc_wait ? M_WAITOK : 698b830359bSPawel Jakub Dawidek M_NOWAIT); 699c6517568SPoul-Henning Kamp if (sp == 0) { 700fde2a2e4SPoul-Henning Kamp error = ENOSPC; 701fde2a2e4SPoul-Henning Kamp break; 702fde2a2e4SPoul-Henning Kamp } 70359ec9023SKonstantin Belousov if (notmapped) { 704a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, 70559ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 70659ec9023SKonstantin Belousov (void *)sp, 0, 70759ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 708a9934668SKenneth D. Merry } else if (vlist != NULL) { 709a9934668SKenneth D. Merry error = md_malloc_move_vlist( 710a9934668SKenneth D. Merry &vlist, &ma_offs, 711a9934668SKenneth D. Merry sc->sectorsize, (void *)sp, 712a9934668SKenneth D. Merry 0, MD_MALLOC_MOVE_WRITE); 71359ec9023SKonstantin Belousov } else { 71459ec9023SKonstantin Belousov bcopy(dst, (void *)sp, 71559ec9023SKonstantin Belousov sc->sectorsize); 71659ec9023SKonstantin Belousov } 717fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, sp); 718c6517568SPoul-Henning Kamp } else { 71959ec9023SKonstantin Belousov if (notmapped) { 720a9934668SKenneth D. Merry error = md_malloc_move_ma(&m, 72159ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 72259ec9023SKonstantin Belousov (void *)osp, 0, 72359ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 724a9934668SKenneth D. Merry } else if (vlist != NULL) { 725a9934668SKenneth D. Merry error = md_malloc_move_vlist( 726a9934668SKenneth D. Merry &vlist, &ma_offs, 727a9934668SKenneth D. Merry sc->sectorsize, (void *)osp, 728a9934668SKenneth D. Merry 0, MD_MALLOC_MOVE_WRITE); 72959ec9023SKonstantin Belousov } else { 73059ec9023SKonstantin Belousov bcopy(dst, (void *)osp, 73159ec9023SKonstantin Belousov sc->sectorsize); 73259ec9023SKonstantin Belousov } 733fde2a2e4SPoul-Henning Kamp osp = 0; 73400a6a3c6SPoul-Henning Kamp } 73500a6a3c6SPoul-Henning Kamp } 736c6517568SPoul-Henning Kamp } else { 737c6517568SPoul-Henning Kamp error = EOPNOTSUPP; 738c6517568SPoul-Henning Kamp } 739c6517568SPoul-Henning Kamp if (osp > 255) 740f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void*)osp); 741e3ed29a7SPawel Jakub Dawidek if (error != 0) 742c6517568SPoul-Henning Kamp break; 74300a6a3c6SPoul-Henning Kamp secno++; 744a9934668SKenneth D. Merry if (!notmapped && vlist == NULL) 745b830359bSPawel Jakub Dawidek dst += sc->sectorsize; 74600a6a3c6SPoul-Henning Kamp } 7478177437dSPoul-Henning Kamp bp->bio_resid = 0; 748c6517568SPoul-Henning Kamp return (error); 74900a6a3c6SPoul-Henning Kamp } 75000a6a3c6SPoul-Henning Kamp 751a9934668SKenneth D. Merry static void 752a9934668SKenneth D. Merry mdcopyto_vlist(void *src, bus_dma_segment_t *vlist, off_t offset, off_t len) 753a9934668SKenneth D. Merry { 754a9934668SKenneth D. Merry off_t seg_len; 755a9934668SKenneth D. Merry 756a9934668SKenneth D. Merry while (offset >= vlist->ds_len) { 757a9934668SKenneth D. Merry offset -= vlist->ds_len; 758a9934668SKenneth D. Merry vlist++; 759a9934668SKenneth D. Merry } 760a9934668SKenneth D. Merry 761a9934668SKenneth D. Merry while (len != 0) { 762a9934668SKenneth D. Merry seg_len = omin(len, vlist->ds_len - offset); 763a9934668SKenneth D. Merry bcopy(src, (void *)(uintptr_t)(vlist->ds_addr + offset), 764a9934668SKenneth D. Merry seg_len); 765a9934668SKenneth D. Merry offset = 0; 766a9934668SKenneth D. Merry src = (uint8_t *)src + seg_len; 767a9934668SKenneth D. Merry len -= seg_len; 768a9934668SKenneth D. Merry vlist++; 769a9934668SKenneth D. Merry } 770a9934668SKenneth D. Merry } 771a9934668SKenneth D. Merry 772a9934668SKenneth D. Merry static void 773a9934668SKenneth D. Merry mdcopyfrom_vlist(bus_dma_segment_t *vlist, off_t offset, void *dst, off_t len) 774a9934668SKenneth D. Merry { 775a9934668SKenneth D. Merry off_t seg_len; 776a9934668SKenneth D. Merry 777a9934668SKenneth D. Merry while (offset >= vlist->ds_len) { 778a9934668SKenneth D. Merry offset -= vlist->ds_len; 779a9934668SKenneth D. Merry vlist++; 780a9934668SKenneth D. Merry } 781a9934668SKenneth D. Merry 782a9934668SKenneth D. Merry while (len != 0) { 783a9934668SKenneth D. Merry seg_len = omin(len, vlist->ds_len - offset); 784a9934668SKenneth D. Merry bcopy((void *)(uintptr_t)(vlist->ds_addr + offset), dst, 785a9934668SKenneth D. Merry seg_len); 786a9934668SKenneth D. Merry offset = 0; 787a9934668SKenneth D. Merry dst = (uint8_t *)dst + seg_len; 788a9934668SKenneth D. Merry len -= seg_len; 789a9934668SKenneth D. Merry vlist++; 790a9934668SKenneth D. Merry } 791a9934668SKenneth D. Merry } 792a9934668SKenneth D. Merry 793b4a4f93cSPoul-Henning Kamp static int 794b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp) 79571e4fff8SPoul-Henning Kamp { 796a9934668SKenneth D. Merry uint8_t *p; 79771e4fff8SPoul-Henning Kamp 798a9934668SKenneth D. Merry p = sc->pl_ptr + bp->bio_offset; 799a8a58d03SPawel Jakub Dawidek switch (bp->bio_cmd) { 800a8a58d03SPawel Jakub Dawidek case BIO_READ: 801a9934668SKenneth D. Merry if ((bp->bio_flags & BIO_VLIST) != 0) { 802a9934668SKenneth D. Merry mdcopyto_vlist(p, (bus_dma_segment_t *)bp->bio_data, 803a9934668SKenneth D. Merry bp->bio_ma_offset, bp->bio_length); 804a9934668SKenneth D. Merry } else { 805a9934668SKenneth D. Merry bcopy(p, bp->bio_data, bp->bio_length); 806a9934668SKenneth D. Merry } 807dbb95048SMarcel Moolenaar cpu_flush_dcache(bp->bio_data, bp->bio_length); 808a8a58d03SPawel Jakub Dawidek break; 809a8a58d03SPawel Jakub Dawidek case BIO_WRITE: 810a9934668SKenneth D. Merry if ((bp->bio_flags & BIO_VLIST) != 0) { 811a9934668SKenneth D. Merry mdcopyfrom_vlist((bus_dma_segment_t *)bp->bio_data, 812a9934668SKenneth D. Merry bp->bio_ma_offset, p, bp->bio_length); 813a9934668SKenneth D. Merry } else { 814a9934668SKenneth D. Merry bcopy(bp->bio_data, p, bp->bio_length); 815a9934668SKenneth D. Merry } 816a8a58d03SPawel Jakub Dawidek break; 81771e4fff8SPoul-Henning Kamp } 8188177437dSPoul-Henning Kamp bp->bio_resid = 0; 819b4a4f93cSPoul-Henning Kamp return (0); 82071e4fff8SPoul-Henning Kamp } 82171e4fff8SPoul-Henning Kamp 822b4a4f93cSPoul-Henning Kamp static int 823b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp) 8248f8def9eSPoul-Henning Kamp { 8255050aa86SKonstantin Belousov int error; 8268f8def9eSPoul-Henning Kamp struct uio auio; 8278f8def9eSPoul-Henning Kamp struct iovec aiov; 828a9934668SKenneth D. Merry struct iovec *piov; 8298f8def9eSPoul-Henning Kamp struct mount *mp; 8305541f25eSPawel Jakub Dawidek struct vnode *vp; 83159ec9023SKonstantin Belousov struct buf *pb; 832a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 8335541f25eSPawel Jakub Dawidek struct thread *td; 834d5f998baSKonstantin Belousov off_t iolen, len, zerosize; 835d5f998baSKonstantin Belousov int ma_offs, npages; 8365541f25eSPawel Jakub Dawidek 8375541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 8385541f25eSPawel Jakub Dawidek case BIO_READ: 839a9934668SKenneth D. Merry auio.uio_rw = UIO_READ; 840a9934668SKenneth D. Merry break; 8415541f25eSPawel Jakub Dawidek case BIO_WRITE: 8420abd21bdSDag-Erling Smørgrav case BIO_DELETE: 843a9934668SKenneth D. Merry auio.uio_rw = UIO_WRITE; 844a9934668SKenneth D. Merry break; 8455541f25eSPawel Jakub Dawidek case BIO_FLUSH: 8465541f25eSPawel Jakub Dawidek break; 8475541f25eSPawel Jakub Dawidek default: 8485541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 8495541f25eSPawel Jakub Dawidek } 8505541f25eSPawel Jakub Dawidek 8515541f25eSPawel Jakub Dawidek td = curthread; 8525541f25eSPawel Jakub Dawidek vp = sc->vnode; 853a9934668SKenneth D. Merry pb = NULL; 854a9934668SKenneth D. Merry piov = NULL; 855a9934668SKenneth D. Merry ma_offs = bp->bio_ma_offset; 856d5f998baSKonstantin Belousov len = bp->bio_length; 8578f8def9eSPoul-Henning Kamp 8588f8def9eSPoul-Henning Kamp /* 8598f8def9eSPoul-Henning Kamp * VNODE I/O 8608f8def9eSPoul-Henning Kamp * 8618f8def9eSPoul-Henning Kamp * If an error occurs, we set BIO_ERROR but we do not set 8628f8def9eSPoul-Henning Kamp * B_INVAL because (for a write anyway), the buffer is 8638f8def9eSPoul-Henning Kamp * still valid. 8648f8def9eSPoul-Henning Kamp */ 8658f8def9eSPoul-Henning Kamp 8665541f25eSPawel Jakub Dawidek if (bp->bio_cmd == BIO_FLUSH) { 8675541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 868cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 8695541f25eSPawel Jakub Dawidek error = VOP_FSYNC(vp, MNT_WAIT, td); 87022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 8715541f25eSPawel Jakub Dawidek vn_finished_write(mp); 8725541f25eSPawel Jakub Dawidek return (error); 8735541f25eSPawel Jakub Dawidek } 8745541f25eSPawel Jakub Dawidek 875a9934668SKenneth D. Merry auio.uio_offset = (vm_ooffset_t)bp->bio_offset; 876a9934668SKenneth D. Merry auio.uio_resid = bp->bio_length; 877a9934668SKenneth D. Merry auio.uio_segflg = UIO_SYSSPACE; 878a9934668SKenneth D. Merry auio.uio_td = td; 8798f8def9eSPoul-Henning Kamp 8800abd21bdSDag-Erling Smørgrav if (bp->bio_cmd == BIO_DELETE) { 881a9934668SKenneth D. Merry /* 882a9934668SKenneth D. Merry * Emulate BIO_DELETE by writing zeros. 883a9934668SKenneth D. Merry */ 88489cb2a19SMatthew D Fleming zerosize = ZERO_REGION_SIZE - 88589cb2a19SMatthew D Fleming (ZERO_REGION_SIZE % sc->sectorsize); 886a9934668SKenneth D. Merry auio.uio_iovcnt = howmany(bp->bio_length, zerosize); 887a9934668SKenneth D. Merry piov = malloc(sizeof(*piov) * auio.uio_iovcnt, M_MD, M_WAITOK); 888a9934668SKenneth D. Merry auio.uio_iov = piov; 889a9934668SKenneth D. Merry while (len > 0) { 890a9934668SKenneth D. Merry piov->iov_base = __DECONST(void *, zero_region); 891a9934668SKenneth D. Merry piov->iov_len = len; 892a9934668SKenneth D. Merry if (len > zerosize) 893a9934668SKenneth D. Merry piov->iov_len = zerosize; 894a9934668SKenneth D. Merry len -= piov->iov_len; 895a9934668SKenneth D. Merry piov++; 8960abd21bdSDag-Erling Smørgrav } 897a9934668SKenneth D. Merry piov = auio.uio_iov; 898a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_VLIST) != 0) { 899a9934668SKenneth D. Merry piov = malloc(sizeof(*piov) * bp->bio_ma_n, M_MD, M_WAITOK); 900a9934668SKenneth D. Merry auio.uio_iov = piov; 901a9934668SKenneth D. Merry vlist = (bus_dma_segment_t *)bp->bio_data; 902a9934668SKenneth D. Merry while (len > 0) { 903a9934668SKenneth D. Merry piov->iov_base = (void *)(uintptr_t)(vlist->ds_addr + 904a9934668SKenneth D. Merry ma_offs); 905a9934668SKenneth D. Merry piov->iov_len = vlist->ds_len - ma_offs; 906a9934668SKenneth D. Merry if (piov->iov_len > len) 907a9934668SKenneth D. Merry piov->iov_len = len; 908a9934668SKenneth D. Merry len -= piov->iov_len; 909a9934668SKenneth D. Merry ma_offs = 0; 910a9934668SKenneth D. Merry vlist++; 911a9934668SKenneth D. Merry piov++; 9120abd21bdSDag-Erling Smørgrav } 913a9934668SKenneth D. Merry auio.uio_iovcnt = piov - auio.uio_iov; 914a9934668SKenneth D. Merry piov = auio.uio_iov; 915a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 91659ec9023SKonstantin Belousov pb = getpbuf(&md_vnode_pbuf_freecnt); 917d5f998baSKonstantin Belousov bp->bio_resid = len; 918d5f998baSKonstantin Belousov unmapped_step: 919d5f998baSKonstantin Belousov npages = atop(min(MAXPHYS, round_page(len + (ma_offs & 920d5f998baSKonstantin Belousov PAGE_MASK)))); 921d5f998baSKonstantin Belousov iolen = min(ptoa(npages) - (ma_offs & PAGE_MASK), len); 922d5f998baSKonstantin Belousov KASSERT(iolen > 0, ("zero iolen")); 923d5f998baSKonstantin Belousov pmap_qenter((vm_offset_t)pb->b_data, 924d5f998baSKonstantin Belousov &bp->bio_ma[atop(ma_offs)], npages); 925d5f998baSKonstantin Belousov aiov.iov_base = (void *)((vm_offset_t)pb->b_data + 926d5f998baSKonstantin Belousov (ma_offs & PAGE_MASK)); 927d5f998baSKonstantin Belousov aiov.iov_len = iolen; 9288f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 9298f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 930d5f998baSKonstantin Belousov auio.uio_resid = iolen; 931a9934668SKenneth D. Merry } else { 932a9934668SKenneth D. Merry aiov.iov_base = bp->bio_data; 933a9934668SKenneth D. Merry aiov.iov_len = bp->bio_length; 934a9934668SKenneth D. Merry auio.uio_iov = &aiov; 935a9934668SKenneth D. Merry auio.uio_iovcnt = 1; 936a9934668SKenneth D. Merry } 9377e76bb56SMatthew Dillon /* 9387e76bb56SMatthew Dillon * When reading set IO_DIRECT to try to avoid double-caching 93917a13919SPoul-Henning Kamp * the data. When writing IO_DIRECT is not optimal. 9407e76bb56SMatthew Dillon */ 941a9934668SKenneth D. Merry if (auio.uio_rw == UIO_READ) { 942cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 9435541f25eSPawel Jakub Dawidek error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred); 94422db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 9458f8def9eSPoul-Henning Kamp } else { 9465541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 947cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 9485541f25eSPawel Jakub Dawidek error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC, 9495541f25eSPawel Jakub Dawidek sc->cred); 95022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 9518f8def9eSPoul-Henning Kamp vn_finished_write(mp); 9528f8def9eSPoul-Henning Kamp } 953a9934668SKenneth D. Merry 954d5f998baSKonstantin Belousov if (pb != NULL) { 955d5f998baSKonstantin Belousov pmap_qremove((vm_offset_t)pb->b_data, npages); 956d5f998baSKonstantin Belousov if (error == 0) { 957d5f998baSKonstantin Belousov len -= iolen; 958d5f998baSKonstantin Belousov bp->bio_resid -= iolen; 959d5f998baSKonstantin Belousov ma_offs += iolen; 960d5f998baSKonstantin Belousov if (len > 0) 961d5f998baSKonstantin Belousov goto unmapped_step; 962d5f998baSKonstantin Belousov } 96359ec9023SKonstantin Belousov relpbuf(pb, &md_vnode_pbuf_freecnt); 96459ec9023SKonstantin Belousov } 965a9934668SKenneth D. Merry 966a9934668SKenneth D. Merry free(piov, M_MD); 967d5f998baSKonstantin Belousov if (pb == NULL) 9688f8def9eSPoul-Henning Kamp bp->bio_resid = auio.uio_resid; 969b4a4f93cSPoul-Henning Kamp return (error); 9708f8def9eSPoul-Henning Kamp } 9718f8def9eSPoul-Henning Kamp 972b4a4f93cSPoul-Henning Kamp static int 973b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp) 9748f8def9eSPoul-Henning Kamp { 9758e28326aSPoul-Henning Kamp vm_page_t m; 9768e28326aSPoul-Henning Kamp u_char *p; 97759ec9023SKonstantin Belousov vm_pindex_t i, lastp; 978a9934668SKenneth D. Merry bus_dma_segment_t *vlist; 97959ec9023SKonstantin Belousov int rv, ma_offs, offs, len, lastend; 9808f8def9eSPoul-Henning Kamp 9815541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 9825541f25eSPawel Jakub Dawidek case BIO_READ: 9835541f25eSPawel Jakub Dawidek case BIO_WRITE: 9845541f25eSPawel Jakub Dawidek case BIO_DELETE: 9855541f25eSPawel Jakub Dawidek break; 9865541f25eSPawel Jakub Dawidek default: 9875541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 9885541f25eSPawel Jakub Dawidek } 9895541f25eSPawel Jakub Dawidek 9908e28326aSPoul-Henning Kamp p = bp->bio_data; 991a9934668SKenneth D. Merry ma_offs = (bp->bio_flags & (BIO_UNMAPPED|BIO_VLIST)) != 0 ? 992a9934668SKenneth D. Merry bp->bio_ma_offset : 0; 993a9934668SKenneth D. Merry vlist = (bp->bio_flags & BIO_VLIST) != 0 ? 994a9934668SKenneth D. Merry (bus_dma_segment_t *)bp->bio_data : NULL; 995e07113d6SColin Percival 996e07113d6SColin Percival /* 9976c3cd0e2SMaxim Konovalov * offs is the offset at which to start operating on the 998e07113d6SColin Percival * next (ie, first) page. lastp is the last page on 999e07113d6SColin Percival * which we're going to operate. lastend is the ending 1000e07113d6SColin Percival * position within that last page (ie, PAGE_SIZE if 1001e07113d6SColin Percival * we're operating on complete aligned pages). 1002e07113d6SColin Percival */ 1003e07113d6SColin Percival offs = bp->bio_offset % PAGE_SIZE; 1004e07113d6SColin Percival lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE; 1005e07113d6SColin Percival lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1; 1006e07113d6SColin Percival 1007812851b6SBrian Feldman rv = VM_PAGER_OK; 100889f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 10098e28326aSPoul-Henning Kamp vm_object_pip_add(sc->object, 1); 1010e07113d6SColin Percival for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) { 1011e07113d6SColin Percival len = ((i == lastp) ? lastend : PAGE_SIZE) - offs; 10121a42d14aSKonstantin Belousov m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM); 10138e28326aSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 1014537cc627SKonstantin Belousov if (m->valid == VM_PAGE_BITS_ALL) 1015537cc627SKonstantin Belousov rv = VM_PAGER_OK; 1016537cc627SKonstantin Belousov else 1017b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(sc->object, &m, 1, 1018b0cd2017SGleb Smirnoff NULL, NULL); 1019812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 1020c7aebda8SAttilio Rao vm_page_xunbusy(m); 1021812851b6SBrian Feldman break; 1022e9f581baSKonstantin Belousov } else if (rv == VM_PAGER_FAIL) { 1023e9f581baSKonstantin Belousov /* 1024e9f581baSKonstantin Belousov * Pager does not have the page. Zero 1025e9f581baSKonstantin Belousov * the allocated page, and mark it as 1026e9f581baSKonstantin Belousov * valid. Do not set dirty, the page 1027e9f581baSKonstantin Belousov * can be recreated if thrown out. 1028e9f581baSKonstantin Belousov */ 102959ec9023SKonstantin Belousov pmap_zero_page(m); 1030e9f581baSKonstantin Belousov m->valid = VM_PAGE_BITS_ALL; 1031812851b6SBrian Feldman } 103259ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 103359ec9023SKonstantin Belousov pmap_copy_pages(&m, offs, bp->bio_ma, 103459ec9023SKonstantin Belousov ma_offs, len); 1035a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_VLIST) != 0) { 1036a9934668SKenneth D. Merry physcopyout_vlist(VM_PAGE_TO_PHYS(m) + offs, 1037a9934668SKenneth D. Merry vlist, ma_offs, len); 1038a9934668SKenneth D. Merry cpu_flush_dcache(p, len); 103959ec9023SKonstantin Belousov } else { 104059ec9023SKonstantin Belousov physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len); 1041dbb95048SMarcel Moolenaar cpu_flush_dcache(p, len); 104259ec9023SKonstantin Belousov } 10438e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 104407be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 1045b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(sc->object, &m, 1, 1046b0cd2017SGleb Smirnoff NULL, NULL); 1047537cc627SKonstantin Belousov else 1048537cc627SKonstantin Belousov rv = VM_PAGER_OK; 1049812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 1050c7aebda8SAttilio Rao vm_page_xunbusy(m); 1051812851b6SBrian Feldman break; 1052812851b6SBrian Feldman } 105359ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 105459ec9023SKonstantin Belousov pmap_copy_pages(bp->bio_ma, ma_offs, &m, 105559ec9023SKonstantin Belousov offs, len); 1056a9934668SKenneth D. Merry } else if ((bp->bio_flags & BIO_VLIST) != 0) { 1057a9934668SKenneth D. Merry physcopyin_vlist(vlist, ma_offs, 1058a9934668SKenneth D. Merry VM_PAGE_TO_PHYS(m) + offs, len); 105959ec9023SKonstantin Belousov } else { 106059ec9023SKonstantin Belousov physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len); 106159ec9023SKonstantin Belousov } 10628e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 10638e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_DELETE) { 106407be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 1065b0cd2017SGleb Smirnoff rv = vm_pager_get_pages(sc->object, &m, 1, 1066b0cd2017SGleb Smirnoff NULL, NULL); 1067537cc627SKonstantin Belousov else 1068537cc627SKonstantin Belousov rv = VM_PAGER_OK; 1069812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 1070c7aebda8SAttilio Rao vm_page_xunbusy(m); 1071812851b6SBrian Feldman break; 1072812851b6SBrian Feldman } 10734a13a769SKonstantin Belousov if (len != PAGE_SIZE) { 107459ec9023SKonstantin Belousov pmap_zero_page_area(m, offs, len); 10754a13a769SKonstantin Belousov vm_page_clear_dirty(m, offs, len); 10768e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 10774a13a769SKonstantin Belousov } else 10784a13a769SKonstantin Belousov vm_pager_page_unswapped(m); 10798e28326aSPoul-Henning Kamp } 1080c7aebda8SAttilio Rao vm_page_xunbusy(m); 1081fc0c3802SKonstantin Belousov vm_page_lock(m); 10824a13a769SKonstantin Belousov if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE) 10834a13a769SKonstantin Belousov vm_page_free(m); 10844a13a769SKonstantin Belousov else 10858e28326aSPoul-Henning Kamp vm_page_activate(m); 1086ecd5dd95SAlan Cox vm_page_unlock(m); 10875d9b4508SKonstantin Belousov if (bp->bio_cmd == BIO_WRITE) { 10888e28326aSPoul-Henning Kamp vm_page_dirty(m); 10895d9b4508SKonstantin Belousov vm_pager_page_unswapped(m); 10905d9b4508SKonstantin Belousov } 1091e07113d6SColin Percival 1092e07113d6SColin Percival /* Actions on further pages start at offset 0 */ 1093e07113d6SColin Percival p += PAGE_SIZE - offs; 1094e07113d6SColin Percival offs = 0; 109559ec9023SKonstantin Belousov ma_offs += len; 10968e28326aSPoul-Henning Kamp } 10970d8243ccSAttilio Rao vm_object_pip_wakeup(sc->object); 109889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1099812851b6SBrian Feldman return (rv != VM_PAGER_ERROR ? 0 : ENOSPC); 11008e28326aSPoul-Henning Kamp } 11018f8def9eSPoul-Henning Kamp 11020efd9bfdSEdward Tomasz Napierala static int 11030efd9bfdSEdward Tomasz Napierala mdstart_null(struct md_s *sc, struct bio *bp) 11040efd9bfdSEdward Tomasz Napierala { 11050efd9bfdSEdward Tomasz Napierala 11060efd9bfdSEdward Tomasz Napierala switch (bp->bio_cmd) { 11070efd9bfdSEdward Tomasz Napierala case BIO_READ: 11080efd9bfdSEdward Tomasz Napierala bzero(bp->bio_data, bp->bio_length); 11090efd9bfdSEdward Tomasz Napierala cpu_flush_dcache(bp->bio_data, bp->bio_length); 11100efd9bfdSEdward Tomasz Napierala break; 11110efd9bfdSEdward Tomasz Napierala case BIO_WRITE: 11120efd9bfdSEdward Tomasz Napierala break; 11130efd9bfdSEdward Tomasz Napierala } 11140efd9bfdSEdward Tomasz Napierala bp->bio_resid = 0; 11150efd9bfdSEdward Tomasz Napierala return (0); 11160efd9bfdSEdward Tomasz Napierala } 11170efd9bfdSEdward Tomasz Napierala 11188f8def9eSPoul-Henning Kamp static void 11195c97ca54SIan Dowse md_kthread(void *arg) 11205c97ca54SIan Dowse { 11215c97ca54SIan Dowse struct md_s *sc; 11225c97ca54SIan Dowse struct bio *bp; 1123a08d2e7fSJohn Baldwin int error; 11245c97ca54SIan Dowse 11255c97ca54SIan Dowse sc = arg; 1126982d11f8SJeff Roberson thread_lock(curthread); 112763710c4dSJohn Baldwin sched_prio(curthread, PRIBIO); 1128982d11f8SJeff Roberson thread_unlock(curthread); 11293b7b5496SKonstantin Belousov if (sc->type == MD_VNODE) 11303b7b5496SKonstantin Belousov curthread->td_pflags |= TDP_NORUNNINGBUF; 11315c97ca54SIan Dowse 1132b4a4f93cSPoul-Henning Kamp for (;;) { 1133a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 11345c97ca54SIan Dowse if (sc->flags & MD_SHUTDOWN) { 1135a08d2e7fSJohn Baldwin sc->flags |= MD_EXITING; 1136a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 11373745c395SJulian Elischer kproc_exit(0); 11385c97ca54SIan Dowse } 11399b00ca19SPoul-Henning Kamp bp = bioq_takefirst(&sc->bio_queue); 11409b00ca19SPoul-Henning Kamp if (!bp) { 11410f8500a5SPoul-Henning Kamp msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0); 11425c97ca54SIan Dowse continue; 11435c97ca54SIan Dowse } 11440f8500a5SPoul-Henning Kamp mtx_unlock(&sc->queue_mtx); 11454e8bfe14SPoul-Henning Kamp if (bp->bio_cmd == BIO_GETATTR) { 1146d91e813cSKonstantin Belousov if ((sc->fwsectors && sc->fwheads && 11474e8bfe14SPoul-Henning Kamp (g_handleattr_int(bp, "GEOM::fwsectors", 11484e8bfe14SPoul-Henning Kamp sc->fwsectors) || 11494e8bfe14SPoul-Henning Kamp g_handleattr_int(bp, "GEOM::fwheads", 1150d91e813cSKonstantin Belousov sc->fwheads))) || 1151d91e813cSKonstantin Belousov g_handleattr_int(bp, "GEOM::candelete", 1)) 11524e8bfe14SPoul-Henning Kamp error = -1; 11534e8bfe14SPoul-Henning Kamp else 11544e8bfe14SPoul-Henning Kamp error = EOPNOTSUPP; 11554e8bfe14SPoul-Henning Kamp } else { 11569b00ca19SPoul-Henning Kamp error = sc->start(sc, bp); 11574e8bfe14SPoul-Henning Kamp } 1158b4a4f93cSPoul-Henning Kamp 11596f4f00f1SPoul-Henning Kamp if (error != -1) { 11606f4f00f1SPoul-Henning Kamp bp->bio_completed = bp->bio_length; 1161a03be42dSMaxim Sobolev if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) 1162a03be42dSMaxim Sobolev devstat_end_transaction_bio(sc->devstat, bp); 116396410b95SKonstantin Belousov g_io_deliver(bp, error); 1164b4a4f93cSPoul-Henning Kamp } 11658f8def9eSPoul-Henning Kamp } 116626d48b40SPoul-Henning Kamp } 11678f8def9eSPoul-Henning Kamp 11688f8def9eSPoul-Henning Kamp static struct md_s * 11698f8def9eSPoul-Henning Kamp mdfind(int unit) 11708f8def9eSPoul-Henning Kamp { 11718f8def9eSPoul-Henning Kamp struct md_s *sc; 11728f8def9eSPoul-Henning Kamp 11733f54a085SPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 11743f54a085SPoul-Henning Kamp if (sc->unit == unit) 11758f8def9eSPoul-Henning Kamp break; 11768f8def9eSPoul-Henning Kamp } 11778f8def9eSPoul-Henning Kamp return (sc); 11788f8def9eSPoul-Henning Kamp } 11798f8def9eSPoul-Henning Kamp 11808f8def9eSPoul-Henning Kamp static struct md_s * 1181947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type) 11828f8def9eSPoul-Henning Kamp { 1183f4e7c5a8SJaakko Heinonen struct md_s *sc; 1184f4e7c5a8SJaakko Heinonen int error; 11858f8def9eSPoul-Henning Kamp 11869b00ca19SPoul-Henning Kamp *errp = 0; 1187f4e7c5a8SJaakko Heinonen if (unit == -1) 1188f4e7c5a8SJaakko Heinonen unit = alloc_unr(md_uh); 1189f4e7c5a8SJaakko Heinonen else 1190f4e7c5a8SJaakko Heinonen unit = alloc_unr_specific(md_uh, unit); 1191f4e7c5a8SJaakko Heinonen 1192f4e7c5a8SJaakko Heinonen if (unit == -1) { 11937ee3c044SPawel Jakub Dawidek *errp = EBUSY; 11943f54a085SPoul-Henning Kamp return (NULL); 11953f54a085SPoul-Henning Kamp } 1196f4e7c5a8SJaakko Heinonen 11979b00ca19SPoul-Henning Kamp sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO); 1198947fc8deSPoul-Henning Kamp sc->type = type; 11999b00ca19SPoul-Henning Kamp bioq_init(&sc->bio_queue); 12009b00ca19SPoul-Henning Kamp mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF); 120140ea77a0SAlexander Motin mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF); 12023f54a085SPoul-Henning Kamp sc->unit = unit; 1203f43b2bacSPoul-Henning Kamp sprintf(sc->name, "md%d", unit); 12047ee3c044SPawel Jakub Dawidek LIST_INSERT_HEAD(&md_softc_list, sc, list); 12053745c395SJulian Elischer error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name); 12069b00ca19SPoul-Henning Kamp if (error == 0) 12079b00ca19SPoul-Henning Kamp return (sc); 12087ee3c044SPawel Jakub Dawidek LIST_REMOVE(sc, list); 120940ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 12107ee3c044SPawel Jakub Dawidek mtx_destroy(&sc->queue_mtx); 1211f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 12125c97ca54SIan Dowse free(sc, M_MD); 12137ee3c044SPawel Jakub Dawidek *errp = error; 12145c97ca54SIan Dowse return (NULL); 12155c97ca54SIan Dowse } 12168f8def9eSPoul-Henning Kamp 12178f8def9eSPoul-Henning Kamp static void 12188f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc) 12198f8def9eSPoul-Henning Kamp { 12206f4f00f1SPoul-Henning Kamp struct g_geom *gp; 12216f4f00f1SPoul-Henning Kamp struct g_provider *pp; 12226f4f00f1SPoul-Henning Kamp 12236f4f00f1SPoul-Henning Kamp g_topology_lock(); 12246f4f00f1SPoul-Henning Kamp gp = g_new_geomf(&g_md_class, "md%d", sc->unit); 12256f4f00f1SPoul-Henning Kamp gp->softc = sc; 12266f4f00f1SPoul-Henning Kamp pp = g_new_providerf(gp, "md%d", sc->unit); 122740ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 1228b830359bSPawel Jakub Dawidek pp->mediasize = sc->mediasize; 1229b830359bSPawel Jakub Dawidek pp->sectorsize = sc->sectorsize; 12301ef76554SKonstantin Belousov switch (sc->type) { 12311ef76554SKonstantin Belousov case MD_MALLOC: 12321ef76554SKonstantin Belousov case MD_VNODE: 12331ef76554SKonstantin Belousov case MD_SWAP: 123459ec9023SKonstantin Belousov pp->flags |= G_PF_ACCEPT_UNMAPPED; 12351ef76554SKonstantin Belousov break; 12361ef76554SKonstantin Belousov case MD_PRELOAD: 12370efd9bfdSEdward Tomasz Napierala case MD_NULL: 12381ef76554SKonstantin Belousov break; 12391ef76554SKonstantin Belousov } 12406f4f00f1SPoul-Henning Kamp sc->gp = gp; 12416f4f00f1SPoul-Henning Kamp sc->pp = pp; 12426f4f00f1SPoul-Henning Kamp g_error_provider(pp, 0); 12436f4f00f1SPoul-Henning Kamp g_topology_unlock(); 1244a03be42dSMaxim Sobolev sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize, 1245a03be42dSMaxim Sobolev DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 12466f4f00f1SPoul-Henning Kamp } 124771e4fff8SPoul-Henning Kamp 12488f8def9eSPoul-Henning Kamp static int 1249b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio) 125095f1a897SPoul-Henning Kamp { 1251c6517568SPoul-Henning Kamp uintptr_t sp; 1252c6517568SPoul-Henning Kamp int error; 1253b830359bSPawel Jakub Dawidek off_t u; 125495f1a897SPoul-Henning Kamp 1255c6517568SPoul-Henning Kamp error = 0; 12568f8def9eSPoul-Henning Kamp if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 12578f8def9eSPoul-Henning Kamp return (EINVAL); 1258b830359bSPawel Jakub Dawidek if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize)) 1259ebe789d6SPoul-Henning Kamp return (EINVAL); 12608f8def9eSPoul-Henning Kamp /* Compression doesn't make sense if we have reserved space */ 12618f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) 12628f8def9eSPoul-Henning Kamp mdio->md_options &= ~MD_COMPRESS; 12634e8bfe14SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 12644e8bfe14SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 12654e8bfe14SPoul-Henning Kamp if (mdio->md_fwheads != 0) 12664e8bfe14SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 126726a0ee75SDima Dorfman sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE); 1268b830359bSPawel Jakub Dawidek sc->indir = dimension(sc->mediasize / sc->sectorsize); 1269b830359bSPawel Jakub Dawidek sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL, 1270b830359bSPawel Jakub Dawidek 0x1ff, 0); 127196b6a55fSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1272b830359bSPawel Jakub Dawidek off_t nsectors; 1273b830359bSPawel Jakub Dawidek 1274b830359bSPawel Jakub Dawidek nsectors = sc->mediasize / sc->sectorsize; 1275b830359bSPawel Jakub Dawidek for (u = 0; u < nsectors; u++) { 1276007777f1SKonstantin Belousov sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ? 1277007777f1SKonstantin Belousov M_WAITOK : M_NOWAIT) | M_ZERO); 1278c6517568SPoul-Henning Kamp if (sp != 0) 1279fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, u, sp); 1280c6517568SPoul-Henning Kamp else 1281c6517568SPoul-Henning Kamp error = ENOMEM; 1282b830359bSPawel Jakub Dawidek if (error != 0) 1283c6517568SPoul-Henning Kamp break; 12848f8def9eSPoul-Henning Kamp } 1285c6517568SPoul-Henning Kamp } 1286c6517568SPoul-Henning Kamp return (error); 128700a6a3c6SPoul-Henning Kamp } 128800a6a3c6SPoul-Henning Kamp 12893f54a085SPoul-Henning Kamp 12908f8def9eSPoul-Henning Kamp static int 12918f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred) 12928f8def9eSPoul-Henning Kamp { 12938f8def9eSPoul-Henning Kamp char *tmpbuf; 12948f8def9eSPoul-Henning Kamp int error = 0; 12958f8def9eSPoul-Henning Kamp 12963f54a085SPoul-Henning Kamp /* 12978f8def9eSPoul-Henning Kamp * Set credits in our softc 12983f54a085SPoul-Henning Kamp */ 12998f8def9eSPoul-Henning Kamp 13008f8def9eSPoul-Henning Kamp if (sc->cred) 13018f8def9eSPoul-Henning Kamp crfree(sc->cred); 1302bd78ceceSJohn Baldwin sc->cred = crhold(cred); 13038f8def9eSPoul-Henning Kamp 13048f8def9eSPoul-Henning Kamp /* 13058f8def9eSPoul-Henning Kamp * Horrible kludge to establish credentials for NFS XXX. 13068f8def9eSPoul-Henning Kamp */ 13078f8def9eSPoul-Henning Kamp 13088f8def9eSPoul-Henning Kamp if (sc->vnode) { 13098f8def9eSPoul-Henning Kamp struct uio auio; 13108f8def9eSPoul-Henning Kamp struct iovec aiov; 13118f8def9eSPoul-Henning Kamp 1312b830359bSPawel Jakub Dawidek tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK); 13138f8def9eSPoul-Henning Kamp bzero(&auio, sizeof(auio)); 13148f8def9eSPoul-Henning Kamp 13158f8def9eSPoul-Henning Kamp aiov.iov_base = tmpbuf; 1316b830359bSPawel Jakub Dawidek aiov.iov_len = sc->sectorsize; 13178f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 13188f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 13198f8def9eSPoul-Henning Kamp auio.uio_offset = 0; 13208f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_READ; 13218f8def9eSPoul-Henning Kamp auio.uio_segflg = UIO_SYSSPACE; 13228f8def9eSPoul-Henning Kamp auio.uio_resid = aiov.iov_len; 1323cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 13248f8def9eSPoul-Henning Kamp error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 132522db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 13268f8def9eSPoul-Henning Kamp free(tmpbuf, M_TEMP); 13278f8def9eSPoul-Henning Kamp } 13288f8def9eSPoul-Henning Kamp return (error); 13298f8def9eSPoul-Henning Kamp } 13308f8def9eSPoul-Henning Kamp 13318f8def9eSPoul-Henning Kamp static int 1332b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 13338f8def9eSPoul-Henning Kamp { 13348f8def9eSPoul-Henning Kamp struct vattr vattr; 13358f8def9eSPoul-Henning Kamp struct nameidata nd; 13363d5c947dSMarcel Moolenaar char *fname; 13375050aa86SKonstantin Belousov int error, flags; 13388f8def9eSPoul-Henning Kamp 13393d5c947dSMarcel Moolenaar /* 13403d5c947dSMarcel Moolenaar * Kernel-originated requests must have the filename appended 13413d5c947dSMarcel Moolenaar * to the mdio structure to protect against malicious software. 13423d5c947dSMarcel Moolenaar */ 13433d5c947dSMarcel Moolenaar fname = mdio->md_file; 13443d5c947dSMarcel Moolenaar if ((void *)fname != (void *)(mdio + 1)) { 13453d5c947dSMarcel Moolenaar error = copyinstr(fname, sc->file, sizeof(sc->file), NULL); 134688b5b78dSPawel Jakub Dawidek if (error != 0) 134788b5b78dSPawel Jakub Dawidek return (error); 13483d5c947dSMarcel Moolenaar } else 13493d5c947dSMarcel Moolenaar strlcpy(sc->file, fname, sizeof(sc->file)); 13503d5c947dSMarcel Moolenaar 135186776891SChristian S.J. Peron /* 13523d5c947dSMarcel Moolenaar * If the user specified that this is a read only device, don't 13533d5c947dSMarcel Moolenaar * set the FWRITE mask before trying to open the backing store. 135486776891SChristian S.J. Peron */ 13553d5c947dSMarcel Moolenaar flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE); 13565050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td); 13579e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 1358e3ed29a7SPawel Jakub Dawidek if (error != 0) 135952c6716fSPawel Jakub Dawidek return (error); 1360b322d85dSPawel Jakub Dawidek NDFREE(&nd, NDF_ONLY_PNBUF); 136133fc3625SJohn Baldwin if (nd.ni_vp->v_type != VREG) { 136233fc3625SJohn Baldwin error = EINVAL; 136333fc3625SJohn Baldwin goto bad; 136433fc3625SJohn Baldwin } 136533fc3625SJohn Baldwin error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred); 136633fc3625SJohn Baldwin if (error != 0) 136733fc3625SJohn Baldwin goto bad; 136833fc3625SJohn Baldwin if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) { 136933fc3625SJohn Baldwin vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY); 137033fc3625SJohn Baldwin if (nd.ni_vp->v_iflag & VI_DOOMED) { 137133fc3625SJohn Baldwin /* Forced unmount. */ 137233fc3625SJohn Baldwin error = EBADF; 137333fc3625SJohn Baldwin goto bad; 137433fc3625SJohn Baldwin } 13758f8def9eSPoul-Henning Kamp } 13763b7b5496SKonstantin Belousov nd.ni_vp->v_vflag |= VV_MD; 137722db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 13789589c256SPoul-Henning Kamp 1379d5a929dcSPoul-Henning Kamp if (mdio->md_fwsectors != 0) 1380d5a929dcSPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 1381d5a929dcSPoul-Henning Kamp if (mdio->md_fwheads != 0) 1382d5a929dcSPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 13837a6b2b64SPoul-Henning Kamp sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC); 13849589c256SPoul-Henning Kamp if (!(flags & FWRITE)) 13859589c256SPoul-Henning Kamp sc->flags |= MD_READONLY; 13868f8def9eSPoul-Henning Kamp sc->vnode = nd.ni_vp; 13878f8def9eSPoul-Henning Kamp 1388a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 1389b830359bSPawel Jakub Dawidek if (error != 0) { 13903cf74e53SPhilip Paeps sc->vnode = NULL; 1391cb05b60aSAttilio Rao vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY); 13923b7b5496SKonstantin Belousov nd.ni_vp->v_vflag &= ~VV_MD; 139333fc3625SJohn Baldwin goto bad; 139433fc3625SJohn Baldwin } 139533fc3625SJohn Baldwin return (0); 139633fc3625SJohn Baldwin bad: 139722db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 139852c6716fSPawel Jakub Dawidek (void)vn_close(nd.ni_vp, flags, td->td_ucred, td); 13998f8def9eSPoul-Henning Kamp return (error); 14008f8def9eSPoul-Henning Kamp } 14018f8def9eSPoul-Henning Kamp 14028f8def9eSPoul-Henning Kamp static int 1403b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td) 14048f8def9eSPoul-Henning Kamp { 14050cddd8f0SMatthew Dillon 14066f4f00f1SPoul-Henning Kamp if (sc->gp) { 14076f4f00f1SPoul-Henning Kamp sc->gp->softc = NULL; 14089b00ca19SPoul-Henning Kamp g_topology_lock(); 14099b00ca19SPoul-Henning Kamp g_wither_geom(sc->gp, ENXIO); 14109b00ca19SPoul-Henning Kamp g_topology_unlock(); 14116b60a2cdSPoul-Henning Kamp sc->gp = NULL; 14126b60a2cdSPoul-Henning Kamp sc->pp = NULL; 14131f4ee1aaSPoul-Henning Kamp } 1414a03be42dSMaxim Sobolev if (sc->devstat) { 1415a03be42dSMaxim Sobolev devstat_remove_entry(sc->devstat); 1416a03be42dSMaxim Sobolev sc->devstat = NULL; 1417a03be42dSMaxim Sobolev } 1418a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 14195c97ca54SIan Dowse sc->flags |= MD_SHUTDOWN; 14205c97ca54SIan Dowse wakeup(sc); 1421a08d2e7fSJohn Baldwin while (!(sc->flags & MD_EXITING)) 1422a08d2e7fSJohn Baldwin msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10); 1423a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 142440ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 14259fbea3e3SPoul-Henning Kamp mtx_destroy(&sc->queue_mtx); 14269b00ca19SPoul-Henning Kamp if (sc->vnode != NULL) { 1427cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 14283b7b5496SKonstantin Belousov sc->vnode->v_vflag &= ~VV_MD; 142922db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 14309d4b5945SMaxim Sobolev (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? 1431b40ce416SJulian Elischer FREAD : (FREAD|FWRITE), sc->cred, td); 14329b00ca19SPoul-Henning Kamp } 14338f8def9eSPoul-Henning Kamp if (sc->cred != NULL) 14348f8def9eSPoul-Henning Kamp crfree(sc->cred); 14351db17c6dSPawel Jakub Dawidek if (sc->object != NULL) 1436f820bc50SAlan Cox vm_object_deallocate(sc->object); 1437f43b2bacSPoul-Henning Kamp if (sc->indir) 1438f43b2bacSPoul-Henning Kamp destroy_indir(sc, sc->indir); 1439f43b2bacSPoul-Henning Kamp if (sc->uma) 1440f43b2bacSPoul-Henning Kamp uma_zdestroy(sc->uma); 14411f4ee1aaSPoul-Henning Kamp 14421f4ee1aaSPoul-Henning Kamp LIST_REMOVE(sc, list); 1443f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 1444c6517568SPoul-Henning Kamp free(sc, M_MD); 14458f8def9eSPoul-Henning Kamp return (0); 14468f8def9eSPoul-Henning Kamp } 14478f8def9eSPoul-Henning Kamp 14488f8def9eSPoul-Henning Kamp static int 1449dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio) 1450dc604f0cSEdward Tomasz Napierala { 1451dc604f0cSEdward Tomasz Napierala int error, res; 1452dc604f0cSEdward Tomasz Napierala vm_pindex_t oldpages, newpages; 1453dc604f0cSEdward Tomasz Napierala 1454dc604f0cSEdward Tomasz Napierala switch (sc->type) { 1455dc604f0cSEdward Tomasz Napierala case MD_VNODE: 14560efd9bfdSEdward Tomasz Napierala case MD_NULL: 1457dc604f0cSEdward Tomasz Napierala break; 1458dc604f0cSEdward Tomasz Napierala case MD_SWAP: 14598cb51643SJaakko Heinonen if (mdio->md_mediasize <= 0 || 1460dc604f0cSEdward Tomasz Napierala (mdio->md_mediasize % PAGE_SIZE) != 0) 1461dc604f0cSEdward Tomasz Napierala return (EDOM); 1462dc604f0cSEdward Tomasz Napierala oldpages = OFF_TO_IDX(round_page(sc->mediasize)); 1463dc604f0cSEdward Tomasz Napierala newpages = OFF_TO_IDX(round_page(mdio->md_mediasize)); 1464dc604f0cSEdward Tomasz Napierala if (newpages < oldpages) { 146589f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1466dc604f0cSEdward Tomasz Napierala vm_object_page_remove(sc->object, newpages, 0, 0); 1467dc604f0cSEdward Tomasz Napierala swap_pager_freespace(sc->object, newpages, 1468dc604f0cSEdward Tomasz Napierala oldpages - newpages); 1469dc604f0cSEdward Tomasz Napierala swap_release_by_cred(IDX_TO_OFF(oldpages - 1470dc604f0cSEdward Tomasz Napierala newpages), sc->cred); 1471dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1472dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 147389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1474dc604f0cSEdward Tomasz Napierala } else if (newpages > oldpages) { 1475dc604f0cSEdward Tomasz Napierala res = swap_reserve_by_cred(IDX_TO_OFF(newpages - 1476dc604f0cSEdward Tomasz Napierala oldpages), sc->cred); 1477dc604f0cSEdward Tomasz Napierala if (!res) 1478dc604f0cSEdward Tomasz Napierala return (ENOMEM); 1479dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & MD_RESERVE) || 1480dc604f0cSEdward Tomasz Napierala (sc->flags & MD_RESERVE)) { 1481dc604f0cSEdward Tomasz Napierala error = swap_pager_reserve(sc->object, 1482dc604f0cSEdward Tomasz Napierala oldpages, newpages - oldpages); 1483dc604f0cSEdward Tomasz Napierala if (error < 0) { 1484dc604f0cSEdward Tomasz Napierala swap_release_by_cred( 1485dc604f0cSEdward Tomasz Napierala IDX_TO_OFF(newpages - oldpages), 1486dc604f0cSEdward Tomasz Napierala sc->cred); 1487dc604f0cSEdward Tomasz Napierala return (EDOM); 1488dc604f0cSEdward Tomasz Napierala } 1489dc604f0cSEdward Tomasz Napierala } 149089f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1491dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1492dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 149389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1494dc604f0cSEdward Tomasz Napierala } 1495dc604f0cSEdward Tomasz Napierala break; 1496dc604f0cSEdward Tomasz Napierala default: 1497dc604f0cSEdward Tomasz Napierala return (EOPNOTSUPP); 1498dc604f0cSEdward Tomasz Napierala } 1499dc604f0cSEdward Tomasz Napierala 1500dc604f0cSEdward Tomasz Napierala sc->mediasize = mdio->md_mediasize; 1501dc604f0cSEdward Tomasz Napierala g_topology_lock(); 1502dc604f0cSEdward Tomasz Napierala g_resize_provider(sc->pp, sc->mediasize); 1503dc604f0cSEdward Tomasz Napierala g_topology_unlock(); 1504dc604f0cSEdward Tomasz Napierala return (0); 1505dc604f0cSEdward Tomasz Napierala } 1506dc604f0cSEdward Tomasz Napierala 1507dc604f0cSEdward Tomasz Napierala static int 1508b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 15098f8def9eSPoul-Henning Kamp { 1510fcd57fbeSPawel Jakub Dawidek vm_ooffset_t npage; 1511fcd57fbeSPawel Jakub Dawidek int error; 15128f8def9eSPoul-Henning Kamp 15138f8def9eSPoul-Henning Kamp /* 1514bc2308d4SEdward Tomasz Napierala * Range check. Disallow negative sizes and sizes not being 1515bc2308d4SEdward Tomasz Napierala * multiple of page size. 15168f8def9eSPoul-Henning Kamp */ 15178cb51643SJaakko Heinonen if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 15188f8def9eSPoul-Henning Kamp return (EDOM); 15198f8def9eSPoul-Henning Kamp 15208f8def9eSPoul-Henning Kamp /* 15218f8def9eSPoul-Henning Kamp * Allocate an OBJT_SWAP object. 15228f8def9eSPoul-Henning Kamp * 15238f8def9eSPoul-Henning Kamp * Note the truncation. 15248f8def9eSPoul-Henning Kamp */ 15258f8def9eSPoul-Henning Kamp 1526b830359bSPawel Jakub Dawidek npage = mdio->md_mediasize / PAGE_SIZE; 15279ed40643SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 15289ed40643SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 15299ed40643SPoul-Henning Kamp if (mdio->md_fwheads != 0) 15309ed40643SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 1531fcd57fbeSPawel Jakub Dawidek sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage, 15323364c323SKonstantin Belousov VM_PROT_DEFAULT, 0, td->td_ucred); 1533812851b6SBrian Feldman if (sc->object == NULL) 1534812851b6SBrian Feldman return (ENOMEM); 1535dc604f0cSEdward Tomasz Napierala sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE); 15368f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1537fcd57fbeSPawel Jakub Dawidek if (swap_pager_reserve(sc->object, 0, npage) < 0) { 15383364c323SKonstantin Belousov error = EDOM; 15393364c323SKonstantin Belousov goto finish; 15408f8def9eSPoul-Henning Kamp } 15418f8def9eSPoul-Henning Kamp } 1542a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 15433364c323SKonstantin Belousov finish: 1544e3ed29a7SPawel Jakub Dawidek if (error != 0) { 15452eafd8b1SPawel Jakub Dawidek vm_object_deallocate(sc->object); 15462eafd8b1SPawel Jakub Dawidek sc->object = NULL; 15478f8def9eSPoul-Henning Kamp } 1548b830359bSPawel Jakub Dawidek return (error); 1549b3b3d1b7SPoul-Henning Kamp } 15508f8def9eSPoul-Henning Kamp 15510efd9bfdSEdward Tomasz Napierala static int 15520efd9bfdSEdward Tomasz Napierala mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 15530efd9bfdSEdward Tomasz Napierala { 15540efd9bfdSEdward Tomasz Napierala 15550efd9bfdSEdward Tomasz Napierala /* 1556bc2308d4SEdward Tomasz Napierala * Range check. Disallow negative sizes and sizes not being 1557bc2308d4SEdward Tomasz Napierala * multiple of page size. 15580efd9bfdSEdward Tomasz Napierala */ 15590efd9bfdSEdward Tomasz Napierala if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 15600efd9bfdSEdward Tomasz Napierala return (EDOM); 15610efd9bfdSEdward Tomasz Napierala 15620efd9bfdSEdward Tomasz Napierala return (0); 15630efd9bfdSEdward Tomasz Napierala } 15649d4b5945SMaxim Sobolev 15659d4b5945SMaxim Sobolev static int 15669b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 15678f8def9eSPoul-Henning Kamp { 15688f8def9eSPoul-Henning Kamp struct md_ioctl *mdio; 15698f8def9eSPoul-Henning Kamp struct md_s *sc; 1570b830359bSPawel Jakub Dawidek int error, i; 15718cb51643SJaakko Heinonen unsigned sectsize; 15728f8def9eSPoul-Henning Kamp 15738f8def9eSPoul-Henning Kamp if (md_debug) 15748f8def9eSPoul-Henning Kamp printf("mdctlioctl(%s %lx %p %x %p)\n", 1575b40ce416SJulian Elischer devtoname(dev), cmd, addr, flags, td); 15768f8def9eSPoul-Henning Kamp 15779b00ca19SPoul-Henning Kamp mdio = (struct md_ioctl *)addr; 15789b00ca19SPoul-Henning Kamp if (mdio->md_version != MDIOVERSION) 15799b00ca19SPoul-Henning Kamp return (EINVAL); 15809b00ca19SPoul-Henning Kamp 158153d745bcSDima Dorfman /* 158253d745bcSDima Dorfman * We assert the version number in the individual ioctl 158353d745bcSDima Dorfman * handlers instead of out here because (a) it is possible we 158453d745bcSDima Dorfman * may add another ioctl in the future which doesn't read an 158553d745bcSDima Dorfman * mdio, and (b) the correct return value for an unknown ioctl 158653d745bcSDima Dorfman * is ENOIOCTL, not EINVAL. 158753d745bcSDima Dorfman */ 15889b00ca19SPoul-Henning Kamp error = 0; 15898f8def9eSPoul-Henning Kamp switch (cmd) { 15908f8def9eSPoul-Henning Kamp case MDIOCATTACH: 15918f8def9eSPoul-Henning Kamp switch (mdio->md_type) { 15928f8def9eSPoul-Henning Kamp case MD_MALLOC: 15938f8def9eSPoul-Henning Kamp case MD_PRELOAD: 15948f8def9eSPoul-Henning Kamp case MD_VNODE: 15958f8def9eSPoul-Henning Kamp case MD_SWAP: 15960efd9bfdSEdward Tomasz Napierala case MD_NULL: 1597b830359bSPawel Jakub Dawidek break; 15988f8def9eSPoul-Henning Kamp default: 15998f8def9eSPoul-Henning Kamp return (EINVAL); 16008f8def9eSPoul-Henning Kamp } 16018cb51643SJaakko Heinonen if (mdio->md_sectorsize == 0) 16028cb51643SJaakko Heinonen sectsize = DEV_BSIZE; 16038cb51643SJaakko Heinonen else 16048cb51643SJaakko Heinonen sectsize = mdio->md_sectorsize; 16058cb51643SJaakko Heinonen if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize) 16068cb51643SJaakko Heinonen return (EINVAL); 16077ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 1608947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, mdio->md_type); 1609f4e7c5a8SJaakko Heinonen else { 1610f4e7c5a8SJaakko Heinonen if (mdio->md_unit > INT_MAX) 1611f4e7c5a8SJaakko Heinonen return (EINVAL); 1612947fc8deSPoul-Henning Kamp sc = mdnew(mdio->md_unit, &error, mdio->md_type); 1613f4e7c5a8SJaakko Heinonen } 1614b830359bSPawel Jakub Dawidek if (sc == NULL) 16157ee3c044SPawel Jakub Dawidek return (error); 16167ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 16177ee3c044SPawel Jakub Dawidek mdio->md_unit = sc->unit; 1618b830359bSPawel Jakub Dawidek sc->mediasize = mdio->md_mediasize; 16198cb51643SJaakko Heinonen sc->sectorsize = sectsize; 1620b830359bSPawel Jakub Dawidek error = EDOOFUS; 1621b830359bSPawel Jakub Dawidek switch (sc->type) { 1622b830359bSPawel Jakub Dawidek case MD_MALLOC: 16239b00ca19SPoul-Henning Kamp sc->start = mdstart_malloc; 1624b830359bSPawel Jakub Dawidek error = mdcreate_malloc(sc, mdio); 1625b830359bSPawel Jakub Dawidek break; 1626b830359bSPawel Jakub Dawidek case MD_PRELOAD: 1627734e78dfSJaakko Heinonen /* 1628734e78dfSJaakko Heinonen * We disallow attaching preloaded memory disks via 1629734e78dfSJaakko Heinonen * ioctl. Preloaded memory disks are automatically 1630734e78dfSJaakko Heinonen * attached in g_md_init(). 1631734e78dfSJaakko Heinonen */ 1632734e78dfSJaakko Heinonen error = EOPNOTSUPP; 1633b830359bSPawel Jakub Dawidek break; 1634b830359bSPawel Jakub Dawidek case MD_VNODE: 16359b00ca19SPoul-Henning Kamp sc->start = mdstart_vnode; 1636b830359bSPawel Jakub Dawidek error = mdcreate_vnode(sc, mdio, td); 1637b830359bSPawel Jakub Dawidek break; 1638b830359bSPawel Jakub Dawidek case MD_SWAP: 16399b00ca19SPoul-Henning Kamp sc->start = mdstart_swap; 1640b830359bSPawel Jakub Dawidek error = mdcreate_swap(sc, mdio, td); 1641b830359bSPawel Jakub Dawidek break; 16420efd9bfdSEdward Tomasz Napierala case MD_NULL: 16430efd9bfdSEdward Tomasz Napierala sc->start = mdstart_null; 16440efd9bfdSEdward Tomasz Napierala error = mdcreate_null(sc, mdio, td); 16450efd9bfdSEdward Tomasz Napierala break; 1646b830359bSPawel Jakub Dawidek } 1647b830359bSPawel Jakub Dawidek if (error != 0) { 1648b830359bSPawel Jakub Dawidek mddestroy(sc, td); 1649b830359bSPawel Jakub Dawidek return (error); 1650b830359bSPawel Jakub Dawidek } 16519b00ca19SPoul-Henning Kamp 16529b00ca19SPoul-Henning Kamp /* Prune off any residual fractional sector */ 16539b00ca19SPoul-Henning Kamp i = sc->mediasize % sc->sectorsize; 16549b00ca19SPoul-Henning Kamp sc->mediasize -= i; 16559b00ca19SPoul-Henning Kamp 1656b830359bSPawel Jakub Dawidek mdinit(sc); 1657b830359bSPawel Jakub Dawidek return (0); 16588f8def9eSPoul-Henning Kamp case MDIOCDETACH: 1659a9ebb311SEdward Tomasz Napierala if (mdio->md_mediasize != 0 || 1660a9ebb311SEdward Tomasz Napierala (mdio->md_options & ~MD_FORCE) != 0) 16618f8def9eSPoul-Henning Kamp return (EINVAL); 16629b00ca19SPoul-Henning Kamp 16639b00ca19SPoul-Henning Kamp sc = mdfind(mdio->md_unit); 16649b00ca19SPoul-Henning Kamp if (sc == NULL) 16659b00ca19SPoul-Henning Kamp return (ENOENT); 1666a9ebb311SEdward Tomasz Napierala if (sc->opencount != 0 && !(sc->flags & MD_FORCE) && 1667a9ebb311SEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 16689b00ca19SPoul-Henning Kamp return (EBUSY); 16699b00ca19SPoul-Henning Kamp return (mddestroy(sc, td)); 1670dc604f0cSEdward Tomasz Napierala case MDIOCRESIZE: 1671dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0) 1672dc604f0cSEdward Tomasz Napierala return (EINVAL); 1673dc604f0cSEdward Tomasz Napierala 1674dc604f0cSEdward Tomasz Napierala sc = mdfind(mdio->md_unit); 1675dc604f0cSEdward Tomasz Napierala if (sc == NULL) 1676dc604f0cSEdward Tomasz Napierala return (ENOENT); 16778cb51643SJaakko Heinonen if (mdio->md_mediasize < sc->sectorsize) 16788cb51643SJaakko Heinonen return (EINVAL); 1679dc604f0cSEdward Tomasz Napierala if (mdio->md_mediasize < sc->mediasize && 1680dc604f0cSEdward Tomasz Napierala !(sc->flags & MD_FORCE) && 1681dc604f0cSEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 1682dc604f0cSEdward Tomasz Napierala return (EBUSY); 1683dc604f0cSEdward Tomasz Napierala return (mdresize(sc, mdio)); 1684174b5e9aSPoul-Henning Kamp case MDIOCQUERY: 1685174b5e9aSPoul-Henning Kamp sc = mdfind(mdio->md_unit); 1686174b5e9aSPoul-Henning Kamp if (sc == NULL) 1687174b5e9aSPoul-Henning Kamp return (ENOENT); 1688174b5e9aSPoul-Henning Kamp mdio->md_type = sc->type; 1689174b5e9aSPoul-Henning Kamp mdio->md_options = sc->flags; 1690b830359bSPawel Jakub Dawidek mdio->md_mediasize = sc->mediasize; 1691b830359bSPawel Jakub Dawidek mdio->md_sectorsize = sc->sectorsize; 1692d2e63913SStephen J. Kiernan if (sc->type == MD_VNODE || 1693d2e63913SStephen J. Kiernan (sc->type == MD_PRELOAD && mdio->md_file != NULL)) 169488b5b78dSPawel Jakub Dawidek error = copyout(sc->file, mdio->md_file, 169588b5b78dSPawel Jakub Dawidek strlen(sc->file) + 1); 169688b5b78dSPawel Jakub Dawidek return (error); 169716bcbe8cSPoul-Henning Kamp case MDIOCLIST: 169816bcbe8cSPoul-Henning Kamp i = 1; 169916bcbe8cSPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 170016bcbe8cSPoul-Henning Kamp if (i == MDNPAD - 1) 170116bcbe8cSPoul-Henning Kamp mdio->md_pad[i] = -1; 170216bcbe8cSPoul-Henning Kamp else 170316bcbe8cSPoul-Henning Kamp mdio->md_pad[i++] = sc->unit; 170416bcbe8cSPoul-Henning Kamp } 170516bcbe8cSPoul-Henning Kamp mdio->md_pad[0] = i - 1; 170616bcbe8cSPoul-Henning Kamp return (0); 17078f8def9eSPoul-Henning Kamp default: 17088f8def9eSPoul-Henning Kamp return (ENOIOCTL); 17098f8def9eSPoul-Henning Kamp }; 17109b00ca19SPoul-Henning Kamp } 17119b00ca19SPoul-Henning Kamp 17129b00ca19SPoul-Henning Kamp static int 17139b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 17149b00ca19SPoul-Henning Kamp { 17159b00ca19SPoul-Henning Kamp int error; 17169b00ca19SPoul-Henning Kamp 17179b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 17189b00ca19SPoul-Henning Kamp error = xmdctlioctl(dev, cmd, addr, flags, td); 17199b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 17209b00ca19SPoul-Henning Kamp return (error); 17213f54a085SPoul-Henning Kamp } 17223f54a085SPoul-Henning Kamp 172300a6a3c6SPoul-Henning Kamp static void 1724341b240dSJaakko Heinonen md_preloaded(u_char *image, size_t length, const char *name) 1725637f671aSPoul-Henning Kamp { 1726637f671aSPoul-Henning Kamp struct md_s *sc; 17279b00ca19SPoul-Henning Kamp int error; 1728637f671aSPoul-Henning Kamp 1729947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, MD_PRELOAD); 1730637f671aSPoul-Henning Kamp if (sc == NULL) 1731637f671aSPoul-Henning Kamp return; 1732b830359bSPawel Jakub Dawidek sc->mediasize = length; 1733b830359bSPawel Jakub Dawidek sc->sectorsize = DEV_BSIZE; 1734637f671aSPoul-Henning Kamp sc->pl_ptr = image; 1735637f671aSPoul-Henning Kamp sc->pl_len = length; 17369b00ca19SPoul-Henning Kamp sc->start = mdstart_preload; 1737d2e63913SStephen J. Kiernan if (name != NULL) 1738d2e63913SStephen J. Kiernan strlcpy(sc->file, name, sizeof(sc->file)); 1739c3d1c73fSMaxim Sobolev #if defined(MD_ROOT) && !defined(ROOTDEVNAME) 1740637f671aSPoul-Henning Kamp if (sc->unit == 0) 174122ff74b2SMarcel Moolenaar rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0"; 17425d4ca75eSLuigi Rizzo #endif 1743637f671aSPoul-Henning Kamp mdinit(sc); 1744341b240dSJaakko Heinonen if (name != NULL) { 1745341b240dSJaakko Heinonen printf("%s%d: Preloaded image <%s> %zd bytes at %p\n", 1746341b240dSJaakko Heinonen MD_NAME, sc->unit, name, length, image); 1747cc787e3dSMarcel Moolenaar } else { 1748c68ea8a6SMarcel Moolenaar printf("%s%d: Embedded image %zd bytes at %p\n", 1749cc787e3dSMarcel Moolenaar MD_NAME, sc->unit, length, image); 1750341b240dSJaakko Heinonen } 1751637f671aSPoul-Henning Kamp } 1752637f671aSPoul-Henning Kamp 1753637f671aSPoul-Henning Kamp static void 175419945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused) 175500a6a3c6SPoul-Henning Kamp { 175695f1a897SPoul-Henning Kamp caddr_t mod; 175795f1a897SPoul-Henning Kamp u_char *ptr, *name, *type; 175895f1a897SPoul-Henning Kamp unsigned len; 1759d12fc952SKonstantin Belousov int i; 1760d12fc952SKonstantin Belousov 1761d12fc952SKonstantin Belousov /* figure out log2(NINDIR) */ 1762d12fc952SKonstantin Belousov for (i = NINDIR, nshift = -1; i; nshift++) 1763d12fc952SKonstantin Belousov i >>= 1; 176495f1a897SPoul-Henning Kamp 17650a937206SPoul-Henning Kamp mod = NULL; 17669b00ca19SPoul-Henning Kamp sx_init(&md_sx, "MD config lock"); 17670a937206SPoul-Henning Kamp g_topology_unlock(); 1768f4e7c5a8SJaakko Heinonen md_uh = new_unrhdr(0, INT_MAX, NULL); 1769cc787e3dSMarcel Moolenaar #ifdef MD_ROOT 1770cc787e3dSMarcel Moolenaar if (mfs_root_size != 0) { 17719b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1772cc787e3dSMarcel Moolenaar md_preloaded(__DEVOLATILE(u_char *, &mfs_root), mfs_root_size, 1773cc787e3dSMarcel Moolenaar NULL); 17749b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 1775cc787e3dSMarcel Moolenaar } 177671e4fff8SPoul-Henning Kamp #endif 17779b00ca19SPoul-Henning Kamp /* XXX: are preload_* static or do they need Giant ? */ 177895f1a897SPoul-Henning Kamp while ((mod = preload_search_next_name(mod)) != NULL) { 177995f1a897SPoul-Henning Kamp name = (char *)preload_search_info(mod, MODINFO_NAME); 178095f1a897SPoul-Henning Kamp if (name == NULL) 178195f1a897SPoul-Henning Kamp continue; 17829b00ca19SPoul-Henning Kamp type = (char *)preload_search_info(mod, MODINFO_TYPE); 178395f1a897SPoul-Henning Kamp if (type == NULL) 178495f1a897SPoul-Henning Kamp continue; 178571e4fff8SPoul-Henning Kamp if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 178695f1a897SPoul-Henning Kamp continue; 17878d5ac6c3SMarcel Moolenaar ptr = preload_fetch_addr(mod); 17888d5ac6c3SMarcel Moolenaar len = preload_fetch_size(mod); 17898d5ac6c3SMarcel Moolenaar if (ptr != NULL && len != 0) { 17909b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1791341b240dSJaakko Heinonen md_preloaded(ptr, len, name); 17929b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 179395f1a897SPoul-Henning Kamp } 17948d5ac6c3SMarcel Moolenaar } 179559ec9023SKonstantin Belousov md_vnode_pbuf_freecnt = nswbuf / 10; 179606d425f9SEd Schouten status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL, 179710b0e058SDima Dorfman 0600, MDCTL_NAME); 17980eb14309SPoul-Henning Kamp g_topology_lock(); 179900a6a3c6SPoul-Henning Kamp } 180000a6a3c6SPoul-Henning Kamp 180119945697SPoul-Henning Kamp static void 1802c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1803c27a8954SWojciech A. Koszek struct g_consumer *cp __unused, struct g_provider *pp) 1804c27a8954SWojciech A. Koszek { 1805c27a8954SWojciech A. Koszek struct md_s *mp; 1806c27a8954SWojciech A. Koszek char *type; 1807c27a8954SWojciech A. Koszek 1808c27a8954SWojciech A. Koszek mp = gp->softc; 1809c27a8954SWojciech A. Koszek if (mp == NULL) 1810c27a8954SWojciech A. Koszek return; 1811c27a8954SWojciech A. Koszek 1812c27a8954SWojciech A. Koszek switch (mp->type) { 1813c27a8954SWojciech A. Koszek case MD_MALLOC: 1814c27a8954SWojciech A. Koszek type = "malloc"; 1815c27a8954SWojciech A. Koszek break; 1816c27a8954SWojciech A. Koszek case MD_PRELOAD: 1817c27a8954SWojciech A. Koszek type = "preload"; 1818c27a8954SWojciech A. Koszek break; 1819c27a8954SWojciech A. Koszek case MD_VNODE: 1820c27a8954SWojciech A. Koszek type = "vnode"; 1821c27a8954SWojciech A. Koszek break; 1822c27a8954SWojciech A. Koszek case MD_SWAP: 1823c27a8954SWojciech A. Koszek type = "swap"; 1824c27a8954SWojciech A. Koszek break; 18250efd9bfdSEdward Tomasz Napierala case MD_NULL: 18260efd9bfdSEdward Tomasz Napierala type = "null"; 18270efd9bfdSEdward Tomasz Napierala break; 1828c27a8954SWojciech A. Koszek default: 1829c27a8954SWojciech A. Koszek type = "unknown"; 1830c27a8954SWojciech A. Koszek break; 1831c27a8954SWojciech A. Koszek } 1832c27a8954SWojciech A. Koszek 1833c27a8954SWojciech A. Koszek if (pp != NULL) { 1834c27a8954SWojciech A. Koszek if (indent == NULL) { 1835c27a8954SWojciech A. Koszek sbuf_printf(sb, " u %d", mp->unit); 1836c27a8954SWojciech A. Koszek sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize); 1837c27a8954SWojciech A. Koszek sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads); 1838c27a8954SWojciech A. Koszek sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors); 1839c27a8954SWojciech A. Koszek sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize); 1840c27a8954SWojciech A. Koszek sbuf_printf(sb, " t %s", type); 1841d2e63913SStephen J. Kiernan if ((mp->type == MD_VNODE && mp->vnode != NULL) || 1842d2e63913SStephen J. Kiernan (mp->type == MD_PRELOAD && mp->file[0] != '\0')) 1843c27a8954SWojciech A. Koszek sbuf_printf(sb, " file %s", mp->file); 1844c27a8954SWojciech A. Koszek } else { 1845c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, 1846c27a8954SWojciech A. Koszek mp->unit); 1847c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n", 1848c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->sectorsize); 1849c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n", 1850c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwheads); 1851c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n", 1852c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwsectors); 1853c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<length>%ju</length>\n", 1854c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->mediasize); 18551f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<compression>%s</compression>\n", indent, 18561f192809SAndrey V. Elsukov (mp->flags & MD_COMPRESS) == 0 ? "off": "on"); 18571f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<access>%s</access>\n", indent, 18581f192809SAndrey V. Elsukov (mp->flags & MD_READONLY) == 0 ? "read-write": 18591f192809SAndrey V. Elsukov "read-only"); 1860c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<type>%s</type>\n", indent, 1861c27a8954SWojciech A. Koszek type); 1862d2e63913SStephen J. Kiernan if ((mp->type == MD_VNODE && mp->vnode != NULL) || 1863e895e7fcSStephen J. Kiernan (mp->type == MD_PRELOAD && mp->file[0] != '\0')) { 1864ec170744SAndrey V. Elsukov sbuf_printf(sb, "%s<file>", indent); 1865ec170744SAndrey V. Elsukov g_conf_printf_escaped(sb, "%s", mp->file); 1866ec170744SAndrey V. Elsukov sbuf_printf(sb, "</file>\n"); 1867ec170744SAndrey V. Elsukov } 1868c27a8954SWojciech A. Koszek } 1869c27a8954SWojciech A. Koszek } 1870c27a8954SWojciech A. Koszek } 1871c27a8954SWojciech A. Koszek 1872c27a8954SWojciech A. Koszek static void 187319945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused) 187457e9624eSPoul-Henning Kamp { 18759d4b5945SMaxim Sobolev 18769b00ca19SPoul-Henning Kamp sx_destroy(&md_sx); 187719945697SPoul-Henning Kamp if (status_dev != NULL) 187857e9624eSPoul-Henning Kamp destroy_dev(status_dev); 1879f4e7c5a8SJaakko Heinonen delete_unrhdr(md_uh); 188057e9624eSPoul-Henning Kamp } 1881