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 #define NPB 8 /* must be a pwer of 2 */
50 static int afs_max_buffers; /* should be an integral multiple of NPB */
53 #define AFS_BUFFER_PAGESIZE 2048
56 /* If you change any of this PH stuff, make sure you don't break DZap() */
57 /* use last two bits for page */
59 /* use next five bits for fid */
61 /* page hash table size - this is pretty intertwined with pHash */
62 #define PHSIZE (PHPAGEMASK + PHFIDMASK + 1)
64 #define pHash(fid,page) ((((afs_int32)(fid)) & PHFIDMASK) \
65 | (page & PHPAGEMASK))
68 #undef dirty /* XXX */
71 static struct buffer *Buffers = 0;
72 static char *BufferData;
75 extern struct buf *geteblk();
78 #define timecounter afs_timecounter
81 /* A note on locking in 'struct buffer'
83 * afs_bufferLock protects the hash chain, and the 'lockers' field where that
84 * has a zero value. It must be held whenever lockers is incremented from zero.
86 * The individual buffer lock protects the contents of the structure, including
89 * For safety: afs_bufferLock and the individual buffer lock must be held
90 * when obtaining a reference on a structure. Only the individual buffer lock
91 * need be held when releasing a reference.
93 * The locking hierarchy is afs_bufferLock-> buffer.lock
97 static afs_lock_t afs_bufferLock;
98 static struct buffer *phTable[PHSIZE]; /* page hash table */
100 static afs_int32 timecounter;
102 /* Prototypes for static routines */
103 static struct buffer *afs_newslot(struct dcache *adc, afs_int32 apage,
106 static int dinit_flag = 0;
110 /* Initialize the venus buffer system. */
118 /* round up to next multiple of NPB, since we allocate multiple pages per chunk */
119 abuffers = ((abuffers - 1) | (NPB - 1)) + 1;
120 afs_max_buffers = abuffers << 2; /* possibly grow up to 4 times as big */
121 LOCK_INIT(&afs_bufferLock, "afs_bufferLock");
122 Buffers = afs_osi_Alloc(afs_max_buffers * sizeof(struct buffer));
123 osi_Assert(Buffers != NULL);
125 afs_stats_cmperf.bufAlloced = nbuffers = abuffers;
126 for (i = 0; i < PHSIZE; i++)
128 for (i = 0; i < abuffers; i++) {
129 if ((i & (NPB - 1)) == 0) {
130 /* time to allocate a fresh buffer */
131 BufferData = afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
132 osi_Assert(BufferData != NULL);
134 /* Fill in each buffer with an empty indication. */
137 afs_reset_inode(&tb->inode);
140 tb->data = &BufferData[AFS_BUFFER_PAGESIZE * (i & (NPB - 1))];
143 AFS_RWLOCK_INIT(&tb->lock, "buffer lock");
149 DRead(struct dcache *adc, int page, struct DirBuffer *entry)
151 /* Read a page from the disk. */
152 struct buffer *tb, *tb2;
153 struct osi_file *tfile;
158 memset(entry, 0, sizeof(struct DirBuffer));
160 ObtainWriteLock(&afs_bufferLock, 256);
162 #define bufmatch(tb) (tb->page == page && tb->fid == adc->index)
163 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
165 /* this apparently-complicated-looking code is simply an example of
166 * a little bit of loop unrolling, and is a standard linked-list
167 * traversal trick. It saves a few assignments at the the expense
168 * of larger code size. This could be simplified by better use of
171 if ((tb = phTable[pHash(adc->index, page)])) {
173 ObtainWriteLock(&tb->lock, 257);
175 ReleaseWriteLock(&afs_bufferLock);
176 tb->accesstime = timecounter++;
177 AFS_STATS(afs_stats_cmperf.bufHits++);
178 ReleaseWriteLock(&tb->lock);
180 entry->data = tb->data;
183 struct buffer **bufhead;
184 bufhead = &(phTable[pHash(adc->index, page)]);
185 while ((tb2 = tb->hashNext)) {
187 buf_Front(bufhead, tb, tb2);
188 ObtainWriteLock(&tb2->lock, 258);
190 ReleaseWriteLock(&afs_bufferLock);
191 tb2->accesstime = timecounter++;
192 AFS_STATS(afs_stats_cmperf.bufHits++);
193 ReleaseWriteLock(&tb2->lock);
195 entry->data = tb2->data;
198 if ((tb = tb2->hashNext)) {
200 buf_Front(bufhead, tb2, tb);
201 ObtainWriteLock(&tb->lock, 259);
203 ReleaseWriteLock(&afs_bufferLock);
204 tb->accesstime = timecounter++;
205 AFS_STATS(afs_stats_cmperf.bufHits++);
206 ReleaseWriteLock(&tb->lock);
208 entry->data = tb->data;
217 AFS_STATS(afs_stats_cmperf.bufMisses++);
219 /* The last thing we looked at was either tb or tb2 (or nothing). That
220 * is at least the oldest buffer on one particular hash chain, so it's
221 * a pretty good place to start looking for the truly oldest buffer.
223 tb = afs_newslot(adc, page, (tb ? tb : tb2));
225 ReleaseWriteLock(&afs_bufferLock);
228 ObtainWriteLock(&tb->lock, 260);
230 ReleaseWriteLock(&afs_bufferLock);
231 if (page * AFS_BUFFER_PAGESIZE >= adc->f.chunkBytes) {
233 afs_reset_inode(&tb->inode);
235 ReleaseWriteLock(&tb->lock);
238 tfile = afs_CFileOpen(&adc->f.inode);
240 afs_CFileRead(tfile, tb->page * AFS_BUFFER_PAGESIZE, tb->data,
241 AFS_BUFFER_PAGESIZE);
242 afs_CFileClose(tfile);
243 if (code < AFS_BUFFER_PAGESIZE) {
245 afs_reset_inode(&tb->inode);
247 ReleaseWriteLock(&tb->lock);
250 /* Note that findslot sets the page field in the buffer equal to
251 * what it is searching for. */
252 ReleaseWriteLock(&tb->lock);
254 entry->data = tb->data;
259 FixupBucket(struct buffer *ap)
261 struct buffer **lp, *tp;
263 /* first try to get it out of its current hash bucket, in which it
265 AFS_STATCNT(FixupBucket);
268 for (tp = *lp; tp; tp = tp->hashNext) {
275 /* now figure the new hash bucket */
276 i = pHash(ap->fid, ap->page);
277 ap->hashIndex = i; /* remember where we are for deletion */
278 ap->hashNext = phTable[i]; /* add us to the list */
279 phTable[i] = ap; /* at the front, since it's LRU */
282 /* lp is pointer to a fairly-old buffer */
283 static struct buffer *
284 afs_newslot(struct dcache *adc, afs_int32 apage, struct buffer *lp)
286 /* Find a usable buffer slot */
290 struct osi_file *tfile;
292 AFS_STATCNT(afs_newslot);
293 /* we take a pointer here to a buffer which was at the end of an
294 * LRU hash chain. Odds are, it's one of the older buffers, not
295 * one of the newer. Having an older buffer to start with may
296 * permit us to avoid a few of the assignments in the "typical
297 * case" for loop below.
299 if (lp && (lp->lockers == 0)) {
305 /* timecounter might have wrapped, if machine is very very busy
306 * and stays up for a long time. Timecounter mustn't wrap twice
307 * (positive->negative->positive) before calling newslot, but that
308 * would require 2 billion consecutive cache hits... Anyway, the
309 * penalty is only that the cache replacement policy will be
310 * almost MRU for the next ~2 billion DReads... newslot doesn't
311 * get called nearly as often as DRead, so in order to avoid the
312 * performance penalty of using the hypers, it's worth doing the
313 * extra check here every time. It's probably cheaper than doing
314 * hcmp, anyway. There is a little performance hit resulting from
315 * resetting all the access times to 0, but it only happens once
316 * every month or so, and the access times will rapidly sort
317 * themselves back out after just a few more DReads.
319 if (timecounter < 0) {
322 for (i = 0; i < nbuffers; i++, tp++) {
324 if (!lp && !tp->lockers) /* one is as good as the rest, I guess */
328 /* this is the typical case */
330 for (i = 0; i < nbuffers; i++, tp++) {
331 if (tp->lockers == 0) {
332 if (!lp || tp->accesstime < lt) {
341 /* No unlocked buffers. If still possible, allocate a new increment */
342 if (nbuffers + NPB > afs_max_buffers) {
343 /* There are no unlocked buffers -- this used to panic, but that
344 * seems extreme. To the best of my knowledge, all the callers
345 * of DRead are prepared to handle a zero return. Some of them
346 * just panic directly, but not all of them. */
347 afs_warn("afs: all buffers locked\n");
351 BufferData = afs_osi_Alloc(AFS_BUFFER_PAGESIZE * NPB);
352 osi_Assert(BufferData != NULL);
353 for (i = 0; i< NPB; i++) {
354 /* Fill in each buffer with an empty indication. */
355 tp = &Buffers[i + nbuffers];
357 afs_reset_inode(&tp->inode);
360 tp->data = &BufferData[AFS_BUFFER_PAGESIZE * i];
363 AFS_RWLOCK_INIT(&tp->lock, "buffer lock");
365 lp = &Buffers[nbuffers];
370 /* see DFlush for rationale for not getting and locking the dcache */
371 tfile = afs_CFileOpen(&lp->inode);
372 afs_CFileWrite(tfile, lp->page * AFS_BUFFER_PAGESIZE, lp->data,
373 AFS_BUFFER_PAGESIZE);
375 afs_CFileClose(tfile);
376 AFS_STATS(afs_stats_cmperf.bufFlushDirty++);
379 /* Now fill in the header. */
380 lp->fid = adc->index;
381 afs_copy_inode(&lp->inode, &adc->f.inode);
383 lp->accesstime = timecounter++;
384 FixupBucket(lp); /* move to the right hash bucket */
390 DRelease(struct DirBuffer *entry, int flag)
394 AFS_STATCNT(DRelease);
401 ObtainWriteLock(&tp->lock, 261);
405 ReleaseWriteLock(&tp->lock);
409 DVOffset(struct DirBuffer *entry)
413 AFS_STATCNT(DVOffset);
416 return AFS_BUFFER_PAGESIZE * bp->page
417 + (char *)entry->data - (char *)bp->data;
421 * Zap one dcache entry: destroy one FID's buffers.
423 * 1/1/91 - I've modified the hash function to take the page as well
424 * as the *fid, so that lookup will be a bit faster. That presents some
425 * difficulties for Zap, which now has to have some knowledge of the nature
426 * of the hash function. Oh well. This should use the list traversal
429 * \param adc The dcache entry to be zapped.
432 DZap(struct dcache *adc)
435 /* Destroy all buffers pertaining to a particular fid. */
439 ObtainReadLock(&afs_bufferLock);
441 for (i = 0; i <= PHPAGEMASK; i++)
442 for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
443 if (tb->fid == adc->index) {
444 ObtainWriteLock(&tb->lock, 262);
446 afs_reset_inode(&tb->inode);
448 ReleaseWriteLock(&tb->lock);
450 ReleaseReadLock(&afs_bufferLock);
454 DFlushBuffer(struct buffer *ab)
456 struct osi_file *tfile;
458 tfile = afs_CFileOpen(&ab->inode);
459 afs_CFileWrite(tfile, ab->page * AFS_BUFFER_PAGESIZE,
460 ab->data, AFS_BUFFER_PAGESIZE);
461 ab->dirty = 0; /* Clear the dirty flag */
462 afs_CFileClose(tfile);
466 DFlushDCache(struct dcache *adc)
471 ObtainReadLock(&afs_bufferLock);
473 for (i = 0; i <= PHPAGEMASK; i++)
474 for (tb = phTable[pHash(adc->index, i)]; tb; tb = tb->hashNext)
475 if (tb->fid == adc->index) {
476 ObtainWriteLock(&tb->lock, 701);
478 ReleaseReadLock(&afs_bufferLock);
483 ReleaseWriteLock(&tb->lock);
484 ObtainReadLock(&afs_bufferLock);
487 ReleaseReadLock(&afs_bufferLock);
493 /* Flush all the modified buffers. */
499 ObtainReadLock(&afs_bufferLock);
500 for (i = 0; i < nbuffers; i++, tb++) {
502 ObtainWriteLock(&tb->lock, 263);
504 ReleaseReadLock(&afs_bufferLock);
506 /* it seems safe to do this I/O without having the dcache
507 * locked, since the only things that will update the data in
508 * a directory are the buffer package, which holds the relevant
509 * tb->lock while doing the write, or afs_GetDCache, which
510 * DZap's the directory while holding the dcache lock.
511 * It is not possible to lock the dcache or even call
512 * afs_GetDSlot to map the index to the dcache since the dir
513 * package's caller has some dcache object locked already (so
514 * we cannot lock afs_xdcache). In addition, we cannot obtain
515 * a dcache lock while holding the tb->lock of the same file
516 * since that can deadlock with DRead/DNew */
520 ReleaseWriteLock(&tb->lock);
521 ObtainReadLock(&afs_bufferLock);
524 ReleaseReadLock(&afs_bufferLock);
528 DNew(struct dcache *adc, int page, struct DirBuffer *entry)
530 /* Same as read, only do *not* even try to read the page, since it
531 * probably doesn't exist. */
535 ObtainWriteLock(&afs_bufferLock, 264);
536 if ((tb = afs_newslot(adc, page, NULL)) == 0) {
537 ReleaseWriteLock(&afs_bufferLock);
540 /* extend the chunk, if needed */
541 /* Do it now, not in DFlush or afs_newslot when the data is written out,
542 * since now our caller has adc->lock writelocked, and we can't acquire
543 * that lock (or even map from a fid to a dcache) in afs_newslot or
544 * DFlush due to lock hierarchy issues */
545 if ((page + 1) * AFS_BUFFER_PAGESIZE > adc->f.chunkBytes) {
546 afs_AdjustSize(adc, (page + 1) * AFS_BUFFER_PAGESIZE);
547 afs_WriteDCache(adc, 1);
549 ObtainWriteLock(&tb->lock, 265);
551 ReleaseWriteLock(&afs_bufferLock);
552 ReleaseWriteLock(&tb->lock);
554 entry->data = tb->data;
560 shutdown_bufferpackage(void)
565 AFS_STATCNT(shutdown_bufferpackage);
566 /* Free all allocated Buffers and associated buffer pages */
568 if (afs_cold_shutdown) {
571 for (i = 0; i < nbuffers; i += NPB, tp += NPB) {
572 afs_osi_Free(tp->data, NPB * AFS_BUFFER_PAGESIZE);
574 afs_osi_Free(Buffers, nbuffers * sizeof(struct buffer));
577 for (i = 0; i < PHSIZE; i++)
579 memset(&afs_bufferLock, 0, sizeof(afs_lock_t));