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