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