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 #ifndef _GEOM_GEOM_H_ 39 #define _GEOM_GEOM_H_ 40 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/sx.h> 44 #include <sys/queue.h> 45 #include <sys/ioccom.h> 46 #include <sys/sbuf.h> 47 48 #ifdef KERNELSIM 49 /* 50 * The GEOM subsystem makes a few concessions in order to be able to run as a 51 * user-land simulation as well as a kernel component. 52 */ 53 #include <geom_sim.h> 54 #endif 55 56 struct g_class; 57 struct g_geom; 58 struct g_consumer; 59 struct g_provider; 60 struct g_event; 61 struct thread; 62 struct bio; 63 struct sbuf; 64 struct g_createargs; 65 66 typedef int g_create_geom_t (struct g_createargs *ca); 67 typedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *, 68 int flags); 69 #define G_TF_NORMAL 0 70 #define G_TF_INSIST 1 71 #define G_TF_TRANSPARENT 2 72 typedef int g_access_t (struct g_provider *, int, int, int); 73 /* XXX: not sure about the thread arg */ 74 typedef void g_orphan_t (struct g_consumer *); 75 76 typedef void g_start_t (struct bio *); 77 typedef void g_spoiled_t (struct g_consumer *); 78 typedef void g_dumpconf_t (struct sbuf *, char *indent, struct g_geom *, 79 struct g_consumer *, struct g_provider *); 80 81 /* 82 * The g_class structure describes a transformation class. In other words 83 * all BSD disklabel handlers share one g_class, all MBR handlers share 84 * one common g_class and so on. 85 * Certain operations are instantiated on the class, most notably the 86 * taste and create_geom functions. 87 */ 88 struct g_class { 89 char *name; 90 g_taste_t *taste; 91 g_create_geom_t *create_geom; 92 /* 93 * The remaning elements are private and classes should use 94 * the G_CLASS_INITIALIZER macro to initialize them. 95 */ 96 LIST_ENTRY(g_class) class; 97 LIST_HEAD(,g_geom) geom; 98 struct g_event *event; 99 u_int protect; 100 }; 101 102 #define G_CLASS_INITIALIZER { 0, 0 }, { 0 }, 0, 0 103 104 /* 105 * The g_geom is an instance of a g_class. 106 */ 107 struct g_geom { 108 u_int protect; 109 char *name; 110 struct g_class *class; 111 LIST_ENTRY(g_geom) geom; 112 LIST_HEAD(,g_consumer) consumer; 113 LIST_HEAD(,g_provider) provider; 114 TAILQ_ENTRY(g_geom) geoms; /* XXX: better name */ 115 int rank; 116 g_start_t *start; 117 g_spoiled_t *spoiled; 118 g_dumpconf_t *dumpconf; 119 g_access_t *access; 120 g_orphan_t *orphan; 121 void *softc; 122 struct g_event *event; 123 unsigned flags; 124 #define G_GEOM_WITHER 1 125 }; 126 127 /* 128 * The g_bioq is a queue of struct bio's. 129 * XXX: possibly collection point for statistics. 130 * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head. 131 */ 132 struct g_bioq { 133 TAILQ_HEAD(, bio) bio_queue; 134 struct mtx bio_queue_lock; 135 int bio_queue_length; 136 }; 137 138 /* 139 * A g_consumer is an attachment point for a g_provider. One g_consumer 140 * can only be attached to one g_provider, but multiple g_consumers 141 * can be attached to one g_provider. 142 */ 143 144 struct g_consumer { 145 u_int protect; 146 struct g_geom *geom; 147 LIST_ENTRY(g_consumer) consumer; 148 struct g_provider *provider; 149 LIST_ENTRY(g_consumer) consumers; /* XXX: better name */ 150 int acr, acw, ace; 151 struct g_event *event; 152 153 int biocount; 154 int spoiled; 155 }; 156 157 /* 158 * A g_provider is a "logical disk". 159 */ 160 struct g_provider { 161 u_int protect; 162 char *name; 163 LIST_ENTRY(g_provider) provider; 164 struct g_geom *geom; 165 LIST_HEAD(,g_consumer) consumers; 166 int acr, acw, ace; 167 int error; 168 struct g_event *event; 169 TAILQ_ENTRY(g_provider) orphan; 170 int index; 171 off_t mediasize; 172 u_int sectorsize; 173 }; 174 175 /* 176 * This gadget is used by userland to pinpoint a particular instance of 177 * something in the kernel. The name is unreadable on purpose, people 178 * should not encounter it directly but use library functions to deal 179 * with it. 180 * If len is zero, "id" contains a cast of the kernel pointer where the 181 * entity is located, (likely derived from the "id=" attribute in the 182 * XML config) and the g_id*() functions will validate this before allowing 183 * it to be used. 184 * If len is non-zero, it is the strlen() of the name which is pointed to 185 * by "name". 186 */ 187 struct geomidorname { 188 u_int len; 189 union { 190 const char *name; 191 uintptr_t id; 192 } u; 193 }; 194 195 /* geom_dump.c */ 196 void g_hexdump(void *ptr, int length); 197 void g_trace(int level, char *, ...); 198 # define G_T_TOPOLOGY 1 199 # define G_T_BIO 2 200 # define G_T_ACCESS 4 201 202 203 /* geom_event.c */ 204 typedef void g_call_me_t(void *); 205 int g_call_me(g_call_me_t *func, void *arg); 206 void g_orphan_provider(struct g_provider *pp, int error); 207 void g_silence(void); 208 void g_waitidle(void); 209 210 /* geom_subr.c */ 211 int g_access_abs(struct g_consumer *cp, int read, int write, int exclusive); 212 int g_access_rel(struct g_consumer *cp, int read, int write, int exclusive); 213 void g_add_class(struct g_class *mp); 214 int g_attach(struct g_consumer *cp, struct g_provider *pp); 215 void g_destroy_consumer(struct g_consumer *cp); 216 void g_destroy_geom(struct g_geom *pp); 217 void g_destroy_provider(struct g_provider *pp); 218 void g_detach(struct g_consumer *cp); 219 void g_error_provider(struct g_provider *pp, int error); 220 int g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len); 221 #define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v)) 222 int g_handleattr(struct bio *bp, char *attribute, void *val, int len); 223 int g_handleattr_int(struct bio *bp, char *attribute, int val); 224 int g_handleattr_off_t(struct bio *bp, char *attribute, off_t val); 225 struct g_geom * g_insert_geom(char *class, struct g_consumer *cp); 226 struct g_consumer * g_new_consumer(struct g_geom *gp); 227 struct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...); 228 struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...); 229 void g_sanity(void *ptr); 230 void g_spoil(struct g_provider *pp, struct g_consumer *cp); 231 int g_std_access(struct g_provider *pp, int dr, int dw, int de); 232 void g_std_done(struct bio *bp); 233 void g_std_spoiled(struct g_consumer *cp); 234 struct g_class *g_idclass(struct geomidorname *); 235 struct g_geom *g_idgeom(struct geomidorname *); 236 struct g_provider *g_idprovider(struct geomidorname *); 237 238 239 /* geom_io.c */ 240 struct bio * g_clone_bio(struct bio *); 241 void g_destroy_bio(struct bio *); 242 void g_io_deliver(struct bio *bp, int error); 243 int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr); 244 void g_io_request(struct bio *bp, struct g_consumer *cp); 245 int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr); 246 struct bio *g_new_bio(void); 247 void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error); 248 int g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length); 249 250 /* geom_kern.c / geom_kernsim.c */ 251 252 #ifndef _SYS_CONF_H_ 253 typedef int d_ioctl_t(dev_t dev, u_long cmd, caddr_t data, 254 int fflag, struct thread *td); 255 #endif 256 257 struct g_ioctl { 258 u_long cmd; 259 void *data; 260 int fflag; 261 struct thread *td; 262 d_ioctl_t *func; 263 dev_t dev; 264 }; 265 266 #ifdef _KERNEL 267 268 struct g_kerneldump { 269 off_t offset; 270 off_t length; 271 }; 272 273 MALLOC_DECLARE(M_GEOM); 274 275 static __inline void * 276 g_malloc(int size, int flags) 277 { 278 void *p; 279 280 p = malloc(size, M_GEOM, flags); 281 g_sanity(p); 282 /* printf("malloc(%d, %x) -> %p\n", size, flags, p); */ 283 return (p); 284 } 285 286 static __inline void 287 g_free(void *ptr) 288 { 289 g_sanity(ptr); 290 /* printf("free(%p)\n", ptr); */ 291 free(ptr, M_GEOM); 292 } 293 294 extern struct sx topology_lock; 295 296 #define g_topology_lock() \ 297 do { \ 298 mtx_assert(&Giant, MA_NOTOWNED); \ 299 sx_xlock(&topology_lock); \ 300 } while (0) 301 302 #define g_topology_unlock() \ 303 do { \ 304 g_sanity(NULL); \ 305 sx_xunlock(&topology_lock); \ 306 } while (0) 307 308 #define g_topology_assert() \ 309 do { \ 310 g_sanity(NULL); \ 311 sx_assert(&topology_lock, SX_XLOCKED); \ 312 } while (0) 313 314 #define DECLARE_GEOM_CLASS_INIT(class, name, init) \ 315 SYSINIT(name, SI_SUB_DRIVERS, SI_ORDER_FIRST, init, NULL); 316 317 #define DECLARE_GEOM_CLASS(class, name) \ 318 static void \ 319 name##init(void) \ 320 { \ 321 mtx_unlock(&Giant); \ 322 g_add_class(&class); \ 323 mtx_lock(&Giant); \ 324 } \ 325 DECLARE_GEOM_CLASS_INIT(class, name, name##init); 326 327 #endif /* _KERNEL */ 328 329 /* 330 * IOCTLS for talking to the geom.ctl device. 331 */ 332 333 struct geomgetconf { 334 char *ptr; 335 u_int len; 336 }; 337 #define GEOMGETCONF _IOW('G', 0, struct geomgetconf) 338 339 struct g_createargs { 340 /* Valid on call */ 341 struct g_class *class; 342 struct g_geom *geom; 343 struct g_provider *provider; 344 u_int flag; 345 u_int len; 346 void *ptr; 347 }; 348 349 struct geomconfiggeom { 350 /* Valid on call */ 351 struct geomidorname class; 352 struct geomidorname geom; 353 struct geomidorname provider; 354 u_int flag; 355 u_int len; 356 void *ptr; 357 /* Valid on return */ 358 }; 359 #define GEOMCONFIGGEOM _IOW('G', 0, struct geomconfiggeom) 360 361 /* geom_enc.c */ 362 uint16_t g_dec_be2(const u_char *p); 363 uint32_t g_dec_be4(const u_char *p); 364 uint16_t g_dec_le2(const u_char *p); 365 uint32_t g_dec_le4(const u_char *p); 366 uint64_t g_dec_le8(const u_char *p); 367 void g_enc_le2(u_char *p, uint16_t u); 368 void g_enc_le4(u_char *p, uint32_t u); 369 void g_enc_le8(u_char *p, uint64_t u); 370 371 #endif /* _GEOM_GEOM_H_ */ 372