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 <afsconfig.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 tfile = afs_CFileOpen(fid[0]);
236 sizep = (afs_int32 *)tfile;
237 if (page * AFS_BUFFER_PAGESIZE >= *sizep) {
239 afs_CFileClose(tfile);
240 MReleaseWriteLock(&afs_bufferLock);
243 MObtainWriteLock(&tb->lock,260);
244 MReleaseWriteLock(&afs_bufferLock);
246 code = afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE,
247 tb->data, AFS_BUFFER_PAGESIZE);
248 afs_CFileClose(tfile);
249 if (code < AFS_BUFFER_PAGESIZE) {
252 MReleaseWriteLock(&tb->lock);
255 /* Note that findslot sets the page field in the buffer equal to
256 * what it is searching for. */
257 MReleaseWriteLock(&tb->lock);
261 static void FixupBucket(ap)
262 register struct buffer *ap; {
263 register struct buffer **lp, *tp;
265 /* first try to get it out of its current hash bucket, in which it
267 AFS_STATCNT(FixupBucket);
270 for(tp = *lp; tp; tp=tp->hashNext) {
277 /* now figure the new hash bucket */
278 i = pHash(ap->fid,ap->page);
279 ap->hashIndex = i; /* remember where we are for deletion */
280 ap->hashNext = phTable[i]; /* add us to the list */
281 phTable[i] = ap; /* at the front, since it's LRU */
284 static struct buffer *afs_newslot (afid,apage,lp)
287 register struct buffer *lp; /* pointer to a fairly-old buffer */
289 /* Find a usable buffer slot */
290 register afs_int32 i;
292 register struct buffer *tp;
295 AFS_STATCNT(afs_newslot);
296 /* we take a pointer here to a buffer which was at the end of an
297 * LRU hash chain. Odds are, it's one of the older buffers, not
298 * one of the newer. Having an older buffer to start with may
299 * permit us to avoid a few of the assignments in the "typical
300 * case" for loop below.
302 if (lp && (lp->lockers == 0)) {
310 /* timecounter might have wrapped, if machine is very very busy
311 * and stays up for a long time. Timecounter mustn't wrap twice
312 * (positive->negative->positive) before calling newslot, but that
313 * would require 2 billion consecutive cache hits... Anyway, the
314 * penalty is only that the cache replacement policy will be
315 * almost MRU for the next ~2 billion DReads... newslot doesn't
316 * get called nearly as often as DRead, so in order to avoid the
317 * performance penalty of using the hypers, it's worth doing the
318 * extra check here every time. It's probably cheaper than doing
319 * hcmp, anyway. There is a little performance hit resulting from
320 * resetting all the access times to 0, but it only happens once
321 * every month or so, and the access times will rapidly sort
322 * themselves back out after just a few more DReads.
324 if (timecounter < 0) {
327 for (i=0;i<nbuffers;i++,tp++) {
329 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
334 /* this is the typical case */
336 for (i=0;i<nbuffers;i++,tp++) {
337 if (tp->lockers == 0) {
338 if (tp->accesstime < lt) {
347 /* There are no unlocked buffers -- this used to panic, but that
348 * seems extreme. To the best of my knowledge, all the callers
349 * of DRead are prepared to handle a zero return. Some of them
350 * just panic directly, but not all of them. */
351 afs_warn ("all buffers locked");
356 tfile = afs_CFileOpen(lp->fid[0]);
357 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE,
358 lp->data, AFS_BUFFER_PAGESIZE);
360 afs_CFileClose(tfile);
361 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
364 /* Now fill in the header. */
365 dirp_Cpy(lp->fid, afid); /* set this */
367 lp->accesstime = timecounter++;
368 FixupBucket(lp); /* move to the right hash bucket */
373 void DRelease (bp,flag)
374 register struct buffer *bp;
376 /* Release a buffer, specifying whether or not the buffer has been
377 * modified by the locker. */
380 register struct buffer *tp;
383 AFS_STATCNT(DRelease);
386 /* look for buffer by scanning Unix buffers for appropriate address */
388 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
389 if ((afs_int32)bp >= (afs_int32)tp->data
390 && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
391 /* we found the right range */
392 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
397 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
399 bp = &(Buffers[index]);
400 MObtainWriteLock(&bp->lock,261);
402 if (flag) bp->dirty=1;
403 MReleaseWriteLock(&bp->lock);
408 /* Return the byte within a file represented by a buffer pointer. */
409 register struct buffer *bp;
412 register struct buffer *tp;
414 AFS_STATCNT(DVOffset);
417 /* look for buffer by scanning Unix buffers for appropriate address */
419 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
420 if ((afs_int32)bp >= (afs_int32)tp->data && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
421 /* we found the right range */
422 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
427 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
429 if (index<0 || index >= nbuffers) return -1;
430 bp = &(Buffers[index]);
431 return AFS_BUFFER_PAGESIZE*bp->page+(int)(((char *)ap)-bp->data);
434 /* 1/1/91 - I've modified the hash function to take the page as well
435 * as the *fid, so that lookup will be a bit faster. That presents some
436 * difficulties for Zap, which now has to have some knowledge of the nature
437 * of the hash function. Oh well. This should use the list traversal
444 /* Destroy all buffers pertaining to a particular fid. */
445 register struct buffer *tb;
448 MObtainReadLock(&afs_bufferLock);
450 for (i=0;i<=PHPAGEMASK;i++)
451 for(tb=phTable[pHash(fid,i)]; tb; tb=tb->hashNext)
452 if (dirp_Eq(tb->fid,fid)) {
453 MObtainWriteLock(&tb->lock,262);
456 MReleaseWriteLock(&tb->lock);
458 MReleaseReadLock(&afs_bufferLock);
462 /* Flush all the modified buffers. */
463 register int i, code;
464 register struct buffer *tb;
469 MObtainReadLock(&afs_bufferLock);
470 for(i=0;i<nbuffers;i++,tb++) {
472 MObtainWriteLock(&tb->lock,263);
474 MReleaseReadLock(&afs_bufferLock);
476 tfile = afs_CFileOpen(tb->fid[0]);
477 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
478 tb->data, AFS_BUFFER_PAGESIZE);
479 tb->dirty = 0; /* Clear the dirty flag */
480 afs_CFileClose(tfile);
483 MReleaseWriteLock(&tb->lock);
484 MObtainReadLock(&afs_bufferLock);
487 MReleaseReadLock(&afs_bufferLock);
490 char *DNew (fid,page)
494 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
495 register struct buffer *tb;
497 MObtainWriteLock(&afs_bufferLock,264);
498 if ((tb = afs_newslot(fid,page,NULL)) == 0) {
499 MReleaseWriteLock(&afs_bufferLock);
502 MObtainWriteLock(&tb->lock,265);
503 MReleaseWriteLock(&afs_bufferLock);
505 MReleaseWriteLock(&tb->lock);
509 void shutdown_bufferpackage() {
511 register struct buffer *tp;
514 extern int afs_cold_shutdown;
516 AFS_STATCNT(shutdown_bufferpackage);
517 /* Free all allocated Buffers and associated buffer pages */
519 if (afs_cold_shutdown) {
522 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
525 for (i=0; i < nbuffers; i+= NPB, tp += NPB) {
526 /* The following check shouldn't be necessary and it will be removed soon */
528 afs_warn("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
535 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
538 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
539 bzero((char *)&afs_bufferLock, sizeof(afs_lock_t));