-
Notifications
You must be signed in to change notification settings - Fork 25
/
jlog.c
3165 lines (2858 loc) · 91.5 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"
#include "jlog_compress.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SYS_UIO_H
#include <sys/uio.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
#include "fassert.h"
#include <pthread.h>
#include <assert.h>
#define BUFFERED_INDICES 1024
#define PRE_COMMIT_BUFFER_SIZE_DEFAULT 0
#define IS_COMPRESS_MAGIC_HDR(hdr) ((hdr & DEFAULT_HDR_MAGIC_COMPRESSION) == DEFAULT_HDR_MAGIC_COMPRESSION)
#define IS_COMPRESS_MAGIC(ctx) IS_COMPRESS_MAGIC_HDR((ctx)->meta->hdr_magic)
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);
static int __jlog_setup_reader(jlog_ctx *ctx, u_int32_t log, u_int8_t force_mmap);
static int __jlog_teardown_reader(jlog_ctx *ctx);
static int __jlog_metastore_atomic_increment(jlog_ctx *ctx);
static int __jlog_get_storage_bounds(jlog_ctx *ctx, unsigned int *earliest, unsigned *latest);
static int repair_metastore(jlog_ctx *ctx, const char *pth, unsigned int lat);
static int validate_metastore(const struct _jlog_meta_info *info, struct _jlog_meta_info *out);
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_compressed hdr;
size_t hdr_size = sizeof(jlog_message_header);
uint32_t *message_disk_len = &hdr.mlen;
if (IS_COMPRESS_MAGIC(ctx)) {
hdr_size = sizeof(jlog_message_header_compressed);
message_disk_len = &hdr.compressed_len;
}
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_setup_reader(ctx, log, 1) != 0)
SYS_FAIL(ctx->last_error);
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 - hdr_size;
hdr.reserved = ctx->meta->hdr_magic;
hdr.mlen = 0;
while (this + hdr_size <= mmap_end) {
next = this + hdr_size + *message_disk_len;
if (next <= (char *)ctx->mmap_base) goto error;
if (next == mmap_end) {
this = next;
break;
}
if (next + hdr_size > mmap_end) goto error;
memcpy(&hdr, next, hdr_size);
if (hdr.reserved != ctx->meta->hdr_magic) goto error;
this = next;
continue;
error:
for (next = this + hdr_size; next + hdr_size <= mmap_end; next++) {
memcpy(&hdr, next, hdr_size);
if (hdr.reserved == ctx->meta->hdr_magic) {
afternext = next + hdr_size + *message_disk_len;
if (afternext <= (char *)ctx->mmap_base) continue;
if (afternext == mmap_end) break;
if (afternext + hdr_size > mmap_end) continue;
memcpy(&hdr, afternext, hdr_size);
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 + hdr_size > 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_teardown_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_compressed hdr;
size_t hdr_size = sizeof(jlog_message_header);
uint32_t *message_disk_len = &hdr.mlen;
char *this, *next, *mmap_end;
int i;
time_t timet;
struct tm tm;
char tbuff[128];
if (IS_COMPRESS_MAGIC(ctx)) {
hdr_size = sizeof(jlog_message_header_compressed);
message_disk_len = &hdr.compressed_len;
}
ctx->last_error = JLOG_ERR_SUCCESS;
__jlog_open_reader(ctx, log);
if (!ctx->data)
SYS_FAIL(JLOG_ERR_FILE_OPEN);
if (__jlog_setup_reader(ctx, log, 1) != 0)
SYS_FAIL(ctx->last_error);
mmap_end = (char*)ctx->mmap_base + ctx->mmap_len;
this = ctx->mmap_base;
i = 0;
while (this + hdr_size <= mmap_end) {
int initial = 1;
memcpy(&hdr, this, hdr_size);
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)hdr_size, *message_disk_len); \
initial = 0; \
} } while(0)
if(verbose) {
PRINTMSGHDR;
}
next = this + hdr_size + *message_disk_len;
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;
memset(file, 0, sizeof(file));
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, int create)
{
char file[MAXPATHLEN];
int len;
memset(file, 0, sizeof(file));
#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
FASSERT(ctx, 0, "__jlog_open_metastore: filename too long");
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, create ? O_CREAT : O_RDWR, ctx->file_mode, ctx->multi_process);
if (!ctx->metastore) {
ctx->last_errno = errno;
FASSERT(ctx, 0, "__jlog_open_metastore: file create failed");
ctx->last_error = JLOG_ERR_CREATE_META;
return -1;
}
return 0;
}
static char *
__jlog_pre_commit_file_name(jlog_ctx *ctx)
{
char file[MAXPATHLEN] = {0};
int len = 0;
#ifdef DEBUG
fprintf(stderr, "__jlog_open_pre_commit\n");
#endif
len = strlen(ctx->path);
if((len + 1 /* IFS_CH */ + 10 /* "pre_commit" */ + 1) > MAXPATHLEN) {
#ifdef ENAMETOOLONG
ctx->last_errno = ENAMETOOLONG;
#endif
FASSERT(ctx, 0, "__jlog_open_pre_commit: filename too long");
ctx->last_error = JLOG_ERR_CREATE_PRE_COMMIT;
return NULL;
}
memcpy(file, ctx->path, len);
file[len++] = IFS_CH;
memcpy(&file[len], "pre_commit", 11); /* "pre_commit" + '\0' */
return strdup(file);
}
static int __jlog_open_pre_commit(jlog_ctx *ctx)
{
#ifdef DEBUG
fprintf(stderr, "__jlog_open_pre_commit\n");
#endif
char *file = __jlog_pre_commit_file_name(ctx);
if (file == NULL) {
return -1;
}
ctx->pre_commit = jlog_file_open(file, O_CREAT, ctx->file_mode, ctx->multi_process);
if (!ctx->pre_commit) {
ctx->last_errno = errno;
FASSERT(ctx, 0, "__jlog_open_pre_commit: file create failed");
ctx->last_error = JLOG_ERR_CREATE_PRE_COMMIT;
free(file);
return -1;
}
free(file);
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;
memset(file, 0, sizeof(file));
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, ctx->multi_process))) {
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;
memset(file, 0, sizeof(file));
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(ctx->context_mode == JLOG_READ) {
FASSERT(ctx, 0, "__jlog_save_metastore: illegal call in JLOG_READ mode");
ctx->last_error = JLOG_ERR_ILLEGAL_WRITE;
ctx->last_errno = EPERM;
return -1;
}
if (!ilocked && !jlog_file_lock(ctx->metastore)) {
FASSERT(ctx, 0, "__jlog_save_metastore: cannot get lock");
ctx->last_error = JLOG_ERR_LOCK;
return -1;
}
if(ctx->meta_is_mapped) {
int rv, flags = MS_INVALIDATE;
if(ctx->meta->safety == JLOG_SAFE) flags |= MS_SYNC;
rv = msync((void *)(ctx->meta), sizeof(*ctx->meta), flags);
FASSERT(ctx, rv >= 0, "jlog_save_metastore");
if (!ilocked) jlog_file_unlock(ctx->metastore);
if ( rv < 0 )
ctx->last_error = JLOG_ERR_FILE_WRITE;
return rv;
}
else {
if (!jlog_file_pwrite(ctx->metastore, ctx->meta, sizeof(*ctx->meta), 0)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
FASSERT(ctx, 0, "jlog_file_pwrite failed");
ctx->last_error = JLOG_ERR_FILE_WRITE;
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, int readonly)
{
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)) {
FASSERT(ctx, 0, "__jlog_restore_metastore: cannot get lock");
ctx->last_error = JLOG_ERR_LOCK;
return -1;
}
if(ctx->meta_is_mapped == 0) {
int rv;
if (readonly == 1) {
rv = jlog_file_map_read(ctx->metastore, &base, &len);
} else {
rv = jlog_file_map_rdwr(ctx->metastore, &base, &len);
}
FASSERT(ctx, rv == 1, "jlog_file_map_r*");
if(rv != 1) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
ctx->last_error = JLOG_ERR_OPEN;
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);
if (readonly == 1) {
rv = jlog_file_map_read(ctx->metastore, &base, &len);
} else {
rv = jlog_file_map_rdwr(ctx->metastore, &base, &len);
}
unsigned int ear, lat;
if(!__jlog_get_storage_bounds(ctx, &ear, &lat) ||
!repair_metastore(ctx, NULL, lat)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
ctx->last_error = JLOG_ERR_OPEN;
return -1;
}
}
else {
struct _jlog_meta_info *meta = base;
if(len != sizeof(*meta) || !validate_metastore(meta, NULL)) {
if(!repair_metastore(ctx, NULL, meta->storage_log)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
ctx->last_error = JLOG_ERR_OPEN;
return -1;
}
}
}
FASSERT(ctx, rv == 1, "jlog_file_map_r*");
if(rv != 1 || len != sizeof(*ctx->meta)) {
if (!ilocked) jlog_file_unlock(ctx->metastore);
ctx->last_error = JLOG_ERR_OPEN;
return -1;
}
ctx->meta = base;
ctx->meta_is_mapped = 1;
if (IS_COMPRESS_MAGIC(ctx)) {
jlog_set_compression_provider(ctx->meta->hdr_magic & 0xFF);
}
}
if (!ilocked) jlog_file_unlock(ctx->metastore);
if(ctx->meta != &ctx->pre_init)
ctx->pre_init.hdr_magic = ctx->meta->hdr_magic;
return 0;
}
static int __jlog_map_pre_commit(jlog_ctx *ctx)
{
off_t pre_commit_size = 0;
#ifdef DEBUG
fprintf(stderr, "__jlog_map_pre_commit\n");
#endif
if(ctx->pre_commit_is_mapped == 1) {
return 0;
}
if (!jlog_file_lock(ctx->pre_commit)) {
FASSERT(ctx, 0, "__jlog_map_pre_commit: cannot get lock");
ctx->last_error = JLOG_ERR_LOCK;
return -1;
}
pre_commit_size = jlog_file_size(ctx->pre_commit);
if (pre_commit_size == 0) {
/* fill the pre_commit file with zero'd memory to hold incoming messages for block writes */
/* add space for the offset in the file at the front of the buffer */
char *space = calloc(1, ctx->desired_pre_commit_buffer_len + sizeof(uint32_t));
if (!jlog_file_pwrite(ctx->pre_commit, space, ctx->desired_pre_commit_buffer_len + sizeof(uint32_t), 0)) {
jlog_file_unlock(ctx->pre_commit);
FASSERT(ctx, 0, "jlog_file_pwrite failed");
ctx->last_error = JLOG_ERR_FILE_WRITE;
free(space);
return -1;
}
if (ctx->meta->safety == JLOG_SAFE) {
jlog_file_sync(ctx->pre_commit);
}
free(space);
}
/* now map it */
if (jlog_file_map_rdwr(ctx->pre_commit, (void **)&ctx->pre_commit_pointer, &ctx->pre_commit_buffer_len) == 0) {
jlog_file_unlock(ctx->pre_commit);
FASSERT(ctx, 0, "jlog_file_map_rdwr failed");
ctx->last_error = JLOG_ERR_PRE_COMMIT_OPEN;
return -1;
}
/* move the writable buffer past the offset pointer */
ctx->pre_commit_buffer = (void *)ctx->pre_commit_pointer + sizeof(*ctx->pre_commit_pointer);
/* the end is the total size minus the space for the leading write pointer location */
ctx->pre_commit_end = ctx->pre_commit_buffer + ctx->pre_commit_buffer_len - sizeof(*ctx->pre_commit_pointer);
/* restore the current pos */
ctx->pre_commit_pos = ctx->pre_commit_buffer + *ctx->pre_commit_pointer;
/* we're good */
ctx->pre_commit_is_mapped = 1;
jlog_file_unlock(ctx->pre_commit);
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 && (s == NULL || !strcmp(ctx->subscriber_name, s))) {
if(!ctx->checkpoint) {
ctx->checkpoint = __jlog_open_named_checkpoint(ctx, ctx->subscriber_name, 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)) {
FASSERT(ctx, 0, "jlog_file_pwrite failed in jlog_set_checkpoint");
ctx->last_error = JLOG_ERR_FILE_WRITE;
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;
}
static int __jlog_close_pre_commit(jlog_ctx *ctx) {
if (ctx->pre_commit) {
jlog_file_close(ctx->pre_commit);
ctx->pre_commit = NULL;
}
if (ctx->pre_commit_is_mapped) {
munmap((void *)ctx->pre_commit_pointer, ctx->pre_commit_buffer_len);
ctx->pre_commit_pointer = NULL;
ctx->pre_commit_buffer = NULL;
ctx->pre_commit_end = NULL;
ctx->pre_commit_buffer_len = 0;
ctx->pre_commit_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, ctx->multi_process);
}
static jlog_file *__jlog_open_reader(jlog_ctx *ctx, u_int32_t log) {
char file[MAXPATHLEN];
memset(file, 0, sizeof(file));
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->multi_process);
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;
}
ctx->data_file_size = ctx->mmap_len;
return 0;
}
static int __jlog_setup_reader(jlog_ctx *ctx, u_int32_t log, u_int8_t force_mmap) {
jlog_read_method_type read_method = ctx->read_method;
switch (read_method) {
case JLOG_READ_METHOD_MMAP:
{
int rv = __jlog_mmap_reader(ctx, log);
if (rv != 0) {
return -1;
}
}
ctx->reader_is_initialized = 1;
return 0;
case JLOG_READ_METHOD_PREAD:
if (force_mmap) {
int rv = __jlog_mmap_reader(ctx, log);
if (rv != 0) {
return -1;
}
}
else {
off_t file_size = jlog_file_size(ctx->data);
if (file_size < 0) {
SYS_FAIL(JLOG_ERR_FILE_SEEK);
}
ctx->data_file_size = file_size;