2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
17 #ifdef AFS_64BIT_IOPS_ENV
18 #define BUFFER_FID_SIZE (9 + 2*sizeof(char*)/sizeof(int))
20 #define BUFFER_FID_SIZE (6 + 2*sizeof(char*)/sizeof(int))
24 /* fid is used for Unique cache key + i/o addressing.
25 * fid size is based on 4 + size of inode and size of pointer
27 afs_int32 fid[BUFFER_FID_SIZE];
30 struct buffer *hashNext;
40 struct Lock afs_bufferLock;
43 #define BUFFER_PAGE_SIZE 2048
46 /* page hash table size */
48 /* The hash table should be somewhat efficient even if there are only
49 * a few partitions (less than 32). So the hash for the fileserver is now
50 * based on the volume id. This means this macro is dependent upon the
51 * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
53 #define pHash(fid) ((fid)[0] & (PHSIZE-1))
54 #define vHash(vid) (vid & (PHSIZE-1))
56 /* admittedly system dependent, this is the maximum signed 32-bit value */
57 #define BUFFER_LONG_MAX 2147483647
62 static struct buffer **Buffers;
66 static struct buffer *phTable[PHSIZE]; /* page hash table */
67 static struct buffer *LastBuffer;
70 static int calls = 0, ios = 0;
72 struct buffer *newslot(afs_int32 *afid, afs_int32 apage,
75 /* XXX - This sucks. The correct prototypes for these functions are ...
77 * extern void FidZero(DirHandle *);
78 * extern int FidEq(DirHandle *a, DirHandle *b);
79 * extern int ReallyRead(DirHandle *a, int block, char *data);
82 extern void FidZero(afs_int32 *file);
83 extern int FidEq(afs_int32 *a, afs_int32 *b);
84 extern int ReallyRead(afs_int32 *file, int block, char *data);
85 extern int ReallyWrite(afs_int32 *file, int block, char *data);
86 extern void FidZap(afs_int32 *file);
87 extern int FidVolEq(afs_int32 *file, afs_int32 vid);
88 extern void FidCpy(afs_int32 *tofile, afs_int32 *fromfile);
89 extern void Die(char *msg);
92 DStat(int *abuffers, int *acalls, int *aios)
103 /* Initialize the venus buffer system. */
104 register int i, tsize;
105 register struct buffer *tb;
108 Lock_Init(&afs_bufferLock);
109 /* Align each element of Buffers on a doubleword boundary */
110 tsize = (sizeof(struct buffer) + 7) & ~7;
111 tp = (char *)malloc(abuffers * tsize);
112 Buffers = (struct buffer **)malloc(abuffers * sizeof(struct buffer *));
113 BufferData = (char *)malloc(abuffers * BUFFER_PAGE_SIZE);
115 LastBuffer = (struct buffer *)tp;
117 for (i = 0; i < PHSIZE; i++)
119 for (i = 0; i < abuffers; i++) {
120 /* Fill in each buffer with an empty indication. */
121 tb = (struct buffer *)tp;
125 tb->accesstime = tb->lockers = 0;
126 tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
129 Lock_Init(&tb->lock);
135 DRead(register afs_int32 *fid, register int page)
137 /* Read a page from the disk. */
138 register struct buffer *tb, *tb2, **bufhead;
140 ObtainWriteLock(&afs_bufferLock);
143 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
144 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
146 /* this apparently-complicated-looking code is simply an example of
147 * a little bit of loop unrolling, and is a standard linked-list
148 * traversal trick. It saves a few assignments at the the expense
149 * of larger code size. This could be simplified by better use of
150 * macros. With the use of these LRU queues, the old one-cache is
153 if ((tb = phTable[pHash(fid)])) { /* ASSMT HERE */
155 ObtainWriteLock(&tb->lock);
157 ReleaseWriteLock(&afs_bufferLock);
158 tb->accesstime = ++timecounter;
159 ReleaseWriteLock(&tb->lock);
162 bufhead = &(phTable[pHash(fid)]);
163 while ((tb2 = tb->hashNext)) {
165 buf_Front(bufhead, tb, tb2);
166 ObtainWriteLock(&tb2->lock);
168 ReleaseWriteLock(&afs_bufferLock);
169 tb2->accesstime = ++timecounter;
170 ReleaseWriteLock(&tb2->lock);
173 if ((tb = tb2->hashNext)) { /* ASSIGNMENT HERE! */
175 buf_Front(bufhead, tb2, tb);
176 ObtainWriteLock(&tb->lock);
178 ReleaseWriteLock(&afs_bufferLock);
179 tb->accesstime = ++timecounter;
180 ReleaseWriteLock(&tb->lock);
191 /* The last thing we looked at was either tb or tb2 (or nothing). That
192 * is at least the oldest buffer on one particular hash chain, so it's
193 * a pretty good place to start looking for the truly oldest buffer.
195 tb = newslot(fid, page, (tb ? tb : tb2));
197 ObtainWriteLock(&tb->lock);
199 ReleaseWriteLock(&afs_bufferLock);
200 if (ReallyRead(tb->fid, tb->page, tb->data)) {
202 FidZap(tb->fid); /* disaster */
203 ReleaseWriteLock(&tb->lock);
206 /* Note that findslot sets the page field in the buffer equal to
207 * what it is searching for.
209 ReleaseWriteLock(&tb->lock);
214 FixupBucket(register struct buffer *ap)
216 register struct buffer **lp, *tp;
219 /* first try to get it out of its current hash bucket, in which it might not be */
222 for (tp = *lp; tp; tp = tp->hashNext) {
229 /* now figure the new hash bucket */
231 ap->hashIndex = i; /* remember where we are for deletion */
232 ap->hashNext = phTable[i]; /* add us to the list */
233 phTable[i] = ap; /* at the front, since it's LRU */
238 newslot(afs_int32 *afid, afs_int32 apage, register struct buffer *lp)
240 /* Find a usable buffer slot */
241 register afs_int32 i;
243 register struct buffer **tbp;
245 if (lp && (lp->lockers == 0)) {
249 lt = BUFFER_LONG_MAX;
253 for (i = 0; i < nbuffers; i++, tbp++) {
254 if ((*tbp)->lockers == 0) {
255 if ((*tbp)->accesstime < lt) {
257 lt = (*tbp)->accesstime;
262 /* There are no unlocked buffers */
265 Die("accesstime counter wrapped");
267 Die("all buffers locked");
270 /* We do not need to lock the buffer here because it has no lockers
271 * and the afs_bufferLock prevents other threads from zapping this
272 * buffer while we are writing it out */
274 if (ReallyWrite(lp->fid, lp->page, lp->data))
275 Die("writing bogus buffer");
279 /* Now fill in the header. */
281 FidCpy(lp->fid, afid); /* set this */
283 lp->accesstime = ++timecounter;
285 FixupBucket(lp); /* move to the right hash bucket */
290 /* Release a buffer, specifying whether or not the buffer has been modified
293 DRelease(void *loc, int flag)
295 struct buffer *bp = (struct buffer *)loc;
300 index = ((char *)bp - BufferData) >> LOGPS;
302 ObtainWriteLock(&bp->lock);
306 ReleaseWriteLock(&bp->lock);
310 DVOffset(register void *ap)
312 /* Return the byte within a file represented by a buffer pointer. */
313 register struct buffer *bp = ap;
316 index = ((char *)bp - BufferData) >> LOGPS;
317 if (index < 0 || index >= nbuffers)
320 return BUFFER_PAGE_SIZE * bp->page + (char *)ap - (char *)bp->data;
324 DZap(register afs_int32 *fid)
326 /* Destroy all buffers pertaining to a particular fid. */
327 register struct buffer *tb;
328 ObtainReadLock(&afs_bufferLock);
329 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
330 if (FidEq(tb->fid, fid)) {
331 ObtainWriteLock(&tb->lock);
334 ReleaseWriteLock(&tb->lock);
336 ReleaseReadLock(&afs_bufferLock);
340 DFlushVolume(register afs_int32 vid)
342 /* Flush all data and release all inode handles for a particular volume */
343 register struct buffer *tb;
344 register int code, rcode = 0;
345 ObtainReadLock(&afs_bufferLock);
346 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
347 if (FidVolEq(tb->fid, vid)) {
348 ObtainWriteLock(&tb->lock);
350 code = ReallyWrite(tb->fid, tb->page, tb->data);
356 ReleaseWriteLock(&tb->lock);
358 ReleaseReadLock(&afs_bufferLock);
363 DFlushEntry(register afs_int32 *fid)
365 /* Flush pages modified by one entry. */
366 register struct buffer *tb;
369 ObtainReadLock(&afs_bufferLock);
370 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
371 if (FidEq(tb->fid, fid) && tb->dirty) {
372 ObtainWriteLock(&tb->lock);
374 code = ReallyWrite(tb->fid, tb->page, tb->data);
376 ReleaseWriteLock(&tb->lock);
377 ReleaseReadLock(&afs_bufferLock);
382 ReleaseWriteLock(&tb->lock);
384 ReleaseReadLock(&afs_bufferLock);
391 /* Flush all the modified buffers. */
393 register struct buffer **tbp;
394 afs_int32 code, rcode;
398 ObtainReadLock(&afs_bufferLock);
399 for (i = 0; i < nbuffers; i++, tbp++) {
401 ObtainWriteLock(&(*tbp)->lock);
403 ReleaseReadLock(&afs_bufferLock);
405 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
407 (*tbp)->dirty = 0; /* Clear the dirty flag */
408 if (code && !rcode) {
413 ReleaseWriteLock(&(*tbp)->lock);
414 ObtainReadLock(&afs_bufferLock);
417 ReleaseReadLock(&afs_bufferLock);
422 DNew(register afs_int32 *fid, register int page)
424 /* Same as read, only do *not* even try to read the page,
425 * since it probably doesn't exist.
427 register struct buffer *tb;
428 ObtainWriteLock(&afs_bufferLock);
429 if ((tb = newslot(fid, page, 0)) == 0) {
430 ReleaseWriteLock(&afs_bufferLock);
433 ObtainWriteLock(&tb->lock);
435 ReleaseWriteLock(&afs_bufferLock);
436 ReleaseWriteLock(&tb->lock);