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 #if defined(AFS_USEBUFFERS)
50 /* number of pages per Unix buffer, when we're using Unix buffer pool */
53 #define NPB 8 /* must be a pwer of 2 */
55 static int afs_max_buffers; /* should be an integral multiple of NPB */
58 #define AFS_BUFFER_PAGESIZE 2048
61 /* If you change any of this PH stuff, make sure you don't break DZap() */
62 /* use last two bits for page */
64 /* use next five bits for fid */
66 /* page hash table size - this is pretty intertwined with pHash */
67 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
69 #define pHash(fid,page) ((((afs_int32)(fid)) & PHFIDMASK) \
70 | (page & PHPAGEMASK))
73 #undef dirty /* XXX */
76 static struct buffer *Buffers = 0;
77 static char *BufferData;
80 extern struct buf *geteblk();
83 #define timecounter afs_timecounter
86 /* A note on locking in 'struct buffer'
88 * afs_bufferLock protects the hash chain, and the 'lockers' field where that
89 * has a zero value. It must be held whenever lockers is incremented from zero.
91 * The individual buffer lock protects the contents of the structure, including
94 * For safety: afs_bufferLock and the individual buffer lock must be held
95 * when obtaining a reference on a structure. Only the individual buffer lock
96 * need be held when releasing a reference.
98 * The locking hierarchy is afs_bufferLock-> buffer.lock
102 static afs_lock_t afs_bufferLock;
103 static struct buffer *phTable[PHSIZE]; /* page hash table */
105 static afs_int32 timecounter;
107 /* Prototypes for static routines */
108 static struct buffer *afs_newslot(struct dcache *adc, afs_int32 apage,
109 register struct buffer *lp);
111 static int dinit_flag = 0;
115 /* Initialize the venus buffer system. */
117 register struct buffer *tb;
118 #if defined(AFS_USEBUFFERS)
119 struct buf *tub; /* unix buffer for allocation */
126 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
127 abuffers = ((abuffers - 1) | (NPB - 1)) + 1;
128 #if defined(AFS_USEBUFFERS)
129 afs_max_buffers = abuffers;
131 afs_max_buffers = abuffers << 2; /* possibly grow up to 4 times as big */
133 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
135 (struct buffer *)afs_osi_Alloc(afs_max_buffers * sizeof(struct buffer));
137 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
138 for (i = 0; i < PHSIZE; i++)
140 for (i = 0; i < abuffers; i++) {
141 if ((i & (NPB - 1)) == 0) {
142 /* time to allocate a fresh buffer */
143 #if defined(AFS_USEBUFFERS)
144 tub = geteblk(AFS_BUFFER_PAGESIZE * NPB);
145 BufferData = (char *)tub->b_un.b_addr;
147 BufferData = (char *) afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
150 /* Fill in each buffer with an empty indication. */
153 afs_reset_inode(&tb->inode);
156 #if defined(AFS_USEBUFFERS)
157 if ((i & (NPB - 1)) == 0)
162 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i & (NPB - 1))];
165 AFS_RWLOCK_INIT(&tb->lock, "buffer lock");
171 DRead(register struct dcache *adc, register int page)
173 /* Read a page from the disk. */
174 register struct buffer *tb, *tb2;
175 struct osi_file *tfile;
179 ObtainWriteLock(&afs_bufferLock, 256);
181 #define bufmatch(tb) (tb->page == page && tb->fid == adc->index)
182 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
184 /* this apparently-complicated-looking code is simply an example of
185 * a little bit of loop unrolling, and is a standard linked-list
186 * traversal trick. It saves a few assignments at the the expense
187 * of larger code size. This could be simplified by better use of
190 if ((tb = phTable[pHash(adc->index, page)])) {
192 ObtainWriteLock(&tb->lock, 257);
194 ReleaseWriteLock(&afs_bufferLock);
195 tb->accesstime = timecounter++;
196 AFS_STATS(afs_stats_cmperf.bufHits++);
197 ReleaseWriteLock(&tb->lock);
200 register struct buffer **bufhead;
201 bufhead = &(phTable[pHash(adc->index, page)]);
202 while ((tb2 = tb->hashNext)) {
204 buf_Front(bufhead, tb, tb2);
205 ObtainWriteLock(&tb2->lock, 258);
207 ReleaseWriteLock(&afs_bufferLock);
208 tb2->accesstime = timecounter++;
209 AFS_STATS(afs_stats_cmperf.bufHits++);
210 ReleaseWriteLock(&tb2->lock);
213 if ((tb = tb2->hashNext)) {
215 buf_Front(bufhead, tb2, tb);
216 ObtainWriteLock(&tb->lock, 259);
218 ReleaseWriteLock(&afs_bufferLock);
219 tb->accesstime = timecounter++;
220 AFS_STATS(afs_stats_cmperf.bufHits++);
221 ReleaseWriteLock(&tb->lock);
231 AFS_STATS(afs_stats_cmperf.bufMisses++);
233 /* The last thing we looked at was either tb or tb2 (or nothing). That
234 * is at least the oldest buffer on one particular hash chain, so it's
235 * a pretty good place to start looking for the truly oldest buffer.
237 tb = afs_newslot(adc, page, (tb ? tb : tb2));
239 ReleaseWriteLock(&afs_bufferLock);
242 ObtainWriteLock(&tb->lock, 260);
244 ReleaseWriteLock(&afs_bufferLock);
245 if (page * AFS_BUFFER_PAGESIZE >= adc->f.chunkBytes) {
247 afs_reset_inode(&tb->inode);
249 ReleaseWriteLock(&tb->lock);
252 tfile = afs_CFileOpen(&adc->f.inode);
254 afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE, tb->data,
255 AFS_BUFFER_PAGESIZE);
256 afs_CFileClose(tfile);
257 if (code < AFS_BUFFER_PAGESIZE) {
259 afs_reset_inode(&tb->inode);
261 ReleaseWriteLock(&tb->lock);
264 /* Note that findslot sets the page field in the buffer equal to
265 * what it is searching for. */
266 ReleaseWriteLock(&tb->lock);
271 FixupBucket(register struct buffer *ap)
273 register struct buffer **lp, *tp;
275 /* first try to get it out of its current hash bucket, in which it
277 AFS_STATCNT(FixupBucket);
280 for (tp = *lp; tp; tp = tp->hashNext) {
287 /* now figure the new hash bucket */
288 i = pHash(ap->fid, ap->page);
289 ap->hashIndex = i; /* remember where we are for deletion */
290 ap->hashNext = phTable[i]; /* add us to the list */
291 phTable[i] = ap; /* at the front, since it's LRU */
294 /* lp is pointer to a fairly-old buffer */
295 static struct buffer *
296 afs_newslot(struct dcache *adc, afs_int32 apage, register struct buffer *lp)
298 /* Find a usable buffer slot */
299 register afs_int32 i;
301 register struct buffer *tp;
302 struct osi_file *tfile;
304 AFS_STATCNT(afs_newslot);
305 /* we take a pointer here to a buffer which was at the end of an
306 * LRU hash chain. Odds are, it's one of the older buffers, not
307 * one of the newer. Having an older buffer to start with may
308 * permit us to avoid a few of the assignments in the "typical
309 * case" for loop below.
311 if (lp && (lp->lockers == 0)) {
317 /* timecounter might have wrapped, if machine is very very busy
318 * and stays up for a long time. Timecounter mustn't wrap twice
319 * (positive->negative->positive) before calling newslot, but that
320 * would require 2 billion consecutive cache hits... Anyway, the
321 * penalty is only that the cache replacement policy will be
322 * almost MRU for the next ~2 billion DReads... newslot doesn't
323 * get called nearly as often as DRead, so in order to avoid the
324 * performance penalty of using the hypers, it's worth doing the
325 * extra check here every time. It's probably cheaper than doing
326 * hcmp, anyway. There is a little performance hit resulting from
327 * resetting all the access times to 0, but it only happens once
328 * every month or so, and the access times will rapidly sort
329 * themselves back out after just a few more DReads.
331 if (timecounter < 0) {
334 for (i = 0; i < nbuffers; i++, tp++) {
336 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
340 /* this is the typical case */
342 for (i = 0; i < nbuffers; i++, tp++) {
343 if (tp->lockers == 0) {
344 if (!lp || tp->accesstime < lt) {
353 /* No unlocked buffers. If still possible, allocate a new increment */
354 if (nbuffers + NPB > afs_max_buffers) {
355 /* There are no unlocked buffers -- this used to panic, but that
356 * seems extreme. To the best of my knowledge, all the callers
357 * of DRead are prepared to handle a zero return. Some of them
358 * just panic directly, but not all of them. */
359 afs_warn("afs: all buffers locked\n");
363 BufferData = (char *) afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
364 for (i = 0; i< NPB; i++) {
365 /* Fill in each buffer with an empty indication. */
366 tp = &Buffers[i + nbuffers];
368 afs_reset_inode(&tp->inode);
371 tp->data = &BufferData[AFS_BUFFER_PAGESIZE * i];
374 AFS_RWLOCK_INIT(&tp->lock, "buffer lock");
376 lp = &Buffers[nbuffers];
381 /* see DFlush for rationale for not getting and locking the dcache */
382 tfile = afs_CFileOpen(&lp->inode);
383 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE, lp->data,
384 AFS_BUFFER_PAGESIZE);
386 afs_CFileClose(tfile);
387 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
390 /* Now fill in the header. */
391 lp->fid = adc->index;
392 afs_copy_inode(&lp->inode, &adc->f.inode);
394 lp->accesstime = timecounter++;
395 FixupBucket(lp); /* move to the right hash bucket */
401 DRelease(void *loc, int flag)
403 /* Release a buffer, specifying whether or not the buffer has been
404 * modified by the locker. */
405 register struct buffer *bp = (struct buffer *)loc;
407 register struct buffer *tp;
409 AFS_STATCNT(DRelease);
412 /* look for buffer by scanning Unix buffers for appropriate address */
413 /* careful: despite the declaration above at this point bp is still simply
414 * an address inside the buffer, not a pointer to the buffer header */
416 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
417 if ( (char *) bp >= (char *) tp->data &&
418 (char *) bp < (char *) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
419 /* we found the right range */
420 index += ((char *) bp - (char *) tp->data) >> LOGPS;
424 tp = &(Buffers[index]);
425 ObtainWriteLock(&tp->lock, 261);
429 ReleaseWriteLock(&tp->lock);
433 DVOffset(register void *ap)
435 /* Return the byte within a file represented by a buffer pointer. */
437 register struct buffer *tp;
438 AFS_STATCNT(DVOffset);
439 /* look for buffer by scanning Unix buffers for appropriate address */
440 /* see comment in DRelease about the meaning of ap/bp */
442 for (index = 0; index < nbuffers; index += NPB, tp += NPB) {
443 if ( (char *) ap >= (char *) tp->data &&
444 (char *) ap < (char *) tp->data + AFS_BUFFER_PAGESIZE * NPB) {
445 /* we found the right range */
446 index += ((char *) ap - (char *) tp->data) >> LOGPS;
450 if (index < 0 || index >= nbuffers)
452 tp = &(Buffers[index]);
453 return AFS_BUFFER_PAGESIZE * tp->page + (int)(((char *)ap) - tp->data);
457 * Zap one dcache entry: destroy one FID's buffers.
459 * 1/1/91 - I've modified the hash function to take the page as well
460 * as the *fid, so that lookup will be a bit faster. That presents some
461 * difficulties for Zap, which now has to have some knowledge of the nature
462 * of the hash function. Oh well. This should use the list traversal
465 * \param adc The dcache entry to be zapped.
468 DZap(struct dcache *adc)
471 /* Destroy all buffers pertaining to a particular fid. */
472 register struct buffer *tb;
475 ObtainReadLock(&afs_bufferLock);
477 for (i = 0; i <= PHPAGEMASK; i++)
478 for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
479 if (tb->fid == adc->index) {
480 ObtainWriteLock(&tb->lock, 262);
482 afs_reset_inode(&tb->inode);
484 ReleaseWriteLock(&tb->lock);
486 ReleaseReadLock(&afs_bufferLock);
490 DFlushBuffer(struct buffer *ab) {
491 struct osi_file *tfile;
493 tfile = afs_CFileOpen(&ab->inode);
494 afs_CFileWrite(tfile, ab->page * AFS_BUFFER_PAGESIZE,
495 ab->data, AFS_BUFFER_PAGESIZE);
496 ab->dirty = 0; /* Clear the dirty flag */
497 afs_CFileClose(tfile);
501 DFlushDCache(struct dcache *adc)
506 ObtainReadLock(&afs_bufferLock);
508 for (i = 0; i <= PHPAGEMASK; i++)
509 for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
510 if (tb->fid == adc->index) {
511 ObtainWriteLock(&tb->lock, 701);
513 ReleaseReadLock(&afs_bufferLock);
518 ReleaseWriteLock(&tb->lock);
519 ObtainReadLock(&afs_bufferLock);
522 ReleaseReadLock(&afs_bufferLock);
528 /* Flush all the modified buffers. */
530 register struct buffer *tb;
534 ObtainReadLock(&afs_bufferLock);
535 for (i = 0; i < nbuffers; i++, tb++) {
537 ObtainWriteLock(&tb->lock, 263);
539 ReleaseReadLock(&afs_bufferLock);
541 /* it seems safe to do this I/O without having the dcache
542 * locked, since the only things that will update the data in
543 * a directory are the buffer package, which holds the relevant
544 * tb->lock while doing the write, or afs_GetDCache, which
545 * DZap's the directory while holding the dcache lock.
546 * It is not possible to lock the dcache or even call
547 * afs_GetDSlot to map the index to the dcache since the dir
548 * package's caller has some dcache object locked already (so
549 * we cannot lock afs_xdcache). In addition, we cannot obtain
550 * a dcache lock while holding the tb->lock of the same file
551 * since that can deadlock with DRead/DNew */
555 ReleaseWriteLock(&tb->lock);
556 ObtainReadLock(&afs_bufferLock);
559 ReleaseReadLock(&afs_bufferLock);
563 DNew(register struct dcache *adc, register int page)
565 /* Same as read, only do *not* even try to read the page, since it probably doesn't exist. */
566 register struct buffer *tb;
568 ObtainWriteLock(&afs_bufferLock, 264);
569 if ((tb = afs_newslot(adc, page, NULL)) == 0) {
570 ReleaseWriteLock(&afs_bufferLock);
573 /* extend the chunk, if needed */
574 /* Do it now, not in DFlush or afs_newslot when the data is written out,
575 * since now our caller has adc->lock writelocked, and we can't acquire
576 * that lock (or even map from a fid to a dcache) in afs_newslot or
577 * DFlush due to lock hierarchy issues */
578 if ((page + 1) * AFS_BUFFER_PAGESIZE > adc->f.chunkBytes) {
579 afs_AdjustSize(adc, (page + 1) * AFS_BUFFER_PAGESIZE);
580 afs_WriteDCache(adc, 1);
582 ObtainWriteLock(&tb->lock, 265);
584 ReleaseWriteLock(&afs_bufferLock);
585 ReleaseWriteLock(&tb->lock);
590 shutdown_bufferpackage(void)
592 register struct buffer *tp;
595 AFS_STATCNT(shutdown_bufferpackage);
596 /* Free all allocated Buffers and associated buffer pages */
598 if (afs_cold_shutdown) {
601 for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
602 #if defined(AFS_USEBUFFERS)
603 /* The following check shouldn't be necessary and it will be removed soon */
606 ("afs: shutdown_bufferpackage: bufp == 0!! Shouldn't happen\n");
612 afs_osi_Free(tp->data, NPB * AFS_BUFFER_PAGESIZE);
615 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
618 for (i = 0; i < PHSIZE; i++)
620 memset(&afs_bufferLock, 0, sizeof(afs_lock_t));