more-must-returns-20031207
[openafs.git] / src / WINNT / afsd / cm_freelance.c
1 #include <afs/param.h>
2 #include <afs/stds.h>
3
4 #ifndef DJGPP
5 #include <windows.h>
6 #include <winsock2.h>
7 #else
8 #include <netdb.h>
9 #endif /* !DJGPP */
10 #include <stdlib.h>
11 #include <malloc.h>
12 #include <string.h>
13
14 #include <rx/rx.h>
15
16 #include "afsd.h"
17 #ifdef AFS_FREELANCE_CLIENT
18 #include "cm_freelance.h"
19 #include "stdio.h"
20
21 extern void afsi_log(char *pattern, ...);
22
23 int cm_noLocalMountPoints;
24 int cm_fakeDirSize;
25 int cm_fakeDirCallback=0;
26 int cm_fakeGettingCallback=0;
27 int cm_fakeDirVersion = 0x8;
28 cm_localMountPoint_t* cm_localMountPoints;
29 osi_mutex_t cm_Freelance_Lock;
30 int cm_localMountPointChangeFlag = 0;
31 int cm_freelanceEnabled = 0;
32
33 void cm_InitFakeRootDir();
34
35 void cm_InitFreelance() {
36   
37         lock_InitializeMutex(&cm_Freelance_Lock, "Freelance Lock");
38   
39         // yj: first we make a call to cm_initLocalMountPoints
40         // to read all the local mount points from an ini file
41         cm_InitLocalMountPoints();
42         
43         // then we make a call to InitFakeRootDir to create
44         // a fake root directory based on the local mount points
45         cm_InitFakeRootDir();
46
47         // --- end of yj code
48 }
49
50 /* yj: Initialization of the fake root directory */
51 /* to be called while holding freelance lock unless during init. */
52 void cm_InitFakeRootDir() {
53         
54         int i, t1, t2;
55         char* currentPos;
56         int noChunks;
57
58         // allocate space for the fake info
59         cm_dirHeader_t fakeDirHeader;
60         cm_dirEntry_t fakeEntry;
61         cm_pageHeader_t fakePageHeader;
62
63         // i'm going to calculate how much space is needed for
64         // this fake root directory. we have these rules:
65         // 1. there are cm_noLocalMountPoints number of entries
66         // 2. each page is CM_DIR_PAGESIZE in size
67         // 3. the first 13 chunks of the first page are used for
68         //    some header stuff
69         // 4. the first chunk of all subsequent pages are used
70         //    for page header stuff
71         // 5. a max of CM_DIR_EPP entries are allowed per page
72         // 6. each entry takes 1 or more chunks, depending on 
73         //    the size of the mount point string, as determined
74         //    by cm_NameEntries
75         // 7. each chunk is CM_DIR_CHUNKSIZE bytes
76
77         int CPP = CM_DIR_PAGESIZE / CM_DIR_CHUNKSIZE;
78         int curChunk = 13;      // chunks 0 - 12 are used for header stuff
79                                                 // of the first page in the directory
80         int curPage = 0;
81         int curDirEntry = 0;
82         int curDirEntryInPage = 0;
83         int sizeOfCurEntry;
84         int dirSize;
85
86         /* Reserve 2 directory chunks for "." and ".." */
87         curChunk += 2;
88
89         while (curDirEntry!=cm_noLocalMountPoints) {
90                 sizeOfCurEntry = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
91                 if ((curChunk + sizeOfCurEntry >= CPP) ||
92                         (curDirEntryInPage + 1 >= CM_DIR_EPP)) {
93                         curPage++;
94                         curDirEntryInPage = 0;
95                         curChunk = 1;
96                 }
97                 curChunk += sizeOfCurEntry;
98                 curDirEntry++;
99                 curDirEntryInPage++;
100         }
101
102         dirSize = (curPage+1) *  CM_DIR_PAGESIZE;
103         cm_FakeRootDir = malloc(dirSize);
104         cm_fakeDirSize = dirSize;
105
106         
107
108         // yj: when we get here, we've figured out how much memory we need and 
109         // allocated the appropriate space for it. we now prceed to fill
110         // it up with entries.
111         curPage = 0;
112         curDirEntry = 0;
113         curDirEntryInPage = 0;
114         curChunk = 0;
115         
116         // fields in the directory entry that are unused.
117         fakeEntry.flag = 1;
118         fakeEntry.length = 0;
119         fakeEntry.next = 0;
120         fakeEntry.fid.unique = htonl(1);
121
122         // the first page is special, it uses fakeDirHeader instead of fakePageHeader
123         // we fill up the page with dirEntries that belong there and we make changes
124         // to the fakeDirHeader.header.freeBitmap along the way. Then when we're done
125         // filling up the dirEntries in this page, we copy the fakeDirHeader into 
126         // the top of the page.
127
128         // init the freeBitmap array
129         for (i=0; i<8; i++) 
130                 fakeDirHeader.header.freeBitmap[i]=0;
131
132         fakeDirHeader.header.freeBitmap[0] = 0xff;
133         fakeDirHeader.header.freeBitmap[1] = 0x7f;
134         
135
136         // we start counting at 13 because the 0th to 12th chunks are used for header
137         curChunk = 13;
138
139         // stick the first 2 entries "." and ".." in
140         fakeEntry.fid.unique = htonl(1);
141         fakeEntry.fid.vnode = htonl(1);
142         strcpy(fakeEntry.name, ".");
143         currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
144         memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
145         curChunk++; curDirEntryInPage++;
146         strcpy(fakeEntry.name, "..");
147         currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
148         memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
149         curChunk++; curDirEntryInPage++;
150
151         // keep putting stuff into page 0 if
152         // 1. we're not done with all entries
153         // 2. we have less than CM_DIR_EPP entries in page 0
154         // 3. we're not out of chunks in page 0
155
156         while( (curDirEntry!=cm_noLocalMountPoints) && 
157                    (curDirEntryInPage < CM_DIR_EPP) &&
158                    (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP)) 
159         {
160
161                 noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
162                 fakeEntry.fid.vnode = htonl(curDirEntry + 2);
163                 currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
164
165                 memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
166                 strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
167                 curDirEntry++;
168                 curDirEntryInPage++;
169                 for (i=0; i<noChunks; i++) {
170                         t1 = (curChunk + i) / 8;
171                         t2 = curChunk + i - (t1*8);
172                         fakeDirHeader.header.freeBitmap[t1] |= (1 << t2);
173                 }
174                 curChunk+=noChunks;
175         }
176
177         // when we get here, we're done with filling in the entries for page 0
178         // copy in the header info
179
180         memcpy(cm_FakeRootDir, &fakeDirHeader, 13 * CM_DIR_CHUNKSIZE);
181
182         curPage++;
183
184         // ok, page 0's done. Move on to the next page.
185         while (curDirEntry!=cm_noLocalMountPoints) {
186                 // setup a new page
187                 curChunk = 1;                   // the zeroth chunk is reserved for page header
188                 curDirEntryInPage = 0; 
189                 for (i=0; i<8; i++) {
190                         fakePageHeader.freeBitmap[i]=0;
191                 }
192                 fakePageHeader.freeCount = 0;
193                 fakePageHeader.pgcount = 0;
194                 fakePageHeader.tag = htons(1234);
195                 
196                 // while we're on the same page...
197                 while ( (curDirEntry!=cm_noLocalMountPoints) &&
198                                 (curDirEntryInPage < CM_DIR_EPP) &&
199                             (curChunk + cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0) <= CPP))
200                 {
201                         // add an entry to this page
202
203                         noChunks = cm_NameEntries((cm_localMountPoints+curDirEntry)->namep, 0);
204                         fakeEntry.fid.vnode=htonl(curDirEntry+2);
205                         currentPos = cm_FakeRootDir + curPage * CM_DIR_PAGESIZE + curChunk * CM_DIR_CHUNKSIZE;
206                         memcpy(currentPos, &fakeEntry, CM_DIR_CHUNKSIZE);
207                         strcpy(currentPos + 12, (cm_localMountPoints+curDirEntry)->namep);
208                         curDirEntry++;
209                         curDirEntryInPage++;
210                         for (i=0; i<noChunks; i++) {
211                                 t1 = (curChunk + i) / 8;
212                                 t2 = curChunk + i - (t1*8);
213                                 fakePageHeader.freeBitmap[t1] |= (1 << t2);
214                         }
215                         curChunk+=noChunks;
216                 }
217                 memcpy(cm_FakeRootDir + curPage * CM_DIR_PAGESIZE, &fakePageHeader, sizeof(fakePageHeader));
218
219                 curPage++;
220         }
221         
222         // we know the fakeDir is setup properly, so we claim that we have callback
223         cm_fakeDirCallback=1;
224
225         // when we get here, we've set up everything! done!
226
227
228 }
229
230 int cm_FakeRootFid(cm_fid_t *fidp)
231 {
232       fidp->cell = 0x1;            /* root cell */
233       fidp->volume = 0x20000001;   /* root.afs ? */
234       fidp->vnode = 0x1;
235       fidp->unique = 0x1;
236       return 0;
237 }
238   
239 int cm_getLocalMountPointChange() {
240   return cm_localMountPointChangeFlag;
241 }
242
243 int cm_clearLocalMountPointChange() {
244   cm_localMountPointChangeFlag = 0;
245   return 0;
246 }
247
248 /* called directly from ioctl */
249 /* called while not holding freelance lock */
250 int cm_noteLocalMountPointChange() {
251   lock_ObtainMutex(&cm_Freelance_Lock);
252   cm_fakeDirVersion++;
253   cm_localMountPointChangeFlag = 1;
254   lock_ReleaseMutex(&cm_Freelance_Lock);
255   return 1;
256 }
257
258 int cm_reInitLocalMountPoints() {
259         cm_fid_t aFid;
260         int i, hash;
261         cm_scache_t *scp, **lscpp, *tscp;
262
263         
264         printf("\n\n----- reinitialization starts ----- \n");
265
266
267         // first we invalidate all the SCPs that were created
268         // for the local mount points
269
270         printf("Invalidating local mount point scp...  ");
271
272         aFid.cell = 0x1;
273         aFid.volume=0x20000001;
274         aFid.unique=0x1;
275         aFid.vnode=0x2;
276
277         lock_ObtainWrite(&cm_scacheLock);
278         lock_ObtainMutex(&cm_Freelance_Lock);  /* always scache then freelance lock */
279         for (i=0; i<cm_noLocalMountPoints; i++) {
280                 hash = CM_SCACHE_HASH(&aFid);
281                 for (scp=cm_hashTablep[hash]; scp; scp=scp->nextp) {
282                         if (scp->fid.volume == aFid.volume &&
283                                 scp->fid.vnode == aFid.vnode &&
284                                 scp->fid.unique == aFid.unique 
285                                 ) {
286
287                                 // mark the scp to be reused
288                                 lock_ReleaseWrite(&cm_scacheLock);
289                                 lock_ObtainMutex(&scp->mx);
290                                 cm_DiscardSCache(scp);
291                                 lock_ReleaseMutex(&scp->mx);
292                                 cm_CallbackNotifyChange(scp);
293                                 lock_ObtainWrite(&cm_scacheLock);
294                                 scp->refCount--;
295
296                                 // take the scp out of the hash
297                                 lscpp = &cm_hashTablep[hash];
298                                 for (tscp=*lscpp; tscp; lscpp = &tscp->nextp, tscp = *lscpp) {
299                                         if (tscp == scp) break;
300                                 }
301                                 *lscpp = scp->nextp;
302                                 scp->flags &= ~CM_SCACHEFLAG_INHASH;
303
304
305                         }
306                 }
307                 aFid.vnode = aFid.vnode + 1;
308         }
309         lock_ReleaseWrite(&cm_scacheLock);
310         printf("\tall old scp cleared!\n");
311
312         // we must free the memory that was allocated in the prev
313         // cm_InitLocalMountPoints call
314         printf("Removing old localmountpoints...  ");
315         free(cm_localMountPoints);
316         printf("\tall old localmountpoints cleared!\n");
317
318         // now re-init the localmountpoints
319         printf("Creating new localmountpoints...  ");
320         cm_InitLocalMountPoints();
321         printf("\tcreated new set of localmountpoints!\n");
322         
323         
324         // now we have to free the memory allocated in cm_initfakerootdir
325         printf("Removing old fakedir...  ");
326         free(cm_FakeRootDir);
327         printf("\t\told fakedir removed!\n");
328
329         // then we re-create that dir
330         printf("Creating new fakedir...  ");
331         cm_InitFakeRootDir();
332         printf("\t\tcreated new fakedir!\n");   
333
334         lock_ReleaseMutex(&cm_Freelance_Lock);
335
336         printf("----- reinit complete -----\n\n");
337         return 0;
338 }
339
340
341 // yj: open up the ini file and read all the local mount 
342 // points that are stored there. Part of the initialization
343 // process for the freelance client.
344 /* to be called while holding freelance lock unless during init. */
345 long cm_InitLocalMountPoints() {
346         
347         FILE *fp;
348         char line[200];
349         int i;
350         char* t;
351         cm_localMountPoint_t* aLocalMountPoint;
352         char hdir[120];
353
354         cm_GetConfigDir(hdir);
355         strcat(hdir, AFS_FREELANCE_INI);
356         // open the ini file for reading
357         fp = fopen(hdir, "r");
358
359         // if we fail to open the file, create an empty one
360         if (!fp) {
361           fp = fopen(hdir, "w");
362           fputs("0\n", fp);
363           fclose(fp);
364           return 0;  /* success */
365         }
366
367         // we successfully opened the file
368 #ifdef DEBUG
369         fprintf(stderr, "opened afs_freelance.ini\n");
370 #endif
371         
372         // now we read the first line to see how many entries
373         // there are
374         fgets(line, 200, fp);
375
376         // if the line is empty at any point when we're reading
377         // we're screwed. report error and return.
378         if (*line==0) {
379                 afsi_log("error occurred while reading afs_freelance.ini");
380                 fprintf(stderr, "error occurred while reading afs_freelance.ini");
381                 return -1;
382         }
383
384         // get the number of entries there are from the first line
385         // that we read
386         cm_noLocalMountPoints = atoi(line);
387
388         // create space to store the local mount points
389         cm_localMountPoints = malloc(sizeof(cm_localMountPoint_t) * cm_noLocalMountPoints);
390         aLocalMountPoint = cm_localMountPoints;
391                 
392         // now we read n lines and parse them into local mount points
393         // where n is the number of local mount points there are, as
394         // determined above.
395         // Each line in the ini file represents 1 local mount point and 
396         // is in the format xxx#yyy:zzz, where xxx is the directory
397         // entry name, yyy is the cell name and zzz is the volume name.
398         // #yyy:zzz together make up the mount point.
399         for (i=0; i<cm_noLocalMountPoints; i++) {
400                 fgets(line, 200, fp);
401                 // check that the line is not empty
402                 if (line[0]==0) {
403                         afsi_log("error occurred while parsing entry in %s: empty line in line %d", AFS_FREELANCE_INI, i);
404                         fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: empty line in line %d", i);
405                         return -1;
406                 }
407                 // line is not empty, so let's parse it
408                 t = strchr(line, '#');
409                 // make sure that there is a '#' separator in the line
410                 if (!t) {
411                         afsi_log("error occurred while parsing entry in %s: no # separator in line %d", AFS_FREELANCE_INI, i);
412                         fprintf(stderr, "error occurred while parsing entry in afs_freelance.ini: no # separator in line %d", i);
413                         return -1;
414                 }
415                 aLocalMountPoint->namep=malloc(t-line+1);
416                 memcpy(aLocalMountPoint->namep, line, t-line);
417                 *(aLocalMountPoint->namep + (t-line)) = 0;
418                 aLocalMountPoint->mountPointStringp=malloc(strlen(line) - (t-line) + 1);
419                 memcpy(aLocalMountPoint->mountPointStringp, t, strlen(line)-(t-line)-2);
420                 *(aLocalMountPoint->mountPointStringp + (strlen(line)-(t-line)-2)) = 0;
421 #ifdef DEBUG
422                 fprintf(stderr, "found mount point: name %s, string %s\n",
423                         aLocalMountPoint->namep,
424                         aLocalMountPoint->mountPointStringp);
425 #endif
426                 
427                 aLocalMountPoint++;
428
429         }
430         fclose(fp);
431         return 0;
432 }
433
434
435 int cm_getNoLocalMountPoints() {
436         return cm_noLocalMountPoints;
437 }
438
439 cm_localMountPoint_t* cm_getLocalMountPoint(int vnode) {
440         return 0;
441 }
442
443 long cm_FreelanceAddMount(char *filename, char *cellname, char *volume, cm_fid_t *fidp)
444 {
445     FILE *fp;
446     char hfile[120];
447     char line[200];
448     char fullname[200];
449     int n;
450
451     /* before adding, verify the cell name; if it is not a valid cell,
452        don't add the mount point */
453     /* major performance issue? */
454     if (!cm_GetCell_Gen(cellname, fullname, CM_FLAG_CREATE))
455       return -1;
456 #if 0
457     if (strcmp(cellname, fullname) != 0)   /* no partial matches allowed */
458       return -1;
459 #endif
460     
461     lock_ObtainMutex(&cm_Freelance_Lock);
462
463      cm_GetConfigDir(hfile);
464      strcat(hfile, AFS_FREELANCE_INI);
465      fp = fopen(hfile, "r+");
466      if (!fp)
467        return CM_ERROR_INVAL;
468      fgets(line, 200, fp);
469      n = atoi(line);
470      n++;
471      fseek(fp, 0, SEEK_SET);
472      fprintf(fp, "%d", n);
473      fseek(fp, 0, SEEK_END);
474      fprintf(fp, "%s#%s:%s\n", filename, fullname, volume);
475      fclose(fp);
476      lock_ReleaseMutex(&cm_Freelance_Lock);
477
478      /*cm_reInitLocalMountPoints(&vnode);*/
479      if (fidp) {
480        fidp->unique = 1;
481        fidp->vnode = cm_noLocalMountPoints + 1;   /* vnode value of last mt pt */
482      }
483      cm_noteLocalMountPointChange();
484      
485      return 0;
486 }
487
488 long cm_FreelanceRemoveMount(char *toremove)
489 {
490      int i, n;
491      char* cp;
492      char line[200];
493      char shortname[200];
494      char hfile[120], hfile2[120];
495      FILE *fp1, *fp2;
496      int found=0;
497
498     lock_ObtainMutex(&cm_Freelance_Lock);
499
500      cm_GetConfigDir(hfile);
501      strcat(hfile, AFS_FREELANCE_INI);
502      strcpy(hfile2, hfile);
503      strcat(hfile2, "2");
504      fp1=fopen(hfile, "r+");
505      if (!fp1)
506        return CM_ERROR_INVAL;
507      fp2=fopen(hfile2, "w+");
508      if (!fp2) {
509        fclose(fp1);
510        return CM_ERROR_INVAL;
511      }
512
513      fgets(line, 200, fp1);
514      n=atoi(line);
515      fprintf(fp2, "%d\n", n-1);
516
517      for (i=0; i<n; i++) {
518           fgets(line, 200, fp1);
519           cp=strchr(line, '#');
520           memcpy(shortname, line, cp-line);
521           shortname[cp-line]=0;
522
523           if (strcmp(shortname, toremove)==0) {
524
525           } else {
526             found = 1;
527             fputs(line, fp2);
528           }
529      }
530
531      fclose(fp1);
532      fclose(fp2);
533      if (!found)
534        return CM_ERROR_NOSUCHFILE;
535
536      unlink(hfile);
537      rename(hfile2, hfile);
538
539      lock_ReleaseMutex(&cm_Freelance_Lock);
540
541      cm_noteLocalMountPointChange();
542      return 0;
543 }
544
545 #endif /* AFS_FREELANCE_CLIENT */