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