Unix CM: Add osi_crypto crypto interface
[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
743     if (strlen(aname) > VL_MAXNAMELEN)  /* Invalid volume name */
744         return NULL;
745
746     tcell = afs_GetCell(acell, READ_LOCK);
747     if (!tcell) {
748         return NULL;
749     }
750
751     /* allow null request if we don't care about ENODEV/ETIMEDOUT distinction */
752     if (!areq)
753         areq = &treq;
754
755
756     afs_Trace2(afs_iclSetp, CM_TRACE_GETVOL, ICL_TYPE_STRING, aname,
757                ICL_TYPE_POINTER, aname);
758     tbuffer = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
759     tve = (struct vldbentry *)(tbuffer + 1024);
760     ntve = (struct nvldbentry *)tve;
761     utve = (struct uvldbentry *)tve;
762     afs_InitReq(&treq, afs_osi_credp);  /* *must* be unauth for vldb */
763     do {
764         tconn =
765             afs_ConnByMHosts(tcell->cellHosts, tcell->vlport, tcell->cellNum,
766                              &treq, SHARED_LOCK);
767         if (tconn) {
768             if (tconn->parent->srvr->server->flags & SNO_LHOSTS) {
769                 type = 0;
770                 RX_AFS_GUNLOCK();
771                 code = VL_GetEntryByNameO(tconn->id, aname, tve);
772                 RX_AFS_GLOCK();
773             } else if (tconn->parent->srvr->server->flags & SYES_LHOSTS) {
774                 type = 1;
775                 RX_AFS_GUNLOCK();
776                 code = VL_GetEntryByNameN(tconn->id, aname, ntve);
777                 RX_AFS_GLOCK();
778             } else {
779                 type = 2;
780                 RX_AFS_GUNLOCK();
781                 code = VL_GetEntryByNameU(tconn->id, aname, utve);
782                 RX_AFS_GLOCK();
783                 if (!(tconn->parent->srvr->server->flags & SVLSRV_UUID)) {
784                     if (code == RXGEN_OPCODE) {
785                         type = 1;
786                         RX_AFS_GUNLOCK();
787                         code = VL_GetEntryByNameN(tconn->id, aname, ntve);
788                         RX_AFS_GLOCK();
789                         if (code == RXGEN_OPCODE) {
790                             type = 0;
791                             tconn->parent->srvr->server->flags |= SNO_LHOSTS;
792                             RX_AFS_GUNLOCK();
793                             code = VL_GetEntryByNameO(tconn->id, aname, tve);
794                             RX_AFS_GLOCK();
795                         } else if (!code)
796                             tconn->parent->srvr->server->flags |= SYES_LHOSTS;
797                     } else if (!code)
798                         tconn->parent->srvr->server->flags |= SVLSRV_UUID;
799                 }
800                 lastnvcode = code;
801             }
802         } else
803             code = -1;
804     } while (afs_Analyze(tconn, code, NULL, &treq, -1,  /* no op code for this */
805                          SHARED_LOCK, tcell));
806
807     if (code) {
808         /* If the client has yet to contact this cell and contact failed due
809          * to network errors, mark the VLDB servers as back up.
810          * That the client tried and failed can be determined from the
811          * fact that there was a downtime incident, but CHasVolRef is not set.
812          */
813     /* RT 48959 - unclear if this should really go */
814 #if 0
815         if (areq->networkError && !(tcell->states & CHasVolRef)) {
816             int i;
817             struct server *sp;
818             struct srvAddr *sap;
819             for (i = 0; i < AFS_MAXCELLHOSTS; i++) {
820                 if ((sp = tcell->cellHosts[i]) == NULL)
821                     break;
822                 for (sap = sp->addr; sap; sap = sap->next_sa)
823                     afs_MarkServerUpOrDown(sap, 0);
824             }
825         }
826 #endif
827         afs_CopyError(&treq, areq);
828         osi_FreeLargeSpace(tbuffer);
829         afs_PutCell(tcell, READ_LOCK);
830         return NULL;
831     }
832     /*
833      * Check to see if this cell has not yet referenced a volume.  If
834      * it hasn't, it's just about to change its status, and we need to mark
835      * this fact down. Note that it is remotely possible that afs_SetupVolume
836      * could fail and we would still not have a volume reference.
837      */
838     if (!(tcell->states & CHasVolRef)) {
839         tcell->states |= CHasVolRef;
840         afs_stats_cmperf.numCellsContacted++;
841     }
842     /*First time a volume in this cell has been referenced */
843     if (type == 2)
844         ve = (char *)utve;
845     else if (type == 1)
846         ve = (char *)ntve;
847     else
848         ve = (char *)tve;
849     tv = afs_SetupVolume(0, aname, ve, tcell, agood, type, areq);
850     if ((agood == 3) && tv && tv->backVol) {
851         /*
852          * This means that very soon we'll ask for the BK volume so
853          * we'll prefetch it (well we did already.)
854          */
855         tv1 =
856             afs_SetupVolume(tv->backVol, (char *)0, ve, tcell, 0, type, areq);
857         if (tv1) {
858             tv1->refCount--;
859         }
860     }
861     if ((agood >= 2) && tv && tv->roVol) {
862         /*
863          * This means that very soon we'll ask for the RO volume so
864          * we'll prefetch it (well we did already.)
865          */
866         tv1 = afs_SetupVolume(tv->roVol, NULL, ve, tcell, 0, type, areq);
867         if (tv1) {
868             tv1->refCount--;
869         }
870     }
871     osi_FreeLargeSpace(tbuffer);
872     afs_PutCell(tcell, READ_LOCK);
873     return tv;
874
875 }                               /*afs_NewVolumeByName */
876
877
878
879 /**
880  *   Call this with the volume structure locked; used for new-style vldb requests.
881  * @param av Volume
882  * @param ve
883  * @param acell
884  */
885 void
886 InstallVolumeEntry(struct volume *av, struct vldbentry *ve, int acell)
887 {
888     struct server *ts;
889     struct cell *cellp;
890     int i, j;
891     afs_int32 mask;
892     afs_uint32 temp;
893
894     AFS_STATCNT(InstallVolumeEntry);
895
896     /* Determine the type of volume we want */
897     if ((ve->flags & VLF_RWEXISTS) && (av->volume == ve->volumeId[RWVOL])) {
898         mask = VLSF_RWVOL;
899     } else if ((ve->flags & VLF_ROEXISTS)
900                && (av->volume == ve->volumeId[ROVOL])) {
901         mask = VLSF_ROVOL;
902         av->states |= VRO;
903     } else if ((ve->flags & VLF_BACKEXISTS)
904                && (av->volume == ve->volumeId[BACKVOL])) {
905         /* backup always is on the same volume as parent */
906         mask = VLSF_RWVOL;
907         av->states |= (VRO | VBackup);
908     } else {
909         mask = 0;               /* Can't find volume in vldb entry */
910     }
911
912     /* fill in volume types */
913     av->rwVol = ((ve->flags & VLF_RWEXISTS) ? ve->volumeId[RWVOL] : 0);
914     av->roVol = ((ve->flags & VLF_ROEXISTS) ? ve->volumeId[ROVOL] : 0);
915     av->backVol = ((ve->flags & VLF_BACKEXISTS) ? ve->volumeId[BACKVOL] : 0);
916
917     if (ve->flags & VLF_DFSFILESET)
918         av->states |= VForeign;
919
920     cellp = afs_GetCell(acell, 0);
921
922     /* This volume, av, is locked. Zero out the serverHosts[] array
923      * so that if afs_GetServer() decides to replace the server
924      * struct, we don't deadlock trying to afs_ResetVolumeInfo()
925      * this volume.
926      */
927     for (j = 0; j < AFS_MAXHOSTS; j++) {
928         av->serverHost[j] = 0;
929     }
930
931     /* Step through the VLDB entry making sure each server listed is there */
932     for (i = 0, j = 0; i < ve->nServers; i++) {
933         if (((ve->serverFlags[i] & mask) == 0)
934             || (ve->serverFlags[i] & VLSF_DONTUSE)) {
935             continue;           /* wrong volume or  don't use this volume */
936         }
937
938         temp = htonl(ve->serverNumber[i]);
939         ts = afs_GetServer(&temp, 1, acell, cellp->fsport, WRITE_LOCK,
940                            (afsUUID *) 0, 0);
941         av->serverHost[j] = ts;
942
943         /*
944          * The cell field could be 0 if the server entry was created
945          * first with the 'fs setserverprefs' call which doesn't set
946          * the cell field. Thus if the afs_GetServer call above
947          * follows later on it will find the server entry thus it will
948          * simply return without setting any fields, so we set the
949          * field ourselves below.
950          */
951         if (!ts->cell)
952             ts->cell = cellp;
953         afs_PutServer(ts, WRITE_LOCK);
954         j++;
955     }
956     if (j < AFS_MAXHOSTS) {
957         av->serverHost[j++] = 0;
958     }
959     afs_SortServers(av->serverHost, AFS_MAXHOSTS);
960 }                               /*InstallVolumeEntry */
961
962
963 void
964 InstallNVolumeEntry(struct volume *av, struct nvldbentry *ve, int acell)
965 {
966     struct server *ts;
967     struct cell *cellp;
968     int i, j;
969     afs_int32 mask;
970     afs_uint32 temp;
971
972     AFS_STATCNT(InstallVolumeEntry);
973
974     /* Determine type of volume we want */
975     if ((ve->flags & VLF_RWEXISTS) && (av->volume == ve->volumeId[RWVOL])) {
976         mask = VLSF_RWVOL;
977     } else if ((ve->flags & VLF_ROEXISTS)
978                && (av->volume == ve->volumeId[ROVOL])) {
979         mask = VLSF_ROVOL;
980         av->states |= VRO;
981     } else if ((ve->flags & VLF_BACKEXISTS)
982                && (av->volume == ve->volumeId[BACKVOL])) {
983         /* backup always is on the same volume as parent */
984         mask = VLSF_RWVOL;
985         av->states |= (VRO | VBackup);
986     } else {
987         mask = 0;               /* Can't find volume in vldb entry */
988     }
989
990     /* fill in volume types */
991     av->rwVol = ((ve->flags & VLF_RWEXISTS) ? ve->volumeId[RWVOL] : 0);
992     av->roVol = ((ve->flags & VLF_ROEXISTS) ? ve->volumeId[ROVOL] : 0);
993     av->backVol = ((ve->flags & VLF_BACKEXISTS) ? ve->volumeId[BACKVOL] : 0);
994
995     if (ve->flags & VLF_DFSFILESET)
996         av->states |= VForeign;
997
998     cellp = afs_GetCell(acell, 0);
999
1000     /* This volume, av, is locked. Zero out the serverHosts[] array
1001      * so that if afs_GetServer() decides to replace the server
1002      * struct, we don't deadlock trying to afs_ResetVolumeInfo()
1003      * this volume.
1004      */
1005     for (j = 0; j < AFS_MAXHOSTS; j++) {
1006         av->serverHost[j] = 0;
1007     }
1008
1009     /* Step through the VLDB entry making sure each server listed is there */
1010     for (i = 0, j = 0; i < ve->nServers; i++) {
1011         if (((ve->serverFlags[i] & mask) == 0)
1012             || (ve->serverFlags[i] & VLSF_DONTUSE)) {
1013             continue;           /* wrong volume or don't use this volume */
1014         }
1015
1016         temp = htonl(ve->serverNumber[i]);
1017         ts = afs_GetServer(&temp, 1, acell, cellp->fsport, WRITE_LOCK,
1018                            (afsUUID *) 0, 0);
1019         av->serverHost[j] = ts;
1020         /*
1021          * The cell field could be 0 if the server entry was created
1022          * first with the 'fs setserverprefs' call which doesn't set
1023          * the cell field. Thus if the afs_GetServer call above
1024          * follows later on it will find the server entry thus it will
1025          * simply return without setting any fields, so we set the
1026          * field ourselves below.
1027          */
1028         if (!ts->cell)
1029             ts->cell = cellp;
1030         afs_PutServer(ts, WRITE_LOCK);
1031         j++;
1032     }
1033     if (j < AFS_MAXHOSTS) {
1034         av->serverHost[j++] = 0;
1035     }
1036     afs_SortServers(av->serverHost, AFS_MAXHOSTS);
1037 }                               /*InstallNVolumeEntry */
1038
1039
1040 void
1041 InstallUVolumeEntry(struct volume *av, struct uvldbentry *ve, int acell,
1042                     struct cell *tcell, struct vrequest *areq)
1043 {
1044     struct server *ts;
1045     struct afs_conn *tconn;
1046     struct cell *cellp;
1047     int i, j;
1048     afs_uint32 serverid;
1049     afs_int32 mask;
1050     int k;
1051
1052     AFS_STATCNT(InstallVolumeEntry);
1053
1054     /* Determine type of volume we want */
1055     if ((ve->flags & VLF_RWEXISTS) && (av->volume == ve->volumeId[RWVOL])) {
1056         mask = VLSF_RWVOL;
1057     } else if ((ve->flags & VLF_ROEXISTS)
1058                && av->volume == ve->volumeId[ROVOL]) {
1059         mask = VLSF_ROVOL;
1060         av->states |= VRO;
1061     } else if ((ve->flags & VLF_BACKEXISTS)
1062                && (av->volume == ve->volumeId[BACKVOL])) {
1063         /* backup always is on the same volume as parent */
1064         mask = VLSF_RWVOL;
1065         av->states |= (VRO | VBackup);
1066     } else {
1067         mask = 0;               /* Can't find volume in vldb entry */
1068     }
1069
1070     /* fill in volume types */
1071     av->rwVol = ((ve->flags & VLF_RWEXISTS) ? ve->volumeId[RWVOL] : 0);
1072     av->roVol = ((ve->flags & VLF_ROEXISTS) ? ve->volumeId[ROVOL] : 0);
1073     av->backVol = ((ve->flags & VLF_BACKEXISTS) ? ve->volumeId[BACKVOL] : 0);
1074
1075     if (ve->flags & VLF_DFSFILESET)
1076         av->states |= VForeign;
1077
1078     cellp = afs_GetCell(acell, 0);
1079
1080     /* This volume, av, is locked. Zero out the serverHosts[] array
1081      * so that if afs_GetServer() decides to replace the server
1082      * struct, we don't deadlock trying to afs_ResetVolumeInfo()
1083      * this volume.
1084      */
1085     for (j = 0; j < AFS_MAXHOSTS; j++) {
1086         av->serverHost[j] = 0;
1087     }
1088
1089     /* Gather the list of servers the VLDB says the volume is on
1090      * and initialize the ve->serverHost[] array. If a server struct
1091      * is not found, then get the list of addresses for the
1092      * server, VL_GetAddrsU(), and create a server struct, afs_GetServer().
1093      */
1094     for (i = 0, j = 0; i < ve->nServers; i++) {
1095         if (((ve->serverFlags[i] & mask) == 0)
1096             || (ve->serverFlags[i] & VLSF_DONTUSE)) {
1097             continue;           /* wrong volume don't use this volume */
1098         }
1099
1100         if (!(ve->serverFlags[i] & VLSERVER_FLAG_UUID)) {
1101             /* The server has no uuid */
1102             serverid = htonl(ve->serverNumber[i].time_low);
1103             ts = afs_GetServer(&serverid, 1, acell, cellp->fsport, WRITE_LOCK,
1104                                (afsUUID *) 0, 0);
1105         } else {
1106             ts = afs_FindServer(0, cellp->fsport, &ve->serverNumber[i], 0);
1107             if (ts && (ts->sr_addr_uniquifier == ve->serverUnique[i])
1108                 && ts->addr) {
1109                 /* uuid, uniquifier, and portal are the same */
1110             } else {
1111                 afs_uint32 *addrp, code;
1112                 afs_int32 nentries, unique;
1113                 bulkaddrs addrs;
1114                 ListAddrByAttributes attrs;
1115                 afsUUID uuid;
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                     if (tconn) {
1127                         RX_AFS_GUNLOCK();
1128                         code =
1129                             VL_GetAddrsU(tconn->id, &attrs, &uuid, &unique,
1130                                          &nentries, &addrs);
1131                         RX_AFS_GLOCK();
1132                     } else {
1133                         code = -1;
1134                     }
1135
1136                     /* Handle corrupt VLDB (defect 7393) */
1137                     if (code == 0 && nentries == 0)
1138                         code = VL_NOENT;
1139
1140                 } while (afs_Analyze
1141                          (tconn, code, NULL, areq, -1, SHARED_LOCK, tcell));
1142                 if (code) {
1143                     /* Better handing of such failures; for now we'll simply retry this call */
1144                     areq->volumeError = 1;
1145                     return;
1146                 }
1147
1148                 addrp = addrs.bulkaddrs_val;
1149                 for (k = 0; k < nentries; k++) {
1150                     addrp[k] = htonl(addrp[k]);
1151                 }
1152                 ts = afs_GetServer(addrp, nentries, acell, cellp->fsport,
1153                                    WRITE_LOCK, &ve->serverNumber[i],
1154                                    ve->serverUnique[i]);
1155                 xdr_free((xdrproc_t) xdr_bulkaddrs, &addrs);
1156             }
1157         }
1158         av->serverHost[j] = ts;
1159
1160         /* The cell field could be 0 if the server entry was created
1161          * first with the 'fs setserverprefs' call which doesn't set
1162          * the cell field. Thus if the afs_GetServer call above
1163          * follows later on it will find the server entry thus it will
1164          * simply return without setting any fields, so we set the
1165          * field ourselves below.
1166          */
1167         if (!ts->cell)
1168             ts->cell = cellp;
1169         afs_PutServer(ts, WRITE_LOCK);
1170         j++;
1171     }
1172
1173     afs_SortServers(av->serverHost, AFS_MAXHOSTS);
1174 }                               /*InstallVolumeEntry */
1175
1176
1177 /**
1178  *   Reset volume info for the specified volume strecture. Mark volume
1179  * to be rechecked next time.
1180  * @param tv
1181  */
1182 void
1183 afs_ResetVolumeInfo(struct volume *tv)
1184 {
1185     int i;
1186
1187     AFS_STATCNT(afs_ResetVolumeInfo);
1188     ObtainWriteLock(&tv->lock, 117);
1189     tv->states |= VRecheck;
1190     for (i = 0; i < AFS_MAXHOSTS; i++)
1191         tv->status[i] = not_busy;
1192     if (tv->name) {
1193         afs_osi_Free(tv->name, strlen(tv->name) + 1);
1194         tv->name = NULL;
1195     }
1196     ReleaseWriteLock(&tv->lock);
1197 }