Fix locking in afs_buffer.c
[openafs.git] / src / afs / afs_buffer.c
index f8894e7..220b496 100644 (file)
@@ -10,8 +10,6 @@
 #include <afsconfig.h>
 #include "afs/param.h"
 
-RCSID
-    ("$Header$");
 
 #include "afs/sysincludes.h"
 #include "afsincludes.h"
@@ -43,7 +41,6 @@ RCSID
 #include "afs/dir.h"
 
 #include "afs/afs_stats.h"
-#include "afs/longc_procs.h"
 #include "afs/afs.h"
 
 #ifndef        BUF_TIME_MAX
@@ -79,9 +76,23 @@ extern struct buf *geteblk();
 #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.
+
+/* A note on locking in 'struct buffer'
+ *
+ * afs_bufferLock protects the hash chain, and the 'lockers' field where that
+ * has a zero value. It must be held whenever lockers is incremented from zero.
+ *
+ * The individual buffer lock protects the contents of the structure, including
+ * the lockers field.
+ *
+ * For safety: afs_bufferLock and the individual buffer lock must be held
+ * when obtaining a reference on a structure. Only the individual buffer lock
+ * need be held when releasing a reference.
+ *
+ * The locking hierarchy is afs_bufferLock-> buffer.lock
+ *
  */
+
 static afs_lock_t afs_bufferLock;
 static struct buffer *phTable[PHSIZE]; /* page hash table */
 static int nbuffers;
@@ -98,7 +109,7 @@ DInit(int abuffers)
     /* Initialize the venus buffer system. */
     register int i;
     register struct buffer *tb;
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
     struct buf *tub;           /* unix buffer for allocation */
 #endif
 
@@ -106,14 +117,14 @@ DInit(int abuffers)
     if (dinit_flag)
        return;
     dinit_flag = 1;
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
     /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
     abuffers = ((abuffers - 1) | (NPB - 1)) + 1;
 #endif
     LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
     Buffers =
        (struct buffer *)afs_osi_Alloc(abuffers * sizeof(struct buffer));
-#if !AFS_USEBUFFERS
+#if !defined(AFS_USEBUFFERS)
     BufferData = (char *)afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
 #endif
     timecounter = 1;
@@ -121,7 +132,7 @@ DInit(int abuffers)
     for (i = 0; i < PHSIZE; i++)
        phTable[i] = 0;
     for (i = 0; i < abuffers; i++) {
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
        if ((i & (NPB - 1)) == 0) {
            /* time to allocate a fresh buffer */
            tub = geteblk(AFS_BUFFER_PAGESIZE * NPB);
@@ -130,11 +141,11 @@ DInit(int abuffers)
 #endif
        /* Fill in each buffer with an empty indication. */
        tb = &Buffers[i];
-       tb->fid = 0;
-       tb->inode = 0;
+       tb->fid = NULLIDX;
+       afs_reset_inode(&tb->inode);
        tb->accesstime = 0;
        tb->lockers = 0;
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
        if ((i & (NPB - 1)) == 0)
            tb->bufp = tub;
        else
@@ -145,7 +156,7 @@ DInit(int abuffers)
 #endif
        tb->hashIndex = 0;
        tb->dirty = 0;
-       RWLOCK_INIT(&tb->lock, "buffer lock");
+       AFS_RWLOCK_INIT(&tb->lock, "buffer lock");
     }
     return;
 }
@@ -173,8 +184,8 @@ DRead(register struct dcache *adc, register int page)
     if ((tb = phTable[pHash(adc->index, page)])) {
        if (bufmatch(tb)) {
            MObtainWriteLock(&tb->lock, 257);
-           ReleaseWriteLock(&afs_bufferLock);
            tb->lockers++;
+           ReleaseWriteLock(&afs_bufferLock);
            tb->accesstime = timecounter++;
            AFS_STATS(afs_stats_cmperf.bufHits++);
            MReleaseWriteLock(&tb->lock);
@@ -186,8 +197,8 @@ DRead(register struct dcache *adc, register int page)
                if (bufmatch(tb2)) {
                    buf_Front(bufhead, tb, tb2);
                    MObtainWriteLock(&tb2->lock, 258);
-                   ReleaseWriteLock(&afs_bufferLock);
                    tb2->lockers++;
+                   ReleaseWriteLock(&afs_bufferLock);
                    tb2->accesstime = timecounter++;
                    AFS_STATS(afs_stats_cmperf.bufHits++);
                    MReleaseWriteLock(&tb2->lock);
@@ -197,8 +208,8 @@ DRead(register struct dcache *adc, register int page)
                    if (bufmatch(tb)) {
                        buf_Front(bufhead, tb2, tb);
                        MObtainWriteLock(&tb->lock, 259);
-                       ReleaseWriteLock(&afs_bufferLock);
                        tb->lockers++;
+                       ReleaseWriteLock(&afs_bufferLock);
                        tb->accesstime = timecounter++;
                        AFS_STATS(afs_stats_cmperf.bufHits++);
                        MReleaseWriteLock(&tb->lock);
@@ -223,23 +234,23 @@ DRead(register struct dcache *adc, register int page)
        return NULL;
     }
     MObtainWriteLock(&tb->lock, 260);
-    MReleaseWriteLock(&afs_bufferLock);
     tb->lockers++;
+    MReleaseWriteLock(&afs_bufferLock);
     if (page * AFS_BUFFER_PAGESIZE >= adc->f.chunkBytes) {
-       tb->fid = 0;
-       tb->inode = 0;
+       tb->fid = NULLIDX;
+       afs_reset_inode(&tb->inode);
        tb->lockers--;
        MReleaseWriteLock(&tb->lock);
        return NULL;
     }
-    tfile = afs_CFileOpen(adc->f.inode);
+    tfile = afs_CFileOpen(&adc->f.inode);
     code =
        afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE, tb->data,
                      AFS_BUFFER_PAGESIZE);
     afs_CFileClose(tfile);
     if (code < AFS_BUFFER_PAGESIZE) {
-       tb->fid = 0;
-       tb->inode = 0;
+       tb->fid = NULLIDX;
+       afs_reset_inode(&tb->inode);
        tb->lockers--;
        MReleaseWriteLock(&tb->lock);
        return NULL;
@@ -338,13 +349,13 @@ afs_newslot(struct dcache *adc, afs_int32 apage, register struct buffer *lp)
         * seems extreme.  To the best of my knowledge, all the callers
         * of DRead are prepared to handle a zero return.  Some of them
         * just panic directly, but not all of them. */
-       afs_warn("all buffers locked");
+       afs_warn("afs: all buffers locked\n");
        return 0;
     }
 
     if (lp->dirty) {
        /* see DFlush for rationale for not getting and locking the dcache */
-       tfile = afs_CFileOpen(lp->inode);
+        tfile = afs_CFileOpen(&lp->inode);
        afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE, lp->data,
                       AFS_BUFFER_PAGESIZE);
        lp->dirty = 0;
@@ -354,7 +365,7 @@ afs_newslot(struct dcache *adc, afs_int32 apage, register struct buffer *lp)
 
     /* Now fill in the header. */
     lp->fid = adc->index;
-    lp->inode = adc->f.inode;
+    afs_copy_inode(&lp->inode, &adc->f.inode);
     lp->page = apage;
     lp->accesstime = timecounter++;
     FixupBucket(lp);           /* move to the right hash bucket */
@@ -363,19 +374,20 @@ afs_newslot(struct dcache *adc, afs_int32 apage, register struct buffer *lp)
 }
 
 void
-DRelease(register struct buffer *bp, int flag)
+DRelease(void *loc, int flag)
 {
     /* Release a buffer, specifying whether or not the buffer has been
      * modified by the locker. */
+    register struct buffer *bp = (struct buffer *)loc;
     register int index;
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
     register struct buffer *tp;
 #endif
 
     AFS_STATCNT(DRelease);
     if (!bp)
        return;
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
     /* look for buffer by scanning Unix buffers for appropriate address */
     tp = Buffers;
     for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
@@ -404,12 +416,12 @@ DVOffset(register void *ap)
     /* Return the byte within a file represented by a buffer pointer. */
     register struct buffer *bp;
     register int index;
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
     register struct buffer *tp;
 #endif
     AFS_STATCNT(DVOffset);
     bp = ap;
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
     /* look for buffer by scanning Unix buffers for appropriate address */
     tp = Buffers;
     for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
@@ -430,11 +442,16 @@ DVOffset(register void *ap)
     return AFS_BUFFER_PAGESIZE * bp->page + (int)(((char *)ap) - bp->data);
 }
 
-/* 1/1/91 - I've modified the hash function to take the page as well
+/*! 
+ * Zap one dcache entry: destroy one FID's buffers.
+ *
+ * 1/1/91 - I've modified the hash function to take the page as well
  * as the *fid, so that lookup will be a bit faster.  That presents some
  * difficulties for Zap, which now has to have some knowledge of the nature
  * of the hash function.  Oh well.  This should use the list traversal 
  * method of DRead...
+ *
+ * \param adc The dcache entry to be zapped.
  */
 void
 DZap(struct dcache *adc)
@@ -450,21 +467,56 @@ DZap(struct dcache *adc)
        for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
            if (tb->fid == adc->index) {
                MObtainWriteLock(&tb->lock, 262);
-               tb->fid = 0;
-               tb->inode = 0;
+               tb->fid = NULLIDX;
+               afs_reset_inode(&tb->inode);
                tb->dirty = 0;
                MReleaseWriteLock(&tb->lock);
            }
     MReleaseReadLock(&afs_bufferLock);
 }
 
+static void
+DFlushBuffer(struct buffer *ab) {
+    struct osi_file *tfile;
+    
+    tfile = afs_CFileOpen(&ab->inode);
+    afs_CFileWrite(tfile, ab->page * AFS_BUFFER_PAGESIZE,
+                  ab->data, AFS_BUFFER_PAGESIZE);
+    ab->dirty = 0;     /* Clear the dirty flag */
+    afs_CFileClose(tfile);
+}
+
+void
+DFlushDCache(struct dcache *adc) 
+{
+    int i;
+    struct buffer *tb;
+
+    ObtainReadLock(&afs_bufferLock);
+
+    for (i = 0; i <= PHPAGEMASK; i++)
+        for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
+           if (tb->fid == adc->index) {
+               ObtainWriteLock(&tb->lock, 701);
+               tb->lockers++;
+               ReleaseReadLock(&afs_bufferLock);
+               if (tb->dirty) {
+                   DFlushBuffer(tb);
+               }
+               tb->lockers--;
+               ReleaseWriteLock(&tb->lock);
+               ObtainReadLock(&afs_bufferLock);
+           }
+
+    ReleaseReadLock(&afs_bufferLock);
+}
+
 void
 DFlush(void)
 {
     /* Flush all the modified buffers. */
     register int i;
     register struct buffer *tb;
-    struct osi_file *tfile;
 
     AFS_STATCNT(DFlush);
     tb = Buffers;
@@ -486,11 +538,7 @@ DFlush(void)
                 * we cannot lock afs_xdcache). In addition, we cannot obtain
                 * a dcache lock while holding the tb->lock of the same file
                 * since that can deadlock with DRead/DNew */
-               tfile = afs_CFileOpen(tb->inode);
-               afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
-                              tb->data, AFS_BUFFER_PAGESIZE);
-               tb->dirty = 0;  /* Clear the dirty flag */
-               afs_CFileClose(tfile);
+               DFlushBuffer(tb);
            }
            tb->lockers--;
            MReleaseWriteLock(&tb->lock);
@@ -521,8 +569,8 @@ DNew(register struct dcache *adc, register int page)
        afs_WriteDCache(adc, 1);
     }
     MObtainWriteLock(&tb->lock, 265);
-    MReleaseWriteLock(&afs_bufferLock);
     tb->lockers++;
+    MReleaseWriteLock(&afs_bufferLock);
     MReleaseWriteLock(&tb->lock);
     return tb->data;
 }
@@ -530,18 +578,17 @@ DNew(register struct dcache *adc, register int page)
 void
 shutdown_bufferpackage(void)
 {
-#if AFS_USEBUFFERS
+#if defined(AFS_USEBUFFERS)
     register struct buffer *tp;
 #endif
     int i;
-    extern int afs_cold_shutdown;
 
     AFS_STATCNT(shutdown_bufferpackage);
     /* Free all allocated Buffers and associated buffer pages */
     DFlush();
     if (afs_cold_shutdown) {
        dinit_flag = 0;
-#if !AFS_USEBUFFERS
+#if !defined(AFS_USEBUFFERS)
        afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
 #else
        tp = Buffers;
@@ -549,7 +596,7 @@ shutdown_bufferpackage(void)
            /* The following check shouldn't be necessary and it will be removed soon */
            if (!tp->bufp)
                afs_warn
-                   ("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
+                   ("afs: shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
            else {
                brelse(tp->bufp);
                tp->bufp = 0;
@@ -561,6 +608,6 @@ shutdown_bufferpackage(void)
        timecounter = 1;
        for (i = 0; i < PHSIZE; i++)
            phTable[i] = 0;
-       memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));
+       memset(&afs_bufferLock, 0, sizeof(afs_lock_t));
     }
 }