]> mj.ucw.cz Git - libucw.git/blob - lib/bucket.c
Replaced obuck_fetch_end() by bclose() (which is a nop as obuck_fetch_end was :) ).
[libucw.git] / lib / bucket.c
1 /*
2  *      Sherlock Library -- Object Buckets
3  *
4  *      (c) 2001--2003 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/bucket.h"
12 #include "lib/fastbuf.h"
13 #include "lib/lfs.h"
14 #include "lib/conf.h"
15
16 #include <string.h>
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/file.h>
21
22 static int obuck_fd;
23 static unsigned int obuck_remains, obuck_check_pad;
24 static struct fastbuf *obuck_fb;
25 static struct obuck_header obuck_hdr;
26 static sh_off_t bucket_start, bucket_current;
27
28 /*** Configuration ***/
29
30 byte *obuck_name = "not/configured";
31 static uns obuck_io_buflen = 65536;
32 static int obuck_shake_buflen = 1048576;
33 static uns obuck_slurp_buflen = 65536;
34
35 static struct cfitem obuck_config[] = {
36   { "Buckets",          CT_SECTION,     NULL },
37   { "BucketFile",       CT_STRING,      &obuck_name },
38   { "BufSize",          CT_INT,         &obuck_io_buflen },
39   { "ShakeBufSize",     CT_INT,         &obuck_shake_buflen },
40   { "SlurpBufSize",     CT_INT,         &obuck_slurp_buflen },
41   { NULL,               CT_STOP,        NULL }
42 };
43
44 static void CONSTRUCTOR obuck_init_config(void)
45 {
46   cf_register(obuck_config);
47 }
48
49 /*** Internal operations ***/
50
51 static void
52 obuck_broken(char *msg)
53 {
54   die("Object pool corrupted: %s (pos=%Lx)", msg, (long long) bucket_start);
55 }
56
57 /*
58  *  Unfortunately we cannot use flock() here since it happily permits
59  *  locking a shared fd (e.g., after fork()) multiple times. The fcntl
60  *  locks are very ugly and they don't support 64-bit offsets, but we
61  *  can work around the problem by always locking the first header
62  *  in the file.
63  */
64
65 static inline void
66 obuck_do_lock(int type)
67 {
68   struct flock fl;
69
70   fl.l_type = type;
71   fl.l_whence = SEEK_SET;
72   fl.l_start = 0;
73   fl.l_len = sizeof(struct obuck_header);
74   if (fcntl(obuck_fd, F_SETLKW, &fl) < 0)
75     die("fcntl lock: %m");
76 }
77
78 inline void
79 obuck_lock_read(void)
80 {
81   obuck_do_lock(F_RDLCK);
82 }
83
84 inline void
85 obuck_lock_write(void)
86 {
87   obuck_do_lock(F_WRLCK);
88 }
89
90 inline void
91 obuck_unlock(void)
92 {
93   obuck_do_lock(F_UNLCK);
94 }
95
96 /*** FastIO emulation ***/
97
98 /* We need to use pread/pwrite since we work on fd's shared between processes */
99
100 static int
101 obuck_fb_refill(struct fastbuf *f)
102 {
103   unsigned limit = (obuck_io_buflen < obuck_remains) ? obuck_io_buflen : obuck_remains;
104   unsigned size = (limit == obuck_remains) ? (limit+obuck_check_pad+4) : limit;
105   int l;
106
107   if (!limit)
108     return 0;
109   l = sh_pread(obuck_fd, f->buffer, size, bucket_current);
110   if (l < 0)
111     die("Error reading bucket: %m");
112   if ((unsigned) l != size)
113     obuck_broken("Short read");
114   f->bptr = f->buffer;
115   f->bstop = f->buffer + limit;
116   bucket_current += limit;
117   f->pos = bucket_current - bucket_start - sizeof(obuck_hdr);
118   obuck_remains -= limit;
119   if (!obuck_remains)   /* Should check the trailer */
120     {
121       if (GET_U32(f->buffer + size - 4) != OBUCK_TRAILER)
122         obuck_broken("Missing trailer");
123     }
124   return limit;
125 }
126
127 static void
128 obuck_fb_spout(struct fastbuf *f)
129 {
130   int l = f->bptr - f->buffer;
131   char *c = f->buffer;
132
133   while (l)
134     {
135       int z = sh_pwrite(obuck_fd, c, l, bucket_current);
136       if (z <= 0)
137         die("Error writing bucket: %m");
138       bucket_current += z;
139       l -= z;
140       c += z;
141     }
142   f->bptr = f->buffer;
143   f->pos = bucket_current - bucket_start - sizeof(obuck_hdr);
144 }
145
146 /*** Exported functions ***/
147
148 void
149 obuck_init(int writeable)
150 {
151   struct fastbuf *b;
152   sh_off_t size;
153
154   obuck_fd = sh_open(obuck_name, (writeable ? O_RDWR | O_CREAT : O_RDONLY), 0666);
155   if (obuck_fd < 0)
156     die("Unable to open bucket file %s: %m", obuck_name);
157   obuck_fb = b = xmalloc_zero(sizeof(struct fastbuf) + obuck_io_buflen + OBUCK_ALIGN + 4);
158   b->buffer = (char *)(b+1);
159   b->bptr = b->bstop = b->buffer;
160   b->bufend = b->buffer + obuck_io_buflen;
161   b->name = "bucket";
162   b->refill = obuck_fb_refill;
163   b->spout = obuck_fb_spout;
164   obuck_lock_read();
165   size = sh_seek(obuck_fd, 0, SEEK_END);
166   if (size)
167     {
168       /* If the bucket pool is not empty, check consistency of its end */
169       u32 check;
170       bucket_start = size - 4;  /* for error reporting */
171       if (sh_pread(obuck_fd, &check, 4, size-4) != 4 ||
172           check != OBUCK_TRAILER)
173         obuck_broken("Missing trailer of last object");
174     }
175   obuck_unlock();
176 }
177
178 void
179 obuck_cleanup(void)
180 {
181   bclose(obuck_fb);
182   close(obuck_fd);
183   xfree(obuck_fb);
184 }
185
186 void
187 obuck_sync(void)
188 {
189   bflush(obuck_fb);
190   fsync(obuck_fd);
191 }
192
193 static void
194 obuck_get(oid_t oid)
195 {
196   struct fastbuf *b = obuck_fb;
197
198   bucket_start = obuck_get_pos(oid);
199   bflush(b);
200   if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
201     obuck_broken("Short header read");
202   bucket_current = bucket_start + sizeof(obuck_hdr);
203   if (obuck_hdr.magic != OBUCK_MAGIC)
204     obuck_broken("Missing magic number");
205   if (obuck_hdr.oid == OBUCK_OID_DELETED)
206     obuck_broken("Access to deleted bucket");
207   if (obuck_hdr.oid != oid)
208     obuck_broken("Invalid backlink");
209 }
210
211 void
212 obuck_find_by_oid(struct obuck_header *hdrp)
213 {
214   oid_t oid = hdrp->oid;
215
216   ASSERT(oid < OBUCK_OID_FIRST_SPECIAL);
217   obuck_lock_read();
218   obuck_get(oid);
219   obuck_unlock();
220   memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
221 }
222
223 int
224 obuck_find_first(struct obuck_header *hdrp, int full)
225 {
226   bucket_start = 0;
227   obuck_hdr.magic = 0;
228   return obuck_find_next(hdrp, full);
229 }
230
231 int
232 obuck_find_next(struct obuck_header *hdrp, int full)
233 {
234   int c;
235   struct fastbuf *b = obuck_fb;
236
237   for(;;)
238     {
239       if (obuck_hdr.magic)
240         bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
241                         4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
242       bflush(b);
243       obuck_lock_read();
244       c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
245       obuck_unlock();
246       if (!c)
247         return 0;
248       if (c != sizeof(obuck_hdr))
249         obuck_broken("Short header read");
250       bucket_current = bucket_start + sizeof(obuck_hdr);
251       if (obuck_hdr.magic != OBUCK_MAGIC)
252         obuck_broken("Missing magic number");
253       if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
254         {
255           memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
256           return 1;
257         }
258     }
259 }
260
261 struct fastbuf *
262 obuck_fetch(void)
263 {
264   obuck_fb->pos = 0;
265   obuck_remains = obuck_hdr.length;
266   obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
267   return obuck_fb;
268 }
269
270 oid_t
271 obuck_predict_last_oid(void)
272 {
273   sh_off_t size = sh_seek(obuck_fd, 0, SEEK_END);
274   return size >> OBUCK_SHIFT;
275 }
276
277 struct fastbuf *
278 obuck_create(u32 type)
279 {
280   obuck_lock_write();
281   bflush(obuck_fb);
282   bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
283   if (bucket_start & (OBUCK_ALIGN - 1))
284     obuck_broken("Misaligned file");
285   obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
286   obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
287   obuck_hdr.length = 0;
288   obuck_hdr.type = type;
289   bucket_current = bucket_start;
290   bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
291   obuck_fb->pos = -sizeof(obuck_hdr);
292   return obuck_fb;
293 }
294
295 void
296 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
297 {
298   int pad;
299   obuck_hdr.magic = OBUCK_MAGIC;
300   obuck_hdr.length = btell(obuck_fb);
301   pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
302   while (pad--)
303     bputc(obuck_fb, 0);
304   bputl(obuck_fb, OBUCK_TRAILER);
305   bflush(obuck_fb);
306   ASSERT(!(bucket_current & (OBUCK_ALIGN - 1)));
307   sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
308   obuck_unlock();
309   memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
310 }
311
312 void
313 obuck_delete(oid_t oid)
314 {
315   obuck_lock_write();
316   obuck_get(oid);
317   obuck_hdr.oid = OBUCK_OID_DELETED;
318   sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
319   obuck_unlock();
320 }
321
322 /*** Fast reading of the whole pool ***/
323
324 static struct fastbuf *obuck_rpf;
325
326 static int
327 obuck_slurp_refill(struct fastbuf *f)
328 {
329   uns l;
330
331   if (!obuck_remains)
332     return 0;
333   l = bdirect_read_prepare(obuck_rpf, &f->buffer);
334   if (!l)
335     obuck_broken("Incomplete object");
336   l = MIN(l, obuck_remains);
337   bdirect_read_commit(obuck_rpf, f->buffer + l);
338   obuck_remains -= l;
339   f->bptr = f->buffer;
340   f->bufend = f->bstop = f->buffer + l;
341   return 1;
342 }
343
344 struct fastbuf *
345 obuck_slurp_pool(struct obuck_header *hdrp)
346 {
347   static struct fastbuf limiter;
348   uns l;
349
350   do
351     {
352       if (!obuck_rpf)
353         {
354           obuck_lock_read();
355           obuck_rpf = bopen(obuck_name, O_RDONLY, obuck_slurp_buflen);
356         }
357       else
358         {
359           bsetpos(obuck_rpf, bucket_current - 4);
360           if (bgetl(obuck_rpf) != OBUCK_TRAILER)
361             obuck_broken("Missing trailer");
362         }
363       bucket_start = btell(obuck_rpf);
364       l = bread(obuck_rpf, hdrp, sizeof(struct obuck_header));
365       if (!l)
366         {
367           bclose(obuck_rpf);
368           obuck_rpf = NULL;
369           obuck_unlock();
370           return NULL;
371         }
372       if (l != sizeof(struct obuck_header))
373         obuck_broken("Short header read");
374       if (hdrp->magic != OBUCK_MAGIC)
375         obuck_broken("Missing magic number");
376       bucket_current = (bucket_start + sizeof(obuck_hdr) + hdrp->length +
377                         4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
378     }
379   while (hdrp->oid == OBUCK_OID_DELETED);
380   if (obuck_get_pos(hdrp->oid) != bucket_start)
381     obuck_broken("Invalid backlink");
382   obuck_remains = hdrp->length;
383   limiter.bptr = limiter.bstop = limiter.buffer = limiter.bufend = NULL;
384   limiter.name = "Bucket";
385   limiter.pos = 0;
386   limiter.refill = obuck_slurp_refill;
387   return &limiter;
388 }
389
390 /*** Shakedown ***/
391
392 void
393 obuck_shakedown(int (*kibitz)(struct obuck_header *old, oid_t new, byte *buck))
394 {
395   byte *rbuf, *wbuf, *msg;
396   sh_off_t rstart, wstart, w_bucket_start;
397   int roff, woff, rsize, l;
398   struct obuck_header *rhdr, *whdr;
399
400   rbuf = xmalloc(obuck_shake_buflen);
401   wbuf = xmalloc(obuck_shake_buflen);
402   rstart = wstart = 0;
403   roff = woff = rsize = 0;
404
405   /* We need to be the only accessor, all the object ID's are becoming invalid */
406   obuck_lock_write();
407
408   for(;;)
409     {
410       bucket_start = rstart + roff;
411       w_bucket_start = wstart + woff;
412       if (rsize - roff < OBUCK_ALIGN)
413         goto reread;
414       rhdr = (struct obuck_header *)(rbuf + roff);
415       if (rhdr->magic != OBUCK_MAGIC ||
416           rhdr->oid != OBUCK_OID_DELETED && rhdr->oid != (oid_t)(bucket_start >> OBUCK_SHIFT))
417         {
418           msg = "header mismatch";
419           goto broken;
420         }
421       l = (sizeof(struct obuck_header) + rhdr->length + 4 + OBUCK_ALIGN - 1) & ~(OBUCK_ALIGN-1);
422       if (l > obuck_shake_buflen)
423         {
424           if (rhdr->oid != OBUCK_OID_DELETED)
425             {
426               msg = "bucket longer than ShakeBufSize";
427               goto broken;
428             }
429           rstart = bucket_start + l;
430           roff = 0;
431           rsize = 0;
432           goto reread;
433         }
434       if (rsize - roff < l)
435         goto reread;
436       if (GET_U32(rbuf + roff + l - 4) != OBUCK_TRAILER)
437         {
438           msg = "missing trailer";
439           goto broken;
440         }
441       if (rhdr->oid != OBUCK_OID_DELETED)
442         {
443           if (kibitz(rhdr, w_bucket_start >> OBUCK_SHIFT, (byte *)(rhdr+1)))
444             {
445               if (bucket_start == w_bucket_start)
446                 {
447                   /* No copying needed now nor ever in the past, hence woff==0 */
448                   wstart += l;
449                 }
450               else
451                 {
452                   if (obuck_shake_buflen - woff < l)
453                     {
454                       if (sh_pwrite(obuck_fd, wbuf, woff, wstart) != woff)
455                         die("obuck_shakedown write failed: %m");
456                       wstart += woff;
457                       woff = 0;
458                     }
459                   whdr = (struct obuck_header *)(wbuf+woff);
460                   memcpy(whdr, rhdr, l);
461                   whdr->oid = w_bucket_start >> OBUCK_SHIFT;
462                   woff += l;
463                 }
464             }
465         }
466       else
467         kibitz(rhdr, OBUCK_OID_DELETED, NULL);
468       roff += l;
469       continue;
470
471     reread:
472       if (roff)
473         {
474           memmove(rbuf, rbuf+roff, rsize-roff);
475           rsize -= roff;
476           rstart += roff;
477           roff = 0;
478         }
479       l = sh_pread(obuck_fd, rbuf+rsize, obuck_shake_buflen-rsize, rstart+rsize);
480       if (l < 0)
481         die("obuck_shakedown read error: %m");
482       if (!l)
483         {
484           if (!rsize)
485             break;
486           msg = "unexpected EOF";
487           goto broken;
488         }
489       rsize += l;
490     }
491   if (woff)
492     {
493       if (sh_pwrite(obuck_fd, wbuf, woff, wstart) != woff)
494         die("obuck_shakedown write failed: %m");
495       wstart += woff;
496     }
497   sh_ftruncate(obuck_fd, wstart);
498
499   obuck_unlock();
500   xfree(rbuf);
501   xfree(wbuf);
502   return;
503
504  broken:
505   log(L_ERROR, "Error during object pool shakedown: %s (pos=%Ld, id=%x), gathering debris", msg, (long long) bucket_start, (uns)(bucket_start >> OBUCK_SHIFT));
506   if (woff)
507     {
508       sh_pwrite(obuck_fd, wbuf, woff, wstart);
509       wstart += woff;
510     }
511   while (wstart + OBUCK_ALIGN <= bucket_start)
512     {
513       u32 check = OBUCK_TRAILER;
514       obuck_hdr.magic = OBUCK_MAGIC;
515       obuck_hdr.oid = OBUCK_OID_DELETED;
516       if (bucket_start - wstart < 0x40000000)
517         obuck_hdr.length = bucket_start - wstart - sizeof(obuck_hdr) - 4;
518       else
519         obuck_hdr.length = 0x40000000 - sizeof(obuck_hdr) - 4;
520       sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), wstart);
521       wstart += sizeof(obuck_hdr) + obuck_hdr.length + 4;
522       sh_pwrite(obuck_fd, &check, 4, wstart-4);
523     }
524   die("Fatal error during object pool shakedown");
525 }
526
527 /*** Testing ***/
528
529 #ifdef TEST
530
531 #define COUNT 5000
532 #define MAXLEN 10000
533 #define KILLPERC 13
534 #define LEN(i) ((259309*(i))%MAXLEN)
535
536 int main(int argc, char **argv)
537 {
538   int ids[COUNT];
539   unsigned int i, j, cnt;
540   struct obuck_header h;
541   struct fastbuf *b;
542
543   log_init(NULL);
544   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
545       optind < argc)
546   {
547     fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);
548     exit(1);
549   }
550
551   unlink(obuck_name);
552   obuck_init(1);
553   for(j=0; j<COUNT; j++)
554     {
555       b = obuck_create();
556       for(i=0; i<LEN(j); i++)
557         bputc(b, (i+j) % 256);
558       obuck_create_end(b, &h);
559       printf("Writing %08x %d\n", h.oid, h.length);
560       ids[j] = h.oid;
561     }
562   for(j=0; j<COUNT; j++)
563     if (j % 100 < KILLPERC)
564       {
565         printf("Deleting %08x\n", ids[j]);
566         obuck_delete(ids[j]);
567       }
568   cnt = 0;
569   for(j=0; j<COUNT; j++)
570     if (j % 100 >= KILLPERC)
571       {
572         cnt++;
573         h.oid = ids[j];
574         obuck_find_by_oid(&h);
575         b = obuck_fetch();
576         printf("Reading %08x %d\n", h.oid, h.length);
577         if (h.length != LEN(j))
578           die("Invalid length");
579         for(i=0; i<h.length; i++)
580           if ((unsigned) bgetc(b) != (i+j) % 256)
581             die("Contents mismatch");
582         if (bgetc(b) != EOF)
583           die("EOF mismatch");
584         bclose(b);
585       }
586   if (obuck_find_first(&h, 0))
587     do
588       {
589         printf("<<< %08x\t%d\n", h.oid, h.length);
590         cnt--;
591       }
592     while (obuck_find_next(&h, 0));
593   if (cnt)
594     die("Walk mismatch");
595   obuck_cleanup();
596   return 0;
597 }
598
599 #endif