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>
23 #include "h/sysmacros.h"
28 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
31 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
35 #if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
36 #include "afs/sysincludes.h"
38 #if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN60_ENV)
40 #endif /* AFS_SGI64_ENV */
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 #include <ufs/inode.h>
47 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
50 #ifndef AFS_LINUX20_ENV
51 #include "netinet/in.h"
55 /* These are needed because afs_prototypes.h is not included here */
59 #else /* !defined(UKERNEL) */
61 #include "afs/sysincludes.h"
64 /* These are needed because afs_prototypes.h is not included here */
68 #endif /* !defined(UKERNEL) */
69 #include "afs/afs_osi.h"
73 #include "afs/longc_procs.h"
74 #ifdef AFS_LINUX20_ENV
78 /* generic renaming */
79 #define NameBlobs afs_dir_NameBlobs
80 #define GetBlob afs_dir_GetBlob
81 #define Create afs_dir_Create
82 #define Length afs_dir_Length
83 #define Delete afs_dir_Delete
84 #define MakeDir afs_dir_MakeDir
85 #define Lookup afs_dir_Lookup
86 #define LookupOffset afs_dir_LookupOffset
87 #define EnumerateDir afs_dir_EnumerateDir
88 #define IsEmpty afs_dir_IsEmpty
94 # include <sys/types.h>
100 #include <netinet/in.h>
108 /* Local static prototypes */
109 static struct DirEntry *FindItem(void *dir, char *ename,
110 unsigned short **previtem);
114 NameBlobs(char *name)
115 { /* Find out how many entries are required to store a name. */
117 i = strlen(name) + 1;
118 return 1 + ((i + 15) >> 5);
121 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
124 Create(void *dir, char *entry, void *voidfid)
126 afs_int32 *vfid = (afs_int32 *) voidfid;
129 register struct DirEntry *ep;
130 unsigned short *pp = NULL;
131 register struct DirHeader *dhp;
133 /* check name quality */
136 /* First check if file already exists. */
137 ep = FindItem(dir, entry, &pp);
143 blobs = NameBlobs(entry); /* number of entries required */
144 firstelt = FindBlobs(dir, blobs);
146 return EFBIG; /* directory is full */
147 /* First, we fill in the directory entry. */
148 ep = GetBlob(dir, firstelt);
152 ep->fid.vnode = htonl(vfid[1]);
153 ep->fid.vunique = htonl(vfid[2]);
154 strcpy(ep->name, entry);
155 /* Now we just have to thread it on the hash table list. */
156 dhp = (struct DirHeader *)DRead(dir, 0);
162 ep->next = dhp->hashTable[i];
163 dhp->hashTable[i] = htons(firstelt);
173 struct DirHeader *dhp;
174 dhp = (struct DirHeader *)DRead(dir, 0);
177 if (dhp->header.pgcount != 0)
178 ctr = ntohs(dhp->header.pgcount);
180 /* old style, count the pages */
182 for (i = 0; i < MAXPAGES; i++)
183 if (dhp->alloMap[i] != EPP)
187 return ctr * AFS_PAGESIZE;
191 Delete(void *dir, char *entry)
193 /* Delete an entry from a directory, including update of all free entry descriptors. */
195 register struct DirEntry *firstitem;
196 unsigned short *previtem;
197 firstitem = FindItem(dir, entry, &previtem);
200 *previtem = firstitem->next;
201 DRelease(previtem, 1);
202 index = DVOffset(firstitem) / 32;
203 nitems = NameBlobs(firstitem->name);
204 DRelease(firstitem, 0);
205 FreeBlobs(dir, index, nitems);
210 FindBlobs(void *dir, int nblobs)
212 /* Find a bunch of contiguous entries; at least nblobs in a row. */
213 register int i, j, k;
215 register struct DirHeader *dhp;
216 struct PageHeader *pp;
219 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
222 for (i = 0; i < BIGMAXPAGES; i++) {
223 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
224 /* if page could contain enough entries */
225 /* If there are EPP free entries, then the page is not even allocated. */
227 /* this pages exists past the end of the old-style dir */
228 pgcount = ntohs(dhp->header.pgcount);
231 dhp->header.pgcount = htons(pgcount);
233 if (i > pgcount - 1) {
234 /* this page is bigger than last allocated page */
236 dhp->header.pgcount = htons(i + 1);
238 } else if (dhp->alloMap[i] == EPP) {
239 /* Add the page to the directory. */
241 dhp->alloMap[i] = EPP - 1;
242 dhp->header.pgcount = htons(i + 1);
244 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
249 for (j = 0; j <= EPP - nblobs; j++) {
251 for (k = 0; k < nblobs; k++)
252 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
261 /* Here we have the first index in j. We update the allocation maps
262 * and free up any resources we've got allocated. */
264 dhp->alloMap[i] -= nblobs;
266 for (k = 0; k < nblobs; k++)
267 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
271 DRelease(pp, 0); /* This dir page is unchanged. */
274 /* If we make it here, the directory is full. */
280 AddPage(void *dir, int pageno)
281 { /* Add a page to a directory. */
283 register struct PageHeader *pp;
285 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
286 pp->tag = htons(1234);
289 pp->freecount = EPP - 1; /* The first dude is already allocated */
290 pp->freebitmap[0] = 0x01;
291 for (i = 1; i < EPP / 8; i++) /* It's a constant */
292 pp->freebitmap[i] = 0;
296 /* Free a whole bunch of directory entries. */
299 FreeBlobs(void *dir, register int firstblob, int nblobs)
303 struct DirHeader *dhp;
304 struct PageHeader *pp;
305 page = firstblob / EPP;
306 firstblob -= EPP * page; /* convert to page-relative entry */
307 dhp = (struct DirHeader *)DRead(dir, 0);
311 dhp->alloMap[page] += nblobs;
313 pp = (struct PageHeader *)DRead(dir, page);
315 for (i = 0; i < nblobs; i++)
316 pp->freebitmap[(firstblob + i) >> 3] &=
317 ~(1 << ((firstblob + i) & 7));
322 * Format an empty directory properly. Note that the first 13 entries in a
323 * directory header page are allocated, 1 to the page header, 4 to the
324 * allocation map and 8 to the hash table.
327 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
330 register struct DirHeader *dhp;
331 dhp = (struct DirHeader *)DNew(dir, 0);
332 dhp->header.pgcount = htons(1);
333 dhp->header.tag = htons(1234);
334 dhp->header.freecount = (EPP - DHE - 1);
335 dhp->header.freebitmap[0] = 0xff;
336 dhp->header.freebitmap[1] = 0x1f;
337 for (i = 2; i < EPP / 8; i++)
338 dhp->header.freebitmap[i] = 0;
339 dhp->alloMap[0] = (EPP - DHE - 1);
340 for (i = 1; i < MAXPAGES; i++)
341 dhp->alloMap[i] = EPP;
342 for (i = 0; i < NHASHENT; i++)
343 dhp->hashTable[i] = 0;
345 Create(dir, ".", me);
346 Create(dir, "..", parent); /* Virtue is its own .. */
350 /* Look up a file name in directory. */
353 Lookup(void *dir, char *entry, void *voidfid)
355 afs_int32 *fid = (afs_int32 *) voidfid;
356 register struct DirEntry *firstitem;
357 unsigned short *previtem;
359 firstitem = FindItem(dir, entry, &previtem);
362 DRelease(previtem, 0);
363 fid[1] = ntohl(firstitem->fid.vnode);
364 fid[2] = ntohl(firstitem->fid.vunique);
365 DRelease(firstitem, 0);
369 /* Look up a file name in directory. */
372 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
374 afs_int32 *fid = (afs_int32 *) voidfid;
375 register struct DirEntry *firstitem;
376 unsigned short *previtem;
378 firstitem = FindItem(dir, entry, &previtem);
381 DRelease(previtem, 0);
382 fid[1] = ntohl(firstitem->fid.vnode);
383 fid[2] = ntohl(firstitem->fid.vunique);
385 *offsetp = DVOffset(firstitem);
386 DRelease(firstitem, 0);
391 EnumerateDir(void *dir, int (*hookproc) (), void *hook)
393 /* Enumerate the contents of a directory. */
396 register struct DirHeader *dhp;
397 register struct DirEntry *ep;
399 dhp = (struct DirHeader *)DRead(dir, 0);
401 return EIO; /* first page should be there */
402 for (i = 0; i < NHASHENT; i++) {
403 /* For each hash chain, enumerate everyone on the list. */
404 num = ntohs(dhp->hashTable[i]);
406 /* Walk down the hash table list. */
408 ep = GetBlob(dir, num);
411 /* we failed, return why */
417 num = ntohs(ep->next);
418 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
419 ntohl(ep->fid.vunique));
430 /* Enumerate the contents of a directory. */
433 register struct DirHeader *dhp;
434 register struct DirEntry *ep;
435 dhp = (struct DirHeader *)DRead(dir, 0);
438 for (i = 0; i < NHASHENT; i++) {
439 /* For each hash chain, enumerate everyone on the list. */
440 num = ntohs(dhp->hashTable[i]);
442 /* Walk down the hash table list. */
443 ep = GetBlob(dir, num);
446 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
451 num = ntohs(ep->next);
460 GetBlob(void *dir, afs_int32 blobno)
462 /* Return a pointer to an entry, given its number. */
464 ep = DRead(dir, blobno >> LEPP);
467 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
471 DirHash(register char *string)
473 /* Hash a string to a number between 0 and NHASHENT. */
474 register unsigned char tc;
478 while ((tc = (*string++))) {
482 tval = hval & (NHASHENT - 1);
485 else if (hval >= 1<<31)
486 tval = NHASHENT - tval;
490 static struct DirEntry *
491 FindItem(void *dir, char *ename, unsigned short **previtem)
493 /* Find a directory entry, given its name. This entry returns a pointer to a locked buffer, and a pointer to a locked buffer (in previtem) referencing the found item (to aid the delete code). If no entry is found, however, no items are left locked, and a null pointer is returned instead. */
495 register struct DirHeader *dhp;
496 register unsigned short *lp;
497 register struct DirEntry *tp;
499 dhp = (struct DirHeader *)DRead(dir, 0);
502 if (dhp->hashTable[i] == 0) {
507 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
512 lp = &(dhp->hashTable[i]);
514 /* Look at each hash conflict entry. */
515 if (!strcmp(ename, tp->name)) {
516 /* Found our entry. */
523 /* The end of the line */
524 DRelease(lp, 0); /* Release all locks. */
527 tp = GetBlob(dir, (u_short) ntohs(tp->next));