geom.h revision 95310
192108Sphk/*-
292108Sphk * Copyright (c) 2002 Poul-Henning Kamp
392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc.
492108Sphk * All rights reserved.
592108Sphk *
692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp
792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc.
892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
992108Sphk * DARPA CHATS research program.
1092108Sphk *
1192108Sphk * Redistribution and use in source and binary forms, with or without
1292108Sphk * modification, are permitted provided that the following conditions
1392108Sphk * are met:
1492108Sphk * 1. Redistributions of source code must retain the above copyright
1592108Sphk *    notice, this list of conditions and the following disclaimer.
1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright
1792108Sphk *    notice, this list of conditions and the following disclaimer in the
1892108Sphk *    documentation and/or other materials provided with the distribution.
1992108Sphk * 3. The names of the authors may not be used to endorse or promote
2092108Sphk *    products derived from this software without specific prior written
2192108Sphk *    permission.
2292108Sphk *
2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2692108Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3392108Sphk * SUCH DAMAGE.
3492108Sphk *
3592108Sphk * $FreeBSD: head/sys/geom/geom.h 95310 2002-04-23 11:48:45Z phk $
3692108Sphk */
3792108Sphk
3895276Sphk#ifndef _GEOM_GEOM_H_
3995276Sphk#define _GEOM_GEOM_H_
4095276Sphk
4192108Sphk#include <sys/lock.h>
4292108Sphk#include <sys/mutex.h>
4392108Sphk#include <sys/sx.h>
4492108Sphk#include <sys/queue.h>
4592108Sphk
4692108Sphk#ifndef _KERNEL
4792108Sphk/*
4892108Sphk * The GEOM subsystem makes a few concessions in order to be able to run as a
4992108Sphk * user-land simulation as well as a kernel component.
5092108Sphk */
5192514Sphk#include <geom_sim.h>
5292108Sphk#endif
5392108Sphk
5493248Sphkstruct g_class;
5592108Sphkstruct g_geom;
5692108Sphkstruct g_consumer;
5792108Sphkstruct g_provider;
5892108Sphkstruct g_event;
5992108Sphkstruct thread;
6092108Sphkstruct bio;
6192108Sphkstruct sbuf;
6292108Sphk
6392108Sphk
6493248Sphktypedef struct g_geom * g_create_geom_t (struct g_class *mp,
6592108Sphk    struct g_provider *pp, char *name);
6693248Sphktypedef struct g_geom * g_taste_t (struct g_class *, struct g_provider *,
6793250Sphk    int flags);
6892108Sphk#define G_TF_NORMAL		0
6992108Sphk#define G_TF_INSIST		1
7092108Sphk#define G_TF_TRANSPARENT	2
7192108Sphktypedef int g_access_t (struct g_provider *, int, int, int);
7292108Sphk/* XXX: not sure about the thread arg */
7393250Sphktypedef void g_orphan_t (struct g_consumer *);
7492108Sphk
7592108Sphktypedef void g_start_t (struct bio *);
7692108Sphktypedef void g_spoiled_t (struct g_consumer *);
7792108Sphktypedef void g_dumpconf_t (struct sbuf *, char *indent, struct g_geom *,
7892108Sphk    struct g_consumer *, struct g_provider *);
7992108Sphk
8092108Sphk/*
8193248Sphk * The g_class structure describes a transformation class.  In other words
8293248Sphk * all BSD disklabel handlers share one g_class, all MBR handlers share
8393248Sphk * one common g_class and so on.
8493248Sphk * Certain operations are instantiated on the class, most notably the
8592108Sphk * taste and create_geom functions.
8692108Sphk */
8793248Sphkstruct g_class {
8892108Sphk	char			*name;
8992108Sphk	g_taste_t		*taste;
9092108Sphk	g_create_geom_t		*create_geom;
9193776Sphk	/*
9293776Sphk	 * The remaning elements are private and classes should use
9393776Sphk	 * the G_CLASS_INITSTUFF macro to initialize them.
9493776Sphk         */
9593248Sphk	LIST_ENTRY(g_class)	class;
9692108Sphk	LIST_HEAD(,g_geom)	geom;
9792108Sphk	struct g_event		*event;
9895310Sphk	u_int			protect;
9992108Sphk};
10092108Sphk
10195310Sphk#define G_CLASS_INITSTUFF { 0, 0 }, { 0 }, 0, 0
10293776Sphk
10392108Sphk/*
10493248Sphk * The g_geom is an instance of a g_class.
10592108Sphk */
10692108Sphkstruct g_geom {
10795310Sphk	u_int			protect;
10892108Sphk	char			*name;
10993248Sphk	struct g_class		*class;
11092108Sphk	LIST_ENTRY(g_geom)	geom;
11192108Sphk	LIST_HEAD(,g_consumer)	consumer;
11292108Sphk	LIST_HEAD(,g_provider)	provider;
11392108Sphk	TAILQ_ENTRY(g_geom)	geoms;	/* XXX: better name */
11492108Sphk	int			rank;
11592108Sphk	g_start_t		*start;
11692108Sphk	g_spoiled_t		*spoiled;
11792108Sphk	g_dumpconf_t		*dumpconf;
11893776Sphk	g_access_t		*access;
11993776Sphk	g_orphan_t		*orphan;
12092108Sphk	void			*softc;
12192108Sphk	struct g_event		*event;
12292108Sphk	unsigned		flags;
12392108Sphk#define	G_GEOM_WITHER		1
12492108Sphk};
12592108Sphk
12692108Sphk/*
12792108Sphk * The g_bioq is a queue of struct bio's.
12892108Sphk * XXX: possibly collection point for statistics.
12992108Sphk * XXX: should (possibly) be collapsed with sys/bio.h::bio_queue_head.
13092108Sphk */
13192108Sphkstruct g_bioq {
13292108Sphk	TAILQ_HEAD(, bio)	bio_queue;
13392108Sphk	struct mtx		bio_queue_lock;
13492108Sphk	int			bio_queue_length;
13592108Sphk};
13692108Sphk
13792108Sphk/*
13892108Sphk * A g_consumer is an attachment point for a g_provider.  One g_consumer
13992108Sphk * can only be attached to one g_provider, but multiple g_consumers
14092108Sphk * can be attached to one g_provider.
14192108Sphk */
14292108Sphk
14392108Sphkstruct g_consumer {
14495310Sphk	u_int			protect;
14592108Sphk	struct g_geom		*geom;
14692108Sphk	LIST_ENTRY(g_consumer)	consumer;
14792108Sphk	struct g_provider	*provider;
14892108Sphk	LIST_ENTRY(g_consumer)	consumers;	/* XXX: better name */
14992108Sphk	int			acr, acw, ace;
15092108Sphk	struct g_event		*event;
15192108Sphk
15292108Sphk	int			biocount;
15392108Sphk	int			spoiled;
15492108Sphk};
15592108Sphk
15692108Sphk/*
15792108Sphk * A g_provider is a "logical disk".
15892108Sphk */
15992108Sphkstruct g_provider {
16095310Sphk	u_int			protect;
16192108Sphk	char			*name;
16292108Sphk	LIST_ENTRY(g_provider)	provider;
16392108Sphk	struct g_geom		*geom;
16492108Sphk	LIST_HEAD(,g_consumer)	consumers;
16592108Sphk	int			acr, acw, ace;
16692108Sphk	int			error;
16792108Sphk	struct g_event		*event;
16892108Sphk	TAILQ_ENTRY(g_provider)	orphan;
16992108Sphk	int			index;
17093778Sphk	off_t			mediasize;
17192108Sphk};
17292108Sphk
17392108Sphk/* geom_dump.c */
17492108Sphkvoid g_hexdump(void *ptr, int length);
17592108Sphkvoid g_trace(int level, char *, ...);
17692108Sphk#	define G_T_TOPOLOGY	1
17792108Sphk#	define G_T_BIO		2
17892108Sphk#	define G_T_ACCESS	4
17992108Sphk
18093090Sphk/* geom_enc.c */
18193090Sphkuint32_t g_dec_be2(u_char *p);
18293090Sphkuint32_t g_dec_be4(u_char *p);
18393090Sphkuint32_t g_dec_le2(u_char *p);
18493090Sphkuint32_t g_dec_le4(u_char *p);
18593090Sphkvoid g_enc_le4(u_char *p, uint32_t u);
18693090Sphk
18792108Sphk/* geom_event.c */
18892108Sphkvoid g_orphan_provider(struct g_provider *pp, int error);
18992108Sphkvoid g_rattle(void);
19092108Sphkvoid g_silence(void);
19192108Sphk
19292108Sphk/* geom_subr.c */
19392108Sphkint g_access_abs(struct g_consumer *cp, int read, int write, int exclusive);
19492108Sphkint g_access_rel(struct g_consumer *cp, int read, int write, int exclusive);
19593248Sphkvoid g_add_class(struct g_class *mp);
19692108Sphkint g_attach(struct g_consumer *cp, struct g_provider *pp);
19793248Sphkstruct g_geom *g_create_geomf(char *class, struct g_provider *, char *fmt, ...);
19892108Sphkvoid g_destroy_consumer(struct g_consumer *cp);
19992108Sphkvoid g_destroy_geom(struct g_geom *pp);
20092108Sphkvoid g_destroy_provider(struct g_provider *pp);
20192108Sphkvoid g_dettach(struct g_consumer *cp);
20292108Sphkvoid g_error_provider(struct g_provider *pp, int error);
20394284Sphkint g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len);
20494284Sphk#define g_getattr(a, c, v) g_getattr__((a), (c), (v), sizeof *(v))
20592108Sphkint g_haveattr(struct bio *bp, char *attribute, void *val, int len);
20692108Sphkint g_haveattr_int(struct bio *bp, char *attribute, int val);
20792108Sphkint g_haveattr_off_t(struct bio *bp, char *attribute, off_t val);
20893248Sphkstruct g_geom * g_insert_geom(char *class, struct g_consumer *cp);
20992108Sphkstruct g_consumer * g_new_consumer(struct g_geom *gp);
21093248Sphkstruct g_geom * g_new_geomf(struct g_class *mp, char *fmt, ...);
21192108Sphkstruct g_provider * g_new_providerf(struct g_geom *gp, char *fmt, ...);
21295310Sphkvoid g_sanity(void *ptr);
21392108Sphkvoid g_spoil(struct g_provider *pp, struct g_consumer *cp);
21492108Sphkint g_std_access(struct g_provider *pp, int dr, int dw, int de);
21592108Sphkvoid g_std_done(struct bio *bp);
21692108Sphkvoid g_std_spoiled(struct g_consumer *cp);
21792108Sphk
21892108Sphk/* geom_io.c */
21992108Sphkstruct bio * g_clone_bio(struct bio *);
22092108Sphkvoid g_destroy_bio(struct bio *);
22192108Sphkvoid g_io_deliver(struct bio *bp);
22293776Sphkvoid g_io_fail(struct bio *bp, int error);
22394283Sphkint g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr);
22492108Sphkvoid g_io_request(struct bio *bp, struct g_consumer *cp);
22594283Sphkint g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr);
22692108Sphkstruct bio *g_new_bio(void);
22792108Sphkvoid * g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error);
22892108Sphk
22992108Sphk/* geom_kern.c / geom_kernsim.c */
23092108Sphk
23192403Sphkstruct g_ioctl {
23292403Sphk	u_long		cmd;
23392403Sphk	void		*data;
23492403Sphk	int		fflag;
23592403Sphk	struct thread	*td;
23692403Sphk};
23792108Sphk
23892108Sphk#ifdef _KERNEL
23992108Sphk
24095038Sphkstruct g_kerneldump {
24195038Sphk	off_t		offset;
24295038Sphk	off_t		length;
24395038Sphk};
24495038Sphk
24592108SphkMALLOC_DECLARE(M_GEOM);
24692108Sphk
24792108Sphkstatic __inline void *
24892108Sphkg_malloc(int size, int flags)
24992108Sphk{
25092108Sphk	void *p;
25192108Sphk
25292108Sphk	mtx_lock(&Giant);
25392108Sphk	p = malloc(size, M_GEOM, flags);
25492108Sphk	mtx_unlock(&Giant);
25595310Sphk	g_sanity(p);
25695310Sphk	/* printf("malloc(%d, %x) -> %p\n", size, flags, p); */
25792108Sphk	return (p);
25892108Sphk}
25992108Sphk
26092108Sphkstatic __inline void
26192108Sphkg_free(void *ptr)
26292108Sphk{
26395310Sphk	g_sanity(ptr);
26495310Sphk	/* printf("free(%p)\n", ptr); */
26592108Sphk	mtx_lock(&Giant);
26692108Sphk	free(ptr, M_GEOM);
26792108Sphk	mtx_unlock(&Giant);
26892108Sphk}
26992108Sphk
27092108Sphkextern struct sx topology_lock;
27192403Sphk#define g_topology_lock() do { mtx_assert(&Giant, MA_NOTOWNED); sx_xlock(&topology_lock); } while (0)
27295310Sphk#define g_topology_unlock() do { g_sanity(NULL); sx_xunlock(&topology_lock); } while (0)
27395310Sphk#define g_topology_assert() do { g_sanity(NULL); sx_assert(&topology_lock, SX_XLOCKED); } while (0)
27492108Sphk
27593248Sphk#define DECLARE_GEOM_CLASS(class, name) 	\
27692108Sphk	static void				\
27792108Sphk	name##init(void)			\
27892108Sphk	{					\
27992108Sphk		mtx_unlock(&Giant);		\
28093248Sphk		g_add_class(&class);		\
28192108Sphk		mtx_lock(&Giant);		\
28292108Sphk	}					\
28392108Sphk	SYSINIT(name, SI_SUB_PSEUDO, SI_ORDER_FIRST, name##init, NULL);
28492108Sphk
28595310Sphk#endif /* _KERNEL */
28692108Sphk
28795276Sphk#endif /* _GEOM_GEOM_H_ */
288