6 extern void afsi_log(char *pattern, ...);
7 extern DWORD cm_ValidateCache;
10 GranularityAdjustment(afs_uint64 size)
13 static afs_uint64 qwGranularity = 0;
15 if ( !qwGranularity ) {
16 GetSystemInfo(&sysInfo);
17 afsi_log("Granularity - %lX", sysInfo.dwAllocationGranularity);
18 qwGranularity = sysInfo.dwAllocationGranularity;
21 size = (size + (qwGranularity - 1)) & ~(qwGranularity - 1);
26 ComputeSizeOfConfigData(void)
29 size = sizeof(cm_config_data_t);
34 ComputeSizeOfVolumes(DWORD maxvols)
37 size = maxvols * sizeof(cm_volume_t);
42 ComputeSizeOfCellHT(DWORD maxcells)
45 size = osi_PrimeLessThan((afs_uint32)(maxcells/7 + 1)) * sizeof(cm_cell_t *);
50 ComputeSizeOfVolumeHT(DWORD maxvols)
53 size = osi_PrimeLessThan((afs_uint32)(maxvols/7 + 1)) * sizeof(cm_volume_t *);
58 ComputeSizeOfCells(DWORD maxcells)
61 size = maxcells * sizeof(cm_cell_t);
66 ComputeSizeOfACLCache(DWORD stats)
69 size = 2 * (stats + 10) * sizeof(cm_aclent_t);
74 ComputeSizeOfSCache(DWORD stats)
77 size = (stats + 10) * sizeof(cm_scache_t);
82 ComputeSizeOfSCacheHT(DWORD stats)
85 size = osi_PrimeLessThan(stats / 2 + 1) * sizeof(cm_scache_t *);;
90 ComputeSizeOfDNLCache(void)
93 size = NHSIZE * sizeof(cm_nc_t *) + NCSIZE * sizeof(cm_nc_t);
98 ComputeSizeOfDataBuffers(afs_uint64 cacheBlocks, DWORD blockSize)
101 size = cacheBlocks * blockSize;
106 ComputeSizeOfDataHT(afs_uint64 cacheBlocks)
109 size = osi_PrimeLessThan((afs_uint32)(cacheBlocks/7 + 1)) * sizeof(cm_buf_t *);
114 ComputeSizeOfDataHeaders(afs_uint64 cacheBlocks)
117 size = cacheBlocks * sizeof(cm_buf_t);
122 ComputeSizeOfMappingFile(DWORD stats, DWORD maxVols, DWORD maxCells, DWORD chunkSize, afs_uint64 cacheBlocks, DWORD blockSize)
126 size = ComputeSizeOfConfigData()
127 + ComputeSizeOfVolumes(maxVols)
128 + 4 * ComputeSizeOfVolumeHT(maxVols)
129 + ComputeSizeOfCells(maxCells)
130 + 2 * ComputeSizeOfCellHT(maxCells)
131 + ComputeSizeOfACLCache(stats)
132 + ComputeSizeOfSCache(stats)
133 + ComputeSizeOfSCacheHT(stats)
134 + ComputeSizeOfDNLCache()
135 + ComputeSizeOfDataBuffers(cacheBlocks, blockSize)
136 + 2 * ComputeSizeOfDataHT(cacheBlocks)
137 + ComputeSizeOfDataHeaders(cacheBlocks);
141 /* Create a security attribute structure suitable for use when the cache file
142 * is created. What we mainly want is that only the administrator should be
143 * able to do anything with the file. We create an ACL with only one entry,
144 * an entry that grants all rights to the administrator.
146 PSECURITY_ATTRIBUTES CreateCacheFileSA()
148 PSECURITY_ATTRIBUTES psa;
149 PSECURITY_DESCRIPTOR psd;
150 SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
152 DWORD AdminSIDlength;
156 /* Get Administrator SID */
157 AllocateAndInitializeSid(&authority, 2,
158 SECURITY_BUILTIN_DOMAIN_RID,
159 DOMAIN_ALIAS_RID_ADMINS,
163 /* Create Administrator-only ACL */
164 AdminSIDlength = GetLengthSid(AdminSID);
165 ACLlength = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE)
166 + AdminSIDlength - sizeof(DWORD);
167 AdminOnlyACL = GlobalAlloc(GMEM_FIXED, ACLlength);
168 InitializeAcl(AdminOnlyACL, ACLlength, ACL_REVISION);
169 AddAccessAllowedAce(AdminOnlyACL, ACL_REVISION,
170 STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL,
173 /* Create security descriptor */
174 psd = GlobalAlloc(GMEM_FIXED, sizeof(SECURITY_DESCRIPTOR));
175 InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
176 SetSecurityDescriptorDacl(psd, TRUE, AdminOnlyACL, FALSE);
178 /* Create security attributes structure */
179 psa = GlobalAlloc(GMEM_FIXED, sizeof(SECURITY_ATTRIBUTES));
180 psa->nLength = sizeof(SECURITY_ATTRIBUTES);
181 psa->lpSecurityDescriptor = psd;
182 psa->bInheritHandle = FALSE;
188 /* Free a security attribute structure created by CreateCacheFileSA() */
189 VOID FreeCacheFileSA(PSECURITY_ATTRIBUTES psa)
194 GetSecurityDescriptorDacl(psa->lpSecurityDescriptor, &b1, &pAcl, &b2);
196 GlobalFree(psa->lpSecurityDescriptor);
200 static HANDLE hMemoryMappedFile = NULL;
203 cm_IsCacheValid(void)
207 afsi_log("Validating Cache Contents");
209 if (cm_ValidateACLCache()) {
210 afsi_log("ACL Cache validation failure");
212 } else if (cm_ValidateDCache()) {
213 afsi_log("Data Cache validation failure");
215 } else if (cm_ValidateVolume()) {
216 afsi_log("Volume validation failure");
218 } else if (cm_ValidateCell()) {
219 afsi_log("Cell validation failure");
221 } else if (cm_ValidateSCache()) {
222 afsi_log("Stat Cache validation failure");
230 cm_ShutdownMappedMemory(void)
232 cm_config_data_t * config_data_p = (cm_config_data_t *)cm_data.baseAddress;
235 afsi_log("Closing AFS Cache:");
236 afsi_log(" Base Address = %p", config_data_p);
237 afsi_log(" stats = %u", cm_data.stats);
238 afsi_log(" chunkSize = %u", cm_data.chunkSize);
239 afsi_log(" blockSize = %u", cm_data.blockSize);
240 afsi_log(" bufferSize = %I64u", cm_data.bufferSize);
241 afsi_log(" cacheType = %u", cm_data.cacheType);
242 afsi_log(" volumeHashTableSize = %u", cm_data.volumeHashTableSize);
243 afsi_log(" currentVolumes = %u", cm_data.currentVolumes);
244 afsi_log(" maxVolumes = %u", cm_data.maxVolumes);
245 afsi_log(" cellHashTableSize = %u", cm_data.cellHashTableSize);
246 afsi_log(" currentCells = %u", cm_data.currentCells);
247 afsi_log(" maxCells = %u", cm_data.maxCells);
248 afsi_log(" scacheHashTableSize = %u", cm_data.scacheHashTableSize);
249 afsi_log(" currentSCaches = %u", cm_data.currentSCaches);
250 afsi_log(" maxSCaches = %u", cm_data.maxSCaches);
254 cm_ShutdownACLCache();
258 if (cm_ValidateCache == 2)
259 dirty = !cm_IsCacheValid();
261 *config_data_p = cm_data;
262 config_data_p->dirty = dirty;
263 UnmapViewOfFile(config_data_p);
264 CloseHandle(hMemoryMappedFile);
265 hMemoryMappedFile = NULL;
267 afsi_log("Memory Mapped File has been closed");
272 cm_ValidateMappedMemory(char * cachePath)
274 HANDLE hf = INVALID_HANDLE_VALUE, hm;
275 PSECURITY_ATTRIBUTES psa;
276 BY_HANDLE_FILE_INFORMATION fileInfo;
278 afs_uint64 mappingSize;
279 char * baseAddress = NULL;
280 cm_config_data_t * config_data_p;
282 psa = CreateCacheFileSA();
283 hf = CreateFile( cachePath,
284 GENERIC_READ | GENERIC_WRITE,
285 FILE_SHARE_READ | FILE_SHARE_WRITE,
288 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM |
289 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_FLAG_RANDOM_ACCESS,
291 FreeCacheFileSA(psa);
293 if (hf == INVALID_HANDLE_VALUE) {
294 fprintf(stderr, "Error creating cache file \"%s\" error %d\n",
295 cachePath, GetLastError());
296 return CM_ERROR_INVAL;
299 /* The file is being re-used; check to see if the existing data can be reused */
300 if ( !GetFileInformationByHandle(hf, &fileInfo) ) {
302 fprintf(stderr, "Unable to obtain File Information\n");
303 return CM_ERROR_INVAL;
306 afsi_log("Existing File Size: %08X:%08X",
307 fileInfo.nFileSizeHigh,
308 fileInfo.nFileSizeLow);
310 hm = CreateFileMapping( hf,
314 sizeof(cm_config_data_t),
317 if (GetLastError() == ERROR_DISK_FULL) {
318 fprintf(stderr, "Error creating file mapping for \"%s\": disk full (%lX)\n",
319 cachePath, sizeof(cm_config_data_t));
321 hm = CreateFileMapping( hf,
325 fileInfo.nFileSizeLow,
328 if (GetLastError() == ERROR_DISK_FULL) {
330 return CM_ERROR_TOOMANYBUFS;
332 fprintf(stderr,"Error creating file mapping for \"%s\": %d\n",
333 cachePath, GetLastError());
335 return CM_ERROR_INVAL;
338 fprintf(stderr, "Retry with file size (%lX) succeeds",
339 fileInfo.nFileSizeLow);
342 afsi_log("Error creating file mapping for \"%s\": %d",
343 cachePath, GetLastError());
345 return CM_ERROR_INVAL;
349 config_data_p = MapViewOfFile( hm,
352 sizeof(cm_config_data_t));
353 if ( config_data_p == NULL ) {
354 fprintf(stderr, "Unable to MapViewOfFile\n");
355 if (hf != INVALID_HANDLE_VALUE)
358 return CM_ERROR_INVAL;
361 if ( config_data_p->dirty ) {
362 fprintf(stderr, "Previous session terminated prematurely\n");
363 UnmapViewOfFile(config_data_p);
366 return CM_ERROR_INVAL;
369 mappingSize = config_data_p->bufferSize;
370 baseAddress = config_data_p->baseAddress;
371 UnmapViewOfFile(config_data_p);
374 hm = CreateFileMapping( hf,
377 (DWORD)(mappingSize >> 32),
378 (DWORD)(mappingSize & 0xFFFFFFFF),
381 if (GetLastError() == ERROR_DISK_FULL) {
382 fprintf(stderr, "Error creating file mapping for \"%s\": disk full [2]\n",
385 return CM_ERROR_TOOMANYBUFS;
387 fprintf(stderr, "Error creating file mapping for \"%s\": %d\n",
388 cachePath, GetLastError());
390 return CM_ERROR_INVAL;
393 baseAddress = MapViewOfFileEx( hm,
398 if (baseAddress == NULL) {
399 fprintf(stderr, "Error mapping view of file: %d\n", GetLastError());
400 baseAddress = MapViewOfFile( hm,
403 (SIZE_T)mappingSize);
404 if (baseAddress == NULL) {
406 if (hf != INVALID_HANDLE_VALUE)
408 return CM_ERROR_INVAL;
410 fprintf(stderr, "Unable to re-load cache file at base address\n");
412 if (hf != INVALID_HANDLE_VALUE)
414 return CM_ERROR_INVAL;
418 config_data_p = (cm_config_data_t *) baseAddress;
420 fprintf(stderr,"AFS Cache data:\n");
421 fprintf(stderr," Base Address = %p\n",baseAddress);
422 fprintf(stderr," stats = %u\n", config_data_p->stats);
423 fprintf(stderr," chunkSize = %u\n", config_data_p->chunkSize);
424 fprintf(stderr," blockSize = %u\n", config_data_p->blockSize);
425 fprintf(stderr," bufferSize = %I64u\n", config_data_p->bufferSize);
426 fprintf(stderr," cacheType = %u\n", config_data_p->cacheType);
427 fprintf(stderr," volumeHashTableSize = %u", config_data_p->volumeHashTableSize);
428 fprintf(stderr," currentVolumes = %u\n", config_data_p->currentVolumes);
429 fprintf(stderr," maxVolumes = %u\n", config_data_p->maxVolumes);
430 fprintf(stderr," cellHashTableSize = %u", config_data_p->cellHashTableSize);
431 fprintf(stderr," currentCells = %u\n", config_data_p->currentCells);
432 fprintf(stderr," maxCells = %u\n", config_data_p->maxCells);
433 fprintf(stderr," scacheHashTableSize = %u\n", config_data_p->scacheHashTableSize);
434 fprintf(stderr," currentSCaches = %u\n", config_data_p->currentSCaches);
435 fprintf(stderr," maxSCaches = %u\n", config_data_p->maxSCaches);
436 cm_data = *config_data_p;
438 // perform validation of persisted data structures
439 // if there is a failure, start from scratch
440 if (!cm_IsCacheValid()) {
441 fprintf(stderr,"Cache file fails validation test\n");
442 UnmapViewOfFile(config_data_p);
443 return CM_ERROR_INVAL;
446 fprintf(stderr,"Cache passes validation test\n");
447 UnmapViewOfFile(config_data_p);
452 GetVolSerialNumber(char * cachePath)
458 if ( cachePath[0] == '\\' && cachePath[1] == '\\' ||
459 cachePath[0] == '/' && cachePath[1] == '/' )
461 rootpath[0]=rootpath[1]='\\';
462 for ( i=2; cachePath[i]; i++ ) {
463 rootpath[i] = cachePath[i];
464 if ( cachePath[i] == '\\' || cachePath[i] == '/' ) {
469 } else if ( cachePath[1] == ':' ) {
470 rootpath[0] = cachePath[0];
475 for ( ; cachePath[i]; i++ ) {
476 rootpath[i] = cachePath[i];
477 if ( cachePath[i] == '\\' || cachePath[i] == '/' ) {
484 GetVolumeInformation(rootpath, NULL, 0, &serial, NULL, NULL, NULL, 0);
488 BOOL GetTextualSid( PSID pSid, PBYTE TextualSid, LPDWORD lpdwBufferLen )
490 PSID_IDENTIFIER_AUTHORITY psia;
491 DWORD dwSubAuthorities;
492 DWORD dwSidRev=SID_REVISION;
496 // Validate the binary SID.
497 if(!IsValidSid(pSid))
500 // Get the identifier authority value from the SID.
502 psia = GetSidIdentifierAuthority(pSid);
504 // Get the number of subauthorities in the SID.
506 dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
508 // Compute the buffer length.
509 // S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL
511 dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1);
513 // Check input buffer length.
514 // If too small, indicate the proper size and set the last error.
516 if (TextualSid == NULL || *lpdwBufferLen < dwSidSize)
518 *lpdwBufferLen = dwSidSize;
519 SetLastError(ERROR_INSUFFICIENT_BUFFER);
523 // Add 'S' prefix and revision number to the string.
524 dwSidSize=sprintf(TextualSid, "S-%lu-", dwSidRev );
526 // Add a SID identifier authority to the string.
527 if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
529 dwSidSize+=sprintf(TextualSid + strlen(TextualSid),
530 "0x%02hx%02hx%02hx%02hx%02hx%02hx",
531 (USHORT)psia->Value[0],
532 (USHORT)psia->Value[1],
533 (USHORT)psia->Value[2],
534 (USHORT)psia->Value[3],
535 (USHORT)psia->Value[4],
536 (USHORT)psia->Value[5]);
540 dwSidSize+=sprintf(TextualSid + strlen(TextualSid),
542 (ULONG)(psia->Value[5] ) +
543 (ULONG)(psia->Value[4] << 8) +
544 (ULONG)(psia->Value[3] << 16) +
545 (ULONG)(psia->Value[2] << 24) );
548 // Add SID subauthorities to the string.
550 for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
552 dwSidSize+=sprintf(TextualSid + dwSidSize, "-%lu",
553 *GetSidSubAuthority(pSid, dwCounter) );
560 IsSubAuthValid( PBYTE SidData, DWORD SidLength )
565 if ( SidLength % sizeof(DWORD) == 0 ) {
566 for ( sidPtr = SidData + SidLength - 5*sizeof(DWORD);
568 sidPtr -= sizeof(DWORD) )
569 if ( ((PDWORD)sidPtr)[1] == 0x05000000 &&
570 ((PDWORD)sidPtr)[2] == 0x00000015 )
572 if ( sidPtr < SidData )
579 GetMachineSid(PBYTE SidBuffer, DWORD SidSize)
592 // Read the last subauthority of the current computer SID
594 if( RegOpenKey( HKEY_LOCAL_MACHINE, "SECURITY\\SAM\\Domains\\Account",
595 &hKey) != ERROR_SUCCESS ) {
600 RegQueryValueEx( hKey, "V", NULL, &dwType, vData, &dwLen );
601 vData = (PBYTE) malloc( dwLen );
602 dwStatus = RegQueryValueEx( hKey, "V", NULL, &dwType, vData, &dwLen );
604 if( dwStatus != ERROR_SUCCESS ) {
609 // Make sure that we're dealing with a SID we understand
611 pSid = IsSubAuthValid( vData, dwLen );
613 if( !pSid || SidSize < dwLen - (pSid - vData)) {
618 memset(SidBuffer, 0, SidSize);
619 memcpy(SidBuffer, pSid, dwLen - (pSid - vData) );
625 cm_InitMappedMemory(DWORD virtualCache, char * cachePath, DWORD stats, DWORD maxVols, DWORD maxCells,
626 DWORD chunkSize, afs_uint64 cacheBlocks, afs_uint32 blockSize)
628 HANDLE hf = INVALID_HANDLE_VALUE, hm;
629 PSECURITY_ATTRIBUTES psa;
631 afs_uint64 mappingSize;
632 DWORD volumeSerialNumber = 0;
633 DWORD sidStringSize = 0;
635 CHAR machineSid[6 * sizeof(DWORD)]="";
636 char * baseAddress = NULL;
637 cm_config_data_t * config_data_p;
640 volumeSerialNumber = GetVolSerialNumber(cachePath);
641 GetMachineSid(machineSid, sizeof(machineSid));
643 mappingSize = ComputeSizeOfMappingFile(stats, maxVols, maxCells, chunkSize, cacheBlocks, blockSize);
645 if ( !virtualCache ) {
646 psa = CreateCacheFileSA();
647 hf = CreateFile( cachePath,
648 GENERIC_READ | GENERIC_WRITE,
649 FILE_SHARE_READ | FILE_SHARE_WRITE,
652 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM |
653 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_FLAG_RANDOM_ACCESS,
655 FreeCacheFileSA(psa);
657 if (hf == INVALID_HANDLE_VALUE) {
658 afsi_log("Error creating cache file \"%s\" error %d",
659 cachePath, GetLastError());
660 return CM_ERROR_INVAL;
663 if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
664 BY_HANDLE_FILE_INFORMATION fileInfo;
666 /* The file is being re-used; check to see if the existing data can be reused */
667 afsi_log("Cache File \"%s\" already exists", cachePath);
669 if ( GetFileInformationByHandle(hf, &fileInfo) ) {
671 afsi_log("Existing File Size: %08X:%08X",
672 fileInfo.nFileSizeHigh,
673 fileInfo.nFileSizeLow);
674 filesize = fileInfo.nFileSizeHigh;
676 filesize += fileInfo.nFileSizeLow;
677 if (filesize > GranularityAdjustment(mappingSize)) {
678 psa = CreateCacheFileSA();
679 hf = CreateFile( cachePath,
680 GENERIC_READ | GENERIC_WRITE,
681 FILE_SHARE_READ | FILE_SHARE_WRITE,
684 FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM |
685 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_FLAG_RANDOM_ACCESS,
687 FreeCacheFileSA(psa);
689 if (hf == INVALID_HANDLE_VALUE) {
690 afsi_log("Error creating cache file \"%s\" error %d",
691 cachePath, GetLastError());
692 return CM_ERROR_INVAL;
695 GetFileInformationByHandle(hf, &fileInfo);
696 afsi_log(" New File Size: %08X:%08X",
697 fileInfo.nFileSizeHigh,
698 fileInfo.nFileSizeLow);
702 hm = CreateFileMapping( hf,
706 sizeof(cm_config_data_t),
709 if (GetLastError() == ERROR_DISK_FULL) {
710 afsi_log("Error creating file mapping for \"%s\": disk full (%lX)",
711 cachePath, sizeof(cm_config_data_t));
713 hm = CreateFileMapping( hf,
716 (DWORD)(mappingSize >> 32),
717 (DWORD)(mappingSize & 0xFFFFFFFF),
720 if (GetLastError() == ERROR_DISK_FULL) {
722 return CM_ERROR_TOOMANYBUFS;
724 afsi_log("Error creating file mapping for \"%s\": %d",
725 cachePath, GetLastError());
727 return CM_ERROR_INVAL;
730 afsi_log("Retry with mapping size (%lX) succeeds", mappingSize);
733 afsi_log("Error creating file mapping for \"%s\": %d",
734 cachePath, GetLastError());
736 return CM_ERROR_INVAL;
740 config_data_p = MapViewOfFile( hm,
743 sizeof(cm_config_data_t));
744 if ( config_data_p == NULL ) {
745 if (hf != INVALID_HANDLE_VALUE)
748 return CM_ERROR_INVAL;
751 if ( config_data_p->size == sizeof(cm_config_data_t) &&
752 config_data_p->magic == CM_CONFIG_DATA_MAGIC &&
753 config_data_p->stats == stats &&
754 config_data_p->maxVolumes == maxVols &&
755 config_data_p->maxCells == maxCells &&
756 config_data_p->chunkSize == chunkSize &&
757 config_data_p->buf_nbuffers == cacheBlocks &&
758 config_data_p->blockSize == blockSize &&
759 config_data_p->bufferSize == mappingSize)
761 if ( config_data_p->dirty ) {
762 afsi_log("Previous session terminated prematurely");
764 baseAddress = config_data_p->baseAddress;
768 afsi_log("Configuration changed or Not a persistent cache file");
770 UnmapViewOfFile(config_data_p);
775 hm = CreateFileMapping( hf,
778 (DWORD)(mappingSize >> 32),
779 (DWORD)(mappingSize & 0xFFFFFFFF),
782 if (GetLastError() == ERROR_DISK_FULL) {
783 afsi_log("Error creating file mapping for \"%s\": disk full [2]",
785 return CM_ERROR_TOOMANYBUFS;
787 afsi_log("Error creating file mapping for \"%s\": %d",
788 cachePath, GetLastError());
789 return CM_ERROR_INVAL;
791 baseAddress = MapViewOfFileEx( hm,
797 if (baseAddress == NULL) {
798 afsi_log("Error mapping view of file: %d", GetLastError());
799 baseAddress = MapViewOfFile( hm,
803 (SIZE_T)mappingSize);
804 if (baseAddress == NULL) {
805 if (hf != INVALID_HANDLE_VALUE)
808 return CM_ERROR_INVAL;
814 config_data_p = (cm_config_data_t *) baseAddress;
817 afsi_log("Reusing existing AFS Cache data:");
818 cm_data = *config_data_p;
820 afsi_log(" Base Address = %p",baseAddress);
821 afsi_log(" stats = %u", config_data_p->stats);
822 afsi_log(" chunkSize = %u", config_data_p->chunkSize);
823 afsi_log(" blockSize = %u", config_data_p->blockSize);
824 afsi_log(" bufferSize = %I64u", config_data_p->bufferSize);
825 afsi_log(" cacheType = %u", config_data_p->cacheType);
826 afsi_log(" volumeHashTableSize = %u", config_data_p->volumeHashTableSize);
827 afsi_log(" currentVolumes = %u", config_data_p->currentVolumes);
828 afsi_log(" maxVolumes = %u", config_data_p->maxVolumes);
829 afsi_log(" cellHashTableSize = %u", config_data_p->cellHashTableSize);
830 afsi_log(" currentCells = %u", config_data_p->currentCells);
831 afsi_log(" maxCells = %u", config_data_p->maxCells);
832 afsi_log(" scacheHashTableSize = %u", config_data_p->scacheHashTableSize);
833 afsi_log(" currentSCaches = %u", config_data_p->currentSCaches);
834 afsi_log(" maxSCaches = %u", config_data_p->maxSCaches);
836 // perform validation of persisted data structures
837 // if there is a failure, start from scratch
838 if (cm_ValidateCache && !cm_IsCacheValid()) {
844 afsi_log("Building AFS Cache from scratch");
845 memset(&cm_data, 0, sizeof(cm_config_data_t));
846 cm_data.size = sizeof(cm_config_data_t);
847 cm_data.magic = CM_CONFIG_DATA_MAGIC;
848 cm_data.baseAddress = baseAddress;
849 cm_data.stats = stats;
850 cm_data.chunkSize = chunkSize;
851 cm_data.blockSize = blockSize;
852 cm_data.bufferSize = mappingSize;
853 cm_data.scacheHashTableSize = osi_PrimeLessThan(stats / 2 + 1);
854 cm_data.volumeHashTableSize = osi_PrimeLessThan((afs_uint32)(maxVols/7 + 1));
855 cm_data.cellHashTableSize = osi_PrimeLessThan((afs_uint32)(maxCells/7 + 1));
857 cm_data.cacheType = CM_BUF_CACHETYPE_VIRTUAL;
859 cm_data.cacheType = CM_BUF_CACHETYPE_FILE;
862 cm_data.buf_nbuffers = cacheBlocks;
863 cm_data.buf_nOrigBuffers = 0;
864 cm_data.buf_blockSize = blockSize;
865 cm_data.buf_hashSize = osi_PrimeLessThan((afs_uint32)(cacheBlocks/7 + 1));
867 cm_data.mountRootGen = time(NULL);
869 baseAddress += ComputeSizeOfConfigData();
870 cm_data.volumeBaseAddress = (cm_volume_t *) baseAddress;
871 baseAddress += ComputeSizeOfVolumes(maxVols);
872 cm_data.volumeNameHashTablep = (cm_volume_t **)baseAddress;
873 baseAddress += ComputeSizeOfVolumeHT(maxVols);
874 cm_data.volumeRWIDHashTablep = (cm_volume_t **)baseAddress;
875 baseAddress += ComputeSizeOfVolumeHT(maxVols);
876 cm_data.volumeROIDHashTablep = (cm_volume_t **)baseAddress;
877 baseAddress += ComputeSizeOfVolumeHT(maxVols);
878 cm_data.volumeBKIDHashTablep = (cm_volume_t **)baseAddress;
879 baseAddress += ComputeSizeOfVolumeHT(maxVols);
880 cm_data.cellNameHashTablep = (cm_cell_t **)baseAddress;
881 baseAddress += ComputeSizeOfCellHT(maxCells);
882 cm_data.cellIDHashTablep = (cm_cell_t **)baseAddress;
883 baseAddress += ComputeSizeOfCellHT(maxCells);
884 cm_data.cellBaseAddress = (cm_cell_t *) baseAddress;
885 baseAddress += ComputeSizeOfCells(maxCells);
886 cm_data.aclBaseAddress = (cm_aclent_t *) baseAddress;
887 baseAddress += ComputeSizeOfACLCache(stats);
888 cm_data.scacheBaseAddress = (cm_scache_t *) baseAddress;
889 baseAddress += ComputeSizeOfSCache(stats);
890 cm_data.scacheHashTablep = (cm_scache_t **) baseAddress;
891 baseAddress += ComputeSizeOfSCacheHT(stats);
892 cm_data.dnlcBaseAddress = (cm_nc_t *) baseAddress;
893 baseAddress += ComputeSizeOfDNLCache();
894 cm_data.buf_scacheHashTablepp = (cm_buf_t **) baseAddress;
895 baseAddress += ComputeSizeOfDataHT(cacheBlocks);
896 cm_data.buf_fileHashTablepp = (cm_buf_t **) baseAddress;
897 baseAddress += ComputeSizeOfDataHT(cacheBlocks);
898 cm_data.bufHeaderBaseAddress = (cm_buf_t *) baseAddress;
899 baseAddress += ComputeSizeOfDataHeaders(cacheBlocks);
900 cm_data.bufDataBaseAddress = (char *) baseAddress;
901 baseAddress += ComputeSizeOfDataBuffers(cacheBlocks, blockSize);
902 cm_data.bufEndOfData = (char *) baseAddress;
903 cm_data.buf_dirtyListp = NULL;
904 cm_data.buf_dirtyListEndp = NULL;
905 cm_data.fakeDirVersion = 0x8;
906 UuidCreate((UUID *)&cm_data.Uuid);
907 cm_data.volSerialNumber = volumeSerialNumber;
908 memcpy(cm_data.Sid, machineSid, sizeof(machineSid));
912 if ( volumeSerialNumber == 0 || volumeSerialNumber != cm_data.volSerialNumber ) {
914 afsi_log("Volume serial number change, generating new UUID");
915 afsi_log("Old volume Serial Number: 0x%x", cm_data.volSerialNumber);
916 } else if ( machineSid[0] && memcmp(machineSid, cm_data.Sid, sizeof(machineSid))) {
918 afsi_log("Machine Sid changed, generating new UUID");
919 GetTextualSid( (PSID)cm_data.Sid, NULL, &sidStringSize );
921 p = malloc(sidStringSize * sizeof(TCHAR));
923 rc = GetTextualSid( (PSID)cm_data.Sid, p, &sidStringSize );
924 afsi_log("Old Machine SID: %s", rc ? p : "unknown");
928 afsi_log("Old Machine SID: unknown");
933 UuidCreate((UUID *)&cm_data.Uuid);
934 cm_data.volSerialNumber = volumeSerialNumber;
935 memcpy(cm_data.Sid, machineSid, sizeof(machineSid));
939 afsi_log("Volume Serial Number: 0x%x", cm_data.volSerialNumber);
941 GetTextualSid( (PSID)cm_data.Sid, NULL, &sidStringSize );
943 p = malloc(sidStringSize * sizeof(TCHAR));
945 rc = GetTextualSid( (PSID)cm_data.Sid, p, &sidStringSize );
946 afsi_log("Machine SID: %s", rc ? p : "unknown");
950 afsi_log("Machine SID: unknown");
953 UuidToString((UUID *)&cm_data.Uuid, &p);
954 afsi_log("Initializing Uuid to %s",p);
957 afsi_log("Initializing Volume Data");
958 cm_InitVolume(newFile, maxVols);
960 afsi_log("Initializing Cell Data");
961 cm_InitCell(newFile, maxCells);
963 afsi_log("Initializing ACL Data");
964 cm_InitACLCache(newFile, 2*stats);
966 afsi_log("Initializing Stat Data");
967 cm_InitSCache(newFile, stats);
969 afsi_log("Initializing Data Buffers");
970 cm_InitDCache(newFile, 0, cacheBlocks);
972 *config_data_p = cm_data;
973 config_data_p->dirty = 1;
975 hMemoryMappedFile = hf;
976 afsi_log("Cache Initialization Complete");