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