forked from omniti-labs/jlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlog.c
1755 lines (1582 loc) · 47.8 KB
/
jlog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2005-2008, Message Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name Message Systems, Inc. nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*****************************************************************
Journaled logging... append only.
(1) find current file, or allocate a file, extendible and mark
it current.
(2) Write records to it, records include their size, so
a simple inspection can detect and incomplete trailing
record.
(3) Write append until the file reaches a certain size.
(4) Allocate a file, extensible.
(5) RESYNC INDEX on 'finished' file (see reading:3) and postpend
an offset '0' to the index.
(2) goto (1)
Reading journals...
(1) find oldest checkpoint of all subscribers, remove all older files.
(2) (file, last_read) = find_checkpoint for this subscriber
(3) RESYNC INDEX:
open record index for file, seek to the end - off_t.
this is the offset of the last noticed record in this file.
open file, seek to this point, roll forward writing the index file
_do not_ write an offset for the last record unless it is found
complete.
(4) read entries from last_read+1 -> index of record index
*/
#include <stdio.h>
#include "jlog_config.h"
#include "jlog_private.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_DIRENT_H
#include <dirent.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_TIME_H
#include <time.h>
#endif
#if HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#define BUFFERED_INDICES 1024
static jlog_file *__jlog_open_writer(jlog_ctx *ctx);
static int __jlog_close_writer(jlog_ctx *ctx);
static jlog_file *__jlog_open_reader(jlog_ctx *ctx, u_int32_t log);
static int __jlog_close_reader(jlog_ctx *ctx);
static int __jlog_close_checkpoint(jlog_ctx *ctx);
static jlog_file *__jlog_open_indexer(jlog_ctx *ctx, u_int32_t log);
static int __jlog_close_indexer(jlog_ctx *ctx);
static int __jlog_resync_index(jlog_ctx *ctx, u_int32_t log, jlog_id *last, int *c);
static jlog_file *__jlog_open_named_checkpoint(jlog_ctx *ctx, const char *cpname, int flags);
static int __jlog_mmap_reader(jlog_ctx *ctx, u_int32_t log);
static int __jlog_munmap_reader(jlog_ctx *ctx);
int jlog_snprint_logid(char *b, int n, const jlog_id *id) {
return snprintf(b, n, "%08x:%08x", id->log, id->marker);
}
int jlog_repair_datafile(jlog_ctx *ctx, u_int32_t log)
{
jlog_message_header hdr;
char *this, *next, *afternext = NULL, *mmap_end;
int i, invalid_count = 0;
struct {
off_t start, end;
} *invalid = NULL;
off_t orig_len, src, dst, len;
#define TAG_INVALID(s, e) do { \
if (invalid_count) \
invalid = realloc(invalid, (invalid_count + 1) * sizeof(*invalid)); \
else \
invalid = malloc(sizeof(*invalid)); \
invalid[invalid_count].start = s - (char *)ctx->mmap_base; \
invalid[invalid_count].end = e - (char *)ctx->mmap_base; \
invalid_count++; \
} while (0)
ctx->last_error = JLOG_ERR_SUCCESS;
/* we want the reader's open logic because this runs in the read path
* the underlying fds are always RDWR anyway */
__jlog_open_reader(ctx, log);
if (!ctx->data) {
ctx->last_error = JLOG_ERR_FILE_OPEN;
ctx->last_errno = errno;
return -1;
}
if (!jlog_file_lock(ctx->data)) {
ctx->last_error = JLOG_ERR_LOCK;
ctx->last_errno = errno;
return -1;
}
if (__jlog_mmap_reader(ctx, log) != 0)
SYS_FAIL(JLOG_ERR_FILE_READ);
orig_len = ctx->mmap_len;
mmap_end = (char*)ctx->mmap_base + ctx->mmap_len;
/* these values will cause us to fall right into the error clause and
* start searching for a valid header from offset 0 */
this = (char*)ctx->mmap_base - sizeof(hdr);
hdr.reserved = ctx->meta->hdr_magic;
hdr.mlen = 0;
while (this + sizeof(hdr) <= mmap_end) {
next = this + sizeof(hdr) + hdr.mlen;
if (next <= (char *)ctx->mmap_base) goto error;
if (next == mmap_end) {
this = next;
break;
}
if (next + sizeof(hdr) > mmap_end) goto error;
memcpy(&hdr, next, sizeof(hdr));
if (hdr.reserved != ctx->meta->hdr_magic) goto error;
this = next;
continue;
error:
for (next = this + sizeof(hdr); next + sizeof(hdr) <= mmap_end; next++) {
memcpy(&hdr, next, sizeof(hdr));
if (hdr.reserved == ctx->meta->hdr_magic) {
afternext = next + sizeof(hdr) + hdr.mlen;
if (afternext <= (char *)ctx->mmap_base) continue;
if (afternext == mmap_end) break;
if (afternext + sizeof(hdr) > mmap_end) continue;
memcpy(&hdr, afternext, sizeof(hdr));
if (hdr.reserved == ctx->meta->hdr_magic) break;
}
}
/* correct for while loop entry condition */
if (this < (char *)ctx->mmap_base) this = ctx->mmap_base;
if (next + sizeof(hdr) > mmap_end) break;
if (next > this) TAG_INVALID(this, next);
this = afternext;
}
if (this != mmap_end) TAG_INVALID(this, mmap_end);
#undef TAG_INVALID
#define MOVE_SEGMENT do { \
char cpbuff[4096]; \
off_t chunk; \
while(len > 0) { \
chunk = len; \
if (chunk > sizeof(cpbuff)) chunk = sizeof(cpbuff); \
if (!jlog_file_pread(ctx->data, &cpbuff, chunk, src)) \
SYS_FAIL(JLOG_ERR_FILE_READ); \
if (!jlog_file_pwrite(ctx->data, &cpbuff, chunk, dst)) \
SYS_FAIL(JLOG_ERR_FILE_WRITE); \
src += chunk; \
dst += chunk; \
len -= chunk; \
} \
} while (0)
if (invalid_count > 0) {
__jlog_munmap_reader(ctx);
dst = invalid[0].start;
for (i = 0; i < invalid_count - 1; ) {
src = invalid[i].end;
len = invalid[++i].start - src;
MOVE_SEGMENT;
}
src = invalid[invalid_count - 1].end;
len = orig_len - src;
if (len > 0) MOVE_SEGMENT;
if (!jlog_file_truncate(ctx->data, dst))
SYS_FAIL(JLOG_ERR_FILE_WRITE);
}
#undef MOVE_SEGMENT
finish:
jlog_file_unlock(ctx->data);
if (invalid) free(invalid);
if (ctx->last_error != JLOG_ERR_SUCCESS) return -1;
return invalid_count;
}
int jlog_inspect_datafile(jlog_ctx *ctx, u_int32_t log, int verbose)
{
jlog_message_header hdr;
char *this, *next, *mmap_end;
int i;
time_t timet;
struct tm tm;
char tbuff[128];
ctx->last_error = JLOG_ERR_SUCCESS;
__jlog_open_reader(ctx, log);
if (!ctx->data)
SYS_FAIL(JLOG_ERR_FILE_OPEN);
if (__jlog_mmap_reader(ctx, log) != 0)
SYS_FAIL(JLOG_ERR_FILE_READ);
mmap_end = (char*)ctx->mmap_base + ctx->mmap_len;
this = ctx->mmap_base;
i = 0;
while (this + sizeof(hdr) <= mmap_end) {
int initial = 1;
memcpy(&hdr, this, sizeof(hdr));
i++;
if (hdr.reserved != ctx->meta->hdr_magic) {
fprintf(stderr, "Message %d at [%ld] has invalid reserved value %u\n",
i, (long int)(this - (char *)ctx->mmap_base), hdr.reserved);
return 1;
}
#define PRINTMSGHDR do { if(initial) { \
fprintf(stderr, "Message %d at [%ld] of (%lu+%u)", i, \
(long int)(this - (char *)ctx->mmap_base), \
(long unsigned int)sizeof(hdr), hdr.mlen); \
initial = 0; \
} } while(0)
if(verbose) {
PRINTMSGHDR;
}
next = this + sizeof(hdr) + hdr.mlen;
if (next <= (char *)ctx->mmap_base) {
PRINTMSGHDR;
fprintf(stderr, " WRAPPED TO NEGATIVE OFFSET!\n");
return 1;
}
if (next > mmap_end) {
PRINTMSGHDR;
fprintf(stderr, " OFF THE END!\n");
return 1;
}
timet = hdr.tv_sec;
localtime_r(&timet, &tm);
strftime(tbuff, sizeof(tbuff), "%c", &tm);
if(verbose) fprintf(stderr, "\n\ttime: %s\n\tmlen: %u\n", tbuff, hdr.mlen);
this = next;
}
if (this < mmap_end) {
fprintf(stderr, "%ld bytes of junk at the end\n",
(long int)(mmap_end - this));
return 1;
}
return 0;
finish:
return -1;
}
int jlog_idx_details(jlog_ctx *ctx, u_int32_t log,
u_int32_t *marker, int *closed)
{
off_t index_len;
u_int64_t index;
__jlog_open_indexer(ctx, log);
if (!ctx->index)
SYS_FAIL(JLOG_ERR_IDX_OPEN);
if ((index_len = jlog_file_size(ctx->index)) == -1)
SYS_FAIL(JLOG_ERR_IDX_SEEK);
if (index_len % sizeof(u_int64_t))
SYS_FAIL(JLOG_ERR_IDX_CORRUPT);
if (index_len > sizeof(u_int64_t)) {
if (!jlog_file_pread(ctx->index, &index, sizeof(u_int64_t),
index_len - sizeof(u_int64_t)))
{
SYS_FAIL(JLOG_ERR_IDX_READ);
}
if (index) {
*marker = index_len / sizeof(u_int64_t);
*closed = 0;
} else {
*marker = (index_len / sizeof(u_int64_t)) - 1;
*closed = 1;
}
} else {
*marker = index_len / sizeof(u_int64_t);
*closed = 0;
}
return 0;
finish:
return -1;
}
static int __jlog_unlink_datafile(jlog_ctx *ctx, u_int32_t log) {
char file[MAXPATHLEN];
int len;
if(ctx->current_log == log) {
__jlog_close_reader(ctx);
__jlog_close_indexer(ctx);
}
STRSETDATAFILE(ctx, file, log);
#ifdef DEBUG
fprintf(stderr, "unlinking %s\n", file);
#endif
unlink(file);
len = strlen(file);
if((len + sizeof(INDEX_EXT)) > sizeof(file)) return -1;
memcpy(file + len, INDEX_EXT, sizeof(INDEX_EXT));
#ifdef DEBUG
fprintf(stderr, "unlinking %s\n", file);
#endif
unlink(file);
return 0;
}
static int __jlog_open_metastore(jlog_ctx *ctx)
{
char file[MAXPATHLEN];
int len;
#ifdef DEBUG
fprintf(stderr, "__jlog_open_metastore\n");
#endif
len = strlen(ctx->path);
if((len + 1 /* IFS_CH */ + 9 /* "metastore" */ + 1) > MAXPATHLEN) {
#ifdef ENAMETOOLONG
ctx->last_errno = ENAMETOOLONG;
#endif
ctx->last_error = JLOG_ERR_CREATE_META;
return -1;
}
memcpy(file, ctx->path, len);
file[len++] = IFS_CH;
memcpy(&file[len], "metastore", 10); /* "metastore" + '\0' */
ctx->metastore = jlog_file_open(file, O_CREAT, ctx->file_mode);
if (!ctx->metastore) {
ctx->last_errno = errno;
ctx->last_error = JLOG_ERR_CREATE_META;
return -1;
}
return 0;
}
/* exported */
int __jlog_pending_readers(jlog_ctx *ctx, u_int32_t log) {
return jlog_pending_readers(ctx, log, NULL);
}
int jlog_pending_readers(jlog_ctx *ctx, u_int32_t log,
u_int32_t *earliest_out) {
int readers;
DIR *dir;
struct dirent *ent;
char file[MAXPATHLEN];
int len, seen = 0;
u_int32_t earliest = 0;
jlog_id id;
readers = 0;
dir = opendir(ctx->path);
if (!dir) return -1;
len = strlen(ctx->path);
if(len + 2 > sizeof(file)) {
closedir(dir);
return -1;
}
memcpy(file, ctx->path, len);
file[len++] = IFS_CH;
file[len] = '\0';
while ((ent = readdir(dir))) {
if (ent->d_name[0] == 'c' && ent->d_name[1] == 'p' && ent->d_name[2] == '.') {
jlog_file *cp;
int dlen;
dlen = strlen(ent->d_name);
if((len + dlen + 1) > sizeof(file)) continue;
memcpy(file + len, ent->d_name, dlen + 1); /* include \0 */
#ifdef DEBUG
fprintf(stderr, "Checking if %s needs %s...\n", ent->d_name, ctx->path);
#endif
if ((cp = jlog_file_open(file, 0, ctx->file_mode))) {
if (jlog_file_lock(cp)) {
(void) jlog_file_pread(cp, &id, sizeof(id), 0);
#ifdef DEBUG
fprintf(stderr, "\t%u <= %u (pending reader)\n", id.log, log);
#endif
if (!seen) {
earliest = id.log;
seen = 1;
}
else {
if(id.log < earliest) {
earliest = id.log;
}
}
if (id.log <= log) {
readers++;
}
jlog_file_unlock(cp);
}
jlog_file_close(cp);
}
}
}
closedir(dir);
if(earliest_out) *earliest_out = earliest;
return readers;
}
struct _jlog_subs {
char **subs;
int used;
int allocd;
};
int jlog_ctx_list_subscribers_dispose(jlog_ctx *ctx, char **subs) {
char *s;
int i = 0;
if(subs) {
while((s = subs[i++]) != NULL) free(s);
free(subs);
}
return 0;
}
int jlog_ctx_list_subscribers(jlog_ctx *ctx, char ***subs) {
struct _jlog_subs js = { NULL, 0, 0 };
DIR *dir;
struct dirent *ent;
unsigned char file[MAXPATHLEN];
char *p;
int len;
js.subs = calloc(16, sizeof(char *));
js.allocd = 16;
dir = opendir(ctx->path);
if (!dir) return -1;
while ((ent = readdir(dir))) {
if (ent->d_name[0] == 'c' && ent->d_name[1] == 'p' && ent->d_name[2] == '.') {
for (len = 0, p = ent->d_name + 3; *p;) {
unsigned char c;
int i;
for (c = 0, i = 0; i < 16; i++) {
if (__jlog_hexchars[i] == *p) {
c = i << 4;
break;
}
}
p++;
for (i = 0; i < 16; i++) {
if (__jlog_hexchars[i] == *p) {
c |= i;
break;
}
}
p++;
file[len++] = c;
}
file[len] = '\0';
js.subs[js.used++] = strdup((char *)file);
if(js.used == js.allocd) {
js.allocd *= 2;
js.subs = realloc(js.subs, js.allocd*sizeof(char *));
}
js.subs[js.used] = NULL;
}
}
closedir(dir);
*subs = js.subs;
return js.used;
}
static int __jlog_save_metastore(jlog_ctx *ctx, int ilocked)
{
#ifdef DEBUG
fprintf(stderr, "__jlog_save_metastore\n");
#endif
if (!ilocked && !jlog_file_lock(ctx->metastore)) {
return -1;
}
if(ctx->meta_is_mapped) {
int rv, flags = MS_INVALIDATE;
if(ctx->meta->safety == JLOG_SAFE) flags |= MS_SYNC;
rv = msync(ctx->meta, sizeof(*ctx->meta), flags);
if (!ilocked) jlog_file_unlock(ctx->metastore);
return rv;
}
else {
if (!jlog_file_pwrite(ctx->metastore, ctx->meta, sizeof(*ctx->meta), 0)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
return -1;
}
if (ctx->meta->safety == JLOG_SAFE) {
jlog_file_sync(ctx->metastore);
}
}
if (!ilocked) jlog_file_unlock(ctx->metastore);
return 0;
}
static int __jlog_restore_metastore(jlog_ctx *ctx, int ilocked)
{
void *base = NULL;
size_t len = 0;
if(ctx->meta_is_mapped) return 0;
#ifdef DEBUG
fprintf(stderr, "__jlog_restore_metastore\n");
#endif
if (!ilocked && !jlog_file_lock(ctx->metastore)) {
return -1;
}
if(ctx->meta_is_mapped == 0) {
int rv;
rv = jlog_file_map_rdwr(ctx->metastore, &base, &len);
if(rv != 1) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
return -1;
}
if(len == 12) {
/* old metastore format doesn't have the new magic hdr in it
* we need to extend it by four bytes, but we know the hdr was
* previously 0, so we write out zero.
*/
u_int32_t dummy = 0;
jlog_file_pwrite(ctx->metastore, &dummy, sizeof(dummy), 12);
rv = jlog_file_map_rdwr(ctx->metastore, &base, &len);
}
if(rv != 1 || len != sizeof(*ctx->meta)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
return -1;
}
ctx->meta = base;
ctx->meta_is_mapped = 1;
}
if (!ilocked) jlog_file_unlock(ctx->metastore);
if(ctx->meta != &ctx->pre_init)
ctx->pre_init.hdr_magic = ctx->meta->hdr_magic;
return 0;
}
int jlog_get_checkpoint(jlog_ctx *ctx, const char *s, jlog_id *id) {
jlog_file *f;
int rv = -1;
if(ctx->subscriber_name && !strcmp(ctx->subscriber_name, s)) {
if(!ctx->checkpoint) {
ctx->checkpoint = __jlog_open_named_checkpoint(ctx, s, 0);
}
f = ctx->checkpoint;
} else
f = __jlog_open_named_checkpoint(ctx, s, 0);
if (f) {
if (jlog_file_lock(f)) {
if (jlog_file_pread(f, id, sizeof(*id), 0)) rv = 0;
jlog_file_unlock(f);
}
}
if (f && f != ctx->checkpoint) jlog_file_close(f);
return rv;
}
static int __jlog_set_checkpoint(jlog_ctx *ctx, const char *s, const jlog_id *id)
{
jlog_file *f;
int rv = -1;
jlog_id old_id;
u_int32_t log;
if(ctx->subscriber_name && !strcmp(ctx->subscriber_name, s)) {
if(!ctx->checkpoint) {
ctx->checkpoint = __jlog_open_named_checkpoint(ctx, s, 0);
}
f = ctx->checkpoint;
} else
f = __jlog_open_named_checkpoint(ctx, s, 0);
if(!f) return -1;
if (!jlog_file_lock(f))
goto failset;
if (jlog_file_size(f) == 0) {
/* we're setting it for the first time, no segments were pending on it */
old_id.log = id->log;
} else {
if (!jlog_file_pread(f, &old_id, sizeof(old_id), 0))
goto failset;
}
if (!jlog_file_pwrite(f, id, sizeof(*id), 0))
goto failset;
if (ctx->meta->safety == JLOG_SAFE) {
jlog_file_sync(f);
}
jlog_file_unlock(f);
rv = 0;
for (log = old_id.log; log < id->log; log++) {
if (__jlog_pending_readers(ctx, log) == 0) {
__jlog_unlink_datafile(ctx, log);
}
}
failset:
if (f && f != ctx->checkpoint) jlog_file_close(f);
return rv;
}
static int __jlog_close_metastore(jlog_ctx *ctx) {
if (ctx->metastore) {
jlog_file_close(ctx->metastore);
ctx->metastore = NULL;
}
if (ctx->meta_is_mapped) {
munmap((void *)ctx->meta, sizeof(*ctx->meta));
ctx->meta = &ctx->pre_init;
ctx->meta_is_mapped = 0;
}
return 0;
}
/* path is assumed to be MAXPATHLEN */
static char *compute_checkpoint_filename(jlog_ctx *ctx, const char *subscriber, char *name)
{
const char *sub;
int len;
/* build checkpoint filename */
len = strlen(ctx->path);
memcpy(name, ctx->path, len);
name[len++] = IFS_CH;
name[len++] = 'c';
name[len++] = 'p';
name[len++] = '.';
for (sub = subscriber; *sub; ) {
name[len++] = __jlog_hexchars[((*sub & 0xf0) >> 4)];
name[len++] = __jlog_hexchars[(*sub & 0x0f)];
sub++;
}
name[len] = '\0';
#ifdef DEBUG
fprintf(stderr, "checkpoint %s filename is %s\n", subscriber, name);
#endif
return name;
}
static jlog_file *__jlog_open_named_checkpoint(jlog_ctx *ctx, const char *cpname, int flags)
{
char name[MAXPATHLEN];
compute_checkpoint_filename(ctx, cpname, name);
return jlog_file_open(name, flags, ctx->file_mode);
}
static jlog_file *__jlog_open_reader(jlog_ctx *ctx, u_int32_t log) {
char file[MAXPATHLEN];
if(ctx->current_log != log) {
__jlog_close_reader(ctx);
__jlog_close_indexer(ctx);
}
if(ctx->data) {
return ctx->data;
}
STRSETDATAFILE(ctx, file, log);
#ifdef DEBUG
fprintf(stderr, "opening log file[ro]: '%s'\n", file);
#endif
ctx->data = jlog_file_open(file, 0, ctx->file_mode);
ctx->current_log = log;
return ctx->data;
}
static int __jlog_munmap_reader(jlog_ctx *ctx) {
if(ctx->mmap_base) {
munmap(ctx->mmap_base, ctx->mmap_len);
ctx->mmap_base = NULL;
ctx->mmap_len = 0;
}
return 0;
}
static int __jlog_mmap_reader(jlog_ctx *ctx, u_int32_t log) {
if(ctx->current_log == log && ctx->mmap_base) return 0;
__jlog_open_reader(ctx, log);
if(!ctx->data)
return -1;
if (!jlog_file_map_read(ctx->data, &ctx->mmap_base, &ctx->mmap_len)) {
ctx->mmap_base = NULL;
ctx->last_error = JLOG_ERR_FILE_READ;
ctx->last_errno = errno;
return -1;
}
return 0;
}
static jlog_file *__jlog_open_writer(jlog_ctx *ctx) {
char file[MAXPATHLEN];
if(ctx->data) {
/* Still open */
return ctx->data;
}
if(!jlog_file_lock(ctx->metastore))
SYS_FAIL(JLOG_ERR_LOCK);
if(__jlog_restore_metastore(ctx, 1))
SYS_FAIL(JLOG_ERR_META_OPEN);
ctx->current_log = ctx->meta->storage_log;
STRSETDATAFILE(ctx, file, ctx->current_log);
#ifdef DEBUG
fprintf(stderr, "opening log file[rw]: '%s'\n", file);
#endif
ctx->data = jlog_file_open(file, O_CREAT, ctx->file_mode);
finish:
jlog_file_unlock(ctx->metastore);
return ctx->data;
}
static int __jlog_close_writer(jlog_ctx *ctx) {
if (ctx->data) {
jlog_file_close(ctx->data);
ctx->data = NULL;
}
return 0;
}
static int __jlog_close_reader(jlog_ctx *ctx) {
__jlog_munmap_reader(ctx);
if (ctx->data) {
jlog_file_close(ctx->data);
ctx->data = NULL;
}
return 0;
}
static int __jlog_close_checkpoint(jlog_ctx *ctx) {
if (ctx->checkpoint) {
jlog_file_close(ctx->checkpoint);
ctx->checkpoint = NULL;
}
return 0;
}
static jlog_file *__jlog_open_indexer(jlog_ctx *ctx, u_int32_t log) {
char file[MAXPATHLEN];
int len;
if(ctx->current_log != log) {
__jlog_close_reader(ctx);
__jlog_close_indexer(ctx);
}
if(ctx->index) {
return ctx->index;
}
STRSETDATAFILE(ctx, file, log);
len = strlen(file);
if((len + sizeof(INDEX_EXT)) > sizeof(file)) return NULL;
memcpy(file + len, INDEX_EXT, sizeof(INDEX_EXT));
#ifdef DEBUG
fprintf(stderr, "opening index file: '%s'\n", file);
#endif
ctx->index = jlog_file_open(file, O_CREAT, ctx->file_mode);
ctx->current_log = log;
return ctx->index;
}
static int __jlog_close_indexer(jlog_ctx *ctx) {
if (ctx->index) {
jlog_file_close(ctx->index);
ctx->index = NULL;
}
return 0;
}
static int
___jlog_resync_index(jlog_ctx *ctx, u_int32_t log, jlog_id *last,
int *closed) {
jlog_message_header logmhdr;
int i, second_try = 0;
off_t index_off, data_off, data_len;
u_int64_t index;
u_int64_t indices[BUFFERED_INDICES];
ctx->last_error = JLOG_ERR_SUCCESS;
if(closed) *closed = 0;
__jlog_open_reader(ctx, log);
if (!ctx->data) {
ctx->last_error = JLOG_ERR_FILE_OPEN;
ctx->last_errno = errno;
return -1;
}
#define RESTART do { \
if (second_try == 0) { \
jlog_file_truncate(ctx->index, index_off); \
jlog_file_unlock(ctx->index); \
second_try = 1; \
ctx->last_error = JLOG_ERR_SUCCESS; \
goto restart; \
} \
SYS_FAIL(JLOG_ERR_IDX_CORRUPT); \
} while (0)
restart:
__jlog_open_indexer(ctx, log);
if (!ctx->index) {
ctx->last_error = JLOG_ERR_IDX_OPEN;
ctx->last_errno = errno;
return -1;
}
if (!jlog_file_lock(ctx->index)) {
ctx->last_error = JLOG_ERR_LOCK;
ctx->last_errno = errno;
return -1;
}
data_off = 0;
if ((data_len = jlog_file_size(ctx->data)) == -1)
SYS_FAIL(JLOG_ERR_FILE_SEEK);
if ((index_off = jlog_file_size(ctx->index)) == -1)
SYS_FAIL(JLOG_ERR_IDX_SEEK);
if (index_off % sizeof(u_int64_t)) {
#ifdef DEBUG
fprintf(stderr, "corrupt index [%llu]\n", index_off);
#endif
RESTART;
}
if (index_off > sizeof(u_int64_t)) {
if (!jlog_file_pread(ctx->index, &index, sizeof(index),
index_off - sizeof(u_int64_t)))
{
SYS_FAIL(JLOG_ERR_IDX_READ);
}
if (index == 0) {
/* This log file has been "closed" */
#ifdef DEBUG
fprintf(stderr, "index closed\n");
#endif
if(last) {
last->log = log;
last->marker = (index_off / sizeof(u_int64_t)) - 1;
}
if(closed) *closed = 1;
goto finish;
} else {
if (index > data_len) {
#ifdef DEBUG
fprintf(stderr, "index told me to seek somehwere I can't\n");
#endif
RESTART;
}
data_off = index;
}
}
if (index_off > 0) {
/* We are adding onto a partial index so we must advance a record */
if (!jlog_file_pread(ctx->data, &logmhdr, sizeof(logmhdr), data_off))
SYS_FAIL(JLOG_ERR_FILE_READ);
if ((data_off += sizeof(logmhdr) + logmhdr.mlen) > data_len)
RESTART;
}
i = 0;
while (data_off + sizeof(logmhdr) <= data_len) {
off_t next_off = data_off;
if (!jlog_file_pread(ctx->data, &logmhdr, sizeof(logmhdr), data_off))
SYS_FAIL(JLOG_ERR_FILE_READ);
if (logmhdr.reserved != ctx->meta->hdr_magic) {
#ifdef DEBUG
fprintf(stderr, "logmhdr.reserved == %d\n", logmhdr.reserved);
#endif
SYS_FAIL(JLOG_ERR_FILE_CORRUPT);
}
if ((next_off += sizeof(logmhdr) + logmhdr.mlen) > data_len)
break;
/* Write our new index offset */
indices[i++] = data_off;
if(i >= BUFFERED_INDICES) {
#ifdef DEBUG
fprintf(stderr, "writing %i offsets\n", i);
#endif
if (!jlog_file_pwrite(ctx->index, indices, i * sizeof(u_int64_t), index_off))
RESTART;
index_off += i * sizeof(u_int64_t);
i = 0;
}
data_off = next_off;
}
if(i > 0) {
#ifdef DEBUG
fprintf(stderr, "writing %i offsets\n", i);
#endif
if (!jlog_file_pwrite(ctx->index, indices, i * sizeof(u_int64_t), index_off))
RESTART;
index_off += i * sizeof(u_int64_t);
}
if(last) {
last->log = log;
last->marker = index_off / sizeof(u_int64_t);
}
if(log < ctx->meta->storage_log) {
if (data_off != data_len) {
#ifdef DEBUG
fprintf(stderr, "closing index, but %llu != %llu\n", data_off, data_len);
#endif
SYS_FAIL(JLOG_ERR_FILE_CORRUPT);
}
/* Special case: if we are closing, we next write a '0'
* we can't write the closing marker if the data segment had no records
* in it, since it will be confused with an index to offset 0 by the
* next reader; this only happens when segments are repaired */
if (index_off) {
index = 0;
if (!jlog_file_pwrite(ctx->index, &index, sizeof(u_int64_t), index_off))
RESTART;
index_off += sizeof(u_int64_t);
}
if(closed) *closed = 1;
}
#undef RESTART
finish:
jlog_file_unlock(ctx->index);
#ifdef DEBUG
fprintf(stderr, "index is %s\n", closed?(*closed?"closed":"open"):"unknown");
#endif
if(ctx->last_error == JLOG_ERR_SUCCESS) return 0;
return -1;
}