-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.c
1685 lines (1352 loc) · 37.9 KB
/
table.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
#include "includes.h"
#include "defines.h"
#include "msgiddbd.h"
#include "misc.h"
#include "logmsg.h"
#include "table.h"
#include "logfile.h"
extern struct ShareData *g; /* global shared data */
ssize_t filesize(fd_t fd)
{
struct stat sb;
if (fstat(fd, &sb))
return -1;
return sb.st_size;
}
/* MurmurHash2 {{{ */
#ifdef X86
uint32_t MurmurHash2(const void * key, uint32_t len, uint32_t seed)
{
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
const uint32_t m = 0x5bd1e995;
const int32_t r = 24;
// Initialize the hash to a 'random' value
uint32_t h = seed ^ len;
// Mix 4 bytes at a time into the hash
const unsigned char * data = (const unsigned char *)key;
while(len >= 4)
{
uint32_t k = *(uint32_t *)data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
// Handle the last few bytes of the input array
switch(len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
// Do a few final mixes of the hash to ensure the last few
// bytes are well-incorporated.
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
#else
// Neutral aligned implementation
uint32_t MurmurHash2(const void *key, uint32_t len, uint32_t seed)
{
const uint32_t m = 0x5bd1e995;
const int32_t r = 24;
uint32_t h = seed ^ len;
const unsigned char *data = (const unsigned char *)key;
while(len >= 4)
{
uint32_t k;
k = data[0];
k |= data[1] << 8;
k |= data[2] << 16;
k |= data[3] << 24;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
switch(len)
{
case 3: h ^= data[2] << 16;
case 2: h ^= data[1] << 8;
case 1: h ^= data[0];
h *= m;
};
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
#endif
/* }}} */
/* given a struct dat_files, and fileid pointer into it, return the offset
* in bytes at which we can find this fileid (for debugging prints mostly) */
uint64_t calc_file_offset(struct dat_files *d, struct fileid *f)
{
return (char *)f - (char *)d->f.m;
}
/* same for segments in sdat */
uint64_t calc_segments_offset(struct dat_files *d, struct segments *ss)
{
return (char *)ss - (char *)d->s.m;
}
int32_t check_segments_page(struct dat_files *d, struct segments *ss, bool tryfix)
{
int32_t r = 0;
uint32_t h;
/* found myself doing this a lot; check both the magic and the hash
* for a segments page; if tryfix=true then try fixing it. */
if (ss->magic != SEGMENTS_MAGIC)
{
r = -1;
/* we have discovered a dodgy page :/ */
logmsg_f(LOG_CRIT, "ss->magic invalid at offset %p! "
"(fileid=%lu(?), magic=%x)",
calc_segments_offset(d, ss), ss->fileid, ss->magic);
if (tryfix)
{
// table_nuke_segments_page(d, ss, f);
r = 0;
}
/* no point checking hash if magic was wrong, we've either
* nuked the page or left it in indeterminate state */
return r;
}
h = SEGMENTS_HASH(ss);
if (ss->hash != h)
{
/* a half-written page maybe? */
r = -1;
logmsg_f(LOG_CRIT, "ss->hash invalid at offset %p! "
"(fileid=%lu(?), hash=%x wanted=%x)",
calc_segments_offset(d, ss), ss->fileid,
ss->hash, h);
if (tryfix)
{
table_recover_segments_page(d, ss);
r = 0;
}
}
return r;
}
/* get_ inline functions {{{ */
inline struct fileid *get_files_offset(struct dat_file *f) /* {{{ */
{
return (struct fileid *)(f->m + PAGE_SIZE);
} /* }}} */
inline struct segments *get_segments_offset(struct dat_file *s) /* {{{ */
{
return (struct segments *)(s->m + PAGE_SIZE);
} /* }}} */
inline struct segments *get_segments_offset_for_page(struct dat_file *s, uint32_t p) /* {{{ */
{
return (struct segments *)(s->m + (PAGE_SIZE*p));
} /* }}} */
inline struct segments *get_first_segments_for_file(struct dat_files *d, struct fileid *f) /* {{{ */
{
if (f->first_segments > d->s.sh->segments_allocated)
{
logmsg_f(LOG_CRIT, "%s looks corrupt, fileid=%lu has "
"first_segments=%lu, but segments_allocated=%lu",
d->basename, f->fileid, f->first_segments,
d->s.sh->segments_allocated);
return &sentinels.segment_page_out_of_bounds;
}
return (struct segments *)(&d->s.m[PAGE_SIZE * f->first_segments]);
} /* }}} */
inline struct segments *get_cur_segments_for_file(struct dat_files *d, struct fileid *f) /* {{{ */
{
if (f->cur_segments > d->s.sh->segments_allocated)
{
logmsg_f(LOG_CRIT, "%s looks corrupt, fileid=%lu has "
"cur_segments=%lu, but segments_allocated=%lu",
d->basename, f->fileid, f->cur_segments,
d->s.sh->segments_allocated);
return &sentinels.segment_page_out_of_bounds;
}
return (struct segments *)(&d->s.m[PAGE_SIZE * f->cur_segments]);
} /* }}} */
inline struct segments *get_next_segments(mmap_t s, struct segments *ss) /* {{{ */
{
return ss->next_segments ?
(struct segments *)(&s[PAGE_SIZE * ss->next_segments]) : NULL;
} /* }}} */
inline struct segment *get_next_segment(struct segment *s) /* {{{ */
{
return (struct segment *) ( (char *)s
+ sizeof(struct segment) + s->msgid_len );
} /* }}} */
/* }}} */
struct fileid *add_fileid(struct dat_file *fdat, const fileid_t fileid)
{
uint64_t n;
struct fileid *f;
/* check we're not overruning mmap_f - this should not happen now as
* we use fixed size .fdats; 100k files per fdat */
if (fdat->fh->files_used == fdat->fh->files_allocated)
{
n = fdat->fh->files_allocated * 2;
fdat_resize(n, fdat); /* updates fdat->fh->files_allocated */
}
f = &fdat->files[fileid - fdat->fh->fileid_min_req];
f->fileid = fileid;
fdat->fh->files_used++;
if (fileid < fdat->fh->fileid_min)
fdat->fh->fileid_min = fileid;
if (fileid > fdat->fh->fileid_max)
fdat->fh->fileid_max = fileid;
return f;
}
int32_t table_lock_init(struct dat_files *d)
{
int32_t e;
if ((e = pthread_rwlock_init(&d->t_lock, NULL)))
{
logmsg_f(LOG_ERR, "pthread_rwlock_init: %s", strerror(e));
return -1;
}
return 0;
}
int32_t table_lock_free(struct dat_files *d)
{
return pthread_rwlock_destroy(&d->t_lock);
}
int32_t table_lock(struct dat_files *d, int8_t flags)
{
int32_t e;
if (flags & T_WRLOCK && (e = pthread_rwlock_wrlock(&d->t_lock)))
{
logmsg_f(LOG_ERR, "pthread_rwlock_wrlock: %s", strerror(e));
return -1;
}
else if (flags & T_RDLOCK && (e = pthread_rwlock_rdlock(&d->t_lock)))
{
logmsg_f(LOG_ERR, "pthread_rwlock_rdlock: %s", strerror(e));
return -1;
}
return 0;
}
int32_t table_unlock(struct dat_files *d)
{
int32_t e;
if ((e = pthread_rwlock_unlock(&d->t_lock)))
{
logmsg_f(LOG_ERR, "pthread_rwlock_unlock: %s", strerror(e));
return -1;
}
return 0;
}
int32_t tables_sync()
{
/* sync all open tables to disk */
table_count_t i;
struct dat_files *r;
for(i = 0 ; i < g->t->dats_allocated ; i++)
{
r = &g->t->dats[i];
if (r->f.fd) fsync(r->f.fd);
if (r->s.fd)
{
fsync(r->s.fd);
/* if the sdat was dirty, it's not now */
if (r->s.sh->flags & SDAT_FLAGS_DIRTY)
r->s.sh->flags &= ~(SDAT_FLAGS_DIRTY);
}
}
return 0;
}
/* *dat resizing functions {{{ */
int32_t resize_mmaped_file(uint64_t newbytes, struct dat_file *d)
{
/* resize the file to newbytes and make a new mmap */
ssize_t oldbytes = filesize(d->fd);
if (-1 == ftruncate(d->fd, newbytes))
{
logmsg_f(LOG_ERR, "ftruncate: %s", STR_errno);
return -1;
}
/* FIXME: we need to lock this dat_file .. */
munmap(d->m, oldbytes);
d->m = mmap(NULL, newbytes, PROT_RW, MAP_SHARED, d->fd, 0);
if (d->m == MAP_FAILED)
{
logmsg_f(LOG_CRIT, "mmap: %s", STR_errno);
return -1;
}
return 0;
}
int32_t fdat_resize(uint64_t new_slots, struct dat_file *f)
{
uint64_t newbytes = PAGE_SIZE + (sizeof(struct fileid) * new_slots);
if (resize_mmaped_file(newbytes, f) == -1)
return -1;
/* always assume the mmap has moved */
f->files = get_files_offset(f);
/* always assume the mmap has moved */
f->fh = (struct fdat_header *)f->m;
#if USE_M_LOCK
/* update the size of m_lock too */
f->m_lock = safe_realloc(f->m_lock, new_slots);
#endif
f->fh->files_allocated = new_slots;
return 0;
}
int32_t sdat_resize(uint64_t new_slots, struct dat_file *s)
{
uint64_t newbytes = PAGE_SIZE + (PAGE_SIZE * new_slots);
if (resize_mmaped_file(newbytes, s) == -1)
return -1;
/* do not include sdat mmaps in core files */
madvise(s->m, newbytes, MADV_NOCORE);
/* always assume the mmap has moved */
s->sh = (struct sdat_header *)s->m;
s->sh->segments_allocated = new_slots;
return 0;
}
/* }}} */
int32_t table_nuke_fileid(struct dat_files *d, struct fileid *f,fileid_t fileid)
{
/* an entire file is unrecoverable and must be nuked. generally,
* this means f->magic was wrong; if we can't trust that, we can't
* trust anything in the page? */
(void)d; /* may come in useful someday */
logmsg_f(LOG_CRIT, "DATALOSS: NUKING FDAT STRUCT FOR FILEID=%lu",
fileid);
bzero(f, sizeof(struct fileid));
return 0;
}
int32_t table_nuke_segments_page(struct dat_files *d, struct segments *ss)
{
(void)d;
/* blat the entire page. Set everything to zero. */
bzero(ss, sizeof(struct segments));
/* this leaves whatever fileid the segments page was for, with a
* broken chain of next_segments (or bad first_segments or
* cur_segments even). We'll deal with it later in
* table_repair_rebuild_fileid? */
return 0;
}
int32_t table_recover_segments_page(struct dat_files *d, struct segments *ss)
{
/* ss has an incorrect hash - see if we can work out which of the
* recently added segments caused it */
(void)d;
segment_count_t new_segment_count = 0;
fileid_size_t hash_check_size = 0, this_segment_size = 0;
struct segment *s;
s = ss->segments;
while(hash_check_size < ss->size_used)
{
new_segment_count++;
this_segment_size = sizeof(struct segment) + s->msgid_len;
hash_check_size += this_segment_size;
if (ss->hash == MurmurHash2((char *)ss + sizeof(struct segments), hash_check_size, MURMURHASH_SEED))
{
logmsg_f(LOG_CRIT,
"DATALOSS: DROPPED %d BYTES OF SEGMENT DATA FROM FILEID=%lu!",
ss->size_used - hash_check_size, ss->fileid);
/* we have found our last-known-good point */
ss->size_used = hash_check_size;
ss->segment_count = new_segment_count;
}
s = (struct segment *)((char *)ss->segments + hash_check_size);
}
return 0;
}
int32_t table_repair_rebuild_fileid(struct dat_files *d, struct fileid *f, fileid_t fileid)
{
/* attempt to reconstruct the fileid struct pointed to by f, by
* scanning the sdat (in d->s) for matching fileid pages.
* fileid is passed separately because nothing in f
* (including f->fileid) can be trusted at this point.
* Do not start writing into f until we are sure we have something
* sane to put there.
* Do not read from f, at all.
*/
uint32_t h;
page_off_t pageno;
struct segments *ss, *prev_ss;
/* we'll write into here, then memcpy it over when we're happy */
struct fileid temp_f;
logmsg_f(LOG_NOTICE, "attempting to rebuild fdat for fileid=%lu", fileid);
/* basic stuff first */
temp_f.fileid = fileid;
temp_f.magic = FILEID_MAGIC;
temp_f.page_alloc = 0;
temp_f.first_segments = 0;
/* for updating the previous next_segments when we find another page */
prev_ss = NULL;
/* start at the beginning (but skip s->sh, which would be pageno = 0) */
for(pageno = 1 ; pageno < d->s.sh->segments_allocated ; pageno++)
{
ss = get_segments_offset_for_page(&d->s, pageno);
/* technically neither of these should happen here, because
* we're called from table_check which has already
* sequentially checked the sdat. checking magic is cheap
* though, but we could comment out hash at some point */
if (ss->magic != SEGMENTS_MAGIC)
{
/* we have discovered a dodgy page :/ */
logmsg_f(LOG_CRIT, "ss->magic invalid at offset %p! "
"(fileid=%lu(?), magic=%x)",
calc_segments_offset(d, ss), ss->fileid, ss->magic);
logmsg_f(LOG_NOTICE, "this shouldn't happen! "
"table_check should have found this");
abort();
}
h = SEGMENTS_HASH(ss);
if (ss->hash != h)
{
logmsg_f(LOG_CRIT, "ss->hash invalid at offset %p! "
"(fileid=%lu(?), hash=%x wanted=%x)",
calc_segments_offset(d, ss), ss->fileid,
ss->hash, h);
abort();
}
if (ss->fileid != fileid)
continue; /* not our fileid */
/* ss->fileid == fileid;
* we have found the fileid we're looking for! */
if (temp_f.first_segments == 0)
temp_f.first_segments = pageno;
if (prev_ss)
prev_ss->next_segments = pageno;
temp_f.page_alloc++;
temp_f.cur_segments = pageno;
prev_ss = ss;
}
if (temp_f.page_alloc == 0)
{
logmsg_f(LOG_NOTICE, "failed to rebuild fdat for fileid=%lu, "
"no sdat data found", fileid);
/* the only thing we can do is blat the fdat page */
table_nuke_fileid(d, f, fileid);
/* we never found a page with our fileid */
return -1;
}
logmsg(LOG_DEBUG, "reconstructing fileid gives: "
"page_alloc=%d, first_segments=%d, cur_segments=%d",
temp_f.page_alloc, temp_f.first_segments,
temp_f.cur_segments);
memcpy(f, &temp_f, sizeof(struct fileid));
return 0;
}
int32_t table_backup_eachfile(struct dat_files *d, struct dat_file *f, char *suffix, struct tm *tm)
{
fd_t fd;
off_t offset;
ssize_t bytes_to_go, r, len;
struct stat sb;
char newfile[PATH_MAX], tmp[PATH_MAX];
char *buf;
basename_r(d->basename, tmp);
snprintf(newfile, PATH_MAX, "%s/backups/%s.%s.%04d%02d%02d-%02d%02d",
g->cfg.dbroot, tmp, suffix,
tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday, tm->tm_hour,tm->tm_min);
if (fstat(f->fd, &sb))
{
logmsg_f(LOG_CRIT, "fstat: %s", STR_errno);
return -1;
}
bytes_to_go = sb.st_size;
len = 128 * 1024; /* 128k read/writes are set here */
buf = safe_malloc(len);
if ((fd = open(newfile, O_WRONLY | O_CREAT, (mode_t)0644)) == -1)
{
logmsg_f(LOG_CRIT, "open: %s", STR_errno);
return -1;
}
offset = 0;
while(bytes_to_go)
{
r = pread(f->fd, buf, len, offset);
if (r == -1)
{
logmsg_f(LOG_CRIT, "pread: %s", STR_errno);
close(fd);
free(buf);
return -1;
}
if (pwrite(fd, buf, r, offset) == -1)
{
logmsg_f(LOG_CRIT, "pwrite: %s", STR_errno);
close(fd);
free(buf);
return -1;
}
bytes_to_go -= r;
offset += r;
}
close(fd);
free(buf);
return 0;
}
int32_t table_backup(struct dat_files *d)
{
/* given a fully open table in d, create backups of both dat files
* into dbroot/backups/basename.sdat.YYYYMMDDHHMM
*/
/* no d->opened check, we could be called from
* table_open -> table_check and it may still be false */
struct tm tm;
time_t now = time(NULL);
gmtime_r(&now, &tm);
logmsg_f(LOG_NOTICE, "backing up table %s", d->basename);
if (table_backup_eachfile(d, &d->s, "sdat", &tm))
return -1;
if (table_backup_eachfile(d, &d->f, "fdat", &tm))
return -1;
logmsg_f(LOG_NOTICE, "backed up table %s", d->basename);
return 0;
}
int32_t table_check(struct dat_files *d)
{
/*
* Checking/Repairing a table:
* 1) check every page in the .sdat sequentially:
* 1a) if magic is wrong, call table_nuke_segments_page
* (set everything to zero to indicate unused page)
* (we'll deal with the fdat in 2)
* 1b) if hash is wrong, invoke table_recover_segments_page
* (FIXME: does this cope with a new size_used=0?
*
* 2) check every page in the .fdat sequentially:
* 2a) if magic is wrong, invoke table_repair_rebuild_fileid
* 2b) ensure the first_segments look sane by checking the
* sdat->fileid matches
* 2c) do the same for the entire chain of ->next_segments
* (this is where we find blatted sdat page)
* 2d) and the same for f->cur_segments
*
* (if any of 2b/c/d are wrong, invoke table_repair_rebuild_fileid)
*/
bool backup_done = false;
uint32_t h;
fileid_t tf;
page_off_t i;
struct fileid *f;
struct segments *ss;
/* no t_lock, we are only ever called from somewhere with the lock */
/* no d->opened check, we could be called from
* table_open and it may still be false */
logmsg_f(LOG_NOTICE, "checking table %s", d->basename);
/* 1) sequential sdat check */
for(i = 1 ; i < d->s.sh->next_free_segment ; i++)
{
ss = get_segments_offset_for_page(&d->s, i);
if (ss->magic == 0 && ss->fileid == 0)
continue;
if (ss->magic != SEGMENTS_MAGIC)
{
logmsg_f(LOG_CRIT, "ss->magic invalid at offset %p! "
"(fileid=%lu(?), magic=%x)",
calc_segments_offset(d, ss), ss->fileid, ss->magic);
/* probably a corrupted page; if the magic
* is wrong then we can't guarantee any of
* the rest of it is right, so we'll have to
* nuke the entire page */
if (!backup_done)
{
table_backup(d); backup_done = true;
}
table_nuke_segments_page(d, ss);
}
if (ss->hash != (h = SEGMENTS_HASH(ss)))
{
/* partially corrupted page */
logmsg_f(LOG_CRIT, "ss->hash invalid at offset %p! "
"(fileid=%lu, hash=%x wanted=%x)",
calc_segments_offset(d, ss), ss->fileid,
ss->hash, h);
if (!backup_done)
{
table_backup(d); backup_done = true;
}
table_recover_segments_page(d, ss);
}
if (ss->next_segments >= d->s.sh->next_free_segment)
{
/* next_segments points to a segments struct that
* hasn't been allocated yet. This is wrong! */
logmsg_f(LOG_CRIT, "ss->next_segments invalid at offset %p! "
"(fileid=%lu, next_segments=%lu",
calc_segments_offset(d, ss), ss->next_segments);
if (!backup_done)
{
table_backup(d); backup_done = true;
}
ss->next_segments = 0;
/* we should check the chain now.. */
}
}
f = get_files_offset(&d->f);
/* 2) sequential fdat check */
for(i = 0 ; i < d->f.fh->files_allocated ; i++, f++)
{
if (f->magic == 0 && f->fileid == 0)
continue;
/* work out the fileid that this page *should* be for */
tf = d->f.fh->fileid_min_req +
((calc_file_offset(d, f) - PAGE_SIZE) /
sizeof(struct fileid));
if (f->fileid != tf)
{
/* fileid in the fdat doesn't match what it should
* be (calculated from offset) */
logmsg_f(LOG_ERR, "f->fileid invalid at offset %p! "
"(fileid=%lu, but got %lu",
calc_file_offset(d, f), tf, f->fileid);
if (!backup_done)
{
table_backup(d); backup_done = true;
}
/* skip all subsequent checks;
* the fdat is broken and needs repairing */
table_repair_rebuild_fileid(d, f, tf);
continue;
}
/* 2a check magic in fdat */
if (f->magic != FILEID_MAGIC)
{
/* incorrect magic in the fdat */
logmsg_f(LOG_ERR, "f->magic invalid at offset %p! "
"(fileid=%lu, magic=%x)",
calc_file_offset(d, f), tf, f->magic);
if (!backup_done)
{
table_backup(d); backup_done = true;
}
/* skip all subsequent checks;
* the fdat is broken and needs repairing */
table_repair_rebuild_fileid(d, f, tf);
continue;
}
/* 2b check first_segments looks sane */
ss = get_first_segments_for_file(d, f);
if (ss->fileid != f->fileid)
{
/* first_segments points at an sdat that isn't ours */
if (!backup_done)
{
table_backup(d); backup_done = true;
}
/* skip all subsequent checks;
* the fdat is broken and needs repairing */
table_repair_rebuild_fileid(d, f, tf);
continue;
}
/* 2c now check the chain of next_segments */
while ((ss = get_next_segments(d->s.m, ss)))
{
if (ss == &sentinels.segment_page_out_of_bounds)
{
/* this segments page is nonsense; this
* usually means the segment points outside
* the allocated sdat range */
abort();
}
if (ss->fileid != f->fileid)
{
/* ss points at an sdat that isn't ours */
if (!backup_done)
{
table_backup(d); backup_done = true;
}
/* skip all subsequent checks;
* the fdat is broken and needs repairing */
table_repair_rebuild_fileid(d, f, tf);
break;
/* we should go the next fileid here;
* continue won't do it because we're in an
* inner loop. break is better but will
* still cause 2d to be run. But that's ok */
}
} /* get_next_segments() */
/* 2d check cur_segments in much the same way */
ss = get_cur_segments_for_file(d, f);
if (ss->fileid != f->fileid)
{
/* cur_segments points at an sdat that isn't ours */
if (!backup_done)
{
table_backup(d); backup_done = true;
}
/* skip all subsequent checks;
* the fdat is broken and needs repairing */
table_repair_rebuild_fileid(d, f, tf);
continue;
}
} /* for() */
/* table has now been verified to be clean */
d->s.sh->flags &= ~(SDAT_FLAGS_DIRTY);
logmsg_f(LOG_NOTICE, "table %s marked clean", d->basename);
return 0;
}
int32_t table_close_no_t_lock(struct dat_files *d)
{
struct dat_file *s = &d->s;
struct dat_file *f = &d->f;
struct stat sb;
if (!d->opened) return 0;
d->opened = false; /* not open anymore */
if (s->fd && s->m)
{
if (fstat(s->fd, &sb))
{
logmsg_f(LOG_ERR, "fstat (sdat): %s", STR_errno);
return -1;
}
munmap(s->m, sb.st_size);
s->m = NULL;
}
if (s->fd)
{
logmsg_f(LOG_NOTICE, "closing %s.sdat [fd=%d]", d->basename, s->fd);
close(s->fd);
s->fd = 0;
}
if (f->fd && f->m)
{
if (fstat(f->fd, &sb))
{
logmsg_f(LOG_ERR, "fstat (fdat): %s", STR_errno);
return -1;
}
munmap(f->m, sb.st_size);
f->m = NULL;
}
if (f->fd)
{
logmsg_f(LOG_NOTICE, "closing %s.fdat [fd=%d]", d->basename, f->fd);
close(f->fd);
f->fd = 0;
}
#if USE_M_LOCK
free(f->m_lock);
f->m_lock = NULL;
#endif
if (d->basename)
{
free(d->basename);
d->basename = NULL;
}
return 0;
}
int32_t table_close(struct dat_files *d)
{
int32_t e;
/* we need a write lock; this ensures nothing is using this table
* while we close it */
if (table_lock(d, T_WRLOCK))
{
logmsg_f(LOG_ERR, "table_lock error");
return -1;
}
e = table_close_no_t_lock(d);
table_unlock(d);
return e;
}
int32_t tables_close_all()
{
table_count_t i;
struct dat_files *r;
tables_sync();
for(i = 0 ; i < g->t->dats_allocated ; i++)
{
r = &g->t->dats[i];
if (r->opened)
table_close(r);
}
return 0;
}
int32_t tables_close_lru()
{
/* close the first 5 tables that haven't been looked at for over a
* day; this is so we don't run out of space in g->t->open_tables.
*
* there is no locking around this - it relies on the fact that we
* have more g->t->dats_allocated than we will ever possibly need,
* so the oldest ones have rolled off our retention and are now
* unreferenced
*/
return 0;
}
/* table_open_existing_* functions {{{ */
int32_t table_open_existing_fdat(struct dat_files *d)
{
uint64_t c;
struct stat sb;
char filename[PATH_MAX];
struct dat_file *f = &d->f;