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