2 * Copyright 2005-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_VOLUME_INLINE_H
11 #define _AFS_VOL_VOLUME_INLINE_H 1
14 #include "partition.h"
16 #ifdef AFS_DEMAND_ATTACH_FS
20 #ifdef AFS_PTHREAD_ENV
22 * @param[in] cv cond var
23 * @param[in] ts deadline, or NULL to wait forever
24 * @param[out] timedout set to 1 if we returned due to the deadline, 0 if we
25 * returned due to the cond var getting signalled. If
26 * NULL, it is ignored.
29 VOL_CV_TIMEDWAIT(pthread_cond_t *cv, const struct timespec *ts, int *timedout)
39 VOL_LOCK_DBG_CV_WAIT_BEGIN;
40 code = CV_TIMEDWAIT(cv, &vol_glock_mutex, ts);
41 VOL_LOCK_DBG_CV_WAIT_END;
42 if (code == ETIMEDOUT) {
48 osi_Assert(code == 0);
50 #endif /* AFS_PTHREAD_ENV */
53 * tell caller whether the given program type represents a salvaging
56 * @param type program type enumeration
58 * @return whether program state is a salvager
59 * @retval 0 type is a non-salvaging program
60 * @retval 1 type is a salvaging program
63 VIsSalvager(ProgramType type)
76 * tells caller whether or not we need to lock the entire partition when
79 * @return whether or not we need to lock the partition
80 * @retval 0 no, we do not
81 * @retval 1 yes, we do
83 * @note for DAFS, always returns 0, since we use per-header locks instead
86 VRequiresPartLock(void)
88 #ifdef AFS_DEMAND_ATTACH_FS
91 switch (programType) {
98 #endif /* AFS_DEMAND_ATTACH_FS */
102 * tells caller whether or not we need to check out a volume from the
103 * fileserver before we can use it.
105 * @param[in] mode the mode of attachment for the volume
107 * @return whether or not we need to check out the volume from the fileserver
108 * @retval 0 no, we can just use the volume
109 * @retval 1 yes, we must check out the volume before use
112 VMustCheckoutVolume(int mode)
114 if (VCanUseFSSYNC() && mode != V_SECRETLY && mode != V_PEEK) {
121 * tells caller whether we should check the inUse field in the volume
122 * header when attaching a volume.
124 * If we check inUse, that generally means we will salvage the volume
125 * (or put it in an error state) if we detect that another program
126 * claims to be using the volume when we try to attach. We don't always
127 * want to do that, since sometimes we know that the volume may be in
128 * use by another program, e.g. when we are attaching with V_PEEK, and
131 * @param mode the mode of attachment for the volume
133 * @return whether or not we should check inUse
134 * @retval 0 no, we should not check inUse
135 * @retval 1 yes, we should check inUse
138 VShouldCheckInUse(int mode)
140 if (VCanUnsafeAttach()) {
143 if (programType == fileServer) {
146 if (VMustCheckoutVolume(mode)) {
147 /* assume we checked out the volume from the fileserver, so inUse
148 * should not be set */
154 #ifdef AFS_DEMAND_ATTACH_FS
156 * acquire a non-blocking disk lock for a particular volume id.
158 * @param[in] volid the volume ID to lock
159 * @param[in] dp the partition on which 'volid' resides
160 * @param[in] locktype READ_LOCK or WRITE_LOCK
162 * @return operation status
163 * @retval 0 success, lock was obtained
164 * @retval EBUSY another process holds a conflicting lock
165 * @retval EIO error acquiring lock
167 * @note Use VLockVolumeNB instead, if possible; only use this directly if
168 * you are not dealing with 'Volume*'s and attached volumes and such
170 * @pre There must not be any other threads acquiring locks on the same volid
171 * and partition; the locks will not work correctly if two threads try to
172 * acquire locks for the same volume
175 VLockVolumeByIdNB(VolumeId volid, struct DiskPartition64 *dp, int locktype)
177 return VLockFileLock(&dp->volLockFile, volid, locktype, 1 /* nonblock */);
181 * release a lock acquired by VLockVolumeByIdNB.
183 * @param[in] volid the volume id to unlock
184 * @param[in] dp the partition on which 'volid' resides
186 * @pre volid was previously locked by VLockVolumeByIdNB
189 VUnlockVolumeById(VolumeId volid, struct DiskPartition64 *dp)
191 VLockFileUnlock(&dp->volLockFile, volid);
194 /***************************************************/
195 /* demand attach fs state machine routines */
196 /***************************************************/
199 * tells caller whether we need to keep volumes locked for the entire time we
200 * are using them, or if we can unlock volumes as soon as they are attached.
202 * @return whether we can unlock attached volumes or not
203 * @retval 1 yes, we can unlock attached volumes
204 * @retval 0 no, do not unlock volumes until we unattach them
207 VCanUnlockAttached(void)
209 switch(programType) {
218 * tells caller whether we need to lock a vol header with a write lock, a
219 * read lock, or if we do not need to lock it at all, when attaching.
221 * @param[in] mode volume attachment mode
222 * @param[in] writeable 1 if the volume is writable, 0 if not
224 * @return how we need to lock the vol header
225 * @retval 0 do not lock the vol header at all
226 * @retval READ_LOCK lock the vol header with a read lock
227 * @retval WRITE_LOCK lock the vol header with a write lock
229 * @note DAFS only (non-DAFS uses partition locks)
232 VVolLockType(int mode, int writeable)
234 switch (programType) {
247 /* volserver, vol utilies, etc */
268 osi_Assert(0 /* unknown checkout mode */);
275 * tells caller whether or not the volume is effectively salvaging.
277 * @param vp volume pointer
279 * @return whether volume is salvaging or not
280 * @retval 0 no, volume is not salvaging
281 * @retval 1 yes, volume is salvaging
283 * @note The volume may not actually be getting salvaged at the moment if
284 * this returns 1, but may have just been requested or scheduled to be
285 * salvaged. Callers should treat these cases as pretty much the same
286 * anyway, since we should not touch a volume that is busy salvaging or
287 * waiting to be salvaged.
290 VIsSalvaging(struct Volume *vp)
292 /* these tests are a bit redundant, but to be safe... */
293 switch(V_attachState(vp)) {
294 case VOL_STATE_SALVAGING:
295 case VOL_STATE_SALVAGE_REQ:
298 if (vp->salvage.requested || vp->salvage.scheduled) {
306 * tells caller whether or not the current state requires
307 * exclusive access without holding glock.
309 * @param state volume state enumeration
311 * @return whether volume state is a mutually exclusive state
312 * @retval 0 no, state is re-entrant
313 * @retval 1 yes, state is mutually exclusive
315 * @note DEMAND_ATTACH_FS only
318 VIsExclusiveState(VolState state)
321 case VOL_STATE_UPDATING:
322 case VOL_STATE_ATTACHING:
323 case VOL_STATE_GET_BITMAP:
324 case VOL_STATE_HDR_LOADING:
325 case VOL_STATE_HDR_ATTACHING:
326 case VOL_STATE_OFFLINING:
327 case VOL_STATE_DETACHING:
328 case VOL_STATE_SALVSYNC_REQ:
329 case VOL_STATE_VNODE_ALLOC:
330 case VOL_STATE_VNODE_GET:
331 case VOL_STATE_VNODE_CLOSE:
332 case VOL_STATE_VNODE_RELEASE:
333 case VOL_STATE_VLRU_ADD:
334 case VOL_STATE_SCANNING_RXCALLS:
342 * tell caller whether V_attachState is an error condition.
344 * @param state volume state enumeration
346 * @return whether volume state is in error state
347 * @retval 0 state is not an error state
348 * @retval 1 state is an error state
350 * @note DEMAND_ATTACH_FS only
353 VIsErrorState(VolState state)
356 case VOL_STATE_ERROR:
357 case VOL_STATE_SALVAGING:
358 case VOL_STATE_SALVAGE_REQ:
366 * tell caller whether V_attachState is an offline condition.
368 * @param state volume state enumeration
370 * @return whether volume state is in offline state
371 * @retval 0 state is not an offline state
372 * @retval 1 state is an offline state
374 * @note DEMAND_ATTACH_FS only
377 VIsOfflineState(VolState state)
380 case VOL_STATE_UNATTACHED:
381 case VOL_STATE_ERROR:
382 case VOL_STATE_SALVAGING:
383 case VOL_STATE_DELETED:
391 * tell caller whether V_attachState is valid.
393 * @param state volume state enumeration
395 * @return whether volume state is a mutually exclusive state
396 * @retval 0 no, state is not valid
397 * @retval 1 yes, state is a valid enumeration member
399 * @note DEMAND_ATTACH_FS only
401 * @note do we really want to treat VOL_STATE_FREED as valid?
404 VIsValidState(VolState state)
407 (state < VOL_STATE_COUNT)) {
414 * increment volume-package internal refcount.
416 * @param vp volume object pointer
418 * @internal volume package internal use only
420 * @pre VOL_LOCK must be held
422 * @post volume waiters refcount is incremented
424 * @see VCancelReservation_r
426 * @note DEMAND_ATTACH_FS only
429 VCreateReservation_r(Volume * vp)
435 * wait for the volume to change states.
437 * @param vp volume object pointer
439 * @pre VOL_LOCK held; ref held on volume
441 * @post VOL_LOCK held; volume state has changed from previous value
443 * @note DEMAND_ATTACH_FS only
446 VWaitStateChange_r(Volume * vp)
448 VolState state_save = V_attachState(vp);
450 osi_Assert(vp->nWaiters || vp->nUsers);
452 VOL_CV_WAIT(&V_attachCV(vp));
453 } while (V_attachState(vp) == state_save);
454 osi_Assert(V_attachState(vp) != VOL_STATE_FREED);
458 * wait for the volume to change states within a certain amount of time
460 * @param[in] vp volume object pointer
461 * @param[in] ts deadline (absolute time) or NULL to wait forever
463 * @pre VOL_LOCK held; ref held on volume
464 * @post VOL_LOCK held; volume state has changed and/or it is after the time
467 * @note DEMAND_ATTACH_FS only
468 * @note if ts is NULL, this is identical to VWaitStateChange_r
471 VTimedWaitStateChange_r(Volume * vp, const struct timespec *ts, int *atimedout)
481 VWaitStateChange_r(vp);
485 state_save = V_attachState(vp);
487 assert(vp->nWaiters || vp->nUsers);
489 VOL_CV_TIMEDWAIT(&V_attachCV(vp), ts, &timeout);
490 } while (V_attachState(vp) == state_save && !timeout);
491 assert(V_attachState(vp) != VOL_STATE_FREED);
493 if (atimedout && timeout) {
499 * wait for blocking ops to end.
501 * @pre VOL_LOCK held; ref held on volume
503 * @post VOL_LOCK held; volume not in exclusive state
505 * @param vp volume object pointer
507 * @note DEMAND_ATTACH_FS only
510 VWaitExclusiveState_r(Volume * vp)
512 osi_Assert(vp->nWaiters || vp->nUsers);
513 while (VIsExclusiveState(V_attachState(vp))) {
514 VOL_CV_WAIT(&V_attachCV(vp));
516 osi_Assert(V_attachState(vp) != VOL_STATE_FREED);
520 * change state, and notify other threads,
521 * return previous state to caller.
523 * @param vp pointer to volume object
524 * @param new_state new volume state value
527 * @post volume state changed; stats updated
529 * @return previous volume state
531 * @note DEMAND_ATTACH_FS only
533 static_inline VolState
534 VChangeState_r(Volume * vp, VolState new_state)
536 VolState old_state = V_attachState(vp);
538 /* XXX profiling need to make sure these counters
539 * don't kill performance... */
540 VStats.state_levels[old_state]--;
541 VStats.state_levels[new_state]++;
543 V_attachState(vp) = new_state;
544 CV_BROADCAST(&V_attachCV(vp));
548 #endif /* AFS_DEMAND_ATTACH_FS */
550 #define VENUMCASE(en) \
556 * translate a ProgramType code to a string.
558 * @param[in] type ProgramType numeric code
560 * @return a human-readable string for that program type
561 * @retval "**UNKNOWN**" an unknown ProgramType was given
564 VPTypeToString(ProgramType type)
567 VENUMCASE(fileServer);
568 VENUMCASE(volumeUtility);
570 VENUMCASE(salvageServer);
571 VENUMCASE(debugUtility);
572 VENUMCASE(volumeServer);
573 VENUMCASE(volumeSalvager);
575 return "**UNKNOWN**";
581 #endif /* _AFS_VOL_VOLUME_INLINE_H */