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.
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
339 * \retval nonzero error code
342 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
345 struct DirBuffer buffer;
346 struct DirHeader *dhp;
349 DNew(dir, 0, &buffer);
350 dhp = (struct DirHeader *)buffer.data;
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);
368 code = afs_dir_Create(dir, "..", parent);
374 /* Look up a file name in directory. */
377 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
379 afs_int32 *fid = (afs_int32 *) voidfid;
380 struct DirBuffer firstbuf, prevbuf;
381 struct DirEntry *firstitem;
384 code = FindItem(dir, entry, &prevbuf, &firstbuf);
388 DRelease(&prevbuf, 0);
389 firstitem = (struct DirEntry *)firstbuf.data;
391 fid[1] = ntohl(firstitem->fid.vnode);
392 fid[2] = ntohl(firstitem->fid.vunique);
393 DRelease(&firstbuf, 0);
397 /* Look up a file name in directory. */
400 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
403 afs_int32 *fid = (afs_int32 *) voidfid;
404 struct DirBuffer firstbuf, prevbuf;
405 struct DirEntry *firstitem;
408 code = FindItem(dir, entry, &prevbuf, &firstbuf);
412 DRelease(&prevbuf, 0);
413 firstitem = (struct DirEntry *)firstbuf.data;
415 fid[1] = ntohl(firstitem->fid.vnode);
416 fid[2] = ntohl(firstitem->fid.vunique);
418 *offsetp = DVOffset(&firstbuf);
419 DRelease(&firstbuf, 0);
424 * Enumerate the contents of a directory. Break when hook function
429 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
436 struct DirBuffer headerbuf, entrybuf;
437 struct DirHeader *dhp;
442 if (DRead(dir, 0, &headerbuf) != 0)
444 dhp = (struct DirHeader *)headerbuf.data;
446 for (i = 0; i < NHASHENT; i++) {
447 /* For each hash chain, enumerate everyone on the list. */
448 num = ntohs(dhp->hashTable[i]);
450 while (num != 0 && elements < BIGMAXPAGES * EPP) {
453 /* Walk down the hash table list. */
454 code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
458 ep = (struct DirEntry *)entrybuf.data;
460 DRelease(&entrybuf, 0);
464 num = ntohs(ep->next);
465 code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
466 ntohl(ep->fid.vunique));
467 DRelease(&entrybuf, 0);
474 DRelease(&headerbuf, 0);
479 afs_dir_IsEmpty(dir_file_t dir)
481 /* Enumerate the contents of a directory. */
484 struct DirBuffer headerbuf, entrybuf;
485 struct DirHeader *dhp;
489 if (DRead(dir, 0, &headerbuf) != 0)
491 dhp = (struct DirHeader *)headerbuf.data;
493 for (i = 0; i < NHASHENT; i++) {
494 /* For each hash chain, enumerate everyone on the list. */
495 num = ntohs(dhp->hashTable[i]);
497 while (num != 0 && elements < BIGMAXPAGES * EPP) {
499 /* Walk down the hash table list. */
500 if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
502 ep = (struct DirEntry *)entrybuf.data;
503 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
504 DRelease(&entrybuf, 0);
505 DRelease(&headerbuf, 0);
508 num = ntohs(ep->next);
509 DRelease(&entrybuf, 0);
512 DRelease(&headerbuf, 0);
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
520 * If physerr is supplied by caller, it will be set to:
521 * 0 for logical errors
522 * errno for physical errors
525 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
526 struct DirBuffer *buffer, afs_size_t *maxlen, int *physerr)
532 memset(buffer, 0, sizeof(struct DirBuffer));
534 code = DReadWithErrno(dir, blobno >> LEPP, buffer, physerr);
538 pos = 32 * (blobno & (EPP - 1));
540 *maxlen = AFS_PAGESIZE - pos - 1;
542 buffer->data = (void *)(((char *)buffer->data) + pos);
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
554 afs_dir_GetBlobWithErrno(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer,
557 afs_size_t maxlen = 0;
558 return GetBlobWithLimit(dir, blobno, buffer, &maxlen, physerr);
561 /* Given an entries number, return a pointer to that entry */
563 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
565 afs_size_t maxlen = 0;
566 return GetBlobWithLimit(dir, blobno, buffer, &maxlen, NULL);
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
575 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
576 struct DirBuffer *outbuf)
578 struct DirEntry *dir;
579 struct DirBuffer buffer;
584 code = GetBlobWithLimit(file, blobno, &buffer, &maxlen, NULL);
588 dir = (struct DirEntry *)buffer.data;
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++);
595 DRelease(&buffer, 0);
604 afs_dir_DirHash(char *string)
606 /* Hash a string to a number between 0 and NHASHENT. */
611 while ((tc = (*string++))) {
615 tval = hval & (NHASHENT - 1);
618 else if (hval >= 1u<<31)
619 tval = NHASHENT - tval;
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. */
631 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
632 struct DirBuffer *itembuf )
635 struct DirBuffer curr, prev;
636 struct DirHeader *dhp;
640 memset(prevbuf, 0, sizeof(struct DirBuffer));
641 memset(itembuf, 0, sizeof(struct DirBuffer));
643 code = DRead(dir, 0, &prev);
646 dhp = (struct DirHeader *)prev.data;
648 i = afs_dir_DirHash(ename);
649 if (dhp->hashTable[i] == 0) {
655 code = afs_dir_GetVerifiedBlob(dir,
656 (u_short) ntohs(dhp->hashTable[i]),
662 prev.data = &(dhp->hashTable[i]);
664 /* Detect circular hash chains. Absolute max size of a directory */
665 while (elements < BIGMAXPAGES * EPP) {
668 /* Look at each entry on the hash chain */
669 tp = (struct DirEntry *)curr.data;
670 if (!strcmp(ename, tp->name)) {
680 prev.data = &(tp->next);
683 /* The end of the line */
688 code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
694 /* If we've reached here, we've hit our loop limit. Something is weird with
695 * the directory; maybe a circular hash chain? */
704 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
705 struct DirBuffer *itembuf)
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
714 struct DirBuffer curr, header;
715 struct DirHeader *dhp;
719 memset(itembuf, 0, sizeof(struct DirBuffer));
721 code = DRead(dir, 0, &header);
724 dhp = (struct DirHeader *)header.data;
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]),
732 DRelease(&header, 0);
736 while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
738 tp = (struct DirEntry *)curr.data;
740 if (vnode == ntohl(tp->fid.vnode)
741 && unique == ntohl(tp->fid.vunique)) {
742 DRelease(&header, 0);
753 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
756 DRelease(&header, 0);
762 DRelease(&header, 0);
767 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
768 char *name, afs_uint32 length)
770 /* Look for the name pointing to given vnode and unique in a directory */
771 struct DirBuffer entrybuf;
772 struct DirEntry *entry;
775 code = FindFid(dir, vnode, unique, &entrybuf);
779 entry = (struct DirEntry *)entrybuf.data;
781 if (strlen(entry->name) >= length)
784 strcpy(name, entry->name);
785 DRelease(&entrybuf, 0);
790 * Change an entry fid.
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).
799 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
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;
809 code = FindItem(dir, entry, &prevbuf, &entrybuf);
813 firstitem = (struct DirEntry *)entrybuf.data;
814 DRelease(&prevbuf, 1);
818 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
819 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
821 firstitem->fid.vnode = htonl(fid_new->vnode);
822 firstitem->fid.vunique = htonl(fid_new->vunique);
825 DRelease(&entrybuf, 1);