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