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: releng/10.3/sys/geom/geom.h 271238 2014-09-07 21:30:47Z smh $ 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> 46219950Smav#include <sys/conf.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 *); 78223089Sgibbstypedef void g_attrchanged_t (struct g_consumer *, const char *attr); 79237518Skentypedef void g_provgone_t (struct g_provider *); 80107953Sphktypedef void g_dumpconf_t (struct sbuf *, const char *indent, struct g_geom *, 8192108Sphk struct g_consumer *, struct g_provider *); 82238213Strasztypedef void g_resize_t(struct g_consumer *cp); 8392108Sphk 8492108Sphk/* 8593248Sphk * The g_class structure describes a transformation class. In other words 8693248Sphk * all BSD disklabel handlers share one g_class, all MBR handlers share 8793248Sphk * one common g_class and so on. 8893248Sphk * Certain operations are instantiated on the class, most notably the 89106518Sphk * taste and config_geom functions. 9092108Sphk */ 9193248Sphkstruct g_class { 92107953Sphk const char *name; 93133312Sphk u_int version; 94224147Spjd u_int spare0; 9592108Sphk g_taste_t *taste; 96106518Sphk g_config_t *config; 97115624Sphk g_ctl_req_t *ctlreq; 98115473Sphk g_init_t *init; 99115473Sphk g_fini_t *fini; 100112709Sphk g_ctl_destroy_geom_t *destroy_geom; 10193776Sphk /* 102149757Sphk * Default values for geom methods 103133312Sphk */ 104133312Sphk g_start_t *start; 105133312Sphk g_spoiled_t *spoiled; 106223089Sgibbs g_attrchanged_t *attrchanged; 107133312Sphk g_dumpconf_t *dumpconf; 108133312Sphk g_access_t *access; 109133312Sphk g_orphan_t *orphan; 110133312Sphk g_ioctl_t *ioctl; 111237545Sken g_provgone_t *providergone; 112238213Strasz g_resize_t *resize; 113238559Sken void *spare1; 114238533Strasz void *spare2; 115133312Sphk /* 116115468Sphk * The remaining elements are private 117115468Sphk */ 11893248Sphk LIST_ENTRY(g_class) class; 11992108Sphk LIST_HEAD(,g_geom) geom; 12092108Sphk}; 12192108Sphk 122133312Sphk#define G_VERSION_00 0x19950323 123138732Sphk#define G_VERSION_01 0x20041207 /* add fflag to g_ioctl_t */ 124138732Sphk#define G_VERSION G_VERSION_01 125133312Sphk 12692108Sphk/* 12793248Sphk * The g_geom is an instance of a g_class. 12892108Sphk */ 12992108Sphkstruct g_geom { 13092108Sphk char *name; 13193248Sphk struct g_class *class; 13292108Sphk LIST_ENTRY(g_geom) geom; 13392108Sphk LIST_HEAD(,g_consumer) consumer; 13492108Sphk LIST_HEAD(,g_provider) provider; 13592108Sphk TAILQ_ENTRY(g_geom) geoms; /* XXX: better name */ 13692108Sphk int rank; 13792108Sphk g_start_t *start; 13892108Sphk g_spoiled_t *spoiled; 139223089Sgibbs g_attrchanged_t *attrchanged; 14092108Sphk g_dumpconf_t *dumpconf; 14193776Sphk g_access_t *access; 14293776Sphk g_orphan_t *orphan; 143119660Sphk g_ioctl_t *ioctl; 144237545Sken g_provgone_t *providergone; 145238213Strasz g_resize_t *resize; 146238559Sken void *spare0; 147238533Strasz void *spare1; 14892108Sphk void *softc; 14992108Sphk unsigned flags; 15092108Sphk#define G_GEOM_WITHER 1 151195195Strasz#define G_GEOM_VOLATILE_BIO 2 15292108Sphk}; 15392108Sphk 15492108Sphk/* 15592108Sphk * The g_bioq is a queue of struct bio's. 15692108Sphk * XXX: possibly collection point for statistics. 15792108Sphk * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head. 15892108Sphk */ 15992108Sphkstruct g_bioq { 16092108Sphk TAILQ_HEAD(, bio) bio_queue; 16192108Sphk struct mtx bio_queue_lock; 16292108Sphk int bio_queue_length; 16392108Sphk}; 16492108Sphk 16592108Sphk/* 16692108Sphk * A g_consumer is an attachment point for a g_provider. One g_consumer 16792108Sphk * can only be attached to one g_provider, but multiple g_consumers 16892108Sphk * can be attached to one g_provider. 16992108Sphk */ 17092108Sphk 17192108Sphkstruct g_consumer { 17292108Sphk struct g_geom *geom; 17392108Sphk LIST_ENTRY(g_consumer) consumer; 17492108Sphk struct g_provider *provider; 17592108Sphk LIST_ENTRY(g_consumer) consumers; /* XXX: better name */ 17692108Sphk int acr, acw, ace; 177238886Smav int flags; 178238886Smav#define G_CF_SPOILED 0x1 179238886Smav#define G_CF_ORPHAN 0x4 180260385Sscottl#define G_CF_DIRECT_SEND 0x10 181260385Sscottl#define G_CF_DIRECT_RECEIVE 0x20 182112370Sphk struct devstat *stat; 183112026Sphk u_int nstart, nend; 184125743Sphk 185125743Sphk /* Two fields for the implementing class to use */ 186125743Sphk void *private; 187125743Sphk u_int index; 18892108Sphk}; 18992108Sphk 19092108Sphk/* 19192108Sphk * A g_provider is a "logical disk". 19292108Sphk */ 19392108Sphkstruct g_provider { 19492108Sphk char *name; 19592108Sphk LIST_ENTRY(g_provider) provider; 19692108Sphk struct g_geom *geom; 19792108Sphk LIST_HEAD(,g_consumer) consumers; 19892108Sphk int acr, acw, ace; 19992108Sphk int error; 20092108Sphk TAILQ_ENTRY(g_provider) orphan; 20193778Sphk off_t mediasize; 202105542Sphk u_int sectorsize; 203110710Sphk u_int stripesize; 204110710Sphk u_int stripeoffset; 205112370Sphk struct devstat *stat; 206112026Sphk u_int nstart, nend; 207110690Sphk u_int flags; 208120851Sphk#define G_PF_WITHER 0x2 209123233Sphk#define G_PF_ORPHAN 0x4 210248508Skib#define G_PF_ACCEPT_UNMAPPED 0x8 211260385Sscottl#define G_PF_DIRECT_SEND 0x10 212260385Sscottl#define G_PF_DIRECT_RECEIVE 0x20 213125743Sphk 214125743Sphk /* Two fields for the implementing class to use */ 215125743Sphk void *private; 216125743Sphk u_int index; 21792108Sphk}; 21892108Sphk 219193981Sluigi/* 220193981Sluigi * Descriptor of a classifier. We can register a function and 221193981Sluigi * an argument, which is called by g_io_request() on bio's 222193981Sluigi * that are not previously classified. 223193981Sluigi */ 224193981Sluigistruct g_classifier_hook { 225193981Sluigi TAILQ_ENTRY(g_classifier_hook) link; 226193981Sluigi int (*func)(void *arg, struct bio *bp); 227193981Sluigi void *arg; 228193981Sluigi}; 229193981Sluigi 230219970Smav/* BIO_GETATTR("GEOM::setstate") argument values. */ 231219970Smav#define G_STATE_FAILED 0 232219970Smav#define G_STATE_REBUILD 1 233219970Smav#define G_STATE_RESYNC 2 234219970Smav#define G_STATE_ACTIVE 3 235219970Smav 236105947Sphk/* geom_dev.c */ 237130585Sphkstruct cdev; 238115960Sphkvoid g_dev_print(void); 239223089Sgibbsvoid g_dev_physpath_changed(void); 240130585Sphkstruct g_provider *g_dev_getprovider(struct cdev *dev); 241105947Sphk 24292108Sphk/* geom_dump.c */ 243107953Sphkvoid g_trace(int level, const char *, ...); 24492108Sphk# define G_T_TOPOLOGY 1 24592108Sphk# define G_T_BIO 2 24692108Sphk# define G_T_ACCESS 4 24792108Sphk 24893090Sphk 24992108Sphk/* geom_event.c */ 250113937Sphktypedef void g_event_t(void *, int flag); 251112989Sphk#define EV_CANCEL 1 252113937Sphkint g_post_event(g_event_t *func, void *arg, int flag, ...); 253113940Sphkint g_waitfor_event(g_event_t *func, void *arg, int flag, ...); 254112989Sphkvoid g_cancel_event(void *ref); 255223089Sgibbsint g_attr_changed(struct g_provider *pp, const char *attr, int flag); 256238886Smavint g_media_changed(struct g_provider *pp, int flag); 257238886Smavint g_media_gone(struct g_provider *pp, int flag); 25892108Sphkvoid g_orphan_provider(struct g_provider *pp, int error); 259137489Spjdvoid g_waitidlelock(void); 26092108Sphk 26192108Sphk/* geom_subr.c */ 262125755Sphkint g_access(struct g_consumer *cp, int nread, int nwrite, int nexcl); 26392108Sphkint g_attach(struct g_consumer *cp, struct g_provider *pp); 264221101Smavint g_compare_names(const char *namea, const char *nameb); 26592108Sphkvoid g_destroy_consumer(struct g_consumer *cp); 26692108Sphkvoid g_destroy_geom(struct g_geom *pp); 26792108Sphkvoid g_destroy_provider(struct g_provider *pp); 26898066Sphkvoid g_detach(struct g_consumer *cp); 26992108Sphkvoid g_error_provider(struct g_provider *pp, int error); 270115850Sphkstruct g_provider *g_provider_by_name(char const *arg); 27194284Sphkint g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len); 27294284Sphk#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v)) 273187973Smarcelint g_handleattr(struct bio *bp, const char *attribute, const void *val, 274187973Smarcel int len); 275107953Sphkint g_handleattr_int(struct bio *bp, const char *attribute, int val); 276107953Sphkint g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val); 277271238Ssmhint g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val); 278187973Smarcelint g_handleattr_str(struct bio *bp, const char *attribute, const char *str); 27992108Sphkstruct g_consumer * g_new_consumer(struct g_geom *gp); 280243333Sjhstruct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...) 281243333Sjh __printflike(2, 3); 282243333Sjhstruct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...) 283243333Sjh __printflike(2, 3); 284238213Straszvoid g_resize_provider(struct g_provider *pp, off_t size); 285177509Smarcelint g_retaste(struct g_class *mp); 28692108Sphkvoid g_spoil(struct g_provider *pp, struct g_consumer *cp); 28792108Sphkint g_std_access(struct g_provider *pp, int dr, int dw, int de); 28892108Sphkvoid g_std_done(struct bio *bp); 28992108Sphkvoid g_std_spoiled(struct g_consumer *cp); 290114495Sphkvoid g_wither_geom(struct g_geom *gp, int error); 291137032Sphkvoid g_wither_geom_close(struct g_geom *gp, int error); 292157619Smarcelvoid g_wither_provider(struct g_provider *pp, int error); 29392108Sphk 294162326Spjd#if defined(DIAGNOSTIC) || defined(DDB) 295162326Spjdint g_valid_obj(void const *ptr); 296162326Spjd#endif 297126798Sphk#ifdef DIAGNOSTIC 298126798Sphk#define G_VALID_CLASS(foo) \ 299126798Sphk KASSERT(g_valid_obj(foo) == 1, ("%p is not a g_class", foo)) 300126798Sphk#define G_VALID_GEOM(foo) \ 301126798Sphk KASSERT(g_valid_obj(foo) == 2, ("%p is not a g_geom", foo)) 302126798Sphk#define G_VALID_CONSUMER(foo) \ 303126798Sphk KASSERT(g_valid_obj(foo) == 3, ("%p is not a g_consumer", foo)) 304126798Sphk#define G_VALID_PROVIDER(foo) \ 305126798Sphk KASSERT(g_valid_obj(foo) == 4, ("%p is not a g_provider", foo)) 306126798Sphk#else 307126798Sphk#define G_VALID_CLASS(foo) do { } while (0) 308126798Sphk#define G_VALID_GEOM(foo) do { } while (0) 309126798Sphk#define G_VALID_CONSUMER(foo) do { } while (0) 310126798Sphk#define G_VALID_PROVIDER(foo) do { } while (0) 311126798Sphk#endif 312126798Sphk 313115473Sphkint g_modevent(module_t, int, void *); 314115473Sphk 31592108Sphk/* geom_io.c */ 31692108Sphkstruct bio * g_clone_bio(struct bio *); 317159304Spjdstruct bio * g_duplicate_bio(struct bio *); 31892108Sphkvoid g_destroy_bio(struct bio *); 319104195Sphkvoid g_io_deliver(struct bio *bp, int error); 32094283Sphkint g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr); 321163832Spjdint g_io_flush(struct g_consumer *cp); 322193981Sluigiint g_register_classifier(struct g_classifier_hook *hook); 323193981Sluigivoid g_unregister_classifier(struct g_classifier_hook *hook); 32492108Sphkvoid g_io_request(struct bio *bp, struct g_consumer *cp); 32592108Sphkstruct bio *g_new_bio(void); 326134379Sphkstruct bio *g_alloc_bio(void); 32792108Sphkvoid * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error); 328104194Sphkint g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length); 329169283Spjdint g_delete_data(struct g_consumer *cp, off_t offset, off_t length); 330125713Spjdvoid g_print_bio(struct bio *bp); 33192108Sphk 33292108Sphk/* geom_kern.c / geom_kernsim.c */ 33392108Sphk 33492108Sphk#ifdef _KERNEL 33592108Sphk 336160301Spjdextern struct sx topology_lock; 337160301Spjd 33895038Sphkstruct g_kerneldump { 33995038Sphk off_t offset; 34095038Sphk off_t length; 341219950Smav struct dumperinfo di; 34295038Sphk}; 34395038Sphk 34492108SphkMALLOC_DECLARE(M_GEOM); 34592108Sphk 34692108Sphkstatic __inline void * 34792108Sphkg_malloc(int size, int flags) 34892108Sphk{ 34992108Sphk void *p; 35092108Sphk 35192108Sphk p = malloc(size, M_GEOM, flags); 35292108Sphk return (p); 35392108Sphk} 35492108Sphk 35592108Sphkstatic __inline void 35692108Sphkg_free(void *ptr) 35792108Sphk{ 358126798Sphk 359126798Sphk#ifdef DIAGNOSTIC 360160301Spjd if (sx_xlocked(&topology_lock)) { 361160301Spjd KASSERT(g_valid_obj(ptr) == 0, 362160301Spjd ("g_free(%p) of live object, type %d", ptr, 363160301Spjd g_valid_obj(ptr))); 364160301Spjd } 365126798Sphk#endif 36692108Sphk free(ptr, M_GEOM); 36792108Sphk} 36892108Sphk 36996987Sphk#define g_topology_lock() \ 37096987Sphk do { \ 37196987Sphk mtx_assert(&Giant, MA_NOTOWNED); \ 37296987Sphk sx_xlock(&topology_lock); \ 37396987Sphk } while (0) 37496987Sphk 375139139Spjd#define g_topology_try_lock() sx_try_xlock(&topology_lock) 376139139Spjd 37796987Sphk#define g_topology_unlock() \ 37896987Sphk do { \ 37996987Sphk sx_xunlock(&topology_lock); \ 38096987Sphk } while (0) 38196987Sphk 38296987Sphk#define g_topology_assert() \ 38396987Sphk do { \ 38496987Sphk sx_assert(&topology_lock, SX_XLOCKED); \ 38596987Sphk } while (0) 38696987Sphk 387125656Spjd#define g_topology_assert_not() \ 388125656Spjd do { \ 389125656Spjd sx_assert(&topology_lock, SX_UNLOCKED); \ 390125656Spjd } while (0) 391125656Spjd 392207671Sjh#define g_topology_sleep(chan, timo) \ 393207671Sjh sx_sleep(chan, &topology_lock, 0, "gtopol", timo) 394207671Sjh 395115473Sphk#define DECLARE_GEOM_CLASS(class, name) \ 396115473Sphk static moduledata_t name##_mod = { \ 397115473Sphk #name, g_modevent, &class \ 398115473Sphk }; \ 399115473Sphk DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 400105061Sphk 401260385Sscottlint g_is_geom_thread(struct thread *td); 402260385Sscottl 40395310Sphk#endif /* _KERNEL */ 40492108Sphk 405112709Sphk/* geom_ctl.c */ 406157581Smarcelint gctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len); 407157581Smarcelvoid gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr, int len); 408112709Sphkvoid *gctl_get_param(struct gctl_req *req, const char *param, int *len); 409115624Sphkchar const *gctl_get_asciiparam(struct gctl_req *req, const char *param); 410113892Sphkvoid *gctl_get_paraml(struct gctl_req *req, const char *param, int len); 411162352Spjdint gctl_error(struct gctl_req *req, const char *fmt, ...) __printflike(2, 3); 412115624Sphkstruct g_class *gctl_get_class(struct gctl_req *req, char const *arg); 413115624Sphkstruct g_geom *gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg); 414115624Sphkstruct g_provider *gctl_get_provider(struct gctl_req *req, char const *arg); 415112709Sphk 41695276Sphk#endif /* _GEOM_GEOM_H_ */ 417