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