generic-inline-20080924
[openafs.git] / src / vol / vnode_inline.h
1 /*
2  * Copyright 2007-2008, Sine Nomine Associates and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef _AFS_VOL_VNODE_INLINE_H
11 #define _AFS_VOL_VNODE_INLINE_H 1
12
13 #include "vnode.h"
14
15 /***************************************************/
16 /* demand attach vnode state machine routines      */
17 /***************************************************/
18
19 /**
20  * get a reference to a vnode object.
21  *
22  * @param[in] vnp  vnode object pointer
23  *
24  * @internal vnode package internal use only
25  *
26  * @pre VOL_LOCK must be held
27  *
28  * @post vnode refcount incremented
29  *
30  * @see VnCancelReservation_r
31  */
32 static_inline void
33 VnCreateReservation_r(Vnode * vnp)
34 {
35     Vn_refcount(vnp)++;
36     if (Vn_refcount(vnp) == 1) {
37         DeleteFromVnLRU(Vn_class(vnp), vnp);
38     }
39 }
40
41 extern int TrustVnodeCacheEntry;
42
43 /**
44  * release a reference to a vnode object.
45  *
46  * @param[in] vnp  vnode object pointer
47  *
48  * @pre VOL_LOCK held
49  *
50  * @post refcount decremented; possibly re-added to vn lru
51  *
52  * @internal vnode package internal use only
53  *
54  * @see VnCreateReservation_r
55  */
56 static_inline void
57 VnCancelReservation_r(Vnode * vnp)
58 {
59     if (--Vn_refcount(vnp) == 0) {
60         AddToVnLRU(Vn_class(vnp), vnp);
61
62         /* If caching is turned off, 
63          * disassociate vnode cache entry from volume object */
64         if (!TrustVnodeCacheEntry) {
65             DeleteFromVVnList(vnp);
66         }
67     }
68 }
69
70 #ifdef AFS_PTHREAD_ENV
71 #define VN_SET_WRITER_THREAD_ID(v)  (((v)->writer) = pthread_self())
72 #else
73 #define VN_SET_WRITER_THREAD_ID(v)  (LWP_CurrentProcess(&((v)->writer)))
74 #endif
75
76 #define VOL_LOCK_NOT_HELD 0
77 #define VOL_LOCK_HELD 1
78 #define MIGHT_DEADLOCK 0
79 #define WILL_NOT_DEADLOCK 1
80
81 /**
82  * acquire a lock on a vnode object.
83  *
84  * @param[in] vnp   vnode object pointer
85  * @param[in] type  lock type
86  * @param[in] held  whether or not vol glock is held
87  * @param[in] safe  whether it it is safe to acquire without dropping vol glock
88  *
89  * @note caller must guarantee deadlock will not occur
90  *
91  * @post lock acquired.
92  *       for write case, thread owner field set.
93  *
94  * @note for DAFS, this is a no-op
95  *
96  * @internal vnode package internal use only
97  */
98 static_inline void
99 VnLock(Vnode * vnp, int type, int held, int safe)
100 {
101 #ifdef AFS_DEMAND_ATTACH_FS
102     if (type == WRITE_LOCK) {
103         VN_SET_WRITER_THREAD_ID(vnp);
104     }
105 #else /* !AFS_DEMAND_ATTACH_FS */
106     if (held && !safe) {
107         VOL_UNLOCK;
108     }
109     if (type == READ_LOCK) {
110         ObtainReadLock(&vnp->lock);
111     } else {
112         ObtainWriteLock(&vnp->lock);
113         VN_SET_WRITER_THREAD_ID(vnp);
114     }
115     if (held && !safe) {
116         VOL_LOCK;
117     }
118 #endif /* !AFS_DEMAND_ATTACH_FS */
119 }
120
121 /**
122  * release a lock on a vnode object.
123  *
124  * @param[in] vnp   vnode object pointer
125  * @param[in] type  lock type
126  *
127  * @note for DAFS, this is a no-op
128  *
129  * @internal vnode package internal use only
130  */
131 static_inline void
132 VnUnlock(Vnode * vnp, int type)
133 {
134     if (type == READ_LOCK) {
135 #ifndef AFS_DEMAND_ATTACH_FS
136         ReleaseReadLock(&vnp->lock);
137 #endif
138     } else {
139         vnp->writer = 0;
140 #ifndef AFS_DEMAND_ATTACH_FS
141         ReleaseWriteLock(&vnp->lock);
142 #endif
143     }
144 }
145
146
147 #ifdef AFS_DEMAND_ATTACH_FS
148 /**
149  * change state, and notify other threads,
150  * return previous state to caller.
151  *
152  * @param[in] vnp        pointer to vnode object
153  * @param[in] new_state  new vnode state value
154  *
155  * @pre VOL_LOCK held
156  *
157  * @post vnode state changed
158  *
159  * @return previous vnode state
160  *
161  * @note DEMAND_ATTACH_FS only
162  *
163  * @internal vnode package internal use only
164  */
165 static_inline VnState
166 VnChangeState_r(Vnode * vnp, VnState new_state)
167 {
168     VnState old_state = Vn_state(vnp);
169
170     Vn_state(vnp) = new_state;
171     assert(pthread_cond_broadcast(&Vn_stateCV(vnp)) == 0);
172     return old_state;
173 }
174
175 /**
176  * tells caller whether or not the current state requires
177  * exclusive access without holding glock.
178  *
179  * @param[in] state  vnode state enumeration
180  *
181  * @return whether vnode state is a mutually exclusive state
182  *   @retval 0  no, state is re-entrant
183  *   @retval 1  yes, state is mutually exclusive
184  *
185  * @note DEMAND_ATTACH_FS only
186  */
187 static_inline int
188 VnIsExclusiveState(VnState state)
189 {
190     switch (state) {
191     case VN_STATE_RELEASING:
192     case VN_STATE_CLOSING:
193     case VN_STATE_ALLOC:
194     case VN_STATE_LOAD:
195     case VN_STATE_EXCLUSIVE:
196     case VN_STATE_STORE:
197         return 1;
198     }
199     return 0;
200 }
201
202 /**
203  * tell caller whether vnode state is an error condition.
204  *
205  * @param[in] state  vnode state enumeration
206  *
207  * @return whether vnode state is in error state
208  *   @retval 0  state is not an error state
209  *   @retval 1  state is an error state
210  *
211  * @note DEMAND_ATTACH_FS only
212  */
213 static_inline int
214 VnIsErrorState(VnState state)
215 {
216     switch (state) {
217     case VN_STATE_ERROR:
218         return 1;
219     }
220     return 0;
221 }
222
223 /**
224  * tell caller whether vnode state is valid.
225  *
226  * @param[in] state  vnode state enumeration
227  *
228  * @return whether vnode state is a mutually exclusive state
229  *   @retval 0  no, state is not valid
230  *   @retval 1  yes, state is a valid enumeration member
231  *
232  * @note DEMAND_ATTACH_FS only
233  */
234 static_inline int
235 VnIsValidState(VnState state)
236 {
237     if ((state >= 0) && 
238         (state < VN_STATE_COUNT)) {
239         return 1;
240     }
241     return 0;
242 }
243
244 /**
245  * wait for the vnode to change states.
246  *
247  * @param[in] vnp  vnode object pointer
248  *
249  * @pre VOL_LOCK held; ref held on vnode
250  *
251  * @post VOL_LOCK held; vnode state has changed from previous value
252  *
253  * @note DEMAND_ATTACH_FS only
254  */
255 static_inline void
256 VnWaitStateChange_r(Vnode * vnp)
257 {
258     VnState state_save = Vn_state(vnp);
259
260     assert(Vn_refcount(vnp));
261     do {
262         VOL_CV_WAIT(&Vn_stateCV(vnp));
263     } while (Vn_state(vnp) == state_save);
264     assert(!(Vn_stateFlags(vnp) & VN_ON_LRU));
265 }
266
267 /**
268  * wait for blocking ops to end.
269  *
270  * @pre VOL_LOCK held; ref held on vnode
271  *
272  * @post VOL_LOCK held; vnode not in exclusive state
273  *
274  * @param[in] vnp  vnode object pointer
275  *
276  * @note DEMAND_ATTACH_FS only
277  */
278 static_inline void
279 VnWaitExclusiveState_r(Vnode * vnp)
280 {
281     assert(Vn_refcount(vnp));
282     while (VnIsExclusiveState(Vn_state(vnp))) {
283         VOL_CV_WAIT(&Vn_stateCV(vnp));
284     }
285     assert(!(Vn_stateFlags(vnp) & VN_ON_LRU));
286 }
287
288 /**
289  * wait until vnode is in non-exclusive state, and there are no active readers.
290  *
291  * @param[in] vnp  vnode object pointer
292  *
293  * @pre VOL_LOCK held; ref held on vnode
294  *
295  * @post VOL_LOCK held; vnode is in non-exclusive state and has no active readers
296  *
297  * @note DEMAND_ATTACH_FS only
298  */
299 static_inline void
300 VnWaitQuiescent_r(Vnode * vnp)
301 {
302     assert(Vn_refcount(vnp));
303     while (VnIsExclusiveState(Vn_state(vnp)) ||
304            Vn_readers(vnp)) {
305         VOL_CV_WAIT(&Vn_stateCV(vnp));
306     }
307     assert(!(Vn_stateFlags(vnp) & VN_ON_LRU));
308 }
309
310 /**
311  * register a new reader on a vnode.
312  *
313  * @param[in] vnp  vnode object pointer
314  *
315  * @pre VOL_LOCK held.
316  *      ref held on vnode.
317  *      vnode in VN_STATE_READ or VN_STATE_ONLINE
318  *
319  * @post refcount incremented.
320  *       state set to VN_STATE_READ.
321  *
322  * @note DEMAND_ATTACH_FS only
323  *
324  * @internal vnode package internal use only
325  */
326 static_inline void
327 VnBeginRead_r(Vnode * vnp)
328 {
329     if (!Vn_readers(vnp)) {
330         assert(Vn_state(vnp) == VN_STATE_ONLINE);
331         VnChangeState_r(vnp, VN_STATE_READ);
332     }
333     Vn_readers(vnp)++;
334     assert(Vn_state(vnp) == VN_STATE_READ);
335 }
336
337 /**
338  * deregister a reader on a vnode.
339  *
340  * @param[in] vnp  vnode object pointer
341  *
342  * @pre VOL_LOCK held.
343  *      ref held on vnode.
344  *      read ref held on vnode.
345  *      vnode in VN_STATE_READ.
346  *
347  * @post refcount decremented.
348  *       when count reaches zero, state set to VN_STATE_ONLINE.
349  *
350  * @note DEMAND_ATTACH_FS only
351  *
352  * @internal vnode package internal use only
353  */
354 static_inline void
355 VnEndRead_r(Vnode * vnp)
356 {
357     assert(Vn_readers(vnp) > 0);
358     Vn_readers(vnp)--;
359     if (!Vn_readers(vnp)) {
360         assert(pthread_cond_broadcast(&Vn_stateCV(vnp)) == 0);
361         VnChangeState_r(vnp, VN_STATE_ONLINE);
362     }
363 }
364
365 #endif /* AFS_DEMAND_ATTACH_FS */
366
367 #endif /* _AFS_VOL_VNODE_INLINE_H */