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>
19 #ifdef AFS_64BIT_IOPS_ENV
20 #define BUFFER_FID_SIZE (9 + 2*sizeof(char*)/sizeof(int))
22 #define BUFFER_FID_SIZE (6 + 2*sizeof(char*)/sizeof(int))
26 /* fid is used for Unique cache key + i/o addressing.
27 * fid size is based on 4 + size of inode and size of pointer
29 afs_int32 fid[BUFFER_FID_SIZE];
32 struct buffer *hashNext;
42 struct Lock afs_bufferLock;
45 #define BUFFER_PAGE_SIZE 2048
48 /* page hash table size */
50 /* The hash table should be somewhat efficient even if there are only
51 * a few partitions (less than 32). So the hash for the fileserver is now
52 * based on the volume id. This means this macro is dependent upon the
53 * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
55 #define pHash(fid) ((fid)[0] & (PHSIZE-1))
56 #define vHash(vid) (vid & (PHSIZE-1))
58 /* admittedly system dependent, this is the maximum signed 32-bit value */
59 #define BUFFER_LONG_MAX 2147483647
64 static struct buffer **Buffers;
68 static struct buffer *phTable[PHSIZE]; /* page hash table */
69 static struct buffer *LastBuffer;
72 static int calls = 0, ios = 0;
74 struct buffer *newslot(afs_int32 *afid, afs_int32 apage,
77 /* XXX - This sucks. The correct prototypes for these functions are ...
79 * extern void FidZero(DirHandle *);
80 * extern int FidEq(DirHandle *a, DirHandle *b);
81 * extern int ReallyRead(DirHandle *a, int block, char *data);
84 extern void FidZero(afs_int32 *file);
85 extern int FidEq(afs_int32 *a, afs_int32 *b);
86 extern int ReallyRead(afs_int32 *file, int block, char *data);
87 extern int ReallyWrite(afs_int32 *file, int block, char *data);
88 extern void FidZap(afs_int32 *file);
89 extern int FidVolEq(afs_int32 *file, afs_int32 vid);
90 extern void FidCpy(afs_int32 *tofile, afs_int32 *fromfile);
91 extern void Die(char *msg);
94 DStat(int *abuffers, int *acalls, int *aios)
105 /* Initialize the venus buffer system. */
106 register int i, tsize;
107 register struct buffer *tb;
110 Lock_Init(&afs_bufferLock);
111 /* Align each element of Buffers on a doubleword boundary */
112 tsize = (sizeof(struct buffer) + 7) & ~7;
113 tp = (char *)malloc(abuffers * tsize);
114 Buffers = (struct buffer **)malloc(abuffers * sizeof(struct buffer *));
115 BufferData = (char *)malloc(abuffers * BUFFER_PAGE_SIZE);
117 LastBuffer = (struct buffer *)tp;
119 for (i = 0; i < PHSIZE; i++)
121 for (i = 0; i < abuffers; i++) {
122 /* Fill in each buffer with an empty indication. */
123 tb = (struct buffer *)tp;
127 tb->accesstime = tb->lockers = 0;
128 tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
131 Lock_Init(&tb->lock);
137 DRead(register afs_int32 *fid, register int page)
139 /* Read a page from the disk. */
140 register struct buffer *tb, *tb2, **bufhead;
142 ObtainWriteLock(&afs_bufferLock);
145 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
146 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
148 /* this apparently-complicated-looking code is simply an example of
149 * a little bit of loop unrolling, and is a standard linked-list
150 * traversal trick. It saves a few assignments at the the expense
151 * of larger code size. This could be simplified by better use of
152 * macros. With the use of these LRU queues, the old one-cache is
155 if ((tb = phTable[pHash(fid)])) { /* ASSMT HERE */
157 ObtainWriteLock(&tb->lock);
159 ReleaseWriteLock(&afs_bufferLock);
160 tb->accesstime = ++timecounter;
161 ReleaseWriteLock(&tb->lock);
164 bufhead = &(phTable[pHash(fid)]);
165 while ((tb2 = tb->hashNext)) {
167 buf_Front(bufhead, tb, tb2);
168 ObtainWriteLock(&tb2->lock);
170 ReleaseWriteLock(&afs_bufferLock);
171 tb2->accesstime = ++timecounter;
172 ReleaseWriteLock(&tb2->lock);
175 if ((tb = tb2->hashNext)) { /* ASSIGNMENT HERE! */
177 buf_Front(bufhead, tb2, tb);
178 ObtainWriteLock(&tb->lock);
180 ReleaseWriteLock(&afs_bufferLock);
181 tb->accesstime = ++timecounter;
182 ReleaseWriteLock(&tb->lock);
193 /* The last thing we looked at was either tb or tb2 (or nothing). That
194 * is at least the oldest buffer on one particular hash chain, so it's
195 * a pretty good place to start looking for the truly oldest buffer.
197 tb = newslot(fid, page, (tb ? tb : tb2));
199 ObtainWriteLock(&tb->lock);
201 ReleaseWriteLock(&afs_bufferLock);
202 if (ReallyRead(tb->fid, tb->page, tb->data)) {
204 FidZap(tb->fid); /* disaster */
205 ReleaseWriteLock(&tb->lock);
208 /* Note that findslot sets the page field in the buffer equal to
209 * what it is searching for.
211 ReleaseWriteLock(&tb->lock);
216 FixupBucket(register struct buffer *ap)
218 register struct buffer **lp, *tp;
221 /* first try to get it out of its current hash bucket, in which it might not be */
224 for (tp = *lp; tp; tp = tp->hashNext) {
231 /* now figure the new hash bucket */
233 ap->hashIndex = i; /* remember where we are for deletion */
234 ap->hashNext = phTable[i]; /* add us to the list */
235 phTable[i] = ap; /* at the front, since it's LRU */
240 newslot(afs_int32 *afid, afs_int32 apage, register struct buffer *lp)
242 /* Find a usable buffer slot */
243 register afs_int32 i;
245 register struct buffer **tbp;
247 if (lp && (lp->lockers == 0)) {
251 lt = BUFFER_LONG_MAX;
255 for (i = 0; i < nbuffers; i++, tbp++) {
256 if ((*tbp)->lockers == 0) {
257 if ((*tbp)->accesstime < lt) {
259 lt = (*tbp)->accesstime;
264 /* There are no unlocked buffers */
267 Die("accesstime counter wrapped");
269 Die("all buffers locked");
272 /* We do not need to lock the buffer here because it has no lockers
273 * and the afs_bufferLock prevents other threads from zapping this
274 * buffer while we are writing it out */
276 if (ReallyWrite(lp->fid, lp->page, lp->data))
277 Die("writing bogus buffer");
281 /* Now fill in the header. */
283 FidCpy(lp->fid, afid); /* set this */
285 lp->accesstime = ++timecounter;
287 FixupBucket(lp); /* move to the right hash bucket */
293 DRelease(register struct buffer *bp, int flag)
295 /* Release a buffer, specifying whether or not the buffer has been modified by the locker. */
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);