2 * Copyright 2007-2008, Sine Nomine Associates and others.
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
10 #ifndef _AFS_VOL_VNODE_INLINE_H
11 #define _AFS_VOL_VNODE_INLINE_H 1
15 /***************************************************/
16 /* demand attach vnode state machine routines */
17 /***************************************************/
20 * get a reference to a vnode object.
22 * @param[in] vnp vnode object pointer
24 * @internal vnode package internal use only
26 * @pre VOL_LOCK must be held
28 * @post vnode refcount incremented
30 * @see VnCancelReservation_r
33 VnCreateReservation_r(Vnode * vnp)
36 if (Vn_refcount(vnp) == 1) {
37 DeleteFromVnLRU(Vn_class(vnp), vnp);
41 extern int TrustVnodeCacheEntry;
44 * release a reference to a vnode object.
46 * @param[in] vnp vnode object pointer
50 * @post refcount decremented; possibly re-added to vn lru
52 * @internal vnode package internal use only
54 * @see VnCreateReservation_r
57 VnCancelReservation_r(Vnode * vnp)
59 if (--Vn_refcount(vnp) == 0) {
60 AddToVnLRU(Vn_class(vnp), vnp);
62 /* If caching is turned off,
63 * disassociate vnode cache entry from volume object */
64 if (!TrustVnodeCacheEntry) {
65 DeleteFromVVnList(vnp);
70 #ifdef AFS_PTHREAD_ENV
71 #define VN_SET_WRITER_THREAD_ID(v) (((v)->writer) = pthread_self())
73 #define VN_SET_WRITER_THREAD_ID(v) (LWP_CurrentProcess(&((v)->writer)))
76 #define VOL_LOCK_NOT_HELD 0
77 #define VOL_LOCK_HELD 1
78 #define MIGHT_DEADLOCK 0
79 #define WILL_NOT_DEADLOCK 1
82 * acquire a lock on a vnode object.
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
89 * @note caller must guarantee deadlock will not occur
91 * @post lock acquired.
92 * for write case, thread owner field set.
94 * @note for DAFS, this is a no-op
96 * @internal vnode package internal use only
99 VnLock(Vnode * vnp, int type, int held, int safe)
101 #ifdef AFS_DEMAND_ATTACH_FS
102 if (type == WRITE_LOCK) {
103 VN_SET_WRITER_THREAD_ID(vnp);
105 #else /* !AFS_DEMAND_ATTACH_FS */
109 if (type == READ_LOCK) {
110 ObtainReadLock(&vnp->lock);
112 ObtainWriteLock(&vnp->lock);
113 VN_SET_WRITER_THREAD_ID(vnp);
118 #endif /* !AFS_DEMAND_ATTACH_FS */
122 * release a lock on a vnode object.
124 * @param[in] vnp vnode object pointer
125 * @param[in] type lock type
127 * @note for DAFS, this is a no-op
129 * @internal vnode package internal use only
132 VnUnlock(Vnode * vnp, int type)
134 if (type == READ_LOCK) {
135 #ifndef AFS_DEMAND_ATTACH_FS
136 ReleaseReadLock(&vnp->lock);
140 #ifndef AFS_DEMAND_ATTACH_FS
141 ReleaseWriteLock(&vnp->lock);
147 #ifdef AFS_DEMAND_ATTACH_FS
149 * change state, and notify other threads,
150 * return previous state to caller.
152 * @param[in] vnp pointer to vnode object
153 * @param[in] new_state new vnode state value
157 * @post vnode state changed
159 * @return previous vnode state
161 * @note DEMAND_ATTACH_FS only
163 * @internal vnode package internal use only
165 static_inline VnState
166 VnChangeState_r(Vnode * vnp, VnState new_state)
168 VnState old_state = Vn_state(vnp);
170 Vn_state(vnp) = new_state;
171 assert(pthread_cond_broadcast(&Vn_stateCV(vnp)) == 0);
176 * tells caller whether or not the current state requires
177 * exclusive access without holding glock.
179 * @param[in] state vnode state enumeration
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
185 * @note DEMAND_ATTACH_FS only
188 VnIsExclusiveState(VnState state)
191 case VN_STATE_RELEASING:
192 case VN_STATE_CLOSING:
195 case VN_STATE_EXCLUSIVE:
203 * tell caller whether vnode state is an error condition.
205 * @param[in] state vnode state enumeration
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
211 * @note DEMAND_ATTACH_FS only
214 VnIsErrorState(VnState state)
224 * tell caller whether vnode state is valid.
226 * @param[in] state vnode state enumeration
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
232 * @note DEMAND_ATTACH_FS only
235 VnIsValidState(VnState state)
238 (state < VN_STATE_COUNT)) {
245 * wait for the vnode to change states.
247 * @param[in] vnp vnode object pointer
249 * @pre VOL_LOCK held; ref held on vnode
251 * @post VOL_LOCK held; vnode state has changed from previous value
253 * @note DEMAND_ATTACH_FS only
256 VnWaitStateChange_r(Vnode * vnp)
258 VnState state_save = Vn_state(vnp);
260 assert(Vn_refcount(vnp));
262 VOL_CV_WAIT(&Vn_stateCV(vnp));
263 } while (Vn_state(vnp) == state_save);
264 assert(!(Vn_stateFlags(vnp) & VN_ON_LRU));
268 * wait for blocking ops to end.
270 * @pre VOL_LOCK held; ref held on vnode
272 * @post VOL_LOCK held; vnode not in exclusive state
274 * @param[in] vnp vnode object pointer
276 * @note DEMAND_ATTACH_FS only
279 VnWaitExclusiveState_r(Vnode * vnp)
281 assert(Vn_refcount(vnp));
282 while (VnIsExclusiveState(Vn_state(vnp))) {
283 VOL_CV_WAIT(&Vn_stateCV(vnp));
285 assert(!(Vn_stateFlags(vnp) & VN_ON_LRU));
289 * wait until vnode is in non-exclusive state, and there are no active readers.
291 * @param[in] vnp vnode object pointer
293 * @pre VOL_LOCK held; ref held on vnode
295 * @post VOL_LOCK held; vnode is in non-exclusive state and has no active readers
297 * @note DEMAND_ATTACH_FS only
300 VnWaitQuiescent_r(Vnode * vnp)
302 assert(Vn_refcount(vnp));
303 while (VnIsExclusiveState(Vn_state(vnp)) ||
305 VOL_CV_WAIT(&Vn_stateCV(vnp));
307 assert(!(Vn_stateFlags(vnp) & VN_ON_LRU));
311 * register a new reader on a vnode.
313 * @param[in] vnp vnode object pointer
315 * @pre VOL_LOCK held.
317 * vnode in VN_STATE_READ or VN_STATE_ONLINE
319 * @post refcount incremented.
320 * state set to VN_STATE_READ.
322 * @note DEMAND_ATTACH_FS only
324 * @internal vnode package internal use only
327 VnBeginRead_r(Vnode * vnp)
329 if (!Vn_readers(vnp)) {
330 assert(Vn_state(vnp) == VN_STATE_ONLINE);
331 VnChangeState_r(vnp, VN_STATE_READ);
334 assert(Vn_state(vnp) == VN_STATE_READ);
338 * deregister a reader on a vnode.
340 * @param[in] vnp vnode object pointer
342 * @pre VOL_LOCK held.
344 * read ref held on vnode.
345 * vnode in VN_STATE_READ.
347 * @post refcount decremented.
348 * when count reaches zero, state set to VN_STATE_ONLINE.
350 * @note DEMAND_ATTACH_FS only
352 * @internal vnode package internal use only
355 VnEndRead_r(Vnode * vnp)
357 assert(Vn_readers(vnp) > 0);
359 if (!Vn_readers(vnp)) {
360 assert(pthread_cond_broadcast(&Vn_stateCV(vnp)) == 0);
361 VnChangeState_r(vnp, VN_STATE_ONLINE);
365 #endif /* AFS_DEMAND_ATTACH_FS */
367 #endif /* _AFS_VOL_VNODE_INLINE_H */