geom.h revision 112026
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 112026 2003-03-09 09:58:36Z phk $ 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; 61110541Sphkstruct g_stat; 6292108Sphkstruct thread; 6392108Sphkstruct bio; 6492108Sphkstruct sbuf; 65106518Sphkstruct g_configargs; 6692108Sphk 67106518Sphktypedef int g_config_t (struct g_configargs *ca); 6893248Sphktypedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *, 6993250Sphk int flags); 7092108Sphk#define G_TF_NORMAL 0 7192108Sphk#define G_TF_INSIST 1 7292108Sphk#define G_TF_TRANSPARENT 2 7392108Sphktypedef int g_access_t (struct g_provider *, int, int, int); 7492108Sphk/* XXX: not sure about the thread arg */ 7593250Sphktypedef void g_orphan_t (struct g_consumer *); 7692108Sphk 7792108Sphktypedef void g_start_t (struct bio *); 7892108Sphktypedef void g_spoiled_t (struct g_consumer *); 79107953Sphktypedef void g_dumpconf_t (struct sbuf *, const char *indent, struct g_geom *, 8092108Sphk struct g_consumer *, struct g_provider *); 8192108Sphk 8292108Sphk/* 8393248Sphk * The g_class structure describes a transformation class. In other words 8493248Sphk * all BSD disklabel handlers share one g_class, all MBR handlers share 8593248Sphk * one common g_class and so on. 8693248Sphk * Certain operations are instantiated on the class, most notably the 87106518Sphk * taste and config_geom functions. 8892108Sphk */ 8993248Sphkstruct g_class { 90107953Sphk const char *name; 9192108Sphk g_taste_t *taste; 92106518Sphk g_config_t *config; 9393776Sphk /* 9493776Sphk * The remaning elements are private and classes should use 9598066Sphk * the G_CLASS_INITIALIZER macro to initialize them. 9693776Sphk */ 9793248Sphk LIST_ENTRY(g_class) class; 9892108Sphk LIST_HEAD(,g_geom) geom; 9992108Sphk struct g_event *event; 10095310Sphk u_int protect; 10192108Sphk}; 10292108Sphk 10398066Sphk#define G_CLASS_INITIALIZER { 0, 0 }, { 0 }, 0, 0 10493776Sphk 10592108Sphk/* 10693248Sphk * The g_geom is an instance of a g_class. 10792108Sphk */ 10892108Sphkstruct g_geom { 10995310Sphk u_int protect; 11092108Sphk char *name; 11193248Sphk struct g_class *class; 11292108Sphk LIST_ENTRY(g_geom) geom; 11392108Sphk LIST_HEAD(,g_consumer) consumer; 11492108Sphk LIST_HEAD(,g_provider) provider; 11592108Sphk TAILQ_ENTRY(g_geom) geoms; /* XXX: better name */ 11692108Sphk int rank; 11792108Sphk g_start_t *start; 11892108Sphk g_spoiled_t *spoiled; 11992108Sphk g_dumpconf_t *dumpconf; 12093776Sphk g_access_t *access; 12193776Sphk g_orphan_t *orphan; 12292108Sphk void *softc; 12392108Sphk struct g_event *event; 12492108Sphk unsigned flags; 12592108Sphk#define G_GEOM_WITHER 1 12692108Sphk}; 12792108Sphk 12892108Sphk/* 12992108Sphk * The g_bioq is a queue of struct bio's. 13092108Sphk * XXX: possibly collection point for statistics. 13192108Sphk * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head. 13292108Sphk */ 13392108Sphkstruct g_bioq { 13492108Sphk TAILQ_HEAD(, bio) bio_queue; 13592108Sphk struct mtx bio_queue_lock; 13692108Sphk int bio_queue_length; 13792108Sphk}; 13892108Sphk 13992108Sphk/* 14092108Sphk * A g_consumer is an attachment point for a g_provider. One g_consumer 14192108Sphk * can only be attached to one g_provider, but multiple g_consumers 14292108Sphk * can be attached to one g_provider. 14392108Sphk */ 14492108Sphk 14592108Sphkstruct g_consumer { 14695310Sphk u_int protect; 14792108Sphk struct g_geom *geom; 14892108Sphk LIST_ENTRY(g_consumer) consumer; 14992108Sphk struct g_provider *provider; 15092108Sphk LIST_ENTRY(g_consumer) consumers; /* XXX: better name */ 15192108Sphk int acr, acw, ace; 15292108Sphk struct g_event *event; 15392108Sphk int spoiled; 154110541Sphk struct g_stat *stat; 155112026Sphk u_int nstart, nend; 15692108Sphk}; 15792108Sphk 15892108Sphk/* 15992108Sphk * A g_provider is a "logical disk". 16092108Sphk */ 16192108Sphkstruct g_provider { 16295310Sphk u_int protect; 16392108Sphk char *name; 16492108Sphk LIST_ENTRY(g_provider) provider; 16592108Sphk struct g_geom *geom; 16692108Sphk LIST_HEAD(,g_consumer) consumers; 16792108Sphk int acr, acw, ace; 16892108Sphk int error; 16992108Sphk struct g_event *event; 17092108Sphk TAILQ_ENTRY(g_provider) orphan; 171107953Sphk u_int index; 17293778Sphk off_t mediasize; 173105542Sphk u_int sectorsize; 174110710Sphk u_int stripesize; 175110710Sphk u_int stripeoffset; 176110541Sphk struct g_stat *stat; 177112026Sphk u_int nstart, nend; 178110690Sphk u_int flags; 179110690Sphk#define G_PF_CANDELETE 0x1 18092108Sphk}; 18192108Sphk 182105092Sphk/* 183105092Sphk * This gadget is used by userland to pinpoint a particular instance of 184105092Sphk * something in the kernel. The name is unreadable on purpose, people 185105092Sphk * should not encounter it directly but use library functions to deal 186105092Sphk * with it. 187105092Sphk * If len is zero, "id" contains a cast of the kernel pointer where the 188105092Sphk * entity is located, (likely derived from the "id=" attribute in the 189105092Sphk * XML config) and the g_id*() functions will validate this before allowing 190105092Sphk * it to be used. 191105092Sphk * If len is non-zero, it is the strlen() of the name which is pointed to 192105092Sphk * by "name". 193105092Sphk */ 194105092Sphkstruct geomidorname { 195105092Sphk u_int len; 196105092Sphk union { 197105092Sphk const char *name; 198105092Sphk uintptr_t id; 199105092Sphk } u; 200105092Sphk}; 201105092Sphk 202105947Sphk/* geom_dev.c */ 203105947Sphkint g_dev_print(void); 204105947Sphk 20592108Sphk/* geom_dump.c */ 20692108Sphkvoid g_hexdump(void *ptr, int length); 207107953Sphkvoid g_trace(int level, const char *, ...); 20892108Sphk# define G_T_TOPOLOGY 1 20992108Sphk# define G_T_BIO 2 21092108Sphk# define G_T_ACCESS 4 21192108Sphk 21293090Sphk 21392108Sphk/* geom_event.c */ 214104056Sphktypedef void g_call_me_t(void *); 215104056Sphkint g_call_me(g_call_me_t *func, void *arg); 21692108Sphkvoid g_orphan_provider(struct g_provider *pp, int error); 21798066Sphkvoid g_waitidle(void); 21892108Sphk 21992108Sphk/* geom_subr.c */ 220107453Sphkint g_access_abs(struct g_consumer *cp, int nread, int nwrite, int nexcl); 221107453Sphkint g_access_rel(struct g_consumer *cp, int nread, int nwrite, int nexcl); 22293248Sphkvoid g_add_class(struct g_class *mp); 22392108Sphkint g_attach(struct g_consumer *cp, struct g_provider *pp); 22492108Sphkvoid g_destroy_consumer(struct g_consumer *cp); 22592108Sphkvoid g_destroy_geom(struct g_geom *pp); 22692108Sphkvoid g_destroy_provider(struct g_provider *pp); 22798066Sphkvoid g_detach(struct g_consumer *cp); 22892108Sphkvoid g_error_provider(struct g_provider *pp, int error); 22994284Sphkint g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len); 23094284Sphk#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v)) 231107953Sphkint g_handleattr(struct bio *bp, const char *attribute, void *val, int len); 232107953Sphkint g_handleattr_int(struct bio *bp, const char *attribute, int val); 233107953Sphkint g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val); 234107953Sphkstruct g_geom * g_insert_geom(const char *class, struct g_consumer *cp); 23592108Sphkstruct g_consumer * g_new_consumer(struct g_geom *gp); 236107953Sphkstruct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...); 237107953Sphkstruct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...); 23895310Sphkvoid g_sanity(void *ptr); 23992108Sphkvoid g_spoil(struct g_provider *pp, struct g_consumer *cp); 24092108Sphkint g_std_access(struct g_provider *pp, int dr, int dw, int de); 24192108Sphkvoid g_std_done(struct bio *bp); 24292108Sphkvoid g_std_spoiled(struct g_consumer *cp); 243105092Sphkstruct g_class *g_idclass(struct geomidorname *); 244105092Sphkstruct g_geom *g_idgeom(struct geomidorname *); 245105092Sphkstruct g_provider *g_idprovider(struct geomidorname *); 24692108Sphk 247105092Sphk 24892108Sphk/* geom_io.c */ 24992108Sphkstruct bio * g_clone_bio(struct bio *); 25092108Sphkvoid g_destroy_bio(struct bio *); 251104195Sphkvoid g_io_deliver(struct bio *bp, int error); 25294283Sphkint g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr); 25392108Sphkvoid g_io_request(struct bio *bp, struct g_consumer *cp); 25494283Sphkint g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr); 25592108Sphkstruct bio *g_new_bio(void); 25692108Sphkvoid * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error); 257104194Sphkint g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length); 25892108Sphk 25992108Sphk/* geom_kern.c / geom_kernsim.c */ 26092108Sphk 261104602Sphk#ifndef _SYS_CONF_H_ 262104602Sphktypedef int d_ioctl_t(dev_t dev, u_long cmd, caddr_t data, 263104602Sphk int fflag, struct thread *td); 264104602Sphk#endif 265104602Sphk 26692403Sphkstruct g_ioctl { 26792403Sphk u_long cmd; 26892403Sphk void *data; 26992403Sphk int fflag; 27092403Sphk struct thread *td; 271104602Sphk d_ioctl_t *func; 272109972Sphk void *dev; 27392403Sphk}; 27492108Sphk 27592108Sphk#ifdef _KERNEL 27692108Sphk 27795038Sphkstruct g_kerneldump { 27895038Sphk off_t offset; 27995038Sphk off_t length; 28095038Sphk}; 28195038Sphk 28292108SphkMALLOC_DECLARE(M_GEOM); 28392108Sphk 28492108Sphkstatic __inline void * 28592108Sphkg_malloc(int size, int flags) 28692108Sphk{ 28792108Sphk void *p; 28892108Sphk 28992108Sphk p = malloc(size, M_GEOM, flags); 29095310Sphk g_sanity(p); 29195310Sphk /* printf("malloc(%d, %x) -> %p\n", size, flags, p); */ 29292108Sphk return (p); 29392108Sphk} 29492108Sphk 29592108Sphkstatic __inline void 29692108Sphkg_free(void *ptr) 29792108Sphk{ 29895310Sphk g_sanity(ptr); 29995310Sphk /* printf("free(%p)\n", ptr); */ 30092108Sphk free(ptr, M_GEOM); 30192108Sphk} 30292108Sphk 30392108Sphkextern struct sx topology_lock; 30492108Sphk 30596987Sphk#define g_topology_lock() \ 30696987Sphk do { \ 30796987Sphk mtx_assert(&Giant, MA_NOTOWNED); \ 30896987Sphk sx_xlock(&topology_lock); \ 30996987Sphk } while (0) 31096987Sphk 31196987Sphk#define g_topology_unlock() \ 31296987Sphk do { \ 31396987Sphk g_sanity(NULL); \ 31496987Sphk sx_xunlock(&topology_lock); \ 31596987Sphk } while (0) 31696987Sphk 31796987Sphk#define g_topology_assert() \ 31896987Sphk do { \ 31996987Sphk g_sanity(NULL); \ 32096987Sphk sx_assert(&topology_lock, SX_XLOCKED); \ 32196987Sphk } while (0) 32296987Sphk 323105061Sphk#define DECLARE_GEOM_CLASS_INIT(class, name, init) \ 324105124Sjake SYSINIT(name, SI_SUB_DRIVERS, SI_ORDER_FIRST, init, NULL); 325105061Sphk 32693248Sphk#define DECLARE_GEOM_CLASS(class, name) \ 32792108Sphk static void \ 32892108Sphk name##init(void) \ 32992108Sphk { \ 33092108Sphk mtx_unlock(&Giant); \ 33193248Sphk g_add_class(&class); \ 33292108Sphk mtx_lock(&Giant); \ 33392108Sphk } \ 334105061Sphk DECLARE_GEOM_CLASS_INIT(class, name, name##init); 33592108Sphk 33695310Sphk#endif /* _KERNEL */ 33792108Sphk 338105068Sphk/* 339105068Sphk * IOCTLS for talking to the geom.ctl device. 340105068Sphk */ 341105092Sphk 342106518Sphk/* 343106518Sphk * This is the structure used internally in the kernel, it is created and 344106518Sphk * populated by geom_ctl.c. 345106518Sphk */ 346106518Sphkstruct g_configargs { 347105092Sphk /* Valid on call */ 348105092Sphk struct g_class *class; 349105504Sphk struct g_geom *geom; 350105092Sphk struct g_provider *provider; 351105092Sphk u_int flag; 352105092Sphk u_int len; 353105092Sphk void *ptr; 354105092Sphk}; 355105092Sphk 356106518Sphk/* 357106518Sphk * This is the structure used to communicate with userland. 358106518Sphk */ 359105092Sphkstruct geomconfiggeom { 360105092Sphk /* Valid on call */ 361105092Sphk struct geomidorname class; 362105504Sphk struct geomidorname geom; 363105092Sphk struct geomidorname provider; 364105092Sphk u_int flag; 365105092Sphk u_int len; 366105092Sphk void *ptr; 367105092Sphk}; 368106518Sphk 369105092Sphk#define GEOMCONFIGGEOM _IOW('G', 0, struct geomconfiggeom) 370105092Sphk 371106518Sphk#define GCFG_GENERIC0 0x00000000 372106518Sphk /* 373106518Sphk * Generic requests suitable for all classes. 374106518Sphk */ 375106518Sphk#define GCFG_CLASS0 0x10000000 376106518Sphk /* 377106518Sphk * Class specific verbs. Allocations in this part of the numberspace 378106518Sphk * can only be done after review and approval of phk@FreeBSD.org. 379106518Sphk * All allocations in this space will be listed in this file. 380106518Sphk */ 381106518Sphk#define GCFG_PRIVATE0 0x20000000 382106518Sphk /* 383106518Sphk * Lowest allocation for private flag definitions. 384106518Sphk * If you define you own private "verbs", please express them in 385106518Sphk * your code as (GCFG_PRIVATE0 + somenumber), where somenumber is 386106518Sphk * a magic number in the range [0x0 ... 0xfffffff] chosen the way 387106518Sphk * magic numbers are chosen. Such allocation SHALL NOT be listed 388106518Sphk * here but SHOULD be listed in some suitable .h file. 389106518Sphk */ 390106518Sphk#define GCFG_RESERVED0 0x30000000 391106518Sphk#define GCFG_RESERVEDN 0xffffffff 392106518Sphk /* 393106518Sphk * This area is reserved for the future. 394106518Sphk */ 395106518Sphk 396106518Sphk#define GCFG_CREATE (GCFG_GENERIC0 + 0x0) 397106518Sphk /* 398106518Sphk * Request geom construction. 399106518Sphk * ptr/len is class-specific. 400106518Sphk */ 401106518Sphk#define GCFG_DISMANTLE (GCFG_GENERIC0 + 0x1) 402106518Sphk /* 403106518Sphk * Request orderly geom dismantling. 404106518Sphk * ptr/len is class-specific. 405106518Sphk */ 406106518Sphk 407106518Sphk 408106518Sphkstruct gcfg_magicrw { 409106518Sphk off_t offset; 410106518Sphk u_int len; 411106518Sphk}; 412106518Sphk 413106518Sphk#define GCFG_MAGICREAD (GCFG_GENERIC0 + 0x100) 414106518Sphk /* 415106518Sphk * Read of magic spaces. 416106518Sphk * ptr/len is gcfgmagicrw structure followed by bufferspace 417106518Sphk * for data to be read. 418106518Sphk */ 419106518Sphk#define GCFG_MAGICWRITE (GCFG_GENERIC0 + 0x101) 420106518Sphk /* 421106518Sphk * Write of magic spaces. 422106518Sphk * as above, only the other way. 423106518Sphk */ 424106518Sphk 425106518Sphk 426103279Sphk/* geom_enc.c */ 427105163Sphkuint16_t g_dec_be2(const u_char *p); 428105163Sphkuint32_t g_dec_be4(const u_char *p); 429105163Sphkuint16_t g_dec_le2(const u_char *p); 430105163Sphkuint32_t g_dec_le4(const u_char *p); 431105163Sphkuint64_t g_dec_le8(const u_char *p); 432104193Sphkvoid g_enc_le2(u_char *p, uint16_t u); 433103279Sphkvoid g_enc_le4(u_char *p, uint32_t u); 434103279Sphkvoid g_enc_le8(u_char *p, uint64_t u); 435103279Sphk 43695276Sphk#endif /* _GEOM_GEOM_H_ */ 437