7863ced221248835e85eab90f0ef99bf1f37dbb0
[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 #include "dir.h"
20
21 struct Lock afs_bufferLock;
22
23 /* page size */
24 #define BUFFER_PAGE_SIZE 2048
25 /* log page size */
26 #define LOGPS 11
27 /* page hash table size */
28 #define PHSIZE 32
29 /* The hash table should be somewhat efficient even if there are only
30  * a few partitions (less than 32). So the hash for the fileserver is now
31  * based on the volume id. This means this macro is dependent upon the
32  * layout of DirHandle in viced/viced.h, vol/salvage.h and volser/salvage.h.
33  */
34 #define pHash(fid) ((fid)[0] & (PHSIZE-1))
35 #define vHash(vid) (vid & (PHSIZE-1))
36
37 /* admittedly system dependent, this is the maximum signed 32-bit value */
38 #define BUFFER_LONG_MAX   2147483647
39 #ifndef NULL
40 #define NULL 0
41 #endif
42
43 #ifdef AFS_64BIT_IOPS_ENV
44 #define BUFFER_FID_SIZE (9 + 2*sizeof(char*)/sizeof(int))
45 #else
46 #define BUFFER_FID_SIZE (6 + 2*sizeof(char*)/sizeof(int))
47 #endif
48
49 static struct buffer {
50     /* fid is used for Unique cache key + i/o addressing.
51      * fid size is based on 4 + size of inode and size of pointer
52      */
53     afs_int32 fid[BUFFER_FID_SIZE];
54     afs_int32 page;
55     afs_int32 accesstime;
56     struct buffer *hashNext;
57     void *data;
58     char lockers;
59     char dirty;
60     char hashIndex;
61     struct Lock lock;
62 } **Buffers;
63
64 char *BufferData;
65
66 static struct buffer *phTable[PHSIZE];  /* page hash table */
67 static struct buffer *LastBuffer;
68 int nbuffers;
69 int timecounter;
70 static int calls = 0, ios = 0;
71
72 struct buffer *newslot();
73
74 int
75 DStat(abuffers, acalls, aios)
76      int *abuffers, *acalls, *aios;
77 {
78     *abuffers = nbuffers;
79     *acalls = calls;
80     *aios = ios;
81     return 0;
82 }
83
84 int
85 DInit(abuffers)
86      int abuffers;
87 {
88     /* Initialize the venus buffer system. */
89     register int i, tsize;
90     register struct buffer *tb;
91     register char *tp;
92
93     Lock_Init(&afs_bufferLock);
94     /* Align each element of Buffers on a doubleword boundary */
95     tsize = (sizeof(struct buffer) + 7) & ~7;
96     tp = (char *)malloc(abuffers * tsize);
97     Buffers = (struct buffer **)malloc(abuffers * sizeof(struct buffer *));
98     BufferData = (char *)malloc(abuffers * BUFFER_PAGE_SIZE);
99     timecounter = 0;
100     LastBuffer = (struct buffer *)tp;
101     nbuffers = abuffers;
102     for (i = 0; i < PHSIZE; i++)
103         phTable[i] = 0;
104     for (i = 0; i < abuffers; i++) {
105         /* Fill in each buffer with an empty indication. */
106         tb = (struct buffer *)tp;
107         Buffers[i] = tb;
108         tp += tsize;
109         FidZero(tb->fid);
110         tb->accesstime = tb->lockers = 0;
111         tb->data = &BufferData[BUFFER_PAGE_SIZE * i];
112         tb->hashIndex = 0;
113         tb->dirty = 0;
114         Lock_Init(&tb->lock);
115     }
116     return 0;
117 }
118
119 void *
120 DRead(fid, page)
121      register afs_int32 *fid;
122      register int page;
123 {
124     /* Read a page from the disk. */
125     register struct buffer *tb, *tb2, **bufhead;
126
127     ObtainWriteLock(&afs_bufferLock);
128     calls++;
129
130 #define bufmatch(tb) (tb->page == page && FidEq(tb->fid, fid))
131 #define buf_Front(head,parent,p) {(parent)->hashNext = (p)->hashNext; (p)->hashNext= *(head);*(head)=(p);}
132
133     /* this apparently-complicated-looking code is simply an example of
134      * a little bit of loop unrolling, and is a standard linked-list
135      * traversal trick. It saves a few assignments at the the expense
136      * of larger code size.  This could be simplified by better use of
137      * macros.  With the use of these LRU queues, the old one-cache is
138      * probably obsolete.
139      */
140     if (tb = phTable[pHash(fid)]) {     /* ASSMT HERE */
141         if (bufmatch(tb)) {
142             ObtainWriteLock(&tb->lock);
143             tb->lockers++;
144             ReleaseWriteLock(&afs_bufferLock);
145             tb->accesstime = ++timecounter;
146             ReleaseWriteLock(&tb->lock);
147             return tb->data;
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                 } else
171                     break;
172             }
173         }
174     } else
175         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     } else {
237         lp = 0;
238         lt = BUFFER_LONG_MAX;
239     }
240
241     tbp = Buffers;
242     for (i = 0; i < nbuffers; i++, tbp++) {
243         if ((*tbp)->lockers == 0) {
244             if ((*tbp)->accesstime < lt) {
245                 lp = (*tbp);
246                 lt = (*tbp)->accesstime;
247             }
248         }
249     }
250
251     /* There are no unlocked buffers */
252     if (lp == 0) {
253         if (lt < 0)
254             Die("accesstime counter wrapped");
255         else
256             Die("all buffers locked");
257     }
258
259     /* We do not need to lock the buffer here because it has no lockers
260      * and the afs_bufferLock prevents other threads from zapping this
261      * buffer while we are writing it out */
262     if (lp->dirty) {
263         if (ReallyWrite(lp->fid, lp->page, lp->data))
264             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)
288         return;
289     index = ((char *)bp - BufferData) >> LOGPS;
290     bp = Buffers[index];
291     ObtainWriteLock(&bp->lock);
292     bp->lockers--;
293     if (flag)
294         bp->dirty = 1;
295     ReleaseWriteLock(&bp->lock);
296 }
297
298 int
299 DVOffset(ap)
300      register void *ap;
301 {
302     /* Return the byte within a file represented by a buffer pointer. */
303     register struct buffer *bp = ap;
304     register int index;
305
306     index = ((char *)bp - BufferData) >> LOGPS;
307     if (index < 0 || index >= nbuffers)
308         return -1;
309     bp = Buffers[index];
310     return BUFFER_PAGE_SIZE * bp->page + (char *)ap - (char *)bp->data;
311 }
312
313 void
314 DZap(fid)
315      register afs_int32 *fid;
316 {
317     /* Destroy all buffers pertaining to a particular fid. */
318     register struct buffer *tb;
319     ObtainReadLock(&afs_bufferLock);
320     for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
321         if (FidEq(tb->fid, fid)) {
322             ObtainWriteLock(&tb->lock);
323             FidZap(tb->fid);
324             tb->dirty = 0;
325             ReleaseWriteLock(&tb->lock);
326         }
327     ReleaseReadLock(&afs_bufferLock);
328 }
329
330 int
331 DFlushVolume(vid)
332      register afs_int32 vid;
333 {
334     /* Flush all data and release all inode handles for a particular volume */
335     register struct buffer *tb;
336     register int code, rcode = 0;
337     ObtainReadLock(&afs_bufferLock);
338     for (tb = phTable[vHash(vid)]; tb; tb = tb->hashNext)
339         if (FidVolEq(tb->fid, vid)) {
340             ObtainWriteLock(&tb->lock);
341             if (tb->dirty) {
342                 code = ReallyWrite(tb->fid, tb->page, tb->data);
343                 if (code && !rcode)
344                     rcode = code;
345                 tb->dirty = 0;
346             }
347             FidZap(tb->fid);
348             ReleaseWriteLock(&tb->lock);
349         }
350     ReleaseReadLock(&afs_bufferLock);
351     return rcode;
352 }
353
354 int
355 DFlushEntry(fid)
356      register afs_int32 *fid;
357 {
358     /* Flush pages modified by one entry. */
359     register struct buffer *tb;
360     int code;
361
362     ObtainReadLock(&afs_bufferLock);
363     for (tb = phTable[pHash(fid)]; tb; tb = tb->hashNext)
364         if (FidEq(tb->fid, fid) && tb->dirty) {
365             ObtainWriteLock(&tb->lock);
366             if (tb->dirty) {
367                 code = ReallyWrite(tb->fid, tb->page, tb->data);
368                 if (code) {
369                     ReleaseWriteLock(&tb->lock);
370                     ReleaseReadLock(&afs_bufferLock);
371                     return code;
372                 }
373                 tb->dirty = 0;
374             }
375             ReleaseWriteLock(&tb->lock);
376         }
377     ReleaseReadLock(&afs_bufferLock);
378     return 0;
379 }
380
381 int
382 DFlush()
383 {
384     /* Flush all the modified buffers. */
385     register int i;
386     register struct buffer **tbp;
387     afs_int32 code, rcode;
388
389     rcode = 0;
390     tbp = Buffers;
391     ObtainReadLock(&afs_bufferLock);
392     for (i = 0; i < nbuffers; i++, tbp++) {
393         if ((*tbp)->dirty) {
394             ObtainWriteLock(&(*tbp)->lock);
395             (*tbp)->lockers++;
396             ReleaseReadLock(&afs_bufferLock);
397             if ((*tbp)->dirty) {
398                 code = ReallyWrite((*tbp)->fid, (*tbp)->page, (*tbp)->data);
399                 if (!code)
400                     (*tbp)->dirty = 0;  /* Clear the dirty flag */
401                 if (code && !rcode) {
402                     rcode = code;
403                 }
404             }
405             (*tbp)->lockers--;
406             ReleaseWriteLock(&(*tbp)->lock);
407             ObtainReadLock(&afs_bufferLock);
408         }
409     }
410     ReleaseReadLock(&afs_bufferLock);
411     return rcode;
412 }
413
414 void *
415 DNew(fid, page)
416      register int page;
417      register afs_int32 *fid;
418 {
419     /* Same as read, only do *not* even try to read the page,
420      * since it probably doesn't exist.
421      */
422     register struct buffer *tb;
423     ObtainWriteLock(&afs_bufferLock);
424     if ((tb = newslot(fid, page, 0)) == 0) {
425         ReleaseWriteLock(&afs_bufferLock);
426         return 0;
427     }
428     ObtainWriteLock(&tb->lock);
429     tb->lockers++;
430     ReleaseWriteLock(&afs_bufferLock);
431     ReleaseWriteLock(&tb->lock);
432     return tb->data;
433 }