geom.h revision 93090
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom.h 93090 2002-03-24 11:21:41Z phk $
36 */
37
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/sx.h>
41#include <sys/queue.h>
42
43#ifndef _KERNEL
44/*
45 * The GEOM subsystem makes a few concessions in order to be able to run as a
46 * user-land simulation as well as a kernel component.
47 */
48#include <geom_sim.h>
49#endif
50
51struct g_method;
52struct g_geom;
53struct g_consumer;
54struct g_provider;
55struct g_event;
56struct thread;
57struct bio;
58struct sbuf;
59
60LIST_HEAD(method_list_head, g_method);
61TAILQ_HEAD(g_tailq_head, g_geom);
62TAILQ_HEAD(event_tailq_head, g_event);
63
64extern struct g_tailq_head geoms;
65extern struct event_tailq_head events;
66extern int g_debugflags;
67
68
69#define G_METHOD_INITSTUFF { 0, 0 }, { 0 }, 0
70
71typedef struct g_geom * g_create_geom_t (struct g_method *mp,
72    struct g_provider *pp, char *name);
73typedef struct g_geom * g_taste_t (struct g_method *, struct g_provider *,
74    struct thread *tp, int flags);
75#define G_TF_NORMAL		0
76#define G_TF_INSIST		1
77#define G_TF_TRANSPARENT	2
78typedef int g_access_t (struct g_provider *, int, int, int);
79/* XXX: not sure about the thread arg */
80typedef void g_orphan_t (struct g_consumer *, struct thread *);
81
82typedef void g_start_t (struct bio *);
83typedef void g_spoiled_t (struct g_consumer *);
84typedef void g_dumpconf_t (struct sbuf *, char *indent, struct g_geom *,
85    struct g_consumer *, struct g_provider *);
86
87/*
88 * The g_method structure describes a transformation method.  In other words
89 * all BSD disklabel handlers share one g_method, all MBR handlers share
90 * one common g_method and so on.
91 * Certain operations are instantiated on the method, most notably the
92 * taste and create_geom functions.
93 * XXX: should access and orphan go into g_geom ?
94 * XXX: would g_class be a better and less confusing name ?
95 */
96struct g_method {
97	char			*name;
98	g_taste_t		*taste;
99	g_access_t		*access;
100	g_orphan_t		*orphan;
101	g_create_geom_t		*create_geom;
102	LIST_ENTRY(g_method)	method;
103	LIST_HEAD(,g_geom)	geom;
104	struct g_event		*event;
105};
106
107/*
108 * The g_geom is an instance of a g_method.
109 */
110struct g_geom {
111	char			*name;
112	struct g_method		*method;
113	LIST_ENTRY(g_geom)	geom;
114	LIST_HEAD(,g_consumer)	consumer;
115	LIST_HEAD(,g_provider)	provider;
116	TAILQ_ENTRY(g_geom)	geoms;	/* XXX: better name */
117	int			rank;
118	g_start_t		*start;
119	g_spoiled_t		*spoiled;
120	g_dumpconf_t		*dumpconf;
121	void			*softc;
122	struct g_event		*event;
123	unsigned		flags;
124#define	G_GEOM_WITHER		1
125};
126
127/*
128 * The g_bioq is a queue of struct bio's.
129 * XXX: possibly collection point for statistics.
130 * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
131 */
132struct g_bioq {
133	TAILQ_HEAD(, bio)	bio_queue;
134	struct mtx		bio_queue_lock;
135	int			bio_queue_length;
136};
137
138/*
139 * A g_consumer is an attachment point for a g_provider.  One g_consumer
140 * can only be attached to one g_provider, but multiple g_consumers
141 * can be attached to one g_provider.
142 */
143
144struct g_consumer {
145	struct g_geom		*geom;
146	LIST_ENTRY(g_consumer)	consumer;
147	struct g_provider	*provider;
148	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
149	int			acr, acw, ace;
150	struct g_event		*event;
151
152	int			biocount;
153	int			spoiled;
154};
155
156/*
157 * A g_provider is a "logical disk".
158 */
159struct g_provider {
160	char			*name;
161	LIST_ENTRY(g_provider)	provider;
162	struct g_geom		*geom;
163	LIST_HEAD(,g_consumer)	consumers;
164	int			acr, acw, ace;
165	int			error;
166	struct g_event		*event;
167	TAILQ_ENTRY(g_provider)	orphan;
168	int			index;
169};
170
171/*
172 * Various internal actions are tracked by tagging g_event[s] onto
173 * an internal eventqueue.
174 */
175enum g_events {
176	EV_NEW_METHOD,		/* method */
177	EV_NEW_PROVIDER,	/* provider */
178	EV_SPOILED,		/* provider, consumer */
179	EV_LAST
180};
181
182struct g_event {
183	enum g_events 		event;
184	TAILQ_ENTRY(g_event)	events;
185	struct g_method		*method;
186	struct g_geom		*geom;
187	struct g_provider	*provider;
188	struct g_consumer	*consumer;
189};
190
191/* geom_dump.c */
192struct sbuf * g_conf(void);
193struct sbuf * g_conf_specific(struct g_method *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp);
194struct sbuf * g_confdot(void);
195void g_hexdump(void *ptr, int length);
196void g_trace(int level, char *, ...);
197#	define G_T_TOPOLOGY	1
198#	define G_T_BIO		2
199#	define G_T_ACCESS	4
200
201/* geom_enc.c */
202uint32_t g_dec_be2(u_char *p);
203uint32_t g_dec_be4(u_char *p);
204uint32_t g_dec_le2(u_char *p);
205uint32_t g_dec_le4(u_char *p);
206void g_enc_le4(u_char *p, uint32_t u);
207
208/* geom_event.c */
209void g_event_init(void);
210void g_orphan_provider(struct g_provider *pp, int error);
211void g_post_event(enum g_events ev, struct g_method *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp);
212void g_rattle(void);
213void g_run_events(struct thread *tp);
214void g_silence(void);
215
216/* geom_subr.c */
217int g_access_abs(struct g_consumer *cp, int read, int write, int exclusive);
218int g_access_rel(struct g_consumer *cp, int read, int write, int exclusive);
219void g_add_method(struct g_method *mp);
220int g_attach(struct g_consumer *cp, struct g_provider *pp);
221struct g_geom *g_create_geomf(char *method, struct g_provider *, char *fmt, ...);
222void g_destroy_consumer(struct g_consumer *cp);
223void g_destroy_geom(struct g_geom *pp);
224void g_destroy_provider(struct g_provider *pp);
225void g_dettach(struct g_consumer *cp);
226void g_error_provider(struct g_provider *pp, int error);
227int g_haveattr(struct bio *bp, char *attribute, void *val, int len);
228int g_haveattr_int(struct bio *bp, char *attribute, int val);
229int g_haveattr_off_t(struct bio *bp, char *attribute, off_t val);
230struct g_geom * g_insert_geom(char *method, struct g_consumer *cp);
231extern struct method_list_head g_methods;
232struct g_consumer * g_new_consumer(struct g_geom *gp);
233struct g_geom * g_new_geomf(struct g_method *mp, char *fmt, ...);
234struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...);
235void g_spoil(struct g_provider *pp, struct g_consumer *cp);
236int g_std_access(struct g_provider *pp, int dr, int dw, int de);
237void g_std_done(struct bio *bp);
238void g_std_spoiled(struct g_consumer *cp);
239extern char *g_wait_event, *g_wait_sim, *g_wait_up, *g_wait_down;
240
241/* geom_io.c */
242struct bio * g_clone_bio(struct bio *);
243void g_destroy_bio(struct bio *);
244void g_io_deliver(struct bio *bp);
245int g_io_getattr(char *attr, struct g_consumer *cp, int *len, void *ptr, struct thread *tp);
246void g_io_init(void);
247void g_io_request(struct bio *bp, struct g_consumer *cp);
248void g_io_schedule_down(struct thread *tp);
249void g_io_schedule_up(struct thread *tp);
250int g_io_setattr(char *attr, struct g_consumer *cp, int len, void *ptr, struct thread *tp);
251struct bio *g_new_bio(void);
252void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
253
254/* geom_kern.c / geom_kernsim.c */
255void g_init(void);
256
257struct g_ioctl {
258	u_long		cmd;
259	void		*data;
260	int		fflag;
261	struct thread	*td;
262};
263
264#ifdef _KERNEL
265
266MALLOC_DECLARE(M_GEOM);
267
268static __inline void *
269g_malloc(int size, int flags)
270{
271	void *p;
272
273	mtx_lock(&Giant);
274	p = malloc(size, M_GEOM, flags);
275	mtx_unlock(&Giant);
276	return (p);
277}
278
279static __inline void
280g_free(void *ptr)
281{
282	mtx_lock(&Giant);
283	free(ptr, M_GEOM);
284	mtx_unlock(&Giant);
285}
286
287extern struct sx topology_lock;
288#define g_topology_lock() do { mtx_assert(&Giant, MA_NOTOWNED); sx_xlock(&topology_lock); } while (0)
289#define g_topology_unlock() sx_xunlock(&topology_lock)
290#define g_topology_assert() sx_assert(&topology_lock, SX_XLOCKED)
291
292#define DECLARE_GEOM_METHOD(method, name) 	\
293	static void				\
294	name##init(void)			\
295	{					\
296		mtx_unlock(&Giant);		\
297		g_add_method(&method);		\
298		mtx_lock(&Giant);		\
299	}					\
300	SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL);
301
302#endif
303
304