2 * Sherlock Library -- Object Buckets
4 * (c) 2001 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;
28 /*** Configuration ***/
30 byte *obuck_name = "not/configured";
31 static int 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 = (f->buflen < obuck_remains) ? f->buflen : obuck_remains;
102 unsigned size = (limit == obuck_remains) ? (limit+obuck_check_pad+4) : limit;
107 l = sh_pread(f->fd, f->buffer, size, f->fdpos);
109 die("Error reading bucket: %m");
110 if ((unsigned) l != size)
111 obuck_broken("Short read");
113 f->bstop = f->buffer + limit;
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(f->fd, c, l, f->fdpos);
135 die("Error writing bucket: %m");
145 obuck_fb_close(struct fastbuf *f)
150 /*** Exported functions ***/
153 obuck_init(int writeable)
158 obuck_fd = sh_open(obuck_name, (writeable ? O_RDWR | O_CREAT : O_RDONLY), 0666);
160 die("Unable to open bucket file %s: %m", obuck_name);
161 obuck_fb = b = xmalloc_zero(sizeof(struct fastbuf) + obuck_io_buflen + OBUCK_ALIGN + 4);
162 b->buflen = obuck_io_buflen;
163 b->buffer = (char *)(b+1);
164 b->bptr = b->bstop = b->buffer;
165 b->bufend = b->buffer + obuck_io_buflen;
168 b->refill = obuck_fb_refill;
169 b->spout = obuck_fb_spout;
170 b->close = obuck_fb_close;
172 size = sh_seek(obuck_fd, 0, SEEK_END);
175 /* If the bucket pool is not empty, check consistency of its end */
177 bucket_start = size - 4; /* for error reporting */
178 if (sh_pread(obuck_fd, &check, 4, size-4) != 4 ||
179 check != OBUCK_TRAILER)
180 obuck_broken("Missing trailer of last object");
201 struct fastbuf *b = obuck_fb;
203 bucket_start = obuck_get_pos(oid);
205 if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
206 obuck_broken("Short header read");
207 b->fdpos = bucket_start + sizeof(obuck_hdr);
208 if (obuck_hdr.magic != OBUCK_MAGIC)
209 obuck_broken("Missing magic number");
210 if (obuck_hdr.oid == OBUCK_OID_DELETED)
211 obuck_broken("Access to deleted bucket");
212 if (obuck_hdr.oid != oid)
213 obuck_broken("Invalid backlink");
217 obuck_find_by_oid(struct obuck_header *hdrp)
219 oid_t oid = hdrp->oid;
221 ASSERT(oid < OBUCK_OID_FIRST_SPECIAL);
225 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
229 obuck_find_first(struct obuck_header *hdrp, int full)
233 return obuck_find_next(hdrp, full);
237 obuck_find_next(struct obuck_header *hdrp, int full)
240 struct fastbuf *b = obuck_fb;
245 bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
246 4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
249 c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
253 if (c != sizeof(obuck_hdr))
254 obuck_broken("Short header read");
255 b->fdpos = bucket_start + sizeof(obuck_hdr);
256 if (obuck_hdr.magic != OBUCK_MAGIC)
257 obuck_broken("Missing magic number");
258 if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
260 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
269 obuck_remains = obuck_hdr.length;
270 obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
275 obuck_fetch_end(struct fastbuf *b UNUSED)
284 bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
285 if (bucket_start & (OBUCK_ALIGN - 1))
286 obuck_broken("Misaligned file");
287 obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
288 obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
289 obuck_hdr.length = obuck_hdr.orig_length = 0;
290 obuck_fb->fdpos = obuck_fb->pos = bucket_start;
291 bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
296 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
299 obuck_hdr.magic = OBUCK_MAGIC;
300 obuck_hdr.length = obuck_hdr.orig_length = btell(obuck_fb) - bucket_start - sizeof(obuck_hdr);
301 pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
304 bputl(obuck_fb, OBUCK_TRAILER);
306 ASSERT(!(btell(obuck_fb) & (OBUCK_ALIGN - 1)));
307 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
309 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
313 obuck_delete(oid_t oid)
317 obuck_hdr.oid = OBUCK_OID_DELETED;
318 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
325 obuck_shakedown(int (*kibitz)(struct obuck_header *old, oid_t new, byte *buck))
327 byte *rbuf, *wbuf, *msg;
328 sh_off_t rstart, wstart, w_bucket_start;
329 int roff, woff, rsize, l;
330 struct obuck_header *rhdr, *whdr;
332 rbuf = xmalloc(obuck_shake_buflen);
333 wbuf = xmalloc(obuck_shake_buflen);
335 roff = woff = rsize = 0;
337 /* We need to be the only accessor, all the object ID's are becoming invalid */
342 bucket_start = rstart + roff;
343 w_bucket_start = wstart + woff;
344 if (rsize - roff < OBUCK_ALIGN)
346 rhdr = (struct obuck_header *)(rbuf + roff);
347 if (rhdr->magic != OBUCK_MAGIC ||
348 rhdr->oid != OBUCK_OID_DELETED && rhdr->oid != (oid_t)(bucket_start >> OBUCK_SHIFT))
350 msg = "header mismatch";
353 l = (sizeof(struct obuck_header) + rhdr->length + 4 + OBUCK_ALIGN - 1) & ~(OBUCK_ALIGN-1);
354 if (rsize - roff < l)
356 if (GET_U32(rbuf + roff + l - 4) != OBUCK_TRAILER)
358 msg = "missing trailer";
361 if (rhdr->oid != OBUCK_OID_DELETED)
363 if (kibitz(rhdr, w_bucket_start >> OBUCK_SHIFT, (byte *)(rhdr+1)))
365 if (bucket_start == w_bucket_start)
367 /* No copying needed now nor ever in the past, hence woff==0 */
372 if (obuck_shake_buflen - woff < l)
374 if (sh_pwrite(obuck_fd, wbuf, woff, wstart) != woff)
375 die("obuck_shakedown write failed: %m");
379 whdr = (struct obuck_header *)(wbuf+woff);
380 memcpy(whdr, rhdr, l);
381 whdr->oid = w_bucket_start >> OBUCK_SHIFT;
387 kibitz(rhdr, OBUCK_OID_DELETED, NULL);
394 memmove(rbuf, rbuf+roff, rsize-roff);
399 l = sh_pread(obuck_fd, rbuf+rsize, obuck_shake_buflen-rsize, rstart+rsize);
401 die("obuck_shakedown read error: %m");
406 msg = "unexpected EOF";
413 if (sh_pwrite(obuck_fd, wbuf, woff, wstart) != woff)
414 die("obuck_shakedown write failed: %m");
417 sh_ftruncate(obuck_fd, wstart);
425 log(L_ERROR, "Error during object pool shakedown: %s (pos=%Ld), gathering debris", msg, (long long) bucket_start);
428 sh_pwrite(obuck_fd, wbuf, woff, wstart);
431 while (wstart + OBUCK_ALIGN <= bucket_start)
433 u32 check = OBUCK_TRAILER;
434 obuck_hdr.magic = OBUCK_MAGIC;
435 obuck_hdr.oid = OBUCK_OID_DELETED;
436 if (bucket_start - wstart < 0x40000000)
437 obuck_hdr.length = bucket_start - wstart - sizeof(obuck_hdr) - 4;
439 obuck_hdr.length = 0x40000000 - sizeof(obuck_hdr) - 4;
440 obuck_hdr.orig_length = obuck_hdr.length;
441 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), wstart);
442 wstart += sizeof(obuck_hdr) + obuck_hdr.length + 4;
443 sh_pwrite(obuck_fd, &check, 4, wstart-4);
445 die("Fatal error during object pool shakedown");
455 #define LEN(i) ((259309*(i))%MAXLEN)
457 int main(int argc, char **argv)
460 unsigned int i, j, cnt;
461 struct obuck_header h;
465 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
468 fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);
474 for(j=0; j<COUNT; j++)
477 for(i=0; i<LEN(j); i++)
478 bputc(b, (i+j) % 256);
479 obuck_create_end(b, &h);
480 printf("Writing %08x %d -> %d\n", h.oid, h.orig_length, h.length);
483 for(j=0; j<COUNT; j++)
484 if (j % 100 < KILLPERC)
486 printf("Deleting %08x\n", ids[j]);
487 obuck_delete(ids[j]);
490 for(j=0; j<COUNT; j++)
491 if (j % 100 >= KILLPERC)
495 obuck_find_by_oid(&h);
497 printf("Reading %08x %d -> %d\n", h.oid, h.orig_length, h.length);
498 if (h.orig_length != LEN(j))
499 die("Invalid length");
500 for(i=0; i<h.orig_length; i++)
501 if ((unsigned) bgetc(b) != (i+j) % 256)
502 die("Contents mismatch");
507 if (obuck_find_first(&h, 0))
510 printf("<<< %08x\t%d\n", h.oid, h.orig_length);
513 while (obuck_find_next(&h, 0));
515 die("Walk mismatch");