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_configargs; 65 66 typedef int g_config_t (struct g_configargs *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 *, const 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 config_geom functions. 87 */ 88 struct g_class { 89 const char *name; 90 g_taste_t *taste; 91 g_config_t *config; 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_stat contains the statistics we collect on consumers and 140 * providers. 141 */ 142 struct g_stat { 143 void *id; 144 uint64_t nop; 145 uint64_t nend; 146 struct bintime it; 147 struct bintime wentidle; 148 struct { 149 uint64_t nop; 150 uint64_t nbyte; 151 uint64_t nmem; 152 uint64_t nerr; 153 struct bintime dt; 154 } ops[5]; 155 #define G_STAT_IDX_READ 0 156 #define G_STAT_IDX_WRITE 2 157 #define G_STAT_IDX_DELETE 3 158 #define G_STAT_IDX_GETATTR 4 159 #define G_STAT_IDX_SETATTR 5 160 }; 161 162 /* 163 * A g_consumer is an attachment point for a g_provider. One g_consumer 164 * can only be attached to one g_provider, but multiple g_consumers 165 * can be attached to one g_provider. 166 */ 167 168 struct g_consumer { 169 u_int protect; 170 struct g_geom *geom; 171 LIST_ENTRY(g_consumer) consumer; 172 struct g_provider *provider; 173 LIST_ENTRY(g_consumer) consumers; /* XXX: better name */ 174 int acr, acw, ace; 175 struct g_event *event; 176 177 int biocount; 178 int spoiled; 179 struct g_stat stat; 180 }; 181 182 /* 183 * A g_provider is a "logical disk". 184 */ 185 struct g_provider { 186 u_int protect; 187 char *name; 188 LIST_ENTRY(g_provider) provider; 189 struct g_geom *geom; 190 LIST_HEAD(,g_consumer) consumers; 191 int acr, acw, ace; 192 int error; 193 struct g_event *event; 194 TAILQ_ENTRY(g_provider) orphan; 195 u_int index; 196 off_t mediasize; 197 u_int sectorsize; 198 struct g_stat stat; 199 }; 200 201 /* 202 * This gadget is used by userland to pinpoint a particular instance of 203 * something in the kernel. The name is unreadable on purpose, people 204 * should not encounter it directly but use library functions to deal 205 * with it. 206 * If len is zero, "id" contains a cast of the kernel pointer where the 207 * entity is located, (likely derived from the "id=" attribute in the 208 * XML config) and the g_id*() functions will validate this before allowing 209 * it to be used. 210 * If len is non-zero, it is the strlen() of the name which is pointed to 211 * by "name". 212 */ 213 struct geomidorname { 214 u_int len; 215 union { 216 const char *name; 217 uintptr_t id; 218 } u; 219 }; 220 221 /* geom_dev.c */ 222 int g_dev_print(void); 223 224 /* geom_dump.c */ 225 void g_hexdump(void *ptr, int length); 226 void g_trace(int level, const char *, ...); 227 # define G_T_TOPOLOGY 1 228 # define G_T_BIO 2 229 # define G_T_ACCESS 4 230 231 232 /* geom_event.c */ 233 typedef void g_call_me_t(void *); 234 int g_call_me(g_call_me_t *func, void *arg); 235 void g_orphan_provider(struct g_provider *pp, int error); 236 void g_waitidle(void); 237 238 /* geom_subr.c */ 239 int g_access_abs(struct g_consumer *cp, int nread, int nwrite, int nexcl); 240 int g_access_rel(struct g_consumer *cp, int nread, int nwrite, int nexcl); 241 void g_add_class(struct g_class *mp); 242 int g_attach(struct g_consumer *cp, struct g_provider *pp); 243 void g_destroy_consumer(struct g_consumer *cp); 244 void g_destroy_geom(struct g_geom *pp); 245 void g_destroy_provider(struct g_provider *pp); 246 void g_detach(struct g_consumer *cp); 247 void g_error_provider(struct g_provider *pp, int error); 248 int g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len); 249 #define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v)) 250 int g_handleattr(struct bio *bp, const char *attribute, void *val, int len); 251 int g_handleattr_int(struct bio *bp, const char *attribute, int val); 252 int g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val); 253 struct g_geom * g_insert_geom(const char *class, struct g_consumer *cp); 254 struct g_consumer * g_new_consumer(struct g_geom *gp); 255 struct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...); 256 struct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...); 257 void g_sanity(void *ptr); 258 void g_spoil(struct g_provider *pp, struct g_consumer *cp); 259 int g_std_access(struct g_provider *pp, int dr, int dw, int de); 260 void g_std_done(struct bio *bp); 261 void g_std_spoiled(struct g_consumer *cp); 262 struct g_class *g_idclass(struct geomidorname *); 263 struct g_geom *g_idgeom(struct geomidorname *); 264 struct g_provider *g_idprovider(struct geomidorname *); 265 266 267 /* geom_io.c */ 268 struct bio * g_clone_bio(struct bio *); 269 void g_destroy_bio(struct bio *); 270 void g_io_deliver(struct bio *bp, int error); 271 int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr); 272 void g_io_request(struct bio *bp, struct g_consumer *cp); 273 int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr); 274 struct bio *g_new_bio(void); 275 void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error); 276 int g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length); 277 278 /* geom_kern.c / geom_kernsim.c */ 279 280 #ifndef _SYS_CONF_H_ 281 typedef int d_ioctl_t(dev_t dev, u_long cmd, caddr_t data, 282 int fflag, struct thread *td); 283 #endif 284 285 struct g_ioctl { 286 u_long cmd; 287 void *data; 288 int fflag; 289 struct thread *td; 290 d_ioctl_t *func; 291 void *dev; 292 }; 293 294 #ifdef _KERNEL 295 296 struct g_kerneldump { 297 off_t offset; 298 off_t length; 299 }; 300 301 MALLOC_DECLARE(M_GEOM); 302 303 static __inline void * 304 g_malloc(int size, int flags) 305 { 306 void *p; 307 308 p = malloc(size, M_GEOM, flags); 309 g_sanity(p); 310 /* printf("malloc(%d, %x) -> %p\n", size, flags, p); */ 311 return (p); 312 } 313 314 static __inline void 315 g_free(void *ptr) 316 { 317 g_sanity(ptr); 318 /* printf("free(%p)\n", ptr); */ 319 free(ptr, M_GEOM); 320 } 321 322 extern struct sx topology_lock; 323 324 #define g_topology_lock() \ 325 do { \ 326 mtx_assert(&Giant, MA_NOTOWNED); \ 327 sx_xlock(&topology_lock); \ 328 } while (0) 329 330 #define g_topology_unlock() \ 331 do { \ 332 g_sanity(NULL); \ 333 sx_xunlock(&topology_lock); \ 334 } while (0) 335 336 #define g_topology_assert() \ 337 do { \ 338 g_sanity(NULL); \ 339 sx_assert(&topology_lock, SX_XLOCKED); \ 340 } while (0) 341 342 #define DECLARE_GEOM_CLASS_INIT(class, name, init) \ 343 SYSINIT(name, SI_SUB_DRIVERS, SI_ORDER_FIRST, init, NULL); 344 345 #define DECLARE_GEOM_CLASS(class, name) \ 346 static void \ 347 name##init(void) \ 348 { \ 349 mtx_unlock(&Giant); \ 350 g_add_class(&class); \ 351 mtx_lock(&Giant); \ 352 } \ 353 DECLARE_GEOM_CLASS_INIT(class, name, name##init); 354 355 #endif /* _KERNEL */ 356 357 /* 358 * IOCTLS for talking to the geom.ctl device. 359 */ 360 361 /* 362 * This is the structure used internally in the kernel, it is created and 363 * populated by geom_ctl.c. 364 */ 365 struct g_configargs { 366 /* Valid on call */ 367 struct g_class *class; 368 struct g_geom *geom; 369 struct g_provider *provider; 370 u_int flag; 371 u_int len; 372 void *ptr; 373 }; 374 375 /* 376 * This is the structure used to communicate with userland. 377 */ 378 struct geomconfiggeom { 379 /* Valid on call */ 380 struct geomidorname class; 381 struct geomidorname geom; 382 struct geomidorname provider; 383 u_int flag; 384 u_int len; 385 void *ptr; 386 }; 387 388 #define GEOMCONFIGGEOM _IOW('G', 0, struct geomconfiggeom) 389 390 #define GCFG_GENERIC0 0x00000000 391 /* 392 * Generic requests suitable for all classes. 393 */ 394 #define GCFG_CLASS0 0x10000000 395 /* 396 * Class specific verbs. Allocations in this part of the numberspace 397 * can only be done after review and approval of phk@FreeBSD.org. 398 * All allocations in this space will be listed in this file. 399 */ 400 #define GCFG_PRIVATE0 0x20000000 401 /* 402 * Lowest allocation for private flag definitions. 403 * If you define you own private "verbs", please express them in 404 * your code as (GCFG_PRIVATE0 + somenumber), where somenumber is 405 * a magic number in the range [0x0 ... 0xfffffff] chosen the way 406 * magic numbers are chosen. Such allocation SHALL NOT be listed 407 * here but SHOULD be listed in some suitable .h file. 408 */ 409 #define GCFG_RESERVED0 0x30000000 410 #define GCFG_RESERVEDN 0xffffffff 411 /* 412 * This area is reserved for the future. 413 */ 414 415 #define GCFG_CREATE (GCFG_GENERIC0 + 0x0) 416 /* 417 * Request geom construction. 418 * ptr/len is class-specific. 419 */ 420 #define GCFG_DISMANTLE (GCFG_GENERIC0 + 0x1) 421 /* 422 * Request orderly geom dismantling. 423 * ptr/len is class-specific. 424 */ 425 426 427 struct gcfg_magicrw { 428 off_t offset; 429 u_int len; 430 }; 431 432 #define GCFG_MAGICREAD (GCFG_GENERIC0 + 0x100) 433 /* 434 * Read of magic spaces. 435 * ptr/len is gcfgmagicrw structure followed by bufferspace 436 * for data to be read. 437 */ 438 #define GCFG_MAGICWRITE (GCFG_GENERIC0 + 0x101) 439 /* 440 * Write of magic spaces. 441 * as above, only the other way. 442 */ 443 444 445 /* geom_enc.c */ 446 uint16_t g_dec_be2(const u_char *p); 447 uint32_t g_dec_be4(const u_char *p); 448 uint16_t g_dec_le2(const u_char *p); 449 uint32_t g_dec_le4(const u_char *p); 450 uint64_t g_dec_le8(const u_char *p); 451 void g_enc_le2(u_char *p, uint16_t u); 452 void g_enc_le4(u_char *p, uint32_t u); 453 void g_enc_le8(u_char *p, uint64_t u); 454 455 #endif /* _GEOM_GEOM_H_ */ 456