/usr/include/afs/volume_inline.h is in libopenafs-dev 1.8.0~pre5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | /*
* Copyright 2005-2008, Sine Nomine Associates and others.
* All Rights Reserved.
*
* This software has been released under the terms of the IBM Public
* License. For details, see the LICENSE file in the top-level source
* directory or online at http://www.openafs.org/dl/license10.html
*/
#ifndef _AFS_VOL_VOLUME_INLINE_H
#define _AFS_VOL_VOLUME_INLINE_H 1
#include "volume.h"
#include "partition.h"
#ifdef AFS_DEMAND_ATTACH_FS
# include "lock.h"
#endif
#ifdef AFS_PTHREAD_ENV
#include <afs/opr_assert.h>
/**
* @param[in] cv cond var
* @param[in] ts deadline, or NULL to wait forever
* @param[out] timedout set to 1 if we returned due to the deadline, 0 if we
* returned due to the cond var getting signalled. If
* NULL, it is ignored.
*/
static_inline void
VOL_CV_TIMEDWAIT(pthread_cond_t *cv, const struct timespec *ts, int *timedout)
{
int code;
if (timedout) {
*timedout = 0;
}
if (!ts) {
VOL_CV_WAIT(cv);
return;
}
VOL_LOCK_DBG_CV_WAIT_BEGIN;
code = opr_cv_timedwait(cv, &vol_glock_mutex, ts);
VOL_LOCK_DBG_CV_WAIT_END;
if (code == ETIMEDOUT) {
code = 0;
if (timedout) {
*timedout = 1;
}
}
opr_Assert(code == 0);
}
#endif /* AFS_PTHREAD_ENV */
/**
* tell caller whether the given program type represents a salvaging
* program.
*
* @param type program type enumeration
*
* @return whether program state is a salvager
* @retval 0 type is a non-salvaging program
* @retval 1 type is a salvaging program
*/
static_inline int
VIsSalvager(ProgramType type)
{
switch(type) {
case salvager:
case salvageServer:
case volumeSalvager:
return 1;
default:
return 0;
}
}
/**
* tells caller whether or not we need to lock the entire partition when
* attaching a volume.
*
* @return whether or not we need to lock the partition
* @retval 0 no, we do not
* @retval 1 yes, we do
*
* @note for DAFS, always returns 0, since we use per-header locks instead
*/
static_inline int
VRequiresPartLock(void)
{
#ifdef AFS_DEMAND_ATTACH_FS
return 0;
#else
switch (programType) {
case volumeServer:
case volumeUtility:
return 1;
default:
return 0;
}
#endif /* AFS_DEMAND_ATTACH_FS */
}
/**
* tells caller whether or not we need to check out a volume from the
* fileserver before we can use it.
*
* @param[in] mode the mode of attachment for the volume
*
* @return whether or not we need to check out the volume from the fileserver
* @retval 0 no, we can just use the volume
* @retval 1 yes, we must check out the volume before use
*/
static_inline int
VMustCheckoutVolume(int mode)
{
if (VCanUseFSSYNC() && mode != V_SECRETLY && mode != V_PEEK) {
return 1;
}
return 0;
}
/**
* tells caller whether we should check the inUse field in the volume
* header when attaching a volume.
*
* If we check inUse, that generally means we will salvage the volume
* (or put it in an error state) if we detect that another program
* claims to be using the volume when we try to attach. We don't always
* want to do that, since sometimes we know that the volume may be in
* use by another program, e.g. when we are attaching with V_PEEK
* or attaching for only reading (V_READONLY).
*
* @param mode the mode of attachment for the volume
*
* @return whether or not we should check inUse
* @retval 0 no, we should not check inUse
* @retval 1 yes, we should check inUse
*/
static_inline int
VShouldCheckInUse(int mode)
{
if (VCanUnsafeAttach()) {
return 0;
}
if (programType == fileServer) {
return 1;
}
if (VMustCheckoutVolume(mode)) {
/*
* Before VShouldCheckInUse() was called, the caller checked out the
* volume from the fileserver. The volume may not be in use by the
* fileserver, or another program, at this point. The caller should
* verify by checking inUse is not set, otherwise the volume state
* is in error.
*
* However, an exception is made for the V_READONLY attach mode. The
* volume may still be in use by the fileserver when a caller has
* checked out the volume from the fileserver with the V_READONLY
* attach mode, and so it is not an error for the inUse field to be set
* at this point. The caller should not check the inUse and may
* not change any volume state.
*/
if (mode == V_READONLY) {
return 0; /* allowed to be inUse; do not check */
}
return 1; /* may not be inUse; check */
}
return 0;
}
#ifdef AFS_DEMAND_ATTACH_FS
/**
* acquire a non-blocking disk lock for a particular volume id.
*
* @param[in] volid the volume ID to lock
* @param[in] dp the partition on which 'volid' resides
* @param[in] locktype READ_LOCK or WRITE_LOCK
*
* @return operation status
* @retval 0 success, lock was obtained
* @retval EBUSY another process holds a conflicting lock
* @retval EIO error acquiring lock
*
* @note Use VLockVolumeNB instead, if possible; only use this directly if
* you are not dealing with 'Volume*'s and attached volumes and such
*
* @pre There must not be any other threads acquiring locks on the same volid
* and partition; the locks will not work correctly if two threads try to
* acquire locks for the same volume
*/
static_inline int
VLockVolumeByIdNB(VolumeId volid, struct DiskPartition64 *dp, int locktype)
{
return VLockFileLock(&dp->volLockFile, volid, locktype, 1 /* nonblock */);
}
/**
* release a lock acquired by VLockVolumeByIdNB.
*
* @param[in] volid the volume id to unlock
* @param[in] dp the partition on which 'volid' resides
*
* @pre volid was previously locked by VLockVolumeByIdNB
*/
static_inline void
VUnlockVolumeById(VolumeId volid, struct DiskPartition64 *dp)
{
VLockFileUnlock(&dp->volLockFile, volid);
}
/***************************************************/
/* demand attach fs state machine routines */
/***************************************************/
/**
* tells caller whether we need to keep volumes locked for the entire time we
* are using them, or if we can unlock volumes as soon as they are attached.
*
* @return whether we can unlock attached volumes or not
* @retval 1 yes, we can unlock attached volumes
* @retval 0 no, do not unlock volumes until we unattach them
*/
static_inline int
VCanUnlockAttached(void)
{
switch(programType) {
case fileServer:
return 1;
default:
return 0;
}
}
/**
* tells caller whether we need to lock a vol header with a write lock, a
* read lock, or if we do not need to lock it at all, when attaching.
*
* @param[in] mode volume attachment mode
* @param[in] writable 1 if the volume is writable, 0 if not
*
* @return how we need to lock the vol header
* @retval 0 do not lock the vol header at all
* @retval READ_LOCK lock the vol header with a read lock
* @retval WRITE_LOCK lock the vol header with a write lock
*
* @note DAFS only (non-DAFS uses partition locks)
*/
static_inline int
VVolLockType(int mode, int writable)
{
switch (programType) {
case fileServer:
if (writable) {
return WRITE_LOCK;
}
return READ_LOCK;
case volumeSalvager:
case salvageServer:
case salvager:
return WRITE_LOCK;
default:
/* volserver, vol utilies, etc */
switch (mode) {
case V_READONLY:
return READ_LOCK;
case V_VOLUPD:
case V_SECRETLY:
return WRITE_LOCK;
case V_CLONE:
case V_DUMP:
if (writable) {
return WRITE_LOCK;
}
return READ_LOCK;
case V_PEEK:
return 0;
default:
opr_Assert(0 /* unknown checkout mode */);
return 0;
}
}
}
/**
* tells caller whether or not the volume is effectively salvaging.
*
* @param vp volume pointer
*
* @return whether volume is salvaging or not
* @retval 0 no, volume is not salvaging
* @retval 1 yes, volume is salvaging
*
* @note The volume may not actually be getting salvaged at the moment if
* this returns 1, but may have just been requested or scheduled to be
* salvaged. Callers should treat these cases as pretty much the same
* anyway, since we should not touch a volume that is busy salvaging or
* waiting to be salvaged.
*/
static_inline int
VIsSalvaging(struct Volume *vp)
{
/* these tests are a bit redundant, but to be safe... */
switch(V_attachState(vp)) {
case VOL_STATE_SALVAGING:
case VOL_STATE_SALVAGE_REQ:
return 1;
default:
if (vp->salvage.requested || vp->salvage.scheduled) {
return 1;
}
}
return 0;
}
/**
* tells caller whether or not the current state requires
* exclusive access without holding glock.
*
* @param state volume state enumeration
*
* @return whether volume state is a mutually exclusive state
* @retval 0 no, state is re-entrant
* @retval 1 yes, state is mutually exclusive
*
* @note DEMAND_ATTACH_FS only
*/
static_inline int
VIsExclusiveState(VolState state)
{
switch (state) {
case VOL_STATE_UPDATING:
case VOL_STATE_ATTACHING:
case VOL_STATE_GET_BITMAP:
case VOL_STATE_HDR_LOADING:
case VOL_STATE_HDR_ATTACHING:
case VOL_STATE_OFFLINING:
case VOL_STATE_DETACHING:
case VOL_STATE_SALVSYNC_REQ:
case VOL_STATE_VNODE_ALLOC:
case VOL_STATE_VNODE_GET:
case VOL_STATE_VNODE_CLOSE:
case VOL_STATE_VNODE_RELEASE:
case VOL_STATE_VLRU_ADD:
case VOL_STATE_SCANNING_RXCALLS:
return 1;
default:
return 0;
}
}
/**
* tell caller whether V_attachState is an error condition.
*
* @param state volume state enumeration
*
* @return whether volume state is in error state
* @retval 0 state is not an error state
* @retval 1 state is an error state
*
* @note DEMAND_ATTACH_FS only
*/
static_inline int
VIsErrorState(VolState state)
{
switch (state) {
case VOL_STATE_ERROR:
case VOL_STATE_SALVAGING:
case VOL_STATE_SALVAGE_REQ:
return 1;
default:
return 0;
}
}
/**
* tell caller whether V_attachState is an offline condition.
*
* @param state volume state enumeration
*
* @return whether volume state is in offline state
* @retval 0 state is not an offline state
* @retval 1 state is an offline state
*
* @note DEMAND_ATTACH_FS only
*/
static_inline int
VIsOfflineState(VolState state)
{
switch (state) {
case VOL_STATE_UNATTACHED:
case VOL_STATE_ERROR:
case VOL_STATE_SALVAGING:
case VOL_STATE_DELETED:
return 1;
default:
return 0;
}
}
/**
* tell caller whether V_attachState is valid.
*
* @param state volume state enumeration
*
* @return whether volume state is a mutually exclusive state
* @retval 0 no, state is not valid
* @retval 1 yes, state is a valid enumeration member
*
* @note DEMAND_ATTACH_FS only
*
* @note do we really want to treat VOL_STATE_FREED as valid?
*/
static_inline int
VIsValidState(VolState state)
{
if (((int) state >= 0) &&
(state < VOL_STATE_COUNT)) {
return 1;
}
return 0;
}
/**
* increment volume-package internal refcount.
*
* @param vp volume object pointer
*
* @internal volume package internal use only
*
* @pre VOL_LOCK must be held
*
* @post volume waiters refcount is incremented
*
* @see VCancelReservation_r
*
* @note DEMAND_ATTACH_FS only
*/
static_inline void
VCreateReservation_r(Volume * vp)
{
vp->nWaiters++;
}
/**
* wait for the volume to change states.
*
* @param vp volume object pointer
*
* @pre VOL_LOCK held; ref held on volume
*
* @post VOL_LOCK held; volume state has changed from previous value
*
* @note DEMAND_ATTACH_FS only
*/
static_inline void
VWaitStateChange_r(Volume * vp)
{
VolState state_save = V_attachState(vp);
opr_Assert(vp->nWaiters || vp->nUsers);
do {
VOL_CV_WAIT(&V_attachCV(vp));
} while (V_attachState(vp) == state_save);
opr_Assert(V_attachState(vp) != VOL_STATE_FREED);
}
/**
* wait for the volume to change states within a certain amount of time
*
* @param[in] vp volume object pointer
* @param[in] ts deadline (absolute time) or NULL to wait forever
*
* @pre VOL_LOCK held; ref held on volume
* @post VOL_LOCK held; volume state has changed and/or it is after the time
* specified in ts
*
* @note DEMAND_ATTACH_FS only
* @note if ts is NULL, this is identical to VWaitStateChange_r
*/
static_inline void
VTimedWaitStateChange_r(Volume * vp, const struct timespec *ts, int *atimedout)
{
VolState state_save;
int timeout;
if (atimedout) {
*atimedout = 0;
}
if (!ts) {
VWaitStateChange_r(vp);
return;
}
state_save = V_attachState(vp);
opr_Assert(vp->nWaiters || vp->nUsers);
do {
VOL_CV_TIMEDWAIT(&V_attachCV(vp), ts, &timeout);
} while (V_attachState(vp) == state_save && !timeout);
opr_Assert(V_attachState(vp) != VOL_STATE_FREED);
if (atimedout && timeout) {
*atimedout = 1;
}
}
/**
* wait for blocking ops to end.
*
* @pre VOL_LOCK held; ref held on volume
*
* @post VOL_LOCK held; volume not in exclusive state
*
* @param vp volume object pointer
*
* @note DEMAND_ATTACH_FS only
*/
static_inline void
VWaitExclusiveState_r(Volume * vp)
{
opr_Assert(vp->nWaiters || vp->nUsers);
while (VIsExclusiveState(V_attachState(vp))) {
VOL_CV_WAIT(&V_attachCV(vp));
}
opr_Assert(V_attachState(vp) != VOL_STATE_FREED);
}
/**
* change state, and notify other threads,
* return previous state to caller.
*
* @param vp pointer to volume object
* @param new_state new volume state value
* @pre VOL_LOCK held
*
* @post volume state changed; stats updated
*
* @return previous volume state
*
* @note DEMAND_ATTACH_FS only
*/
static_inline VolState
VChangeState_r(Volume * vp, VolState new_state)
{
VolState old_state = V_attachState(vp);
/* XXX profiling need to make sure these counters
* don't kill performance... */
VStats.state_levels[old_state]--;
VStats.state_levels[new_state]++;
V_attachState(vp) = new_state;
opr_cv_broadcast(&V_attachCV(vp));
return old_state;
}
#endif /* AFS_DEMAND_ATTACH_FS */
#define VENUMCASE(en) \
case en: return #en
/**
* translate a ProgramType code to a string.
*
* @param[in] type ProgramType numeric code
*
* @return a human-readable string for that program type
* @retval "**UNKNOWN**" an unknown ProgramType was given
*/
static_inline char *
VPTypeToString(ProgramType type)
{
switch (type) {
VENUMCASE(fileServer);
VENUMCASE(volumeUtility);
VENUMCASE(salvager);
VENUMCASE(salvageServer);
VENUMCASE(debugUtility);
VENUMCASE(volumeServer);
VENUMCASE(volumeSalvager);
default:
return "**UNKNOWN**";
}
}
#undef VENUMCASE
#endif /* _AFS_VOL_VOLUME_INLINE_H */
|