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