1 /*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD$ 36 */ 37 38 #include "opt_geom.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/sysctl.h> 44 #include <sys/bio.h> 45 #include <sys/conf.h> 46 #include <sys/disk.h> 47 #include <sys/malloc.h> 48 #include <sys/sysctl.h> 49 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <geom/geom.h> 53 #include <geom/geom_int.h> 54 55 static g_access_t g_ctl_access; 56 static g_start_t g_ctl_start; 57 static void g_ctl_init(void); 58 static d_ioctl_t g_ctl_ioctl; 59 60 struct g_class g_ctl_class = { 61 "GEOMCTL", 62 NULL, 63 NULL, 64 G_CLASS_INITIALIZER 65 }; 66 67 DECLARE_GEOM_CLASS_INIT(g_ctl_class, g_ctl, g_ctl_init); 68 69 /* 70 * We cannot do create our geom.ctl geom/provider in g_ctl_init() because 71 * the event thread has to finish adding our class first and that doesn't 72 * happen until later. We know however, that the events are processed in 73 * FIFO order, so scheduling g_ctl_init2() with g_call_me() is safe. 74 */ 75 76 static void 77 g_ctl_init2(void *p __unused) 78 { 79 struct g_geom *gp; 80 struct g_provider *pp; 81 82 g_topology_assert(); 83 gp = g_new_geomf(&g_ctl_class, "geom.ctl"); 84 gp->start = g_ctl_start; 85 gp->access = g_ctl_access; 86 pp = g_new_providerf(gp, "%s", gp->name); 87 pp->sectorsize = 512; 88 g_error_provider(pp, 0); 89 } 90 91 static void 92 g_ctl_init(void) 93 { 94 mtx_unlock(&Giant); 95 g_add_class(&g_ctl_class); 96 g_call_me(g_ctl_init2, NULL); 97 mtx_lock(&Giant); 98 } 99 100 /* 101 * We allow any kind of access. Access control is handled at the devfs 102 * level. 103 */ 104 105 static int 106 g_ctl_access(struct g_provider *pp, int r, int w, int e) 107 { 108 int error; 109 110 g_trace(G_T_ACCESS, "g_ctl_access(%s, %d, %d, %d)", 111 pp->name, r, w, e); 112 113 g_topology_assert(); 114 error = 0; 115 return (error); 116 } 117 118 static void 119 g_ctl_start(struct bio *bp) 120 { 121 struct g_ioctl *gio; 122 int error; 123 124 switch(bp->bio_cmd) { 125 case BIO_DELETE: 126 case BIO_READ: 127 case BIO_WRITE: 128 error = EOPNOTSUPP; 129 break; 130 case BIO_GETATTR: 131 case BIO_SETATTR: 132 if (strcmp(bp->bio_attribute, "GEOM::ioctl") || 133 bp->bio_length != sizeof *gio) { 134 error = EOPNOTSUPP; 135 break; 136 } 137 gio = (struct g_ioctl *)bp->bio_data; 138 gio->func = g_ctl_ioctl; 139 error = EDIRIOCTL; 140 break; 141 default: 142 error = EOPNOTSUPP; 143 break; 144 } 145 g_io_deliver(bp, error); 146 return; 147 } 148 149 /* 150 * All the stuff above is really just needed to get to the stuff below 151 */ 152 153 static int 154 g_ctl_ioctl_configgeom(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 155 { 156 struct geomconfiggeom *gcp; 157 struct g_configargs ga; 158 int error; 159 160 error = 0; 161 bzero(&ga, sizeof ga); 162 gcp = (struct geomconfiggeom *)data; 163 ga.class = g_idclass(&gcp->class); 164 if (ga.class == NULL) 165 return (EINVAL); 166 if (ga.class->config == NULL) 167 return (EOPNOTSUPP); 168 ga.geom = g_idgeom(&gcp->geom); 169 ga.provider = g_idprovider(&gcp->provider); 170 ga.len = gcp->len; 171 if (gcp->len > 64 * 1024) 172 return (EINVAL); 173 else if (gcp->len == 0) { 174 ga.ptr = NULL; 175 } else { 176 ga.ptr = g_malloc(gcp->len, M_WAITOK); 177 error = copyin(gcp->ptr, ga.ptr, gcp->len); 178 if (error) { 179 g_free(ga.ptr); 180 return (error); 181 } 182 } 183 ga.flag = gcp->flag; 184 error = ga.class->config(&ga); 185 if (gcp->len != 0) 186 copyout(ga.ptr, gcp->ptr, gcp->len); /* Ignore error */ 187 gcp->class.u.id = (uintptr_t)ga.class; 188 gcp->class.len = 0; 189 gcp->geom.u.id = (uintptr_t)ga.geom; 190 gcp->geom.len = 0; 191 gcp->provider.u.id = (uintptr_t)ga.provider; 192 gcp->provider.len = 0; 193 return(error); 194 } 195 196 static int 197 g_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 198 { 199 int error; 200 201 DROP_GIANT(); 202 g_topology_lock(); 203 switch(cmd) { 204 case GEOMCONFIGGEOM: 205 error = g_ctl_ioctl_configgeom(dev, cmd, data, fflag, td); 206 break; 207 default: 208 error = ENOTTY; 209 break; 210 } 211 g_topology_unlock(); 212 PICKUP_GIANT(); 213 return (error); 214 215 } 216