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>
22 #include "h/sysmacros.h"
27 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV)
30 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
34 #if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV)
35 #include "afs/sysincludes.h"
37 #if defined(AFS_FBSD_ENV)
40 #include "vm/vm_extern.h"
42 #include "vm/vm_map.h"
43 #endif /* AFS_FBSD_ENV */
44 #if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN60_ENV)
46 #endif /* AFS_SGI64_ENV */
49 #include "afs/gfs_vfs.h"
50 #include "afs/gfs_vnode.h"
55 #include <sys/vnode.h>
56 #include <ufs/inode.h>
59 #include <vfs/vnode.h>
60 #include <sys/inode.h>
62 #else /* AFS_MACH_ENV */
64 #include <sys/mount.h>
65 #include <sys/vnode.h>
66 #include <ufs/inode.h>
67 #else /* AFS_OSF_ENV */
70 #if !defined(AFS_SGI_ENV)
72 #endif /* AFS_OSF_ENV */
73 #endif /* AFS_MACH_ENV */
76 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
79 #ifndef AFS_LINUX20_ENV
80 #include "netinet/in.h"
82 #else /* !defined(UKERNEL) */
84 #include "afs/sysincludes.h"
85 #endif /* !defined(UKERNEL) */
86 #include "afs/afs_osi.h"
90 #include "afs/longc_procs.h"
91 #ifdef AFS_LINUX20_ENV
95 /* generic renaming */
96 #define NameBlobs afs_dir_NameBlobs
97 #define GetBlob afs_dir_GetBlob
98 #define Create afs_dir_Create
99 #define Length afs_dir_Length
100 #define Delete afs_dir_Delete
101 #define MakeDir afs_dir_MakeDir
102 #define Lookup afs_dir_Lookup
103 #define LookupOffset afs_dir_LookupOffset
104 #define EnumerateDir afs_dir_EnumerateDir
105 #define IsEmpty afs_dir_IsEmpty
108 # ifdef HAVE_UNISTD_H
111 # include <sys/types.h>
115 #include <winsock2.h>
117 #include <netinet/in.h>
123 #ifdef HAVE_STRINGS_H
131 struct DirEntry *DRead();
132 struct DirEntry *DNew();
134 /* Local static prototypes */
135 static struct DirEntry *FindItem (void *dir, char *ename,
136 unsigned short **previtem);
139 int NameBlobs (char *name)
140 {/* Find out how many entries are required to store a name. */
143 return 1+((i+15)>>5);
146 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
148 int Create (void *dir, char *entry, void *voidfid)
150 afs_int32 *vfid = (afs_int32 *) voidfid;
153 register struct DirEntry *ep;
154 unsigned short *pp = NULL;
155 register struct DirHeader *dhp;
157 /* check name quality */
158 if (*entry == 0) return EINVAL;
159 /* First check if file already exists. */
160 ep = FindItem(dir, entry, &pp);
166 blobs = NameBlobs(entry); /* number of entries required */
167 firstelt = FindBlobs(dir, blobs);
168 if (firstelt < 0) return EFBIG; /* directory is full */
169 /* First, we fill in the directory entry. */
170 ep = GetBlob(dir, firstelt);
171 if (ep == 0) return EIO;
173 ep->fid.vnode = htonl(vfid[1]);
174 ep->fid.vunique = htonl(vfid[2]);
175 strcpy(ep->name, entry);
176 /* Now we just have to thread it on the hash table list. */
177 dhp = (struct DirHeader *) DRead(dir,0);
183 ep->next = dhp->hashTable[i];
184 dhp->hashTable[i] = htons(firstelt);
190 int Length (void *dir)
193 struct DirHeader *dhp;
194 dhp = (struct DirHeader *) DRead(dir,0);
196 if (dhp->header.pgcount != 0) ctr = ntohs(dhp->header.pgcount);
198 /* old style, count the pages */
200 for(i=0;i<MAXPAGES;i++)
201 if (dhp->alloMap[i] != EPP) ctr++;
204 return ctr*AFS_PAGESIZE;
207 int Delete (void *dir, char *entry)
209 /* Delete an entry from a directory, including update of all free entry descriptors. */
211 register struct DirEntry *firstitem;
212 unsigned short *previtem;
213 firstitem = FindItem(dir,entry,&previtem);
214 if (firstitem == 0) return ENOENT;
215 *previtem = firstitem->next;
216 DRelease(previtem,1);
217 index = DVOffset(firstitem)/32;
218 nitems = NameBlobs(firstitem->name);
219 DRelease(firstitem,0);
220 FreeBlobs(dir,index,nitems);
224 int FindBlobs (void *dir, int nblobs)
226 /* Find a bunch of contiguous entries; at least nblobs in a row. */
227 register int i, j, k;
229 register struct DirHeader *dhp;
230 struct PageHeader *pp;
233 dhp = (struct DirHeader *) DRead(dir,0); /* read the dir header in first. */
235 for(i=0;i<BIGMAXPAGES;i++) {
236 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
237 /* if page could contain enough entries */
238 /* If there are EPP free entries, then the page is not even allocated. */
240 /* this pages exists past the end of the old-style dir */
241 pgcount = ntohs(dhp->header.pgcount);
244 dhp->header.pgcount = htons(pgcount);
246 if (i > pgcount - 1) {
247 /* this page is bigger than last allocated page */
249 dhp->header.pgcount = htons(i+1);
252 else if (dhp->alloMap[i] == EPP) {
253 /* Add the page to the directory. */
255 dhp->alloMap[i] = EPP-1;
256 dhp->header.pgcount = htons(i+1);
258 pp = (struct PageHeader *) DRead(dir,i); /* read the page in. */
263 for(j=0;j<=EPP-nblobs;j++) {
265 for(k=0;k<nblobs;k++)
266 if ((pp->freebitmap[(j+k)>>3]>>((j+k)&7)) & 1) {
274 /* Here we have the first index in j. We update the allocation maps
275 and free up any resources we've got allocated. */
276 if (i < MAXPAGES) dhp->alloMap[i] -= nblobs;
278 for (k=0;k<nblobs;k++)
279 pp->freebitmap[(j+k)>>3] |= 1<<((j+k)&7);
283 DRelease(pp, 0); /* This dir page is unchanged. */
286 /* If we make it here, the directory is full. */
291 void AddPage (void *dir, int pageno)
292 {/* Add a page to a directory. */
294 register struct PageHeader *pp;
296 pp = (struct PageHeader *) DNew(dir,pageno); /* Get a new buffer labelled dir,pageno */
297 pp->tag = htons(1234);
298 if (pageno > 0) pp->pgcount = 0;
299 pp->freecount = EPP-1; /* The first dude is already allocated */
300 pp->freebitmap[0] = 0x01;
301 for (i=1;i<EPP/8;i++) /* It's a constant */
302 pp->freebitmap[i] = 0;
306 /* Free a whole bunch of directory entries. */
308 void FreeBlobs(void *dir, register int firstblob, int nblobs)
312 struct DirHeader *dhp;
313 struct PageHeader *pp;
314 page = firstblob/EPP;
315 firstblob -= EPP*page; /* convert to page-relative entry */
316 dhp = (struct DirHeader *) DRead(dir,0);
318 if (page < MAXPAGES) dhp->alloMap[page] += nblobs;
320 pp = (struct PageHeader *) DRead(dir,page);
321 if (pp) for (i=0;i<nblobs;i++)
322 pp->freebitmap[(firstblob+i)>>3] &= ~(1<<((firstblob+i)&7));
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.
331 int MakeDir (void *dir, afs_int32 *me, afs_int32 *parent)
334 register struct DirHeader *dhp;
335 dhp = (struct DirHeader *) DNew(dir,0);
336 dhp->header.pgcount = htons(1);
337 dhp->header.tag = htons(1234);
338 dhp->header.freecount = (EPP-DHE-1);
339 dhp->header.freebitmap[0] = 0xff;
340 dhp->header.freebitmap[1] = 0x1f;
341 for(i=2;i<EPP/8;i++) dhp->header.freebitmap[i] = 0;
342 dhp->alloMap[0]=(EPP-DHE-1);
343 for(i=1;i<MAXPAGES;i++)dhp->alloMap[i] = EPP;
344 for(i=0;i<NHASHENT;i++)dhp->hashTable[i] = 0;
347 Create(dir,"..",parent); /* Virtue is its own .. */
351 /* Look up a file name in directory. */
353 int 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);
360 if (firstitem == 0) return ENOENT;
361 DRelease(previtem,0);
362 fid[1] = ntohl(firstitem->fid.vnode);
363 fid[2] = ntohl(firstitem->fid.vunique);
364 DRelease(firstitem,0);
368 /* Look up a file name in directory. */
370 int LookupOffset (void *dir, char *entry, void *voidfid, long *offsetp)
372 afs_int32 *fid = (afs_int32 *) voidfid;
373 register struct DirEntry *firstitem;
374 unsigned short *previtem;
376 firstitem = FindItem(dir,entry,&previtem);
377 if (firstitem == 0) return ENOENT;
378 DRelease(previtem,0);
379 fid[1] = ntohl(firstitem->fid.vnode);
380 fid[2] = ntohl(firstitem->fid.vunique);
382 *offsetp = DVOffset(firstitem);
383 DRelease(firstitem,0);
387 int EnumerateDir (void *dir, int (*hookproc)(), void *hook)
389 /* Enumerate the contents of a directory. */
392 register struct DirHeader *dhp;
393 register struct DirEntry *ep;
395 dhp = (struct DirHeader *) DRead(dir,0);
396 if (!dhp) return EIO; /* first page should be there */
397 for(i=0; i<NHASHENT; i++) {
398 /* For each hash chain, enumerate everyone on the list. */
399 num = ntohs(dhp->hashTable[i]);
401 /* Walk down the hash table list. */
403 ep = GetBlob(dir,num);
406 /* we failed, return why */
412 num = ntohs(ep->next);
413 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode), ntohl(ep->fid.vunique));
421 int IsEmpty (void *dir)
423 /* Enumerate the contents of a directory. */
426 register struct DirHeader *dhp;
427 register struct DirEntry *ep;
428 dhp = (struct DirHeader *) DRead(dir,0);
430 for(i=0;i<NHASHENT;i++) {
431 /* For each hash chain, enumerate everyone on the list. */
432 num = ntohs(dhp->hashTable[i]);
434 /* Walk down the hash table list. */
435 ep = GetBlob(dir,num);
437 if (strcmp(ep->name,"..") && strcmp(ep->name,".")) {
442 num = ntohs(ep->next);
450 struct DirEntry *GetBlob (void *dir, afs_int32 blobno)
452 /* Return a pointer to an entry, given its number. */
454 ep=DRead(dir,blobno>>LEPP);
456 return (struct DirEntry *) (((long)ep)+32*(blobno&(EPP-1)));
459 int DirHash (register char *string)
461 /* Hash a string to a number between 0 and NHASHENT. */
462 register unsigned char tc;
466 while((tc=(*string++))) {
470 tval = hval & (NHASHENT-1);
471 if (tval == 0) return tval;
472 else if (hval < 0) tval = NHASHENT-tval;
476 static struct DirEntry *FindItem (void *dir, char *ename,
477 unsigned short **previtem)
479 /* 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. */
481 register struct DirHeader *dhp;
482 register unsigned short *lp;
483 register struct DirEntry *tp;
485 dhp = (struct DirHeader *) DRead(dir,0);
487 if (dhp->hashTable[i] == 0) {
492 tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
497 lp = &(dhp->hashTable[i]);
499 /* Look at each hash conflict entry. */
500 if (!strcmp(ename,tp->name)) {
501 /* Found our entry. */
508 /* The end of the line */
509 DRelease(lp,0); /* Release all locks. */
512 tp = GetBlob(dir,(u_short)ntohs(tp->next));