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)
20 # include "h/sysmacros.h"
21 # include "h/signal.h"
25 # if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
28 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
29 # include "h/kernel.h"
32 # if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
33 # include "afs/sysincludes.h"
35 # if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_OBSD48_ENV) && !defined(AFS_NBSD_ENV)
37 # endif /* AFS_SGI64_ENV */
40 # include <sys/mount.h>
41 # include <sys/vnode.h>
42 # include <ufs/inode.h>
44 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
47 # ifndef AFS_LINUX20_ENV
48 # include "netinet/in.h"
50 # else /* !defined(UKERNEL) */
51 # include "afs/stds.h"
52 # include "afs/sysincludes.h"
53 # endif /* !defined(UKERNEL) */
56 /* These are needed because afs_prototypes.h is not included here */
59 extern int DRead(struct dcache *adc, int page, struct DirBuffer *);
60 extern int DNew(struct dcache *adc, int page, struct DirBuffer *);
62 # include "afs/afs_osi.h"
66 # ifdef AFS_LINUX20_ENV
67 # include "h/string.h"
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 *,
85 /* Find out how many entries are required to store a name. */
87 afs_dir_NameBlobs(char *name)
91 return 1 + ((i + 15) >> 5);
94 /* Create an entry in a file. Dir is a file representation, while entry is
97 afs_dir_Create(dir_file_t dir, char *entry, void *voidfid)
99 afs_int32 *vfid = (afs_int32 *) voidfid;
102 struct DirBuffer entrybuf, prevbuf, headerbuf;
104 struct DirHeader *dhp;
106 /* check name quality */
110 /* First check if file already exists. */
111 if (FindItem(dir, entry, &prevbuf, &entrybuf) == 0) {
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;
182 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
185 firstitem = (struct DirEntry *)entrybuf.data;
186 previtem = (unsigned short *)prevbuf.data;
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);
197 /* Find a bunch of contiguous entries; at least nblobs in a row. */
199 FindBlobs(dir_file_t dir, int nblobs)
203 struct DirBuffer headerbuf, pagebuf;
204 struct DirHeader *dhp;
205 struct PageHeader *pp;
208 /* read the dir header in first. */
209 if (DRead(dir, 0, &headerbuf) != 0)
211 dhp = (struct DirHeader *)headerbuf.data;
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. */
218 /* this pages exists past the end of the old-style dir */
219 pgcount = ntohs(dhp->header.pgcount);
222 dhp->header.pgcount = htons(pgcount);
224 if (i > pgcount - 1) {
225 /* this page is bigger than last allocated page */
227 dhp->header.pgcount = htons(i + 1);
229 } else if (dhp->alloMap[i] == EPP) {
230 /* Add the page to the directory. */
232 dhp->alloMap[i] = EPP - 1;
233 dhp->header.pgcount = htons(i + 1);
236 /* read the page in. */
237 if (DRead(dir, i, &pagebuf) != 0) {
240 pp = (struct PageHeader *)pagebuf.data;
241 for (j = 0; j <= EPP - nblobs; j++) {
243 for (k = 0; k < nblobs; k++)
244 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
253 /* Here we have the first index in j. We update the allocation maps
254 * and free up any resources we've got allocated. */
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);
263 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
266 /* If we make it here, the directory is full. */
267 DRelease(&headerbuf, 1);
272 AddPage(dir_file_t dir, int pageno)
273 { /* Add a page to a directory. */
275 struct PageHeader *pp;
276 struct DirBuffer pagebuf;
278 /* Get a new buffer labelled dir,pageno */
279 DNew(dir, pageno, &pagebuf);
280 pp = (struct PageHeader *)pagebuf.data;
282 pp->tag = htons(1234);
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);
292 /* Free a whole bunch of directory entries. */
295 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
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 */
305 if (DRead(dir, 0, &headerbuf) != 0)
307 dhp = (struct DirHeader *)headerbuf.data;
310 dhp->alloMap[page] += nblobs;
312 DRelease(&headerbuf, 1);
314 if (DRead(dir, page, &pagehdbuf) != 0)
316 pp = (struct PageHeader *)pagehdbuf.data;
318 for (i = 0; i < nblobs; i++)
319 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
321 DRelease(&pagehdbuf, 1);
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.
330 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
333 struct DirBuffer buffer;
334 struct DirHeader *dhp;
336 DNew(dir, 0, &buffer);
337 dhp = (struct DirHeader *)buffer.data;
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 .. */
357 /* Look up a file name in directory. */
360 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
362 afs_int32 *fid = (afs_int32 *) voidfid;
363 struct DirBuffer firstbuf, prevbuf;
364 struct DirEntry *firstitem;
366 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
368 DRelease(&prevbuf, 0);
369 firstitem = (struct DirEntry *)firstbuf.data;
371 fid[1] = ntohl(firstitem->fid.vnode);
372 fid[2] = ntohl(firstitem->fid.vunique);
373 DRelease(&firstbuf, 0);
377 /* Look up a file name in directory. */
380 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
383 afs_int32 *fid = (afs_int32 *) voidfid;
384 struct DirBuffer firstbuf, prevbuf;
385 struct DirEntry *firstitem;
387 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
389 DRelease(&prevbuf, 0);
390 firstitem = (struct DirEntry *)firstbuf.data;
392 fid[1] = ntohl(firstitem->fid.vnode);
393 fid[2] = ntohl(firstitem->fid.vunique);
395 *offsetp = DVOffset(&firstbuf);
396 DRelease(&firstbuf, 0);
401 * Enumerate the contents of a directory. Break when hook function
406 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
413 struct DirBuffer headerbuf, entrybuf;
414 struct DirHeader *dhp;
419 if (DRead(dir, 0, &headerbuf) != 0)
421 dhp = (struct DirHeader *)headerbuf.data;
423 for (i = 0; i < NHASHENT; i++) {
424 /* For each hash chain, enumerate everyone on the list. */
425 num = ntohs(dhp->hashTable[i]);
427 while (num != 0 && elements < BIGMAXPAGES * EPP) {
430 /* Walk down the hash table list. */
431 code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
435 ep = (struct DirEntry *)entrybuf.data;
439 num = ntohs(ep->next);
440 code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
441 ntohl(ep->fid.vunique));
442 DRelease(&entrybuf, 0);
449 DRelease(&headerbuf, 0);
454 afs_dir_IsEmpty(dir_file_t dir)
456 /* Enumerate the contents of a directory. */
459 struct DirBuffer headerbuf, entrybuf;
460 struct DirHeader *dhp;
464 if (DRead(dir, 0, &headerbuf) != 0)
466 dhp = (struct DirHeader *)headerbuf.data;
468 for (i = 0; i < NHASHENT; i++) {
469 /* For each hash chain, enumerate everyone on the list. */
470 num = ntohs(dhp->hashTable[i]);
472 while (num != 0 && elements < BIGMAXPAGES * EPP) {
474 /* Walk down the hash table list. */
475 if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
477 ep = (struct DirEntry *)entrybuf.data;
478 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
479 DRelease(&entrybuf, 0);
480 DRelease(&headerbuf, 0);
483 num = ntohs(ep->next);
484 DRelease(&entrybuf, 0);
487 DRelease(&headerbuf, 0);
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
497 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
498 struct DirBuffer *buffer, afs_size_t *maxlen)
504 memset(buffer, 0, sizeof(struct DirBuffer));
506 code = DRead(dir, blobno >> LEPP, buffer);
510 pos = 32 * (blobno & (EPP - 1));
512 *maxlen = AFS_PAGESIZE - pos - 1;
514 buffer->data = (void *)(((char *)buffer->data) + pos);
519 /* Given an entries number, return a pointer to that entry */
521 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
523 afs_size_t maxlen = 0;
524 return GetBlobWithLimit(dir, blobno, buffer, &maxlen);
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
533 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
534 struct DirBuffer *outbuf)
536 struct DirEntry *dir;
537 struct DirBuffer buffer;
542 code = GetBlobWithLimit(file, blobno, &buffer, &maxlen);
546 dir = (struct DirEntry *)buffer.data;
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++);
553 DRelease(&buffer, 0);
562 afs_dir_DirHash(char *string)
564 /* Hash a string to a number between 0 and NHASHENT. */
569 while ((tc = (*string++))) {
573 tval = hval & (NHASHENT - 1);
576 else if (hval >= 1<<31)
577 tval = NHASHENT - tval;
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. */
589 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
590 struct DirBuffer *itembuf )
593 struct DirBuffer curr, prev;
594 struct DirHeader *dhp;
598 memset(prevbuf, 0, sizeof(struct DirBuffer));
599 memset(itembuf, 0, sizeof(struct DirBuffer));
601 code = DRead(dir, 0, &prev);
604 dhp = (struct DirHeader *)prev.data;
606 i = afs_dir_DirHash(ename);
607 if (dhp->hashTable[i] == 0) {
613 code = afs_dir_GetVerifiedBlob(dir,
614 (u_short) ntohs(dhp->hashTable[i]),
621 prev.data = &(dhp->hashTable[i]);
623 /* Detect circular hash chains. Absolute max size of a directory */
624 while (elements < BIGMAXPAGES * EPP) {
627 /* Look at each entry on the hash chain */
628 tp = (struct DirEntry *)curr.data;
629 if (!strcmp(ename, tp->name)) {
639 prev.data = &(tp->next);
642 goto out; /* The end of the line */
644 code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
656 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
657 struct DirBuffer *itembuf)
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
666 struct DirBuffer curr, header;
667 struct DirHeader *dhp;
671 memset(itembuf, 0, sizeof(struct DirBuffer));
673 code = DRead(dir, 0, &header);
676 dhp = (struct DirHeader *)header.data;
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]),
684 DRelease(&header, 0);
688 while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
690 tp = (struct DirEntry *)curr.data;
692 if (vnode == ntohl(tp->fid.vnode)
693 && unique == ntohl(tp->fid.vunique)) {
694 DRelease(&header, 0);
705 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
708 DRelease(&header, 0);
714 DRelease(&header, 0);
719 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
720 char *name, afs_uint32 length)
722 /* Look for the name pointing to given vnode and unique in a directory */
723 struct DirBuffer entrybuf;
724 struct DirEntry *entry;
727 if (FindFid(dir, vnode, unique, &entrybuf) != 0)
729 entry = (struct DirEntry *)entrybuf.data;
731 if (strlen(entry->name) >= length)
734 strcpy(name, entry->name);
735 DRelease(&entrybuf, 0);
740 * Change an entry fid.
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).
749 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
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;
758 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
760 firstitem = (struct DirEntry *)entrybuf.data;
761 DRelease(&prevbuf, 1);
765 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
766 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
768 firstitem->fid.vnode = htonl(fid_new->vnode);
769 firstitem->fid.vunique = htonl(fid_new->vunique);
772 DRelease(&entrybuf, 1);