1 /* $Id: cfile.c,v 1.27 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
33 static int write_head (CFile cf)
35 int left = cf->head.hash_size * sizeof(int);
37 const char *tab = (char*) cf->array;
41 while (left >= (int) HASH_BSIZE)
43 mf_write (cf->hash_mf, bno++, 0, 0, tab);
48 mf_write (cf->hash_mf, bno, 0, left, tab);
52 static int read_head (CFile cf)
54 int left = cf->head.hash_size * sizeof(int);
56 char *tab = (char*) cf->array;
60 while (left >= (int) HASH_BSIZE)
62 mf_read (cf->hash_mf, bno++, 0, 0, tab);
67 mf_read (cf->hash_mf, bno, 0, left, tab);
72 CFile cf_open (MFile mf, MFile_area area, const char *fname,
73 int block_size, int wflag, int *firstp)
77 CFile cf = (CFile) xmalloc (sizeof(*cf));
81 logf (LOG_DEBUG, "cf: open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
82 sprintf (path, "%s-b", fname);
83 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
85 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
88 sprintf (path, "%s-i", fname);
89 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
91 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
95 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
100 cf->head.block_size = block_size;
101 cf->head.hash_size = 199;
102 hash_bytes = cf->head.hash_size * sizeof(int);
103 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
104 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
105 cf->head.next_block = 1;
107 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
108 cf->array = (int *) xmalloc (hash_bytes);
109 for (i = 0; i<cf->head.hash_size; i++)
117 assert (cf->head.block_size == block_size);
118 assert (cf->head.hash_size > 2);
119 hash_bytes = cf->head.hash_size * sizeof(int);
120 assert (cf->head.next_bucket > 0);
121 assert (cf->head.next_block > 0);
122 if (cf->head.state == 1)
123 cf->array = (int *) xmalloc (hash_bytes);
128 if (cf->head.state == 1)
130 cf->parray = (struct CFile_hash_bucket **)
131 xmalloc (cf->head.hash_size * sizeof(*cf->parray));
132 for (i = 0; i<cf->head.hash_size; i++)
133 cf->parray[i] = NULL;
137 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
138 cf->bucket_in_memory = 0;
139 cf->max_bucket_in_memory = 100;
141 cf->iobuf = (char *) xmalloc (cf->head.block_size);
142 memset (cf->iobuf, 0, cf->head.block_size);
145 zebra_mutex_init (&cf->mutex);
149 static int cf_hash (CFile cf, int no)
151 return (no>>3) % cf->head.hash_size;
154 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
157 p->lru_prev->lru_next = p->lru_next;
159 cf->bucket_lru_back = p->lru_next;
161 p->lru_next->lru_prev = p->lru_prev;
163 cf->bucket_lru_front = p->lru_prev;
165 *p->h_prev = p->h_next;
167 p->h_next->h_prev = p->h_prev;
169 --(cf->bucket_in_memory);
173 static void flush_bucket (CFile cf, int no_to_flush)
176 struct CFile_hash_bucket *p;
178 for (i = 0; i != no_to_flush; i++)
180 p = cf->bucket_lru_back;
185 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
188 release_bucket (cf, p);
192 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
194 struct CFile_hash_bucket *p, **pp;
196 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
197 flush_bucket (cf, 1);
198 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
199 ++(cf->bucket_in_memory);
200 p = (struct CFile_hash_bucket *) xmalloc (sizeof(*p));
203 p->lru_prev = cf->bucket_lru_front;
204 if (cf->bucket_lru_front)
205 cf->bucket_lru_front->lru_next = p;
207 cf->bucket_lru_back = p;
208 cf->bucket_lru_front = p;
210 pp = cf->parray + hno;
214 (*pp)->h_prev = &p->h_next;
219 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
221 struct CFile_hash_bucket *p;
223 p = alloc_bucket (cf, block_no, hno);
224 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
226 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
229 assert (p->ph.this_bucket == block_no);
234 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
236 struct CFile_hash_bucket *p;
239 block_no = *block_nop = cf->head.next_bucket++;
240 p = alloc_bucket (cf, block_no, hno);
242 for (i = 0; i<HASH_BUCKET; i++)
247 p->ph.next_bucket = 0;
248 p->ph.this_bucket = block_no;
253 static int cf_lookup_flat (CFile cf, int no)
255 int hno = (no*sizeof(int))/HASH_BSIZE;
256 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
259 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
263 static int cf_lookup_hash (CFile cf, int no)
265 int hno = cf_hash (cf, no);
266 struct CFile_hash_bucket *hb;
269 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
271 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
272 if (hb->ph.no[i] == no)
275 return hb->ph.vno[i];
278 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
280 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
282 if (hb->ph.this_bucket == block_no)
288 /* extra check ... */
289 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
291 if (hb->ph.this_bucket == block_no)
293 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
296 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
297 if (hb->ph.no[i] == no)
299 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
305 hb = get_bucket (cf, block_no, hno);
306 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
307 if (hb->ph.no[i] == no)
308 return hb->ph.vno[i];
313 static void cf_write_flat (CFile cf, int no, int vno)
315 int hno = (no*sizeof(int))/HASH_BSIZE;
316 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
318 hno += cf->head.next_bucket;
319 if (hno >= cf->head.flat_bucket)
320 cf->head.flat_bucket = hno+1;
322 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
325 static void cf_moveto_flat (CFile cf)
327 struct CFile_hash_bucket *p;
330 logf (LOG_DEBUG, "cf: Moving to flat shadow: %s", cf->rmf->name);
331 logf (LOG_DEBUG, "cf: hits=%d miss=%d bucket_in_memory=%d total=%d",
332 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
333 cf->head.next_bucket - cf->head.first_bucket);
334 assert (cf->head.state == 1);
335 flush_bucket (cf, -1);
336 assert (cf->bucket_in_memory == 0);
337 p = (struct CFile_hash_bucket *) xmalloc (sizeof(*p));
338 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
340 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
342 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
345 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
346 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
357 static int cf_lookup (CFile cf, int no)
359 if (cf->head.state > 1)
360 return cf_lookup_flat (cf, no);
361 return cf_lookup_hash (cf, no);
364 static int cf_new_flat (CFile cf, int no)
366 int vno = (cf->head.next_block)++;
368 cf_write_flat (cf, no, vno);
372 static int cf_new_hash (CFile cf, int no)
374 int hno = cf_hash (cf, no);
375 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
376 int *bucketpp = &cf->array[hno];
377 int i, vno = (cf->head.next_block)++;
379 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
380 if (!hb->ph.vno[HASH_BUCKET-1])
381 for (i = 0; i<HASH_BUCKET; i++)
393 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
394 if (hb->ph.this_bucket == *bucketpp)
396 bucketpp = &hb->ph.next_bucket;
404 /* extra check ... */
405 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
407 if (hb->ph.this_bucket == *bucketpp)
409 logf (LOG_FATAL, "Found hash bucket on other chain");
415 hb = get_bucket (cf, *bucketpp, hno);
417 for (i = 0; i<HASH_BUCKET; i++)
425 bucketpp = &hb->ph.next_bucket;
430 hb = new_bucket (cf, bucketpp, hno);
436 int cf_new (CFile cf, int no)
438 if (cf->head.state > 1)
439 return cf_new_flat (cf, no);
440 if (cf->no_miss*2 > cf->no_hits)
443 assert (cf->head.state > 1);
444 return cf_new_flat (cf, no);
446 return cf_new_hash (cf, no);
450 int cf_read (CFile cf, int no, int offset, int nbytes, void *buf)
455 zebra_mutex_lock (&cf->mutex);
456 if (!(block = cf_lookup (cf, no)))
458 zebra_mutex_unlock (&cf->mutex);
461 zebra_mutex_unlock (&cf->mutex);
462 if (!mf_read (cf->block_mf, block, offset, nbytes, buf))
464 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
470 int cf_write (CFile cf, int no, int offset, int nbytes, const void *buf)
475 zebra_mutex_lock (&cf->mutex);
476 if (!(block = cf_lookup (cf, no)))
478 block = cf_new (cf, no);
479 if (offset || nbytes)
481 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
482 memcpy (cf->iobuf + offset, buf, nbytes);
488 zebra_mutex_unlock (&cf->mutex);
489 if (mf_write (cf->block_mf, block, offset, nbytes, buf))
491 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
497 int cf_close (CFile cf)
499 logf (LOG_DEBUG, "cf: close hits=%d miss=%d bucket_in_memory=%d total=%d",
500 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
501 cf->head.next_bucket - cf->head.first_bucket);
502 flush_bucket (cf, -1);
505 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
508 mf_close (cf->hash_mf);
509 mf_close (cf->block_mf);
513 zebra_mutex_destroy (&cf->mutex);