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 */
42 # include <sys/mount.h>
43 # include <sys/vnode.h>
44 # include <ufs/inode.h>
46 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
49 # ifndef AFS_LINUX20_ENV
50 # include "netinet/in.h"
52 # else /* !defined(UKERNEL) */
53 # include "afs/stds.h"
54 # include "afs/sysincludes.h"
55 # endif /* !defined(UKERNEL) */
58 /* These are needed because afs_prototypes.h is not included here */
61 extern int DRead(struct dcache *adc, int page, struct DirBuffer *);
62 extern int DNew(struct dcache *adc, int page, struct DirBuffer *);
64 # include "afs/afs_osi.h"
68 # ifdef AFS_LINUX20_ENV
69 # include "h/string.h"
80 /* Local static prototypes */
81 static int FindBlobs(dir_file_t, int);
82 static void AddPage(dir_file_t, int);
83 static void FreeBlobs(dir_file_t, int, int);
84 static int FindItem(dir_file_t, char *, struct DirBuffer *,
87 /* Find out how many entries are required to store a name. */
89 afs_dir_NameBlobs(char *name)
93 return 1 + ((i + 15) >> 5);
96 /* Create an entry in a file. Dir is a file representation, while entry is
99 afs_dir_Create(dir_file_t dir, char *entry, void *voidfid)
101 afs_int32 *vfid = (afs_int32 *) voidfid;
104 struct DirBuffer entrybuf, prevbuf, headerbuf;
106 struct DirHeader *dhp;
108 /* check name quality */
112 /* First check if file already exists. */
113 if (FindItem(dir, entry, &prevbuf, &entrybuf) == 0) {
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;
184 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
187 firstitem = (struct DirEntry *)entrybuf.data;
188 previtem = (unsigned short *)prevbuf.data;
190 *previtem = firstitem->next;
191 DRelease(&prevbuf, 1);
192 index = DVOffset(&entrybuf) / 32;
193 nitems = afs_dir_NameBlobs(firstitem->name);
194 DRelease(&entrybuf, 0);
195 FreeBlobs(dir, index, nitems);
199 /* Find a bunch of contiguous entries; at least nblobs in a row. */
201 FindBlobs(dir_file_t dir, int nblobs)
205 struct DirBuffer headerbuf, pagebuf;
206 struct DirHeader *dhp;
207 struct PageHeader *pp;
210 /* read the dir header in first. */
211 if (DRead(dir, 0, &headerbuf) != 0)
213 dhp = (struct DirHeader *)headerbuf.data;
215 for (i = 0; i < BIGMAXPAGES; i++) {
216 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
217 /* if page could contain enough entries */
218 /* If there are EPP free entries, then the page is not even allocated. */
220 /* this pages exists past the end of the old-style dir */
221 pgcount = ntohs(dhp->header.pgcount);
224 dhp->header.pgcount = htons(pgcount);
226 if (i > pgcount - 1) {
227 /* this page is bigger than last allocated page */
229 dhp->header.pgcount = htons(i + 1);
231 } else if (dhp->alloMap[i] == EPP) {
232 /* Add the page to the directory. */
234 dhp->alloMap[i] = EPP - 1;
235 dhp->header.pgcount = htons(i + 1);
238 /* read the page in. */
239 if (DRead(dir, i, &pagebuf) != 0) {
242 pp = (struct PageHeader *)pagebuf.data;
243 for (j = 0; j <= EPP - nblobs; j++) {
245 for (k = 0; k < nblobs; k++)
246 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
255 /* Here we have the first index in j. We update the allocation maps
256 * and free up any resources we've got allocated. */
258 dhp->alloMap[i] -= nblobs;
259 DRelease(&headerbuf, 1);
260 for (k = 0; k < nblobs; k++)
261 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
262 DRelease(&pagebuf, 1);
265 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
268 /* If we make it here, the directory is full. */
269 DRelease(&headerbuf, 1);
274 AddPage(dir_file_t dir, int pageno)
275 { /* Add a page to a directory. */
277 struct PageHeader *pp;
278 struct DirBuffer pagebuf;
280 /* Get a new buffer labelled dir,pageno */
281 DNew(dir, pageno, &pagebuf);
282 pp = (struct PageHeader *)pagebuf.data;
284 pp->tag = htons(1234);
287 pp->freecount = EPP - 1; /* The first dude is already allocated */
288 pp->freebitmap[0] = 0x01;
289 for (i = 1; i < EPP / 8; i++) /* It's a constant */
290 pp->freebitmap[i] = 0;
291 DRelease(&pagebuf, 1);
294 /* Free a whole bunch of directory entries. */
297 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
301 struct DirBuffer headerbuf, pagehdbuf;
302 struct DirHeader *dhp;
303 struct PageHeader *pp;
304 page = firstblob / EPP;
305 firstblob -= EPP * page; /* convert to page-relative entry */
307 if (DRead(dir, 0, &headerbuf) != 0)
309 dhp = (struct DirHeader *)headerbuf.data;
312 dhp->alloMap[page] += nblobs;
314 DRelease(&headerbuf, 1);
316 if (DRead(dir, page, &pagehdbuf) != 0)
318 pp = (struct PageHeader *)pagehdbuf.data;
320 for (i = 0; i < nblobs; i++)
321 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
323 DRelease(&pagehdbuf, 1);
327 * Format an empty directory properly. Note that the first 13 entries in a
328 * directory header page are allocated, 1 to the page header, 4 to the
329 * allocation map and 8 to the hash table.
332 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
335 struct DirBuffer buffer;
336 struct DirHeader *dhp;
338 DNew(dir, 0, &buffer);
339 dhp = (struct DirHeader *)buffer.data;
341 dhp->header.pgcount = htons(1);
342 dhp->header.tag = htons(1234);
343 dhp->header.freecount = (EPP - DHE - 1);
344 dhp->header.freebitmap[0] = 0xff;
345 dhp->header.freebitmap[1] = 0x1f;
346 for (i = 2; i < EPP / 8; i++)
347 dhp->header.freebitmap[i] = 0;
348 dhp->alloMap[0] = (EPP - DHE - 1);
349 for (i = 1; i < MAXPAGES; i++)
350 dhp->alloMap[i] = EPP;
351 for (i = 0; i < NHASHENT; i++)
352 dhp->hashTable[i] = 0;
353 DRelease(&buffer, 1);
354 afs_dir_Create(dir, ".", me);
355 afs_dir_Create(dir, "..", parent); /* Virtue is its own .. */
359 /* Look up a file name in directory. */
362 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
364 afs_int32 *fid = (afs_int32 *) voidfid;
365 struct DirBuffer firstbuf, prevbuf;
366 struct DirEntry *firstitem;
368 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
370 DRelease(&prevbuf, 0);
371 firstitem = (struct DirEntry *)firstbuf.data;
373 fid[1] = ntohl(firstitem->fid.vnode);
374 fid[2] = ntohl(firstitem->fid.vunique);
375 DRelease(&firstbuf, 0);
379 /* Look up a file name in directory. */
382 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
385 afs_int32 *fid = (afs_int32 *) voidfid;
386 struct DirBuffer firstbuf, prevbuf;
387 struct DirEntry *firstitem;
389 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
391 DRelease(&prevbuf, 0);
392 firstitem = (struct DirEntry *)firstbuf.data;
394 fid[1] = ntohl(firstitem->fid.vnode);
395 fid[2] = ntohl(firstitem->fid.vunique);
397 *offsetp = DVOffset(&firstbuf);
398 DRelease(&firstbuf, 0);
403 * Enumerate the contents of a directory. Break when hook function
408 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
415 struct DirBuffer headerbuf, entrybuf;
416 struct DirHeader *dhp;
421 if (DRead(dir, 0, &headerbuf) != 0)
423 dhp = (struct DirHeader *)headerbuf.data;
425 for (i = 0; i < NHASHENT; i++) {
426 /* For each hash chain, enumerate everyone on the list. */
427 num = ntohs(dhp->hashTable[i]);
429 while (num != 0 && elements < BIGMAXPAGES * EPP) {
432 /* Walk down the hash table list. */
433 code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
437 ep = (struct DirEntry *)entrybuf.data;
439 DRelease(&entrybuf, 0);
443 num = ntohs(ep->next);
444 code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
445 ntohl(ep->fid.vunique));
446 DRelease(&entrybuf, 0);
453 DRelease(&headerbuf, 0);
458 afs_dir_IsEmpty(dir_file_t dir)
460 /* Enumerate the contents of a directory. */
463 struct DirBuffer headerbuf, entrybuf;
464 struct DirHeader *dhp;
468 if (DRead(dir, 0, &headerbuf) != 0)
470 dhp = (struct DirHeader *)headerbuf.data;
472 for (i = 0; i < NHASHENT; i++) {
473 /* For each hash chain, enumerate everyone on the list. */
474 num = ntohs(dhp->hashTable[i]);
476 while (num != 0 && elements < BIGMAXPAGES * EPP) {
478 /* Walk down the hash table list. */
479 if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
481 ep = (struct DirEntry *)entrybuf.data;
482 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
483 DRelease(&entrybuf, 0);
484 DRelease(&headerbuf, 0);
487 num = ntohs(ep->next);
488 DRelease(&entrybuf, 0);
491 DRelease(&headerbuf, 0);
495 /* Return a pointer to an entry, given its number. Also return the maximum
496 * size of the entry, which is determined by its position within the directory
501 GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
502 struct DirBuffer *buffer, afs_size_t *maxlen)
508 memset(buffer, 0, sizeof(struct DirBuffer));
510 code = DRead(dir, blobno >> LEPP, buffer);
514 pos = 32 * (blobno & (EPP - 1));
516 *maxlen = AFS_PAGESIZE - pos - 1;
518 buffer->data = (void *)(((char *)buffer->data) + pos);
523 /* Given an entries number, return a pointer to that entry */
525 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
527 afs_size_t maxlen = 0;
528 return GetBlobWithLimit(dir, blobno, buffer, &maxlen);
531 /* Return an entry, having verified that the name held within the entry
532 * doesn't overflow off the end of the directory page it is contained
537 afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
538 struct DirBuffer *outbuf)
540 struct DirEntry *dir;
541 struct DirBuffer buffer;
546 code = GetBlobWithLimit(file, blobno, &buffer, &maxlen);
550 dir = (struct DirEntry *)buffer.data;
552 /* A blob is only valid if the name within it is NULL terminated before
553 * the end of the blob's containing page */
554 for (cp = dir->name; *cp != '\0' && cp < ((char *)dir) + maxlen; cp++);
557 DRelease(&buffer, 0);
566 afs_dir_DirHash(char *string)
568 /* Hash a string to a number between 0 and NHASHENT. */
573 while ((tc = (*string++))) {
577 tval = hval & (NHASHENT - 1);
580 else if (hval >= 1<<31)
581 tval = NHASHENT - tval;
586 /* Find a directory entry, given its name. This entry returns a pointer
587 * to a locked buffer, and a pointer to a locked buffer (in previtem)
588 * referencing the found item (to aid the delete code). If no entry is
589 * found, however, no items are left locked, and a null pointer is
590 * returned instead. */
593 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
594 struct DirBuffer *itembuf )
597 struct DirBuffer curr, prev;
598 struct DirHeader *dhp;
602 memset(prevbuf, 0, sizeof(struct DirBuffer));
603 memset(itembuf, 0, sizeof(struct DirBuffer));
605 code = DRead(dir, 0, &prev);
608 dhp = (struct DirHeader *)prev.data;
610 i = afs_dir_DirHash(ename);
611 if (dhp->hashTable[i] == 0) {
617 code = afs_dir_GetVerifiedBlob(dir,
618 (u_short) ntohs(dhp->hashTable[i]),
625 prev.data = &(dhp->hashTable[i]);
627 /* Detect circular hash chains. Absolute max size of a directory */
628 while (elements < BIGMAXPAGES * EPP) {
631 /* Look at each entry on the hash chain */
632 tp = (struct DirEntry *)curr.data;
633 if (!strcmp(ename, tp->name)) {
643 prev.data = &(tp->next);
646 goto out; /* The end of the line */
648 code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
660 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
661 struct DirBuffer *itembuf)
663 /* Find a directory entry, given the vnode and uniquifier of a object.
664 * This entry returns a pointer to a locked buffer. If no entry is found,
665 * however, no items are left locked, and a null pointer is returned
670 struct DirBuffer curr, header;
671 struct DirHeader *dhp;
675 memset(itembuf, 0, sizeof(struct DirBuffer));
677 code = DRead(dir, 0, &header);
680 dhp = (struct DirHeader *)header.data;
682 for (i=0; i<NHASHENT; i++) {
683 if (dhp->hashTable[i] != 0) {
684 code = afs_dir_GetVerifiedBlob(dir,
685 (u_short)ntohs(dhp->hashTable[i]),
688 DRelease(&header, 0);
692 while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
694 tp = (struct DirEntry *)curr.data;
696 if (vnode == ntohl(tp->fid.vnode)
697 && unique == ntohl(tp->fid.vunique)) {
698 DRelease(&header, 0);
709 code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
712 DRelease(&header, 0);
718 DRelease(&header, 0);
723 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
724 char *name, afs_uint32 length)
726 /* Look for the name pointing to given vnode and unique in a directory */
727 struct DirBuffer entrybuf;
728 struct DirEntry *entry;
731 if (FindFid(dir, vnode, unique, &entrybuf) != 0)
733 entry = (struct DirEntry *)entrybuf.data;
735 if (strlen(entry->name) >= length)
738 strcpy(name, entry->name);
739 DRelease(&entrybuf, 0);
744 * Change an entry fid.
747 * \param entry The entry name.
748 * \param old_fid The old find in MKFid format (host order).
749 * It can be omitted if you don't need a safety check...
750 * \param new_fid The new find in MKFid format (host order).
753 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
756 struct DirBuffer prevbuf, entrybuf;
757 struct DirEntry *firstitem;
758 struct MKFid *fid_old = (struct MKFid *) old_fid;
759 struct MKFid *fid_new = (struct MKFid *) new_fid;
762 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
764 firstitem = (struct DirEntry *)entrybuf.data;
765 DRelease(&prevbuf, 1);
769 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
770 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
772 firstitem->fid.vnode = htonl(fid_new->vnode);
773 firstitem->fid.vunique = htonl(fid_new->vunique);
776 DRelease(&entrybuf, 1);