1098ca2bdSWarner Losh /*- 200a6a3c6SPoul-Henning Kamp * ---------------------------------------------------------------------------- 300a6a3c6SPoul-Henning Kamp * "THE BEER-WARE LICENSE" (Revision 42): 400a6a3c6SPoul-Henning Kamp * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 500a6a3c6SPoul-Henning Kamp * can do whatever you want with this stuff. If we meet some day, and you think 600a6a3c6SPoul-Henning Kamp * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 700a6a3c6SPoul-Henning Kamp * ---------------------------------------------------------------------------- 800a6a3c6SPoul-Henning Kamp * 900a6a3c6SPoul-Henning Kamp * $FreeBSD$ 1000a6a3c6SPoul-Henning Kamp * 1100a6a3c6SPoul-Henning Kamp */ 1200a6a3c6SPoul-Henning Kamp 13098ca2bdSWarner Losh /*- 14637f671aSPoul-Henning Kamp * The following functions are based in the vn(4) driver: mdstart_swap(), 15637f671aSPoul-Henning Kamp * mdstart_vnode(), mdcreate_swap(), mdcreate_vnode() and mddestroy(), 16637f671aSPoul-Henning Kamp * and as such under the following copyright: 17637f671aSPoul-Henning Kamp * 18637f671aSPoul-Henning Kamp * Copyright (c) 1988 University of Utah. 19637f671aSPoul-Henning Kamp * Copyright (c) 1990, 1993 20637f671aSPoul-Henning Kamp * The Regents of the University of California. All rights reserved. 2159ec9023SKonstantin Belousov * Copyright (c) 2013 The FreeBSD Foundation 2259ec9023SKonstantin Belousov * All rights reserved. 23637f671aSPoul-Henning Kamp * 24ed010cdfSWarner Losh * This code is derived from software contributed to Berkeley by 25ed010cdfSWarner Losh * the Systems Programming Group of the University of Utah Computer 26ed010cdfSWarner Losh * Science Department. 27ed010cdfSWarner Losh * 2859ec9023SKonstantin Belousov * Portions of this software were developed by Konstantin Belousov 2959ec9023SKonstantin Belousov * under sponsorship from the FreeBSD Foundation. 3059ec9023SKonstantin Belousov * 31637f671aSPoul-Henning Kamp * Redistribution and use in source and binary forms, with or without 32637f671aSPoul-Henning Kamp * modification, are permitted provided that the following conditions 33637f671aSPoul-Henning Kamp * are met: 34637f671aSPoul-Henning Kamp * 1. Redistributions of source code must retain the above copyright 35637f671aSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer. 36637f671aSPoul-Henning Kamp * 2. Redistributions in binary form must reproduce the above copyright 37637f671aSPoul-Henning Kamp * notice, this list of conditions and the following disclaimer in the 38637f671aSPoul-Henning Kamp * documentation and/or other materials provided with the distribution. 39637f671aSPoul-Henning Kamp * 4. Neither the name of the University nor the names of its contributors 40637f671aSPoul-Henning Kamp * may be used to endorse or promote products derived from this software 41637f671aSPoul-Henning Kamp * without specific prior written permission. 42637f671aSPoul-Henning Kamp * 43637f671aSPoul-Henning Kamp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 44637f671aSPoul-Henning Kamp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45637f671aSPoul-Henning Kamp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46637f671aSPoul-Henning Kamp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 47637f671aSPoul-Henning Kamp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48637f671aSPoul-Henning Kamp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49637f671aSPoul-Henning Kamp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50637f671aSPoul-Henning Kamp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51637f671aSPoul-Henning Kamp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52637f671aSPoul-Henning Kamp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53637f671aSPoul-Henning Kamp * SUCH DAMAGE. 54637f671aSPoul-Henning Kamp * 55637f671aSPoul-Henning Kamp * from: Utah Hdr: vn.c 1.13 94/04/02 56637f671aSPoul-Henning Kamp * 57637f671aSPoul-Henning Kamp * from: @(#)vn.c 8.6 (Berkeley) 4/1/94 58637f671aSPoul-Henning Kamp * From: src/sys/dev/vn/vn.c,v 1.122 2000/12/16 16:06:03 59637f671aSPoul-Henning Kamp */ 60637f671aSPoul-Henning Kamp 616f4f00f1SPoul-Henning Kamp #include "opt_geom.h" 623f54a085SPoul-Henning Kamp #include "opt_md.h" 6371e4fff8SPoul-Henning Kamp 6400a6a3c6SPoul-Henning Kamp #include <sys/param.h> 6500a6a3c6SPoul-Henning Kamp #include <sys/systm.h> 669626b608SPoul-Henning Kamp #include <sys/bio.h> 6759ec9023SKonstantin Belousov #include <sys/buf.h> 6800a6a3c6SPoul-Henning Kamp #include <sys/conf.h> 69a03be42dSMaxim Sobolev #include <sys/devicestat.h> 708f8def9eSPoul-Henning Kamp #include <sys/fcntl.h> 71fb919e4dSMark Murray #include <sys/kernel.h> 725c97ca54SIan Dowse #include <sys/kthread.h> 7306d425f9SEd Schouten #include <sys/limits.h> 74fb919e4dSMark Murray #include <sys/linker.h> 75fb919e4dSMark Murray #include <sys/lock.h> 76fb919e4dSMark Murray #include <sys/malloc.h> 77fb919e4dSMark Murray #include <sys/mdioctl.h> 78a08d2e7fSJohn Baldwin #include <sys/mount.h> 799dceb26bSJohn Baldwin #include <sys/mutex.h> 809b00ca19SPoul-Henning Kamp #include <sys/sx.h> 81fb919e4dSMark Murray #include <sys/namei.h> 828f8def9eSPoul-Henning Kamp #include <sys/proc.h> 83fb919e4dSMark Murray #include <sys/queue.h> 8489f6b863SAttilio Rao #include <sys/rwlock.h> 85657bd8b1SAndrey V. Elsukov #include <sys/sbuf.h> 8663710c4dSJohn Baldwin #include <sys/sched.h> 877cd53fddSAlan Cox #include <sys/sf_buf.h> 88fb919e4dSMark Murray #include <sys/sysctl.h> 89fb919e4dSMark Murray #include <sys/vnode.h> 90fb919e4dSMark Murray 916f4f00f1SPoul-Henning Kamp #include <geom/geom.h> 926f4f00f1SPoul-Henning Kamp 938f8def9eSPoul-Henning Kamp #include <vm/vm.h> 941c771f92SKonstantin Belousov #include <vm/vm_param.h> 958f8def9eSPoul-Henning Kamp #include <vm/vm_object.h> 968f8def9eSPoul-Henning Kamp #include <vm/vm_page.h> 978f8def9eSPoul-Henning Kamp #include <vm/vm_pager.h> 988f8def9eSPoul-Henning Kamp #include <vm/swap_pager.h> 99f43b2bacSPoul-Henning Kamp #include <vm/uma.h> 1003f54a085SPoul-Henning Kamp 10157e9624eSPoul-Henning Kamp #define MD_MODVER 1 10257e9624eSPoul-Henning Kamp 1035c97ca54SIan Dowse #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */ 104a08d2e7fSJohn Baldwin #define MD_EXITING 0x20000 /* Worker thread is exiting. */ 1055c97ca54SIan Dowse 106f2744793SSheldon Hearn #ifndef MD_NSECT 107f2744793SSheldon Hearn #define MD_NSECT (10000 * 2) 10833edfabeSPoul-Henning Kamp #endif 10933edfabeSPoul-Henning Kamp 1105bb84bc8SRobert Watson static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk"); 1115bb84bc8SRobert Watson static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors"); 11200a6a3c6SPoul-Henning Kamp 11371e4fff8SPoul-Henning Kamp static int md_debug; 1143eb9ab52SEitan Adler SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, 1153eb9ab52SEitan Adler "Enable md(4) debug messages"); 116c44d423eSKonstantin Belousov static int md_malloc_wait; 1173eb9ab52SEitan Adler SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0, 1183eb9ab52SEitan Adler "Allow malloc to wait for memory allocations"); 11900a6a3c6SPoul-Henning Kamp 12022ff74b2SMarcel Moolenaar #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE) 12122ff74b2SMarcel Moolenaar #define MD_ROOT_FSTYPE "ufs" 12222ff74b2SMarcel Moolenaar #endif 12322ff74b2SMarcel Moolenaar 12471e4fff8SPoul-Henning Kamp #if defined(MD_ROOT) && defined(MD_ROOT_SIZE) 125de64f22aSLuigi Rizzo /* 126de64f22aSLuigi Rizzo * Preloaded image gets put here. 127de64f22aSLuigi Rizzo * Applications that patch the object with the image can determine 128de64f22aSLuigi Rizzo * the size looking at the start and end markers (strings), 129de64f22aSLuigi Rizzo * so we want them contiguous. 130de64f22aSLuigi Rizzo */ 131de64f22aSLuigi Rizzo static struct { 132de64f22aSLuigi Rizzo u_char start[MD_ROOT_SIZE*1024]; 133de64f22aSLuigi Rizzo u_char end[128]; 134de64f22aSLuigi Rizzo } mfs_root = { 135de64f22aSLuigi Rizzo .start = "MFS Filesystem goes here", 136de64f22aSLuigi Rizzo .end = "MFS Filesystem had better STOP here", 137de64f22aSLuigi Rizzo }; 13871e4fff8SPoul-Henning Kamp #endif 13971e4fff8SPoul-Henning Kamp 14019945697SPoul-Henning Kamp static g_init_t g_md_init; 14119945697SPoul-Henning Kamp static g_fini_t g_md_fini; 14219945697SPoul-Henning Kamp static g_start_t g_md_start; 14319945697SPoul-Henning Kamp static g_access_t g_md_access; 144b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent, 145b42f40b8SJaakko Heinonen struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp); 1460eb14309SPoul-Henning Kamp 14789c9c53dSPoul-Henning Kamp static struct cdev *status_dev = 0; 1489b00ca19SPoul-Henning Kamp static struct sx md_sx; 149f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh; 15057e9624eSPoul-Henning Kamp 151a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl; 1528f8def9eSPoul-Henning Kamp 1538f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = { 154dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 1557ac40f5fSPoul-Henning Kamp .d_ioctl = mdctlioctl, 1567ac40f5fSPoul-Henning Kamp .d_name = MD_NAME, 15700a6a3c6SPoul-Henning Kamp }; 15800a6a3c6SPoul-Henning Kamp 15919945697SPoul-Henning Kamp struct g_class g_md_class = { 16019945697SPoul-Henning Kamp .name = "MD", 1615721c9c7SPoul-Henning Kamp .version = G_VERSION, 16219945697SPoul-Henning Kamp .init = g_md_init, 16319945697SPoul-Henning Kamp .fini = g_md_fini, 16419945697SPoul-Henning Kamp .start = g_md_start, 16519945697SPoul-Henning Kamp .access = g_md_access, 166c27a8954SWojciech A. Koszek .dumpconf = g_md_dumpconf, 16719945697SPoul-Henning Kamp }; 16819945697SPoul-Henning Kamp 16919945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md); 17019945697SPoul-Henning Kamp 1710cfaeeeeSPoul-Henning Kamp 17213e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list); 1733f54a085SPoul-Henning Kamp 174c6517568SPoul-Henning Kamp #define NINDIR (PAGE_SIZE / sizeof(uintptr_t)) 175c6517568SPoul-Henning Kamp #define NMASK (NINDIR-1) 176c6517568SPoul-Henning Kamp static int nshift; 177c6517568SPoul-Henning Kamp 17859ec9023SKonstantin Belousov static int md_vnode_pbuf_freecnt; 17959ec9023SKonstantin Belousov 180c6517568SPoul-Henning Kamp struct indir { 181c6517568SPoul-Henning Kamp uintptr_t *array; 1828b149b51SJohn Baldwin u_int total; 1838b149b51SJohn Baldwin u_int used; 1848b149b51SJohn Baldwin u_int shift; 185c6517568SPoul-Henning Kamp }; 186c6517568SPoul-Henning Kamp 18700a6a3c6SPoul-Henning Kamp struct md_s { 18800a6a3c6SPoul-Henning Kamp int unit; 1893f54a085SPoul-Henning Kamp LIST_ENTRY(md_s) list; 1908177437dSPoul-Henning Kamp struct bio_queue_head bio_queue; 1910f8500a5SPoul-Henning Kamp struct mtx queue_mtx; 19240ea77a0SAlexander Motin struct mtx stat_mtx; 19389c9c53dSPoul-Henning Kamp struct cdev *dev; 1948f8def9eSPoul-Henning Kamp enum md_types type; 195b830359bSPawel Jakub Dawidek off_t mediasize; 196b830359bSPawel Jakub Dawidek unsigned sectorsize; 197fe603109SMaxim Sobolev unsigned opencount; 1984e8bfe14SPoul-Henning Kamp unsigned fwheads; 1994e8bfe14SPoul-Henning Kamp unsigned fwsectors; 2008f8def9eSPoul-Henning Kamp unsigned flags; 201f43b2bacSPoul-Henning Kamp char name[20]; 2025c97ca54SIan Dowse struct proc *procp; 2036f4f00f1SPoul-Henning Kamp struct g_geom *gp; 2046f4f00f1SPoul-Henning Kamp struct g_provider *pp; 2059b00ca19SPoul-Henning Kamp int (*start)(struct md_s *sc, struct bio *bp); 206a03be42dSMaxim Sobolev struct devstat *devstat; 20795f1a897SPoul-Henning Kamp 20895f1a897SPoul-Henning Kamp /* MD_MALLOC related fields */ 209c6517568SPoul-Henning Kamp struct indir *indir; 210f43b2bacSPoul-Henning Kamp uma_zone_t uma; 21100a6a3c6SPoul-Henning Kamp 21295f1a897SPoul-Henning Kamp /* MD_PRELOAD related fields */ 21395f1a897SPoul-Henning Kamp u_char *pl_ptr; 214b830359bSPawel Jakub Dawidek size_t pl_len; 21500a6a3c6SPoul-Henning Kamp 2168f8def9eSPoul-Henning Kamp /* MD_VNODE related fields */ 2178f8def9eSPoul-Henning Kamp struct vnode *vnode; 21861a6eb62SPawel Jakub Dawidek char file[PATH_MAX]; 2198f8def9eSPoul-Henning Kamp struct ucred *cred; 2208f8def9eSPoul-Henning Kamp 221e0cebb40SDima Dorfman /* MD_SWAP related fields */ 2228f8def9eSPoul-Henning Kamp vm_object_t object; 2238f8def9eSPoul-Henning Kamp }; 22400a6a3c6SPoul-Henning Kamp 225c6517568SPoul-Henning Kamp static struct indir * 2268b149b51SJohn Baldwin new_indir(u_int shift) 227c6517568SPoul-Henning Kamp { 228c6517568SPoul-Henning Kamp struct indir *ip; 229c6517568SPoul-Henning Kamp 230c44d423eSKonstantin Belousov ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT) 231c44d423eSKonstantin Belousov | M_ZERO); 232c6517568SPoul-Henning Kamp if (ip == NULL) 233c6517568SPoul-Henning Kamp return (NULL); 234c6517568SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 235c44d423eSKonstantin Belousov M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO); 236c6517568SPoul-Henning Kamp if (ip->array == NULL) { 237c6517568SPoul-Henning Kamp free(ip, M_MD); 238c6517568SPoul-Henning Kamp return (NULL); 239c6517568SPoul-Henning Kamp } 240c6517568SPoul-Henning Kamp ip->total = NINDIR; 241c6517568SPoul-Henning Kamp ip->shift = shift; 242c6517568SPoul-Henning Kamp return (ip); 243c6517568SPoul-Henning Kamp } 244c6517568SPoul-Henning Kamp 245c6517568SPoul-Henning Kamp static void 246c6517568SPoul-Henning Kamp del_indir(struct indir *ip) 247c6517568SPoul-Henning Kamp { 248c6517568SPoul-Henning Kamp 249f43b2bacSPoul-Henning Kamp free(ip->array, M_MDSECT); 250c6517568SPoul-Henning Kamp free(ip, M_MD); 251c6517568SPoul-Henning Kamp } 252c6517568SPoul-Henning Kamp 253f43b2bacSPoul-Henning Kamp static void 254f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip) 255f43b2bacSPoul-Henning Kamp { 256f43b2bacSPoul-Henning Kamp int i; 257f43b2bacSPoul-Henning Kamp 258f43b2bacSPoul-Henning Kamp for (i = 0; i < NINDIR; i++) { 259f43b2bacSPoul-Henning Kamp if (!ip->array[i]) 260f43b2bacSPoul-Henning Kamp continue; 261f43b2bacSPoul-Henning Kamp if (ip->shift) 262f43b2bacSPoul-Henning Kamp destroy_indir(sc, (struct indir*)(ip->array[i])); 263f43b2bacSPoul-Henning Kamp else if (ip->array[i] > 255) 264f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void *)(ip->array[i])); 265f43b2bacSPoul-Henning Kamp } 266f43b2bacSPoul-Henning Kamp del_indir(ip); 267f43b2bacSPoul-Henning Kamp } 268f43b2bacSPoul-Henning Kamp 269c6517568SPoul-Henning Kamp /* 2706c3cd0e2SMaxim Konovalov * This function does the math and allocates the top level "indir" structure 271c6517568SPoul-Henning Kamp * for a device of "size" sectors. 272c6517568SPoul-Henning Kamp */ 273c6517568SPoul-Henning Kamp 274c6517568SPoul-Henning Kamp static struct indir * 275c6517568SPoul-Henning Kamp dimension(off_t size) 276c6517568SPoul-Henning Kamp { 277c6517568SPoul-Henning Kamp off_t rcnt; 278c6517568SPoul-Henning Kamp struct indir *ip; 279d12fc952SKonstantin Belousov int layer; 280c6517568SPoul-Henning Kamp 281c6517568SPoul-Henning Kamp rcnt = size; 282c6517568SPoul-Henning Kamp layer = 0; 283c6517568SPoul-Henning Kamp while (rcnt > NINDIR) { 284c6517568SPoul-Henning Kamp rcnt /= NINDIR; 285c6517568SPoul-Henning Kamp layer++; 286c6517568SPoul-Henning Kamp } 287c6517568SPoul-Henning Kamp 288c6517568SPoul-Henning Kamp /* 289c6517568SPoul-Henning Kamp * XXX: the top layer is probably not fully populated, so we allocate 29083e13864SPoul-Henning Kamp * too much space for ip->array in here. 291c6517568SPoul-Henning Kamp */ 29283e13864SPoul-Henning Kamp ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO); 29383e13864SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 29483e13864SPoul-Henning Kamp M_MDSECT, M_WAITOK | M_ZERO); 29583e13864SPoul-Henning Kamp ip->total = NINDIR; 29683e13864SPoul-Henning Kamp ip->shift = layer * nshift; 297c6517568SPoul-Henning Kamp return (ip); 298c6517568SPoul-Henning Kamp } 299c6517568SPoul-Henning Kamp 300c6517568SPoul-Henning Kamp /* 301c6517568SPoul-Henning Kamp * Read a given sector 302c6517568SPoul-Henning Kamp */ 303c6517568SPoul-Henning Kamp 304c6517568SPoul-Henning Kamp static uintptr_t 305c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset) 306c6517568SPoul-Henning Kamp { 307c6517568SPoul-Henning Kamp struct indir *cip; 308c6517568SPoul-Henning Kamp int idx; 309c6517568SPoul-Henning Kamp uintptr_t up; 310c6517568SPoul-Henning Kamp 311c6517568SPoul-Henning Kamp if (md_debug > 1) 3126569d6f3SMaxime Henrion printf("s_read(%jd)\n", (intmax_t)offset); 313c6517568SPoul-Henning Kamp up = 0; 314c6517568SPoul-Henning Kamp for (cip = ip; cip != NULL;) { 315c6517568SPoul-Henning Kamp if (cip->shift) { 316c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 317c6517568SPoul-Henning Kamp up = cip->array[idx]; 318c6517568SPoul-Henning Kamp cip = (struct indir *)up; 319c6517568SPoul-Henning Kamp continue; 320c6517568SPoul-Henning Kamp } 321c6517568SPoul-Henning Kamp idx = offset & NMASK; 322c6517568SPoul-Henning Kamp return (cip->array[idx]); 323c6517568SPoul-Henning Kamp } 324c6517568SPoul-Henning Kamp return (0); 325c6517568SPoul-Henning Kamp } 326c6517568SPoul-Henning Kamp 327c6517568SPoul-Henning Kamp /* 328c6517568SPoul-Henning Kamp * Write a given sector, prune the tree if the value is 0 329c6517568SPoul-Henning Kamp */ 330c6517568SPoul-Henning Kamp 331c6517568SPoul-Henning Kamp static int 332fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr) 333c6517568SPoul-Henning Kamp { 334c6517568SPoul-Henning Kamp struct indir *cip, *lip[10]; 335c6517568SPoul-Henning Kamp int idx, li; 336c6517568SPoul-Henning Kamp uintptr_t up; 337c6517568SPoul-Henning Kamp 338c6517568SPoul-Henning Kamp if (md_debug > 1) 3396569d6f3SMaxime Henrion printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr); 340c6517568SPoul-Henning Kamp up = 0; 341c6517568SPoul-Henning Kamp li = 0; 342c6517568SPoul-Henning Kamp cip = ip; 343c6517568SPoul-Henning Kamp for (;;) { 344c6517568SPoul-Henning Kamp lip[li++] = cip; 345c6517568SPoul-Henning Kamp if (cip->shift) { 346c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 347c6517568SPoul-Henning Kamp up = cip->array[idx]; 348c6517568SPoul-Henning Kamp if (up != 0) { 349c6517568SPoul-Henning Kamp cip = (struct indir *)up; 350c6517568SPoul-Henning Kamp continue; 351c6517568SPoul-Henning Kamp } 352c6517568SPoul-Henning Kamp /* Allocate branch */ 353c6517568SPoul-Henning Kamp cip->array[idx] = 354c6517568SPoul-Henning Kamp (uintptr_t)new_indir(cip->shift - nshift); 355c6517568SPoul-Henning Kamp if (cip->array[idx] == 0) 356975b628fSPoul-Henning Kamp return (ENOSPC); 357c6517568SPoul-Henning Kamp cip->used++; 358c6517568SPoul-Henning Kamp up = cip->array[idx]; 359c6517568SPoul-Henning Kamp cip = (struct indir *)up; 360c6517568SPoul-Henning Kamp continue; 361c6517568SPoul-Henning Kamp } 362c6517568SPoul-Henning Kamp /* leafnode */ 363c6517568SPoul-Henning Kamp idx = offset & NMASK; 364c6517568SPoul-Henning Kamp up = cip->array[idx]; 365c6517568SPoul-Henning Kamp if (up != 0) 366c6517568SPoul-Henning Kamp cip->used--; 367c6517568SPoul-Henning Kamp cip->array[idx] = ptr; 368c6517568SPoul-Henning Kamp if (ptr != 0) 369c6517568SPoul-Henning Kamp cip->used++; 370c6517568SPoul-Henning Kamp break; 371c6517568SPoul-Henning Kamp } 372c6517568SPoul-Henning Kamp if (cip->used != 0 || li == 1) 373c6517568SPoul-Henning Kamp return (0); 374c6517568SPoul-Henning Kamp li--; 375c6517568SPoul-Henning Kamp while (cip->used == 0 && cip != ip) { 376c6517568SPoul-Henning Kamp li--; 377c6517568SPoul-Henning Kamp idx = (offset >> lip[li]->shift) & NMASK; 378c6517568SPoul-Henning Kamp up = lip[li]->array[idx]; 379c6517568SPoul-Henning Kamp KASSERT(up == (uintptr_t)cip, ("md screwed up")); 380c6517568SPoul-Henning Kamp del_indir(cip); 3814a6a94d8SArchie Cobbs lip[li]->array[idx] = 0; 382c6517568SPoul-Henning Kamp lip[li]->used--; 383c6517568SPoul-Henning Kamp cip = lip[li]; 384c6517568SPoul-Henning Kamp } 385c6517568SPoul-Henning Kamp return (0); 386c6517568SPoul-Henning Kamp } 387c6517568SPoul-Henning Kamp 3886f4f00f1SPoul-Henning Kamp 3896f4f00f1SPoul-Henning Kamp static int 3906f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e) 3916f4f00f1SPoul-Henning Kamp { 3926f4f00f1SPoul-Henning Kamp struct md_s *sc; 3936f4f00f1SPoul-Henning Kamp 3946f4f00f1SPoul-Henning Kamp sc = pp->geom->softc; 39541c8b468SEdward Tomasz Napierala if (sc == NULL) { 39641c8b468SEdward Tomasz Napierala if (r <= 0 && w <= 0 && e <= 0) 39741c8b468SEdward Tomasz Napierala return (0); 3986b60a2cdSPoul-Henning Kamp return (ENXIO); 39941c8b468SEdward Tomasz Napierala } 4006f4f00f1SPoul-Henning Kamp r += pp->acr; 4016f4f00f1SPoul-Henning Kamp w += pp->acw; 4026f4f00f1SPoul-Henning Kamp e += pp->ace; 40386776891SChristian S.J. Peron if ((sc->flags & MD_READONLY) != 0 && w > 0) 40486776891SChristian S.J. Peron return (EROFS); 4056f4f00f1SPoul-Henning Kamp if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 4066f4f00f1SPoul-Henning Kamp sc->opencount = 1; 4076f4f00f1SPoul-Henning Kamp } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 4086f4f00f1SPoul-Henning Kamp sc->opencount = 0; 4096f4f00f1SPoul-Henning Kamp } 4106f4f00f1SPoul-Henning Kamp return (0); 4116f4f00f1SPoul-Henning Kamp } 4126f4f00f1SPoul-Henning Kamp 4136f4f00f1SPoul-Henning Kamp static void 4146f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp) 4156f4f00f1SPoul-Henning Kamp { 4166f4f00f1SPoul-Henning Kamp struct md_s *sc; 4176f4f00f1SPoul-Henning Kamp 4186f4f00f1SPoul-Henning Kamp sc = bp->bio_to->geom->softc; 41940ea77a0SAlexander Motin if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) { 42040ea77a0SAlexander Motin mtx_lock(&sc->stat_mtx); 421a03be42dSMaxim Sobolev devstat_start_transaction_bio(sc->devstat, bp); 42240ea77a0SAlexander Motin mtx_unlock(&sc->stat_mtx); 42340ea77a0SAlexander Motin } 4240f8500a5SPoul-Henning Kamp mtx_lock(&sc->queue_mtx); 425891619a6SPoul-Henning Kamp bioq_disksort(&sc->bio_queue, bp); 4264b07ede4SPawel Jakub Dawidek mtx_unlock(&sc->queue_mtx); 427e4cdd0d4SPawel Jakub Dawidek wakeup(sc); 4286f4f00f1SPoul-Henning Kamp } 4296f4f00f1SPoul-Henning Kamp 43059ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_ZERO 1 43159ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_FILL 2 43259ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_READ 3 43359ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_WRITE 4 43459ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_CMP 5 43559ec9023SKonstantin Belousov 43659ec9023SKonstantin Belousov static int 43759ec9023SKonstantin Belousov md_malloc_move(vm_page_t **mp, int *ma_offs, unsigned sectorsize, 43859ec9023SKonstantin Belousov void *ptr, u_char fill, int op) 43959ec9023SKonstantin Belousov { 44059ec9023SKonstantin Belousov struct sf_buf *sf; 44159ec9023SKonstantin Belousov vm_page_t m, *mp1; 44259ec9023SKonstantin Belousov char *p, first; 44359ec9023SKonstantin Belousov off_t *uc; 44459ec9023SKonstantin Belousov unsigned n; 44559ec9023SKonstantin Belousov int error, i, ma_offs1, sz, first_read; 44659ec9023SKonstantin Belousov 44759ec9023SKonstantin Belousov m = NULL; 44859ec9023SKonstantin Belousov error = 0; 44959ec9023SKonstantin Belousov sf = NULL; 45059ec9023SKonstantin Belousov /* if (op == MD_MALLOC_MOVE_CMP) { gcc */ 45159ec9023SKonstantin Belousov first = 0; 45259ec9023SKonstantin Belousov first_read = 0; 45359ec9023SKonstantin Belousov uc = ptr; 45459ec9023SKonstantin Belousov mp1 = *mp; 45559ec9023SKonstantin Belousov ma_offs1 = *ma_offs; 45659ec9023SKonstantin Belousov /* } */ 45759ec9023SKonstantin Belousov sched_pin(); 45859ec9023SKonstantin Belousov for (n = sectorsize; n != 0; n -= sz) { 45959ec9023SKonstantin Belousov sz = imin(PAGE_SIZE - *ma_offs, n); 46059ec9023SKonstantin Belousov if (m != **mp) { 46159ec9023SKonstantin Belousov if (sf != NULL) 46259ec9023SKonstantin Belousov sf_buf_free(sf); 46359ec9023SKonstantin Belousov m = **mp; 46459ec9023SKonstantin Belousov sf = sf_buf_alloc(m, SFB_CPUPRIVATE | 46559ec9023SKonstantin Belousov (md_malloc_wait ? 0 : SFB_NOWAIT)); 46659ec9023SKonstantin Belousov if (sf == NULL) { 46759ec9023SKonstantin Belousov error = ENOMEM; 46859ec9023SKonstantin Belousov break; 46959ec9023SKonstantin Belousov } 47059ec9023SKonstantin Belousov } 47159ec9023SKonstantin Belousov p = (char *)sf_buf_kva(sf) + *ma_offs; 47259ec9023SKonstantin Belousov switch (op) { 47359ec9023SKonstantin Belousov case MD_MALLOC_MOVE_ZERO: 47459ec9023SKonstantin Belousov bzero(p, sz); 47559ec9023SKonstantin Belousov break; 47659ec9023SKonstantin Belousov case MD_MALLOC_MOVE_FILL: 47759ec9023SKonstantin Belousov memset(p, fill, sz); 47859ec9023SKonstantin Belousov break; 47959ec9023SKonstantin Belousov case MD_MALLOC_MOVE_READ: 48059ec9023SKonstantin Belousov bcopy(ptr, p, sz); 48159ec9023SKonstantin Belousov cpu_flush_dcache(p, sz); 48259ec9023SKonstantin Belousov break; 48359ec9023SKonstantin Belousov case MD_MALLOC_MOVE_WRITE: 48459ec9023SKonstantin Belousov bcopy(p, ptr, sz); 48559ec9023SKonstantin Belousov break; 48659ec9023SKonstantin Belousov case MD_MALLOC_MOVE_CMP: 48759ec9023SKonstantin Belousov for (i = 0; i < sz; i++, p++) { 48859ec9023SKonstantin Belousov if (!first_read) { 48959ec9023SKonstantin Belousov *uc = (u_char)*p; 49059ec9023SKonstantin Belousov first = *p; 49159ec9023SKonstantin Belousov first_read = 1; 49259ec9023SKonstantin Belousov } else if (*p != first) { 49359ec9023SKonstantin Belousov error = EDOOFUS; 49459ec9023SKonstantin Belousov break; 49559ec9023SKonstantin Belousov } 49659ec9023SKonstantin Belousov } 49759ec9023SKonstantin Belousov break; 49859ec9023SKonstantin Belousov default: 49959ec9023SKonstantin Belousov KASSERT(0, ("md_malloc_move unknown op %d\n", op)); 50059ec9023SKonstantin Belousov break; 50159ec9023SKonstantin Belousov } 50259ec9023SKonstantin Belousov if (error != 0) 50359ec9023SKonstantin Belousov break; 50459ec9023SKonstantin Belousov *ma_offs += sz; 50559ec9023SKonstantin Belousov *ma_offs %= PAGE_SIZE; 50659ec9023SKonstantin Belousov if (*ma_offs == 0) 50759ec9023SKonstantin Belousov (*mp)++; 50859ec9023SKonstantin Belousov ptr = (char *)ptr + sz; 50959ec9023SKonstantin Belousov } 51059ec9023SKonstantin Belousov 51159ec9023SKonstantin Belousov if (sf != NULL) 51259ec9023SKonstantin Belousov sf_buf_free(sf); 51359ec9023SKonstantin Belousov sched_unpin(); 51459ec9023SKonstantin Belousov if (op == MD_MALLOC_MOVE_CMP && error != 0) { 51559ec9023SKonstantin Belousov *mp = mp1; 51659ec9023SKonstantin Belousov *ma_offs = ma_offs1; 51759ec9023SKonstantin Belousov } 51859ec9023SKonstantin Belousov return (error); 51959ec9023SKonstantin Belousov } 52059ec9023SKonstantin Belousov 521b4a4f93cSPoul-Henning Kamp static int 522b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp) 52300a6a3c6SPoul-Henning Kamp { 524c6517568SPoul-Henning Kamp u_char *dst; 52559ec9023SKonstantin Belousov vm_page_t *m; 52659ec9023SKonstantin Belousov int i, error, error1, ma_offs, notmapped; 527b830359bSPawel Jakub Dawidek off_t secno, nsec, uc; 528c6517568SPoul-Henning Kamp uintptr_t sp, osp; 52900a6a3c6SPoul-Henning Kamp 5305541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 5315541f25eSPawel Jakub Dawidek case BIO_READ: 5325541f25eSPawel Jakub Dawidek case BIO_WRITE: 5335541f25eSPawel Jakub Dawidek case BIO_DELETE: 5345541f25eSPawel Jakub Dawidek break; 5355541f25eSPawel Jakub Dawidek default: 5365541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 5375541f25eSPawel Jakub Dawidek } 5385541f25eSPawel Jakub Dawidek 53959ec9023SKonstantin Belousov notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0; 54059ec9023SKonstantin Belousov if (notmapped) { 54159ec9023SKonstantin Belousov m = bp->bio_ma; 54259ec9023SKonstantin Belousov ma_offs = bp->bio_ma_offset; 54359ec9023SKonstantin Belousov dst = NULL; 54459ec9023SKonstantin Belousov } else { 54559ec9023SKonstantin Belousov dst = bp->bio_data; 54659ec9023SKonstantin Belousov } 54759ec9023SKonstantin Belousov 548b830359bSPawel Jakub Dawidek nsec = bp->bio_length / sc->sectorsize; 549b830359bSPawel Jakub Dawidek secno = bp->bio_offset / sc->sectorsize; 550c6517568SPoul-Henning Kamp error = 0; 55100a6a3c6SPoul-Henning Kamp while (nsec--) { 552fde2a2e4SPoul-Henning Kamp osp = s_read(sc->indir, secno); 5538177437dSPoul-Henning Kamp if (bp->bio_cmd == BIO_DELETE) { 554fde2a2e4SPoul-Henning Kamp if (osp != 0) 555fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, 0); 5568177437dSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_READ) { 55759ec9023SKonstantin Belousov if (osp == 0) { 55859ec9023SKonstantin Belousov if (notmapped) { 55959ec9023SKonstantin Belousov error = md_malloc_move(&m, &ma_offs, 56059ec9023SKonstantin Belousov sc->sectorsize, NULL, 0, 56159ec9023SKonstantin Belousov MD_MALLOC_MOVE_ZERO); 56259ec9023SKonstantin Belousov } else 563b830359bSPawel Jakub Dawidek bzero(dst, sc->sectorsize); 56459ec9023SKonstantin Belousov } else if (osp <= 255) { 56559ec9023SKonstantin Belousov if (notmapped) { 56659ec9023SKonstantin Belousov error = md_malloc_move(&m, &ma_offs, 56759ec9023SKonstantin Belousov sc->sectorsize, NULL, osp, 56859ec9023SKonstantin Belousov MD_MALLOC_MOVE_FILL); 56959ec9023SKonstantin Belousov } else 570dbb95048SMarcel Moolenaar memset(dst, osp, sc->sectorsize); 57159ec9023SKonstantin Belousov } else { 57259ec9023SKonstantin Belousov if (notmapped) { 57359ec9023SKonstantin Belousov error = md_malloc_move(&m, &ma_offs, 57459ec9023SKonstantin Belousov sc->sectorsize, (void *)osp, 0, 57559ec9023SKonstantin Belousov MD_MALLOC_MOVE_READ); 57659ec9023SKonstantin Belousov } else { 577b830359bSPawel Jakub Dawidek bcopy((void *)osp, dst, sc->sectorsize); 578dbb95048SMarcel Moolenaar cpu_flush_dcache(dst, sc->sectorsize); 579dbb95048SMarcel Moolenaar } 58059ec9023SKonstantin Belousov } 581fde2a2e4SPoul-Henning Kamp osp = 0; 582c6517568SPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 5838f8def9eSPoul-Henning Kamp if (sc->flags & MD_COMPRESS) { 58459ec9023SKonstantin Belousov if (notmapped) { 58559ec9023SKonstantin Belousov error1 = md_malloc_move(&m, &ma_offs, 58659ec9023SKonstantin Belousov sc->sectorsize, &uc, 0, 58759ec9023SKonstantin Belousov MD_MALLOC_MOVE_CMP); 58859ec9023SKonstantin Belousov i = error1 == 0 ? sc->sectorsize : 0; 58959ec9023SKonstantin Belousov } else { 59000a6a3c6SPoul-Henning Kamp uc = dst[0]; 59159ec9023SKonstantin Belousov for (i = 1; i < sc->sectorsize; i++) { 59200a6a3c6SPoul-Henning Kamp if (dst[i] != uc) 59300a6a3c6SPoul-Henning Kamp break; 59459ec9023SKonstantin Belousov } 59559ec9023SKonstantin Belousov } 5968f8def9eSPoul-Henning Kamp } else { 5978f8def9eSPoul-Henning Kamp i = 0; 5988f8def9eSPoul-Henning Kamp uc = 0; 5998f8def9eSPoul-Henning Kamp } 600b830359bSPawel Jakub Dawidek if (i == sc->sectorsize) { 601fde2a2e4SPoul-Henning Kamp if (osp != uc) 602fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, uc); 60300a6a3c6SPoul-Henning Kamp } else { 604fde2a2e4SPoul-Henning Kamp if (osp <= 255) { 605b830359bSPawel Jakub Dawidek sp = (uintptr_t)uma_zalloc(sc->uma, 606c44d423eSKonstantin Belousov md_malloc_wait ? M_WAITOK : 607b830359bSPawel Jakub Dawidek M_NOWAIT); 608c6517568SPoul-Henning Kamp if (sp == 0) { 609fde2a2e4SPoul-Henning Kamp error = ENOSPC; 610fde2a2e4SPoul-Henning Kamp break; 611fde2a2e4SPoul-Henning Kamp } 61259ec9023SKonstantin Belousov if (notmapped) { 61359ec9023SKonstantin Belousov error = md_malloc_move(&m, 61459ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 61559ec9023SKonstantin Belousov (void *)sp, 0, 61659ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 61759ec9023SKonstantin Belousov } else { 61859ec9023SKonstantin Belousov bcopy(dst, (void *)sp, 61959ec9023SKonstantin Belousov sc->sectorsize); 62059ec9023SKonstantin Belousov } 621fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, sp); 622c6517568SPoul-Henning Kamp } else { 62359ec9023SKonstantin Belousov if (notmapped) { 62459ec9023SKonstantin Belousov error = md_malloc_move(&m, 62559ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 62659ec9023SKonstantin Belousov (void *)osp, 0, 62759ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 62859ec9023SKonstantin Belousov } else { 62959ec9023SKonstantin Belousov bcopy(dst, (void *)osp, 63059ec9023SKonstantin Belousov sc->sectorsize); 63159ec9023SKonstantin Belousov } 632fde2a2e4SPoul-Henning Kamp osp = 0; 63300a6a3c6SPoul-Henning Kamp } 63400a6a3c6SPoul-Henning Kamp } 635c6517568SPoul-Henning Kamp } else { 636c6517568SPoul-Henning Kamp error = EOPNOTSUPP; 637c6517568SPoul-Henning Kamp } 638c6517568SPoul-Henning Kamp if (osp > 255) 639f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void*)osp); 640e3ed29a7SPawel Jakub Dawidek if (error != 0) 641c6517568SPoul-Henning Kamp break; 64200a6a3c6SPoul-Henning Kamp secno++; 64359ec9023SKonstantin Belousov if (!notmapped) 644b830359bSPawel Jakub Dawidek dst += sc->sectorsize; 64500a6a3c6SPoul-Henning Kamp } 6468177437dSPoul-Henning Kamp bp->bio_resid = 0; 647c6517568SPoul-Henning Kamp return (error); 64800a6a3c6SPoul-Henning Kamp } 64900a6a3c6SPoul-Henning Kamp 650b4a4f93cSPoul-Henning Kamp static int 651b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp) 65271e4fff8SPoul-Henning Kamp { 65371e4fff8SPoul-Henning Kamp 654a8a58d03SPawel Jakub Dawidek switch (bp->bio_cmd) { 655a8a58d03SPawel Jakub Dawidek case BIO_READ: 656a8a58d03SPawel Jakub Dawidek bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data, 657a8a58d03SPawel Jakub Dawidek bp->bio_length); 658dbb95048SMarcel Moolenaar cpu_flush_dcache(bp->bio_data, bp->bio_length); 659a8a58d03SPawel Jakub Dawidek break; 660a8a58d03SPawel Jakub Dawidek case BIO_WRITE: 661a8a58d03SPawel Jakub Dawidek bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset, 662a8a58d03SPawel Jakub Dawidek bp->bio_length); 663a8a58d03SPawel Jakub Dawidek break; 66471e4fff8SPoul-Henning Kamp } 6658177437dSPoul-Henning Kamp bp->bio_resid = 0; 666b4a4f93cSPoul-Henning Kamp return (0); 66771e4fff8SPoul-Henning Kamp } 66871e4fff8SPoul-Henning Kamp 669b4a4f93cSPoul-Henning Kamp static int 670b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp) 6718f8def9eSPoul-Henning Kamp { 6725050aa86SKonstantin Belousov int error; 6738f8def9eSPoul-Henning Kamp struct uio auio; 6748f8def9eSPoul-Henning Kamp struct iovec aiov; 6758f8def9eSPoul-Henning Kamp struct mount *mp; 6765541f25eSPawel Jakub Dawidek struct vnode *vp; 67759ec9023SKonstantin Belousov struct buf *pb; 6785541f25eSPawel Jakub Dawidek struct thread *td; 6790abd21bdSDag-Erling Smørgrav off_t end, zerosize; 6805541f25eSPawel Jakub Dawidek 6815541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 6825541f25eSPawel Jakub Dawidek case BIO_READ: 6835541f25eSPawel Jakub Dawidek case BIO_WRITE: 6840abd21bdSDag-Erling Smørgrav case BIO_DELETE: 6855541f25eSPawel Jakub Dawidek case BIO_FLUSH: 6865541f25eSPawel Jakub Dawidek break; 6875541f25eSPawel Jakub Dawidek default: 6885541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 6895541f25eSPawel Jakub Dawidek } 6905541f25eSPawel Jakub Dawidek 6915541f25eSPawel Jakub Dawidek td = curthread; 6925541f25eSPawel Jakub Dawidek vp = sc->vnode; 6938f8def9eSPoul-Henning Kamp 6948f8def9eSPoul-Henning Kamp /* 6958f8def9eSPoul-Henning Kamp * VNODE I/O 6968f8def9eSPoul-Henning Kamp * 6978f8def9eSPoul-Henning Kamp * If an error occurs, we set BIO_ERROR but we do not set 6988f8def9eSPoul-Henning Kamp * B_INVAL because (for a write anyway), the buffer is 6998f8def9eSPoul-Henning Kamp * still valid. 7008f8def9eSPoul-Henning Kamp */ 7018f8def9eSPoul-Henning Kamp 7025541f25eSPawel Jakub Dawidek if (bp->bio_cmd == BIO_FLUSH) { 7035541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 704cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7055541f25eSPawel Jakub Dawidek error = VOP_FSYNC(vp, MNT_WAIT, td); 70622db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 7075541f25eSPawel Jakub Dawidek vn_finished_write(mp); 7085541f25eSPawel Jakub Dawidek return (error); 7095541f25eSPawel Jakub Dawidek } 7105541f25eSPawel Jakub Dawidek 7118f8def9eSPoul-Henning Kamp bzero(&auio, sizeof(auio)); 7128f8def9eSPoul-Henning Kamp 7130abd21bdSDag-Erling Smørgrav /* 7140abd21bdSDag-Erling Smørgrav * Special case for BIO_DELETE. On the surface, this is very 7150abd21bdSDag-Erling Smørgrav * similar to BIO_WRITE, except that we write from our own 7160abd21bdSDag-Erling Smørgrav * fixed-length buffer, so we have to loop. The net result is 7170abd21bdSDag-Erling Smørgrav * that the two cases end up having very little in common. 7180abd21bdSDag-Erling Smørgrav */ 7190abd21bdSDag-Erling Smørgrav if (bp->bio_cmd == BIO_DELETE) { 72089cb2a19SMatthew D Fleming zerosize = ZERO_REGION_SIZE - 72189cb2a19SMatthew D Fleming (ZERO_REGION_SIZE % sc->sectorsize); 7220abd21bdSDag-Erling Smørgrav auio.uio_iov = &aiov; 7230abd21bdSDag-Erling Smørgrav auio.uio_iovcnt = 1; 7240abd21bdSDag-Erling Smørgrav auio.uio_offset = (vm_ooffset_t)bp->bio_offset; 7250abd21bdSDag-Erling Smørgrav auio.uio_segflg = UIO_SYSSPACE; 7260abd21bdSDag-Erling Smørgrav auio.uio_rw = UIO_WRITE; 7270abd21bdSDag-Erling Smørgrav auio.uio_td = td; 7280abd21bdSDag-Erling Smørgrav end = bp->bio_offset + bp->bio_length; 7290abd21bdSDag-Erling Smørgrav (void) vn_start_write(vp, &mp, V_WAIT); 7300abd21bdSDag-Erling Smørgrav vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7310abd21bdSDag-Erling Smørgrav error = 0; 7320abd21bdSDag-Erling Smørgrav while (auio.uio_offset < end) { 73389cb2a19SMatthew D Fleming aiov.iov_base = __DECONST(void *, zero_region); 7340abd21bdSDag-Erling Smørgrav aiov.iov_len = end - auio.uio_offset; 7350abd21bdSDag-Erling Smørgrav if (aiov.iov_len > zerosize) 7360abd21bdSDag-Erling Smørgrav aiov.iov_len = zerosize; 7370abd21bdSDag-Erling Smørgrav auio.uio_resid = aiov.iov_len; 7380abd21bdSDag-Erling Smørgrav error = VOP_WRITE(vp, &auio, 7390abd21bdSDag-Erling Smørgrav sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred); 7400abd21bdSDag-Erling Smørgrav if (error != 0) 7410abd21bdSDag-Erling Smørgrav break; 7420abd21bdSDag-Erling Smørgrav } 7430abd21bdSDag-Erling Smørgrav VOP_UNLOCK(vp, 0); 7440abd21bdSDag-Erling Smørgrav vn_finished_write(mp); 7450abd21bdSDag-Erling Smørgrav bp->bio_resid = end - auio.uio_offset; 7460abd21bdSDag-Erling Smørgrav return (error); 7470abd21bdSDag-Erling Smørgrav } 7480abd21bdSDag-Erling Smørgrav 74959ec9023SKonstantin Belousov KASSERT(bp->bio_length <= MAXPHYS, ("bio_length %jd", 75059ec9023SKonstantin Belousov (uintmax_t)bp->bio_length)); 75159ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) == 0) { 75259ec9023SKonstantin Belousov pb = NULL; 7538f8def9eSPoul-Henning Kamp aiov.iov_base = bp->bio_data; 75459ec9023SKonstantin Belousov } else { 75559ec9023SKonstantin Belousov pb = getpbuf(&md_vnode_pbuf_freecnt); 75659ec9023SKonstantin Belousov pmap_qenter((vm_offset_t)pb->b_data, bp->bio_ma, bp->bio_ma_n); 75759ec9023SKonstantin Belousov aiov.iov_base = (void *)((vm_offset_t)pb->b_data + 75859ec9023SKonstantin Belousov bp->bio_ma_offset); 75959ec9023SKonstantin Belousov } 760a8a58d03SPawel Jakub Dawidek aiov.iov_len = bp->bio_length; 7618f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 7628f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 763a8a58d03SPawel Jakub Dawidek auio.uio_offset = (vm_ooffset_t)bp->bio_offset; 7648f8def9eSPoul-Henning Kamp auio.uio_segflg = UIO_SYSSPACE; 7658f8def9eSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) 7668f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_READ; 7678e28326aSPoul-Henning Kamp else if (bp->bio_cmd == BIO_WRITE) 7688f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_WRITE; 7698e28326aSPoul-Henning Kamp else 7708e28326aSPoul-Henning Kamp panic("wrong BIO_OP in mdstart_vnode"); 771a8a58d03SPawel Jakub Dawidek auio.uio_resid = bp->bio_length; 7725541f25eSPawel Jakub Dawidek auio.uio_td = td; 7737e76bb56SMatthew Dillon /* 7747e76bb56SMatthew Dillon * When reading set IO_DIRECT to try to avoid double-caching 77517a13919SPoul-Henning Kamp * the data. When writing IO_DIRECT is not optimal. 7767e76bb56SMatthew Dillon */ 7778f8def9eSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 778cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7795541f25eSPawel Jakub Dawidek error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred); 78022db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 7818f8def9eSPoul-Henning Kamp } else { 7825541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 783cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7845541f25eSPawel Jakub Dawidek error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC, 7855541f25eSPawel Jakub Dawidek sc->cred); 78622db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 7878f8def9eSPoul-Henning Kamp vn_finished_write(mp); 7888f8def9eSPoul-Henning Kamp } 78959ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 79059ec9023SKonstantin Belousov pmap_qremove((vm_offset_t)pb->b_data, bp->bio_ma_n); 79159ec9023SKonstantin Belousov relpbuf(pb, &md_vnode_pbuf_freecnt); 79259ec9023SKonstantin Belousov } 7938f8def9eSPoul-Henning Kamp bp->bio_resid = auio.uio_resid; 794b4a4f93cSPoul-Henning Kamp return (error); 7958f8def9eSPoul-Henning Kamp } 7968f8def9eSPoul-Henning Kamp 797b4a4f93cSPoul-Henning Kamp static int 798b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp) 7998f8def9eSPoul-Henning Kamp { 8008e28326aSPoul-Henning Kamp vm_page_t m; 8018e28326aSPoul-Henning Kamp u_char *p; 80259ec9023SKonstantin Belousov vm_pindex_t i, lastp; 80359ec9023SKonstantin Belousov int rv, ma_offs, offs, len, lastend; 8048f8def9eSPoul-Henning Kamp 8055541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 8065541f25eSPawel Jakub Dawidek case BIO_READ: 8075541f25eSPawel Jakub Dawidek case BIO_WRITE: 8085541f25eSPawel Jakub Dawidek case BIO_DELETE: 8095541f25eSPawel Jakub Dawidek break; 8105541f25eSPawel Jakub Dawidek default: 8115541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 8125541f25eSPawel Jakub Dawidek } 8135541f25eSPawel Jakub Dawidek 8148e28326aSPoul-Henning Kamp p = bp->bio_data; 81559ec9023SKonstantin Belousov ma_offs = (bp->bio_flags & BIO_UNMAPPED) == 0 ? 0 : bp->bio_ma_offset; 816e07113d6SColin Percival 817e07113d6SColin Percival /* 8186c3cd0e2SMaxim Konovalov * offs is the offset at which to start operating on the 819e07113d6SColin Percival * next (ie, first) page. lastp is the last page on 820e07113d6SColin Percival * which we're going to operate. lastend is the ending 821e07113d6SColin Percival * position within that last page (ie, PAGE_SIZE if 822e07113d6SColin Percival * we're operating on complete aligned pages). 823e07113d6SColin Percival */ 824e07113d6SColin Percival offs = bp->bio_offset % PAGE_SIZE; 825e07113d6SColin Percival lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE; 826e07113d6SColin Percival lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1; 827e07113d6SColin Percival 828812851b6SBrian Feldman rv = VM_PAGER_OK; 82989f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 8308e28326aSPoul-Henning Kamp vm_object_pip_add(sc->object, 1); 831e07113d6SColin Percival for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) { 832e07113d6SColin Percival len = ((i == lastp) ? lastend : PAGE_SIZE) - offs; 8331a42d14aSKonstantin Belousov m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM); 8348e28326aSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 835537cc627SKonstantin Belousov if (m->valid == VM_PAGE_BITS_ALL) 836537cc627SKonstantin Belousov rv = VM_PAGER_OK; 837537cc627SKonstantin Belousov else 83807be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 839812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 840c7aebda8SAttilio Rao vm_page_xunbusy(m); 841812851b6SBrian Feldman break; 842e9f581baSKonstantin Belousov } else if (rv == VM_PAGER_FAIL) { 843e9f581baSKonstantin Belousov /* 844e9f581baSKonstantin Belousov * Pager does not have the page. Zero 845e9f581baSKonstantin Belousov * the allocated page, and mark it as 846e9f581baSKonstantin Belousov * valid. Do not set dirty, the page 847e9f581baSKonstantin Belousov * can be recreated if thrown out. 848e9f581baSKonstantin Belousov */ 84959ec9023SKonstantin Belousov pmap_zero_page(m); 850e9f581baSKonstantin Belousov m->valid = VM_PAGE_BITS_ALL; 851812851b6SBrian Feldman } 85259ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 85359ec9023SKonstantin Belousov pmap_copy_pages(&m, offs, bp->bio_ma, 85459ec9023SKonstantin Belousov ma_offs, len); 85559ec9023SKonstantin Belousov } else { 85659ec9023SKonstantin Belousov physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len); 857dbb95048SMarcel Moolenaar cpu_flush_dcache(p, len); 85859ec9023SKonstantin Belousov } 8598e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 86007be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 86107be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 862537cc627SKonstantin Belousov else 863537cc627SKonstantin Belousov rv = VM_PAGER_OK; 864812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 865c7aebda8SAttilio Rao vm_page_xunbusy(m); 866812851b6SBrian Feldman break; 867812851b6SBrian Feldman } 86859ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 86959ec9023SKonstantin Belousov pmap_copy_pages(bp->bio_ma, ma_offs, &m, 87059ec9023SKonstantin Belousov offs, len); 87159ec9023SKonstantin Belousov } else { 87259ec9023SKonstantin Belousov physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len); 87359ec9023SKonstantin Belousov } 8748e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 8758e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_DELETE) { 87607be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 87707be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 878537cc627SKonstantin Belousov else 879537cc627SKonstantin Belousov rv = VM_PAGER_OK; 880812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 881c7aebda8SAttilio Rao vm_page_xunbusy(m); 882812851b6SBrian Feldman break; 883812851b6SBrian Feldman } 8844a13a769SKonstantin Belousov if (len != PAGE_SIZE) { 88559ec9023SKonstantin Belousov pmap_zero_page_area(m, offs, len); 8864a13a769SKonstantin Belousov vm_page_clear_dirty(m, offs, len); 8878e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 8884a13a769SKonstantin Belousov } else 8894a13a769SKonstantin Belousov vm_pager_page_unswapped(m); 8908e28326aSPoul-Henning Kamp } 891c7aebda8SAttilio Rao vm_page_xunbusy(m); 892fc0c3802SKonstantin Belousov vm_page_lock(m); 8934a13a769SKonstantin Belousov if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE) 8944a13a769SKonstantin Belousov vm_page_free(m); 8954a13a769SKonstantin Belousov else 8968e28326aSPoul-Henning Kamp vm_page_activate(m); 897ecd5dd95SAlan Cox vm_page_unlock(m); 89807be617fSAlan Cox if (bp->bio_cmd == BIO_WRITE) 8998e28326aSPoul-Henning Kamp vm_page_dirty(m); 900e07113d6SColin Percival 901e07113d6SColin Percival /* Actions on further pages start at offset 0 */ 902e07113d6SColin Percival p += PAGE_SIZE - offs; 903e07113d6SColin Percival offs = 0; 90459ec9023SKonstantin Belousov ma_offs += len; 9058e28326aSPoul-Henning Kamp } 9068e28326aSPoul-Henning Kamp vm_object_pip_subtract(sc->object, 1); 90789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 908812851b6SBrian Feldman return (rv != VM_PAGER_ERROR ? 0 : ENOSPC); 9098e28326aSPoul-Henning Kamp } 9108f8def9eSPoul-Henning Kamp 911*0efd9bfdSEdward Tomasz Napierala static int 912*0efd9bfdSEdward Tomasz Napierala mdstart_null(struct md_s *sc, struct bio *bp) 913*0efd9bfdSEdward Tomasz Napierala { 914*0efd9bfdSEdward Tomasz Napierala 915*0efd9bfdSEdward Tomasz Napierala switch (bp->bio_cmd) { 916*0efd9bfdSEdward Tomasz Napierala case BIO_READ: 917*0efd9bfdSEdward Tomasz Napierala bzero(bp->bio_data, bp->bio_length); 918*0efd9bfdSEdward Tomasz Napierala cpu_flush_dcache(bp->bio_data, bp->bio_length); 919*0efd9bfdSEdward Tomasz Napierala break; 920*0efd9bfdSEdward Tomasz Napierala case BIO_WRITE: 921*0efd9bfdSEdward Tomasz Napierala break; 922*0efd9bfdSEdward Tomasz Napierala } 923*0efd9bfdSEdward Tomasz Napierala bp->bio_resid = 0; 924*0efd9bfdSEdward Tomasz Napierala return (0); 925*0efd9bfdSEdward Tomasz Napierala } 926*0efd9bfdSEdward Tomasz Napierala 9278f8def9eSPoul-Henning Kamp static void 9285c97ca54SIan Dowse md_kthread(void *arg) 9295c97ca54SIan Dowse { 9305c97ca54SIan Dowse struct md_s *sc; 9315c97ca54SIan Dowse struct bio *bp; 932a08d2e7fSJohn Baldwin int error; 9335c97ca54SIan Dowse 9345c97ca54SIan Dowse sc = arg; 935982d11f8SJeff Roberson thread_lock(curthread); 93663710c4dSJohn Baldwin sched_prio(curthread, PRIBIO); 937982d11f8SJeff Roberson thread_unlock(curthread); 9383b7b5496SKonstantin Belousov if (sc->type == MD_VNODE) 9393b7b5496SKonstantin Belousov curthread->td_pflags |= TDP_NORUNNINGBUF; 9405c97ca54SIan Dowse 941b4a4f93cSPoul-Henning Kamp for (;;) { 942a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 9435c97ca54SIan Dowse if (sc->flags & MD_SHUTDOWN) { 944a08d2e7fSJohn Baldwin sc->flags |= MD_EXITING; 945a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 9463745c395SJulian Elischer kproc_exit(0); 9475c97ca54SIan Dowse } 9489b00ca19SPoul-Henning Kamp bp = bioq_takefirst(&sc->bio_queue); 9499b00ca19SPoul-Henning Kamp if (!bp) { 9500f8500a5SPoul-Henning Kamp msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0); 9515c97ca54SIan Dowse continue; 9525c97ca54SIan Dowse } 9530f8500a5SPoul-Henning Kamp mtx_unlock(&sc->queue_mtx); 9544e8bfe14SPoul-Henning Kamp if (bp->bio_cmd == BIO_GETATTR) { 955d91e813cSKonstantin Belousov if ((sc->fwsectors && sc->fwheads && 9564e8bfe14SPoul-Henning Kamp (g_handleattr_int(bp, "GEOM::fwsectors", 9574e8bfe14SPoul-Henning Kamp sc->fwsectors) || 9584e8bfe14SPoul-Henning Kamp g_handleattr_int(bp, "GEOM::fwheads", 959d91e813cSKonstantin Belousov sc->fwheads))) || 960d91e813cSKonstantin Belousov g_handleattr_int(bp, "GEOM::candelete", 1)) 9614e8bfe14SPoul-Henning Kamp error = -1; 9624e8bfe14SPoul-Henning Kamp else 9634e8bfe14SPoul-Henning Kamp error = EOPNOTSUPP; 9644e8bfe14SPoul-Henning Kamp } else { 9659b00ca19SPoul-Henning Kamp error = sc->start(sc, bp); 9664e8bfe14SPoul-Henning Kamp } 967b4a4f93cSPoul-Henning Kamp 9686f4f00f1SPoul-Henning Kamp if (error != -1) { 9696f4f00f1SPoul-Henning Kamp bp->bio_completed = bp->bio_length; 970a03be42dSMaxim Sobolev if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) 971a03be42dSMaxim Sobolev devstat_end_transaction_bio(sc->devstat, bp); 97296410b95SKonstantin Belousov g_io_deliver(bp, error); 973b4a4f93cSPoul-Henning Kamp } 9748f8def9eSPoul-Henning Kamp } 97526d48b40SPoul-Henning Kamp } 9768f8def9eSPoul-Henning Kamp 9778f8def9eSPoul-Henning Kamp static struct md_s * 9788f8def9eSPoul-Henning Kamp mdfind(int unit) 9798f8def9eSPoul-Henning Kamp { 9808f8def9eSPoul-Henning Kamp struct md_s *sc; 9818f8def9eSPoul-Henning Kamp 9823f54a085SPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 9833f54a085SPoul-Henning Kamp if (sc->unit == unit) 9848f8def9eSPoul-Henning Kamp break; 9858f8def9eSPoul-Henning Kamp } 9868f8def9eSPoul-Henning Kamp return (sc); 9878f8def9eSPoul-Henning Kamp } 9888f8def9eSPoul-Henning Kamp 9898f8def9eSPoul-Henning Kamp static struct md_s * 990947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type) 9918f8def9eSPoul-Henning Kamp { 992f4e7c5a8SJaakko Heinonen struct md_s *sc; 993f4e7c5a8SJaakko Heinonen int error; 9948f8def9eSPoul-Henning Kamp 9959b00ca19SPoul-Henning Kamp *errp = 0; 996f4e7c5a8SJaakko Heinonen if (unit == -1) 997f4e7c5a8SJaakko Heinonen unit = alloc_unr(md_uh); 998f4e7c5a8SJaakko Heinonen else 999f4e7c5a8SJaakko Heinonen unit = alloc_unr_specific(md_uh, unit); 1000f4e7c5a8SJaakko Heinonen 1001f4e7c5a8SJaakko Heinonen if (unit == -1) { 10027ee3c044SPawel Jakub Dawidek *errp = EBUSY; 10033f54a085SPoul-Henning Kamp return (NULL); 10043f54a085SPoul-Henning Kamp } 1005f4e7c5a8SJaakko Heinonen 10069b00ca19SPoul-Henning Kamp sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO); 1007947fc8deSPoul-Henning Kamp sc->type = type; 10089b00ca19SPoul-Henning Kamp bioq_init(&sc->bio_queue); 10099b00ca19SPoul-Henning Kamp mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF); 101040ea77a0SAlexander Motin mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF); 10113f54a085SPoul-Henning Kamp sc->unit = unit; 1012f43b2bacSPoul-Henning Kamp sprintf(sc->name, "md%d", unit); 10137ee3c044SPawel Jakub Dawidek LIST_INSERT_HEAD(&md_softc_list, sc, list); 10143745c395SJulian Elischer error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name); 10159b00ca19SPoul-Henning Kamp if (error == 0) 10169b00ca19SPoul-Henning Kamp return (sc); 10177ee3c044SPawel Jakub Dawidek LIST_REMOVE(sc, list); 101840ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 10197ee3c044SPawel Jakub Dawidek mtx_destroy(&sc->queue_mtx); 1020f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 10215c97ca54SIan Dowse free(sc, M_MD); 10227ee3c044SPawel Jakub Dawidek *errp = error; 10235c97ca54SIan Dowse return (NULL); 10245c97ca54SIan Dowse } 10258f8def9eSPoul-Henning Kamp 10268f8def9eSPoul-Henning Kamp static void 10278f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc) 10288f8def9eSPoul-Henning Kamp { 10296f4f00f1SPoul-Henning Kamp struct g_geom *gp; 10306f4f00f1SPoul-Henning Kamp struct g_provider *pp; 10316f4f00f1SPoul-Henning Kamp 10326f4f00f1SPoul-Henning Kamp g_topology_lock(); 10336f4f00f1SPoul-Henning Kamp gp = g_new_geomf(&g_md_class, "md%d", sc->unit); 10346f4f00f1SPoul-Henning Kamp gp->softc = sc; 10356f4f00f1SPoul-Henning Kamp pp = g_new_providerf(gp, "md%d", sc->unit); 103640ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 1037b830359bSPawel Jakub Dawidek pp->mediasize = sc->mediasize; 1038b830359bSPawel Jakub Dawidek pp->sectorsize = sc->sectorsize; 10391ef76554SKonstantin Belousov switch (sc->type) { 10401ef76554SKonstantin Belousov case MD_MALLOC: 10411ef76554SKonstantin Belousov case MD_VNODE: 10421ef76554SKonstantin Belousov case MD_SWAP: 104359ec9023SKonstantin Belousov pp->flags |= G_PF_ACCEPT_UNMAPPED; 10441ef76554SKonstantin Belousov break; 10451ef76554SKonstantin Belousov case MD_PRELOAD: 1046*0efd9bfdSEdward Tomasz Napierala case MD_NULL: 10471ef76554SKonstantin Belousov break; 10481ef76554SKonstantin Belousov } 10496f4f00f1SPoul-Henning Kamp sc->gp = gp; 10506f4f00f1SPoul-Henning Kamp sc->pp = pp; 10516f4f00f1SPoul-Henning Kamp g_error_provider(pp, 0); 10526f4f00f1SPoul-Henning Kamp g_topology_unlock(); 1053a03be42dSMaxim Sobolev sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize, 1054a03be42dSMaxim Sobolev DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 10556f4f00f1SPoul-Henning Kamp } 105671e4fff8SPoul-Henning Kamp 10578f8def9eSPoul-Henning Kamp static int 1058b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio) 105995f1a897SPoul-Henning Kamp { 1060c6517568SPoul-Henning Kamp uintptr_t sp; 1061c6517568SPoul-Henning Kamp int error; 1062b830359bSPawel Jakub Dawidek off_t u; 106395f1a897SPoul-Henning Kamp 1064c6517568SPoul-Henning Kamp error = 0; 10658f8def9eSPoul-Henning Kamp if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 10668f8def9eSPoul-Henning Kamp return (EINVAL); 1067b830359bSPawel Jakub Dawidek if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize)) 1068ebe789d6SPoul-Henning Kamp return (EINVAL); 10698f8def9eSPoul-Henning Kamp /* Compression doesn't make sense if we have reserved space */ 10708f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) 10718f8def9eSPoul-Henning Kamp mdio->md_options &= ~MD_COMPRESS; 10724e8bfe14SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 10734e8bfe14SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 10744e8bfe14SPoul-Henning Kamp if (mdio->md_fwheads != 0) 10754e8bfe14SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 107626a0ee75SDima Dorfman sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE); 1077b830359bSPawel Jakub Dawidek sc->indir = dimension(sc->mediasize / sc->sectorsize); 1078b830359bSPawel Jakub Dawidek sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL, 1079b830359bSPawel Jakub Dawidek 0x1ff, 0); 108096b6a55fSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1081b830359bSPawel Jakub Dawidek off_t nsectors; 1082b830359bSPawel Jakub Dawidek 1083b830359bSPawel Jakub Dawidek nsectors = sc->mediasize / sc->sectorsize; 1084b830359bSPawel Jakub Dawidek for (u = 0; u < nsectors; u++) { 1085007777f1SKonstantin Belousov sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ? 1086007777f1SKonstantin Belousov M_WAITOK : M_NOWAIT) | M_ZERO); 1087c6517568SPoul-Henning Kamp if (sp != 0) 1088fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, u, sp); 1089c6517568SPoul-Henning Kamp else 1090c6517568SPoul-Henning Kamp error = ENOMEM; 1091b830359bSPawel Jakub Dawidek if (error != 0) 1092c6517568SPoul-Henning Kamp break; 10938f8def9eSPoul-Henning Kamp } 1094c6517568SPoul-Henning Kamp } 1095c6517568SPoul-Henning Kamp return (error); 109600a6a3c6SPoul-Henning Kamp } 109700a6a3c6SPoul-Henning Kamp 10983f54a085SPoul-Henning Kamp 10998f8def9eSPoul-Henning Kamp static int 11008f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred) 11018f8def9eSPoul-Henning Kamp { 11028f8def9eSPoul-Henning Kamp char *tmpbuf; 11038f8def9eSPoul-Henning Kamp int error = 0; 11048f8def9eSPoul-Henning Kamp 11053f54a085SPoul-Henning Kamp /* 11068f8def9eSPoul-Henning Kamp * Set credits in our softc 11073f54a085SPoul-Henning Kamp */ 11088f8def9eSPoul-Henning Kamp 11098f8def9eSPoul-Henning Kamp if (sc->cred) 11108f8def9eSPoul-Henning Kamp crfree(sc->cred); 1111bd78ceceSJohn Baldwin sc->cred = crhold(cred); 11128f8def9eSPoul-Henning Kamp 11138f8def9eSPoul-Henning Kamp /* 11148f8def9eSPoul-Henning Kamp * Horrible kludge to establish credentials for NFS XXX. 11158f8def9eSPoul-Henning Kamp */ 11168f8def9eSPoul-Henning Kamp 11178f8def9eSPoul-Henning Kamp if (sc->vnode) { 11188f8def9eSPoul-Henning Kamp struct uio auio; 11198f8def9eSPoul-Henning Kamp struct iovec aiov; 11208f8def9eSPoul-Henning Kamp 1121b830359bSPawel Jakub Dawidek tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK); 11228f8def9eSPoul-Henning Kamp bzero(&auio, sizeof(auio)); 11238f8def9eSPoul-Henning Kamp 11248f8def9eSPoul-Henning Kamp aiov.iov_base = tmpbuf; 1125b830359bSPawel Jakub Dawidek aiov.iov_len = sc->sectorsize; 11268f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 11278f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 11288f8def9eSPoul-Henning Kamp auio.uio_offset = 0; 11298f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_READ; 11308f8def9eSPoul-Henning Kamp auio.uio_segflg = UIO_SYSSPACE; 11318f8def9eSPoul-Henning Kamp auio.uio_resid = aiov.iov_len; 1132cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 11338f8def9eSPoul-Henning Kamp error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 113422db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 11358f8def9eSPoul-Henning Kamp free(tmpbuf, M_TEMP); 11368f8def9eSPoul-Henning Kamp } 11378f8def9eSPoul-Henning Kamp return (error); 11388f8def9eSPoul-Henning Kamp } 11398f8def9eSPoul-Henning Kamp 11408f8def9eSPoul-Henning Kamp static int 1141b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 11428f8def9eSPoul-Henning Kamp { 11438f8def9eSPoul-Henning Kamp struct vattr vattr; 11448f8def9eSPoul-Henning Kamp struct nameidata nd; 11453d5c947dSMarcel Moolenaar char *fname; 11465050aa86SKonstantin Belousov int error, flags; 11478f8def9eSPoul-Henning Kamp 11483d5c947dSMarcel Moolenaar /* 11493d5c947dSMarcel Moolenaar * Kernel-originated requests must have the filename appended 11503d5c947dSMarcel Moolenaar * to the mdio structure to protect against malicious software. 11513d5c947dSMarcel Moolenaar */ 11523d5c947dSMarcel Moolenaar fname = mdio->md_file; 11533d5c947dSMarcel Moolenaar if ((void *)fname != (void *)(mdio + 1)) { 11543d5c947dSMarcel Moolenaar error = copyinstr(fname, sc->file, sizeof(sc->file), NULL); 115588b5b78dSPawel Jakub Dawidek if (error != 0) 115688b5b78dSPawel Jakub Dawidek return (error); 11573d5c947dSMarcel Moolenaar } else 11583d5c947dSMarcel Moolenaar strlcpy(sc->file, fname, sizeof(sc->file)); 11593d5c947dSMarcel Moolenaar 116086776891SChristian S.J. Peron /* 11613d5c947dSMarcel Moolenaar * If the user specified that this is a read only device, don't 11623d5c947dSMarcel Moolenaar * set the FWRITE mask before trying to open the backing store. 116386776891SChristian S.J. Peron */ 11643d5c947dSMarcel Moolenaar flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE); 11655050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td); 11669e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 1167e3ed29a7SPawel Jakub Dawidek if (error != 0) 116852c6716fSPawel Jakub Dawidek return (error); 1169b322d85dSPawel Jakub Dawidek NDFREE(&nd, NDF_ONLY_PNBUF); 117033fc3625SJohn Baldwin if (nd.ni_vp->v_type != VREG) { 117133fc3625SJohn Baldwin error = EINVAL; 117233fc3625SJohn Baldwin goto bad; 117333fc3625SJohn Baldwin } 117433fc3625SJohn Baldwin error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred); 117533fc3625SJohn Baldwin if (error != 0) 117633fc3625SJohn Baldwin goto bad; 117733fc3625SJohn Baldwin if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) { 117833fc3625SJohn Baldwin vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY); 117933fc3625SJohn Baldwin if (nd.ni_vp->v_iflag & VI_DOOMED) { 118033fc3625SJohn Baldwin /* Forced unmount. */ 118133fc3625SJohn Baldwin error = EBADF; 118233fc3625SJohn Baldwin goto bad; 118333fc3625SJohn Baldwin } 11848f8def9eSPoul-Henning Kamp } 11853b7b5496SKonstantin Belousov nd.ni_vp->v_vflag |= VV_MD; 118622db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 11879589c256SPoul-Henning Kamp 1188d5a929dcSPoul-Henning Kamp if (mdio->md_fwsectors != 0) 1189d5a929dcSPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 1190d5a929dcSPoul-Henning Kamp if (mdio->md_fwheads != 0) 1191d5a929dcSPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 11927a6b2b64SPoul-Henning Kamp sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC); 11939589c256SPoul-Henning Kamp if (!(flags & FWRITE)) 11949589c256SPoul-Henning Kamp sc->flags |= MD_READONLY; 11958f8def9eSPoul-Henning Kamp sc->vnode = nd.ni_vp; 11968f8def9eSPoul-Henning Kamp 1197a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 1198b830359bSPawel Jakub Dawidek if (error != 0) { 11993cf74e53SPhilip Paeps sc->vnode = NULL; 1200cb05b60aSAttilio Rao vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY); 12013b7b5496SKonstantin Belousov nd.ni_vp->v_vflag &= ~VV_MD; 120233fc3625SJohn Baldwin goto bad; 120333fc3625SJohn Baldwin } 120433fc3625SJohn Baldwin return (0); 120533fc3625SJohn Baldwin bad: 120622db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 120752c6716fSPawel Jakub Dawidek (void)vn_close(nd.ni_vp, flags, td->td_ucred, td); 12088f8def9eSPoul-Henning Kamp return (error); 12098f8def9eSPoul-Henning Kamp } 12108f8def9eSPoul-Henning Kamp 12118f8def9eSPoul-Henning Kamp static int 1212b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td) 12138f8def9eSPoul-Henning Kamp { 12140cddd8f0SMatthew Dillon 12156f4f00f1SPoul-Henning Kamp if (sc->gp) { 12166f4f00f1SPoul-Henning Kamp sc->gp->softc = NULL; 12179b00ca19SPoul-Henning Kamp g_topology_lock(); 12189b00ca19SPoul-Henning Kamp g_wither_geom(sc->gp, ENXIO); 12199b00ca19SPoul-Henning Kamp g_topology_unlock(); 12206b60a2cdSPoul-Henning Kamp sc->gp = NULL; 12216b60a2cdSPoul-Henning Kamp sc->pp = NULL; 12221f4ee1aaSPoul-Henning Kamp } 1223a03be42dSMaxim Sobolev if (sc->devstat) { 1224a03be42dSMaxim Sobolev devstat_remove_entry(sc->devstat); 1225a03be42dSMaxim Sobolev sc->devstat = NULL; 1226a03be42dSMaxim Sobolev } 1227a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 12285c97ca54SIan Dowse sc->flags |= MD_SHUTDOWN; 12295c97ca54SIan Dowse wakeup(sc); 1230a08d2e7fSJohn Baldwin while (!(sc->flags & MD_EXITING)) 1231a08d2e7fSJohn Baldwin msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10); 1232a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 123340ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 12349fbea3e3SPoul-Henning Kamp mtx_destroy(&sc->queue_mtx); 12359b00ca19SPoul-Henning Kamp if (sc->vnode != NULL) { 1236cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 12373b7b5496SKonstantin Belousov sc->vnode->v_vflag &= ~VV_MD; 123822db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 12399d4b5945SMaxim Sobolev (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? 1240b40ce416SJulian Elischer FREAD : (FREAD|FWRITE), sc->cred, td); 12419b00ca19SPoul-Henning Kamp } 12428f8def9eSPoul-Henning Kamp if (sc->cred != NULL) 12438f8def9eSPoul-Henning Kamp crfree(sc->cred); 12441db17c6dSPawel Jakub Dawidek if (sc->object != NULL) 1245f820bc50SAlan Cox vm_object_deallocate(sc->object); 1246f43b2bacSPoul-Henning Kamp if (sc->indir) 1247f43b2bacSPoul-Henning Kamp destroy_indir(sc, sc->indir); 1248f43b2bacSPoul-Henning Kamp if (sc->uma) 1249f43b2bacSPoul-Henning Kamp uma_zdestroy(sc->uma); 12501f4ee1aaSPoul-Henning Kamp 12511f4ee1aaSPoul-Henning Kamp LIST_REMOVE(sc, list); 1252f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 1253c6517568SPoul-Henning Kamp free(sc, M_MD); 12548f8def9eSPoul-Henning Kamp return (0); 12558f8def9eSPoul-Henning Kamp } 12568f8def9eSPoul-Henning Kamp 12578f8def9eSPoul-Henning Kamp static int 1258dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio) 1259dc604f0cSEdward Tomasz Napierala { 1260dc604f0cSEdward Tomasz Napierala int error, res; 1261dc604f0cSEdward Tomasz Napierala vm_pindex_t oldpages, newpages; 1262dc604f0cSEdward Tomasz Napierala 1263dc604f0cSEdward Tomasz Napierala switch (sc->type) { 1264dc604f0cSEdward Tomasz Napierala case MD_VNODE: 1265*0efd9bfdSEdward Tomasz Napierala case MD_NULL: 1266dc604f0cSEdward Tomasz Napierala break; 1267dc604f0cSEdward Tomasz Napierala case MD_SWAP: 12688cb51643SJaakko Heinonen if (mdio->md_mediasize <= 0 || 1269dc604f0cSEdward Tomasz Napierala (mdio->md_mediasize % PAGE_SIZE) != 0) 1270dc604f0cSEdward Tomasz Napierala return (EDOM); 1271dc604f0cSEdward Tomasz Napierala oldpages = OFF_TO_IDX(round_page(sc->mediasize)); 1272dc604f0cSEdward Tomasz Napierala newpages = OFF_TO_IDX(round_page(mdio->md_mediasize)); 1273dc604f0cSEdward Tomasz Napierala if (newpages < oldpages) { 127489f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1275dc604f0cSEdward Tomasz Napierala vm_object_page_remove(sc->object, newpages, 0, 0); 1276dc604f0cSEdward Tomasz Napierala swap_pager_freespace(sc->object, newpages, 1277dc604f0cSEdward Tomasz Napierala oldpages - newpages); 1278dc604f0cSEdward Tomasz Napierala swap_release_by_cred(IDX_TO_OFF(oldpages - 1279dc604f0cSEdward Tomasz Napierala newpages), sc->cred); 1280dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1281dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 128289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1283dc604f0cSEdward Tomasz Napierala } else if (newpages > oldpages) { 1284dc604f0cSEdward Tomasz Napierala res = swap_reserve_by_cred(IDX_TO_OFF(newpages - 1285dc604f0cSEdward Tomasz Napierala oldpages), sc->cred); 1286dc604f0cSEdward Tomasz Napierala if (!res) 1287dc604f0cSEdward Tomasz Napierala return (ENOMEM); 1288dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & MD_RESERVE) || 1289dc604f0cSEdward Tomasz Napierala (sc->flags & MD_RESERVE)) { 1290dc604f0cSEdward Tomasz Napierala error = swap_pager_reserve(sc->object, 1291dc604f0cSEdward Tomasz Napierala oldpages, newpages - oldpages); 1292dc604f0cSEdward Tomasz Napierala if (error < 0) { 1293dc604f0cSEdward Tomasz Napierala swap_release_by_cred( 1294dc604f0cSEdward Tomasz Napierala IDX_TO_OFF(newpages - oldpages), 1295dc604f0cSEdward Tomasz Napierala sc->cred); 1296dc604f0cSEdward Tomasz Napierala return (EDOM); 1297dc604f0cSEdward Tomasz Napierala } 1298dc604f0cSEdward Tomasz Napierala } 129989f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1300dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1301dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 130289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1303dc604f0cSEdward Tomasz Napierala } 1304dc604f0cSEdward Tomasz Napierala break; 1305dc604f0cSEdward Tomasz Napierala default: 1306dc604f0cSEdward Tomasz Napierala return (EOPNOTSUPP); 1307dc604f0cSEdward Tomasz Napierala } 1308dc604f0cSEdward Tomasz Napierala 1309dc604f0cSEdward Tomasz Napierala sc->mediasize = mdio->md_mediasize; 1310dc604f0cSEdward Tomasz Napierala g_topology_lock(); 1311dc604f0cSEdward Tomasz Napierala g_resize_provider(sc->pp, sc->mediasize); 1312dc604f0cSEdward Tomasz Napierala g_topology_unlock(); 1313dc604f0cSEdward Tomasz Napierala return (0); 1314dc604f0cSEdward Tomasz Napierala } 1315dc604f0cSEdward Tomasz Napierala 1316dc604f0cSEdward Tomasz Napierala static int 1317b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 13188f8def9eSPoul-Henning Kamp { 1319fcd57fbeSPawel Jakub Dawidek vm_ooffset_t npage; 1320fcd57fbeSPawel Jakub Dawidek int error; 13218f8def9eSPoul-Henning Kamp 13228f8def9eSPoul-Henning Kamp /* 13238f8def9eSPoul-Henning Kamp * Range check. Disallow negative sizes or any size less then the 13248f8def9eSPoul-Henning Kamp * size of a page. Then round to a page. 13258f8def9eSPoul-Henning Kamp */ 13268cb51643SJaakko Heinonen if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 13278f8def9eSPoul-Henning Kamp return (EDOM); 13288f8def9eSPoul-Henning Kamp 13298f8def9eSPoul-Henning Kamp /* 13308f8def9eSPoul-Henning Kamp * Allocate an OBJT_SWAP object. 13318f8def9eSPoul-Henning Kamp * 13328f8def9eSPoul-Henning Kamp * Note the truncation. 13338f8def9eSPoul-Henning Kamp */ 13348f8def9eSPoul-Henning Kamp 1335b830359bSPawel Jakub Dawidek npage = mdio->md_mediasize / PAGE_SIZE; 13369ed40643SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 13379ed40643SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 13389ed40643SPoul-Henning Kamp if (mdio->md_fwheads != 0) 13399ed40643SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 1340fcd57fbeSPawel Jakub Dawidek sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage, 13413364c323SKonstantin Belousov VM_PROT_DEFAULT, 0, td->td_ucred); 1342812851b6SBrian Feldman if (sc->object == NULL) 1343812851b6SBrian Feldman return (ENOMEM); 1344dc604f0cSEdward Tomasz Napierala sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE); 13458f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1346fcd57fbeSPawel Jakub Dawidek if (swap_pager_reserve(sc->object, 0, npage) < 0) { 13473364c323SKonstantin Belousov error = EDOM; 13483364c323SKonstantin Belousov goto finish; 13498f8def9eSPoul-Henning Kamp } 13508f8def9eSPoul-Henning Kamp } 1351a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 13523364c323SKonstantin Belousov finish: 1353e3ed29a7SPawel Jakub Dawidek if (error != 0) { 13542eafd8b1SPawel Jakub Dawidek vm_object_deallocate(sc->object); 13552eafd8b1SPawel Jakub Dawidek sc->object = NULL; 13568f8def9eSPoul-Henning Kamp } 1357b830359bSPawel Jakub Dawidek return (error); 1358b3b3d1b7SPoul-Henning Kamp } 13598f8def9eSPoul-Henning Kamp 1360*0efd9bfdSEdward Tomasz Napierala static int 1361*0efd9bfdSEdward Tomasz Napierala mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 1362*0efd9bfdSEdward Tomasz Napierala { 1363*0efd9bfdSEdward Tomasz Napierala 1364*0efd9bfdSEdward Tomasz Napierala /* 1365*0efd9bfdSEdward Tomasz Napierala * Range check. Disallow negative sizes or any size less then the 1366*0efd9bfdSEdward Tomasz Napierala * size of a page. Then round to a page. 1367*0efd9bfdSEdward Tomasz Napierala */ 1368*0efd9bfdSEdward Tomasz Napierala if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 1369*0efd9bfdSEdward Tomasz Napierala return (EDOM); 1370*0efd9bfdSEdward Tomasz Napierala 1371*0efd9bfdSEdward Tomasz Napierala return (0); 1372*0efd9bfdSEdward Tomasz Napierala } 13739d4b5945SMaxim Sobolev 13749d4b5945SMaxim Sobolev static int 13759b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 13768f8def9eSPoul-Henning Kamp { 13778f8def9eSPoul-Henning Kamp struct md_ioctl *mdio; 13788f8def9eSPoul-Henning Kamp struct md_s *sc; 1379b830359bSPawel Jakub Dawidek int error, i; 13808cb51643SJaakko Heinonen unsigned sectsize; 13818f8def9eSPoul-Henning Kamp 13828f8def9eSPoul-Henning Kamp if (md_debug) 13838f8def9eSPoul-Henning Kamp printf("mdctlioctl(%s %lx %p %x %p)\n", 1384b40ce416SJulian Elischer devtoname(dev), cmd, addr, flags, td); 13858f8def9eSPoul-Henning Kamp 13869b00ca19SPoul-Henning Kamp mdio = (struct md_ioctl *)addr; 13879b00ca19SPoul-Henning Kamp if (mdio->md_version != MDIOVERSION) 13889b00ca19SPoul-Henning Kamp return (EINVAL); 13899b00ca19SPoul-Henning Kamp 139053d745bcSDima Dorfman /* 139153d745bcSDima Dorfman * We assert the version number in the individual ioctl 139253d745bcSDima Dorfman * handlers instead of out here because (a) it is possible we 139353d745bcSDima Dorfman * may add another ioctl in the future which doesn't read an 139453d745bcSDima Dorfman * mdio, and (b) the correct return value for an unknown ioctl 139553d745bcSDima Dorfman * is ENOIOCTL, not EINVAL. 139653d745bcSDima Dorfman */ 13979b00ca19SPoul-Henning Kamp error = 0; 13988f8def9eSPoul-Henning Kamp switch (cmd) { 13998f8def9eSPoul-Henning Kamp case MDIOCATTACH: 14008f8def9eSPoul-Henning Kamp switch (mdio->md_type) { 14018f8def9eSPoul-Henning Kamp case MD_MALLOC: 14028f8def9eSPoul-Henning Kamp case MD_PRELOAD: 14038f8def9eSPoul-Henning Kamp case MD_VNODE: 14048f8def9eSPoul-Henning Kamp case MD_SWAP: 1405*0efd9bfdSEdward Tomasz Napierala case MD_NULL: 1406b830359bSPawel Jakub Dawidek break; 14078f8def9eSPoul-Henning Kamp default: 14088f8def9eSPoul-Henning Kamp return (EINVAL); 14098f8def9eSPoul-Henning Kamp } 14108cb51643SJaakko Heinonen if (mdio->md_sectorsize == 0) 14118cb51643SJaakko Heinonen sectsize = DEV_BSIZE; 14128cb51643SJaakko Heinonen else 14138cb51643SJaakko Heinonen sectsize = mdio->md_sectorsize; 14148cb51643SJaakko Heinonen if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize) 14158cb51643SJaakko Heinonen return (EINVAL); 14167ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 1417947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, mdio->md_type); 1418f4e7c5a8SJaakko Heinonen else { 1419f4e7c5a8SJaakko Heinonen if (mdio->md_unit > INT_MAX) 1420f4e7c5a8SJaakko Heinonen return (EINVAL); 1421947fc8deSPoul-Henning Kamp sc = mdnew(mdio->md_unit, &error, mdio->md_type); 1422f4e7c5a8SJaakko Heinonen } 1423b830359bSPawel Jakub Dawidek if (sc == NULL) 14247ee3c044SPawel Jakub Dawidek return (error); 14257ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 14267ee3c044SPawel Jakub Dawidek mdio->md_unit = sc->unit; 1427b830359bSPawel Jakub Dawidek sc->mediasize = mdio->md_mediasize; 14288cb51643SJaakko Heinonen sc->sectorsize = sectsize; 1429b830359bSPawel Jakub Dawidek error = EDOOFUS; 1430b830359bSPawel Jakub Dawidek switch (sc->type) { 1431b830359bSPawel Jakub Dawidek case MD_MALLOC: 14329b00ca19SPoul-Henning Kamp sc->start = mdstart_malloc; 1433b830359bSPawel Jakub Dawidek error = mdcreate_malloc(sc, mdio); 1434b830359bSPawel Jakub Dawidek break; 1435b830359bSPawel Jakub Dawidek case MD_PRELOAD: 1436734e78dfSJaakko Heinonen /* 1437734e78dfSJaakko Heinonen * We disallow attaching preloaded memory disks via 1438734e78dfSJaakko Heinonen * ioctl. Preloaded memory disks are automatically 1439734e78dfSJaakko Heinonen * attached in g_md_init(). 1440734e78dfSJaakko Heinonen */ 1441734e78dfSJaakko Heinonen error = EOPNOTSUPP; 1442b830359bSPawel Jakub Dawidek break; 1443b830359bSPawel Jakub Dawidek case MD_VNODE: 14449b00ca19SPoul-Henning Kamp sc->start = mdstart_vnode; 1445b830359bSPawel Jakub Dawidek error = mdcreate_vnode(sc, mdio, td); 1446b830359bSPawel Jakub Dawidek break; 1447b830359bSPawel Jakub Dawidek case MD_SWAP: 14489b00ca19SPoul-Henning Kamp sc->start = mdstart_swap; 1449b830359bSPawel Jakub Dawidek error = mdcreate_swap(sc, mdio, td); 1450b830359bSPawel Jakub Dawidek break; 1451*0efd9bfdSEdward Tomasz Napierala case MD_NULL: 1452*0efd9bfdSEdward Tomasz Napierala sc->start = mdstart_null; 1453*0efd9bfdSEdward Tomasz Napierala error = mdcreate_null(sc, mdio, td); 1454*0efd9bfdSEdward Tomasz Napierala break; 1455b830359bSPawel Jakub Dawidek } 1456b830359bSPawel Jakub Dawidek if (error != 0) { 1457b830359bSPawel Jakub Dawidek mddestroy(sc, td); 1458b830359bSPawel Jakub Dawidek return (error); 1459b830359bSPawel Jakub Dawidek } 14609b00ca19SPoul-Henning Kamp 14619b00ca19SPoul-Henning Kamp /* Prune off any residual fractional sector */ 14629b00ca19SPoul-Henning Kamp i = sc->mediasize % sc->sectorsize; 14639b00ca19SPoul-Henning Kamp sc->mediasize -= i; 14649b00ca19SPoul-Henning Kamp 1465b830359bSPawel Jakub Dawidek mdinit(sc); 1466b830359bSPawel Jakub Dawidek return (0); 14678f8def9eSPoul-Henning Kamp case MDIOCDETACH: 1468a9ebb311SEdward Tomasz Napierala if (mdio->md_mediasize != 0 || 1469a9ebb311SEdward Tomasz Napierala (mdio->md_options & ~MD_FORCE) != 0) 14708f8def9eSPoul-Henning Kamp return (EINVAL); 14719b00ca19SPoul-Henning Kamp 14729b00ca19SPoul-Henning Kamp sc = mdfind(mdio->md_unit); 14739b00ca19SPoul-Henning Kamp if (sc == NULL) 14749b00ca19SPoul-Henning Kamp return (ENOENT); 1475a9ebb311SEdward Tomasz Napierala if (sc->opencount != 0 && !(sc->flags & MD_FORCE) && 1476a9ebb311SEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 14779b00ca19SPoul-Henning Kamp return (EBUSY); 14789b00ca19SPoul-Henning Kamp return (mddestroy(sc, td)); 1479dc604f0cSEdward Tomasz Napierala case MDIOCRESIZE: 1480dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0) 1481dc604f0cSEdward Tomasz Napierala return (EINVAL); 1482dc604f0cSEdward Tomasz Napierala 1483dc604f0cSEdward Tomasz Napierala sc = mdfind(mdio->md_unit); 1484dc604f0cSEdward Tomasz Napierala if (sc == NULL) 1485dc604f0cSEdward Tomasz Napierala return (ENOENT); 14868cb51643SJaakko Heinonen if (mdio->md_mediasize < sc->sectorsize) 14878cb51643SJaakko Heinonen return (EINVAL); 1488dc604f0cSEdward Tomasz Napierala if (mdio->md_mediasize < sc->mediasize && 1489dc604f0cSEdward Tomasz Napierala !(sc->flags & MD_FORCE) && 1490dc604f0cSEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 1491dc604f0cSEdward Tomasz Napierala return (EBUSY); 1492dc604f0cSEdward Tomasz Napierala return (mdresize(sc, mdio)); 1493174b5e9aSPoul-Henning Kamp case MDIOCQUERY: 1494174b5e9aSPoul-Henning Kamp sc = mdfind(mdio->md_unit); 1495174b5e9aSPoul-Henning Kamp if (sc == NULL) 1496174b5e9aSPoul-Henning Kamp return (ENOENT); 1497174b5e9aSPoul-Henning Kamp mdio->md_type = sc->type; 1498174b5e9aSPoul-Henning Kamp mdio->md_options = sc->flags; 1499b830359bSPawel Jakub Dawidek mdio->md_mediasize = sc->mediasize; 1500b830359bSPawel Jakub Dawidek mdio->md_sectorsize = sc->sectorsize; 15019b00ca19SPoul-Henning Kamp if (sc->type == MD_VNODE) 150288b5b78dSPawel Jakub Dawidek error = copyout(sc->file, mdio->md_file, 150388b5b78dSPawel Jakub Dawidek strlen(sc->file) + 1); 150488b5b78dSPawel Jakub Dawidek return (error); 150516bcbe8cSPoul-Henning Kamp case MDIOCLIST: 150616bcbe8cSPoul-Henning Kamp i = 1; 150716bcbe8cSPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 150816bcbe8cSPoul-Henning Kamp if (i == MDNPAD - 1) 150916bcbe8cSPoul-Henning Kamp mdio->md_pad[i] = -1; 151016bcbe8cSPoul-Henning Kamp else 151116bcbe8cSPoul-Henning Kamp mdio->md_pad[i++] = sc->unit; 151216bcbe8cSPoul-Henning Kamp } 151316bcbe8cSPoul-Henning Kamp mdio->md_pad[0] = i - 1; 151416bcbe8cSPoul-Henning Kamp return (0); 15158f8def9eSPoul-Henning Kamp default: 15168f8def9eSPoul-Henning Kamp return (ENOIOCTL); 15178f8def9eSPoul-Henning Kamp }; 15189b00ca19SPoul-Henning Kamp } 15199b00ca19SPoul-Henning Kamp 15209b00ca19SPoul-Henning Kamp static int 15219b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 15229b00ca19SPoul-Henning Kamp { 15239b00ca19SPoul-Henning Kamp int error; 15249b00ca19SPoul-Henning Kamp 15259b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 15269b00ca19SPoul-Henning Kamp error = xmdctlioctl(dev, cmd, addr, flags, td); 15279b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 15289b00ca19SPoul-Henning Kamp return (error); 15293f54a085SPoul-Henning Kamp } 15303f54a085SPoul-Henning Kamp 153100a6a3c6SPoul-Henning Kamp static void 1532341b240dSJaakko Heinonen md_preloaded(u_char *image, size_t length, const char *name) 1533637f671aSPoul-Henning Kamp { 1534637f671aSPoul-Henning Kamp struct md_s *sc; 15359b00ca19SPoul-Henning Kamp int error; 1536637f671aSPoul-Henning Kamp 1537947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, MD_PRELOAD); 1538637f671aSPoul-Henning Kamp if (sc == NULL) 1539637f671aSPoul-Henning Kamp return; 1540b830359bSPawel Jakub Dawidek sc->mediasize = length; 1541b830359bSPawel Jakub Dawidek sc->sectorsize = DEV_BSIZE; 1542637f671aSPoul-Henning Kamp sc->pl_ptr = image; 1543637f671aSPoul-Henning Kamp sc->pl_len = length; 15449b00ca19SPoul-Henning Kamp sc->start = mdstart_preload; 15455d4ca75eSLuigi Rizzo #ifdef MD_ROOT 1546637f671aSPoul-Henning Kamp if (sc->unit == 0) 154722ff74b2SMarcel Moolenaar rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0"; 15485d4ca75eSLuigi Rizzo #endif 1549637f671aSPoul-Henning Kamp mdinit(sc); 1550341b240dSJaakko Heinonen if (name != NULL) { 1551341b240dSJaakko Heinonen printf("%s%d: Preloaded image <%s> %zd bytes at %p\n", 1552341b240dSJaakko Heinonen MD_NAME, sc->unit, name, length, image); 1553341b240dSJaakko Heinonen } 1554637f671aSPoul-Henning Kamp } 1555637f671aSPoul-Henning Kamp 1556637f671aSPoul-Henning Kamp static void 155719945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused) 155800a6a3c6SPoul-Henning Kamp { 155995f1a897SPoul-Henning Kamp caddr_t mod; 156095f1a897SPoul-Henning Kamp u_char *ptr, *name, *type; 156195f1a897SPoul-Henning Kamp unsigned len; 1562d12fc952SKonstantin Belousov int i; 1563d12fc952SKonstantin Belousov 1564d12fc952SKonstantin Belousov /* figure out log2(NINDIR) */ 1565d12fc952SKonstantin Belousov for (i = NINDIR, nshift = -1; i; nshift++) 1566d12fc952SKonstantin Belousov i >>= 1; 156795f1a897SPoul-Henning Kamp 15680a937206SPoul-Henning Kamp mod = NULL; 15699b00ca19SPoul-Henning Kamp sx_init(&md_sx, "MD config lock"); 15700a937206SPoul-Henning Kamp g_topology_unlock(); 1571f4e7c5a8SJaakko Heinonen md_uh = new_unrhdr(0, INT_MAX, NULL); 157271e4fff8SPoul-Henning Kamp #ifdef MD_ROOT_SIZE 15739b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1574341b240dSJaakko Heinonen md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL); 15759b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 157671e4fff8SPoul-Henning Kamp #endif 15779b00ca19SPoul-Henning Kamp /* XXX: are preload_* static or do they need Giant ? */ 157895f1a897SPoul-Henning Kamp while ((mod = preload_search_next_name(mod)) != NULL) { 157995f1a897SPoul-Henning Kamp name = (char *)preload_search_info(mod, MODINFO_NAME); 158095f1a897SPoul-Henning Kamp if (name == NULL) 158195f1a897SPoul-Henning Kamp continue; 15829b00ca19SPoul-Henning Kamp type = (char *)preload_search_info(mod, MODINFO_TYPE); 158395f1a897SPoul-Henning Kamp if (type == NULL) 158495f1a897SPoul-Henning Kamp continue; 158571e4fff8SPoul-Henning Kamp if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 158695f1a897SPoul-Henning Kamp continue; 15878d5ac6c3SMarcel Moolenaar ptr = preload_fetch_addr(mod); 15888d5ac6c3SMarcel Moolenaar len = preload_fetch_size(mod); 15898d5ac6c3SMarcel Moolenaar if (ptr != NULL && len != 0) { 15909b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1591341b240dSJaakko Heinonen md_preloaded(ptr, len, name); 15929b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 159395f1a897SPoul-Henning Kamp } 15948d5ac6c3SMarcel Moolenaar } 159559ec9023SKonstantin Belousov md_vnode_pbuf_freecnt = nswbuf / 10; 159606d425f9SEd Schouten status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL, 159710b0e058SDima Dorfman 0600, MDCTL_NAME); 15980eb14309SPoul-Henning Kamp g_topology_lock(); 159900a6a3c6SPoul-Henning Kamp } 160000a6a3c6SPoul-Henning Kamp 160119945697SPoul-Henning Kamp static void 1602c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1603c27a8954SWojciech A. Koszek struct g_consumer *cp __unused, struct g_provider *pp) 1604c27a8954SWojciech A. Koszek { 1605c27a8954SWojciech A. Koszek struct md_s *mp; 1606c27a8954SWojciech A. Koszek char *type; 1607c27a8954SWojciech A. Koszek 1608c27a8954SWojciech A. Koszek mp = gp->softc; 1609c27a8954SWojciech A. Koszek if (mp == NULL) 1610c27a8954SWojciech A. Koszek return; 1611c27a8954SWojciech A. Koszek 1612c27a8954SWojciech A. Koszek switch (mp->type) { 1613c27a8954SWojciech A. Koszek case MD_MALLOC: 1614c27a8954SWojciech A. Koszek type = "malloc"; 1615c27a8954SWojciech A. Koszek break; 1616c27a8954SWojciech A. Koszek case MD_PRELOAD: 1617c27a8954SWojciech A. Koszek type = "preload"; 1618c27a8954SWojciech A. Koszek break; 1619c27a8954SWojciech A. Koszek case MD_VNODE: 1620c27a8954SWojciech A. Koszek type = "vnode"; 1621c27a8954SWojciech A. Koszek break; 1622c27a8954SWojciech A. Koszek case MD_SWAP: 1623c27a8954SWojciech A. Koszek type = "swap"; 1624c27a8954SWojciech A. Koszek break; 1625*0efd9bfdSEdward Tomasz Napierala case MD_NULL: 1626*0efd9bfdSEdward Tomasz Napierala type = "null"; 1627*0efd9bfdSEdward Tomasz Napierala break; 1628c27a8954SWojciech A. Koszek default: 1629c27a8954SWojciech A. Koszek type = "unknown"; 1630c27a8954SWojciech A. Koszek break; 1631c27a8954SWojciech A. Koszek } 1632c27a8954SWojciech A. Koszek 1633c27a8954SWojciech A. Koszek if (pp != NULL) { 1634c27a8954SWojciech A. Koszek if (indent == NULL) { 1635c27a8954SWojciech A. Koszek sbuf_printf(sb, " u %d", mp->unit); 1636c27a8954SWojciech A. Koszek sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize); 1637c27a8954SWojciech A. Koszek sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads); 1638c27a8954SWojciech A. Koszek sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors); 1639c27a8954SWojciech A. Koszek sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize); 1640c27a8954SWojciech A. Koszek sbuf_printf(sb, " t %s", type); 1641c27a8954SWojciech A. Koszek if (mp->type == MD_VNODE && mp->vnode != NULL) 1642c27a8954SWojciech A. Koszek sbuf_printf(sb, " file %s", mp->file); 1643c27a8954SWojciech A. Koszek } else { 1644c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, 1645c27a8954SWojciech A. Koszek mp->unit); 1646c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n", 1647c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->sectorsize); 1648c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n", 1649c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwheads); 1650c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n", 1651c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwsectors); 1652c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<length>%ju</length>\n", 1653c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->mediasize); 16541f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<compression>%s</compression>\n", indent, 16551f192809SAndrey V. Elsukov (mp->flags & MD_COMPRESS) == 0 ? "off": "on"); 16561f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<access>%s</access>\n", indent, 16571f192809SAndrey V. Elsukov (mp->flags & MD_READONLY) == 0 ? "read-write": 16581f192809SAndrey V. Elsukov "read-only"); 1659c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<type>%s</type>\n", indent, 1660c27a8954SWojciech A. Koszek type); 1661c27a8954SWojciech A. Koszek if (mp->type == MD_VNODE && mp->vnode != NULL) 1662c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<file>%s</file>\n", 1663c27a8954SWojciech A. Koszek indent, mp->file); 1664c27a8954SWojciech A. Koszek } 1665c27a8954SWojciech A. Koszek } 1666c27a8954SWojciech A. Koszek } 1667c27a8954SWojciech A. Koszek 1668c27a8954SWojciech A. Koszek static void 166919945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused) 167057e9624eSPoul-Henning Kamp { 16719d4b5945SMaxim Sobolev 16729b00ca19SPoul-Henning Kamp sx_destroy(&md_sx); 167319945697SPoul-Henning Kamp if (status_dev != NULL) 167457e9624eSPoul-Henning Kamp destroy_dev(status_dev); 1675f4e7c5a8SJaakko Heinonen delete_unrhdr(md_uh); 167657e9624eSPoul-Henning Kamp } 1677