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