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"
84 #else /* !defined(UKERNEL) */
86 #include "afs/sysincludes.h"
87 #endif /* !defined(UKERNEL) */
88 #include "afs/afs_osi.h"
92 #include "afs/longc_procs.h"
93 #ifdef AFS_LINUX20_ENV
97 /* generic renaming */
98 #define NameBlobs afs_dir_NameBlobs
99 #define GetBlob afs_dir_GetBlob
100 #define Create afs_dir_Create
101 #define Length afs_dir_Length
102 #define Delete afs_dir_Delete
103 #define MakeDir afs_dir_MakeDir
104 #define Lookup afs_dir_Lookup
105 #define LookupOffset afs_dir_LookupOffset
106 #define EnumerateDir afs_dir_EnumerateDir
107 #define IsEmpty afs_dir_IsEmpty
110 # ifdef HAVE_UNISTD_H
113 # include <sys/types.h>
117 #include <winsock2.h>
119 #include <netinet/in.h>
125 #ifdef HAVE_STRINGS_H
133 struct DirEntry *DRead();
134 struct DirEntry *DNew();
136 /* Local static prototypes */
137 static struct DirEntry *FindItem(void *dir, char *ename,
138 unsigned short **previtem);
142 NameBlobs(char *name)
143 { /* Find out how many entries are required to store a name. */
145 i = strlen(name) + 1;
146 return 1 + ((i + 15) >> 5);
149 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
152 Create(void *dir, char *entry, void *voidfid)
154 afs_int32 *vfid = (afs_int32 *) voidfid;
157 register struct DirEntry *ep;
158 unsigned short *pp = NULL;
159 register struct DirHeader *dhp;
161 /* check name quality */
164 /* First check if file already exists. */
165 ep = FindItem(dir, entry, &pp);
171 blobs = NameBlobs(entry); /* number of entries required */
172 firstelt = FindBlobs(dir, blobs);
174 return EFBIG; /* directory is full */
175 /* First, we fill in the directory entry. */
176 ep = GetBlob(dir, firstelt);
180 ep->fid.vnode = htonl(vfid[1]);
181 ep->fid.vunique = htonl(vfid[2]);
182 strcpy(ep->name, entry);
183 /* Now we just have to thread it on the hash table list. */
184 dhp = (struct DirHeader *)DRead(dir, 0);
190 ep->next = dhp->hashTable[i];
191 dhp->hashTable[i] = htons(firstelt);
201 struct DirHeader *dhp;
202 dhp = (struct DirHeader *)DRead(dir, 0);
205 if (dhp->header.pgcount != 0)
206 ctr = ntohs(dhp->header.pgcount);
208 /* old style, count the pages */
210 for (i = 0; i < MAXPAGES; i++)
211 if (dhp->alloMap[i] != EPP)
215 return ctr * AFS_PAGESIZE;
219 Delete(void *dir, char *entry)
221 /* Delete an entry from a directory, including update of all free entry descriptors. */
223 register struct DirEntry *firstitem;
224 unsigned short *previtem;
225 firstitem = FindItem(dir, entry, &previtem);
228 *previtem = firstitem->next;
229 DRelease(previtem, 1);
230 index = DVOffset(firstitem) / 32;
231 nitems = NameBlobs(firstitem->name);
232 DRelease(firstitem, 0);
233 FreeBlobs(dir, index, nitems);
238 FindBlobs(void *dir, int nblobs)
240 /* Find a bunch of contiguous entries; at least nblobs in a row. */
241 register int i, j, k;
243 register struct DirHeader *dhp;
244 struct PageHeader *pp;
247 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
250 for (i = 0; i < BIGMAXPAGES; i++) {
251 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
252 /* if page could contain enough entries */
253 /* If there are EPP free entries, then the page is not even allocated. */
255 /* this pages exists past the end of the old-style dir */
256 pgcount = ntohs(dhp->header.pgcount);
259 dhp->header.pgcount = htons(pgcount);
261 if (i > pgcount - 1) {
262 /* this page is bigger than last allocated page */
264 dhp->header.pgcount = htons(i + 1);
266 } else if (dhp->alloMap[i] == EPP) {
267 /* Add the page to the directory. */
269 dhp->alloMap[i] = EPP - 1;
270 dhp->header.pgcount = htons(i + 1);
272 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
277 for (j = 0; j <= EPP - nblobs; j++) {
279 for (k = 0; k < nblobs; k++)
280 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
289 /* Here we have the first index in j. We update the allocation maps
290 * and free up any resources we've got allocated. */
292 dhp->alloMap[i] -= nblobs;
294 for (k = 0; k < nblobs; k++)
295 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
299 DRelease(pp, 0); /* This dir page is unchanged. */
302 /* If we make it here, the directory is full. */
308 AddPage(void *dir, int pageno)
309 { /* Add a page to a directory. */
311 register struct PageHeader *pp;
313 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
314 pp->tag = htons(1234);
317 pp->freecount = EPP - 1; /* The first dude is already allocated */
318 pp->freebitmap[0] = 0x01;
319 for (i = 1; i < EPP / 8; i++) /* It's a constant */
320 pp->freebitmap[i] = 0;
324 /* Free a whole bunch of directory entries. */
327 FreeBlobs(void *dir, register int firstblob, int nblobs)
331 struct DirHeader *dhp;
332 struct PageHeader *pp;
333 page = firstblob / EPP;
334 firstblob -= EPP * page; /* convert to page-relative entry */
335 dhp = (struct DirHeader *)DRead(dir, 0);
339 dhp->alloMap[page] += nblobs;
341 pp = (struct PageHeader *)DRead(dir, page);
343 for (i = 0; i < nblobs; i++)
344 pp->freebitmap[(firstblob + i) >> 3] &=
345 ~(1 << ((firstblob + i) & 7));
350 * Format an empty directory properly. Note that the first 13 entries in a
351 * directory header page are allocated, 1 to the page header, 4 to the
352 * allocation map and 8 to the hash table.
355 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
358 register struct DirHeader *dhp;
359 dhp = (struct DirHeader *)DNew(dir, 0);
360 dhp->header.pgcount = htons(1);
361 dhp->header.tag = htons(1234);
362 dhp->header.freecount = (EPP - DHE - 1);
363 dhp->header.freebitmap[0] = 0xff;
364 dhp->header.freebitmap[1] = 0x1f;
365 for (i = 2; i < EPP / 8; i++)
366 dhp->header.freebitmap[i] = 0;
367 dhp->alloMap[0] = (EPP - DHE - 1);
368 for (i = 1; i < MAXPAGES; i++)
369 dhp->alloMap[i] = EPP;
370 for (i = 0; i < NHASHENT; i++)
371 dhp->hashTable[i] = 0;
373 Create(dir, ".", me);
374 Create(dir, "..", parent); /* Virtue is its own .. */
378 /* Look up a file name in directory. */
381 Lookup(void *dir, char *entry, void *voidfid)
383 afs_int32 *fid = (afs_int32 *) voidfid;
384 register struct DirEntry *firstitem;
385 unsigned short *previtem;
387 firstitem = FindItem(dir, entry, &previtem);
390 DRelease(previtem, 0);
391 fid[1] = ntohl(firstitem->fid.vnode);
392 fid[2] = ntohl(firstitem->fid.vunique);
393 DRelease(firstitem, 0);
397 /* Look up a file name in directory. */
400 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
402 afs_int32 *fid = (afs_int32 *) voidfid;
403 register struct DirEntry *firstitem;
404 unsigned short *previtem;
406 firstitem = FindItem(dir, entry, &previtem);
409 DRelease(previtem, 0);
410 fid[1] = ntohl(firstitem->fid.vnode);
411 fid[2] = ntohl(firstitem->fid.vunique);
413 *offsetp = DVOffset(firstitem);
414 DRelease(firstitem, 0);
419 EnumerateDir(void *dir, int (*hookproc) (), void *hook)
421 /* Enumerate the contents of a directory. */
424 register struct DirHeader *dhp;
425 register struct DirEntry *ep;
427 dhp = (struct DirHeader *)DRead(dir, 0);
429 return EIO; /* first page should be there */
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. */
436 ep = GetBlob(dir, num);
439 /* we failed, return why */
445 num = ntohs(ep->next);
446 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
447 ntohl(ep->fid.vunique));
458 /* Enumerate the contents of a directory. */
461 register struct DirHeader *dhp;
462 register struct DirEntry *ep;
463 dhp = (struct DirHeader *)DRead(dir, 0);
466 for (i = 0; i < NHASHENT; i++) {
467 /* For each hash chain, enumerate everyone on the list. */
468 num = ntohs(dhp->hashTable[i]);
470 /* Walk down the hash table list. */
471 ep = GetBlob(dir, num);
474 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
479 num = ntohs(ep->next);
488 GetBlob(void *dir, afs_int32 blobno)
490 /* Return a pointer to an entry, given its number. */
492 ep = DRead(dir, blobno >> LEPP);
495 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
499 DirHash(register char *string)
501 /* Hash a string to a number between 0 and NHASHENT. */
502 register unsigned char tc;
506 while ((tc = (*string++))) {
510 tval = hval & (NHASHENT - 1);
514 tval = NHASHENT - tval;
518 static struct DirEntry *
519 FindItem(void *dir, char *ename, unsigned short **previtem)
521 /* 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. */
523 register struct DirHeader *dhp;
524 register unsigned short *lp;
525 register struct DirEntry *tp;
527 dhp = (struct DirHeader *)DRead(dir, 0);
530 if (dhp->hashTable[i] == 0) {
535 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
540 lp = &(dhp->hashTable[i]);
542 /* Look at each hash conflict entry. */
543 if (!strcmp(ename, tp->name)) {
544 /* Found our entry. */
551 /* The end of the line */
552 DRelease(lp, 0); /* Release all locks. */
555 tp = GetBlob(dir, (u_short) ntohs(tp->next));