2 * Sherlock Library -- Object Buckets
4 * (c) 2001 Martin Mares <mj@ucw.cz>
8 #include "lib/bucket.h"
9 #include "lib/fastbuf.h"
19 static unsigned int obuck_remains, obuck_check_pad;
20 static struct fastbuf *obuck_fb;
21 static struct obuck_header obuck_hdr;
22 static sh_off_t bucket_start;
24 /*** Configuration ***/
26 byte *obuck_name = "not/configured";
27 static int obuck_io_buflen = 65536;
29 static struct cfitem obuck_config[] = {
30 { "Buckets", CT_SECTION, NULL },
31 { "BucketFile", CT_STRING, &obuck_name },
32 { "BufSize", CT_INT, &obuck_io_buflen },
33 { NULL, CT_STOP, NULL }
36 static void CONSTRUCTOR obuck_init_config(void)
38 cf_register(obuck_config);
41 /*** Internal operations ***/
44 obuck_broken(char *msg)
46 die("Object pool corrupted: %s (pos=%Lx)", msg, (long long) bucket_start);
50 * Unfortunately we cannot use flock() here since it happily permits
51 * locking a shared fd (e.g., after fork()) multiple times. The fcntl
52 * locks are very ugly and they don't support 64-bit offsets, but we
53 * can work around the problem by always locking the first header
58 obuck_do_lock(int type)
63 fl.l_whence = SEEK_SET;
65 fl.l_len = sizeof(struct obuck_header);
66 if (fcntl(obuck_fd, F_SETLKW, &fl) < 0)
67 die("fcntl lock: %m");
73 obuck_do_lock(F_RDLCK);
77 obuck_lock_write(void)
79 obuck_do_lock(F_WRLCK);
85 obuck_do_lock(F_UNLCK);
88 /*** FastIO emulation ***/
90 /* We need to use pread/pwrite since we work on fd's shared between processes */
93 obuck_fb_refill(struct fastbuf *f)
95 unsigned limit = (f->buflen < obuck_remains) ? f->buflen : obuck_remains;
96 unsigned size = (limit == obuck_remains) ? (limit+obuck_check_pad+4) : limit;
101 l = sh_pread(f->fd, f->buffer, size, f->fdpos);
103 die("Error reading bucket: %m");
104 if ((unsigned) l != size)
105 obuck_broken("Short read");
107 f->bstop = f->buffer + limit;
110 obuck_remains -= limit;
111 if (!obuck_remains) /* Should check the trailer */
114 memcpy(&check, f->buffer + size - 4, 4);
115 if (check != OBUCK_TRAILER)
116 obuck_broken("Missing trailer");
122 obuck_fb_spout(struct fastbuf *f)
124 int l = f->bptr - f->buffer;
129 int z = sh_pwrite(f->fd, c, l, f->fdpos);
131 die("Error writing bucket: %m");
141 obuck_fb_close(struct fastbuf *f)
146 /*** Exported functions ***/
149 obuck_init(int writeable)
154 obuck_fd = sh_open(obuck_name, (writeable ? O_RDWR | O_CREAT : O_RDONLY), 0666);
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->buflen = obuck_io_buflen;
159 b->buffer = (char *)(b+1);
160 b->bptr = b->bstop = b->buffer;
161 b->bufend = b->buffer + obuck_io_buflen;
164 b->refill = obuck_fb_refill;
165 b->spout = obuck_fb_spout;
166 b->close = obuck_fb_close;
168 size = sh_seek(obuck_fd, 0, SEEK_END);
171 /* If the bucket pool is not empty, check consistency of its end */
173 bucket_start = size - 4; /* for error reporting */
174 if (sh_pread(obuck_fd, &check, 4, size-4) != 4 ||
175 check != OBUCK_TRAILER)
176 obuck_broken("Missing trailer of last object");
197 struct fastbuf *b = obuck_fb;
199 bucket_start = obuck_get_pos(oid);
201 if (sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start) != sizeof(obuck_hdr))
202 obuck_broken("Short header read");
203 b->fdpos = bucket_start + sizeof(obuck_hdr);
204 if (obuck_hdr.magic != OBUCK_MAGIC)
205 obuck_broken("Missing magic number");
206 if (obuck_hdr.oid == OBUCK_OID_DELETED)
207 obuck_broken("Access to deleted bucket");
208 if (obuck_hdr.oid != oid)
209 obuck_broken("Invalid backlink");
213 obuck_find_by_oid(struct obuck_header *hdrp)
215 oid_t oid = hdrp->oid;
217 ASSERT(oid < OBUCK_OID_FIRST_SPECIAL);
221 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
225 obuck_find_first(struct obuck_header *hdrp, int full)
229 return obuck_find_next(hdrp, full);
233 obuck_find_next(struct obuck_header *hdrp, int full)
236 struct fastbuf *b = obuck_fb;
241 bucket_start = (bucket_start + sizeof(obuck_hdr) + obuck_hdr.length +
242 4 + OBUCK_ALIGN - 1) & ~((sh_off_t)(OBUCK_ALIGN - 1));
245 c = sh_pread(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
249 if (c != sizeof(obuck_hdr))
250 obuck_broken("Short header read");
251 b->fdpos = bucket_start + sizeof(obuck_hdr);
252 if (obuck_hdr.magic != OBUCK_MAGIC)
253 obuck_broken("Missing magic number");
254 if (obuck_hdr.oid != OBUCK_OID_DELETED || full)
256 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
265 obuck_remains = obuck_hdr.length;
266 obuck_check_pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
271 obuck_fetch_end(struct fastbuf *b UNUSED)
280 bucket_start = sh_seek(obuck_fd, 0, SEEK_END);
281 if (bucket_start & (OBUCK_ALIGN - 1))
282 obuck_broken("Misaligned file");
283 obuck_hdr.magic = OBUCK_INCOMPLETE_MAGIC;
284 obuck_hdr.oid = bucket_start >> OBUCK_SHIFT;
285 obuck_hdr.length = obuck_hdr.orig_length = 0;
286 obuck_fb->fdpos = obuck_fb->pos = bucket_start;
287 bwrite(obuck_fb, &obuck_hdr, sizeof(obuck_hdr));
292 obuck_create_end(struct fastbuf *b UNUSED, struct obuck_header *hdrp)
295 obuck_hdr.magic = OBUCK_MAGIC;
296 obuck_hdr.length = obuck_hdr.orig_length = btell(obuck_fb) - bucket_start - sizeof(obuck_hdr);
297 pad = (OBUCK_ALIGN - sizeof(obuck_hdr) - obuck_hdr.length - 4) & (OBUCK_ALIGN - 1);
300 bputl(obuck_fb, OBUCK_TRAILER);
302 ASSERT(!(btell(obuck_fb) & (OBUCK_ALIGN - 1)));
303 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
305 memcpy(hdrp, &obuck_hdr, sizeof(obuck_hdr));
309 obuck_delete(oid_t oid)
313 obuck_hdr.oid = OBUCK_OID_DELETED;
314 sh_pwrite(obuck_fd, &obuck_hdr, sizeof(obuck_hdr), bucket_start);
325 #define LEN(i) ((259309*(i))%MAXLEN)
327 int main(int argc, char **argv)
330 unsigned int i, j, cnt;
331 struct obuck_header h;
335 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
337 die("This program has no command-line arguments.");
341 for(j=0; j<COUNT; j++)
344 for(i=0; i<LEN(j); i++)
345 bputc(b, (i+j) % 256);
346 obuck_create_end(b, &h);
347 printf("Writing %08x %d -> %d\n", h.oid, h.orig_length, h.length);
350 for(j=0; j<COUNT; j++)
351 if (j % 100 < KILLPERC)
353 printf("Deleting %08x\n", ids[j]);
354 obuck_delete(ids[j]);
357 for(j=0; j<COUNT; j++)
358 if (j % 100 >= KILLPERC)
362 obuck_find_by_oid(&h);
364 printf("Reading %08x %d -> %d\n", h.oid, h.orig_length, h.length);
365 if (h.orig_length != LEN(j))
366 die("Invalid length");
367 for(i=0; i<h.orig_length; i++)
368 if ((unsigned) bgetc(b) != (i+j) % 256)
369 die("Contents mismatch");
374 if (obuck_find_first(&h, 0))
377 printf("<<< %08x\t%d\n", h.oid, h.orig_length);
380 while (obuck_find_next(&h, 0));
382 die("Walk mismatch");