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) {
238 DRelease(&headerbuf, 1);
241 pp = (struct PageHeader *)pagebuf.data;
242 for (j = 0; j <= EPP - nblobs; j++) {
244 for (k = 0; k < nblobs; k++)
245 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
254 /* Here we have the first index in j. We update the allocation maps
255 * and free up any resources we've got allocated. */
257 dhp->alloMap[i] -= nblobs;
258 DRelease(&headerbuf, 1);
259 for (k = 0; k < nblobs; k++)
260 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
261 DRelease(&pagebuf, 1);
264 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
267 /* If we make it here, the directory is full. */
268 DRelease(&headerbuf, 1);
273 AddPage(dir_file_t dir, int pageno)
274 { /* Add a page to a directory. */
276 struct PageHeader *pp;
277 struct DirBuffer pagebuf;
279 /* Get a new buffer labelled dir,pageno */
280 DNew(dir, pageno, &pagebuf);
281 pp = (struct PageHeader *)pagebuf.data;
283 pp->tag = htons(1234);
286 pp->freecount = EPP - 1; /* The first dude is already allocated */
287 pp->freebitmap[0] = 0x01;
288 for (i = 1; i < EPP / 8; i++) /* It's a constant */
289 pp->freebitmap[i] = 0;
290 DRelease(&pagebuf, 1);
293 /* Free a whole bunch of directory entries. */
296 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
300 struct DirBuffer headerbuf, pagehdbuf;
301 struct DirHeader *dhp;
302 struct PageHeader *pp;
303 page = firstblob / EPP;
304 firstblob -= EPP * page; /* convert to page-relative entry */
306 if (DRead(dir, 0, &headerbuf) != 0)
308 dhp = (struct DirHeader *)headerbuf.data;
311 dhp->alloMap[page] += nblobs;
313 DRelease(&headerbuf, 1);
315 if (DRead(dir, page, &pagehdbuf) != 0)
317 pp = (struct PageHeader *)pagehdbuf.data;
319 for (i = 0; i < nblobs; i++)
320 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
322 DRelease(&pagehdbuf, 1);
326 * Format an empty directory properly. Note that the first 13 entries in a
327 * directory header page are allocated, 1 to the page header, 4 to the
328 * allocation map and 8 to the hash table.
331 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
334 struct DirBuffer buffer;
335 struct DirHeader *dhp;
337 DNew(dir, 0, &buffer);
338 dhp = (struct DirHeader *)buffer.data;
340 dhp->header.pgcount = htons(1);
341 dhp->header.tag = htons(1234);
342 dhp->header.freecount = (EPP - DHE - 1);
343 dhp->header.freebitmap[0] = 0xff;
344 dhp->header.freebitmap[1] = 0x1f;
345 for (i = 2; i < EPP / 8; i++)
346 dhp->header.freebitmap[i] = 0;
347 dhp->alloMap[0] = (EPP - DHE - 1);
348 for (i = 1; i < MAXPAGES; i++)
349 dhp->alloMap[i] = EPP;
350 for (i = 0; i < NHASHENT; i++)
351 dhp->hashTable[i] = 0;
352 DRelease(&buffer, 1);
353 afs_dir_Create(dir, ".", me);
354 afs_dir_Create(dir, "..", parent); /* Virtue is its own .. */
358 /* Look up a file name in directory. */
361 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
363 afs_int32 *fid = (afs_int32 *) voidfid;
364 struct DirBuffer firstbuf, prevbuf;
365 struct DirEntry *firstitem;
367 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
369 DRelease(&prevbuf, 0);
370 firstitem = (struct DirEntry *)firstbuf.data;
372 fid[1] = ntohl(firstitem->fid.vnode);
373 fid[2] = ntohl(firstitem->fid.vunique);
374 DRelease(&firstbuf, 0);
378 /* Look up a file name in directory. */
381 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
384 afs_int32 *fid = (afs_int32 *) voidfid;
385 struct DirBuffer firstbuf, prevbuf;
386 struct DirEntry *firstitem;
388 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
390 DRelease(&prevbuf, 0);
391 firstitem = (struct DirEntry *)firstbuf.data;
393 fid[1] = ntohl(firstitem->fid.vnode);
394 fid[2] = ntohl(firstitem->fid.vunique);
396 *offsetp = DVOffset(&firstbuf);
397 DRelease(&firstbuf, 0);
402 * Enumerate the contents of a directory. Break when hook function
407 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
414 struct DirBuffer headerbuf, entrybuf;
415 struct DirHeader *dhp;
420 if (DRead(dir, 0, &headerbuf) != 0)
422 dhp = (struct DirHeader *)headerbuf.data;
424 for (i = 0; i < NHASHENT; i++) {
425 /* For each hash chain, enumerate everyone on the list. */
426 num = ntohs(dhp->hashTable[i]);
428 while (num != 0 && elements < BIGMAXPAGES * EPP) {
431 /* Walk down the hash table list. */
432 code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
436 ep = (struct DirEntry *)entrybuf.data;
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);
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;
545 code = GetBlobWithLimit(file, blobno, &buffer, &maxlen);
549 dir = (struct DirEntry *)buffer.data;
551 /* A blob is only valid if the name within it is NULL terminated before
552 * the end of the blob's containing page */
553 for (cp = dir->name; *cp != '\0' && cp < ((char *)dir) + maxlen; cp++);
556 DRelease(&buffer, 0);
565 afs_dir_DirHash(char *string)
567 /* Hash a string to a number between 0 and NHASHENT. */
572 while ((tc = (*string++))) {
576 tval = hval & (NHASHENT - 1);
579 else if (hval >= 1<<31)
580 tval = NHASHENT - tval;
585 /* Find a directory entry, given its name. This entry returns a pointer
586 * to a locked buffer, and a pointer to a locked buffer (in previtem)
587 * referencing the found item (to aid the delete code). If no entry is
588 * found, however, no items are left locked, and a null pointer is
589 * returned instead. */
592 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
593 struct DirBuffer *itembuf )
596 struct DirBuffer curr, prev;
597 struct DirHeader *dhp;
601 memset(prevbuf, 0, sizeof(struct DirBuffer));
602 memset(itembuf, 0, sizeof(struct DirBuffer));
604 code = DRead(dir, 0, &prev);
607 dhp = (struct DirHeader *)prev.data;
609 i = afs_dir_DirHash(ename);
610 if (dhp->hashTable[i] == 0) {
616 code = afs_dir_GetVerifiedBlob(dir,
617 (u_short) ntohs(dhp->hashTable[i]),
624 prev.data = &(dhp->hashTable[i]);
626 /* Detect circular hash chains. Absolute max size of a directory */
627 while (elements < BIGMAXPAGES * EPP) {
630 /* Look at each entry on the hash chain */
631 tp = (struct DirEntry *)curr.data;
632 if (!strcmp(ename, tp->name)) {
642 prev.data = &(tp->next);
645 goto out; /* The end of the line */
647 code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
659 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
660 struct DirBuffer *itembuf)
662 /* Find a directory entry, given the vnode and uniquifier of a object.
663 * This entry returns a pointer to a locked buffer. If no entry is found,
664 * however, no items are left locked, and a null pointer is returned
669 struct DirBuffer curr, header;
670 struct DirHeader *dhp;
674 memset(itembuf, 0, sizeof(struct DirBuffer));
676 code = DRead(dir, 0, &header);
679 dhp = (struct DirHeader *)header.data;
681 for (i=0; i<NHASHENT; i++) {
682 if (dhp->hashTable[i] != 0) {
683 code = afs_dir_GetVerifiedBlob(dir,
684 (u_short)ntohs(dhp->hashTable[i]),
687 DRelease(&header, 0);
691 while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
693 tp = (struct DirEntry *)curr.data;
695 if (vnode == ntohl(tp->fid.vnode)
696 && unique == ntohl(tp->fid.vunique)) {
697 DRelease(&header, 0);
708 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
711 DRelease(&header, 0);
717 DRelease(&header, 0);
722 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
723 char *name, afs_uint32 length)
725 /* Look for the name pointing to given vnode and unique in a directory */
726 struct DirBuffer entrybuf;
727 struct DirEntry *entry;
730 if (FindFid(dir, vnode, unique, &entrybuf) != 0)
732 entry = (struct DirEntry *)entrybuf.data;
734 if (strlen(entry->name) >= length)
737 strcpy(name, entry->name);
738 DRelease(&entrybuf, 0);
743 * Change an entry fid.
746 * \param entry The entry name.
747 * \param old_fid The old find in MKFid format (host order).
748 * It can be omitted if you don't need a safety check...
749 * \param new_fid The new find in MKFid format (host order).
752 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
755 struct DirBuffer prevbuf, entrybuf;
756 struct DirEntry *firstitem;
757 struct MKFid *fid_old = (struct MKFid *) old_fid;
758 struct MKFid *fid_new = (struct MKFid *) new_fid;
761 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
763 firstitem = (struct DirEntry *)entrybuf.data;
764 DRelease(&prevbuf, 1);
768 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
769 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
771 firstitem->fid.vnode = htonl(fid_new->vnode);
772 firstitem->fid.vunique = htonl(fid_new->vunique);
775 DRelease(&entrybuf, 1);