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