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)
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"
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)) & 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(struct dcache *adc, afs_int32 apage,
91 register struct buffer *lp);
93 static int dinit_flag = 0;
97 /* Initialize the venus buffer system. */
99 register struct buffer *tb;
100 #if defined(AFS_USEBUFFERS)
101 struct buf *tub; /* unix buffer for allocation */
108 #if defined(AFS_USEBUFFERS)
109 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
110 abuffers = ((abuffers - 1) | (NPB - 1)) + 1;
112 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
114 (struct buffer *)afs_osi_Alloc(abuffers * sizeof(struct buffer));
115 #if !defined(AFS_USEBUFFERS)
116 BufferData = (char *)afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
119 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
120 for (i = 0; i < PHSIZE; i++)
122 for (i = 0; i < abuffers; i++) {
123 #if defined(AFS_USEBUFFERS)
124 if ((i & (NPB - 1)) == 0) {
125 /* time to allocate a fresh buffer */
126 tub = geteblk(AFS_BUFFER_PAGESIZE * NPB);
127 BufferData = (char *)tub->b_un.b_addr;
130 /* Fill in each buffer with an empty indication. */
136 #if defined(AFS_USEBUFFERS)
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 AFS_RWLOCK_INIT(&tb->lock, "buffer lock");
153 DRead(register struct dcache *adc, 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 && tb->fid == adc->index)
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(adc->index, 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(adc->index, 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(adc, page, (tb ? tb : tb2));
221 MReleaseWriteLock(&afs_bufferLock);
224 MObtainWriteLock(&tb->lock, 260);
225 MReleaseWriteLock(&afs_bufferLock);
227 if (page * AFS_BUFFER_PAGESIZE >= adc->f.chunkBytes) {
231 MReleaseWriteLock(&tb->lock);
234 tfile = afs_CFileOpen(adc->f.inode);
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) {
243 MReleaseWriteLock(&tb->lock);
246 /* Note that findslot sets the page field in the buffer equal to
247 * what it is searching for. */
248 MReleaseWriteLock(&tb->lock);
253 FixupBucket(register struct buffer *ap)
255 register struct buffer **lp, *tp;
257 /* first try to get it out of its current hash bucket, in which it
259 AFS_STATCNT(FixupBucket);
262 for (tp = *lp; tp; tp = tp->hashNext) {
269 /* now figure the new hash bucket */
270 i = pHash(ap->fid, ap->page);
271 ap->hashIndex = i; /* remember where we are for deletion */
272 ap->hashNext = phTable[i]; /* add us to the list */
273 phTable[i] = ap; /* at the front, since it's LRU */
276 /* lp is pointer to a fairly-old buffer */
277 static struct buffer *
278 afs_newslot(struct dcache *adc, afs_int32 apage, register struct buffer *lp)
280 /* Find a usable buffer slot */
281 register afs_int32 i;
283 register struct buffer *tp;
284 struct osi_file *tfile;
286 AFS_STATCNT(afs_newslot);
287 /* we take a pointer here to a buffer which was at the end of an
288 * LRU hash chain. Odds are, it's one of the older buffers, not
289 * one of the newer. Having an older buffer to start with may
290 * permit us to avoid a few of the assignments in the "typical
291 * case" for loop below.
293 if (lp && (lp->lockers == 0)) {
300 /* timecounter might have wrapped, if machine is very very busy
301 * and stays up for a long time. Timecounter mustn't wrap twice
302 * (positive->negative->positive) before calling newslot, but that
303 * would require 2 billion consecutive cache hits... Anyway, the
304 * penalty is only that the cache replacement policy will be
305 * almost MRU for the next ~2 billion DReads... newslot doesn't
306 * get called nearly as often as DRead, so in order to avoid the
307 * performance penalty of using the hypers, it's worth doing the
308 * extra check here every time. It's probably cheaper than doing
309 * hcmp, anyway. There is a little performance hit resulting from
310 * resetting all the access times to 0, but it only happens once
311 * every month or so, and the access times will rapidly sort
312 * themselves back out after just a few more DReads.
314 if (timecounter < 0) {
317 for (i = 0; i < nbuffers; i++, tp++) {
319 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
323 /* this is the typical case */
325 for (i = 0; i < nbuffers; i++, tp++) {
326 if (tp->lockers == 0) {
327 if (tp->accesstime < lt) {
336 /* There are no unlocked buffers -- this used to panic, but that
337 * seems extreme. To the best of my knowledge, all the callers
338 * of DRead are prepared to handle a zero return. Some of them
339 * just panic directly, but not all of them. */
340 afs_warn("all buffers locked");
345 /* see DFlush for rationale for not getting and locking the dcache */
346 tfile = afs_CFileOpen(lp->inode);
347 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE, lp->data,
348 AFS_BUFFER_PAGESIZE);
350 afs_CFileClose(tfile);
351 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
354 /* Now fill in the header. */
355 lp->fid = adc->index;
356 lp->inode = adc->f.inode;
358 lp->accesstime = timecounter++;
359 FixupBucket(lp); /* move to the right hash bucket */
365 DRelease(register struct buffer *bp, int flag)
367 /* Release a buffer, specifying whether or not the buffer has been
368 * modified by the locker. */
370 #if defined(AFS_USEBUFFERS)
371 register struct buffer *tp;
374 AFS_STATCNT(DRelease);
377 #if defined(AFS_USEBUFFERS)
378 /* look for buffer by scanning Unix buffers for appropriate address */
380 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
381 if ((afs_int32) bp >= (afs_int32) tp->data
383 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
384 /* we found the right range */
385 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
390 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
392 bp = &(Buffers[index]);
393 MObtainWriteLock(&bp->lock, 261);
397 MReleaseWriteLock(&bp->lock);
401 DVOffset(register void *ap)
403 /* Return the byte within a file represented by a buffer pointer. */
404 register struct buffer *bp;
406 #if defined(AFS_USEBUFFERS)
407 register struct buffer *tp;
409 AFS_STATCNT(DVOffset);
411 #if defined(AFS_USEBUFFERS)
412 /* look for buffer by scanning Unix buffers for appropriate address */
414 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
415 if ((afs_int32) bp >= (afs_int32) tp->data
417 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
418 /* we found the right range */
419 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
424 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
426 if (index < 0 || index >= nbuffers)
428 bp = &(Buffers[index]);
429 return AFS_BUFFER_PAGESIZE * bp->page + (int)(((char *)ap) - bp->data);
433 * Zap one dcache entry: destroy one FID's buffers.
435 * 1/1/91 - I've modified the hash function to take the page as well
436 * as the *fid, so that lookup will be a bit faster. That presents some
437 * difficulties for Zap, which now has to have some knowledge of the nature
438 * of the hash function. Oh well. This should use the list traversal
441 * \param adc The dcache entry to be zapped.
444 DZap(struct dcache *adc)
447 /* Destroy all buffers pertaining to a particular fid. */
448 register struct buffer *tb;
451 MObtainReadLock(&afs_bufferLock);
453 for (i = 0; i <= PHPAGEMASK; i++)
454 for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
455 if (tb->fid == adc->index) {
456 MObtainWriteLock(&tb->lock, 262);
460 MReleaseWriteLock(&tb->lock);
462 MReleaseReadLock(&afs_bufferLock);
468 /* Flush all the modified buffers. */
470 register struct buffer *tb;
471 struct osi_file *tfile;
475 MObtainReadLock(&afs_bufferLock);
476 for (i = 0; i < nbuffers; i++, tb++) {
478 MObtainWriteLock(&tb->lock, 263);
480 MReleaseReadLock(&afs_bufferLock);
482 /* it seems safe to do this I/O without having the dcache
483 * locked, since the only things that will update the data in
484 * a directory are the buffer package, which holds the relevant
485 * tb->lock while doing the write, or afs_GetDCache, which
486 * DZap's the directory while holding the dcache lock.
487 * It is not possible to lock the dcache or even call
488 * afs_GetDSlot to map the index to the dcache since the dir
489 * package's caller has some dcache object locked already (so
490 * we cannot lock afs_xdcache). In addition, we cannot obtain
491 * a dcache lock while holding the tb->lock of the same file
492 * since that can deadlock with DRead/DNew */
493 tfile = afs_CFileOpen(tb->inode);
494 afs_CFileWrite(tfile, tb->page * AFS_BUFFER_PAGESIZE,
495 tb->data, AFS_BUFFER_PAGESIZE);
496 tb->dirty = 0; /* Clear the dirty flag */
497 afs_CFileClose(tfile);
500 MReleaseWriteLock(&tb->lock);
501 MObtainReadLock(&afs_bufferLock);
504 MReleaseReadLock(&afs_bufferLock);
508 DNew(register struct dcache *adc, register int page)
510 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
511 register struct buffer *tb;
513 MObtainWriteLock(&afs_bufferLock, 264);
514 if ((tb = afs_newslot(adc, page, NULL)) == 0) {
515 MReleaseWriteLock(&afs_bufferLock);
518 /* extend the chunk, if needed */
519 /* Do it now, not in DFlush or afs_newslot when the data is written out,
520 * since now our caller has adc->lock writelocked, and we can't acquire
521 * that lock (or even map from a fid to a dcache) in afs_newslot or
522 * DFlush due to lock hierarchy issues */
523 if ((page + 1) * AFS_BUFFER_PAGESIZE > adc->f.chunkBytes) {
524 afs_AdjustSize(adc, (page + 1) * AFS_BUFFER_PAGESIZE);
525 afs_WriteDCache(adc, 1);
527 MObtainWriteLock(&tb->lock, 265);
528 MReleaseWriteLock(&afs_bufferLock);
530 MReleaseWriteLock(&tb->lock);
535 shutdown_bufferpackage(void)
537 #if defined(AFS_USEBUFFERS)
538 register struct buffer *tp;
542 AFS_STATCNT(shutdown_bufferpackage);
543 /* Free all allocated Buffers and associated buffer pages */
545 if (afs_cold_shutdown) {
547 #if !defined(AFS_USEBUFFERS)
548 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
551 for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
552 /* The following check shouldn't be necessary and it will be removed soon */
555 ("shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
562 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
565 for (i = 0; i < PHSIZE; i++)
567 memset((char *)&afs_bufferLock, 0, sizeof(afs_lock_t));