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>
17 #ifdef AFS_64BIT_IOPS_ENV
18 #define BUFFER_FID_SIZE (9 + 2*sizeof(char*)/sizeof(int))
20 #define BUFFER_FID_SIZE (6 + 2*sizeof(char*)/sizeof(int))
24 /* fid is used for Unique cache key + i/o addressing.
25 * fid size is based on 4 + size of inode and size of pointer
27 afs_int32 fid[BUFFER_FID_SIZE];
30 struct buffer *hashNext;
40 struct Lock afs_bufferLock;
43 #define BUFFER_PAGE_SIZE 2048
46 /* page hash table size */
48 /* The hash table should be somewhat efficient even if there are only
49 * a few partitions (less than 32). So the hash for the fileserver is now
50 * based on the volume id. This means this macro is dependent upon the
51 * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
53 #define pHash(fid) ((fid)[0] & (PHSIZE-1))
54 #define vHash(vid) (vid & (PHSIZE-1))
56 /* admittedly system dependent, this is the maximum signed 32-bit value */
57 #define BUFFER_LONG_MAX 2147483647
62 static struct buffer **Buffers;
66 static struct buffer *phTable[PHSIZE]; /* page hash table */
67 static struct buffer *LastBuffer;
70 static int calls = 0, ios = 0;
72 struct buffer *newslot(afs_int32 *afid, afs_int32 apage,
75 /* XXX - This sucks. The correct prototypes for these functions are ...
77 * extern void FidZero(DirHandle *);
78 * extern int FidEq(DirHandle *a, DirHandle *b);
79 * extern int ReallyRead(DirHandle *a, int block, char *data);
82 extern void FidZero(afs_int32 *file);
83 extern int FidEq(afs_int32 *a, afs_int32 *b);
84 extern int ReallyRead(afs_int32 *file, int block, char *data);
85 extern int ReallyWrite(afs_int32 *file, int block, char *data);
86 extern void FidZap(afs_int32 *file);
87 extern int FidVolEq(afs_int32 *file, afs_int32 vid);
88 extern void FidCpy(afs_int32 *tofile, afs_int32 *fromfile);
89 extern void Die(char *msg);
92 DStat(int *abuffers, int *acalls, int *aios)
101 * initialize the directory package.
103 * @param[in] abuffers size of directory buffer cache
105 * @return operation status
111 /* Initialize the venus buffer system. */
112 register int i, tsize;
113 register struct buffer *tb;
116 Lock_Init(&afs_bufferLock);
117 /* Align each element of Buffers on a doubleword boundary */
118 tsize = (sizeof(struct buffer) + 7) & ~7;
119 tp = (char *)malloc(abuffers * tsize);
120 Buffers = (struct buffer **)malloc(abuffers * sizeof(struct buffer *));
121 BufferData = (char *)malloc(abuffers * BUFFER_PAGE_SIZE);
123 LastBuffer = (struct buffer *)tp;
125 for (i = 0; i < PHSIZE; i++)
127 for (i = 0; i < abuffers; i++) {
128 /* Fill in each buffer with an empty indication. */
129 tb = (struct buffer *)tp;
133 tb->accesstime = tb->lockers = 0;
134 tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
137 Lock_Init(&tb->lock);
143 * read a page out of a directory object.
145 * @param[in] fid directory object fid
146 * @param[in] page page in hash table to be read
148 * @return pointer to requested page in directory cache
149 * @retval NULL read failed
152 DRead(register afs_int32 *fid, register int page)
154 /* Read a page from the disk. */
155 register struct buffer *tb, *tb2, **bufhead;
157 ObtainWriteLock(&afs_bufferLock);
160 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
161 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
163 /* this apparently-complicated-looking code is simply an example of
164 * a little bit of loop unrolling, and is a standard linked-list
165 * traversal trick. It saves a few assignments at the the expense
166 * of larger code size. This could be simplified by better use of
167 * macros. With the use of these LRU queues, the old one-cache is
170 if ((tb = phTable[pHash(fid)])) { /* ASSMT HERE */
172 ObtainWriteLock(&tb->lock);
174 ReleaseWriteLock(&afs_bufferLock);
175 tb->accesstime = ++timecounter;
176 ReleaseWriteLock(&tb->lock);
179 bufhead = &(phTable[pHash(fid)]);
180 while ((tb2 = tb->hashNext)) {
182 buf_Front(bufhead, tb, tb2);
183 ObtainWriteLock(&tb2->lock);
185 ReleaseWriteLock(&afs_bufferLock);
186 tb2->accesstime = ++timecounter;
187 ReleaseWriteLock(&tb2->lock);
190 if ((tb = tb2->hashNext)) { /* ASSIGNMENT HERE! */
192 buf_Front(bufhead, tb2, tb);
193 ObtainWriteLock(&tb->lock);
195 ReleaseWriteLock(&afs_bufferLock);
196 tb->accesstime = ++timecounter;
197 ReleaseWriteLock(&tb->lock);
208 /* The last thing we looked at was either tb or tb2 (or nothing). That
209 * is at least the oldest buffer on one particular hash chain, so it's
210 * a pretty good place to start looking for the truly oldest buffer.
212 tb = newslot(fid, page, (tb ? tb : tb2));
214 ObtainWriteLock(&tb->lock);
216 ReleaseWriteLock(&afs_bufferLock);
217 if (ReallyRead(tb->fid, tb->page, tb->data)) {
219 FidZap(tb->fid); /* disaster */
220 ReleaseWriteLock(&tb->lock);
223 /* Note that findslot sets the page field in the buffer equal to
224 * what it is searching for.
226 ReleaseWriteLock(&tb->lock);
231 FixupBucket(register struct buffer *ap)
233 register struct buffer **lp, *tp;
236 /* first try to get it out of its current hash bucket, in which it might not be */
239 for (tp = *lp; tp; tp = tp->hashNext) {
246 /* now figure the new hash bucket */
248 ap->hashIndex = i; /* remember where we are for deletion */
249 ap->hashNext = phTable[i]; /* add us to the list */
250 phTable[i] = ap; /* at the front, since it's LRU */
255 newslot(afs_int32 *afid, afs_int32 apage, register struct buffer *lp)
257 /* Find a usable buffer slot */
258 register afs_int32 i;
260 register struct buffer **tbp;
262 if (lp && (lp->lockers == 0)) {
266 lt = BUFFER_LONG_MAX;
270 for (i = 0; i < nbuffers; i++, tbp++) {
271 if ((*tbp)->lockers == 0) {
272 if ((*tbp)->accesstime < lt) {
274 lt = (*tbp)->accesstime;
279 /* There are no unlocked buffers */
282 Die("accesstime counter wrapped");
284 Die("all buffers locked");
287 /* We do not need to lock the buffer here because it has no lockers
288 * and the afs_bufferLock prevents other threads from zapping this
289 * buffer while we are writing it out */
291 if (ReallyWrite(lp->fid, lp->page, lp->data))
292 Die("writing bogus buffer");
296 /* Now fill in the header. */
298 FidCpy(lp->fid, afid); /* set this */
300 lp->accesstime = ++timecounter;
302 FixupBucket(lp); /* move to the right hash bucket */
307 /* Release a buffer, specifying whether or not the buffer has been modified
310 DRelease(void *loc, int flag)
312 struct buffer *bp = (struct buffer *)loc;
317 index = ((char *)bp - BufferData) >> LOGPS;
319 ObtainWriteLock(&bp->lock);
323 ReleaseWriteLock(&bp->lock);
327 DVOffset(register void *ap)
329 /* Return the byte within a file represented by a buffer pointer. */
330 register struct buffer *bp = ap;
333 index = ((char *)bp - BufferData) >> LOGPS;
334 if (index < 0 || index >= nbuffers)
337 return BUFFER_PAGE_SIZE * bp->page + (char *)ap - (char *)bp->data;
341 DZap(register afs_int32 *fid)
343 /* Destroy all buffers pertaining to a particular fid. */
344 register struct buffer *tb;
345 ObtainReadLock(&afs_bufferLock);
346 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
347 if (FidEq(tb->fid, fid)) {
348 ObtainWriteLock(&tb->lock);
351 ReleaseWriteLock(&tb->lock);
353 ReleaseReadLock(&afs_bufferLock);
357 DFlushVolume(register afs_int32 vid)
359 /* Flush all data and release all inode handles for a particular volume */
360 register struct buffer *tb;
361 register int code, rcode = 0;
362 ObtainReadLock(&afs_bufferLock);
363 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
364 if (FidVolEq(tb->fid, vid)) {
365 ObtainWriteLock(&tb->lock);
367 code = ReallyWrite(tb->fid, tb->page, tb->data);
373 ReleaseWriteLock(&tb->lock);
375 ReleaseReadLock(&afs_bufferLock);
380 DFlushEntry(register afs_int32 *fid)
382 /* Flush pages modified by one entry. */
383 register struct buffer *tb;
386 ObtainReadLock(&afs_bufferLock);
387 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
388 if (FidEq(tb->fid, fid) && tb->dirty) {
389 ObtainWriteLock(&tb->lock);
391 code = ReallyWrite(tb->fid, tb->page, tb->data);
393 ReleaseWriteLock(&tb->lock);
394 ReleaseReadLock(&afs_bufferLock);
399 ReleaseWriteLock(&tb->lock);
401 ReleaseReadLock(&afs_bufferLock);
408 /* Flush all the modified buffers. */
410 register struct buffer **tbp;
411 afs_int32 code, rcode;
415 ObtainReadLock(&afs_bufferLock);
416 for (i = 0; i < nbuffers; i++, tbp++) {
418 ObtainWriteLock(&(*tbp)->lock);
420 ReleaseReadLock(&afs_bufferLock);
422 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
424 (*tbp)->dirty = 0; /* Clear the dirty flag */
425 if (code && !rcode) {
430 ReleaseWriteLock(&(*tbp)->lock);
431 ObtainReadLock(&afs_bufferLock);
434 ReleaseReadLock(&afs_bufferLock);
439 DNew(register afs_int32 *fid, register int page)
441 /* Same as read, only do *not* even try to read the page,
442 * since it probably doesn't exist.
444 register struct buffer *tb;
445 ObtainWriteLock(&afs_bufferLock);
446 if ((tb = newslot(fid, page, 0)) == 0) {
447 ReleaseWriteLock(&afs_bufferLock);
450 ObtainWriteLock(&tb->lock);
452 ReleaseWriteLock(&afs_bufferLock);
453 ReleaseWriteLock(&tb->lock);