geom.h revision 94283
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 94283 2002-04-09 15:12:05Z phk $
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
51struct g_class;
52struct g_geom;
53struct g_consumer;
54struct g_provider;
55struct g_event;
56struct thread;
57struct bio;
58struct sbuf;
59
60
61typedef struct g_geom * g_create_geom_t (struct g_class *mp,
62    struct g_provider *pp, char *name);
63typedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *,
64    int flags);
65#define G_TF_NORMAL		0
66#define G_TF_INSIST		1
67#define G_TF_TRANSPARENT	2
68typedef int g_access_t (struct g_provider *, int, int, int);
69/* XXX: not sure about the thread arg */
70typedef void g_orphan_t (struct g_consumer *);
71
72typedef void g_start_t (struct bio *);
73typedef void g_spoiled_t (struct g_consumer *);
74typedef void g_dumpconf_t (struct sbuf *, char *indent, struct g_geom *,
75    struct g_consumer *, struct g_provider *);
76
77/*
78 * The g_class structure describes a transformation class.  In other words
79 * all BSD disklabel handlers share one g_class, all MBR handlers share
80 * one common g_class and so on.
81 * Certain operations are instantiated on the class, most notably the
82 * taste and create_geom functions.
83 */
84struct g_class {
85	char			*name;
86	g_taste_t		*taste;
87	g_create_geom_t		*create_geom;
88	/*
89	 * The remaning elements are private and classes should use
90	 * the G_CLASS_INITSTUFF macro to initialize them.
91         */
92	LIST_ENTRY(g_class)	class;
93	LIST_HEAD(,g_geom)	geom;
94	struct g_event		*event;
95};
96
97#define G_CLASS_INITSTUFF { 0, 0 }, { 0 }, 0
98
99/*
100 * The g_geom is an instance of a g_class.
101 */
102struct g_geom {
103	char			*name;
104	struct g_class		*class;
105	LIST_ENTRY(g_geom)	geom;
106	LIST_HEAD(,g_consumer)	consumer;
107	LIST_HEAD(,g_provider)	provider;
108	TAILQ_ENTRY(g_geom)	geoms;	/* XXX: better name */
109	int			rank;
110	g_start_t		*start;
111	g_spoiled_t		*spoiled;
112	g_dumpconf_t		*dumpconf;
113	g_access_t		*access;
114	g_orphan_t		*orphan;
115	void			*softc;
116	struct g_event		*event;
117	unsigned		flags;
118#define	G_GEOM_WITHER		1
119};
120
121/*
122 * The g_bioq is a queue of struct bio's.
123 * XXX: possibly collection point for statistics.
124 * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
125 */
126struct g_bioq {
127	TAILQ_HEAD(, bio)	bio_queue;
128	struct mtx		bio_queue_lock;
129	int			bio_queue_length;
130};
131
132/*
133 * A g_consumer is an attachment point for a g_provider.  One g_consumer
134 * can only be attached to one g_provider, but multiple g_consumers
135 * can be attached to one g_provider.
136 */
137
138struct g_consumer {
139	struct g_geom		*geom;
140	LIST_ENTRY(g_consumer)	consumer;
141	struct g_provider	*provider;
142	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
143	int			acr, acw, ace;
144	struct g_event		*event;
145
146	int			biocount;
147	int			spoiled;
148};
149
150/*
151 * A g_provider is a "logical disk".
152 */
153struct g_provider {
154	char			*name;
155	LIST_ENTRY(g_provider)	provider;
156	struct g_geom		*geom;
157	LIST_HEAD(,g_consumer)	consumers;
158	int			acr, acw, ace;
159	int			error;
160	struct g_event		*event;
161	TAILQ_ENTRY(g_provider)	orphan;
162	int			index;
163	off_t			mediasize;
164};
165
166/* geom_dump.c */
167void g_hexdump(void *ptr, int length);
168void g_trace(int level, char *, ...);
169#	define G_T_TOPOLOGY	1
170#	define G_T_BIO		2
171#	define G_T_ACCESS	4
172
173/* geom_enc.c */
174uint32_t g_dec_be2(u_char *p);
175uint32_t g_dec_be4(u_char *p);
176uint32_t g_dec_le2(u_char *p);
177uint32_t g_dec_le4(u_char *p);
178void g_enc_le4(u_char *p, uint32_t u);
179
180/* geom_event.c */
181void g_orphan_provider(struct g_provider *pp, int error);
182void g_rattle(void);
183void g_silence(void);
184
185/* geom_subr.c */
186int g_access_abs(struct g_consumer *cp, int read, int write, int exclusive);
187int g_access_rel(struct g_consumer *cp, int read, int write, int exclusive);
188void g_add_class(struct g_class *mp);
189int g_attach(struct g_consumer *cp, struct g_provider *pp);
190struct g_geom *g_create_geomf(char *class, struct g_provider *, char *fmt, ...);
191void g_destroy_consumer(struct g_consumer *cp);
192void g_destroy_geom(struct g_geom *pp);
193void g_destroy_provider(struct g_provider *pp);
194void g_dettach(struct g_consumer *cp);
195void g_error_provider(struct g_provider *pp, int error);
196int g_haveattr(struct bio *bp, char *attribute, void *val, int len);
197int g_haveattr_int(struct bio *bp, char *attribute, int val);
198int g_haveattr_off_t(struct bio *bp, char *attribute, off_t val);
199struct g_geom * g_insert_geom(char *class, struct g_consumer *cp);
200struct g_consumer * g_new_consumer(struct g_geom *gp);
201struct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...);
202struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...);
203void g_spoil(struct g_provider *pp, struct g_consumer *cp);
204int g_std_access(struct g_provider *pp, int dr, int dw, int de);
205void g_std_done(struct bio *bp);
206void g_std_spoiled(struct g_consumer *cp);
207
208/* geom_io.c */
209struct bio * g_clone_bio(struct bio *);
210void g_destroy_bio(struct bio *);
211void g_io_deliver(struct bio *bp);
212void g_io_fail(struct bio *bp, int error);
213int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
214void g_io_request(struct bio *bp, struct g_consumer *cp);
215int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr);
216struct bio *g_new_bio(void);
217void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
218
219/* geom_kern.c / geom_kernsim.c */
220
221struct g_ioctl {
222	u_long		cmd;
223	void		*data;
224	int		fflag;
225	struct thread	*td;
226};
227
228#ifdef _KERNEL
229
230MALLOC_DECLARE(M_GEOM);
231
232static __inline void *
233g_malloc(int size, int flags)
234{
235	void *p;
236
237	mtx_lock(&Giant);
238	p = malloc(size, M_GEOM, flags);
239	mtx_unlock(&Giant);
240	return (p);
241}
242
243static __inline void
244g_free(void *ptr)
245{
246	mtx_lock(&Giant);
247	free(ptr, M_GEOM);
248	mtx_unlock(&Giant);
249}
250
251extern struct sx topology_lock;
252#define g_topology_lock() do { mtx_assert(&Giant, MA_NOTOWNED); sx_xlock(&topology_lock); } while (0)
253#define g_topology_unlock() sx_xunlock(&topology_lock)
254#define g_topology_assert() sx_assert(&topology_lock, SX_XLOCKED)
255
256#define DECLARE_GEOM_CLASS(class, name) 	\
257	static void				\
258	name##init(void)			\
259	{					\
260		mtx_unlock(&Giant);		\
261		g_add_class(&class);		\
262		mtx_lock(&Giant);		\
263	}					\
264	SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL);
265
266#endif
267
268