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