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"
16 #include "afs/sysincludes.h"
17 #include "afsincludes.h"
22 #if defined(AFS_AIX31_ENV) || defined(AFS_DEC_ENV)
25 #if !defined(AFS_AIX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_LINUX20_ENV)
26 #include "h/kernel.h" /* Doesn't needed, so it should go */
28 #endif /* !defined(UKERNEL) */
30 #include "afs/afs_osi.h"
34 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
36 #endif /* !defined(UKERNEL) */
39 #include "afs/volerrors.h"
40 #include "afs/exporter.h"
41 #include "afs/prs_fs.h"
42 #include "afs/afs_chunkops.h"
45 #include "afs/afs_stats.h"
46 #include "afs/longc_procs.h"
50 #define BUF_TIME_MAX 0x7fffffff
52 /* number of pages per Unix buffer, when we're using Unix buffer pool */
55 #define AFS_BUFFER_PAGESIZE 2048
58 /* If you change any of this PH stuff, make sure you don't break DZap() */
59 /* use last two bits for page */
61 /* use next five bits for fid */
63 /* page hash table size - this is pretty intertwined with pHash */
64 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
66 #define pHash(fid,page) ((((afs_int32)((fid)[0])) & PHFIDMASK) \
67 | (page & PHPAGEMASK))
70 #undef dirty /* XXX */
73 static struct buffer *Buffers = 0;
74 static char *BufferData;
77 extern struct buf *geteblk();
80 #define timecounter afs_timecounter
82 /* The locks for individual buffer entries are now sometimes obtained while holding the
83 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
85 static afs_lock_t afs_bufferLock;
86 static struct buffer *phTable[PHSIZE]; /* page hash table */
88 static afs_int32 timecounter;
90 /* Prototypes for static routines */
91 static struct buffer *afs_newslot(afs_inode_t * afid, afs_int32 apage,
92 register struct buffer *lp);
94 static int dinit_flag = 0;
98 /* Initialize the venus buffer system. */
100 register struct buffer *tb;
102 struct buf *tub; /* unix buffer for allocation */
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");
115 (struct buffer *)afs_osi_Alloc(abuffers * sizeof(struct buffer));
117 BufferData = (char *)afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
120 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
121 for (i = 0; i < PHSIZE; i++)
123 for (i = 0; i < abuffers; i++) {
125 if ((i & (NPB - 1)) == 0) {
126 /* time to allocate a fresh buffer */
127 tub = geteblk(AFS_BUFFER_PAGESIZE * NPB);
128 BufferData = (char *)tub->b_un.b_addr;
131 /* Fill in each buffer with an empty indication. */
137 if ((i & (NPB - 1)) == 0)
141 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i & (NPB - 1))];
143 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * i];
147 RWLOCK_INIT(&tb->lock, "buffer lock");
153 DRead(register afs_inode_t * fid, register int page)
155 /* Read a page from the disk. */
156 register struct buffer *tb, *tb2;
157 struct osi_file *tfile;
161 MObtainWriteLock(&afs_bufferLock, 256);
163 #define bufmatch(tb) (tb->page == page && dirp_Eq(tb->fid, fid))
164 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
166 /* this apparently-complicated-looking code is simply an example of
167 * a little bit of loop unrolling, and is a standard linked-list
168 * traversal trick. It saves a few assignments at the the expense
169 * of larger code size. This could be simplified by better use of
172 if ((tb = phTable[pHash(fid, page)])) {
174 MObtainWriteLock(&tb->lock, 257);
175 ReleaseWriteLock(&afs_bufferLock);
177 tb->accesstime = timecounter++;
178 AFS_STATS(afs_stats_cmperf.bufHits++);
179 MReleaseWriteLock(&tb->lock);
182 register struct buffer **bufhead;
183 bufhead = &(phTable[pHash(fid, page)]);
184 while ((tb2 = tb->hashNext)) {
186 buf_Front(bufhead, tb, tb2);
187 MObtainWriteLock(&tb2->lock, 258);
188 ReleaseWriteLock(&afs_bufferLock);
190 tb2->accesstime = timecounter++;
191 AFS_STATS(afs_stats_cmperf.bufHits++);
192 MReleaseWriteLock(&tb2->lock);
195 if ((tb = tb2->hashNext)) {
197 buf_Front(bufhead, tb2, tb);
198 MObtainWriteLock(&tb->lock, 259);
199 ReleaseWriteLock(&afs_bufferLock);
201 tb->accesstime = timecounter++;
202 AFS_STATS(afs_stats_cmperf.bufHits++);
203 MReleaseWriteLock(&tb->lock);
213 AFS_STATS(afs_stats_cmperf.bufMisses++);
215 /* The last thing we looked at was either tb or tb2 (or nothing). That
216 * is at least the oldest buffer on one particular hash chain, so it's
217 * a pretty good place to start looking for the truly oldest buffer.
219 tb = afs_newslot(fid, page, (tb ? tb : tb2));
221 MReleaseWriteLock(&afs_bufferLock);
224 MObtainWriteLock(&tb->lock, 260);
225 MReleaseWriteLock(&afs_bufferLock);
227 tfile = afs_CFileOpen(fid[0]);
228 if (page * AFS_BUFFER_PAGESIZE >= tfile->size) {
231 MReleaseWriteLock(&tb->lock);
232 afs_CFileClose(tfile);
236 afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE, tb->data,
237 AFS_BUFFER_PAGESIZE);
238 afs_CFileClose(tfile);
239 if (code < AFS_BUFFER_PAGESIZE) {
242 MReleaseWriteLock(&tb->lock);
245 /* Note that findslot sets the page field in the buffer equal to
246 * what it is searching for. */
247 MReleaseWriteLock(&tb->lock);
252 FixupBucket(register struct buffer *ap)
254 register struct buffer **lp, *tp;
256 /* first try to get it out of its current hash bucket, in which it
258 AFS_STATCNT(FixupBucket);
261 for (tp = *lp; tp; tp = tp->hashNext) {
268 /* now figure the new hash bucket */
269 i = pHash(ap->fid, ap->page);
270 ap->hashIndex = i; /* remember where we are for deletion */
271 ap->hashNext = phTable[i]; /* add us to the list */
272 phTable[i] = ap; /* at the front, since it's LRU */
275 /* lp is pointer to a fairly-old buffer */
276 static struct buffer *
277 afs_newslot(afs_inode_t * afid, afs_int32 apage, register struct buffer *lp)
279 /* Find a usable buffer slot */
280 register afs_int32 i;
282 register struct buffer *tp;
283 struct osi_file *tfile;
285 AFS_STATCNT(afs_newslot);
286 /* we take a pointer here to a buffer which was at the end of an
287 * LRU hash chain. Odds are, it's one of the older buffers, not
288 * one of the newer. Having an older buffer to start with may
289 * permit us to avoid a few of the assignments in the "typical
290 * case" for loop below.
292 if (lp && (lp->lockers == 0)) {
299 /* timecounter might have wrapped, if machine is very very busy
300 * and stays up for a long time. Timecounter mustn't wrap twice
301 * (positive->negative->positive) before calling newslot, but that
302 * would require 2 billion consecutive cache hits... Anyway, the
303 * penalty is only that the cache replacement policy will be
304 * almost MRU for the next ~2 billion DReads... newslot doesn't
305 * get called nearly as often as DRead, so in order to avoid the
306 * performance penalty of using the hypers, it's worth doing the
307 * extra check here every time. It's probably cheaper than doing
308 * hcmp, anyway. There is a little performance hit resulting from
309 * resetting all the access times to 0, but it only happens once
310 * every month or so, and the access times will rapidly sort
311 * themselves back out after just a few more DReads.
313 if (timecounter < 0) {
316 for (i = 0; i < nbuffers; i++, tp++) {
318 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
322 /* this is the typical case */
324 for (i = 0; i < nbuffers; i++, tp++) {
325 if (tp->lockers == 0) {
326 if (tp->accesstime < lt) {
335 /* There are no unlocked buffers -- this used to panic, but that
336 * seems extreme. To the best of my knowledge, all the callers
337 * of DRead are prepared to handle a zero return. Some of them
338 * just panic directly, but not all of them. */
339 afs_warn("all buffers locked");
344 tfile = afs_CFileOpen(lp->fid[0]);
345 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE, lp->data,
346 AFS_BUFFER_PAGESIZE);
348 afs_CFileClose(tfile);
349 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
352 /* Now fill in the header. */
353 dirp_Cpy(lp->fid, afid); /* set this */
355 lp->accesstime = timecounter++;
356 FixupBucket(lp); /* move to the right hash bucket */
362 DRelease(register struct buffer *bp, int flag)
364 /* Release a buffer, specifying whether or not the buffer has been
365 * modified by the locker. */
368 register struct buffer *tp;
371 AFS_STATCNT(DRelease);
375 /* look for buffer by scanning Unix buffers for appropriate address */
377 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
378 if ((afs_int32) bp >= (afs_int32) tp->data
380 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
381 /* we found the right range */
382 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
387 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
389 bp = &(Buffers[index]);
390 MObtainWriteLock(&bp->lock, 261);
394 MReleaseWriteLock(&bp->lock);
398 DVOffset(register void *ap)
400 /* Return the byte within a file represented by a buffer pointer. */
401 register struct buffer *bp;
404 register struct buffer *tp;
406 AFS_STATCNT(DVOffset);
409 /* look for buffer by scanning Unix buffers for appropriate address */
411 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
412 if ((afs_int32) bp >= (afs_int32) tp->data
414 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
415 /* we found the right range */
416 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
421 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
423 if (index < 0 || index >= nbuffers)
425 bp = &(Buffers[index]);
426 return AFS_BUFFER_PAGESIZE * bp->page + (int)(((char *)ap) - bp->data);
429 /* 1/1/91 - I've modified the hash function to take the page as well
430 * as the *fid, so that lookup will be a bit faster. That presents some
431 * difficulties for Zap, which now has to have some knowledge of the nature
432 * of the hash function. Oh well. This should use the list traversal
436 DZap(afs_inode_t * fid)
439 /* Destroy all buffers pertaining to a particular fid. */
440 register struct buffer *tb;
443 MObtainReadLock(&afs_bufferLock);
445 for (i = 0; i <= PHPAGEMASK; i++)
446 for (tb = phTable[pHash(fid, i)]; tb; tb = tb->hashNext)
447 if (dirp_Eq(tb->fid, fid)) {
448 MObtainWriteLock(&tb->lock, 262);
451 MReleaseWriteLock(&tb->lock);
453 MReleaseReadLock(&afs_bufferLock);
459 /* Flush all the modified buffers. */
461 register struct buffer *tb;
462 struct osi_file *tfile;
466 MObtainReadLock(&afs_bufferLock);
467 for (i = 0; i < nbuffers; i++, tb++) {
469 MObtainWriteLock(&tb->lock, 263);
471 MReleaseReadLock(&afs_bufferLock);
473 tfile = afs_CFileOpen(tb->fid[0]);
474 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
475 tb->data, AFS_BUFFER_PAGESIZE);
476 tb->dirty = 0; /* Clear the dirty flag */
477 afs_CFileClose(tfile);
480 MReleaseWriteLock(&tb->lock);
481 MObtainReadLock(&afs_bufferLock);
484 MReleaseReadLock(&afs_bufferLock);
488 DNew(register afs_inode_t * fid, register int 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);
506 shutdown_bufferpackage(void)
509 register struct buffer *tp;
512 extern int afs_cold_shutdown;
514 AFS_STATCNT(shutdown_bufferpackage);
515 /* Free all allocated Buffers and associated buffer pages */
517 if (afs_cold_shutdown) {
520 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
523 for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
524 /* The following check shouldn't be necessary and it will be removed soon */
527 ("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
534 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
537 for (i = 0; i < PHSIZE; i++)
539 memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));