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