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