9a4e1ad315702fefd0ae38ea2b5fad0158eea588
[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[AFS_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) || (c->states & CHush)) {
345             afs_PutCell(c, READ_LOCK);
346             continue;
347         }
348         dotLen = strlen(c->cellName) + 2;
349         dotCell = afs_osi_Alloc(dotLen);
350         strcpy(dotCell, ".");
351         afs_strcat(dotCell, c->cellName);
352
353         afs_dynroot_computeDirEnt(c->cellName, &curPage, &curChunk);
354         afs_dynroot_computeDirEnt(dotCell, &curPage, &curChunk);
355
356         afs_osi_Free(dotCell, dotLen);
357         afs_PutCell(c, READ_LOCK);
358     }
359     maxcellidx = cellidx;
360
361     for (aliasidx = 0;; aliasidx++) {
362         ca = afs_GetCellAlias(aliasidx);
363         if (!ca)
364             break;
365
366         dotLen = strlen(ca->alias) + 2;
367         dotCell = afs_osi_Alloc(dotLen);
368         strcpy(dotCell, ".");
369         afs_strcat(dotCell, ca->alias);
370
371         afs_dynroot_computeDirEnt(ca->alias, &curPage, &curChunk);
372         afs_dynroot_computeDirEnt(dotCell, &curPage, &curChunk);
373
374         afs_osi_Free(dotCell, dotLen);
375         afs_PutCellAlias(ca);
376     }
377     maxaliasidx = aliasidx;
378
379     ObtainReadLock(&afs_dynSymlinkLock);
380     ts = afs_dynSymlinkBase;
381     while (ts) {
382         afs_dynroot_computeDirEnt(ts->name, &curPage, &curChunk);
383         ts = ts->next;
384     }
385
386     dirSize = (curPage + 1) * AFS_PAGESIZE;
387     newDir = afs_osi_Alloc(dirSize);
388
389     /*
390      * Now actually construct the directory.
391      */
392     curChunk = 13;
393     curPage = 0;
394     dirHeader = (struct DirHeader *)newDir;
395
396     dirHeader->header.pgcount = 0;
397     dirHeader->header.tag = htons(1234);
398     dirHeader->header.freecount = 0;
399
400     dirHeader->header.freebitmap[0] = 0xff;
401     dirHeader->header.freebitmap[1] = 0x1f;
402     for (i = 2; i < EPP / 8; i++)
403         dirHeader->header.freebitmap[i] = 0;
404     dirHeader->alloMap[0] = EPP - DHE - 1;
405     for (i = 1; i < MAXPAGES; i++)
406         dirHeader->alloMap[i] = EPP;
407     for (i = 0; i < NHASHENT; i++)
408         dirHeader->hashTable[i] = 0;
409
410     /* Install ".", "..", and the dynamic mount directory */
411     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ".", 1);
412     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, "..", 1);
413     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk,
414                           AFS_DYNROOT_MOUNTNAME, AFS_DYNROOT_MOUNT_VNODE);
415     linkCount += 3;
416
417     for (cellidx = 0; cellidx < maxcellidx; cellidx++) {
418         c = afs_GetCellByIndex(cellidx, READ_LOCK);
419         if (!c)
420             continue;
421         if ((c->cellNum == afs_dynrootCell) || (c->states & CHush)) {
422             afs_PutCell(c, READ_LOCK);
423             continue;
424         }
425
426         dotLen = strlen(c->cellName) + 2;
427         dotCell = afs_osi_Alloc(dotLen);
428         strcpy(dotCell, ".");
429         afs_strcat(dotCell, c->cellName);
430         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, c->cellName,
431                               VNUM_FROM_CIDX_RW(cellidx, 0));
432         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, dotCell,
433                               VNUM_FROM_CIDX_RW(cellidx, 1));
434         afs_osi_Free(dotCell, dotLen);
435
436         linkCount += 2;
437         afs_PutCell(c, READ_LOCK);
438     }
439
440     for (aliasidx = 0; aliasidx < maxaliasidx; aliasidx++) {
441         ca = afs_GetCellAlias(aliasidx);
442         if (!ca)
443             continue;
444
445         dotLen = strlen(ca->alias) + 2;
446         dotCell = afs_osi_Alloc(dotLen);
447         strcpy(dotCell, ".");
448         afs_strcat(dotCell, ca->alias);
449         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ca->alias,
450                               VNUM_FROM_CAIDX_RW(aliasidx, 0));
451         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, dotCell,
452                               VNUM_FROM_CAIDX_RW(aliasidx, 1));
453         afs_osi_Free(dotCell, dotLen);
454         afs_PutCellAlias(ca);
455     }
456
457     ts = afs_dynSymlinkBase;
458     while (ts) {
459         int vnum = VNUM_FROM_TYPEID(VN_TYPE_SYMLINK, ts->index);
460         afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ts->name, vnum);
461         ts = ts->next;
462     }
463
464     ReleaseReadLock(&afs_dynSymlinkLock);
465
466     ObtainWriteLock(&afs_dynrootDirLock, 549);
467     if (afs_dynrootDir)
468         afs_osi_Free(afs_dynrootDir, afs_dynrootDirLen);
469     afs_dynrootDir = newDir;
470     afs_dynrootDirLen = dirSize;
471     afs_dynrootDirLinkcnt = linkCount;
472     afs_dynrootDirVersion = newVersion;
473     ReleaseWriteLock(&afs_dynrootDirLock);
474 }
475
476 static void
477 afs_RebuildDynrootMount(void)
478 {
479     int i;
480     int curChunk, curPage;
481     char *newDir;
482     struct DirHeader *dirHeader;
483
484     newDir = afs_osi_Alloc(AFS_PAGESIZE);
485
486     /*
487      * Now actually construct the directory.
488      */
489     curChunk = 13;
490     curPage = 0;
491     dirHeader = (struct DirHeader *)newDir;
492
493     dirHeader->header.pgcount = 0;
494     dirHeader->header.tag = htons(1234);
495     dirHeader->header.freecount = 0;
496
497     dirHeader->header.freebitmap[0] = 0xff;
498     dirHeader->header.freebitmap[1] = 0x1f;
499     for (i = 2; i < EPP / 8; i++)
500         dirHeader->header.freebitmap[i] = 0;
501     dirHeader->alloMap[0] = EPP - DHE - 1;
502     for (i = 1; i < MAXPAGES; i++)
503         dirHeader->alloMap[i] = EPP;
504     for (i = 0; i < NHASHENT; i++)
505         dirHeader->hashTable[i] = 0;
506
507     /* Install "." and ".." */
508     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, ".", 1);
509     afs_dynroot_addDirEnt(dirHeader, &curPage, &curChunk, "..", 1);
510
511     ObtainWriteLock(&afs_dynrootDirLock, 549);
512     if (afs_dynrootMountDir)
513         afs_osi_Free(afs_dynrootMountDir, afs_dynrootMountDirLen);
514     afs_dynrootMountDir = newDir;
515     afs_dynrootMountDirLen = AFS_PAGESIZE;
516     ReleaseWriteLock(&afs_dynrootDirLock);
517 }
518
519 /*
520  * Returns a pointer to the base of the dynroot directory in memory,
521  * length thereof, and a FetchStatus.
522  */
523 void
524 afs_GetDynroot(char **dynrootDir, int *dynrootLen,
525                struct AFSFetchStatus *status)
526 {
527     ObtainReadLock(&afs_dynrootDirLock);
528     if (!afs_dynrootDir || afs_dynrootDirVersion != afs_dynrootVersion) {
529         ReleaseReadLock(&afs_dynrootDirLock);
530         afs_RebuildDynroot();
531         ObtainReadLock(&afs_dynrootDirLock);
532     }
533
534     if (dynrootDir)
535         *dynrootDir = afs_dynrootDir;
536     if (dynrootLen)
537         *dynrootLen = afs_dynrootDirLen;
538
539     if (status) {
540         memset(status, 0, sizeof(struct AFSFetchStatus));
541         status->FileType = Directory;
542         status->LinkCount = afs_dynrootDirLinkcnt;
543         status->Length = afs_dynrootDirLen;
544         status->DataVersion = afs_dynrootVersion;
545         status->CallerAccess = PRSFS_LOOKUP | PRSFS_READ;
546         status->AnonymousAccess = PRSFS_LOOKUP | PRSFS_READ;
547         status->UnixModeBits = 0755;
548         status->ParentVnode = 1;
549         status->ParentUnique = 1;
550         status->dataVersionHigh = afs_dynrootVersionHigh;
551     }
552 }
553
554 void
555 afs_GetDynrootMount(char **dynrootDir, int *dynrootLen,
556                     struct AFSFetchStatus *status)
557 {
558     ObtainReadLock(&afs_dynrootDirLock);
559     if (!afs_dynrootMountDir) {
560         ReleaseReadLock(&afs_dynrootDirLock);
561         afs_RebuildDynrootMount();
562         ObtainReadLock(&afs_dynrootDirLock);
563     }
564
565     if (dynrootDir)
566         *dynrootDir = afs_dynrootMountDir;
567     if (dynrootLen)
568         *dynrootLen = afs_dynrootMountDirLen;
569
570     if (status) {
571         memset(status, 0, sizeof(struct AFSFetchStatus));
572         status->FileType = Directory;
573         status->LinkCount = 1;
574         status->Length = afs_dynrootMountDirLen;
575         status->DataVersion = 1;
576         status->CallerAccess = PRSFS_LOOKUP | PRSFS_READ;
577         status->AnonymousAccess = PRSFS_LOOKUP | PRSFS_READ;
578         status->UnixModeBits = 0755;
579         status->ParentVnode = 1;
580         status->ParentUnique = 1;
581         status->dataVersionHigh = 0;
582     }
583 }
584
585 /*
586  * Puts back the dynroot read lock.
587  */
588 void
589 afs_PutDynroot(void)
590 {
591     ReleaseReadLock(&afs_dynrootDirLock);
592 }
593
594 /*
595  * Inform dynroot that a new vnode is being created.  Return value
596  * is non-zero if this vnode is handled by dynroot, in which case
597  * FetchStatus will be filled in.
598  */
599 int
600 afs_DynrootNewVnode(struct vcache *avc, struct AFSFetchStatus *status)
601 {
602     char *bp, tbuf[CVBS];
603
604     if (_afs_IsDynrootFid(&avc->f.fid)) {
605         if (!afs_dynrootEnable)
606             return 0;
607         afs_GetDynroot(0, 0, status);
608         afs_PutDynroot();
609         return 1;
610     }
611
612     if (afs_IsDynrootMount(avc)) {
613         afs_GetDynrootMount(0, 0, status);
614         afs_PutDynroot();
615         return 1;
616     }
617
618     /*
619      * Check if this is an entry under /afs, e.g. /afs/cellname.
620      */
621     if (avc->f.fid.Cell == afs_dynrootCell
622         && avc->f.fid.Fid.Volume == AFS_DYNROOT_VOLUME) {
623
624         struct cell *c;
625         struct cell_alias *ca;
626         int namelen, linklen, cellidx, rw;
627
628         memset(status, 0, sizeof(struct AFSFetchStatus));
629
630         status->FileType = SymbolicLink;
631         status->LinkCount = 1;
632         status->DataVersion = 1;
633         status->CallerAccess = PRSFS_LOOKUP | PRSFS_READ;
634         status->AnonymousAccess = PRSFS_LOOKUP | PRSFS_READ;
635         status->ParentVnode = 1;
636         status->ParentUnique = 1;
637
638         if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) == VN_TYPE_SYMLINK) {
639             struct afs_dynSymlink *ts;
640             int index = VNUM_TO_VNID(avc->f.fid.Fid.Vnode);
641
642             ObtainReadLock(&afs_dynSymlinkLock);
643             ts = afs_dynSymlinkBase;
644             while (ts) {
645                 if (ts->index == index)
646                     break;
647                 ts = ts->next;
648             }
649
650             if (ts) {
651                 linklen = strlen(ts->target);
652                 avc->linkData = afs_osi_Alloc(linklen + 1);
653                 strcpy(avc->linkData, ts->target);
654
655                 status->Length = linklen;
656                 status->UnixModeBits = 0755;
657             }
658             ReleaseReadLock(&afs_dynSymlinkLock);
659
660             return ts ? 1 : 0;
661         }
662
663         if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) != VN_TYPE_CELL
664             && VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) != VN_TYPE_ALIAS
665             && VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) != VN_TYPE_MOUNT) {
666             afs_warn("dynroot vnode inconsistency, unknown VNTYPE %d\n",
667                      VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode));
668             return 0;
669         }
670
671         cellidx = VNUM_TO_CIDX(avc->f.fid.Fid.Vnode);
672         rw = VNUM_TO_RW(avc->f.fid.Fid.Vnode);
673
674         if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) == VN_TYPE_ALIAS) {
675             char *realName;
676
677             ca = afs_GetCellAlias(cellidx);
678             if (!ca) {
679                 afs_warn("dynroot vnode inconsistency, can't find alias %d\n",
680                          cellidx);
681                 return 0;
682             }
683
684             /*
685              * linkData needs to contain the name of the cell
686              * we're aliasing for.
687              */
688             realName = ca->cell;
689             if (!realName) {
690                 afs_warn("dynroot: alias %s missing real cell name\n",
691                          ca->alias);
692                 avc->linkData = afs_strdup("unknown");
693                 linklen = 7;
694             } else {
695                 int namelen = strlen(realName);
696                 linklen = rw + namelen;
697                 avc->linkData = afs_osi_Alloc(linklen + 1);
698                 strcpy(avc->linkData, rw ? "." : "");
699                 afs_strcat(avc->linkData, realName);
700             }
701
702             status->UnixModeBits = 0755;
703             afs_PutCellAlias(ca);
704
705         } else if (VNUM_TO_VNTYPE(avc->f.fid.Fid.Vnode) == VN_TYPE_MOUNT) {
706             c = afs_GetCellByIndex(cellidx, READ_LOCK);
707             if (!c) {
708                 afs_warn("dynroot vnode inconsistency, can't find cell %d\n",
709                          cellidx);
710                 return 0;
711             }
712
713             /*
714              * linkData needs to contain "%cell:volumeid"
715              */
716             namelen = strlen(c->cellName);
717             bp = afs_cv2string(&tbuf[CVBS], avc->f.fid.Fid.Unique);
718             linklen = 2 + namelen + strlen(bp);
719             avc->linkData = afs_osi_Alloc(linklen + 1);
720             strcpy(avc->linkData, "%");
721             afs_strcat(avc->linkData, c->cellName);
722             afs_strcat(avc->linkData, ":");
723             afs_strcat(avc->linkData, bp);
724
725             status->UnixModeBits = 0644;
726             status->ParentVnode = AFS_DYNROOT_MOUNT_VNODE;
727             afs_PutCell(c, READ_LOCK);
728
729         } else {
730             c = afs_GetCellByIndex(cellidx, READ_LOCK);
731             if (!c) {
732                 afs_warn("dynroot vnode inconsistency, can't find cell %d\n",
733                          cellidx);
734                 return 0;
735             }
736
737             /*
738              * linkData needs to contain "#cell:root.cell" or "%cell:root.cell"
739              */
740             namelen = strlen(c->cellName);
741             linklen = 1 + namelen + 10;
742             avc->linkData = afs_osi_Alloc(linklen + 1);
743             strcpy(avc->linkData, rw ? "%" : "#");
744             afs_strcat(avc->linkData, c->cellName);
745             afs_strcat(avc->linkData, ":root.cell");
746
747             status->UnixModeBits = 0644;
748             afs_PutCell(c, READ_LOCK);
749         }
750
751         status->Length = linklen;
752         return 1;
753     }
754
755     return 0;
756 }
757
758 /*
759  * Make sure dynroot initialization has been done.
760  */
761 int
762 afs_InitDynroot(void)
763 {
764     if (afs_dynrootInit)
765         return 0;
766     AFS_RWLOCK_INIT(&afs_dynrootDirLock, "afs_dynrootDirLock");
767     AFS_RWLOCK_INIT(&afs_dynSymlinkLock, "afs_dynSymlinkLock");
768     afs_dynrootInit = 0;
769     return afs_dynrootCellInit();
770 }
771
772 /*
773  * Enable or disable dynroot.  Returns 0 if successful.
774  */
775 int
776 afs_SetDynrootEnable(int enable)
777 {
778     afs_dynrootEnable = enable;
779     return afs_InitDynroot();
780 }
781
782 /*
783  * Check if dynroot support is enabled.
784  */
785 int
786 afs_GetDynrootEnable(void)
787 {
788     return afs_dynrootEnable;
789 }
790
791 /*
792  * Remove a temporary symlink entry from /afs.
793  */
794 int
795 afs_DynrootVOPRemove(struct vcache *avc, afs_ucred_t *acred, char *aname)
796 {
797     struct afs_dynSymlink **tpps;
798     struct afs_dynSymlink *tps;
799     int found = 0;
800
801 #if defined(AFS_SUN510_ENV)
802     if (crgetruid(acred))
803 #else
804     if (afs_cr_uid(acred))
805 #endif
806         return EPERM;
807
808     ObtainWriteLock(&afs_dynSymlinkLock, 97);
809     tpps = &afs_dynSymlinkBase;
810     while (*tpps) {
811         tps = *tpps;
812         if (afs_strcasecmp(aname, tps->name) == 0) {
813             afs_osi_Free(tps->name, strlen(tps->name) + 1);
814             afs_osi_Free(tps->target, strlen(tps->target) + 1);
815             *tpps = tps->next;
816             afs_osi_Free(tps, sizeof(*tps));
817             afs_dynSymlinkIndex++;
818             found = 1;
819             break;
820         }
821         tpps = &(tps->next);
822     }
823     ReleaseWriteLock(&afs_dynSymlinkLock);
824     if (found) {
825         afs_DynrootInvalidate();
826         return 0;
827     }
828
829     if (afs_CellOrAliasExists(aname))
830         return EROFS;
831     else
832         return ENOENT;
833 }
834
835 /*
836  * Create a temporary symlink entry in /afs.
837  */
838 int
839 afs_DynrootVOPSymlink(struct vcache *avc, afs_ucred_t *acred,
840                       char *aname, char *atargetName)
841 {
842     struct afs_dynSymlink *tps;
843
844     if (afs_cr_uid(acred))
845         return EPERM;
846     if (afs_CellOrAliasExists(aname))
847         return EEXIST;
848
849     /* Check if it's already a symlink */
850     ObtainWriteLock(&afs_dynSymlinkLock, 91);
851     tps = afs_dynSymlinkBase;
852     while (tps) {
853         if (afs_strcasecmp(aname, tps->name) == 0) {
854             ReleaseWriteLock(&afs_dynSymlinkLock);
855             return EEXIST;
856         }
857         tps = tps->next;
858     }
859
860     /* Doesn't already exist -- go ahead and create it */
861     tps = afs_osi_Alloc(sizeof(*tps));
862     tps->index = afs_dynSymlinkIndex++;
863     tps->next = afs_dynSymlinkBase;
864     tps->name = afs_osi_Alloc(strlen(aname) + 1);
865     strcpy(tps->name, aname);
866     tps->target = afs_osi_Alloc(strlen(atargetName) + 1);
867     strcpy(tps->target, atargetName);
868     afs_dynSymlinkBase = tps;
869     ReleaseWriteLock(&afs_dynSymlinkLock);
870
871     afs_DynrootInvalidate();
872     return 0;
873 }