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