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) || defined(AFS_FBSD_ENV)
31 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
35 #if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV)
36 #include "afs/sysincludes.h"
38 #if defined(AFS_FBSD_ENV)
39 #include "h/libkern.h"
42 #include "vm/vm_extern.h"
44 #include "vm/vm_map.h"
45 #endif /* AFS_FBSD_ENV */
46 #if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN60_ENV)
48 #endif /* AFS_SGI64_ENV */
51 #include "afs/gfs_vfs.h"
52 #include "afs/gfs_vnode.h"
57 #include <sys/vnode.h>
58 #include <ufs/inode.h>
61 #include <vfs/vnode.h>
62 #include <sys/inode.h>
64 #else /* AFS_MACH_ENV */
66 #include <sys/mount.h>
67 #include <sys/vnode.h>
68 #include <ufs/inode.h>
69 #else /* AFS_OSF_ENV */
72 #if !defined(AFS_SGI_ENV)
74 #endif /* AFS_OSF_ENV */
75 #endif /* AFS_MACH_ENV */
78 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
81 #ifndef AFS_LINUX20_ENV
82 #include "netinet/in.h"
86 /* These are needed because afs_prototypes.h is not included here */
90 #else /* !defined(UKERNEL) */
92 #include "afs/sysincludes.h"
95 /* These are needed because afs_prototypes.h is not included here */
99 #endif /* !defined(UKERNEL) */
100 #include "afs/afs_osi.h"
104 #include "afs/longc_procs.h"
105 #ifdef AFS_LINUX20_ENV
106 #include "h/string.h"
109 /* generic renaming */
110 #define NameBlobs afs_dir_NameBlobs
111 #define GetBlob afs_dir_GetBlob
112 #define Create afs_dir_Create
113 #define Length afs_dir_Length
114 #define Delete afs_dir_Delete
115 #define MakeDir afs_dir_MakeDir
116 #define Lookup afs_dir_Lookup
117 #define LookupOffset afs_dir_LookupOffset
118 #define EnumerateDir afs_dir_EnumerateDir
119 #define IsEmpty afs_dir_IsEmpty
122 # ifdef HAVE_UNISTD_H
125 # include <sys/types.h>
129 #include <winsock2.h>
131 #include <netinet/in.h>
137 #ifdef HAVE_STRINGS_H
145 /* Local static prototypes */
146 static struct DirEntry *FindItem(void *dir, char *ename,
147 unsigned short **previtem);
151 NameBlobs(char *name)
152 { /* Find out how many entries are required to store a name. */
154 i = strlen(name) + 1;
155 return 1 + ((i + 15) >> 5);
158 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
161 Create(void *dir, char *entry, void *voidfid)
163 afs_int32 *vfid = (afs_int32 *) voidfid;
166 register struct DirEntry *ep;
167 unsigned short *pp = NULL;
168 register struct DirHeader *dhp;
170 /* check name quality */
173 /* First check if file already exists. */
174 ep = FindItem(dir, entry, &pp);
180 blobs = NameBlobs(entry); /* number of entries required */
181 firstelt = FindBlobs(dir, blobs);
183 return EFBIG; /* directory is full */
184 /* First, we fill in the directory entry. */
185 ep = GetBlob(dir, firstelt);
189 ep->fid.vnode = htonl(vfid[1]);
190 ep->fid.vunique = htonl(vfid[2]);
191 strcpy(ep->name, entry);
192 /* Now we just have to thread it on the hash table list. */
193 dhp = (struct DirHeader *)DRead(dir, 0);
199 ep->next = dhp->hashTable[i];
200 dhp->hashTable[i] = htons(firstelt);
210 struct DirHeader *dhp;
211 dhp = (struct DirHeader *)DRead(dir, 0);
214 if (dhp->header.pgcount != 0)
215 ctr = ntohs(dhp->header.pgcount);
217 /* old style, count the pages */
219 for (i = 0; i < MAXPAGES; i++)
220 if (dhp->alloMap[i] != EPP)
224 return ctr * AFS_PAGESIZE;
228 Delete(void *dir, char *entry)
230 /* Delete an entry from a directory, including update of all free entry descriptors. */
232 register struct DirEntry *firstitem;
233 unsigned short *previtem;
234 firstitem = FindItem(dir, entry, &previtem);
237 *previtem = firstitem->next;
238 DRelease(previtem, 1);
239 index = DVOffset(firstitem) / 32;
240 nitems = NameBlobs(firstitem->name);
241 DRelease(firstitem, 0);
242 FreeBlobs(dir, index, nitems);
247 FindBlobs(void *dir, int nblobs)
249 /* Find a bunch of contiguous entries; at least nblobs in a row. */
250 register int i, j, k;
252 register struct DirHeader *dhp;
253 struct PageHeader *pp;
256 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
259 for (i = 0; i < BIGMAXPAGES; i++) {
260 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
261 /* if page could contain enough entries */
262 /* If there are EPP free entries, then the page is not even allocated. */
264 /* this pages exists past the end of the old-style dir */
265 pgcount = ntohs(dhp->header.pgcount);
268 dhp->header.pgcount = htons(pgcount);
270 if (i > pgcount - 1) {
271 /* this page is bigger than last allocated page */
273 dhp->header.pgcount = htons(i + 1);
275 } else if (dhp->alloMap[i] == EPP) {
276 /* Add the page to the directory. */
278 dhp->alloMap[i] = EPP - 1;
279 dhp->header.pgcount = htons(i + 1);
281 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
286 for (j = 0; j <= EPP - nblobs; j++) {
288 for (k = 0; k < nblobs; k++)
289 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
298 /* Here we have the first index in j. We update the allocation maps
299 * and free up any resources we've got allocated. */
301 dhp->alloMap[i] -= nblobs;
303 for (k = 0; k < nblobs; k++)
304 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
308 DRelease(pp, 0); /* This dir page is unchanged. */
311 /* If we make it here, the directory is full. */
317 AddPage(void *dir, int pageno)
318 { /* Add a page to a directory. */
320 register struct PageHeader *pp;
322 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
323 pp->tag = htons(1234);
326 pp->freecount = EPP - 1; /* The first dude is already allocated */
327 pp->freebitmap[0] = 0x01;
328 for (i = 1; i < EPP / 8; i++) /* It's a constant */
329 pp->freebitmap[i] = 0;
333 /* Free a whole bunch of directory entries. */
336 FreeBlobs(void *dir, register int firstblob, int nblobs)
340 struct DirHeader *dhp;
341 struct PageHeader *pp;
342 page = firstblob / EPP;
343 firstblob -= EPP * page; /* convert to page-relative entry */
344 dhp = (struct DirHeader *)DRead(dir, 0);
348 dhp->alloMap[page] += nblobs;
350 pp = (struct PageHeader *)DRead(dir, page);
352 for (i = 0; i < nblobs; i++)
353 pp->freebitmap[(firstblob + i) >> 3] &=
354 ~(1 << ((firstblob + i) & 7));
359 * Format an empty directory properly. Note that the first 13 entries in a
360 * directory header page are allocated, 1 to the page header, 4 to the
361 * allocation map and 8 to the hash table.
364 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
367 register struct DirHeader *dhp;
368 dhp = (struct DirHeader *)DNew(dir, 0);
369 dhp->header.pgcount = htons(1);
370 dhp->header.tag = htons(1234);
371 dhp->header.freecount = (EPP - DHE - 1);
372 dhp->header.freebitmap[0] = 0xff;
373 dhp->header.freebitmap[1] = 0x1f;
374 for (i = 2; i < EPP / 8; i++)
375 dhp->header.freebitmap[i] = 0;
376 dhp->alloMap[0] = (EPP - DHE - 1);
377 for (i = 1; i < MAXPAGES; i++)
378 dhp->alloMap[i] = EPP;
379 for (i = 0; i < NHASHENT; i++)
380 dhp->hashTable[i] = 0;
382 Create(dir, ".", me);
383 Create(dir, "..", parent); /* Virtue is its own .. */
387 /* Look up a file name in directory. */
390 Lookup(void *dir, char *entry, void *voidfid)
392 afs_int32 *fid = (afs_int32 *) voidfid;
393 register struct DirEntry *firstitem;
394 unsigned short *previtem;
396 firstitem = FindItem(dir, entry, &previtem);
399 DRelease(previtem, 0);
400 fid[1] = ntohl(firstitem->fid.vnode);
401 fid[2] = ntohl(firstitem->fid.vunique);
402 DRelease(firstitem, 0);
406 /* Look up a file name in directory. */
409 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
411 afs_int32 *fid = (afs_int32 *) voidfid;
412 register struct DirEntry *firstitem;
413 unsigned short *previtem;
415 firstitem = FindItem(dir, entry, &previtem);
418 DRelease(previtem, 0);
419 fid[1] = ntohl(firstitem->fid.vnode);
420 fid[2] = ntohl(firstitem->fid.vunique);
422 *offsetp = DVOffset(firstitem);
423 DRelease(firstitem, 0);
428 EnumerateDir(void *dir, int (*hookproc) (), void *hook)
430 /* Enumerate the contents of a directory. */
433 register struct DirHeader *dhp;
434 register struct DirEntry *ep;
436 dhp = (struct DirHeader *)DRead(dir, 0);
438 return EIO; /* first page should be there */
439 for (i = 0; i < NHASHENT; i++) {
440 /* For each hash chain, enumerate everyone on the list. */
441 num = ntohs(dhp->hashTable[i]);
443 /* Walk down the hash table list. */
445 ep = GetBlob(dir, num);
448 /* we failed, return why */
454 num = ntohs(ep->next);
455 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
456 ntohl(ep->fid.vunique));
467 /* Enumerate the contents of a directory. */
470 register struct DirHeader *dhp;
471 register struct DirEntry *ep;
472 dhp = (struct DirHeader *)DRead(dir, 0);
475 for (i = 0; i < NHASHENT; i++) {
476 /* For each hash chain, enumerate everyone on the list. */
477 num = ntohs(dhp->hashTable[i]);
479 /* Walk down the hash table list. */
480 ep = GetBlob(dir, num);
483 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
488 num = ntohs(ep->next);
497 GetBlob(void *dir, afs_int32 blobno)
499 /* Return a pointer to an entry, given its number. */
501 ep = DRead(dir, blobno >> LEPP);
504 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
508 DirHash(register char *string)
510 /* Hash a string to a number between 0 and NHASHENT. */
511 register unsigned char tc;
515 while ((tc = (*string++))) {
519 tval = hval & (NHASHENT - 1);
523 tval = NHASHENT - tval;
527 static struct DirEntry *
528 FindItem(void *dir, char *ename, unsigned short **previtem)
530 /* 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. */
532 register struct DirHeader *dhp;
533 register unsigned short *lp;
534 register struct DirEntry *tp;
536 dhp = (struct DirHeader *)DRead(dir, 0);
539 if (dhp->hashTable[i] == 0) {
544 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
549 lp = &(dhp->hashTable[i]);
551 /* Look at each hash conflict entry. */
552 if (!strcmp(ename, tp->name)) {
553 /* Found our entry. */
560 /* The end of the line */
561 DRelease(lp, 0); /* Release all locks. */
564 tp = GetBlob(dir, (u_short) ntohs(tp->next));