2 * Copyright (C) 1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1996-04-19 16:23:47 adam
8 * Serious bug fix in shadow implementation; function new_bucket might
9 * set wrong bucket number on new bucket.
11 * Revision 1.15 1996/04/18 16:02:56 adam
12 * Changed logging a bit.
13 * Removed warning message when commiting flat shadow files.
15 * Revision 1.14 1996/04/12 07:01:55 adam
16 * Yet another bug fix (next_block was initialized to 0; now set to 1).
18 * Revision 1.13 1996/04/09 14:48:49 adam
19 * Bug fix: offset calculation when using flat files was completely broken.
21 * Revision 1.12 1996/04/09 06:47:28 adam
22 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
24 * Revision 1.11 1996/03/26 15:59:05 adam
25 * The directory of the shadow table file can be specified by the new
28 * Revision 1.10 1996/02/07 14:03:46 adam
29 * Work on flat indexed shadow files.
31 * Revision 1.9 1996/02/07 10:08:43 adam
32 * Work on flat shadow (not finished yet).
34 * Revision 1.8 1995/12/15 12:36:52 adam
35 * Moved hash file information to union.
36 * Renamed commit files.
38 * Revision 1.7 1995/12/15 10:35:07 adam
39 * Changed names of commit files.
41 * Revision 1.6 1995/12/11 09:03:53 adam
42 * New function: cf_unlink.
43 * New member of commit file head: state (0) deleted, (1) hash file.
45 * Revision 1.5 1995/12/08 16:21:14 adam
46 * Work on commit/update.
48 * Revision 1.4 1995/12/01 16:24:28 adam
49 * Commit files use separate meta file area.
51 * Revision 1.3 1995/12/01 11:37:22 adam
52 * Cached/commit files implemented as meta-files.
54 * Revision 1.2 1995/11/30 17:00:49 adam
55 * Several bug fixes. Commit system runs now.
57 * Revision 1.1 1995/11/30 08:33:11 adam
58 * Started work on commit facility.
70 static int write_head (CFile cf)
72 int left = cf->head.hash_size * sizeof(int);
74 const char *tab = (char*) cf->array;
78 while (left >= HASH_BSIZE)
80 mf_write (cf->hash_mf, bno++, 0, 0, tab);
85 mf_write (cf->hash_mf, bno, 0, left, tab);
89 static int read_head (CFile cf)
91 int left = cf->head.hash_size * sizeof(int);
93 char *tab = (char*) cf->array;
97 while (left >= HASH_BSIZE)
99 mf_read (cf->hash_mf, bno++, 0, 0, tab);
104 mf_read (cf->hash_mf, bno, 0, left, tab);
109 CFile cf_open (MFile mf, MFile_area area, const char *fname,
110 int block_size, int wflag, int *firstp)
114 CFile cf = xmalloc (sizeof(*cf));
118 logf (LOG_LOG, "cf_open %s %s", cf->rmf->name, wflag ? "rdwr" : "rd");
119 sprintf (path, "%s-b", fname);
120 if (!(cf->block_mf = mf_open (area, path, block_size, wflag)))
122 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
125 sprintf (path, "%s-i", fname);
126 if (!(cf->hash_mf = mf_open (area, path, HASH_BSIZE, wflag)))
128 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", path);
132 if (!mf_read (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head) ||
137 cf->head.block_size = block_size;
138 cf->head.hash_size = 199;
139 hash_bytes = cf->head.hash_size * sizeof(int);
140 cf->head.flat_bucket = cf->head.next_bucket = cf->head.first_bucket =
141 (hash_bytes+sizeof(cf->head))/HASH_BSIZE + 2;
142 cf->head.next_block = 1;
144 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
145 cf->array = xmalloc (hash_bytes);
146 for (i = 0; i<cf->head.hash_size; i++)
154 assert (cf->head.block_size == block_size);
155 assert (cf->head.hash_size > 2);
156 hash_bytes = cf->head.hash_size * sizeof(int);
157 assert (cf->head.next_bucket > 0);
158 assert (cf->head.next_block > 0);
159 if (cf->head.state == 1)
160 cf->array = xmalloc (hash_bytes);
165 if (cf->head.state == 1)
167 cf->parray = xmalloc (cf->head.hash_size * sizeof(*cf->parray));
168 for (i = 0; i<cf->head.hash_size; i++)
169 cf->parray[i] = NULL;
173 cf->bucket_lru_front = cf->bucket_lru_back = NULL;
174 cf->bucket_in_memory = 0;
175 cf->max_bucket_in_memory = 100;
177 cf->iobuf = xmalloc (cf->head.block_size);
178 memset (cf->iobuf, 0, cf->head.block_size);
184 static int cf_hash (CFile cf, int no)
187 return (no>>3) % cf->head.hash_size;
189 return (no/(HASH_BUCKET*2+2)) % cf->head.hash_size;
193 static void release_bucket (CFile cf, struct CFile_hash_bucket *p)
196 p->lru_prev->lru_next = p->lru_next;
198 cf->bucket_lru_back = p->lru_next;
200 p->lru_next->lru_prev = p->lru_prev;
202 cf->bucket_lru_front = p->lru_prev;
204 *p->h_prev = p->h_next;
206 p->h_next->h_prev = p->h_prev;
208 --(cf->bucket_in_memory);
212 static void flush_bucket (CFile cf, int no_to_flush)
215 struct CFile_hash_bucket *p;
217 for (i = 0; i != no_to_flush; i++)
219 p = cf->bucket_lru_back;
224 mf_write (cf->hash_mf, p->ph.this_bucket, 0, 0, &p->ph);
227 release_bucket (cf, p);
231 static struct CFile_hash_bucket *alloc_bucket (CFile cf, int block_no, int hno)
233 struct CFile_hash_bucket *p, **pp;
235 if (cf->bucket_in_memory == cf->max_bucket_in_memory)
236 flush_bucket (cf, 1);
237 assert (cf->bucket_in_memory < cf->max_bucket_in_memory);
238 ++(cf->bucket_in_memory);
239 p = xmalloc (sizeof(*p));
242 p->lru_prev = cf->bucket_lru_front;
243 if (cf->bucket_lru_front)
244 cf->bucket_lru_front->lru_next = p;
246 cf->bucket_lru_back = p;
247 cf->bucket_lru_front = p;
249 pp = cf->parray + hno;
253 (*pp)->h_prev = &p->h_next;
258 static struct CFile_hash_bucket *get_bucket (CFile cf, int block_no, int hno)
260 struct CFile_hash_bucket *p;
262 p = alloc_bucket (cf, block_no, hno);
263 if (!mf_read (cf->hash_mf, block_no, 0, 0, &p->ph))
265 logf (LOG_FATAL|LOG_ERRNO, "read get_bucket");
268 assert (p->ph.this_bucket == block_no);
273 static struct CFile_hash_bucket *new_bucket (CFile cf, int *block_nop, int hno)
275 struct CFile_hash_bucket *p;
278 block_no = *block_nop = cf->head.next_bucket++;
279 p = alloc_bucket (cf, block_no, hno);
281 for (i = 0; i<HASH_BUCKET; i++)
286 p->ph.next_bucket = 0;
287 p->ph.this_bucket = block_no;
292 static int cf_lookup_flat (CFile cf, int no)
294 int hno = (no*sizeof(int))/HASH_BSIZE;
295 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
298 mf_read (cf->hash_mf, hno+cf->head.next_bucket, off, sizeof(int), &vno);
302 static int cf_lookup_hash (CFile cf, int no)
304 int hno = cf_hash (cf, no);
305 struct CFile_hash_bucket *hb;
308 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
310 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
311 if (hb->ph.no[i] == no)
314 return hb->ph.vno[i];
317 for (block_no = cf->array[hno]; block_no; block_no = hb->ph.next_bucket)
319 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
321 if (hb->ph.this_bucket == block_no)
327 /* extra check ... */
328 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
330 if (hb->ph.this_bucket == block_no)
332 logf (LOG_FATAL, "Found hash bucket on other chain (1)");
335 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
336 if (hb->ph.no[i] == no)
338 logf (LOG_FATAL, "Found hash bucket on other chain (2)");
344 hb = get_bucket (cf, block_no, hno);
345 for (i = 0; i<HASH_BUCKET && hb->ph.vno[i]; i++)
346 if (hb->ph.no[i] == no)
347 return hb->ph.vno[i];
352 static void cf_write_flat (CFile cf, int no, int vno)
354 int hno = (no*sizeof(int))/HASH_BSIZE;
355 int off = (no*sizeof(int)) - hno*HASH_BSIZE;
357 hno += cf->head.next_bucket;
358 if (hno >= cf->head.flat_bucket)
359 cf->head.flat_bucket = hno+1;
361 mf_write (cf->hash_mf, hno, off, sizeof(int), &vno);
364 static void cf_moveto_flat (CFile cf)
366 struct CFile_hash_bucket *p;
369 logf (LOG_LOG, "Moving to flat shadow: %s", cf->rmf->name);
370 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
371 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
372 cf->head.next_bucket - cf->head.first_bucket);
373 assert (cf->head.state == 1);
374 flush_bucket (cf, -1);
375 assert (cf->bucket_in_memory == 0);
376 p = xmalloc (sizeof(*p));
377 for (i = cf->head.first_bucket; i < cf->head.next_bucket; i++)
379 if (!mf_read (cf->hash_mf, i, 0, 0, &p->ph))
381 logf (LOG_FATAL|LOG_ERRNO, "read bucket moveto flat");
384 for (j = 0; j < HASH_BUCKET && p->ph.vno[j]; j++)
385 cf_write_flat (cf, p->ph.no[j], p->ph.vno[j]);
396 static int cf_lookup (CFile cf, int no)
398 if (cf->head.state > 1)
399 return cf_lookup_flat (cf, no);
400 return cf_lookup_hash (cf, no);
403 static int cf_new_flat (CFile cf, int no)
405 int vno = (cf->head.next_block)++;
407 cf_write_flat (cf, no, vno);
411 static int cf_new_hash (CFile cf, int no)
413 int hno = cf_hash (cf, no);
414 struct CFile_hash_bucket *hbprev = NULL, *hb = cf->parray[hno];
415 int *bucketpp = &cf->array[hno];
416 int i, vno = (cf->head.next_block)++;
418 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
419 if (!hb->ph.vno[HASH_BUCKET-1])
420 for (i = 0; i<HASH_BUCKET; i++)
432 for (hb = cf->parray[hno]; hb; hb = hb->h_next)
433 if (hb->ph.this_bucket == *bucketpp)
435 bucketpp = &hb->ph.next_bucket;
443 /* extra check ... */
444 for (hb = cf->bucket_lru_back; hb; hb = hb->lru_next)
446 if (hb->ph.this_bucket == *bucketpp)
448 logf (LOG_FATAL, "Found hash bucket on other chain");
454 hb = get_bucket (cf, *bucketpp, hno);
456 for (i = 0; i<HASH_BUCKET; i++)
464 bucketpp = &hb->ph.next_bucket;
469 hb = new_bucket (cf, bucketpp, hno);
475 int cf_new (CFile cf, int no)
477 if (cf->head.state > 1)
478 return cf_new_flat (cf, no);
479 if (cf->no_miss*2 > cf->no_hits)
482 assert (cf->head.state > 1);
483 return cf_new_flat (cf, no);
485 return cf_new_hash (cf, no);
489 int cf_read (CFile cf, int no, int offset, int num, void *buf)
494 if (!(block = cf_lookup (cf, no)))
496 if (!mf_read (cf->block_mf, block, offset, num, buf))
498 logf (LOG_FATAL|LOG_ERRNO, "cf_read no=%d, block=%d", no, block);
504 int cf_write (CFile cf, int no, int offset, int num, const void *buf)
509 if (!(block = cf_lookup (cf, no)))
511 block = cf_new (cf, no);
514 mf_read (cf->rmf, no, 0, 0, cf->iobuf);
515 memcpy (cf->iobuf + offset, buf, num);
521 if (mf_write (cf->block_mf, block, offset, num, buf))
523 logf (LOG_FATAL|LOG_ERRNO, "cf_write no=%d, block=%d", no, block);
529 int cf_close (CFile cf)
531 logf (LOG_LOG, "hits=%d miss=%d bucket_in_memory=%d total=%d",
532 cf->no_hits, cf->no_miss, cf->bucket_in_memory,
533 cf->head.next_bucket - cf->head.first_bucket);
534 flush_bucket (cf, -1);
537 logf (LOG_LOG, "cf_close %s, dirty", cf->rmf->name);
538 mf_write (cf->hash_mf, 0, 0, sizeof(cf->head), &cf->head);
542 logf (LOG_LOG, "cf_close %s", cf->rmf->name);
543 mf_close (cf->hash_mf);
544 mf_close (cf->block_mf);