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