/*
* Copyright 2000, International Business Machines Corporation and others.
* All Rights Reserved.
- *
+ *
* This software has been released under the terms of the IBM Public
* License. For details, see the LICENSE file in the top-level source
* directory or online at http://www.openafs.org/dl/license10.html
#include "h/param.h"
#include "h/types.h"
#include "h/time.h"
-#if defined(AFS_AIX31_ENV)
+#if defined(AFS_AIX31_ENV)
#include "h/limits.h"
#endif
#if !defined(AFS_AIX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
#ifndef BUF_TIME_MAX
#define BUF_TIME_MAX 0x7fffffff
#endif
-/* number of pages per Unix buffer, when we're using Unix buffer pool */
-#define NPB 4
+#define NPB 8 /* must be a pwer of 2 */
+static int afs_max_buffers; /* should be an integral multiple of NPB */
+
/* page size */
#define AFS_BUFFER_PAGESIZE 2048
/* log page size */
#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;
/* Prototypes for static routines */
static struct buffer *afs_newslot(struct dcache *adc, afs_int32 apage,
- register struct buffer *lp);
+ struct buffer *lp);
static int dinit_flag = 0;
void
DInit(int abuffers)
{
/* Initialize the venus buffer system. */
- register int i;
- register struct buffer *tb;
-#if defined(AFS_USEBUFFERS)
- struct buf *tub; /* unix buffer for allocation */
-#endif
+ int i;
+ struct buffer *tb;
AFS_STATCNT(DInit);
if (dinit_flag)
return;
dinit_flag = 1;
-#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
+ afs_max_buffers = abuffers << 2; /* possibly grow up to 4 times as big */
LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
- Buffers =
- (struct buffer *)afs_osi_Alloc(abuffers * sizeof(struct buffer));
-#if !defined(AFS_USEBUFFERS)
- BufferData = (char *)afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
-#endif
+ Buffers = afs_osi_Alloc(afs_max_buffers * sizeof(struct buffer));
+ osi_Assert(Buffers != NULL);
timecounter = 1;
afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
for (i = 0; i < PHSIZE; i++)
phTable[i] = 0;
for (i = 0; i < abuffers; i++) {
-#if defined(AFS_USEBUFFERS)
if ((i & (NPB - 1)) == 0) {
/* time to allocate a fresh buffer */
- tub = geteblk(AFS_BUFFER_PAGESIZE * NPB);
- BufferData = (char *)tub->b_un.b_addr;
+ BufferData = afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
+ osi_Assert(BufferData != NULL);
}
-#endif
/* Fill in each buffer with an empty indication. */
tb = &Buffers[i];
tb->fid = NULLIDX;
afs_reset_inode(&tb->inode);
tb->accesstime = 0;
tb->lockers = 0;
-#if defined(AFS_USEBUFFERS)
- if ((i & (NPB - 1)) == 0)
- tb->bufp = tub;
- else
- tb->bufp = 0;
tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i & (NPB - 1))];
-#else
- tb->data = &BufferData[AFS_BUFFER_PAGESIZE * i];
-#endif
tb->hashIndex = 0;
tb->dirty = 0;
AFS_RWLOCK_INIT(&tb->lock, "buffer lock");
return;
}
-void *
-DRead(register struct dcache *adc, register int page)
+int
+DRead(struct dcache *adc, int page, struct DirBuffer *entry)
{
/* Read a page from the disk. */
- register struct buffer *tb, *tb2;
+ struct buffer *tb, *tb2;
struct osi_file *tfile;
int code;
AFS_STATCNT(DRead);
- MObtainWriteLock(&afs_bufferLock, 256);
+
+ memset(entry, 0, sizeof(struct DirBuffer));
+
+ ObtainWriteLock(&afs_bufferLock, 256);
#define bufmatch(tb) (tb->page == page && tb->fid == adc->index)
#define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
/* this apparently-complicated-looking code is simply an example of
- * a little bit of loop unrolling, and is a standard linked-list
+ * a little bit of loop unrolling, and is a standard linked-list
* traversal trick. It saves a few assignments at the the expense
* of larger code size. This could be simplified by better use of
- * macros.
+ * macros.
*/
if ((tb = phTable[pHash(adc->index, page)])) {
if (bufmatch(tb)) {
- MObtainWriteLock(&tb->lock, 257);
- ReleaseWriteLock(&afs_bufferLock);
+ ObtainWriteLock(&tb->lock, 257);
tb->lockers++;
+ ReleaseWriteLock(&afs_bufferLock);
tb->accesstime = timecounter++;
AFS_STATS(afs_stats_cmperf.bufHits++);
- MReleaseWriteLock(&tb->lock);
- return tb->data;
+ ReleaseWriteLock(&tb->lock);
+ entry->buffer = tb;
+ entry->data = tb->data;
+ return 0;
} else {
- register struct buffer **bufhead;
+ struct buffer **bufhead;
bufhead = &(phTable[pHash(adc->index, page)]);
while ((tb2 = tb->hashNext)) {
if (bufmatch(tb2)) {
buf_Front(bufhead, tb, tb2);
- MObtainWriteLock(&tb2->lock, 258);
- ReleaseWriteLock(&afs_bufferLock);
+ ObtainWriteLock(&tb2->lock, 258);
tb2->lockers++;
+ ReleaseWriteLock(&afs_bufferLock);
tb2->accesstime = timecounter++;
AFS_STATS(afs_stats_cmperf.bufHits++);
- MReleaseWriteLock(&tb2->lock);
- return tb2->data;
+ ReleaseWriteLock(&tb2->lock);
+ entry->buffer = tb2;
+ entry->data = tb2->data;
+ return 0;
}
if ((tb = tb2->hashNext)) {
if (bufmatch(tb)) {
buf_Front(bufhead, tb2, tb);
- MObtainWriteLock(&tb->lock, 259);
- ReleaseWriteLock(&afs_bufferLock);
+ ObtainWriteLock(&tb->lock, 259);
tb->lockers++;
+ ReleaseWriteLock(&afs_bufferLock);
tb->accesstime = timecounter++;
AFS_STATS(afs_stats_cmperf.bufHits++);
- MReleaseWriteLock(&tb->lock);
- return tb->data;
+ ReleaseWriteLock(&tb->lock);
+ entry->buffer = tb;
+ entry->data = tb->data;
+ return 0;
}
} else
break;
AFS_STATS(afs_stats_cmperf.bufMisses++);
/* can't find it */
/* The last thing we looked at was either tb or tb2 (or nothing). That
- * is at least the oldest buffer on one particular hash chain, so it's
+ * is at least the oldest buffer on one particular hash chain, so it's
* a pretty good place to start looking for the truly oldest buffer.
*/
tb = afs_newslot(adc, page, (tb ? tb : tb2));
if (!tb) {
- MReleaseWriteLock(&afs_bufferLock);
- return NULL;
+ ReleaseWriteLock(&afs_bufferLock);
+ return EIO;
}
- MObtainWriteLock(&tb->lock, 260);
- MReleaseWriteLock(&afs_bufferLock);
+ ObtainWriteLock(&tb->lock, 260);
tb->lockers++;
+ ReleaseWriteLock(&afs_bufferLock);
if (page * AFS_BUFFER_PAGESIZE >= adc->f.chunkBytes) {
tb->fid = NULLIDX;
afs_reset_inode(&tb->inode);
tb->lockers--;
- MReleaseWriteLock(&tb->lock);
- return NULL;
+ ReleaseWriteLock(&tb->lock);
+ return EIO;
}
tfile = afs_CFileOpen(&adc->f.inode);
code =
tb->fid = NULLIDX;
afs_reset_inode(&tb->inode);
tb->lockers--;
- MReleaseWriteLock(&tb->lock);
- return NULL;
+ ReleaseWriteLock(&tb->lock);
+ return EIO;
}
/* Note that findslot sets the page field in the buffer equal to
* what it is searching for. */
- MReleaseWriteLock(&tb->lock);
- return tb->data;
+ ReleaseWriteLock(&tb->lock);
+ entry->buffer = tb;
+ entry->data = tb->data;
+ return 0;
}
static void
-FixupBucket(register struct buffer *ap)
+FixupBucket(struct buffer *ap)
{
- register struct buffer **lp, *tp;
- register int i;
+ struct buffer **lp, *tp;
+ int i;
/* first try to get it out of its current hash bucket, in which it
* might not be */
AFS_STATCNT(FixupBucket);
/* lp is pointer to a fairly-old buffer */
static struct buffer *
-afs_newslot(struct dcache *adc, afs_int32 apage, register struct buffer *lp)
+afs_newslot(struct dcache *adc, afs_int32 apage, struct buffer *lp)
{
/* Find a usable buffer slot */
- register afs_int32 i;
- afs_int32 lt;
- register struct buffer *tp;
+ afs_int32 i;
+ afs_int32 lt = 0;
+ struct buffer *tp;
struct osi_file *tfile;
AFS_STATCNT(afs_newslot);
if (lp && (lp->lockers == 0)) {
lt = lp->accesstime;
} else {
- lp = 0;
- lt = BUF_TIME_MAX;
+ lp = NULL;
}
/* timecounter might have wrapped, if machine is very very busy
tp = Buffers;
for (i = 0; i < nbuffers; i++, tp++) {
if (tp->lockers == 0) {
- if (tp->accesstime < lt) {
+ if (!lp || tp->accesstime < lt) {
lp = tp;
lt = tp->accesstime;
}
}
if (lp == 0) {
- /* There are no unlocked buffers -- this used to panic, but that
- * 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("afs: all buffers locked\n");
- return 0;
+ /* No unlocked buffers. If still possible, allocate a new increment */
+ if (nbuffers + NPB > afs_max_buffers) {
+ /* There are no unlocked buffers -- this used to panic, but that
+ * 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("afs: all buffers locked\n");
+ return 0;
+ }
+
+ BufferData = afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
+ osi_Assert(BufferData != NULL);
+ for (i = 0; i< NPB; i++) {
+ /* Fill in each buffer with an empty indication. */
+ tp = &Buffers[i + nbuffers];
+ tp->fid = NULLIDX;
+ afs_reset_inode(&tp->inode);
+ tp->accesstime = 0;
+ tp->lockers = 0;
+ tp->data = &BufferData[AFS_BUFFER_PAGESIZE * i];
+ tp->hashIndex = 0;
+ tp->dirty = 0;
+ AFS_RWLOCK_INIT(&tp->lock, "buffer lock");
+ }
+ lp = &Buffers[nbuffers];
+ nbuffers += NPB;
}
if (lp->dirty) {
}
void
-DRelease(void *loc, int flag)
+DRelease(struct DirBuffer *entry, 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 defined(AFS_USEBUFFERS)
- register struct buffer *tp;
-#endif
+ struct buffer *tp;
AFS_STATCNT(DRelease);
- if (!bp)
+
+ tp = entry->buffer;
+ if (tp == NULL)
return;
-#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) {
- if ((afs_int32) bp >= (afs_int32) tp->data
- && (afs_int32) bp <
- (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
- /* we found the right range */
- index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
- break;
- }
- }
-#else
- index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
-#endif
- bp = &(Buffers[index]);
- MObtainWriteLock(&bp->lock, 261);
- bp->lockers--;
+
+ tp = entry->buffer;
+ ObtainWriteLock(&tp->lock, 261);
+ tp->lockers--;
if (flag)
- bp->dirty = 1;
- MReleaseWriteLock(&bp->lock);
+ tp->dirty = 1;
+ ReleaseWriteLock(&tp->lock);
}
int
-DVOffset(register void *ap)
+DVOffset(struct DirBuffer *entry)
{
- /* Return the byte within a file represented by a buffer pointer. */
- register struct buffer *bp;
- register int index;
-#if defined(AFS_USEBUFFERS)
- register struct buffer *tp;
-#endif
+ struct buffer *bp;
+
AFS_STATCNT(DVOffset);
- bp = ap;
-#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) {
- if ((afs_int32) bp >= (afs_int32) tp->data
- && (afs_int32) bp <
- (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
- /* we found the right range */
- index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
- break;
- }
- }
-#else
- index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
-#endif
- if (index < 0 || index >= nbuffers)
- return -1;
- bp = &(Buffers[index]);
- return AFS_BUFFER_PAGESIZE * bp->page + (int)(((char *)ap) - bp->data);
+
+ bp = entry->buffer;
+ return AFS_BUFFER_PAGESIZE * bp->page
+ + (char *)entry->data - (char *)bp->data;
}
-/*!
+/*!
* 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
+ * 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)
{
- register int i;
+ int i;
/* Destroy all buffers pertaining to a particular fid. */
- register struct buffer *tb;
+ struct buffer *tb;
AFS_STATCNT(DZap);
- MObtainReadLock(&afs_bufferLock);
+ 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) {
- MObtainWriteLock(&tb->lock, 262);
+ ObtainWriteLock(&tb->lock, 262);
tb->fid = NULLIDX;
afs_reset_inode(&tb->inode);
tb->dirty = 0;
- MReleaseWriteLock(&tb->lock);
+ ReleaseWriteLock(&tb->lock);
}
- MReleaseReadLock(&afs_bufferLock);
+ ReleaseReadLock(&afs_bufferLock);
}
static void
-DFlushBuffer(struct buffer *ab) {
+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);
}
void
-DFlushDCache(struct dcache *adc)
+DFlushDCache(struct dcache *adc)
{
int i;
struct buffer *tb;
ReleaseReadLock(&afs_bufferLock);
}
-void
+int
DFlush(void)
{
/* Flush all the modified buffers. */
- register int i;
- register struct buffer *tb;
+ int i;
+ struct buffer *tb;
AFS_STATCNT(DFlush);
tb = Buffers;
- MObtainReadLock(&afs_bufferLock);
+ ObtainReadLock(&afs_bufferLock);
for (i = 0; i < nbuffers; i++, tb++) {
if (tb->dirty) {
- MObtainWriteLock(&tb->lock, 263);
+ ObtainWriteLock(&tb->lock, 263);
tb->lockers++;
- MReleaseReadLock(&afs_bufferLock);
+ ReleaseReadLock(&afs_bufferLock);
if (tb->dirty) {
/* it seems safe to do this I/O without having the dcache
* locked, since the only things that will update the data in
* a directory are the buffer package, which holds the relevant
- * tb->lock while doing the write, or afs_GetDCache, which
+ * tb->lock while doing the write, or afs_GetDCache, which
* DZap's the directory while holding the dcache lock.
* It is not possible to lock the dcache or even call
* afs_GetDSlot to map the index to the dcache since the dir
DFlushBuffer(tb);
}
tb->lockers--;
- MReleaseWriteLock(&tb->lock);
- MObtainReadLock(&afs_bufferLock);
+ ReleaseWriteLock(&tb->lock);
+ ObtainReadLock(&afs_bufferLock);
}
}
- MReleaseReadLock(&afs_bufferLock);
+ ReleaseReadLock(&afs_bufferLock);
+
+ return 0;
}
-void *
-DNew(register struct dcache *adc, register int page)
+int
+DNew(struct dcache *adc, int page, struct DirBuffer *entry)
{
- /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
- register struct buffer *tb;
+ /* Same as read, only do *not* even try to read the page, since it
+ * probably doesn't exist. */
+ struct buffer *tb;
AFS_STATCNT(DNew);
- MObtainWriteLock(&afs_bufferLock, 264);
+
+ ObtainWriteLock(&afs_bufferLock, 264);
if ((tb = afs_newslot(adc, page, NULL)) == 0) {
- MReleaseWriteLock(&afs_bufferLock);
- return 0;
+ ReleaseWriteLock(&afs_bufferLock);
+ return EIO;
}
/* extend the chunk, if needed */
/* Do it now, not in DFlush or afs_newslot when the data is written out,
* DFlush due to lock hierarchy issues */
if ((page + 1) * AFS_BUFFER_PAGESIZE > adc->f.chunkBytes) {
afs_AdjustSize(adc, (page + 1) * AFS_BUFFER_PAGESIZE);
- afs_WriteDCache(adc, 1);
+ osi_Assert(afs_WriteDCache(adc, 1) == 0);
}
- MObtainWriteLock(&tb->lock, 265);
- MReleaseWriteLock(&afs_bufferLock);
+ ObtainWriteLock(&tb->lock, 265);
tb->lockers++;
- MReleaseWriteLock(&tb->lock);
- return tb->data;
+ ReleaseWriteLock(&afs_bufferLock);
+ ReleaseWriteLock(&tb->lock);
+ entry->buffer = tb;
+ entry->data = tb->data;
+
+ return 0;
}
void
shutdown_bufferpackage(void)
{
-#if defined(AFS_USEBUFFERS)
- register struct buffer *tp;
-#endif
+ struct buffer *tp;
int i;
AFS_STATCNT(shutdown_bufferpackage);
DFlush();
if (afs_cold_shutdown) {
dinit_flag = 0;
-#if !defined(AFS_USEBUFFERS)
- afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
-#else
tp = Buffers;
for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
- /* The following check shouldn't be necessary and it will be removed soon */
- if (!tp->bufp)
- afs_warn
- ("afs: shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
- else {
- brelse(tp->bufp);
- tp->bufp = 0;
- }
+ afs_osi_Free(tp->data, NPB * AFS_BUFFER_PAGESIZE);
}
-#endif
afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
nbuffers = 0;
timecounter = 1;