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 struct bhv_head_lock;
82 
83 /*
84  * Behavior head.  Head of the chain of behaviors.
85  * Contained within each virtualized object data structure.
86  */
87 typedef struct bhv_head {
88 	struct bhv_desc *bh_first;	/* first behavior in chain */
89 	struct bhv_head_lock *bh_lockp;	/* pointer to lock info struct */
90 } bhv_head_t;
91 
92 /*
93  * Behavior descriptor.	 Descriptor associated with each behavior.
94  * Contained within the behavior's private data structure.
95  */
96 typedef struct bhv_desc {
97 	void		*bd_pdata;	/* private data for this behavior */
98 	void		*bd_vobj;	/* virtual object associated with */
99 	void		*bd_ops;	/* ops for this behavior */
100 	struct bhv_desc *bd_next;	/* next behavior in chain */
101 } bhv_desc_t;
102 
103 /*
104  * Behavior identity field.  A behavior's identity determines the position
105  * where it lives within a behavior chain, and it's always the first field
106  * of the behavior's ops vector. The optional id field further identifies the
107  * subsystem responsible for the behavior.
108  */
109 typedef struct bhv_identity {
110 	__u16	bi_id;		/* owning subsystem id */
111 	__u16	bi_position;	/* position in chain */
112 } bhv_identity_t;
113 
114 typedef bhv_identity_t bhv_position_t;
115 
116 #define BHV_IDENTITY_INIT(id,pos)	{id, pos}
117 #define BHV_IDENTITY_INIT_POSITION(pos) BHV_IDENTITY_INIT(0, pos)
118 
119 /*
120  * Define boundaries of position values.
121  */
122 #define BHV_POSITION_INVALID	0	/* invalid position number */
123 #define BHV_POSITION_BASE	1	/* base (last) implementation layer */
124 #define BHV_POSITION_TOP	63	/* top (first) implementation layer */
125 
126 /*
127  * Plumbing macros.
128  */
129 #define BHV_HEAD_FIRST(bhp)	(ASSERT((bhp)->bh_first), (bhp)->bh_first)
130 #define BHV_NEXT(bdp)		(ASSERT((bdp)->bd_next), (bdp)->bd_next)
131 #define BHV_NEXTNULL(bdp)	((bdp)->bd_next)
132 #define BHV_VOBJ(bdp)		(ASSERT((bdp)->bd_vobj), (bdp)->bd_vobj)
133 #define BHV_VOBJNULL(bdp)	((bdp)->bd_vobj)
134 #define BHV_PDATA(bdp)		(bdp)->bd_pdata
135 #define BHV_OPS(bdp)		(bdp)->bd_ops
136 #define BHV_IDENTITY(bdp)	((bhv_identity_t *)(bdp)->bd_ops)
137 #define BHV_POSITION(bdp)	(BHV_IDENTITY(bdp)->bi_position)
138 
139 extern void bhv_head_init(bhv_head_t *, char *);
140 extern void bhv_head_destroy(bhv_head_t *);
141 extern int  bhv_insert(bhv_head_t *, bhv_desc_t *);
142 extern void bhv_insert_initial(bhv_head_t *, bhv_desc_t *);
143 
144 /*
145  * Initialize a new behavior descriptor.
146  * Arguments:
147  *   bdp - pointer to behavior descriptor
148  *   pdata - pointer to behavior's private data
149  *   vobj - pointer to associated virtual object
150  *   ops - pointer to ops for this behavior
151  */
152 #define bhv_desc_init(bdp, pdata, vobj, ops)		\
153  {							\
154 	(bdp)->bd_pdata = pdata;			\
155 	(bdp)->bd_vobj = vobj;				\
156 	(bdp)->bd_ops = ops;				\
157 	(bdp)->bd_next = NULL;				\
158  }
159 
160 /*
161  * Remove a behavior descriptor from a behavior chain.
162  */
163 #define bhv_remove(bhp, bdp)				\
164  {							\
165 	if ((bhp)->bh_first == (bdp)) {			\
166 		/*					\
167 		* Remove from front of chain.		\
168 		* Atomic wrt oip's.			\
169 		*/					\
170 	       (bhp)->bh_first = (bdp)->bd_next;	\
171 	} else {					\
172 	       /* remove from non-front of chain */	\
173 	       bhv_remove_not_first(bhp, bdp);		\
174 	}						\
175 	(bdp)->bd_vobj = NULL;				\
176  }
177 
178 /*
179  * Behavior module prototypes.
180  */
181 extern void		bhv_remove_not_first(bhv_head_t *bhp, bhv_desc_t *bdp);
182 extern bhv_desc_t *	bhv_lookup(bhv_head_t *bhp, void *ops);
183 extern bhv_desc_t *	bhv_lookup_range(bhv_head_t *bhp, int low, int high);
184 extern bhv_desc_t *	bhv_base(bhv_head_t *bhp);
185 
186 /* No bhv locking on Linux */
187 #define bhv_lookup_unlocked	bhv_lookup
188 #define bhv_base_unlocked	bhv_base
189 
190 #endif /* __XFS_BEHAVIOR_H__ */
191