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