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