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>
14 # if !defined(UKERNEL)
20 # include "h/sysmacros.h"
21 # include "h/signal.h"
25 # if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
28 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
29 # include "h/kernel.h"
32 # if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
33 # include "afs/sysincludes.h"
35 # if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_OBSD48_ENV)
37 # endif /* AFS_SGI64_ENV */
40 # include <sys/mount.h>
41 # include <sys/vnode.h>
42 # include <ufs/inode.h>
44 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
47 # ifndef AFS_LINUX20_ENV
48 # include "netinet/in.h"
52 /* These are needed because afs_prototypes.h is not included here */
54 extern void *DRead(struct dcache *adc, int page);
55 extern void *DNew(struct dcache *adc, int page);
57 # else /* !defined(UKERNEL) */
58 # include "afs/stds.h"
59 # include "afs/sysincludes.h"
62 /* These are needed because afs_prototypes.h is not included here */
63 extern void *DRead(afs_int32 *fid, int page);
64 extern void *DNew(afs_int32 *fid, int page);
66 # endif /* !defined(UKERNEL) */
67 # include "afs/afs_osi.h"
71 # ifdef AFS_LINUX20_ENV
72 # include "h/string.h"
75 /* generic renaming */
76 # define NameBlobs afs_dir_NameBlobs
77 # define GetBlob afs_dir_GetBlob
78 # define Create afs_dir_Create
79 # define Length afs_dir_Length
80 # define Delete afs_dir_Delete
81 # define MakeDir afs_dir_MakeDir
82 # define Lookup afs_dir_Lookup
83 # define LookupOffset afs_dir_LookupOffset
84 # define EnumerateDir afs_dir_EnumerateDir
85 # define IsEmpty afs_dir_IsEmpty
86 # define InverseLookup afs_dir_InverseLookup
87 # define ChangeFid afs_dir_ChangeFid
97 /* Local static prototypes */
98 static struct DirEntry *FindItem(void *dir, char *ename,
99 unsigned short **previtem);
102 /* Find out how many entries are required to store a name. */
104 NameBlobs(char *name)
107 i = strlen(name) + 1;
108 return 1 + ((i + 15) >> 5);
111 /* Create an entry in a file. Dir is a file representation, while entry is
114 Create(void *dir, char *entry, void *voidfid)
116 afs_int32 *vfid = (afs_int32 *) voidfid;
120 unsigned short *pp = NULL;
121 struct DirHeader *dhp;
123 /* check name quality */
126 /* First check if file already exists. */
127 ep = FindItem(dir, entry, &pp);
133 blobs = NameBlobs(entry); /* number of entries required */
134 firstelt = FindBlobs(dir, blobs);
136 return EFBIG; /* directory is full */
137 /* First, we fill in the directory entry. */
138 ep = GetBlob(dir, firstelt);
142 ep->fid.vnode = htonl(vfid[1]);
143 ep->fid.vunique = htonl(vfid[2]);
144 strcpy(ep->name, entry);
145 /* Now we just have to thread it on the hash table list. */
146 dhp = (struct DirHeader *)DRead(dir, 0);
152 ep->next = dhp->hashTable[i];
153 dhp->hashTable[i] = htons(firstelt);
163 struct DirHeader *dhp;
164 dhp = (struct DirHeader *)DRead(dir, 0);
167 if (dhp->header.pgcount != 0)
168 ctr = ntohs(dhp->header.pgcount);
170 /* old style, count the pages */
172 for (i = 0; i < MAXPAGES; i++)
173 if (dhp->alloMap[i] != EPP)
177 return ctr * AFS_PAGESIZE;
181 Delete(void *dir, char *entry)
183 /* Delete an entry from a directory, including update of all free entry descriptors. */
185 struct DirEntry *firstitem;
186 unsigned short *previtem;
187 firstitem = FindItem(dir, entry, &previtem);
190 *previtem = firstitem->next;
191 DRelease(previtem, 1);
192 index = DVOffset(firstitem) / 32;
193 nitems = NameBlobs(firstitem->name);
194 DRelease(firstitem, 0);
195 FreeBlobs(dir, index, nitems);
200 FindBlobs(void *dir, int nblobs)
202 /* Find a bunch of contiguous entries; at least nblobs in a row. */
205 struct DirHeader *dhp;
206 struct PageHeader *pp;
209 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
212 for (i = 0; i < BIGMAXPAGES; i++) {
213 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
214 /* if page could contain enough entries */
215 /* If there are EPP free entries, then the page is not even allocated. */
217 /* this pages exists past the end of the old-style dir */
218 pgcount = ntohs(dhp->header.pgcount);
221 dhp->header.pgcount = htons(pgcount);
223 if (i > pgcount - 1) {
224 /* this page is bigger than last allocated page */
226 dhp->header.pgcount = htons(i + 1);
228 } else if (dhp->alloMap[i] == EPP) {
229 /* Add the page to the directory. */
231 dhp->alloMap[i] = EPP - 1;
232 dhp->header.pgcount = htons(i + 1);
234 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
239 for (j = 0; j <= EPP - nblobs; j++) {
241 for (k = 0; k < nblobs; k++)
242 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
251 /* Here we have the first index in j. We update the allocation maps
252 * and free up any resources we've got allocated. */
254 dhp->alloMap[i] -= nblobs;
256 for (k = 0; k < nblobs; k++)
257 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
261 DRelease(pp, 0); /* This dir page is unchanged. */
264 /* If we make it here, the directory is full. */
270 AddPage(void *dir, int pageno)
271 { /* Add a page to a directory. */
273 struct PageHeader *pp;
275 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
276 pp->tag = htons(1234);
279 pp->freecount = EPP - 1; /* The first dude is already allocated */
280 pp->freebitmap[0] = 0x01;
281 for (i = 1; i < EPP / 8; i++) /* It's a constant */
282 pp->freebitmap[i] = 0;
286 /* Free a whole bunch of directory entries. */
289 FreeBlobs(void *dir, int firstblob, int nblobs)
293 struct DirHeader *dhp;
294 struct PageHeader *pp;
295 page = firstblob / EPP;
296 firstblob -= EPP * page; /* convert to page-relative entry */
297 dhp = (struct DirHeader *)DRead(dir, 0);
301 dhp->alloMap[page] += nblobs;
303 pp = (struct PageHeader *)DRead(dir, page);
305 for (i = 0; i < nblobs; i++)
306 pp->freebitmap[(firstblob + i) >> 3] &=
307 ~(1 << ((firstblob + i) & 7));
312 * Format an empty directory properly. Note that the first 13 entries in a
313 * directory header page are allocated, 1 to the page header, 4 to the
314 * allocation map and 8 to the hash table.
317 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
320 struct DirHeader *dhp;
321 dhp = (struct DirHeader *)DNew(dir, 0);
322 dhp->header.pgcount = htons(1);
323 dhp->header.tag = htons(1234);
324 dhp->header.freecount = (EPP - DHE - 1);
325 dhp->header.freebitmap[0] = 0xff;
326 dhp->header.freebitmap[1] = 0x1f;
327 for (i = 2; i < EPP / 8; i++)
328 dhp->header.freebitmap[i] = 0;
329 dhp->alloMap[0] = (EPP - DHE - 1);
330 for (i = 1; i < MAXPAGES; i++)
331 dhp->alloMap[i] = EPP;
332 for (i = 0; i < NHASHENT; i++)
333 dhp->hashTable[i] = 0;
335 Create(dir, ".", me);
336 Create(dir, "..", parent); /* Virtue is its own .. */
340 /* Look up a file name in directory. */
343 Lookup(void *dir, char *entry, void *voidfid)
345 afs_int32 *fid = (afs_int32 *) voidfid;
346 struct DirEntry *firstitem;
347 unsigned short *previtem;
349 firstitem = FindItem(dir, entry, &previtem);
352 DRelease(previtem, 0);
353 fid[1] = ntohl(firstitem->fid.vnode);
354 fid[2] = ntohl(firstitem->fid.vunique);
355 DRelease(firstitem, 0);
359 /* Look up a file name in directory. */
362 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
364 afs_int32 *fid = (afs_int32 *) voidfid;
365 struct DirEntry *firstitem;
366 unsigned short *previtem;
368 firstitem = FindItem(dir, entry, &previtem);
371 DRelease(previtem, 0);
372 fid[1] = ntohl(firstitem->fid.vnode);
373 fid[2] = ntohl(firstitem->fid.vunique);
375 *offsetp = DVOffset(firstitem);
376 DRelease(firstitem, 0);
381 EnumerateDir(void *dir, int (*hookproc) (void *dir, char *name,
382 afs_int32 vnode, afs_int32 unique),
385 /* Enumerate the contents of a directory.
386 * Break when hook function returns non 0.
390 struct DirHeader *dhp;
394 dhp = (struct DirHeader *)DRead(dir, 0);
396 return EIO; /* first page should be there */
398 for (i = 0; i < NHASHENT; i++) {
399 /* For each hash chain, enumerate everyone on the list. */
400 num = ntohs(dhp->hashTable[i]);
402 /* Walk down the hash table list. */
404 ep = GetBlob(dir, num);
407 /* we failed, return why */
414 num = ntohs(ep->next);
415 code = (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
416 ntohl(ep->fid.vunique));
429 /* Enumerate the contents of a directory. */
432 struct DirHeader *dhp;
434 dhp = (struct DirHeader *)DRead(dir, 0);
437 for (i = 0; i < NHASHENT; i++) {
438 /* For each hash chain, enumerate everyone on the list. */
439 num = ntohs(dhp->hashTable[i]);
441 /* Walk down the hash table list. */
442 ep = GetBlob(dir, num);
445 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
450 num = ntohs(ep->next);
459 GetBlob(void *dir, afs_int32 blobno)
461 /* Return a pointer to an entry, given its number. */
463 ep = DRead(dir, blobno >> LEPP);
466 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
470 DirHash(char *string)
472 /* Hash a string to a number between 0 and NHASHENT. */
477 while ((tc = (*string++))) {
481 tval = hval & (NHASHENT - 1);
484 else if (hval >= 1<<31)
485 tval = NHASHENT - tval;
490 /* Find a directory entry, given its name. This entry returns a pointer
491 * to a locked buffer, and a pointer to a locked buffer (in previtem)
492 * referencing the found item (to aid the delete code). If no entry is
493 * found, however, no items are left locked, and a null pointer is
494 * returned instead. */
496 static struct DirEntry *
497 FindItem(void *dir, char *ename, unsigned short **previtem)
500 struct DirHeader *dhp;
504 dhp = (struct DirHeader *)DRead(dir, 0);
507 if (dhp->hashTable[i] == 0) {
512 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
517 lp = &(dhp->hashTable[i]);
519 /* Look at each hash conflict entry. */
520 if (!strcmp(ename, tp->name)) {
521 /* Found our entry. */
528 /* The end of the line */
529 DRelease(lp, 0); /* Release all locks. */
532 tp = GetBlob(dir, (u_short) ntohs(tp->next));
540 static struct DirEntry *
541 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique)
543 /* Find a directory entry, given the vnode and uniquifier of a object.
544 * This entry returns a pointer to a locked buffer. If no entry is found,
545 * however, no items are left locked, and a null pointer is returned
549 struct DirHeader *dhp;
552 dhp = (struct DirHeader *) DRead(dir,0);
554 for (i=0; i<NHASHENT; i++) {
555 if (dhp->hashTable[i] != 0) {
556 tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
557 if (!tp) { /* should not happen */
562 if (vnode == ntohl(tp->fid.vnode)
563 && unique == ntohl(tp->fid.vunique)) {
570 tp = GetBlob(dir,(u_short)ntohs(tp->next));
581 InverseLookup (void *dir, afs_uint32 vnode, afs_uint32 unique, char *name,
584 /* Look for the name pointing to given vnode and unique in a directory */
585 struct DirEntry *entry;
588 entry = FindFid(dir, vnode, unique);
591 if (strlen(entry->name) >= length)
594 strcpy(name, entry->name);
600 * Change an entry fid.
603 * \param entry The entry name.
604 * \param old_fid The old find in MKFid format (host order).
605 * It can be omitted if you don't need a safety check...
606 * \param new_fid The new find in MKFid format (host order).
608 int ChangeFid(void *dir,
613 struct DirEntry *firstitem;
614 unsigned short *previtem;
615 struct MKFid *fid_old = (struct MKFid *) old_fid;
616 struct MKFid *fid_new = (struct MKFid *) new_fid;
619 firstitem = FindItem(dir, entry, &previtem);
620 if (firstitem == 0) {
623 DRelease(previtem, 1);
626 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
627 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
629 firstitem->fid.vnode = htonl(fid_new->vnode);
630 firstitem->fid.vunique = htonl(fid_new->vunique);
633 DRelease(firstitem, 1);