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>
17 #include "../h/types.h"
18 #include "../h/param.h"
22 #include "../h/sysmacros.h"
23 #include "../h/signal.h"
24 #include "../h/errno.h"
26 #include "../h/time.h"
27 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV)
28 #include "../h/errno.h"
30 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
31 #include "../h/kernel.h"
34 #if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV)
35 #include "../afs/sysincludes.h"
38 #if defined(AFS_FBSD_ENV)
39 #include "../h/lock.h"
41 #include "../vm/vm_extern.h"
42 #include "../vm/pmap.h"
43 #include "../vm/vm_map.h"
44 #endif /* AFS_FBSD_ENV */
45 #include "../h/user.h"
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 #ifndef AFS_LINUX20_ENV
77 #include "../netinet/in.h"
79 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
80 #include "../h/mbuf.h"
82 #else /* !defined(UKERNEL) */
83 #include "../afs/stds.h"
84 #include "../afs/sysincludes.h"
85 #endif /* !defined(UKERNEL) */
86 #include "../afs/afs_osi.h"
88 #include "../afs/dir.h"
90 #include "../afs/longc_procs.h"
91 #ifdef AFS_LINUX20_ENV
92 #include "../h/string.h"
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 (char *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 int Create (char *dir, char *entry, afs_int32 *vfid)
148 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
151 register struct DirEntry *ep;
152 unsigned short *pp = NULL;
153 register struct DirHeader *dhp;
155 /* check name quality */
156 if (*entry == 0) return EINVAL;
157 /* First check if file already exists. */
158 ep = FindItem(dir,entry,&pp);
164 blobs = NameBlobs(entry); /* number of entries required */
165 firstelt = FindBlobs(dir,blobs);
166 if (firstelt < 0) return EFBIG; /* directory is full */
167 /* First, we fill in the directory entry. */
168 ep = GetBlob(dir,firstelt);
169 if (ep == 0) return EIO;
171 ep->fid.vnode = htonl(vfid[1]);
172 ep->fid.vunique = htonl(vfid[2]);
173 strcpy(ep->name,entry);
174 /* Now we just have to thread it on the hash table list. */
175 dhp = (struct DirHeader *) DRead(dir,0);
181 ep->next = dhp->hashTable[i];
182 dhp->hashTable[i] = htons(firstelt);
188 int Length (char *dir)
191 struct DirHeader *dhp;
192 dhp = (struct DirHeader *) DRead(dir,0);
194 if (dhp->header.pgcount != 0) ctr = ntohs(dhp->header.pgcount);
196 /* old style, count the pages */
198 for(i=0;i<MAXPAGES;i++)
199 if (dhp->alloMap[i] != EPP) ctr++;
202 return ctr*AFS_PAGESIZE;
205 int Delete (char *dir, char *entry)
207 /* Delete an entry from a directory, including update of all free entry descriptors. */
209 register struct DirEntry *firstitem;
210 unsigned short *previtem;
211 firstitem = FindItem(dir,entry,&previtem);
212 if (firstitem == 0) return ENOENT;
213 *previtem = firstitem->next;
214 DRelease(previtem,1);
215 index = DVOffset(firstitem)/32;
216 nitems = NameBlobs(firstitem->name);
217 DRelease(firstitem,0);
218 FreeBlobs(dir,index,nitems);
222 int FindBlobs (char *dir, int nblobs)
224 /* Find a bunch of contiguous entries; at least nblobs in a row. */
225 register int i, j, k;
227 register struct DirHeader *dhp;
228 struct PageHeader *pp;
231 dhp = (struct DirHeader *) DRead(dir,0); /* read the dir header in first. */
233 for(i=0;i<BIGMAXPAGES;i++) {
234 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
235 /* if page could contain enough entries */
236 /* If there are EPP free entries, then the page is not even allocated. */
238 /* this pages exists past the end of the old-style dir */
239 pgcount = ntohs(dhp->header.pgcount);
242 dhp->header.pgcount = htons(pgcount);
244 if (i > pgcount - 1) {
245 /* this page is bigger than last allocated page */
247 dhp->header.pgcount = htons(i+1);
250 else if (dhp->alloMap[i] == EPP) {
251 /* Add the page to the directory. */
253 dhp->alloMap[i] = EPP-1;
254 dhp->header.pgcount = htons(i+1);
256 pp = (struct PageHeader *) DRead(dir,i); /* read the page in. */
261 for(j=0;j<=EPP-nblobs;j++) {
263 for(k=0;k<nblobs;k++)
264 if ((pp->freebitmap[(j+k)>>3]>>((j+k)&7)) & 1) {
272 /* Here we have the first index in j. We update the allocation maps
273 and free up any resources we've got allocated. */
274 if (i < MAXPAGES) dhp->alloMap[i] -= nblobs;
276 for (k=0;k<nblobs;k++)
277 pp->freebitmap[(j+k)>>3] |= 1<<((j+k)&7);
281 DRelease(pp, 0); /* This dir page is unchanged. */
284 /* If we make it here, the directory is full. */
289 void AddPage (char *dir, int pageno)
290 {/* Add a page to a directory. */
292 register struct PageHeader *pp;
294 pp = (struct PageHeader *) DNew(dir,pageno); /* Get a new buffer labelled dir,pageno */
295 pp->tag = htons(1234);
296 if (pageno > 0) pp->pgcount = 0;
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;
304 void FreeBlobs(char *dir, register int firstblob, int nblobs)
306 /* Free a whole bunch of directory entries. */
309 struct DirHeader *dhp;
310 struct PageHeader *pp;
311 page = firstblob/EPP;
312 firstblob -= EPP*page; /* convert to page-relative entry */
313 dhp = (struct DirHeader *) DRead(dir,0);
315 if (page < MAXPAGES) dhp->alloMap[page] += nblobs;
317 pp = (struct PageHeader *) DRead(dir,page);
318 if (pp) for (i=0;i<nblobs;i++)
319 pp->freebitmap[(firstblob+i)>>3] &= ~(1<<((firstblob+i)&7));
323 int MakeDir (char *dir, afs_int32 *me, afs_int32 *parent)
325 /* Format an empty directory properly. Note that the first 13 entries in a directory header
326 page are allocated, 1 to the page header, 4 to the allocation map and 8 to the hash table. */
328 register struct DirHeader *dhp;
329 dhp = (struct DirHeader *) DNew(dir,0);
330 dhp->header.pgcount = htons(1);
331 dhp->header.tag = htons(1234);
332 dhp->header.freecount = (EPP-DHE-1);
333 dhp->header.freebitmap[0] = 0xff;
334 dhp->header.freebitmap[1] = 0x1f;
335 for(i=2;i<EPP/8;i++) dhp->header.freebitmap[i] = 0;
336 dhp->alloMap[0]=(EPP-DHE-1);
337 for(i=1;i<MAXPAGES;i++)dhp->alloMap[i] = EPP;
338 for(i=0;i<NHASHENT;i++)dhp->hashTable[i] = 0;
341 Create(dir,"..",parent); /* Virtue is its own .. */
345 int Lookup (char *dir, char *entry, register afs_int32 *fid)
347 /* Look up a file name in directory. */
348 register struct DirEntry *firstitem;
349 unsigned short *previtem;
351 firstitem = FindItem(dir,entry,&previtem);
352 if (firstitem == 0) return ENOENT;
353 DRelease(previtem,0);
354 fid[1] = ntohl(firstitem->fid.vnode);
355 fid[2] = ntohl(firstitem->fid.vunique);
356 DRelease(firstitem,0);
360 int LookupOffset (char *dir, char *entry, register afs_int32 *fid, long *offsetp)
362 /* Look up a file name in directory. */
363 register struct DirEntry *firstitem;
364 unsigned short *previtem;
366 firstitem = FindItem(dir,entry,&previtem);
367 if (firstitem == 0) return ENOENT;
368 DRelease(previtem,0);
369 fid[1] = ntohl(firstitem->fid.vnode);
370 fid[2] = ntohl(firstitem->fid.vunique);
372 *offsetp = DVOffset(firstitem);
373 DRelease(firstitem,0);
377 int EnumerateDir (char *dir, int (*hookproc)(), void *hook)
379 /* Enumerate the contents of a directory. */
382 register struct DirHeader *dhp;
383 register struct DirEntry *ep;
385 dhp = (struct DirHeader *) DRead(dir,0);
386 if (!dhp) return EIO; /* first page should be there */
387 for(i=0; i<NHASHENT; i++) {
388 /* For each hash chain, enumerate everyone on the list. */
389 num = ntohs(dhp->hashTable[i]);
391 /* Walk down the hash table list. */
393 ep = GetBlob(dir,num);
396 /* we failed, return why */
402 num = ntohs(ep->next);
403 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode), ntohl(ep->fid.vunique));
411 int IsEmpty (char *dir)
413 /* Enumerate the contents of a directory. */
416 register struct DirHeader *dhp;
417 register struct DirEntry *ep;
418 dhp = (struct DirHeader *) DRead(dir,0);
420 for(i=0;i<NHASHENT;i++) {
421 /* For each hash chain, enumerate everyone on the list. */
422 num = ntohs(dhp->hashTable[i]);
424 /* Walk down the hash table list. */
425 ep = GetBlob(dir,num);
427 if (strcmp(ep->name,"..") && strcmp(ep->name,".")) {
432 num = ntohs(ep->next);
440 struct DirEntry *GetBlob (char *dir, afs_int32 blobno)
442 /* Return a pointer to an entry, given its number. */
444 ep=DRead(dir,blobno>>LEPP);
446 return (struct DirEntry *) (((long)ep)+32*(blobno&(EPP-1)));
449 int DirHash (register char *string)
451 /* Hash a string to a number between 0 and NHASHENT. */
452 register unsigned char tc;
456 while((tc=(*string++))) {
460 tval = hval & (NHASHENT-1);
461 if (tval == 0) return tval;
462 else if (hval < 0) tval = NHASHENT-tval;
466 static struct DirEntry *FindItem (char *dir, char *ename,
467 unsigned short **previtem)
469 /* 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. */
471 register struct DirHeader *dhp;
472 register unsigned short *lp;
473 register struct DirEntry *tp;
475 dhp = (struct DirHeader *) DRead(dir,0);
477 if (dhp->hashTable[i] == 0) {
482 tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
487 lp = &(dhp->hashTable[i]);
489 /* Look at each hash conflict entry. */
490 if (!strcmp(ename,tp->name)) {
491 /* Found our entry. */
498 /* The end of the line */
499 DRelease(lp,0); /* Release all locks. */
502 tp = GetBlob(dir,(u_short)ntohs(tp->next));