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