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> 92*ec170744SAndrey V. Elsukov #include <geom/geom_int.h> 936f4f00f1SPoul-Henning Kamp 948f8def9eSPoul-Henning Kamp #include <vm/vm.h> 951c771f92SKonstantin Belousov #include <vm/vm_param.h> 968f8def9eSPoul-Henning Kamp #include <vm/vm_object.h> 978f8def9eSPoul-Henning Kamp #include <vm/vm_page.h> 988f8def9eSPoul-Henning Kamp #include <vm/vm_pager.h> 998f8def9eSPoul-Henning Kamp #include <vm/swap_pager.h> 100f43b2bacSPoul-Henning Kamp #include <vm/uma.h> 1013f54a085SPoul-Henning Kamp 10257e9624eSPoul-Henning Kamp #define MD_MODVER 1 10357e9624eSPoul-Henning Kamp 1045c97ca54SIan Dowse #define MD_SHUTDOWN 0x10000 /* Tell worker thread to terminate. */ 105a08d2e7fSJohn Baldwin #define MD_EXITING 0x20000 /* Worker thread is exiting. */ 1065c97ca54SIan Dowse 107f2744793SSheldon Hearn #ifndef MD_NSECT 108f2744793SSheldon Hearn #define MD_NSECT (10000 * 2) 10933edfabeSPoul-Henning Kamp #endif 11033edfabeSPoul-Henning Kamp 1115bb84bc8SRobert Watson static MALLOC_DEFINE(M_MD, "md_disk", "Memory Disk"); 1125bb84bc8SRobert Watson static MALLOC_DEFINE(M_MDSECT, "md_sectors", "Memory Disk Sectors"); 11300a6a3c6SPoul-Henning Kamp 11471e4fff8SPoul-Henning Kamp static int md_debug; 1153eb9ab52SEitan Adler SYSCTL_INT(_debug, OID_AUTO, mddebug, CTLFLAG_RW, &md_debug, 0, 1163eb9ab52SEitan Adler "Enable md(4) debug messages"); 117c44d423eSKonstantin Belousov static int md_malloc_wait; 1183eb9ab52SEitan Adler SYSCTL_INT(_vm, OID_AUTO, md_malloc_wait, CTLFLAG_RW, &md_malloc_wait, 0, 1193eb9ab52SEitan Adler "Allow malloc to wait for memory allocations"); 12000a6a3c6SPoul-Henning Kamp 12122ff74b2SMarcel Moolenaar #if defined(MD_ROOT) && !defined(MD_ROOT_FSTYPE) 12222ff74b2SMarcel Moolenaar #define MD_ROOT_FSTYPE "ufs" 12322ff74b2SMarcel Moolenaar #endif 12422ff74b2SMarcel Moolenaar 12571e4fff8SPoul-Henning Kamp #if defined(MD_ROOT) && defined(MD_ROOT_SIZE) 126de64f22aSLuigi Rizzo /* 127de64f22aSLuigi Rizzo * Preloaded image gets put here. 128de64f22aSLuigi Rizzo * Applications that patch the object with the image can determine 129de64f22aSLuigi Rizzo * the size looking at the start and end markers (strings), 130de64f22aSLuigi Rizzo * so we want them contiguous. 131de64f22aSLuigi Rizzo */ 132de64f22aSLuigi Rizzo static struct { 133de64f22aSLuigi Rizzo u_char start[MD_ROOT_SIZE*1024]; 134de64f22aSLuigi Rizzo u_char end[128]; 135de64f22aSLuigi Rizzo } mfs_root = { 136de64f22aSLuigi Rizzo .start = "MFS Filesystem goes here", 137de64f22aSLuigi Rizzo .end = "MFS Filesystem had better STOP here", 138de64f22aSLuigi Rizzo }; 13971e4fff8SPoul-Henning Kamp #endif 14071e4fff8SPoul-Henning Kamp 14119945697SPoul-Henning Kamp static g_init_t g_md_init; 14219945697SPoul-Henning Kamp static g_fini_t g_md_fini; 14319945697SPoul-Henning Kamp static g_start_t g_md_start; 14419945697SPoul-Henning Kamp static g_access_t g_md_access; 145b42f40b8SJaakko Heinonen static void g_md_dumpconf(struct sbuf *sb, const char *indent, 146b42f40b8SJaakko Heinonen struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp); 1470eb14309SPoul-Henning Kamp 14889c9c53dSPoul-Henning Kamp static struct cdev *status_dev = 0; 1499b00ca19SPoul-Henning Kamp static struct sx md_sx; 150f4e7c5a8SJaakko Heinonen static struct unrhdr *md_uh; 15157e9624eSPoul-Henning Kamp 152a522a159SPoul-Henning Kamp static d_ioctl_t mdctlioctl; 1538f8def9eSPoul-Henning Kamp 1548f8def9eSPoul-Henning Kamp static struct cdevsw mdctl_cdevsw = { 155dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 1567ac40f5fSPoul-Henning Kamp .d_ioctl = mdctlioctl, 1577ac40f5fSPoul-Henning Kamp .d_name = MD_NAME, 15800a6a3c6SPoul-Henning Kamp }; 15900a6a3c6SPoul-Henning Kamp 16019945697SPoul-Henning Kamp struct g_class g_md_class = { 16119945697SPoul-Henning Kamp .name = "MD", 1625721c9c7SPoul-Henning Kamp .version = G_VERSION, 16319945697SPoul-Henning Kamp .init = g_md_init, 16419945697SPoul-Henning Kamp .fini = g_md_fini, 16519945697SPoul-Henning Kamp .start = g_md_start, 16619945697SPoul-Henning Kamp .access = g_md_access, 167c27a8954SWojciech A. Koszek .dumpconf = g_md_dumpconf, 16819945697SPoul-Henning Kamp }; 16919945697SPoul-Henning Kamp 17019945697SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_md_class, g_md); 17119945697SPoul-Henning Kamp 1720cfaeeeeSPoul-Henning Kamp 17313e403fdSAntoine Brodin static LIST_HEAD(, md_s) md_softc_list = LIST_HEAD_INITIALIZER(md_softc_list); 1743f54a085SPoul-Henning Kamp 175c6517568SPoul-Henning Kamp #define NINDIR (PAGE_SIZE / sizeof(uintptr_t)) 176c6517568SPoul-Henning Kamp #define NMASK (NINDIR-1) 177c6517568SPoul-Henning Kamp static int nshift; 178c6517568SPoul-Henning Kamp 17959ec9023SKonstantin Belousov static int md_vnode_pbuf_freecnt; 18059ec9023SKonstantin Belousov 181c6517568SPoul-Henning Kamp struct indir { 182c6517568SPoul-Henning Kamp uintptr_t *array; 1838b149b51SJohn Baldwin u_int total; 1848b149b51SJohn Baldwin u_int used; 1858b149b51SJohn Baldwin u_int shift; 186c6517568SPoul-Henning Kamp }; 187c6517568SPoul-Henning Kamp 18800a6a3c6SPoul-Henning Kamp struct md_s { 18900a6a3c6SPoul-Henning Kamp int unit; 1903f54a085SPoul-Henning Kamp LIST_ENTRY(md_s) list; 1918177437dSPoul-Henning Kamp struct bio_queue_head bio_queue; 1920f8500a5SPoul-Henning Kamp struct mtx queue_mtx; 19340ea77a0SAlexander Motin struct mtx stat_mtx; 19489c9c53dSPoul-Henning Kamp struct cdev *dev; 1958f8def9eSPoul-Henning Kamp enum md_types type; 196b830359bSPawel Jakub Dawidek off_t mediasize; 197b830359bSPawel Jakub Dawidek unsigned sectorsize; 198fe603109SMaxim Sobolev unsigned opencount; 1994e8bfe14SPoul-Henning Kamp unsigned fwheads; 2004e8bfe14SPoul-Henning Kamp unsigned fwsectors; 2018f8def9eSPoul-Henning Kamp unsigned flags; 202f43b2bacSPoul-Henning Kamp char name[20]; 2035c97ca54SIan Dowse struct proc *procp; 2046f4f00f1SPoul-Henning Kamp struct g_geom *gp; 2056f4f00f1SPoul-Henning Kamp struct g_provider *pp; 2069b00ca19SPoul-Henning Kamp int (*start)(struct md_s *sc, struct bio *bp); 207a03be42dSMaxim Sobolev struct devstat *devstat; 20895f1a897SPoul-Henning Kamp 20995f1a897SPoul-Henning Kamp /* MD_MALLOC related fields */ 210c6517568SPoul-Henning Kamp struct indir *indir; 211f43b2bacSPoul-Henning Kamp uma_zone_t uma; 21200a6a3c6SPoul-Henning Kamp 21395f1a897SPoul-Henning Kamp /* MD_PRELOAD related fields */ 21495f1a897SPoul-Henning Kamp u_char *pl_ptr; 215b830359bSPawel Jakub Dawidek size_t pl_len; 21600a6a3c6SPoul-Henning Kamp 2178f8def9eSPoul-Henning Kamp /* MD_VNODE related fields */ 2188f8def9eSPoul-Henning Kamp struct vnode *vnode; 21961a6eb62SPawel Jakub Dawidek char file[PATH_MAX]; 2208f8def9eSPoul-Henning Kamp struct ucred *cred; 2218f8def9eSPoul-Henning Kamp 222e0cebb40SDima Dorfman /* MD_SWAP related fields */ 2238f8def9eSPoul-Henning Kamp vm_object_t object; 2248f8def9eSPoul-Henning Kamp }; 22500a6a3c6SPoul-Henning Kamp 226c6517568SPoul-Henning Kamp static struct indir * 2278b149b51SJohn Baldwin new_indir(u_int shift) 228c6517568SPoul-Henning Kamp { 229c6517568SPoul-Henning Kamp struct indir *ip; 230c6517568SPoul-Henning Kamp 231c44d423eSKonstantin Belousov ip = malloc(sizeof *ip, M_MD, (md_malloc_wait ? M_WAITOK : M_NOWAIT) 232c44d423eSKonstantin Belousov | M_ZERO); 233c6517568SPoul-Henning Kamp if (ip == NULL) 234c6517568SPoul-Henning Kamp return (NULL); 235c6517568SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 236c44d423eSKonstantin Belousov M_MDSECT, (md_malloc_wait ? M_WAITOK : M_NOWAIT) | M_ZERO); 237c6517568SPoul-Henning Kamp if (ip->array == NULL) { 238c6517568SPoul-Henning Kamp free(ip, M_MD); 239c6517568SPoul-Henning Kamp return (NULL); 240c6517568SPoul-Henning Kamp } 241c6517568SPoul-Henning Kamp ip->total = NINDIR; 242c6517568SPoul-Henning Kamp ip->shift = shift; 243c6517568SPoul-Henning Kamp return (ip); 244c6517568SPoul-Henning Kamp } 245c6517568SPoul-Henning Kamp 246c6517568SPoul-Henning Kamp static void 247c6517568SPoul-Henning Kamp del_indir(struct indir *ip) 248c6517568SPoul-Henning Kamp { 249c6517568SPoul-Henning Kamp 250f43b2bacSPoul-Henning Kamp free(ip->array, M_MDSECT); 251c6517568SPoul-Henning Kamp free(ip, M_MD); 252c6517568SPoul-Henning Kamp } 253c6517568SPoul-Henning Kamp 254f43b2bacSPoul-Henning Kamp static void 255f43b2bacSPoul-Henning Kamp destroy_indir(struct md_s *sc, struct indir *ip) 256f43b2bacSPoul-Henning Kamp { 257f43b2bacSPoul-Henning Kamp int i; 258f43b2bacSPoul-Henning Kamp 259f43b2bacSPoul-Henning Kamp for (i = 0; i < NINDIR; i++) { 260f43b2bacSPoul-Henning Kamp if (!ip->array[i]) 261f43b2bacSPoul-Henning Kamp continue; 262f43b2bacSPoul-Henning Kamp if (ip->shift) 263f43b2bacSPoul-Henning Kamp destroy_indir(sc, (struct indir*)(ip->array[i])); 264f43b2bacSPoul-Henning Kamp else if (ip->array[i] > 255) 265f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void *)(ip->array[i])); 266f43b2bacSPoul-Henning Kamp } 267f43b2bacSPoul-Henning Kamp del_indir(ip); 268f43b2bacSPoul-Henning Kamp } 269f43b2bacSPoul-Henning Kamp 270c6517568SPoul-Henning Kamp /* 2716c3cd0e2SMaxim Konovalov * This function does the math and allocates the top level "indir" structure 272c6517568SPoul-Henning Kamp * for a device of "size" sectors. 273c6517568SPoul-Henning Kamp */ 274c6517568SPoul-Henning Kamp 275c6517568SPoul-Henning Kamp static struct indir * 276c6517568SPoul-Henning Kamp dimension(off_t size) 277c6517568SPoul-Henning Kamp { 278c6517568SPoul-Henning Kamp off_t rcnt; 279c6517568SPoul-Henning Kamp struct indir *ip; 280d12fc952SKonstantin Belousov int layer; 281c6517568SPoul-Henning Kamp 282c6517568SPoul-Henning Kamp rcnt = size; 283c6517568SPoul-Henning Kamp layer = 0; 284c6517568SPoul-Henning Kamp while (rcnt > NINDIR) { 285c6517568SPoul-Henning Kamp rcnt /= NINDIR; 286c6517568SPoul-Henning Kamp layer++; 287c6517568SPoul-Henning Kamp } 288c6517568SPoul-Henning Kamp 289c6517568SPoul-Henning Kamp /* 290c6517568SPoul-Henning Kamp * XXX: the top layer is probably not fully populated, so we allocate 29183e13864SPoul-Henning Kamp * too much space for ip->array in here. 292c6517568SPoul-Henning Kamp */ 29383e13864SPoul-Henning Kamp ip = malloc(sizeof *ip, M_MD, M_WAITOK | M_ZERO); 29483e13864SPoul-Henning Kamp ip->array = malloc(sizeof(uintptr_t) * NINDIR, 29583e13864SPoul-Henning Kamp M_MDSECT, M_WAITOK | M_ZERO); 29683e13864SPoul-Henning Kamp ip->total = NINDIR; 29783e13864SPoul-Henning Kamp ip->shift = layer * nshift; 298c6517568SPoul-Henning Kamp return (ip); 299c6517568SPoul-Henning Kamp } 300c6517568SPoul-Henning Kamp 301c6517568SPoul-Henning Kamp /* 302c6517568SPoul-Henning Kamp * Read a given sector 303c6517568SPoul-Henning Kamp */ 304c6517568SPoul-Henning Kamp 305c6517568SPoul-Henning Kamp static uintptr_t 306c6517568SPoul-Henning Kamp s_read(struct indir *ip, off_t offset) 307c6517568SPoul-Henning Kamp { 308c6517568SPoul-Henning Kamp struct indir *cip; 309c6517568SPoul-Henning Kamp int idx; 310c6517568SPoul-Henning Kamp uintptr_t up; 311c6517568SPoul-Henning Kamp 312c6517568SPoul-Henning Kamp if (md_debug > 1) 3136569d6f3SMaxime Henrion printf("s_read(%jd)\n", (intmax_t)offset); 314c6517568SPoul-Henning Kamp up = 0; 315c6517568SPoul-Henning Kamp for (cip = ip; cip != NULL;) { 316c6517568SPoul-Henning Kamp if (cip->shift) { 317c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 318c6517568SPoul-Henning Kamp up = cip->array[idx]; 319c6517568SPoul-Henning Kamp cip = (struct indir *)up; 320c6517568SPoul-Henning Kamp continue; 321c6517568SPoul-Henning Kamp } 322c6517568SPoul-Henning Kamp idx = offset & NMASK; 323c6517568SPoul-Henning Kamp return (cip->array[idx]); 324c6517568SPoul-Henning Kamp } 325c6517568SPoul-Henning Kamp return (0); 326c6517568SPoul-Henning Kamp } 327c6517568SPoul-Henning Kamp 328c6517568SPoul-Henning Kamp /* 329c6517568SPoul-Henning Kamp * Write a given sector, prune the tree if the value is 0 330c6517568SPoul-Henning Kamp */ 331c6517568SPoul-Henning Kamp 332c6517568SPoul-Henning Kamp static int 333fde2a2e4SPoul-Henning Kamp s_write(struct indir *ip, off_t offset, uintptr_t ptr) 334c6517568SPoul-Henning Kamp { 335c6517568SPoul-Henning Kamp struct indir *cip, *lip[10]; 336c6517568SPoul-Henning Kamp int idx, li; 337c6517568SPoul-Henning Kamp uintptr_t up; 338c6517568SPoul-Henning Kamp 339c6517568SPoul-Henning Kamp if (md_debug > 1) 3406569d6f3SMaxime Henrion printf("s_write(%jd, %p)\n", (intmax_t)offset, (void *)ptr); 341c6517568SPoul-Henning Kamp up = 0; 342c6517568SPoul-Henning Kamp li = 0; 343c6517568SPoul-Henning Kamp cip = ip; 344c6517568SPoul-Henning Kamp for (;;) { 345c6517568SPoul-Henning Kamp lip[li++] = cip; 346c6517568SPoul-Henning Kamp if (cip->shift) { 347c6517568SPoul-Henning Kamp idx = (offset >> cip->shift) & NMASK; 348c6517568SPoul-Henning Kamp up = cip->array[idx]; 349c6517568SPoul-Henning Kamp if (up != 0) { 350c6517568SPoul-Henning Kamp cip = (struct indir *)up; 351c6517568SPoul-Henning Kamp continue; 352c6517568SPoul-Henning Kamp } 353c6517568SPoul-Henning Kamp /* Allocate branch */ 354c6517568SPoul-Henning Kamp cip->array[idx] = 355c6517568SPoul-Henning Kamp (uintptr_t)new_indir(cip->shift - nshift); 356c6517568SPoul-Henning Kamp if (cip->array[idx] == 0) 357975b628fSPoul-Henning Kamp return (ENOSPC); 358c6517568SPoul-Henning Kamp cip->used++; 359c6517568SPoul-Henning Kamp up = cip->array[idx]; 360c6517568SPoul-Henning Kamp cip = (struct indir *)up; 361c6517568SPoul-Henning Kamp continue; 362c6517568SPoul-Henning Kamp } 363c6517568SPoul-Henning Kamp /* leafnode */ 364c6517568SPoul-Henning Kamp idx = offset & NMASK; 365c6517568SPoul-Henning Kamp up = cip->array[idx]; 366c6517568SPoul-Henning Kamp if (up != 0) 367c6517568SPoul-Henning Kamp cip->used--; 368c6517568SPoul-Henning Kamp cip->array[idx] = ptr; 369c6517568SPoul-Henning Kamp if (ptr != 0) 370c6517568SPoul-Henning Kamp cip->used++; 371c6517568SPoul-Henning Kamp break; 372c6517568SPoul-Henning Kamp } 373c6517568SPoul-Henning Kamp if (cip->used != 0 || li == 1) 374c6517568SPoul-Henning Kamp return (0); 375c6517568SPoul-Henning Kamp li--; 376c6517568SPoul-Henning Kamp while (cip->used == 0 && cip != ip) { 377c6517568SPoul-Henning Kamp li--; 378c6517568SPoul-Henning Kamp idx = (offset >> lip[li]->shift) & NMASK; 379c6517568SPoul-Henning Kamp up = lip[li]->array[idx]; 380c6517568SPoul-Henning Kamp KASSERT(up == (uintptr_t)cip, ("md screwed up")); 381c6517568SPoul-Henning Kamp del_indir(cip); 3824a6a94d8SArchie Cobbs lip[li]->array[idx] = 0; 383c6517568SPoul-Henning Kamp lip[li]->used--; 384c6517568SPoul-Henning Kamp cip = lip[li]; 385c6517568SPoul-Henning Kamp } 386c6517568SPoul-Henning Kamp return (0); 387c6517568SPoul-Henning Kamp } 388c6517568SPoul-Henning Kamp 3896f4f00f1SPoul-Henning Kamp 3906f4f00f1SPoul-Henning Kamp static int 3916f4f00f1SPoul-Henning Kamp g_md_access(struct g_provider *pp, int r, int w, int e) 3926f4f00f1SPoul-Henning Kamp { 3936f4f00f1SPoul-Henning Kamp struct md_s *sc; 3946f4f00f1SPoul-Henning Kamp 3956f4f00f1SPoul-Henning Kamp sc = pp->geom->softc; 39641c8b468SEdward Tomasz Napierala if (sc == NULL) { 39741c8b468SEdward Tomasz Napierala if (r <= 0 && w <= 0 && e <= 0) 39841c8b468SEdward Tomasz Napierala return (0); 3996b60a2cdSPoul-Henning Kamp return (ENXIO); 40041c8b468SEdward Tomasz Napierala } 4016f4f00f1SPoul-Henning Kamp r += pp->acr; 4026f4f00f1SPoul-Henning Kamp w += pp->acw; 4036f4f00f1SPoul-Henning Kamp e += pp->ace; 40486776891SChristian S.J. Peron if ((sc->flags & MD_READONLY) != 0 && w > 0) 40586776891SChristian S.J. Peron return (EROFS); 4066f4f00f1SPoul-Henning Kamp if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 4076f4f00f1SPoul-Henning Kamp sc->opencount = 1; 4086f4f00f1SPoul-Henning Kamp } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 4096f4f00f1SPoul-Henning Kamp sc->opencount = 0; 4106f4f00f1SPoul-Henning Kamp } 4116f4f00f1SPoul-Henning Kamp return (0); 4126f4f00f1SPoul-Henning Kamp } 4136f4f00f1SPoul-Henning Kamp 4146f4f00f1SPoul-Henning Kamp static void 4156f4f00f1SPoul-Henning Kamp g_md_start(struct bio *bp) 4166f4f00f1SPoul-Henning Kamp { 4176f4f00f1SPoul-Henning Kamp struct md_s *sc; 4186f4f00f1SPoul-Henning Kamp 4196f4f00f1SPoul-Henning Kamp sc = bp->bio_to->geom->softc; 42040ea77a0SAlexander Motin if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) { 42140ea77a0SAlexander Motin mtx_lock(&sc->stat_mtx); 422a03be42dSMaxim Sobolev devstat_start_transaction_bio(sc->devstat, bp); 42340ea77a0SAlexander Motin mtx_unlock(&sc->stat_mtx); 42440ea77a0SAlexander Motin } 4250f8500a5SPoul-Henning Kamp mtx_lock(&sc->queue_mtx); 426891619a6SPoul-Henning Kamp bioq_disksort(&sc->bio_queue, bp); 4274b07ede4SPawel Jakub Dawidek mtx_unlock(&sc->queue_mtx); 428e4cdd0d4SPawel Jakub Dawidek wakeup(sc); 4296f4f00f1SPoul-Henning Kamp } 4306f4f00f1SPoul-Henning Kamp 43159ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_ZERO 1 43259ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_FILL 2 43359ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_READ 3 43459ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_WRITE 4 43559ec9023SKonstantin Belousov #define MD_MALLOC_MOVE_CMP 5 43659ec9023SKonstantin Belousov 43759ec9023SKonstantin Belousov static int 43859ec9023SKonstantin Belousov md_malloc_move(vm_page_t **mp, int *ma_offs, unsigned sectorsize, 43959ec9023SKonstantin Belousov void *ptr, u_char fill, int op) 44059ec9023SKonstantin Belousov { 44159ec9023SKonstantin Belousov struct sf_buf *sf; 44259ec9023SKonstantin Belousov vm_page_t m, *mp1; 44359ec9023SKonstantin Belousov char *p, first; 44459ec9023SKonstantin Belousov off_t *uc; 44559ec9023SKonstantin Belousov unsigned n; 44659ec9023SKonstantin Belousov int error, i, ma_offs1, sz, first_read; 44759ec9023SKonstantin Belousov 44859ec9023SKonstantin Belousov m = NULL; 44959ec9023SKonstantin Belousov error = 0; 45059ec9023SKonstantin Belousov sf = NULL; 45159ec9023SKonstantin Belousov /* if (op == MD_MALLOC_MOVE_CMP) { gcc */ 45259ec9023SKonstantin Belousov first = 0; 45359ec9023SKonstantin Belousov first_read = 0; 45459ec9023SKonstantin Belousov uc = ptr; 45559ec9023SKonstantin Belousov mp1 = *mp; 45659ec9023SKonstantin Belousov ma_offs1 = *ma_offs; 45759ec9023SKonstantin Belousov /* } */ 45859ec9023SKonstantin Belousov sched_pin(); 45959ec9023SKonstantin Belousov for (n = sectorsize; n != 0; n -= sz) { 46059ec9023SKonstantin Belousov sz = imin(PAGE_SIZE - *ma_offs, n); 46159ec9023SKonstantin Belousov if (m != **mp) { 46259ec9023SKonstantin Belousov if (sf != NULL) 46359ec9023SKonstantin Belousov sf_buf_free(sf); 46459ec9023SKonstantin Belousov m = **mp; 46559ec9023SKonstantin Belousov sf = sf_buf_alloc(m, SFB_CPUPRIVATE | 46659ec9023SKonstantin Belousov (md_malloc_wait ? 0 : SFB_NOWAIT)); 46759ec9023SKonstantin Belousov if (sf == NULL) { 46859ec9023SKonstantin Belousov error = ENOMEM; 46959ec9023SKonstantin Belousov break; 47059ec9023SKonstantin Belousov } 47159ec9023SKonstantin Belousov } 47259ec9023SKonstantin Belousov p = (char *)sf_buf_kva(sf) + *ma_offs; 47359ec9023SKonstantin Belousov switch (op) { 47459ec9023SKonstantin Belousov case MD_MALLOC_MOVE_ZERO: 47559ec9023SKonstantin Belousov bzero(p, sz); 47659ec9023SKonstantin Belousov break; 47759ec9023SKonstantin Belousov case MD_MALLOC_MOVE_FILL: 47859ec9023SKonstantin Belousov memset(p, fill, sz); 47959ec9023SKonstantin Belousov break; 48059ec9023SKonstantin Belousov case MD_MALLOC_MOVE_READ: 48159ec9023SKonstantin Belousov bcopy(ptr, p, sz); 48259ec9023SKonstantin Belousov cpu_flush_dcache(p, sz); 48359ec9023SKonstantin Belousov break; 48459ec9023SKonstantin Belousov case MD_MALLOC_MOVE_WRITE: 48559ec9023SKonstantin Belousov bcopy(p, ptr, sz); 48659ec9023SKonstantin Belousov break; 48759ec9023SKonstantin Belousov case MD_MALLOC_MOVE_CMP: 48859ec9023SKonstantin Belousov for (i = 0; i < sz; i++, p++) { 48959ec9023SKonstantin Belousov if (!first_read) { 49059ec9023SKonstantin Belousov *uc = (u_char)*p; 49159ec9023SKonstantin Belousov first = *p; 49259ec9023SKonstantin Belousov first_read = 1; 49359ec9023SKonstantin Belousov } else if (*p != first) { 49459ec9023SKonstantin Belousov error = EDOOFUS; 49559ec9023SKonstantin Belousov break; 49659ec9023SKonstantin Belousov } 49759ec9023SKonstantin Belousov } 49859ec9023SKonstantin Belousov break; 49959ec9023SKonstantin Belousov default: 50059ec9023SKonstantin Belousov KASSERT(0, ("md_malloc_move unknown op %d\n", op)); 50159ec9023SKonstantin Belousov break; 50259ec9023SKonstantin Belousov } 50359ec9023SKonstantin Belousov if (error != 0) 50459ec9023SKonstantin Belousov break; 50559ec9023SKonstantin Belousov *ma_offs += sz; 50659ec9023SKonstantin Belousov *ma_offs %= PAGE_SIZE; 50759ec9023SKonstantin Belousov if (*ma_offs == 0) 50859ec9023SKonstantin Belousov (*mp)++; 50959ec9023SKonstantin Belousov ptr = (char *)ptr + sz; 51059ec9023SKonstantin Belousov } 51159ec9023SKonstantin Belousov 51259ec9023SKonstantin Belousov if (sf != NULL) 51359ec9023SKonstantin Belousov sf_buf_free(sf); 51459ec9023SKonstantin Belousov sched_unpin(); 51559ec9023SKonstantin Belousov if (op == MD_MALLOC_MOVE_CMP && error != 0) { 51659ec9023SKonstantin Belousov *mp = mp1; 51759ec9023SKonstantin Belousov *ma_offs = ma_offs1; 51859ec9023SKonstantin Belousov } 51959ec9023SKonstantin Belousov return (error); 52059ec9023SKonstantin Belousov } 52159ec9023SKonstantin Belousov 522b4a4f93cSPoul-Henning Kamp static int 523b4a4f93cSPoul-Henning Kamp mdstart_malloc(struct md_s *sc, struct bio *bp) 52400a6a3c6SPoul-Henning Kamp { 525c6517568SPoul-Henning Kamp u_char *dst; 52659ec9023SKonstantin Belousov vm_page_t *m; 52759ec9023SKonstantin Belousov int i, error, error1, ma_offs, notmapped; 528b830359bSPawel Jakub Dawidek off_t secno, nsec, uc; 529c6517568SPoul-Henning Kamp uintptr_t sp, osp; 53000a6a3c6SPoul-Henning Kamp 5315541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 5325541f25eSPawel Jakub Dawidek case BIO_READ: 5335541f25eSPawel Jakub Dawidek case BIO_WRITE: 5345541f25eSPawel Jakub Dawidek case BIO_DELETE: 5355541f25eSPawel Jakub Dawidek break; 5365541f25eSPawel Jakub Dawidek default: 5375541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 5385541f25eSPawel Jakub Dawidek } 5395541f25eSPawel Jakub Dawidek 54059ec9023SKonstantin Belousov notmapped = (bp->bio_flags & BIO_UNMAPPED) != 0; 54159ec9023SKonstantin Belousov if (notmapped) { 54259ec9023SKonstantin Belousov m = bp->bio_ma; 54359ec9023SKonstantin Belousov ma_offs = bp->bio_ma_offset; 54459ec9023SKonstantin Belousov dst = NULL; 54559ec9023SKonstantin Belousov } else { 54659ec9023SKonstantin Belousov dst = bp->bio_data; 54759ec9023SKonstantin Belousov } 54859ec9023SKonstantin Belousov 549b830359bSPawel Jakub Dawidek nsec = bp->bio_length / sc->sectorsize; 550b830359bSPawel Jakub Dawidek secno = bp->bio_offset / sc->sectorsize; 551c6517568SPoul-Henning Kamp error = 0; 55200a6a3c6SPoul-Henning Kamp while (nsec--) { 553fde2a2e4SPoul-Henning Kamp osp = s_read(sc->indir, secno); 5548177437dSPoul-Henning Kamp if (bp->bio_cmd == BIO_DELETE) { 555fde2a2e4SPoul-Henning Kamp if (osp != 0) 556fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, 0); 5578177437dSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_READ) { 55859ec9023SKonstantin Belousov if (osp == 0) { 55959ec9023SKonstantin Belousov if (notmapped) { 56059ec9023SKonstantin Belousov error = md_malloc_move(&m, &ma_offs, 56159ec9023SKonstantin Belousov sc->sectorsize, NULL, 0, 56259ec9023SKonstantin Belousov MD_MALLOC_MOVE_ZERO); 56359ec9023SKonstantin Belousov } else 564b830359bSPawel Jakub Dawidek bzero(dst, sc->sectorsize); 56559ec9023SKonstantin Belousov } else if (osp <= 255) { 56659ec9023SKonstantin Belousov if (notmapped) { 56759ec9023SKonstantin Belousov error = md_malloc_move(&m, &ma_offs, 56859ec9023SKonstantin Belousov sc->sectorsize, NULL, osp, 56959ec9023SKonstantin Belousov MD_MALLOC_MOVE_FILL); 57059ec9023SKonstantin Belousov } else 571dbb95048SMarcel Moolenaar memset(dst, osp, sc->sectorsize); 57259ec9023SKonstantin Belousov } else { 57359ec9023SKonstantin Belousov if (notmapped) { 57459ec9023SKonstantin Belousov error = md_malloc_move(&m, &ma_offs, 57559ec9023SKonstantin Belousov sc->sectorsize, (void *)osp, 0, 57659ec9023SKonstantin Belousov MD_MALLOC_MOVE_READ); 57759ec9023SKonstantin Belousov } else { 578b830359bSPawel Jakub Dawidek bcopy((void *)osp, dst, sc->sectorsize); 579dbb95048SMarcel Moolenaar cpu_flush_dcache(dst, sc->sectorsize); 580dbb95048SMarcel Moolenaar } 58159ec9023SKonstantin Belousov } 582fde2a2e4SPoul-Henning Kamp osp = 0; 583c6517568SPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 5848f8def9eSPoul-Henning Kamp if (sc->flags & MD_COMPRESS) { 58559ec9023SKonstantin Belousov if (notmapped) { 58659ec9023SKonstantin Belousov error1 = md_malloc_move(&m, &ma_offs, 58759ec9023SKonstantin Belousov sc->sectorsize, &uc, 0, 58859ec9023SKonstantin Belousov MD_MALLOC_MOVE_CMP); 58959ec9023SKonstantin Belousov i = error1 == 0 ? sc->sectorsize : 0; 59059ec9023SKonstantin Belousov } else { 59100a6a3c6SPoul-Henning Kamp uc = dst[0]; 59259ec9023SKonstantin Belousov for (i = 1; i < sc->sectorsize; i++) { 59300a6a3c6SPoul-Henning Kamp if (dst[i] != uc) 59400a6a3c6SPoul-Henning Kamp break; 59559ec9023SKonstantin Belousov } 59659ec9023SKonstantin Belousov } 5978f8def9eSPoul-Henning Kamp } else { 5988f8def9eSPoul-Henning Kamp i = 0; 5998f8def9eSPoul-Henning Kamp uc = 0; 6008f8def9eSPoul-Henning Kamp } 601b830359bSPawel Jakub Dawidek if (i == sc->sectorsize) { 602fde2a2e4SPoul-Henning Kamp if (osp != uc) 603fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, uc); 60400a6a3c6SPoul-Henning Kamp } else { 605fde2a2e4SPoul-Henning Kamp if (osp <= 255) { 606b830359bSPawel Jakub Dawidek sp = (uintptr_t)uma_zalloc(sc->uma, 607c44d423eSKonstantin Belousov md_malloc_wait ? M_WAITOK : 608b830359bSPawel Jakub Dawidek M_NOWAIT); 609c6517568SPoul-Henning Kamp if (sp == 0) { 610fde2a2e4SPoul-Henning Kamp error = ENOSPC; 611fde2a2e4SPoul-Henning Kamp break; 612fde2a2e4SPoul-Henning Kamp } 61359ec9023SKonstantin Belousov if (notmapped) { 61459ec9023SKonstantin Belousov error = md_malloc_move(&m, 61559ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 61659ec9023SKonstantin Belousov (void *)sp, 0, 61759ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 61859ec9023SKonstantin Belousov } else { 61959ec9023SKonstantin Belousov bcopy(dst, (void *)sp, 62059ec9023SKonstantin Belousov sc->sectorsize); 62159ec9023SKonstantin Belousov } 622fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, secno, sp); 623c6517568SPoul-Henning Kamp } else { 62459ec9023SKonstantin Belousov if (notmapped) { 62559ec9023SKonstantin Belousov error = md_malloc_move(&m, 62659ec9023SKonstantin Belousov &ma_offs, sc->sectorsize, 62759ec9023SKonstantin Belousov (void *)osp, 0, 62859ec9023SKonstantin Belousov MD_MALLOC_MOVE_WRITE); 62959ec9023SKonstantin Belousov } else { 63059ec9023SKonstantin Belousov bcopy(dst, (void *)osp, 63159ec9023SKonstantin Belousov sc->sectorsize); 63259ec9023SKonstantin Belousov } 633fde2a2e4SPoul-Henning Kamp osp = 0; 63400a6a3c6SPoul-Henning Kamp } 63500a6a3c6SPoul-Henning Kamp } 636c6517568SPoul-Henning Kamp } else { 637c6517568SPoul-Henning Kamp error = EOPNOTSUPP; 638c6517568SPoul-Henning Kamp } 639c6517568SPoul-Henning Kamp if (osp > 255) 640f43b2bacSPoul-Henning Kamp uma_zfree(sc->uma, (void*)osp); 641e3ed29a7SPawel Jakub Dawidek if (error != 0) 642c6517568SPoul-Henning Kamp break; 64300a6a3c6SPoul-Henning Kamp secno++; 64459ec9023SKonstantin Belousov if (!notmapped) 645b830359bSPawel Jakub Dawidek dst += sc->sectorsize; 64600a6a3c6SPoul-Henning Kamp } 6478177437dSPoul-Henning Kamp bp->bio_resid = 0; 648c6517568SPoul-Henning Kamp return (error); 64900a6a3c6SPoul-Henning Kamp } 65000a6a3c6SPoul-Henning Kamp 651b4a4f93cSPoul-Henning Kamp static int 652b4a4f93cSPoul-Henning Kamp mdstart_preload(struct md_s *sc, struct bio *bp) 65371e4fff8SPoul-Henning Kamp { 65471e4fff8SPoul-Henning Kamp 655a8a58d03SPawel Jakub Dawidek switch (bp->bio_cmd) { 656a8a58d03SPawel Jakub Dawidek case BIO_READ: 657a8a58d03SPawel Jakub Dawidek bcopy(sc->pl_ptr + bp->bio_offset, bp->bio_data, 658a8a58d03SPawel Jakub Dawidek bp->bio_length); 659dbb95048SMarcel Moolenaar cpu_flush_dcache(bp->bio_data, bp->bio_length); 660a8a58d03SPawel Jakub Dawidek break; 661a8a58d03SPawel Jakub Dawidek case BIO_WRITE: 662a8a58d03SPawel Jakub Dawidek bcopy(bp->bio_data, sc->pl_ptr + bp->bio_offset, 663a8a58d03SPawel Jakub Dawidek bp->bio_length); 664a8a58d03SPawel Jakub Dawidek break; 66571e4fff8SPoul-Henning Kamp } 6668177437dSPoul-Henning Kamp bp->bio_resid = 0; 667b4a4f93cSPoul-Henning Kamp return (0); 66871e4fff8SPoul-Henning Kamp } 66971e4fff8SPoul-Henning Kamp 670b4a4f93cSPoul-Henning Kamp static int 671b4a4f93cSPoul-Henning Kamp mdstart_vnode(struct md_s *sc, struct bio *bp) 6728f8def9eSPoul-Henning Kamp { 6735050aa86SKonstantin Belousov int error; 6748f8def9eSPoul-Henning Kamp struct uio auio; 6758f8def9eSPoul-Henning Kamp struct iovec aiov; 6768f8def9eSPoul-Henning Kamp struct mount *mp; 6775541f25eSPawel Jakub Dawidek struct vnode *vp; 67859ec9023SKonstantin Belousov struct buf *pb; 6795541f25eSPawel Jakub Dawidek struct thread *td; 6800abd21bdSDag-Erling Smørgrav off_t end, zerosize; 6815541f25eSPawel Jakub Dawidek 6825541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 6835541f25eSPawel Jakub Dawidek case BIO_READ: 6845541f25eSPawel Jakub Dawidek case BIO_WRITE: 6850abd21bdSDag-Erling Smørgrav case BIO_DELETE: 6865541f25eSPawel Jakub Dawidek case BIO_FLUSH: 6875541f25eSPawel Jakub Dawidek break; 6885541f25eSPawel Jakub Dawidek default: 6895541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 6905541f25eSPawel Jakub Dawidek } 6915541f25eSPawel Jakub Dawidek 6925541f25eSPawel Jakub Dawidek td = curthread; 6935541f25eSPawel Jakub Dawidek vp = sc->vnode; 6948f8def9eSPoul-Henning Kamp 6958f8def9eSPoul-Henning Kamp /* 6968f8def9eSPoul-Henning Kamp * VNODE I/O 6978f8def9eSPoul-Henning Kamp * 6988f8def9eSPoul-Henning Kamp * If an error occurs, we set BIO_ERROR but we do not set 6998f8def9eSPoul-Henning Kamp * B_INVAL because (for a write anyway), the buffer is 7008f8def9eSPoul-Henning Kamp * still valid. 7018f8def9eSPoul-Henning Kamp */ 7028f8def9eSPoul-Henning Kamp 7035541f25eSPawel Jakub Dawidek if (bp->bio_cmd == BIO_FLUSH) { 7045541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 705cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7065541f25eSPawel Jakub Dawidek error = VOP_FSYNC(vp, MNT_WAIT, td); 70722db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 7085541f25eSPawel Jakub Dawidek vn_finished_write(mp); 7095541f25eSPawel Jakub Dawidek return (error); 7105541f25eSPawel Jakub Dawidek } 7115541f25eSPawel Jakub Dawidek 7128f8def9eSPoul-Henning Kamp bzero(&auio, sizeof(auio)); 7138f8def9eSPoul-Henning Kamp 7140abd21bdSDag-Erling Smørgrav /* 7150abd21bdSDag-Erling Smørgrav * Special case for BIO_DELETE. On the surface, this is very 7160abd21bdSDag-Erling Smørgrav * similar to BIO_WRITE, except that we write from our own 7170abd21bdSDag-Erling Smørgrav * fixed-length buffer, so we have to loop. The net result is 7180abd21bdSDag-Erling Smørgrav * that the two cases end up having very little in common. 7190abd21bdSDag-Erling Smørgrav */ 7200abd21bdSDag-Erling Smørgrav if (bp->bio_cmd == BIO_DELETE) { 72189cb2a19SMatthew D Fleming zerosize = ZERO_REGION_SIZE - 72289cb2a19SMatthew D Fleming (ZERO_REGION_SIZE % sc->sectorsize); 7230abd21bdSDag-Erling Smørgrav auio.uio_iov = &aiov; 7240abd21bdSDag-Erling Smørgrav auio.uio_iovcnt = 1; 7250abd21bdSDag-Erling Smørgrav auio.uio_offset = (vm_ooffset_t)bp->bio_offset; 7260abd21bdSDag-Erling Smørgrav auio.uio_segflg = UIO_SYSSPACE; 7270abd21bdSDag-Erling Smørgrav auio.uio_rw = UIO_WRITE; 7280abd21bdSDag-Erling Smørgrav auio.uio_td = td; 7290abd21bdSDag-Erling Smørgrav end = bp->bio_offset + bp->bio_length; 7300abd21bdSDag-Erling Smørgrav (void) vn_start_write(vp, &mp, V_WAIT); 7310abd21bdSDag-Erling Smørgrav vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7320abd21bdSDag-Erling Smørgrav error = 0; 7330abd21bdSDag-Erling Smørgrav while (auio.uio_offset < end) { 73489cb2a19SMatthew D Fleming aiov.iov_base = __DECONST(void *, zero_region); 7350abd21bdSDag-Erling Smørgrav aiov.iov_len = end - auio.uio_offset; 7360abd21bdSDag-Erling Smørgrav if (aiov.iov_len > zerosize) 7370abd21bdSDag-Erling Smørgrav aiov.iov_len = zerosize; 7380abd21bdSDag-Erling Smørgrav auio.uio_resid = aiov.iov_len; 7390abd21bdSDag-Erling Smørgrav error = VOP_WRITE(vp, &auio, 7400abd21bdSDag-Erling Smørgrav sc->flags & MD_ASYNC ? 0 : IO_SYNC, sc->cred); 7410abd21bdSDag-Erling Smørgrav if (error != 0) 7420abd21bdSDag-Erling Smørgrav break; 7430abd21bdSDag-Erling Smørgrav } 7440abd21bdSDag-Erling Smørgrav VOP_UNLOCK(vp, 0); 7450abd21bdSDag-Erling Smørgrav vn_finished_write(mp); 7460abd21bdSDag-Erling Smørgrav bp->bio_resid = end - auio.uio_offset; 7470abd21bdSDag-Erling Smørgrav return (error); 7480abd21bdSDag-Erling Smørgrav } 7490abd21bdSDag-Erling Smørgrav 75059ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) == 0) { 75159ec9023SKonstantin Belousov pb = NULL; 7528f8def9eSPoul-Henning Kamp aiov.iov_base = bp->bio_data; 75359ec9023SKonstantin Belousov } else { 75460b6e197SKonstantin Belousov KASSERT(bp->bio_length <= MAXPHYS, ("bio_length %jd", 75560b6e197SKonstantin Belousov (uintmax_t)bp->bio_length)); 75659ec9023SKonstantin Belousov pb = getpbuf(&md_vnode_pbuf_freecnt); 75759ec9023SKonstantin Belousov pmap_qenter((vm_offset_t)pb->b_data, bp->bio_ma, bp->bio_ma_n); 75859ec9023SKonstantin Belousov aiov.iov_base = (void *)((vm_offset_t)pb->b_data + 75959ec9023SKonstantin Belousov bp->bio_ma_offset); 76059ec9023SKonstantin Belousov } 761a8a58d03SPawel Jakub Dawidek aiov.iov_len = bp->bio_length; 7628f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 7638f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 764a8a58d03SPawel Jakub Dawidek auio.uio_offset = (vm_ooffset_t)bp->bio_offset; 7658f8def9eSPoul-Henning Kamp auio.uio_segflg = UIO_SYSSPACE; 7668f8def9eSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) 7678f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_READ; 7688e28326aSPoul-Henning Kamp else if (bp->bio_cmd == BIO_WRITE) 7698f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_WRITE; 7708e28326aSPoul-Henning Kamp else 7718e28326aSPoul-Henning Kamp panic("wrong BIO_OP in mdstart_vnode"); 772a8a58d03SPawel Jakub Dawidek auio.uio_resid = bp->bio_length; 7735541f25eSPawel Jakub Dawidek auio.uio_td = td; 7747e76bb56SMatthew Dillon /* 7757e76bb56SMatthew Dillon * When reading set IO_DIRECT to try to avoid double-caching 77617a13919SPoul-Henning Kamp * the data. When writing IO_DIRECT is not optimal. 7777e76bb56SMatthew Dillon */ 7788f8def9eSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 779cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7805541f25eSPawel Jakub Dawidek error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred); 78122db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 7828f8def9eSPoul-Henning Kamp } else { 7835541f25eSPawel Jakub Dawidek (void) vn_start_write(vp, &mp, V_WAIT); 784cb05b60aSAttilio Rao vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 7855541f25eSPawel Jakub Dawidek error = VOP_WRITE(vp, &auio, sc->flags & MD_ASYNC ? 0 : IO_SYNC, 7865541f25eSPawel Jakub Dawidek sc->cred); 78722db15c0SAttilio Rao VOP_UNLOCK(vp, 0); 7888f8def9eSPoul-Henning Kamp vn_finished_write(mp); 7898f8def9eSPoul-Henning Kamp } 79059ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 79159ec9023SKonstantin Belousov pmap_qremove((vm_offset_t)pb->b_data, bp->bio_ma_n); 79259ec9023SKonstantin Belousov relpbuf(pb, &md_vnode_pbuf_freecnt); 79359ec9023SKonstantin Belousov } 7948f8def9eSPoul-Henning Kamp bp->bio_resid = auio.uio_resid; 795b4a4f93cSPoul-Henning Kamp return (error); 7968f8def9eSPoul-Henning Kamp } 7978f8def9eSPoul-Henning Kamp 798b4a4f93cSPoul-Henning Kamp static int 799b4a4f93cSPoul-Henning Kamp mdstart_swap(struct md_s *sc, struct bio *bp) 8008f8def9eSPoul-Henning Kamp { 8018e28326aSPoul-Henning Kamp vm_page_t m; 8028e28326aSPoul-Henning Kamp u_char *p; 80359ec9023SKonstantin Belousov vm_pindex_t i, lastp; 80459ec9023SKonstantin Belousov int rv, ma_offs, offs, len, lastend; 8058f8def9eSPoul-Henning Kamp 8065541f25eSPawel Jakub Dawidek switch (bp->bio_cmd) { 8075541f25eSPawel Jakub Dawidek case BIO_READ: 8085541f25eSPawel Jakub Dawidek case BIO_WRITE: 8095541f25eSPawel Jakub Dawidek case BIO_DELETE: 8105541f25eSPawel Jakub Dawidek break; 8115541f25eSPawel Jakub Dawidek default: 8125541f25eSPawel Jakub Dawidek return (EOPNOTSUPP); 8135541f25eSPawel Jakub Dawidek } 8145541f25eSPawel Jakub Dawidek 8158e28326aSPoul-Henning Kamp p = bp->bio_data; 81659ec9023SKonstantin Belousov ma_offs = (bp->bio_flags & BIO_UNMAPPED) == 0 ? 0 : bp->bio_ma_offset; 817e07113d6SColin Percival 818e07113d6SColin Percival /* 8196c3cd0e2SMaxim Konovalov * offs is the offset at which to start operating on the 820e07113d6SColin Percival * next (ie, first) page. lastp is the last page on 821e07113d6SColin Percival * which we're going to operate. lastend is the ending 822e07113d6SColin Percival * position within that last page (ie, PAGE_SIZE if 823e07113d6SColin Percival * we're operating on complete aligned pages). 824e07113d6SColin Percival */ 825e07113d6SColin Percival offs = bp->bio_offset % PAGE_SIZE; 826e07113d6SColin Percival lastp = (bp->bio_offset + bp->bio_length - 1) / PAGE_SIZE; 827e07113d6SColin Percival lastend = (bp->bio_offset + bp->bio_length - 1) % PAGE_SIZE + 1; 828e07113d6SColin Percival 829812851b6SBrian Feldman rv = VM_PAGER_OK; 83089f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 8318e28326aSPoul-Henning Kamp vm_object_pip_add(sc->object, 1); 832e07113d6SColin Percival for (i = bp->bio_offset / PAGE_SIZE; i <= lastp; i++) { 833e07113d6SColin Percival len = ((i == lastp) ? lastend : PAGE_SIZE) - offs; 8341a42d14aSKonstantin Belousov m = vm_page_grab(sc->object, i, VM_ALLOC_SYSTEM); 8358e28326aSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) { 836537cc627SKonstantin Belousov if (m->valid == VM_PAGE_BITS_ALL) 837537cc627SKonstantin Belousov rv = VM_PAGER_OK; 838537cc627SKonstantin Belousov else 83907be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 840812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 841c7aebda8SAttilio Rao vm_page_xunbusy(m); 842812851b6SBrian Feldman break; 843e9f581baSKonstantin Belousov } else if (rv == VM_PAGER_FAIL) { 844e9f581baSKonstantin Belousov /* 845e9f581baSKonstantin Belousov * Pager does not have the page. Zero 846e9f581baSKonstantin Belousov * the allocated page, and mark it as 847e9f581baSKonstantin Belousov * valid. Do not set dirty, the page 848e9f581baSKonstantin Belousov * can be recreated if thrown out. 849e9f581baSKonstantin Belousov */ 85059ec9023SKonstantin Belousov pmap_zero_page(m); 851e9f581baSKonstantin Belousov m->valid = VM_PAGE_BITS_ALL; 852812851b6SBrian Feldman } 85359ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 85459ec9023SKonstantin Belousov pmap_copy_pages(&m, offs, bp->bio_ma, 85559ec9023SKonstantin Belousov ma_offs, len); 85659ec9023SKonstantin Belousov } else { 85759ec9023SKonstantin Belousov physcopyout(VM_PAGE_TO_PHYS(m) + offs, p, len); 858dbb95048SMarcel Moolenaar cpu_flush_dcache(p, len); 85959ec9023SKonstantin Belousov } 8608e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_WRITE) { 86107be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 86207be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 863537cc627SKonstantin Belousov else 864537cc627SKonstantin Belousov rv = VM_PAGER_OK; 865812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 866c7aebda8SAttilio Rao vm_page_xunbusy(m); 867812851b6SBrian Feldman break; 868812851b6SBrian Feldman } 86959ec9023SKonstantin Belousov if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 87059ec9023SKonstantin Belousov pmap_copy_pages(bp->bio_ma, ma_offs, &m, 87159ec9023SKonstantin Belousov offs, len); 87259ec9023SKonstantin Belousov } else { 87359ec9023SKonstantin Belousov physcopyin(p, VM_PAGE_TO_PHYS(m) + offs, len); 87459ec9023SKonstantin Belousov } 8758e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 8768e28326aSPoul-Henning Kamp } else if (bp->bio_cmd == BIO_DELETE) { 87707be617fSAlan Cox if (len != PAGE_SIZE && m->valid != VM_PAGE_BITS_ALL) 87807be617fSAlan Cox rv = vm_pager_get_pages(sc->object, &m, 1, 0); 879537cc627SKonstantin Belousov else 880537cc627SKonstantin Belousov rv = VM_PAGER_OK; 881812851b6SBrian Feldman if (rv == VM_PAGER_ERROR) { 882c7aebda8SAttilio Rao vm_page_xunbusy(m); 883812851b6SBrian Feldman break; 884812851b6SBrian Feldman } 8854a13a769SKonstantin Belousov if (len != PAGE_SIZE) { 88659ec9023SKonstantin Belousov pmap_zero_page_area(m, offs, len); 8874a13a769SKonstantin Belousov vm_page_clear_dirty(m, offs, len); 8888e28326aSPoul-Henning Kamp m->valid = VM_PAGE_BITS_ALL; 8894a13a769SKonstantin Belousov } else 8904a13a769SKonstantin Belousov vm_pager_page_unswapped(m); 8918e28326aSPoul-Henning Kamp } 892c7aebda8SAttilio Rao vm_page_xunbusy(m); 893fc0c3802SKonstantin Belousov vm_page_lock(m); 8944a13a769SKonstantin Belousov if (bp->bio_cmd == BIO_DELETE && len == PAGE_SIZE) 8954a13a769SKonstantin Belousov vm_page_free(m); 8964a13a769SKonstantin Belousov else 8978e28326aSPoul-Henning Kamp vm_page_activate(m); 898ecd5dd95SAlan Cox vm_page_unlock(m); 8995d9b4508SKonstantin Belousov if (bp->bio_cmd == BIO_WRITE) { 9008e28326aSPoul-Henning Kamp vm_page_dirty(m); 9015d9b4508SKonstantin Belousov vm_pager_page_unswapped(m); 9025d9b4508SKonstantin Belousov } 903e07113d6SColin Percival 904e07113d6SColin Percival /* Actions on further pages start at offset 0 */ 905e07113d6SColin Percival p += PAGE_SIZE - offs; 906e07113d6SColin Percival offs = 0; 90759ec9023SKonstantin Belousov ma_offs += len; 9088e28326aSPoul-Henning Kamp } 9090d8243ccSAttilio Rao vm_object_pip_wakeup(sc->object); 91089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 911812851b6SBrian Feldman return (rv != VM_PAGER_ERROR ? 0 : ENOSPC); 9128e28326aSPoul-Henning Kamp } 9138f8def9eSPoul-Henning Kamp 9140efd9bfdSEdward Tomasz Napierala static int 9150efd9bfdSEdward Tomasz Napierala mdstart_null(struct md_s *sc, struct bio *bp) 9160efd9bfdSEdward Tomasz Napierala { 9170efd9bfdSEdward Tomasz Napierala 9180efd9bfdSEdward Tomasz Napierala switch (bp->bio_cmd) { 9190efd9bfdSEdward Tomasz Napierala case BIO_READ: 9200efd9bfdSEdward Tomasz Napierala bzero(bp->bio_data, bp->bio_length); 9210efd9bfdSEdward Tomasz Napierala cpu_flush_dcache(bp->bio_data, bp->bio_length); 9220efd9bfdSEdward Tomasz Napierala break; 9230efd9bfdSEdward Tomasz Napierala case BIO_WRITE: 9240efd9bfdSEdward Tomasz Napierala break; 9250efd9bfdSEdward Tomasz Napierala } 9260efd9bfdSEdward Tomasz Napierala bp->bio_resid = 0; 9270efd9bfdSEdward Tomasz Napierala return (0); 9280efd9bfdSEdward Tomasz Napierala } 9290efd9bfdSEdward Tomasz Napierala 9308f8def9eSPoul-Henning Kamp static void 9315c97ca54SIan Dowse md_kthread(void *arg) 9325c97ca54SIan Dowse { 9335c97ca54SIan Dowse struct md_s *sc; 9345c97ca54SIan Dowse struct bio *bp; 935a08d2e7fSJohn Baldwin int error; 9365c97ca54SIan Dowse 9375c97ca54SIan Dowse sc = arg; 938982d11f8SJeff Roberson thread_lock(curthread); 93963710c4dSJohn Baldwin sched_prio(curthread, PRIBIO); 940982d11f8SJeff Roberson thread_unlock(curthread); 9413b7b5496SKonstantin Belousov if (sc->type == MD_VNODE) 9423b7b5496SKonstantin Belousov curthread->td_pflags |= TDP_NORUNNINGBUF; 9435c97ca54SIan Dowse 944b4a4f93cSPoul-Henning Kamp for (;;) { 945a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 9465c97ca54SIan Dowse if (sc->flags & MD_SHUTDOWN) { 947a08d2e7fSJohn Baldwin sc->flags |= MD_EXITING; 948a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 9493745c395SJulian Elischer kproc_exit(0); 9505c97ca54SIan Dowse } 9519b00ca19SPoul-Henning Kamp bp = bioq_takefirst(&sc->bio_queue); 9529b00ca19SPoul-Henning Kamp if (!bp) { 9530f8500a5SPoul-Henning Kamp msleep(sc, &sc->queue_mtx, PRIBIO | PDROP, "mdwait", 0); 9545c97ca54SIan Dowse continue; 9555c97ca54SIan Dowse } 9560f8500a5SPoul-Henning Kamp mtx_unlock(&sc->queue_mtx); 9574e8bfe14SPoul-Henning Kamp if (bp->bio_cmd == BIO_GETATTR) { 958d91e813cSKonstantin Belousov if ((sc->fwsectors && sc->fwheads && 9594e8bfe14SPoul-Henning Kamp (g_handleattr_int(bp, "GEOM::fwsectors", 9604e8bfe14SPoul-Henning Kamp sc->fwsectors) || 9614e8bfe14SPoul-Henning Kamp g_handleattr_int(bp, "GEOM::fwheads", 962d91e813cSKonstantin Belousov sc->fwheads))) || 963d91e813cSKonstantin Belousov g_handleattr_int(bp, "GEOM::candelete", 1)) 9644e8bfe14SPoul-Henning Kamp error = -1; 9654e8bfe14SPoul-Henning Kamp else 9664e8bfe14SPoul-Henning Kamp error = EOPNOTSUPP; 9674e8bfe14SPoul-Henning Kamp } else { 9689b00ca19SPoul-Henning Kamp error = sc->start(sc, bp); 9694e8bfe14SPoul-Henning Kamp } 970b4a4f93cSPoul-Henning Kamp 9716f4f00f1SPoul-Henning Kamp if (error != -1) { 9726f4f00f1SPoul-Henning Kamp bp->bio_completed = bp->bio_length; 973a03be42dSMaxim Sobolev if ((bp->bio_cmd == BIO_READ) || (bp->bio_cmd == BIO_WRITE)) 974a03be42dSMaxim Sobolev devstat_end_transaction_bio(sc->devstat, bp); 97596410b95SKonstantin Belousov g_io_deliver(bp, error); 976b4a4f93cSPoul-Henning Kamp } 9778f8def9eSPoul-Henning Kamp } 97826d48b40SPoul-Henning Kamp } 9798f8def9eSPoul-Henning Kamp 9808f8def9eSPoul-Henning Kamp static struct md_s * 9818f8def9eSPoul-Henning Kamp mdfind(int unit) 9828f8def9eSPoul-Henning Kamp { 9838f8def9eSPoul-Henning Kamp struct md_s *sc; 9848f8def9eSPoul-Henning Kamp 9853f54a085SPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 9863f54a085SPoul-Henning Kamp if (sc->unit == unit) 9878f8def9eSPoul-Henning Kamp break; 9888f8def9eSPoul-Henning Kamp } 9898f8def9eSPoul-Henning Kamp return (sc); 9908f8def9eSPoul-Henning Kamp } 9918f8def9eSPoul-Henning Kamp 9928f8def9eSPoul-Henning Kamp static struct md_s * 993947fc8deSPoul-Henning Kamp mdnew(int unit, int *errp, enum md_types type) 9948f8def9eSPoul-Henning Kamp { 995f4e7c5a8SJaakko Heinonen struct md_s *sc; 996f4e7c5a8SJaakko Heinonen int error; 9978f8def9eSPoul-Henning Kamp 9989b00ca19SPoul-Henning Kamp *errp = 0; 999f4e7c5a8SJaakko Heinonen if (unit == -1) 1000f4e7c5a8SJaakko Heinonen unit = alloc_unr(md_uh); 1001f4e7c5a8SJaakko Heinonen else 1002f4e7c5a8SJaakko Heinonen unit = alloc_unr_specific(md_uh, unit); 1003f4e7c5a8SJaakko Heinonen 1004f4e7c5a8SJaakko Heinonen if (unit == -1) { 10057ee3c044SPawel Jakub Dawidek *errp = EBUSY; 10063f54a085SPoul-Henning Kamp return (NULL); 10073f54a085SPoul-Henning Kamp } 1008f4e7c5a8SJaakko Heinonen 10099b00ca19SPoul-Henning Kamp sc = (struct md_s *)malloc(sizeof *sc, M_MD, M_WAITOK | M_ZERO); 1010947fc8deSPoul-Henning Kamp sc->type = type; 10119b00ca19SPoul-Henning Kamp bioq_init(&sc->bio_queue); 10129b00ca19SPoul-Henning Kamp mtx_init(&sc->queue_mtx, "md bio queue", NULL, MTX_DEF); 101340ea77a0SAlexander Motin mtx_init(&sc->stat_mtx, "md stat", NULL, MTX_DEF); 10143f54a085SPoul-Henning Kamp sc->unit = unit; 1015f43b2bacSPoul-Henning Kamp sprintf(sc->name, "md%d", unit); 10167ee3c044SPawel Jakub Dawidek LIST_INSERT_HEAD(&md_softc_list, sc, list); 10173745c395SJulian Elischer error = kproc_create(md_kthread, sc, &sc->procp, 0, 0,"%s", sc->name); 10189b00ca19SPoul-Henning Kamp if (error == 0) 10199b00ca19SPoul-Henning Kamp return (sc); 10207ee3c044SPawel Jakub Dawidek LIST_REMOVE(sc, list); 102140ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 10227ee3c044SPawel Jakub Dawidek mtx_destroy(&sc->queue_mtx); 1023f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 10245c97ca54SIan Dowse free(sc, M_MD); 10257ee3c044SPawel Jakub Dawidek *errp = error; 10265c97ca54SIan Dowse return (NULL); 10275c97ca54SIan Dowse } 10288f8def9eSPoul-Henning Kamp 10298f8def9eSPoul-Henning Kamp static void 10308f8def9eSPoul-Henning Kamp mdinit(struct md_s *sc) 10318f8def9eSPoul-Henning Kamp { 10326f4f00f1SPoul-Henning Kamp struct g_geom *gp; 10336f4f00f1SPoul-Henning Kamp struct g_provider *pp; 10346f4f00f1SPoul-Henning Kamp 10356f4f00f1SPoul-Henning Kamp g_topology_lock(); 10366f4f00f1SPoul-Henning Kamp gp = g_new_geomf(&g_md_class, "md%d", sc->unit); 10376f4f00f1SPoul-Henning Kamp gp->softc = sc; 10386f4f00f1SPoul-Henning Kamp pp = g_new_providerf(gp, "md%d", sc->unit); 103940ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 1040b830359bSPawel Jakub Dawidek pp->mediasize = sc->mediasize; 1041b830359bSPawel Jakub Dawidek pp->sectorsize = sc->sectorsize; 10421ef76554SKonstantin Belousov switch (sc->type) { 10431ef76554SKonstantin Belousov case MD_MALLOC: 10441ef76554SKonstantin Belousov case MD_VNODE: 10451ef76554SKonstantin Belousov case MD_SWAP: 104659ec9023SKonstantin Belousov pp->flags |= G_PF_ACCEPT_UNMAPPED; 10471ef76554SKonstantin Belousov break; 10481ef76554SKonstantin Belousov case MD_PRELOAD: 10490efd9bfdSEdward Tomasz Napierala case MD_NULL: 10501ef76554SKonstantin Belousov break; 10511ef76554SKonstantin Belousov } 10526f4f00f1SPoul-Henning Kamp sc->gp = gp; 10536f4f00f1SPoul-Henning Kamp sc->pp = pp; 10546f4f00f1SPoul-Henning Kamp g_error_provider(pp, 0); 10556f4f00f1SPoul-Henning Kamp g_topology_unlock(); 1056a03be42dSMaxim Sobolev sc->devstat = devstat_new_entry("md", sc->unit, sc->sectorsize, 1057a03be42dSMaxim Sobolev DEVSTAT_ALL_SUPPORTED, DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 10586f4f00f1SPoul-Henning Kamp } 105971e4fff8SPoul-Henning Kamp 10608f8def9eSPoul-Henning Kamp static int 1061b830359bSPawel Jakub Dawidek mdcreate_malloc(struct md_s *sc, struct md_ioctl *mdio) 106295f1a897SPoul-Henning Kamp { 1063c6517568SPoul-Henning Kamp uintptr_t sp; 1064c6517568SPoul-Henning Kamp int error; 1065b830359bSPawel Jakub Dawidek off_t u; 106695f1a897SPoul-Henning Kamp 1067c6517568SPoul-Henning Kamp error = 0; 10688f8def9eSPoul-Henning Kamp if (mdio->md_options & ~(MD_AUTOUNIT | MD_COMPRESS | MD_RESERVE)) 10698f8def9eSPoul-Henning Kamp return (EINVAL); 1070b830359bSPawel Jakub Dawidek if (mdio->md_sectorsize != 0 && !powerof2(mdio->md_sectorsize)) 1071ebe789d6SPoul-Henning Kamp return (EINVAL); 10728f8def9eSPoul-Henning Kamp /* Compression doesn't make sense if we have reserved space */ 10738f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) 10748f8def9eSPoul-Henning Kamp mdio->md_options &= ~MD_COMPRESS; 10754e8bfe14SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 10764e8bfe14SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 10774e8bfe14SPoul-Henning Kamp if (mdio->md_fwheads != 0) 10784e8bfe14SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 107926a0ee75SDima Dorfman sc->flags = mdio->md_options & (MD_COMPRESS | MD_FORCE); 1080b830359bSPawel Jakub Dawidek sc->indir = dimension(sc->mediasize / sc->sectorsize); 1081b830359bSPawel Jakub Dawidek sc->uma = uma_zcreate(sc->name, sc->sectorsize, NULL, NULL, NULL, NULL, 1082b830359bSPawel Jakub Dawidek 0x1ff, 0); 108396b6a55fSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1084b830359bSPawel Jakub Dawidek off_t nsectors; 1085b830359bSPawel Jakub Dawidek 1086b830359bSPawel Jakub Dawidek nsectors = sc->mediasize / sc->sectorsize; 1087b830359bSPawel Jakub Dawidek for (u = 0; u < nsectors; u++) { 1088007777f1SKonstantin Belousov sp = (uintptr_t)uma_zalloc(sc->uma, (md_malloc_wait ? 1089007777f1SKonstantin Belousov M_WAITOK : M_NOWAIT) | M_ZERO); 1090c6517568SPoul-Henning Kamp if (sp != 0) 1091fde2a2e4SPoul-Henning Kamp error = s_write(sc->indir, u, sp); 1092c6517568SPoul-Henning Kamp else 1093c6517568SPoul-Henning Kamp error = ENOMEM; 1094b830359bSPawel Jakub Dawidek if (error != 0) 1095c6517568SPoul-Henning Kamp break; 10968f8def9eSPoul-Henning Kamp } 1097c6517568SPoul-Henning Kamp } 1098c6517568SPoul-Henning Kamp return (error); 109900a6a3c6SPoul-Henning Kamp } 110000a6a3c6SPoul-Henning Kamp 11013f54a085SPoul-Henning Kamp 11028f8def9eSPoul-Henning Kamp static int 11038f8def9eSPoul-Henning Kamp mdsetcred(struct md_s *sc, struct ucred *cred) 11048f8def9eSPoul-Henning Kamp { 11058f8def9eSPoul-Henning Kamp char *tmpbuf; 11068f8def9eSPoul-Henning Kamp int error = 0; 11078f8def9eSPoul-Henning Kamp 11083f54a085SPoul-Henning Kamp /* 11098f8def9eSPoul-Henning Kamp * Set credits in our softc 11103f54a085SPoul-Henning Kamp */ 11118f8def9eSPoul-Henning Kamp 11128f8def9eSPoul-Henning Kamp if (sc->cred) 11138f8def9eSPoul-Henning Kamp crfree(sc->cred); 1114bd78ceceSJohn Baldwin sc->cred = crhold(cred); 11158f8def9eSPoul-Henning Kamp 11168f8def9eSPoul-Henning Kamp /* 11178f8def9eSPoul-Henning Kamp * Horrible kludge to establish credentials for NFS XXX. 11188f8def9eSPoul-Henning Kamp */ 11198f8def9eSPoul-Henning Kamp 11208f8def9eSPoul-Henning Kamp if (sc->vnode) { 11218f8def9eSPoul-Henning Kamp struct uio auio; 11228f8def9eSPoul-Henning Kamp struct iovec aiov; 11238f8def9eSPoul-Henning Kamp 1124b830359bSPawel Jakub Dawidek tmpbuf = malloc(sc->sectorsize, M_TEMP, M_WAITOK); 11258f8def9eSPoul-Henning Kamp bzero(&auio, sizeof(auio)); 11268f8def9eSPoul-Henning Kamp 11278f8def9eSPoul-Henning Kamp aiov.iov_base = tmpbuf; 1128b830359bSPawel Jakub Dawidek aiov.iov_len = sc->sectorsize; 11298f8def9eSPoul-Henning Kamp auio.uio_iov = &aiov; 11308f8def9eSPoul-Henning Kamp auio.uio_iovcnt = 1; 11318f8def9eSPoul-Henning Kamp auio.uio_offset = 0; 11328f8def9eSPoul-Henning Kamp auio.uio_rw = UIO_READ; 11338f8def9eSPoul-Henning Kamp auio.uio_segflg = UIO_SYSSPACE; 11348f8def9eSPoul-Henning Kamp auio.uio_resid = aiov.iov_len; 1135cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 11368f8def9eSPoul-Henning Kamp error = VOP_READ(sc->vnode, &auio, 0, sc->cred); 113722db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 11388f8def9eSPoul-Henning Kamp free(tmpbuf, M_TEMP); 11398f8def9eSPoul-Henning Kamp } 11408f8def9eSPoul-Henning Kamp return (error); 11418f8def9eSPoul-Henning Kamp } 11428f8def9eSPoul-Henning Kamp 11438f8def9eSPoul-Henning Kamp static int 1144b830359bSPawel Jakub Dawidek mdcreate_vnode(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 11458f8def9eSPoul-Henning Kamp { 11468f8def9eSPoul-Henning Kamp struct vattr vattr; 11478f8def9eSPoul-Henning Kamp struct nameidata nd; 11483d5c947dSMarcel Moolenaar char *fname; 11495050aa86SKonstantin Belousov int error, flags; 11508f8def9eSPoul-Henning Kamp 11513d5c947dSMarcel Moolenaar /* 11523d5c947dSMarcel Moolenaar * Kernel-originated requests must have the filename appended 11533d5c947dSMarcel Moolenaar * to the mdio structure to protect against malicious software. 11543d5c947dSMarcel Moolenaar */ 11553d5c947dSMarcel Moolenaar fname = mdio->md_file; 11563d5c947dSMarcel Moolenaar if ((void *)fname != (void *)(mdio + 1)) { 11573d5c947dSMarcel Moolenaar error = copyinstr(fname, sc->file, sizeof(sc->file), NULL); 115888b5b78dSPawel Jakub Dawidek if (error != 0) 115988b5b78dSPawel Jakub Dawidek return (error); 11603d5c947dSMarcel Moolenaar } else 11613d5c947dSMarcel Moolenaar strlcpy(sc->file, fname, sizeof(sc->file)); 11623d5c947dSMarcel Moolenaar 116386776891SChristian S.J. Peron /* 11643d5c947dSMarcel Moolenaar * If the user specified that this is a read only device, don't 11653d5c947dSMarcel Moolenaar * set the FWRITE mask before trying to open the backing store. 116686776891SChristian S.J. Peron */ 11673d5c947dSMarcel Moolenaar flags = FREAD | ((mdio->md_options & MD_READONLY) ? 0 : FWRITE); 11685050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, sc->file, td); 11699e223287SKonstantin Belousov error = vn_open(&nd, &flags, 0, NULL); 1170e3ed29a7SPawel Jakub Dawidek if (error != 0) 117152c6716fSPawel Jakub Dawidek return (error); 1172b322d85dSPawel Jakub Dawidek NDFREE(&nd, NDF_ONLY_PNBUF); 117333fc3625SJohn Baldwin if (nd.ni_vp->v_type != VREG) { 117433fc3625SJohn Baldwin error = EINVAL; 117533fc3625SJohn Baldwin goto bad; 117633fc3625SJohn Baldwin } 117733fc3625SJohn Baldwin error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred); 117833fc3625SJohn Baldwin if (error != 0) 117933fc3625SJohn Baldwin goto bad; 118033fc3625SJohn Baldwin if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) { 118133fc3625SJohn Baldwin vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY); 118233fc3625SJohn Baldwin if (nd.ni_vp->v_iflag & VI_DOOMED) { 118333fc3625SJohn Baldwin /* Forced unmount. */ 118433fc3625SJohn Baldwin error = EBADF; 118533fc3625SJohn Baldwin goto bad; 118633fc3625SJohn Baldwin } 11878f8def9eSPoul-Henning Kamp } 11883b7b5496SKonstantin Belousov nd.ni_vp->v_vflag |= VV_MD; 118922db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 11909589c256SPoul-Henning Kamp 1191d5a929dcSPoul-Henning Kamp if (mdio->md_fwsectors != 0) 1192d5a929dcSPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 1193d5a929dcSPoul-Henning Kamp if (mdio->md_fwheads != 0) 1194d5a929dcSPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 11957a6b2b64SPoul-Henning Kamp sc->flags = mdio->md_options & (MD_FORCE | MD_ASYNC); 11969589c256SPoul-Henning Kamp if (!(flags & FWRITE)) 11979589c256SPoul-Henning Kamp sc->flags |= MD_READONLY; 11988f8def9eSPoul-Henning Kamp sc->vnode = nd.ni_vp; 11998f8def9eSPoul-Henning Kamp 1200a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 1201b830359bSPawel Jakub Dawidek if (error != 0) { 12023cf74e53SPhilip Paeps sc->vnode = NULL; 1203cb05b60aSAttilio Rao vn_lock(nd.ni_vp, LK_EXCLUSIVE | LK_RETRY); 12043b7b5496SKonstantin Belousov nd.ni_vp->v_vflag &= ~VV_MD; 120533fc3625SJohn Baldwin goto bad; 120633fc3625SJohn Baldwin } 120733fc3625SJohn Baldwin return (0); 120833fc3625SJohn Baldwin bad: 120922db15c0SAttilio Rao VOP_UNLOCK(nd.ni_vp, 0); 121052c6716fSPawel Jakub Dawidek (void)vn_close(nd.ni_vp, flags, td->td_ucred, td); 12118f8def9eSPoul-Henning Kamp return (error); 12128f8def9eSPoul-Henning Kamp } 12138f8def9eSPoul-Henning Kamp 12148f8def9eSPoul-Henning Kamp static int 1215b40ce416SJulian Elischer mddestroy(struct md_s *sc, struct thread *td) 12168f8def9eSPoul-Henning Kamp { 12170cddd8f0SMatthew Dillon 12186f4f00f1SPoul-Henning Kamp if (sc->gp) { 12196f4f00f1SPoul-Henning Kamp sc->gp->softc = NULL; 12209b00ca19SPoul-Henning Kamp g_topology_lock(); 12219b00ca19SPoul-Henning Kamp g_wither_geom(sc->gp, ENXIO); 12229b00ca19SPoul-Henning Kamp g_topology_unlock(); 12236b60a2cdSPoul-Henning Kamp sc->gp = NULL; 12246b60a2cdSPoul-Henning Kamp sc->pp = NULL; 12251f4ee1aaSPoul-Henning Kamp } 1226a03be42dSMaxim Sobolev if (sc->devstat) { 1227a03be42dSMaxim Sobolev devstat_remove_entry(sc->devstat); 1228a03be42dSMaxim Sobolev sc->devstat = NULL; 1229a03be42dSMaxim Sobolev } 1230a08d2e7fSJohn Baldwin mtx_lock(&sc->queue_mtx); 12315c97ca54SIan Dowse sc->flags |= MD_SHUTDOWN; 12325c97ca54SIan Dowse wakeup(sc); 1233a08d2e7fSJohn Baldwin while (!(sc->flags & MD_EXITING)) 1234a08d2e7fSJohn Baldwin msleep(sc->procp, &sc->queue_mtx, PRIBIO, "mddestroy", hz / 10); 1235a08d2e7fSJohn Baldwin mtx_unlock(&sc->queue_mtx); 123640ea77a0SAlexander Motin mtx_destroy(&sc->stat_mtx); 12379fbea3e3SPoul-Henning Kamp mtx_destroy(&sc->queue_mtx); 12389b00ca19SPoul-Henning Kamp if (sc->vnode != NULL) { 1239cb05b60aSAttilio Rao vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY); 12403b7b5496SKonstantin Belousov sc->vnode->v_vflag &= ~VV_MD; 124122db15c0SAttilio Rao VOP_UNLOCK(sc->vnode, 0); 12429d4b5945SMaxim Sobolev (void)vn_close(sc->vnode, sc->flags & MD_READONLY ? 1243b40ce416SJulian Elischer FREAD : (FREAD|FWRITE), sc->cred, td); 12449b00ca19SPoul-Henning Kamp } 12458f8def9eSPoul-Henning Kamp if (sc->cred != NULL) 12468f8def9eSPoul-Henning Kamp crfree(sc->cred); 12471db17c6dSPawel Jakub Dawidek if (sc->object != NULL) 1248f820bc50SAlan Cox vm_object_deallocate(sc->object); 1249f43b2bacSPoul-Henning Kamp if (sc->indir) 1250f43b2bacSPoul-Henning Kamp destroy_indir(sc, sc->indir); 1251f43b2bacSPoul-Henning Kamp if (sc->uma) 1252f43b2bacSPoul-Henning Kamp uma_zdestroy(sc->uma); 12531f4ee1aaSPoul-Henning Kamp 12541f4ee1aaSPoul-Henning Kamp LIST_REMOVE(sc, list); 1255f4e7c5a8SJaakko Heinonen free_unr(md_uh, sc->unit); 1256c6517568SPoul-Henning Kamp free(sc, M_MD); 12578f8def9eSPoul-Henning Kamp return (0); 12588f8def9eSPoul-Henning Kamp } 12598f8def9eSPoul-Henning Kamp 12608f8def9eSPoul-Henning Kamp static int 1261dc604f0cSEdward Tomasz Napierala mdresize(struct md_s *sc, struct md_ioctl *mdio) 1262dc604f0cSEdward Tomasz Napierala { 1263dc604f0cSEdward Tomasz Napierala int error, res; 1264dc604f0cSEdward Tomasz Napierala vm_pindex_t oldpages, newpages; 1265dc604f0cSEdward Tomasz Napierala 1266dc604f0cSEdward Tomasz Napierala switch (sc->type) { 1267dc604f0cSEdward Tomasz Napierala case MD_VNODE: 12680efd9bfdSEdward Tomasz Napierala case MD_NULL: 1269dc604f0cSEdward Tomasz Napierala break; 1270dc604f0cSEdward Tomasz Napierala case MD_SWAP: 12718cb51643SJaakko Heinonen if (mdio->md_mediasize <= 0 || 1272dc604f0cSEdward Tomasz Napierala (mdio->md_mediasize % PAGE_SIZE) != 0) 1273dc604f0cSEdward Tomasz Napierala return (EDOM); 1274dc604f0cSEdward Tomasz Napierala oldpages = OFF_TO_IDX(round_page(sc->mediasize)); 1275dc604f0cSEdward Tomasz Napierala newpages = OFF_TO_IDX(round_page(mdio->md_mediasize)); 1276dc604f0cSEdward Tomasz Napierala if (newpages < oldpages) { 127789f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1278dc604f0cSEdward Tomasz Napierala vm_object_page_remove(sc->object, newpages, 0, 0); 1279dc604f0cSEdward Tomasz Napierala swap_pager_freespace(sc->object, newpages, 1280dc604f0cSEdward Tomasz Napierala oldpages - newpages); 1281dc604f0cSEdward Tomasz Napierala swap_release_by_cred(IDX_TO_OFF(oldpages - 1282dc604f0cSEdward Tomasz Napierala newpages), sc->cred); 1283dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1284dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 128589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1286dc604f0cSEdward Tomasz Napierala } else if (newpages > oldpages) { 1287dc604f0cSEdward Tomasz Napierala res = swap_reserve_by_cred(IDX_TO_OFF(newpages - 1288dc604f0cSEdward Tomasz Napierala oldpages), sc->cred); 1289dc604f0cSEdward Tomasz Napierala if (!res) 1290dc604f0cSEdward Tomasz Napierala return (ENOMEM); 1291dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & MD_RESERVE) || 1292dc604f0cSEdward Tomasz Napierala (sc->flags & MD_RESERVE)) { 1293dc604f0cSEdward Tomasz Napierala error = swap_pager_reserve(sc->object, 1294dc604f0cSEdward Tomasz Napierala oldpages, newpages - oldpages); 1295dc604f0cSEdward Tomasz Napierala if (error < 0) { 1296dc604f0cSEdward Tomasz Napierala swap_release_by_cred( 1297dc604f0cSEdward Tomasz Napierala IDX_TO_OFF(newpages - oldpages), 1298dc604f0cSEdward Tomasz Napierala sc->cred); 1299dc604f0cSEdward Tomasz Napierala return (EDOM); 1300dc604f0cSEdward Tomasz Napierala } 1301dc604f0cSEdward Tomasz Napierala } 130289f6b863SAttilio Rao VM_OBJECT_WLOCK(sc->object); 1303dc604f0cSEdward Tomasz Napierala sc->object->charge = IDX_TO_OFF(newpages); 1304dc604f0cSEdward Tomasz Napierala sc->object->size = newpages; 130589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(sc->object); 1306dc604f0cSEdward Tomasz Napierala } 1307dc604f0cSEdward Tomasz Napierala break; 1308dc604f0cSEdward Tomasz Napierala default: 1309dc604f0cSEdward Tomasz Napierala return (EOPNOTSUPP); 1310dc604f0cSEdward Tomasz Napierala } 1311dc604f0cSEdward Tomasz Napierala 1312dc604f0cSEdward Tomasz Napierala sc->mediasize = mdio->md_mediasize; 1313dc604f0cSEdward Tomasz Napierala g_topology_lock(); 1314dc604f0cSEdward Tomasz Napierala g_resize_provider(sc->pp, sc->mediasize); 1315dc604f0cSEdward Tomasz Napierala g_topology_unlock(); 1316dc604f0cSEdward Tomasz Napierala return (0); 1317dc604f0cSEdward Tomasz Napierala } 1318dc604f0cSEdward Tomasz Napierala 1319dc604f0cSEdward Tomasz Napierala static int 1320b830359bSPawel Jakub Dawidek mdcreate_swap(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 13218f8def9eSPoul-Henning Kamp { 1322fcd57fbeSPawel Jakub Dawidek vm_ooffset_t npage; 1323fcd57fbeSPawel Jakub Dawidek int error; 13248f8def9eSPoul-Henning Kamp 13258f8def9eSPoul-Henning Kamp /* 1326bc2308d4SEdward Tomasz Napierala * Range check. Disallow negative sizes and sizes not being 1327bc2308d4SEdward Tomasz Napierala * multiple of page size. 13288f8def9eSPoul-Henning Kamp */ 13298cb51643SJaakko Heinonen if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 13308f8def9eSPoul-Henning Kamp return (EDOM); 13318f8def9eSPoul-Henning Kamp 13328f8def9eSPoul-Henning Kamp /* 13338f8def9eSPoul-Henning Kamp * Allocate an OBJT_SWAP object. 13348f8def9eSPoul-Henning Kamp * 13358f8def9eSPoul-Henning Kamp * Note the truncation. 13368f8def9eSPoul-Henning Kamp */ 13378f8def9eSPoul-Henning Kamp 1338b830359bSPawel Jakub Dawidek npage = mdio->md_mediasize / PAGE_SIZE; 13399ed40643SPoul-Henning Kamp if (mdio->md_fwsectors != 0) 13409ed40643SPoul-Henning Kamp sc->fwsectors = mdio->md_fwsectors; 13419ed40643SPoul-Henning Kamp if (mdio->md_fwheads != 0) 13429ed40643SPoul-Henning Kamp sc->fwheads = mdio->md_fwheads; 1343fcd57fbeSPawel Jakub Dawidek sc->object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * npage, 13443364c323SKonstantin Belousov VM_PROT_DEFAULT, 0, td->td_ucred); 1345812851b6SBrian Feldman if (sc->object == NULL) 1346812851b6SBrian Feldman return (ENOMEM); 1347dc604f0cSEdward Tomasz Napierala sc->flags = mdio->md_options & (MD_FORCE | MD_RESERVE); 13488f8def9eSPoul-Henning Kamp if (mdio->md_options & MD_RESERVE) { 1349fcd57fbeSPawel Jakub Dawidek if (swap_pager_reserve(sc->object, 0, npage) < 0) { 13503364c323SKonstantin Belousov error = EDOM; 13513364c323SKonstantin Belousov goto finish; 13528f8def9eSPoul-Henning Kamp } 13538f8def9eSPoul-Henning Kamp } 1354a854ed98SJohn Baldwin error = mdsetcred(sc, td->td_ucred); 13553364c323SKonstantin Belousov finish: 1356e3ed29a7SPawel Jakub Dawidek if (error != 0) { 13572eafd8b1SPawel Jakub Dawidek vm_object_deallocate(sc->object); 13582eafd8b1SPawel Jakub Dawidek sc->object = NULL; 13598f8def9eSPoul-Henning Kamp } 1360b830359bSPawel Jakub Dawidek return (error); 1361b3b3d1b7SPoul-Henning Kamp } 13628f8def9eSPoul-Henning Kamp 13630efd9bfdSEdward Tomasz Napierala static int 13640efd9bfdSEdward Tomasz Napierala mdcreate_null(struct md_s *sc, struct md_ioctl *mdio, struct thread *td) 13650efd9bfdSEdward Tomasz Napierala { 13660efd9bfdSEdward Tomasz Napierala 13670efd9bfdSEdward Tomasz Napierala /* 1368bc2308d4SEdward Tomasz Napierala * Range check. Disallow negative sizes and sizes not being 1369bc2308d4SEdward Tomasz Napierala * multiple of page size. 13700efd9bfdSEdward Tomasz Napierala */ 13710efd9bfdSEdward Tomasz Napierala if (sc->mediasize <= 0 || (sc->mediasize % PAGE_SIZE) != 0) 13720efd9bfdSEdward Tomasz Napierala return (EDOM); 13730efd9bfdSEdward Tomasz Napierala 13740efd9bfdSEdward Tomasz Napierala return (0); 13750efd9bfdSEdward Tomasz Napierala } 13769d4b5945SMaxim Sobolev 13779d4b5945SMaxim Sobolev static int 13789b00ca19SPoul-Henning Kamp xmdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 13798f8def9eSPoul-Henning Kamp { 13808f8def9eSPoul-Henning Kamp struct md_ioctl *mdio; 13818f8def9eSPoul-Henning Kamp struct md_s *sc; 1382b830359bSPawel Jakub Dawidek int error, i; 13838cb51643SJaakko Heinonen unsigned sectsize; 13848f8def9eSPoul-Henning Kamp 13858f8def9eSPoul-Henning Kamp if (md_debug) 13868f8def9eSPoul-Henning Kamp printf("mdctlioctl(%s %lx %p %x %p)\n", 1387b40ce416SJulian Elischer devtoname(dev), cmd, addr, flags, td); 13888f8def9eSPoul-Henning Kamp 13899b00ca19SPoul-Henning Kamp mdio = (struct md_ioctl *)addr; 13909b00ca19SPoul-Henning Kamp if (mdio->md_version != MDIOVERSION) 13919b00ca19SPoul-Henning Kamp return (EINVAL); 13929b00ca19SPoul-Henning Kamp 139353d745bcSDima Dorfman /* 139453d745bcSDima Dorfman * We assert the version number in the individual ioctl 139553d745bcSDima Dorfman * handlers instead of out here because (a) it is possible we 139653d745bcSDima Dorfman * may add another ioctl in the future which doesn't read an 139753d745bcSDima Dorfman * mdio, and (b) the correct return value for an unknown ioctl 139853d745bcSDima Dorfman * is ENOIOCTL, not EINVAL. 139953d745bcSDima Dorfman */ 14009b00ca19SPoul-Henning Kamp error = 0; 14018f8def9eSPoul-Henning Kamp switch (cmd) { 14028f8def9eSPoul-Henning Kamp case MDIOCATTACH: 14038f8def9eSPoul-Henning Kamp switch (mdio->md_type) { 14048f8def9eSPoul-Henning Kamp case MD_MALLOC: 14058f8def9eSPoul-Henning Kamp case MD_PRELOAD: 14068f8def9eSPoul-Henning Kamp case MD_VNODE: 14078f8def9eSPoul-Henning Kamp case MD_SWAP: 14080efd9bfdSEdward Tomasz Napierala case MD_NULL: 1409b830359bSPawel Jakub Dawidek break; 14108f8def9eSPoul-Henning Kamp default: 14118f8def9eSPoul-Henning Kamp return (EINVAL); 14128f8def9eSPoul-Henning Kamp } 14138cb51643SJaakko Heinonen if (mdio->md_sectorsize == 0) 14148cb51643SJaakko Heinonen sectsize = DEV_BSIZE; 14158cb51643SJaakko Heinonen else 14168cb51643SJaakko Heinonen sectsize = mdio->md_sectorsize; 14178cb51643SJaakko Heinonen if (sectsize > MAXPHYS || mdio->md_mediasize < sectsize) 14188cb51643SJaakko Heinonen return (EINVAL); 14197ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 1420947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, mdio->md_type); 1421f4e7c5a8SJaakko Heinonen else { 1422f4e7c5a8SJaakko Heinonen if (mdio->md_unit > INT_MAX) 1423f4e7c5a8SJaakko Heinonen return (EINVAL); 1424947fc8deSPoul-Henning Kamp sc = mdnew(mdio->md_unit, &error, mdio->md_type); 1425f4e7c5a8SJaakko Heinonen } 1426b830359bSPawel Jakub Dawidek if (sc == NULL) 14277ee3c044SPawel Jakub Dawidek return (error); 14287ee3c044SPawel Jakub Dawidek if (mdio->md_options & MD_AUTOUNIT) 14297ee3c044SPawel Jakub Dawidek mdio->md_unit = sc->unit; 1430b830359bSPawel Jakub Dawidek sc->mediasize = mdio->md_mediasize; 14318cb51643SJaakko Heinonen sc->sectorsize = sectsize; 1432b830359bSPawel Jakub Dawidek error = EDOOFUS; 1433b830359bSPawel Jakub Dawidek switch (sc->type) { 1434b830359bSPawel Jakub Dawidek case MD_MALLOC: 14359b00ca19SPoul-Henning Kamp sc->start = mdstart_malloc; 1436b830359bSPawel Jakub Dawidek error = mdcreate_malloc(sc, mdio); 1437b830359bSPawel Jakub Dawidek break; 1438b830359bSPawel Jakub Dawidek case MD_PRELOAD: 1439734e78dfSJaakko Heinonen /* 1440734e78dfSJaakko Heinonen * We disallow attaching preloaded memory disks via 1441734e78dfSJaakko Heinonen * ioctl. Preloaded memory disks are automatically 1442734e78dfSJaakko Heinonen * attached in g_md_init(). 1443734e78dfSJaakko Heinonen */ 1444734e78dfSJaakko Heinonen error = EOPNOTSUPP; 1445b830359bSPawel Jakub Dawidek break; 1446b830359bSPawel Jakub Dawidek case MD_VNODE: 14479b00ca19SPoul-Henning Kamp sc->start = mdstart_vnode; 1448b830359bSPawel Jakub Dawidek error = mdcreate_vnode(sc, mdio, td); 1449b830359bSPawel Jakub Dawidek break; 1450b830359bSPawel Jakub Dawidek case MD_SWAP: 14519b00ca19SPoul-Henning Kamp sc->start = mdstart_swap; 1452b830359bSPawel Jakub Dawidek error = mdcreate_swap(sc, mdio, td); 1453b830359bSPawel Jakub Dawidek break; 14540efd9bfdSEdward Tomasz Napierala case MD_NULL: 14550efd9bfdSEdward Tomasz Napierala sc->start = mdstart_null; 14560efd9bfdSEdward Tomasz Napierala error = mdcreate_null(sc, mdio, td); 14570efd9bfdSEdward Tomasz Napierala break; 1458b830359bSPawel Jakub Dawidek } 1459b830359bSPawel Jakub Dawidek if (error != 0) { 1460b830359bSPawel Jakub Dawidek mddestroy(sc, td); 1461b830359bSPawel Jakub Dawidek return (error); 1462b830359bSPawel Jakub Dawidek } 14639b00ca19SPoul-Henning Kamp 14649b00ca19SPoul-Henning Kamp /* Prune off any residual fractional sector */ 14659b00ca19SPoul-Henning Kamp i = sc->mediasize % sc->sectorsize; 14669b00ca19SPoul-Henning Kamp sc->mediasize -= i; 14679b00ca19SPoul-Henning Kamp 1468b830359bSPawel Jakub Dawidek mdinit(sc); 1469b830359bSPawel Jakub Dawidek return (0); 14708f8def9eSPoul-Henning Kamp case MDIOCDETACH: 1471a9ebb311SEdward Tomasz Napierala if (mdio->md_mediasize != 0 || 1472a9ebb311SEdward Tomasz Napierala (mdio->md_options & ~MD_FORCE) != 0) 14738f8def9eSPoul-Henning Kamp return (EINVAL); 14749b00ca19SPoul-Henning Kamp 14759b00ca19SPoul-Henning Kamp sc = mdfind(mdio->md_unit); 14769b00ca19SPoul-Henning Kamp if (sc == NULL) 14779b00ca19SPoul-Henning Kamp return (ENOENT); 1478a9ebb311SEdward Tomasz Napierala if (sc->opencount != 0 && !(sc->flags & MD_FORCE) && 1479a9ebb311SEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 14809b00ca19SPoul-Henning Kamp return (EBUSY); 14819b00ca19SPoul-Henning Kamp return (mddestroy(sc, td)); 1482dc604f0cSEdward Tomasz Napierala case MDIOCRESIZE: 1483dc604f0cSEdward Tomasz Napierala if ((mdio->md_options & ~(MD_FORCE | MD_RESERVE)) != 0) 1484dc604f0cSEdward Tomasz Napierala return (EINVAL); 1485dc604f0cSEdward Tomasz Napierala 1486dc604f0cSEdward Tomasz Napierala sc = mdfind(mdio->md_unit); 1487dc604f0cSEdward Tomasz Napierala if (sc == NULL) 1488dc604f0cSEdward Tomasz Napierala return (ENOENT); 14898cb51643SJaakko Heinonen if (mdio->md_mediasize < sc->sectorsize) 14908cb51643SJaakko Heinonen return (EINVAL); 1491dc604f0cSEdward Tomasz Napierala if (mdio->md_mediasize < sc->mediasize && 1492dc604f0cSEdward Tomasz Napierala !(sc->flags & MD_FORCE) && 1493dc604f0cSEdward Tomasz Napierala !(mdio->md_options & MD_FORCE)) 1494dc604f0cSEdward Tomasz Napierala return (EBUSY); 1495dc604f0cSEdward Tomasz Napierala return (mdresize(sc, mdio)); 1496174b5e9aSPoul-Henning Kamp case MDIOCQUERY: 1497174b5e9aSPoul-Henning Kamp sc = mdfind(mdio->md_unit); 1498174b5e9aSPoul-Henning Kamp if (sc == NULL) 1499174b5e9aSPoul-Henning Kamp return (ENOENT); 1500174b5e9aSPoul-Henning Kamp mdio->md_type = sc->type; 1501174b5e9aSPoul-Henning Kamp mdio->md_options = sc->flags; 1502b830359bSPawel Jakub Dawidek mdio->md_mediasize = sc->mediasize; 1503b830359bSPawel Jakub Dawidek mdio->md_sectorsize = sc->sectorsize; 15049b00ca19SPoul-Henning Kamp if (sc->type == MD_VNODE) 150588b5b78dSPawel Jakub Dawidek error = copyout(sc->file, mdio->md_file, 150688b5b78dSPawel Jakub Dawidek strlen(sc->file) + 1); 150788b5b78dSPawel Jakub Dawidek return (error); 150816bcbe8cSPoul-Henning Kamp case MDIOCLIST: 150916bcbe8cSPoul-Henning Kamp i = 1; 151016bcbe8cSPoul-Henning Kamp LIST_FOREACH(sc, &md_softc_list, list) { 151116bcbe8cSPoul-Henning Kamp if (i == MDNPAD - 1) 151216bcbe8cSPoul-Henning Kamp mdio->md_pad[i] = -1; 151316bcbe8cSPoul-Henning Kamp else 151416bcbe8cSPoul-Henning Kamp mdio->md_pad[i++] = sc->unit; 151516bcbe8cSPoul-Henning Kamp } 151616bcbe8cSPoul-Henning Kamp mdio->md_pad[0] = i - 1; 151716bcbe8cSPoul-Henning Kamp return (0); 15188f8def9eSPoul-Henning Kamp default: 15198f8def9eSPoul-Henning Kamp return (ENOIOCTL); 15208f8def9eSPoul-Henning Kamp }; 15219b00ca19SPoul-Henning Kamp } 15229b00ca19SPoul-Henning Kamp 15239b00ca19SPoul-Henning Kamp static int 15249b00ca19SPoul-Henning Kamp mdctlioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 15259b00ca19SPoul-Henning Kamp { 15269b00ca19SPoul-Henning Kamp int error; 15279b00ca19SPoul-Henning Kamp 15289b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 15299b00ca19SPoul-Henning Kamp error = xmdctlioctl(dev, cmd, addr, flags, td); 15309b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 15319b00ca19SPoul-Henning Kamp return (error); 15323f54a085SPoul-Henning Kamp } 15333f54a085SPoul-Henning Kamp 153400a6a3c6SPoul-Henning Kamp static void 1535341b240dSJaakko Heinonen md_preloaded(u_char *image, size_t length, const char *name) 1536637f671aSPoul-Henning Kamp { 1537637f671aSPoul-Henning Kamp struct md_s *sc; 15389b00ca19SPoul-Henning Kamp int error; 1539637f671aSPoul-Henning Kamp 1540947fc8deSPoul-Henning Kamp sc = mdnew(-1, &error, MD_PRELOAD); 1541637f671aSPoul-Henning Kamp if (sc == NULL) 1542637f671aSPoul-Henning Kamp return; 1543b830359bSPawel Jakub Dawidek sc->mediasize = length; 1544b830359bSPawel Jakub Dawidek sc->sectorsize = DEV_BSIZE; 1545637f671aSPoul-Henning Kamp sc->pl_ptr = image; 1546637f671aSPoul-Henning Kamp sc->pl_len = length; 15479b00ca19SPoul-Henning Kamp sc->start = mdstart_preload; 15485d4ca75eSLuigi Rizzo #ifdef MD_ROOT 1549637f671aSPoul-Henning Kamp if (sc->unit == 0) 155022ff74b2SMarcel Moolenaar rootdevnames[0] = MD_ROOT_FSTYPE ":/dev/md0"; 15515d4ca75eSLuigi Rizzo #endif 1552637f671aSPoul-Henning Kamp mdinit(sc); 1553341b240dSJaakko Heinonen if (name != NULL) { 1554341b240dSJaakko Heinonen printf("%s%d: Preloaded image <%s> %zd bytes at %p\n", 1555341b240dSJaakko Heinonen MD_NAME, sc->unit, name, length, image); 1556341b240dSJaakko Heinonen } 1557637f671aSPoul-Henning Kamp } 1558637f671aSPoul-Henning Kamp 1559637f671aSPoul-Henning Kamp static void 156019945697SPoul-Henning Kamp g_md_init(struct g_class *mp __unused) 156100a6a3c6SPoul-Henning Kamp { 156295f1a897SPoul-Henning Kamp caddr_t mod; 156395f1a897SPoul-Henning Kamp u_char *ptr, *name, *type; 156495f1a897SPoul-Henning Kamp unsigned len; 1565d12fc952SKonstantin Belousov int i; 1566d12fc952SKonstantin Belousov 1567d12fc952SKonstantin Belousov /* figure out log2(NINDIR) */ 1568d12fc952SKonstantin Belousov for (i = NINDIR, nshift = -1; i; nshift++) 1569d12fc952SKonstantin Belousov i >>= 1; 157095f1a897SPoul-Henning Kamp 15710a937206SPoul-Henning Kamp mod = NULL; 15729b00ca19SPoul-Henning Kamp sx_init(&md_sx, "MD config lock"); 15730a937206SPoul-Henning Kamp g_topology_unlock(); 1574f4e7c5a8SJaakko Heinonen md_uh = new_unrhdr(0, INT_MAX, NULL); 157571e4fff8SPoul-Henning Kamp #ifdef MD_ROOT_SIZE 15769b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1577341b240dSJaakko Heinonen md_preloaded(mfs_root.start, sizeof(mfs_root.start), NULL); 15789b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 157971e4fff8SPoul-Henning Kamp #endif 15809b00ca19SPoul-Henning Kamp /* XXX: are preload_* static or do they need Giant ? */ 158195f1a897SPoul-Henning Kamp while ((mod = preload_search_next_name(mod)) != NULL) { 158295f1a897SPoul-Henning Kamp name = (char *)preload_search_info(mod, MODINFO_NAME); 158395f1a897SPoul-Henning Kamp if (name == NULL) 158495f1a897SPoul-Henning Kamp continue; 15859b00ca19SPoul-Henning Kamp type = (char *)preload_search_info(mod, MODINFO_TYPE); 158695f1a897SPoul-Henning Kamp if (type == NULL) 158795f1a897SPoul-Henning Kamp continue; 158871e4fff8SPoul-Henning Kamp if (strcmp(type, "md_image") && strcmp(type, "mfs_root")) 158995f1a897SPoul-Henning Kamp continue; 15908d5ac6c3SMarcel Moolenaar ptr = preload_fetch_addr(mod); 15918d5ac6c3SMarcel Moolenaar len = preload_fetch_size(mod); 15928d5ac6c3SMarcel Moolenaar if (ptr != NULL && len != 0) { 15939b00ca19SPoul-Henning Kamp sx_xlock(&md_sx); 1594341b240dSJaakko Heinonen md_preloaded(ptr, len, name); 15959b00ca19SPoul-Henning Kamp sx_xunlock(&md_sx); 159695f1a897SPoul-Henning Kamp } 15978d5ac6c3SMarcel Moolenaar } 159859ec9023SKonstantin Belousov md_vnode_pbuf_freecnt = nswbuf / 10; 159906d425f9SEd Schouten status_dev = make_dev(&mdctl_cdevsw, INT_MAX, UID_ROOT, GID_WHEEL, 160010b0e058SDima Dorfman 0600, MDCTL_NAME); 16010eb14309SPoul-Henning Kamp g_topology_lock(); 160200a6a3c6SPoul-Henning Kamp } 160300a6a3c6SPoul-Henning Kamp 160419945697SPoul-Henning Kamp static void 1605c27a8954SWojciech A. Koszek g_md_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1606c27a8954SWojciech A. Koszek struct g_consumer *cp __unused, struct g_provider *pp) 1607c27a8954SWojciech A. Koszek { 1608c27a8954SWojciech A. Koszek struct md_s *mp; 1609c27a8954SWojciech A. Koszek char *type; 1610c27a8954SWojciech A. Koszek 1611c27a8954SWojciech A. Koszek mp = gp->softc; 1612c27a8954SWojciech A. Koszek if (mp == NULL) 1613c27a8954SWojciech A. Koszek return; 1614c27a8954SWojciech A. Koszek 1615c27a8954SWojciech A. Koszek switch (mp->type) { 1616c27a8954SWojciech A. Koszek case MD_MALLOC: 1617c27a8954SWojciech A. Koszek type = "malloc"; 1618c27a8954SWojciech A. Koszek break; 1619c27a8954SWojciech A. Koszek case MD_PRELOAD: 1620c27a8954SWojciech A. Koszek type = "preload"; 1621c27a8954SWojciech A. Koszek break; 1622c27a8954SWojciech A. Koszek case MD_VNODE: 1623c27a8954SWojciech A. Koszek type = "vnode"; 1624c27a8954SWojciech A. Koszek break; 1625c27a8954SWojciech A. Koszek case MD_SWAP: 1626c27a8954SWojciech A. Koszek type = "swap"; 1627c27a8954SWojciech A. Koszek break; 16280efd9bfdSEdward Tomasz Napierala case MD_NULL: 16290efd9bfdSEdward Tomasz Napierala type = "null"; 16300efd9bfdSEdward Tomasz Napierala break; 1631c27a8954SWojciech A. Koszek default: 1632c27a8954SWojciech A. Koszek type = "unknown"; 1633c27a8954SWojciech A. Koszek break; 1634c27a8954SWojciech A. Koszek } 1635c27a8954SWojciech A. Koszek 1636c27a8954SWojciech A. Koszek if (pp != NULL) { 1637c27a8954SWojciech A. Koszek if (indent == NULL) { 1638c27a8954SWojciech A. Koszek sbuf_printf(sb, " u %d", mp->unit); 1639c27a8954SWojciech A. Koszek sbuf_printf(sb, " s %ju", (uintmax_t) mp->sectorsize); 1640c27a8954SWojciech A. Koszek sbuf_printf(sb, " f %ju", (uintmax_t) mp->fwheads); 1641c27a8954SWojciech A. Koszek sbuf_printf(sb, " fs %ju", (uintmax_t) mp->fwsectors); 1642c27a8954SWojciech A. Koszek sbuf_printf(sb, " l %ju", (uintmax_t) mp->mediasize); 1643c27a8954SWojciech A. Koszek sbuf_printf(sb, " t %s", type); 1644c27a8954SWojciech A. Koszek if (mp->type == MD_VNODE && mp->vnode != NULL) 1645c27a8954SWojciech A. Koszek sbuf_printf(sb, " file %s", mp->file); 1646c27a8954SWojciech A. Koszek } else { 1647c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, 1648c27a8954SWojciech A. Koszek mp->unit); 1649c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<sectorsize>%ju</sectorsize>\n", 1650c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->sectorsize); 1651c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwheads>%ju</fwheads>\n", 1652c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwheads); 1653c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<fwsectors>%ju</fwsectors>\n", 1654c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->fwsectors); 1655c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<length>%ju</length>\n", 1656c27a8954SWojciech A. Koszek indent, (uintmax_t) mp->mediasize); 16571f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<compression>%s</compression>\n", indent, 16581f192809SAndrey V. Elsukov (mp->flags & MD_COMPRESS) == 0 ? "off": "on"); 16591f192809SAndrey V. Elsukov sbuf_printf(sb, "%s<access>%s</access>\n", indent, 16601f192809SAndrey V. Elsukov (mp->flags & MD_READONLY) == 0 ? "read-write": 16611f192809SAndrey V. Elsukov "read-only"); 1662c27a8954SWojciech A. Koszek sbuf_printf(sb, "%s<type>%s</type>\n", indent, 1663c27a8954SWojciech A. Koszek type); 1664*ec170744SAndrey V. Elsukov if (mp->type == MD_VNODE && mp->vnode != NULL) { 1665*ec170744SAndrey V. Elsukov sbuf_printf(sb, "%s<file>", indent); 1666*ec170744SAndrey V. Elsukov g_conf_printf_escaped(sb, "%s", mp->file); 1667*ec170744SAndrey V. Elsukov sbuf_printf(sb, "</file>\n"); 1668*ec170744SAndrey V. Elsukov } 1669c27a8954SWojciech A. Koszek } 1670c27a8954SWojciech A. Koszek } 1671c27a8954SWojciech A. Koszek } 1672c27a8954SWojciech A. Koszek 1673c27a8954SWojciech A. Koszek static void 167419945697SPoul-Henning Kamp g_md_fini(struct g_class *mp __unused) 167557e9624eSPoul-Henning Kamp { 16769d4b5945SMaxim Sobolev 16779b00ca19SPoul-Henning Kamp sx_destroy(&md_sx); 167819945697SPoul-Henning Kamp if (status_dev != NULL) 167957e9624eSPoul-Henning Kamp destroy_dev(status_dev); 1680f4e7c5a8SJaakko Heinonen delete_unrhdr(md_uh); 168157e9624eSPoul-Henning Kamp } 1682