Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.0 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. //+============================================================================
  3. //
  4. // volcache.cxx
  5. //
  6. // Implementation for CVolumeLocationCache. This class is used by trkwks
  7. // to keep a cache of volid->mcid mappings.
  8. //
  9. //+============================================================================
  10. #include "pch.cxx"
  11. #pragma hdrstop
  12. #include "trkwks.hxx"
  13. //+----------------------------------------------------------------------------
  14. //
  15. // CVolumeLocationCache::Initialize
  16. //
  17. // Init the critsec and set the lifetime value.
  18. //
  19. //+----------------------------------------------------------------------------
  20. void
  21. CVolumeLocationCache::Initialize( DWORD dwEntryLifetimeSeconds )
  22. {
  23. _cs.Initialize();
  24. _fInitialized = TRUE;
  25. _cVols = 0;
  26. _cftEntryLifetime = 0;
  27. _cftEntryLifetime.IncrementSeconds( dwEntryLifetimeSeconds );
  28. }
  29. //+----------------------------------------------------------------------------
  30. //
  31. // CVolumeLocationCache::UnInitialize
  32. //
  33. // Free the critsec.
  34. //
  35. //+----------------------------------------------------------------------------
  36. void
  37. CVolumeLocationCache::UnInitialize()
  38. {
  39. if (_fInitialized)
  40. {
  41. _fInitialized = FALSE;
  42. _cs.UnInitialize();
  43. }
  44. }
  45. //+----------------------------------------------------------------------------
  46. //
  47. // CVolumeLocationCache::_FindVolume
  48. //
  49. // Search the cache for a volid. If it's found, return it's index in the
  50. // cache array.
  51. //
  52. //+----------------------------------------------------------------------------
  53. int
  54. CVolumeLocationCache::_FindVolume(const CVolumeId & volid)
  55. {
  56. for (int i=0; i<_cVols; i++)
  57. {
  58. if (_vl[i].volid == volid)
  59. {
  60. return(i);
  61. }
  62. }
  63. return(-1);
  64. }
  65. //+----------------------------------------------------------------------------
  66. //
  67. // CVolumeLocationCache::FindVolume
  68. //
  69. // search the cache for a volid. If found, return the machine ID it was
  70. // last known to be on, and a bool indicating if this entry in the cache
  71. // is old or new. If it's old and doesn't work, the caller shouldn't
  72. // trust the cache. If it's new and doesn't work, the caller shouldn't
  73. // bother the DC trying to find it.
  74. //
  75. //+----------------------------------------------------------------------------
  76. BOOL
  77. CVolumeLocationCache::FindVolume(const CVolumeId & volid, CMachineId * pmcid, BOOL *pfRecentEntry )
  78. {
  79. int i;
  80. _cs.Enter();
  81. i=_FindVolume(volid );
  82. if (i != -1)
  83. {
  84. // We found the volume. Load the data.
  85. CFILETIME cftNow;
  86. *pmcid=_vl[i].mcid;
  87. *pfRecentEntry = ( (cftNow - _vl[i].cft) <= _cftEntryLifetime );
  88. }
  89. _cs.Leave();
  90. return(i != -1);
  91. }
  92. //+----------------------------------------------------------------------------
  93. //
  94. // CVolumeLocationCache::AddVolume
  95. //
  96. // Add a volid->mcid mapping to the cache. The entry in the cache is
  97. // timestamped so we know how trustworthy it is. If the entry is already
  98. // in the cache, we just update the timestamp. In either case, this entry
  99. // will be at the front of the array.
  100. //
  101. //+----------------------------------------------------------------------------
  102. void
  103. CVolumeLocationCache::AddVolume(const CVolumeId & volid, const CMachineId & mcid)
  104. {
  105. int i;
  106. if( CVolumeId() == volid )
  107. return;
  108. _cs.Enter();
  109. i = _FindVolume(volid);
  110. if (i == -1)
  111. {
  112. // This volid isn't already in the cache. Shift back the existing
  113. // entries so that this entry can be in front.
  114. BOOL fFull = _cVols >= MAX_CACHED_VOLUME_LOCATIONS;
  115. memcpy(&_vl[1], &_vl[0], sizeof(VolumeLocation)*(fFull ? _cVols-1 : _cVols));
  116. if (!fFull)
  117. {
  118. _cVols++;
  119. }
  120. }
  121. else
  122. {
  123. // Move the prior part of the array back, overwriting
  124. // the this entry. That way we can put this entry
  125. // at the front.
  126. memcpy(&_vl[1], &_vl[0], sizeof(VolumeLocation)*i);
  127. }
  128. // Put this entry at the front of the array.
  129. _vl[0].volid = volid;
  130. _vl[0].mcid = mcid;
  131. _vl[0].cft = CFILETIME();
  132. #if DBG
  133. for (i=0; i<_cVols; i++)
  134. {
  135. TrkLog((TRKDBG_VOLCACHE,
  136. TEXT("%02d: %s -> %s") MCID_BYTE_FORMAT_STRING,
  137. i,
  138. (const TCHAR*)CDebugString(_vl[i].volid),
  139. (const TCHAR*)CDebugString(_vl[i].mcid),
  140. ((BYTE*)&(_vl[i].mcid))[0], ((BYTE*)&(_vl[i].mcid))[1], ((BYTE*)&(_vl[i].mcid))[2], ((BYTE*)&(_vl[i].mcid))[3],
  141. ((BYTE*)&(_vl[i].mcid))[4], ((BYTE*)&(_vl[i].mcid))[5], ((BYTE*)&(_vl[i].mcid))[6], ((BYTE*)&(_vl[i].mcid))[7],
  142. ((BYTE*)&(_vl[i].mcid))[8], ((BYTE*)&(_vl[i].mcid))[9], ((BYTE*)&(_vl[i].mcid))[10], ((BYTE*)&(_vl[i].mcid))[11],
  143. ((BYTE*)&(_vl[i].mcid))[12], ((BYTE*)&(_vl[i].mcid))[13], ((BYTE*)&(_vl[i].mcid))[14], ((BYTE*)&(_vl[i].mcid))[15] ));
  144. }
  145. #endif
  146. _cs.Leave();
  147. }