11 #include <WINNT/afsreg.h>
15 #ifdef AFS_FREELANCE_CLIENT
16 #include "cm_freelance.h"
19 extern void afsi_log(char *pattern, ...);
21 static int cm_noLocalMountPoints;
22 char * cm_FakeRootDir = NULL;
23 int cm_fakeDirSize = 0;
24 int cm_fakeDirCallback=0;
25 int cm_fakeGettingCallback=0;
26 static cm_localMountPoint_t* cm_localMountPoints;
27 osi_mutex_t cm_Freelance_Lock;
28 static int cm_localMountPointChangeFlag = 0;
29 int cm_freelanceEnabled = 1;
30 time_t FakeFreelanceModTime = 0x3b49f6e2;
32 static int freelance_ShutdownFlag = 0;
33 static HANDLE hFreelanceChangeEvent = 0;
34 static HANDLE hFreelanceSymlinkChangeEvent = 0;
36 void cm_InitFakeRootDir();
38 void cm_FreelanceChangeNotifier(void * parmp) {
41 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
42 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
45 &hkFreelance) == ERROR_SUCCESS) {
47 hFreelanceChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
48 if (hFreelanceChangeEvent == NULL) {
49 RegCloseKey(hkFreelance);
55 /* check hFreelanceChangeEvent to see if it is set.
56 * if so, call cm_noteLocalMountPointChange()
58 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
59 FALSE, /* bWatchSubtree */
60 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
61 hFreelanceChangeEvent, /* hEvent */
62 TRUE /* fAsynchronous */
64 RegCloseKey(hkFreelance);
65 CloseHandle(hFreelanceChangeEvent);
66 hFreelanceChangeEvent = 0;
70 if (WaitForSingleObject(hFreelanceChangeEvent, INFINITE) == WAIT_OBJECT_0)
72 if (freelance_ShutdownFlag == 1) {
73 RegCloseKey(hkFreelance);
74 CloseHandle(hFreelanceChangeEvent);
75 hFreelanceChangeEvent = 0;
78 cm_noteLocalMountPointChange();
83 void cm_FreelanceSymlinkChangeNotifier(void * parmp) {
86 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
87 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
90 &hkFreelance) == ERROR_SUCCESS) {
92 hFreelanceSymlinkChangeEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
93 if (hFreelanceSymlinkChangeEvent == NULL) {
94 RegCloseKey(hkFreelance);
100 /* check hFreelanceSymlinkChangeEvent to see if it is set.
101 * if so, call cm_noteLocalMountPointSymlinkChange()
103 if (RegNotifyChangeKeyValue( hkFreelance, /* hKey */
104 FALSE, /* bWatchSubtree */
105 REG_NOTIFY_CHANGE_LAST_SET, /* dwNotifyFilter */
106 hFreelanceSymlinkChangeEvent, /* hEvent */
107 TRUE /* fAsynchronous */
108 ) != ERROR_SUCCESS) {
109 RegCloseKey(hkFreelance);
110 CloseHandle(hFreelanceSymlinkChangeEvent);
111 hFreelanceSymlinkChangeEvent = 0;
115 if (WaitForSingleObject(hFreelanceSymlinkChangeEvent, INFINITE) == WAIT_OBJECT_0)
117 if (freelance_ShutdownFlag == 1) {
118 RegCloseKey(hkFreelance);
119 CloseHandle(hFreelanceSymlinkChangeEvent);
120 hFreelanceSymlinkChangeEvent = 0;
123 cm_noteLocalMountPointChange();
129 cm_FreelanceShutdown(void)
131 freelance_ShutdownFlag = 1;
132 if (hFreelanceChangeEvent != 0)
133 thrd_SetEvent(hFreelanceChangeEvent);
134 if (hFreelanceSymlinkChangeEvent != 0)
135 thrd_SetEvent(hFreelanceSymlinkChangeEvent);
138 void cm_InitFreelance() {
142 lock_InitializeMutex(&cm_Freelance_Lock, "Freelance Lock", LOCK_HIERARCHY_FREELANCE_GLOBAL);
144 lock_ObtainMutex(&cm_Freelance_Lock);
145 // yj: first we make a call to cm_initLocalMountPoints
146 // to read all the local mount points from the registry
147 cm_InitLocalMountPoints();
149 // then we make a call to InitFakeRootDir to create
150 // a fake root directory based on the local mount points
151 cm_InitFakeRootDir();
152 // --- end of yj code
153 lock_ReleaseMutex(&cm_Freelance_Lock);
155 /* Start the registry monitor */
156 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceChangeNotifier,
157 NULL, 0, &lpid, "cm_FreelanceChangeNotifier");
158 osi_assertx(phandle != NULL, "cm_FreelanceChangeNotifier thread create failure");
159 thrd_CloseHandle(phandle);
161 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceSymlinkChangeNotifier,
162 NULL, 0, &lpid, "cm_FreelanceSymlinkChangeNotifier");
163 osi_assertx(phandle != NULL, "cm_FreelanceSymlinkChangeNotifier thread create failure");
164 thrd_CloseHandle(phandle);
167 /* yj: Initialization of the fake root directory */
168 /* to be called while holding freelance lock. */
169 void cm_InitFakeRootDir() {
174 // allocate space for the fake info
175 cm_dirHeader_t fakeDirHeader;
176 cm_dirEntry_t fakeEntry;
177 cm_pageHeader_t fakePageHeader;
179 // i'm going to calculate how much space is needed for
180 // this fake root directory. we have these rules:
181 // 1. there are cm_noLocalMountPoints number of entries
182 // 2. each page is CM_DIR_PAGESIZE in size
183 // 3. the first 13 chunks of the first page are used for
185 // 4. the first chunk of all subsequent pages are used
186 // for page header stuff
187 // 5. a max of CM_DIR_EPP entries are allowed per page
188 // 6. each entry takes 1 or more chunks, depending on
189 // the size of the mount point string, as determined
191 // 7. each chunk is CM_DIR_CHUNKSIZE bytes
193 int CPP = CM_DIR_PAGESIZE / CM_DIR_CHUNKSIZE;
194 int curChunk = 13; // chunks 0 - 12 are used for header stuff
195 // of the first page in the directory
198 int curDirEntryInPage = 0;
202 /* Reserve 2 directory chunks for "." and ".." */
205 while (curDirEntry<cm_noLocalMountPoints) {
206 sizeOfCurEntry = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
207 if ((curChunk + sizeOfCurEntry >= CPP) ||
208 (curDirEntryInPage + 1 >= CM_DIR_EPP)) {
210 curDirEntryInPage = 0;
213 curChunk += sizeOfCurEntry;
218 dirSize = (curPage+1) * CM_DIR_PAGESIZE;
219 if (cm_fakeDirSize != dirSize) {
221 free(cm_FakeRootDir);
222 cm_FakeRootDir = malloc(dirSize);
223 cm_fakeDirSize = dirSize;
226 // yj: when we get here, we've figured out how much memory we need and
227 // allocated the appropriate space for it. we now prceed to fill
228 // it up with entries.
231 curDirEntryInPage = 0;
234 // fields in the directory entry that are unused.
236 fakeEntry.length = 0;
238 fakeEntry.fid.unique = htonl(1);
240 // the first page is special, it uses fakeDirHeader instead of fakePageHeader
241 // we fill up the page with dirEntries that belong there and we make changes
242 // to the fakeDirHeader.header.freeBitmap along the way. Then when we're done
243 // filling up the dirEntries in this page, we copy the fakeDirHeader into
244 // the top of the page.
246 // init the freeBitmap array
248 fakeDirHeader.header.freeBitmap[i]=0;
250 fakeDirHeader.header.freeBitmap[0] = 0xff;
251 fakeDirHeader.header.freeBitmap[1] = 0x7f;
254 // we start counting at 13 because the 0th to 12th chunks are used for header
257 // stick the first 2 entries "." and ".." in
258 fakeEntry.fid.vnode = htonl(1);
259 strcpy(fakeEntry.name, ".");
260 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
261 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
262 curChunk++; curDirEntryInPage++;
263 strcpy(fakeEntry.name, "..");
264 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
265 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
266 curChunk++; curDirEntryInPage++;
268 // keep putting stuff into page 0 if
269 // 1. we're not done with all entries
270 // 2. we have less than CM_DIR_EPP entries in page 0
271 // 3. we're not out of chunks in page 0
273 while( (curDirEntry<cm_noLocalMountPoints) &&
274 (curDirEntryInPage < CM_DIR_EPP) &&
275 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
278 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
279 /* enforce the rule that only directories have odd vnode values */
280 fakeEntry.fid.vnode = htonl((curDirEntry + 1) * 2);
281 fakeEntry.fid.unique = htonl(curDirEntry + 1);
282 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
284 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
285 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
288 for (i=0; i<noChunks; i++) {
289 t1 = (curChunk + i) / 8;
290 t2 = curChunk + i - (t1*8);
291 fakeDirHeader.header.freeBitmap[t1] |= (1 << t2);
296 // when we get here, we're done with filling in the entries for page 0
297 // copy in the header info
299 memcpy(cm_FakeRootDir, &fakeDirHeader, 13 * CM_DIR_CHUNKSIZE);
303 // ok, page 0's done. Move on to the next page.
304 while (curDirEntry<cm_noLocalMountPoints) {
306 curChunk = 1; // the zeroth chunk is reserved for page header
307 curDirEntryInPage = 0;
308 for (i=0; i<8; i++) {
309 fakePageHeader.freeBitmap[i]=0;
311 fakePageHeader.freeCount = 0;
312 fakePageHeader.pgcount = 0;
313 fakePageHeader.tag = htons(1234);
315 // while we're on the same page...
316 while ( (curDirEntry<cm_noLocalMountPoints) &&
317 (curDirEntryInPage < CM_DIR_EPP) &&
318 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
320 // add an entry to this page
322 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
323 /* enforce the rule that only directories have odd vnode values */
324 fakeEntry.fid.vnode = htonl((curDirEntry + 1) * 2);
325 fakeEntry.fid.unique = htonl(curDirEntry + 1);
326 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
327 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
328 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
331 for (i=0; i<noChunks; i++) {
332 t1 = (curChunk + i) / 8;
333 t2 = curChunk + i - (t1*8);
334 fakePageHeader.freeBitmap[t1] |= (1 << t2);
338 memcpy(cm_FakeRootDir + curPage * CM_DIR_PAGESIZE, &fakePageHeader, sizeof(fakePageHeader));
343 // we know the fakeDir is setup properly, so we claim that we have callback
344 osi_Log0(afsd_logp,"cm_InitFakeRootDir fakeDirCallback=1");
345 cm_fakeDirCallback=1;
347 // when we get here, we've set up everything! done!
350 int cm_FakeRootFid(cm_fid_t *fidp)
353 AFS_FAKE_ROOT_CELL_ID, /* root cell */
354 AFS_FAKE_ROOT_VOL_ID, /* root.afs ? */
359 /* called directly from ioctl */
360 /* called while not holding freelance lock */
361 int cm_noteLocalMountPointChange(void) {
362 lock_ObtainMutex(&cm_Freelance_Lock);
363 cm_data.fakeDirVersion++;
364 cm_localMountPointChangeFlag = 1;
365 lock_ReleaseMutex(&cm_Freelance_Lock);
369 int cm_getLocalMountPointChange() {
370 return cm_localMountPointChangeFlag;
373 int cm_clearLocalMountPointChange() {
374 cm_localMountPointChangeFlag = 0;
378 int cm_reInitLocalMountPoints() {
381 cm_scache_t *scp, **lscpp, *tscp;
383 osi_Log0(afsd_logp,"----- freelance reinitialization starts ----- ");
385 // first we invalidate all the SCPs that were created
386 // for the local mount points
388 osi_Log0(afsd_logp,"Invalidating local mount point scp... ");
390 lock_ObtainWrite(&cm_scacheLock);
391 lock_ObtainMutex(&cm_Freelance_Lock); /* always scache then freelance lock */
392 for (i=0; i<=cm_noLocalMountPoints; i++) {
394 cm_SetFid(&aFid, AFS_FAKE_ROOT_CELL_ID, AFS_FAKE_ROOT_VOL_ID, 1, 1);
396 cm_SetFid(&aFid, AFS_FAKE_ROOT_CELL_ID, AFS_FAKE_ROOT_VOL_ID, i*2, i);
397 hash = CM_SCACHE_HASH(&aFid);
398 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
399 if (scp != cm_data.rootSCachep && cm_FidCmp(&scp->fid, &aFid) == 0) {
400 // mark the scp to be reused
401 cm_HoldSCacheNoLock(scp);
402 lock_ReleaseMutex(&cm_Freelance_Lock);
403 lock_ReleaseWrite(&cm_scacheLock);
404 lock_ObtainWrite(&scp->rw);
405 cm_DiscardSCache(scp);
407 // take the scp out of the hash
408 lock_ObtainWrite(&cm_scacheLock);
409 for (lscpp = &cm_data.scacheHashTablep[hash], tscp = cm_data.scacheHashTablep[hash];
411 lscpp = &tscp->nextp, tscp = tscp->nextp) {
415 scp->flags &= ~CM_SCACHEFLAG_INHASH;
420 lock_ReleaseWrite(&scp->rw);
421 lock_ReleaseWrite(&cm_scacheLock);
422 cm_CallbackNotifyChange(scp);
423 lock_ObtainWrite(&cm_scacheLock);
424 cm_ReleaseSCacheNoLock(scp);
425 lock_ObtainMutex(&cm_Freelance_Lock);
429 lock_ReleaseWrite(&cm_scacheLock);
430 osi_Log0(afsd_logp,"\tall old scp cleared!");
432 // we must free the memory that was allocated in the prev
433 // cm_InitLocalMountPoints call
434 osi_Log0(afsd_logp,"Removing old localmountpoints... ");
435 free(cm_localMountPoints);
436 osi_Log0(afsd_logp,"\tall old localmountpoints cleared!");
438 // now re-init the localmountpoints
439 osi_Log0(afsd_logp,"Creating new localmountpoints... ");
440 cm_InitLocalMountPoints();
441 osi_Log0(afsd_logp,"\tcreated new set of localmountpoints!");
443 // then we re-create that dir
444 osi_Log0(afsd_logp,"Creating new fakedir... ");
445 cm_InitFakeRootDir();
446 osi_Log0(afsd_logp,"\t\tcreated new fakedir!");
448 lock_ReleaseMutex(&cm_Freelance_Lock);
450 osi_Log0(afsd_logp,"----- freelance reinit complete -----");
455 // yj: open up the registry and read all the local mount
456 // points that are stored there. Part of the initialization
457 // process for the freelance client.
458 /* to be called while holding freelance lock. */
459 long cm_InitLocalMountPoints() {
464 cm_localMountPoint_t* aLocalMountPoint;
467 char rootCellName[256];
468 HKEY hkFreelance = 0, hkFreelanceSymlinks = 0;
469 DWORD dwType, dwSize;
470 DWORD dwMountPoints = 0;
472 DWORD dwSymlinks = 0;
473 FILETIME ftLastWriteTime;
475 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
476 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
478 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
479 &hkFreelance) == ERROR_SUCCESS) {
481 RegQueryInfoKey( hkFreelance,
484 NULL, /* lpReserved */
485 NULL, /* lpcSubKeys */
486 NULL, /* lpcMaxSubKeyLen */
487 NULL, /* lpcMaxClassLen */
488 &dwMountPoints, /* lpcValues */
489 NULL, /* lpcMaxValueNameLen */
490 NULL, /* lpcMaxValueLen */
491 NULL, /* lpcbSecurityDescriptor */
492 &ftLastWriteTime /* lpftLastWriteTime */
495 cm_UnixTimeFromLargeSearchTime(&FakeFreelanceModTime, &ftLastWriteTime);
497 if ( dwMountPoints == 0 ) {
498 rootCellName[0] = '.';
499 code = cm_GetRootCellName(&rootCellName[1]);
501 lock_ReleaseMutex(&cm_Freelance_Lock);
502 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
503 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
504 cm_FreelanceAddMount(".root", &rootCellName[1], "root.afs.", 1, NULL);
505 lock_ObtainMutex(&cm_Freelance_Lock);
510 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
511 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
514 REG_OPTION_NON_VOLATILE,
515 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
517 &hkFreelanceSymlinks,
518 NULL) == ERROR_SUCCESS) {
520 RegQueryInfoKey( hkFreelanceSymlinks,
523 NULL, /* lpReserved */
524 NULL, /* lpcSubKeys */
525 NULL, /* lpcMaxSubKeyLen */
526 NULL, /* lpcMaxClassLen */
527 &dwSymlinks, /* lpcValues */
528 NULL, /* lpcMaxValueNameLen */
529 NULL, /* lpcMaxValueLen */
530 NULL, /* lpcbSecurityDescriptor */
531 NULL /* lpftLastWriteTime */
535 // get the number of entries there are from the first line
537 cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
539 // create space to store the local mount points
540 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
541 aLocalMountPoint = cm_localMountPoints;
543 // now we read n lines and parse them into local mount points
544 // where n is the number of local mount points there are, as
546 // Each line in the ini file represents 1 local mount point and
547 // is in the format xxx#yyy:zzz, where xxx is the directory
548 // entry name, yyy is the cell name and zzz is the volume name.
549 // #yyy:zzz together make up the mount point.
550 for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
551 TCHAR szValueName[16];
552 DWORD dwValueSize = 16;
553 dwSize = sizeof(line);
554 if (RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
555 &dwType, line, &dwSize))
557 afsi_log("RegEnumValue(hkFreelance) failed");
558 cm_noLocalMountPoints--;
562 afsi_log("Mountpoint[%d] = %s",dwIndex, line);
564 /* find the trailing dot; null terminate after it */
565 t2 = strrchr(line, '.');
569 for ( t=line;*t;t++ ) {
570 if ( !isprint(*t) ) {
571 afsi_log("error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
572 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
573 cm_noLocalMountPoints--;
578 // line is not empty, so let's parse it
579 t = strchr(line, '#');
581 t = strchr(line, '%');
582 // make sure that there is a '#' or '%' separator in the line
584 afsi_log("error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
585 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
586 cm_noLocalMountPoints--;
590 aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
591 aLocalMountPoint->namep=malloc(t-line+1);
592 strncpy(aLocalMountPoint->namep, line, t-line);
593 aLocalMountPoint->namep[t-line] = '\0';
595 /* copy the mount point string */
596 aLocalMountPoint->mountPointStringp=malloc(strlen(t));
597 strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
598 aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
600 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
601 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
602 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
607 for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
608 TCHAR szValueName[16];
609 DWORD dwValueSize = 16;
610 dwSize = sizeof(line);
611 if (RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
612 &dwType, line, &dwSize))
614 afsi_log("RegEnumValue(hkFreelanceSymlinks) failed");
615 cm_noLocalMountPoints--;
619 afsi_log("Symlink[%d] = %s",dwIndex, line);
621 /* find the trailing dot; null terminate after it */
622 t2 = strrchr(line, '.');
626 for ( t=line;*t;t++ ) {
627 if ( !isprint(*t) ) {
628 afsi_log("error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
629 fprintf(stderr, "error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
630 cm_noLocalMountPoints--;
635 // line is not empty, so let's parse it
636 t = strchr(line, ':');
638 // make sure that there is a ':' separator in the line
640 afsi_log("error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
641 fprintf(stderr, "error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
642 cm_noLocalMountPoints--;
646 aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
647 aLocalMountPoint->namep=malloc(t-line+1);
648 strncpy(aLocalMountPoint->namep, line, t-line);
649 aLocalMountPoint->namep[t-line] = '\0';
651 /* copy the symlink string */
652 aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
653 strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
654 aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
656 osi_Log2(afsd_logp,"found symlink: name %s, string %s",
657 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
658 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
663 if ( hkFreelanceSymlinks )
664 RegCloseKey( hkFreelanceSymlinks );
665 RegCloseKey(hkFreelance);
669 /* What follows is the old code to read freelance mount points
670 * out of a text file modified to copy the data into the registry
672 cm_GetConfigDir(hdir, sizeof(hdir));
673 strcat(hdir, AFS_FREELANCE_INI);
674 // open the ini file for reading
675 fp = fopen(hdir, "r");
677 /* look in the Windows directory where we used to store the file */
678 GetWindowsDirectory(hdir, sizeof(hdir));
680 strcat(hdir, AFS_FREELANCE_INI);
681 fp = fopen(hdir, "r");
684 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
685 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
688 REG_OPTION_NON_VOLATILE,
696 RegCloseKey(hkFreelance);
697 rootCellName[0] = '.';
698 code = cm_GetRootCellName(&rootCellName[1]);
700 lock_ReleaseMutex(&cm_Freelance_Lock);
701 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
702 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
703 cm_FreelanceAddMount(".root", &rootCellName[1], "root.afs.", 1, NULL);
704 lock_ObtainMutex(&cm_Freelance_Lock);
709 // we successfully opened the file
710 osi_Log0(afsd_logp,"opened afs_freelance.ini");
712 // now we read the first line to see how many entries
714 fgets(line, sizeof(line), fp);
716 // if the line is empty at any point when we're reading
717 // we're screwed. report error and return.
719 afsi_log("error occurred while reading afs_freelance.ini");
720 fprintf(stderr, "error occurred while reading afs_freelance.ini");
724 // get the number of entries there are from the first line
726 cm_noLocalMountPoints = atoi(line);
728 if (cm_noLocalMountPoints > 0) {
729 // create space to store the local mount points
730 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
731 aLocalMountPoint = cm_localMountPoints;
734 // now we read n lines and parse them into local mount points
735 // where n is the number of local mount points there are, as
737 // Each line in the ini file represents 1 local mount point and
738 // is in the format xxx#yyy:zzz, where xxx is the directory
739 // entry name, yyy is the cell name and zzz is the volume name.
740 // #yyy:zzz together make up the mount point.
741 for (i=0; i<cm_noLocalMountPoints; i++) {
742 fgets(line, sizeof(line), fp);
743 // check that the line is not empty
745 afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
746 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
750 /* find the trailing dot; null terminate after it */
751 t2 = strrchr(line, '.');
757 /* we are migrating to the registry */
758 sprintf(szIndex,"%d",dwIndex++);
760 dwSize = (DWORD)strlen(line) + 1;
761 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
764 // line is not empty, so let's parse it
765 t = strchr(line, '#');
767 t = strchr(line, '%');
768 // make sure that there is a '#' or '%' separator in the line
770 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
771 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
774 aLocalMountPoint->namep=malloc(t-line+1);
775 memcpy(aLocalMountPoint->namep, line, t-line);
776 *(aLocalMountPoint->namep + (t-line)) = 0;
778 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
779 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
780 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
782 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
783 aLocalMountPoint->namep,
784 aLocalMountPoint->mountPointStringp);
790 RegCloseKey(hkFreelance);
796 int cm_getNoLocalMountPoints() {
797 return cm_noLocalMountPoints;
800 long cm_FreelanceMountPointExists(char * filename, int prefix_ok)
806 HKEY hkFreelance = 0;
807 DWORD dwType, dwSize;
811 lock_ObtainMutex(&cm_Freelance_Lock);
813 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
814 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
816 KEY_READ|KEY_QUERY_VALUE,
817 &hkFreelance) == ERROR_SUCCESS)
819 RegQueryInfoKey( hkFreelance,
822 NULL, /* lpReserved */
823 NULL, /* lpcSubKeys */
824 NULL, /* lpcMaxSubKeyLen */
825 NULL, /* lpcMaxClassLen */
826 &dwMountPoints, /* lpcValues */
827 NULL, /* lpcMaxValueNameLen */
828 NULL, /* lpcMaxValueLen */
829 NULL, /* lpcbSecurityDescriptor */
830 NULL /* lpftLastWriteTime */
833 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
834 TCHAR szValueName[16];
835 DWORD dwValueSize = 16;
836 dwSize = sizeof(line);
837 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
838 &dwType, line, &dwSize);
840 cp=strchr(line, '#');
842 cp=strchr(line, '%');
843 memcpy(shortname, line, cp-line);
844 shortname[cp-line]=0;
846 if (!strcmp(shortname, filename)) {
851 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
852 TCHAR szValueName[16];
853 DWORD dwValueSize = 16;
854 dwSize = sizeof(line);
855 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
856 &dwType, line, &dwSize);
858 cp=strchr(line, '#');
860 cp=strchr(line, '%');
861 memcpy(shortname, line, cp-line);
862 shortname[cp-line]=0;
864 if (!cm_stricmp_utf8(shortname, filename)) {
869 if (prefix_ok && strlen(shortname) - strlen(filename) == 1 && !strncmp(shortname, filename, strlen(filename))) {
874 RegCloseKey(hkFreelance);
877 lock_ReleaseMutex(&cm_Freelance_Lock);
882 long cm_FreelanceSymlinkExists(char * filename, int prefix_ok)
888 HKEY hkFreelance = 0;
889 DWORD dwType, dwSize;
893 lock_ObtainMutex(&cm_Freelance_Lock);
895 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
896 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
898 KEY_READ|KEY_QUERY_VALUE,
899 &hkFreelance) == ERROR_SUCCESS)
901 RegQueryInfoKey( hkFreelance,
904 NULL, /* lpReserved */
905 NULL, /* lpcSubKeys */
906 NULL, /* lpcMaxSubKeyLen */
907 NULL, /* lpcMaxClassLen */
908 &dwSymlinks, /* lpcValues */
909 NULL, /* lpcMaxValueNameLen */
910 NULL, /* lpcMaxValueLen */
911 NULL, /* lpcbSecurityDescriptor */
912 NULL /* lpftLastWriteTime */
915 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
916 TCHAR szValueName[16];
917 DWORD dwValueSize = 16;
918 dwSize = sizeof(line);
919 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
920 &dwType, line, &dwSize);
922 cp=strchr(line, ':');
923 memcpy(shortname, line, cp-line);
924 shortname[cp-line]=0;
926 if (!strcmp(shortname, filename)) {
931 if (prefix_ok && strlen(shortname) - strlen(filename) == 1 && !strncmp(shortname, filename, strlen(filename))) {
936 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
937 TCHAR szValueName[16];
938 DWORD dwValueSize = 16;
939 dwSize = sizeof(line);
940 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
941 &dwType, line, &dwSize);
943 cp=strchr(line, ':');
944 memcpy(shortname, line, cp-line);
945 shortname[cp-line]=0;
947 if (!cm_stricmp_utf8(shortname, filename)) {
952 RegCloseKey(hkFreelance);
955 lock_ReleaseMutex(&cm_Freelance_Lock);
960 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
965 char fullname[CELL_MAXNAMELEN];
968 HKEY hkFreelance = 0;
969 DWORD dwType, dwSize;
973 /* before adding, verify the cell name; if it is not a valid cell,
974 don't add the mount point.
975 allow partial matches as a means of poor man's alias. */
976 /* major performance issue? */
977 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
978 osi_LogSaveString(afsd_logp,filename),
979 osi_LogSaveString(afsd_logp,cellname),
980 osi_LogSaveString(afsd_logp,volume),
983 if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
986 if (cellname[0] == '.') {
987 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
990 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
994 if ( cm_FreelanceMountPointExists(filename, 0) ||
995 cm_FreelanceSymlinkExists(filename, 0) )
998 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
999 osi_LogSaveString(afsd_logp,cellname));
1001 lock_ObtainMutex(&cm_Freelance_Lock);
1003 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1004 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1006 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1007 &hkFreelance) == ERROR_SUCCESS) {
1009 RegQueryInfoKey( hkFreelance,
1011 NULL, /* lpcClass */
1012 NULL, /* lpReserved */
1013 NULL, /* lpcSubKeys */
1014 NULL, /* lpcMaxSubKeyLen */
1015 NULL, /* lpcMaxClassLen */
1016 &dwMountPoints, /* lpcValues */
1017 NULL, /* lpcMaxValueNameLen */
1018 NULL, /* lpcMaxValueLen */
1019 NULL, /* lpcbSecurityDescriptor */
1020 NULL /* lpftLastWriteTime */
1024 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
1026 sprintf(line, "%s#%s:%s", filename, fullname, volume);
1028 /* If we are adding a new value, there must be an unused name
1029 * within the range 0 to dwMountPoints
1031 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
1035 dwSize = sizeof(szMount);
1036 sprintf(szIndex, "%d", dwIndex);
1037 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
1038 /* found an unused value */
1040 dwSize = (DWORD)strlen(line) + 1;
1041 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1044 int len = (int)strlen(filename);
1045 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
1046 (szMount[len] == '%' || szMount[len] == '#')) {
1047 /* Replace the existing value */
1049 dwSize = (DWORD)strlen(line) + 1;
1050 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1055 RegCloseKey(hkFreelance);
1058 cm_GetConfigDir(hfile, sizeof(hfile));
1059 strcat(hfile, AFS_FREELANCE_INI);
1060 fp = fopen(hfile, "r+");
1062 return CM_ERROR_INVAL;
1063 fgets(line, sizeof(line), fp);
1066 fseek(fp, 0, SEEK_SET);
1067 fprintf(fp, "%d", n);
1068 fseek(fp, 0, SEEK_END);
1070 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1072 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1076 /* Do this while we are holding the lock */
1077 cm_data.fakeDirVersion++;
1078 cm_localMountPointChangeFlag = 1;
1079 lock_ReleaseMutex(&cm_Freelance_Lock);
1085 clientchar_t *cpath;
1089 cpath = cm_FsStringToClientStringAlloc(filename, -1, NULL);
1091 return CM_ERROR_NOSUCHPATH;
1092 code = cm_NameI(cm_data.rootSCachep, cpath,
1093 CM_FLAG_FOLLOW | CM_FLAG_CASEFOLD | CM_FLAG_DFS_REFERRAL,
1094 cm_rootUserp, NULL, &req, &scp);
1099 cm_ReleaseSCache(scp);
1105 long cm_FreelanceRemoveMount(char *toremove)
1110 char shortname[200];
1111 char hfile[260], hfile2[260];
1114 HKEY hkFreelance = 0;
1115 DWORD dwType, dwSize;
1116 DWORD dwMountPoints;
1119 lock_ObtainMutex(&cm_Freelance_Lock);
1121 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1122 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1124 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1125 &hkFreelance) == ERROR_SUCCESS) {
1127 RegQueryInfoKey( hkFreelance,
1129 NULL, /* lpcClass */
1130 NULL, /* lpReserved */
1131 NULL, /* lpcSubKeys */
1132 NULL, /* lpcMaxSubKeyLen */
1133 NULL, /* lpcMaxClassLen */
1134 &dwMountPoints, /* lpcValues */
1135 NULL, /* lpcMaxValueNameLen */
1136 NULL, /* lpcMaxValueLen */
1137 NULL, /* lpcbSecurityDescriptor */
1138 NULL /* lpftLastWriteTime */
1141 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1142 TCHAR szValueName[16];
1143 DWORD dwValueSize = 16;
1144 dwSize = sizeof(line);
1145 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1146 &dwType, line, &dwSize);
1148 cp=strchr(line, '#');
1150 cp=strchr(line, '%');
1151 memcpy(shortname, line, cp-line);
1152 shortname[cp-line]=0;
1154 if (!strcmp(shortname, toremove)) {
1155 RegDeleteValue( hkFreelance, szValueName );
1160 RegCloseKey(hkFreelance);
1163 cm_GetConfigDir(hfile, sizeof(hfile));
1164 strcat(hfile, AFS_FREELANCE_INI);
1165 strcpy(hfile2, hfile);
1166 strcat(hfile2, "2");
1167 fp1=fopen(hfile, "r+");
1169 return CM_ERROR_INVAL;
1170 fp2=fopen(hfile2, "w+");
1173 return CM_ERROR_INVAL;
1176 fgets(line, sizeof(line), fp1);
1178 fprintf(fp2, "%d\n", n-1);
1180 for (i=0; i<n; i++) {
1181 fgets(line, sizeof(line), fp1);
1182 cp=strchr(line, '#');
1184 cp=strchr(line, '%');
1185 memcpy(shortname, line, cp-line);
1186 shortname[cp-line]=0;
1188 if (strcmp(shortname, toremove)==0) {
1200 rename(hfile2, hfile);
1205 /* Do this while we are holding the lock */
1206 cm_data.fakeDirVersion++;
1207 cm_localMountPointChangeFlag = 1;
1209 lock_ReleaseMutex(&cm_Freelance_Lock);
1210 return (found ? 0 : CM_ERROR_NOSUCHFILE);
1213 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1216 char fullname[CELL_MAXNAMELEN];
1218 HKEY hkFreelanceSymlinks = 0;
1219 DWORD dwType, dwSize;
1223 /* before adding, verify the filename. If it is already in use, either as
1224 * as mount point or a cellname, do not permit the creation of the symlink.
1226 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1227 osi_LogSaveString(afsd_logp,filename),
1228 osi_LogSaveString(afsd_logp,destination));
1230 if ( filename[0] == '\0' || destination[0] == '\0' )
1231 return CM_ERROR_INVAL;
1234 if (filename[0] == '.') {
1235 cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1236 if (cm_stricmp_utf8(&filename[1],fullname) == 0)
1237 return CM_ERROR_EXISTS;
1239 cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1240 if (cm_stricmp_utf8(filename,fullname) == 0)
1241 return CM_ERROR_EXISTS;
1244 if ( cm_FreelanceMountPointExists(filename, 0) ||
1245 cm_FreelanceSymlinkExists(filename, 0) )
1246 return CM_ERROR_EXISTS;
1248 lock_ObtainMutex(&cm_Freelance_Lock);
1250 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1251 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1254 REG_OPTION_NON_VOLATILE,
1255 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1257 &hkFreelanceSymlinks,
1258 NULL) == ERROR_SUCCESS) {
1260 RegQueryInfoKey( hkFreelanceSymlinks,
1262 NULL, /* lpcClass */
1263 NULL, /* lpReserved */
1264 NULL, /* lpcSubKeys */
1265 NULL, /* lpcMaxSubKeyLen */
1266 NULL, /* lpcMaxClassLen */
1267 &dwSymlinks, /* lpcValues */
1268 NULL, /* lpcMaxValueNameLen */
1269 NULL, /* lpcMaxValueLen */
1270 NULL, /* lpcbSecurityDescriptor */
1271 NULL /* lpftLastWriteTime */
1274 sprintf(line, "%s:%s.", filename, destination);
1276 /* If we are adding a new value, there must be an unused name
1277 * within the range 0 to dwSymlinks
1279 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1283 dwSize = sizeof(szLink);
1284 sprintf(szIndex, "%d", dwIndex);
1285 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1286 /* found an unused value */
1288 dwSize = (DWORD)strlen(line) + 1;
1289 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1292 int len = (int)strlen(filename);
1293 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1294 /* Replace the existing value */
1296 dwSize = (DWORD)strlen(line) + 1;
1297 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1302 RegCloseKey(hkFreelanceSymlinks);
1305 /* Do this while we are holding the lock */
1306 cm_data.fakeDirVersion++;
1307 cm_localMountPointChangeFlag = 1;
1308 lock_ReleaseMutex(&cm_Freelance_Lock);
1314 clientchar_t *cpath;
1318 cpath = cm_FsStringToClientStringAlloc(filename, -1, NULL);
1320 return CM_ERROR_NOSUCHPATH;
1321 code = cm_NameI(cm_data.rootSCachep, cpath,
1322 CM_FLAG_FOLLOW | CM_FLAG_CASEFOLD | CM_FLAG_DFS_REFERRAL,
1323 cm_rootUserp, NULL, &req, &scp);
1328 cm_ReleaseSCache(scp);
1334 long cm_FreelanceRemoveSymlink(char *toremove)
1338 char shortname[200];
1340 HKEY hkFreelanceSymlinks = 0;
1341 DWORD dwType, dwSize;
1345 lock_ObtainMutex(&cm_Freelance_Lock);
1347 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1348 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1350 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1351 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1353 RegQueryInfoKey( hkFreelanceSymlinks,
1355 NULL, /* lpcClass */
1356 NULL, /* lpReserved */
1357 NULL, /* lpcSubKeys */
1358 NULL, /* lpcMaxSubKeyLen */
1359 NULL, /* lpcMaxClassLen */
1360 &dwSymlinks, /* lpcValues */
1361 NULL, /* lpcMaxValueNameLen */
1362 NULL, /* lpcMaxValueLen */
1363 NULL, /* lpcbSecurityDescriptor */
1364 NULL /* lpftLastWriteTime */
1367 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1368 TCHAR szValueName[16];
1369 DWORD dwValueSize = 16;
1370 dwSize = sizeof(line);
1371 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1372 &dwType, line, &dwSize);
1374 cp=strchr(line, ':');
1375 memcpy(shortname, line, cp-line);
1376 shortname[cp-line]=0;
1378 if (!strcmp(shortname, toremove)) {
1379 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1384 RegCloseKey(hkFreelanceSymlinks);
1388 /* Do this while we are holding the lock */
1389 cm_data.fakeDirVersion++;
1390 cm_localMountPointChangeFlag = 1;
1392 lock_ReleaseMutex(&cm_Freelance_Lock);
1393 return (found ? 0 : CM_ERROR_NOSUCHFILE);
1397 cm_FreelanceFetchMountPointString(cm_scache_t *scp)
1399 lock_ObtainMutex(&cm_Freelance_Lock);
1400 if (!scp->mountPointStringp[0] &&
1401 scp->fid.cell == AFS_FAKE_ROOT_CELL_ID &&
1402 scp->fid.volume == AFS_FAKE_ROOT_VOL_ID &&
1403 scp->fid.unique <= cm_noLocalMountPoints) {
1404 strncpy(scp->mountPointStringp, cm_localMountPoints[scp->fid.unique-1].mountPointStringp, MOUNTPOINTLEN);
1405 scp->mountPointStringp[MOUNTPOINTLEN-1] = 0; /* null terminate */
1407 lock_ReleaseMutex(&cm_Freelance_Lock);
1413 cm_FreelanceFetchFileType(cm_scache_t *scp)
1415 lock_ObtainMutex(&cm_Freelance_Lock);
1416 if (scp->fid.cell == AFS_FAKE_ROOT_CELL_ID &&
1417 scp->fid.volume == AFS_FAKE_ROOT_VOL_ID &&
1418 scp->fid.unique <= cm_noLocalMountPoints)
1420 scp->fileType = cm_localMountPoints[scp->fid.unique-1].fileType;
1422 if ( scp->fileType == CM_SCACHETYPE_SYMLINK &&
1423 !strnicmp(cm_localMountPoints[scp->fid.unique-1].mountPointStringp, "msdfs:", strlen("msdfs:")) )
1425 scp->fileType = CM_SCACHETYPE_DFSLINK;
1428 scp->fileType = CM_SCACHETYPE_INVALID;
1430 lock_ReleaseMutex(&cm_Freelance_Lock);
1434 #endif /* AFS_FREELANCE_CLIENT */