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"
93 #endif /* !defined(UKERNEL) */
94 #include "afs/afs_osi.h"
98 #include "afs/longc_procs.h"
99 #ifdef AFS_LINUX20_ENV
100 #include "h/string.h"
103 /* generic renaming */
104 #define NameBlobs afs_dir_NameBlobs
105 #define GetBlob afs_dir_GetBlob
106 #define Create afs_dir_Create
107 #define Length afs_dir_Length
108 #define Delete afs_dir_Delete
109 #define MakeDir afs_dir_MakeDir
110 #define Lookup afs_dir_Lookup
111 #define LookupOffset afs_dir_LookupOffset
112 #define EnumerateDir afs_dir_EnumerateDir
113 #define IsEmpty afs_dir_IsEmpty
116 # ifdef HAVE_UNISTD_H
119 # include <sys/types.h>
123 #include <winsock2.h>
125 #include <netinet/in.h>
131 #ifdef HAVE_STRINGS_H
139 /* Local static prototypes */
140 static struct DirEntry *FindItem(void *dir, char *ename,
141 unsigned short **previtem);
145 NameBlobs(char *name)
146 { /* Find out how many entries are required to store a name. */
148 i = strlen(name) + 1;
149 return 1 + ((i + 15) >> 5);
152 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
155 Create(void *dir, char *entry, void *voidfid)
157 afs_int32 *vfid = (afs_int32 *) voidfid;
160 register struct DirEntry *ep;
161 unsigned short *pp = NULL;
162 register struct DirHeader *dhp;
164 /* check name quality */
167 /* First check if file already exists. */
168 ep = FindItem(dir, entry, &pp);
174 blobs = NameBlobs(entry); /* number of entries required */
175 firstelt = FindBlobs(dir, blobs);
177 return EFBIG; /* directory is full */
178 /* First, we fill in the directory entry. */
179 ep = GetBlob(dir, firstelt);
183 ep->fid.vnode = htonl(vfid[1]);
184 ep->fid.vunique = htonl(vfid[2]);
185 strcpy(ep->name, entry);
186 /* Now we just have to thread it on the hash table list. */
187 dhp = (struct DirHeader *)DRead(dir, 0);
193 ep->next = dhp->hashTable[i];
194 dhp->hashTable[i] = htons(firstelt);
204 struct DirHeader *dhp;
205 dhp = (struct DirHeader *)DRead(dir, 0);
208 if (dhp->header.pgcount != 0)
209 ctr = ntohs(dhp->header.pgcount);
211 /* old style, count the pages */
213 for (i = 0; i < MAXPAGES; i++)
214 if (dhp->alloMap[i] != EPP)
218 return ctr * AFS_PAGESIZE;
222 Delete(void *dir, char *entry)
224 /* Delete an entry from a directory, including update of all free entry descriptors. */
226 register struct DirEntry *firstitem;
227 unsigned short *previtem;
228 firstitem = FindItem(dir, entry, &previtem);
231 *previtem = firstitem->next;
232 DRelease(previtem, 1);
233 index = DVOffset(firstitem) / 32;
234 nitems = NameBlobs(firstitem->name);
235 DRelease(firstitem, 0);
236 FreeBlobs(dir, index, nitems);
241 FindBlobs(void *dir, int nblobs)
243 /* Find a bunch of contiguous entries; at least nblobs in a row. */
244 register int i, j, k;
246 register struct DirHeader *dhp;
247 struct PageHeader *pp;
250 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
253 for (i = 0; i < BIGMAXPAGES; i++) {
254 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
255 /* if page could contain enough entries */
256 /* If there are EPP free entries, then the page is not even allocated. */
258 /* this pages exists past the end of the old-style dir */
259 pgcount = ntohs(dhp->header.pgcount);
262 dhp->header.pgcount = htons(pgcount);
264 if (i > pgcount - 1) {
265 /* this page is bigger than last allocated page */
267 dhp->header.pgcount = htons(i + 1);
269 } else if (dhp->alloMap[i] == EPP) {
270 /* Add the page to the directory. */
272 dhp->alloMap[i] = EPP - 1;
273 dhp->header.pgcount = htons(i + 1);
275 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
280 for (j = 0; j <= EPP - nblobs; j++) {
282 for (k = 0; k < nblobs; k++)
283 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
292 /* Here we have the first index in j. We update the allocation maps
293 * and free up any resources we've got allocated. */
295 dhp->alloMap[i] -= nblobs;
297 for (k = 0; k < nblobs; k++)
298 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
302 DRelease(pp, 0); /* This dir page is unchanged. */
305 /* If we make it here, the directory is full. */
311 AddPage(void *dir, int pageno)
312 { /* Add a page to a directory. */
314 register struct PageHeader *pp;
316 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
317 pp->tag = htons(1234);
320 pp->freecount = EPP - 1; /* The first dude is already allocated */
321 pp->freebitmap[0] = 0x01;
322 for (i = 1; i < EPP / 8; i++) /* It's a constant */
323 pp->freebitmap[i] = 0;
327 /* Free a whole bunch of directory entries. */
330 FreeBlobs(void *dir, register int firstblob, int nblobs)
334 struct DirHeader *dhp;
335 struct PageHeader *pp;
336 page = firstblob / EPP;
337 firstblob -= EPP * page; /* convert to page-relative entry */
338 dhp = (struct DirHeader *)DRead(dir, 0);
342 dhp->alloMap[page] += nblobs;
344 pp = (struct PageHeader *)DRead(dir, page);
346 for (i = 0; i < nblobs; i++)
347 pp->freebitmap[(firstblob + i) >> 3] &=
348 ~(1 << ((firstblob + i) & 7));
353 * Format an empty directory properly. Note that the first 13 entries in a
354 * directory header page are allocated, 1 to the page header, 4 to the
355 * allocation map and 8 to the hash table.
358 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
361 register struct DirHeader *dhp;
362 dhp = (struct DirHeader *)DNew(dir, 0);
363 dhp->header.pgcount = htons(1);
364 dhp->header.tag = htons(1234);
365 dhp->header.freecount = (EPP - DHE - 1);
366 dhp->header.freebitmap[0] = 0xff;
367 dhp->header.freebitmap[1] = 0x1f;
368 for (i = 2; i < EPP / 8; i++)
369 dhp->header.freebitmap[i] = 0;
370 dhp->alloMap[0] = (EPP - DHE - 1);
371 for (i = 1; i < MAXPAGES; i++)
372 dhp->alloMap[i] = EPP;
373 for (i = 0; i < NHASHENT; i++)
374 dhp->hashTable[i] = 0;
376 Create(dir, ".", me);
377 Create(dir, "..", parent); /* Virtue is its own .. */
381 /* Look up a file name in directory. */
384 Lookup(void *dir, char *entry, void *voidfid)
386 afs_int32 *fid = (afs_int32 *) voidfid;
387 register struct DirEntry *firstitem;
388 unsigned short *previtem;
390 firstitem = FindItem(dir, entry, &previtem);
393 DRelease(previtem, 0);
394 fid[1] = ntohl(firstitem->fid.vnode);
395 fid[2] = ntohl(firstitem->fid.vunique);
396 DRelease(firstitem, 0);
400 /* Look up a file name in directory. */
403 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
405 afs_int32 *fid = (afs_int32 *) voidfid;
406 register struct DirEntry *firstitem;
407 unsigned short *previtem;
409 firstitem = FindItem(dir, entry, &previtem);
412 DRelease(previtem, 0);
413 fid[1] = ntohl(firstitem->fid.vnode);
414 fid[2] = ntohl(firstitem->fid.vunique);
416 *offsetp = DVOffset(firstitem);
417 DRelease(firstitem, 0);
422 EnumerateDir(void *dir, int (*hookproc) (), void *hook)
424 /* Enumerate the contents of a directory. */
427 register struct DirHeader *dhp;
428 register struct DirEntry *ep;
430 dhp = (struct DirHeader *)DRead(dir, 0);
432 return EIO; /* first page should be there */
433 for (i = 0; i < NHASHENT; i++) {
434 /* For each hash chain, enumerate everyone on the list. */
435 num = ntohs(dhp->hashTable[i]);
437 /* Walk down the hash table list. */
439 ep = GetBlob(dir, num);
442 /* we failed, return why */
448 num = ntohs(ep->next);
449 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
450 ntohl(ep->fid.vunique));
461 /* Enumerate the contents of a directory. */
464 register struct DirHeader *dhp;
465 register struct DirEntry *ep;
466 dhp = (struct DirHeader *)DRead(dir, 0);
469 for (i = 0; i < NHASHENT; i++) {
470 /* For each hash chain, enumerate everyone on the list. */
471 num = ntohs(dhp->hashTable[i]);
473 /* Walk down the hash table list. */
474 ep = GetBlob(dir, num);
477 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
482 num = ntohs(ep->next);
491 GetBlob(void *dir, afs_int32 blobno)
493 /* Return a pointer to an entry, given its number. */
495 ep = DRead(dir, blobno >> LEPP);
498 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
502 DirHash(register char *string)
504 /* Hash a string to a number between 0 and NHASHENT. */
505 register unsigned char tc;
509 while ((tc = (*string++))) {
513 tval = hval & (NHASHENT - 1);
517 tval = NHASHENT - tval;
521 static struct DirEntry *
522 FindItem(void *dir, char *ename, unsigned short **previtem)
524 /* 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. */
526 register struct DirHeader *dhp;
527 register unsigned short *lp;
528 register struct DirEntry *tp;
530 dhp = (struct DirHeader *)DRead(dir, 0);
533 if (dhp->hashTable[i] == 0) {
538 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
543 lp = &(dhp->hashTable[i]);
545 /* Look at each hash conflict entry. */
546 if (!strcmp(ename, tp->name)) {
547 /* Found our entry. */
554 /* The end of the line */
555 DRelease(lp, 0); /* Release all locks. */
558 tp = GetBlob(dir, (u_short) ntohs(tp->next));