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