dir-prototypes-20090316
[openafs.git] / src / dir / buffer.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <stdlib.h>
17 #include <lock.h>
18
19 #ifdef AFS_64BIT_IOPS_ENV
20 #define BUFFER_FID_SIZE (9 + 2*sizeof(char*)/sizeof(int))
21 #else
22 #define BUFFER_FID_SIZE (6 + 2*sizeof(char*)/sizeof(int))
23 #endif
24
25 struct buffer {
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
28      */
29     afs_int32 fid[BUFFER_FID_SIZE];
30     afs_int32 page;
31     afs_int32 accesstime;
32     struct buffer *hashNext;
33     void *data;
34     char lockers;
35     char dirty;
36     char hashIndex;
37     struct Lock lock;
38 };
39
40 #include "dir.h"
41
42 struct Lock afs_bufferLock;
43
44 /* page size */
45 #define BUFFER_PAGE_SIZE 2048
46 /* log page size */
47 #define LOGPS 11
48 /* page hash table size */
49 #define PHSIZE 32
50 /* The hash table should be somewhat efficient even if there are only
51  * a few partitions (less than 32). So the hash for the fileserver is now
52  * based on the volume id. This means this macro is dependent upon the
53  * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
54  */
55 #define pHash(fid) ((fid)[0] & (PHSIZE-1))
56 #define vHash(vid) (vid & (PHSIZE-1))
57
58 /* admittedly system dependent, this is the maximum signed 32-bit value */
59 #define BUFFER_LONG_MAX   2147483647
60 #ifndef NULL
61 #define NULL 0
62 #endif
63
64 static struct buffer **Buffers;
65
66 char *BufferData;
67
68 static struct buffer *phTable[PHSIZE];  /* page hash table */
69 static struct buffer *LastBuffer;
70 int nbuffers;
71 int timecounter;
72 static int calls = 0, ios = 0;
73
74 struct buffer *newslot(afs_int32 *afid, afs_int32 apage, 
75                        struct buffer *lp);
76
77 /* XXX - This sucks. The correct prototypes for these functions are ...
78  *
79  * extern void FidZero(DirHandle *);
80  * extern int  FidEq(DirHandle *a, DirHandle *b);
81  * extern int  ReallyRead(DirHandle *a, int block, char *data);
82  */
83
84 extern void FidZero(afs_int32 *file);
85 extern int FidEq(afs_int32 *a, afs_int32 *b);
86 extern int ReallyRead(afs_int32 *file, int block, char *data);
87 extern int ReallyWrite(afs_int32 *file, int block, char *data);
88 extern void FidZap(afs_int32 *file);
89 extern int  FidVolEq(afs_int32 *file, afs_int32 vid);
90 extern void FidCpy(afs_int32 *tofile, afs_int32 *fromfile);
91 extern void Die(char *msg);
92
93 int
94 DStat(int *abuffers, int *acalls, int *aios)
95 {
96     *abuffers = nbuffers;
97     *acalls = calls;
98     *aios = ios;
99     return 0;
100 }
101
102 int
103 DInit(int abuffers)
104 {
105     /* Initialize the venus buffer system. */
106     register int i, tsize;
107     register struct buffer *tb;
108     register char *tp;
109
110     Lock_Init(&afs_bufferLock);
111     /* Align each element of Buffers on a doubleword boundary */
112     tsize = (sizeof(struct buffer) + 7) & ~7;
113     tp = (char *)malloc(abuffers * tsize);
114     Buffers = (struct buffer **)malloc(abuffers * sizeof(struct buffer *));
115     BufferData = (char *)malloc(abuffers * BUFFER_PAGE_SIZE);
116     timecounter = 0;
117     LastBuffer = (struct buffer *)tp;
118     nbuffers = abuffers;
119     for (i = 0; i < PHSIZE; i++)
120         phTable[i] = 0;
121     for (i = 0; i < abuffers; i++) {
122         /* Fill in each buffer with an empty indication. */
123         tb = (struct buffer *)tp;
124         Buffers[i] = tb;
125         tp += tsize;
126         FidZero(tb->fid);
127         tb->accesstime = tb->lockers = 0;
128         tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
129         tb->hashIndex = 0;
130         tb->dirty = 0;
131         Lock_Init(&tb->lock);
132     }
133     return 0;
134 }
135
136 void *
137 DRead(register afs_int32 *fid, register int page)
138 {
139     /* Read a page from the disk. */
140     register struct buffer *tb, *tb2, **bufhead;
141
142     ObtainWriteLock(&afs_bufferLock);
143     calls++;
144
145 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
146 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
147
148     /* this apparently-complicated-looking code is simply an example of
149      * a little bit of loop unrolling, and is a standard linked-list
150      * traversal trick. It saves a few assignments at the the expense
151      * of larger code size.  This could be simplified by better use of
152      * macros.  With the use of these LRU queues, the old one-cache is
153      * probably obsolete.
154      */
155     if ((tb = phTable[pHash(fid)])) {   /* ASSMT HERE */
156         if (bufmatch(tb)) {
157             ObtainWriteLock(&tb->lock);
158             tb->lockers++;
159             ReleaseWriteLock(&afs_bufferLock);
160             tb->accesstime = ++timecounter;
161             ReleaseWriteLock(&tb->lock);
162             return tb->data;
163         } else {
164             bufhead = &(phTable[pHash(fid)]);
165             while ((tb2 = tb->hashNext)) {
166                 if (bufmatch(tb2)) {
167                     buf_Front(bufhead, tb, tb2);
168                     ObtainWriteLock(&tb2->lock);
169                     tb2->lockers++;
170                     ReleaseWriteLock(&afs_bufferLock);
171                     tb2->accesstime = ++timecounter;
172                     ReleaseWriteLock(&tb2->lock);
173                     return tb2->data;
174                 }
175                 if ((tb = tb2->hashNext)) {     /* ASSIGNMENT HERE! */
176                     if (bufmatch(tb)) {
177                         buf_Front(bufhead, tb2, tb);
178                         ObtainWriteLock(&tb->lock);
179                         tb->lockers++;
180                         ReleaseWriteLock(&afs_bufferLock);
181                         tb->accesstime = ++timecounter;
182                         ReleaseWriteLock(&tb->lock);
183                         return tb->data;
184                     }
185                 } else
186                     break;
187             }
188         }
189     } else
190         tb2 = NULL;
191
192     /* can't find it */
193     /* The last thing we looked at was either tb or tb2 (or nothing). That
194      * is at least the oldest buffer on one particular hash chain, so it's
195      * a pretty good place to start looking for the truly oldest buffer.
196      */
197     tb = newslot(fid, page, (tb ? tb : tb2));
198     ios++;
199     ObtainWriteLock(&tb->lock);
200     tb->lockers++;
201     ReleaseWriteLock(&afs_bufferLock);
202     if (ReallyRead(tb->fid, tb->page, tb->data)) {
203         tb->lockers--;
204         FidZap(tb->fid);        /* disaster */
205         ReleaseWriteLock(&tb->lock);
206         return 0;
207     }
208     /* Note that findslot sets the page field in the buffer equal to
209      * what it is searching for.
210      */
211     ReleaseWriteLock(&tb->lock);
212     return tb->data;
213 }
214
215 static int
216 FixupBucket(register struct buffer *ap)
217 {
218     register struct buffer **lp, *tp;
219     register int i;
220
221     /* first try to get it out of its current hash bucket, in which it might not be */
222     i = ap->hashIndex;
223     lp = &phTable[i];
224     for (tp = *lp; tp; tp = tp->hashNext) {
225         if (tp == ap) {
226             *lp = tp->hashNext;
227             break;
228         }
229         lp = &tp->hashNext;
230     }
231     /* now figure the new hash bucket */
232     i = pHash(ap->fid);
233     ap->hashIndex = i;          /* remember where we are for deletion */
234     ap->hashNext = phTable[i];  /* add us to the list */
235     phTable[i] = ap;            /* at the front, since it's LRU */
236     return 0;
237 }
238
239 struct buffer *
240 newslot(afs_int32 *afid, afs_int32 apage, register struct buffer *lp)
241 {
242     /* Find a usable buffer slot */
243     register afs_int32 i;
244     afs_int32 lt;
245     register struct buffer **tbp;
246
247     if (lp && (lp->lockers == 0)) {
248         lt = lp->accesstime;
249     } else {
250         lp = 0;
251         lt = BUFFER_LONG_MAX;
252     }
253
254     tbp = Buffers;
255     for (i = 0; i < nbuffers; i++, tbp++) {
256         if ((*tbp)->lockers == 0) {
257             if ((*tbp)->accesstime < lt) {
258                 lp = (*tbp);
259                 lt = (*tbp)->accesstime;
260             }
261         }
262     }
263
264     /* There are no unlocked buffers */
265     if (lp == 0) {
266         if (lt < 0)
267             Die("accesstime counter wrapped");
268         else
269             Die("all buffers locked");
270     }
271
272     /* We do not need to lock the buffer here because it has no lockers
273      * and the afs_bufferLock prevents other threads from zapping this
274      * buffer while we are writing it out */
275     if (lp->dirty) {
276         if (ReallyWrite(lp->fid, lp->page, lp->data))
277             Die("writing bogus buffer");
278         lp->dirty = 0;
279     }
280
281     /* Now fill in the header. */
282     FidZap(lp->fid);
283     FidCpy(lp->fid, afid);      /* set this */
284     lp->page = apage;
285     lp->accesstime = ++timecounter;
286
287     FixupBucket(lp);            /* move to the right hash bucket */
288
289     return lp;
290 }
291
292 void
293 DRelease(register struct buffer *bp, int flag)
294 {
295     /* Release a buffer, specifying whether or not the buffer has been modified by the locker. */
296     register int index;
297
298     if (!bp)
299         return;
300     index = ((char *)bp - BufferData) >> LOGPS;
301     bp = Buffers[index];
302     ObtainWriteLock(&bp->lock);
303     bp->lockers--;
304     if (flag)
305         bp->dirty = 1;
306     ReleaseWriteLock(&bp->lock);
307 }
308
309 int
310 DVOffset(register void *ap)
311 {
312     /* Return the byte within a file represented by a buffer pointer. */
313     register struct buffer *bp = ap;
314     register int index;
315
316     index = ((char *)bp - BufferData) >> LOGPS;
317     if (index < 0 || index >= nbuffers)
318         return -1;
319     bp = Buffers[index];
320     return BUFFER_PAGE_SIZE * bp->page + (char *)ap - (char *)bp->data;
321 }
322
323 void
324 DZap(register afs_int32 *fid)
325 {
326     /* Destroy all buffers pertaining to a particular fid. */
327     register struct buffer *tb;
328     ObtainReadLock(&afs_bufferLock);
329     for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
330         if (FidEq(tb->fid, fid)) {
331             ObtainWriteLock(&tb->lock);
332             FidZap(tb->fid);
333             tb->dirty = 0;
334             ReleaseWriteLock(&tb->lock);
335         }
336     ReleaseReadLock(&afs_bufferLock);
337 }
338
339 int
340 DFlushVolume(register afs_int32 vid)
341 {
342     /* Flush all data and release all inode handles for a particular volume */
343     register struct buffer *tb;
344     register int code, rcode = 0;
345     ObtainReadLock(&afs_bufferLock);
346     for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
347         if (FidVolEq(tb->fid, vid)) {
348             ObtainWriteLock(&tb->lock);
349             if (tb->dirty) {
350                 code = ReallyWrite(tb->fid, tb->page, tb->data);
351                 if (code && !rcode)
352                     rcode = code;
353                 tb->dirty = 0;
354             }
355             FidZap(tb->fid);
356             ReleaseWriteLock(&tb->lock);
357         }
358     ReleaseReadLock(&afs_bufferLock);
359     return rcode;
360 }
361
362 int
363 DFlushEntry(register afs_int32 *fid)
364 {
365     /* Flush pages modified by one entry. */
366     register struct buffer *tb;
367     int code;
368
369     ObtainReadLock(&afs_bufferLock);
370     for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
371         if (FidEq(tb->fid, fid) && tb->dirty) {
372             ObtainWriteLock(&tb->lock);
373             if (tb->dirty) {
374                 code = ReallyWrite(tb->fid, tb->page, tb->data);
375                 if (code) {
376                     ReleaseWriteLock(&tb->lock);
377                     ReleaseReadLock(&afs_bufferLock);
378                     return code;
379                 }
380                 tb->dirty = 0;
381             }
382             ReleaseWriteLock(&tb->lock);
383         }
384     ReleaseReadLock(&afs_bufferLock);
385     return 0;
386 }
387
388 int
389 DFlush(void)
390 {
391     /* Flush all the modified buffers. */
392     register int i;
393     register struct buffer **tbp;
394     afs_int32 code, rcode;
395
396     rcode = 0;
397     tbp = Buffers;
398     ObtainReadLock(&afs_bufferLock);
399     for (i = 0; i < nbuffers; i++, tbp++) {
400         if ((*tbp)->dirty) {
401             ObtainWriteLock(&(*tbp)->lock);
402             (*tbp)->lockers++;
403             ReleaseReadLock(&afs_bufferLock);
404             if ((*tbp)->dirty) {
405                 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
406                 if (!code)
407                     (*tbp)->dirty = 0;  /* Clear the dirty flag */
408                 if (code && !rcode) {
409                     rcode = code;
410                 }
411             }
412             (*tbp)->lockers--;
413             ReleaseWriteLock(&(*tbp)->lock);
414             ObtainReadLock(&afs_bufferLock);
415         }
416     }
417     ReleaseReadLock(&afs_bufferLock);
418     return rcode;
419 }
420
421 void *
422 DNew(register afs_int32 *fid, register int page)
423 {
424     /* Same as read, only do *not* even try to read the page,
425      * since it probably doesn't exist.
426      */
427     register struct buffer *tb;
428     ObtainWriteLock(&afs_bufferLock);
429     if ((tb = newslot(fid, page, 0)) == 0) {
430         ReleaseWriteLock(&afs_bufferLock);
431         return 0;
432     }
433     ObtainWriteLock(&tb->lock);
434     tb->lockers++;
435     ReleaseWriteLock(&afs_bufferLock);
436     ReleaseWriteLock(&tb->lock);
437     return tb->data;
438 }