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>
20 #ifdef AFS_64BIT_IOPS_ENV
21 #define BUFFER_FID_SIZE (9*sizeof(int) + 2*sizeof(char*))
23 #define BUFFER_FID_SIZE (6*sizeof(int) + 2*sizeof(char*))
27 /* fid is used for Unique cache key + i/o addressing.
28 * fid size is based on 4 + size of inode and size of pointer
30 char fid[BUFFER_FID_SIZE];
33 struct buffer *hashNext;
41 static_inline dir_file_t
42 bufferDir(struct buffer *b)
44 return (dir_file_t) &b->fid;
47 struct Lock afs_bufferLock;
50 #define BUFFER_PAGE_SIZE 2048
53 /* page hash table size */
55 /* The hash table should be somewhat efficient even if there are only
56 * a few partitions (less than 32). So the hash for the fileserver is now
57 * based on the volume id. This means this macro is dependent upon the
58 * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
60 #define pHash(fid) (((afs_int32 *)fid)[0] & (PHSIZE-1))
61 #define vHash(vid) (vid & (PHSIZE-1))
63 /* admittedly system dependent, this is the maximum signed 32-bit value */
64 #define BUFFER_LONG_MAX 2147483647
69 static struct buffer **Buffers;
73 static struct buffer *phTable[PHSIZE]; /* page hash table */
74 static struct buffer *LastBuffer;
77 static int calls = 0, ios = 0;
79 struct buffer *newslot(dir_file_t dir, afs_int32 apage,
82 /* XXX - This sucks. The correct prototypes for these functions are ...
84 * extern void FidZero(DirHandle *);
85 * extern int FidEq(DirHandle *a, DirHandle *b);
86 * extern int ReallyRead(DirHandle *a, int block, char *data, int *physerr);
89 extern void FidZero(dir_file_t);
90 extern int FidEq(dir_file_t, dir_file_t);
91 extern int ReallyRead(dir_file_t, int block, char *data, int *physerr);
92 extern int ReallyWrite(dir_file_t, int block, char *data);
93 extern void FidZap(dir_file_t);
94 extern int FidVolEq(dir_file_t, afs_int32 vid);
95 extern void FidCpy(dir_file_t, dir_file_t fromfile);
98 DStat(int *abuffers, int *acalls, int *aios)
100 *abuffers = nbuffers;
107 * initialize the directory package.
109 * @param[in] abuffers size of directory buffer cache
111 * @return operation status
117 /* Initialize the venus buffer system. */
122 Lock_Init(&afs_bufferLock);
123 /* Align each element of Buffers on a doubleword boundary */
124 tsize = (sizeof(struct buffer) + 7) & ~7;
125 tp = malloc(abuffers * tsize);
126 Buffers = malloc(abuffers * sizeof(struct buffer *));
127 BufferData = malloc(abuffers * BUFFER_PAGE_SIZE);
129 LastBuffer = (struct buffer *)tp;
131 for (i = 0; i < PHSIZE; i++)
133 for (i = 0; i < abuffers; i++) {
134 /* Fill in each buffer with an empty indication. */
135 tb = (struct buffer *)tp;
138 FidZero(bufferDir(tb));
139 tb->accesstime = tb->lockers = 0;
140 tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
143 Lock_Init(&tb->lock);
149 * read a page out of a directory object.
151 * @param[in] fid directory object fid
152 * @param[in] page page in hash table to be read
153 * @param[out] entry buffer to be filled w/ page contents
154 * @param[out] physerr (optional) pointer to return possible errno
157 * @retval EIO logical directory error or physical IO error;
158 * if *physerr was supplied, it will be set to 0
159 * for logical errors, or the errno for physical
163 DReadWithErrno(dir_file_t fid, int page, struct DirBuffer *entry, int *physerr)
165 /* Read a page from the disk. */
166 struct buffer *tb, *tb2, **bufhead;
172 memset(entry, 0, sizeof(struct DirBuffer));
174 ObtainWriteLock(&afs_bufferLock);
177 #define bufmatch(tb,fid) (tb->page == page && FidEq(bufferDir(tb), fid))
178 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
180 /* this apparently-complicated-looking code is simply an example of
181 * a little bit of loop unrolling, and is a standard linked-list
182 * traversal trick. It saves a few assignments at the the expense
183 * of larger code size. This could be simplified by better use of
184 * macros. With the use of these LRU queues, the old one-cache is
187 if ((tb = phTable[pHash(fid)])) { /* ASSMT HERE */
188 if (bufmatch(tb, fid)) {
189 ObtainWriteLock(&tb->lock);
191 ReleaseWriteLock(&afs_bufferLock);
192 tb->accesstime = ++timecounter;
193 ReleaseWriteLock(&tb->lock);
195 entry->data = tb->data;
198 bufhead = &(phTable[pHash(fid)]);
199 while ((tb2 = tb->hashNext)) {
200 if (bufmatch(tb2, fid)) {
201 buf_Front(bufhead, tb, tb2);
202 ObtainWriteLock(&tb2->lock);
204 ReleaseWriteLock(&afs_bufferLock);
205 tb2->accesstime = ++timecounter;
206 ReleaseWriteLock(&tb2->lock);
208 entry->data = tb2->data;
211 if ((tb = tb2->hashNext)) { /* ASSIGNMENT HERE! */
212 if (bufmatch(tb, fid)) {
213 buf_Front(bufhead, tb2, tb);
214 ObtainWriteLock(&tb->lock);
216 ReleaseWriteLock(&afs_bufferLock);
217 tb->accesstime = ++timecounter;
218 ReleaseWriteLock(&tb->lock);
220 entry->data = tb->data;
231 /* The last thing we looked at was either tb or tb2 (or nothing). That
232 * is at least the oldest buffer on one particular hash chain, so it's
233 * a pretty good place to start looking for the truly oldest buffer.
235 tb = newslot(fid, page, (tb ? tb : tb2));
237 ObtainWriteLock(&tb->lock);
239 ReleaseWriteLock(&afs_bufferLock);
240 code = ReallyRead(bufferDir(tb), tb->page, tb->data, physerr);
243 FidZap(bufferDir(tb)); /* disaster */
244 ReleaseWriteLock(&tb->lock);
247 /* Note that findslot sets the page field in the buffer equal to
248 * what it is searching for.
250 ReleaseWriteLock(&tb->lock);
252 entry->data = tb->data;
257 * read a page out of a directory object.
259 * @param[in] fid directory object fid
260 * @param[in] page page in hash table to be read
261 * @param[out] entry buffer to be filled w/ page contents
264 * @retval EIO logical directory error or physical IO error
267 DRead(dir_file_t fid, int page, struct DirBuffer *entry)
269 return DReadWithErrno(fid, page, entry, NULL);
274 FixupBucket(struct buffer *ap)
276 struct buffer **lp, *tp;
279 /* first try to get it out of its current hash bucket, in which it might not be */
282 for (tp = *lp; tp; tp = tp->hashNext) {
289 /* now figure the new hash bucket */
291 ap->hashIndex = i; /* remember where we are for deletion */
292 ap->hashNext = phTable[i]; /* add us to the list */
293 phTable[i] = ap; /* at the front, since it's LRU */
298 newslot(dir_file_t dir, afs_int32 apage, struct buffer *lp)
300 /* Find a usable buffer slot */
305 if (lp && (lp->lockers == 0)) {
309 lt = BUFFER_LONG_MAX;
313 for (i = 0; i < nbuffers; i++, tbp++) {
314 if ((*tbp)->lockers == 0) {
315 if ((*tbp)->accesstime < lt) {
317 lt = (*tbp)->accesstime;
322 /* There are no unlocked buffers */
325 Die("accesstime counter wrapped");
327 Die("all buffers locked");
330 /* We do not need to lock the buffer here because it has no lockers
331 * and the afs_bufferLock prevents other threads from zapping this
332 * buffer while we are writing it out */
334 if (ReallyWrite(bufferDir(lp), lp->page, lp->data))
335 Die("writing bogus buffer");
339 /* Now fill in the header. */
340 FidZap(bufferDir(lp));
341 FidCpy(bufferDir(lp), dir); /* set this */
342 memset(lp->data, 0, BUFFER_PAGE_SIZE); /* Don't leak stale data. */
344 lp->accesstime = ++timecounter;
346 FixupBucket(lp); /* move to the right hash bucket */
351 /* Release a buffer, specifying whether or not the buffer has been modified
354 DRelease(struct DirBuffer *entry, int flag)
358 bp = (struct buffer *) entry->buffer;
361 ObtainWriteLock(&bp->lock);
365 ReleaseWriteLock(&bp->lock);
368 /* Return the byte within a file represented by a buffer pointer. */
370 DVOffset(struct DirBuffer *entry)
375 return BUFFER_PAGE_SIZE * bp->page + (char *)entry->data - (char *)bp->data;
381 /* Destroy all buffers pertaining to a particular fid. */
383 ObtainReadLock(&afs_bufferLock);
384 for (tb = phTable[pHash(dir)]; tb; tb = tb->hashNext)
385 if (FidEq(bufferDir(tb), dir)) {
386 ObtainWriteLock(&tb->lock);
387 FidZap(bufferDir(tb));
389 ReleaseWriteLock(&tb->lock);
391 ReleaseReadLock(&afs_bufferLock);
395 DFlushVolume(afs_int32 vid)
397 /* Flush all data and release all inode handles for a particular volume */
400 ObtainReadLock(&afs_bufferLock);
401 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
402 if (FidVolEq(bufferDir(tb), vid)) {
403 ObtainWriteLock(&tb->lock);
405 code = ReallyWrite(bufferDir(tb), tb->page, tb->data);
410 FidZap(bufferDir(tb));
411 ReleaseWriteLock(&tb->lock);
413 ReleaseReadLock(&afs_bufferLock);
418 DFlushEntry(dir_file_t fid)
420 /* Flush pages modified by one entry. */
424 ObtainReadLock(&afs_bufferLock);
425 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
426 if (FidEq(bufferDir(tb), fid) && tb->dirty) {
427 ObtainWriteLock(&tb->lock);
429 code = ReallyWrite(bufferDir(tb), tb->page, tb->data);
431 ReleaseWriteLock(&tb->lock);
432 ReleaseReadLock(&afs_bufferLock);
437 ReleaseWriteLock(&tb->lock);
439 ReleaseReadLock(&afs_bufferLock);
446 /* Flush all the modified buffers. */
449 afs_int32 code, rcode;
453 ObtainReadLock(&afs_bufferLock);
454 for (i = 0; i < nbuffers; i++, tbp++) {
456 ObtainWriteLock(&(*tbp)->lock);
458 ReleaseReadLock(&afs_bufferLock);
460 code = ReallyWrite(bufferDir(*tbp), (*tbp)->page, (*tbp)->data);
462 (*tbp)->dirty = 0; /* Clear the dirty flag */
463 if (code && !rcode) {
468 ReleaseWriteLock(&(*tbp)->lock);
469 ObtainReadLock(&afs_bufferLock);
472 ReleaseReadLock(&afs_bufferLock);
476 /* Same as read, only do *not* even try to read the page,
477 * since it probably doesn't exist.
480 DNew(dir_file_t dir, int page, struct DirBuffer *entry)
484 memset(entry,0, sizeof(struct DirBuffer));
486 ObtainWriteLock(&afs_bufferLock);
487 if ((tb = newslot(dir, page, 0)) == 0) {
488 ReleaseWriteLock(&afs_bufferLock);
491 ObtainWriteLock(&tb->lock);
493 ReleaseWriteLock(&afs_bufferLock);
494 ReleaseWriteLock(&tb->lock);
497 entry->data = tb->data;