openbsd-20021114
[openafs.git] / src / afs / afs_volume.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 /*
11  * Implements:
12  * afs_vtoi (local)
13  * afs_UFSGetVolSlot
14  * afs_MemGetVolSlot
15  * afs_CheckVolumeNames
16  * afs_FindVolume
17  */
18 #include <afsconfig.h>
19 #include "afs/param.h"
20
21 RCSID("$Header$");
22
23 #include "afs/stds.h"
24 #include "afs/sysincludes.h"    /* Standard vendor system headers */
25
26 #if !defined(UKERNEL)
27 #include <net/if.h>
28 #include <netinet/in.h>
29
30 #ifdef AFS_SGI62_ENV
31 #include "h/hashing.h"
32 #endif
33 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
34 #include <netinet/in_var.h>
35 #endif /* ! AFS_HPUX110_ENV */
36 #endif /* !defined(UKERNEL) */
37
38 #include "afsincludes.h"        /* Afs-based standard headers */
39 #include "afs/afs_stats.h"   /* afs statistics */
40
41 #if     defined(AFS_SUN56_ENV)
42 #include <inet/led.h>
43 #include <inet/common.h>
44 #if     defined(AFS_SUN58_ENV)
45 #include <netinet/ip6.h>
46 #endif
47 #include <inet/ip.h>
48 #endif
49
50 /* In case we don't have the vl error table yet. */
51 #ifndef ERROR_TABLE_BASE_VL
52 #define ERROR_TABLE_BASE_VL     (363520L)
53 #define VL_NOENT                (363524L)
54 #endif /* vlserver error base define */
55
56 /* Exported variables */
57 ino_t volumeInode;                      /*Inode for VolumeItems file*/
58 afs_rwlock_t afs_xvolume;               /* allocation lock for volumes */
59 struct volume *afs_freeVolList;
60 struct volume *afs_volumes[NVOLS];
61 afs_int32 afs_volCounter = 1; /* for allocating volume indices */
62 afs_int32 fvTable[NFENTRIES];
63
64 /* Forward declarations */
65 static struct volume *afs_NewVolumeByName(char *aname, afs_int32 acell, int agood,
66                                    struct vrequest *areq, afs_int32 locktype);
67 static struct volume *afs_NewDynrootVolume(struct VenusFid *fid);
68 static int inVolList(struct VenusFid *fid, afs_int32 nvols, afs_int32 *vID, afs_int32 *cID);
69
70
71 /* Convert a volume name to a #; return 0 if can't parse as a number */
72 static int afs_vtoi(register char *aname)
73 {
74     register afs_int32 temp;
75     register int tc;
76     temp = 0;
77     AFS_STATCNT(afs_vtoi);
78     while((tc = *aname++)) {
79         if (tc > '9' || tc < '0')
80             return 0; /* invalid name */
81         temp *= 10;
82         temp += tc - '0';
83     }
84     return temp;
85
86 } /*afs_vtoi*/
87
88
89 /*
90  * All of the vol cache routines must be called with the afs_xvolume
91  * lock held in exclusive mode, since they use static variables.
92  * In addition, we don't want two people adding the same volume
93  * at the same time.
94  */
95
96 static struct fvolume staticFVolume;
97 afs_int32 afs_FVIndex = -1;
98
99 /* UFS specific version of afs_GetVolSlot */
100
101 struct volume *afs_UFSGetVolSlot(void)
102 {
103     register struct volume *tv, **lv;
104     struct osi_file *tfile;
105     register afs_int32 i, code;
106     afs_int32 bestTime;
107     struct volume *bestVp, **bestLp;
108
109     AFS_STATCNT(afs_UFSGetVolSlot);
110     if (!afs_freeVolList) {
111         /* get free slot */
112         bestTime = 0x7fffffff;
113         bestVp = 0;
114         bestLp = 0;
115         for (i=0;i<NVOLS;i++) {
116             lv = &afs_volumes[i];
117             for (tv = *lv; tv; lv = &tv->next, tv = *lv) {
118                 if (tv->refCount == 0) {    /* is this one available? */
119                     if (tv->accessTime < bestTime) { /* best one available? */
120                         bestTime = tv->accessTime;
121                         bestLp = lv;
122                         bestVp = tv;
123                     }
124                 }
125             }
126         }
127         if (!bestVp)
128             osi_Panic("getvolslot none");
129         tv = bestVp;
130         *bestLp = tv->next;
131         if (tv->name)
132             afs_osi_Free(tv->name, strlen(tv->name)+1);
133         tv->name = NULL;
134         /* now write out volume structure to file */
135         if (tv->vtix < 0) {
136             tv->vtix = afs_volCounter++;
137             /* now put on hash chain */
138             i = FVHash(tv->cell, tv->volume);
139             staticFVolume.next = fvTable[i];
140             fvTable[i]=tv->vtix;
141         }
142         else {
143             /*
144              * Haul the guy in from disk so we don't overwrite hash table
145              * next chain
146              */
147             if (afs_FVIndex != tv->vtix) {
148                 tfile = osi_UFSOpen(volumeInode);
149                 code = afs_osi_Read(tfile, sizeof(struct fvolume) * tv->vtix,
150                                 &staticFVolume, sizeof(struct fvolume));
151                 if (code != sizeof(struct fvolume))
152                     osi_Panic("read volumeinfo");
153                 osi_UFSClose(tfile);
154                 afs_FVIndex = tv->vtix;
155             }
156         }
157         afs_FVIndex = tv->vtix;
158         staticFVolume.volume = tv->volume;
159         staticFVolume.cell = tv->cell;
160         staticFVolume.mtpoint = tv->mtpoint;
161         staticFVolume.dotdot = tv->dotdot;
162         staticFVolume.rootVnode = tv->rootVnode;
163         staticFVolume.rootUnique = tv->rootUnique;
164         tfile = osi_UFSOpen(volumeInode);
165         code = afs_osi_Write(tfile, sizeof(struct fvolume) * afs_FVIndex, 
166                          &staticFVolume, sizeof(struct fvolume));
167         if (code != sizeof(struct fvolume))
168             osi_Panic("write volumeinfo");
169         osi_UFSClose(tfile);    
170     }
171     else {
172         tv = afs_freeVolList;
173         afs_freeVolList = tv->next;
174     }
175     return tv;
176
177 } /*afs_UFSGetVolSlot*/
178
179
180 struct volume *afs_MemGetVolSlot(void)
181 {
182     register struct volume *tv;
183
184     AFS_STATCNT(afs_MemGetVolSlot);
185     if (!afs_freeVolList) {
186         struct volume *newVp;
187
188         newVp = (struct volume *) afs_osi_Alloc(sizeof(struct volume));
189
190         newVp->next = NULL;
191         afs_freeVolList = newVp;
192     }
193     tv = afs_freeVolList;
194     afs_freeVolList = tv->next;
195     return tv;
196
197 } /*afs_MemGetVolSlot*/
198
199 /* afs_ResetVolumes()
200  * Reset volume inforamation for all volume structs that
201  * point to a speicific server.
202  */
203 void afs_ResetVolumes(struct server *srvp)
204 {
205   int j, k;
206   struct volume *vp;
207
208   /* Find any volumes residing on this server and flush their state */
209   for (j=0; j<NVOLS; j++) {
210      for (vp=afs_volumes[j]; vp; vp=vp->next) {
211         for (k=0; k<MAXHOSTS; k++) {
212            if (!srvp || (vp->serverHost[k] == srvp)) {
213               vp->serverHost[k] = 0;
214               afs_ResetVolumeInfo(vp);
215               break;
216            }
217         }
218      }
219   }
220 }
221
222
223 /* reset volume name to volume id mapping  cache */
224 void afs_CheckVolumeNames(int flags) 
225 {
226     afs_int32 i,j;
227     struct volume *tv;
228     unsigned int now;
229     struct vcache *tvc;
230     afs_int32 *volumeID, *cellID, vsize, nvols;
231     AFS_STATCNT(afs_CheckVolumeNames);
232
233     nvols = 0;
234     volumeID = cellID = NULL;
235     vsize=0;
236     ObtainReadLock(&afs_xvolume);
237     if (flags & AFS_VOLCHECK_EXPIRED) {
238         /*
239          * allocate space to hold the volumeIDs and cellIDs, only if
240          * we will be invalidating the mountpoints later on
241          */
242         for (i=0; i< NVOLS; i++)
243             for (tv = afs_volumes[i]; tv; tv=tv->next)
244                 ++vsize;
245
246         volumeID = (afs_int32 *) afs_osi_Alloc(2 * vsize * sizeof(*volumeID));
247         cellID   = (volumeID) ? volumeID + vsize : 0;
248     }
249
250     now = osi_Time();
251     for (i=0;i<NVOLS;i++) {
252         for (tv = afs_volumes[i]; tv; tv=tv->next) {
253             if (flags & AFS_VOLCHECK_EXPIRED) {
254                     if ( ((tv->expireTime<(now+10)) && (tv->states & VRO)) ||
255                                 (flags & AFS_VOLCHECK_FORCE)){
256                     afs_ResetVolumeInfo(tv);  /* also resets status */
257                     if (volumeID) {
258                         volumeID[nvols] = tv->volume;
259                         cellID[nvols]   = tv->cell;
260                     }
261                     ++nvols;
262                     continue;
263                 }
264             }
265             if (flags & (AFS_VOLCHECK_BUSY | AFS_VOLCHECK_FORCE)) {
266                 for (j=0; j<MAXHOSTS; j++) 
267                     tv->status[j] = not_busy;
268             }
269
270         }
271     }
272     ReleaseReadLock(&afs_xvolume);
273
274
275     /* next ensure all mt points are re-evaluated */
276     if (nvols || (flags & (AFS_VOLCHECK_FORCE | AFS_VOLCHECK_MTPTS))) {
277         ObtainReadLock(&afs_xvcache);
278         for(i=0;i<VCSIZE;i++) {
279             for(tvc = afs_vhashT[i]; tvc; tvc=tvc->hnext) {
280
281                 /* if the volume of "mvid" of the vcache entry is among the
282                  * ones we found earlier, then we re-evaluate it.  Also, if the
283                  * force bit is set or we explicitly asked to reevaluate the
284                  * mt-pts, we clean the cmvalid bit */
285
286                 if ((flags & (AFS_VOLCHECK_FORCE | AFS_VOLCHECK_MTPTS)) ||
287                     (tvc->mvid &&
288                      inVolList(tvc->mvid, nvols, volumeID, cellID)))
289                     tvc->states &= ~CMValid;
290
291                 /* If the volume that this file belongs to was reset earlier,
292                  * then we should remove its callback.
293                  * Again, if forced, always do it.
294                  */
295                 if ((tvc->states & CRO) &&
296                     (inVolList(&tvc->fid, nvols, volumeID, cellID) ||
297                      (flags & AFS_VOLCHECK_FORCE))) {
298
299                     AFS_FAST_HOLD(tvc);
300                     ReleaseReadLock(&afs_xvcache);
301
302                     ObtainWriteLock(&afs_xcbhash, 485);
303                     /* LOCKXXX: We aren't holding tvc write lock? */
304                     afs_DequeueCallback(tvc);
305                     tvc->states &= ~CStatd;
306                     ReleaseWriteLock(&afs_xcbhash);
307                     if (tvc->fid.Fid.Vnode & 1 || (vType(tvc) == VDIR))
308                         osi_dnlc_purgedp(tvc);
309
310                     ObtainReadLock(&afs_xvcache);
311
312                     /* our tvc ptr is still good until now */
313                     AFS_FAST_RELE(tvc);
314                 }
315             }
316         }
317         osi_dnlc_purge ();   /* definitely overkill, but it's safer this way. */
318         ReleaseReadLock(&afs_xvcache);
319     }
320
321     if (volumeID)
322         afs_osi_Free(volumeID, 2 * vsize * sizeof(*volumeID));
323
324 } /*afs_CheckVolumeNames*/
325
326
327 static int inVolList(struct VenusFid *fid, afs_int32 nvols, afs_int32 *vID, afs_int32 *cID)
328 {
329     afs_int32 i;
330
331     /* if no arrays present, be conservative and return true */
332     if ( nvols && (!vID || !cID))
333         return 1;
334
335     for (i=0; i< nvols; ++i) {
336         if (fid->Fid.Volume == vID[i] && fid->Cell == cID[i])
337             return 1;
338     }
339     return 0;
340 }
341
342
343 /* afs_PutVolume is now a macro in afs.h */
344
345
346 /* afs_FindVolume()
347  *  return volume struct if we have it cached and it's up-to-date
348  *
349  *  Environment:  Must be called with afs_xvolume unlocked.
350  */
351 struct volume *afs_FindVolume(struct VenusFid *afid, afs_int32 locktype)
352 {
353     struct volume *tv;
354     afs_int32 i;
355
356     if (afid == NULL)
357         return NULL;
358
359     i = VHash(afid->Fid.Volume);
360     ObtainWriteLock(&afs_xvolume,106);
361     for (tv = afs_volumes[i]; tv; tv=tv->next) {
362         if (tv->volume == afid->Fid.Volume && tv->cell == afid->Cell
363             && (tv->states & VRecheck) == 0) {
364             tv->refCount++;
365             break;
366           }
367     }
368     ReleaseWriteLock(&afs_xvolume);
369     return tv;      /* NULL if we didn't find it */
370 } /*afs_FindVolume*/
371
372
373
374 /*
375  * Note that areq may be null, in which case we don't bother to set any
376  * request status information.
377  */
378 struct volume *afs_GetVolume(struct VenusFid *afid, struct vrequest *areq, 
379         afs_int32 locktype)
380 {
381     struct volume *tv;
382     char *bp, tbuf[CVBS];
383     AFS_STATCNT(afs_GetVolume);
384
385     tv = afs_FindVolume(afid, locktype);
386     if (!tv) {
387        if (afs_IsDynrootFid(afid)) {
388           tv = afs_NewDynrootVolume(afid);
389        } else {
390           bp = afs_cv2string(&tbuf[CVBS], afid->Fid.Volume);
391           tv = afs_NewVolumeByName(bp, afid->Cell, 0, areq, locktype);
392        }
393     }
394     return tv;
395 } /*afs_GetVolume*/
396
397
398
399 static struct volume *afs_SetupVolume(afs_int32 volid, char *aname, 
400         char *ve, struct cell *tcell, afs_int32 agood, afs_int32 type, struct vrequest *areq)
401 {
402     struct volume *tv;
403     struct vldbentry *ove = (struct vldbentry *)ve;
404     struct nvldbentry *nve = (struct nvldbentry *)ve;
405     struct uvldbentry *uve = (struct uvldbentry *)ve;
406
407     int whichType;  /* which type of volume to look for */
408     int i, j, err=0;
409
410     if (!volid) {
411         int len;
412         /* special hint from file server to use vlserver */
413         len = strlen(aname);
414         if (len >= 8 && strcmp(aname+len-7, ".backup") == 0)
415             whichType = BACKVOL;
416         else if (len >= 10 && strcmp(aname+len-9, ".readonly")==0)
417             whichType = ROVOL;
418         else 
419             whichType = RWVOL;
420
421         /* figure out which one we're really interested in (a set is returned) */
422         volid = afs_vtoi(aname);
423         if (volid == 0) {
424             if (type == 2) {
425                 volid = uve->volumeId[whichType];
426             }
427             else if (type == 1) {
428                 volid = nve->volumeId[whichType];
429             }
430             else {
431                 volid = ove->volumeId[whichType];
432             }
433         }
434     }
435
436     
437     ObtainWriteLock(&afs_xvolume,108);
438     i = VHash(volid);
439     for (tv = afs_volumes[i]; tv; tv=tv->next) {
440         if (tv->volume == volid && tv->cell == tcell->cellNum) {
441             break;
442         }
443     }
444     if (!tv) {
445         struct fvolume *tf=0;
446
447         tv = afs_GetVolSlot();
448         memset((char *)tv, 0, sizeof(struct volume));
449         tv->cell = tcell->cellNum;
450         RWLOCK_INIT(&tv->lock, "volume lock");
451         tv->next = afs_volumes[i];      /* thread into list */
452         afs_volumes[i] = tv;
453         tv->volume = volid;
454         for (j=fvTable[FVHash(tv->cell,volid)]; j!=0; j=tf->next) {
455             if (afs_FVIndex != j) {
456                 struct osi_file *tfile;
457                 tfile = osi_UFSOpen(volumeInode);
458                 err = afs_osi_Read(tfile, sizeof(struct fvolume) * j, &staticFVolume, sizeof(struct fvolume));
459                 if (err != sizeof(struct fvolume))
460                     osi_Panic("read volumeinfo2");
461                 osi_UFSClose(tfile);
462                 afs_FVIndex = j;
463             }
464             tf = &staticFVolume;
465             if (tf->cell == tv->cell && tf->volume == volid)
466                 break;
467         }
468         if (tf && (j != 0)) {
469             tv->vtix = afs_FVIndex;
470             tv->mtpoint = tf->mtpoint;
471             tv->dotdot = tf->dotdot;
472             tv->rootVnode = tf->rootVnode;          
473             tv->rootUnique = tf->rootUnique;        
474         } else {
475             tv->vtix = -1;
476             tv->rootVnode = tv->rootUnique = 0;
477         }
478     }
479     tv->refCount++;
480     tv->states &= ~VRecheck;        /* just checked it */
481     tv->accessTime = osi_Time();
482     ReleaseWriteLock(&afs_xvolume);
483     ObtainWriteLock(&tv->lock,111);
484     if (type == 2) {
485         InstallUVolumeEntry(tv, uve, tcell->cellNum, tcell, areq);
486     }
487     else if (type == 1)
488         InstallNVolumeEntry(tv, nve, tcell->cellNum);
489     else
490         InstallVolumeEntry(tv, ove, tcell->cellNum);
491     if (agood) {
492         if (!tv->name) {
493             tv->name = afs_osi_Alloc(strlen(aname) + 1);
494             strcpy(tv->name, aname);
495         }
496     }
497     for (i=0; i<NMAXNSERVERS; i++) {
498        tv->status[i] = not_busy;
499     }
500     ReleaseWriteLock(&tv->lock);
501     return tv;
502 }
503
504
505 struct volume *afs_GetVolumeByName(register char *aname, afs_int32 acell, 
506         int agood, struct vrequest *areq, afs_int32 locktype)
507 {
508   afs_int32 i;
509   struct volume *tv;
510
511     AFS_STATCNT(afs_GetVolumeByName);
512     ObtainWriteLock(&afs_xvolume,112);
513   for (i=0;i<NVOLS;i++) {
514         for (tv = afs_volumes[i]; tv; tv=tv->next) {
515             if (tv->name && !strcmp(aname,tv->name) && tv->cell == acell
516                 && (tv->states&VRecheck) == 0) {
517                 tv->refCount++;
518                 ReleaseWriteLock(&afs_xvolume);
519                 return tv;
520             }
521         }
522     }
523
524     ReleaseWriteLock(&afs_xvolume);
525
526   tv = afs_NewVolumeByName(aname, acell, agood, areq, locktype);
527   return(tv);
528 }
529
530 static struct volume *afs_NewDynrootVolume(struct VenusFid *fid)
531 {
532     struct cell *tcell;
533     struct volume *tv;
534     struct vldbentry tve;
535     char *bp, tbuf[CVBS];
536
537     tcell = afs_GetCell(fid->Cell, READ_LOCK);
538     if (!tcell)
539         return NULL;
540     if (!(tcell->states & CHasVolRef))
541         tcell->states |= CHasVolRef;
542
543     bp = afs_cv2string(&tbuf[CVBS], fid->Fid.Volume);
544     memset(&tve, 0, sizeof(tve));
545     strcpy(tve.name, "local-dynroot");
546     tve.volumeId[ROVOL] = fid->Fid.Volume;
547     tve.flags = VLF_ROEXISTS;
548
549     tv = afs_SetupVolume(0, bp, (char *) &tve, tcell, 0, 0, 0);
550     afs_PutCell(tcell, READ_LOCK);
551     return tv;
552 }
553
554 int lastnvcode;
555 static struct volume *afs_NewVolumeByName(char *aname, afs_int32 acell, int agood,
556                                    struct vrequest *areq, afs_int32 locktype)
557 {
558     afs_int32 code, type=0;
559     struct volume *tv, *tv1;
560     struct vldbentry *tve;
561     struct nvldbentry *ntve;
562     struct uvldbentry *utve;
563     struct cell *tcell;
564     char *tbuffer, *ve;
565     struct conn *tconn;
566     struct vrequest treq;
567
568     if (strlen(aname) > VL_MAXNAMELEN)  /* Invalid volume name */
569         return NULL;
570
571     tcell = afs_GetCell(acell, READ_LOCK);
572     if (!tcell) {
573         return NULL;
574     }
575
576     /* allow null request if we don't care about ENODEV/ETIMEDOUT distinction */
577     if (!areq)  areq = &treq;
578
579
580     afs_Trace2(afs_iclSetp, CM_TRACE_GETVOL, ICL_TYPE_STRING, aname,
581                            ICL_TYPE_POINTER, aname);
582     tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
583     tve = (struct vldbentry *) (tbuffer+1024);
584     ntve = (struct nvldbentry *)tve;
585     utve = (struct uvldbentry *)tve;
586     afs_InitReq(&treq, &afs_osi_cred);  /* *must* be unauth for vldb */
587     do {
588         tconn = afs_ConnByMHosts(tcell->cellHosts, tcell->vlport,
589                                  tcell->cellNum, &treq, SHARED_LOCK);
590         if (tconn) {
591             if (tconn->srvr->server->flags & SNO_LHOSTS) {
592                 type = 0;
593                 RX_AFS_GUNLOCK();
594                 code = VL_GetEntryByNameO(tconn->id, aname, tve);
595                 RX_AFS_GLOCK();
596             } else if (tconn->srvr->server->flags & SYES_LHOSTS) {
597                 type = 1;
598                 RX_AFS_GUNLOCK();
599                 code = VL_GetEntryByNameN(tconn->id, aname, ntve);
600                 RX_AFS_GLOCK();
601             } else {
602                 type = 2;
603                 RX_AFS_GUNLOCK();
604                 code = VL_GetEntryByNameU(tconn->id, aname, utve);
605                 RX_AFS_GLOCK();
606                 if (!(tconn->srvr->server->flags & SVLSRV_UUID)) {
607                     if (code == RXGEN_OPCODE) {
608                         type = 1;
609                         RX_AFS_GUNLOCK();
610                         code = VL_GetEntryByNameN(tconn->id, aname, ntve);
611                         RX_AFS_GLOCK();
612                         if (code == RXGEN_OPCODE) {
613                             type = 0;
614                             tconn->srvr->server->flags |= SNO_LHOSTS;
615                             RX_AFS_GUNLOCK();
616                             code = VL_GetEntryByNameO(tconn->id, aname, tve);
617                             RX_AFS_GLOCK();
618                         } else if (!code)
619                             tconn->srvr->server->flags |= SYES_LHOSTS;
620                     } else if (!code)
621                             tconn->srvr->server->flags |= SVLSRV_UUID;
622                 }
623                 lastnvcode = code;
624             }
625         } else
626             code = -1;
627     } while
628       (afs_Analyze(tconn, code, NULL, &treq,
629                    -1, /* no op code for this */
630                    SHARED_LOCK, tcell));
631
632     if (code) {
633         /* If the client has yet to contact this cell and contact failed due
634          * to network errors, mark the VLDB servers as back up.
635          * That the client tried and failed can be determined from the
636          * fact that there was a downtime incident, but CHasVolRef is not set.
637          */
638         if (areq->networkError && !(tcell->states & CHasVolRef)) {
639             int i;
640             struct server *sp;
641             struct srvAddr *sap;
642             for (i=0; i<MAXCELLHOSTS; i++) {
643                 if ((sp = tcell->cellHosts[i]) == NULL) break;
644                 for (sap = sp->addr; sap; sap = sap->next_sa)
645                     afs_MarkServerUpOrDown(sap, 0);
646             }
647         }
648         afs_CopyError(&treq, areq);
649         osi_FreeLargeSpace(tbuffer);
650         afs_PutCell(tcell, READ_LOCK);
651         return NULL;
652     }
653     /*
654      * Check to see if this cell has not yet referenced a volume.  If
655      * it hasn't, it's just about to change its status, and we need to mark
656      * this fact down. Note that it is remotely possible that afs_SetupVolume
657      * could fail and we would still not have a volume reference.
658      */
659     if (!(tcell->states & CHasVolRef)) {
660         tcell->states |= CHasVolRef;
661         afs_stats_cmperf.numCellsContacted++;
662     } /*First time a volume in this cell has been referenced*/
663
664     if (type == 2)
665         ve = (char *)utve;
666     else if (type == 1)
667         ve = (char *)ntve;
668     else
669         ve = (char *)tve;
670     tv = afs_SetupVolume(0, aname, ve, tcell, agood, type, areq);
671     if ((agood == 2) && tv->roVol) {
672         /*
673          * This means that very soon we'll ask for the RO volume so
674          * we'll prefetch it (well we did already.)
675          */
676         tv1 = afs_SetupVolume(tv->roVol, NULL, ve, tcell, 0, type, areq);
677         tv1->refCount--;
678     }
679     osi_FreeLargeSpace(tbuffer);
680     afs_PutCell(tcell, READ_LOCK);
681     return tv;
682
683 } /*afs_NewVolumeByName*/
684
685
686
687 /* call this with the volume structure locked; used for new-style vldb requests */
688 void InstallVolumeEntry(struct volume *av, struct vldbentry *ve, int acell)
689 {
690     register struct server *ts;
691     struct cell *cellp;
692     register int i, j;
693     afs_int32 mask;
694     afs_uint32 temp;
695
696     AFS_STATCNT(InstallVolumeEntry);
697
698     /* Determine the type of volume we want */
699     if ((ve->flags & VLF_RWEXISTS) && (av->volume == ve->volumeId[RWVOL])) {
700        mask = VLSF_RWVOL;
701     }
702     else if ((ve->flags & VLF_ROEXISTS) && (av->volume == ve->volumeId[ROVOL])) {
703        mask = VLSF_ROVOL;
704        av->states |= VRO;
705     }
706     else if ((ve->flags & VLF_BACKEXISTS) && (av->volume == ve->volumeId[BACKVOL])) {
707        /* backup always is on the same volume as parent */
708        mask = VLSF_RWVOL;
709        av->states |= (VRO|VBackup);
710     }
711     else {
712        mask = 0;  /* Can't find volume in vldb entry */
713     }
714
715     /* fill in volume types */
716     av->rwVol   = ((ve->flags & VLF_RWEXISTS)   ? ve->volumeId[RWVOL]   : 0);
717     av->roVol   = ((ve->flags & VLF_ROEXISTS)   ? ve->volumeId[ROVOL]   : 0);
718     av->backVol = ((ve->flags & VLF_BACKEXISTS) ? ve->volumeId[BACKVOL] : 0);
719
720     if (ve->flags & VLF_DFSFILESET)
721         av->states |= VForeign;
722
723     cellp = afs_GetCell(acell, 0);
724
725     /* This volume, av, is locked. Zero out the serverHosts[] array 
726      * so that if afs_GetServer() decides to replace the server 
727      * struct, we don't deadlock trying to afs_ResetVolumeInfo()
728      * this volume.
729      */
730     for (j=0; j<MAXHOSTS; j++) {
731        av->serverHost[j] = 0;
732     }
733
734     /* Step through the VLDB entry making sure each server listed is there */
735     for (i=0,j=0; i<ve->nServers; i++) {
736         if ( ((ve->serverFlags[i] & mask) == 0) || (ve->serverFlags[i] & VLSF_DONTUSE) ) {
737            continue;      /* wrong volume or  don't use this volume */
738         }
739
740         temp = htonl(ve->serverNumber[i]);
741         ts = afs_GetServer(&temp, 1, acell, cellp->fsport, WRITE_LOCK, (afsUUID *)0, 0);
742         av->serverHost[j] = ts;
743
744         /*
745          * The cell field could be 0 if the server entry was created
746          * first with the 'fs setserverprefs' call which doesn't set
747          * the cell field. Thus if the afs_GetServer call above
748          * follows later on it will find the server entry thus it will
749          * simply return without setting any fields, so we set the
750          * field ourselves below.
751          */
752         if (!ts->cell)
753             ts->cell = cellp;
754         afs_PutServer(ts, WRITE_LOCK);
755         j++;
756     }
757     if (j < MAXHOSTS) {
758         av->serverHost[j++] = 0;
759     }
760     afs_SortServers(av->serverHost, MAXHOSTS);
761 } /*InstallVolumeEntry*/
762
763
764 void InstallNVolumeEntry(struct volume *av, struct nvldbentry *ve, int acell)
765 {
766     register struct server *ts;
767     struct cell *cellp;
768     register int i, j;
769     afs_int32 mask;
770     afs_uint32 temp;
771
772     AFS_STATCNT(InstallVolumeEntry);
773
774     /* Determine type of volume we want */
775     if ((ve->flags & VLF_RWEXISTS) && (av->volume == ve->volumeId[RWVOL])) {
776        mask = VLSF_RWVOL;
777     }
778     else if ((ve->flags & VLF_ROEXISTS) && (av->volume == ve->volumeId[ROVOL])) {
779        mask = VLSF_ROVOL;
780        av->states |= VRO;
781     }
782     else if ((ve->flags&VLF_BACKEXISTS) && (av->volume == ve->volumeId[BACKVOL])) {
783        /* backup always is on the same volume as parent */
784        mask = VLSF_RWVOL;
785        av->states |= (VRO|VBackup);
786     }
787     else {
788        mask = 0;     /* Can't find volume in vldb entry */
789     }
790
791     /* fill in volume types */
792     av->rwVol   = ((ve->flags & VLF_RWEXISTS)   ? ve->volumeId[RWVOL]   : 0);
793     av->roVol   = ((ve->flags & VLF_ROEXISTS)   ? ve->volumeId[ROVOL]   : 0);
794     av->backVol = ((ve->flags & VLF_BACKEXISTS) ? ve->volumeId[BACKVOL] : 0);
795
796     if (ve->flags & VLF_DFSFILESET)
797         av->states |= VForeign;
798
799     cellp = afs_GetCell(acell, 0);
800
801     /* This volume, av, is locked. Zero out the serverHosts[] array 
802      * so that if afs_GetServer() decides to replace the server 
803      * struct, we don't deadlock trying to afs_ResetVolumeInfo()
804      * this volume.
805      */
806     for (j=0; j<MAXHOSTS; j++) {
807        av->serverHost[j] = 0;
808     }
809
810     /* Step through the VLDB entry making sure each server listed is there */
811     for (i=0,j=0; i<ve->nServers; i++) {
812         if ( ((ve->serverFlags[i] & mask) == 0) || (ve->serverFlags[i] & VLSF_DONTUSE) ) {
813            continue;      /* wrong volume or don't use this volume */
814         }
815
816         temp = htonl(ve->serverNumber[i]);
817         ts = afs_GetServer(&temp, 1, acell, cellp->fsport, WRITE_LOCK, (afsUUID *)0,0);
818         av->serverHost[j] = ts;
819         /*
820          * The cell field could be 0 if the server entry was created
821          * first with the 'fs setserverprefs' call which doesn't set
822          * the cell field. Thus if the afs_GetServer call above
823          * follows later on it will find the server entry thus it will
824          * simply return without setting any fields, so we set the
825          * field ourselves below.
826          */
827         if (!ts->cell)
828             ts->cell = cellp;
829         afs_PutServer(ts, WRITE_LOCK);
830         j++;
831     }
832     if (j < MAXHOSTS) {
833         av->serverHost[j++] = 0;
834     }
835     afs_SortServers(av->serverHost, MAXHOSTS);
836 } /*InstallNVolumeEntry*/
837
838
839 void InstallUVolumeEntry(struct volume *av, struct uvldbentry *ve,
840                          int acell, struct cell *tcell, struct vrequest *areq)
841 {
842     register struct server *ts;
843     struct conn *tconn;
844     struct cell *cellp;
845     register int i, j;
846     afs_uint32 serverid;
847     afs_int32 mask;
848     int k;
849
850     AFS_STATCNT(InstallVolumeEntry);
851
852     /* Determine type of volume we want */
853     if ((ve->flags & VLF_RWEXISTS) && (av->volume == ve->volumeId[RWVOL])) {
854        mask = VLSF_RWVOL;
855     }
856     else if ((ve->flags & VLF_ROEXISTS) && av->volume == ve->volumeId[ROVOL]) {
857        mask = VLSF_ROVOL;
858        av->states |= VRO;
859     }
860     else if ((ve->flags & VLF_BACKEXISTS) && (av->volume == ve->volumeId[BACKVOL])) {
861        /* backup always is on the same volume as parent */
862        mask = VLSF_RWVOL;
863        av->states |= (VRO|VBackup);
864     }
865     else {
866        mask = 0;      /* Can't find volume in vldb entry */
867     }
868
869     /* fill in volume types */
870     av->rwVol   = ((ve->flags & VLF_RWEXISTS)   ? ve->volumeId[RWVOL]   : 0);
871     av->roVol   = ((ve->flags & VLF_ROEXISTS)   ? ve->volumeId[ROVOL]   : 0);
872     av->backVol = ((ve->flags & VLF_BACKEXISTS) ? ve->volumeId[BACKVOL] : 0);
873
874     if (ve->flags & VLF_DFSFILESET)
875         av->states |= VForeign;
876
877     cellp = afs_GetCell(acell, 0);
878
879     /* This volume, av, is locked. Zero out the serverHosts[] array 
880      * so that if afs_GetServer() decides to replace the server 
881      * struct, we don't deadlock trying to afs_ResetVolumeInfo()
882      * this volume.
883      */
884     for (j=0; j<MAXHOSTS; j++) {
885        av->serverHost[j] = 0;
886     }
887
888     /* Gather the list of servers the VLDB says the volume is on
889      * and initialize the ve->serverHost[] array. If a server struct
890      * is not found, then get the list of addresses for the
891      * server, VL_GetAddrsU(), and create a server struct, afs_GetServer().
892      */
893     for (i=0,j=0; i<ve->nServers; i++) {
894         if ( ((ve->serverFlags[i] & mask) == 0) || (ve->serverFlags[i] & VLSF_DONTUSE) ) {
895            continue;       /* wrong volume don't use this volume */
896         }
897
898         if (!(ve->serverFlags[i] & VLSERVER_FLAG_UUID)) {
899             /* The server has no uuid */
900             serverid = htonl(ve->serverNumber[i].time_low);
901             ts = afs_GetServer(&serverid, 1, acell, cellp->fsport, WRITE_LOCK, (afsUUID *)0,0);
902         } else {
903             ts = afs_FindServer(0, cellp->fsport, &ve->serverNumber[i], 0);
904             if (ts && (ts->sr_addr_uniquifier == ve->serverUnique[i]) && ts->addr) {
905                /* uuid, uniquifier, and portal are the same */
906             } else {
907                 afs_uint32 *addrp, nentries, code, unique;
908                 bulkaddrs addrs;
909                 ListAddrByAttributes attrs;
910                 afsUUID uuid;
911
912                 memset((char *)&attrs, 0, sizeof(attrs));
913                 attrs.Mask = VLADDR_UUID;
914                 attrs.uuid = ve->serverNumber[i];
915                 memset((char *)&uuid, 0, sizeof(uuid));
916                 memset((char *)&addrs, 0, sizeof(addrs));
917                 do {
918                     tconn = afs_ConnByMHosts(tcell->cellHosts, tcell->vlport,
919                                              tcell->cellNum, areq, SHARED_LOCK);
920                     if (tconn) {
921                         RX_AFS_GUNLOCK();
922                         code = VL_GetAddrsU(tconn->id, &attrs, &uuid, &unique, &nentries, &addrs);
923                         RX_AFS_GLOCK();
924                     } else {
925                         code = -1;
926                     }
927
928                     /* Handle corrupt VLDB (defect 7393) */
929                     if (code == 0 && nentries == 0)
930                         code = VL_NOENT;
931
932                 } while (afs_Analyze(tconn, code, NULL, areq,
933                                      -1, SHARED_LOCK, tcell));
934                 if (code) {
935                     /* Better handing of such failures; for now we'll simply retry this call */
936                     areq->volumeError = 1;
937                     return;
938                 }
939
940                 addrp = addrs.bulkaddrs_val;
941                 for (k = 0; k < nentries; k++) {
942                    addrp[k] = htonl(addrp[k]);
943                 }
944                 ts = afs_GetServer(addrp, nentries, acell, cellp->fsport,
945                                    WRITE_LOCK, &ve->serverNumber[i],
946                                    ve->serverUnique[i]);
947                 afs_osi_Free(addrs.bulkaddrs_val, addrs.bulkaddrs_len*sizeof(*addrp));
948             }
949         }
950         av->serverHost[j] = ts;
951
952         /* The cell field could be 0 if the server entry was created
953          * first with the 'fs setserverprefs' call which doesn't set
954          * the cell field. Thus if the afs_GetServer call above
955          * follows later on it will find the server entry thus it will
956          * simply return without setting any fields, so we set the
957          * field ourselves below.
958          */
959         if (!ts->cell)
960             ts->cell = cellp;
961         afs_PutServer(ts, WRITE_LOCK);
962         j++;
963     }
964
965     afs_SortServers(av->serverHost, MAXHOSTS);
966 } /*InstallVolumeEntry*/
967
968
969 void afs_ResetVolumeInfo(struct volume *tv)
970 {
971     int i;
972
973     AFS_STATCNT(afs_ResetVolumeInfo);
974     ObtainWriteLock(&tv->lock,117);
975     tv->states |= VRecheck;
976     for (i=0; i<MAXHOSTS; i++)
977       tv->status[i] = not_busy;
978     if (tv->name) {
979         afs_osi_Free(tv->name, strlen(tv->name)+1);
980         tv->name = NULL;
981       }
982     ReleaseWriteLock(&tv->lock);
983 }