2 * Sherlock Library -- Object Buckets
4 * (c) 2001--2002 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/bucket.h"
12 #include "lib/fastbuf.h"
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;
28 /*** Configuration ***/
30 byte *obuck_name = "not/configured";
31 static uns obuck_io_buflen = 65536;
32 static int obuck_shake_buflen = 1048576;
34 static struct cfitem obuck_config[] = {
35 { "Buckets", CT_SECTION, NULL },
36 { "BucketFile", CT_STRING, &obuck_name },
37 { "BufSize", CT_INT, &obuck_io_buflen },
38 { "ShakeBufSize", CT_INT, &obuck_shake_buflen },
39 { NULL, CT_STOP, NULL }
42 static void CONSTRUCTOR obuck_init_config(void)
44 cf_register(obuck_config);
47 /*** Internal operations ***/
50 obuck_broken(char *msg)
52 die("Object pool corrupted: %s (pos=%Lx)", msg, (long long) bucket_start);
56 * Unfortunately we cannot use flock() here since it happily permits
57 * locking a shared fd (e.g., after fork()) multiple times. The fcntl
58 * locks are very ugly and they don't support 64-bit offsets, but we
59 * can work around the problem by always locking the first header
64 obuck_do_lock(int type)
69 fl.l_whence = SEEK_SET;
71 fl.l_len = sizeof(struct obuck_header);
72 if (fcntl(obuck_fd, F_SETLKW, &fl) < 0)
73 die("fcntl lock: %m");
79 obuck_do_lock(F_RDLCK);
83 obuck_lock_write(void)
85 obuck_do_lock(F_WRLCK);
91 obuck_do_lock(F_UNLCK);
94 /*** FastIO emulation ***/
96 /* We need to use pread/pwrite since we work on fd's shared between processes */
99 obuck_fb_refill(struct fastbuf *f)
101 unsigned limit = (obuck_io_buflen < obuck_remains) ? obuck_io_buflen : obuck_remains;
102 unsigned size = (limit == obuck_remains) ? (limit+obuck_check_pad+4) : limit;
107 l = sh_pread(obuck_fd, f->buffer, size, bucket_current);
109 die("Error reading bucket: %m");
110 if ((unsigned) l != size)
111 obuck_broken("Short read");
113 f->bstop = f->buffer + limit;
114 bucket_current += limit;
115 f->pos = bucket_current - bucket_start - sizeof(obuck_hdr);
116 obuck_remains -= limit;
117 if (!obuck_remains) /* Should check the trailer */
119 if (GET_U32(f->buffer + size - 4) != OBUCK_TRAILER)
120 obuck_broken("Missing trailer");
126 obuck_fb_spout(struct fastbuf *f)
128 int l = f->bptr - f->buffer;
133 int z = sh_pwrite(obuck_fd, c, l, bucket_current);
135 die("Error writing bucket: %m");
141 f->pos = bucket_current - bucket_start - sizeof(obuck_hdr);
144 /*** Exported functions ***/
147 obuck_init(int writeable)
152 obuck_fd = sh_open(obuck_name, (writeable ? O_RDWR | O_CREAT : O_RDONLY), 0666);
154 die("Unable to open bucket file %s: %m", obuck_name);
155 obuck_fb = b = xmalloc_zero(sizeof(struct fastbuf) + obuck_io_buflen + OBUCK_ALIGN + 4);
156 b->buffer = (char *)(b+1);
157 b->bptr = b->bstop = b->buffer;
158 b->bufend = b->buffer + obuck_io_buflen;
160 b->refill = obuck_fb_refill;
161 b->spout = obuck_fb_spout;
163 size = sh_seek(obuck_fd, 0, SEEK_END);
166 /* If the bucket pool is not empty, check consistency of its end */
168 bucket_start = size - 4; /* for error reporting */
169 if (sh_pread(obuck_fd, &check, 4, size-4) != 4 ||
170 check != OBUCK_TRAILER)
171 obuck_broken("Missing trailer of last object");
194 struct fastbuf *b = obuck_fb;
196 bucket_start = obuck_get_pos(oid);
198 if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
199 obuck_broken("Short header read");
200 bucket_current = bucket_start + sizeof(obuck_hdr);
201 if (obuck_hdr.magic != OBUCK_MAGIC)
202 obuck_broken("Missing magic number");
203 if (obuck_hdr.oid == OBUCK_OID_DELETED)
204 obuck_broken("Access to deleted bucket");
205 if (obuck_hdr.oid != oid)
206 obuck_broken("Invalid backlink");
210 obuck_find_by_oid(struct obuck_header *hdrp)
212 oid_t oid = hdrp->oid;
214 ASSERT(oid < OBUCK_OID_FIRST_SPECIAL);
218 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
222 obuck_find_first(struct obuck_header *hdrp, int full)
226 return obuck_find_next(hdrp, full);
230 obuck_find_next(struct obuck_header *hdrp, int full)
233 struct fastbuf *b = obuck_fb;
238 bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
239 4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
242 c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
246 if (c != sizeof(obuck_hdr))
247 obuck_broken("Short header read");
248 bucket_current = bucket_start + sizeof(obuck_hdr);
249 if (obuck_hdr.magic != OBUCK_MAGIC)
250 obuck_broken("Missing magic number");
251 if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
253 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
263 obuck_remains = obuck_hdr.length;
264 obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
269 obuck_fetch_end(struct fastbuf *b UNUSED)
278 bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
279 if (bucket_start & (OBUCK_ALIGN - 1))
280 obuck_broken("Misaligned file");
281 obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
282 obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
283 obuck_hdr.length = obuck_hdr.orig_length = 0;
284 bucket_current = bucket_start;
285 bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
286 obuck_fb->pos = -sizeof(obuck_hdr);
291 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
294 obuck_hdr.magic = OBUCK_MAGIC;
295 obuck_hdr.length = obuck_hdr.orig_length = btell(obuck_fb);
296 pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
299 bputl(obuck_fb, OBUCK_TRAILER);
301 ASSERT(!(bucket_current & (OBUCK_ALIGN - 1)));
302 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
304 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
308 obuck_delete(oid_t oid)
312 obuck_hdr.oid = OBUCK_OID_DELETED;
313 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
320 obuck_shakedown(int (*kibitz)(struct obuck_header *old, oid_t new, byte *buck))
322 byte *rbuf, *wbuf, *msg;
323 sh_off_t rstart, wstart, w_bucket_start;
324 int roff, woff, rsize, l;
325 struct obuck_header *rhdr, *whdr;
327 rbuf = xmalloc(obuck_shake_buflen);
328 wbuf = xmalloc(obuck_shake_buflen);
330 roff = woff = rsize = 0;
332 /* We need to be the only accessor, all the object ID's are becoming invalid */
337 bucket_start = rstart + roff;
338 w_bucket_start = wstart + woff;
339 if (rsize - roff < OBUCK_ALIGN)
341 rhdr = (struct obuck_header *)(rbuf + roff);
342 if (rhdr->magic != OBUCK_MAGIC ||
343 rhdr->oid != OBUCK_OID_DELETED && rhdr->oid != (oid_t)(bucket_start >> OBUCK_SHIFT))
345 msg = "header mismatch";
348 l = (sizeof(struct obuck_header) + rhdr->length + 4 + OBUCK_ALIGN - 1) & ~(OBUCK_ALIGN-1);
349 if (rsize - roff < l)
351 if (GET_U32(rbuf + roff + l - 4) != OBUCK_TRAILER)
353 msg = "missing trailer";
356 if (rhdr->oid != OBUCK_OID_DELETED)
358 if (kibitz(rhdr, w_bucket_start >> OBUCK_SHIFT, (byte *)(rhdr+1)))
360 if (bucket_start == w_bucket_start)
362 /* No copying needed now nor ever in the past, hence woff==0 */
367 if (obuck_shake_buflen - woff < l)
369 if (sh_pwrite(obuck_fd, wbuf, woff, wstart) != woff)
370 die("obuck_shakedown write failed: %m");
374 whdr = (struct obuck_header *)(wbuf+woff);
375 memcpy(whdr, rhdr, l);
376 whdr->oid = w_bucket_start >> OBUCK_SHIFT;
382 kibitz(rhdr, OBUCK_OID_DELETED, NULL);
389 memmove(rbuf, rbuf+roff, rsize-roff);
394 l = sh_pread(obuck_fd, rbuf+rsize, obuck_shake_buflen-rsize, rstart+rsize);
396 die("obuck_shakedown read error: %m");
401 msg = "unexpected EOF";
408 if (sh_pwrite(obuck_fd, wbuf, woff, wstart) != woff)
409 die("obuck_shakedown write failed: %m");
412 sh_ftruncate(obuck_fd, wstart);
420 log(L_ERROR, "Error during object pool shakedown: %s (pos=%Ld), gathering debris", msg, (long long) bucket_start);
423 sh_pwrite(obuck_fd, wbuf, woff, wstart);
426 while (wstart + OBUCK_ALIGN <= bucket_start)
428 u32 check = OBUCK_TRAILER;
429 obuck_hdr.magic = OBUCK_MAGIC;
430 obuck_hdr.oid = OBUCK_OID_DELETED;
431 if (bucket_start - wstart < 0x40000000)
432 obuck_hdr.length = bucket_start - wstart - sizeof(obuck_hdr) - 4;
434 obuck_hdr.length = 0x40000000 - sizeof(obuck_hdr) - 4;
435 obuck_hdr.orig_length = obuck_hdr.length;
436 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), wstart);
437 wstart += sizeof(obuck_hdr) + obuck_hdr.length + 4;
438 sh_pwrite(obuck_fd, &check, 4, wstart-4);
440 die("Fatal error during object pool shakedown");
450 #define LEN(i) ((259309*(i))%MAXLEN)
452 int main(int argc, char **argv)
455 unsigned int i, j, cnt;
456 struct obuck_header h;
460 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
463 fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);
469 for(j=0; j<COUNT; j++)
472 for(i=0; i<LEN(j); i++)
473 bputc(b, (i+j) % 256);
474 obuck_create_end(b, &h);
475 printf("Writing %08x %d -> %d\n", h.oid, h.orig_length, h.length);
478 for(j=0; j<COUNT; j++)
479 if (j % 100 < KILLPERC)
481 printf("Deleting %08x\n", ids[j]);
482 obuck_delete(ids[j]);
485 for(j=0; j<COUNT; j++)
486 if (j % 100 >= KILLPERC)
490 obuck_find_by_oid(&h);
492 printf("Reading %08x %d -> %d\n", h.oid, h.orig_length, h.length);
493 if (h.orig_length != LEN(j))
494 die("Invalid length");
495 for(i=0; i<h.orig_length; i++)
496 if ((unsigned) bgetc(b) != (i+j) % 256)
497 die("Contents mismatch");
502 if (obuck_find_first(&h, 0))
505 printf("<<< %08x\t%d\n", h.oid, h.orig_length);
508 while (obuck_find_next(&h, 0));
510 die("Walk mismatch");