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 /* Local static prototypes */
134 static struct DirEntry *FindItem(void *dir, char *ename,
135 unsigned short **previtem);
139 NameBlobs(char *name)
140 { /* Find out how many entries are required to store a name. */
142 i = strlen(name) + 1;
143 return 1 + ((i + 15) >> 5);
146 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
149 Create(void *dir, char *entry, void *voidfid)
151 afs_int32 *vfid = (afs_int32 *) voidfid;
154 register struct DirEntry *ep;
155 unsigned short *pp = NULL;
156 register struct DirHeader *dhp;
158 /* check name quality */
161 /* First check if file already exists. */
162 ep = FindItem(dir, entry, &pp);
168 blobs = NameBlobs(entry); /* number of entries required */
169 firstelt = FindBlobs(dir, blobs);
171 return EFBIG; /* directory is full */
172 /* First, we fill in the directory entry. */
173 ep = GetBlob(dir, firstelt);
177 ep->fid.vnode = htonl(vfid[1]);
178 ep->fid.vunique = htonl(vfid[2]);
179 strcpy(ep->name, entry);
180 /* Now we just have to thread it on the hash table list. */
181 dhp = (struct DirHeader *)DRead(dir, 0);
187 ep->next = dhp->hashTable[i];
188 dhp->hashTable[i] = htons(firstelt);
198 struct DirHeader *dhp;
199 dhp = (struct DirHeader *)DRead(dir, 0);
202 if (dhp->header.pgcount != 0)
203 ctr = ntohs(dhp->header.pgcount);
205 /* old style, count the pages */
207 for (i = 0; i < MAXPAGES; i++)
208 if (dhp->alloMap[i] != EPP)
212 return ctr * AFS_PAGESIZE;
216 Delete(void *dir, char *entry)
218 /* Delete an entry from a directory, including update of all free entry descriptors. */
220 register struct DirEntry *firstitem;
221 unsigned short *previtem;
222 firstitem = FindItem(dir, entry, &previtem);
225 *previtem = firstitem->next;
226 DRelease(previtem, 1);
227 index = DVOffset(firstitem) / 32;
228 nitems = NameBlobs(firstitem->name);
229 DRelease(firstitem, 0);
230 FreeBlobs(dir, index, nitems);
235 FindBlobs(void *dir, int nblobs)
237 /* Find a bunch of contiguous entries; at least nblobs in a row. */
238 register int i, j, k;
240 register struct DirHeader *dhp;
241 struct PageHeader *pp;
244 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
247 for (i = 0; i < BIGMAXPAGES; i++) {
248 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
249 /* if page could contain enough entries */
250 /* If there are EPP free entries, then the page is not even allocated. */
252 /* this pages exists past the end of the old-style dir */
253 pgcount = ntohs(dhp->header.pgcount);
256 dhp->header.pgcount = htons(pgcount);
258 if (i > pgcount - 1) {
259 /* this page is bigger than last allocated page */
261 dhp->header.pgcount = htons(i + 1);
263 } else if (dhp->alloMap[i] == EPP) {
264 /* Add the page to the directory. */
266 dhp->alloMap[i] = EPP - 1;
267 dhp->header.pgcount = htons(i + 1);
269 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
274 for (j = 0; j <= EPP - nblobs; j++) {
276 for (k = 0; k < nblobs; k++)
277 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
286 /* Here we have the first index in j. We update the allocation maps
287 * and free up any resources we've got allocated. */
289 dhp->alloMap[i] -= nblobs;
291 for (k = 0; k < nblobs; k++)
292 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
296 DRelease(pp, 0); /* This dir page is unchanged. */
299 /* If we make it here, the directory is full. */
305 AddPage(void *dir, int pageno)
306 { /* Add a page to a directory. */
308 register struct PageHeader *pp;
310 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
311 pp->tag = htons(1234);
314 pp->freecount = EPP - 1; /* The first dude is already allocated */
315 pp->freebitmap[0] = 0x01;
316 for (i = 1; i < EPP / 8; i++) /* It's a constant */
317 pp->freebitmap[i] = 0;
321 /* Free a whole bunch of directory entries. */
324 FreeBlobs(void *dir, register int firstblob, int nblobs)
328 struct DirHeader *dhp;
329 struct PageHeader *pp;
330 page = firstblob / EPP;
331 firstblob -= EPP * page; /* convert to page-relative entry */
332 dhp = (struct DirHeader *)DRead(dir, 0);
336 dhp->alloMap[page] += nblobs;
338 pp = (struct PageHeader *)DRead(dir, page);
340 for (i = 0; i < nblobs; i++)
341 pp->freebitmap[(firstblob + i) >> 3] &=
342 ~(1 << ((firstblob + i) & 7));
347 * Format an empty directory properly. Note that the first 13 entries in a
348 * directory header page are allocated, 1 to the page header, 4 to the
349 * allocation map and 8 to the hash table.
352 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
355 register struct DirHeader *dhp;
356 dhp = (struct DirHeader *)DNew(dir, 0);
357 dhp->header.pgcount = htons(1);
358 dhp->header.tag = htons(1234);
359 dhp->header.freecount = (EPP - DHE - 1);
360 dhp->header.freebitmap[0] = 0xff;
361 dhp->header.freebitmap[1] = 0x1f;
362 for (i = 2; i < EPP / 8; i++)
363 dhp->header.freebitmap[i] = 0;
364 dhp->alloMap[0] = (EPP - DHE - 1);
365 for (i = 1; i < MAXPAGES; i++)
366 dhp->alloMap[i] = EPP;
367 for (i = 0; i < NHASHENT; i++)
368 dhp->hashTable[i] = 0;
370 Create(dir, ".", me);
371 Create(dir, "..", parent); /* Virtue is its own .. */
375 /* Look up a file name in directory. */
378 Lookup(void *dir, char *entry, void *voidfid)
380 afs_int32 *fid = (afs_int32 *) voidfid;
381 register struct DirEntry *firstitem;
382 unsigned short *previtem;
384 firstitem = FindItem(dir, entry, &previtem);
387 DRelease(previtem, 0);
388 fid[1] = ntohl(firstitem->fid.vnode);
389 fid[2] = ntohl(firstitem->fid.vunique);
390 DRelease(firstitem, 0);
394 /* Look up a file name in directory. */
397 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
399 afs_int32 *fid = (afs_int32 *) voidfid;
400 register struct DirEntry *firstitem;
401 unsigned short *previtem;
403 firstitem = FindItem(dir, entry, &previtem);
406 DRelease(previtem, 0);
407 fid[1] = ntohl(firstitem->fid.vnode);
408 fid[2] = ntohl(firstitem->fid.vunique);
410 *offsetp = DVOffset(firstitem);
411 DRelease(firstitem, 0);
416 EnumerateDir(void *dir, int (*hookproc) (), void *hook)
418 /* Enumerate the contents of a directory. */
421 register struct DirHeader *dhp;
422 register struct DirEntry *ep;
424 dhp = (struct DirHeader *)DRead(dir, 0);
426 return EIO; /* first page should be there */
427 for (i = 0; i < NHASHENT; i++) {
428 /* For each hash chain, enumerate everyone on the list. */
429 num = ntohs(dhp->hashTable[i]);
431 /* Walk down the hash table list. */
433 ep = GetBlob(dir, num);
436 /* we failed, return why */
442 num = ntohs(ep->next);
443 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
444 ntohl(ep->fid.vunique));
455 /* Enumerate the contents of a directory. */
458 register struct DirHeader *dhp;
459 register struct DirEntry *ep;
460 dhp = (struct DirHeader *)DRead(dir, 0);
463 for (i = 0; i < NHASHENT; i++) {
464 /* For each hash chain, enumerate everyone on the list. */
465 num = ntohs(dhp->hashTable[i]);
467 /* Walk down the hash table list. */
468 ep = GetBlob(dir, num);
471 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
476 num = ntohs(ep->next);
485 GetBlob(void *dir, afs_int32 blobno)
487 /* Return a pointer to an entry, given its number. */
489 ep = DRead(dir, blobno >> LEPP);
492 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
496 DirHash(register char *string)
498 /* Hash a string to a number between 0 and NHASHENT. */
499 register unsigned char tc;
503 while ((tc = (*string++))) {
507 tval = hval & (NHASHENT - 1);
511 tval = NHASHENT - tval;
515 static struct DirEntry *
516 FindItem(void *dir, char *ename, unsigned short **previtem)
518 /* 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. */
520 register struct DirHeader *dhp;
521 register unsigned short *lp;
522 register struct DirEntry *tp;
524 dhp = (struct DirHeader *)DRead(dir, 0);
527 if (dhp->hashTable[i] == 0) {
532 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
537 lp = &(dhp->hashTable[i]);
539 /* Look at each hash conflict entry. */
540 if (!strcmp(ename, tp->name)) {
541 /* Found our entry. */
548 /* The end of the line */
549 DRelease(lp, 0); /* Release all locks. */
552 tp = GetBlob(dir, (u_short) ntohs(tp->next));