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. */
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(afs_int32 *fid, int page, struct DirBuffer *entry)
154 /* Read a page from the disk. */
155 struct buffer *tb, *tb2, **bufhead;
157 memset(entry, 0, sizeof(struct DirBuffer));
159 ObtainWriteLock(&afs_bufferLock);
162 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
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
169 * macros. With the use of these LRU queues, the old one-cache is
172 if ((tb = phTable[pHash(fid)])) { /* ASSMT HERE */
174 ObtainWriteLock(&tb->lock);
176 ReleaseWriteLock(&afs_bufferLock);
177 tb->accesstime = ++timecounter;
178 ReleaseWriteLock(&tb->lock);
180 entry->data = tb->data;
183 bufhead = &(phTable[pHash(fid)]);
184 while ((tb2 = tb->hashNext)) {
186 buf_Front(bufhead, tb, tb2);
187 ObtainWriteLock(&tb2->lock);
189 ReleaseWriteLock(&afs_bufferLock);
190 tb2->accesstime = ++timecounter;
191 ReleaseWriteLock(&tb2->lock);
193 entry->data = tb2->data;
196 if ((tb = tb2->hashNext)) { /* ASSIGNMENT HERE! */
198 buf_Front(bufhead, tb2, tb);
199 ObtainWriteLock(&tb->lock);
201 ReleaseWriteLock(&afs_bufferLock);
202 tb->accesstime = ++timecounter;
203 ReleaseWriteLock(&tb->lock);
205 entry->data = tb->data;
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 = newslot(fid, page, (tb ? tb : tb2));
221 ObtainWriteLock(&tb->lock);
223 ReleaseWriteLock(&afs_bufferLock);
224 if (ReallyRead(tb->fid, tb->page, tb->data)) {
226 FidZap(tb->fid); /* disaster */
227 ReleaseWriteLock(&tb->lock);
230 /* Note that findslot sets the page field in the buffer equal to
231 * what it is searching for.
233 ReleaseWriteLock(&tb->lock);
235 entry->data = tb->data;
241 FixupBucket(struct buffer *ap)
243 struct buffer **lp, *tp;
246 /* first try to get it out of its current hash bucket, in which it might not be */
249 for (tp = *lp; tp; tp = tp->hashNext) {
256 /* now figure the new hash bucket */
258 ap->hashIndex = i; /* remember where we are for deletion */
259 ap->hashNext = phTable[i]; /* add us to the list */
260 phTable[i] = ap; /* at the front, since it's LRU */
265 newslot(afs_int32 *afid, afs_int32 apage, struct buffer *lp)
267 /* Find a usable buffer slot */
272 if (lp && (lp->lockers == 0)) {
276 lt = BUFFER_LONG_MAX;
280 for (i = 0; i < nbuffers; i++, tbp++) {
281 if ((*tbp)->lockers == 0) {
282 if ((*tbp)->accesstime < lt) {
284 lt = (*tbp)->accesstime;
289 /* There are no unlocked buffers */
292 Die("accesstime counter wrapped");
294 Die("all buffers locked");
297 /* We do not need to lock the buffer here because it has no lockers
298 * and the afs_bufferLock prevents other threads from zapping this
299 * buffer while we are writing it out */
301 if (ReallyWrite(lp->fid, lp->page, lp->data))
302 Die("writing bogus buffer");
306 /* Now fill in the header. */
308 FidCpy(lp->fid, afid); /* set this */
310 lp->accesstime = ++timecounter;
312 FixupBucket(lp); /* move to the right hash bucket */
317 /* Release a buffer, specifying whether or not the buffer has been modified
320 DRelease(struct DirBuffer *entry, int flag)
324 bp = (struct buffer *) entry->buffer;
327 ObtainWriteLock(&bp->lock);
331 ReleaseWriteLock(&bp->lock);
334 /* Return the byte within a file represented by a buffer pointer. */
336 DVOffset(struct DirBuffer *entry)
341 return BUFFER_PAGE_SIZE * bp->page + (char *)entry->data - (char *)bp->data;
347 /* Destroy all buffers pertaining to a particular fid. */
349 ObtainReadLock(&afs_bufferLock);
350 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
351 if (FidEq(tb->fid, fid)) {
352 ObtainWriteLock(&tb->lock);
355 ReleaseWriteLock(&tb->lock);
357 ReleaseReadLock(&afs_bufferLock);
361 DFlushVolume(afs_int32 vid)
363 /* Flush all data and release all inode handles for a particular volume */
366 ObtainReadLock(&afs_bufferLock);
367 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
368 if (FidVolEq(tb->fid, vid)) {
369 ObtainWriteLock(&tb->lock);
371 code = ReallyWrite(tb->fid, tb->page, tb->data);
377 ReleaseWriteLock(&tb->lock);
379 ReleaseReadLock(&afs_bufferLock);
384 DFlushEntry(afs_int32 *fid)
386 /* Flush pages modified by one entry. */
390 ObtainReadLock(&afs_bufferLock);
391 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
392 if (FidEq(tb->fid, fid) && tb->dirty) {
393 ObtainWriteLock(&tb->lock);
395 code = ReallyWrite(tb->fid, tb->page, tb->data);
397 ReleaseWriteLock(&tb->lock);
398 ReleaseReadLock(&afs_bufferLock);
403 ReleaseWriteLock(&tb->lock);
405 ReleaseReadLock(&afs_bufferLock);
412 /* Flush all the modified buffers. */
415 afs_int32 code, rcode;
419 ObtainReadLock(&afs_bufferLock);
420 for (i = 0; i < nbuffers; i++, tbp++) {
422 ObtainWriteLock(&(*tbp)->lock);
424 ReleaseReadLock(&afs_bufferLock);
426 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
428 (*tbp)->dirty = 0; /* Clear the dirty flag */
429 if (code && !rcode) {
434 ReleaseWriteLock(&(*tbp)->lock);
435 ObtainReadLock(&afs_bufferLock);
438 ReleaseReadLock(&afs_bufferLock);
442 /* Same as read, only do *not* even try to read the page,
443 * since it probably doesn't exist.
446 DNew(afs_int32 *fid, int page, struct DirBuffer *entry)
450 memset(entry,0, sizeof(struct DirBuffer));
452 ObtainWriteLock(&afs_bufferLock);
453 if ((tb = newslot(fid, page, 0)) == 0) {
454 ReleaseWriteLock(&afs_bufferLock);
457 ObtainWriteLock(&tb->lock);
459 ReleaseWriteLock(&afs_bufferLock);
460 ReleaseWriteLock(&tb->lock);
463 entry->data = tb->data;