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 <afs/param.h>
11 #include <afsconfig.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/sysincludes.h"
84 #endif /* !defined(UKERNEL) */
85 #include "../afs/afs_osi.h"
87 #include "../afs/dir.h"
89 #include "../afs/longc_procs.h"
90 #ifdef AFS_LINUX20_ENV
91 #include "../h/string.h"
94 /* generic renaming */
95 #define NameBlobs afs_dir_NameBlobs
96 #define GetBlob afs_dir_GetBlob
97 #define Create afs_dir_Create
98 #define Length afs_dir_Length
99 #define Delete afs_dir_Delete
100 #define MakeDir afs_dir_MakeDir
101 #define Lookup afs_dir_Lookup
102 #define LookupOffset afs_dir_LookupOffset
103 #define EnumerateDir afs_dir_EnumerateDir
104 #define IsEmpty afs_dir_IsEmpty
107 # include <sys/types.h>
111 #include <winsock2.h>
113 #include <netinet/in.h>
119 static struct DirEntry *FindItem();
120 struct DirEntry *GetBlob();
121 struct DirEntry *DRead();
122 struct DirEntry *DNew();
128 char * name; {/* Find out how many entries are required to store a name. */
131 return 1+((i+15)>>5);
134 int Create (dir, entry, vfid)
138 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
141 register struct DirEntry *ep;
143 register struct DirHeader *dhp;
145 /* check name quality */
146 if (*entry == 0) return EINVAL;
147 /* First check if file already exists. */
148 ep = FindItem(dir,entry,&pp);
154 blobs = NameBlobs(entry); /* number of entries required */
155 firstelt = FindBlobs(dir,blobs);
156 if (firstelt < 0) return EFBIG; /* directory is full */
157 /* First, we fill in the directory entry. */
158 ep = GetBlob(dir,firstelt);
159 if (ep == 0) return EIO;
161 ep->fid.vnode = htonl(vfid[1]);
162 ep->fid.vunique = htonl(vfid[2]);
163 strcpy(ep->name,entry);
164 /* Now we just have to thread it on the hash table list. */
165 dhp = (struct DirHeader *) DRead(dir,0);
171 ep->next = dhp->hashTable[i];
172 dhp->hashTable[i] = htons(firstelt);
181 struct DirHeader *dhp;
182 dhp = (struct DirHeader *) DRead(dir,0);
184 if (dhp->header.pgcount != 0) ctr = ntohs(dhp->header.pgcount);
186 /* old style, count the pages */
188 for(i=0;i<MAXPAGES;i++)
189 if (dhp->alloMap[i] != EPP) ctr++;
192 return ctr*AFS_PAGESIZE;
198 /* Delete an entry from a directory, including update of all free entry descriptors. */
200 register struct DirEntry *firstitem;
201 unsigned short *previtem;
202 firstitem = FindItem(dir,entry,&previtem);
203 if (firstitem == 0) return ENOENT;
204 *previtem = firstitem->next;
205 DRelease(previtem,1);
206 index = DVOffset(firstitem)/32;
207 nitems = NameBlobs(firstitem->name);
208 DRelease(firstitem,0);
209 FreeBlobs(dir,index,nitems);
213 FindBlobs (dir,nblobs)
216 /* Find a bunch of contiguous entries; at least nblobs in a row. */
217 register int i, j, k;
219 register struct DirHeader *dhp;
220 struct PageHeader *pp;
223 dhp = (struct DirHeader *) DRead(dir,0); /* read the dir header in first. */
225 for(i=0;i<BIGMAXPAGES;i++) {
226 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
227 /* if page could contain enough entries */
228 /* If there are EPP free entries, then the page is not even allocated. */
230 /* this pages exists past the end of the old-style dir */
231 pgcount = ntohs(dhp->header.pgcount);
234 dhp->header.pgcount = htons(pgcount);
236 if (i > pgcount - 1) {
237 /* this page is bigger than last allocated page */
239 dhp->header.pgcount = htons(i+1);
242 else if (dhp->alloMap[i] == EPP) {
243 /* Add the page to the directory. */
245 dhp->alloMap[i] = EPP-1;
246 dhp->header.pgcount = htons(i+1);
248 pp = (struct PageHeader *) DRead(dir,i); /* read the page in. */
253 for(j=0;j<=EPP-nblobs;j++) {
255 for(k=0;k<nblobs;k++)
256 if ((pp->freebitmap[(j+k)>>3]>>((j+k)&7)) & 1) {
264 /* Here we have the first index in j. We update the allocation maps
265 and free up any resources we've got allocated. */
266 if (i < MAXPAGES) dhp->alloMap[i] -= nblobs;
268 for (k=0;k<nblobs;k++)
269 pp->freebitmap[(j+k)>>3] |= 1<<((j+k)&7);
273 DRelease(pp, 0); /* This dir page is unchanged. */
276 /* If we make it here, the directory is full. */
281 void AddPage (dir,pageno)
283 int pageno; {/* Add a page to a directory. */
285 register struct PageHeader *pp;
287 pp = (struct PageHeader *) DNew(dir,pageno); /* Get a new buffer labelled dir,pageno */
288 pp->tag = htons(1234);
289 if (pageno > 0) pp->pgcount = 0;
290 pp->freecount = EPP-1; /* The first dude is already allocated */
291 pp->freebitmap[0] = 0x01;
292 for (i=1;i<EPP/8;i++) /* It's a constant */
293 pp->freebitmap[i] = 0;
297 void FreeBlobs(dir,firstblob,nblobs)
299 register int firstblob;
301 /* Free a whole bunch of directory entries. */
304 struct DirHeader *dhp;
305 struct PageHeader *pp;
306 page = firstblob/EPP;
307 firstblob -= EPP*page; /* convert to page-relative entry */
308 dhp = (struct DirHeader *) DRead(dir,0);
310 if (page < MAXPAGES) dhp->alloMap[page] += nblobs;
312 pp = (struct PageHeader *) DRead(dir,page);
313 if (pp) for (i=0;i<nblobs;i++)
314 pp->freebitmap[(firstblob+i)>>3] &= ~(1<<((firstblob+i)&7));
318 MakeDir (dir,me,parent)
322 /* Format an empty directory properly. Note that the first 13 entries in a directory header
323 page are allocated, 1 to the page header, 4 to the allocation map and 8 to the hash table. */
325 register struct DirHeader *dhp;
326 dhp = (struct DirHeader *) DNew(dir,0);
327 dhp->header.pgcount = htons(1);
328 dhp->header.tag = htons(1234);
329 dhp->header.freecount = (EPP-DHE-1);
330 dhp->header.freebitmap[0] = 0xff;
331 dhp->header.freebitmap[1] = 0x1f;
332 for(i=2;i<EPP/8;i++) dhp->header.freebitmap[i] = 0;
333 dhp->alloMap[0]=(EPP-DHE-1);
334 for(i=1;i<MAXPAGES;i++)dhp->alloMap[i] = EPP;
335 for(i=0;i<NHASHENT;i++)dhp->hashTable[i] = 0;
338 Create(dir,"..",parent); /* Virtue is its own .. */
342 Lookup (dir, entry, fid)
345 register afs_int32 *fid; {
346 /* Look up a file name in directory. */
347 register struct DirEntry *firstitem;
348 unsigned short *previtem;
350 firstitem = FindItem(dir,entry,&previtem);
351 if (firstitem == 0) return ENOENT;
352 DRelease(previtem,0);
353 fid[1] = ntohl(firstitem->fid.vnode);
354 fid[2] = ntohl(firstitem->fid.vunique);
355 DRelease(firstitem,0);
359 LookupOffset (dir, entry, fid, offsetp)
363 register afs_int32 *fid; {
364 /* Look up a file name in directory. */
365 register struct DirEntry *firstitem;
366 unsigned short *previtem;
368 firstitem = FindItem(dir,entry,&previtem);
369 if (firstitem == 0) return ENOENT;
370 DRelease(previtem,0);
371 fid[1] = ntohl(firstitem->fid.vnode);
372 fid[2] = ntohl(firstitem->fid.vunique);
374 *offsetp = DVOffset(firstitem);
375 DRelease(firstitem,0);
379 EnumerateDir (dir,hookproc,hook)
383 /* Enumerate the contents of a directory. */
386 register struct DirHeader *dhp;
387 register struct DirEntry *ep;
389 dhp = (struct DirHeader *) DRead(dir,0);
390 if (!dhp) return EIO; /* first page should be there */
391 for(i=0; i<NHASHENT; i++) {
392 /* For each hash chain, enumerate everyone on the list. */
393 num = ntohs(dhp->hashTable[i]);
395 /* Walk down the hash table list. */
397 ep = GetBlob(dir,num);
400 /* we failed, return why */
406 num = ntohs(ep->next);
407 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode), ntohl(ep->fid.vunique));
417 /* Enumerate the contents of a directory. */
420 register struct DirHeader *dhp;
421 register struct DirEntry *ep;
422 dhp = (struct DirHeader *) DRead(dir,0);
424 for(i=0;i<NHASHENT;i++) {
425 /* For each hash chain, enumerate everyone on the list. */
426 num = ntohs(dhp->hashTable[i]);
428 /* Walk down the hash table list. */
429 ep = GetBlob(dir,num);
431 if (strcmp(ep->name,"..") && strcmp(ep->name,".")) {
436 num = ntohs(ep->next);
444 struct DirEntry *GetBlob (dir, blobno)
447 /* Return a pointer to an entry, given its number. */
449 ep=DRead(dir,blobno>>LEPP);
451 return (struct DirEntry *) (((long)ep)+32*(blobno&(EPP-1)));
455 register char *string; {
456 /* Hash a string to a number between 0 and NHASHENT. */
457 register unsigned char tc;
461 while(tc=(*string++)) {
465 tval = hval & (NHASHENT-1);
466 if (tval == 0) return tval;
467 else if (hval < 0) tval = NHASHENT-tval;
471 static struct DirEntry *FindItem (dir,ename,previtem)
474 unsigned short **previtem; {
475 /* 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. */
477 register struct DirHeader *dhp;
478 register unsigned short *lp;
479 register struct DirEntry *tp;
481 dhp = (struct DirHeader *) DRead(dir,0);
483 if (dhp->hashTable[i] == 0) {
488 tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
493 lp = &(dhp->hashTable[i]);
495 /* Look at each hash conflict entry. */
496 if (!strcmp(ename,tp->name)) {
497 /* Found our entry. */
504 /* The end of the line */
505 DRelease(lp,0); /* Release all locks. */
508 tp = GetBlob(dir,(u_short)ntohs(tp->next));