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