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"
14 #include "afs/sysincludes.h"
15 #include "afsincludes.h"
20 #if defined(AFS_AIX31_ENV)
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"
32 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
34 #endif /* !defined(UKERNEL) */
37 #include "afs/volerrors.h"
38 #include "afs/exporter.h"
39 #include "afs/prs_fs.h"
40 #include "afs/afs_chunkops.h"
43 #include "afs/afs_stats.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)) & PHFIDMASK) \
64 | (page & PHPAGEMASK))
67 #undef dirty /* XXX */
70 static struct buffer *Buffers = 0;
71 static char *BufferData;
74 extern struct buf *geteblk();
77 #define timecounter afs_timecounter
79 /* The locks for individual buffer entries are now sometimes obtained while holding the
80 * afs_bufferLock. Thus we now have a locking hierarchy: afs_bufferLock -> Buffers[].lock.
82 static afs_lock_t afs_bufferLock;
83 static struct buffer *phTable[PHSIZE]; /* page hash table */
85 static afs_int32 timecounter;
87 /* Prototypes for static routines */
88 static struct buffer *afs_newslot(struct dcache *adc, afs_int32 apage,
89 register struct buffer *lp);
91 static int dinit_flag = 0;
95 /* Initialize the venus buffer system. */
97 register struct buffer *tb;
98 #if defined(AFS_USEBUFFERS)
99 struct buf *tub; /* unix buffer for allocation */
106 #if defined(AFS_USEBUFFERS)
107 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
108 abuffers = ((abuffers - 1) | (NPB - 1)) + 1;
110 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
112 (struct buffer *)afs_osi_Alloc(abuffers * sizeof(struct buffer));
113 #if !defined(AFS_USEBUFFERS)
114 BufferData = (char *)afs_osi_Alloc(abuffers * AFS_BUFFER_PAGESIZE);
117 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
118 for (i = 0; i < PHSIZE; i++)
120 for (i = 0; i < abuffers; i++) {
121 #if defined(AFS_USEBUFFERS)
122 if ((i & (NPB - 1)) == 0) {
123 /* time to allocate a fresh buffer */
124 tub = geteblk(AFS_BUFFER_PAGESIZE * NPB);
125 BufferData = (char *)tub->b_un.b_addr;
128 /* Fill in each buffer with an empty indication. */
131 afs_reset_inode(&tb->inode);
134 #if defined(AFS_USEBUFFERS)
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 AFS_RWLOCK_INIT(&tb->lock, "buffer lock");
151 DRead(register struct dcache *adc, register int page)
153 /* Read a page from the disk. */
154 register struct buffer *tb, *tb2;
155 struct osi_file *tfile;
159 MObtainWriteLock(&afs_bufferLock, 256);
161 #define bufmatch(tb) (tb->page == page && tb->fid == adc->index)
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(adc->index, 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);
180 register struct buffer **bufhead;
181 bufhead = &(phTable[pHash(adc->index, page)]);
182 while ((tb2 = tb->hashNext)) {
184 buf_Front(bufhead, tb, tb2);
185 MObtainWriteLock(&tb2->lock, 258);
186 ReleaseWriteLock(&afs_bufferLock);
188 tb2->accesstime = timecounter++;
189 AFS_STATS(afs_stats_cmperf.bufHits++);
190 MReleaseWriteLock(&tb2->lock);
193 if ((tb = tb2->hashNext)) {
195 buf_Front(bufhead, tb2, tb);
196 MObtainWriteLock(&tb->lock, 259);
197 ReleaseWriteLock(&afs_bufferLock);
199 tb->accesstime = timecounter++;
200 AFS_STATS(afs_stats_cmperf.bufHits++);
201 MReleaseWriteLock(&tb->lock);
211 AFS_STATS(afs_stats_cmperf.bufMisses++);
213 /* The last thing we looked at was either tb or tb2 (or nothing). That
214 * is at least the oldest buffer on one particular hash chain, so it's
215 * a pretty good place to start looking for the truly oldest buffer.
217 tb = afs_newslot(adc, page, (tb ? tb : tb2));
219 MReleaseWriteLock(&afs_bufferLock);
222 MObtainWriteLock(&tb->lock, 260);
223 MReleaseWriteLock(&afs_bufferLock);
225 if (page * AFS_BUFFER_PAGESIZE >= adc->f.chunkBytes) {
227 afs_reset_inode(&tb->inode);
229 MReleaseWriteLock(&tb->lock);
232 tfile = afs_CFileOpen(&adc->f.inode);
234 afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE, tb->data,
235 AFS_BUFFER_PAGESIZE);
236 afs_CFileClose(tfile);
237 if (code < AFS_BUFFER_PAGESIZE) {
239 afs_reset_inode(&tb->inode);
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);
251 FixupBucket(register struct buffer *ap)
253 register struct buffer **lp, *tp;
255 /* first try to get it out of its current hash bucket, in which it
257 AFS_STATCNT(FixupBucket);
260 for (tp = *lp; tp; tp = tp->hashNext) {
267 /* now figure the new hash bucket */
268 i = pHash(ap->fid, ap->page);
269 ap->hashIndex = i; /* remember where we are for deletion */
270 ap->hashNext = phTable[i]; /* add us to the list */
271 phTable[i] = ap; /* at the front, since it's LRU */
274 /* lp is pointer to a fairly-old buffer */
275 static struct buffer *
276 afs_newslot(struct dcache *adc, afs_int32 apage, register struct buffer *lp)
278 /* Find a usable buffer slot */
279 register afs_int32 i;
281 register struct buffer *tp;
282 struct osi_file *tfile;
284 AFS_STATCNT(afs_newslot);
285 /* we take a pointer here to a buffer which was at the end of an
286 * LRU hash chain. Odds are, it's one of the older buffers, not
287 * one of the newer. Having an older buffer to start with may
288 * permit us to avoid a few of the assignments in the "typical
289 * case" for loop below.
291 if (lp && (lp->lockers == 0)) {
298 /* timecounter might have wrapped, if machine is very very busy
299 * and stays up for a long time. Timecounter mustn't wrap twice
300 * (positive->negative->positive) before calling newslot, but that
301 * would require 2 billion consecutive cache hits... Anyway, the
302 * penalty is only that the cache replacement policy will be
303 * almost MRU for the next ~2 billion DReads... newslot doesn't
304 * get called nearly as often as DRead, so in order to avoid the
305 * performance penalty of using the hypers, it's worth doing the
306 * extra check here every time. It's probably cheaper than doing
307 * hcmp, anyway. There is a little performance hit resulting from
308 * resetting all the access times to 0, but it only happens once
309 * every month or so, and the access times will rapidly sort
310 * themselves back out after just a few more DReads.
312 if (timecounter < 0) {
315 for (i = 0; i < nbuffers; i++, tp++) {
317 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("afs: all buffers locked\n");
343 /* see DFlush for rationale for not getting and locking the dcache */
344 tfile = afs_CFileOpen(&lp->inode);
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 lp->fid = adc->index;
354 afs_copy_inode(&lp->inode, &adc->f.inode);
356 lp->accesstime = timecounter++;
357 FixupBucket(lp); /* move to the right hash bucket */
363 DRelease(void *loc, int flag)
365 /* Release a buffer, specifying whether or not the buffer has been
366 * modified by the locker. */
367 register struct buffer *bp = (struct buffer *)loc;
369 #if defined(AFS_USEBUFFERS)
370 register struct buffer *tp;
373 AFS_STATCNT(DRelease);
376 #if defined(AFS_USEBUFFERS)
377 /* look for buffer by scanning Unix buffers for appropriate address */
379 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
380 if ((afs_int32) bp >= (afs_int32) tp->data
382 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
383 /* we found the right range */
384 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
389 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
391 bp = &(Buffers[index]);
392 MObtainWriteLock(&bp->lock, 261);
396 MReleaseWriteLock(&bp->lock);
400 DVOffset(register void *ap)
402 /* Return the byte within a file represented by a buffer pointer. */
403 register struct buffer *bp;
405 #if defined(AFS_USEBUFFERS)
406 register struct buffer *tp;
408 AFS_STATCNT(DVOffset);
410 #if defined(AFS_USEBUFFERS)
411 /* look for buffer by scanning Unix buffers for appropriate address */
413 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
414 if ((afs_int32) bp >= (afs_int32) tp->data
416 (afs_int32) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
417 /* we found the right range */
418 index += ((afs_int32) bp - (afs_int32) tp->data) >> LOGPS;
423 index = (((char *)bp) - ((char *)BufferData)) >> LOGPS;
425 if (index < 0 || index >= nbuffers)
427 bp = &(Buffers[index]);
428 return AFS_BUFFER_PAGESIZE * bp->page + (int)(((char *)ap) - bp->data);
432 * Zap one dcache entry: destroy one FID's buffers.
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
440 * \param adc The dcache entry to be zapped.
443 DZap(struct dcache *adc)
446 /* Destroy all buffers pertaining to a particular fid. */
447 register struct buffer *tb;
450 MObtainReadLock(&afs_bufferLock);
452 for (i = 0; i <= PHPAGEMASK; i++)
453 for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
454 if (tb->fid == adc->index) {
455 MObtainWriteLock(&tb->lock, 262);
457 afs_reset_inode(&tb->inode);
459 MReleaseWriteLock(&tb->lock);
461 MReleaseReadLock(&afs_bufferLock);
465 DFlushBuffer(struct buffer *ab) {
466 struct osi_file *tfile;
468 tfile = afs_CFileOpen(&ab->inode);
469 afs_CFileWrite(tfile, ab->page * AFS_BUFFER_PAGESIZE,
470 ab->data, AFS_BUFFER_PAGESIZE);
471 ab->dirty = 0; /* Clear the dirty flag */
472 afs_CFileClose(tfile);
476 DFlushDCache(struct dcache *adc)
481 ObtainReadLock(&afs_bufferLock);
483 for (i = 0; i <= PHPAGEMASK; i++)
484 for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
485 if (tb->fid == adc->index) {
486 ObtainWriteLock(&tb->lock, 701);
488 ReleaseReadLock(&afs_bufferLock);
493 ReleaseWriteLock(&tb->lock);
494 ObtainReadLock(&afs_bufferLock);
497 ReleaseReadLock(&afs_bufferLock);
503 /* Flush all the modified buffers. */
505 register struct buffer *tb;
509 MObtainReadLock(&afs_bufferLock);
510 for (i = 0; i < nbuffers; i++, tb++) {
512 MObtainWriteLock(&tb->lock, 263);
514 MReleaseReadLock(&afs_bufferLock);
516 /* it seems safe to do this I/O without having the dcache
517 * locked, since the only things that will update the data in
518 * a directory are the buffer package, which holds the relevant
519 * tb->lock while doing the write, or afs_GetDCache, which
520 * DZap's the directory while holding the dcache lock.
521 * It is not possible to lock the dcache or even call
522 * afs_GetDSlot to map the index to the dcache since the dir
523 * package's caller has some dcache object locked already (so
524 * we cannot lock afs_xdcache). In addition, we cannot obtain
525 * a dcache lock while holding the tb->lock of the same file
526 * since that can deadlock with DRead/DNew */
530 MReleaseWriteLock(&tb->lock);
531 MObtainReadLock(&afs_bufferLock);
534 MReleaseReadLock(&afs_bufferLock);
538 DNew(register struct dcache *adc, register int page)
540 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
541 register struct buffer *tb;
543 MObtainWriteLock(&afs_bufferLock, 264);
544 if ((tb = afs_newslot(adc, page, NULL)) == 0) {
545 MReleaseWriteLock(&afs_bufferLock);
548 /* extend the chunk, if needed */
549 /* Do it now, not in DFlush or afs_newslot when the data is written out,
550 * since now our caller has adc->lock writelocked, and we can't acquire
551 * that lock (or even map from a fid to a dcache) in afs_newslot or
552 * DFlush due to lock hierarchy issues */
553 if ((page + 1) * AFS_BUFFER_PAGESIZE > adc->f.chunkBytes) {
554 afs_AdjustSize(adc, (page + 1) * AFS_BUFFER_PAGESIZE);
555 afs_WriteDCache(adc, 1);
557 MObtainWriteLock(&tb->lock, 265);
558 MReleaseWriteLock(&afs_bufferLock);
560 MReleaseWriteLock(&tb->lock);
565 shutdown_bufferpackage(void)
567 #if defined(AFS_USEBUFFERS)
568 register struct buffer *tp;
572 AFS_STATCNT(shutdown_bufferpackage);
573 /* Free all allocated Buffers and associated buffer pages */
575 if (afs_cold_shutdown) {
577 #if !defined(AFS_USEBUFFERS)
578 afs_osi_Free(BufferData, nbuffers * AFS_BUFFER_PAGESIZE);
581 for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
582 /* The following check shouldn't be necessary and it will be removed soon */
585 ("afs: shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
592 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
595 for (i = 0; i < PHSIZE; i++)
597 memset(&afs_bufferLock, 0, sizeof(afs_lock_t));