geom.h revision 105124
192108Sphk/*- 292108Sphk * Copyright (c) 2002 Poul-Henning Kamp 392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc. 492108Sphk * All rights reserved. 592108Sphk * 692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp 792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc. 892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 992108Sphk * DARPA CHATS research program. 1092108Sphk * 1192108Sphk * Redistribution and use in source and binary forms, with or without 1292108Sphk * modification, are permitted provided that the following conditions 1392108Sphk * are met: 1492108Sphk * 1. Redistributions of source code must retain the above copyright 1592108Sphk * notice, this list of conditions and the following disclaimer. 1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright 1792108Sphk * notice, this list of conditions and the following disclaimer in the 1892108Sphk * documentation and/or other materials provided with the distribution. 1992108Sphk * 3. The names of the authors may not be used to endorse or promote 2092108Sphk * products derived from this software without specific prior written 2192108Sphk * permission. 2292108Sphk * 2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2692108Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3392108Sphk * SUCH DAMAGE. 3492108Sphk * 3592108Sphk * $FreeBSD: head/sys/geom/geom.h 105124 2002-10-14 20:20:17Z jake $ 3692108Sphk */ 3792108Sphk 3895276Sphk#ifndef _GEOM_GEOM_H_ 3995276Sphk#define _GEOM_GEOM_H_ 4095276Sphk 4192108Sphk#include <sys/lock.h> 4292108Sphk#include <sys/mutex.h> 4392108Sphk#include <sys/sx.h> 4492108Sphk#include <sys/queue.h> 4595323Sphk#include <sys/ioccom.h> 4695323Sphk#include <sys/sbuf.h> 4792108Sphk 4895362Sphk#ifdef KERNELSIM 4992108Sphk/* 5092108Sphk * The GEOM subsystem makes a few concessions in order to be able to run as a 5192108Sphk * user-land simulation as well as a kernel component. 5292108Sphk */ 5392514Sphk#include <geom_sim.h> 5492108Sphk#endif 5592108Sphk 5693248Sphkstruct g_class; 5792108Sphkstruct g_geom; 5892108Sphkstruct g_consumer; 5992108Sphkstruct g_provider; 6092108Sphkstruct g_event; 6192108Sphkstruct thread; 6292108Sphkstruct bio; 6392108Sphkstruct sbuf; 64105092Sphkstruct g_createargs; 6592108Sphk 66105092Sphktypedef int g_create_geom_t (struct g_createargs *ca); 6793248Sphktypedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *, 6893250Sphk int flags); 6992108Sphk#define G_TF_NORMAL 0 7092108Sphk#define G_TF_INSIST 1 7192108Sphk#define G_TF_TRANSPARENT 2 7292108Sphktypedef int g_access_t (struct g_provider *, int, int, int); 7392108Sphk/* XXX: not sure about the thread arg */ 7493250Sphktypedef void g_orphan_t (struct g_consumer *); 7592108Sphk 7692108Sphktypedef void g_start_t (struct bio *); 7792108Sphktypedef void g_spoiled_t (struct g_consumer *); 7892108Sphktypedef void g_dumpconf_t (struct sbuf *, char *indent, struct g_geom *, 7992108Sphk struct g_consumer *, struct g_provider *); 8092108Sphk 8192108Sphk/* 8293248Sphk * The g_class structure describes a transformation class. In other words 8393248Sphk * all BSD disklabel handlers share one g_class, all MBR handlers share 8493248Sphk * one common g_class and so on. 8593248Sphk * Certain operations are instantiated on the class, most notably the 8692108Sphk * taste and create_geom functions. 8792108Sphk */ 8893248Sphkstruct g_class { 8992108Sphk char *name; 9092108Sphk g_taste_t *taste; 9192108Sphk g_create_geom_t *create_geom; 9293776Sphk /* 9393776Sphk * The remaning elements are private and classes should use 9498066Sphk * the G_CLASS_INITIALIZER macro to initialize them. 9593776Sphk */ 9693248Sphk LIST_ENTRY(g_class) class; 9792108Sphk LIST_HEAD(,g_geom) geom; 9892108Sphk struct g_event *event; 9995310Sphk u_int protect; 10092108Sphk}; 10192108Sphk 10298066Sphk#define G_CLASS_INITIALIZER { 0, 0 }, { 0 }, 0, 0 10393776Sphk 10492108Sphk/* 10593248Sphk * The g_geom is an instance of a g_class. 10692108Sphk */ 10792108Sphkstruct g_geom { 10895310Sphk u_int protect; 10992108Sphk char *name; 11093248Sphk struct g_class *class; 11192108Sphk LIST_ENTRY(g_geom) geom; 11292108Sphk LIST_HEAD(,g_consumer) consumer; 11392108Sphk LIST_HEAD(,g_provider) provider; 11492108Sphk TAILQ_ENTRY(g_geom) geoms; /* XXX: better name */ 11592108Sphk int rank; 11692108Sphk g_start_t *start; 11792108Sphk g_spoiled_t *spoiled; 11892108Sphk g_dumpconf_t *dumpconf; 11993776Sphk g_access_t *access; 12093776Sphk g_orphan_t *orphan; 12192108Sphk void *softc; 12292108Sphk struct g_event *event; 12392108Sphk unsigned flags; 12492108Sphk#define G_GEOM_WITHER 1 12592108Sphk}; 12692108Sphk 12792108Sphk/* 12892108Sphk * The g_bioq is a queue of struct bio's. 12992108Sphk * XXX: possibly collection point for statistics. 13092108Sphk * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head. 13192108Sphk */ 13292108Sphkstruct g_bioq { 13392108Sphk TAILQ_HEAD(, bio) bio_queue; 13492108Sphk struct mtx bio_queue_lock; 13592108Sphk int bio_queue_length; 13692108Sphk}; 13792108Sphk 13892108Sphk/* 13992108Sphk * A g_consumer is an attachment point for a g_provider. One g_consumer 14092108Sphk * can only be attached to one g_provider, but multiple g_consumers 14192108Sphk * can be attached to one g_provider. 14292108Sphk */ 14392108Sphk 14492108Sphkstruct g_consumer { 14595310Sphk u_int protect; 14692108Sphk struct g_geom *geom; 14792108Sphk LIST_ENTRY(g_consumer) consumer; 14892108Sphk struct g_provider *provider; 14992108Sphk LIST_ENTRY(g_consumer) consumers; /* XXX: better name */ 15092108Sphk int acr, acw, ace; 15192108Sphk struct g_event *event; 15292108Sphk 15392108Sphk int biocount; 15492108Sphk int spoiled; 15592108Sphk}; 15692108Sphk 15792108Sphk/* 15892108Sphk * A g_provider is a "logical disk". 15992108Sphk */ 16092108Sphkstruct g_provider { 16195310Sphk u_int protect; 16292108Sphk char *name; 16392108Sphk LIST_ENTRY(g_provider) provider; 16492108Sphk struct g_geom *geom; 16592108Sphk LIST_HEAD(,g_consumer) consumers; 16692108Sphk int acr, acw, ace; 16792108Sphk int error; 16892108Sphk struct g_event *event; 16992108Sphk TAILQ_ENTRY(g_provider) orphan; 17092108Sphk int index; 17193778Sphk off_t mediasize; 17292108Sphk}; 17392108Sphk 174105092Sphk/* 175105092Sphk * This gadget is used by userland to pinpoint a particular instance of 176105092Sphk * something in the kernel. The name is unreadable on purpose, people 177105092Sphk * should not encounter it directly but use library functions to deal 178105092Sphk * with it. 179105092Sphk * If len is zero, "id" contains a cast of the kernel pointer where the 180105092Sphk * entity is located, (likely derived from the "id=" attribute in the 181105092Sphk * XML config) and the g_id*() functions will validate this before allowing 182105092Sphk * it to be used. 183105092Sphk * If len is non-zero, it is the strlen() of the name which is pointed to 184105092Sphk * by "name". 185105092Sphk */ 186105092Sphkstruct geomidorname { 187105092Sphk u_int len; 188105092Sphk union { 189105092Sphk const char *name; 190105092Sphk uintptr_t id; 191105092Sphk } u; 192105092Sphk}; 193105092Sphk 19492108Sphk/* geom_dump.c */ 19592108Sphkvoid g_hexdump(void *ptr, int length); 19692108Sphkvoid g_trace(int level, char *, ...); 19792108Sphk# define G_T_TOPOLOGY 1 19892108Sphk# define G_T_BIO 2 19992108Sphk# define G_T_ACCESS 4 20092108Sphk 20193090Sphk 20292108Sphk/* geom_event.c */ 203104056Sphktypedef void g_call_me_t(void *); 204104056Sphkint g_call_me(g_call_me_t *func, void *arg); 20592108Sphkvoid g_orphan_provider(struct g_provider *pp, int error); 206104056Sphkvoid g_silence(void); 20798066Sphkvoid g_waitidle(void); 20892108Sphk 20992108Sphk/* geom_subr.c */ 21092108Sphkint g_access_abs(struct g_consumer *cp, int read, int write, int exclusive); 21192108Sphkint g_access_rel(struct g_consumer *cp, int read, int write, int exclusive); 21293248Sphkvoid g_add_class(struct g_class *mp); 21392108Sphkint g_attach(struct g_consumer *cp, struct g_provider *pp); 21492108Sphkvoid g_destroy_consumer(struct g_consumer *cp); 21592108Sphkvoid g_destroy_geom(struct g_geom *pp); 21692108Sphkvoid g_destroy_provider(struct g_provider *pp); 21798066Sphkvoid g_detach(struct g_consumer *cp); 21892108Sphkvoid g_error_provider(struct g_provider *pp, int error); 21994284Sphkint g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len); 22094284Sphk#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v)) 22198066Sphkint g_handleattr(struct bio *bp, char *attribute, void *val, int len); 22298066Sphkint g_handleattr_int(struct bio *bp, char *attribute, int val); 22398066Sphkint g_handleattr_off_t(struct bio *bp, char *attribute, off_t val); 22493248Sphkstruct g_geom * g_insert_geom(char *class, struct g_consumer *cp); 22592108Sphkstruct g_consumer * g_new_consumer(struct g_geom *gp); 22693248Sphkstruct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...); 22792108Sphkstruct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...); 22895310Sphkvoid g_sanity(void *ptr); 22992108Sphkvoid g_spoil(struct g_provider *pp, struct g_consumer *cp); 23092108Sphkint g_std_access(struct g_provider *pp, int dr, int dw, int de); 23192108Sphkvoid g_std_done(struct bio *bp); 23292108Sphkvoid g_std_spoiled(struct g_consumer *cp); 233105092Sphkstruct g_class *g_idclass(struct geomidorname *); 234105092Sphkstruct g_geom *g_idgeom(struct geomidorname *); 235105092Sphkstruct g_provider *g_idprovider(struct geomidorname *); 23692108Sphk 237105092Sphk 23892108Sphk/* geom_io.c */ 23992108Sphkstruct bio * g_clone_bio(struct bio *); 24092108Sphkvoid g_destroy_bio(struct bio *); 241104195Sphkvoid g_io_deliver(struct bio *bp, int error); 24294283Sphkint g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr); 24392108Sphkvoid g_io_request(struct bio *bp, struct g_consumer *cp); 24494283Sphkint g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr); 24592108Sphkstruct bio *g_new_bio(void); 24692108Sphkvoid * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error); 247104194Sphkint g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length); 24892108Sphk 24992108Sphk/* geom_kern.c / geom_kernsim.c */ 25092108Sphk 251104602Sphk#ifndef _SYS_CONF_H_ 252104602Sphktypedef int d_ioctl_t(dev_t dev, u_long cmd, caddr_t data, 253104602Sphk int fflag, struct thread *td); 254104602Sphk#endif 255104602Sphk 25692403Sphkstruct g_ioctl { 25792403Sphk u_long cmd; 25892403Sphk void *data; 25992403Sphk int fflag; 26092403Sphk struct thread *td; 261104602Sphk d_ioctl_t *func; 262104602Sphk dev_t dev; 26392403Sphk}; 26492108Sphk 26592108Sphk#ifdef _KERNEL 26692108Sphk 26795038Sphkstruct g_kerneldump { 26895038Sphk off_t offset; 26995038Sphk off_t length; 27095038Sphk}; 27195038Sphk 27292108SphkMALLOC_DECLARE(M_GEOM); 27392108Sphk 27492108Sphkstatic __inline void * 27592108Sphkg_malloc(int size, int flags) 27692108Sphk{ 27792108Sphk void *p; 27892108Sphk 27992108Sphk p = malloc(size, M_GEOM, flags); 28095310Sphk g_sanity(p); 28195310Sphk /* printf("malloc(%d, %x) -> %p\n", size, flags, p); */ 28292108Sphk return (p); 28392108Sphk} 28492108Sphk 28592108Sphkstatic __inline void 28692108Sphkg_free(void *ptr) 28792108Sphk{ 28895310Sphk g_sanity(ptr); 28995310Sphk /* printf("free(%p)\n", ptr); */ 29092108Sphk free(ptr, M_GEOM); 29192108Sphk} 29292108Sphk 29392108Sphkextern struct sx topology_lock; 29492108Sphk 29596987Sphk#define g_topology_lock() \ 29696987Sphk do { \ 29796987Sphk mtx_assert(&Giant, MA_NOTOWNED); \ 29896987Sphk sx_xlock(&topology_lock); \ 29996987Sphk } while (0) 30096987Sphk 30196987Sphk#define g_topology_unlock() \ 30296987Sphk do { \ 30396987Sphk g_sanity(NULL); \ 30496987Sphk sx_xunlock(&topology_lock); \ 30596987Sphk } while (0) 30696987Sphk 30796987Sphk#define g_topology_assert() \ 30896987Sphk do { \ 30996987Sphk g_sanity(NULL); \ 31096987Sphk sx_assert(&topology_lock, SX_XLOCKED); \ 31196987Sphk } while (0) 31296987Sphk 313105061Sphk#define DECLARE_GEOM_CLASS_INIT(class, name, init) \ 314105124Sjake SYSINIT(name, SI_SUB_DRIVERS, SI_ORDER_FIRST, init, NULL); 315105061Sphk 31693248Sphk#define DECLARE_GEOM_CLASS(class, name) \ 31792108Sphk static void \ 31892108Sphk name##init(void) \ 31992108Sphk { \ 32092108Sphk mtx_unlock(&Giant); \ 32193248Sphk g_add_class(&class); \ 32292108Sphk mtx_lock(&Giant); \ 32392108Sphk } \ 324105061Sphk DECLARE_GEOM_CLASS_INIT(class, name, name##init); 32592108Sphk 32695310Sphk#endif /* _KERNEL */ 32792108Sphk 328105068Sphk/* 329105068Sphk * IOCTLS for talking to the geom.ctl device. 330105068Sphk */ 331105092Sphk 332105068Sphkstruct geomgetconf { 333105068Sphk char *ptr; 334105068Sphk u_int len; 335105068Sphk}; 336105068Sphk#define GEOMGETCONF _IOW('G', 0, struct geomgetconf) 33795323Sphk 338105092Sphkstruct g_createargs { 339105092Sphk /* Valid on call */ 340105092Sphk struct g_class *class; 341105092Sphk struct g_provider *provider; 342105092Sphk u_int flag; 343105092Sphk u_int len; 344105092Sphk void *ptr; 345105092Sphk /* Valid on return */ 346105092Sphk struct g_geom *geom; 347105092Sphk}; 348105092Sphk 349105092Sphkstruct geomconfiggeom { 350105092Sphk /* Valid on call */ 351105092Sphk struct geomidorname class; 352105092Sphk struct geomidorname provider; 353105092Sphk u_int flag; 354105092Sphk u_int len; 355105092Sphk void *ptr; 356105092Sphk /* Valid on return */ 357105092Sphk uintptr_t geom; 358105092Sphk}; 359105092Sphk#define GEOMCONFIGGEOM _IOW('G', 0, struct geomconfiggeom) 360105092Sphk 361105092Sphk 362103279Sphk/* geom_enc.c */ 363104060Sphkuint16_t g_dec_be2(u_char *p); 364103279Sphkuint32_t g_dec_be4(u_char *p); 365104060Sphkuint16_t g_dec_le2(u_char *p); 366103279Sphkuint32_t g_dec_le4(u_char *p); 367103279Sphkuint64_t g_dec_le8(u_char *p); 368104193Sphkvoid g_enc_le2(u_char *p, uint16_t u); 369103279Sphkvoid g_enc_le4(u_char *p, uint32_t u); 370103279Sphkvoid g_enc_le8(u_char *p, uint64_t u); 371103279Sphk 37295276Sphk#endif /* _GEOM_GEOM_H_ */ 373