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