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>
19 #ifdef AFS_64BIT_IOPS_ENV
20 #define BUFFER_FID_SIZE (9*sizeof(int) + 2*sizeof(char*))
22 #define BUFFER_FID_SIZE (6*sizeof(int) + 2*sizeof(char*))
26 /* fid is used for Unique cache key + i/o addressing.
27 * fid size is based on 4 + size of inode and size of pointer
29 char fid[BUFFER_FID_SIZE];
32 struct buffer *hashNext;
40 static_inline dir_file_t
41 bufferDir(struct buffer *b)
43 return (dir_file_t) &b->fid;
46 struct Lock afs_bufferLock;
49 #define BUFFER_PAGE_SIZE 2048
52 /* page hash table size */
54 /* The hash table should be somewhat efficient even if there are only
55 * a few partitions (less than 32). So the hash for the fileserver is now
56 * based on the volume id. This means this macro is dependent upon the
57 * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
59 #define pHash(fid) (((afs_int32 *)fid)[0] & (PHSIZE-1))
60 #define vHash(vid) (vid & (PHSIZE-1))
62 /* admittedly system dependent, this is the maximum signed 32-bit value */
63 #define BUFFER_LONG_MAX 2147483647
68 static struct buffer **Buffers;
72 static struct buffer *phTable[PHSIZE]; /* page hash table */
73 static struct buffer *LastBuffer;
76 static int calls = 0, ios = 0;
78 struct buffer *newslot(dir_file_t dir, afs_int32 apage,
81 /* XXX - This sucks. The correct prototypes for these functions are ...
83 * extern void FidZero(DirHandle *);
84 * extern int FidEq(DirHandle *a, DirHandle *b);
85 * extern int ReallyRead(DirHandle *a, int block, char *data);
88 extern void FidZero(dir_file_t);
89 extern int FidEq(dir_file_t, dir_file_t);
90 extern int ReallyRead(dir_file_t, int block, char *data);
91 extern int ReallyWrite(dir_file_t, int block, char *data);
92 extern void FidZap(dir_file_t);
93 extern int FidVolEq(dir_file_t, afs_int32 vid);
94 extern void FidCpy(dir_file_t, dir_file_t fromfile);
97 DStat(int *abuffers, int *acalls, int *aios)
106 * initialize the directory package.
108 * @param[in] abuffers size of directory buffer cache
110 * @return operation status
116 /* Initialize the venus buffer system. */
121 Lock_Init(&afs_bufferLock);
122 /* Align each element of Buffers on a doubleword boundary */
123 tsize = (sizeof(struct buffer) + 7) & ~7;
124 tp = malloc(abuffers * tsize);
125 Buffers = malloc(abuffers * sizeof(struct buffer *));
126 BufferData = malloc(abuffers * BUFFER_PAGE_SIZE);
128 LastBuffer = (struct buffer *)tp;
130 for (i = 0; i < PHSIZE; i++)
132 for (i = 0; i < abuffers; i++) {
133 /* Fill in each buffer with an empty indication. */
134 tb = (struct buffer *)tp;
137 FidZero(bufferDir(tb));
138 tb->accesstime = tb->lockers = 0;
139 tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
142 Lock_Init(&tb->lock);
148 * read a page out of a directory object.
150 * @param[in] fid directory object fid
151 * @param[in] page page in hash table to be read
153 * @return pointer to requested page in directory cache
154 * @retval NULL read failed
157 DRead(dir_file_t fid, int page, struct DirBuffer *entry)
159 /* Read a page from the disk. */
160 struct buffer *tb, *tb2, **bufhead;
162 memset(entry, 0, sizeof(struct DirBuffer));
164 ObtainWriteLock(&afs_bufferLock);
167 #define bufmatch(tb,fid) (tb->page == page && FidEq(bufferDir(tb), fid))
168 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
170 /* this apparently-complicated-looking code is simply an example of
171 * a little bit of loop unrolling, and is a standard linked-list
172 * traversal trick. It saves a few assignments at the the expense
173 * of larger code size. This could be simplified by better use of
174 * macros. With the use of these LRU queues, the old one-cache is
177 if ((tb = phTable[pHash(fid)])) { /* ASSMT HERE */
178 if (bufmatch(tb, fid)) {
179 ObtainWriteLock(&tb->lock);
181 ReleaseWriteLock(&afs_bufferLock);
182 tb->accesstime = ++timecounter;
183 ReleaseWriteLock(&tb->lock);
185 entry->data = tb->data;
188 bufhead = &(phTable[pHash(fid)]);
189 while ((tb2 = tb->hashNext)) {
190 if (bufmatch(tb2, fid)) {
191 buf_Front(bufhead, tb, tb2);
192 ObtainWriteLock(&tb2->lock);
194 ReleaseWriteLock(&afs_bufferLock);
195 tb2->accesstime = ++timecounter;
196 ReleaseWriteLock(&tb2->lock);
198 entry->data = tb2->data;
201 if ((tb = tb2->hashNext)) { /* ASSIGNMENT HERE! */
202 if (bufmatch(tb, fid)) {
203 buf_Front(bufhead, tb2, tb);
204 ObtainWriteLock(&tb->lock);
206 ReleaseWriteLock(&afs_bufferLock);
207 tb->accesstime = ++timecounter;
208 ReleaseWriteLock(&tb->lock);
210 entry->data = tb->data;
221 /* The last thing we looked at was either tb or tb2 (or nothing). That
222 * is at least the oldest buffer on one particular hash chain, so it's
223 * a pretty good place to start looking for the truly oldest buffer.
225 tb = newslot(fid, page, (tb ? tb : tb2));
227 ObtainWriteLock(&tb->lock);
229 ReleaseWriteLock(&afs_bufferLock);
230 if (ReallyRead(bufferDir(tb), tb->page, tb->data)) {
232 FidZap(bufferDir(tb)); /* disaster */
233 ReleaseWriteLock(&tb->lock);
236 /* Note that findslot sets the page field in the buffer equal to
237 * what it is searching for.
239 ReleaseWriteLock(&tb->lock);
241 entry->data = tb->data;
247 FixupBucket(struct buffer *ap)
249 struct buffer **lp, *tp;
252 /* first try to get it out of its current hash bucket, in which it might not be */
255 for (tp = *lp; tp; tp = tp->hashNext) {
262 /* now figure the new hash bucket */
264 ap->hashIndex = i; /* remember where we are for deletion */
265 ap->hashNext = phTable[i]; /* add us to the list */
266 phTable[i] = ap; /* at the front, since it's LRU */
271 newslot(dir_file_t dir, afs_int32 apage, struct buffer *lp)
273 /* Find a usable buffer slot */
278 if (lp && (lp->lockers == 0)) {
282 lt = BUFFER_LONG_MAX;
286 for (i = 0; i < nbuffers; i++, tbp++) {
287 if ((*tbp)->lockers == 0) {
288 if ((*tbp)->accesstime < lt) {
290 lt = (*tbp)->accesstime;
295 /* There are no unlocked buffers */
298 Die("accesstime counter wrapped");
300 Die("all buffers locked");
303 /* We do not need to lock the buffer here because it has no lockers
304 * and the afs_bufferLock prevents other threads from zapping this
305 * buffer while we are writing it out */
307 if (ReallyWrite(bufferDir(lp), lp->page, lp->data))
308 Die("writing bogus buffer");
312 /* Now fill in the header. */
313 FidZap(bufferDir(lp));
314 FidCpy(bufferDir(lp), dir); /* set this */
316 lp->accesstime = ++timecounter;
318 FixupBucket(lp); /* move to the right hash bucket */
323 /* Release a buffer, specifying whether or not the buffer has been modified
326 DRelease(struct DirBuffer *entry, int flag)
330 bp = (struct buffer *) entry->buffer;
333 ObtainWriteLock(&bp->lock);
337 ReleaseWriteLock(&bp->lock);
340 /* Return the byte within a file represented by a buffer pointer. */
342 DVOffset(struct DirBuffer *entry)
347 return BUFFER_PAGE_SIZE * bp->page + (char *)entry->data - (char *)bp->data;
353 /* Destroy all buffers pertaining to a particular fid. */
355 ObtainReadLock(&afs_bufferLock);
356 for (tb = phTable[pHash(dir)]; tb; tb = tb->hashNext)
357 if (FidEq(bufferDir(tb), dir)) {
358 ObtainWriteLock(&tb->lock);
359 FidZap(bufferDir(tb));
361 ReleaseWriteLock(&tb->lock);
363 ReleaseReadLock(&afs_bufferLock);
367 DFlushVolume(afs_int32 vid)
369 /* Flush all data and release all inode handles for a particular volume */
372 ObtainReadLock(&afs_bufferLock);
373 for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
374 if (FidVolEq(bufferDir(tb), vid)) {
375 ObtainWriteLock(&tb->lock);
377 code = ReallyWrite(bufferDir(tb), tb->page, tb->data);
382 FidZap(bufferDir(tb));
383 ReleaseWriteLock(&tb->lock);
385 ReleaseReadLock(&afs_bufferLock);
390 DFlushEntry(dir_file_t fid)
392 /* Flush pages modified by one entry. */
396 ObtainReadLock(&afs_bufferLock);
397 for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
398 if (FidEq(bufferDir(tb), fid) && tb->dirty) {
399 ObtainWriteLock(&tb->lock);
401 code = ReallyWrite(bufferDir(tb), tb->page, tb->data);
403 ReleaseWriteLock(&tb->lock);
404 ReleaseReadLock(&afs_bufferLock);
409 ReleaseWriteLock(&tb->lock);
411 ReleaseReadLock(&afs_bufferLock);
418 /* Flush all the modified buffers. */
421 afs_int32 code, rcode;
425 ObtainReadLock(&afs_bufferLock);
426 for (i = 0; i < nbuffers; i++, tbp++) {
428 ObtainWriteLock(&(*tbp)->lock);
430 ReleaseReadLock(&afs_bufferLock);
432 code = ReallyWrite(bufferDir(*tbp), (*tbp)->page, (*tbp)->data);
434 (*tbp)->dirty = 0; /* Clear the dirty flag */
435 if (code && !rcode) {
440 ReleaseWriteLock(&(*tbp)->lock);
441 ObtainReadLock(&afs_bufferLock);
444 ReleaseReadLock(&afs_bufferLock);
448 /* Same as read, only do *not* even try to read the page,
449 * since it probably doesn't exist.
452 DNew(dir_file_t dir, int page, struct DirBuffer *entry)
456 memset(entry,0, sizeof(struct DirBuffer));
458 ObtainWriteLock(&afs_bufferLock);
459 if ((tb = newslot(dir, page, 0)) == 0) {
460 ReleaseWriteLock(&afs_bufferLock);
463 ObtainWriteLock(&tb->lock);
465 ReleaseWriteLock(&afs_bufferLock);
466 ReleaseWriteLock(&tb->lock);
469 entry->data = tb->data;