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