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 #include "../afs/stds.h"
38 #include "../afs/volerrors.h"
39 #include "../afs/exporter.h"
40 #include "../afs/prs_fs.h"
41 #include "../afs/afs_chunkops.h"
42 #include "../afs/dir.h"
44 #include "../afs/afs_stats.h"
45 #include "../afs/longc_procs.h"
46 #include "../afs/afs.h"
49 #define BUF_TIME_MAX 0x7fffffff
51 /* number of pages per Unix buffer, when we're using Unix buffer pool */
54 #define AFS_BUFFER_PAGESIZE 2048
57 /* If you change any of this PH stuff, make sure you don't break DZap() */
58 /* use last two bits for page */
60 /* use next five bits for fid */
62 /* page hash table size - this is pretty intertwined with pHash */
63 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
65 #define pHash(fid,page) ((((afs_int32)((fid)[0])) & PHFIDMASK) \
66 | (page & PHPAGEMASK))
69 #undef dirty /* XXX */
72 static struct buffer *Buffers = 0;
73 static char *BufferData;
76 extern struct buf *geteblk();
79 #define timecounter afs_timecounter
81 /* The locks for individual buffer entries are now sometimes obtained while holding the
82 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
84 static afs_lock_t afs_bufferLock;
85 static struct buffer *phTable[PHSIZE]; /* page hash table */
87 static afs_int32 timecounter;
89 /* Prototypes for static routines */
90 static struct buffer *afs_newslot (afs_inode_t *afid, afs_int32 apage,register struct buffer *lp);
92 static int dinit_flag = 0;
93 void DInit (int abuffers)
95 /* Initialize the venus buffer system. */
97 register struct buffer *tb;
99 struct buf *tub; /* unix buffer for allocation */
103 if (dinit_flag) return;
106 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
107 abuffers = ((abuffers-1) | (NPB-1)) + 1;
109 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
110 Buffers = (struct buffer *) afs_osi_Alloc(abuffers * sizeof(struct buffer));
112 BufferData = (char *) afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
115 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
116 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
117 for (i=0;i<abuffers;i++) {
119 if ((i & (NPB-1)) == 0) {
120 /* time to allocate a fresh buffer */
121 tub = geteblk(AFS_BUFFER_PAGESIZE*NPB);
122 BufferData = (char *) tub->b_un.b_addr;
125 /* Fill in each buffer with an empty indication. */
131 if ((i & (NPB-1)) == 0)
135 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i&(NPB-1))];
137 tb->data = &BufferData[AFS_BUFFER_PAGESIZE*i];
141 RWLOCK_INIT(&tb->lock, "buffer lock");
146 char *DRead(register afs_inode_t *fid, register int page)
148 /* Read a page from the disk. */
149 register struct buffer *tb, *tb2;
151 register afs_int32 code, *sizep;
154 MObtainWriteLock(&afs_bufferLock,256);
156 /* some new code added 1/1/92 */
157 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
158 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
160 /* this apparently-complicated-looking code is simply an example of
161 * a little bit of loop unrolling, and is a standard linked-list
162 * traversal trick. It saves a few assignments at the the expense
163 * of larger code size. This could be simplified by better use of
166 if ((tb = phTable[pHash(fid,page)])) {
168 MObtainWriteLock(&tb->lock,257);
169 ReleaseWriteLock(&afs_bufferLock);
171 tb->accesstime = timecounter++;
172 AFS_STATS(afs_stats_cmperf.bufHits++);
173 MReleaseWriteLock(&tb->lock);
177 register struct buffer **bufhead;
178 bufhead = &( phTable[pHash(fid,page)] );
179 while ((tb2 = tb->hashNext)) {
181 buf_Front(bufhead,tb,tb2);
182 MObtainWriteLock(&tb2->lock,258);
183 ReleaseWriteLock(&afs_bufferLock);
185 tb2->accesstime = timecounter++;
186 AFS_STATS(afs_stats_cmperf.bufHits++);
187 MReleaseWriteLock(&tb2->lock);
190 if ((tb = tb2->hashNext)) {
192 buf_Front(bufhead,tb2,tb);
193 MObtainWriteLock(&tb->lock,259);
194 ReleaseWriteLock(&afs_bufferLock);
196 tb->accesstime = timecounter++;
197 AFS_STATS(afs_stats_cmperf.bufHits++);
198 MReleaseWriteLock(&tb->lock);
208 AFS_STATS(afs_stats_cmperf.bufMisses++);
210 /* The last thing we looked at was either tb or tb2 (or nothing). That
211 * is at least the oldest buffer on one particular hash chain, so it's
212 * a pretty good place to start looking for the truly oldest buffer.
214 tb = afs_newslot(fid, page, (tb ? tb : tb2));
216 MReleaseWriteLock(&afs_bufferLock);
219 MObtainWriteLock(&tb->lock,260);
220 MReleaseWriteLock(&afs_bufferLock);
222 tfile = afs_CFileOpen(fid[0]);
223 sizep = (afs_int32 *)tfile;
224 if (page * AFS_BUFFER_PAGESIZE >= *sizep) {
227 MReleaseWriteLock(&tb->lock);
228 afs_CFileClose(tfile);
231 code = afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE,
232 tb->data, AFS_BUFFER_PAGESIZE);
233 afs_CFileClose(tfile);
234 if (code < AFS_BUFFER_PAGESIZE) {
237 MReleaseWriteLock(&tb->lock);
240 /* Note that findslot sets the page field in the buffer equal to
241 * what it is searching for. */
242 MReleaseWriteLock(&tb->lock);
246 static void FixupBucket(register struct buffer *ap)
248 register struct buffer **lp, *tp;
250 /* first try to get it out of its current hash bucket, in which it
252 AFS_STATCNT(FixupBucket);
255 for(tp = *lp; tp; tp=tp->hashNext) {
262 /* now figure the new hash bucket */
263 i = pHash(ap->fid,ap->page);
264 ap->hashIndex = i; /* remember where we are for deletion */
265 ap->hashNext = phTable[i]; /* add us to the list */
266 phTable[i] = ap; /* at the front, since it's LRU */
269 /* lp is pointer to a fairly-old buffer */
270 static struct buffer *afs_newslot (afs_inode_t *afid, afs_int32 apage,register struct buffer *lp)
272 /* Find a usable buffer slot */
273 register afs_int32 i;
275 register struct buffer *tp;
278 AFS_STATCNT(afs_newslot);
279 /* we take a pointer here to a buffer which was at the end of an
280 * LRU hash chain. Odds are, it's one of the older buffers, not
281 * one of the newer. Having an older buffer to start with may
282 * permit us to avoid a few of the assignments in the "typical
283 * case" for loop below.
285 if (lp && (lp->lockers == 0)) {
293 /* timecounter might have wrapped, if machine is very very busy
294 * and stays up for a long time. Timecounter mustn't wrap twice
295 * (positive->negative->positive) before calling newslot, but that
296 * would require 2 billion consecutive cache hits... Anyway, the
297 * penalty is only that the cache replacement policy will be
298 * almost MRU for the next ~2 billion DReads... newslot doesn't
299 * get called nearly as often as DRead, so in order to avoid the
300 * performance penalty of using the hypers, it's worth doing the
301 * extra check here every time. It's probably cheaper than doing
302 * hcmp, anyway. There is a little performance hit resulting from
303 * resetting all the access times to 0, but it only happens once
304 * every month or so, and the access times will rapidly sort
305 * themselves back out after just a few more DReads.
307 if (timecounter < 0) {
310 for (i=0;i<nbuffers;i++,tp++) {
312 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
317 /* this is the typical case */
319 for (i=0;i<nbuffers;i++,tp++) {
320 if (tp->lockers == 0) {
321 if (tp->accesstime < lt) {
330 /* There are no unlocked buffers -- this used to panic, but that
331 * seems extreme. To the best of my knowledge, all the callers
332 * of DRead are prepared to handle a zero return. Some of them
333 * just panic directly, but not all of them. */
334 afs_warn ("all buffers locked");
339 tfile = afs_CFileOpen(lp->fid[0]);
340 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE,
341 lp->data, AFS_BUFFER_PAGESIZE);
343 afs_CFileClose(tfile);
344 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
347 /* Now fill in the header. */
348 dirp_Cpy(lp->fid, afid); /* set this */
350 lp->accesstime = timecounter++;
351 FixupBucket(lp); /* move to the right hash bucket */
356 void DRelease (register struct buffer *bp, int flag)
358 /* Release a buffer, specifying whether or not the buffer has been
359 * modified by the locker. */
362 register struct buffer *tp;
365 AFS_STATCNT(DRelease);
368 /* look for buffer by scanning Unix buffers for appropriate address */
370 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
371 if ((afs_int32)bp >= (afs_int32)tp->data
372 && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
373 /* we found the right range */
374 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
379 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
381 bp = &(Buffers[index]);
382 MObtainWriteLock(&bp->lock,261);
384 if (flag) bp->dirty=1;
385 MReleaseWriteLock(&bp->lock);
388 int DVOffset (register void *ap)
390 /* Return the byte within a file represented by a buffer pointer. */
391 register struct buffer *bp;
394 register struct buffer *tp;
396 AFS_STATCNT(DVOffset);
399 /* look for buffer by scanning Unix buffers for appropriate address */
401 for(index = 0; index < nbuffers; index += NPB, tp += NPB) {
402 if ((afs_int32)bp >= (afs_int32)tp->data && (afs_int32)bp < (afs_int32)tp->data + AFS_BUFFER_PAGESIZE*NPB) {
403 /* we found the right range */
404 index += ((afs_int32)bp - (afs_int32)tp->data) >> LOGPS;
409 index = (((char *)bp)-((char *)BufferData))>>LOGPS;
411 if (index<0 || index >= nbuffers) return -1;
412 bp = &(Buffers[index]);
413 return AFS_BUFFER_PAGESIZE*bp->page+(int)(((char *)ap)-bp->data);
416 /* 1/1/91 - I've modified the hash function to take the page as well
417 * as the *fid, so that lookup will be a bit faster. That presents some
418 * difficulties for Zap, which now has to have some knowledge of the nature
419 * of the hash function. Oh well. This should use the list traversal
422 void DZap (afs_inode_t *fid)
425 /* Destroy all buffers pertaining to a particular fid. */
426 register struct buffer *tb;
429 MObtainReadLock(&afs_bufferLock);
431 for (i=0;i<=PHPAGEMASK;i++)
432 for(tb=phTable[pHash(fid,i)]; tb; tb=tb->hashNext)
433 if (dirp_Eq(tb->fid,fid)) {
434 MObtainWriteLock(&tb->lock,262);
437 MReleaseWriteLock(&tb->lock);
439 MReleaseReadLock(&afs_bufferLock);
444 /* Flush all the modified buffers. */
446 register struct buffer *tb;
451 MObtainReadLock(&afs_bufferLock);
452 for(i=0;i<nbuffers;i++,tb++) {
454 MObtainWriteLock(&tb->lock,263);
456 MReleaseReadLock(&afs_bufferLock);
458 tfile = afs_CFileOpen(tb->fid[0]);
459 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
460 tb->data, AFS_BUFFER_PAGESIZE);
461 tb->dirty = 0; /* Clear the dirty flag */
462 afs_CFileClose(tfile);
465 MReleaseWriteLock(&tb->lock);
466 MObtainReadLock(&afs_bufferLock);
469 MReleaseReadLock(&afs_bufferLock);
472 char *DNew (register afs_inode_t *fid, register int page)
474 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
475 register struct buffer *tb;
477 MObtainWriteLock(&afs_bufferLock,264);
478 if ((tb = afs_newslot(fid,page,NULL)) == 0) {
479 MReleaseWriteLock(&afs_bufferLock);
482 MObtainWriteLock(&tb->lock,265);
483 MReleaseWriteLock(&afs_bufferLock);
485 MReleaseWriteLock(&tb->lock);
489 void shutdown_bufferpackage(void)
492 register struct buffer *tp;
495 extern int afs_cold_shutdown;
497 AFS_STATCNT(shutdown_bufferpackage);
498 /* Free all allocated Buffers and associated buffer pages */
500 if (afs_cold_shutdown) {
503 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
506 for (i=0; i < nbuffers; i+= NPB, tp += NPB) {
507 /* The following check shouldn't be necessary and it will be removed soon */
509 afs_warn("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
516 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
519 for(i=0;i<PHSIZE;i++) phTable[i] = 0;
520 memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));