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_SUN56_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"
70 /* generic renaming */
71 # define NameBlobs afs_dir_NameBlobs
72 # define GetBlob afs_dir_GetBlob
73 # define Create afs_dir_Create
74 # define Length afs_dir_Length
75 # define Delete afs_dir_Delete
76 # define MakeDir afs_dir_MakeDir
77 # define Lookup afs_dir_Lookup
78 # define LookupOffset afs_dir_LookupOffset
79 # define EnumerateDir afs_dir_EnumerateDir
80 # define IsEmpty afs_dir_IsEmpty
81 # define InverseLookup afs_dir_InverseLookup
82 # define ChangeFid afs_dir_ChangeFid
92 /* Local static prototypes */
93 static int FindItem(void *dir, char *ename, struct DirBuffer *,
96 /* Find out how many entries are required to store a name. */
101 i = strlen(name) + 1;
102 return 1 + ((i + 15) >> 5);
105 /* Create an entry in a file. Dir is a file representation, while entry is
108 Create(void *dir, char *entry, void *voidfid)
110 afs_int32 *vfid = (afs_int32 *) voidfid;
113 struct DirBuffer entrybuf, prevbuf, headerbuf;
115 struct DirHeader *dhp;
117 /* check name quality */
121 /* First check if file already exists. */
122 if (FindItem(dir, entry, &prevbuf, &entrybuf) == 0) {
123 DRelease(&entrybuf, 0);
124 DRelease(&prevbuf, 0);
125 printf("Exists ...\n");
129 blobs = NameBlobs(entry); /* number of entries required */
130 firstelt = FindBlobs(dir, blobs);
132 return EFBIG; /* directory is full */
134 /* First, we fill in the directory entry. */
135 if (GetBlob(dir, firstelt, &entrybuf) != 0)
137 ep = (struct DirEntry *)entrybuf.data;
140 ep->fid.vnode = htonl(vfid[1]);
141 ep->fid.vunique = htonl(vfid[2]);
142 strcpy(ep->name, entry);
144 /* Now we just have to thread it on the hash table list. */
145 if (DRead(dir, 0, &headerbuf) != 0) {
146 DRelease(&entrybuf, 1);
149 dhp = (struct DirHeader *)headerbuf.data;
152 ep->next = dhp->hashTable[i];
153 dhp->hashTable[i] = htons(firstelt);
154 DRelease(&headerbuf, 1);
155 DRelease(&entrybuf, 1);
163 struct DirBuffer headerbuf;
164 struct DirHeader *dhp;
166 if (DRead(dir, 0, &headerbuf) != 0)
168 dhp = (struct DirHeader *)headerbuf.data;
170 if (dhp->header.pgcount != 0)
171 ctr = ntohs(dhp->header.pgcount);
173 /* old style, count the pages */
175 for (i = 0; i < MAXPAGES; i++)
176 if (dhp->alloMap[i] != EPP)
179 DRelease(&headerbuf, 0);
180 return ctr * AFS_PAGESIZE;
184 Delete(void *dir, char *entry)
186 /* Delete an entry from a directory, including update of all free entry
189 struct DirBuffer entrybuf, prevbuf;
190 struct DirEntry *firstitem;
191 unsigned short *previtem;
193 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
196 firstitem = (struct DirEntry *)entrybuf.data;
197 previtem = (unsigned short *)prevbuf.data;
199 *previtem = firstitem->next;
200 DRelease(&prevbuf, 1);
201 index = DVOffset(&entrybuf) / 32;
202 nitems = NameBlobs(firstitem->name);
203 DRelease(&entrybuf, 0);
204 FreeBlobs(dir, index, nitems);
209 FindBlobs(void *dir, int nblobs)
211 /* Find a bunch of contiguous entries; at least nblobs in a row. */
214 struct DirBuffer headerbuf, pagebuf;
215 struct DirHeader *dhp;
216 struct PageHeader *pp;
219 /* read the dir header in first. */
220 if (DRead(dir, 0, &headerbuf) != 0)
222 dhp = (struct DirHeader *)headerbuf.data;
224 for (i = 0; i < BIGMAXPAGES; i++) {
225 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
226 /* if page could contain enough entries */
227 /* If there are EPP free entries, then the page is not even allocated. */
229 /* this pages exists past the end of the old-style dir */
230 pgcount = ntohs(dhp->header.pgcount);
233 dhp->header.pgcount = htons(pgcount);
235 if (i > pgcount - 1) {
236 /* this page is bigger than last allocated page */
238 dhp->header.pgcount = htons(i + 1);
240 } else if (dhp->alloMap[i] == EPP) {
241 /* Add the page to the directory. */
243 dhp->alloMap[i] = EPP - 1;
244 dhp->header.pgcount = htons(i + 1);
247 /* read the page in. */
248 if (DRead(dir, i, &pagebuf) != 0) {
249 DRelease(&headerbuf, 1);
252 pp = (struct PageHeader *)pagebuf.data;
253 for (j = 0; j <= EPP - nblobs; j++) {
255 for (k = 0; k < nblobs; k++)
256 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
265 /* Here we have the first index in j. We update the allocation maps
266 * and free up any resources we've got allocated. */
268 dhp->alloMap[i] -= nblobs;
269 DRelease(&headerbuf, 1);
270 for (k = 0; k < nblobs; k++)
271 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
272 DRelease(&pagebuf, 1);
275 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
278 /* If we make it here, the directory is full. */
279 DRelease(&headerbuf, 1);
284 AddPage(void *dir, int pageno)
285 { /* Add a page to a directory. */
287 struct PageHeader *pp;
288 struct DirBuffer pagebuf;
290 /* Get a new buffer labelled dir,pageno */
291 DNew(dir, pageno, &pagebuf);
292 pp = (struct PageHeader *)pagebuf.data;
294 pp->tag = htons(1234);
297 pp->freecount = EPP - 1; /* The first dude is already allocated */
298 pp->freebitmap[0] = 0x01;
299 for (i = 1; i < EPP / 8; i++) /* It's a constant */
300 pp->freebitmap[i] = 0;
301 DRelease(&pagebuf, 1);
304 /* Free a whole bunch of directory entries. */
307 FreeBlobs(void *dir, int firstblob, int nblobs)
311 struct DirBuffer headerbuf, pagehdbuf;
312 struct DirHeader *dhp;
313 struct PageHeader *pp;
314 page = firstblob / EPP;
315 firstblob -= EPP * page; /* convert to page-relative entry */
317 if (DRead(dir, 0, &headerbuf) != 0)
319 dhp = (struct DirHeader *)headerbuf.data;
322 dhp->alloMap[page] += nblobs;
324 DRelease(&headerbuf, 1);
326 if (DRead(dir, page, &pagehdbuf) != 0)
328 pp = (struct PageHeader *)pagehdbuf.data;
330 for (i = 0; i < nblobs; i++)
331 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
333 DRelease(&pagehdbuf, 1);
337 * Format an empty directory properly. Note that the first 13 entries in a
338 * directory header page are allocated, 1 to the page header, 4 to the
339 * allocation map and 8 to the hash table.
342 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
345 struct DirBuffer buffer;
346 struct DirHeader *dhp;
348 DNew(dir, 0, &buffer);
349 dhp = (struct DirHeader *)buffer.data;
351 dhp->header.pgcount = htons(1);
352 dhp->header.tag = htons(1234);
353 dhp->header.freecount = (EPP - DHE - 1);
354 dhp->header.freebitmap[0] = 0xff;
355 dhp->header.freebitmap[1] = 0x1f;
356 for (i = 2; i < EPP / 8; i++)
357 dhp->header.freebitmap[i] = 0;
358 dhp->alloMap[0] = (EPP - DHE - 1);
359 for (i = 1; i < MAXPAGES; i++)
360 dhp->alloMap[i] = EPP;
361 for (i = 0; i < NHASHENT; i++)
362 dhp->hashTable[i] = 0;
363 DRelease(&buffer, 1);
364 Create(dir, ".", me);
365 Create(dir, "..", parent); /* Virtue is its own .. */
369 /* Look up a file name in directory. */
372 Lookup(void *dir, char *entry, void *voidfid)
374 afs_int32 *fid = (afs_int32 *) voidfid;
375 struct DirBuffer firstbuf, prevbuf;
376 struct DirEntry *firstitem;
378 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
380 DRelease(&prevbuf, 0);
381 firstitem = (struct DirEntry *)firstbuf.data;
383 fid[1] = ntohl(firstitem->fid.vnode);
384 fid[2] = ntohl(firstitem->fid.vunique);
385 DRelease(&firstbuf, 0);
389 /* Look up a file name in directory. */
392 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
394 afs_int32 *fid = (afs_int32 *) voidfid;
395 struct DirBuffer firstbuf, prevbuf;
396 struct DirEntry *firstitem;
398 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
400 DRelease(&prevbuf, 0);
401 firstitem = (struct DirEntry *)firstbuf.data;
403 fid[1] = ntohl(firstitem->fid.vnode);
404 fid[2] = ntohl(firstitem->fid.vunique);
406 *offsetp = DVOffset(&firstbuf);
407 DRelease(&firstbuf, 0);
412 * Enumerate the contents of a directory. Break when hook function
417 EnumerateDir(void *dir, int (*hookproc) (void *dir, char *name,
418 afs_int32 vnode, afs_int32 unique),
423 struct DirBuffer headerbuf, entrybuf;
424 struct DirHeader *dhp;
428 if (DRead(dir, 0, &headerbuf) != 0)
430 dhp = (struct DirHeader *)headerbuf.data;
432 for (i = 0; i < NHASHENT; i++) {
433 /* For each hash chain, enumerate everyone on the list. */
434 num = ntohs(dhp->hashTable[i]);
436 /* Walk down the hash table list. */
438 if (GetBlob(dir, num, &entrybuf) != 0) {
440 /* we failed, return why */
441 DRelease(&headerbuf, 0);
446 ep = (struct DirEntry *)entrybuf.data;
448 num = ntohs(ep->next);
449 code = (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
450 ntohl(ep->fid.vunique));
451 DRelease(&entrybuf, 0);
456 DRelease(&headerbuf, 0);
463 /* Enumerate the contents of a directory. */
466 struct DirBuffer headerbuf, entrybuf;
467 struct DirHeader *dhp;
470 if (DRead(dir, 0, &headerbuf) != 0)
472 dhp = (struct DirHeader *)headerbuf.data;
474 for (i = 0; i < NHASHENT; i++) {
475 /* For each hash chain, enumerate everyone on the list. */
476 num = ntohs(dhp->hashTable[i]);
478 /* Walk down the hash table list. */
479 if (GetBlob(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);
496 GetBlob(void *dir, afs_int32 blobno, struct DirBuffer *buffer)
500 memset(buffer, 0, sizeof(struct DirBuffer));
502 code = DRead(dir, blobno >> LEPP, buffer);
506 buffer->data = (void *)(((long)buffer->data) + 32 * (blobno & (EPP - 1)));
512 DirHash(char *string)
514 /* Hash a string to a number between 0 and NHASHENT. */
519 while ((tc = (*string++))) {
523 tval = hval & (NHASHENT - 1);
526 else if (hval >= 1<<31)
527 tval = NHASHENT - tval;
532 /* Find a directory entry, given its name. This entry returns a pointer
533 * to a locked buffer, and a pointer to a locked buffer (in previtem)
534 * referencing the found item (to aid the delete code). If no entry is
535 * found, however, no items are left locked, and a null pointer is
536 * returned instead. */
539 FindItem(void *dir, char *ename, struct DirBuffer *prevbuf,
540 struct DirBuffer *itembuf )
543 struct DirBuffer curr, prev;
544 struct DirHeader *dhp;
547 memset(prevbuf, 0, sizeof(struct DirBuffer));
548 memset(itembuf, 0, sizeof(struct DirBuffer));
550 code = DRead(dir, 0, &prev);
553 dhp = (struct DirHeader *)prev.data;
556 if (dhp->hashTable[i] == 0) {
562 code = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]), &curr);
568 prev.data = &(dhp->hashTable[i]);
571 /* Look at each entry on the hash chain */
572 tp = (struct DirEntry *)curr.data;
573 if (!strcmp(ename, tp->name)) {
581 /* The end of the line */
587 prev.data = &(tp->next);
589 code = GetBlob(dir, (u_short) ntohs(tp->next), &curr);
599 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
600 struct DirBuffer *itembuf)
602 /* Find a directory entry, given the vnode and uniquifier of a object.
603 * This entry returns a pointer to a locked buffer. If no entry is found,
604 * however, no items are left locked, and a null pointer is returned
609 struct DirBuffer curr, header;
610 struct DirHeader *dhp;
613 memset(itembuf, 0, sizeof(struct DirBuffer));
615 code = DRead(dir, 0, &header);
618 dhp = (struct DirHeader *)header.data;
620 for (i=0; i<NHASHENT; i++) {
621 if (dhp->hashTable[i] != 0) {
622 code = GetBlob(dir, (u_short)ntohs(dhp->hashTable[i]),
625 DRelease(&header, 0);
629 while (curr.data != NULL) {
630 tp = (struct DirEntry *)curr.data;
632 if (vnode == ntohl(tp->fid.vnode)
633 && unique == ntohl(tp->fid.vunique)) {
634 DRelease(&header, 0);
645 code = GetBlob(dir, (u_short)ntohs(next), &curr);
647 DRelease(&header, 0);
653 DRelease(&header, 0);
658 InverseLookup (void *dir, afs_uint32 vnode, afs_uint32 unique, char *name,
661 /* Look for the name pointing to given vnode and unique in a directory */
662 struct DirBuffer entrybuf;
663 struct DirEntry *entry;
666 if (FindFid(dir, vnode, unique, &entrybuf) != 0)
668 entry = (struct DirEntry *)entrybuf.data;
670 if (strlen(entry->name) >= length)
673 strcpy(name, entry->name);
674 DRelease(&entrybuf, 0);
679 * Change an entry fid.
682 * \param entry The entry name.
683 * \param old_fid The old find in MKFid format (host order).
684 * It can be omitted if you don't need a safety check...
685 * \param new_fid The new find in MKFid format (host order).
687 int ChangeFid(void *dir,
692 struct DirBuffer prevbuf, entrybuf;
693 struct DirEntry *firstitem;
694 struct MKFid *fid_old = (struct MKFid *) old_fid;
695 struct MKFid *fid_new = (struct MKFid *) new_fid;
698 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
700 firstitem = (struct DirEntry *)entrybuf.data;
701 DRelease(&prevbuf, 1);
705 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
706 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
708 firstitem->fid.vnode = htonl(fid_new->vnode);
709 firstitem->fid.vunique = htonl(fid_new->vunique);
712 DRelease(&entrybuf, 1);