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>
21 #include "h/sysmacros.h"
26 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
29 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
33 #if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
34 #include "afs/sysincludes.h"
36 #if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN_ENV)
38 #endif /* AFS_SGI64_ENV */
41 #include <sys/mount.h>
42 #include <sys/vnode.h>
43 #include <ufs/inode.h>
45 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
48 #ifndef AFS_LINUX20_ENV
49 #include "netinet/in.h"
53 /* These are needed because afs_prototypes.h is not included here */
55 extern void *DRead(struct dcache *adc, int page);
56 extern void *DNew(struct dcache *adc, int page);
58 #else /* !defined(UKERNEL) */
60 #include "afs/sysincludes.h"
63 /* These are needed because afs_prototypes.h is not included here */
64 extern void *DRead(register afs_int32 *fid, register int page);
65 extern void *DNew(register afs_int32 *fid, register int page);
67 #endif /* !defined(UKERNEL) */
68 #include "afs/afs_osi.h"
72 #ifdef AFS_LINUX20_ENV
76 /* generic renaming */
77 #define NameBlobs afs_dir_NameBlobs
78 #define GetBlob afs_dir_GetBlob
79 #define Create afs_dir_Create
80 #define Length afs_dir_Length
81 #define Delete afs_dir_Delete
82 #define MakeDir afs_dir_MakeDir
83 #define Lookup afs_dir_Lookup
84 #define LookupOffset afs_dir_LookupOffset
85 #define EnumerateDir afs_dir_EnumerateDir
86 #define IsEmpty afs_dir_IsEmpty
87 #define InverseLookup afs_dir_InverseLookup
89 #if defined(AFS_DISCON_ENV)
90 #define ChangeFid afs_dir_ChangeFid
98 # include <sys/types.h>
102 #include <winsock2.h>
104 #include <netinet/in.h>
112 /* Local static prototypes */
113 static struct DirEntry *FindItem(void *dir, char *ename,
114 unsigned short **previtem);
117 /* Find out how many entries are required to store a name. */
119 NameBlobs(char *name)
122 i = strlen(name) + 1;
123 return 1 + ((i + 15) >> 5);
126 /* Create an entry in a file. Dir is a file representation, while entry is
129 Create(void *dir, char *entry, void *voidfid)
131 afs_int32 *vfid = (afs_int32 *) voidfid;
134 register struct DirEntry *ep;
135 unsigned short *pp = NULL;
136 register struct DirHeader *dhp;
138 /* check name quality */
141 /* First check if file already exists. */
142 ep = FindItem(dir, entry, &pp);
148 blobs = NameBlobs(entry); /* number of entries required */
149 firstelt = FindBlobs(dir, blobs);
151 return EFBIG; /* directory is full */
152 /* First, we fill in the directory entry. */
153 ep = GetBlob(dir, firstelt);
157 ep->fid.vnode = htonl(vfid[1]);
158 ep->fid.vunique = htonl(vfid[2]);
159 strcpy(ep->name, entry);
160 /* Now we just have to thread it on the hash table list. */
161 dhp = (struct DirHeader *)DRead(dir, 0);
167 ep->next = dhp->hashTable[i];
168 dhp->hashTable[i] = htons(firstelt);
178 struct DirHeader *dhp;
179 dhp = (struct DirHeader *)DRead(dir, 0);
182 if (dhp->header.pgcount != 0)
183 ctr = ntohs(dhp->header.pgcount);
185 /* old style, count the pages */
187 for (i = 0; i < MAXPAGES; i++)
188 if (dhp->alloMap[i] != EPP)
192 return ctr * AFS_PAGESIZE;
196 Delete(void *dir, char *entry)
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);
205 *previtem = firstitem->next;
206 DRelease(previtem, 1);
207 index = DVOffset(firstitem) / 32;
208 nitems = NameBlobs(firstitem->name);
209 DRelease(firstitem, 0);
210 FreeBlobs(dir, index, nitems);
215 FindBlobs(void *dir, int nblobs)
217 /* Find a bunch of contiguous entries; at least nblobs in a row. */
218 register int i, j, k;
220 register struct DirHeader *dhp;
221 struct PageHeader *pp;
224 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
227 for (i = 0; i < BIGMAXPAGES; i++) {
228 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
229 /* if page could contain enough entries */
230 /* If there are EPP free entries, then the page is not even allocated. */
232 /* this pages exists past the end of the old-style dir */
233 pgcount = ntohs(dhp->header.pgcount);
236 dhp->header.pgcount = htons(pgcount);
238 if (i > pgcount - 1) {
239 /* this page is bigger than last allocated page */
241 dhp->header.pgcount = htons(i + 1);
243 } else if (dhp->alloMap[i] == EPP) {
244 /* Add the page to the directory. */
246 dhp->alloMap[i] = EPP - 1;
247 dhp->header.pgcount = htons(i + 1);
249 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
254 for (j = 0; j <= EPP - nblobs; j++) {
256 for (k = 0; k < nblobs; k++)
257 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
266 /* Here we have the first index in j. We update the allocation maps
267 * and free up any resources we've got allocated. */
269 dhp->alloMap[i] -= nblobs;
271 for (k = 0; k < nblobs; k++)
272 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
276 DRelease(pp, 0); /* This dir page is unchanged. */
279 /* If we make it here, the directory is full. */
285 AddPage(void *dir, int pageno)
286 { /* Add a page to a directory. */
288 register struct PageHeader *pp;
290 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
291 pp->tag = htons(1234);
294 pp->freecount = EPP - 1; /* The first dude is already allocated */
295 pp->freebitmap[0] = 0x01;
296 for (i = 1; i < EPP / 8; i++) /* It's a constant */
297 pp->freebitmap[i] = 0;
301 /* Free a whole bunch of directory entries. */
304 FreeBlobs(void *dir, register int firstblob, int nblobs)
308 struct DirHeader *dhp;
309 struct PageHeader *pp;
310 page = firstblob / EPP;
311 firstblob -= EPP * page; /* convert to page-relative entry */
312 dhp = (struct DirHeader *)DRead(dir, 0);
316 dhp->alloMap[page] += nblobs;
318 pp = (struct PageHeader *)DRead(dir, page);
320 for (i = 0; i < nblobs; i++)
321 pp->freebitmap[(firstblob + i) >> 3] &=
322 ~(1 << ((firstblob + i) & 7));
327 * Format an empty directory properly. Note that the first 13 entries in a
328 * directory header page are allocated, 1 to the page header, 4 to the
329 * allocation map and 8 to the hash table.
332 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
335 register struct DirHeader *dhp;
336 dhp = (struct DirHeader *)DNew(dir, 0);
337 dhp->header.pgcount = htons(1);
338 dhp->header.tag = htons(1234);
339 dhp->header.freecount = (EPP - DHE - 1);
340 dhp->header.freebitmap[0] = 0xff;
341 dhp->header.freebitmap[1] = 0x1f;
342 for (i = 2; i < EPP / 8; i++)
343 dhp->header.freebitmap[i] = 0;
344 dhp->alloMap[0] = (EPP - DHE - 1);
345 for (i = 1; i < MAXPAGES; i++)
346 dhp->alloMap[i] = EPP;
347 for (i = 0; i < NHASHENT; i++)
348 dhp->hashTable[i] = 0;
350 Create(dir, ".", me);
351 Create(dir, "..", parent); /* Virtue is its own .. */
355 /* Look up a file name in directory. */
358 Lookup(void *dir, char *entry, void *voidfid)
360 afs_int32 *fid = (afs_int32 *) voidfid;
361 register struct DirEntry *firstitem;
362 unsigned short *previtem;
364 firstitem = FindItem(dir, entry, &previtem);
367 DRelease(previtem, 0);
368 fid[1] = ntohl(firstitem->fid.vnode);
369 fid[2] = ntohl(firstitem->fid.vunique);
370 DRelease(firstitem, 0);
374 /* Look up a file name in directory. */
377 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
379 afs_int32 *fid = (afs_int32 *) voidfid;
380 register struct DirEntry *firstitem;
381 unsigned short *previtem;
383 firstitem = FindItem(dir, entry, &previtem);
386 DRelease(previtem, 0);
387 fid[1] = ntohl(firstitem->fid.vnode);
388 fid[2] = ntohl(firstitem->fid.vunique);
390 *offsetp = DVOffset(firstitem);
391 DRelease(firstitem, 0);
396 EnumerateDir(void *dir, int (*hookproc) (void *dir, char *name,
397 afs_int32 vnode, afs_int32 unique),
400 /* Enumerate the contents of a directory.
401 * Break when hook function returns non 0.
405 register struct DirHeader *dhp;
406 register struct DirEntry *ep;
409 dhp = (struct DirHeader *)DRead(dir, 0);
411 return EIO; /* first page should be there */
413 for (i = 0; i < NHASHENT; i++) {
414 /* For each hash chain, enumerate everyone on the list. */
415 num = ntohs(dhp->hashTable[i]);
417 /* Walk down the hash table list. */
419 ep = GetBlob(dir, num);
422 /* we failed, return why */
429 num = ntohs(ep->next);
430 code = (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
431 ntohl(ep->fid.vunique));
444 /* Enumerate the contents of a directory. */
447 register struct DirHeader *dhp;
448 register struct DirEntry *ep;
449 dhp = (struct DirHeader *)DRead(dir, 0);
452 for (i = 0; i < NHASHENT; i++) {
453 /* For each hash chain, enumerate everyone on the list. */
454 num = ntohs(dhp->hashTable[i]);
456 /* Walk down the hash table list. */
457 ep = GetBlob(dir, num);
460 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
465 num = ntohs(ep->next);
474 GetBlob(void *dir, afs_int32 blobno)
476 /* Return a pointer to an entry, given its number. */
478 ep = DRead(dir, blobno >> LEPP);
481 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
485 DirHash(register char *string)
487 /* Hash a string to a number between 0 and NHASHENT. */
488 register unsigned char tc;
492 while ((tc = (*string++))) {
496 tval = hval & (NHASHENT - 1);
499 else if (hval >= 1<<31)
500 tval = NHASHENT - tval;
505 /* Find a directory entry, given its name. This entry returns a pointer
506 * to a locked buffer, and a pointer to a locked buffer (in previtem)
507 * referencing the found item (to aid the delete code). If no entry is
508 * found, however, no items are left locked, and a null pointer is
509 * returned instead. */
511 static struct DirEntry *
512 FindItem(void *dir, char *ename, unsigned short **previtem)
515 register struct DirHeader *dhp;
516 register unsigned short *lp;
517 register struct DirEntry *tp;
519 dhp = (struct DirHeader *)DRead(dir, 0);
522 if (dhp->hashTable[i] == 0) {
527 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
532 lp = &(dhp->hashTable[i]);
534 /* Look at each hash conflict entry. */
535 if (!strcmp(ename, tp->name)) {
536 /* Found our entry. */
543 /* The end of the line */
544 DRelease(lp, 0); /* Release all locks. */
547 tp = GetBlob(dir, (u_short) ntohs(tp->next));
555 static struct DirEntry *
556 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique)
558 /* Find a directory entry, given the vnode and uniquifier of a object.
559 * This entry returns a pointer to a locked buffer. If no entry is found,
560 * however, no items are left locked, and a null pointer is returned
564 register struct DirHeader *dhp;
565 register unsigned short *lp;
566 register struct DirEntry *tp;
567 dhp = (struct DirHeader *) DRead(dir,0);
569 for (i=0; i<NHASHENT; i++) {
570 if (dhp->hashTable[i] != 0) {
571 tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
572 if (!tp) { /* should not happen */
577 if (vnode == ntohl(tp->fid.vnode)
578 && unique == ntohl(tp->fid.vunique)) {
585 tp = GetBlob(dir,(u_short)ntohs(tp->next));
596 InverseLookup (void *dir, afs_uint32 vnode, afs_uint32 unique, char *name,
599 /* Look for the name pointing to given vnode and unique in a directory */
600 register struct DirEntry *entry;
603 entry = FindFid(dir, vnode, unique);
606 if (strlen(entry->name) >= length)
609 strcpy(name, entry->name);
614 #if defined(AFS_DISCON_ENV)
616 * Change an entry fid.
619 * \param entry The entry name.
620 * \param old_fid The old find in MKFid format (host order).
621 * It can be omitted if you don't need a safety check...
622 * \param new_fid The new find in MKFid format (host order).
624 int ChangeFid(void *dir,
629 struct DirEntry *firstitem;
630 unsigned short *previtem;
631 struct MKFid *fid_old = (struct MKFid *) old_fid;
632 struct MKFid *fid_new = (struct MKFid *) new_fid;
635 firstitem = FindItem(dir, entry, &previtem);
636 if (firstitem == 0) {
639 DRelease(previtem, 1);
642 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
643 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
645 firstitem->fid.vnode = htonl(fid_new->vnode);
646 firstitem->fid.vunique = htonl(fid_new->vunique);
649 DRelease(firstitem, 1);