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;
103 /* check name quality */
107 /* First check if file already exists. */
108 if (FindItem(dir, entry, &prevbuf, &entrybuf) == 0) {
109 DRelease(&entrybuf, 0);
110 DRelease(&prevbuf, 0);
114 blobs = afs_dir_NameBlobs(entry); /* number of entries required */
115 firstelt = FindBlobs(dir, blobs);
117 return EFBIG; /* directory is full */
119 /* First, we fill in the directory entry. */
120 if (afs_dir_GetBlob(dir, firstelt, &entrybuf) != 0)
122 ep = (struct DirEntry *)entrybuf.data;
125 ep->fid.vnode = htonl(vfid[1]);
126 ep->fid.vunique = htonl(vfid[2]);
127 strcpy(ep->name, entry);
129 /* Now we just have to thread it on the hash table list. */
130 if (DRead(dir, 0, &headerbuf) != 0) {
131 DRelease(&entrybuf, 1);
134 dhp = (struct DirHeader *)headerbuf.data;
136 i = afs_dir_DirHash(entry);
137 ep->next = dhp->hashTable[i];
138 dhp->hashTable[i] = htons(firstelt);
139 DRelease(&headerbuf, 1);
140 DRelease(&entrybuf, 1);
145 afs_dir_Length(dir_file_t dir)
148 struct DirBuffer headerbuf;
149 struct DirHeader *dhp;
151 if (DRead(dir, 0, &headerbuf) != 0)
153 dhp = (struct DirHeader *)headerbuf.data;
155 if (dhp->header.pgcount != 0)
156 ctr = ntohs(dhp->header.pgcount);
158 /* old style, count the pages */
160 for (i = 0; i < MAXPAGES; i++)
161 if (dhp->alloMap[i] != EPP)
164 DRelease(&headerbuf, 0);
165 return ctr * AFS_PAGESIZE;
168 /* Delete an entry from a directory, including update of all free entry
171 afs_dir_Delete(dir_file_t dir, char *entry)
175 struct DirBuffer entrybuf, prevbuf;
176 struct DirEntry *firstitem;
177 unsigned short *previtem;
179 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
182 firstitem = (struct DirEntry *)entrybuf.data;
183 previtem = (unsigned short *)prevbuf.data;
185 *previtem = firstitem->next;
186 DRelease(&prevbuf, 1);
187 index = DVOffset(&entrybuf) / 32;
188 nitems = afs_dir_NameBlobs(firstitem->name);
189 /* Clear entire DirEntry and any DirXEntry extensions */
190 memset(firstitem, 0, nitems * sizeof(*firstitem));
191 DRelease(&entrybuf, 1);
192 FreeBlobs(dir, index, nitems);
196 /* Find a bunch of contiguous entries; at least nblobs in a row. */
198 FindBlobs(dir_file_t dir, int nblobs)
202 struct DirBuffer headerbuf, pagebuf;
203 struct DirHeader *dhp;
204 struct PageHeader *pp;
207 /* read the dir header in first. */
208 if (DRead(dir, 0, &headerbuf) != 0)
210 dhp = (struct DirHeader *)headerbuf.data;
212 for (i = 0; i < BIGMAXPAGES; i++) {
213 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
214 /* if page could contain enough entries */
215 /* If there are EPP free entries, then the page is not even allocated. */
217 /* this pages exists past the end of the old-style dir */
218 pgcount = ntohs(dhp->header.pgcount);
221 dhp->header.pgcount = htons(pgcount);
223 if (i > pgcount - 1) {
224 /* this page is bigger than last allocated page */
226 dhp->header.pgcount = htons(i + 1);
228 } else if (dhp->alloMap[i] == EPP) {
229 /* Add the page to the directory. */
231 dhp->alloMap[i] = EPP - 1;
232 dhp->header.pgcount = htons(i + 1);
235 /* read the page in. */
236 if (DRead(dir, i, &pagebuf) != 0) {
239 pp = (struct PageHeader *)pagebuf.data;
240 for (j = 0; j <= EPP - nblobs; j++) {
242 for (k = 0; k < nblobs; k++)
243 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
252 /* Here we have the first index in j. We update the allocation maps
253 * and free up any resources we've got allocated. */
255 dhp->alloMap[i] -= nblobs;
256 DRelease(&headerbuf, 1);
257 for (k = 0; k < nblobs; k++)
258 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
259 DRelease(&pagebuf, 1);
262 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
265 /* If we make it here, the directory is full. */
266 DRelease(&headerbuf, 1);
271 AddPage(dir_file_t dir, int pageno)
272 { /* Add a page to a directory. */
274 struct PageHeader *pp;
275 struct DirBuffer pagebuf;
277 /* Get a new buffer labelled dir,pageno */
278 DNew(dir, pageno, &pagebuf);
279 pp = (struct PageHeader *)pagebuf.data;
281 pp->tag = htons(1234);
284 pp->freecount = EPP - 1; /* The first dude is already allocated */
285 pp->freebitmap[0] = 0x01;
286 for (i = 1; i < EPP / 8; i++) /* It's a constant */
287 pp->freebitmap[i] = 0;
288 DRelease(&pagebuf, 1);
291 /* Free a whole bunch of directory entries. */
294 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
298 struct DirBuffer headerbuf, pagehdbuf;
299 struct DirHeader *dhp;
300 struct PageHeader *pp;
301 page = firstblob / EPP;
302 firstblob -= EPP * page; /* convert to page-relative entry */
304 if (DRead(dir, 0, &headerbuf) != 0)
306 dhp = (struct DirHeader *)headerbuf.data;
309 dhp->alloMap[page] += nblobs;
311 DRelease(&headerbuf, 1);
313 if (DRead(dir, page, &pagehdbuf) != 0)
315 pp = (struct PageHeader *)pagehdbuf.data;
317 for (i = 0; i < nblobs; i++)
318 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
320 DRelease(&pagehdbuf, 1);
324 * Format an empty directory properly. Note that the first 13 entries in a
325 * directory header page are allocated, 1 to the page header, 4 to the
326 * allocation map and 8 to the hash table.
329 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
332 struct DirBuffer buffer;
333 struct DirHeader *dhp;
335 DNew(dir, 0, &buffer);
336 dhp = (struct DirHeader *)buffer.data;
338 dhp->header.pgcount = htons(1);
339 dhp->header.tag = htons(1234);
340 dhp->header.freecount = (EPP - DHE - 1);
341 dhp->header.freebitmap[0] = 0xff;
342 dhp->header.freebitmap[1] = 0x1f;
343 for (i = 2; i < EPP / 8; i++)
344 dhp->header.freebitmap[i] = 0;
345 dhp->alloMap[0] = (EPP - DHE - 1);
346 for (i = 1; i < MAXPAGES; i++)
347 dhp->alloMap[i] = EPP;
348 for (i = 0; i < NHASHENT; i++)
349 dhp->hashTable[i] = 0;
350 DRelease(&buffer, 1);
351 afs_dir_Create(dir, ".", me);
352 afs_dir_Create(dir, "..", parent); /* Virtue is its own .. */
356 /* Look up a file name in directory. */
359 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
361 afs_int32 *fid = (afs_int32 *) voidfid;
362 struct DirBuffer firstbuf, prevbuf;
363 struct DirEntry *firstitem;
365 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
367 DRelease(&prevbuf, 0);
368 firstitem = (struct DirEntry *)firstbuf.data;
370 fid[1] = ntohl(firstitem->fid.vnode);
371 fid[2] = ntohl(firstitem->fid.vunique);
372 DRelease(&firstbuf, 0);
376 /* Look up a file name in directory. */
379 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
382 afs_int32 *fid = (afs_int32 *) voidfid;
383 struct DirBuffer firstbuf, prevbuf;
384 struct DirEntry *firstitem;
386 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
388 DRelease(&prevbuf, 0);
389 firstitem = (struct DirEntry *)firstbuf.data;
391 fid[1] = ntohl(firstitem->fid.vnode);
392 fid[2] = ntohl(firstitem->fid.vunique);
394 *offsetp = DVOffset(&firstbuf);
395 DRelease(&firstbuf, 0);
400 * Enumerate the contents of a directory. Break when hook function
405 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
412 struct DirBuffer headerbuf, entrybuf;
413 struct DirHeader *dhp;
418 if (DRead(dir, 0, &headerbuf) != 0)
420 dhp = (struct DirHeader *)headerbuf.data;
422 for (i = 0; i < NHASHENT; i++) {
423 /* For each hash chain, enumerate everyone on the list. */
424 num = ntohs(dhp->hashTable[i]);
426 while (num != 0 && elements < BIGMAXPAGES * EPP) {
429 /* Walk down the hash table list. */
430 code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
434 ep = (struct DirEntry *)entrybuf.data;
436 DRelease(&entrybuf, 0);
440 num = ntohs(ep->next);
441 code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
442 ntohl(ep->fid.vunique));
443 DRelease(&entrybuf, 0);
450 DRelease(&headerbuf, 0);
455 afs_dir_IsEmpty(dir_file_t dir)
457 /* Enumerate the contents of a directory. */
460 struct DirBuffer headerbuf, entrybuf;
461 struct DirHeader *dhp;
465 if (DRead(dir, 0, &headerbuf) != 0)
467 dhp = (struct DirHeader *)headerbuf.data;
469 for (i = 0; i < NHASHENT; i++) {
470 /* For each hash chain, enumerate everyone on the list. */
471 num = ntohs(dhp->hashTable[i]);
473 while (num != 0 && elements < BIGMAXPAGES * EPP) {
475 /* Walk down the hash table list. */
476 if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
478 ep = (struct DirEntry *)entrybuf.data;
479 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
480 DRelease(&entrybuf, 0);
481 DRelease(&headerbuf, 0);
484 num = ntohs(ep->next);
485 DRelease(&entrybuf, 0);
488 DRelease(&headerbuf, 0);
492 /* Return a pointer to an entry, given its number. Also return the maximum
493 * size of the entry, which is determined by its position within the directory
498 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
499 struct DirBuffer *buffer, afs_size_t *maxlen)
505 memset(buffer, 0, sizeof(struct DirBuffer));
507 code = DRead(dir, blobno >> LEPP, buffer);
511 pos = 32 * (blobno & (EPP - 1));
513 *maxlen = AFS_PAGESIZE - pos - 1;
515 buffer->data = (void *)(((char *)buffer->data) + pos);
520 /* Given an entries number, return a pointer to that entry */
522 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
524 afs_size_t maxlen = 0;
525 return GetBlobWithLimit(dir, blobno, buffer, &maxlen);
528 /* Return an entry, having verified that the name held within the entry
529 * doesn't overflow off the end of the directory page it is contained
534 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
535 struct DirBuffer *outbuf)
537 struct DirEntry *dir;
538 struct DirBuffer buffer;
543 code = GetBlobWithLimit(file, blobno, &buffer, &maxlen);
547 dir = (struct DirEntry *)buffer.data;
549 /* A blob is only valid if the name within it is NULL terminated before
550 * the end of the blob's containing page */
551 for (cp = dir->name; *cp != '\0' && cp < ((char *)dir) + maxlen; cp++);
554 DRelease(&buffer, 0);
563 afs_dir_DirHash(char *string)
565 /* Hash a string to a number between 0 and NHASHENT. */
570 while ((tc = (*string++))) {
574 tval = hval & (NHASHENT - 1);
577 else if (hval >= 1u<<31)
578 tval = NHASHENT - tval;
583 /* Find a directory entry, given its name. This entry returns a pointer
584 * to a locked buffer, and a pointer to a locked buffer (in previtem)
585 * referencing the found item (to aid the delete code). If no entry is
586 * found, however, no items are left locked, and a null pointer is
587 * returned instead. */
590 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
591 struct DirBuffer *itembuf )
594 struct DirBuffer curr, prev;
595 struct DirHeader *dhp;
599 memset(prevbuf, 0, sizeof(struct DirBuffer));
600 memset(itembuf, 0, sizeof(struct DirBuffer));
602 code = DRead(dir, 0, &prev);
605 dhp = (struct DirHeader *)prev.data;
607 i = afs_dir_DirHash(ename);
608 if (dhp->hashTable[i] == 0) {
614 code = afs_dir_GetVerifiedBlob(dir,
615 (u_short) ntohs(dhp->hashTable[i]),
622 prev.data = &(dhp->hashTable[i]);
624 /* Detect circular hash chains. Absolute max size of a directory */
625 while (elements < BIGMAXPAGES * EPP) {
628 /* Look at each entry on the hash chain */
629 tp = (struct DirEntry *)curr.data;
630 if (!strcmp(ename, tp->name)) {
640 prev.data = &(tp->next);
643 goto out; /* The end of the line */
645 code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
657 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
658 struct DirBuffer *itembuf)
660 /* Find a directory entry, given the vnode and uniquifier of a object.
661 * This entry returns a pointer to a locked buffer. If no entry is found,
662 * however, no items are left locked, and a null pointer is returned
667 struct DirBuffer curr, header;
668 struct DirHeader *dhp;
672 memset(itembuf, 0, sizeof(struct DirBuffer));
674 code = DRead(dir, 0, &header);
677 dhp = (struct DirHeader *)header.data;
679 for (i=0; i<NHASHENT; i++) {
680 if (dhp->hashTable[i] != 0) {
681 code = afs_dir_GetVerifiedBlob(dir,
682 (u_short)ntohs(dhp->hashTable[i]),
685 DRelease(&header, 0);
689 while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
691 tp = (struct DirEntry *)curr.data;
693 if (vnode == ntohl(tp->fid.vnode)
694 && unique == ntohl(tp->fid.vunique)) {
695 DRelease(&header, 0);
706 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
709 DRelease(&header, 0);
715 DRelease(&header, 0);
720 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
721 char *name, afs_uint32 length)
723 /* Look for the name pointing to given vnode and unique in a directory */
724 struct DirBuffer entrybuf;
725 struct DirEntry *entry;
728 if (FindFid(dir, vnode, unique, &entrybuf) != 0)
730 entry = (struct DirEntry *)entrybuf.data;
732 if (strlen(entry->name) >= length)
735 strcpy(name, entry->name);
736 DRelease(&entrybuf, 0);
741 * Change an entry fid.
744 * \param entry The entry name.
745 * \param old_fid The old find in MKFid format (host order).
746 * It can be omitted if you don't need a safety check...
747 * \param new_fid The new find in MKFid format (host order).
750 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
753 struct DirBuffer prevbuf, entrybuf;
754 struct DirEntry *firstitem;
755 struct MKFid *fid_old = (struct MKFid *) old_fid;
756 struct MKFid *fid_new = (struct MKFid *) new_fid;
759 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
761 firstitem = (struct DirEntry *)entrybuf.data;
762 DRelease(&prevbuf, 1);
766 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
767 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
769 firstitem->fid.vnode = htonl(fid_new->vnode);
770 firstitem->fid.vunique = htonl(fid_new->vunique);
773 DRelease(&entrybuf, 1);