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