b6f92cdce358f5f6159f647316f4c7bc8fed075c
[openafs.git] / src / vol / volume_inline.h
1 /*
2  * Copyright 2005-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_VOLUME_INLINE_H
11 #define _AFS_VOL_VOLUME_INLINE_H 1
12
13 #include "volume.h"
14 #include "partition.h"
15
16 #ifdef AFS_DEMAND_ATTACH_FS
17 # include "lock.h"
18 #endif
19
20 #ifdef AFS_PTHREAD_ENV
21 /**
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.
27  */
28 static_inline void
29 VOL_CV_TIMEDWAIT(pthread_cond_t *cv, const struct timespec *ts, int *timedout)
30 {
31     int code;
32     if (timedout) {
33         *timedout = 0;
34     }
35     if (!ts) {
36         VOL_CV_WAIT(cv);
37         return;
38     }
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) {
43         code = 0;
44         if (timedout) {
45             *timedout = 1;
46         }
47     }
48     osi_Assert(code == 0);
49 }
50 #endif /* AFS_PTHREAD_ENV */
51
52 /**
53  * tell caller whether the given program type represents a salvaging
54  * program.
55  *
56  * @param type  program type enumeration
57  *
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
61  */
62 static_inline int
63 VIsSalvager(ProgramType type)
64 {
65     switch(type) {
66     case salvager:
67     case salvageServer:
68     case volumeSalvager:
69         return 1;
70     default:
71         return 0;
72     }
73 }
74
75 /**
76  * tells caller whether or not we need to lock the entire partition when
77  * attaching a volume.
78  *
79  * @return whether or not we need to lock the partition
80  *  @retval 0  no, we do not
81  *  @retval 1  yes, we do
82  *
83  * @note for DAFS, always returns 0, since we use per-header locks instead
84  */
85 static_inline int
86 VRequiresPartLock(void)
87 {
88 #ifdef AFS_DEMAND_ATTACH_FS
89     return 0;
90 #else
91     switch (programType) {
92     case volumeServer:
93     case volumeUtility:
94         return 1;
95     default:
96         return 0;
97     }
98 #endif /* AFS_DEMAND_ATTACH_FS */
99 }
100
101 /**
102  * tells caller whether or not we need to check out a volume from the
103  * fileserver before we can use it.
104  *
105  * @param[in] mode the mode of attachment for the volume
106  *
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
110  */
111 static_inline int
112 VMustCheckoutVolume(int mode)
113 {
114     if (VCanUseFSSYNC() && mode != V_SECRETLY && mode != V_PEEK) {
115         return 1;
116     }
117     return 0;
118 }
119
120 /**
121  * tells caller whether we should check the inUse field in the volume
122  * header when attaching a volume.
123  *
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
129  * we don't care.
130  *
131  * @param mode  the mode of attachment for the volume
132  *
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
136  */
137 static_inline int
138 VShouldCheckInUse(int mode)
139 {
140     if (VCanUnsafeAttach()) {
141         return 0;
142     }
143     if (programType == fileServer) {
144        return 1;
145     }
146     if (VMustCheckoutVolume(mode)) {
147         /* assume we checked out the volume from the fileserver, so inUse
148          * should not be set */
149         return 1;
150     }
151     return 0;
152 }
153
154 #ifdef AFS_DEMAND_ATTACH_FS
155 /**
156  * acquire a non-blocking disk lock for a particular volume id.
157  *
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
161  *
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
166  *
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
169  *
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
173  */
174 static_inline int
175 VLockVolumeByIdNB(VolumeId volid, struct DiskPartition64 *dp, int locktype)
176 {
177     return VLockFileLock(&dp->volLockFile, volid, locktype, 1 /* nonblock */);
178 }
179
180 /**
181  * release a lock acquired by VLockVolumeByIdNB.
182  *
183  * @param[in] volid the volume id to unlock
184  * @param[in] dp    the partition on which 'volid' resides
185  *
186  * @pre volid was previously locked by VLockVolumeByIdNB
187  */
188 static_inline void
189 VUnlockVolumeById(VolumeId volid, struct DiskPartition64 *dp)
190 {
191     VLockFileUnlock(&dp->volLockFile, volid);
192 }
193
194 /***************************************************/
195 /* demand attach fs state machine routines         */
196 /***************************************************/
197
198 /**
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.
201  *
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
205  */
206 static_inline int
207 VCanUnlockAttached(void)
208 {
209     switch(programType) {
210     case fileServer:
211         return 1;
212     default:
213         return 0;
214     }
215 }
216
217 /**
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.
220  *
221  * @param[in]  mode  volume attachment mode
222  * @param[in]  writeable  1 if the volume is writable, 0 if not
223  *
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
228  *
229  * @note DAFS only (non-DAFS uses partition locks)
230  */
231 static_inline int
232 VVolLockType(int mode, int writeable)
233 {
234     switch (programType) {
235     case fileServer:
236         if (writeable) {
237             return WRITE_LOCK;
238         }
239         return READ_LOCK;
240
241     case volumeSalvager:
242     case salvageServer:
243     case salvager:
244         return WRITE_LOCK;
245
246     default:
247         /* volserver, vol utilies, etc */
248
249         switch (mode) {
250         case V_READONLY:
251             return READ_LOCK;
252
253         case V_VOLUPD:
254         case V_SECRETLY:
255             return WRITE_LOCK;
256
257         case V_CLONE:
258         case V_DUMP:
259             if (writeable) {
260                 return WRITE_LOCK;
261             }
262             return READ_LOCK;
263
264         case V_PEEK:
265             return 0;
266
267         default:
268             osi_Assert(0 /* unknown checkout mode */);
269             return 0;
270         }
271     }
272 }
273
274 /**
275  * tells caller whether or not the volume is effectively salvaging.
276  *
277  * @param vp  volume pointer
278  *
279  * @return whether volume is salvaging or not
280  *  @retval 0 no, volume is not salvaging
281  *  @retval 1 yes, volume is salvaging
282  *
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.
288  */
289 static_inline int
290 VIsSalvaging(struct Volume *vp)
291 {
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:
296         return 1;
297     default:
298         if (vp->salvage.requested || vp->salvage.scheduled) {
299             return 1;
300         }
301     }
302     return 0;
303 }
304
305 /**
306  * tells caller whether or not the current state requires
307  * exclusive access without holding glock.
308  *
309  * @param state  volume state enumeration
310  *
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
314  *
315  * @note DEMAND_ATTACH_FS only
316  */
317 static_inline int
318 VIsExclusiveState(VolState state)
319 {
320     switch (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:
335         return 1;
336     default:
337         return 0;
338     }
339 }
340
341 /**
342  * tell caller whether V_attachState is an error condition.
343  *
344  * @param state  volume state enumeration
345  *
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
349  *
350  * @note DEMAND_ATTACH_FS only
351  */
352 static_inline int
353 VIsErrorState(VolState state)
354 {
355     switch (state) {
356     case VOL_STATE_ERROR:
357     case VOL_STATE_SALVAGING:
358     case VOL_STATE_SALVAGE_REQ:
359         return 1;
360     default:
361         return 0;
362     }
363 }
364
365 /**
366  * tell caller whether V_attachState is an offline condition.
367  *
368  * @param state  volume state enumeration
369  *
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
373  *
374  * @note DEMAND_ATTACH_FS only
375  */
376 static_inline int
377 VIsOfflineState(VolState state)
378 {
379     switch (state) {
380     case VOL_STATE_UNATTACHED:
381     case VOL_STATE_ERROR:
382     case VOL_STATE_SALVAGING:
383     case VOL_STATE_DELETED:
384         return 1;
385     default:
386         return 0;
387     }
388 }
389
390 /**
391  * tell caller whether V_attachState is valid.
392  *
393  * @param state  volume state enumeration
394  *
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
398  *
399  * @note DEMAND_ATTACH_FS only
400  *
401  * @note do we really want to treat VOL_STATE_FREED as valid?
402  */
403 static_inline int
404 VIsValidState(VolState state)
405 {
406     if ((state >= 0) &&
407         (state < VOL_STATE_COUNT)) {
408         return 1;
409     }
410     return 0;
411 }
412
413 /**
414  * increment volume-package internal refcount.
415  *
416  * @param vp  volume object pointer
417  *
418  * @internal volume package internal use only
419  *
420  * @pre VOL_LOCK must be held
421  *
422  * @post volume waiters refcount is incremented
423  *
424  * @see VCancelReservation_r
425  *
426  * @note DEMAND_ATTACH_FS only
427  */
428 static_inline void
429 VCreateReservation_r(Volume * vp)
430 {
431     vp->nWaiters++;
432 }
433
434 /**
435  * wait for the volume to change states.
436  *
437  * @param vp  volume object pointer
438  *
439  * @pre VOL_LOCK held; ref held on volume
440  *
441  * @post VOL_LOCK held; volume state has changed from previous value
442  *
443  * @note DEMAND_ATTACH_FS only
444  */
445 static_inline void
446 VWaitStateChange_r(Volume * vp)
447 {
448     VolState state_save = V_attachState(vp);
449
450     osi_Assert(vp->nWaiters || vp->nUsers);
451     do {
452         VOL_CV_WAIT(&V_attachCV(vp));
453     } while (V_attachState(vp) == state_save);
454     osi_Assert(V_attachState(vp) != VOL_STATE_FREED);
455 }
456
457 /**
458  * wait for the volume to change states within a certain amount of time
459  *
460  * @param[in] vp  volume object pointer
461  * @param[in] ts  deadline (absolute time) or NULL to wait forever
462  *
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
465  *       specified in ts
466  *
467  * @note DEMAND_ATTACH_FS only
468  * @note if ts is NULL, this is identical to VWaitStateChange_r
469  */
470 static_inline void
471 VTimedWaitStateChange_r(Volume * vp, const struct timespec *ts, int *atimedout)
472 {
473     VolState state_save;
474     int timeout;
475
476     if (atimedout) {
477         *atimedout = 0;
478     }
479
480     if (!ts) {
481         VWaitStateChange_r(vp);
482         return;
483     }
484
485     state_save = V_attachState(vp);
486
487     assert(vp->nWaiters || vp->nUsers);
488     do {
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);
492
493     if (atimedout && timeout) {
494         *atimedout = 1;
495     }
496 }
497
498 /**
499  * wait for blocking ops to end.
500  *
501  * @pre VOL_LOCK held; ref held on volume
502  *
503  * @post VOL_LOCK held; volume not in exclusive state
504  *
505  * @param vp  volume object pointer
506  *
507  * @note DEMAND_ATTACH_FS only
508  */
509 static_inline void
510 VWaitExclusiveState_r(Volume * vp)
511 {
512     osi_Assert(vp->nWaiters || vp->nUsers);
513     while (VIsExclusiveState(V_attachState(vp))) {
514         VOL_CV_WAIT(&V_attachCV(vp));
515     }
516     osi_Assert(V_attachState(vp) != VOL_STATE_FREED);
517 }
518
519 /**
520  * change state, and notify other threads,
521  * return previous state to caller.
522  *
523  * @param vp         pointer to volume object
524  * @param new_state  new volume state value
525  * @pre VOL_LOCK held
526  *
527  * @post volume state changed; stats updated
528  *
529  * @return previous volume state
530  *
531  * @note DEMAND_ATTACH_FS only
532  */
533 static_inline VolState
534 VChangeState_r(Volume * vp, VolState new_state)
535 {
536     VolState old_state = V_attachState(vp);
537
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]++;
542
543     V_attachState(vp) = new_state;
544     CV_BROADCAST(&V_attachCV(vp));
545     return old_state;
546 }
547
548 #endif /* AFS_DEMAND_ATTACH_FS */
549
550 #define VENUMCASE(en) \
551     case en: \
552         return #en; \
553         break
554
555 /**
556  * translate a ProgramType code to a string.
557  *
558  * @param[in] type ProgramType numeric code
559  *
560  * @return a human-readable string for that program type
561  *  @retval "**UNKNOWN**" an unknown ProgramType was given
562  */
563 static_inline char *
564 VPTypeToString(ProgramType type)
565 {
566     switch (type) {
567         VENUMCASE(fileServer);
568         VENUMCASE(volumeUtility);
569         VENUMCASE(salvager);
570         VENUMCASE(salvageServer);
571         VENUMCASE(debugUtility);
572         VENUMCASE(volumeServer);
573         VENUMCASE(volumeSalvager);
574     default:
575         return "**UNKNOWN**";
576     }
577 }
578
579 #undef VENUMCASE
580
581 #endif /* _AFS_VOL_VOLUME_INLINE_H */