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>
21 struct Lock afs_bufferLock;
24 #define BUFFER_PAGE_SIZE 2048
27 /* page hash table size */
29 /* The hash table should be somewhat efficient even if there are only
30 * a few partitions (less than 32). So the hash for the fileserver is now
31 * based on the volume id. This means this macro is dependent upon the
32 * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
34 #define pHash(fid) ((fid)[0] & (PHSIZE-1))
35 #define vHash(vid) (vid & (PHSIZE-1))
37 /* admittedly system dependent, this is the maximum signed 32-bit value */
38 #define BUFFER_LONG_MAX 2147483647
43 #ifdef AFS_64BIT_IOPS_ENV
44 #define BUFFER_FID_SIZE (9 + 2*sizeof(char*)/sizeof(int))
46 #define BUFFER_FID_SIZE (6 + 2*sizeof(char*)/sizeof(int))
49 static struct buffer {
50 /* fid is used for Unique cache key + i/o addressing.
51 * fid size is based on 4 + size of inode and size of pointer
53 afs_int32 fid[BUFFER_FID_SIZE];
56 struct buffer *hashNext;
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();
75 DStat(abuffers, acalls, aios)
76 int *abuffers, *acalls, *aios;
88 /* Initialize the venus buffer system. */
89 register int i, tsize;
90 register struct buffer *tb;
93 Lock_Init(&afs_bufferLock);
94 /* Align each element of Buffers on a doubleword boundary */
95 tsize = (sizeof(struct buffer) + 7) & ~7;
96 tp = (char *)malloc(abuffers * tsize);
97 Buffers = (struct buffer **)malloc(abuffers * sizeof(struct buffer *));
98 BufferData = (char *)malloc(abuffers * BUFFER_PAGE_SIZE);
100 LastBuffer = (struct buffer *)tp;
102 for (i = 0; i < PHSIZE; i++)
104 for (i = 0; i < abuffers; i++) {
105 /* Fill in each buffer with an empty indication. */
106 tb = (struct buffer *)tp;
110 tb->accesstime = tb->lockers = 0;
111 tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
114 Lock_Init(&tb->lock);
121 register afs_int32 *fid;
124 /* Read a page from the disk. */
125 register struct buffer *tb, *tb2, **bufhead;
127 ObtainWriteLock(&afs_bufferLock);
130 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
131 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
133 /* this apparently-complicated-looking code is simply an example of
134 * a little bit of loop unrolling, and is a standard linked-list
135 * traversal trick. It saves a few assignments at the the expense
136 * of larger code size. This could be simplified by better use of
137 * macros. With the use of these LRU queues, the old one-cache is
140 if (tb = phTable[pHash(fid)]) { /* ASSMT HERE */
142 ObtainWriteLock(&tb->lock);
144 ReleaseWriteLock(&afs_bufferLock);
145 tb->accesstime = ++timecounter;
146 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)) {
238 lt = BUFFER_LONG_MAX;
242 for (i = 0; i < nbuffers; i++, tbp++) {
243 if ((*tbp)->lockers == 0) {
244 if ((*tbp)->accesstime < lt) {
246 lt = (*tbp)->accesstime;
251 /* There are no unlocked buffers */
254 Die("accesstime counter wrapped");
256 Die("all buffers locked");
259 /* We do not need to lock the buffer here because it has no lockers
260 * and the afs_bufferLock prevents other threads from zapping this
261 * buffer while we are writing it out */
263 if (ReallyWrite(lp->fid, lp->page, lp->data))
264 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. */
289 index = ((char *)bp - BufferData) >> LOGPS;
291 ObtainWriteLock(&bp->lock);
295 ReleaseWriteLock(&bp->lock);
302 /* Return the byte within a file represented by a buffer pointer. */
303 register struct buffer *bp = ap;
306 index = ((char *)bp - BufferData) >> LOGPS;
307 if (index < 0 || index >= nbuffers)
310 return BUFFER_PAGE_SIZE * bp->page + (char *)ap - (char *)bp->data;
315 register afs_int32 *fid;
317 /* Destroy all buffers pertaining to a particular fid. */
318 register struct buffer *tb;
319 ObtainReadLock(&afs_bufferLock);
320 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
321 if (FidEq(tb->fid, fid)) {
322 ObtainWriteLock(&tb->lock);
325 ReleaseWriteLock(&tb->lock);
327 ReleaseReadLock(&afs_bufferLock);
332 register afs_int32 vid;
334 /* Flush all data and release all inode handles for a particular volume */
335 register struct buffer *tb;
336 register int code, rcode = 0;
337 ObtainReadLock(&afs_bufferLock);
338 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
339 if (FidVolEq(tb->fid, vid)) {
340 ObtainWriteLock(&tb->lock);
342 code = ReallyWrite(tb->fid, tb->page, tb->data);
348 ReleaseWriteLock(&tb->lock);
350 ReleaseReadLock(&afs_bufferLock);
356 register afs_int32 *fid;
358 /* Flush pages modified by one entry. */
359 register struct buffer *tb;
362 ObtainReadLock(&afs_bufferLock);
363 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
364 if (FidEq(tb->fid, fid) && tb->dirty) {
365 ObtainWriteLock(&tb->lock);
367 code = ReallyWrite(tb->fid, tb->page, tb->data);
369 ReleaseWriteLock(&tb->lock);
370 ReleaseReadLock(&afs_bufferLock);
375 ReleaseWriteLock(&tb->lock);
377 ReleaseReadLock(&afs_bufferLock);
384 /* Flush all the modified buffers. */
386 register struct buffer **tbp;
387 afs_int32 code, rcode;
391 ObtainReadLock(&afs_bufferLock);
392 for (i = 0; i < nbuffers; i++, tbp++) {
394 ObtainWriteLock(&(*tbp)->lock);
396 ReleaseReadLock(&afs_bufferLock);
398 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
400 (*tbp)->dirty = 0; /* Clear the dirty flag */
401 if (code && !rcode) {
406 ReleaseWriteLock(&(*tbp)->lock);
407 ObtainReadLock(&afs_bufferLock);
410 ReleaseReadLock(&afs_bufferLock);
417 register afs_int32 *fid;
419 /* Same as read, only do *not* even try to read the page,
420 * since it probably doesn't exist.
422 register struct buffer *tb;
423 ObtainWriteLock(&afs_bufferLock);
424 if ((tb = newslot(fid, page, 0)) == 0) {
425 ReleaseWriteLock(&afs_bufferLock);
428 ObtainWriteLock(&tb->lock);
430 ReleaseWriteLock(&afs_bufferLock);
431 ReleaseWriteLock(&tb->lock);