DRead-check-file-size-20021113
[openafs.git] / src / afs / afs_buffer.c
index 3fc6e4f..4c8556c 100644 (file)
@@ -8,40 +8,42 @@
  */
 
 #include <afsconfig.h>
-#include "../afs/param.h"
+#include "afs/param.h"
 
 RCSID("$Header$");
 
-#include "../afs/sysincludes.h"
+#include "afs/sysincludes.h"
+#include "afsincludes.h"
 #if !defined(UKERNEL)
-#include "../h/param.h"
-#include "../h/types.h"
-#include "../h/time.h"
+#include "h/param.h"
+#include "h/types.h"
+#include "h/time.h"
 #if    defined(AFS_AIX31_ENV) || defined(AFS_DEC_ENV)
-#include "../h/limits.h"
+#include "h/limits.h"
 #endif
 #if    !defined(AFS_AIX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
-#include "../h/kernel.h"    /* Doesn't needed, so it should go */
+#include "h/kernel.h"    /* Doesn't needed, so it should go */
 #endif
 #endif /* !defined(UKERNEL) */
 
-#include "../afs/afs_osi.h"
-#include "../afsint/afsint.h"
-#include "../afs/lock.h"
+#include "afs/afs_osi.h"
+#include "afsint.h"
+#include "afs/lock.h"
 
 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
-#include "../h/buf.h"
+#include "h/buf.h"
 #endif /* !defined(UKERNEL) */
 
-#include "../afs/stds.h"
-#include "../afs/volerrors.h"
-#include "../afs/exporter.h"
-#include "../afs/prs_fs.h"
-#include "../afs/afs_chunkops.h"
-#include "../afs/dir.h"
+#include "afs/stds.h"
+#include "afs/volerrors.h"
+#include "afs/exporter.h"
+#include "afs/prs_fs.h"
+#include "afs/afs_chunkops.h"
+#include "afs/dir.h"
 
-#include "../afs/afs_stats.h"
-#include "../afs/longc_procs.h"
+#include "afs/afs_stats.h"
+#include "afs/longc_procs.h"
+#include "afs/afs.h"
 
 #ifndef        BUF_TIME_MAX
 #define        BUF_TIME_MAX    0x7fffffff
@@ -63,50 +65,33 @@ RCSID("$Header$");
 #define pHash(fid,page) ((((afs_int32)((fid)[0])) & PHFIDMASK) \
                         | (page & PHPAGEMASK))
 
-/* Note: this should agree with the definition in kdump.c */
-#if    defined(AFS_OSF_ENV)
-#if    !defined(UKERNEL)
-#define        AFS_USEBUFFERS  1
-#endif
-#endif
-
 #ifdef dirty
 #undef dirty   /* XXX */
 #endif
 
-struct buffer {
-    ino_t fid[1];      /* Unique cache key + i/o addressing */
-    afs_int32 page;
-    afs_int32 accesstime;
-    struct buffer *hashNext;
-    char *data;
-    char lockers;
-    char dirty;
-    char hashIndex;
-#if AFS_USEBUFFERS
-    struct buf *bufp;
-#endif
-    afs_rwlock_t lock;         /* the lock for this structure */
-} *Buffers = 0;
-
-char *BufferData;
+static struct buffer *Buffers = 0;
+static char *BufferData;
 
 #ifdef AFS_AIX_ENV
 extern struct buf *geteblk();
 #endif
+#ifdef AFS_FBSD_ENV
+#define timecounter afs_timecounter
+#endif
 /* The locks for individual buffer entries are now sometimes obtained while holding the
  * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
  */
 static afs_lock_t afs_bufferLock;
 static struct buffer *phTable[PHSIZE]; /* page hash table */
-int nbuffers;
-afs_int32 timecounter;
+static int nbuffers;
+static afs_int32 timecounter;
 
-static struct buffer *afs_newslot();
+/* Prototypes for static routines */
+static struct buffer *afs_newslot (afs_inode_t *afid, afs_int32 apage,register struct buffer *lp);
 
 static int dinit_flag = 0;
-void DInit (abuffers)
-    int abuffers; {
+void DInit (int abuffers)
+{
     /* Initialize the venus buffer system. */
     register int i;
     register struct buffer *tb;
@@ -158,18 +143,16 @@ void DInit (abuffers)
     return;
 }
 
-char *DRead(fid,page)
-    register ino_t *fid;
-    register int page; {
+char *DRead(register afs_inode_t *fid, register int page)
+{
     /* Read a page from the disk. */
     register struct buffer *tb, *tb2;
-    void *tfile;
-    register afs_int32 code, *sizep;
+    struct osi_file *tfile;
+    int code;
 
     AFS_STATCNT(DRead);
     MObtainWriteLock(&afs_bufferLock,256);
 
-/* some new code added 1/1/92 */
 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
 
@@ -179,7 +162,7 @@ char *DRead(fid,page)
      * of larger code size.  This could be simplified by better use of
      * macros. 
      */
-    if ( tb = phTable[pHash(fid,page)] ) {  /* ASSMT HERE */
+    if ((tb = phTable[pHash(fid,page)])) {
        if (bufmatch(tb)) {
            MObtainWriteLock(&tb->lock,257);
            ReleaseWriteLock(&afs_bufferLock);
@@ -192,7 +175,7 @@ char *DRead(fid,page)
        else {
          register struct buffer **bufhead;
          bufhead = &( phTable[pHash(fid,page)] );
-         while (tb2 = tb->hashNext) {
+         while ((tb2 = tb->hashNext)) {
            if (bufmatch(tb2)) {
              buf_Front(bufhead,tb,tb2);
              MObtainWriteLock(&tb2->lock,258);
@@ -203,7 +186,7 @@ char *DRead(fid,page)
              MReleaseWriteLock(&tb2->lock);
              return tb2->data;
            }
-           if (tb = tb2->hashNext) { /* ASSIGNMENT HERE! */ 
+           if ((tb = tb2->hashNext)) {
              if (bufmatch(tb)) {
                buf_Front(bufhead,tb2,tb);
                MObtainWriteLock(&tb->lock,259);
@@ -229,20 +212,19 @@ char *DRead(fid,page)
      */
     tb = afs_newslot(fid, page, (tb ? tb : tb2));
     if (!tb) {
-      MReleaseWriteLock(&afs_bufferLock);
-      return 0;
+       MReleaseWriteLock(&afs_bufferLock);
+       return NULL;
     }
     MObtainWriteLock(&tb->lock,260);
     MReleaseWriteLock(&afs_bufferLock);
     tb->lockers++;
     tfile = afs_CFileOpen(fid[0]);
-    sizep = (afs_int32 *)tfile;
-    if (page * AFS_BUFFER_PAGESIZE >= *sizep) {
+    if (page * AFS_BUFFER_PAGESIZE >= tfile->size) {
        dirp_Zap(tb->fid);
        tb->lockers--;
        MReleaseWriteLock(&tb->lock);
        afs_CFileClose(tfile);
-       return 0;
+       return NULL;
     }
     code = afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE,
                         tb->data, AFS_BUFFER_PAGESIZE);
@@ -251,7 +233,7 @@ char *DRead(fid,page)
        dirp_Zap(tb->fid);
        tb->lockers--;
        MReleaseWriteLock(&tb->lock);
-       return 0;
+       return NULL;
     }
     /* Note that findslot sets the page field in the buffer equal to
      * what it is searching for. */
@@ -259,8 +241,8 @@ char *DRead(fid,page)
     return tb->data;
 }
 
-static void FixupBucket(ap)
-    register struct buffer *ap; {
+static void FixupBucket(register struct buffer *ap)
+{
     register struct buffer **lp, *tp;
     register int i;
     /* first try to get it out of its current hash bucket, in which it
@@ -282,16 +264,14 @@ static void FixupBucket(ap)
     phTable[i] = ap;            /* at the front, since it's LRU */
 }
 
-static struct buffer *afs_newslot (afid,apage,lp)
-     ino_t *afid;
-     afs_int32 apage; 
-     register struct buffer *lp;   /* pointer to a fairly-old buffer */
+/* lp is pointer to a fairly-old buffer */
+static struct buffer *afs_newslot (afs_inode_t *afid, afs_int32 apage,register struct buffer *lp)
 {
     /* Find a usable buffer slot */
     register afs_int32 i;
     afs_int32 lt;
     register struct buffer *tp;
-    void *tfile;
+    struct osi_file *tfile;
 
     AFS_STATCNT(afs_newslot);
     /* we take a pointer here to a buffer which was at the end of an
@@ -371,9 +351,8 @@ static struct buffer *afs_newslot (afid,apage,lp)
     return lp;
 }
 
-void DRelease (bp,flag)
-    register struct buffer *bp;
-    int flag; {
+void DRelease (register struct buffer *bp, int flag)
+{
     /* Release a buffer, specifying whether or not the buffer has been
      * modified by the locker. */
     register int index;
@@ -404,8 +383,8 @@ void DRelease (bp,flag)
     MReleaseWriteLock(&bp->lock);
 }
 
-DVOffset (ap)
-    register void *ap; {
+int DVOffset (register void *ap)
+{
     /* Return the byte within a file represented by a buffer pointer. */
     register struct buffer *bp;
     register int index;
@@ -438,8 +417,7 @@ DVOffset (ap)
  * of the hash function.  Oh well.  This should use the list traversal 
  * method of DRead...
  */
-void DZap (fid)
-    ino_t *fid;
+void DZap (afs_inode_t *fid)
 {
     register int i;
     /* Destroy all buffers pertaining to a particular fid. */
@@ -459,11 +437,12 @@ void DZap (fid)
     MReleaseReadLock(&afs_bufferLock);
 }
 
-void DFlush () {
+void DFlush (void)
+{
     /* Flush all the modified buffers. */
-    register int i, code;
+    register int i;
     register struct buffer *tb;
-    void *tfile;
+    struct osi_file *tfile;
 
     AFS_STATCNT(DFlush);
     tb = Buffers;
@@ -488,9 +467,7 @@ void DFlush () {
     MReleaseReadLock(&afs_bufferLock);
 }
 
-char *DNew (fid,page)
-    register int page;
-    register ino_t *fid;
+char *DNew (register afs_inode_t *fid, register int page)
 {
     /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
     register struct buffer *tb;
@@ -507,7 +484,8 @@ char *DNew (fid,page)
     return tb->data;
 }
 
-void shutdown_bufferpackage() {
+void shutdown_bufferpackage(void)
+{
 #if AFS_USEBUFFERS
     register struct buffer *tp;
 #endif