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"
15 #include "../afs/sysincludes.h"
17 #include "../h/param.h"
18 #include "../h/types.h"
19 #include "../h/time.h"
20 #if defined(AFS_AIX31_ENV) || defined(AFS_DEC_ENV)
21 #include "../h/limits.h"
23 #if !defined(AFS_AIX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
24 #include "../h/kernel.h" /* Doesn't needed, so it should go */
26 #endif /* !defined(UKERNEL) */
28 #include "../afs/afs_osi.h"
29 #include "../afsint/afsint.h"
30 #include "../afs/lock.h"
32 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
34 #endif /* !defined(UKERNEL) */
36 #include "../afs/stds.h"
37 #include "../afs/volerrors.h"
38 #include "../afs/exporter.h"
39 #include "../afs/prs_fs.h"
40 #include "../afs/afs_chunkops.h"
41 #include "../afs/dir.h"
43 #include "../afs/afs_stats.h"
44 #include "../afs/longc_procs.h"
47 #define BUF_TIME_MAX 0x7fffffff
49 /* number of pages per Unix buffer, when we're using Unix buffer pool */
52 #define AFS_BUFFER_PAGESIZE 2048
55 /* If you change any of this PH stuff, make sure you don't break DZap() */
56 /* use last two bits for page */
58 /* use next five bits for fid */
60 /* page hash table size - this is pretty intertwined with pHash */
61 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
63 #define pHash(fid,page) ((((afs_int32)((fid)[0])) & PHFIDMASK) \
64 | (page & PHPAGEMASK))
66 /* Note: this should agree with the definition in kdump.c */
67 #if defined(AFS_OSF_ENV)
69 #define AFS_USEBUFFERS 1
74 #undef dirty /* XXX */
78 ino_t fid[1]; /* Unique cache key + i/o addressing */
81 struct buffer *hashNext;
89 afs_rwlock_t lock; /* the lock for this structure */
95 extern struct buf *geteblk();
97 /* The locks for individual buffer entries are now sometimes obtained while holding the
98 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
100 static afs_lock_t afs_bufferLock;
101 static struct buffer *phTable[PHSIZE]; /* page hash table */
103 afs_int32 timecounter;
105 static struct buffer *afs_newslot();
107 static int dinit_flag = 0;
108 void DInit (abuffers)
110 /* Initialize the venus buffer system. */
112 register struct buffer *tb;
114 struct buf *tub; /* unix buffer for allocation */
118 if (dinit_flag) return;
121 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
122 abuffers = ((abuffers-1) | (NPB-1)) + 1;
124 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
125 Buffers = (struct buffer *) afs_osi_Alloc(abuffers * sizeof(struct buffer));
127 BufferData = (char *) afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
130 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
131 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
132 for (i=0;i<abuffers;i++) {
134 if ((i & (NPB-1)) == 0) {
135 /* time to allocate a fresh buffer */
136 tub = geteblk(AFS_BUFFER_PAGESIZE*NPB);
137 BufferData = (char *) tub->b_un.b_addr;
140 /* Fill in each buffer with an empty indication. */
146 if ((i & (NPB-1)) == 0)
150 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i&(NPB-1))];
152 tb->data = &BufferData[AFS_BUFFER_PAGESIZE*i];
156 RWLOCK_INIT(&tb->lock, "buffer lock");
161 char *DRead(fid,page)
164 /* Read a page from the disk. */
165 register struct buffer *tb, *tb2;
167 register afs_int32 code, *sizep;
170 MObtainWriteLock(&afs_bufferLock,256);
172 /* some new code added 1/1/92 */
173 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
174 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
176 /* this apparently-complicated-looking code is simply an example of
177 * a little bit of loop unrolling, and is a standard linked-list
178 * traversal trick. It saves a few assignments at the the expense
179 * of larger code size. This could be simplified by better use of
182 if ( tb = phTable[pHash(fid,page)] ) { /* ASSMT HERE */
184 MObtainWriteLock(&tb->lock,257);
185 ReleaseWriteLock(&afs_bufferLock);
187 tb->accesstime = timecounter++;
188 AFS_STATS(afs_stats_cmperf.bufHits++);
189 MReleaseWriteLock(&tb->lock);
193 register struct buffer **bufhead;
194 bufhead = &( phTable[pHash(fid,page)] );
195 while (tb2 = tb->hashNext) {
197 buf_Front(bufhead,tb,tb2);
198 MObtainWriteLock(&tb2->lock,258);
199 ReleaseWriteLock(&afs_bufferLock);
201 tb2->accesstime = timecounter++;
202 AFS_STATS(afs_stats_cmperf.bufHits++);
203 MReleaseWriteLock(&tb2->lock);
206 if (tb = tb2->hashNext) { /* ASSIGNMENT HERE! */
208 buf_Front(bufhead,tb2,tb);
209 MObtainWriteLock(&tb->lock,259);
210 ReleaseWriteLock(&afs_bufferLock);
212 tb->accesstime = timecounter++;
213 AFS_STATS(afs_stats_cmperf.bufHits++);
214 MReleaseWriteLock(&tb->lock);
224 AFS_STATS(afs_stats_cmperf.bufMisses++);
226 /* The last thing we looked at was either tb or tb2 (or nothing). That
227 * is at least the oldest buffer on one particular hash chain, so it's
228 * a pretty good place to start looking for the truly oldest buffer.
230 tb = afs_newslot(fid, page, (tb ? tb : tb2));
232 MReleaseWriteLock(&afs_bufferLock);
235 MObtainWriteLock(&tb->lock,260);
236 MReleaseWriteLock(&afs_bufferLock);
238 tfile = afs_CFileOpen(fid[0]);
239 sizep = (afs_int32 *)tfile;
240 if (page * AFS_BUFFER_PAGESIZE >= *sizep) {
243 MReleaseWriteLock(&tb->lock);
244 afs_CFileClose(tfile);
247 code = afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE,
248 tb->data, AFS_BUFFER_PAGESIZE);
249 afs_CFileClose(tfile);
250 if (code < AFS_BUFFER_PAGESIZE) {
253 MReleaseWriteLock(&tb->lock);
256 /* Note that findslot sets the page field in the buffer equal to
257 * what it is searching for. */
258 MReleaseWriteLock(&tb->lock);
262 static void FixupBucket(ap)
263 register struct buffer *ap; {
264 register struct buffer **lp, *tp;
266 /* first try to get it out of its current hash bucket, in which it
268 AFS_STATCNT(FixupBucket);
271 for(tp = *lp; tp; tp=tp->hashNext) {
278 /* now figure the new hash bucket */
279 i = pHash(ap->fid,ap->page);
280 ap->hashIndex = i; /* remember where we are for deletion */
281 ap->hashNext = phTable[i]; /* add us to the list */
282 phTable[i] = ap; /* at the front, since it's LRU */
285 static struct buffer *afs_newslot (afid,apage,lp)
288 register struct buffer *lp; /* pointer to a fairly-old buffer */
290 /* Find a usable buffer slot */
291 register afs_int32 i;
293 register struct buffer *tp;
296 AFS_STATCNT(afs_newslot);
297 /* we take a pointer here to a buffer which was at the end of an
298 * LRU hash chain. Odds are, it's one of the older buffers, not
299 * one of the newer. Having an older buffer to start with may
300 * permit us to avoid a few of the assignments in the "typical
301 * case" for loop below.
303 if (lp && (lp->lockers == 0)) {
311 /* timecounter might have wrapped, if machine is very very busy
312 * and stays up for a long time. Timecounter mustn't wrap twice
313 * (positive->negative->positive) before calling newslot, but that
314 * would require 2 billion consecutive cache hits... Anyway, the
315 * penalty is only that the cache replacement policy will be
316 * almost MRU for the next ~2 billion DReads... newslot doesn't
317 * get called nearly as often as DRead, so in order to avoid the
318 * performance penalty of using the hypers, it's worth doing the
319 * extra check here every time. It's probably cheaper than doing
320 * hcmp, anyway. There is a little performance hit resulting from
321 * resetting all the access times to 0, but it only happens once
322 * every month or so, and the access times will rapidly sort
323 * themselves back out after just a few more DReads.
325 if (timecounter < 0) {
328 for (i=0;i<nbuffers;i++,tp++) {
330 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
335 /* this is the typical case */
337 for (i=0;i<nbuffers;i++,tp++) {
338 if (tp->lockers == 0) {
339 if (tp->accesstime < lt) {
348 /* There are no unlocked buffers -- this used to panic, but that
349 * seems extreme. To the best of my knowledge, all the callers
350 * of DRead are prepared to handle a zero return. Some of them
351 * just panic directly, but not all of them. */
352 afs_warn ("all buffers locked");
357 tfile = afs_CFileOpen(lp->fid[0]);
358 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE,
359 lp->data, AFS_BUFFER_PAGESIZE);
361 afs_CFileClose(tfile);
362 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
365 /* Now fill in the header. */
366 dirp_Cpy(lp->fid, afid); /* set this */
368 lp->accesstime = timecounter++;
369 FixupBucket(lp); /* move to the right hash bucket */
374 void DRelease (bp,flag)
375 register struct buffer *bp;
377 /* Release a buffer, specifying whether or not the buffer has been
378 * modified by the locker. */
381 register struct buffer *tp;
384 AFS_STATCNT(DRelease);
387 /* look for buffer by scanning Unix buffers for appropriate address */
389 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
390 if ((afs_int32)bp >= (afs_int32)tp->data
391 && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
392 /* we found the right range */
393 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
398 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
400 bp = &(Buffers[index]);
401 MObtainWriteLock(&bp->lock,261);
403 if (flag) bp->dirty=1;
404 MReleaseWriteLock(&bp->lock);
409 /* Return the byte within a file represented by a buffer pointer. */
410 register struct buffer *bp;
413 register struct buffer *tp;
415 AFS_STATCNT(DVOffset);
418 /* look for buffer by scanning Unix buffers for appropriate address */
420 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
421 if ((afs_int32)bp >= (afs_int32)tp->data && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
422 /* we found the right range */
423 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
428 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
430 if (index<0 || index >= nbuffers) return -1;
431 bp = &(Buffers[index]);
432 return AFS_BUFFER_PAGESIZE*bp->page+(int)(((char *)ap)-bp->data);
435 /* 1/1/91 - I've modified the hash function to take the page as well
436 * as the *fid, so that lookup will be a bit faster. That presents some
437 * difficulties for Zap, which now has to have some knowledge of the nature
438 * of the hash function. Oh well. This should use the list traversal
445 /* Destroy all buffers pertaining to a particular fid. */
446 register struct buffer *tb;
449 MObtainReadLock(&afs_bufferLock);
451 for (i=0;i<=PHPAGEMASK;i++)
452 for(tb=phTable[pHash(fid,i)]; tb; tb=tb->hashNext)
453 if (dirp_Eq(tb->fid,fid)) {
454 MObtainWriteLock(&tb->lock,262);
457 MReleaseWriteLock(&tb->lock);
459 MReleaseReadLock(&afs_bufferLock);
463 /* Flush all the modified buffers. */
464 register int i, code;
465 register struct buffer *tb;
470 MObtainReadLock(&afs_bufferLock);
471 for(i=0;i<nbuffers;i++,tb++) {
473 MObtainWriteLock(&tb->lock,263);
475 MReleaseReadLock(&afs_bufferLock);
477 tfile = afs_CFileOpen(tb->fid[0]);
478 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
479 tb->data, AFS_BUFFER_PAGESIZE);
480 tb->dirty = 0; /* Clear the dirty flag */
481 afs_CFileClose(tfile);
484 MReleaseWriteLock(&tb->lock);
485 MObtainReadLock(&afs_bufferLock);
488 MReleaseReadLock(&afs_bufferLock);
491 char *DNew (fid,page)
495 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
496 register struct buffer *tb;
498 MObtainWriteLock(&afs_bufferLock,264);
499 if ((tb = afs_newslot(fid,page,NULL)) == 0) {
500 MReleaseWriteLock(&afs_bufferLock);
503 MObtainWriteLock(&tb->lock,265);
504 MReleaseWriteLock(&afs_bufferLock);
506 MReleaseWriteLock(&tb->lock);
510 void shutdown_bufferpackage() {
512 register struct buffer *tp;
515 extern int afs_cold_shutdown;
517 AFS_STATCNT(shutdown_bufferpackage);
518 /* Free all allocated Buffers and associated buffer pages */
520 if (afs_cold_shutdown) {
523 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
526 for (i=0; i < nbuffers; i+= NPB, tp += NPB) {
527 /* The following check shouldn't be necessary and it will be removed soon */
529 afs_warn("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
536 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
539 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
540 memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));