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/conf.h>
47#include <sys/module.h>
48
49struct g_class;
50struct g_geom;
51struct g_consumer;
52struct g_provider;
53struct g_stat;
54struct thread;
55struct bio;
56struct sbuf;
57struct gctl_req;
58struct g_configargs;
59
60typedef int g_config_t (struct g_configargs *ca);
61typedef void g_ctl_req_t (struct gctl_req *, struct g_class *cp, char const *verb);
62typedef int g_ctl_create_geom_t (struct gctl_req *, struct g_class *cp, struct g_provider *pp);
63typedef int g_ctl_destroy_geom_t (struct gctl_req *, struct g_class *cp, struct g_geom *gp);
64typedef int g_ctl_config_geom_t (struct gctl_req *, struct g_geom *gp, const char *verb);
65typedef void g_init_t (struct g_class *mp);
66typedef void g_fini_t (struct g_class *mp);
67typedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *, int flags);
68typedef int g_ioctl_t(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td);
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_attrchanged_t (struct g_consumer *, const char *attr);
79typedef void g_provgone_t (struct g_provider *);
80typedef void g_dumpconf_t (struct sbuf *, const char *indent, struct g_geom *,
81    struct g_consumer *, struct g_provider *);
82
83/*
84 * The g_class structure describes a transformation class.  In other words
85 * all BSD disklabel handlers share one g_class, all MBR handlers share
86 * one common g_class and so on.
87 * Certain operations are instantiated on the class, most notably the
88 * taste and config_geom functions.
89 */
90struct g_class {
91	const char		*name;
92	u_int			version;
93	u_int			spare0;
94	g_taste_t		*taste;
95	g_config_t		*config;
96	g_ctl_req_t		*ctlreq;
97	g_init_t		*init;
98	g_fini_t		*fini;
99	g_ctl_destroy_geom_t	*destroy_geom;
100	/*
101	 * Default values for geom methods
102	 */
103	g_start_t		*start;
104	g_spoiled_t		*spoiled;
105	g_attrchanged_t		*attrchanged;
106	g_dumpconf_t		*dumpconf;
107	g_access_t		*access;
108	g_orphan_t		*orphan;
109	g_ioctl_t		*ioctl;
110	g_provgone_t		*providergone;
111	void			*spare2;
112	/*
113	 * The remaining elements are private
114	 */
115	LIST_ENTRY(g_class)	class;
116	LIST_HEAD(,g_geom)	geom;
117};
118
119#define G_VERSION_00	0x19950323
120#define G_VERSION_01	0x20041207	/* add fflag to g_ioctl_t */
121#define G_VERSION	G_VERSION_01
122
123/*
124 * The g_geom is an instance of a g_class.
125 */
126struct g_geom {
127	char			*name;
128	struct g_class		*class;
129	LIST_ENTRY(g_geom)	geom;
130	LIST_HEAD(,g_consumer)	consumer;
131	LIST_HEAD(,g_provider)	provider;
132	TAILQ_ENTRY(g_geom)	geoms;	/* XXX: better name */
133	int			rank;
134	g_start_t		*start;
135	g_spoiled_t		*spoiled;
136	g_attrchanged_t		*attrchanged;
137	g_dumpconf_t		*dumpconf;
138	g_access_t		*access;
139	g_orphan_t		*orphan;
140	g_ioctl_t		*ioctl;
141	g_provgone_t		*providergone;
142	void			*spare1;
143	void			*softc;
144	unsigned		flags;
145#define	G_GEOM_WITHER		1
146#define	G_GEOM_VOLATILE_BIO	2
147};
148
149/*
150 * The g_bioq is a queue of struct bio's.
151 * XXX: possibly collection point for statistics.
152 * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
153 */
154struct g_bioq {
155	TAILQ_HEAD(, bio)	bio_queue;
156	struct mtx		bio_queue_lock;
157	int			bio_queue_length;
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	struct g_geom		*geom;
168	LIST_ENTRY(g_consumer)	consumer;
169	struct g_provider	*provider;
170	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
171	int			acr, acw, ace;
172	int			flags;
173#define G_CF_SPOILED		0x1
174#define G_CF_ORPHAN		0x4
175	struct devstat		*stat;
176	u_int			nstart, nend;
177
178	/* Two fields for the implementing class to use */
179	void			*private;
180	u_int			index;
181};
182
183/*
184 * A g_provider is a "logical disk".
185 */
186struct g_provider {
187	char			*name;
188	LIST_ENTRY(g_provider)	provider;
189	struct g_geom		*geom;
190	LIST_HEAD(,g_consumer)	consumers;
191	int			acr, acw, ace;
192	int			error;
193	TAILQ_ENTRY(g_provider)	orphan;
194	off_t			mediasize;
195	u_int			sectorsize;
196	u_int			stripesize;
197	u_int			stripeoffset;
198	struct devstat		*stat;
199	u_int			nstart, nend;
200	u_int			flags;
201#define G_PF_CANDELETE		0x1
202#define G_PF_WITHER		0x2
203#define G_PF_ORPHAN		0x4
204#define	G_PF_ACCEPT_UNMAPPED	0x8
205
206	/* Two fields for the implementing class to use */
207	void			*private;
208	u_int			index;
209};
210
211/*
212 * Descriptor of a classifier. We can register a function and
213 * an argument, which is called by g_io_request() on bio's
214 * that are not previously classified.
215 */
216struct g_classifier_hook {
217	TAILQ_ENTRY(g_classifier_hook) link;
218	int			(*func)(void *arg, struct bio *bp);
219	void			*arg;
220};
221
222/* BIO_GETATTR("GEOM::setstate") argument values. */
223#define G_STATE_FAILED		0
224#define G_STATE_REBUILD		1
225#define G_STATE_RESYNC		2
226#define G_STATE_ACTIVE		3
227
228/* geom_dev.c */
229struct cdev;
230void g_dev_print(void);
231void g_dev_physpath_changed(void);
232struct g_provider *g_dev_getprovider(struct cdev *dev);
233
234/* geom_dump.c */
235void g_trace(int level, const char *, ...);
236#	define G_T_TOPOLOGY	1
237#	define G_T_BIO		2
238#	define G_T_ACCESS	4
239
240
241/* geom_event.c */
242typedef void g_event_t(void *, int flag);
243#define EV_CANCEL	1
244int g_post_event(g_event_t *func, void *arg, int flag, ...);
245int g_waitfor_event(g_event_t *func, void *arg, int flag, ...);
246void g_cancel_event(void *ref);
247int g_attr_changed(struct g_provider *pp, const char *attr, int flag);
248int g_media_changed(struct g_provider *pp, int flag);
249int g_media_gone(struct g_provider *pp, int flag);
250void g_orphan_provider(struct g_provider *pp, int error);
251void g_waitidlelock(void);
252
253/* geom_subr.c */
254int g_access(struct g_consumer *cp, int nread, int nwrite, int nexcl);
255int g_attach(struct g_consumer *cp, struct g_provider *pp);
256int g_compare_names(const char *namea, const char *nameb);
257void g_destroy_consumer(struct g_consumer *cp);
258void g_destroy_geom(struct g_geom *pp);
259void g_destroy_provider(struct g_provider *pp);
260void g_detach(struct g_consumer *cp);
261void g_error_provider(struct g_provider *pp, int error);
262struct g_provider *g_provider_by_name(char const *arg);
263int g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
264#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
265int g_handleattr(struct bio *bp, const char *attribute, const void *val,
266    int len);
267int g_handleattr_int(struct bio *bp, const char *attribute, int val);
268int g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val);
269int g_handleattr_str(struct bio *bp, const char *attribute, const char *str);
270struct g_consumer * g_new_consumer(struct g_geom *gp);
271struct g_geom * g_new_geomf(struct g_class *mp, const char *fmt, ...)
272    __printflike(2, 3);
273struct g_provider * g_new_providerf(struct g_geom *gp, const char *fmt, ...)
274    __printflike(2, 3);
275int g_retaste(struct g_class *mp);
276void g_spoil(struct g_provider *pp, struct g_consumer *cp);
277int g_std_access(struct g_provider *pp, int dr, int dw, int de);
278void g_std_done(struct bio *bp);
279void g_std_spoiled(struct g_consumer *cp);
280void g_wither_geom(struct g_geom *gp, int error);
281void g_wither_geom_close(struct g_geom *gp, int error);
282void g_wither_provider(struct g_provider *pp, int error);
283
284#if defined(DIAGNOSTIC) || defined(DDB)
285int g_valid_obj(void const *ptr);
286#endif
287#ifdef DIAGNOSTIC
288#define G_VALID_CLASS(foo) \
289    KASSERT(g_valid_obj(foo) == 1, ("%p is not a g_class", foo))
290#define G_VALID_GEOM(foo) \
291    KASSERT(g_valid_obj(foo) == 2, ("%p is not a g_geom", foo))
292#define G_VALID_CONSUMER(foo) \
293    KASSERT(g_valid_obj(foo) == 3, ("%p is not a g_consumer", foo))
294#define G_VALID_PROVIDER(foo) \
295    KASSERT(g_valid_obj(foo) == 4, ("%p is not a g_provider", foo))
296#else
297#define G_VALID_CLASS(foo) do { } while (0)
298#define G_VALID_GEOM(foo) do { } while (0)
299#define G_VALID_CONSUMER(foo) do { } while (0)
300#define G_VALID_PROVIDER(foo) do { } while (0)
301#endif
302
303int g_modevent(module_t, int, void *);
304
305/* geom_io.c */
306struct bio * g_clone_bio(struct bio *);
307struct bio * g_duplicate_bio(struct bio *);
308void g_destroy_bio(struct bio *);
309void g_io_deliver(struct bio *bp, int error);
310int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
311int g_io_flush(struct g_consumer *cp);
312int g_register_classifier(struct g_classifier_hook *hook);
313void g_unregister_classifier(struct g_classifier_hook *hook);
314void g_io_request(struct bio *bp, struct g_consumer *cp);
315struct bio *g_new_bio(void);
316struct bio *g_alloc_bio(void);
317void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
318int g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length);
319int g_delete_data(struct g_consumer *cp, off_t offset, off_t length);
320void g_print_bio(struct bio *bp);
321
322/* geom_kern.c / geom_kernsim.c */
323
324#ifdef _KERNEL
325
326extern struct sx topology_lock;
327
328struct g_kerneldump {
329	off_t		offset;
330	off_t		length;
331	struct dumperinfo di;
332};
333
334MALLOC_DECLARE(M_GEOM);
335
336static __inline void *
337g_malloc(int size, int flags)
338{
339	void *p;
340
341	p = malloc(size, M_GEOM, flags);
342	return (p);
343}
344
345static __inline void
346g_free(void *ptr)
347{
348
349#ifdef DIAGNOSTIC
350	if (sx_xlocked(&topology_lock)) {
351		KASSERT(g_valid_obj(ptr) == 0,
352		    ("g_free(%p) of live object, type %d", ptr,
353		    g_valid_obj(ptr)));
354	}
355#endif
356	free(ptr, M_GEOM);
357}
358
359#define g_topology_lock() 					\
360	do {							\
361		mtx_assert(&Giant, MA_NOTOWNED);		\
362		sx_xlock(&topology_lock);			\
363	} while (0)
364
365#define g_topology_try_lock()	sx_try_xlock(&topology_lock)
366
367#define g_topology_unlock()					\
368	do {							\
369		sx_xunlock(&topology_lock);			\
370	} while (0)
371
372#define g_topology_assert()					\
373	do {							\
374		sx_assert(&topology_lock, SX_XLOCKED);		\
375	} while (0)
376
377#define g_topology_assert_not()					\
378	do {							\
379		sx_assert(&topology_lock, SX_UNLOCKED);		\
380	} while (0)
381
382#define g_topology_sleep(chan, timo)				\
383	sx_sleep(chan, &topology_lock, 0, "gtopol", timo)
384
385#define DECLARE_GEOM_CLASS(class, name) 			\
386	static moduledata_t name##_mod = {			\
387		#name, g_modevent, &class			\
388	};							\
389	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
390
391#endif /* _KERNEL */
392
393/* geom_ctl.c */
394int gctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len);
395void gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr, int len);
396void *gctl_get_param(struct gctl_req *req, const char *param, int *len);
397char const *gctl_get_asciiparam(struct gctl_req *req, const char *param);
398void *gctl_get_paraml(struct gctl_req *req, const char *param, int len);
399int gctl_error(struct gctl_req *req, const char *fmt, ...) __printflike(2, 3);
400struct g_class *gctl_get_class(struct gctl_req *req, char const *arg);
401struct g_geom *gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg);
402struct g_provider *gctl_get_provider(struct gctl_req *req, char const *arg);
403
404#endif /* _GEOM_GEOM_H_ */
405