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