dir: check afs_dir_Create return code in afs_dir_MakeDir
[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  * \param dir       pointer to the directory object
335  * \param me        fid (vnode+uniq) for new dir
336  * \param parent    fid (vnode+uniq) for parent dir
337  *
338  * \retval 0        success
339  * \retval nonzero  error code
340  */
341 int
342 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
343 {
344     int i;
345     struct DirBuffer buffer;
346     struct DirHeader *dhp;
347     int code;
348
349     DNew(dir, 0, &buffer);
350     dhp = (struct DirHeader *)buffer.data;
351
352     dhp->header.pgcount = htons(1);
353     dhp->header.tag = htons(1234);
354     dhp->header.freecount = (EPP - DHE - 1);
355     dhp->header.freebitmap[0] = 0xff;
356     dhp->header.freebitmap[1] = 0x1f;
357     for (i = 2; i < EPP / 8; i++)
358         dhp->header.freebitmap[i] = 0;
359     dhp->alloMap[0] = (EPP - DHE - 1);
360     for (i = 1; i < MAXPAGES; i++)
361         dhp->alloMap[i] = EPP;
362     for (i = 0; i < NHASHENT; i++)
363         dhp->hashTable[i] = 0;
364     DRelease(&buffer, 1);
365     code = afs_dir_Create(dir, ".", me);
366     if (code)
367         return code;
368     code = afs_dir_Create(dir, "..", parent);
369     if (code)
370         return code;
371     return 0;
372 }
373
374 /* Look up a file name in directory. */
375
376 int
377 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
378 {
379     afs_int32 *fid = (afs_int32 *) voidfid;
380     struct DirBuffer firstbuf, prevbuf;
381     struct DirEntry *firstitem;
382     int code;
383
384     code = FindItem(dir, entry, &prevbuf, &firstbuf);
385     if (code) {
386         return code;
387     }
388     DRelease(&prevbuf, 0);
389     firstitem = (struct DirEntry *)firstbuf.data;
390
391     fid[1] = ntohl(firstitem->fid.vnode);
392     fid[2] = ntohl(firstitem->fid.vunique);
393     DRelease(&firstbuf, 0);
394     return 0;
395 }
396
397 /* Look up a file name in directory. */
398
399 int
400 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
401                      long *offsetp)
402 {
403     afs_int32 *fid = (afs_int32 *) voidfid;
404     struct DirBuffer firstbuf, prevbuf;
405     struct DirEntry *firstitem;
406     int code;
407
408     code = FindItem(dir, entry, &prevbuf, &firstbuf);
409     if (code) {
410         return code;
411     }
412     DRelease(&prevbuf, 0);
413     firstitem = (struct DirEntry *)firstbuf.data;
414
415     fid[1] = ntohl(firstitem->fid.vnode);
416     fid[2] = ntohl(firstitem->fid.vunique);
417     if (offsetp)
418         *offsetp = DVOffset(&firstbuf);
419     DRelease(&firstbuf, 0);
420     return 0;
421 }
422
423 /*
424  * Enumerate the contents of a directory. Break when hook function
425  * returns non 0.
426  */
427
428 int
429 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
430                                                   afs_int32 vnode,
431                                                   afs_int32 unique),
432                      void *hook)
433 {
434     int i;
435     int num;
436     struct DirBuffer headerbuf, entrybuf;
437     struct DirHeader *dhp;
438     struct DirEntry *ep;
439     int code = 0;
440     int elements;
441
442     if (DRead(dir, 0, &headerbuf) != 0)
443         return EIO;
444     dhp = (struct DirHeader *)headerbuf.data;
445
446     for (i = 0; i < NHASHENT; i++) {
447         /* For each hash chain, enumerate everyone on the list. */
448         num = ntohs(dhp->hashTable[i]);
449         elements = 0;
450         while (num != 0 && elements < BIGMAXPAGES * EPP) {
451             elements++;
452
453             /* Walk down the hash table list. */
454             code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
455             if (code)
456                 goto out;
457
458             ep = (struct DirEntry *)entrybuf.data;
459             if (!ep) {
460                 DRelease(&entrybuf, 0);
461                 break;
462             }
463
464             num = ntohs(ep->next);
465             code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
466                             ntohl(ep->fid.vunique));
467             DRelease(&entrybuf, 0);
468             if (code)
469                 goto out;
470         }
471     }
472
473 out:
474     DRelease(&headerbuf, 0);
475     return 0;
476 }
477
478 int
479 afs_dir_IsEmpty(dir_file_t dir)
480 {
481     /* Enumerate the contents of a directory. */
482     int i;
483     int num;
484     struct DirBuffer headerbuf, entrybuf;
485     struct DirHeader *dhp;
486     struct DirEntry *ep;
487     int elements;
488
489     if (DRead(dir, 0, &headerbuf) != 0)
490         return 0;
491     dhp = (struct DirHeader *)headerbuf.data;
492
493     for (i = 0; i < NHASHENT; i++) {
494         /* For each hash chain, enumerate everyone on the list. */
495         num = ntohs(dhp->hashTable[i]);
496         elements = 0;
497         while (num != 0 && elements < BIGMAXPAGES * EPP) {
498             elements++;
499             /* Walk down the hash table list. */
500             if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
501                 break;
502             ep = (struct DirEntry *)entrybuf.data;
503             if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
504                 DRelease(&entrybuf, 0);
505                 DRelease(&headerbuf, 0);
506                 return 1;
507             }
508             num = ntohs(ep->next);
509             DRelease(&entrybuf, 0);
510         }
511     }
512     DRelease(&headerbuf, 0);
513     return 0;
514 }
515
516 /* Return a pointer to an entry, given its number. Also return the maximum
517  * size of the entry, which is determined by its position within the directory
518  * page.
519  *
520  * If physerr is supplied by caller, it will be set to:
521  *      0       for logical errors
522  *      errno   for physical errors
523  */
524 static int
525 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
526                 struct DirBuffer *buffer, afs_size_t *maxlen, int *physerr)
527 {
528     afs_size_t pos;
529     int code;
530
531     *maxlen = 0;
532     memset(buffer, 0, sizeof(struct DirBuffer));
533
534     code = DReadWithErrno(dir, blobno >> LEPP, buffer, physerr);
535     if (code)
536         return code;
537
538     pos = 32 * (blobno & (EPP - 1));
539
540     *maxlen = AFS_PAGESIZE - pos - 1;
541
542     buffer->data = (void *)(((char *)buffer->data) + pos);
543
544     return 0;
545 }
546
547 /*
548  * Given an entry's number, return a pointer to that entry.
549  * If physerr is supplied by caller, it will be set to:
550  *      0       for logical errors
551  *      errno   for physical errors
552  */
553 int
554 afs_dir_GetBlobWithErrno(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer,
555                         int *physerr)
556 {
557     afs_size_t maxlen = 0;
558     return GetBlobWithLimit(dir, blobno, buffer, &maxlen, physerr);
559 }
560
561 /* Given an entries number, return a pointer to that entry */
562 int
563 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
564 {
565     afs_size_t maxlen = 0;
566     return GetBlobWithLimit(dir, blobno, buffer, &maxlen, NULL);
567 }
568
569 /* Return an entry, having verified that the name held within the entry
570  * doesn't overflow off the end of the directory page it is contained
571  * within
572  */
573
574 int
575 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
576                         struct DirBuffer *outbuf)
577 {
578     struct DirEntry *dir;
579     struct DirBuffer buffer;
580     afs_size_t maxlen;
581     int code;
582     char *cp;
583
584     code = GetBlobWithLimit(file, blobno, &buffer, &maxlen, NULL);
585     if (code)
586         return code;
587
588     dir = (struct DirEntry *)buffer.data;
589
590     /* A blob is only valid if the name within it is NULL terminated before
591      * the end of the blob's containing page */
592     for (cp = dir->name; *cp != '\0' && cp < ((char *)dir) + maxlen; cp++);
593
594     if (*cp != '\0') {
595         DRelease(&buffer, 0);
596         return EIO;
597     }
598
599     *outbuf = buffer;
600     return 0;
601 }
602
603 int
604 afs_dir_DirHash(char *string)
605 {
606     /* Hash a string to a number between 0 and NHASHENT. */
607     unsigned char tc;
608     unsigned int hval;
609     int tval;
610     hval = 0;
611     while ((tc = (*string++))) {
612         hval *= 173;
613         hval += tc;
614     }
615     tval = hval & (NHASHENT - 1);
616     if (tval == 0)
617         return tval;
618     else if (hval >= 1u<<31)
619         tval = NHASHENT - tval;
620     return tval;
621 }
622
623
624 /* Find a directory entry, given its name.  This entry returns a pointer
625  * to a locked buffer, and a pointer to a locked buffer (in previtem)
626  * referencing the found item (to aid the delete code).  If no entry is
627  * found, however, no items are left locked, and a null pointer is
628  * returned instead. */
629
630 static int
631 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
632          struct DirBuffer *itembuf )
633 {
634     int i, code;
635     struct DirBuffer curr, prev;
636     struct DirHeader *dhp;
637     struct DirEntry *tp;
638     int elements;
639
640     memset(prevbuf, 0, sizeof(struct DirBuffer));
641     memset(itembuf, 0, sizeof(struct DirBuffer));
642
643     code = DRead(dir, 0, &prev);
644     if (code)
645         return code;
646     dhp = (struct DirHeader *)prev.data;
647
648     i = afs_dir_DirHash(ename);
649     if (dhp->hashTable[i] == 0) {
650         /* no such entry */
651         code = ENOENT;
652         goto out;
653     }
654
655     code = afs_dir_GetVerifiedBlob(dir,
656                                    (u_short) ntohs(dhp->hashTable[i]),
657                                    &curr);
658     if (code) {
659         goto out;
660     }
661
662     prev.data = &(dhp->hashTable[i]);
663     elements = 0;
664     /* Detect circular hash chains. Absolute max size of a directory */
665     while (elements < BIGMAXPAGES * EPP) {
666         elements++;
667
668         /* Look at each entry on the hash chain */
669         tp = (struct DirEntry *)curr.data;
670         if (!strcmp(ename, tp->name)) {
671             /* Found it! */
672             *prevbuf = prev;
673             *itembuf = curr;
674             return 0;
675         }
676
677         DRelease(&prev, 0);
678
679         prev = curr;
680         prev.data = &(tp->next);
681
682         if (tp->next == 0) {
683             /* The end of the line */
684             code = ENOENT;
685             goto out;
686         }
687
688         code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
689                                        &curr);
690         if (code)
691             goto out;
692     }
693
694     /* If we've reached here, we've hit our loop limit. Something is weird with
695      * the directory; maybe a circular hash chain? */
696     code = EIO;
697
698 out:
699     DRelease(&prev, 0);
700     return code;
701 }
702
703 static int
704 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
705          struct DirBuffer *itembuf)
706 {
707     /* Find a directory entry, given the vnode and uniquifier of a object.
708      * This entry returns a pointer to a locked buffer.  If no entry is found,
709      * however, no items are left locked, and a null pointer is returned
710      * instead.
711      */
712     int i, code;
713     unsigned short next;
714     struct DirBuffer curr, header;
715     struct DirHeader *dhp;
716     struct DirEntry *tp;
717     int elements;
718
719     memset(itembuf, 0, sizeof(struct DirBuffer));
720
721     code = DRead(dir, 0, &header);
722     if (code)
723         return code;
724     dhp = (struct DirHeader *)header.data;
725
726     for (i=0; i<NHASHENT; i++) {
727         if (dhp->hashTable[i] != 0) {
728             code = afs_dir_GetVerifiedBlob(dir,
729                                            (u_short)ntohs(dhp->hashTable[i]),
730                                            &curr);
731             if (code) {
732                 DRelease(&header, 0);
733                 return code;
734             }
735             elements = 0;
736             while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
737                 elements++;
738                 tp = (struct DirEntry *)curr.data;
739
740                 if (vnode == ntohl(tp->fid.vnode)
741                     && unique == ntohl(tp->fid.vunique)) {
742                     DRelease(&header, 0);
743                     *itembuf = curr;
744                     return 0;
745                 }
746
747                 next = tp->next;
748                 DRelease(&curr, 0);
749
750                 if (next == 0)
751                     break;
752
753                 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
754                                                &curr);
755                 if (code) {
756                     DRelease(&header, 0);
757                     return code;
758                 }
759             }
760         }
761     }
762     DRelease(&header, 0);
763     return ENOENT;
764 }
765
766 int
767 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
768                       char *name, afs_uint32 length)
769 {
770     /* Look for the name pointing to given vnode and unique in a directory */
771     struct DirBuffer entrybuf;
772     struct DirEntry *entry;
773     int code = 0;
774
775     code = FindFid(dir, vnode, unique, &entrybuf);
776     if (code) {
777         return code;
778     }
779     entry = (struct DirEntry *)entrybuf.data;
780
781     if (strlen(entry->name) >= length)
782         code = E2BIG;
783     else
784         strcpy(name, entry->name);
785     DRelease(&entrybuf, 0);
786     return code;
787 }
788
789 /*!
790  * Change an entry fid.
791  *
792  * \param dir
793  * \param entry The entry name.
794  * \param old_fid The old find in MKFid format (host order).
795  * It can be omitted if you don't need a safety check...
796  * \param new_fid The new find in MKFid format (host order).
797  */
798 int
799 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
800                   afs_uint32 *new_fid)
801 {
802     struct DirBuffer prevbuf, entrybuf;
803     struct DirEntry *firstitem;
804     struct MKFid *fid_old = (struct MKFid *) old_fid;
805     struct MKFid *fid_new = (struct MKFid *) new_fid;
806     int code;
807
808     /* Find entry. */
809     code = FindItem(dir, entry, &prevbuf, &entrybuf);
810     if (code) {
811         return code;
812     }
813     firstitem = (struct DirEntry *)entrybuf.data;
814     DRelease(&prevbuf, 1);
815
816     /* Replace fid. */
817     if (!old_fid ||
818         ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
819         (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
820
821         firstitem->fid.vnode = htonl(fid_new->vnode);
822         firstitem->fid.vunique = htonl(fid_new->vunique);
823     }
824
825     DRelease(&entrybuf, 1);
826
827     return 0;
828 }