1/*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18#ifndef __XFS_BEHAVIOR_H__
19#define __XFS_BEHAVIOR_H__
20
21/*
22 * Header file used to associate behaviors with virtualized objects.
23 *
24 * A virtualized object is an internal, virtualized representation of
25 * OS entities such as persistent files, processes, or sockets.  Examples
26 * of virtualized objects include vnodes, vprocs, and vsockets.  Often
27 * a virtualized object is referred to simply as an "object."
28 *
29 * A behavior is essentially an implementation layer associated with
30 * an object.  Multiple behaviors for an object are chained together,
31 * the order of chaining determining the order of invocation.  Each
32 * behavior of a given object implements the same set of interfaces
33 * (e.g., the VOP interfaces).
34 *
35 * Behaviors may be dynamically inserted into an object's behavior chain,
36 * such that the addition is transparent to consumers that already have
37 * references to the object.  Typically, a given behavior will be inserted
38 * at a particular location in the behavior chain.  Insertion of new
39 * behaviors is synchronized with operations-in-progress (oip's) so that
40 * the oip's always see a consistent view of the chain.
41 *
42 * The term "interposition" is used to refer to the act of inserting
43 * a behavior such that it interposes on (i.e., is inserted in front
44 * of) a particular other behavior.  A key example of this is when a
45 * system implementing distributed single system image wishes to
46 * interpose a distribution layer (providing distributed coherency)
47 * in front of an object that is otherwise only accessed locally.
48 *
49 * Note that the traditional vnode/inode combination is simply a virtualized
50 * object that has exactly one associated behavior.
51 *
52 * Behavior synchronization is logic which is necessary under certain
53 * circumstances that there is no conflict between ongoing operations
54 * traversing the behavior chain and those dynamically modifying the
55 * behavior chain.  Because behavior synchronization adds extra overhead
56 * to virtual operation invocation, we want to restrict, as much as
57 * we can, the requirement for this extra code, to those situations
58 * in which it is truly necessary.
59 *
60 * Behavior synchronization is needed whenever there's at least one class
61 * of object in the system for which:
62 * 1) multiple behaviors for a given object are supported,
63 * -- AND --
64 * 2a) insertion of a new behavior can happen dynamically at any time during
65 *     the life of an active object,
66 *	-- AND --
67 *	3a) insertion of a new behavior needs to synchronize with existing
68 *	    ops-in-progress.
69 *	-- OR --
70 *	3b) multiple different behaviors can be dynamically inserted at
71 *	    any time during the life of an active object
72 *	-- OR --
73 *	3c) removal of a behavior can occur at any time during the life of
74 *	    an active object.
75 * -- OR --
76 * 2b) removal of a behavior can occur at any time during the life of an
77 *     active object
78 *
79 */
80
81/*
82 * Behavior head.  Head of the chain of behaviors.
83 * Contained within each virtualized object data structure.
84 */
85typedef struct bhv_head {
86	struct bhv_desc *bh_first;	/* first behavior in chain */
87} bhv_head_t;
88
89/*
90 * Behavior descriptor.	 Descriptor associated with each behavior.
91 * Contained within the behavior's private data structure.
92 */
93typedef struct bhv_desc {
94	void		*bd_pdata;	/* private data for this behavior */
95	void		*bd_vobj;	/* virtual object associated with */
96	void		*bd_ops;	/* ops for this behavior */
97	struct bhv_desc *bd_next;	/* next behavior in chain */
98} bhv_desc_t;
99
100/*
101 * Behavior identity field.  A behavior's identity determines the position
102 * where it lives within a behavior chain, and it's always the first field
103 * of the behavior's ops vector. The optional id field further identifies the
104 * subsystem responsible for the behavior.
105 */
106typedef struct bhv_identity {
107	__u16	bi_id;		/* owning subsystem id */
108	__u16	bi_position;	/* position in chain */
109} bhv_identity_t;
110
111typedef bhv_identity_t bhv_position_t;
112
113#define BHV_IDENTITY_INIT(id,pos)	{id, pos}
114#define BHV_IDENTITY_INIT_POSITION(pos) BHV_IDENTITY_INIT(0, pos)
115
116/*
117 * Define boundaries of position values.
118 */
119#define BHV_POSITION_INVALID	0	/* invalid position number */
120#define BHV_POSITION_BASE	1	/* base (last) implementation layer */
121#define BHV_POSITION_TOP	63	/* top (first) implementation layer */
122
123/*
124 * Plumbing macros.
125 */
126#define BHV_HEAD_FIRST(bhp)	(ASSERT((bhp)->bh_first), (bhp)->bh_first)
127#define BHV_NEXT(bdp)		(ASSERT((bdp)->bd_next), (bdp)->bd_next)
128#define BHV_NEXTNULL(bdp)	((bdp)->bd_next)
129#define BHV_VOBJ(bdp)		(ASSERT((bdp)->bd_vobj), (bdp)->bd_vobj)
130#define BHV_VOBJNULL(bdp)	((bdp)->bd_vobj)
131#define BHV_PDATA(bdp)		(bdp)->bd_pdata
132#define BHV_OPS(bdp)		(bdp)->bd_ops
133#define BHV_IDENTITY(bdp)	((bhv_identity_t *)(bdp)->bd_ops)
134#define BHV_POSITION(bdp)	(BHV_IDENTITY(bdp)->bi_position)
135
136extern void bhv_head_init(bhv_head_t *, char *);
137extern void bhv_head_destroy(bhv_head_t *);
138extern int  bhv_insert(bhv_head_t *, bhv_desc_t *);
139extern void bhv_insert_initial(bhv_head_t *, bhv_desc_t *);
140
141/*
142 * Initialize a new behavior descriptor.
143 * Arguments:
144 *   bdp - pointer to behavior descriptor
145 *   pdata - pointer to behavior's private data
146 *   vobj - pointer to associated virtual object
147 *   ops - pointer to ops for this behavior
148 */
149#define bhv_desc_init(bdp, pdata, vobj, ops)		\
150 {							\
151	(bdp)->bd_pdata = pdata;			\
152	(bdp)->bd_vobj = vobj;				\
153	(bdp)->bd_ops = ops;				\
154	(bdp)->bd_next = NULL;				\
155 }
156
157/*
158 * Remove a behavior descriptor from a behavior chain.
159 */
160#define bhv_remove(bhp, bdp)				\
161 {							\
162	if ((bhp)->bh_first == (bdp)) {			\
163		/*					\
164		* Remove from front of chain.		\
165		* Atomic wrt oip's.			\
166		*/					\
167	       (bhp)->bh_first = (bdp)->bd_next;	\
168	} else {					\
169	       /* remove from non-front of chain */	\
170	       bhv_remove_not_first(bhp, bdp);		\
171	}						\
172	(bdp)->bd_vobj = NULL;				\
173 }
174
175/*
176 * Behavior module prototypes.
177 */
178extern void		bhv_remove_not_first(bhv_head_t *bhp, bhv_desc_t *bdp);
179extern bhv_desc_t *	bhv_lookup_range(bhv_head_t *bhp, int low, int high);
180extern bhv_desc_t *	bhv_base(bhv_head_t *bhp);
181
182/* No bhv locking on Linux */
183#define bhv_base_unlocked	bhv_base
184
185#endif /* __XFS_BEHAVIOR_H__ */
186