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"
75 /* Local static prototypes */
76 static int FindBlobs(dir_file_t, int);
77 static void AddPage(dir_file_t, int);
78 static void FreeBlobs(dir_file_t, int, int);
79 static int FindItem(dir_file_t, char *, struct DirBuffer *,
82 /* Find out how many entries are required to store a name. */
84 afs_dir_NameBlobs(char *name)
88 return 1 + ((i + 15) >> 5);
91 /* Create an entry in a file. Dir is a file representation, while entry is
94 afs_dir_Create(dir_file_t dir, char *entry, void *voidfid)
96 afs_int32 *vfid = (afs_int32 *) voidfid;
99 struct DirBuffer entrybuf, prevbuf, headerbuf;
101 struct DirHeader *dhp;
104 /* check name quality */
108 /* First check if file already exists. */
109 code = FindItem(dir, entry, &prevbuf, &entrybuf);
110 if (code && code != ENOENT) {
114 DRelease(&entrybuf, 0);
115 DRelease(&prevbuf, 0);
119 blobs = afs_dir_NameBlobs(entry); /* number of entries required */
120 firstelt = FindBlobs(dir, blobs);
122 return EFBIG; /* directory is full */
124 /* First, we fill in the directory entry. */
125 if (afs_dir_GetBlob(dir, firstelt, &entrybuf) != 0)
127 ep = (struct DirEntry *)entrybuf.data;
130 ep->fid.vnode = htonl(vfid[1]);
131 ep->fid.vunique = htonl(vfid[2]);
132 strcpy(ep->name, entry);
134 /* Now we just have to thread it on the hash table list. */
135 if (DRead(dir, 0, &headerbuf) != 0) {
136 DRelease(&entrybuf, 1);
139 dhp = (struct DirHeader *)headerbuf.data;
141 i = afs_dir_DirHash(entry);
142 ep->next = dhp->hashTable[i];
143 dhp->hashTable[i] = htons(firstelt);
144 DRelease(&headerbuf, 1);
145 DRelease(&entrybuf, 1);
150 afs_dir_Length(dir_file_t dir)
153 struct DirBuffer headerbuf;
154 struct DirHeader *dhp;
156 if (DRead(dir, 0, &headerbuf) != 0)
158 dhp = (struct DirHeader *)headerbuf.data;
160 if (dhp->header.pgcount != 0)
161 ctr = ntohs(dhp->header.pgcount);
163 /* old style, count the pages */
165 for (i = 0; i < MAXPAGES; i++)
166 if (dhp->alloMap[i] != EPP)
169 DRelease(&headerbuf, 0);
170 return ctr * AFS_PAGESIZE;
173 /* Delete an entry from a directory, including update of all free entry
176 afs_dir_Delete(dir_file_t dir, char *entry)
180 struct DirBuffer entrybuf, prevbuf;
181 struct DirEntry *firstitem;
182 unsigned short *previtem;
185 code = FindItem(dir, entry, &prevbuf, &entrybuf);
190 firstitem = (struct DirEntry *)entrybuf.data;
191 previtem = (unsigned short *)prevbuf.data;
193 *previtem = firstitem->next;
194 DRelease(&prevbuf, 1);
195 index = DVOffset(&entrybuf) / 32;
196 nitems = afs_dir_NameBlobs(firstitem->name);
197 /* Clear entire DirEntry and any DirXEntry extensions */
198 memset(firstitem, 0, nitems * sizeof(*firstitem));
199 DRelease(&entrybuf, 1);
200 FreeBlobs(dir, index, nitems);
204 /* Find a bunch of contiguous entries; at least nblobs in a row. */
206 FindBlobs(dir_file_t dir, int nblobs)
210 struct DirBuffer headerbuf, pagebuf;
211 struct DirHeader *dhp;
212 struct PageHeader *pp;
215 /* read the dir header in first. */
216 if (DRead(dir, 0, &headerbuf) != 0)
218 dhp = (struct DirHeader *)headerbuf.data;
220 for (i = 0; i < BIGMAXPAGES; i++) {
221 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
222 /* if page could contain enough entries */
223 /* If there are EPP free entries, then the page is not even allocated. */
225 /* this pages exists past the end of the old-style dir */
226 pgcount = ntohs(dhp->header.pgcount);
229 dhp->header.pgcount = htons(pgcount);
231 if (i > pgcount - 1) {
232 /* this page is bigger than last allocated page */
234 dhp->header.pgcount = htons(i + 1);
236 } else if (dhp->alloMap[i] == EPP) {
237 /* Add the page to the directory. */
239 dhp->alloMap[i] = EPP - 1;
240 dhp->header.pgcount = htons(i + 1);
243 /* read the page in. */
244 if (DRead(dir, i, &pagebuf) != 0) {
247 pp = (struct PageHeader *)pagebuf.data;
248 for (j = 0; j <= EPP - nblobs; j++) {
250 for (k = 0; k < nblobs; k++)
251 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
260 /* Here we have the first index in j. We update the allocation maps
261 * and free up any resources we've got allocated. */
263 dhp->alloMap[i] -= nblobs;
264 DRelease(&headerbuf, 1);
265 for (k = 0; k < nblobs; k++)
266 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
267 DRelease(&pagebuf, 1);
270 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
273 /* If we make it here, the directory is full. */
274 DRelease(&headerbuf, 1);
279 AddPage(dir_file_t dir, int pageno)
280 { /* Add a page to a directory. */
282 struct PageHeader *pp;
283 struct DirBuffer pagebuf;
285 /* Get a new buffer labelled dir,pageno */
286 DNew(dir, pageno, &pagebuf);
287 pp = (struct PageHeader *)pagebuf.data;
289 pp->tag = htons(1234);
292 pp->freecount = EPP - 1; /* The first dude is already allocated */
293 pp->freebitmap[0] = 0x01;
294 for (i = 1; i < EPP / 8; i++) /* It's a constant */
295 pp->freebitmap[i] = 0;
296 DRelease(&pagebuf, 1);
299 /* Free a whole bunch of directory entries. */
302 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
306 struct DirBuffer headerbuf, pagehdbuf;
307 struct DirHeader *dhp;
308 struct PageHeader *pp;
309 page = firstblob / EPP;
310 firstblob -= EPP * page; /* convert to page-relative entry */
312 if (DRead(dir, 0, &headerbuf) != 0)
314 dhp = (struct DirHeader *)headerbuf.data;
317 dhp->alloMap[page] += nblobs;
319 DRelease(&headerbuf, 1);
321 if (DRead(dir, page, &pagehdbuf) != 0)
323 pp = (struct PageHeader *)pagehdbuf.data;
325 for (i = 0; i < nblobs; i++)
326 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
328 DRelease(&pagehdbuf, 1);
332 * Format an empty directory properly. Note that the first 13 entries in a
333 * directory header page are allocated, 1 to the page header, 4 to the
334 * allocation map and 8 to the hash table.
337 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
340 struct DirBuffer buffer;
341 struct DirHeader *dhp;
343 DNew(dir, 0, &buffer);
344 dhp = (struct DirHeader *)buffer.data;
346 dhp->header.pgcount = htons(1);
347 dhp->header.tag = htons(1234);
348 dhp->header.freecount = (EPP - DHE - 1);
349 dhp->header.freebitmap[0] = 0xff;
350 dhp->header.freebitmap[1] = 0x1f;
351 for (i = 2; i < EPP / 8; i++)
352 dhp->header.freebitmap[i] = 0;
353 dhp->alloMap[0] = (EPP - DHE - 1);
354 for (i = 1; i < MAXPAGES; i++)
355 dhp->alloMap[i] = EPP;
356 for (i = 0; i < NHASHENT; i++)
357 dhp->hashTable[i] = 0;
358 DRelease(&buffer, 1);
359 afs_dir_Create(dir, ".", me);
360 afs_dir_Create(dir, "..", parent); /* Virtue is its own .. */
364 /* Look up a file name in directory. */
367 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
369 afs_int32 *fid = (afs_int32 *) voidfid;
370 struct DirBuffer firstbuf, prevbuf;
371 struct DirEntry *firstitem;
374 code = FindItem(dir, entry, &prevbuf, &firstbuf);
378 DRelease(&prevbuf, 0);
379 firstitem = (struct DirEntry *)firstbuf.data;
381 fid[1] = ntohl(firstitem->fid.vnode);
382 fid[2] = ntohl(firstitem->fid.vunique);
383 DRelease(&firstbuf, 0);
387 /* Look up a file name in directory. */
390 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
393 afs_int32 *fid = (afs_int32 *) voidfid;
394 struct DirBuffer firstbuf, prevbuf;
395 struct DirEntry *firstitem;
398 code = FindItem(dir, entry, &prevbuf, &firstbuf);
402 DRelease(&prevbuf, 0);
403 firstitem = (struct DirEntry *)firstbuf.data;
405 fid[1] = ntohl(firstitem->fid.vnode);
406 fid[2] = ntohl(firstitem->fid.vunique);
408 *offsetp = DVOffset(&firstbuf);
409 DRelease(&firstbuf, 0);
414 * Enumerate the contents of a directory. Break when hook function
419 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
426 struct DirBuffer headerbuf, entrybuf;
427 struct DirHeader *dhp;
432 if (DRead(dir, 0, &headerbuf) != 0)
434 dhp = (struct DirHeader *)headerbuf.data;
436 for (i = 0; i < NHASHENT; i++) {
437 /* For each hash chain, enumerate everyone on the list. */
438 num = ntohs(dhp->hashTable[i]);
440 while (num != 0 && elements < BIGMAXPAGES * EPP) {
443 /* Walk down the hash table list. */
444 code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
448 ep = (struct DirEntry *)entrybuf.data;
450 DRelease(&entrybuf, 0);
454 num = ntohs(ep->next);
455 code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
456 ntohl(ep->fid.vunique));
457 DRelease(&entrybuf, 0);
464 DRelease(&headerbuf, 0);
469 afs_dir_IsEmpty(dir_file_t dir)
471 /* Enumerate the contents of a directory. */
474 struct DirBuffer headerbuf, entrybuf;
475 struct DirHeader *dhp;
479 if (DRead(dir, 0, &headerbuf) != 0)
481 dhp = (struct DirHeader *)headerbuf.data;
483 for (i = 0; i < NHASHENT; i++) {
484 /* For each hash chain, enumerate everyone on the list. */
485 num = ntohs(dhp->hashTable[i]);
487 while (num != 0 && elements < BIGMAXPAGES * EPP) {
489 /* Walk down the hash table list. */
490 if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
492 ep = (struct DirEntry *)entrybuf.data;
493 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
494 DRelease(&entrybuf, 0);
495 DRelease(&headerbuf, 0);
498 num = ntohs(ep->next);
499 DRelease(&entrybuf, 0);
502 DRelease(&headerbuf, 0);
506 /* Return a pointer to an entry, given its number. Also return the maximum
507 * size of the entry, which is determined by its position within the directory
512 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
513 struct DirBuffer *buffer, afs_size_t *maxlen)
519 memset(buffer, 0, sizeof(struct DirBuffer));
521 code = DRead(dir, blobno >> LEPP, buffer);
525 pos = 32 * (blobno & (EPP - 1));
527 *maxlen = AFS_PAGESIZE - pos - 1;
529 buffer->data = (void *)(((char *)buffer->data) + pos);
534 /* Given an entries number, return a pointer to that entry */
536 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
538 afs_size_t maxlen = 0;
539 return GetBlobWithLimit(dir, blobno, buffer, &maxlen);
542 /* Return an entry, having verified that the name held within the entry
543 * doesn't overflow off the end of the directory page it is contained
548 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
549 struct DirBuffer *outbuf)
551 struct DirEntry *dir;
552 struct DirBuffer buffer;
557 code = GetBlobWithLimit(file, blobno, &buffer, &maxlen);
561 dir = (struct DirEntry *)buffer.data;
563 /* A blob is only valid if the name within it is NULL terminated before
564 * the end of the blob's containing page */
565 for (cp = dir->name; *cp != '\0' && cp < ((char *)dir) + maxlen; cp++);
568 DRelease(&buffer, 0);
577 afs_dir_DirHash(char *string)
579 /* Hash a string to a number between 0 and NHASHENT. */
584 while ((tc = (*string++))) {
588 tval = hval & (NHASHENT - 1);
591 else if (hval >= 1u<<31)
592 tval = NHASHENT - tval;
597 /* Find a directory entry, given its name. This entry returns a pointer
598 * to a locked buffer, and a pointer to a locked buffer (in previtem)
599 * referencing the found item (to aid the delete code). If no entry is
600 * found, however, no items are left locked, and a null pointer is
601 * returned instead. */
604 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
605 struct DirBuffer *itembuf )
608 struct DirBuffer curr, prev;
609 struct DirHeader *dhp;
613 memset(prevbuf, 0, sizeof(struct DirBuffer));
614 memset(itembuf, 0, sizeof(struct DirBuffer));
616 code = DRead(dir, 0, &prev);
619 dhp = (struct DirHeader *)prev.data;
621 i = afs_dir_DirHash(ename);
622 if (dhp->hashTable[i] == 0) {
628 code = afs_dir_GetVerifiedBlob(dir,
629 (u_short) ntohs(dhp->hashTable[i]),
635 prev.data = &(dhp->hashTable[i]);
637 /* Detect circular hash chains. Absolute max size of a directory */
638 while (elements < BIGMAXPAGES * EPP) {
641 /* Look at each entry on the hash chain */
642 tp = (struct DirEntry *)curr.data;
643 if (!strcmp(ename, tp->name)) {
653 prev.data = &(tp->next);
656 /* The end of the line */
661 code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
667 /* If we've reached here, we've hit our loop limit. Something is weird with
668 * the directory; maybe a circular hash chain? */
677 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
678 struct DirBuffer *itembuf)
680 /* Find a directory entry, given the vnode and uniquifier of a object.
681 * This entry returns a pointer to a locked buffer. If no entry is found,
682 * however, no items are left locked, and a null pointer is returned
687 struct DirBuffer curr, header;
688 struct DirHeader *dhp;
692 memset(itembuf, 0, sizeof(struct DirBuffer));
694 code = DRead(dir, 0, &header);
697 dhp = (struct DirHeader *)header.data;
699 for (i=0; i<NHASHENT; i++) {
700 if (dhp->hashTable[i] != 0) {
701 code = afs_dir_GetVerifiedBlob(dir,
702 (u_short)ntohs(dhp->hashTable[i]),
705 DRelease(&header, 0);
709 while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
711 tp = (struct DirEntry *)curr.data;
713 if (vnode == ntohl(tp->fid.vnode)
714 && unique == ntohl(tp->fid.vunique)) {
715 DRelease(&header, 0);
726 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
729 DRelease(&header, 0);
735 DRelease(&header, 0);
740 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
741 char *name, afs_uint32 length)
743 /* Look for the name pointing to given vnode and unique in a directory */
744 struct DirBuffer entrybuf;
745 struct DirEntry *entry;
748 code = FindFid(dir, vnode, unique, &entrybuf);
752 entry = (struct DirEntry *)entrybuf.data;
754 if (strlen(entry->name) >= length)
757 strcpy(name, entry->name);
758 DRelease(&entrybuf, 0);
763 * Change an entry fid.
766 * \param entry The entry name.
767 * \param old_fid The old find in MKFid format (host order).
768 * It can be omitted if you don't need a safety check...
769 * \param new_fid The new find in MKFid format (host order).
772 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
775 struct DirBuffer prevbuf, entrybuf;
776 struct DirEntry *firstitem;
777 struct MKFid *fid_old = (struct MKFid *) old_fid;
778 struct MKFid *fid_new = (struct MKFid *) new_fid;
782 code = FindItem(dir, entry, &prevbuf, &entrybuf);
786 firstitem = (struct DirEntry *)entrybuf.data;
787 DRelease(&prevbuf, 1);
791 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
792 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
794 firstitem->fid.vnode = htonl(fid_new->vnode);
795 firstitem->fid.vunique = htonl(fid_new->vunique);
798 DRelease(&entrybuf, 1);