geom.h revision 162352
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 162352 2006-09-16 10:39:07Z pjd $ 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> 47115473Sphk#include <sys/module.h> 4892108Sphk 4993248Sphkstruct g_class; 5092108Sphkstruct g_geom; 5192108Sphkstruct g_consumer; 5292108Sphkstruct g_provider; 53110541Sphkstruct g_stat; 5492108Sphkstruct thread; 5592108Sphkstruct bio; 5692108Sphkstruct sbuf; 57112709Sphkstruct gctl_req; 58106518Sphkstruct g_configargs; 5992108Sphk 60106518Sphktypedef int g_config_t (struct g_configargs *ca); 61115624Sphktypedef void g_ctl_req_t (struct gctl_req *, struct g_class *cp, char const *verb); 62112709Sphktypedef int g_ctl_create_geom_t (struct gctl_req *, struct g_class *cp, struct g_provider *pp); 63112709Sphktypedef int g_ctl_destroy_geom_t (struct gctl_req *, struct g_class *cp, struct g_geom *gp); 64113876Sphktypedef int g_ctl_config_geom_t (struct gctl_req *, struct g_geom *gp, const char *verb); 65115473Sphktypedef void g_init_t (struct g_class *mp); 66115473Sphktypedef void g_fini_t (struct g_class *mp); 67119660Sphktypedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *, int flags); 68138732Sphktypedef int g_ioctl_t(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td); 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 *); 78107953Sphktypedef void g_dumpconf_t (struct sbuf *, const 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 86106518Sphk * taste and config_geom functions. 8792108Sphk */ 8893248Sphkstruct g_class { 89107953Sphk const char *name; 90133312Sphk u_int version; 9192108Sphk g_taste_t *taste; 92106518Sphk g_config_t *config; 93115624Sphk g_ctl_req_t *ctlreq; 94115473Sphk g_init_t *init; 95115473Sphk g_fini_t *fini; 96112709Sphk g_ctl_destroy_geom_t *destroy_geom; 9793776Sphk /* 98149757Sphk * Default values for geom methods 99133312Sphk */ 100133312Sphk g_start_t *start; 101133312Sphk g_spoiled_t *spoiled; 102133312Sphk g_dumpconf_t *dumpconf; 103133312Sphk g_access_t *access; 104133312Sphk g_orphan_t *orphan; 105133312Sphk g_ioctl_t *ioctl; 106133312Sphk /* 107115468Sphk * The remaining elements are private 108115468Sphk */ 10993248Sphk LIST_ENTRY(g_class) class; 11092108Sphk LIST_HEAD(,g_geom) geom; 11192108Sphk}; 11292108Sphk 113133312Sphk#define G_VERSION_00 0x19950323 114138732Sphk#define G_VERSION_01 0x20041207 /* add fflag to g_ioctl_t */ 115138732Sphk#define G_VERSION G_VERSION_01 116133312Sphk 11792108Sphk/* 11893248Sphk * The g_geom is an instance of a g_class. 11992108Sphk */ 12092108Sphkstruct g_geom { 12192108Sphk char *name; 12293248Sphk struct g_class *class; 12392108Sphk LIST_ENTRY(g_geom) geom; 12492108Sphk LIST_HEAD(,g_consumer) consumer; 12592108Sphk LIST_HEAD(,g_provider) provider; 12692108Sphk TAILQ_ENTRY(g_geom) geoms; /* XXX: better name */ 12792108Sphk int rank; 12892108Sphk g_start_t *start; 12992108Sphk g_spoiled_t *spoiled; 13092108Sphk g_dumpconf_t *dumpconf; 13193776Sphk g_access_t *access; 13293776Sphk g_orphan_t *orphan; 133119660Sphk g_ioctl_t *ioctl; 13492108Sphk void *softc; 13592108Sphk unsigned flags; 13692108Sphk#define G_GEOM_WITHER 1 13792108Sphk}; 13892108Sphk 13992108Sphk/* 14092108Sphk * The g_bioq is a queue of struct bio's. 14192108Sphk * XXX: possibly collection point for statistics. 14292108Sphk * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head. 14392108Sphk */ 14492108Sphkstruct g_bioq { 14592108Sphk TAILQ_HEAD(, bio) bio_queue; 14692108Sphk struct mtx bio_queue_lock; 14792108Sphk int bio_queue_length; 14892108Sphk}; 14992108Sphk 15092108Sphk/* 15192108Sphk * A g_consumer is an attachment point for a g_provider. One g_consumer 15292108Sphk * can only be attached to one g_provider, but multiple g_consumers 15392108Sphk * can be attached to one g_provider. 15492108Sphk */ 15592108Sphk 15692108Sphkstruct g_consumer { 15792108Sphk struct g_geom *geom; 15892108Sphk LIST_ENTRY(g_consumer) consumer; 15992108Sphk struct g_provider *provider; 16092108Sphk LIST_ENTRY(g_consumer) consumers; /* XXX: better name */ 16192108Sphk int acr, acw, ace; 16292108Sphk int spoiled; 163112370Sphk struct devstat *stat; 164112026Sphk u_int nstart, nend; 165125743Sphk 166125743Sphk /* Two fields for the implementing class to use */ 167125743Sphk void *private; 168125743Sphk u_int index; 16992108Sphk}; 17092108Sphk 17192108Sphk/* 17292108Sphk * A g_provider is a "logical disk". 17392108Sphk */ 17492108Sphkstruct g_provider { 17592108Sphk char *name; 17692108Sphk LIST_ENTRY(g_provider) provider; 17792108Sphk struct g_geom *geom; 17892108Sphk LIST_HEAD(,g_consumer) consumers; 17992108Sphk int acr, acw, ace; 18092108Sphk int error; 18192108Sphk TAILQ_ENTRY(g_provider) orphan; 18293778Sphk off_t mediasize; 183105542Sphk u_int sectorsize; 184110710Sphk u_int stripesize; 185110710Sphk u_int stripeoffset; 186112370Sphk struct devstat *stat; 187112026Sphk u_int nstart, nend; 188110690Sphk u_int flags; 189110690Sphk#define G_PF_CANDELETE 0x1 190120851Sphk#define G_PF_WITHER 0x2 191123233Sphk#define G_PF_ORPHAN 0x4 192125743Sphk 193125743Sphk /* Two fields for the implementing class to use */ 194125743Sphk void *private; 195125743Sphk u_int index; 19692108Sphk}; 19792108Sphk 198105947Sphk/* geom_dev.c */ 199130585Sphkstruct cdev; 200115960Sphkvoid g_dev_print(void); 201130585Sphkstruct g_provider *g_dev_getprovider(struct cdev *dev); 202105947Sphk 20392108Sphk/* geom_dump.c */ 204107953Sphkvoid g_trace(int level, const char *, ...); 20592108Sphk# define G_T_TOPOLOGY 1 20692108Sphk# define G_T_BIO 2 20792108Sphk# define G_T_ACCESS 4 20892108Sphk 20993090Sphk 21092108Sphk/* geom_event.c */ 211113937Sphktypedef void g_event_t(void *, int flag); 212112989Sphk#define EV_CANCEL 1 213113937Sphkint g_post_event(g_event_t *func, void *arg, int flag, ...); 214113940Sphkint g_waitfor_event(g_event_t *func, void *arg, int flag, ...); 215112989Sphkvoid g_cancel_event(void *ref); 21692108Sphkvoid g_orphan_provider(struct g_provider *pp, int error); 217137489Spjdvoid g_waitidlelock(void); 21892108Sphk 21992108Sphk/* geom_subr.c */ 220125755Sphkint g_access(struct g_consumer *cp, int nread, int nwrite, int nexcl); 22192108Sphkint g_attach(struct g_consumer *cp, struct g_provider *pp); 22292108Sphkvoid g_destroy_consumer(struct g_consumer *cp); 22392108Sphkvoid g_destroy_geom(struct g_geom *pp); 22492108Sphkvoid g_destroy_provider(struct g_provider *pp); 22598066Sphkvoid g_detach(struct g_consumer *cp); 22692108Sphkvoid g_error_provider(struct g_provider *pp, int error); 227115850Sphkstruct g_provider *g_provider_by_name(char const *arg); 22894284Sphkint g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len); 22994284Sphk#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v)) 230107953Sphkint g_handleattr(struct bio *bp, const char *attribute, void *val, int len); 231107953Sphkint g_handleattr_int(struct bio *bp, const char *attribute, int val); 232107953Sphkint g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val); 23392108Sphkstruct g_consumer * g_new_consumer(struct g_geom *gp); 234107953Sphkstruct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...); 235107953Sphkstruct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...); 23692108Sphkvoid g_spoil(struct g_provider *pp, struct g_consumer *cp); 23792108Sphkint g_std_access(struct g_provider *pp, int dr, int dw, int de); 23892108Sphkvoid g_std_done(struct bio *bp); 23992108Sphkvoid g_std_spoiled(struct g_consumer *cp); 240114495Sphkvoid g_wither_geom(struct g_geom *gp, int error); 241137032Sphkvoid g_wither_geom_close(struct g_geom *gp, int error); 242157619Smarcelvoid g_wither_provider(struct g_provider *pp, int error); 24392108Sphk 244162326Spjd#if defined(DIAGNOSTIC) || defined(DDB) 245162326Spjdint g_valid_obj(void const *ptr); 246162326Spjd#endif 247126798Sphk#ifdef DIAGNOSTIC 248126798Sphk#define G_VALID_CLASS(foo) \ 249126798Sphk KASSERT(g_valid_obj(foo) == 1, ("%p is not a g_class", foo)) 250126798Sphk#define G_VALID_GEOM(foo) \ 251126798Sphk KASSERT(g_valid_obj(foo) == 2, ("%p is not a g_geom", foo)) 252126798Sphk#define G_VALID_CONSUMER(foo) \ 253126798Sphk KASSERT(g_valid_obj(foo) == 3, ("%p is not a g_consumer", foo)) 254126798Sphk#define G_VALID_PROVIDER(foo) \ 255126798Sphk KASSERT(g_valid_obj(foo) == 4, ("%p is not a g_provider", foo)) 256126798Sphk#else 257126798Sphk#define G_VALID_CLASS(foo) do { } while (0) 258126798Sphk#define G_VALID_GEOM(foo) do { } while (0) 259126798Sphk#define G_VALID_CONSUMER(foo) do { } while (0) 260126798Sphk#define G_VALID_PROVIDER(foo) do { } while (0) 261126798Sphk#endif 262126798Sphk 263115473Sphkint g_modevent(module_t, int, void *); 264115473Sphk 26592108Sphk/* geom_io.c */ 26692108Sphkstruct bio * g_clone_bio(struct bio *); 267159304Spjdstruct bio * g_duplicate_bio(struct bio *); 26892108Sphkvoid g_destroy_bio(struct bio *); 269104195Sphkvoid g_io_deliver(struct bio *bp, int error); 27094283Sphkint g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr); 27192108Sphkvoid g_io_request(struct bio *bp, struct g_consumer *cp); 27292108Sphkstruct bio *g_new_bio(void); 273134379Sphkstruct bio *g_alloc_bio(void); 27492108Sphkvoid * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error); 275104194Sphkint g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length); 276125713Spjdvoid g_print_bio(struct bio *bp); 27792108Sphk 27892108Sphk/* geom_kern.c / geom_kernsim.c */ 27992108Sphk 28092108Sphk#ifdef _KERNEL 28192108Sphk 282160301Spjdextern struct sx topology_lock; 283160301Spjd 28495038Sphkstruct g_kerneldump { 28595038Sphk off_t offset; 28695038Sphk off_t length; 28795038Sphk}; 28895038Sphk 28992108SphkMALLOC_DECLARE(M_GEOM); 29092108Sphk 29192108Sphkstatic __inline void * 29292108Sphkg_malloc(int size, int flags) 29392108Sphk{ 29492108Sphk void *p; 29592108Sphk 29692108Sphk p = malloc(size, M_GEOM, flags); 29792108Sphk return (p); 29892108Sphk} 29992108Sphk 30092108Sphkstatic __inline void 30192108Sphkg_free(void *ptr) 30292108Sphk{ 303126798Sphk 304126798Sphk#ifdef DIAGNOSTIC 305160301Spjd if (sx_xlocked(&topology_lock)) { 306160301Spjd KASSERT(g_valid_obj(ptr) == 0, 307160301Spjd ("g_free(%p) of live object, type %d", ptr, 308160301Spjd g_valid_obj(ptr))); 309160301Spjd } 310126798Sphk#endif 31192108Sphk free(ptr, M_GEOM); 31292108Sphk} 31392108Sphk 31496987Sphk#define g_topology_lock() \ 31596987Sphk do { \ 31696987Sphk mtx_assert(&Giant, MA_NOTOWNED); \ 31796987Sphk sx_xlock(&topology_lock); \ 31896987Sphk } while (0) 31996987Sphk 320139139Spjd#define g_topology_try_lock() sx_try_xlock(&topology_lock) 321139139Spjd 32296987Sphk#define g_topology_unlock() \ 32396987Sphk do { \ 32496987Sphk sx_xunlock(&topology_lock); \ 32596987Sphk } while (0) 32696987Sphk 32796987Sphk#define g_topology_assert() \ 32896987Sphk do { \ 32996987Sphk sx_assert(&topology_lock, SX_XLOCKED); \ 33096987Sphk } while (0) 33196987Sphk 332125656Spjd#define g_topology_assert_not() \ 333125656Spjd do { \ 334125656Spjd sx_assert(&topology_lock, SX_UNLOCKED); \ 335125656Spjd } while (0) 336125656Spjd 337115473Sphk#define DECLARE_GEOM_CLASS(class, name) \ 338115473Sphk static moduledata_t name##_mod = { \ 339115473Sphk #name, g_modevent, &class \ 340115473Sphk }; \ 341115473Sphk DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 342105061Sphk 34395310Sphk#endif /* _KERNEL */ 34492108Sphk 345112709Sphk/* geom_ctl.c */ 346157581Smarcelint gctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len); 347157581Smarcelvoid gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr, int len); 348112709Sphkvoid *gctl_get_param(struct gctl_req *req, const char *param, int *len); 349115624Sphkchar const *gctl_get_asciiparam(struct gctl_req *req, const char *param); 350113892Sphkvoid *gctl_get_paraml(struct gctl_req *req, const char *param, int len); 351162352Spjdint gctl_error(struct gctl_req *req, const char *fmt, ...) __printflike(2, 3); 352115624Sphkstruct g_class *gctl_get_class(struct gctl_req *req, char const *arg); 353115624Sphkstruct g_geom *gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg); 354115624Sphkstruct g_provider *gctl_get_provider(struct gctl_req *req, char const *arg); 355112709Sphk 35695276Sphk#endif /* _GEOM_GEOM_H_ */ 357