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();
77 DStat(int *abuffers, int *acalls, int *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);
120 DRead(register afs_int32 *fid, register int page)
122 /* Read a page from the disk. */
123 register struct buffer *tb, *tb2, **bufhead;
125 ObtainWriteLock(&afs_bufferLock);
128 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
129 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
131 /* this apparently-complicated-looking code is simply an example of
132 * a little bit of loop unrolling, and is a standard linked-list
133 * traversal trick. It saves a few assignments at the the expense
134 * of larger code size. This could be simplified by better use of
135 * macros. With the use of these LRU queues, the old one-cache is
138 if (tb = phTable[pHash(fid)]) { /* ASSMT HERE */
140 ObtainWriteLock(&tb->lock);
142 ReleaseWriteLock(&afs_bufferLock);
143 tb->accesstime = ++timecounter;
144 ReleaseWriteLock(&tb->lock);
147 bufhead = &(phTable[pHash(fid)]);
148 while (tb2 = tb->hashNext) {
150 buf_Front(bufhead, tb, tb2);
151 ObtainWriteLock(&tb2->lock);
153 ReleaseWriteLock(&afs_bufferLock);
154 tb2->accesstime = ++timecounter;
155 ReleaseWriteLock(&tb2->lock);
158 if (tb = tb2->hashNext) { /* ASSIGNMENT HERE! */
160 buf_Front(bufhead, tb2, tb);
161 ObtainWriteLock(&tb->lock);
163 ReleaseWriteLock(&afs_bufferLock);
164 tb->accesstime = ++timecounter;
165 ReleaseWriteLock(&tb->lock);
176 /* The last thing we looked at was either tb or tb2 (or nothing). That
177 * is at least the oldest buffer on one particular hash chain, so it's
178 * a pretty good place to start looking for the truly oldest buffer.
180 tb = newslot(fid, page, (tb ? tb : tb2));
182 ObtainWriteLock(&tb->lock);
184 ReleaseWriteLock(&afs_bufferLock);
185 if (ReallyRead(tb->fid, tb->page, tb->data)) {
187 FidZap(tb->fid); /* disaster */
188 ReleaseWriteLock(&tb->lock);
191 /* Note that findslot sets the page field in the buffer equal to
192 * what it is searching for.
194 ReleaseWriteLock(&tb->lock);
199 FixupBucket(register struct buffer *ap)
201 register struct buffer **lp, *tp;
204 /* first try to get it out of its current hash bucket, in which it might not be */
207 for (tp = *lp; tp; tp = tp->hashNext) {
214 /* now figure the new hash bucket */
216 ap->hashIndex = i; /* remember where we are for deletion */
217 ap->hashNext = phTable[i]; /* add us to the list */
218 phTable[i] = ap; /* at the front, since it's LRU */
223 newslot(afs_int32 *afid, afs_int32 apage, register struct buffer *lp)
225 /* Find a usable buffer slot */
226 register afs_int32 i;
228 register struct buffer **tbp;
230 if (lp && (lp->lockers == 0)) {
234 lt = BUFFER_LONG_MAX;
238 for (i = 0; i < nbuffers; i++, tbp++) {
239 if ((*tbp)->lockers == 0) {
240 if ((*tbp)->accesstime < lt) {
242 lt = (*tbp)->accesstime;
247 /* There are no unlocked buffers */
250 Die("accesstime counter wrapped");
252 Die("all buffers locked");
255 /* We do not need to lock the buffer here because it has no lockers
256 * and the afs_bufferLock prevents other threads from zapping this
257 * buffer while we are writing it out */
259 if (ReallyWrite(lp->fid, lp->page, lp->data))
260 Die("writing bogus buffer");
264 /* Now fill in the header. */
266 FidCpy(lp->fid, afid); /* set this */
268 lp->accesstime = ++timecounter;
270 FixupBucket(lp); /* move to the right hash bucket */
276 DRelease(register struct buffer *bp, int flag)
278 /* Release a buffer, specifying whether or not the buffer has been modified by the locker. */
283 index = ((char *)bp - BufferData) >> LOGPS;
285 ObtainWriteLock(&bp->lock);
289 ReleaseWriteLock(&bp->lock);
293 DVOffset(register void *ap)
295 /* Return the byte within a file represented by a buffer pointer. */
296 register struct buffer *bp = ap;
299 index = ((char *)bp - BufferData) >> LOGPS;
300 if (index < 0 || index >= nbuffers)
303 return BUFFER_PAGE_SIZE * bp->page + (char *)ap - (char *)bp->data;
307 DZap(register afs_int32 *fid)
309 /* Destroy all buffers pertaining to a particular fid. */
310 register struct buffer *tb;
311 ObtainReadLock(&afs_bufferLock);
312 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
313 if (FidEq(tb->fid, fid)) {
314 ObtainWriteLock(&tb->lock);
317 ReleaseWriteLock(&tb->lock);
319 ReleaseReadLock(&afs_bufferLock);
323 DFlushVolume(register afs_int32 vid)
325 /* Flush all data and release all inode handles for a particular volume */
326 register struct buffer *tb;
327 register int code, rcode = 0;
328 ObtainReadLock(&afs_bufferLock);
329 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
330 if (FidVolEq(tb->fid, vid)) {
331 ObtainWriteLock(&tb->lock);
333 code = ReallyWrite(tb->fid, tb->page, tb->data);
339 ReleaseWriteLock(&tb->lock);
341 ReleaseReadLock(&afs_bufferLock);
346 DFlushEntry(register afs_int32 *fid)
348 /* Flush pages modified by one entry. */
349 register struct buffer *tb;
352 ObtainReadLock(&afs_bufferLock);
353 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
354 if (FidEq(tb->fid, fid) && tb->dirty) {
355 ObtainWriteLock(&tb->lock);
357 code = ReallyWrite(tb->fid, tb->page, tb->data);
359 ReleaseWriteLock(&tb->lock);
360 ReleaseReadLock(&afs_bufferLock);
365 ReleaseWriteLock(&tb->lock);
367 ReleaseReadLock(&afs_bufferLock);
374 /* Flush all the modified buffers. */
376 register struct buffer **tbp;
377 afs_int32 code, rcode;
381 ObtainReadLock(&afs_bufferLock);
382 for (i = 0; i < nbuffers; i++, tbp++) {
384 ObtainWriteLock(&(*tbp)->lock);
386 ReleaseReadLock(&afs_bufferLock);
388 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
390 (*tbp)->dirty = 0; /* Clear the dirty flag */
391 if (code && !rcode) {
396 ReleaseWriteLock(&(*tbp)->lock);
397 ObtainReadLock(&afs_bufferLock);
400 ReleaseReadLock(&afs_bufferLock);
405 DNew(register afs_int32 *fid, register int page)
407 /* Same as read, only do *not* even try to read the page,
408 * since it probably doesn't exist.
410 register struct buffer *tb;
411 ObtainWriteLock(&afs_bufferLock);
412 if ((tb = newslot(fid, page, 0)) == 0) {
413 ReleaseWriteLock(&afs_bufferLock);
416 ObtainWriteLock(&tb->lock);
418 ReleaseWriteLock(&afs_bufferLock);
419 ReleaseWriteLock(&tb->lock);