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