xref: /freebsd/sys/geom/geom.h (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
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$
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 
51 struct g_method;
52 struct g_geom;
53 struct g_consumer;
54 struct g_provider;
55 struct g_event;
56 struct thread;
57 struct bio;
58 struct sbuf;
59 
60 LIST_HEAD(method_list_head, g_method);
61 TAILQ_HEAD(g_tailq_head, g_geom);
62 TAILQ_HEAD(event_tailq_head, g_event);
63 
64 extern struct g_tailq_head geoms;
65 extern struct event_tailq_head events;
66 extern int g_debugflags;
67 
68 
69 #define G_METHOD_INITSTUFF { 0, 0 }, { 0 }, 0
70 
71 typedef struct g_geom * g_create_geom_t (struct g_method *mp,
72     struct g_provider *pp, char *name);
73 typedef 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
78 typedef int g_access_t (struct g_provider *, int, int, int);
79 /* XXX: not sure about the thread arg */
80 typedef void g_orphan_t (struct g_consumer *, struct thread *);
81 
82 typedef void g_start_t (struct bio *);
83 typedef void g_spoiled_t (struct g_consumer *);
84 typedef 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  */
96 struct 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  */
110 struct 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  */
132 struct 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 
144 struct 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  */
159 struct 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  */
175 enum g_events {
176 	EV_NEW_METHOD,		/* method */
177 	EV_NEW_PROVIDER,	/* provider */
178 	EV_SPOILED,		/* provider, consumer */
179 	EV_LAST
180 };
181 
182 struct 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 */
192 struct sbuf * g_conf(void);
193 struct sbuf * g_conf_specific(struct g_method *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp);
194 struct sbuf * g_confdot(void);
195 void g_hexdump(void *ptr, int length);
196 void 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_event.c */
202 void g_event_init(void);
203 void g_orphan_provider(struct g_provider *pp, int error);
204 void g_post_event(enum g_events ev, struct g_method *mp, struct g_geom *gp, struct g_provider *pp, struct g_consumer *cp);
205 void g_rattle(void);
206 void g_run_events(struct thread *tp);
207 void g_silence(void);
208 
209 /* geom_subr.c */
210 int g_access_abs(struct g_consumer *cp, int read, int write, int exclusive);
211 int g_access_rel(struct g_consumer *cp, int read, int write, int exclusive);
212 void g_add_method(struct g_method *mp);
213 int g_attach(struct g_consumer *cp, struct g_provider *pp);
214 struct g_geom *g_create_geomf(char *method, struct g_provider *, char *fmt, ...);
215 void g_destroy_consumer(struct g_consumer *cp);
216 void g_destroy_geom(struct g_geom *pp);
217 void g_destroy_provider(struct g_provider *pp);
218 void g_dettach(struct g_consumer *cp);
219 void g_error_provider(struct g_provider *pp, int error);
220 int g_haveattr(struct bio *bp, char *attribute, void *val, int len);
221 int g_haveattr_int(struct bio *bp, char *attribute, int val);
222 int g_haveattr_off_t(struct bio *bp, char *attribute, off_t val);
223 struct g_geom * g_insert_geom(char *method, struct g_consumer *cp);
224 extern struct method_list_head g_methods;
225 struct g_consumer * g_new_consumer(struct g_geom *gp);
226 struct g_geom * g_new_geomf(struct g_method *mp, char *fmt, ...);
227 struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...);
228 void g_spoil(struct g_provider *pp, struct g_consumer *cp);
229 int g_std_access(struct g_provider *pp, int dr, int dw, int de);
230 void g_std_done(struct bio *bp);
231 void g_std_spoiled(struct g_consumer *cp);
232 extern char *g_wait_event, *g_wait_sim, *g_wait_up, *g_wait_down;
233 
234 /* geom_io.c */
235 struct bio * g_clone_bio(struct bio *);
236 void g_destroy_bio(struct bio *);
237 void g_io_deliver(struct bio *bp);
238 int g_io_getattr(char *attr, struct g_consumer *cp, int *len, void *ptr, struct thread *tp);
239 void g_io_init(void);
240 void g_io_request(struct bio *bp, struct g_consumer *cp);
241 void g_io_schedule_down(struct thread *tp);
242 void g_io_schedule_up(struct thread *tp);
243 int g_io_setattr(char *attr, struct g_consumer *cp, int len, void *ptr, struct thread *tp);
244 struct bio *g_new_bio(void);
245 void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
246 
247 /* geom_kern.c / geom_kernsim.c */
248 void g_init(void);
249 
250 struct g_ioctl {
251 	u_long		cmd;
252 	void		*data;
253 	int		fflag;
254 	struct thread	*td;
255 };
256 
257 #ifdef _KERNEL
258 
259 MALLOC_DECLARE(M_GEOM);
260 
261 static __inline void *
262 g_malloc(int size, int flags)
263 {
264 	void *p;
265 
266 	mtx_lock(&Giant);
267 	p = malloc(size, M_GEOM, flags);
268 	mtx_unlock(&Giant);
269 	return (p);
270 }
271 
272 static __inline void
273 g_free(void *ptr)
274 {
275 	mtx_lock(&Giant);
276 	free(ptr, M_GEOM);
277 	mtx_unlock(&Giant);
278 }
279 
280 extern struct sx topology_lock;
281 #define g_topology_lock() do { mtx_assert(&Giant, MA_NOTOWNED); sx_xlock(&topology_lock); } while (0)
282 #define g_topology_unlock() sx_xunlock(&topology_lock)
283 #define g_topology_assert() sx_assert(&topology_lock, SX_XLOCKED)
284 
285 #define DECLARE_GEOM_METHOD(method, name) 	\
286 	static void				\
287 	name##init(void)			\
288 	{					\
289 		mtx_unlock(&Giant);		\
290 		g_add_method(&method);		\
291 		mtx_lock(&Giant);		\
292 	}					\
293 	SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL);
294 
295 #endif
296 
297