Deleted Added
full compact
geom.h (94284) geom.h (95038)
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 94284 2002-04-09 15:13:42Z phk $
35 * $FreeBSD: head/sys/geom/geom.h 95038 2002-04-19 09:24:12Z 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_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
197#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
198int g_haveattr(struct bio *bp, char *attribute, void *val, int len);
199int g_haveattr_int(struct bio *bp, char *attribute, int val);
200int g_haveattr_off_t(struct bio *bp, char *attribute, off_t val);
201struct g_geom * g_insert_geom(char *class, struct g_consumer *cp);
202struct g_consumer * g_new_consumer(struct g_geom *gp);
203struct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...);
204struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...);
205void g_spoil(struct g_provider *pp, struct g_consumer *cp);
206int g_std_access(struct g_provider *pp, int dr, int dw, int de);
207void g_std_done(struct bio *bp);
208void g_std_spoiled(struct g_consumer *cp);
209
210/* geom_io.c */
211struct bio * g_clone_bio(struct bio *);
212void g_destroy_bio(struct bio *);
213void g_io_deliver(struct bio *bp);
214void g_io_fail(struct bio *bp, int error);
215int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
216void g_io_request(struct bio *bp, struct g_consumer *cp);
217int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr);
218struct bio *g_new_bio(void);
219void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
220
221/* geom_kern.c / geom_kernsim.c */
222
223struct g_ioctl {
224 u_long cmd;
225 void *data;
226 int fflag;
227 struct thread *td;
228};
229
230#ifdef _KERNEL
231
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_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
197#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
198int g_haveattr(struct bio *bp, char *attribute, void *val, int len);
199int g_haveattr_int(struct bio *bp, char *attribute, int val);
200int g_haveattr_off_t(struct bio *bp, char *attribute, off_t val);
201struct g_geom * g_insert_geom(char *class, struct g_consumer *cp);
202struct g_consumer * g_new_consumer(struct g_geom *gp);
203struct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...);
204struct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...);
205void g_spoil(struct g_provider *pp, struct g_consumer *cp);
206int g_std_access(struct g_provider *pp, int dr, int dw, int de);
207void g_std_done(struct bio *bp);
208void g_std_spoiled(struct g_consumer *cp);
209
210/* geom_io.c */
211struct bio * g_clone_bio(struct bio *);
212void g_destroy_bio(struct bio *);
213void g_io_deliver(struct bio *bp);
214void g_io_fail(struct bio *bp, int error);
215int g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
216void g_io_request(struct bio *bp, struct g_consumer *cp);
217int g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr);
218struct bio *g_new_bio(void);
219void * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
220
221/* geom_kern.c / geom_kernsim.c */
222
223struct g_ioctl {
224 u_long cmd;
225 void *data;
226 int fflag;
227 struct thread *td;
228};
229
230#ifdef _KERNEL
231
232struct g_kerneldump {
233 off_t offset;
234 off_t length;
235};
236
232MALLOC_DECLARE(M_GEOM);
233
234static __inline void *
235g_malloc(int size, int flags)
236{
237 void *p;
238
239 mtx_lock(&Giant);
240 p = malloc(size, M_GEOM, flags);
241 mtx_unlock(&Giant);
242 return (p);
243}
244
245static __inline void
246g_free(void *ptr)
247{
248 mtx_lock(&Giant);
249 free(ptr, M_GEOM);
250 mtx_unlock(&Giant);
251}
252
253extern struct sx topology_lock;
254#define g_topology_lock() do { mtx_assert(&Giant, MA_NOTOWNED); sx_xlock(&topology_lock); } while (0)
255#define g_topology_unlock() sx_xunlock(&topology_lock)
256#define g_topology_assert() sx_assert(&topology_lock, SX_XLOCKED)
257
258#define DECLARE_GEOM_CLASS(class, name) \
259 static void \
260 name##init(void) \
261 { \
262 mtx_unlock(&Giant); \
263 g_add_class(&class); \
264 mtx_lock(&Giant); \
265 } \
266 SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL);
267
268#endif
269
237MALLOC_DECLARE(M_GEOM);
238
239static __inline void *
240g_malloc(int size, int flags)
241{
242 void *p;
243
244 mtx_lock(&Giant);
245 p = malloc(size, M_GEOM, flags);
246 mtx_unlock(&Giant);
247 return (p);
248}
249
250static __inline void
251g_free(void *ptr)
252{
253 mtx_lock(&Giant);
254 free(ptr, M_GEOM);
255 mtx_unlock(&Giant);
256}
257
258extern struct sx topology_lock;
259#define g_topology_lock() do { mtx_assert(&Giant, MA_NOTOWNED); sx_xlock(&topology_lock); } while (0)
260#define g_topology_unlock() sx_xunlock(&topology_lock)
261#define g_topology_assert() sx_assert(&topology_lock, SX_XLOCKED)
262
263#define DECLARE_GEOM_CLASS(class, name) \
264 static void \
265 name##init(void) \
266 { \
267 mtx_unlock(&Giant); \
268 g_add_class(&class); \
269 mtx_lock(&Giant); \
270 } \
271 SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL);
272
273#endif
274