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"
16 #include "../afs/afsincludes.h"
18 #include "../h/param.h"
19 #include "../h/types.h"
20 #include "../h/time.h"
21 #if defined(AFS_AIX31_ENV) || defined(AFS_DEC_ENV)
22 #include "../h/limits.h"
24 #if !defined(AFS_AIX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
25 #include "../h/kernel.h" /* Doesn't needed, so it should go */
27 #endif /* !defined(UKERNEL) */
29 #include "../afs/afs_osi.h"
30 #include "../afsint/afsint.h"
31 #include "../afs/lock.h"
33 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
35 #endif /* !defined(UKERNEL) */
37 #if !defined(UKERNEL) && !defined(AFS_SUN5_ENV)
38 #include "../afs/osi_vfs.h"
41 #include "../afs/stds.h"
42 #include "../afs/volerrors.h"
43 #include "../afs/exporter.h"
44 #include "../afs/prs_fs.h"
45 #include "../afs/afs_chunkops.h"
46 #include "../afs/dir.h"
48 #include "../afs/afs_stats.h"
49 #include "../afs/longc_procs.h"
50 #include "../afs/afs.h"
53 #define BUF_TIME_MAX 0x7fffffff
55 /* number of pages per Unix buffer, when we're using Unix buffer pool */
58 #define AFS_BUFFER_PAGESIZE 2048
61 /* If you change any of this PH stuff, make sure you don't break DZap() */
62 /* use last two bits for page */
64 /* use next five bits for fid */
66 /* page hash table size - this is pretty intertwined with pHash */
67 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
69 #define pHash(fid,page) ((((afs_int32)((fid)[0])) & PHFIDMASK) \
70 | (page & PHPAGEMASK))
73 #undef dirty /* XXX */
76 static struct buffer *Buffers = 0;
77 static char *BufferData;
80 extern struct buf *geteblk();
83 #define timecounter afs_timecounter
85 /* The locks for individual buffer entries are now sometimes obtained while holding the
86 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
88 static afs_lock_t afs_bufferLock;
89 static struct buffer *phTable[PHSIZE]; /* page hash table */
91 static afs_int32 timecounter;
93 /* Prototypes for static routines */
94 static struct buffer *afs_newslot (ino_t *afid, afs_int32 apage,register struct buffer *lp);
96 static int dinit_flag = 0;
97 void DInit (int abuffers)
99 /* Initialize the venus buffer system. */
101 register struct buffer *tb;
103 struct buf *tub; /* unix buffer for allocation */
107 if (dinit_flag) return;
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");
114 Buffers = (struct buffer *) afs_osi_Alloc(abuffers * sizeof(struct buffer));
116 BufferData = (char *) afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
119 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
120 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
121 for (i=0;i<abuffers;i++) {
123 if ((i & (NPB-1)) == 0) {
124 /* time to allocate a fresh buffer */
125 tub = geteblk(AFS_BUFFER_PAGESIZE*NPB);
126 BufferData = (char *) tub->b_un.b_addr;
129 /* Fill in each buffer with an empty indication. */
135 if ((i & (NPB-1)) == 0)
139 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i&(NPB-1))];
141 tb->data = &BufferData[AFS_BUFFER_PAGESIZE*i];
145 RWLOCK_INIT(&tb->lock, "buffer lock");
150 char *DRead(register ino_t *fid, register int page)
152 /* Read a page from the disk. */
153 register struct buffer *tb, *tb2;
155 register afs_int32 code, *sizep;
158 MObtainWriteLock(&afs_bufferLock,256);
160 /* some new code added 1/1/92 */
161 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
162 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
164 /* this apparently-complicated-looking code is simply an example of
165 * a little bit of loop unrolling, and is a standard linked-list
166 * traversal trick. It saves a few assignments at the the expense
167 * of larger code size. This could be simplified by better use of
170 if ((tb = phTable[pHash(fid,page)])) {
172 MObtainWriteLock(&tb->lock,257);
173 ReleaseWriteLock(&afs_bufferLock);
175 tb->accesstime = timecounter++;
176 AFS_STATS(afs_stats_cmperf.bufHits++);
177 MReleaseWriteLock(&tb->lock);
181 register struct buffer **bufhead;
182 bufhead = &( phTable[pHash(fid,page)] );
183 while ((tb2 = tb->hashNext)) {
185 buf_Front(bufhead,tb,tb2);
186 MObtainWriteLock(&tb2->lock,258);
187 ReleaseWriteLock(&afs_bufferLock);
189 tb2->accesstime = timecounter++;
190 AFS_STATS(afs_stats_cmperf.bufHits++);
191 MReleaseWriteLock(&tb2->lock);
194 if ((tb = tb2->hashNext)) {
196 buf_Front(bufhead,tb2,tb);
197 MObtainWriteLock(&tb->lock,259);
198 ReleaseWriteLock(&afs_bufferLock);
200 tb->accesstime = timecounter++;
201 AFS_STATS(afs_stats_cmperf.bufHits++);
202 MReleaseWriteLock(&tb->lock);
212 AFS_STATS(afs_stats_cmperf.bufMisses++);
214 /* The last thing we looked at was either tb or tb2 (or nothing). That
215 * is at least the oldest buffer on one particular hash chain, so it's
216 * a pretty good place to start looking for the truly oldest buffer.
218 tb = afs_newslot(fid, page, (tb ? tb : tb2));
220 MReleaseWriteLock(&afs_bufferLock);
223 MObtainWriteLock(&tb->lock,260);
224 MReleaseWriteLock(&afs_bufferLock);
226 tfile = afs_CFileOpen(fid[0]);
227 sizep = (afs_int32 *)tfile;
228 if (page * AFS_BUFFER_PAGESIZE >= *sizep) {
231 MReleaseWriteLock(&tb->lock);
232 afs_CFileClose(tfile);
235 code = afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE,
236 tb->data, 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);
250 static void FixupBucket(register struct buffer *ap)
252 register struct buffer **lp, *tp;
254 /* first try to get it out of its current hash bucket, in which it
256 AFS_STATCNT(FixupBucket);
259 for(tp = *lp; tp; tp=tp->hashNext) {
266 /* now figure the new hash bucket */
267 i = pHash(ap->fid,ap->page);
268 ap->hashIndex = i; /* remember where we are for deletion */
269 ap->hashNext = phTable[i]; /* add us to the list */
270 phTable[i] = ap; /* at the front, since it's LRU */
273 /* lp is pointer to a fairly-old buffer */
274 static struct buffer *afs_newslot (ino_t *afid, afs_int32 apage,register struct buffer *lp)
276 /* Find a usable buffer slot */
277 register afs_int32 i;
279 register struct buffer *tp;
282 AFS_STATCNT(afs_newslot);
283 /* we take a pointer here to a buffer which was at the end of an
284 * LRU hash chain. Odds are, it's one of the older buffers, not
285 * one of the newer. Having an older buffer to start with may
286 * permit us to avoid a few of the assignments in the "typical
287 * case" for loop below.
289 if (lp && (lp->lockers == 0)) {
297 /* timecounter might have wrapped, if machine is very very busy
298 * and stays up for a long time. Timecounter mustn't wrap twice
299 * (positive->negative->positive) before calling newslot, but that
300 * would require 2 billion consecutive cache hits... Anyway, the
301 * penalty is only that the cache replacement policy will be
302 * almost MRU for the next ~2 billion DReads... newslot doesn't
303 * get called nearly as often as DRead, so in order to avoid the
304 * performance penalty of using the hypers, it's worth doing the
305 * extra check here every time. It's probably cheaper than doing
306 * hcmp, anyway. There is a little performance hit resulting from
307 * resetting all the access times to 0, but it only happens once
308 * every month or so, and the access times will rapidly sort
309 * themselves back out after just a few more DReads.
311 if (timecounter < 0) {
314 for (i=0;i<nbuffers;i++,tp++) {
316 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[0]);
344 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE,
345 lp->data, 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 */
360 void DRelease (register struct buffer *bp, int flag)
362 /* Release a buffer, specifying whether or not the buffer has been
363 * modified by the locker. */
366 register struct buffer *tp;
369 AFS_STATCNT(DRelease);
372 /* look for buffer by scanning Unix buffers for appropriate address */
374 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
375 if ((afs_int32)bp >= (afs_int32)tp->data
376 && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
377 /* we found the right range */
378 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
383 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
385 bp = &(Buffers[index]);
386 MObtainWriteLock(&bp->lock,261);
388 if (flag) bp->dirty=1;
389 MReleaseWriteLock(&bp->lock);
392 int DVOffset (register void *ap)
394 /* Return the byte within a file represented by a buffer pointer. */
395 register struct buffer *bp;
398 register struct buffer *tp;
400 AFS_STATCNT(DVOffset);
403 /* look for buffer by scanning Unix buffers for appropriate address */
405 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
406 if ((afs_int32)bp >= (afs_int32)tp->data && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
407 /* we found the right range */
408 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
413 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
415 if (index<0 || index >= nbuffers) return -1;
416 bp = &(Buffers[index]);
417 return AFS_BUFFER_PAGESIZE*bp->page+(int)(((char *)ap)-bp->data);
420 /* 1/1/91 - I've modified the hash function to take the page as well
421 * as the *fid, so that lookup will be a bit faster. That presents some
422 * difficulties for Zap, which now has to have some knowledge of the nature
423 * of the hash function. Oh well. This should use the list traversal
426 void DZap (ino_t *fid)
429 /* Destroy all buffers pertaining to a particular fid. */
430 register struct buffer *tb;
433 MObtainReadLock(&afs_bufferLock);
435 for (i=0;i<=PHPAGEMASK;i++)
436 for(tb=phTable[pHash(fid,i)]; tb; tb=tb->hashNext)
437 if (dirp_Eq(tb->fid,fid)) {
438 MObtainWriteLock(&tb->lock,262);
441 MReleaseWriteLock(&tb->lock);
443 MReleaseReadLock(&afs_bufferLock);
448 /* Flush all the modified buffers. */
450 register struct buffer *tb;
455 MObtainReadLock(&afs_bufferLock);
456 for(i=0;i<nbuffers;i++,tb++) {
458 MObtainWriteLock(&tb->lock,263);
460 MReleaseReadLock(&afs_bufferLock);
462 tfile = afs_CFileOpen(tb->fid[0]);
463 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
464 tb->data, AFS_BUFFER_PAGESIZE);
465 tb->dirty = 0; /* Clear the dirty flag */
466 afs_CFileClose(tfile);
469 MReleaseWriteLock(&tb->lock);
470 MObtainReadLock(&afs_bufferLock);
473 MReleaseReadLock(&afs_bufferLock);
476 char *DNew (register ino_t *fid, register int page)
478 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
479 register struct buffer *tb;
481 MObtainWriteLock(&afs_bufferLock,264);
482 if ((tb = afs_newslot(fid,page,NULL)) == 0) {
483 MReleaseWriteLock(&afs_bufferLock);
486 MObtainWriteLock(&tb->lock,265);
487 MReleaseWriteLock(&afs_bufferLock);
489 MReleaseWriteLock(&tb->lock);
493 void shutdown_bufferpackage(void)
496 register struct buffer *tp;
499 extern int afs_cold_shutdown;
501 AFS_STATCNT(shutdown_bufferpackage);
502 /* Free all allocated Buffers and associated buffer pages */
504 if (afs_cold_shutdown) {
507 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
510 for (i=0; i < nbuffers; i+= NPB, tp += NPB) {
511 /* The following check shouldn't be necessary and it will be removed soon */
513 afs_warn("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
520 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
523 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
524 memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));