geom.h revision 115623
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 115623 2003-06-01 13:30:39Z 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>
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);
61112709Sphktypedef int g_ctl_create_geom_t (struct gctl_req *, struct g_class *cp, struct g_provider *pp);
62112709Sphktypedef int g_ctl_destroy_geom_t (struct gctl_req *, struct g_class *cp, struct g_geom *gp);
63113876Sphktypedef int g_ctl_config_geom_t (struct gctl_req *, struct g_geom *gp, const char *verb);
64115473Sphktypedef void g_init_t (struct g_class *mp);
65115473Sphktypedef void g_fini_t (struct g_class *mp);
6693248Sphktypedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *,
6793250Sphk    int flags);
6892108Sphk#define G_TF_NORMAL		0
6992108Sphk#define G_TF_INSIST		1
7092108Sphk#define G_TF_TRANSPARENT	2
7192108Sphktypedef int g_access_t (struct g_provider *, int, int, int);
7292108Sphk/* XXX: not sure about the thread arg */
7393250Sphktypedef void g_orphan_t (struct g_consumer *);
7492108Sphk
7592108Sphktypedef void g_start_t (struct bio *);
7692108Sphktypedef void g_spoiled_t (struct g_consumer *);
77107953Sphktypedef void g_dumpconf_t (struct sbuf *, const char *indent, struct g_geom *,
7892108Sphk    struct g_consumer *, struct g_provider *);
7992108Sphk
8092108Sphk/*
8193248Sphk * The g_class structure describes a transformation class.  In other words
8293248Sphk * all BSD disklabel handlers share one g_class, all MBR handlers share
8393248Sphk * one common g_class and so on.
8493248Sphk * Certain operations are instantiated on the class, most notably the
85106518Sphk * taste and config_geom functions.
8692108Sphk */
8793248Sphkstruct g_class {
88107953Sphk	const char		*name;
8992108Sphk	g_taste_t		*taste;
90106518Sphk	g_config_t		*config;
91115473Sphk	g_init_t		*init;
92115473Sphk	g_fini_t		*fini;
93112709Sphk	g_ctl_create_geom_t	*create_geom;
94112709Sphk	g_ctl_destroy_geom_t	*destroy_geom;
95113876Sphk	g_ctl_config_geom_t	*config_geom;
9693776Sphk	/*
97115468Sphk	 * The remaining elements are private
98115468Sphk	 */
9993248Sphk	LIST_ENTRY(g_class)	class;
10092108Sphk	LIST_HEAD(,g_geom)	geom;
10195310Sphk	u_int			protect;
10292108Sphk};
10392108Sphk
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	unsigned		flags;
12392108Sphk#define	G_GEOM_WITHER		1
12492108Sphk};
12592108Sphk
12692108Sphk/*
12792108Sphk * The g_bioq is a queue of struct bio's.
12892108Sphk * XXX: possibly collection point for statistics.
12992108Sphk * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
13092108Sphk */
13192108Sphkstruct g_bioq {
13292108Sphk	TAILQ_HEAD(, bio)	bio_queue;
13392108Sphk	struct mtx		bio_queue_lock;
13492108Sphk	int			bio_queue_length;
13592108Sphk};
13692108Sphk
13792108Sphk/*
13892108Sphk * A g_consumer is an attachment point for a g_provider.  One g_consumer
13992108Sphk * can only be attached to one g_provider, but multiple g_consumers
14092108Sphk * can be attached to one g_provider.
14192108Sphk */
14292108Sphk
14392108Sphkstruct g_consumer {
14495310Sphk	u_int			protect;
14592108Sphk	struct g_geom		*geom;
14692108Sphk	LIST_ENTRY(g_consumer)	consumer;
14792108Sphk	struct g_provider	*provider;
14892108Sphk	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
14992108Sphk	int			acr, acw, ace;
15092108Sphk	int			spoiled;
151112370Sphk	struct devstat		*stat;
152112026Sphk	u_int			nstart, nend;
15392108Sphk};
15492108Sphk
15592108Sphk/*
15692108Sphk * A g_provider is a "logical disk".
15792108Sphk */
15892108Sphkstruct g_provider {
15995310Sphk	u_int			protect;
16092108Sphk	char			*name;
16192108Sphk	LIST_ENTRY(g_provider)	provider;
16292108Sphk	struct g_geom		*geom;
16392108Sphk	LIST_HEAD(,g_consumer)	consumers;
16492108Sphk	int			acr, acw, ace;
16592108Sphk	int			error;
16692108Sphk	TAILQ_ENTRY(g_provider)	orphan;
167107953Sphk	u_int			index;
16893778Sphk	off_t			mediasize;
169105542Sphk	u_int			sectorsize;
170110710Sphk	u_int			stripesize;
171110710Sphk	u_int			stripeoffset;
172112370Sphk	struct devstat		*stat;
173112026Sphk	u_int			nstart, nend;
174110690Sphk	u_int			flags;
175110690Sphk#define G_PF_CANDELETE		0x1
17692108Sphk};
17792108Sphk
178105947Sphk/* geom_dev.c */
179105947Sphkint g_dev_print(void);
180105947Sphk
18192108Sphk/* geom_dump.c */
18292108Sphkvoid g_hexdump(void *ptr, int length);
183107953Sphkvoid g_trace(int level, const char *, ...);
18492108Sphk#	define G_T_TOPOLOGY	1
18592108Sphk#	define G_T_BIO		2
18692108Sphk#	define G_T_ACCESS	4
18792108Sphk
18893090Sphk
18992108Sphk/* geom_event.c */
190113937Sphktypedef void g_event_t(void *, int flag);
191112989Sphk#define EV_CANCEL	1
192113937Sphkint g_post_event(g_event_t *func, void *arg, int flag, ...);
193113940Sphkint g_waitfor_event(g_event_t *func, void *arg, int flag, ...);
194112989Sphkvoid g_cancel_event(void *ref);
19592108Sphkvoid g_orphan_provider(struct g_provider *pp, int error);
19698066Sphkvoid g_waitidle(void);
19792108Sphk
19892108Sphk/* geom_subr.c */
199107453Sphkint g_access_abs(struct g_consumer *cp, int nread, int nwrite, int nexcl);
200107453Sphkint g_access_rel(struct g_consumer *cp, int nread, int nwrite, int nexcl);
20192108Sphkint g_attach(struct g_consumer *cp, struct g_provider *pp);
20292108Sphkvoid g_destroy_consumer(struct g_consumer *cp);
20392108Sphkvoid g_destroy_geom(struct g_geom *pp);
20492108Sphkvoid g_destroy_provider(struct g_provider *pp);
20598066Sphkvoid g_detach(struct g_consumer *cp);
20692108Sphkvoid g_error_provider(struct g_provider *pp, int error);
20794284Sphkint g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
20894284Sphk#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
209107953Sphkint g_handleattr(struct bio *bp, const char *attribute, void *val, int len);
210107953Sphkint g_handleattr_int(struct bio *bp, const char *attribute, int val);
211107953Sphkint g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val);
21292108Sphkstruct g_consumer * g_new_consumer(struct g_geom *gp);
213107953Sphkstruct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...);
214107953Sphkstruct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...);
215115623Sphkvoid g_sanity(void const *ptr);
21692108Sphkvoid g_spoil(struct g_provider *pp, struct g_consumer *cp);
21792108Sphkint g_std_access(struct g_provider *pp, int dr, int dw, int de);
21892108Sphkvoid g_std_done(struct bio *bp);
21992108Sphkvoid g_std_spoiled(struct g_consumer *cp);
220114495Sphkvoid g_wither_geom(struct g_geom *gp, int error);
22192108Sphk
222115473Sphkint g_modevent(module_t, int, void *);
223115473Sphk
22492108Sphk/* geom_io.c */
22592108Sphkstruct bio * g_clone_bio(struct bio *);
22692108Sphkvoid g_destroy_bio(struct bio *);
227104195Sphkvoid g_io_deliver(struct bio *bp, int error);
22894283Sphkint g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
22992108Sphkvoid g_io_request(struct bio *bp, struct g_consumer *cp);
23092108Sphkstruct bio *g_new_bio(void);
23192108Sphkvoid * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
232104194Sphkint g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length);
23392108Sphk
23492108Sphk/* geom_kern.c / geom_kernsim.c */
23592108Sphk
236104602Sphk#ifndef _SYS_CONF_H_
237104602Sphktypedef int d_ioctl_t(dev_t dev, u_long cmd, caddr_t data,
238104602Sphk		      int fflag, struct thread *td);
239104602Sphk#endif
240104602Sphk
24192403Sphkstruct g_ioctl {
24292403Sphk	u_long		cmd;
24392403Sphk	void		*data;
24492403Sphk	int		fflag;
24592403Sphk	struct thread	*td;
246104602Sphk	d_ioctl_t	*func;
247109972Sphk	void		*dev;
24892403Sphk};
24992108Sphk
25092108Sphk#ifdef _KERNEL
25192108Sphk
25295038Sphkstruct g_kerneldump {
25395038Sphk	off_t		offset;
25495038Sphk	off_t		length;
25595038Sphk};
25695038Sphk
25792108SphkMALLOC_DECLARE(M_GEOM);
25892108Sphk
25992108Sphkstatic __inline void *
26092108Sphkg_malloc(int size, int flags)
26192108Sphk{
26292108Sphk	void *p;
26392108Sphk
26492108Sphk	p = malloc(size, M_GEOM, flags);
26595310Sphk	g_sanity(p);
26695310Sphk	/* printf("malloc(%d, %x) -> %p\n", size, flags, p); */
26792108Sphk	return (p);
26892108Sphk}
26992108Sphk
27092108Sphkstatic __inline void
27192108Sphkg_free(void *ptr)
27292108Sphk{
27395310Sphk	g_sanity(ptr);
27495310Sphk	/* printf("free(%p)\n", ptr); */
27592108Sphk	free(ptr, M_GEOM);
27692108Sphk}
27792108Sphk
27892108Sphkextern struct sx topology_lock;
27992108Sphk
28096987Sphk#define g_topology_lock() 					\
28196987Sphk	do {							\
28296987Sphk		mtx_assert(&Giant, MA_NOTOWNED);		\
28396987Sphk		sx_xlock(&topology_lock);			\
28496987Sphk	} while (0)
28596987Sphk
28696987Sphk#define g_topology_unlock()					\
28796987Sphk	do {							\
28896987Sphk		g_sanity(NULL);					\
28996987Sphk		sx_xunlock(&topology_lock);			\
29096987Sphk	} while (0)
29196987Sphk
29296987Sphk#define g_topology_assert()					\
29396987Sphk	do {							\
29496987Sphk		g_sanity(NULL);					\
29596987Sphk		sx_assert(&topology_lock, SX_XLOCKED);		\
29696987Sphk	} while (0)
29796987Sphk
298115473Sphk#define DECLARE_GEOM_CLASS(class, name) 			\
299115473Sphk	static moduledata_t name##_mod = {			\
300115473Sphk		#name, g_modevent, &class			\
301115473Sphk	};							\
302115473Sphk	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
303105061Sphk
30495310Sphk#endif /* _KERNEL */
30592108Sphk
306112709Sphk/* geom_ctl.c */
307114670Sphkint gctl_set_param(struct gctl_req *req, const char *param, void *ptr, int len);
308112709Sphkvoid *gctl_get_param(struct gctl_req *req, const char *param, int *len);
309113892Sphkvoid *gctl_get_paraml(struct gctl_req *req, const char *param, int len);
310113892Sphkint gctl_error(struct gctl_req *req, const char *fmt, ...);
311112709Sphk
31295276Sphk#endif /* _GEOM_GEOM_H_ */
313