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>
20 struct Lock afs_bufferLock;
23 #define BUFFER_PAGE_SIZE 2048
26 /* page hash table size */
28 /* The hash table should be somewhat efficient even if there are only
29 * a few partitions (less than 32). So the hash for the fileserver is now
30 * based on the volume id. This means this macro is dependent upon the
31 * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
33 #define pHash(fid) ((fid)[0] & (PHSIZE-1))
34 #define vHash(vid) (vid & (PHSIZE-1))
36 /* admittedly system dependent, this is the maximum signed 32-bit value */
37 #define BUFFER_LONG_MAX 2147483647
42 #ifdef AFS_64BIT_IOPS_ENV
43 #define BUFFER_FID_SIZE (9 + 2*sizeof(char*)/sizeof(int))
45 #define BUFFER_FID_SIZE (6 + 2*sizeof(char*)/sizeof(int))
49 /* fid is used for Unique cache key + i/o addressing.
50 * fid size is based on 4 + size of inode and size of pointer
52 afs_int32 fid[BUFFER_FID_SIZE];
55 struct buffer *hashNext;
65 static struct buffer *phTable[PHSIZE]; /* page hash table */
66 static struct buffer *LastBuffer;
69 static int calls=0, ios=0;
71 struct buffer *newslot();
74 DStat (abuffers, acalls, aios)
75 int *abuffers, *acalls, *aios;
87 /* Initialize the venus buffer system. */
88 register int i, tsize;
89 register struct buffer *tb;
92 Lock_Init(&afs_bufferLock);
93 /* Align each element of Buffers on a doubleword boundary */
94 tsize = (sizeof(struct buffer) + 7) & ~7;
95 tp = (char *) malloc(abuffers * tsize);
96 Buffers = (struct buffer **) malloc(abuffers * sizeof(struct buffer *));
97 BufferData = (char *) malloc(abuffers * BUFFER_PAGE_SIZE);
99 LastBuffer = (struct buffer *)tp;
101 for (i=0;i<PHSIZE;i++)
103 for (i=0;i<abuffers;i++) {
104 /* Fill in each buffer with an empty indication. */
105 tb = (struct buffer *)tp;
109 tb->accesstime = tb->lockers = 0;
110 tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
113 Lock_Init(&tb->lock);
120 register afs_int32 *fid;
123 /* Read a page from the disk. */
124 register struct buffer *tb, *tb2, **bufhead;
126 ObtainWriteLock(&afs_bufferLock);
129 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
130 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
132 /* this apparently-complicated-looking code is simply an example of
133 * a little bit of loop unrolling, and is a standard linked-list
134 * traversal trick. It saves a few assignments at the the expense
135 * of larger code size. This could be simplified by better use of
136 * macros. With the use of these LRU queues, the old one-cache is
139 if ( tb = phTable[pHash(fid)] ) { /* ASSMT HERE */
141 ObtainWriteLock(&tb->lock);
143 ReleaseWriteLock(&afs_bufferLock);
144 tb->accesstime = ++timecounter;
145 ReleaseWriteLock(&tb->lock);
149 bufhead = &( phTable[pHash(fid)] );
150 while (tb2 = tb->hashNext) {
152 buf_Front(bufhead,tb,tb2);
153 ObtainWriteLock(&tb2->lock);
155 ReleaseWriteLock(&afs_bufferLock);
156 tb2->accesstime = ++timecounter;
157 ReleaseWriteLock(&tb2->lock);
160 if (tb = tb2->hashNext) { /* ASSIGNMENT HERE! */
162 buf_Front(bufhead,tb2,tb);
163 ObtainWriteLock(&tb->lock);
165 ReleaseWriteLock(&afs_bufferLock);
166 tb->accesstime = ++timecounter;
167 ReleaseWriteLock(&tb->lock);
178 /* The last thing we looked at was either tb or tb2 (or nothing). That
179 * is at least the oldest buffer on one particular hash chain, so it's
180 * a pretty good place to start looking for the truly oldest buffer.
182 tb = newslot(fid, page, (tb ? tb : tb2));
184 ObtainWriteLock(&tb->lock);
186 ReleaseWriteLock(&afs_bufferLock);
187 if (ReallyRead(tb->fid,tb->page,tb->data)) {
189 FidZap(tb->fid); /* disaster */
190 ReleaseWriteLock(&tb->lock);
193 /* Note that findslot sets the page field in the buffer equal to
194 * what it is searching for.
196 ReleaseWriteLock(&tb->lock);
202 register struct buffer *ap;
204 register struct buffer **lp, *tp;
207 /* first try to get it out of its current hash bucket, in which it might not be */
210 for (tp = *lp; tp; tp = tp->hashNext) {
217 /* now figure the new hash bucket */
219 ap->hashIndex = i; /* remember where we are for deletion */
220 ap->hashNext = phTable[i]; /* add us to the list */
221 phTable[i] = ap; /* at the front, since it's LRU */
225 newslot (afid, apage, lp)
226 afs_int32 *afid, apage;
227 register struct buffer *lp; /* pointer to a fairly-old buffer */
229 /* Find a usable buffer slot */
230 register afs_int32 i;
232 register struct buffer **tbp;
234 if (lp && (lp->lockers == 0)) {
239 lt = BUFFER_LONG_MAX;
243 for (i=0;i<nbuffers;i++,tbp++) {
244 if ((*tbp)->lockers == 0) {
245 if ((*tbp)->accesstime < lt) {
247 lt = (*tbp)->accesstime;
252 /* There are no unlocked buffers */
255 Die("accesstime counter wrapped");
257 Die ("all buffers locked");
260 /* We do not need to lock the buffer here because it has no lockers
261 * and the afs_bufferLock prevents other threads from zapping this
262 * buffer while we are writing it out */
264 if (ReallyWrite(lp->fid,lp->page,lp->data)) Die("writing bogus buffer");
268 /* Now fill in the header. */
270 FidCpy(lp->fid, afid); /* set this */
272 lp->accesstime = ++timecounter;
274 FixupBucket(lp); /* move to the right hash bucket */
281 register struct buffer *bp;
284 /* Release a buffer, specifying whether or not the buffer has been modified by the locker. */
288 index = ((char *) bp - BufferData) >> LOGPS;
290 ObtainWriteLock(&bp->lock);
292 if (flag) bp->dirty=1;
293 ReleaseWriteLock(&bp->lock);
300 /* Return the byte within a file represented by a buffer pointer. */
301 register struct buffer *bp = ap;
304 index = ((char *) bp - BufferData) >> LOGPS;
305 if (index < 0 || index >= nbuffers)
308 return BUFFER_PAGE_SIZE * bp->page + (char *) ap - (char *) bp->data;
313 register afs_int32 *fid;
315 /* Destroy all buffers pertaining to a particular fid. */
316 register struct buffer *tb;
317 ObtainReadLock(&afs_bufferLock);
318 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
319 if (FidEq(tb->fid,fid)) {
320 ObtainWriteLock(&tb->lock);
323 ReleaseWriteLock(&tb->lock);
325 ReleaseReadLock(&afs_bufferLock);
329 register afs_int32 vid;
331 /* Flush all data and release all inode handles for a particular volume */
332 register struct buffer *tb;
333 register int code, rcode = 0;
334 ObtainReadLock(&afs_bufferLock);
335 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
336 if (FidVolEq(tb->fid,vid)) {
337 ObtainWriteLock(&tb->lock);
339 code = ReallyWrite(tb->fid, tb->page, tb->data);
345 ReleaseWriteLock(&tb->lock);
347 ReleaseReadLock(&afs_bufferLock);
353 register afs_int32 *fid;
355 /* Flush pages modified by one entry. */
356 register struct buffer *tb;
359 ObtainReadLock(&afs_bufferLock);
360 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
361 if (FidEq(tb->fid, fid) && tb->dirty) {
362 ObtainWriteLock(&tb->lock);
364 code = ReallyWrite(tb->fid, tb->page, tb->data);
366 ReleaseWriteLock(&tb->lock);
367 ReleaseReadLock(&afs_bufferLock);
372 ReleaseWriteLock(&tb->lock);
374 ReleaseReadLock(&afs_bufferLock);
381 /* Flush all the modified buffers. */
383 register struct buffer **tbp;
384 afs_int32 code, rcode;
388 ObtainReadLock(&afs_bufferLock);
389 for (i = 0; i < nbuffers; i++, tbp++) {
391 ObtainWriteLock(&(*tbp)->lock);
393 ReleaseReadLock(&afs_bufferLock);
395 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
397 (*tbp)->dirty = 0; /* Clear the dirty flag */
398 if (code && !rcode) {
403 ReleaseWriteLock(&(*tbp)->lock);
404 ObtainReadLock(&afs_bufferLock);
407 ReleaseReadLock(&afs_bufferLock);
414 register afs_int32 *fid;
416 /* Same as read, only do *not* even try to read the page,
417 * since it probably doesn't exist.
419 register struct buffer *tb;
420 ObtainWriteLock(&afs_bufferLock);
421 if ((tb = newslot(fid,page,0)) == 0) {
422 ReleaseWriteLock(&afs_bufferLock);
425 ObtainWriteLock(&tb->lock);
427 ReleaseWriteLock(&afs_bufferLock);
428 ReleaseWriteLock(&tb->lock);