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