geom.h revision 113876
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 113876 2003-04-22 21:01:46Z 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
4893248Sphkstruct g_class;
4992108Sphkstruct g_geom;
5092108Sphkstruct g_consumer;
5192108Sphkstruct g_provider;
5292108Sphkstruct g_event;
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);
6493248Sphktypedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *,
6593250Sphk    int flags);
6692108Sphk#define G_TF_NORMAL		0
6792108Sphk#define G_TF_INSIST		1
6892108Sphk#define G_TF_TRANSPARENT	2
6992108Sphktypedef int g_access_t (struct g_provider *, int, int, int);
7092108Sphk/* XXX: not sure about the thread arg */
7193250Sphktypedef void g_orphan_t (struct g_consumer *);
7292108Sphk
7392108Sphktypedef void g_start_t (struct bio *);
7492108Sphktypedef void g_spoiled_t (struct g_consumer *);
75107953Sphktypedef void g_dumpconf_t (struct sbuf *, const char *indent, struct g_geom *,
7692108Sphk    struct g_consumer *, struct g_provider *);
7792108Sphk
7892108Sphk/*
7993248Sphk * The g_class structure describes a transformation class.  In other words
8093248Sphk * all BSD disklabel handlers share one g_class, all MBR handlers share
8193248Sphk * one common g_class and so on.
8293248Sphk * Certain operations are instantiated on the class, most notably the
83106518Sphk * taste and config_geom functions.
8492108Sphk */
8593248Sphkstruct g_class {
86107953Sphk	const char		*name;
8792108Sphk	g_taste_t		*taste;
88106518Sphk	g_config_t		*config;
89112709Sphk	g_ctl_create_geom_t	*create_geom;
90112709Sphk	g_ctl_destroy_geom_t	*destroy_geom;
91113876Sphk	g_ctl_config_geom_t	*config_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
102112552Sphk#define G_CLASS_INITIALIZER 	\
103112552Sphk	.class = { 0, 0 },	\
104112552Sphk	.geom = { 0 },		\
105112552Sphk	.event = 0,		\
106112552Sphk	.protect = 0
10793776Sphk
10892108Sphk/*
10993248Sphk * The g_geom is an instance of a g_class.
11092108Sphk */
11192108Sphkstruct g_geom {
11295310Sphk	u_int			protect;
11392108Sphk	char			*name;
11493248Sphk	struct g_class		*class;
11592108Sphk	LIST_ENTRY(g_geom)	geom;
11692108Sphk	LIST_HEAD(,g_consumer)	consumer;
11792108Sphk	LIST_HEAD(,g_provider)	provider;
11892108Sphk	TAILQ_ENTRY(g_geom)	geoms;	/* XXX: better name */
11992108Sphk	int			rank;
12092108Sphk	g_start_t		*start;
12192108Sphk	g_spoiled_t		*spoiled;
12292108Sphk	g_dumpconf_t		*dumpconf;
12393776Sphk	g_access_t		*access;
12493776Sphk	g_orphan_t		*orphan;
12592108Sphk	void			*softc;
12692108Sphk	struct g_event		*event;
12792108Sphk	unsigned		flags;
12892108Sphk#define	G_GEOM_WITHER		1
12992108Sphk};
13092108Sphk
13192108Sphk/*
13292108Sphk * The g_bioq is a queue of struct bio's.
13392108Sphk * XXX: possibly collection point for statistics.
13492108Sphk * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
13592108Sphk */
13692108Sphkstruct g_bioq {
13792108Sphk	TAILQ_HEAD(, bio)	bio_queue;
13892108Sphk	struct mtx		bio_queue_lock;
13992108Sphk	int			bio_queue_length;
14092108Sphk};
14192108Sphk
14292108Sphk/*
14392108Sphk * A g_consumer is an attachment point for a g_provider.  One g_consumer
14492108Sphk * can only be attached to one g_provider, but multiple g_consumers
14592108Sphk * can be attached to one g_provider.
14692108Sphk */
14792108Sphk
14892108Sphkstruct g_consumer {
14995310Sphk	u_int			protect;
15092108Sphk	struct g_geom		*geom;
15192108Sphk	LIST_ENTRY(g_consumer)	consumer;
15292108Sphk	struct g_provider	*provider;
15392108Sphk	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
15492108Sphk	int			acr, acw, ace;
15592108Sphk	struct g_event		*event;
15692108Sphk	int			spoiled;
157112370Sphk	struct devstat		*stat;
158112026Sphk	u_int			nstart, nend;
15992108Sphk};
16092108Sphk
16192108Sphk/*
16292108Sphk * A g_provider is a "logical disk".
16392108Sphk */
16492108Sphkstruct g_provider {
16595310Sphk	u_int			protect;
16692108Sphk	char			*name;
16792108Sphk	LIST_ENTRY(g_provider)	provider;
16892108Sphk	struct g_geom		*geom;
16992108Sphk	LIST_HEAD(,g_consumer)	consumers;
17092108Sphk	int			acr, acw, ace;
17192108Sphk	int			error;
17292108Sphk	struct g_event		*event;
17392108Sphk	TAILQ_ENTRY(g_provider)	orphan;
174107953Sphk	u_int			index;
17593778Sphk	off_t			mediasize;
176105542Sphk	u_int			sectorsize;
177110710Sphk	u_int			stripesize;
178110710Sphk	u_int			stripeoffset;
179112370Sphk	struct devstat		*stat;
180112026Sphk	u_int			nstart, nend;
181110690Sphk	u_int			flags;
182110690Sphk#define G_PF_CANDELETE		0x1
18392108Sphk};
18492108Sphk
185105092Sphk/*
186105092Sphk * This gadget is used by userland to pinpoint a particular instance of
187105092Sphk * something in the kernel.  The name is unreadable on purpose, people
188105092Sphk * should not encounter it directly but use library functions to deal
189105092Sphk * with it.
190105092Sphk * If len is zero, "id" contains a cast of the kernel pointer where the
191105092Sphk * entity is located, (likely derived from the "id=" attribute in the
192105092Sphk * XML config) and the g_id*() functions will validate this before allowing
193105092Sphk * it to be used.
194105092Sphk * If len is non-zero, it is the strlen() of the name which is pointed to
195105092Sphk * by "name".
196105092Sphk */
197105092Sphkstruct geomidorname {
198105092Sphk	u_int len;
199105092Sphk	union {
200105092Sphk		const char	*name;
201105092Sphk		uintptr_t	id;
202105092Sphk	} u;
203105092Sphk};
204105092Sphk
205105947Sphk/* geom_dev.c */
206105947Sphkint g_dev_print(void);
207105947Sphk
20892108Sphk/* geom_dump.c */
20992108Sphkvoid g_hexdump(void *ptr, int length);
210107953Sphkvoid g_trace(int level, const char *, ...);
21192108Sphk#	define G_T_TOPOLOGY	1
21292108Sphk#	define G_T_BIO		2
21392108Sphk#	define G_T_ACCESS	4
21492108Sphk
21593090Sphk
21692108Sphk/* geom_event.c */
217112988Sphktypedef void g_call_me_t(void *, int flag);
218112989Sphk#define EV_CANCEL	1
219112988Sphkint g_call_me(g_call_me_t *func, void *arg, ...);
220112989Sphkvoid g_cancel_event(void *ref);
22192108Sphkvoid g_orphan_provider(struct g_provider *pp, int error);
22298066Sphkvoid g_waitidle(void);
22392108Sphk
22492108Sphk/* geom_subr.c */
225107453Sphkint g_access_abs(struct g_consumer *cp, int nread, int nwrite, int nexcl);
226107453Sphkint g_access_rel(struct g_consumer *cp, int nread, int nwrite, int nexcl);
22793248Sphkvoid g_add_class(struct g_class *mp);
22892108Sphkint g_attach(struct g_consumer *cp, struct g_provider *pp);
22992108Sphkvoid g_destroy_consumer(struct g_consumer *cp);
23092108Sphkvoid g_destroy_geom(struct g_geom *pp);
23192108Sphkvoid g_destroy_provider(struct g_provider *pp);
23298066Sphkvoid g_detach(struct g_consumer *cp);
23392108Sphkvoid g_error_provider(struct g_provider *pp, int error);
23494284Sphkint g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
23594284Sphk#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
236107953Sphkint g_handleattr(struct bio *bp, const char *attribute, void *val, int len);
237107953Sphkint g_handleattr_int(struct bio *bp, const char *attribute, int val);
238107953Sphkint g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val);
23992108Sphkstruct g_consumer * g_new_consumer(struct g_geom *gp);
240107953Sphkstruct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...);
241107953Sphkstruct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...);
24295310Sphkvoid g_sanity(void *ptr);
24392108Sphkvoid g_spoil(struct g_provider *pp, struct g_consumer *cp);
24492108Sphkint g_std_access(struct g_provider *pp, int dr, int dw, int de);
24592108Sphkvoid g_std_done(struct bio *bp);
24692108Sphkvoid g_std_spoiled(struct g_consumer *cp);
247105092Sphkstruct g_class *g_idclass(struct geomidorname *);
248105092Sphkstruct g_geom *g_idgeom(struct geomidorname *);
249105092Sphkstruct g_provider *g_idprovider(struct geomidorname *);
25092108Sphk
251105092Sphk
25292108Sphk/* geom_io.c */
25392108Sphkstruct bio * g_clone_bio(struct bio *);
25492108Sphkvoid g_destroy_bio(struct bio *);
255104195Sphkvoid g_io_deliver(struct bio *bp, int error);
25694283Sphkint g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
25792108Sphkvoid g_io_request(struct bio *bp, struct g_consumer *cp);
25892108Sphkstruct bio *g_new_bio(void);
25992108Sphkvoid * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
260104194Sphkint g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length);
26192108Sphk
26292108Sphk/* geom_kern.c / geom_kernsim.c */
26392108Sphk
264104602Sphk#ifndef _SYS_CONF_H_
265104602Sphktypedef int d_ioctl_t(dev_t dev, u_long cmd, caddr_t data,
266104602Sphk		      int fflag, struct thread *td);
267104602Sphk#endif
268104602Sphk
26992403Sphkstruct g_ioctl {
27092403Sphk	u_long		cmd;
27192403Sphk	void		*data;
27292403Sphk	int		fflag;
27392403Sphk	struct thread	*td;
274104602Sphk	d_ioctl_t	*func;
275109972Sphk	void		*dev;
27692403Sphk};
27792108Sphk
27892108Sphk#ifdef _KERNEL
27992108Sphk
28095038Sphkstruct g_kerneldump {
28195038Sphk	off_t		offset;
28295038Sphk	off_t		length;
28395038Sphk};
28495038Sphk
28592108SphkMALLOC_DECLARE(M_GEOM);
28692108Sphk
28792108Sphkstatic __inline void *
28892108Sphkg_malloc(int size, int flags)
28992108Sphk{
29092108Sphk	void *p;
29192108Sphk
29292108Sphk	p = malloc(size, M_GEOM, flags);
29395310Sphk	g_sanity(p);
29495310Sphk	/* printf("malloc(%d, %x) -> %p\n", size, flags, p); */
29592108Sphk	return (p);
29692108Sphk}
29792108Sphk
29892108Sphkstatic __inline void
29992108Sphkg_free(void *ptr)
30092108Sphk{
30195310Sphk	g_sanity(ptr);
30295310Sphk	/* printf("free(%p)\n", ptr); */
30392108Sphk	free(ptr, M_GEOM);
30492108Sphk}
30592108Sphk
30692108Sphkextern struct sx topology_lock;
30792108Sphk
30896987Sphk#define g_topology_lock() 					\
30996987Sphk	do {							\
31096987Sphk		mtx_assert(&Giant, MA_NOTOWNED);		\
31196987Sphk		sx_xlock(&topology_lock);			\
31296987Sphk	} while (0)
31396987Sphk
31496987Sphk#define g_topology_unlock()					\
31596987Sphk	do {							\
31696987Sphk		g_sanity(NULL);					\
31796987Sphk		sx_xunlock(&topology_lock);			\
31896987Sphk	} while (0)
31996987Sphk
32096987Sphk#define g_topology_assert()					\
32196987Sphk	do {							\
32296987Sphk		g_sanity(NULL);					\
32396987Sphk		sx_assert(&topology_lock, SX_XLOCKED);		\
32496987Sphk	} while (0)
32596987Sphk
326105061Sphk#define DECLARE_GEOM_CLASS_INIT(class, name, init) 	\
327105124Sjake	SYSINIT(name, SI_SUB_DRIVERS, SI_ORDER_FIRST, init, NULL);
328105061Sphk
32993248Sphk#define DECLARE_GEOM_CLASS(class, name) 	\
33092108Sphk	static void				\
33192108Sphk	name##init(void)			\
33292108Sphk	{					\
33392108Sphk		mtx_unlock(&Giant);		\
33493248Sphk		g_add_class(&class);		\
33592108Sphk		mtx_lock(&Giant);		\
33692108Sphk	}					\
337105061Sphk	DECLARE_GEOM_CLASS_INIT(class, name, name##init);
33892108Sphk
33995310Sphk#endif /* _KERNEL */
34092108Sphk
341112709Sphk/* geom_ctl.c */
342112709Sphkvoid *gctl_get_param(struct gctl_req *req, const char *param, int *len);
343112709Sphkint gctl_error(struct gctl_req *req, const char *errtxt);
344112709Sphk
34595276Sphk#endif /* _GEOM_GEOM_H_ */
346