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(afs_int32 *fid, int page);
65 extern void *DNew(afs_int32 *fid, 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
88 #define ChangeFid afs_dir_ChangeFid
95 # include <sys/types.h>
101 #include <netinet/in.h>
109 /* Local static prototypes */
110 static struct DirEntry *FindItem(void *dir, char *ename,
111 unsigned short **previtem);
114 /* Find out how many entries are required to store a name. */
116 NameBlobs(char *name)
119 i = strlen(name) + 1;
120 return 1 + ((i + 15) >> 5);
123 /* Create an entry in a file. Dir is a file representation, while entry is
126 Create(void *dir, char *entry, void *voidfid)
128 afs_int32 *vfid = (afs_int32 *) voidfid;
132 unsigned short *pp = NULL;
133 struct DirHeader *dhp;
135 /* check name quality */
138 /* First check if file already exists. */
139 ep = FindItem(dir, entry, &pp);
145 blobs = NameBlobs(entry); /* number of entries required */
146 firstelt = FindBlobs(dir, blobs);
148 return EFBIG; /* directory is full */
149 /* First, we fill in the directory entry. */
150 ep = GetBlob(dir, firstelt);
154 ep->fid.vnode = htonl(vfid[1]);
155 ep->fid.vunique = htonl(vfid[2]);
156 strcpy(ep->name, entry);
157 /* Now we just have to thread it on the hash table list. */
158 dhp = (struct DirHeader *)DRead(dir, 0);
164 ep->next = dhp->hashTable[i];
165 dhp->hashTable[i] = htons(firstelt);
175 struct DirHeader *dhp;
176 dhp = (struct DirHeader *)DRead(dir, 0);
179 if (dhp->header.pgcount != 0)
180 ctr = ntohs(dhp->header.pgcount);
182 /* old style, count the pages */
184 for (i = 0; i < MAXPAGES; i++)
185 if (dhp->alloMap[i] != EPP)
189 return ctr * AFS_PAGESIZE;
193 Delete(void *dir, char *entry)
195 /* Delete an entry from a directory, including update of all free entry descriptors. */
197 struct DirEntry *firstitem;
198 unsigned short *previtem;
199 firstitem = FindItem(dir, entry, &previtem);
202 *previtem = firstitem->next;
203 DRelease(previtem, 1);
204 index = DVOffset(firstitem) / 32;
205 nitems = NameBlobs(firstitem->name);
206 DRelease(firstitem, 0);
207 FreeBlobs(dir, index, nitems);
212 FindBlobs(void *dir, int nblobs)
214 /* Find a bunch of contiguous entries; at least nblobs in a row. */
217 struct DirHeader *dhp;
218 struct PageHeader *pp;
221 dhp = (struct DirHeader *)DRead(dir, 0); /* read the dir header in first. */
224 for (i = 0; i < BIGMAXPAGES; i++) {
225 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
226 /* if page could contain enough entries */
227 /* If there are EPP free entries, then the page is not even allocated. */
229 /* this pages exists past the end of the old-style dir */
230 pgcount = ntohs(dhp->header.pgcount);
233 dhp->header.pgcount = htons(pgcount);
235 if (i > pgcount - 1) {
236 /* this page is bigger than last allocated page */
238 dhp->header.pgcount = htons(i + 1);
240 } else if (dhp->alloMap[i] == EPP) {
241 /* Add the page to the directory. */
243 dhp->alloMap[i] = EPP - 1;
244 dhp->header.pgcount = htons(i + 1);
246 pp = (struct PageHeader *)DRead(dir, i); /* read the page in. */
251 for (j = 0; j <= EPP - nblobs; j++) {
253 for (k = 0; k < nblobs; k++)
254 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
263 /* Here we have the first index in j. We update the allocation maps
264 * and free up any resources we've got allocated. */
266 dhp->alloMap[i] -= nblobs;
268 for (k = 0; k < nblobs; k++)
269 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
273 DRelease(pp, 0); /* This dir page is unchanged. */
276 /* If we make it here, the directory is full. */
282 AddPage(void *dir, int pageno)
283 { /* Add a page to a directory. */
285 struct PageHeader *pp;
287 pp = (struct PageHeader *)DNew(dir, pageno); /* Get a new buffer labelled dir,pageno */
288 pp->tag = htons(1234);
291 pp->freecount = EPP - 1; /* The first dude is already allocated */
292 pp->freebitmap[0] = 0x01;
293 for (i = 1; i < EPP / 8; i++) /* It's a constant */
294 pp->freebitmap[i] = 0;
298 /* Free a whole bunch of directory entries. */
301 FreeBlobs(void *dir, int firstblob, int nblobs)
305 struct DirHeader *dhp;
306 struct PageHeader *pp;
307 page = firstblob / EPP;
308 firstblob -= EPP * page; /* convert to page-relative entry */
309 dhp = (struct DirHeader *)DRead(dir, 0);
313 dhp->alloMap[page] += nblobs;
315 pp = (struct PageHeader *)DRead(dir, page);
317 for (i = 0; i < nblobs; i++)
318 pp->freebitmap[(firstblob + i) >> 3] &=
319 ~(1 << ((firstblob + i) & 7));
324 * Format an empty directory properly. Note that the first 13 entries in a
325 * directory header page are allocated, 1 to the page header, 4 to the
326 * allocation map and 8 to the hash table.
329 MakeDir(void *dir, afs_int32 * me, afs_int32 * parent)
332 struct DirHeader *dhp;
333 dhp = (struct DirHeader *)DNew(dir, 0);
334 dhp->header.pgcount = htons(1);
335 dhp->header.tag = htons(1234);
336 dhp->header.freecount = (EPP - DHE - 1);
337 dhp->header.freebitmap[0] = 0xff;
338 dhp->header.freebitmap[1] = 0x1f;
339 for (i = 2; i < EPP / 8; i++)
340 dhp->header.freebitmap[i] = 0;
341 dhp->alloMap[0] = (EPP - DHE - 1);
342 for (i = 1; i < MAXPAGES; i++)
343 dhp->alloMap[i] = EPP;
344 for (i = 0; i < NHASHENT; i++)
345 dhp->hashTable[i] = 0;
347 Create(dir, ".", me);
348 Create(dir, "..", parent); /* Virtue is its own .. */
352 /* Look up a file name in directory. */
355 Lookup(void *dir, char *entry, void *voidfid)
357 afs_int32 *fid = (afs_int32 *) voidfid;
358 struct DirEntry *firstitem;
359 unsigned short *previtem;
361 firstitem = FindItem(dir, entry, &previtem);
364 DRelease(previtem, 0);
365 fid[1] = ntohl(firstitem->fid.vnode);
366 fid[2] = ntohl(firstitem->fid.vunique);
367 DRelease(firstitem, 0);
371 /* Look up a file name in directory. */
374 LookupOffset(void *dir, char *entry, void *voidfid, long *offsetp)
376 afs_int32 *fid = (afs_int32 *) voidfid;
377 struct DirEntry *firstitem;
378 unsigned short *previtem;
380 firstitem = FindItem(dir, entry, &previtem);
383 DRelease(previtem, 0);
384 fid[1] = ntohl(firstitem->fid.vnode);
385 fid[2] = ntohl(firstitem->fid.vunique);
387 *offsetp = DVOffset(firstitem);
388 DRelease(firstitem, 0);
393 EnumerateDir(void *dir, int (*hookproc) (void *dir, char *name,
394 afs_int32 vnode, afs_int32 unique),
397 /* Enumerate the contents of a directory.
398 * Break when hook function returns non 0.
402 struct DirHeader *dhp;
406 dhp = (struct DirHeader *)DRead(dir, 0);
408 return EIO; /* first page should be there */
410 for (i = 0; i < NHASHENT; i++) {
411 /* For each hash chain, enumerate everyone on the list. */
412 num = ntohs(dhp->hashTable[i]);
414 /* Walk down the hash table list. */
416 ep = GetBlob(dir, num);
419 /* we failed, return why */
426 num = ntohs(ep->next);
427 code = (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode),
428 ntohl(ep->fid.vunique));
441 /* Enumerate the contents of a directory. */
444 struct DirHeader *dhp;
446 dhp = (struct DirHeader *)DRead(dir, 0);
449 for (i = 0; i < NHASHENT; i++) {
450 /* For each hash chain, enumerate everyone on the list. */
451 num = ntohs(dhp->hashTable[i]);
453 /* Walk down the hash table list. */
454 ep = GetBlob(dir, num);
457 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
462 num = ntohs(ep->next);
471 GetBlob(void *dir, afs_int32 blobno)
473 /* Return a pointer to an entry, given its number. */
475 ep = DRead(dir, blobno >> LEPP);
478 return (struct DirEntry *)(((long)ep) + 32 * (blobno & (EPP - 1)));
482 DirHash(char *string)
484 /* Hash a string to a number between 0 and NHASHENT. */
489 while ((tc = (*string++))) {
493 tval = hval & (NHASHENT - 1);
496 else if (hval >= 1<<31)
497 tval = NHASHENT - tval;
502 /* Find a directory entry, given its name. This entry returns a pointer
503 * to a locked buffer, and a pointer to a locked buffer (in previtem)
504 * referencing the found item (to aid the delete code). If no entry is
505 * found, however, no items are left locked, and a null pointer is
506 * returned instead. */
508 static struct DirEntry *
509 FindItem(void *dir, char *ename, unsigned short **previtem)
512 struct DirHeader *dhp;
516 dhp = (struct DirHeader *)DRead(dir, 0);
519 if (dhp->hashTable[i] == 0) {
524 tp = GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]));
529 lp = &(dhp->hashTable[i]);
531 /* Look at each hash conflict entry. */
532 if (!strcmp(ename, tp->name)) {
533 /* Found our entry. */
540 /* The end of the line */
541 DRelease(lp, 0); /* Release all locks. */
544 tp = GetBlob(dir, (u_short) ntohs(tp->next));
552 static struct DirEntry *
553 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique)
555 /* Find a directory entry, given the vnode and uniquifier of a object.
556 * This entry returns a pointer to a locked buffer. If no entry is found,
557 * however, no items are left locked, and a null pointer is returned
561 struct DirHeader *dhp;
564 dhp = (struct DirHeader *) DRead(dir,0);
566 for (i=0; i<NHASHENT; i++) {
567 if (dhp->hashTable[i] != 0) {
568 tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
569 if (!tp) { /* should not happen */
574 if (vnode == ntohl(tp->fid.vnode)
575 && unique == ntohl(tp->fid.vunique)) {
582 tp = GetBlob(dir,(u_short)ntohs(tp->next));
593 InverseLookup (void *dir, afs_uint32 vnode, afs_uint32 unique, char *name,
596 /* Look for the name pointing to given vnode and unique in a directory */
597 struct DirEntry *entry;
600 entry = FindFid(dir, vnode, unique);
603 if (strlen(entry->name) >= length)
606 strcpy(name, entry->name);
612 * Change an entry fid.
615 * \param entry The entry name.
616 * \param old_fid The old find in MKFid format (host order).
617 * It can be omitted if you don't need a safety check...
618 * \param new_fid The new find in MKFid format (host order).
620 int ChangeFid(void *dir,
625 struct DirEntry *firstitem;
626 unsigned short *previtem;
627 struct MKFid *fid_old = (struct MKFid *) old_fid;
628 struct MKFid *fid_new = (struct MKFid *) new_fid;
631 firstitem = FindItem(dir, entry, &previtem);
632 if (firstitem == 0) {
635 DRelease(previtem, 1);
638 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
639 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
641 firstitem->fid.vnode = htonl(fid_new->vnode);
642 firstitem->fid.vunique = htonl(fid_new->vunique);
645 DRelease(firstitem, 1);