xref: /freebsd/sys/geom/geom.h (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 #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 #include <sys/ioccom.h>
46 #include <sys/sbuf.h>
47 
48 #ifdef KERNELSIM
49 /*
50  * The GEOM subsystem makes a few concessions in order to be able to run as a
51  * user-land simulation as well as a kernel component.
52  */
53 #include <geom_sim.h>
54 #endif
55 
56 struct g_class;
57 struct g_geom;
58 struct g_consumer;
59 struct g_provider;
60 struct g_event;
61 struct g_stat;
62 struct thread;
63 struct bio;
64 struct sbuf;
65 struct g_configargs;
66 
67 typedef int g_config_t (struct g_configargs *ca);
68 typedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *,
69     int flags);
70 #define G_TF_NORMAL		0
71 #define G_TF_INSIST		1
72 #define G_TF_TRANSPARENT	2
73 typedef int g_access_t (struct g_provider *, int, int, int);
74 /* XXX: not sure about the thread arg */
75 typedef void g_orphan_t (struct g_consumer *);
76 
77 typedef void g_start_t (struct bio *);
78 typedef void g_spoiled_t (struct g_consumer *);
79 typedef void g_dumpconf_t (struct sbuf *, const char *indent, struct g_geom *,
80     struct g_consumer *, struct g_provider *);
81 
82 /*
83  * The g_class structure describes a transformation class.  In other words
84  * all BSD disklabel handlers share one g_class, all MBR handlers share
85  * one common g_class and so on.
86  * Certain operations are instantiated on the class, most notably the
87  * taste and config_geom functions.
88  */
89 struct g_class {
90 	const char		*name;
91 	g_taste_t		*taste;
92 	g_config_t		*config;
93 	/*
94 	 * The remaning elements are private and classes should use
95 	 * the G_CLASS_INITIALIZER macro to initialize them.
96          */
97 	LIST_ENTRY(g_class)	class;
98 	LIST_HEAD(,g_geom)	geom;
99 	struct g_event		*event;
100 	u_int			protect;
101 };
102 
103 #define G_CLASS_INITIALIZER { 0, 0 }, { 0 }, 0, 0
104 
105 /*
106  * The g_geom is an instance of a g_class.
107  */
108 struct g_geom {
109 	u_int			protect;
110 	char			*name;
111 	struct g_class		*class;
112 	LIST_ENTRY(g_geom)	geom;
113 	LIST_HEAD(,g_consumer)	consumer;
114 	LIST_HEAD(,g_provider)	provider;
115 	TAILQ_ENTRY(g_geom)	geoms;	/* XXX: better name */
116 	int			rank;
117 	g_start_t		*start;
118 	g_spoiled_t		*spoiled;
119 	g_dumpconf_t		*dumpconf;
120 	g_access_t		*access;
121 	g_orphan_t		*orphan;
122 	void			*softc;
123 	struct g_event		*event;
124 	unsigned		flags;
125 #define	G_GEOM_WITHER		1
126 };
127 
128 /*
129  * The g_bioq is a queue of struct bio's.
130  * XXX: possibly collection point for statistics.
131  * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
132  */
133 struct g_bioq {
134 	TAILQ_HEAD(, bio)	bio_queue;
135 	struct mtx		bio_queue_lock;
136 	int			bio_queue_length;
137 };
138 
139 /*
140  * A g_consumer is an attachment point for a g_provider.  One g_consumer
141  * can only be attached to one g_provider, but multiple g_consumers
142  * can be attached to one g_provider.
143  */
144 
145 struct g_consumer {
146 	u_int			protect;
147 	struct g_geom		*geom;
148 	LIST_ENTRY(g_consumer)	consumer;
149 	struct g_provider	*provider;
150 	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
151 	int			acr, acw, ace;
152 	struct g_event		*event;
153 	int			spoiled;
154 	struct g_stat		*stat;
155 };
156 
157 /*
158  * A g_provider is a "logical disk".
159  */
160 struct g_provider {
161 	u_int			protect;
162 	char			*name;
163 	LIST_ENTRY(g_provider)	provider;
164 	struct g_geom		*geom;
165 	LIST_HEAD(,g_consumer)	consumers;
166 	int			acr, acw, ace;
167 	int			error;
168 	struct g_event		*event;
169 	TAILQ_ENTRY(g_provider)	orphan;
170 	u_int			index;
171 	off_t			mediasize;
172 	u_int			sectorsize;
173 	u_int			stripesize;
174 	u_int			stripeoffset;
175 	struct g_stat		*stat;
176 	u_int			flags;
177 #define G_PF_CANDELETE		0x1
178 };
179 
180 /*
181  * This gadget is used by userland to pinpoint a particular instance of
182  * something in the kernel.  The name is unreadable on purpose, people
183  * should not encounter it directly but use library functions to deal
184  * with it.
185  * If len is zero, "id" contains a cast of the kernel pointer where the
186  * entity is located, (likely derived from the "id=" attribute in the
187  * XML config) and the g_id*() functions will validate this before allowing
188  * it to be used.
189  * If len is non-zero, it is the strlen() of the name which is pointed to
190  * by "name".
191  */
192 struct geomidorname {
193 	u_int len;
194 	union {
195 		const char	*name;
196 		uintptr_t	id;
197 	} u;
198 };
199 
200 /* geom_dev.c */
201 int g_dev_print(void);
202 
203 /* geom_dump.c */
204 void g_hexdump(void *ptr, int length);
205 void g_trace(int level, const char *, ...);
206 #	define G_T_TOPOLOGY	1
207 #	define G_T_BIO		2
208 #	define G_T_ACCESS	4
209 
210 
211 /* geom_event.c */
212 typedef void g_call_me_t(void *);
213 int g_call_me(g_call_me_t *func, void *arg);
214 void g_orphan_provider(struct g_provider *pp, int error);
215 void g_waitidle(void);
216 
217 /* geom_subr.c */
218 int g_access_abs(struct g_consumer *cp, int nread, int nwrite, int nexcl);
219 int g_access_rel(struct g_consumer *cp, int nread, int nwrite, int nexcl);
220 void g_add_class(struct g_class *mp);
221 int g_attach(struct g_consumer *cp, struct g_provider *pp);
222 void g_destroy_consumer(struct g_consumer *cp);
223 void g_destroy_geom(struct g_geom *pp);
224 void g_destroy_provider(struct g_provider *pp);
225 void g_detach(struct g_consumer *cp);
226 void g_error_provider(struct g_provider *pp, int error);
227 int g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
228 #define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
229 int g_handleattr(struct bio *bp, const char *attribute, void *val, int len);
230 int g_handleattr_int(struct bio *bp, const char *attribute, int val);
231 int g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val);
232 struct g_geom * g_insert_geom(const char *class, struct g_consumer *cp);
233 struct g_consumer * g_new_consumer(struct g_geom *gp);
234 struct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...);
235 struct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...);
236 void g_sanity(void *ptr);
237 void g_spoil(struct g_provider *pp, struct g_consumer *cp);
238 int g_std_access(struct g_provider *pp, int dr, int dw, int de);
239 void g_std_done(struct bio *bp);
240 void g_std_spoiled(struct g_consumer *cp);
241 struct g_class *g_idclass(struct geomidorname *);
242 struct g_geom *g_idgeom(struct geomidorname *);
243 struct g_provider *g_idprovider(struct geomidorname *);
244 
245 
246 /* geom_io.c */
247 struct bio * g_clone_bio(struct bio *);
248 void g_destroy_bio(struct bio *);
249 void g_io_deliver(struct bio *bp, int error);
250 int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
251 void g_io_request(struct bio *bp, struct g_consumer *cp);
252 int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr);
253 struct bio *g_new_bio(void);
254 void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
255 int g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length);
256 
257 /* geom_kern.c / geom_kernsim.c */
258 
259 #ifndef _SYS_CONF_H_
260 typedef int d_ioctl_t(dev_t dev, u_long cmd, caddr_t data,
261 		      int fflag, struct thread *td);
262 #endif
263 
264 struct g_ioctl {
265 	u_long		cmd;
266 	void		*data;
267 	int		fflag;
268 	struct thread	*td;
269 	d_ioctl_t	*func;
270 	void		*dev;
271 };
272 
273 #ifdef _KERNEL
274 
275 struct g_kerneldump {
276 	off_t		offset;
277 	off_t		length;
278 };
279 
280 MALLOC_DECLARE(M_GEOM);
281 
282 static __inline void *
283 g_malloc(int size, int flags)
284 {
285 	void *p;
286 
287 	p = malloc(size, M_GEOM, flags);
288 	g_sanity(p);
289 	/* printf("malloc(%d, %x) -> %p\n", size, flags, p); */
290 	return (p);
291 }
292 
293 static __inline void
294 g_free(void *ptr)
295 {
296 	g_sanity(ptr);
297 	/* printf("free(%p)\n", ptr); */
298 	free(ptr, M_GEOM);
299 }
300 
301 extern struct sx topology_lock;
302 
303 #define g_topology_lock() 					\
304 	do {							\
305 		mtx_assert(&Giant, MA_NOTOWNED);		\
306 		sx_xlock(&topology_lock);			\
307 	} while (0)
308 
309 #define g_topology_unlock()					\
310 	do {							\
311 		g_sanity(NULL);					\
312 		sx_xunlock(&topology_lock);			\
313 	} while (0)
314 
315 #define g_topology_assert()					\
316 	do {							\
317 		g_sanity(NULL);					\
318 		sx_assert(&topology_lock, SX_XLOCKED);		\
319 	} while (0)
320 
321 #define DECLARE_GEOM_CLASS_INIT(class, name, init) 	\
322 	SYSINIT(name, SI_SUB_DRIVERS, SI_ORDER_FIRST, init, NULL);
323 
324 #define DECLARE_GEOM_CLASS(class, name) 	\
325 	static void				\
326 	name##init(void)			\
327 	{					\
328 		mtx_unlock(&Giant);		\
329 		g_add_class(&class);		\
330 		mtx_lock(&Giant);		\
331 	}					\
332 	DECLARE_GEOM_CLASS_INIT(class, name, name##init);
333 
334 #endif /* _KERNEL */
335 
336 /*
337  * IOCTLS for talking to the geom.ctl device.
338  */
339 
340 /*
341  * This is the structure used internally in the kernel, it is created and
342  * populated by geom_ctl.c.
343  */
344 struct g_configargs {
345 	/* Valid on call */
346 	struct g_class		*class;
347 	struct g_geom		*geom;
348 	struct g_provider	*provider;
349 	u_int			flag;
350 	u_int			len;
351 	void			*ptr;
352 };
353 
354 /*
355  * This is the structure used to communicate with userland.
356  */
357 struct geomconfiggeom {
358 	/* Valid on call */
359 	struct geomidorname	class;
360 	struct geomidorname	geom;
361 	struct geomidorname	provider;
362 	u_int			flag;
363 	u_int			len;
364 	void			*ptr;
365 };
366 
367 #define GEOMCONFIGGEOM _IOW('G',  0, struct geomconfiggeom)
368 
369 #define GCFG_GENERIC0		0x00000000
370 	/*
371 	 * Generic requests suitable for all classes.
372 	 */
373 #define GCFG_CLASS0		0x10000000
374 	/*
375 	 * Class specific verbs.  Allocations in this part of the numberspace
376 	 * can only be done after review and approval of phk@FreeBSD.org.
377 	 * All allocations in this space will be listed in this file.
378 	 */
379 #define GCFG_PRIVATE0		0x20000000
380 	/*
381 	 * Lowest allocation for private flag definitions.
382 	 * If you define you own private "verbs", please express them in
383 	 * your code as (GCFG_PRIVATE0 + somenumber), where somenumber is
384 	 * a magic number in the range [0x0 ... 0xfffffff] chosen the way
385 	 * magic numbers are chosen.  Such allocation SHALL NOT be listed
386 	 * here but SHOULD be listed in some suitable .h file.
387 	 */
388 #define GCFG_RESERVED0		0x30000000
389 #define GCFG_RESERVEDN		0xffffffff
390 	/*
391 	 * This area is reserved for the future.
392 	 */
393 
394 #define GCFG_CREATE		(GCFG_GENERIC0 + 0x0)
395 	/*
396 	 * Request geom construction.
397 	 * ptr/len is class-specific.
398 	 */
399 #define GCFG_DISMANTLE		(GCFG_GENERIC0 + 0x1)
400 	/*
401 	 * Request orderly geom dismantling.
402 	 * ptr/len is class-specific.
403 	 */
404 
405 
406 struct gcfg_magicrw {
407 	off_t	offset;
408 	u_int	len;
409 };
410 
411 #define GCFG_MAGICREAD		(GCFG_GENERIC0 + 0x100)
412 	/*
413 	 * Read of magic spaces.
414 	 * ptr/len is gcfgmagicrw structure followed by bufferspace
415 	 * for data to be read.
416 	 */
417 #define GCFG_MAGICWRITE		(GCFG_GENERIC0 + 0x101)
418 	/*
419 	 * Write of magic spaces.
420 	 * as above, only the other way.
421 	 */
422 
423 
424 /* geom_enc.c */
425 uint16_t g_dec_be2(const u_char *p);
426 uint32_t g_dec_be4(const u_char *p);
427 uint16_t g_dec_le2(const u_char *p);
428 uint32_t g_dec_le4(const u_char *p);
429 uint64_t g_dec_le8(const u_char *p);
430 void g_enc_le2(u_char *p, uint16_t u);
431 void g_enc_le4(u_char *p, uint32_t u);
432 void g_enc_le8(u_char *p, uint64_t u);
433 
434 #endif /* _GEOM_GEOM_H_ */
435