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_magicspaces; 65 66 67 typedef struct g_geom * g_create_geom_t (struct g_class *mp, 68 struct g_provider *pp, char *name); 69 typedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *, 70 int flags); 71 #define G_TF_NORMAL 0 72 #define G_TF_INSIST 1 73 #define G_TF_TRANSPARENT 2 74 typedef int g_access_t (struct g_provider *, int, int, int); 75 /* XXX: not sure about the thread arg */ 76 typedef void g_orphan_t (struct g_consumer *); 77 78 typedef void g_start_t (struct bio *); 79 typedef void g_spoiled_t (struct g_consumer *); 80 typedef void g_dumpconf_t (struct sbuf *, char *indent, struct g_geom *, 81 struct g_consumer *, struct g_provider *); 82 83 /* 84 * The g_class structure describes a transformation class. In other words 85 * all BSD disklabel handlers share one g_class, all MBR handlers share 86 * one common g_class and so on. 87 * Certain operations are instantiated on the class, most notably the 88 * taste and create_geom functions. 89 */ 90 struct g_class { 91 char *name; 92 g_taste_t *taste; 93 g_create_geom_t *create_geom; 94 /* 95 * The remaning elements are private and classes should use 96 * the G_CLASS_INITIALIZER macro to initialize them. 97 */ 98 LIST_ENTRY(g_class) class; 99 LIST_HEAD(,g_geom) geom; 100 struct g_event *event; 101 u_int protect; 102 }; 103 104 #define G_CLASS_INITIALIZER { 0, 0 }, { 0 }, 0, 0 105 106 /* 107 * The g_geom is an instance of a g_class. 108 */ 109 struct g_geom { 110 u_int protect; 111 char *name; 112 struct g_class *class; 113 LIST_ENTRY(g_geom) geom; 114 LIST_HEAD(,g_consumer) consumer; 115 LIST_HEAD(,g_provider) provider; 116 TAILQ_ENTRY(g_geom) geoms; /* XXX: better name */ 117 int rank; 118 g_start_t *start; 119 g_spoiled_t *spoiled; 120 g_dumpconf_t *dumpconf; 121 g_access_t *access; 122 g_orphan_t *orphan; 123 void *softc; 124 struct g_event *event; 125 unsigned flags; 126 struct g_magicspaces *magicspaces; 127 #define G_GEOM_WITHER 1 128 }; 129 130 /* 131 * The g_bioq is a queue of struct bio's. 132 * XXX: possibly collection point for statistics. 133 * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head. 134 */ 135 struct g_bioq { 136 TAILQ_HEAD(, bio) bio_queue; 137 struct mtx bio_queue_lock; 138 int bio_queue_length; 139 }; 140 141 /* 142 * A g_consumer is an attachment point for a g_provider. One g_consumer 143 * can only be attached to one g_provider, but multiple g_consumers 144 * can be attached to one g_provider. 145 */ 146 147 struct g_consumer { 148 u_int protect; 149 struct g_geom *geom; 150 LIST_ENTRY(g_consumer) consumer; 151 struct g_provider *provider; 152 LIST_ENTRY(g_consumer) consumers; /* XXX: better name */ 153 int acr, acw, ace; 154 struct g_event *event; 155 156 int biocount; 157 int spoiled; 158 }; 159 160 /* 161 * A g_provider is a "logical disk". 162 */ 163 struct g_provider { 164 u_int protect; 165 char *name; 166 LIST_ENTRY(g_provider) provider; 167 struct g_geom *geom; 168 LIST_HEAD(,g_consumer) consumers; 169 int acr, acw, ace; 170 int error; 171 struct g_event *event; 172 TAILQ_ENTRY(g_provider) orphan; 173 int index; 174 off_t mediasize; 175 }; 176 177 /* 178 * Some methods may implement various "magic spaces", this is reserved 179 * or magic areas on the disk, set a side for various and sundry purposes. 180 * A good example is the BSD disklabel and boot code on i386 which occupies 181 * a total of four magic spaces: boot1, the disklabel, the padding behind 182 * the disklabel and boot2. The reason we don't simply tell people to 183 * write the appropriate stuff on the underlying device is that (some of) 184 * the magic spaces might be real-time modifiable. It is for instance 185 * possible to change a disklabel while partitions are open, provided 186 * the open partitions do not get trampled in the process. 187 */ 188 189 struct g_magicspace { 190 char name[8]; 191 off_t offset; 192 u_int len; 193 u_int flags; 194 }; 195 196 struct g_magicspaces { 197 uintptr_t geom_id; 198 char class[8]; 199 uint nmagic; 200 uint nspace; 201 struct g_magicspace *magicspace; 202 }; 203 204 /* geom_dump.c */ 205 void g_hexdump(void *ptr, int length); 206 void g_trace(int level, char *, ...); 207 # define G_T_TOPOLOGY 1 208 # define G_T_BIO 2 209 # define G_T_ACCESS 4 210 211 /* geom_enc.c */ 212 uint32_t g_dec_be2(u_char *p); 213 uint32_t g_dec_be4(u_char *p); 214 uint32_t g_dec_le2(u_char *p); 215 uint32_t g_dec_le4(u_char *p); 216 void g_enc_le4(u_char *p, uint32_t u); 217 218 /* geom_event.c */ 219 void g_orphan_provider(struct g_provider *pp, int error); 220 void g_waitidle(void); 221 void g_silence(void); 222 223 /* geom_subr.c */ 224 int g_access_abs(struct g_consumer *cp, int read, int write, int exclusive); 225 int g_access_rel(struct g_consumer *cp, int read, int write, int exclusive); 226 void g_add_class(struct g_class *mp); 227 int g_add_magicspace(struct g_geom *gp, const char *name, off_t start, u_int len, u_int flags); 228 int g_attach(struct g_consumer *cp, struct g_provider *pp); 229 struct g_geom *g_create_geomf(char *class, struct g_provider *, char *fmt, ...); 230 void g_destroy_consumer(struct g_consumer *cp); 231 void g_destroy_geom(struct g_geom *pp); 232 void g_destroy_provider(struct g_provider *pp); 233 void g_detach(struct g_consumer *cp); 234 void g_error_provider(struct g_provider *pp, int error); 235 int g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len); 236 #define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v)) 237 int g_handleattr(struct bio *bp, char *attribute, void *val, int len); 238 int g_handleattr_int(struct bio *bp, char *attribute, int val); 239 int g_handleattr_off_t(struct bio *bp, char *attribute, off_t val); 240 struct g_geom * g_insert_geom(char *class, struct g_consumer *cp); 241 struct g_consumer * g_new_consumer(struct g_geom *gp); 242 struct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...); 243 struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...); 244 void g_sanity(void *ptr); 245 void g_spoil(struct g_provider *pp, struct g_consumer *cp); 246 int g_std_access(struct g_provider *pp, int dr, int dw, int de); 247 void g_std_done(struct bio *bp); 248 void g_std_spoiled(struct g_consumer *cp); 249 250 /* geom_io.c */ 251 struct bio * g_clone_bio(struct bio *); 252 void g_destroy_bio(struct bio *); 253 void g_io_deliver(struct bio *bp); 254 void g_io_fail(struct bio *bp, int error); 255 int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr); 256 void g_io_request(struct bio *bp, struct g_consumer *cp); 257 int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr); 258 struct bio *g_new_bio(void); 259 void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error); 260 261 /* geom_kern.c / geom_kernsim.c */ 262 263 struct g_ioctl { 264 u_long cmd; 265 void *data; 266 int fflag; 267 struct thread *td; 268 }; 269 270 #ifdef _KERNEL 271 272 struct g_kerneldump { 273 off_t offset; 274 off_t length; 275 }; 276 277 MALLOC_DECLARE(M_GEOM); 278 279 static __inline void * 280 g_malloc(int size, int flags) 281 { 282 void *p; 283 284 p = malloc(size, M_GEOM, flags); 285 g_sanity(p); 286 /* printf("malloc(%d, %x) -> %p\n", size, flags, p); */ 287 return (p); 288 } 289 290 static __inline void 291 g_free(void *ptr) 292 { 293 g_sanity(ptr); 294 /* printf("free(%p)\n", ptr); */ 295 free(ptr, M_GEOM); 296 } 297 298 extern struct sx topology_lock; 299 300 #define g_topology_lock() \ 301 do { \ 302 mtx_assert(&Giant, MA_NOTOWNED); \ 303 sx_xlock(&topology_lock); \ 304 } while (0) 305 306 #define g_topology_unlock() \ 307 do { \ 308 g_sanity(NULL); \ 309 sx_xunlock(&topology_lock); \ 310 } while (0) 311 312 #define g_topology_assert() \ 313 do { \ 314 g_sanity(NULL); \ 315 sx_assert(&topology_lock, SX_XLOCKED); \ 316 } while (0) 317 318 #define DECLARE_GEOM_CLASS(class, name) \ 319 static void \ 320 name##init(void) \ 321 { \ 322 mtx_unlock(&Giant); \ 323 g_add_class(&class); \ 324 mtx_lock(&Giant); \ 325 } \ 326 SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL); 327 328 #endif /* _KERNEL */ 329 330 #define GEOMGETCONF _IOWR('G', 0, struct sbuf) 331 332 #endif /* _GEOM_GEOM_H_ */ 333