1 /*- 2 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/bio.h> 32 #include <sys/kernel.h> 33 #include <sys/limits.h> 34 #include <sys/malloc.h> 35 #include <sys/queue.h> 36 #include <sys/sysctl.h> 37 #include <sys/systm.h> 38 39 #include <geom/geom.h> 40 41 42 #define G_ZERO_CLASS_NAME "ZERO" 43 44 static int g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS); 45 46 SYSCTL_DECL(_kern_geom); 47 static SYSCTL_NODE(_kern_geom, OID_AUTO, zero, CTLFLAG_RW, 0, 48 "GEOM_ZERO stuff"); 49 static int g_zero_clear = 1; 50 SYSCTL_PROC(_kern_geom_zero, OID_AUTO, clear, CTLTYPE_INT|CTLFLAG_RW, 51 &g_zero_clear, 0, g_zero_clear_sysctl, "I", "Clear read data buffer"); 52 static int g_zero_byte = 0; 53 SYSCTL_INT(_kern_geom_zero, OID_AUTO, byte, CTLFLAG_RW, &g_zero_byte, 0, 54 "Byte (octet) value to clear the buffers with"); 55 56 static struct g_provider *gpp; 57 58 static int 59 g_zero_clear_sysctl(SYSCTL_HANDLER_ARGS) 60 { 61 int error; 62 63 error = sysctl_handle_int(oidp, &g_zero_clear, 0, req); 64 if (error != 0 || req->newptr == NULL) 65 return (error); 66 if (gpp == NULL) 67 return (ENXIO); 68 if (g_zero_clear) 69 gpp->flags &= ~G_PF_ACCEPT_UNMAPPED; 70 else 71 gpp->flags |= G_PF_ACCEPT_UNMAPPED; 72 return (0); 73 } 74 75 static void 76 g_zero_start(struct bio *bp) 77 { 78 int error = ENXIO; 79 80 switch (bp->bio_cmd) { 81 case BIO_READ: 82 if (g_zero_clear && (bp->bio_flags & BIO_UNMAPPED) == 0) 83 memset(bp->bio_data, g_zero_byte, bp->bio_length); 84 /* FALLTHROUGH */ 85 case BIO_DELETE: 86 case BIO_WRITE: 87 bp->bio_completed = bp->bio_length; 88 error = 0; 89 break; 90 case BIO_GETATTR: 91 default: 92 error = EOPNOTSUPP; 93 break; 94 } 95 g_io_deliver(bp, error); 96 } 97 98 static void 99 g_zero_init(struct g_class *mp) 100 { 101 struct g_geom *gp; 102 struct g_provider *pp; 103 104 g_topology_assert(); 105 gp = g_new_geomf(mp, "gzero"); 106 gp->start = g_zero_start; 107 gp->access = g_std_access; 108 gpp = pp = g_new_providerf(gp, "%s", gp->name); 109 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 110 if (!g_zero_clear) 111 pp->flags |= G_PF_ACCEPT_UNMAPPED; 112 pp->mediasize = 1152921504606846976LLU; 113 pp->sectorsize = 512; 114 g_error_provider(pp, 0); 115 } 116 117 static int 118 g_zero_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused, 119 struct g_geom *gp) 120 { 121 struct g_provider *pp; 122 123 g_topology_assert(); 124 if (gp == NULL) 125 return (0); 126 pp = LIST_FIRST(&gp->provider); 127 if (pp == NULL) 128 return (0); 129 if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0) 130 return (EBUSY); 131 gpp = NULL; 132 g_wither_geom(gp, ENXIO); 133 return (0); 134 } 135 136 static struct g_class g_zero_class = { 137 .name = G_ZERO_CLASS_NAME, 138 .version = G_VERSION, 139 .init = g_zero_init, 140 .destroy_geom = g_zero_destroy_geom 141 }; 142 143 DECLARE_GEOM_CLASS(g_zero_class, g_zero); 144