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