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"
16 #include "afs/sysincludes.h"
17 #include "afsincludes.h"
22 #if defined(AFS_AIX31_ENV) || defined(AFS_DEC_ENV)
25 #if !defined(AFS_AIX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
26 #include "h/kernel.h" /* Doesn't needed, so it should go */
28 #endif /* !defined(UKERNEL) */
30 #include "afs/afs_osi.h"
34 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
36 #endif /* !defined(UKERNEL) */
39 #include "afs/volerrors.h"
40 #include "afs/exporter.h"
41 #include "afs/prs_fs.h"
42 #include "afs/afs_chunkops.h"
45 #include "afs/afs_stats.h"
46 #include "afs/longc_procs.h"
50 #define BUF_TIME_MAX 0x7fffffff
52 /* number of pages per Unix buffer, when we're using Unix buffer pool */
55 #define AFS_BUFFER_PAGESIZE 2048
58 /* If you change any of this PH stuff, make sure you don't break DZap() */
59 /* use last two bits for page */
61 /* use next five bits for fid */
63 /* page hash table size - this is pretty intertwined with pHash */
64 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
66 #define pHash(fid,page) ((((afs_int32)((fid)->inode)) & PHFIDMASK) \
67 | (page & PHPAGEMASK))
70 #undef dirty /* XXX */
73 static struct buffer *Buffers = 0;
74 static char *BufferData;
77 extern struct buf *geteblk();
80 #define timecounter afs_timecounter
82 /* The locks for individual buffer entries are now sometimes obtained while holding the
83 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
85 static afs_lock_t afs_bufferLock;
86 static struct buffer *phTable[PHSIZE]; /* page hash table */
88 static afs_int32 timecounter;
90 /* Prototypes for static routines */
91 static struct buffer *afs_newslot(struct fcache * afid, afs_int32 apage,
92 register struct buffer *lp);
94 static int dinit_flag = 0;
98 /* Initialize the venus buffer system. */
100 register struct buffer *tb;
102 struct buf *tub; /* unix buffer for allocation */
110 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
111 abuffers = ((abuffers - 1) | (NPB - 1)) + 1;
113 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
115 (struct buffer *)afs_osi_Alloc(abuffers * sizeof(struct buffer));
117 BufferData = (char *)afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
120 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
121 for (i = 0; i < PHSIZE; i++)
123 for (i = 0; i < abuffers; i++) {
125 if ((i & (NPB - 1)) == 0) {
126 /* time to allocate a fresh buffer */
127 tub = geteblk(AFS_BUFFER_PAGESIZE * NPB);
128 BufferData = (char *)tub->b_un.b_addr;
131 /* Fill in each buffer with an empty indication. */
137 if ((i & (NPB - 1)) == 0)
141 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i & (NPB - 1))];
143 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * i];
147 RWLOCK_INIT(&tb->lock, "buffer lock");
153 DRead(register struct fcache * fid, register int page)
155 /* Read a page from the disk. */
156 register struct buffer *tb, *tb2;
157 struct osi_file *tfile;
161 MObtainWriteLock(&afs_bufferLock, 256);
163 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
164 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
166 /* this apparently-complicated-looking code is simply an example of
167 * a little bit of loop unrolling, and is a standard linked-list
168 * traversal trick. It saves a few assignments at the the expense
169 * of larger code size. This could be simplified by better use of
172 if ((tb = phTable[pHash(fid, page)])) {
174 MObtainWriteLock(&tb->lock, 257);
175 ReleaseWriteLock(&afs_bufferLock);
177 tb->accesstime = timecounter++;
178 AFS_STATS(afs_stats_cmperf.bufHits++);
179 MReleaseWriteLock(&tb->lock);
182 register struct buffer **bufhead;
183 bufhead = &(phTable[pHash(fid, page)]);
184 while ((tb2 = tb->hashNext)) {
186 buf_Front(bufhead, tb, tb2);
187 MObtainWriteLock(&tb2->lock, 258);
188 ReleaseWriteLock(&afs_bufferLock);
190 tb2->accesstime = timecounter++;
191 AFS_STATS(afs_stats_cmperf.bufHits++);
192 MReleaseWriteLock(&tb2->lock);
195 if ((tb = tb2->hashNext)) {
197 buf_Front(bufhead, tb2, tb);
198 MObtainWriteLock(&tb->lock, 259);
199 ReleaseWriteLock(&afs_bufferLock);
201 tb->accesstime = timecounter++;
202 AFS_STATS(afs_stats_cmperf.bufHits++);
203 MReleaseWriteLock(&tb->lock);
213 AFS_STATS(afs_stats_cmperf.bufMisses++);
215 /* The last thing we looked at was either tb or tb2 (or nothing). That
216 * is at least the oldest buffer on one particular hash chain, so it's
217 * a pretty good place to start looking for the truly oldest buffer.
219 tb = afs_newslot(fid, page, (tb ? tb : tb2));
221 MReleaseWriteLock(&afs_bufferLock);
224 MObtainWriteLock(&tb->lock, 260);
225 MReleaseWriteLock(&afs_bufferLock);
227 if (page * AFS_BUFFER_PAGESIZE >= fid->chunkBytes) {
230 MReleaseWriteLock(&tb->lock);
233 tfile = afs_CFileOpen(fid->inode);
235 afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE, tb->data,
236 AFS_BUFFER_PAGESIZE);
237 afs_CFileClose(tfile);
238 if (code < AFS_BUFFER_PAGESIZE) {
241 MReleaseWriteLock(&tb->lock);
244 /* Note that findslot sets the page field in the buffer equal to
245 * what it is searching for. */
246 MReleaseWriteLock(&tb->lock);
251 FixupBucket(register struct buffer *ap)
253 register struct buffer **lp, *tp;
255 /* first try to get it out of its current hash bucket, in which it
257 AFS_STATCNT(FixupBucket);
260 for (tp = *lp; tp; tp = tp->hashNext) {
267 /* now figure the new hash bucket */
268 i = pHash(ap->fid, ap->page);
269 ap->hashIndex = i; /* remember where we are for deletion */
270 ap->hashNext = phTable[i]; /* add us to the list */
271 phTable[i] = ap; /* at the front, since it's LRU */
274 /* lp is pointer to a fairly-old buffer */
275 static struct buffer *
276 afs_newslot(struct fcache * afid, afs_int32 apage, register struct buffer *lp)
278 /* Find a usable buffer slot */
279 register afs_int32 i;
281 register struct buffer *tp;
282 struct osi_file *tfile;
284 AFS_STATCNT(afs_newslot);
285 /* we take a pointer here to a buffer which was at the end of an
286 * LRU hash chain. Odds are, it's one of the older buffers, not
287 * one of the newer. Having an older buffer to start with may
288 * permit us to avoid a few of the assignments in the "typical
289 * case" for loop below.
291 if (lp && (lp->lockers == 0)) {
298 /* timecounter might have wrapped, if machine is very very busy
299 * and stays up for a long time. Timecounter mustn't wrap twice
300 * (positive->negative->positive) before calling newslot, but that
301 * would require 2 billion consecutive cache hits... Anyway, the
302 * penalty is only that the cache replacement policy will be
303 * almost MRU for the next ~2 billion DReads... newslot doesn't
304 * get called nearly as often as DRead, so in order to avoid the
305 * performance penalty of using the hypers, it's worth doing the
306 * extra check here every time. It's probably cheaper than doing
307 * hcmp, anyway. There is a little performance hit resulting from
308 * resetting all the access times to 0, but it only happens once
309 * every month or so, and the access times will rapidly sort
310 * themselves back out after just a few more DReads.
312 if (timecounter < 0) {
315 for (i = 0; i < nbuffers; i++, tp++) {
317 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
321 /* this is the typical case */
323 for (i = 0; i < nbuffers; i++, tp++) {
324 if (tp->lockers == 0) {
325 if (tp->accesstime < lt) {
334 /* There are no unlocked buffers -- this used to panic, but that
335 * seems extreme. To the best of my knowledge, all the callers
336 * of DRead are prepared to handle a zero return. Some of them
337 * just panic directly, but not all of them. */
338 afs_warn("all buffers locked");
343 tfile = afs_CFileOpen(lp->fid->inode);
344 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE, lp->data,
345 AFS_BUFFER_PAGESIZE);
347 afs_CFileClose(tfile);
348 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
351 /* Now fill in the header. */
352 dirp_Cpy(lp->fid, afid); /* set this */
354 lp->accesstime = timecounter++;
355 FixupBucket(lp); /* move to the right hash bucket */
361 DRelease(register struct buffer *bp, int flag)
363 /* Release a buffer, specifying whether or not the buffer has been
364 * modified by the locker. */
367 register struct buffer *tp;
370 AFS_STATCNT(DRelease);
374 /* look for buffer by scanning Unix buffers for appropriate address */
376 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
377 if ((afs_int32) bp >= (afs_int32) tp->data
379 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
380 /* we found the right range */
381 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
386 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
388 bp = &(Buffers[index]);
389 MObtainWriteLock(&bp->lock, 261);
393 MReleaseWriteLock(&bp->lock);
397 DVOffset(register void *ap)
399 /* Return the byte within a file represented by a buffer pointer. */
400 register struct buffer *bp;
403 register struct buffer *tp;
405 AFS_STATCNT(DVOffset);
408 /* look for buffer by scanning Unix buffers for appropriate address */
410 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
411 if ((afs_int32) bp >= (afs_int32) tp->data
413 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
414 /* we found the right range */
415 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
420 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
422 if (index < 0 || index >= nbuffers)
424 bp = &(Buffers[index]);
425 return AFS_BUFFER_PAGESIZE * bp->page + (int)(((char *)ap) - bp->data);
428 /* 1/1/91 - I've modified the hash function to take the page as well
429 * as the *fid, so that lookup will be a bit faster. That presents some
430 * difficulties for Zap, which now has to have some knowledge of the nature
431 * of the hash function. Oh well. This should use the list traversal
435 DZap(struct fcache * fid)
438 /* Destroy all buffers pertaining to a particular fid. */
439 register struct buffer *tb;
442 MObtainReadLock(&afs_bufferLock);
444 for (i = 0; i <= PHPAGEMASK; i++)
445 for (tb = phTable[pHash(fid, i)]; tb; tb = tb->hashNext)
446 if (dirp_Eq(tb->fid, fid)) {
447 MObtainWriteLock(&tb->lock, 262);
450 MReleaseWriteLock(&tb->lock);
452 MReleaseReadLock(&afs_bufferLock);
458 /* Flush all the modified buffers. */
460 register struct buffer *tb;
461 struct osi_file *tfile;
465 MObtainReadLock(&afs_bufferLock);
466 for (i = 0; i < nbuffers; i++, tb++) {
468 MObtainWriteLock(&tb->lock, 263);
470 MReleaseReadLock(&afs_bufferLock);
472 tfile = afs_CFileOpen(tb->fid->inode);
473 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
474 tb->data, AFS_BUFFER_PAGESIZE);
475 tb->dirty = 0; /* Clear the dirty flag */
476 afs_CFileClose(tfile);
479 MReleaseWriteLock(&tb->lock);
480 MObtainReadLock(&afs_bufferLock);
483 MReleaseReadLock(&afs_bufferLock);
487 DNew(register struct fcache * fid, register int page)
489 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
490 register struct buffer *tb;
492 MObtainWriteLock(&afs_bufferLock, 264);
493 if ((tb = afs_newslot(fid, page, NULL)) == 0) {
494 MReleaseWriteLock(&afs_bufferLock);
497 MObtainWriteLock(&tb->lock, 265);
498 MReleaseWriteLock(&afs_bufferLock);
500 MReleaseWriteLock(&tb->lock);
505 shutdown_bufferpackage(void)
508 register struct buffer *tp;
511 extern int afs_cold_shutdown;
513 AFS_STATCNT(shutdown_bufferpackage);
514 /* Free all allocated Buffers and associated buffer pages */
516 if (afs_cold_shutdown) {
519 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
522 for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
523 /* The following check shouldn't be necessary and it will be removed soon */
526 ("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
533 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
536 for (i = 0; i < PHSIZE; i++)
538 memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));