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