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