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