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"
86 struct DirEntry *DRead();
88 #else /* !defined(UKERNEL) */
90 #include "afs/sysincludes.h"
91 #endif /* !defined(UKERNEL) */
92 #include "afs/afs_osi.h"
96 #include "afs/longc_procs.h"
97 #ifdef AFS_LINUX20_ENV
101 /* generic renaming */
102 #define NameBlobs afs_dir_NameBlobs
103 #define GetBlob afs_dir_GetBlob
104 #define Create afs_dir_Create
105 #define Length afs_dir_Length
106 #define Delete afs_dir_Delete
107 #define MakeDir afs_dir_MakeDir
108 #define Lookup afs_dir_Lookup
109 #define LookupOffset afs_dir_LookupOffset
110 #define EnumerateDir afs_dir_EnumerateDir
111 #define IsEmpty afs_dir_IsEmpty
114 # ifdef HAVE_UNISTD_H
117 # include <sys/types.h>
121 #include <winsock2.h>
123 #include <netinet/in.h>
129 #ifdef HAVE_STRINGS_H
137 /* Local static prototypes */
138 static struct DirEntry *FindItem(void *dir, char *ename,
139 unsigned short **previtem);
143 NameBlobs(char *name)
144 { /* Find out how many entries are required to store a name. */
146 i = strlen(name) + 1;
147 return 1 + ((i + 15) >> 5);
150 /* Create an entry in a file. Dir is a file representation, while entry is a string name. */
153 Create(void *dir, char *entry, void *voidfid)
155 afs_int32 *vfid = (afs_int32 *) voidfid;
158 register struct DirEntry *ep;
159 unsigned short *pp = NULL;
160 register struct DirHeader *dhp;
162 /* check name quality */
165 /* First check if file already exists. */
166 ep = FindItem(dir, entry, &pp);
172 blobs = NameBlobs(entry); /* number of entries required */
173 firstelt = FindBlobs(dir, blobs);
175 return EFBIG; /* directory is full */
176 /* First, we fill in the directory entry. */
177 ep = GetBlob(dir, firstelt);
181 ep->fid.vnode = htonl(vfid[1]);
182 ep->fid.vunique = htonl(vfid[2]);
183 strcpy(ep->name, entry);
184 /* Now we just have to thread it on the hash table list. */
185 dhp = (struct DirHeader *)DRead(dir, 0);
191 ep->next = dhp->hashTable[i];
192 dhp->hashTable[i] = htons(firstelt);
202 struct DirHeader *dhp;
203 dhp = (struct DirHeader *)DRead(dir, 0);
206 if (dhp->header.pgcount != 0)
207 ctr = ntohs(dhp->header.pgcount);
209 /* old style, count the pages */
211 for (i = 0; i < MAXPAGES; i++)
212 if (dhp->alloMap[i] != EPP)
216 return ctr * AFS_PAGESIZE;
220 Delete(void *dir, char *entry)
222 /* Delete an entry from a directory, including update of all free entry descriptors. */
224 register struct DirEntry *firstitem;
225 unsigned short *previtem;
226 firstitem = FindItem(dir, entry, &previtem);
229 *previtem = firstitem->next;
230 DRelease(previtem, 1);
231 index = DVOffset(firstitem) / 32;
232 nitems = NameBlobs(firstitem->name);
233 DRelease(firstitem, 0);
234 FreeBlobs(dir, index, nitems);
239 FindBlobs(void *dir, int nblobs)
241 /* Find a bunch of contiguous entries; at least nblobs in a row. */
242 register int i, j, k;
244 register struct DirHeader *dhp;
245 struct PageHeader *pp;
248 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
251 for (i = 0; i < BIGMAXPAGES; i++) {
252 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
253 /* if page could contain enough entries */
254 /* If there are EPP free entries, then the page is not even allocated. */
256 /* this pages exists past the end of the old-style dir */
257 pgcount = ntohs(dhp->header.pgcount);
260 dhp->header.pgcount = htons(pgcount);
262 if (i > pgcount - 1) {
263 /* this page is bigger than last allocated page */
265 dhp->header.pgcount = htons(i + 1);
267 } else if (dhp->alloMap[i] == EPP) {
268 /* Add the page to the directory. */
270 dhp->alloMap[i] = EPP - 1;
271 dhp->header.pgcount = htons(i + 1);
273 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
278 for (j = 0; j <= EPP - nblobs; j++) {
280 for (k = 0; k < nblobs; k++)
281 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
290 /* Here we have the first index in j. We update the allocation maps
291 * and free up any resources we've got allocated. */
293 dhp->alloMap[i] -= nblobs;
295 for (k = 0; k < nblobs; k++)
296 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
300 DRelease(pp, 0); /* This dir page is unchanged. */
303 /* If we make it here, the directory is full. */
309 AddPage(void *dir, int pageno)
310 { /* Add a page to a directory. */
312 register struct PageHeader *pp;
314 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
315 pp->tag = htons(1234);
318 pp->freecount = EPP - 1; /* The first dude is already allocated */
319 pp->freebitmap[0] = 0x01;
320 for (i = 1; i < EPP / 8; i++) /* It's a constant */
321 pp->freebitmap[i] = 0;
325 /* Free a whole bunch of directory entries. */
328 FreeBlobs(void *dir, register int firstblob, int nblobs)
332 struct DirHeader *dhp;
333 struct PageHeader *pp;
334 page = firstblob / EPP;
335 firstblob -= EPP * page; /* convert to page-relative entry */
336 dhp = (struct DirHeader *)DRead(dir, 0);
340 dhp->alloMap[page] += nblobs;
342 pp = (struct PageHeader *)DRead(dir, page);
344 for (i = 0; i < nblobs; i++)
345 pp->freebitmap[(firstblob + i) >> 3] &=
346 ~(1 << ((firstblob + i) & 7));
351 * Format an empty directory properly. Note that the first 13 entries in a
352 * directory header page are allocated, 1 to the page header, 4 to the
353 * allocation map and 8 to the hash table.
356 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
359 register struct DirHeader *dhp;
360 dhp = (struct DirHeader *)DNew(dir, 0);
361 dhp->header.pgcount = htons(1);
362 dhp->header.tag = htons(1234);
363 dhp->header.freecount = (EPP - DHE - 1);
364 dhp->header.freebitmap[0] = 0xff;
365 dhp->header.freebitmap[1] = 0x1f;
366 for (i = 2; i < EPP / 8; i++)
367 dhp->header.freebitmap[i] = 0;
368 dhp->alloMap[0] = (EPP - DHE - 1);
369 for (i = 1; i < MAXPAGES; i++)
370 dhp->alloMap[i] = EPP;
371 for (i = 0; i < NHASHENT; i++)
372 dhp->hashTable[i] = 0;
374 Create(dir, ".", me);
375 Create(dir, "..", parent); /* Virtue is its own .. */
379 /* Look up a file name in directory. */
382 Lookup(void *dir, char *entry, void *voidfid)
384 afs_int32 *fid = (afs_int32 *) voidfid;
385 register struct DirEntry *firstitem;
386 unsigned short *previtem;
388 firstitem = FindItem(dir, entry, &previtem);
391 DRelease(previtem, 0);
392 fid[1] = ntohl(firstitem->fid.vnode);
393 fid[2] = ntohl(firstitem->fid.vunique);
394 DRelease(firstitem, 0);
398 /* Look up a file name in directory. */
401 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
403 afs_int32 *fid = (afs_int32 *) voidfid;
404 register struct DirEntry *firstitem;
405 unsigned short *previtem;
407 firstitem = FindItem(dir, entry, &previtem);
410 DRelease(previtem, 0);
411 fid[1] = ntohl(firstitem->fid.vnode);
412 fid[2] = ntohl(firstitem->fid.vunique);
414 *offsetp = DVOffset(firstitem);
415 DRelease(firstitem, 0);
420 EnumerateDir(void *dir, int (*hookproc) (), void *hook)
422 /* Enumerate the contents of a directory. */
425 register struct DirHeader *dhp;
426 register struct DirEntry *ep;
428 dhp = (struct DirHeader *)DRead(dir, 0);
430 return EIO; /* first page should be there */
431 for (i = 0; i < NHASHENT; i++) {
432 /* For each hash chain, enumerate everyone on the list. */
433 num = ntohs(dhp->hashTable[i]);
435 /* Walk down the hash table list. */
437 ep = GetBlob(dir, num);
440 /* we failed, return why */
446 num = ntohs(ep->next);
447 (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
448 ntohl(ep->fid.vunique));
459 /* Enumerate the contents of a directory. */
462 register struct DirHeader *dhp;
463 register struct DirEntry *ep;
464 dhp = (struct DirHeader *)DRead(dir, 0);
467 for (i = 0; i < NHASHENT; i++) {
468 /* For each hash chain, enumerate everyone on the list. */
469 num = ntohs(dhp->hashTable[i]);
471 /* Walk down the hash table list. */
472 ep = GetBlob(dir, num);
475 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
480 num = ntohs(ep->next);
489 GetBlob(void *dir, afs_int32 blobno)
491 /* Return a pointer to an entry, given its number. */
493 ep = DRead(dir, blobno >> LEPP);
496 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
500 DirHash(register char *string)
502 /* Hash a string to a number between 0 and NHASHENT. */
503 register unsigned char tc;
507 while ((tc = (*string++))) {
511 tval = hval & (NHASHENT - 1);
515 tval = NHASHENT - tval;
519 static struct DirEntry *
520 FindItem(void *dir, char *ename, unsigned short **previtem)
522 /* 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. */
524 register struct DirHeader *dhp;
525 register unsigned short *lp;
526 register struct DirEntry *tp;
528 dhp = (struct DirHeader *)DRead(dir, 0);
531 if (dhp->hashTable[i] == 0) {
536 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
541 lp = &(dhp->hashTable[i]);
543 /* Look at each hash conflict entry. */
544 if (!strcmp(ename, tp->name)) {
545 /* Found our entry. */
552 /* The end of the line */
553 DRelease(lp, 0); /* Release all locks. */
556 tp = GetBlob(dir, (u_short) ntohs(tp->next));