ptserver: rename NameToID and IDToName helpers
[openafs.git] / src / dir / dir.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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #ifdef KERNEL
14 # if !defined(UKERNEL)
15 #  include "h/types.h"
16 #  if !defined(AFS_LINUX26_ENV)
17 #   include "h/param.h"
18 #  endif
19 #  ifdef        AFS_AUX_ENV
20 #   include "h/mmu.h"
21 #   include "h/seg.h"
22 #   include "h/sysmacros.h"
23 #   include "h/signal.h"
24 #   include "h/errno.h"
25 #  endif
26 #  include "h/time.h"
27 #  if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
28 #   include "h/errno.h"
29 #  else
30 #   if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
31 #    include "h/kernel.h"
32 #   endif
33 #  endif
34 #  if   defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
35 #   include "afs/sysincludes.h"
36 #  endif
37 #  if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_OBSD48_ENV) && !defined(AFS_NBSD_ENV)
38 #   include "h/user.h"
39 #  endif /* AFS_SGI64_ENV */
40 #  include "h/uio.h"
41 #  if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
42 #   include "h/mbuf.h"
43 #  endif
44 #  ifndef AFS_LINUX20_ENV
45 #   include "netinet/in.h"
46 #  endif
47 # else /* !defined(UKERNEL) */
48 #  include "afs/stds.h"
49 #  include "afs/sysincludes.h"
50 # endif /* !defined(UKERNEL) */
51
52 /* afs_buffer.c */
53 /* These are needed because afs_prototypes.h is not included here */
54 struct dcache;
55 struct DirBuffer;
56 extern int DRead(struct dcache *adc, int page, struct DirBuffer *);
57 extern int DNew(struct dcache *adc, int page, struct DirBuffer *);
58
59 # include "afs/afs_osi.h"
60
61 # include "afs/dir.h"
62
63 # ifdef AFS_LINUX20_ENV
64 #  include "h/string.h"
65 # endif
66
67 #else /* KERNEL */
68
69 # include <roken.h>
70 # include "dir.h"
71 #endif /* KERNEL */
72
73 /* Local static prototypes */
74 static int FindBlobs(dir_file_t, int);
75 static void AddPage(dir_file_t, int);
76 static void FreeBlobs(dir_file_t, int, int);
77 static int FindItem(dir_file_t, char *, struct DirBuffer *,
78                     struct DirBuffer *);
79
80 /* Find out how many entries are required to store a name. */
81 int
82 afs_dir_NameBlobs(char *name)
83 {
84     int i;
85     i = strlen(name) + 1;
86     return 1 + ((i + 15) >> 5);
87 }
88
89 /* Create an entry in a file.  Dir is a file representation, while entry is
90  * a string name. */
91 int
92 afs_dir_Create(dir_file_t dir, char *entry, void *voidfid)
93 {
94     afs_int32 *vfid = (afs_int32 *) voidfid;
95     int blobs, firstelt;
96     int i;
97     struct DirBuffer entrybuf, prevbuf, headerbuf;
98     struct DirEntry *ep;
99     struct DirHeader *dhp;
100     int code;
101
102     /* check name quality */
103     if (*entry == 0)
104         return EINVAL;
105
106     /* First check if file already exists. */
107     code = FindItem(dir, entry, &prevbuf, &entrybuf);
108     if (code && code != ENOENT) {
109         return code;
110     }
111     if (code == 0) {
112         DRelease(&entrybuf, 0);
113         DRelease(&prevbuf, 0);
114         return EEXIST;
115     }
116
117     blobs = afs_dir_NameBlobs(entry);   /* number of entries required */
118     firstelt = FindBlobs(dir, blobs);
119     if (firstelt < 0)
120         return EFBIG;           /* directory is full */
121
122     /* First, we fill in the directory entry. */
123     if (afs_dir_GetBlob(dir, firstelt, &entrybuf) != 0)
124         return EIO;
125     ep = (struct DirEntry *)entrybuf.data;
126
127     ep->flag = FFIRST;
128     ep->fid.vnode = htonl(vfid[1]);
129     ep->fid.vunique = htonl(vfid[2]);
130     strcpy(ep->name, entry);
131
132     /* Now we just have to thread it on the hash table list. */
133     if (DRead(dir, 0, &headerbuf) != 0) {
134         DRelease(&entrybuf, 1);
135         return EIO;
136     }
137     dhp = (struct DirHeader *)headerbuf.data;
138
139     i = afs_dir_DirHash(entry);
140     ep->next = dhp->hashTable[i];
141     dhp->hashTable[i] = htons(firstelt);
142     DRelease(&headerbuf, 1);
143     DRelease(&entrybuf, 1);
144     return 0;
145 }
146
147 int
148 afs_dir_Length(dir_file_t dir)
149 {
150     int i, ctr;
151     struct DirBuffer headerbuf;
152     struct DirHeader *dhp;
153
154     if (DRead(dir, 0, &headerbuf) != 0)
155         return 0;
156     dhp = (struct DirHeader *)headerbuf.data;
157
158     if (dhp->header.pgcount != 0)
159         ctr = ntohs(dhp->header.pgcount);
160     else {
161         /* old style, count the pages */
162         ctr = 0;
163         for (i = 0; i < MAXPAGES; i++)
164             if (dhp->alloMap[i] != EPP)
165                 ctr++;
166     }
167     DRelease(&headerbuf, 0);
168     return ctr * AFS_PAGESIZE;
169 }
170
171 /* Delete an entry from a directory, including update of all free entry
172  * descriptors. */
173 int
174 afs_dir_Delete(dir_file_t dir, char *entry)
175 {
176
177     int nitems, index;
178     struct DirBuffer entrybuf, prevbuf;
179     struct DirEntry *firstitem;
180     unsigned short *previtem;
181     int code;
182
183     code = FindItem(dir, entry, &prevbuf, &entrybuf);
184     if (code) {
185         return code;
186     }
187
188     firstitem = (struct DirEntry *)entrybuf.data;
189     previtem = (unsigned short *)prevbuf.data;
190
191     *previtem = firstitem->next;
192     DRelease(&prevbuf, 1);
193     index = DVOffset(&entrybuf) / 32;
194     nitems = afs_dir_NameBlobs(firstitem->name);
195     /* Clear entire DirEntry and any DirXEntry extensions */
196     memset(firstitem, 0, nitems * sizeof(*firstitem));
197     DRelease(&entrybuf, 1);
198     FreeBlobs(dir, index, nitems);
199     return 0;
200 }
201
202 /* Find a bunch of contiguous entries; at least nblobs in a row. */
203 static int
204 FindBlobs(dir_file_t dir, int nblobs)
205 {
206     int i, j, k;
207     int failed = 0;
208     struct DirBuffer headerbuf, pagebuf;
209     struct DirHeader *dhp;
210     struct PageHeader *pp;
211     int pgcount;
212
213     /* read the dir header in first. */
214     if (DRead(dir, 0, &headerbuf) != 0)
215         return -1;
216     dhp = (struct DirHeader *)headerbuf.data;
217
218     for (i = 0; i < BIGMAXPAGES; i++) {
219         if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
220             /* if page could contain enough entries */
221             /* If there are EPP free entries, then the page is not even allocated. */
222             if (i >= MAXPAGES) {
223                 /* this pages exists past the end of the old-style dir */
224                 pgcount = ntohs(dhp->header.pgcount);
225                 if (pgcount == 0) {
226                     pgcount = MAXPAGES;
227                     dhp->header.pgcount = htons(pgcount);
228                 }
229                 if (i > pgcount - 1) {
230                     /* this page is bigger than last allocated page */
231                     AddPage(dir, i);
232                     dhp->header.pgcount = htons(i + 1);
233                 }
234             } else if (dhp->alloMap[i] == EPP) {
235                 /* Add the page to the directory. */
236                 AddPage(dir, i);
237                 dhp->alloMap[i] = EPP - 1;
238                 dhp->header.pgcount = htons(i + 1);
239             }
240
241             /* read the page in. */
242             if (DRead(dir, i, &pagebuf) != 0) {
243                 break;
244             }
245             pp = (struct PageHeader *)pagebuf.data;
246             for (j = 0; j <= EPP - nblobs; j++) {
247                 failed = 0;
248                 for (k = 0; k < nblobs; k++)
249                     if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
250                         failed = 1;
251                         break;
252                     }
253                 if (!failed)
254                     break;
255                 failed = 1;
256             }
257             if (!failed) {
258                 /* Here we have the first index in j.  We update the allocation maps
259                  * and free up any resources we've got allocated. */
260                 if (i < MAXPAGES)
261                     dhp->alloMap[i] -= nblobs;
262                 DRelease(&headerbuf, 1);
263                 for (k = 0; k < nblobs; k++)
264                     pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
265                 DRelease(&pagebuf, 1);
266                 return j + i * EPP;
267             }
268             DRelease(&pagebuf, 0);      /* This dir page is unchanged. */
269         }
270     }
271     /* If we make it here, the directory is full. */
272     DRelease(&headerbuf, 1);
273     return -1;
274 }
275
276 static void
277 AddPage(dir_file_t dir, int pageno)
278 {                               /* Add a page to a directory. */
279     int i;
280     struct PageHeader *pp;
281     struct DirBuffer pagebuf;
282
283     /* Get a new buffer labelled dir,pageno */
284     DNew(dir, pageno, &pagebuf);
285     pp = (struct PageHeader *)pagebuf.data;
286
287     pp->tag = htons(1234);
288     if (pageno > 0)
289         pp->pgcount = 0;
290     pp->freecount = EPP - 1;    /* The first dude is already allocated */
291     pp->freebitmap[0] = 0x01;
292     for (i = 1; i < EPP / 8; i++)       /* It's a constant */
293         pp->freebitmap[i] = 0;
294     DRelease(&pagebuf, 1);
295 }
296
297 /* Free a whole bunch of directory entries. */
298
299 static void
300 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
301 {
302     int i;
303     int page;
304     struct DirBuffer headerbuf, pagehdbuf;
305     struct DirHeader *dhp;
306     struct PageHeader *pp;
307     page = firstblob / EPP;
308     firstblob -= EPP * page;    /* convert to page-relative entry */
309
310     if (DRead(dir, 0, &headerbuf) != 0)
311         return;
312     dhp = (struct DirHeader *)headerbuf.data;
313
314     if (page < MAXPAGES)
315         dhp->alloMap[page] += nblobs;
316
317     DRelease(&headerbuf, 1);
318
319     if (DRead(dir, page, &pagehdbuf) != 0)
320         return;
321     pp = (struct PageHeader *)pagehdbuf.data;
322
323     for (i = 0; i < nblobs; i++)
324         pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
325
326     DRelease(&pagehdbuf, 1);
327 }
328
329 /*
330  * Format an empty directory properly.  Note that the first 13 entries in a
331  * directory header page are allocated, 1 to the page header, 4 to the
332  * allocation map and 8 to the hash table.
333  */
334 int
335 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
336 {
337     int i;
338     struct DirBuffer buffer;
339     struct DirHeader *dhp;
340
341     DNew(dir, 0, &buffer);
342     dhp = (struct DirHeader *)buffer.data;
343
344     dhp->header.pgcount = htons(1);
345     dhp->header.tag = htons(1234);
346     dhp->header.freecount = (EPP - DHE - 1);
347     dhp->header.freebitmap[0] = 0xff;
348     dhp->header.freebitmap[1] = 0x1f;
349     for (i = 2; i < EPP / 8; i++)
350         dhp->header.freebitmap[i] = 0;
351     dhp->alloMap[0] = (EPP - DHE - 1);
352     for (i = 1; i < MAXPAGES; i++)
353         dhp->alloMap[i] = EPP;
354     for (i = 0; i < NHASHENT; i++)
355         dhp->hashTable[i] = 0;
356     DRelease(&buffer, 1);
357     afs_dir_Create(dir, ".", me);
358     afs_dir_Create(dir, "..", parent);  /* Virtue is its own .. */
359     return 0;
360 }
361
362 /* Look up a file name in directory. */
363
364 int
365 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
366 {
367     afs_int32 *fid = (afs_int32 *) voidfid;
368     struct DirBuffer firstbuf, prevbuf;
369     struct DirEntry *firstitem;
370     int code;
371
372     code = FindItem(dir, entry, &prevbuf, &firstbuf);
373     if (code) {
374         return code;
375     }
376     DRelease(&prevbuf, 0);
377     firstitem = (struct DirEntry *)firstbuf.data;
378
379     fid[1] = ntohl(firstitem->fid.vnode);
380     fid[2] = ntohl(firstitem->fid.vunique);
381     DRelease(&firstbuf, 0);
382     return 0;
383 }
384
385 /* Look up a file name in directory. */
386
387 int
388 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
389                      long *offsetp)
390 {
391     afs_int32 *fid = (afs_int32 *) voidfid;
392     struct DirBuffer firstbuf, prevbuf;
393     struct DirEntry *firstitem;
394     int code;
395
396     code = FindItem(dir, entry, &prevbuf, &firstbuf);
397     if (code) {
398         return code;
399     }
400     DRelease(&prevbuf, 0);
401     firstitem = (struct DirEntry *)firstbuf.data;
402
403     fid[1] = ntohl(firstitem->fid.vnode);
404     fid[2] = ntohl(firstitem->fid.vunique);
405     if (offsetp)
406         *offsetp = DVOffset(&firstbuf);
407     DRelease(&firstbuf, 0);
408     return 0;
409 }
410
411 /*
412  * Enumerate the contents of a directory. Break when hook function
413  * returns non 0.
414  */
415
416 int
417 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
418                                                   afs_int32 vnode,
419                                                   afs_int32 unique),
420                      void *hook)
421 {
422     int i;
423     int num;
424     struct DirBuffer headerbuf, entrybuf;
425     struct DirHeader *dhp;
426     struct DirEntry *ep;
427     int code = 0;
428     int elements;
429
430     if (DRead(dir, 0, &headerbuf) != 0)
431         return EIO;
432     dhp = (struct DirHeader *)headerbuf.data;
433
434     for (i = 0; i < NHASHENT; i++) {
435         /* For each hash chain, enumerate everyone on the list. */
436         num = ntohs(dhp->hashTable[i]);
437         elements = 0;
438         while (num != 0 && elements < BIGMAXPAGES * EPP) {
439             elements++;
440
441             /* Walk down the hash table list. */
442             code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
443             if (code)
444                 goto out;
445
446             ep = (struct DirEntry *)entrybuf.data;
447             if (!ep) {
448                 DRelease(&entrybuf, 0);
449                 break;
450             }
451
452             num = ntohs(ep->next);
453             code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
454                             ntohl(ep->fid.vunique));
455             DRelease(&entrybuf, 0);
456             if (code)
457                 goto out;
458         }
459     }
460
461 out:
462     DRelease(&headerbuf, 0);
463     return 0;
464 }
465
466 int
467 afs_dir_IsEmpty(dir_file_t dir)
468 {
469     /* Enumerate the contents of a directory. */
470     int i;
471     int num;
472     struct DirBuffer headerbuf, entrybuf;
473     struct DirHeader *dhp;
474     struct DirEntry *ep;
475     int elements;
476
477     if (DRead(dir, 0, &headerbuf) != 0)
478         return 0;
479     dhp = (struct DirHeader *)headerbuf.data;
480
481     for (i = 0; i < NHASHENT; i++) {
482         /* For each hash chain, enumerate everyone on the list. */
483         num = ntohs(dhp->hashTable[i]);
484         elements = 0;
485         while (num != 0 && elements < BIGMAXPAGES * EPP) {
486             elements++;
487             /* Walk down the hash table list. */
488             if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
489                 break;
490             ep = (struct DirEntry *)entrybuf.data;
491             if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
492                 DRelease(&entrybuf, 0);
493                 DRelease(&headerbuf, 0);
494                 return 1;
495             }
496             num = ntohs(ep->next);
497             DRelease(&entrybuf, 0);
498         }
499     }
500     DRelease(&headerbuf, 0);
501     return 0;
502 }
503
504 /* Return a pointer to an entry, given its number. Also return the maximum
505  * size of the entry, which is determined by its position within the directory
506  * page.
507  *
508  * If physerr is supplied by caller, it will be set to:
509  *      0       for logical errors
510  *      errno   for physical errors
511  */
512 static int
513 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
514                 struct DirBuffer *buffer, afs_size_t *maxlen, int *physerr)
515 {
516     afs_size_t pos;
517     int code;
518
519     *maxlen = 0;
520     memset(buffer, 0, sizeof(struct DirBuffer));
521
522     code = DReadWithErrno(dir, blobno >> LEPP, buffer, physerr);
523     if (code)
524         return code;
525
526     pos = 32 * (blobno & (EPP - 1));
527
528     *maxlen = AFS_PAGESIZE - pos - 1;
529
530     buffer->data = (void *)(((char *)buffer->data) + pos);
531
532     return 0;
533 }
534
535 /*
536  * Given an entry's number, return a pointer to that entry.
537  * If physerr is supplied by caller, it will be set to:
538  *      0       for logical errors
539  *      errno   for physical errors
540  */
541 int
542 afs_dir_GetBlobWithErrno(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer,
543                         int *physerr)
544 {
545     afs_size_t maxlen = 0;
546     return GetBlobWithLimit(dir, blobno, buffer, &maxlen, physerr);
547 }
548
549 /* Given an entries number, return a pointer to that entry */
550 int
551 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
552 {
553     afs_size_t maxlen = 0;
554     return GetBlobWithLimit(dir, blobno, buffer, &maxlen, NULL);
555 }
556
557 /* Return an entry, having verified that the name held within the entry
558  * doesn't overflow off the end of the directory page it is contained
559  * within
560  */
561
562 int
563 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
564                         struct DirBuffer *outbuf)
565 {
566     struct DirEntry *dir;
567     struct DirBuffer buffer;
568     afs_size_t maxlen;
569     int code;
570     char *cp;
571
572     code = GetBlobWithLimit(file, blobno, &buffer, &maxlen, NULL);
573     if (code)
574         return code;
575
576     dir = (struct DirEntry *)buffer.data;
577
578     /* A blob is only valid if the name within it is NULL terminated before
579      * the end of the blob's containing page */
580     for (cp = dir->name; *cp != '\0' && cp < ((char *)dir) + maxlen; cp++);
581
582     if (*cp != '\0') {
583         DRelease(&buffer, 0);
584         return EIO;
585     }
586
587     *outbuf = buffer;
588     return 0;
589 }
590
591 int
592 afs_dir_DirHash(char *string)
593 {
594     /* Hash a string to a number between 0 and NHASHENT. */
595     unsigned char tc;
596     unsigned int hval;
597     int tval;
598     hval = 0;
599     while ((tc = (*string++))) {
600         hval *= 173;
601         hval += tc;
602     }
603     tval = hval & (NHASHENT - 1);
604     if (tval == 0)
605         return tval;
606     else if (hval >= 1u<<31)
607         tval = NHASHENT - tval;
608     return tval;
609 }
610
611
612 /* Find a directory entry, given its name.  This entry returns a pointer
613  * to a locked buffer, and a pointer to a locked buffer (in previtem)
614  * referencing the found item (to aid the delete code).  If no entry is
615  * found, however, no items are left locked, and a null pointer is
616  * returned instead. */
617
618 static int
619 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
620          struct DirBuffer *itembuf )
621 {
622     int i, code;
623     struct DirBuffer curr, prev;
624     struct DirHeader *dhp;
625     struct DirEntry *tp;
626     int elements;
627
628     memset(prevbuf, 0, sizeof(struct DirBuffer));
629     memset(itembuf, 0, sizeof(struct DirBuffer));
630
631     code = DRead(dir, 0, &prev);
632     if (code)
633         return code;
634     dhp = (struct DirHeader *)prev.data;
635
636     i = afs_dir_DirHash(ename);
637     if (dhp->hashTable[i] == 0) {
638         /* no such entry */
639         code = ENOENT;
640         goto out;
641     }
642
643     code = afs_dir_GetVerifiedBlob(dir,
644                                    (u_short) ntohs(dhp->hashTable[i]),
645                                    &curr);
646     if (code) {
647         goto out;
648     }
649
650     prev.data = &(dhp->hashTable[i]);
651     elements = 0;
652     /* Detect circular hash chains. Absolute max size of a directory */
653     while (elements < BIGMAXPAGES * EPP) {
654         elements++;
655
656         /* Look at each entry on the hash chain */
657         tp = (struct DirEntry *)curr.data;
658         if (!strcmp(ename, tp->name)) {
659             /* Found it! */
660             *prevbuf = prev;
661             *itembuf = curr;
662             return 0;
663         }
664
665         DRelease(&prev, 0);
666
667         prev = curr;
668         prev.data = &(tp->next);
669
670         if (tp->next == 0) {
671             /* The end of the line */
672             code = ENOENT;
673             goto out;
674         }
675
676         code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
677                                        &curr);
678         if (code)
679             goto out;
680     }
681
682     /* If we've reached here, we've hit our loop limit. Something is weird with
683      * the directory; maybe a circular hash chain? */
684     code = EIO;
685
686 out:
687     DRelease(&prev, 0);
688     return code;
689 }
690
691 static int
692 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
693          struct DirBuffer *itembuf)
694 {
695     /* Find a directory entry, given the vnode and uniquifier of a object.
696      * This entry returns a pointer to a locked buffer.  If no entry is found,
697      * however, no items are left locked, and a null pointer is returned
698      * instead.
699      */
700     int i, code;
701     unsigned short next;
702     struct DirBuffer curr, header;
703     struct DirHeader *dhp;
704     struct DirEntry *tp;
705     int elements;
706
707     memset(itembuf, 0, sizeof(struct DirBuffer));
708
709     code = DRead(dir, 0, &header);
710     if (code)
711         return code;
712     dhp = (struct DirHeader *)header.data;
713
714     for (i=0; i<NHASHENT; i++) {
715         if (dhp->hashTable[i] != 0) {
716             code = afs_dir_GetVerifiedBlob(dir,
717                                            (u_short)ntohs(dhp->hashTable[i]),
718                                            &curr);
719             if (code) {
720                 DRelease(&header, 0);
721                 return code;
722             }
723             elements = 0;
724             while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
725                 elements++;
726                 tp = (struct DirEntry *)curr.data;
727
728                 if (vnode == ntohl(tp->fid.vnode)
729                     && unique == ntohl(tp->fid.vunique)) {
730                     DRelease(&header, 0);
731                     *itembuf = curr;
732                     return 0;
733                 }
734
735                 next = tp->next;
736                 DRelease(&curr, 0);
737
738                 if (next == 0)
739                     break;
740
741                 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
742                                                &curr);
743                 if (code) {
744                     DRelease(&header, 0);
745                     return code;
746                 }
747             }
748         }
749     }
750     DRelease(&header, 0);
751     return ENOENT;
752 }
753
754 int
755 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
756                       char *name, afs_uint32 length)
757 {
758     /* Look for the name pointing to given vnode and unique in a directory */
759     struct DirBuffer entrybuf;
760     struct DirEntry *entry;
761     int code = 0;
762
763     code = FindFid(dir, vnode, unique, &entrybuf);
764     if (code) {
765         return code;
766     }
767     entry = (struct DirEntry *)entrybuf.data;
768
769     if (strlen(entry->name) >= length)
770         code = E2BIG;
771     else
772         strcpy(name, entry->name);
773     DRelease(&entrybuf, 0);
774     return code;
775 }
776
777 /*!
778  * Change an entry fid.
779  *
780  * \param dir
781  * \param entry The entry name.
782  * \param old_fid The old find in MKFid format (host order).
783  * It can be omitted if you don't need a safety check...
784  * \param new_fid The new find in MKFid format (host order).
785  */
786 int
787 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
788                   afs_uint32 *new_fid)
789 {
790     struct DirBuffer prevbuf, entrybuf;
791     struct DirEntry *firstitem;
792     struct MKFid *fid_old = (struct MKFid *) old_fid;
793     struct MKFid *fid_new = (struct MKFid *) new_fid;
794     int code;
795
796     /* Find entry. */
797     code = FindItem(dir, entry, &prevbuf, &entrybuf);
798     if (code) {
799         return code;
800     }
801     firstitem = (struct DirEntry *)entrybuf.data;
802     DRelease(&prevbuf, 1);
803
804     /* Replace fid. */
805     if (!old_fid ||
806         ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
807         (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
808
809         firstitem->fid.vnode = htonl(fid_new->vnode);
810         firstitem->fid.vunique = htonl(fid_new->vunique);
811     }
812
813     DRelease(&entrybuf, 1);
814
815     return 0;
816 }