2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
14 # if !defined(UKERNEL)
16 # if !defined(AFS_LINUX26_ENV)
22 # include "h/sysmacros.h"
23 # include "h/signal.h"
27 # if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
30 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
31 # include "h/kernel.h"
34 # if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
35 # include "afs/sysincludes.h"
37 # if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_OBSD48_ENV) && !defined(AFS_NBSD_ENV)
39 # endif /* AFS_SGI64_ENV */
41 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
44 # ifndef AFS_LINUX20_ENV
45 # include "netinet/in.h"
47 # else /* !defined(UKERNEL) */
48 # include "afs/stds.h"
49 # include "afs/sysincludes.h"
50 # endif /* !defined(UKERNEL) */
53 /* These are needed because afs_prototypes.h is not included here */
56 extern int DRead(struct dcache *adc, int page, struct DirBuffer *);
57 extern int DNew(struct dcache *adc, int page, struct DirBuffer *);
59 # include "afs/afs_osi.h"
63 # ifdef AFS_LINUX20_ENV
64 # include "h/string.h"
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 *,
80 /* Find out how many entries are required to store a name. */
82 afs_dir_NameBlobs(char *name)
86 return 1 + ((i + 15) >> 5);
89 /* Create an entry in a file. Dir is a file representation, while entry is
92 afs_dir_Create(dir_file_t dir, char *entry, void *voidfid)
94 afs_int32 *vfid = (afs_int32 *) voidfid;
97 struct DirBuffer entrybuf, prevbuf, headerbuf;
99 struct DirHeader *dhp;
102 /* check name quality */
106 /* First check if file already exists. */
107 code = FindItem(dir, entry, &prevbuf, &entrybuf);
108 if (code && code != ENOENT) {
112 DRelease(&entrybuf, 0);
113 DRelease(&prevbuf, 0);
117 blobs = afs_dir_NameBlobs(entry); /* number of entries required */
118 firstelt = FindBlobs(dir, blobs);
120 return EFBIG; /* directory is full */
122 /* First, we fill in the directory entry. */
123 if (afs_dir_GetBlob(dir, firstelt, &entrybuf) != 0)
125 ep = (struct DirEntry *)entrybuf.data;
128 ep->fid.vnode = htonl(vfid[1]);
129 ep->fid.vunique = htonl(vfid[2]);
130 strcpy(ep->name, entry);
132 /* Now we just have to thread it on the hash table list. */
133 if (DRead(dir, 0, &headerbuf) != 0) {
134 DRelease(&entrybuf, 1);
137 dhp = (struct DirHeader *)headerbuf.data;
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);
148 afs_dir_Length(dir_file_t dir)
151 struct DirBuffer headerbuf;
152 struct DirHeader *dhp;
154 if (DRead(dir, 0, &headerbuf) != 0)
156 dhp = (struct DirHeader *)headerbuf.data;
158 if (dhp->header.pgcount != 0)
159 ctr = ntohs(dhp->header.pgcount);
161 /* old style, count the pages */
163 for (i = 0; i < MAXPAGES; i++)
164 if (dhp->alloMap[i] != EPP)
167 DRelease(&headerbuf, 0);
168 return ctr * AFS_PAGESIZE;
171 /* Delete an entry from a directory, including update of all free entry
174 afs_dir_Delete(dir_file_t dir, char *entry)
178 struct DirBuffer entrybuf, prevbuf;
179 struct DirEntry *firstitem;
180 unsigned short *previtem;
183 code = FindItem(dir, entry, &prevbuf, &entrybuf);
188 firstitem = (struct DirEntry *)entrybuf.data;
189 previtem = (unsigned short *)prevbuf.data;
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);
202 /* Find a bunch of contiguous entries; at least nblobs in a row. */
204 FindBlobs(dir_file_t dir, int nblobs)
208 struct DirBuffer headerbuf, pagebuf;
209 struct DirHeader *dhp;
210 struct PageHeader *pp;
213 /* read the dir header in first. */
214 if (DRead(dir, 0, &headerbuf) != 0)
216 dhp = (struct DirHeader *)headerbuf.data;
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. */
223 /* this pages exists past the end of the old-style dir */
224 pgcount = ntohs(dhp->header.pgcount);
227 dhp->header.pgcount = htons(pgcount);
229 if (i > pgcount - 1) {
230 /* this page is bigger than last allocated page */
232 dhp->header.pgcount = htons(i + 1);
234 } else if (dhp->alloMap[i] == EPP) {
235 /* Add the page to the directory. */
237 dhp->alloMap[i] = EPP - 1;
238 dhp->header.pgcount = htons(i + 1);
241 /* read the page in. */
242 if (DRead(dir, i, &pagebuf) != 0) {
245 pp = (struct PageHeader *)pagebuf.data;
246 for (j = 0; j <= EPP - nblobs; j++) {
248 for (k = 0; k < nblobs; k++)
249 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
258 /* Here we have the first index in j. We update the allocation maps
259 * and free up any resources we've got allocated. */
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);
268 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
271 /* If we make it here, the directory is full. */
272 DRelease(&headerbuf, 1);
277 AddPage(dir_file_t dir, int pageno)
278 { /* Add a page to a directory. */
280 struct PageHeader *pp;
281 struct DirBuffer pagebuf;
283 /* Get a new buffer labelled dir,pageno */
284 DNew(dir, pageno, &pagebuf);
285 pp = (struct PageHeader *)pagebuf.data;
287 pp->tag = htons(1234);
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);
297 /* Free a whole bunch of directory entries. */
300 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
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 */
310 if (DRead(dir, 0, &headerbuf) != 0)
312 dhp = (struct DirHeader *)headerbuf.data;
315 dhp->alloMap[page] += nblobs;
317 DRelease(&headerbuf, 1);
319 if (DRead(dir, page, &pagehdbuf) != 0)
321 pp = (struct PageHeader *)pagehdbuf.data;
323 for (i = 0; i < nblobs; i++)
324 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
326 DRelease(&pagehdbuf, 1);
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.
335 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
338 struct DirBuffer buffer;
339 struct DirHeader *dhp;
341 DNew(dir, 0, &buffer);
342 dhp = (struct DirHeader *)buffer.data;
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 .. */
362 /* Look up a file name in directory. */
365 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
367 afs_int32 *fid = (afs_int32 *) voidfid;
368 struct DirBuffer firstbuf, prevbuf;
369 struct DirEntry *firstitem;
372 code = FindItem(dir, entry, &prevbuf, &firstbuf);
376 DRelease(&prevbuf, 0);
377 firstitem = (struct DirEntry *)firstbuf.data;
379 fid[1] = ntohl(firstitem->fid.vnode);
380 fid[2] = ntohl(firstitem->fid.vunique);
381 DRelease(&firstbuf, 0);
385 /* Look up a file name in directory. */
388 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
391 afs_int32 *fid = (afs_int32 *) voidfid;
392 struct DirBuffer firstbuf, prevbuf;
393 struct DirEntry *firstitem;
396 code = FindItem(dir, entry, &prevbuf, &firstbuf);
400 DRelease(&prevbuf, 0);
401 firstitem = (struct DirEntry *)firstbuf.data;
403 fid[1] = ntohl(firstitem->fid.vnode);
404 fid[2] = ntohl(firstitem->fid.vunique);
406 *offsetp = DVOffset(&firstbuf);
407 DRelease(&firstbuf, 0);
412 * Enumerate the contents of a directory. Break when hook function
417 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
424 struct DirBuffer headerbuf, entrybuf;
425 struct DirHeader *dhp;
430 if (DRead(dir, 0, &headerbuf) != 0)
432 dhp = (struct DirHeader *)headerbuf.data;
434 for (i = 0; i < NHASHENT; i++) {
435 /* For each hash chain, enumerate everyone on the list. */
436 num = ntohs(dhp->hashTable[i]);
438 while (num != 0 && elements < BIGMAXPAGES * EPP) {
441 /* Walk down the hash table list. */
442 code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
446 ep = (struct DirEntry *)entrybuf.data;
448 DRelease(&entrybuf, 0);
452 num = ntohs(ep->next);
453 code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
454 ntohl(ep->fid.vunique));
455 DRelease(&entrybuf, 0);
462 DRelease(&headerbuf, 0);
467 afs_dir_IsEmpty(dir_file_t dir)
469 /* Enumerate the contents of a directory. */
472 struct DirBuffer headerbuf, entrybuf;
473 struct DirHeader *dhp;
477 if (DRead(dir, 0, &headerbuf) != 0)
479 dhp = (struct DirHeader *)headerbuf.data;
481 for (i = 0; i < NHASHENT; i++) {
482 /* For each hash chain, enumerate everyone on the list. */
483 num = ntohs(dhp->hashTable[i]);
485 while (num != 0 && elements < BIGMAXPAGES * EPP) {
487 /* Walk down the hash table list. */
488 if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
490 ep = (struct DirEntry *)entrybuf.data;
491 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
492 DRelease(&entrybuf, 0);
493 DRelease(&headerbuf, 0);
496 num = ntohs(ep->next);
497 DRelease(&entrybuf, 0);
500 DRelease(&headerbuf, 0);
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
508 * If physerr is supplied by caller, it will be set to:
509 * 0 for logical errors
510 * errno for physical errors
513 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
514 struct DirBuffer *buffer, afs_size_t *maxlen, int *physerr)
520 memset(buffer, 0, sizeof(struct DirBuffer));
522 code = DReadWithErrno(dir, blobno >> LEPP, buffer, physerr);
526 pos = 32 * (blobno & (EPP - 1));
528 *maxlen = AFS_PAGESIZE - pos - 1;
530 buffer->data = (void *)(((char *)buffer->data) + pos);
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
542 afs_dir_GetBlobWithErrno(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer,
545 afs_size_t maxlen = 0;
546 return GetBlobWithLimit(dir, blobno, buffer, &maxlen, physerr);
549 /* Given an entries number, return a pointer to that entry */
551 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
553 afs_size_t maxlen = 0;
554 return GetBlobWithLimit(dir, blobno, buffer, &maxlen, NULL);
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
563 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
564 struct DirBuffer *outbuf)
566 struct DirEntry *dir;
567 struct DirBuffer buffer;
572 code = GetBlobWithLimit(file, blobno, &buffer, &maxlen, NULL);
576 dir = (struct DirEntry *)buffer.data;
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++);
583 DRelease(&buffer, 0);
592 afs_dir_DirHash(char *string)
594 /* Hash a string to a number between 0 and NHASHENT. */
599 while ((tc = (*string++))) {
603 tval = hval & (NHASHENT - 1);
606 else if (hval >= 1u<<31)
607 tval = NHASHENT - tval;
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. */
619 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
620 struct DirBuffer *itembuf )
623 struct DirBuffer curr, prev;
624 struct DirHeader *dhp;
628 memset(prevbuf, 0, sizeof(struct DirBuffer));
629 memset(itembuf, 0, sizeof(struct DirBuffer));
631 code = DRead(dir, 0, &prev);
634 dhp = (struct DirHeader *)prev.data;
636 i = afs_dir_DirHash(ename);
637 if (dhp->hashTable[i] == 0) {
643 code = afs_dir_GetVerifiedBlob(dir,
644 (u_short) ntohs(dhp->hashTable[i]),
650 prev.data = &(dhp->hashTable[i]);
652 /* Detect circular hash chains. Absolute max size of a directory */
653 while (elements < BIGMAXPAGES * EPP) {
656 /* Look at each entry on the hash chain */
657 tp = (struct DirEntry *)curr.data;
658 if (!strcmp(ename, tp->name)) {
668 prev.data = &(tp->next);
671 /* The end of the line */
676 code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
682 /* If we've reached here, we've hit our loop limit. Something is weird with
683 * the directory; maybe a circular hash chain? */
692 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
693 struct DirBuffer *itembuf)
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
702 struct DirBuffer curr, header;
703 struct DirHeader *dhp;
707 memset(itembuf, 0, sizeof(struct DirBuffer));
709 code = DRead(dir, 0, &header);
712 dhp = (struct DirHeader *)header.data;
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]),
720 DRelease(&header, 0);
724 while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
726 tp = (struct DirEntry *)curr.data;
728 if (vnode == ntohl(tp->fid.vnode)
729 && unique == ntohl(tp->fid.vunique)) {
730 DRelease(&header, 0);
741 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
744 DRelease(&header, 0);
750 DRelease(&header, 0);
755 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
756 char *name, afs_uint32 length)
758 /* Look for the name pointing to given vnode and unique in a directory */
759 struct DirBuffer entrybuf;
760 struct DirEntry *entry;
763 code = FindFid(dir, vnode, unique, &entrybuf);
767 entry = (struct DirEntry *)entrybuf.data;
769 if (strlen(entry->name) >= length)
772 strcpy(name, entry->name);
773 DRelease(&entrybuf, 0);
778 * Change an entry fid.
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).
787 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
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;
797 code = FindItem(dir, entry, &prevbuf, &entrybuf);
801 firstitem = (struct DirEntry *)entrybuf.data;
802 DRelease(&prevbuf, 1);
806 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
807 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
809 firstitem->fid.vnode = htonl(fid_new->vnode);
810 firstitem->fid.vunique = htonl(fid_new->vunique);
813 DRelease(&entrybuf, 1);