linux-warning-reduction-20090318
[openafs.git] / src / afs / afs_dynroot.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  * Dynamic /afs volume support.
12  *
13  * Implements:
14  * afs_IsDynrootFid
15  * afs_IsDynrootMountFid
16  * afs_IsDynrootAnyFid
17  * afs_GetDynrootFid
18  * afs_GetDynrootMountFid
19  * afs_IsDynroot
20  * afs_IsDynrootMount
21  * afs_IsDynrootAny
22  * afs_DynrootInvalidate
23  * afs_GetDynroot
24  * afs_PutDynroot
25  * afs_DynrootNewVnode
26  * afs_SetDynrootEnable
27  * afs_GetDynrootEnable
28  * afs_DynrootVOPRemove
29  * afs_DynrootVOPSymlink
30  *
31  */
32
33 #include <afsconfig.h>
34 #include "afs/param.h"
35
36 #include "afs/stds.h"
37 #include "afs/sysincludes.h"    /* Standard vendor system headers */
38 #include "afsincludes.h"
39 #include "afs/afs_osi.h"
40 #include "afsint.h"
41 #include "afs/lock.h"
42
43 #include "afs/prs_fs.h"
44 #include "afs/dir.h"
45 #include "afs/afs_dynroot.h"
46
47 #define AFS_DYNROOT_CELLNAME    "dynroot"
48 #define AFS_DYNROOT_VOLUME      1
49 #define AFS_DYNROOT_VNODE       1
50 #define AFS_DYNROOT_MOUNT_VNODE 3
51 #define AFS_DYNROOT_UNIQUE      1
52
53 static int afs_dynrootInit = 0;
54 static int afs_dynrootEnable = 0;
55 static int afs_dynrootCell = 0;
56
57 static afs_rwlock_t afs_dynrootDirLock;
58 /* Start of variables protected by afs_dynrootDirLock */
59 static char *afs_dynrootDir = NULL;
60 static int afs_dynrootDirLen;
61 static char *afs_dynrootMountDir = NULL;
62 static int afs_dynrootMountDirLen;
63 static int afs_dynrootDirLinkcnt;
64 static int afs_dynrootDirVersion;
65 static int afs_dynrootVersion = 1;
66 static int afs_dynrootVersionHigh = 1;
67 /* End of variables protected by afs_dynrootDirLock */
68
69 /* A dynamically-created symlink in a dynroot /afs */
70 struct afs_dynSymlink {
71     struct afs_dynSymlink *next;
72     int index;
73     char *name;
74     char *target;
75 };
76
77 static afs_rwlock_t afs_dynSymlinkLock;
78 /* Start of variables protected by afs_dynSymlinkLock */
79 static struct afs_dynSymlink *afs_dynSymlinkBase = NULL;
80 static int afs_dynSymlinkIndex = 0;
81 /* End of variables protected by afs_dynSymlinkLock */
82
83 /*
84  * Set up a cell for dynroot if it's not there yet.
85  */
86 static int
87 afs_dynrootCellInit(void)
88 {
89     if (!afs_dynrootCell) {
90         afs_int32 cellHosts[MAXCELLHOSTS];
91         struct cell *tc;
92         int code;
93
94         memset(cellHosts, 0, sizeof(cellHosts));
95         code =
96             afs_NewCell(AFS_DYNROOT_CELLNAME, cellHosts, CNoSUID | CNoAFSDB,
97                         NULL, 0, 0, 0);
98         if (code)
99             return code;
100         tc = afs_GetCellByName(AFS_DYNROOT_CELLNAME, READ_LOCK);
101         if (!tc)
102             return ENODEV;
103         afs_dynrootCell = tc->cellNum;
104         afs_PutCell(tc, READ_LOCK);
105     }
106
107     return 0;
108 }
109
110 /*
111  * Returns non-zero iff fid corresponds to the top of the dynroot volume.
112  */
113 static int
114 _afs_IsDynrootFid(struct VenusFid *fid)
115 {
116     return (fid->Cell == afs_dynrootCell
117             && fid->Fid.Volume == AFS_DYNROOT_VOLUME
118             && fid->Fid.Vnode == AFS_DYNROOT_VNODE
119             && fid->Fid.Unique == AFS_DYNROOT_UNIQUE);
120 }
121
122 int
123 afs_IsDynrootFid(struct VenusFid *fid)
124 {
125     return (afs_dynrootEnable && _afs_IsDynrootFid(fid));
126 }
127
128 int
129 afs_IsDynrootMountFid(struct VenusFid *fid)
130 {
131     return (fid->Cell == afs_dynrootCell
132             && fid->Fid.Volume == AFS_DYNROOT_VOLUME
133             && fid->Fid.Vnode == AFS_DYNROOT_MOUNT_VNODE
134             && fid->Fid.Unique == AFS_DYNROOT_UNIQUE);
135 }
136
137 int
138 afs_IsDynrootAnyFid(struct VenusFid *fid)
139 {
140     return (fid->Cell == afs_dynrootCell
141             && fid->Fid.Volume == AFS_DYNROOT_VOLUME);
142 }
143
144 /*
145  * Obtain the magic dynroot volume Fid.
146  */
147 void
148 afs_GetDynrootFid(struct VenusFid *fid)
149 {
150     fid->Cell = afs_dynrootCell;
151     fid->Fid.Volume = AFS_DYNROOT_VOLUME;
152     fid->Fid.Vnode = AFS_DYNROOT_VNODE;
153     fid->Fid.Unique = AFS_DYNROOT_UNIQUE;
154 }
155
156 void
157 afs_GetDynrootMountFid(struct VenusFid *fid)
158 {
159     fid->Cell = afs_dynrootCell;
160     fid->Fid.Volume = AFS_DYNROOT_VOLUME;
161     fid->Fid.Vnode = AFS_DYNROOT_MOUNT_VNODE;
162     fid->Fid.Unique = AFS_DYNROOT_UNIQUE;
163 }
164
165 /*
166  * Returns non-zero iff avc is a pointer to the dynroot /afs vnode.
167  */
168 int
169 afs_IsDynroot(struct vcache *avc)
170 {
171     return afs_IsDynrootFid(&avc->f.fid);
172 }
173
174 int
175 afs_IsDynrootMount(struct vcache *avc)
176 {
177     return afs_IsDynrootMountFid(&avc->f.fid);
178 }
179
180 int
181 afs_IsDynrootAny(struct vcache *avc)
182 {
183     return afs_IsDynrootAnyFid(&avc->f.fid);
184 }
185
186 /*
187  * Given the current page and chunk pointers in a directory, adjust them
188  * appropriately so that the given file name can be appended.  Used for
189  * computing the size of a directory.
190  */
191 static void
192 afs_dynroot_computeDirEnt(char *name, int *curPageP, int *curChunkP)
193 {
194     int esize;
195
196     esize = afs_dir_NameBlobs(name);
197     if (*curChunkP + esize > EPP) {
198         *curPageP += 1;
199         *curChunkP = 1;
200     }
201     *curChunkP += esize;
202 }
203
204 /*
205  * Add directory entry by given name to a directory.  Assumes the
206  * caller has allocated the directory to be large enough to hold
207  * the necessary entry.
208  */
209 static void
210 afs_dynroot_addDirEnt(struct DirHeader *dirHeader, int *curPageP,
211                       int *curChunkP, char *name, int vnode)
212 {
213     char *dirBase = (char *)dirHeader;
214     struct PageHeader *pageHeader;
215     struct DirEntry *dirEntry;
216     int sizeOfEntry, i, t1, t2;
217     int curPage = *curPageP;
218     int curChunk = *curChunkP;
219     int didNewPage = 0;
220
221     /*
222      * Check if we need to flip pages..  If so, init the new page.
223      */
224     sizeOfEntry = afs_dir_NameBlobs(name);
225     if (curChunk + sizeOfEntry > EPP) {
226         curPage++;
227         curChunk = 1;
228         didNewPage = 1;
229     }
230
231     pageHeader = (struct PageHeader *)(dirBase + curPage * AFS_PAGESIZE);
232     if (didNewPage) {
233         pageHeader->pgcount = 0;
234         pageHeader->tag = htons(1234);
235         pageHeader->freecount = 0;
236         pageHeader->freebitmap[0] = 0x01;
237         for (i = 1; i < EPP / 8; i++)
238             pageHeader->freebitmap[i] = 0;
239
240         dirHeader->alloMap[curPage] = EPP - 1;
241     }
242
243     dirEntry = (struct DirEntry *)(pageHeader + curChunk);
244     dirEntry->flag = 1;
245     dirEntry->length = 0;
246     dirEntry->next = 0;
247     dirEntry->fid.vnode = htonl(vnode);
248     dirEntry->fid.vunique = htonl(1);
249     strcpy(dirEntry->name, name);
250
251     for (i = curChunk; i < curChunk + sizeOfEntry; i++) {
252         t1 = i / 8;
253         t2 = i % 8;
254         pageHeader->freebitmap[t1] |= (1 << t2);
255     }
256
257     /*
258      * Add the new entry to the correct hash chain.
259      */
260     i = DirHash(name);
261     dirEntry->next = dirHeader->hashTable[i];
262     dirHeader->hashTable[i] = htons(curPage * EPP + curChunk);
263
264     curChunk += sizeOfEntry;
265     dirHeader->alloMap[curPage] -= sizeOfEntry;
266
267     *curPageP = curPage;
268     *curChunkP = curChunk;
269 }
270
271 /*
272  * Invalidate the /afs vnode for dynroot; called when the underlying
273  * directory has changed and needs to be re-read.
274  */
275 void
276 afs_DynrootInvalidate(void)
277 {
278     afs_int32 retry;
279     struct vcache *tvc;
280     struct VenusFid tfid;
281
282     if (!afs_dynrootEnable)
283         return;
284
285     ObtainWriteLock(&afs_dynrootDirLock, 687);
286     afs_dynrootVersion++;
287     afs_dynrootVersionHigh = osi_Time();
288     ReleaseWriteLock(&afs_dynrootDirLock);
289
290     afs_GetDynrootFid(&tfid);
291     do {
292         retry = 0;
293         ObtainReadLock(&afs_xvcache);
294         tvc = afs_FindVCache(&tfid, &retry, 0);
295         ReleaseReadLock(&afs_xvcache);
296     } while (retry);
297     if (tvc) {
298         tvc->f.states &= ~(CStatd | CUnique);
299         osi_dnlc_purgedp(tvc);
300         afs_PutVCache(tvc);
301     }
302 }
303
304 /*
305  * Regenerates the dynroot contents from the current list of
306  * cells.  Useful when the list of cells has changed due to
307  * an AFSDB lookup, for instance.
308  */
309 static void
310 afs_RebuildDynroot(void)
311 {
312     int cellidx, maxcellidx, i;
313     int aliasidx, maxaliasidx;
314     struct cell *c;
315     struct cell_alias *ca;
316     int curChunk, curPage;
317     int dirSize, dotLen;
318     char *newDir, *dotCell;
319     struct DirHeader *dirHeader;
320     int linkCount = 0;
321     struct afs_dynSymlink *ts;
322     int newVersion;
323
324     ObtainReadLock(&afs_dynrootDirLock);
325     newVersion = afs_dynrootVersion;
326     ReleaseReadLock(&afs_dynrootDirLock);
327
328     /*
329      * Compute the amount of space we need for the fake dir
330      */
331     curChunk = 13;
332     curPage = 0;
333
334     /* Reserve space for "." and ".." */
335     curChunk += 2;
336
337     /* Reserve space for the dynamic-mount directory */
338     afs_dynroot_computeDirEnt(AFS_DYNROOT_MOUNTNAME, &curPage, &curChunk);
339
340     for (cellidx = 0;; cellidx++) {
341         c = afs_GetCellByIndex(cellidx, READ_LOCK);
342         if (!c)
343             break;
344         if (c->cellNum == afs_dynrootCell)
345             continue;
346
347         dotLen = strlen(c->cellName) + 2;
348         dotCell = afs_osi_Alloc(dotLen);
349         strcpy(dotCell, ".");
350         afs_strcat(dotCell, c->cellName);
351
352         afs_dynroot_computeDirEnt(c->cellName, &curPage, &curChunk);
353         afs_dynroot_computeDirEnt(dotCell, &curPage, &curChunk);
354
355         afs_osi_Free(dotCell, dotLen);
356         afs_PutCell(c, READ_LOCK);
357     }
358     maxcellidx = cellidx;
359
360     for (aliasidx = 0;; aliasidx++) {
361         ca = afs_GetCellAlias(aliasidx);
362         if (!ca)
363             break;
364
365         dotLen = strlen(ca->alias) + 2;
366         dotCell = afs_osi_Alloc(dotLen);
367         strcpy(dotCell, ".");
368         afs_strcat(dotCell, ca->alias);
369
370         afs_dynroot_computeDirEnt(ca->alias, &curPage, &curChunk);
371         afs_dynroot_computeDirEnt(dotCell, &curPage, &curChunk);
372
373         afs_osi_Free(dotCell, dotLen);
374         afs_PutCellAlias(ca);
375     }
376     maxaliasidx = aliasidx;
377
378     ObtainReadLock(&afs_dynSymlinkLock);
379     ts = afs_dynSymlinkBase;
380     while (ts) {
381         afs_dynroot_computeDirEnt(ts->name, &curPage, &curChunk);
382         ts = ts->next;
383     }
384
385     dirSize = (curPage + 1) * AFS_PAGESIZE;
386     newDir = afs_osi_Alloc(dirSize);
387
388     /*
389      * Now actually construct the directory.
390      */
391     curChunk = 13;
392     curPage = 0;
393     dirHeader = (struct DirHeader *)newDir;
394
395     dirHeader->header.pgcount = 0;
396     dirHeader->header.tag = htons(1234);
397     dirHeader->header.freecount = 0;
398
399     dirHeader->header.freebitmap[0] = 0xff;
400     dirHeader->header.freebitmap[1] = 0x1f;
401     for (i = 2; i < EPP / 8; i++)
402         dirHeader->header.freebitmap[i] = 0;
403     dirHeader->alloMap[0] = EPP - DHE - 1;
404     for (i = 1; i < MAXPAGES; i++)
405         dirHeader->alloMap[i] = EPP;
406     for (i = 0; i < NHASHENT; i++)
407         dirHeader->hashTable[i] = 0;
408
409     /* Install ".", "..", and the dynamic mount directory */
410     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ".", 1);
411     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, "..", 1);
412     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk,
413                           AFS_DYNROOT_MOUNTNAME, AFS_DYNROOT_MOUNT_VNODE);
414     linkCount += 3;
415
416     for (cellidx = 0; cellidx < maxcellidx; cellidx++) {
417         c = afs_GetCellByIndex(cellidx, READ_LOCK);
418         if (!c)
419             continue;
420         if (c->cellNum == afs_dynrootCell)
421             continue;
422
423         dotLen = strlen(c->cellName) + 2;
424         dotCell = afs_osi_Alloc(dotLen);
425         strcpy(dotCell, ".");
426         afs_strcat(dotCell, c->cellName);
427         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, c->cellName,
428                               VNUM_FROM_CIDX_RW(cellidx, 0));
429         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, dotCell,
430                               VNUM_FROM_CIDX_RW(cellidx, 1));
431         afs_osi_Free(dotCell, dotLen);
432
433         linkCount += 2;
434         afs_PutCell(c, READ_LOCK);
435     }
436
437     for (aliasidx = 0; aliasidx < maxaliasidx; aliasidx++) {
438         ca = afs_GetCellAlias(aliasidx);
439         if (!ca)
440             continue;
441
442         dotLen = strlen(ca->alias) + 2;
443         dotCell = afs_osi_Alloc(dotLen);
444         strcpy(dotCell, ".");
445         afs_strcat(dotCell, ca->alias);
446         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ca->alias,
447                               VNUM_FROM_CAIDX_RW(aliasidx, 0));
448         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, dotCell,
449                               VNUM_FROM_CAIDX_RW(aliasidx, 1));
450         afs_osi_Free(dotCell, dotLen);
451         afs_PutCellAlias(ca);
452     }
453
454     ts = afs_dynSymlinkBase;
455     while (ts) {
456         int vnum = VNUM_FROM_TYPEID(VN_TYPE_SYMLINK, ts->index);
457         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ts->name, vnum);
458         ts = ts->next;
459     }
460
461     ReleaseReadLock(&afs_dynSymlinkLock);
462
463     ObtainWriteLock(&afs_dynrootDirLock, 549);
464     if (afs_dynrootDir)
465         afs_osi_Free(afs_dynrootDir, afs_dynrootDirLen);
466     afs_dynrootDir = newDir;
467     afs_dynrootDirLen = dirSize;
468     afs_dynrootDirLinkcnt = linkCount;
469     afs_dynrootDirVersion = newVersion;
470     ReleaseWriteLock(&afs_dynrootDirLock);
471 }
472
473 static void
474 afs_RebuildDynrootMount(void)
475 {
476     int i;
477     int curChunk, curPage;
478     char *newDir;
479     struct DirHeader *dirHeader;
480
481     newDir = afs_osi_Alloc(AFS_PAGESIZE);
482
483     /*
484      * Now actually construct the directory.
485      */
486     curChunk = 13;
487     curPage = 0;
488     dirHeader = (struct DirHeader *)newDir;
489
490     dirHeader->header.pgcount = 0;
491     dirHeader->header.tag = htons(1234);
492     dirHeader->header.freecount = 0;
493
494     dirHeader->header.freebitmap[0] = 0xff;
495     dirHeader->header.freebitmap[1] = 0x1f;
496     for (i = 2; i < EPP / 8; i++)
497         dirHeader->header.freebitmap[i] = 0;
498     dirHeader->alloMap[0] = EPP - DHE - 1;
499     for (i = 1; i < MAXPAGES; i++)
500         dirHeader->alloMap[i] = EPP;
501     for (i = 0; i < NHASHENT; i++)
502         dirHeader->hashTable[i] = 0;
503
504     /* Install "." and ".." */
505     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ".", 1);
506     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, "..", 1);
507
508     ObtainWriteLock(&afs_dynrootDirLock, 549);
509     if (afs_dynrootMountDir)
510         afs_osi_Free(afs_dynrootMountDir, afs_dynrootMountDirLen);
511     afs_dynrootMountDir = newDir;
512     afs_dynrootMountDirLen = AFS_PAGESIZE;
513     ReleaseWriteLock(&afs_dynrootDirLock);
514 }
515
516 /*
517  * Returns a pointer to the base of the dynroot directory in memory,
518  * length thereof, and a FetchStatus.
519  */
520 void
521 afs_GetDynroot(char **dynrootDir, int *dynrootLen,
522                struct AFSFetchStatus *status)
523 {
524     ObtainReadLock(&afs_dynrootDirLock);
525     if (!afs_dynrootDir || afs_dynrootDirVersion != afs_dynrootVersion) {
526         ReleaseReadLock(&afs_dynrootDirLock);
527         afs_RebuildDynroot();
528         ObtainReadLock(&afs_dynrootDirLock);
529     }
530
531     if (dynrootDir)
532         *dynrootDir = afs_dynrootDir;
533     if (dynrootLen)
534         *dynrootLen = afs_dynrootDirLen;
535
536     if (status) {
537         memset(status, 0, sizeof(struct AFSFetchStatus));
538         status->FileType = Directory;
539         status->LinkCount = afs_dynrootDirLinkcnt;
540         status->Length = afs_dynrootDirLen;
541         status->DataVersion = afs_dynrootVersion;
542         status->CallerAccess = PRSFS_LOOKUP | PRSFS_READ;
543         status->AnonymousAccess = PRSFS_LOOKUP | PRSFS_READ;
544         status->UnixModeBits = 0755;
545         status->ParentVnode = 1;
546         status->ParentUnique = 1;
547         status->dataVersionHigh = afs_dynrootVersionHigh;
548     }
549 }
550
551 void
552 afs_GetDynrootMount(char **dynrootDir, int *dynrootLen,
553                     struct AFSFetchStatus *status)
554 {
555     ObtainReadLock(&afs_dynrootDirLock);
556     if (!afs_dynrootMountDir) {
557         ReleaseReadLock(&afs_dynrootDirLock);
558         afs_RebuildDynrootMount();
559         ObtainReadLock(&afs_dynrootDirLock);
560     }
561
562     if (dynrootDir)
563         *dynrootDir = afs_dynrootMountDir;
564     if (dynrootLen)
565         *dynrootLen = afs_dynrootMountDirLen;
566
567     if (status) {
568         memset(status, 0, sizeof(struct AFSFetchStatus));
569         status->FileType = Directory;
570         status->LinkCount = 1;
571         status->Length = afs_dynrootMountDirLen;
572         status->DataVersion = 1;
573         status->CallerAccess = PRSFS_LOOKUP | PRSFS_READ;
574         status->AnonymousAccess = PRSFS_LOOKUP | PRSFS_READ;
575         status->UnixModeBits = 0755;
576         status->ParentVnode = 1;
577         status->ParentUnique = 1;
578         status->dataVersionHigh = 0;
579     }
580 }
581
582 /*
583  * Puts back the dynroot read lock.
584  */
585 void
586 afs_PutDynroot(void)
587 {
588     ReleaseReadLock(&afs_dynrootDirLock);
589 }
590
591 /*
592  * Inform dynroot that a new vnode is being created.  Return value
593  * is non-zero if this vnode is handled by dynroot, in which case
594  * FetchStatus will be filled in.
595  */
596 int
597 afs_DynrootNewVnode(struct vcache *avc, struct AFSFetchStatus *status)
598 {
599     char *bp, tbuf[CVBS];
600
601     if (_afs_IsDynrootFid(&avc->f.fid)) {
602         if (!afs_dynrootEnable)
603             return 0;
604         afs_GetDynroot(0, 0, status);
605         afs_PutDynroot();
606         return 1;
607     }
608
609     if (afs_IsDynrootMount(avc)) {
610         afs_GetDynrootMount(0, 0, status);
611         afs_PutDynroot();
612         return 1;
613     }
614
615     /*
616      * Check if this is an entry under /afs, e.g. /afs/cellname.
617      */
618     if (avc->f.fid.Cell == afs_dynrootCell
619         && avc->f.fid.Fid.Volume == AFS_DYNROOT_VOLUME) {
620
621         struct cell *c;
622         struct cell_alias *ca;
623         int namelen, linklen, cellidx, rw;
624
625         memset(status, 0, sizeof(struct AFSFetchStatus));
626
627         status->FileType = SymbolicLink;
628         status->LinkCount = 1;
629         status->DataVersion = 1;
630         status->CallerAccess = PRSFS_LOOKUP | PRSFS_READ;
631         status->AnonymousAccess = PRSFS_LOOKUP | PRSFS_READ;
632         status->ParentVnode = 1;
633         status->ParentUnique = 1;
634
635         if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) == VN_TYPE_SYMLINK) {
636             struct afs_dynSymlink *ts;
637             int index = VNUM_TO_VNID(avc->f.fid.Fid.Vnode);
638
639             ObtainReadLock(&afs_dynSymlinkLock);
640             ts = afs_dynSymlinkBase;
641             while (ts) {
642                 if (ts->index == index)
643                     break;
644                 ts = ts->next;
645             }
646
647             if (ts) {
648                 linklen = strlen(ts->target);
649                 avc->linkData = afs_osi_Alloc(linklen + 1);
650                 strcpy(avc->linkData, ts->target);
651
652                 status->Length = linklen;
653                 status->UnixModeBits = 0755;
654             }
655             ReleaseReadLock(&afs_dynSymlinkLock);
656
657             return ts ? 1 : 0;
658         }
659
660         if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) != VN_TYPE_CELL
661             && VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) != VN_TYPE_ALIAS
662             && VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) != VN_TYPE_MOUNT) {
663             afs_warn("dynroot vnode inconsistency, unknown VNTYPE %d\n",
664                      VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode));
665             return 0;
666         }
667
668         cellidx = VNUM_TO_CIDX(avc->f.fid.Fid.Vnode);
669         rw = VNUM_TO_RW(avc->f.fid.Fid.Vnode);
670
671         if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) == VN_TYPE_ALIAS) {
672             char *realName;
673
674             ca = afs_GetCellAlias(cellidx);
675             if (!ca) {
676                 afs_warn("dynroot vnode inconsistency, can't find alias %d\n",
677                          cellidx);
678                 return 0;
679             }
680
681             /*
682              * linkData needs to contain the name of the cell
683              * we're aliasing for.
684              */
685             realName = ca->cell;
686             if (!realName) {
687                 afs_warn("dynroot: alias %s missing real cell name\n",
688                          ca->alias);
689                 avc->linkData = afs_strdup("unknown");
690                 linklen = 7;
691             } else {
692                 int namelen = strlen(realName);
693                 linklen = rw + namelen;
694                 avc->linkData = afs_osi_Alloc(linklen + 1);
695                 strcpy(avc->linkData, rw ? "." : "");
696                 afs_strcat(avc->linkData, realName);
697             }
698
699             status->UnixModeBits = 0755;
700             afs_PutCellAlias(ca);
701
702         } else if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) == VN_TYPE_MOUNT) {
703             c = afs_GetCellByIndex(cellidx, READ_LOCK);
704             if (!c) {
705                 afs_warn("dynroot vnode inconsistency, can't find cell %d\n",
706                          cellidx);
707                 return 0;
708             }
709
710             /*
711              * linkData needs to contain "%cell:volumeid"
712              */
713             namelen = strlen(c->cellName);
714             bp = afs_cv2string(&tbuf[CVBS], avc->f.fid.Fid.Unique);
715             linklen = 2 + namelen + strlen(bp);
716             avc->linkData = afs_osi_Alloc(linklen + 1);
717             strcpy(avc->linkData, "%");
718             afs_strcat(avc->linkData, c->cellName);
719             afs_strcat(avc->linkData, ":");
720             afs_strcat(avc->linkData, bp);
721
722             status->UnixModeBits = 0644;
723             status->ParentVnode = AFS_DYNROOT_MOUNT_VNODE;
724             afs_PutCell(c, READ_LOCK);
725
726         } else {
727             c = afs_GetCellByIndex(cellidx, READ_LOCK);
728             if (!c) {
729                 afs_warn("dynroot vnode inconsistency, can't find cell %d\n",
730                          cellidx);
731                 return 0;
732             }
733
734             /*
735              * linkData needs to contain "#cell:root.cell" or "%cell:root.cell"
736              */
737             namelen = strlen(c->cellName);
738             linklen = 1 + namelen + 10;
739             avc->linkData = afs_osi_Alloc(linklen + 1);
740             strcpy(avc->linkData, rw ? "%" : "#");
741             afs_strcat(avc->linkData, c->cellName);
742             afs_strcat(avc->linkData, ":root.cell");
743
744             status->UnixModeBits = 0644;
745             afs_PutCell(c, READ_LOCK);
746         }
747
748         status->Length = linklen;
749         return 1;
750     }
751
752     return 0;
753 }
754
755 /*
756  * Make sure dynroot initialization has been done.
757  */
758 int
759 afs_InitDynroot(void)
760 {
761     if (afs_dynrootInit)
762         return 0;
763     AFS_RWLOCK_INIT(&afs_dynrootDirLock, "afs_dynrootDirLock");
764     AFS_RWLOCK_INIT(&afs_dynSymlinkLock, "afs_dynSymlinkLock");
765     afs_dynrootInit = 0;
766     return afs_dynrootCellInit();
767 }
768
769 /*
770  * Enable or disable dynroot.  Returns 0 if successful.
771  */
772 int
773 afs_SetDynrootEnable(int enable)
774 {
775     afs_dynrootEnable = enable;
776     return afs_InitDynroot();
777 }
778
779 /*
780  * Check if dynroot support is enabled.
781  */
782 int
783 afs_GetDynrootEnable(void)
784 {
785     return afs_dynrootEnable;
786 }
787
788 /*
789  * Remove a temporary symlink entry from /afs.
790  */
791 int
792 afs_DynrootVOPRemove(struct vcache *avc, struct AFS_UCRED *acred, char *aname)
793 {
794     struct afs_dynSymlink **tpps;
795     struct afs_dynSymlink *tps;
796     int found = 0;
797
798 #if defined(AFS_SUN510_ENV)
799     if (crgetruid(acred))
800 #else
801     if (acred->cr_uid)
802 #endif
803         return EPERM;
804
805     ObtainWriteLock(&afs_dynSymlinkLock, 97);
806     tpps = &afs_dynSymlinkBase;
807     while (*tpps) {
808         tps = *tpps;
809         if (afs_strcasecmp(aname, tps->name) == 0) {
810             afs_osi_Free(tps->name, strlen(tps->name) + 1);
811             afs_osi_Free(tps->target, strlen(tps->target) + 1);
812             *tpps = tps->next;
813             afs_osi_Free(tps, sizeof(*tps));
814             afs_dynSymlinkIndex++;
815             found = 1;
816             break;
817         }
818         tpps = &(tps->next);
819     }
820     ReleaseWriteLock(&afs_dynSymlinkLock);
821     if (found) {
822         afs_DynrootInvalidate();
823         return 0;
824     }
825
826     if (afs_CellOrAliasExists(aname))
827         return EROFS;
828     else
829         return ENOENT;
830 }
831
832 /*
833  * Create a temporary symlink entry in /afs.
834  */
835 int
836 afs_DynrootVOPSymlink(struct vcache *avc, struct AFS_UCRED *acred,
837                       char *aname, char *atargetName)
838 {
839     struct afs_dynSymlink *tps;
840
841     if (acred->cr_uid)
842         return EPERM;
843     if (afs_CellOrAliasExists(aname))
844         return EEXIST;
845
846     /* Check if it's already a symlink */
847     ObtainWriteLock(&afs_dynSymlinkLock, 91);
848     tps = afs_dynSymlinkBase;
849     while (tps) {
850         if (afs_strcasecmp(aname, tps->name) == 0) {
851             ReleaseWriteLock(&afs_dynSymlinkLock);
852             return EEXIST;
853         }
854         tps = tps->next;
855     }
856
857     /* Doesn't already exist -- go ahead and create it */
858     tps = afs_osi_Alloc(sizeof(*tps));
859     tps->index = afs_dynSymlinkIndex++;
860     tps->next = afs_dynSymlinkBase;
861     tps->name = afs_osi_Alloc(strlen(aname) + 1);
862     strcpy(tps->name, aname);
863     tps->target = afs_osi_Alloc(strlen(atargetName) + 1);
864     strcpy(tps->target, atargetName);
865     afs_dynSymlinkBase = tps;
866     ReleaseWriteLock(&afs_dynSymlinkLock);
867
868     afs_DynrootInvalidate();
869     return 0;
870 }