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