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