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 // yj: first we make a call to cm_initLocalMountPoints
145 // to read all the local mount points from the registry
146 cm_InitLocalMountPoints();
148 // then we make a call to InitFakeRootDir to create
149 // a fake root directory based on the local mount points
150 cm_InitFakeRootDir();
151 // --- end of yj code
153 /* Start the registry monitor */
154 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceChangeNotifier,
155 NULL, 0, &lpid, "cm_FreelanceChangeNotifier");
156 osi_assertx(phandle != NULL, "cm_FreelanceChangeNotifier thread create failure");
157 thrd_CloseHandle(phandle);
159 phandle = thrd_Create(NULL, 65536, (ThreadFunc) cm_FreelanceSymlinkChangeNotifier,
160 NULL, 0, &lpid, "cm_FreelanceSymlinkChangeNotifier");
161 osi_assertx(phandle != NULL, "cm_FreelanceSymlinkChangeNotifier thread create failure");
162 thrd_CloseHandle(phandle);
165 /* yj: Initialization of the fake root directory */
166 /* to be called while holding freelance lock unless during init. */
167 void cm_InitFakeRootDir() {
172 // allocate space for the fake info
173 cm_dirHeader_t fakeDirHeader;
174 cm_dirEntry_t fakeEntry;
175 cm_pageHeader_t fakePageHeader;
177 // i'm going to calculate how much space is needed for
178 // this fake root directory. we have these rules:
179 // 1. there are cm_noLocalMountPoints number of entries
180 // 2. each page is CM_DIR_PAGESIZE in size
181 // 3. the first 13 chunks of the first page are used for
183 // 4. the first chunk of all subsequent pages are used
184 // for page header stuff
185 // 5. a max of CM_DIR_EPP entries are allowed per page
186 // 6. each entry takes 1 or more chunks, depending on
187 // the size of the mount point string, as determined
189 // 7. each chunk is CM_DIR_CHUNKSIZE bytes
191 int CPP = CM_DIR_PAGESIZE / CM_DIR_CHUNKSIZE;
192 int curChunk = 13; // chunks 0 - 12 are used for header stuff
193 // of the first page in the directory
196 int curDirEntryInPage = 0;
200 /* Reserve 2 directory chunks for "." and ".." */
203 while (curDirEntry<cm_noLocalMountPoints) {
204 sizeOfCurEntry = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
205 if ((curChunk + sizeOfCurEntry >= CPP) ||
206 (curDirEntryInPage + 1 >= CM_DIR_EPP)) {
208 curDirEntryInPage = 0;
211 curChunk += sizeOfCurEntry;
216 dirSize = (curPage+1) * CM_DIR_PAGESIZE;
217 if (cm_fakeDirSize != dirSize) {
219 free(cm_FakeRootDir);
220 cm_FakeRootDir = malloc(dirSize);
221 cm_fakeDirSize = dirSize;
224 // yj: when we get here, we've figured out how much memory we need and
225 // allocated the appropriate space for it. we now prceed to fill
226 // it up with entries.
229 curDirEntryInPage = 0;
232 // fields in the directory entry that are unused.
234 fakeEntry.length = 0;
236 fakeEntry.fid.unique = htonl(1);
238 // the first page is special, it uses fakeDirHeader instead of fakePageHeader
239 // we fill up the page with dirEntries that belong there and we make changes
240 // to the fakeDirHeader.header.freeBitmap along the way. Then when we're done
241 // filling up the dirEntries in this page, we copy the fakeDirHeader into
242 // the top of the page.
244 // init the freeBitmap array
246 fakeDirHeader.header.freeBitmap[i]=0;
248 fakeDirHeader.header.freeBitmap[0] = 0xff;
249 fakeDirHeader.header.freeBitmap[1] = 0x7f;
252 // we start counting at 13 because the 0th to 12th chunks are used for header
255 // stick the first 2 entries "." and ".." in
256 fakeEntry.fid.vnode = htonl(1);
257 strcpy(fakeEntry.name, ".");
258 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
259 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
260 curChunk++; curDirEntryInPage++;
261 strcpy(fakeEntry.name, "..");
262 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
263 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
264 curChunk++; curDirEntryInPage++;
266 // keep putting stuff into page 0 if
267 // 1. we're not done with all entries
268 // 2. we have less than CM_DIR_EPP entries in page 0
269 // 3. we're not out of chunks in page 0
271 while( (curDirEntry<cm_noLocalMountPoints) &&
272 (curDirEntryInPage < CM_DIR_EPP) &&
273 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
276 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
277 /* enforce the rule that only directories have odd vnode values */
278 fakeEntry.fid.vnode = htonl((curDirEntry + 1) * 2);
279 fakeEntry.fid.unique = htonl(curDirEntry + 1);
280 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
282 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
283 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
286 for (i=0; i<noChunks; i++) {
287 t1 = (curChunk + i) / 8;
288 t2 = curChunk + i - (t1*8);
289 fakeDirHeader.header.freeBitmap[t1] |= (1 << t2);
294 // when we get here, we're done with filling in the entries for page 0
295 // copy in the header info
297 memcpy(cm_FakeRootDir, &fakeDirHeader, 13 * CM_DIR_CHUNKSIZE);
301 // ok, page 0's done. Move on to the next page.
302 while (curDirEntry<cm_noLocalMountPoints) {
304 curChunk = 1; // the zeroth chunk is reserved for page header
305 curDirEntryInPage = 0;
306 for (i=0; i<8; i++) {
307 fakePageHeader.freeBitmap[i]=0;
309 fakePageHeader.freeCount = 0;
310 fakePageHeader.pgcount = 0;
311 fakePageHeader.tag = htons(1234);
313 // while we're on the same page...
314 while ( (curDirEntry<cm_noLocalMountPoints) &&
315 (curDirEntryInPage < CM_DIR_EPP) &&
316 (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
318 // add an entry to this page
320 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
321 /* enforce the rule that only directories have odd vnode values */
322 fakeEntry.fid.vnode = htonl((curDirEntry + 1) * 2);
323 fakeEntry.fid.unique = htonl(curDirEntry + 1);
324 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
325 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
326 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
329 for (i=0; i<noChunks; i++) {
330 t1 = (curChunk + i) / 8;
331 t2 = curChunk + i - (t1*8);
332 fakePageHeader.freeBitmap[t1] |= (1 << t2);
336 memcpy(cm_FakeRootDir + curPage * CM_DIR_PAGESIZE, &fakePageHeader, sizeof(fakePageHeader));
341 // we know the fakeDir is setup properly, so we claim that we have callback
342 osi_Log0(afsd_logp,"cm_InitFakeRootDir fakeDirCallback=1");
343 cm_fakeDirCallback=1;
345 // when we get here, we've set up everything! done!
348 int cm_FakeRootFid(cm_fid_t *fidp)
351 AFS_FAKE_ROOT_CELL_ID, /* root cell */
352 AFS_FAKE_ROOT_VOL_ID, /* root.afs ? */
357 /* called directly from ioctl */
358 /* called while not holding freelance lock */
359 int cm_noteLocalMountPointChange(void) {
360 lock_ObtainMutex(&cm_Freelance_Lock);
361 cm_data.fakeDirVersion++;
362 cm_localMountPointChangeFlag = 1;
363 lock_ReleaseMutex(&cm_Freelance_Lock);
367 int cm_getLocalMountPointChange() {
368 return cm_localMountPointChangeFlag;
371 int cm_clearLocalMountPointChange() {
372 cm_localMountPointChangeFlag = 0;
376 int cm_reInitLocalMountPoints() {
379 cm_scache_t *scp, **lscpp, *tscp;
381 osi_Log0(afsd_logp,"----- freelance reinitialization starts ----- ");
383 // first we invalidate all the SCPs that were created
384 // for the local mount points
386 osi_Log0(afsd_logp,"Invalidating local mount point scp... ");
388 lock_ObtainWrite(&cm_scacheLock);
389 lock_ObtainMutex(&cm_Freelance_Lock); /* always scache then freelance lock */
390 for (i=0; i<=cm_noLocalMountPoints; i++) {
392 cm_SetFid(&aFid, AFS_FAKE_ROOT_CELL_ID, AFS_FAKE_ROOT_VOL_ID, 1, 1);
394 cm_SetFid(&aFid, AFS_FAKE_ROOT_CELL_ID, AFS_FAKE_ROOT_VOL_ID, i*2, i);
395 hash = CM_SCACHE_HASH(&aFid);
396 for (scp=cm_data.scacheHashTablep[hash]; scp; scp=scp->nextp) {
397 if (scp != cm_data.rootSCachep && cm_FidCmp(&scp->fid, &aFid) == 0) {
398 // mark the scp to be reused
399 cm_HoldSCacheNoLock(scp);
400 lock_ReleaseMutex(&cm_Freelance_Lock);
401 lock_ReleaseWrite(&cm_scacheLock);
402 lock_ObtainWrite(&scp->rw);
403 cm_DiscardSCache(scp);
405 // take the scp out of the hash
406 lock_ObtainWrite(&cm_scacheLock);
407 for (lscpp = &cm_data.scacheHashTablep[hash], tscp = cm_data.scacheHashTablep[hash];
409 lscpp = &tscp->nextp, tscp = tscp->nextp) {
413 scp->flags &= ~CM_SCACHEFLAG_INHASH;
418 lock_ReleaseWrite(&scp->rw);
419 lock_ReleaseWrite(&cm_scacheLock);
420 cm_CallbackNotifyChange(scp);
421 lock_ObtainWrite(&cm_scacheLock);
422 cm_ReleaseSCacheNoLock(scp);
423 lock_ObtainMutex(&cm_Freelance_Lock);
427 lock_ReleaseWrite(&cm_scacheLock);
428 osi_Log0(afsd_logp,"\tall old scp cleared!");
430 // we must free the memory that was allocated in the prev
431 // cm_InitLocalMountPoints call
432 osi_Log0(afsd_logp,"Removing old localmountpoints... ");
433 free(cm_localMountPoints);
434 osi_Log0(afsd_logp,"\tall old localmountpoints cleared!");
436 // now re-init the localmountpoints
437 osi_Log0(afsd_logp,"Creating new localmountpoints... ");
438 cm_InitLocalMountPoints();
439 osi_Log0(afsd_logp,"\tcreated new set of localmountpoints!");
441 // then we re-create that dir
442 osi_Log0(afsd_logp,"Creating new fakedir... ");
443 cm_InitFakeRootDir();
444 osi_Log0(afsd_logp,"\t\tcreated new fakedir!");
446 lock_ReleaseMutex(&cm_Freelance_Lock);
448 osi_Log0(afsd_logp,"----- freelance reinit complete -----");
453 // yj: open up the registry and read all the local mount
454 // points that are stored there. Part of the initialization
455 // process for the freelance client.
456 /* to be called while holding freelance lock unless during init. */
457 long cm_InitLocalMountPoints() {
462 cm_localMountPoint_t* aLocalMountPoint;
465 char rootCellName[256];
466 HKEY hkFreelance = 0, hkFreelanceSymlinks = 0;
467 DWORD dwType, dwSize;
468 DWORD dwMountPoints = 0;
470 DWORD dwSymlinks = 0;
471 FILETIME ftLastWriteTime;
473 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
474 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
476 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
477 &hkFreelance) == ERROR_SUCCESS) {
479 RegQueryInfoKey( hkFreelance,
482 NULL, /* lpReserved */
483 NULL, /* lpcSubKeys */
484 NULL, /* lpcMaxSubKeyLen */
485 NULL, /* lpcMaxClassLen */
486 &dwMountPoints, /* lpcValues */
487 NULL, /* lpcMaxValueNameLen */
488 NULL, /* lpcMaxValueLen */
489 NULL, /* lpcbSecurityDescriptor */
490 &ftLastWriteTime /* lpftLastWriteTime */
493 smb_UnixTimeFromLargeSearchTime(&FakeFreelanceModTime, &ftLastWriteTime);
495 if ( dwMountPoints == 0 ) {
496 rootCellName[0] = '.';
497 code = cm_GetRootCellName(&rootCellName[1]);
499 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
500 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
501 cm_FreelanceAddMount(".root", &rootCellName[1], "root.afs.", 1, NULL);
506 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
507 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
510 REG_OPTION_NON_VOLATILE,
511 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
513 &hkFreelanceSymlinks,
514 NULL) == ERROR_SUCCESS) {
516 RegQueryInfoKey( hkFreelanceSymlinks,
519 NULL, /* lpReserved */
520 NULL, /* lpcSubKeys */
521 NULL, /* lpcMaxSubKeyLen */
522 NULL, /* lpcMaxClassLen */
523 &dwSymlinks, /* lpcValues */
524 NULL, /* lpcMaxValueNameLen */
525 NULL, /* lpcMaxValueLen */
526 NULL, /* lpcbSecurityDescriptor */
527 NULL /* lpftLastWriteTime */
531 // get the number of entries there are from the first line
533 cm_noLocalMountPoints = dwMountPoints + dwSymlinks;
535 // create space to store the local mount points
536 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
537 aLocalMountPoint = cm_localMountPoints;
539 // now we read n lines and parse them into local mount points
540 // where n is the number of local mount points there are, as
542 // Each line in the ini file represents 1 local mount point and
543 // is in the format xxx#yyy:zzz, where xxx is the directory
544 // entry name, yyy is the cell name and zzz is the volume name.
545 // #yyy:zzz together make up the mount point.
546 for ( dwIndex = 0 ; dwIndex < dwMountPoints; dwIndex++ ) {
547 TCHAR szValueName[16];
548 DWORD dwValueSize = 16;
549 dwSize = sizeof(line);
550 if (RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
551 &dwType, line, &dwSize))
553 afsi_log("RegEnumValue(hkFreelance) failed");
554 cm_noLocalMountPoints--;
558 afsi_log("Mountpoint[%d] = %s",dwIndex, line);
560 /* find the trailing dot; null terminate after it */
561 t2 = strrchr(line, '.');
565 for ( t=line;*t;t++ ) {
566 if ( !isprint(*t) ) {
567 afsi_log("error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
568 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: non-printable character", dwIndex);
569 cm_noLocalMountPoints--;
574 // line is not empty, so let's parse it
575 t = strchr(line, '#');
577 t = strchr(line, '%');
578 // make sure that there is a '#' or '%' separator in the line
580 afsi_log("error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
581 fprintf(stderr, "error occurred while parsing mountpoint entry [%d]: no # or %% separator", dwIndex);
582 cm_noLocalMountPoints--;
586 aLocalMountPoint->fileType = CM_SCACHETYPE_MOUNTPOINT;
587 aLocalMountPoint->namep=malloc(t-line+1);
588 strncpy(aLocalMountPoint->namep, line, t-line);
589 aLocalMountPoint->namep[t-line] = '\0';
591 /* copy the mount point string */
592 aLocalMountPoint->mountPointStringp=malloc(strlen(t));
593 strncpy(aLocalMountPoint->mountPointStringp, t, strlen(t)-1);
594 aLocalMountPoint->mountPointStringp[strlen(t)-1] = '\0';
596 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
597 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
598 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
603 for ( dwIndex = 0 ; dwIndex < dwSymlinks; dwIndex++ ) {
604 TCHAR szValueName[16];
605 DWORD dwValueSize = 16;
606 dwSize = sizeof(line);
607 if (RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
608 &dwType, line, &dwSize))
610 afsi_log("RegEnumValue(hkFreelanceSymlinks) failed");
611 cm_noLocalMountPoints--;
615 afsi_log("Symlink[%d] = %s",dwIndex, line);
617 /* find the trailing dot; null terminate after it */
618 t2 = strrchr(line, '.');
622 for ( t=line;*t;t++ ) {
623 if ( !isprint(*t) ) {
624 afsi_log("error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
625 fprintf(stderr, "error occurred while parsing symlink entry [%d]: non-printable character", dwIndex);
626 cm_noLocalMountPoints--;
631 // line is not empty, so let's parse it
632 t = strchr(line, ':');
634 // make sure that there is a ':' separator in the line
636 afsi_log("error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
637 fprintf(stderr, "error occurred while parsing symlink entry [%d]: no ':' separator", dwIndex);
638 cm_noLocalMountPoints--;
642 aLocalMountPoint->fileType = CM_SCACHETYPE_SYMLINK;
643 aLocalMountPoint->namep=malloc(t-line+1);
644 strncpy(aLocalMountPoint->namep, line, t-line);
645 aLocalMountPoint->namep[t-line] = '\0';
647 /* copy the symlink string */
648 aLocalMountPoint->mountPointStringp=malloc(strlen(t)-1);
649 strncpy(aLocalMountPoint->mountPointStringp, t+1, strlen(t)-2);
650 aLocalMountPoint->mountPointStringp[strlen(t)-2] = '\0';
652 osi_Log2(afsd_logp,"found symlink: name %s, string %s",
653 osi_LogSaveString(afsd_logp,aLocalMountPoint->namep),
654 osi_LogSaveString(afsd_logp,aLocalMountPoint->mountPointStringp));
659 if ( hkFreelanceSymlinks )
660 RegCloseKey( hkFreelanceSymlinks );
661 RegCloseKey(hkFreelance);
665 /* What follows is the old code to read freelance mount points
666 * out of a text file modified to copy the data into the registry
668 cm_GetConfigDir(hdir, sizeof(hdir));
669 strcat(hdir, AFS_FREELANCE_INI);
670 // open the ini file for reading
671 fp = fopen(hdir, "r");
673 /* look in the Windows directory where we used to store the file */
674 GetWindowsDirectory(hdir, sizeof(hdir));
676 strcat(hdir, AFS_FREELANCE_INI);
677 fp = fopen(hdir, "r");
680 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
681 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
684 REG_OPTION_NON_VOLATILE,
692 RegCloseKey(hkFreelance);
693 rootCellName[0] = '.';
694 code = cm_GetRootCellName(&rootCellName[1]);
696 cm_FreelanceAddMount(&rootCellName[1], &rootCellName[1], "root.cell.", 0, NULL);
697 cm_FreelanceAddMount(rootCellName, &rootCellName[1], "root.cell.", 1, NULL);
698 cm_FreelanceAddMount(".root", &rootCellName[1], "root.afs.", 1, NULL);
703 // we successfully opened the file
704 osi_Log0(afsd_logp,"opened afs_freelance.ini");
706 // now we read the first line to see how many entries
708 fgets(line, sizeof(line), fp);
710 // if the line is empty at any point when we're reading
711 // we're screwed. report error and return.
713 afsi_log("error occurred while reading afs_freelance.ini");
714 fprintf(stderr, "error occurred while reading afs_freelance.ini");
718 // get the number of entries there are from the first line
720 cm_noLocalMountPoints = atoi(line);
722 if (cm_noLocalMountPoints > 0) {
723 // create space to store the local mount points
724 cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
725 aLocalMountPoint = cm_localMountPoints;
728 // now we read n lines and parse them into local mount points
729 // where n is the number of local mount points there are, as
731 // Each line in the ini file represents 1 local mount point and
732 // is in the format xxx#yyy:zzz, where xxx is the directory
733 // entry name, yyy is the cell name and zzz is the volume name.
734 // #yyy:zzz together make up the mount point.
735 for (i=0; i<cm_noLocalMountPoints; i++) {
736 fgets(line, sizeof(line), fp);
737 // check that the line is not empty
739 afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
740 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
744 /* find the trailing dot; null terminate after it */
745 t2 = strrchr(line, '.');
751 /* we are migrating to the registry */
752 sprintf(szIndex,"%d",dwIndex++);
754 dwSize = (DWORD)strlen(line) + 1;
755 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
758 // line is not empty, so let's parse it
759 t = strchr(line, '#');
761 t = strchr(line, '%');
762 // make sure that there is a '#' or '%' separator in the line
764 afsi_log("error occurred while parsing entry in %s: no # or %% separator in line %d", AFS_FREELANCE_INI, i);
765 fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # or %% separator in line %d", i);
768 aLocalMountPoint->namep=malloc(t-line+1);
769 memcpy(aLocalMountPoint->namep, line, t-line);
770 *(aLocalMountPoint->namep + (t-line)) = 0;
772 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
773 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-1);
774 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-1)) = 0;
776 osi_Log2(afsd_logp,"found mount point: name %s, string %s",
777 aLocalMountPoint->namep,
778 aLocalMountPoint->mountPointStringp);
784 RegCloseKey(hkFreelance);
790 int cm_getNoLocalMountPoints() {
791 return cm_noLocalMountPoints;
794 long cm_FreelanceMountPointExists(char * filename, int prefix_ok)
800 HKEY hkFreelance = 0;
801 DWORD dwType, dwSize;
805 lock_ObtainMutex(&cm_Freelance_Lock);
807 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
808 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
810 KEY_READ|KEY_QUERY_VALUE,
811 &hkFreelance) == ERROR_SUCCESS)
813 RegQueryInfoKey( hkFreelance,
816 NULL, /* lpReserved */
817 NULL, /* lpcSubKeys */
818 NULL, /* lpcMaxSubKeyLen */
819 NULL, /* lpcMaxClassLen */
820 &dwMountPoints, /* lpcValues */
821 NULL, /* lpcMaxValueNameLen */
822 NULL, /* lpcMaxValueLen */
823 NULL, /* lpcbSecurityDescriptor */
824 NULL /* lpftLastWriteTime */
827 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
828 TCHAR szValueName[16];
829 DWORD dwValueSize = 16;
830 dwSize = sizeof(line);
831 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
832 &dwType, line, &dwSize);
834 cp=strchr(line, '#');
836 cp=strchr(line, '%');
837 memcpy(shortname, line, cp-line);
838 shortname[cp-line]=0;
840 if (!strcmp(shortname, filename)) {
845 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
846 TCHAR szValueName[16];
847 DWORD dwValueSize = 16;
848 dwSize = sizeof(line);
849 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
850 &dwType, line, &dwSize);
852 cp=strchr(line, '#');
854 cp=strchr(line, '%');
855 memcpy(shortname, line, cp-line);
856 shortname[cp-line]=0;
858 if (!cm_stricmp_utf8(shortname, filename)) {
863 if (prefix_ok && strlen(shortname) - strlen(filename) == 1 && !strncmp(shortname, filename, strlen(filename))) {
868 RegCloseKey(hkFreelance);
871 lock_ReleaseMutex(&cm_Freelance_Lock);
876 long cm_FreelanceSymlinkExists(char * filename, int prefix_ok)
882 HKEY hkFreelance = 0;
883 DWORD dwType, dwSize;
887 lock_ObtainMutex(&cm_Freelance_Lock);
889 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
890 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
892 KEY_READ|KEY_QUERY_VALUE,
893 &hkFreelance) == ERROR_SUCCESS)
895 RegQueryInfoKey( hkFreelance,
898 NULL, /* lpReserved */
899 NULL, /* lpcSubKeys */
900 NULL, /* lpcMaxSubKeyLen */
901 NULL, /* lpcMaxClassLen */
902 &dwSymlinks, /* lpcValues */
903 NULL, /* lpcMaxValueNameLen */
904 NULL, /* lpcMaxValueLen */
905 NULL, /* lpcbSecurityDescriptor */
906 NULL /* lpftLastWriteTime */
909 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
910 TCHAR szValueName[16];
911 DWORD dwValueSize = 16;
912 dwSize = sizeof(line);
913 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
914 &dwType, line, &dwSize);
916 cp=strchr(line, ':');
917 memcpy(shortname, line, cp-line);
918 shortname[cp-line]=0;
920 if (!strcmp(shortname, filename)) {
925 if (prefix_ok && strlen(shortname) - strlen(filename) == 1 && !strncmp(shortname, filename, strlen(filename))) {
930 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
931 TCHAR szValueName[16];
932 DWORD dwValueSize = 16;
933 dwSize = sizeof(line);
934 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
935 &dwType, line, &dwSize);
937 cp=strchr(line, ':');
938 memcpy(shortname, line, cp-line);
939 shortname[cp-line]=0;
941 if (!cm_stricmp_utf8(shortname, filename)) {
946 RegCloseKey(hkFreelance);
949 lock_ReleaseMutex(&cm_Freelance_Lock);
954 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, int rw, cm_fid_t *fidp)
959 char fullname[CELL_MAXNAMELEN];
962 HKEY hkFreelance = 0;
963 DWORD dwType, dwSize;
967 /* before adding, verify the cell name; if it is not a valid cell,
968 don't add the mount point.
969 allow partial matches as a means of poor man's alias. */
970 /* major performance issue? */
971 osi_Log4(afsd_logp,"Freelance Add Mount request: filename=%s cellname=%s volume=%s %s",
972 osi_LogSaveString(afsd_logp,filename),
973 osi_LogSaveString(afsd_logp,cellname),
974 osi_LogSaveString(afsd_logp,volume),
977 if ( filename[0] == '\0' || cellname[0] == '\0' || volume[0] == '\0' )
980 if (cellname[0] == '.') {
981 if (!cm_GetCell_Gen(&cellname[1], fullname, CM_FLAG_CREATE))
984 if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
988 if ( cm_FreelanceMountPointExists(filename, 0) ||
989 cm_FreelanceSymlinkExists(filename, 0) )
992 osi_Log1(afsd_logp,"Freelance Adding Mount for Cell: %s",
993 osi_LogSaveString(afsd_logp,cellname));
995 lock_ObtainMutex(&cm_Freelance_Lock);
997 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
998 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1000 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1001 &hkFreelance) == ERROR_SUCCESS) {
1003 RegQueryInfoKey( hkFreelance,
1005 NULL, /* lpcClass */
1006 NULL, /* lpReserved */
1007 NULL, /* lpcSubKeys */
1008 NULL, /* lpcMaxSubKeyLen */
1009 NULL, /* lpcMaxClassLen */
1010 &dwMountPoints, /* lpcValues */
1011 NULL, /* lpcMaxValueNameLen */
1012 NULL, /* lpcMaxValueLen */
1013 NULL, /* lpcbSecurityDescriptor */
1014 NULL /* lpftLastWriteTime */
1018 sprintf(line, "%s%%%s:%s", filename, fullname, volume);
1020 sprintf(line, "%s#%s:%s", filename, fullname, volume);
1022 /* If we are adding a new value, there must be an unused name
1023 * within the range 0 to dwMountPoints
1025 for ( dwIndex = 0; dwIndex <= dwMountPoints; dwIndex++ ) {
1029 dwSize = sizeof(szMount);
1030 sprintf(szIndex, "%d", dwIndex);
1031 if (RegQueryValueEx( hkFreelance, szIndex, 0, &dwType, szMount, &dwSize) != ERROR_SUCCESS) {
1032 /* found an unused value */
1034 dwSize = (DWORD)strlen(line) + 1;
1035 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1038 int len = (int)strlen(filename);
1039 if ( dwType == REG_SZ && !strncmp(filename, szMount, len) &&
1040 (szMount[len] == '%' || szMount[len] == '#')) {
1041 /* Replace the existing value */
1043 dwSize = (DWORD)strlen(line) + 1;
1044 RegSetValueEx( hkFreelance, szIndex, 0, dwType, line, dwSize);
1049 RegCloseKey(hkFreelance);
1052 cm_GetConfigDir(hfile, sizeof(hfile));
1053 strcat(hfile, AFS_FREELANCE_INI);
1054 fp = fopen(hfile, "r+");
1056 return CM_ERROR_INVAL;
1057 fgets(line, sizeof(line), fp);
1060 fseek(fp, 0, SEEK_SET);
1061 fprintf(fp, "%d", n);
1062 fseek(fp, 0, SEEK_END);
1064 fprintf(fp, "%s%%%s:%s\n", filename, fullname, volume);
1066 fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
1070 /* Do this while we are holding the lock */
1071 cm_data.fakeDirVersion++;
1072 cm_localMountPointChangeFlag = 1;
1073 lock_ReleaseMutex(&cm_Freelance_Lock);
1079 clientchar_t *cpath;
1083 cpath = cm_FsStringToClientStringAlloc(filename, -1, NULL);
1085 return CM_ERROR_NOSUCHPATH;
1086 code = cm_NameI(cm_data.rootSCachep, filename,
1087 CM_FLAG_FOLLOW | CM_FLAG_CASEFOLD | CM_FLAG_DFS_REFERRAL,
1088 cm_rootUserp, NULL, &req, &scp);
1093 cm_ReleaseSCache(scp);
1099 long cm_FreelanceRemoveMount(char *toremove)
1104 char shortname[200];
1105 char hfile[260], hfile2[260];
1108 HKEY hkFreelance = 0;
1109 DWORD dwType, dwSize;
1110 DWORD dwMountPoints;
1113 lock_ObtainMutex(&cm_Freelance_Lock);
1115 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1116 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance",
1118 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1119 &hkFreelance) == ERROR_SUCCESS) {
1121 RegQueryInfoKey( hkFreelance,
1123 NULL, /* lpcClass */
1124 NULL, /* lpReserved */
1125 NULL, /* lpcSubKeys */
1126 NULL, /* lpcMaxSubKeyLen */
1127 NULL, /* lpcMaxClassLen */
1128 &dwMountPoints, /* lpcValues */
1129 NULL, /* lpcMaxValueNameLen */
1130 NULL, /* lpcMaxValueLen */
1131 NULL, /* lpcbSecurityDescriptor */
1132 NULL /* lpftLastWriteTime */
1135 for ( dwIndex = 0; dwIndex < dwMountPoints; dwIndex++ ) {
1136 TCHAR szValueName[16];
1137 DWORD dwValueSize = 16;
1138 dwSize = sizeof(line);
1139 RegEnumValue( hkFreelance, dwIndex, szValueName, &dwValueSize, NULL,
1140 &dwType, line, &dwSize);
1142 cp=strchr(line, '#');
1144 cp=strchr(line, '%');
1145 memcpy(shortname, line, cp-line);
1146 shortname[cp-line]=0;
1148 if (!strcmp(shortname, toremove)) {
1149 RegDeleteValue( hkFreelance, szValueName );
1154 RegCloseKey(hkFreelance);
1157 cm_GetConfigDir(hfile, sizeof(hfile));
1158 strcat(hfile, AFS_FREELANCE_INI);
1159 strcpy(hfile2, hfile);
1160 strcat(hfile2, "2");
1161 fp1=fopen(hfile, "r+");
1163 return CM_ERROR_INVAL;
1164 fp2=fopen(hfile2, "w+");
1167 return CM_ERROR_INVAL;
1170 fgets(line, sizeof(line), fp1);
1172 fprintf(fp2, "%d\n", n-1);
1174 for (i=0; i<n; i++) {
1175 fgets(line, sizeof(line), fp1);
1176 cp=strchr(line, '#');
1178 cp=strchr(line, '%');
1179 memcpy(shortname, line, cp-line);
1180 shortname[cp-line]=0;
1182 if (strcmp(shortname, toremove)==0) {
1194 rename(hfile2, hfile);
1199 /* Do this while we are holding the lock */
1200 cm_data.fakeDirVersion++;
1201 cm_localMountPointChangeFlag = 1;
1203 lock_ReleaseMutex(&cm_Freelance_Lock);
1204 return (found ? 0 : CM_ERROR_NOSUCHFILE);
1207 long cm_FreelanceAddSymlink(char *filename, char *destination, cm_fid_t *fidp)
1210 char fullname[CELL_MAXNAMELEN];
1212 HKEY hkFreelanceSymlinks = 0;
1213 DWORD dwType, dwSize;
1217 /* before adding, verify the filename. If it is already in use, either as
1218 * as mount point or a cellname, do not permit the creation of the symlink.
1220 osi_Log2(afsd_logp,"Freelance Add Symlink request: filename=%s destination=%s",
1221 osi_LogSaveString(afsd_logp,filename),
1222 osi_LogSaveString(afsd_logp,destination));
1224 if ( filename[0] == '\0' || destination[0] == '\0' )
1225 return CM_ERROR_INVAL;
1228 if (filename[0] == '.') {
1229 cm_GetCell_Gen(&filename[1], fullname, CM_FLAG_CREATE);
1230 if (cm_stricmp_utf8(&filename[1],fullname) == 0)
1231 return CM_ERROR_EXISTS;
1233 cm_GetCell_Gen(filename, fullname, CM_FLAG_CREATE);
1234 if (cm_stricmp_utf8(filename,fullname) == 0)
1235 return CM_ERROR_EXISTS;
1238 if ( cm_FreelanceMountPointExists(filename, 0) ||
1239 cm_FreelanceSymlinkExists(filename, 0) )
1240 return CM_ERROR_EXISTS;
1242 lock_ObtainMutex(&cm_Freelance_Lock);
1244 if (RegCreateKeyEx( HKEY_LOCAL_MACHINE,
1245 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1248 REG_OPTION_NON_VOLATILE,
1249 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1251 &hkFreelanceSymlinks,
1252 NULL) == ERROR_SUCCESS) {
1254 RegQueryInfoKey( hkFreelanceSymlinks,
1256 NULL, /* lpcClass */
1257 NULL, /* lpReserved */
1258 NULL, /* lpcSubKeys */
1259 NULL, /* lpcMaxSubKeyLen */
1260 NULL, /* lpcMaxClassLen */
1261 &dwSymlinks, /* lpcValues */
1262 NULL, /* lpcMaxValueNameLen */
1263 NULL, /* lpcMaxValueLen */
1264 NULL, /* lpcbSecurityDescriptor */
1265 NULL /* lpftLastWriteTime */
1268 sprintf(line, "%s:%s.", filename, destination);
1270 /* If we are adding a new value, there must be an unused name
1271 * within the range 0 to dwSymlinks
1273 for ( dwIndex = 0; dwIndex <= dwSymlinks; dwIndex++ ) {
1277 dwSize = sizeof(szLink);
1278 sprintf(szIndex, "%d", dwIndex);
1279 if (RegQueryValueEx( hkFreelanceSymlinks, szIndex, 0, &dwType, szLink, &dwSize) != ERROR_SUCCESS) {
1280 /* found an unused value */
1282 dwSize = (DWORD)strlen(line) + 1;
1283 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1286 int len = (int)strlen(filename);
1287 if ( dwType == REG_SZ && !strncmp(filename, szLink, len) && szLink[len] == ':') {
1288 /* Replace the existing value */
1290 dwSize = (DWORD)strlen(line) + 1;
1291 RegSetValueEx( hkFreelanceSymlinks, szIndex, 0, dwType, line, dwSize);
1296 RegCloseKey(hkFreelanceSymlinks);
1299 /* Do this while we are holding the lock */
1300 cm_data.fakeDirVersion++;
1301 cm_localMountPointChangeFlag = 1;
1302 lock_ReleaseMutex(&cm_Freelance_Lock);
1308 clientchar_t *cpath;
1312 cpath = cm_FsStringToClientStringAlloc(filename, -1, NULL);
1314 return CM_ERROR_NOSUCHPATH;
1315 code = cm_NameI(cm_data.rootSCachep, filename,
1316 CM_FLAG_FOLLOW | CM_FLAG_CASEFOLD | CM_FLAG_DFS_REFERRAL,
1317 cm_rootUserp, NULL, &req, &scp);
1322 cm_ReleaseSCache(scp);
1328 long cm_FreelanceRemoveSymlink(char *toremove)
1332 char shortname[200];
1334 HKEY hkFreelanceSymlinks = 0;
1335 DWORD dwType, dwSize;
1339 lock_ObtainMutex(&cm_Freelance_Lock);
1341 if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1342 AFSREG_CLT_OPENAFS_SUBKEY "\\Freelance\\Symlinks",
1344 KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
1345 &hkFreelanceSymlinks) == ERROR_SUCCESS) {
1347 RegQueryInfoKey( hkFreelanceSymlinks,
1349 NULL, /* lpcClass */
1350 NULL, /* lpReserved */
1351 NULL, /* lpcSubKeys */
1352 NULL, /* lpcMaxSubKeyLen */
1353 NULL, /* lpcMaxClassLen */
1354 &dwSymlinks, /* lpcValues */
1355 NULL, /* lpcMaxValueNameLen */
1356 NULL, /* lpcMaxValueLen */
1357 NULL, /* lpcbSecurityDescriptor */
1358 NULL /* lpftLastWriteTime */
1361 for ( dwIndex = 0; dwIndex < dwSymlinks; dwIndex++ ) {
1362 TCHAR szValueName[16];
1363 DWORD dwValueSize = 16;
1364 dwSize = sizeof(line);
1365 RegEnumValue( hkFreelanceSymlinks, dwIndex, szValueName, &dwValueSize, NULL,
1366 &dwType, line, &dwSize);
1368 cp=strchr(line, ':');
1369 memcpy(shortname, line, cp-line);
1370 shortname[cp-line]=0;
1372 if (!strcmp(shortname, toremove)) {
1373 RegDeleteValue( hkFreelanceSymlinks, szValueName );
1378 RegCloseKey(hkFreelanceSymlinks);
1382 /* Do this while we are holding the lock */
1383 cm_data.fakeDirVersion++;
1384 cm_localMountPointChangeFlag = 1;
1386 lock_ReleaseMutex(&cm_Freelance_Lock);
1387 return (found ? 0 : CM_ERROR_NOSUCHFILE);
1391 cm_FreelanceFetchMountPointString(cm_scache_t *scp)
1393 lock_ObtainMutex(&cm_Freelance_Lock);
1394 if (!scp->mountPointStringp[0] &&
1395 scp->fid.cell == AFS_FAKE_ROOT_CELL_ID &&
1396 scp->fid.volume == AFS_FAKE_ROOT_VOL_ID &&
1397 scp->fid.unique <= cm_noLocalMountPoints) {
1398 strncpy(scp->mountPointStringp, cm_localMountPoints[scp->fid.unique-1].mountPointStringp, MOUNTPOINTLEN);
1399 scp->mountPointStringp[MOUNTPOINTLEN-1] = 0; /* null terminate */
1401 lock_ReleaseMutex(&cm_Freelance_Lock);
1407 cm_FreelanceFetchFileType(cm_scache_t *scp)
1409 lock_ObtainMutex(&cm_Freelance_Lock);
1410 if (scp->fid.cell == AFS_FAKE_ROOT_CELL_ID &&
1411 scp->fid.volume == AFS_FAKE_ROOT_VOL_ID &&
1412 scp->fid.unique <= cm_noLocalMountPoints)
1414 scp->fileType = cm_localMountPoints[scp->fid.unique-1].fileType;
1416 if ( scp->fileType == CM_SCACHETYPE_SYMLINK &&
1417 !strnicmp(cm_localMountPoints[scp->fid.unique-1].mountPointStringp, "msdfs:", strlen("msdfs:")) )
1419 scp->fileType = CM_SCACHETYPE_DFSLINK;
1422 scp->fileType = CM_SCACHETYPE_INVALID;
1424 lock_ReleaseMutex(&cm_Freelance_Lock);
1428 #endif /* AFS_FREELANCE_CLIENT */