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();
98 #define timecounter afs_timecounter
100 /* The locks for individual buffer entries are now sometimes obtained while holding the
101 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
103 static afs_lock_t afs_bufferLock;
104 static struct buffer *phTable[PHSIZE]; /* page hash table */
106 afs_int32 timecounter;
108 static struct buffer *afs_newslot();
110 static int dinit_flag = 0;
111 void DInit (abuffers)
113 /* Initialize the venus buffer system. */
115 register struct buffer *tb;
117 struct buf *tub; /* unix buffer for allocation */
121 if (dinit_flag) return;
124 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
125 abuffers = ((abuffers-1) | (NPB-1)) + 1;
127 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
128 Buffers = (struct buffer *) afs_osi_Alloc(abuffers * sizeof(struct buffer));
130 BufferData = (char *) afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
133 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
134 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
135 for (i=0;i<abuffers;i++) {
137 if ((i & (NPB-1)) == 0) {
138 /* time to allocate a fresh buffer */
139 tub = geteblk(AFS_BUFFER_PAGESIZE*NPB);
140 BufferData = (char *) tub->b_un.b_addr;
143 /* Fill in each buffer with an empty indication. */
149 if ((i & (NPB-1)) == 0)
153 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i&(NPB-1))];
155 tb->data = &BufferData[AFS_BUFFER_PAGESIZE*i];
159 RWLOCK_INIT(&tb->lock, "buffer lock");
164 char *DRead(fid,page)
167 /* Read a page from the disk. */
168 register struct buffer *tb, *tb2;
170 register afs_int32 code, *sizep;
173 MObtainWriteLock(&afs_bufferLock,256);
175 /* some new code added 1/1/92 */
176 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
177 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
179 /* this apparently-complicated-looking code is simply an example of
180 * a little bit of loop unrolling, and is a standard linked-list
181 * traversal trick. It saves a few assignments at the the expense
182 * of larger code size. This could be simplified by better use of
185 if ( tb = phTable[pHash(fid,page)] ) { /* ASSMT HERE */
187 MObtainWriteLock(&tb->lock,257);
188 ReleaseWriteLock(&afs_bufferLock);
190 tb->accesstime = timecounter++;
191 AFS_STATS(afs_stats_cmperf.bufHits++);
192 MReleaseWriteLock(&tb->lock);
196 register struct buffer **bufhead;
197 bufhead = &( phTable[pHash(fid,page)] );
198 while (tb2 = tb->hashNext) {
200 buf_Front(bufhead,tb,tb2);
201 MObtainWriteLock(&tb2->lock,258);
202 ReleaseWriteLock(&afs_bufferLock);
204 tb2->accesstime = timecounter++;
205 AFS_STATS(afs_stats_cmperf.bufHits++);
206 MReleaseWriteLock(&tb2->lock);
209 if (tb = tb2->hashNext) { /* ASSIGNMENT HERE! */
211 buf_Front(bufhead,tb2,tb);
212 MObtainWriteLock(&tb->lock,259);
213 ReleaseWriteLock(&afs_bufferLock);
215 tb->accesstime = timecounter++;
216 AFS_STATS(afs_stats_cmperf.bufHits++);
217 MReleaseWriteLock(&tb->lock);
227 AFS_STATS(afs_stats_cmperf.bufMisses++);
229 /* The last thing we looked at was either tb or tb2 (or nothing). That
230 * is at least the oldest buffer on one particular hash chain, so it's
231 * a pretty good place to start looking for the truly oldest buffer.
233 tb = afs_newslot(fid, page, (tb ? tb : tb2));
235 MReleaseWriteLock(&afs_bufferLock);
238 MObtainWriteLock(&tb->lock,260);
239 MReleaseWriteLock(&afs_bufferLock);
241 tfile = afs_CFileOpen(fid[0]);
242 sizep = (afs_int32 *)tfile;
243 if (page * AFS_BUFFER_PAGESIZE >= *sizep) {
246 MReleaseWriteLock(&tb->lock);
247 afs_CFileClose(tfile);
250 code = afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE,
251 tb->data, AFS_BUFFER_PAGESIZE);
252 afs_CFileClose(tfile);
253 if (code < AFS_BUFFER_PAGESIZE) {
256 MReleaseWriteLock(&tb->lock);
259 /* Note that findslot sets the page field in the buffer equal to
260 * what it is searching for. */
261 MReleaseWriteLock(&tb->lock);
265 static void FixupBucket(ap)
266 register struct buffer *ap; {
267 register struct buffer **lp, *tp;
269 /* first try to get it out of its current hash bucket, in which it
271 AFS_STATCNT(FixupBucket);
274 for(tp = *lp; tp; tp=tp->hashNext) {
281 /* now figure the new hash bucket */
282 i = pHash(ap->fid,ap->page);
283 ap->hashIndex = i; /* remember where we are for deletion */
284 ap->hashNext = phTable[i]; /* add us to the list */
285 phTable[i] = ap; /* at the front, since it's LRU */
288 static struct buffer *afs_newslot (afid,apage,lp)
291 register struct buffer *lp; /* pointer to a fairly-old buffer */
293 /* Find a usable buffer slot */
294 register afs_int32 i;
296 register struct buffer *tp;
299 AFS_STATCNT(afs_newslot);
300 /* we take a pointer here to a buffer which was at the end of an
301 * LRU hash chain. Odds are, it's one of the older buffers, not
302 * one of the newer. Having an older buffer to start with may
303 * permit us to avoid a few of the assignments in the "typical
304 * case" for loop below.
306 if (lp && (lp->lockers == 0)) {
314 /* timecounter might have wrapped, if machine is very very busy
315 * and stays up for a long time. Timecounter mustn't wrap twice
316 * (positive->negative->positive) before calling newslot, but that
317 * would require 2 billion consecutive cache hits... Anyway, the
318 * penalty is only that the cache replacement policy will be
319 * almost MRU for the next ~2 billion DReads... newslot doesn't
320 * get called nearly as often as DRead, so in order to avoid the
321 * performance penalty of using the hypers, it's worth doing the
322 * extra check here every time. It's probably cheaper than doing
323 * hcmp, anyway. There is a little performance hit resulting from
324 * resetting all the access times to 0, but it only happens once
325 * every month or so, and the access times will rapidly sort
326 * themselves back out after just a few more DReads.
328 if (timecounter < 0) {
331 for (i=0;i<nbuffers;i++,tp++) {
333 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
338 /* this is the typical case */
340 for (i=0;i<nbuffers;i++,tp++) {
341 if (tp->lockers == 0) {
342 if (tp->accesstime < lt) {
351 /* There are no unlocked buffers -- this used to panic, but that
352 * seems extreme. To the best of my knowledge, all the callers
353 * of DRead are prepared to handle a zero return. Some of them
354 * just panic directly, but not all of them. */
355 afs_warn ("all buffers locked");
360 tfile = afs_CFileOpen(lp->fid[0]);
361 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE,
362 lp->data, AFS_BUFFER_PAGESIZE);
364 afs_CFileClose(tfile);
365 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
368 /* Now fill in the header. */
369 dirp_Cpy(lp->fid, afid); /* set this */
371 lp->accesstime = timecounter++;
372 FixupBucket(lp); /* move to the right hash bucket */
377 void DRelease (bp,flag)
378 register struct buffer *bp;
380 /* Release a buffer, specifying whether or not the buffer has been
381 * modified by the locker. */
384 register struct buffer *tp;
387 AFS_STATCNT(DRelease);
390 /* look for buffer by scanning Unix buffers for appropriate address */
392 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
393 if ((afs_int32)bp >= (afs_int32)tp->data
394 && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
395 /* we found the right range */
396 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
401 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
403 bp = &(Buffers[index]);
404 MObtainWriteLock(&bp->lock,261);
406 if (flag) bp->dirty=1;
407 MReleaseWriteLock(&bp->lock);
412 /* Return the byte within a file represented by a buffer pointer. */
413 register struct buffer *bp;
416 register struct buffer *tp;
418 AFS_STATCNT(DVOffset);
421 /* look for buffer by scanning Unix buffers for appropriate address */
423 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
424 if ((afs_int32)bp >= (afs_int32)tp->data && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
425 /* we found the right range */
426 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
431 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
433 if (index<0 || index >= nbuffers) return -1;
434 bp = &(Buffers[index]);
435 return AFS_BUFFER_PAGESIZE*bp->page+(int)(((char *)ap)-bp->data);
438 /* 1/1/91 - I've modified the hash function to take the page as well
439 * as the *fid, so that lookup will be a bit faster. That presents some
440 * difficulties for Zap, which now has to have some knowledge of the nature
441 * of the hash function. Oh well. This should use the list traversal
448 /* Destroy all buffers pertaining to a particular fid. */
449 register struct buffer *tb;
452 MObtainReadLock(&afs_bufferLock);
454 for (i=0;i<=PHPAGEMASK;i++)
455 for(tb=phTable[pHash(fid,i)]; tb; tb=tb->hashNext)
456 if (dirp_Eq(tb->fid,fid)) {
457 MObtainWriteLock(&tb->lock,262);
460 MReleaseWriteLock(&tb->lock);
462 MReleaseReadLock(&afs_bufferLock);
466 /* Flush all the modified buffers. */
467 register int i, code;
468 register struct buffer *tb;
473 MObtainReadLock(&afs_bufferLock);
474 for(i=0;i<nbuffers;i++,tb++) {
476 MObtainWriteLock(&tb->lock,263);
478 MReleaseReadLock(&afs_bufferLock);
480 tfile = afs_CFileOpen(tb->fid[0]);
481 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
482 tb->data, AFS_BUFFER_PAGESIZE);
483 tb->dirty = 0; /* Clear the dirty flag */
484 afs_CFileClose(tfile);
487 MReleaseWriteLock(&tb->lock);
488 MObtainReadLock(&afs_bufferLock);
491 MReleaseReadLock(&afs_bufferLock);
494 char *DNew (fid,page)
498 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
499 register struct buffer *tb;
501 MObtainWriteLock(&afs_bufferLock,264);
502 if ((tb = afs_newslot(fid,page,NULL)) == 0) {
503 MReleaseWriteLock(&afs_bufferLock);
506 MObtainWriteLock(&tb->lock,265);
507 MReleaseWriteLock(&afs_bufferLock);
509 MReleaseWriteLock(&tb->lock);
513 void shutdown_bufferpackage() {
515 register struct buffer *tp;
518 extern int afs_cold_shutdown;
520 AFS_STATCNT(shutdown_bufferpackage);
521 /* Free all allocated Buffers and associated buffer pages */
523 if (afs_cold_shutdown) {
526 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
529 for (i=0; i < nbuffers; i+= NPB, tp += NPB) {
530 /* The following check shouldn't be necessary and it will be removed soon */
532 afs_warn("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
539 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
542 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
543 memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));