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