geom.h revision 95276
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 95276 2002-04-22 19:54:17Z phk $
36 */
37
38#ifndef _GEOM_GEOM_H_
39#define _GEOM_GEOM_H_
40
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/sx.h>
44#include <sys/queue.h>
45
46#ifndef _KERNEL
47/*
48 * The GEOM subsystem makes a few concessions in order to be able to run as a
49 * user-land simulation as well as a kernel component.
50 */
51#include <geom_sim.h>
52#endif
53
54struct g_class;
55struct g_geom;
56struct g_consumer;
57struct g_provider;
58struct g_event;
59struct thread;
60struct bio;
61struct sbuf;
62
63
64typedef struct g_geom * g_create_geom_t (struct g_class *mp,
65    struct g_provider *pp, char *name);
66typedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *,
67    int flags);
68#define G_TF_NORMAL		0
69#define G_TF_INSIST		1
70#define G_TF_TRANSPARENT	2
71typedef int g_access_t (struct g_provider *, int, int, int);
72/* XXX: not sure about the thread arg */
73typedef void g_orphan_t (struct g_consumer *);
74
75typedef void g_start_t (struct bio *);
76typedef void g_spoiled_t (struct g_consumer *);
77typedef void g_dumpconf_t (struct sbuf *, char *indent, struct g_geom *,
78    struct g_consumer *, struct g_provider *);
79
80/*
81 * The g_class structure describes a transformation class.  In other words
82 * all BSD disklabel handlers share one g_class, all MBR handlers share
83 * one common g_class and so on.
84 * Certain operations are instantiated on the class, most notably the
85 * taste and create_geom functions.
86 */
87struct g_class {
88	char			*name;
89	g_taste_t		*taste;
90	g_create_geom_t		*create_geom;
91	/*
92	 * The remaning elements are private and classes should use
93	 * the G_CLASS_INITSTUFF macro to initialize them.
94         */
95	LIST_ENTRY(g_class)	class;
96	LIST_HEAD(,g_geom)	geom;
97	struct g_event		*event;
98};
99
100#define G_CLASS_INITSTUFF { 0, 0 }, { 0 }, 0
101
102/*
103 * The g_geom is an instance of a g_class.
104 */
105struct g_geom {
106	char			*name;
107	struct g_class		*class;
108	LIST_ENTRY(g_geom)	geom;
109	LIST_HEAD(,g_consumer)	consumer;
110	LIST_HEAD(,g_provider)	provider;
111	TAILQ_ENTRY(g_geom)	geoms;	/* XXX: better name */
112	int			rank;
113	g_start_t		*start;
114	g_spoiled_t		*spoiled;
115	g_dumpconf_t		*dumpconf;
116	g_access_t		*access;
117	g_orphan_t		*orphan;
118	void			*softc;
119	struct g_event		*event;
120	unsigned		flags;
121#define	G_GEOM_WITHER		1
122};
123
124/*
125 * The g_bioq is a queue of struct bio's.
126 * XXX: possibly collection point for statistics.
127 * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
128 */
129struct g_bioq {
130	TAILQ_HEAD(, bio)	bio_queue;
131	struct mtx		bio_queue_lock;
132	int			bio_queue_length;
133};
134
135/*
136 * A g_consumer is an attachment point for a g_provider.  One g_consumer
137 * can only be attached to one g_provider, but multiple g_consumers
138 * can be attached to one g_provider.
139 */
140
141struct g_consumer {
142	struct g_geom		*geom;
143	LIST_ENTRY(g_consumer)	consumer;
144	struct g_provider	*provider;
145	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
146	int			acr, acw, ace;
147	struct g_event		*event;
148
149	int			biocount;
150	int			spoiled;
151};
152
153/*
154 * A g_provider is a "logical disk".
155 */
156struct g_provider {
157	char			*name;
158	LIST_ENTRY(g_provider)	provider;
159	struct g_geom		*geom;
160	LIST_HEAD(,g_consumer)	consumers;
161	int			acr, acw, ace;
162	int			error;
163	struct g_event		*event;
164	TAILQ_ENTRY(g_provider)	orphan;
165	int			index;
166	off_t			mediasize;
167};
168
169/* geom_dump.c */
170void g_hexdump(void *ptr, int length);
171void g_trace(int level, char *, ...);
172#	define G_T_TOPOLOGY	1
173#	define G_T_BIO		2
174#	define G_T_ACCESS	4
175
176/* geom_enc.c */
177uint32_t g_dec_be2(u_char *p);
178uint32_t g_dec_be4(u_char *p);
179uint32_t g_dec_le2(u_char *p);
180uint32_t g_dec_le4(u_char *p);
181void g_enc_le4(u_char *p, uint32_t u);
182
183/* geom_event.c */
184void g_orphan_provider(struct g_provider *pp, int error);
185void g_rattle(void);
186void g_silence(void);
187
188/* geom_subr.c */
189int g_access_abs(struct g_consumer *cp, int read, int write, int exclusive);
190int g_access_rel(struct g_consumer *cp, int read, int write, int exclusive);
191void g_add_class(struct g_class *mp);
192int g_attach(struct g_consumer *cp, struct g_provider *pp);
193struct g_geom *g_create_geomf(char *class, struct g_provider *, char *fmt, ...);
194void g_destroy_consumer(struct g_consumer *cp);
195void g_destroy_geom(struct g_geom *pp);
196void g_destroy_provider(struct g_provider *pp);
197void g_dettach(struct g_consumer *cp);
198void g_error_provider(struct g_provider *pp, int error);
199int g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
200#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
201int g_haveattr(struct bio *bp, char *attribute, void *val, int len);
202int g_haveattr_int(struct bio *bp, char *attribute, int val);
203int g_haveattr_off_t(struct bio *bp, char *attribute, off_t val);
204struct g_geom * g_insert_geom(char *class, struct g_consumer *cp);
205struct g_consumer * g_new_consumer(struct g_geom *gp);
206struct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...);
207struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...);
208void g_spoil(struct g_provider *pp, struct g_consumer *cp);
209int g_std_access(struct g_provider *pp, int dr, int dw, int de);
210void g_std_done(struct bio *bp);
211void g_std_spoiled(struct g_consumer *cp);
212
213/* geom_io.c */
214struct bio * g_clone_bio(struct bio *);
215void g_destroy_bio(struct bio *);
216void g_io_deliver(struct bio *bp);
217void g_io_fail(struct bio *bp, int error);
218int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
219void g_io_request(struct bio *bp, struct g_consumer *cp);
220int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr);
221struct bio *g_new_bio(void);
222void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
223
224/* geom_kern.c / geom_kernsim.c */
225
226struct g_ioctl {
227	u_long		cmd;
228	void		*data;
229	int		fflag;
230	struct thread	*td;
231};
232
233#ifdef _KERNEL
234
235struct g_kerneldump {
236	off_t		offset;
237	off_t		length;
238};
239
240MALLOC_DECLARE(M_GEOM);
241
242static __inline void *
243g_malloc(int size, int flags)
244{
245	void *p;
246
247	mtx_lock(&Giant);
248	p = malloc(size, M_GEOM, flags);
249	mtx_unlock(&Giant);
250	return (p);
251}
252
253static __inline void
254g_free(void *ptr)
255{
256	mtx_lock(&Giant);
257	free(ptr, M_GEOM);
258	mtx_unlock(&Giant);
259}
260
261extern struct sx topology_lock;
262#define g_topology_lock() do { mtx_assert(&Giant, MA_NOTOWNED); sx_xlock(&topology_lock); } while (0)
263#define g_topology_unlock() sx_xunlock(&topology_lock)
264#define g_topology_assert() sx_assert(&topology_lock, SX_XLOCKED)
265
266#define DECLARE_GEOM_CLASS(class, name) 	\
267	static void				\
268	name##init(void)			\
269	{					\
270		mtx_unlock(&Giant);		\
271		g_add_class(&class);		\
272		mtx_lock(&Giant);		\
273	}					\
274	SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL);
275
276#endif
277
278#endif /* _GEOM_GEOM_H_ */
279