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