7c3f74c21e66ad2d5c2b9b60efd0acc1b28000a7
[openafs.git] / src / WINNT / afsd / cm_cell.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <nb30.h>
15 #include <winsock2.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <malloc.h>
19 #include <osi.h>
20 #include <string.h>
21
22 #include "afsd.h"
23
24 osi_rwlock_t cm_cellLock;
25
26 /* function called as callback proc from cm_SearchCellFile.  Return 0 to
27  * continue processing.  
28  *
29  * At the present time the return value is ignored by the caller.
30  */
31 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep, unsigned short ipRank)
32 {
33     cm_server_t *tsp;
34     cm_serverRef_t *tsrp;
35     cm_cell_t *cellp;
36     cm_cell_rock_t *cellrockp = (cm_cell_rock_t *)rockp;
37     afs_uint32 probe;
38         
39     cellp = cellrockp->cellp;
40     probe = !(cellrockp->flags & CM_FLAG_NOPROBE);
41
42     /* if this server was previously created by fs setserverprefs */
43     if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
44     {
45         if ( !tsp->cellp )
46             tsp->cellp = cellp;
47         else if (tsp->cellp != cellp) {
48             osi_Log3(afsd_logp, "found a vlserver %s associated with two cells named %s and %s",
49                      osi_LogSaveString(afsd_logp,hostnamep),
50                      osi_LogSaveString(afsd_logp,tsp->cellp->name), 
51                      osi_LogSaveString(afsd_logp,cellp->name));
52         }
53     }       
54     else
55         tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, NULL, probe ? 0 : CM_FLAG_NOPROBE);
56
57     tsp->ipRank = ipRank;
58
59     /* Insert the vlserver into a sorted list, sorted by server rank */
60     tsrp = cm_NewServerRef(tsp, 0);
61     cm_InsertServerList(&cellp->vlServersp, tsrp);
62     /* drop the allocation reference */
63     lock_ObtainWrite(&cm_serverLock);
64     tsrp->refCount--;
65     lock_ReleaseWrite(&cm_serverLock);
66
67     return 0;
68 }
69
70 /* if it's from DNS, see if it has expired 
71  * and check to make sure we have a valid set of volume servers
72  * this function must not be called with a lock on cm_cellLock
73  */
74 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
75 {
76     long code = 0;
77     cm_cell_rock_t rock;
78
79     if (cp == NULL)
80         return NULL;
81
82     lock_ObtainMutex(&cp->mx);
83     if ((cp->vlServersp == NULL 
84 #ifdef AFS_FREELANCE_CLIENT
85           && !(cp->flags & CM_CELLFLAG_FREELANCE)
86 #endif
87           ) || (time(0) > cp->timeout)
88 #ifdef AFS_AFSDB_ENV
89         || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
90          ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
91 #endif
92             ) 
93     {
94         lock_ReleaseMutex(&cp->mx);
95
96         /* must empty cp->vlServersp */
97         if (cp->vlServersp) {
98             cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
99             cp->vlServersp = NULL;
100         }
101
102         rock.cellp = cp;
103         rock.flags = flags;
104         code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
105         if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
106             code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
107         if (code == 0) {
108             lock_ObtainMutex(&cp->mx);
109             cp->timeout = time(0) + 7200;
110             lock_ReleaseMutex(&cp->mx);
111         }
112 #ifdef AFS_AFSDB_ENV
113         else {
114             if (cm_dnsEnabled) {
115                 int ttl;
116
117                 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
118                 if (code == 0) {   /* got cell from DNS */
119                     lock_ObtainMutex(&cp->mx);
120                     cp->flags |= CM_CELLFLAG_DNS;
121                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
122                     cp->timeout = time(0) + ttl;
123                     lock_ReleaseMutex(&cp->mx);
124 #ifdef DEBUG
125                     fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
126 #endif
127                 } else {
128                     /* if we fail to find it this time, we'll just do nothing and leave the
129                      * current entry alone 
130                      */
131                     lock_ObtainMutex(&cp->mx);
132                     cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
133                     lock_ReleaseMutex(&cp->mx);
134                 }
135             }
136         }
137 #endif /* AFS_AFSDB_ENV */
138     } else {
139         lock_ReleaseMutex(&cp->mx);
140     }
141     return code ? NULL : cp;
142 }
143
144 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
145 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
146 {
147     return cm_GetCell_Gen(namep, NULL, flags);
148 }
149
150 void cm_FreeCell(cm_cell_t *cellp)
151 {
152     lock_AssertWrite(&cm_cellLock);
153
154     if (cellp->vlServersp)
155         cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
156     cellp->name[0] = '\0';    
157
158     cellp->freeNextp = cm_data.freeCellsp;
159     cm_data.freeCellsp = cellp;
160 }
161
162 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
163 {
164     cm_cell_t *cp, *cp2;
165     long code;
166     char fullname[CELL_MAXNAMELEN]="";
167     char linkedName[CELL_MAXNAMELEN]="";
168     char name[CELL_MAXNAMELEN]="";
169     int  hasWriteLock = 0;
170     int  hasMutex = 0;
171     afs_uint32 hash;
172     cm_cell_rock_t rock;
173     size_t len;
174
175     if (namep == NULL || !namep[0] || !strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
176         return NULL;
177
178     /* 
179      * Strip off any trailing dots at the end of the cell name.
180      * Failure to do so results in an undesireable alias as the
181      * result of DNS AFSDB record lookups where a trailing dot
182      * has special meaning.
183      */
184     strncpy(name, namep, CELL_MAXNAMELEN);
185     for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
186         name[len-1] = '\0';
187     }
188     if (len == 0)
189         return NULL;
190     namep = name;
191
192     hash = CM_CELL_NAME_HASH(namep);
193
194     lock_ObtainRead(&cm_cellLock);
195     for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
196         if (cm_stricmp_utf8(namep, cp->name) == 0) {
197             strncpy(fullname, cp->name, CELL_MAXNAMELEN);
198             fullname[CELL_MAXNAMELEN-1] = '\0';
199             break;
200         }
201     }
202
203     if (!cp) {
204         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
205             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
206                 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
207                 fullname[CELL_MAXNAMELEN-1] = '\0';
208                 break;
209             }
210         }   
211     }
212
213     if (cp) {
214         lock_ReleaseRead(&cm_cellLock);
215         cm_UpdateCell(cp, flags);
216     } else if (flags & CM_FLAG_CREATE) {
217         lock_ConvertRToW(&cm_cellLock);
218         hasWriteLock = 1;
219
220         /* when we dropped the lock the cell could have been added
221          * to the list so check again while holding the write lock 
222          */
223         for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
224             if (cm_stricmp_utf8(namep, cp->name) == 0) {
225                 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
226                 fullname[CELL_MAXNAMELEN-1] = '\0';
227                 break;
228             }
229         }   
230
231         if (cp)
232             goto done;
233
234         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
235             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
236                 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
237                 fullname[CELL_MAXNAMELEN-1] = '\0';
238                 break;
239             }
240         }   
241
242         if (cp) {
243             lock_ReleaseWrite(&cm_cellLock);
244             lock_ObtainMutex(&cp->mx);
245             lock_ObtainWrite(&cm_cellLock);
246             cm_AddCellToNameHashTable(cp);
247             cm_AddCellToIDHashTable(cp);           
248             lock_ReleaseMutex(&cp->mx);
249             goto done;
250         }
251
252         if ( cm_data.freeCellsp != NULL ) {
253             cp = cm_data.freeCellsp;
254             cm_data.freeCellsp = cp->freeNextp;
255
256             /* 
257              * The magic, cellID, and mx fields are already set.
258              */
259         } else {
260             if ( cm_data.currentCells >= cm_data.maxCells )
261                 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
262
263             /* don't increment currentCells until we know that we 
264              * are going to keep this entry 
265              */
266             cp = &cm_data.cellBaseAddress[cm_data.currentCells];
267             memset(cp, 0, sizeof(cm_cell_t));
268             cp->magic = CM_CELL_MAGIC;
269
270             /* the cellID cannot be 0 */
271             cp->cellID = ++cm_data.currentCells;
272
273             /* otherwise we found the cell, and so we're nearly done */
274             lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
275         }
276
277         lock_ReleaseWrite(&cm_cellLock);
278         hasWriteLock = 0;
279
280         rock.cellp = cp;
281         rock.flags = flags;
282         code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
283         if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
284             code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
285         if (code) {
286             osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s", 
287                       osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
288                       osi_LogSaveString(afsd_logp,linkedName));
289
290 #ifdef AFS_AFSDB_ENV
291             if (cm_dnsEnabled) {
292                 int ttl;
293
294                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
295                 if ( code ) {
296                     osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
297                              osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
298                     lock_ObtainMutex(&cp->mx);
299                     lock_ObtainWrite(&cm_cellLock);
300                     hasWriteLock = 1;
301                     cm_RemoveCellFromIDHashTable(cp);
302                     cm_RemoveCellFromNameHashTable(cp);
303                     lock_ReleaseMutex(&cp->mx);
304                     cm_FreeCell(cp);
305                     cp = NULL;
306                     goto done;
307                 } else {   /* got cell from DNS */
308                     lock_ObtainMutex(&cp->mx);
309                     hasMutex = 1;
310                     cp->flags |= CM_CELLFLAG_DNS;
311                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
312                     cp->timeout = time(0) + ttl;
313                 }
314             } 
315             else 
316 #endif
317             {
318                 lock_ObtainMutex(&cp->mx);
319                 lock_ObtainWrite(&cm_cellLock);
320                 hasWriteLock = 1;
321                 cm_RemoveCellFromIDHashTable(cp);
322                 cm_RemoveCellFromNameHashTable(cp);
323                 lock_ReleaseMutex(&cp->mx);
324                 cm_FreeCell(cp);
325                 cp = NULL;
326                 goto done;
327             }
328         } else {
329             lock_ObtainMutex(&cp->mx);
330             hasMutex = 1;
331             cp->timeout = time(0) + 7200;       /* two hour timeout */
332         }
333
334         /* we have now been given the fullname of the cell.  It may
335          * be that we already have a cell with that name.  If so,
336          * we should use it instead of completing the allocation
337          * of a new cm_cell_t 
338          */
339         lock_ObtainRead(&cm_cellLock);
340         hash = CM_CELL_NAME_HASH(fullname);
341         for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
342             if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
343                 break;
344             }
345         }   
346
347         if (cp2) {
348             if (!hasMutex) {
349                 lock_ObtainMutex(&cp->mx);
350                 hasMutex = 1;
351             }
352             lock_ConvertRToW(&cm_cellLock);
353             hasWriteLock = 1;
354             cm_RemoveCellFromIDHashTable(cp);
355             cm_RemoveCellFromNameHashTable(cp);
356             lock_ReleaseMutex(&cp->mx);
357             hasMutex = 0;
358             cm_FreeCell(cp);
359             cp = cp2;
360             goto done;
361         }
362         lock_ReleaseRead(&cm_cellLock);
363
364         /* randomise among those vlservers having the same rank*/ 
365         cm_RandomizeServer(&cp->vlServersp);
366
367         if (!hasMutex)
368             lock_ObtainMutex(&cp->mx);
369
370         /* copy in name */
371         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
372         cp->name[CELL_MAXNAMELEN-1] = '\0';
373
374         strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
375         cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
376
377         lock_ObtainWrite(&cm_cellLock);
378         hasWriteLock = 1;
379         cm_AddCellToNameHashTable(cp);
380         cm_AddCellToIDHashTable(cp);   
381         lock_ReleaseMutex(&cp->mx);
382         hasMutex = 0;
383
384         /* append cell to global list */
385         if (cm_data.allCellsp == NULL) {
386             cm_data.allCellsp = cp;
387         } else {
388             for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
389                 ;
390             cp2->allNextp = cp;
391         }
392         cp->allNextp = NULL;
393
394     } else {
395         lock_ReleaseRead(&cm_cellLock);
396     }
397   done:
398     if (hasMutex && cp)
399         lock_ReleaseMutex(&cp->mx);
400     if (hasWriteLock)
401         lock_ReleaseWrite(&cm_cellLock);
402     
403     /* fullname is not valid if cp == NULL */
404     if (newnamep) {
405         if (cp) {
406             strncpy(newnamep, fullname, CELL_MAXNAMELEN);
407             newnamep[CELL_MAXNAMELEN-1]='\0';
408         } else {
409             newnamep[0] = '\0';
410         }
411     }
412
413     if (cp && cp->linkedName[0]) {
414         cm_cell_t * linkedCellp = NULL;
415         
416         if (!strcmp(cp->name, cp->linkedName)) {
417             cp->linkedName[0] = '\0'; 
418         } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
419             linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
420
421             lock_ObtainWrite(&cm_cellLock);
422             if (!linkedCellp || 
423                 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
424                 cp->linkedName[0] = '\0';
425             } else {
426                 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
427                 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
428             }
429             lock_ReleaseWrite(&cm_cellLock);
430         }
431     }
432     return cp;
433 }
434
435 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
436 {
437     cm_cell_t *cp;
438     afs_uint32 hash;
439
440     lock_ObtainRead(&cm_cellLock);
441
442     hash = CM_CELL_ID_HASH(cellID);
443
444     for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
445         if (cellID == cp->cellID) 
446             break;
447     }
448     lock_ReleaseRead(&cm_cellLock);     
449
450     if (cp)
451         cm_UpdateCell(cp, flags);
452
453     return cp;
454 }
455
456 long 
457 cm_ValidateCell(void)
458 {
459     cm_cell_t * cellp;
460     afs_uint32 count1, count2;
461
462     for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
463         if ( cellp->magic != CM_CELL_MAGIC ) {
464             afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
465             fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
466             return -1;
467         }
468         if ( count1 != 0 && cellp == cm_data.allCellsp ||
469              count1 > cm_data.maxCells ) {
470             afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
471             fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
472             return -2;
473         }
474     }
475
476     for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
477         if ( count2 != 0 && cellp == cm_data.freeCellsp ||
478              count2 > cm_data.maxCells ) {
479             afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
480             fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
481             return -3;
482         }
483     }
484
485     if ( (count1 + count2) != cm_data.currentCells ) {
486         afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
487         fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
488         return -4;
489     }
490     
491     return 0;
492 }
493
494
495 long 
496 cm_ShutdownCell(void)
497 {
498     cm_cell_t * cellp;
499
500     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
501         lock_FinalizeMutex(&cellp->mx);
502
503     return 0;
504 }
505
506
507 void cm_InitCell(int newFile, long maxCells)
508 {
509     static osi_once_t once;
510         
511     if (osi_Once(&once)) {
512         cm_cell_t * cellp;
513
514         lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
515
516         if ( newFile ) {
517             cm_data.allCellsp = NULL;
518             cm_data.currentCells = 0;
519             cm_data.maxCells = maxCells;
520             memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
521             memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
522         
523 #ifdef AFS_FREELANCE_CLIENT
524             /* Generate a dummy entry for the Freelance cell whether or not 
525              * freelance mode is being used in this session 
526              */
527
528             cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
529             memset(cellp, 0, sizeof(cm_cell_t));
530             cellp->magic = CM_CELL_MAGIC;
531
532             lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
533
534             lock_ObtainMutex(&cellp->mx);
535             lock_ObtainWrite(&cm_cellLock);
536
537             /* copy in name */
538             strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
539             cellp->name[CELL_MAXNAMELEN-1] = '\0';
540
541             /* thread on global list */
542             cellp->allNextp = cm_data.allCellsp;
543             cm_data.allCellsp = cellp;
544                 
545             cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
546             cellp->vlServersp = NULL;
547             cellp->flags = CM_CELLFLAG_FREELANCE;
548
549             cm_AddCellToNameHashTable(cellp);
550             cm_AddCellToIDHashTable(cellp);
551             lock_ReleaseWrite(&cm_cellLock);
552             lock_ReleaseMutex(&cellp->mx);
553 #endif  
554         } else {
555             lock_ObtainRead(&cm_cellLock);
556             for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
557                 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
558                 cellp->vlServersp = NULL;
559                 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
560             }
561             lock_ReleaseRead(&cm_cellLock);
562         }
563
564         osi_EndOnce(&once);
565     }
566 }
567
568 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
569 {
570     cm_cell_t *cp;
571     int code;
572
573     cp = tsp->cellp;    /* cell that this vlserver belongs to */
574     if (cp) {
575         lock_ObtainMutex(&cp->mx);
576         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
577
578         if ( !code )            /* if the server list was rearranged */
579             cm_RandomizeServer(&cp->vlServersp);
580
581         lock_ReleaseMutex(&cp->mx);
582     }
583 }       
584
585 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
586 {
587     cm_cell_t *cellp;
588     int zilch;
589     char output[1024];
590
591     if (lock)
592         lock_ObtainRead(&cm_cellLock);
593
594     sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n", 
595             cookie, cm_data.currentCells, cm_data.maxCells);
596     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
597
598     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
599         sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n", 
600                 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
601         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
602     }
603
604     sprintf(output, "%s - Done dumping cells.\r\n", cookie);
605     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
606
607     if (lock)
608         lock_ReleaseRead(&cm_cellLock);
609
610     return(0);
611 }
612
613 /* call with volume write-locked and mutex held */
614 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
615 {
616     int i;
617     
618     lock_AssertWrite(&cm_cellLock);
619     lock_AssertMutex(&cellp->mx);
620
621     if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
622         return;
623
624     i = CM_CELL_NAME_HASH(cellp->name);
625
626     cellp->nameNextp = cm_data.cellNameHashTablep[i];
627     cm_data.cellNameHashTablep[i] = cellp;
628     cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
629 }
630
631 /* call with cell write-locked and mutex held */
632 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
633 {
634     cm_cell_t **lcellpp;
635     cm_cell_t *tcellp;
636     int i;
637         
638     lock_AssertWrite(&cm_cellLock);
639     lock_AssertMutex(&cellp->mx);
640
641     if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
642         /* hash it out first */
643         i = CM_CELL_NAME_HASH(cellp->name);
644         for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
645              tcellp;
646              lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
647             if (tcellp == cellp) {
648                 *lcellpp = cellp->nameNextp;
649                 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
650                 cellp->nameNextp = NULL;
651                 break;
652             }
653         }
654     }
655 }
656
657 /* call with cell write-locked and mutex held */
658 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
659 {
660     int i;
661     
662     lock_AssertWrite(&cm_cellLock);
663     lock_AssertMutex(&cellp->mx);
664
665     if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
666         return;
667
668     i = CM_CELL_ID_HASH(cellp->cellID);
669
670     cellp->idNextp = cm_data.cellIDHashTablep[i];
671     cm_data.cellIDHashTablep[i] = cellp;
672     cellp->flags |= CM_CELLFLAG_IN_IDHASH;
673 }
674
675 /* call with cell write-locked and mutex held */
676 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
677 {
678     cm_cell_t **lcellpp;
679     cm_cell_t *tcellp;
680     int i;
681         
682     lock_AssertWrite(&cm_cellLock);
683     lock_AssertMutex(&cellp->mx);
684
685     if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
686         /* hash it out first */
687         i = CM_CELL_ID_HASH(cellp->cellID);
688         for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
689              tcellp;
690              lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
691             if (tcellp == cellp) {
692                 *lcellpp = cellp->idNextp;
693                 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
694                 cellp->idNextp = NULL;
695                 break;
696             }
697         }
698     }
699 }
700