#include <afsconfig.h>
#include <afs/param.h>
-RCSID("$Header$");
-
#ifdef KERNEL
-#if !defined(UKERNEL)
-#include "h/types.h"
-#include "h/param.h"
-#ifdef AFS_AUX_ENV
-#include "h/mmu.h"
-#include "h/seg.h"
-#include "h/sysmacros.h"
-#include "h/signal.h"
-#include "h/errno.h"
-#endif
-#include "h/time.h"
-#if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV)
-#include "h/errno.h"
-#else
-#if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
-#include "h/kernel.h"
-#endif
-#endif
-#if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV)
-#include "afs/sysincludes.h"
-#endif
-#if defined(AFS_FBSD_ENV)
-#include "h/lock.h"
-#include "vm/vm.h"
-#include "vm/vm_extern.h"
-#include "vm/pmap.h"
-#include "vm/vm_map.h"
-#endif /* AFS_FBSD_ENV */
-#if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN60_ENV)
-#include "h/user.h"
-#endif /* AFS_SGI64_ENV */
-#include "h/uio.h"
-#ifdef AFS_DEC_ENV
-#include "afs/gfs_vfs.h"
-#include "afs/gfs_vnode.h"
-#else
-#ifdef AFS_MACH_ENV
-#ifdef NeXT
-#include <sys/vfs.h>
-#include <sys/vnode.h>
-#include <ufs/inode.h>
-#else
-#include <vfs/vfs.h>
-#include <vfs/vnode.h>
-#include <sys/inode.h>
-#endif /* NeXT */
-#else /* AFS_MACH_ENV */
-#ifdef AFS_OSF_ENV
-#include <sys/mount.h>
-#include <sys/vnode.h>
-#include <ufs/inode.h>
-#else /* AFS_OSF_ENV */
-#ifdef AFS_SUN5_ENV
-#else
-#if !defined(AFS_SGI_ENV)
-#endif
-#endif /* AFS_OSF_ENV */
-#endif /* AFS_MACH_ENV */
-#endif
-#endif
-#if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX1122_ENV)
-#include "h/mbuf.h"
-#endif
-#ifndef AFS_LINUX20_ENV
-#include "netinet/in.h"
-#endif
-#else /* !defined(UKERNEL) */
-#include "afs/stds.h"
-#include "afs/sysincludes.h"
-#endif /* !defined(UKERNEL) */
-#include "afs/afs_osi.h"
-
-#include "afs/dir.h"
-
-#include "afs/longc_procs.h"
-#ifdef AFS_LINUX20_ENV
-#include "h/string.h"
-#endif
-
-/* generic renaming */
-#define NameBlobs afs_dir_NameBlobs
-#define GetBlob afs_dir_GetBlob
-#define Create afs_dir_Create
-#define Length afs_dir_Length
-#define Delete afs_dir_Delete
-#define MakeDir afs_dir_MakeDir
-#define Lookup afs_dir_Lookup
-#define LookupOffset afs_dir_LookupOffset
-#define EnumerateDir afs_dir_EnumerateDir
-#define IsEmpty afs_dir_IsEmpty
+# if !defined(UKERNEL)
+# include "h/types.h"
+# if !defined(AFS_LINUX26_ENV)
+# include "h/param.h"
+# endif
+# ifdef AFS_AUX_ENV
+# include "h/mmu.h"
+# include "h/seg.h"
+# include "h/sysmacros.h"
+# include "h/signal.h"
+# include "h/errno.h"
+# endif
+# include "h/time.h"
+# if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
+# include "h/errno.h"
+# else
+# if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
+# include "h/kernel.h"
+# endif
+# endif
+# if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
+# include "afs/sysincludes.h"
+# endif
+# if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_OBSD48_ENV) && !defined(AFS_NBSD_ENV)
+# include "h/user.h"
+# endif /* AFS_SGI64_ENV */
+# include "h/uio.h"
+# if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
+# include "h/mbuf.h"
+# endif
+# ifndef AFS_LINUX20_ENV
+# include "netinet/in.h"
+# endif
+# else /* !defined(UKERNEL) */
+# include "afs/stds.h"
+# include "afs/sysincludes.h"
+# endif /* !defined(UKERNEL) */
+
+/* afs_buffer.c */
+/* These are needed because afs_prototypes.h is not included here */
+struct dcache;
+struct DirBuffer;
+extern int DRead(struct dcache *adc, int page, struct DirBuffer *);
+extern int DNew(struct dcache *adc, int page, struct DirBuffer *);
+
+# include "afs/afs_osi.h"
+
+# include "afs/dir.h"
+
+# ifdef AFS_LINUX20_ENV
+# include "h/string.h"
+# endif
+
#else /* KERNEL */
-# ifdef HAVE_UNISTD_H
-# include <unistd.h>
-# endif
-# include <sys/types.h>
-# include <errno.h>
+# include <roken.h>
# include "dir.h"
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#else
-#include <netinet/in.h>
-#endif
-
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
#endif /* KERNEL */
afs_int32 DErrno;
-char *DRelease();
-struct DirEntry *DRead();
-struct DirEntry *DNew();
/* Local static prototypes */
-static struct DirEntry *FindItem (void *dir, char *ename,
- unsigned short **previtem);
-
-
-int NameBlobs (char *name)
-{/* Find out how many entries are required to store a name. */
- register int i;
- i = strlen(name)+1;
- return 1+((i+15)>>5);
+static int FindBlobs(dir_file_t, int);
+static void AddPage(dir_file_t, int);
+static void FreeBlobs(dir_file_t, int, int);
+static int FindItem(dir_file_t, char *, struct DirBuffer *,
+ struct DirBuffer *);
+
+/* Find out how many entries are required to store a name. */
+int
+afs_dir_NameBlobs(char *name)
+{
+ int i;
+ i = strlen(name) + 1;
+ return 1 + ((i + 15) >> 5);
}
-/* Create an entry in a file. Dir is a file representation, while entry is a string name. */
-
-int Create (void *dir, char *entry, void *voidfid)
+/* Create an entry in a file. Dir is a file representation, while entry is
+ * a string name. */
+int
+afs_dir_Create(dir_file_t dir, char *entry, void *voidfid)
{
afs_int32 *vfid = (afs_int32 *) voidfid;
int blobs, firstelt;
- register int i;
- register struct DirEntry *ep;
- unsigned short *pp = NULL;
- register struct DirHeader *dhp;
+ int i;
+ struct DirBuffer entrybuf, prevbuf, headerbuf;
+ struct DirEntry *ep;
+ struct DirHeader *dhp;
+ int code;
/* check name quality */
- if (*entry == 0) return EINVAL;
+ if (*entry == 0)
+ return EINVAL;
+
/* First check if file already exists. */
- ep = FindItem(dir, entry, &pp);
- if (ep) {
- DRelease(ep, 0);
- DRelease(pp, 0);
- return EEXIST;
+ code = FindItem(dir, entry, &prevbuf, &entrybuf);
+ if (code && code != ENOENT) {
+ return code;
}
- blobs = NameBlobs(entry); /* number of entries required */
+ if (code == 0) {
+ DRelease(&entrybuf, 0);
+ DRelease(&prevbuf, 0);
+ return EEXIST;
+ }
+
+ blobs = afs_dir_NameBlobs(entry); /* number of entries required */
firstelt = FindBlobs(dir, blobs);
- if (firstelt < 0) return EFBIG; /* directory is full */
+ if (firstelt < 0)
+ return EFBIG; /* directory is full */
+
/* First, we fill in the directory entry. */
- ep = GetBlob(dir, firstelt);
- if (ep == 0) return EIO;
+ if (afs_dir_GetBlob(dir, firstelt, &entrybuf) != 0)
+ return EIO;
+ ep = (struct DirEntry *)entrybuf.data;
+
ep->flag = FFIRST;
ep->fid.vnode = htonl(vfid[1]);
ep->fid.vunique = htonl(vfid[2]);
strcpy(ep->name, entry);
+
/* Now we just have to thread it on the hash table list. */
- dhp = (struct DirHeader *) DRead(dir,0);
- if (!dhp) {
- DRelease(ep, 1);
+ if (DRead(dir, 0, &headerbuf) != 0) {
+ DRelease(&entrybuf, 1);
return EIO;
}
- i = DirHash(entry);
+ dhp = (struct DirHeader *)headerbuf.data;
+
+ i = afs_dir_DirHash(entry);
ep->next = dhp->hashTable[i];
dhp->hashTable[i] = htons(firstelt);
- DRelease(dhp,1);
- DRelease(ep,1);
+ DRelease(&headerbuf, 1);
+ DRelease(&entrybuf, 1);
return 0;
}
-int Length (void *dir)
+int
+afs_dir_Length(dir_file_t dir)
{
- int i,ctr;
+ int i, ctr;
+ struct DirBuffer headerbuf;
struct DirHeader *dhp;
- dhp = (struct DirHeader *) DRead(dir,0);
- if (!dhp) return 0;
- if (dhp->header.pgcount != 0) ctr = ntohs(dhp->header.pgcount);
+
+ if (DRead(dir, 0, &headerbuf) != 0)
+ return 0;
+ dhp = (struct DirHeader *)headerbuf.data;
+
+ if (dhp->header.pgcount != 0)
+ ctr = ntohs(dhp->header.pgcount);
else {
/* old style, count the pages */
- ctr=0;
- for(i=0;i<MAXPAGES;i++)
- if (dhp->alloMap[i] != EPP) ctr++;
+ ctr = 0;
+ for (i = 0; i < MAXPAGES; i++)
+ if (dhp->alloMap[i] != EPP)
+ ctr++;
}
- DRelease(dhp,0);
- return ctr*AFS_PAGESIZE;
+ DRelease(&headerbuf, 0);
+ return ctr * AFS_PAGESIZE;
}
-int Delete (void *dir, char *entry)
+/* Delete an entry from a directory, including update of all free entry
+ * descriptors. */
+int
+afs_dir_Delete(dir_file_t dir, char *entry)
{
- /* Delete an entry from a directory, including update of all free entry descriptors. */
+
int nitems, index;
- register struct DirEntry *firstitem;
+ struct DirBuffer entrybuf, prevbuf;
+ struct DirEntry *firstitem;
unsigned short *previtem;
- firstitem = FindItem(dir,entry,&previtem);
- if (firstitem == 0) return ENOENT;
+ int code;
+
+ code = FindItem(dir, entry, &prevbuf, &entrybuf);
+ if (code) {
+ return code;
+ }
+
+ firstitem = (struct DirEntry *)entrybuf.data;
+ previtem = (unsigned short *)prevbuf.data;
+
*previtem = firstitem->next;
- DRelease(previtem,1);
- index = DVOffset(firstitem)/32;
- nitems = NameBlobs(firstitem->name);
- DRelease(firstitem,0);
- FreeBlobs(dir,index,nitems);
+ DRelease(&prevbuf, 1);
+ index = DVOffset(&entrybuf) / 32;
+ nitems = afs_dir_NameBlobs(firstitem->name);
+ /* Clear entire DirEntry and any DirXEntry extensions */
+ memset(firstitem, 0, nitems * sizeof(*firstitem));
+ DRelease(&entrybuf, 1);
+ FreeBlobs(dir, index, nitems);
return 0;
}
-int FindBlobs (void *dir, int nblobs)
+/* Find a bunch of contiguous entries; at least nblobs in a row. */
+static int
+FindBlobs(dir_file_t dir, int nblobs)
{
- /* Find a bunch of contiguous entries; at least nblobs in a row. */
- register int i, j, k;
+ int i, j, k;
int failed = 0;
- register struct DirHeader *dhp;
+ struct DirBuffer headerbuf, pagebuf;
+ struct DirHeader *dhp;
struct PageHeader *pp;
int pgcount;
- dhp = (struct DirHeader *) DRead(dir,0); /* read the dir header in first. */
- if (!dhp) return -1;
- for(i=0;i<BIGMAXPAGES;i++) {
- if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
+ /* read the dir header in first. */
+ if (DRead(dir, 0, &headerbuf) != 0)
+ return -1;
+ dhp = (struct DirHeader *)headerbuf.data;
+
+ for (i = 0; i < BIGMAXPAGES; i++) {
+ if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
/* if page could contain enough entries */
- /* If there are EPP free entries, then the page is not even allocated. */
+ /* If there are EPP free entries, then the page is not even allocated. */
if (i >= MAXPAGES) {
/* this pages exists past the end of the old-style dir */
pgcount = ntohs(dhp->header.pgcount);
if (i > pgcount - 1) {
/* this page is bigger than last allocated page */
AddPage(dir, i);
- dhp->header.pgcount = htons(i+1);
+ dhp->header.pgcount = htons(i + 1);
}
+ } else if (dhp->alloMap[i] == EPP) {
+ /* Add the page to the directory. */
+ AddPage(dir, i);
+ dhp->alloMap[i] = EPP - 1;
+ dhp->header.pgcount = htons(i + 1);
}
- else if (dhp->alloMap[i] == EPP) {
- /* Add the page to the directory. */
- AddPage(dir,i);
- dhp->alloMap[i] = EPP-1;
- dhp->header.pgcount = htons(i+1);
- }
- pp = (struct PageHeader *) DRead(dir,i); /* read the page in. */
- if (!pp) {
- DRelease(dhp, 1);
+
+ /* read the page in. */
+ if (DRead(dir, i, &pagebuf) != 0) {
break;
}
- for(j=0;j<=EPP-nblobs;j++) {
- failed = 0;
- for(k=0;k<nblobs;k++)
- if ((pp->freebitmap[(j+k)>>3]>>((j+k)&7)) & 1) {
- failed = 1;
- break;
+ pp = (struct PageHeader *)pagebuf.data;
+ for (j = 0; j <= EPP - nblobs; j++) {
+ failed = 0;
+ for (k = 0; k < nblobs; k++)
+ if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
+ failed = 1;
+ break;
}
- if (!failed) break;
- failed = 1;
+ if (!failed)
+ break;
+ failed = 1;
}
- if (!failed) {
- /* Here we have the first index in j. We update the allocation maps
- and free up any resources we've got allocated. */
- if (i < MAXPAGES) dhp->alloMap[i] -= nblobs;
- DRelease(dhp,1);
- for (k=0;k<nblobs;k++)
- pp->freebitmap[(j+k)>>3] |= 1<<((j+k)&7);
- DRelease(pp,1);
- return j+i*EPP;
+ if (!failed) {
+ /* Here we have the first index in j. We update the allocation maps
+ * and free up any resources we've got allocated. */
+ if (i < MAXPAGES)
+ dhp->alloMap[i] -= nblobs;
+ DRelease(&headerbuf, 1);
+ for (k = 0; k < nblobs; k++)
+ pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
+ DRelease(&pagebuf, 1);
+ return j + i * EPP;
}
- DRelease(pp, 0); /* This dir page is unchanged. */
+ DRelease(&pagebuf, 0); /* This dir page is unchanged. */
}
}
/* If we make it here, the directory is full. */
- DRelease(dhp, 1);
+ DRelease(&headerbuf, 1);
return -1;
}
-void AddPage (void *dir, int pageno)
-{/* Add a page to a directory. */
- register int i;
- register struct PageHeader *pp;
+static void
+AddPage(dir_file_t dir, int pageno)
+{ /* Add a page to a directory. */
+ int i;
+ struct PageHeader *pp;
+ struct DirBuffer pagebuf;
+
+ /* Get a new buffer labelled dir,pageno */
+ DNew(dir, pageno, &pagebuf);
+ pp = (struct PageHeader *)pagebuf.data;
- pp = (struct PageHeader *) DNew(dir,pageno); /* Get a new buffer labelled dir,pageno */
pp->tag = htons(1234);
- if (pageno > 0) pp->pgcount = 0;
- pp->freecount = EPP-1; /* The first dude is already allocated */
+ if (pageno > 0)
+ pp->pgcount = 0;
+ pp->freecount = EPP - 1; /* The first dude is already allocated */
pp->freebitmap[0] = 0x01;
- for (i=1;i<EPP/8;i++) /* It's a constant */
- pp->freebitmap[i] = 0;
- DRelease(pp,1);
+ for (i = 1; i < EPP / 8; i++) /* It's a constant */
+ pp->freebitmap[i] = 0;
+ DRelease(&pagebuf, 1);
}
/* Free a whole bunch of directory entries. */
-void FreeBlobs(void *dir, register int firstblob, int nblobs)
+static void
+FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
{
- register int i;
+ int i;
int page;
+ struct DirBuffer headerbuf, pagehdbuf;
struct DirHeader *dhp;
struct PageHeader *pp;
- page = firstblob/EPP;
- firstblob -= EPP*page; /* convert to page-relative entry */
- dhp = (struct DirHeader *) DRead(dir,0);
- if (!dhp) return;
- if (page < MAXPAGES) dhp->alloMap[page] += nblobs;
- DRelease(dhp,1);
- pp = (struct PageHeader *) DRead(dir,page);
- if (pp) for (i=0;i<nblobs;i++)
- pp->freebitmap[(firstblob+i)>>3] &= ~(1<<((firstblob+i)&7));
- DRelease(pp,1);
+ page = firstblob / EPP;
+ firstblob -= EPP * page; /* convert to page-relative entry */
+
+ if (DRead(dir, 0, &headerbuf) != 0)
+ return;
+ dhp = (struct DirHeader *)headerbuf.data;
+
+ if (page < MAXPAGES)
+ dhp->alloMap[page] += nblobs;
+
+ DRelease(&headerbuf, 1);
+
+ if (DRead(dir, page, &pagehdbuf) != 0)
+ return;
+ pp = (struct PageHeader *)pagehdbuf.data;
+
+ for (i = 0; i < nblobs; i++)
+ pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
+
+ DRelease(&pagehdbuf, 1);
}
/*
* directory header page are allocated, 1 to the page header, 4 to the
* allocation map and 8 to the hash table.
*/
-int MakeDir (void *dir, afs_int32 *me, afs_int32 *parent)
+int
+afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
{
- register int i;
- register struct DirHeader *dhp;
- dhp = (struct DirHeader *) DNew(dir,0);
+ int i;
+ struct DirBuffer buffer;
+ struct DirHeader *dhp;
+
+ DNew(dir, 0, &buffer);
+ dhp = (struct DirHeader *)buffer.data;
+
dhp->header.pgcount = htons(1);
dhp->header.tag = htons(1234);
- dhp->header.freecount = (EPP-DHE-1);
+ dhp->header.freecount = (EPP - DHE - 1);
dhp->header.freebitmap[0] = 0xff;
dhp->header.freebitmap[1] = 0x1f;
- for(i=2;i<EPP/8;i++) dhp->header.freebitmap[i] = 0;
- dhp->alloMap[0]=(EPP-DHE-1);
- for(i=1;i<MAXPAGES;i++)dhp->alloMap[i] = EPP;
- for(i=0;i<NHASHENT;i++)dhp->hashTable[i] = 0;
- DRelease(dhp,1);
- Create(dir,".",me);
- Create(dir,"..",parent); /* Virtue is its own .. */
+ for (i = 2; i < EPP / 8; i++)
+ dhp->header.freebitmap[i] = 0;
+ dhp->alloMap[0] = (EPP - DHE - 1);
+ for (i = 1; i < MAXPAGES; i++)
+ dhp->alloMap[i] = EPP;
+ for (i = 0; i < NHASHENT; i++)
+ dhp->hashTable[i] = 0;
+ DRelease(&buffer, 1);
+ afs_dir_Create(dir, ".", me);
+ afs_dir_Create(dir, "..", parent); /* Virtue is its own .. */
return 0;
}
/* Look up a file name in directory. */
-int Lookup (void *dir, char *entry, void *voidfid)
+int
+afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
{
afs_int32 *fid = (afs_int32 *) voidfid;
- register struct DirEntry *firstitem;
- unsigned short *previtem;
+ struct DirBuffer firstbuf, prevbuf;
+ struct DirEntry *firstitem;
+ int code;
+
+ code = FindItem(dir, entry, &prevbuf, &firstbuf);
+ if (code) {
+ return code;
+ }
+ DRelease(&prevbuf, 0);
+ firstitem = (struct DirEntry *)firstbuf.data;
- firstitem = FindItem(dir,entry,&previtem);
- if (firstitem == 0) return ENOENT;
- DRelease(previtem,0);
fid[1] = ntohl(firstitem->fid.vnode);
fid[2] = ntohl(firstitem->fid.vunique);
- DRelease(firstitem,0);
+ DRelease(&firstbuf, 0);
return 0;
}
/* Look up a file name in directory. */
-int LookupOffset (void *dir, char *entry, void *voidfid, long *offsetp)
+int
+afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
+ long *offsetp)
{
afs_int32 *fid = (afs_int32 *) voidfid;
- register struct DirEntry *firstitem;
- unsigned short *previtem;
+ struct DirBuffer firstbuf, prevbuf;
+ struct DirEntry *firstitem;
+ int code;
+
+ code = FindItem(dir, entry, &prevbuf, &firstbuf);
+ if (code) {
+ return code;
+ }
+ DRelease(&prevbuf, 0);
+ firstitem = (struct DirEntry *)firstbuf.data;
- firstitem = FindItem(dir,entry,&previtem);
- if (firstitem == 0) return ENOENT;
- DRelease(previtem,0);
fid[1] = ntohl(firstitem->fid.vnode);
fid[2] = ntohl(firstitem->fid.vunique);
if (offsetp)
- *offsetp = DVOffset(firstitem);
- DRelease(firstitem,0);
+ *offsetp = DVOffset(&firstbuf);
+ DRelease(&firstbuf, 0);
return 0;
}
-int EnumerateDir (void *dir, int (*hookproc)(), void *hook)
+/*
+ * Enumerate the contents of a directory. Break when hook function
+ * returns non 0.
+ */
+
+int
+afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
+ afs_int32 vnode,
+ afs_int32 unique),
+ void *hook)
{
- /* Enumerate the contents of a directory. */
- register int i;
+ int i;
int num;
- register struct DirHeader *dhp;
- register struct DirEntry *ep;
-
- dhp = (struct DirHeader *) DRead(dir,0);
- if (!dhp) return EIO; /* first page should be there */
- for(i=0; i<NHASHENT; i++) {
- /* For each hash chain, enumerate everyone on the list. */
- num = ntohs(dhp->hashTable[i]);
- while (num != 0) {
- /* Walk down the hash table list. */
- DErrno = 0;
- ep = GetBlob(dir,num);
- if (!ep) {
- if (DErrno) {
- /* we failed, return why */
- DRelease(dhp, 0);
- return DErrno;
- }
+ struct DirBuffer headerbuf, entrybuf;
+ struct DirHeader *dhp;
+ struct DirEntry *ep;
+ int code = 0;
+ int elements;
+
+ if (DRead(dir, 0, &headerbuf) != 0)
+ return EIO;
+ dhp = (struct DirHeader *)headerbuf.data;
+
+ for (i = 0; i < NHASHENT; i++) {
+ /* For each hash chain, enumerate everyone on the list. */
+ num = ntohs(dhp->hashTable[i]);
+ elements = 0;
+ while (num != 0 && elements < BIGMAXPAGES * EPP) {
+ elements++;
+
+ /* Walk down the hash table list. */
+ code = afs_dir_GetVerifiedBlob(dir, num, &entrybuf);
+ if (code)
+ goto out;
+
+ ep = (struct DirEntry *)entrybuf.data;
+ if (!ep) {
+ DRelease(&entrybuf, 0);
break;
}
- num = ntohs(ep->next);
- (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode), ntohl(ep->fid.vunique));
- DRelease(ep,0);
+
+ num = ntohs(ep->next);
+ code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
+ ntohl(ep->fid.vunique));
+ DRelease(&entrybuf, 0);
+ if (code)
+ goto out;
}
}
- DRelease(dhp,0);
+
+out:
+ DRelease(&headerbuf, 0);
return 0;
}
-int IsEmpty (void *dir)
+int
+afs_dir_IsEmpty(dir_file_t dir)
{
/* Enumerate the contents of a directory. */
- register int i;
+ int i;
int num;
- register struct DirHeader *dhp;
- register struct DirEntry *ep;
- dhp = (struct DirHeader *) DRead(dir,0);
- if (!dhp) return 0;
- for(i=0;i<NHASHENT;i++) {
- /* For each hash chain, enumerate everyone on the list. */
- num = ntohs(dhp->hashTable[i]);
- while (num != 0) {
- /* Walk down the hash table list. */
- ep = GetBlob(dir,num);
- if (!ep) break;
- if (strcmp(ep->name,"..") && strcmp(ep->name,".")) {
- DRelease(ep, 0);
- DRelease(dhp, 0);
- return 1;
+ struct DirBuffer headerbuf, entrybuf;
+ struct DirHeader *dhp;
+ struct DirEntry *ep;
+ int elements;
+
+ if (DRead(dir, 0, &headerbuf) != 0)
+ return 0;
+ dhp = (struct DirHeader *)headerbuf.data;
+
+ for (i = 0; i < NHASHENT; i++) {
+ /* For each hash chain, enumerate everyone on the list. */
+ num = ntohs(dhp->hashTable[i]);
+ elements = 0;
+ while (num != 0 && elements < BIGMAXPAGES * EPP) {
+ elements++;
+ /* Walk down the hash table list. */
+ if (afs_dir_GetVerifiedBlob(dir, num, &entrybuf) != 0)
+ break;
+ ep = (struct DirEntry *)entrybuf.data;
+ if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
+ DRelease(&entrybuf, 0);
+ DRelease(&headerbuf, 0);
+ return 1;
}
- num = ntohs(ep->next);
- DRelease(ep,0);
+ num = ntohs(ep->next);
+ DRelease(&entrybuf, 0);
}
}
- DRelease(dhp,0);
+ DRelease(&headerbuf, 0);
return 0;
}
-struct DirEntry *GetBlob (void *dir, afs_int32 blobno)
+/* Return a pointer to an entry, given its number. Also return the maximum
+ * size of the entry, which is determined by its position within the directory
+ * page.
+ */
+
+static int
+GetBlobWithLimit(dir_file_t dir, afs_int32 blobno,
+ struct DirBuffer *buffer, afs_size_t *maxlen)
{
- /* Return a pointer to an entry, given its number. */
- struct DirEntry *ep;
- ep=DRead(dir,blobno>>LEPP);
- if (!ep) return 0;
- return (struct DirEntry *) (((long)ep)+32*(blobno&(EPP-1)));
+ afs_size_t pos;
+ int code;
+
+ *maxlen = 0;
+ memset(buffer, 0, sizeof(struct DirBuffer));
+
+ code = DRead(dir, blobno >> LEPP, buffer);
+ if (code)
+ return code;
+
+ pos = 32 * (blobno & (EPP - 1));
+
+ *maxlen = AFS_PAGESIZE - pos - 1;
+
+ buffer->data = (void *)(((char *)buffer->data) + pos);
+
+ return 0;
}
-int DirHash (register char *string)
+/* Given an entries number, return a pointer to that entry */
+int
+afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
+{
+ afs_size_t maxlen = 0;
+ return GetBlobWithLimit(dir, blobno, buffer, &maxlen);
+}
+
+/* Return an entry, having verified that the name held within the entry
+ * doesn't overflow off the end of the directory page it is contained
+ * within
+ */
+
+int
+afs_dir_GetVerifiedBlob(dir_file_t file, afs_int32 blobno,
+ struct DirBuffer *outbuf)
+{
+ struct DirEntry *dir;
+ struct DirBuffer buffer;
+ afs_size_t maxlen;
+ int code;
+ char *cp;
+
+ code = GetBlobWithLimit(file, blobno, &buffer, &maxlen);
+ if (code)
+ return code;
+
+ dir = (struct DirEntry *)buffer.data;
+
+ /* A blob is only valid if the name within it is NULL terminated before
+ * the end of the blob's containing page */
+ for (cp = dir->name; *cp != '\0' && cp < ((char *)dir) + maxlen; cp++);
+
+ if (*cp != '\0') {
+ DRelease(&buffer, 0);
+ return EIO;
+ }
+
+ *outbuf = buffer;
+ return 0;
+}
+
+int
+afs_dir_DirHash(char *string)
{
/* Hash a string to a number between 0 and NHASHENT. */
- register unsigned char tc;
- register int hval;
- register int tval;
+ unsigned char tc;
+ unsigned int hval;
+ int tval;
hval = 0;
- while((tc=(*string++))) {
- hval *= 173;
- hval += tc;
+ while ((tc = (*string++))) {
+ hval *= 173;
+ hval += tc;
}
- tval = hval & (NHASHENT-1);
- if (tval == 0) return tval;
- else if (hval < 0) tval = NHASHENT-tval;
+ tval = hval & (NHASHENT - 1);
+ if (tval == 0)
+ return tval;
+ else if (hval >= 1u<<31)
+ tval = NHASHENT - tval;
return tval;
}
-static struct DirEntry *FindItem (void *dir, char *ename,
- unsigned short **previtem)
+
+/* 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. */
+
+static int
+FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
+ struct DirBuffer *itembuf )
{
- /* 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. */
- register int i;
- register struct DirHeader *dhp;
- register unsigned short *lp;
- register struct DirEntry *tp;
- i = DirHash(ename);
- dhp = (struct DirHeader *) DRead(dir,0);
- if (!dhp) return 0;
+ int i, code;
+ struct DirBuffer curr, prev;
+ struct DirHeader *dhp;
+ struct DirEntry *tp;
+ int elements;
+
+ memset(prevbuf, 0, sizeof(struct DirBuffer));
+ memset(itembuf, 0, sizeof(struct DirBuffer));
+
+ code = DRead(dir, 0, &prev);
+ if (code)
+ return code;
+ dhp = (struct DirHeader *)prev.data;
+
+ i = afs_dir_DirHash(ename);
if (dhp->hashTable[i] == 0) {
- /* no such entry */
- DRelease(dhp,0);
- return 0;
+ /* no such entry */
+ code = ENOENT;
+ goto out;
}
- tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
- if (!tp) {
- DRelease(dhp, 0);
- return 0;
+
+ code = afs_dir_GetVerifiedBlob(dir,
+ (u_short) ntohs(dhp->hashTable[i]),
+ &curr);
+ if (code) {
+ goto out;
}
- lp = &(dhp->hashTable[i]);
- while(1) {
- /* Look at each hash conflict entry. */
- if (!strcmp(ename,tp->name)) {
- /* Found our entry. */
- *previtem = lp;
- return tp;
+
+ prev.data = &(dhp->hashTable[i]);
+ elements = 0;
+ /* Detect circular hash chains. Absolute max size of a directory */
+ while (elements < BIGMAXPAGES * EPP) {
+ elements++;
+
+ /* Look at each entry on the hash chain */
+ tp = (struct DirEntry *)curr.data;
+ if (!strcmp(ename, tp->name)) {
+ /* Found it! */
+ *prevbuf = prev;
+ *itembuf = curr;
+ return 0;
}
- DRelease(lp,0);
- lp = &(tp->next);
- if (tp->next == 0) {
- /* The end of the line */
- DRelease(lp,0); /* Release all locks. */
- return 0;
+
+ DRelease(&prev, 0);
+
+ prev = curr;
+ prev.data = &(tp->next);
+
+ if (tp->next == 0) {
+ /* The end of the line */
+ code = ENOENT;
+ goto out;
}
- tp = GetBlob(dir,(u_short)ntohs(tp->next));
- if (!tp) {
- DRelease(lp, 0);
- return 0;
+
+ code = afs_dir_GetVerifiedBlob(dir, (u_short) ntohs(tp->next),
+ &curr);
+ if (code)
+ goto out;
+ }
+
+ /* If we've reached here, we've hit our loop limit. Something is weird with
+ * the directory; maybe a circular hash chain? */
+ code = EIO;
+
+out:
+ DRelease(&prev, 0);
+ return code;
+}
+
+static int
+FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
+ struct DirBuffer *itembuf)
+{
+ /* Find a directory entry, given the vnode and uniquifier of a object.
+ * This entry returns a pointer to a locked buffer. If no entry is found,
+ * however, no items are left locked, and a null pointer is returned
+ * instead.
+ */
+ int i, code;
+ unsigned short next;
+ struct DirBuffer curr, header;
+ struct DirHeader *dhp;
+ struct DirEntry *tp;
+ int elements;
+
+ memset(itembuf, 0, sizeof(struct DirBuffer));
+
+ code = DRead(dir, 0, &header);
+ if (code)
+ return code;
+ dhp = (struct DirHeader *)header.data;
+
+ for (i=0; i<NHASHENT; i++) {
+ if (dhp->hashTable[i] != 0) {
+ code = afs_dir_GetVerifiedBlob(dir,
+ (u_short)ntohs(dhp->hashTable[i]),
+ &curr);
+ if (code) {
+ DRelease(&header, 0);
+ return code;
+ }
+ elements = 0;
+ while(curr.data != NULL && elements < BIGMAXPAGES * EPP) {
+ elements++;
+ tp = (struct DirEntry *)curr.data;
+
+ if (vnode == ntohl(tp->fid.vnode)
+ && unique == ntohl(tp->fid.vunique)) {
+ DRelease(&header, 0);
+ *itembuf = curr;
+ return 0;
+ }
+
+ next = tp->next;
+ DRelease(&curr, 0);
+
+ if (next == 0)
+ break;
+
+ code = afs_dir_GetVerifiedBlob(dir, (u_short)ntohs(next),
+ &curr);
+ if (code) {
+ DRelease(&header, 0);
+ return code;
+ }
+ }
}
}
+ DRelease(&header, 0);
+ return ENOENT;
+}
+
+int
+afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
+ char *name, afs_uint32 length)
+{
+ /* Look for the name pointing to given vnode and unique in a directory */
+ struct DirBuffer entrybuf;
+ struct DirEntry *entry;
+ int code = 0;
+
+ code = FindFid(dir, vnode, unique, &entrybuf);
+ if (code) {
+ return code;
+ }
+ entry = (struct DirEntry *)entrybuf.data;
+
+ if (strlen(entry->name) >= length)
+ code = E2BIG;
+ else
+ strcpy(name, entry->name);
+ DRelease(&entrybuf, 0);
+ return code;
+}
+
+/*!
+ * Change an entry fid.
+ *
+ * \param dir
+ * \param entry The entry name.
+ * \param old_fid The old find in MKFid format (host order).
+ * It can be omitted if you don't need a safety check...
+ * \param new_fid The new find in MKFid format (host order).
+ */
+int
+afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
+ afs_uint32 *new_fid)
+{
+ struct DirBuffer prevbuf, entrybuf;
+ struct DirEntry *firstitem;
+ struct MKFid *fid_old = (struct MKFid *) old_fid;
+ struct MKFid *fid_new = (struct MKFid *) new_fid;
+ int code;
+
+ /* Find entry. */
+ code = FindItem(dir, entry, &prevbuf, &entrybuf);
+ if (code) {
+ return code;
+ }
+ firstitem = (struct DirEntry *)entrybuf.data;
+ DRelease(&prevbuf, 1);
+
+ /* Replace fid. */
+ if (!old_fid ||
+ ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
+ (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
+
+ firstitem->fid.vnode = htonl(fid_new->vnode);
+ firstitem->fid.vunique = htonl(fid_new->vunique);
+ }
+
+ DRelease(&entrybuf, 1);
+
+ return 0;
}