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 "../afs/param.h"
11 #include "../afs/sysincludes.h"
13 #include "../h/param.h"
14 #include "../h/types.h"
15 #include "../h/time.h"
16 #if defined(AFS_AIX31_ENV) || defined(AFS_DEC_ENV)
17 #include "../h/limits.h"
19 #if !defined(AFS_AIX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
20 #include "../h/kernel.h" /* Doesn't needed, so it should go */
22 #endif /* !defined(UKERNEL) */
24 #include "../afs/afs_osi.h"
25 #include "../afsint/afsint.h"
26 #include "../afs/lock.h"
28 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
30 #endif /* !defined(UKERNEL) */
32 #include "../afs/stds.h"
33 #include "../afs/volerrors.h"
34 #include "../afs/exporter.h"
35 #include "../afs/prs_fs.h"
36 #include "../afs/afs_chunkops.h"
37 #include "../afs/dir.h"
39 #include "../afs/afs_stats.h"
40 #include "../afs/longc_procs.h"
43 #define BUF_TIME_MAX 0x7fffffff
45 /* number of pages per Unix buffer, when we're using Unix buffer pool */
48 #define AFS_BUFFER_PAGESIZE 2048
51 /* If you change any of this PH stuff, make sure you don't break DZap() */
52 /* use last two bits for page */
54 /* use next five bits for fid */
56 /* page hash table size - this is pretty intertwined with pHash */
57 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
59 #define pHash(fid,page) ((((afs_int32)((fid)[0])) & PHFIDMASK) \
60 | (page & PHPAGEMASK))
62 /* Note: this should agree with the definition in kdump.c */
63 #if defined(AFS_OSF_ENV)
65 #define AFS_USEBUFFERS 1
70 #undef dirty /* XXX */
74 ino_t fid[1]; /* Unique cache key + i/o addressing */
77 struct buffer *hashNext;
85 afs_rwlock_t lock; /* the lock for this structure */
91 extern struct buf *geteblk();
93 /* The locks for individual buffer entries are now sometimes obtained while holding the
94 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
96 static afs_lock_t afs_bufferLock;
97 static struct buffer *phTable[PHSIZE]; /* page hash table */
99 afs_int32 timecounter;
101 static struct buffer *afs_newslot();
103 static int dinit_flag = 0;
104 void DInit (abuffers)
106 /* Initialize the venus buffer system. */
108 register struct buffer *tb;
110 struct buf *tub; /* unix buffer for allocation */
114 if (dinit_flag) return;
117 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
118 abuffers = ((abuffers-1) | (NPB-1)) + 1;
120 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
121 Buffers = (struct buffer *) afs_osi_Alloc(abuffers * sizeof(struct buffer));
123 BufferData = (char *) afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
126 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
127 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
128 for (i=0;i<abuffers;i++) {
130 if ((i & (NPB-1)) == 0) {
131 /* time to allocate a fresh buffer */
132 tub = geteblk(AFS_BUFFER_PAGESIZE*NPB);
133 BufferData = (char *) tub->b_un.b_addr;
136 /* Fill in each buffer with an empty indication. */
142 if ((i & (NPB-1)) == 0)
146 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i&(NPB-1))];
148 tb->data = &BufferData[AFS_BUFFER_PAGESIZE*i];
152 RWLOCK_INIT(&tb->lock, "buffer lock");
157 char *DRead(fid,page)
160 /* Read a page from the disk. */
161 register struct buffer *tb, *tb2;
163 register afs_int32 code, *sizep;
166 MObtainWriteLock(&afs_bufferLock,256);
168 /* some new code added 1/1/92 */
169 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
170 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
172 /* this apparently-complicated-looking code is simply an example of
173 * a little bit of loop unrolling, and is a standard linked-list
174 * traversal trick. It saves a few assignments at the the expense
175 * of larger code size. This could be simplified by better use of
178 if ( tb = phTable[pHash(fid,page)] ) { /* ASSMT HERE */
180 MObtainWriteLock(&tb->lock,257);
181 ReleaseWriteLock(&afs_bufferLock);
183 tb->accesstime = timecounter++;
184 AFS_STATS(afs_stats_cmperf.bufHits++);
185 MReleaseWriteLock(&tb->lock);
189 register struct buffer **bufhead;
190 bufhead = &( phTable[pHash(fid,page)] );
191 while (tb2 = tb->hashNext) {
193 buf_Front(bufhead,tb,tb2);
194 MObtainWriteLock(&tb2->lock,258);
195 ReleaseWriteLock(&afs_bufferLock);
197 tb2->accesstime = timecounter++;
198 AFS_STATS(afs_stats_cmperf.bufHits++);
199 MReleaseWriteLock(&tb2->lock);
202 if (tb = tb2->hashNext) { /* ASSIGNMENT HERE! */
204 buf_Front(bufhead,tb2,tb);
205 MObtainWriteLock(&tb->lock,259);
206 ReleaseWriteLock(&afs_bufferLock);
208 tb->accesstime = timecounter++;
209 AFS_STATS(afs_stats_cmperf.bufHits++);
210 MReleaseWriteLock(&tb->lock);
220 AFS_STATS(afs_stats_cmperf.bufMisses++);
222 /* The last thing we looked at was either tb or tb2 (or nothing). That
223 * is at least the oldest buffer on one particular hash chain, so it's
224 * a pretty good place to start looking for the truly oldest buffer.
226 tb = afs_newslot(fid, page, (tb ? tb : tb2));
228 MReleaseWriteLock(&afs_bufferLock);
231 tfile = afs_CFileOpen(fid[0]);
232 sizep = (afs_int32 *)tfile;
233 if (page * AFS_BUFFER_PAGESIZE >= *sizep) {
235 afs_CFileClose(tfile);
236 MReleaseWriteLock(&afs_bufferLock);
239 MObtainWriteLock(&tb->lock,260);
240 MReleaseWriteLock(&afs_bufferLock);
242 code = afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE,
243 tb->data, AFS_BUFFER_PAGESIZE);
244 afs_CFileClose(tfile);
245 if (code < AFS_BUFFER_PAGESIZE) {
248 MReleaseWriteLock(&tb->lock);
251 /* Note that findslot sets the page field in the buffer equal to
252 * what it is searching for. */
253 MReleaseWriteLock(&tb->lock);
257 static void FixupBucket(ap)
258 register struct buffer *ap; {
259 register struct buffer **lp, *tp;
261 /* first try to get it out of its current hash bucket, in which it
263 AFS_STATCNT(FixupBucket);
266 for(tp = *lp; tp; tp=tp->hashNext) {
273 /* now figure the new hash bucket */
274 i = pHash(ap->fid,ap->page);
275 ap->hashIndex = i; /* remember where we are for deletion */
276 ap->hashNext = phTable[i]; /* add us to the list */
277 phTable[i] = ap; /* at the front, since it's LRU */
280 static struct buffer *afs_newslot (afid,apage,lp)
283 register struct buffer *lp; /* pointer to a fairly-old buffer */
285 /* Find a usable buffer slot */
286 register afs_int32 i;
288 register struct buffer *tp;
291 AFS_STATCNT(afs_newslot);
292 /* we take a pointer here to a buffer which was at the end of an
293 * LRU hash chain. Odds are, it's one of the older buffers, not
294 * one of the newer. Having an older buffer to start with may
295 * permit us to avoid a few of the assignments in the "typical
296 * case" for loop below.
298 if (lp && (lp->lockers == 0)) {
306 /* timecounter might have wrapped, if machine is very very busy
307 * and stays up for a long time. Timecounter mustn't wrap twice
308 * (positive->negative->positive) before calling newslot, but that
309 * would require 2 billion consecutive cache hits... Anyway, the
310 * penalty is only that the cache replacement policy will be
311 * almost MRU for the next ~2 billion DReads... newslot doesn't
312 * get called nearly as often as DRead, so in order to avoid the
313 * performance penalty of using the hypers, it's worth doing the
314 * extra check here every time. It's probably cheaper than doing
315 * hcmp, anyway. There is a little performance hit resulting from
316 * resetting all the access times to 0, but it only happens once
317 * every month or so, and the access times will rapidly sort
318 * themselves back out after just a few more DReads.
320 if (timecounter < 0) {
323 for (i=0;i<nbuffers;i++,tp++) {
325 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
330 /* this is the typical case */
332 for (i=0;i<nbuffers;i++,tp++) {
333 if (tp->lockers == 0) {
334 if (tp->accesstime < lt) {
343 /* There are no unlocked buffers -- this used to panic, but that
344 * seems extreme. To the best of my knowledge, all the callers
345 * of DRead are prepared to handle a zero return. Some of them
346 * just panic directly, but not all of them. */
347 afs_warn ("all buffers locked");
352 tfile = afs_CFileOpen(lp->fid[0]);
353 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE,
354 lp->data, AFS_BUFFER_PAGESIZE);
356 afs_CFileClose(tfile);
357 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
360 /* Now fill in the header. */
361 dirp_Cpy(lp->fid, afid); /* set this */
363 lp->accesstime = timecounter++;
364 FixupBucket(lp); /* move to the right hash bucket */
369 void DRelease (bp,flag)
370 register struct buffer *bp;
372 /* Release a buffer, specifying whether or not the buffer has been
373 * modified by the locker. */
376 register struct buffer *tp;
379 AFS_STATCNT(DRelease);
382 /* look for buffer by scanning Unix buffers for appropriate address */
384 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
385 if ((afs_int32)bp >= (afs_int32)tp->data
386 && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
387 /* we found the right range */
388 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
393 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
395 bp = &(Buffers[index]);
396 MObtainWriteLock(&bp->lock,261);
398 if (flag) bp->dirty=1;
399 MReleaseWriteLock(&bp->lock);
404 /* Return the byte within a file represented by a buffer pointer. */
405 register struct buffer *bp;
408 register struct buffer *tp;
410 AFS_STATCNT(DVOffset);
413 /* look for buffer by scanning Unix buffers for appropriate address */
415 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
416 if ((afs_int32)bp >= (afs_int32)tp->data && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
417 /* we found the right range */
418 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
423 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
425 if (index<0 || index >= nbuffers) return -1;
426 bp = &(Buffers[index]);
427 return AFS_BUFFER_PAGESIZE*bp->page+(int)(((char *)ap)-bp->data);
430 /* 1/1/91 - I've modified the hash function to take the page as well
431 * as the *fid, so that lookup will be a bit faster. That presents some
432 * difficulties for Zap, which now has to have some knowledge of the nature
433 * of the hash function. Oh well. This should use the list traversal
440 /* Destroy all buffers pertaining to a particular fid. */
441 register struct buffer *tb;
444 MObtainReadLock(&afs_bufferLock);
446 for (i=0;i<=PHPAGEMASK;i++)
447 for(tb=phTable[pHash(fid,i)]; tb; tb=tb->hashNext)
448 if (dirp_Eq(tb->fid,fid)) {
449 MObtainWriteLock(&tb->lock,262);
452 MReleaseWriteLock(&tb->lock);
454 MReleaseReadLock(&afs_bufferLock);
458 /* Flush all the modified buffers. */
459 register int i, code;
460 register struct buffer *tb;
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[0]);
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);
486 char *DNew (fid,page)
490 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
491 register struct buffer *tb;
493 MObtainWriteLock(&afs_bufferLock,264);
494 if ((tb = afs_newslot(fid,page,NULL)) == 0) {
495 MReleaseWriteLock(&afs_bufferLock);
498 MObtainWriteLock(&tb->lock,265);
499 MReleaseWriteLock(&afs_bufferLock);
501 MReleaseWriteLock(&tb->lock);
505 void shutdown_bufferpackage() {
507 register struct buffer *tp;
510 extern int afs_cold_shutdown;
512 AFS_STATCNT(shutdown_bufferpackage);
513 /* Free all allocated Buffers and associated buffer pages */
515 if (afs_cold_shutdown) {
518 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
521 for (i=0; i < nbuffers; i+= NPB, tp += NPB) {
522 /* The following check shouldn't be necessary and it will be removed soon */
524 afs_warn("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
531 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
534 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
535 bzero((char *)&afs_bufferLock, sizeof(afs_lock_t));