#include "afs/sysincludes.h"
#include "afsincludes.h"
#if !defined(UKERNEL)
-#include "h/param.h"
+#if !defined(AFS_LINUX26_ENV)
+# include "h/param.h"
+#endif
#include "h/types.h"
#include "h/time.h"
#if defined(AFS_AIX31_ENV)
abuffers = ((abuffers - 1) | (NPB - 1)) + 1;
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(afs_max_buffers * sizeof(struct buffer));
+ 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++)
for (i = 0; i < abuffers; i++) {
if ((i & (NPB - 1)) == 0) {
/* time to allocate a fresh buffer */
- BufferData = (char *) afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
+ BufferData = afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
+ osi_Assert(BufferData != NULL);
}
/* Fill in each buffer with an empty indication. */
tb = &Buffers[i];
return;
}
-void *
-DRead(struct dcache *adc, int page)
+/*!
+ * Read and return the requested directory page.
+ *
+ * \param[in] adc pointer to directory dcache
+ * \param[in] page number of the desired directory page
+ * \param[out] entry buffer to return requested page
+ * \param[out] physerr (optional) pointer to return errno, if any
+ *
+ * \retval 0 success
+ * \retval non-zero invalid directory or internal IO error;
+ * if physerr is supplied by caller, it will be set:
+ * 0 logical error
+ * errno physical error
+ */
+int
+DReadWithErrno(struct dcache *adc, int page, struct DirBuffer *entry, int *physerr)
{
/* Read a page from the disk. */
struct buffer *tb, *tb2;
int code;
AFS_STATCNT(DRead);
+
+ if (physerr != NULL)
+ *physerr = 0;
+
+ memset(entry, 0, sizeof(struct DirBuffer));
+
+ if (adc->f.chunk == 0 && adc->f.chunkBytes == 0) {
+ /* The directory blob is empty, apparently. This is not a valid dir
+ * blob, so throw an error. */
+ return EIO;
+ }
+ if (page * AFS_BUFFER_PAGESIZE >= adc->f.chunkBytes) {
+ return ENOENT; /* past the end */
+ }
+
ObtainWriteLock(&afs_bufferLock, 256);
#define bufmatch(tb) (tb->page == page && tb->fid == adc->index)
tb->accesstime = timecounter++;
AFS_STATS(afs_stats_cmperf.bufHits++);
ReleaseWriteLock(&tb->lock);
- return tb->data;
+ entry->buffer = tb;
+ entry->data = tb->data;
+ return 0;
} else {
struct buffer **bufhead;
bufhead = &(phTable[pHash(adc->index, page)]);
tb2->accesstime = timecounter++;
AFS_STATS(afs_stats_cmperf.bufHits++);
ReleaseWriteLock(&tb2->lock);
- return tb2->data;
+ entry->buffer = tb2;
+ entry->data = tb2->data;
+ return 0;
}
if ((tb = tb2->hashNext)) {
if (bufmatch(tb)) {
tb->accesstime = timecounter++;
AFS_STATS(afs_stats_cmperf.bufHits++);
ReleaseWriteLock(&tb->lock);
- return tb->data;
+ entry->buffer = tb;
+ entry->data = tb->data;
+ return 0;
}
} else
break;
tb = afs_newslot(adc, page, (tb ? tb : tb2));
if (!tb) {
ReleaseWriteLock(&afs_bufferLock);
- return NULL;
+ return EIO;
}
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--;
- ReleaseWriteLock(&tb->lock);
- return NULL;
- }
tfile = afs_CFileOpen(&adc->f.inode);
+ if (!tfile) {
+ code = EIO;
+ goto error;
+ }
code =
afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE, tb->data,
AFS_BUFFER_PAGESIZE);
afs_CFileClose(tfile);
if (code < AFS_BUFFER_PAGESIZE) {
- tb->fid = NULLIDX;
- afs_reset_inode(&tb->inode);
- tb->lockers--;
- ReleaseWriteLock(&tb->lock);
- return NULL;
+ if (code < 0 && physerr != NULL)
+ *physerr = -code;
+ code = EIO;
+ goto error;
}
/* Note that findslot sets the page field in the buffer equal to
* what it is searching for. */
ReleaseWriteLock(&tb->lock);
- return tb->data;
+ entry->buffer = tb;
+ entry->data = tb->data;
+ return 0;
+
+ error:
+ tb->fid = NULLIDX;
+ afs_reset_inode(&tb->inode);
+ tb->lockers--;
+ ReleaseWriteLock(&tb->lock);
+ return code;
+}
+
+/*!
+ * Read and return the requested directory page.
+ *
+ * \param[in] adc pointer to directory dcache
+ * \param[in] page number of the desired directory page
+ * \param[out] entry buffer to return requested page
+ *
+ * \retval 0 success
+ * \retval non-zero invalid directory or internal IO error;
+ */
+int
+DRead(struct dcache *adc, int page, struct DirBuffer *entry)
+{
+ return DReadWithErrno(adc, page, entry, NULL);
}
static void
return 0;
}
- BufferData = (char *) afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
+ 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];
if (lp->dirty) {
/* see DFlush for rationale for not getting and locking the dcache */
tfile = afs_CFileOpen(&lp->inode);
+ if (!tfile)
+ return NULL; /* Callers will flag as EIO */
+
afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE, lp->data,
AFS_BUFFER_PAGESIZE);
lp->dirty = 0;
AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
}
+ /* Zero out the data so we don't leak something we shouldn't. */
+ memset(lp->data, 0, AFS_BUFFER_PAGESIZE);
/* Now fill in the header. */
lp->fid = adc->index;
afs_copy_inode(&lp->inode, &adc->f.inode);
}
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. */
- struct buffer *bp = (struct buffer *)loc;
- int index;
struct buffer *tp;
AFS_STATCNT(DRelease);
- if (!bp)
+
+ tp = entry->buffer;
+ if (tp == NULL)
return;
- /* look for buffer by scanning Unix buffers for appropriate address */
- /* careful: despite the declaration above at this point bp is still simply
- * an address inside the buffer, not a pointer to the buffer header */
- tp = Buffers;
- for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
- if ( (char *) bp >= (char *) tp->data &&
- (char *) bp < (char *) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
- /* we found the right range */
- index += ((char *) bp - (char *) tp->data) >> LOGPS;
- break;
- }
- }
- tp = &(Buffers[index]);
+
ObtainWriteLock(&tp->lock, 261);
tp->lockers--;
if (flag)
}
int
-DVOffset(void *ap)
+DVOffset(struct DirBuffer *entry)
{
- /* Return the byte within a file represented by a buffer pointer. */
- int index;
- struct buffer *tp;
+ struct buffer *bp;
+
AFS_STATCNT(DVOffset);
- /* look for buffer by scanning Unix buffers for appropriate address */
- /* see comment in DRelease about the meaning of ap/bp */
- tp = Buffers;
- for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
- if ( (char *) ap >= (char *) tp->data &&
- (char *) ap < (char *) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
- /* we found the right range */
- index += ((char *) ap - (char *) tp->data) >> LOGPS;
- break;
- }
- }
- if (index < 0 || index >= nbuffers)
- return -1;
- tp = &(Buffers[index]);
- return AFS_BUFFER_PAGESIZE * tp->page + (int)(((char *)ap) - tp->data);
+
+ bp = entry->buffer;
+ return AFS_BUFFER_PAGESIZE * bp->page
+ + (char *)entry->data - (char *)bp->data;
}
/*!
struct osi_file *tfile;
tfile = afs_CFileOpen(&ab->inode);
+ osi_Assert(tfile);
afs_CFileWrite(tfile, ab->page * AFS_BUFFER_PAGESIZE,
ab->data, AFS_BUFFER_PAGESIZE);
ab->dirty = 0; /* Clear the dirty flag */
ReleaseReadLock(&afs_bufferLock);
}
-void
+int
DFlush(void)
{
/* Flush all the modified buffers. */
}
}
ReleaseReadLock(&afs_bufferLock);
+
+ return 0;
}
-void *
-DNew(struct dcache *adc, 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. */
+ /* Same as read, only do *not* even try to read the page, since it
+ * probably doesn't exist. */
struct buffer *tb;
AFS_STATCNT(DNew);
+
ObtainWriteLock(&afs_bufferLock, 264);
if ((tb = afs_newslot(adc, page, NULL)) == 0) {
ReleaseWriteLock(&afs_bufferLock);
- return 0;
+ 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);
}
ObtainWriteLock(&tb->lock, 265);
tb->lockers++;
ReleaseWriteLock(&afs_bufferLock);
ReleaseWriteLock(&tb->lock);
- return tb->data;
+ entry->buffer = tb;
+ entry->data = tb->data;
+
+ return 0;
}
void
AFS_STATCNT(shutdown_bufferpackage);
/* Free all allocated Buffers and associated buffer pages */
DFlush();
+
+ dinit_flag = 0;
+ tp = Buffers;
+ for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
+ afs_osi_Free(tp->data, NPB * AFS_BUFFER_PAGESIZE);
+ }
+ afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
+ Buffers = NULL;
+ nbuffers = 0;
+ timecounter = 1;
+ for (i = 0; i < PHSIZE; i++)
+ phTable[i] = NULL;
+
if (afs_cold_shutdown) {
- dinit_flag = 0;
- tp = Buffers;
- for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
- afs_osi_Free(tp->data, NPB * AFS_BUFFER_PAGESIZE);
- }
- afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
- nbuffers = 0;
- timecounter = 1;
- for (i = 0; i < PHSIZE; i++)
- phTable[i] = 0;
memset(&afs_bufferLock, 0, sizeof(afs_lock_t));
}
}