forked from NLnetLabs/nsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udb.c
2100 lines (1970 loc) · 58.3 KB
/
udb.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
/* udb.c - u(micro) data base.
* By W.C.A. Wijngaards
* Copyright 2010, NLnet Labs.
* BSD, see LICENSE.
*/
#include "config.h"
#include "udb.h"
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "lookup3.h"
#include "util.h"
/* mmap and friends */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
/* for systems without, portable definition, failed-1 and async is a flag */
#ifndef MAP_FAILED
#define MAP_FAILED ((void*)-1)
#endif
#ifndef MS_SYNC
#define MS_SYNC 0
#endif
/** move and fixup xl segment */
static void move_xl_segment(void* base, udb_base* udb, udb_void xl,
udb_void n, uint64_t sz, uint64_t startseg);
/** attempt to compact the data and move free space to the end */
static int udb_alloc_compact(void* base, udb_alloc* alloc);
/** convert pointer to the data part to a pointer to the base of the chunk */
static udb_void
chunk_from_dataptr(udb_void data)
{
/* we use that sizeof(udb_chunk_d) != sizeof(udb_xl_chunk_d) and
* that xl_chunk_d is aligned on x**1024 boundaries. */
udb_void xl = data - sizeof(udb_xl_chunk_d);
if( (xl & (UDB_ALLOC_CHUNK_SIZE-1)) == 0)
return xl;
return data - sizeof(udb_chunk_d);
}
udb_void chunk_from_dataptr_ext(udb_void data) {
return chunk_from_dataptr(data);
}
#ifndef NDEBUG
/** read last octet from a chunk */
static uint8_t
chunk_get_last(void* base, udb_void chunk, int exp)
{
return *((uint8_t*)UDB_REL(base, chunk+(1<<exp)-1));
}
#endif
/** write last octet of a chunk */
static void
chunk_set_last(void* base, udb_void chunk, int exp, uint8_t value)
{
assert(exp >= 0 && exp <= 63);
*((uint8_t*)UDB_REL(base, chunk+((uint64_t)1<<exp)-1)) = value;
}
/** create udb_base from a file descriptor (must be at start of file) */
udb_base*
udb_base_create_fd(const char* fname, int fd, udb_walk_relptr_func walkfunc,
void* arg)
{
uint64_t m, fsz;
udb_glob_d g;
ssize_t r;
udb_base* udb = (udb_base*)xalloc_zero(sizeof(*udb));
if(!udb) {
log_msg(LOG_ERR, "out of memory");
close(fd);
return NULL;
}
udb->fname = strdup(fname);
if(!udb->fname) {
log_msg(LOG_ERR, "out of memory");
free(udb);
close(fd);
return NULL;
}
udb->walkfunc = walkfunc;
udb->walkarg = arg;
udb->fd = fd;
udb->ram_size = 1024;
udb->ram_mask = (int)udb->ram_size - 1;
udb->ram_hash = (udb_ptr**)xalloc_array_zero(sizeof(udb_ptr*),
udb->ram_size);
if(!udb->ram_hash) {
free(udb->fname);
free(udb);
log_msg(LOG_ERR, "out of memory");
close(fd);
return NULL;
}
/* read magic */
if((r=read(fd, &m, sizeof(m))) == -1) {
log_msg(LOG_ERR, "%s: %s", fname, strerror(errno));
goto fail;
} else if(r != (ssize_t)sizeof(m)) {
log_msg(LOG_ERR, "%s: file too short", fname);
goto fail;
}
/* TODO : what if bigendian and littleendian file, see magic */
if(m != UDB_MAGIC) {
log_msg(LOG_ERR, "%s: wrong type of file", fname);
goto fail;
}
/* read header */
if((r=read(fd, &g, sizeof(g))) == -1) {
log_msg(LOG_ERR, "%s: %s\n", fname, strerror(errno));
goto fail;
} else if(r != (ssize_t)sizeof(g)) {
log_msg(LOG_ERR, "%s: file too short", fname);
goto fail;
}
if(g.version != 0) {
log_msg(LOG_ERR, "%s: unknown file version %d", fname,
(int)g.version);
goto fail;
}
if(g.hsize < UDB_HEADER_SIZE) {
log_msg(LOG_ERR, "%s: header size too small %d", fname,
(int)g.hsize);
goto fail;
}
if(g.hsize > UDB_HEADER_SIZE) {
log_msg(LOG_WARNING, "%s: header size too large %d", fname,
(int)g.hsize);
goto fail;
}
if(g.clean_close != 1) {
log_msg(LOG_WARNING, "%s: not cleanly closed %d", fname,
(int)g.clean_close);
goto fail;
}
if(g.dirty_alloc != 0) {
log_msg(LOG_WARNING, "%s: not cleanly closed (alloc:%d)", fname,
(int)g.dirty_alloc);
goto fail;
}
/* check file size correctly written, for 4.0.2 nsd.db failure */
fsz = (uint64_t)lseek(fd, (off_t)0, SEEK_END);
(void)lseek(fd, (off_t)0, SEEK_SET);
if(g.fsize != fsz) {
log_msg(LOG_WARNING, "%s: file size %llu but mmap header "
"has size %llu", fname, (unsigned long long)fsz,
(unsigned long long)g.fsize);
goto fail;
}
/* mmap it */
if(g.fsize < UDB_HEADER_SIZE || g.fsize < g.hsize) {
log_msg(LOG_ERR, "%s: file too short", fname);
goto fail;
}
if(g.fsize > (uint64_t)400*1024*1024*1024*1024) /* 400 Tb */ {
log_msg(LOG_WARNING, "%s: file size too large %llu",
fname, (unsigned long long)g.fsize);
goto fail;
}
udb->base_size = (size_t)g.fsize;
#ifdef HAVE_MMAP
/* note the size_t casts must be there for portability, on some
* systems the layout of memory is otherwise broken. */
udb->base = mmap(NULL, (size_t)udb->base_size,
(int)PROT_READ|PROT_WRITE, (int)MAP_SHARED,
(int)udb->fd, (off_t)0);
#else
udb->base = MAP_FAILED; errno = ENOSYS;
#endif
if(udb->base == MAP_FAILED) {
udb->base = NULL;
log_msg(LOG_ERR, "mmap(size %u) error: %s",
(unsigned)udb->base_size, strerror(errno));
fail:
close(fd);
free(udb->fname);
free(udb->ram_hash);
free(udb);
return NULL;
}
/* init completion */
udb->glob_data = (udb_glob_d*)((char*)udb->base+sizeof(uint64_t));
r = 0;
/* cannot be dirty because that is goto fail above */
if(udb->glob_data->dirty_alloc != udb_dirty_clean)
r = 1;
udb->alloc = udb_alloc_create(udb, (udb_alloc_d*)(
(char*)udb->glob_data+sizeof(*udb->glob_data)));
if(!udb->alloc) {
log_msg(LOG_ERR, "out of memory");
udb_base_free(udb);
return NULL;
}
if(r) {
/* and compact now, or resume compacting */
udb_alloc_compact(udb, udb->alloc);
udb_base_sync(udb, 1);
}
udb->glob_data->clean_close = 0;
return udb;
}
udb_base* udb_base_create_read(const char* fname, udb_walk_relptr_func walkfunc,
void* arg)
{
int fd = open(fname, O_RDWR);
if(fd == -1) {
log_msg(LOG_ERR, "%s: %s", fname, strerror(errno));
return NULL;
}
return udb_base_create_fd(fname, fd, walkfunc, arg);
}
/** init new udb_global structure */
static void udb_glob_init_new(udb_glob_d* g)
{
memset(g, 0, sizeof(*g));
g->hsize = UDB_HEADER_SIZE;
g->fsize = UDB_HEADER_SIZE;
}
/** write data to file and check result */
static int
write_fdata(const char* fname, int fd, void* data, size_t len)
{
ssize_t w;
if((w=write(fd, data, len)) == -1) {
log_msg(LOG_ERR, "%s: %s", fname, strerror(errno));
close(fd);
return 0;
} else if(w != (ssize_t)len) {
log_msg(LOG_ERR, "%s: short write (disk full?)", fname);
close(fd);
return 0;
}
return 1;
}
udb_base* udb_base_create_new(const char* fname, udb_walk_relptr_func walkfunc,
void* arg)
{
uint64_t m;
udb_glob_d g;
udb_alloc_d a;
uint64_t endsize = UDB_HEADER_SIZE;
uint64_t endexp = 0;
int fd = open(fname, O_CREAT|O_RDWR, 0600);
if(fd == -1) {
log_msg(LOG_ERR, "%s: %s", fname, strerror(errno));
return NULL;
}
m = UDB_MAGIC;
udb_glob_init_new(&g);
udb_alloc_init_new(&a);
g.clean_close = 1;
/* write new data to file (closes fd on error) */
if(!write_fdata(fname, fd, &m, sizeof(m)))
return NULL;
if(!write_fdata(fname, fd, &g, sizeof(g)))
return NULL;
if(!write_fdata(fname, fd, &a, sizeof(a)))
return NULL;
if(!write_fdata(fname, fd, &endsize, sizeof(endsize)))
return NULL;
if(!write_fdata(fname, fd, &endexp, sizeof(endexp)))
return NULL;
/* rewind to start */
if(lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
log_msg(LOG_ERR, "%s: lseek %s", fname, strerror(errno));
close(fd);
return NULL;
}
/* truncate to the right size */
if(ftruncate(fd, (off_t)g.fsize) < 0) {
log_msg(LOG_ERR, "%s: ftruncate(%d): %s", fname,
(int)g.fsize, strerror(errno));
close(fd);
return NULL;
}
return udb_base_create_fd(fname, fd, walkfunc, arg);
}
/** shrink the udb base if it has unused space at the end */
static void
udb_base_shrink(udb_base* udb, uint64_t nsize)
{
udb->glob_data->dirty_alloc = udb_dirty_fsize;
udb->glob_data->fsize = nsize;
/* sync, does not *seem* to be required on Linux, but it is
certainly required on OpenBSD. Otherwise changed data is lost. */
#ifdef HAVE_MMAP
msync(udb->base, udb->base_size, MS_ASYNC);
#endif
if(ftruncate(udb->fd, (off_t)nsize) != 0) {
log_msg(LOG_ERR, "%s: ftruncate(%u) %s", udb->fname,
(unsigned)nsize, strerror(errno));
}
udb->glob_data->dirty_alloc = udb_dirty_clean;
}
void udb_base_close(udb_base* udb)
{
if(!udb)
return;
if(udb->fd != -1 && udb->base && udb->alloc) {
uint64_t nsize = udb->alloc->disk->nextgrow;
if(nsize < udb->base_size)
udb_base_shrink(udb, nsize);
}
if(udb->fd != -1) {
udb->glob_data->clean_close = 1;
close(udb->fd);
udb->fd = -1;
}
if(udb->base) {
#ifdef HAVE_MMAP
if(munmap(udb->base, udb->base_size) == -1) {
log_msg(LOG_ERR, "munmap: %s", strerror(errno));
}
#endif
udb->base = NULL;
}
}
void udb_base_free(udb_base* udb)
{
if(!udb)
return;
udb_base_close(udb);
udb_alloc_delete(udb->alloc);
free(udb->ram_hash);
free(udb->fname);
free(udb);
}
void udb_base_free_keep_mmap(udb_base* udb)
{
if(!udb) return;
if(udb->fd != -1) {
close(udb->fd);
udb->fd = -1;
}
udb->base = NULL;
udb_alloc_delete(udb->alloc);
free(udb->ram_hash);
free(udb->fname);
free(udb);
}
void udb_base_sync(udb_base* udb, int wait)
{
if(!udb) return;
#ifdef HAVE_MMAP
if(msync(udb->base, udb->base_size, wait?MS_SYNC:MS_ASYNC) != 0) {
log_msg(LOG_ERR, "msync(%s) error %s",
udb->fname, strerror(errno));
}
#else
(void)wait;
#endif
}
/** hash a chunk pointer */
static uint32_t
chunk_hash_ptr(udb_void p)
{
/* put p into an array of uint32 */
uint32_t h[sizeof(p)/sizeof(uint32_t)];
memcpy(&h, &p, sizeof(h));
return hashword(h, sizeof(p)/sizeof(uint32_t), 0x8763);
}
/** check that the given pointer is on the bucket for the given offset */
int udb_ptr_is_on_bucket(udb_base* udb, udb_ptr* ptr, udb_void to)
{
uint32_t i = chunk_hash_ptr(to) & udb->ram_mask;
udb_ptr* p;
assert((size_t)i < udb->ram_size);
for(p = udb->ram_hash[i]; p; p=p->next) {
if(p == ptr)
return 1;
}
return 0;
}
/** grow the ram array */
static void
grow_ram_hash(udb_base* udb, udb_ptr** newhash)
{
size_t i;
size_t osize= udb->ram_size;
udb_ptr* p, *np;
udb_ptr** oldhash = udb->ram_hash;
udb->ram_size *= 2;
udb->ram_mask <<= 1;
udb->ram_mask |= 1;
udb->ram_hash = newhash;
/* have to link in every element in the old list into the new list*/
for(i=0; i<osize; i++) {
p = oldhash[i];
while(p) {
np = p->next;
/* link into newhash */
p->prev=NULL;
p->next=newhash[chunk_hash_ptr(p->data)&udb->ram_mask];
if(p->next) p->next->prev = p;
/* go to next element of oldhash */
p = np;
}
}
free(oldhash);
}
void udb_base_link_ptr(udb_base* udb, udb_ptr* ptr)
{
uint32_t i;
#ifdef UDB_CHECK
assert(udb_valid_dataptr(udb, ptr->data)); /* must be to whole chunk*/
#endif
udb->ram_num++;
if(udb->ram_num == udb->ram_size && udb->ram_size<(size_t)0x7fffffff) {
/* grow the array, if allocation succeeds */
udb_ptr** newram = (udb_ptr**)xalloc_array_zero(
sizeof(udb_ptr*), udb->ram_size*2);
if(newram) {
grow_ram_hash(udb, newram);
}
}
i = chunk_hash_ptr(ptr->data) & udb->ram_mask;
assert((size_t)i < udb->ram_size);
ptr->prev = NULL;
ptr->next = udb->ram_hash[i];
udb->ram_hash[i] = ptr;
if(ptr->next)
ptr->next->prev = ptr;
}
void udb_base_unlink_ptr(udb_base* udb, udb_ptr* ptr)
{
assert(ptr->data);
#ifdef UDB_CHECK
assert(udb_valid_dataptr(udb, ptr->data)); /* ptr must be inited */
assert(udb_ptr_is_on_bucket(udb, ptr, ptr->data));
#endif
udb->ram_num--;
if(ptr->next)
ptr->next->prev = ptr->prev;
if(ptr->prev)
ptr->prev->next = ptr->next;
else {
uint32_t i = chunk_hash_ptr(ptr->data) & udb->ram_mask;
assert((size_t)i < udb->ram_size);
udb->ram_hash[i] = ptr->next;
}
}
/** change a set of ram ptrs to a new value */
static void
udb_base_ram_ptr_edit(udb_base* udb, udb_void old, udb_void newd)
{
uint32_t io = chunk_hash_ptr(old) & udb->ram_mask;
udb_ptr* p, *np;
/* edit them and move them into the new position */
p = udb->ram_hash[io];
while(p) {
np = p->next;
if(p->data == old) {
udb_base_unlink_ptr(udb, p);
p->data = newd;
udb_base_link_ptr(udb, p);
}
p = np;
}
}
udb_rel_ptr* udb_base_get_userdata(udb_base* udb)
{
return &udb->glob_data->user_global;
}
void udb_base_set_userdata(udb_base* udb, udb_void user)
{
#ifdef UDB_CHECK
if(user) { assert(udb_valid_dataptr(udb, user)); }
#endif
udb_rel_ptr_set(udb->base, &udb->glob_data->user_global, user);
}
void udb_base_set_userflags(udb_base* udb, uint8_t v)
{
udb->glob_data->userflags = v;
}
uint8_t udb_base_get_userflags(udb_base* udb)
{
return udb->glob_data->userflags;
}
/** re-mmap the udb to specified size */
static void*
udb_base_remap(udb_base* udb, udb_alloc* alloc, uint64_t nsize)
{
#ifdef HAVE_MMAP
void* nb;
/* for use with valgrind, do not use mremap, but the other version */
#ifdef MREMAP_MAYMOVE
nb = mremap(udb->base, udb->base_size, nsize, MREMAP_MAYMOVE);
if(nb == MAP_FAILED) {
log_msg(LOG_ERR, "mremap(%s, size %u) error %s",
udb->fname, (unsigned)nsize, strerror(errno));
return 0;
}
#else /* !HAVE MREMAP */
/* use munmap-mmap to simulate mremap */
if(munmap(udb->base, udb->base_size) != 0) {
log_msg(LOG_ERR, "munmap(%s) error %s",
udb->fname, strerror(errno));
}
/* provide hint for new location */
/* note the size_t casts must be there for portability, on some
* systems the layout of memory is otherwise broken. */
nb = mmap(udb->base, (size_t)nsize, (int)PROT_READ|PROT_WRITE,
(int)MAP_SHARED, (int)udb->fd, (off_t)0);
/* retry the mmap without basept in case of ENOMEM (FreeBSD8),
* the kernel can then try to mmap it at a different location
* where more memory is available */
if(nb == MAP_FAILED && errno == ENOMEM) {
nb = mmap(NULL, (size_t)nsize, (int)PROT_READ|PROT_WRITE,
(int)MAP_SHARED, (int)udb->fd, (off_t)0);
}
if(nb == MAP_FAILED) {
log_msg(LOG_ERR, "mmap(%s, size %u) error %s",
udb->fname, (unsigned)nsize, strerror(errno));
udb->base = NULL;
return 0;
}
#endif /* HAVE MREMAP */
if(nb != udb->base) {
/* fix up realpointers in udb and alloc */
/* but mremap may have been nice and not move the base */
udb->base = nb;
udb->glob_data = (udb_glob_d*)((char*)nb+sizeof(uint64_t));
/* use passed alloc pointer because the udb->alloc may not
* be initialized yet */
alloc->disk = (udb_alloc_d*)((char*)udb->glob_data
+sizeof(*udb->glob_data));
}
udb->base_size = nsize;
return nb;
#else /* HAVE_MMAP */
(void)udb; (void)alloc; (void)nsize;
return NULL;
#endif /* HAVE_MMAP */
}
void
udb_base_remap_process(udb_base* udb)
{
/* assume that fsize is still accessible */
udb_base_remap(udb, udb->alloc, udb->glob_data->fsize);
}
/** grow file to specified size and re-mmap, return new base */
static void*
udb_base_grow_and_remap(udb_base* udb, uint64_t nsize)
{
/* grow file by writing a single zero at that spot, the
* rest is filled in with zeroes. */
uint8_t z = 0;
ssize_t w;
assert(nsize > 0);
udb->glob_data->dirty_alloc = udb_dirty_fsize;
#ifdef HAVE_PWRITE
if((w=pwrite(udb->fd, &z, sizeof(z), (off_t)(nsize-1))) == -1) {
#else
if(lseek(udb->fd, (off_t)(nsize-1), SEEK_SET) == -1) {
log_msg(LOG_ERR, "fseek %s: %s", udb->fname, strerror(errno));
return 0;
}
if((w=write(udb->fd, &z, sizeof(z))) == -1) {
#endif
log_msg(LOG_ERR, "grow(%s, size %u) error %s",
udb->fname, (unsigned)nsize, strerror(errno));
return 0;
} else if(w != (ssize_t)sizeof(z)) {
log_msg(LOG_ERR, "grow(%s, size %u) failed (disk full?)",
udb->fname, (unsigned)nsize);
return 0;
}
udb->glob_data->fsize = nsize;
udb->glob_data->dirty_alloc = udb_dirty_clean;
return udb_base_remap(udb, udb->alloc, nsize);
}
int udb_exp_size(uint64_t a)
{
/* find enclosing value such that 2**x >= a */
int x = 0;
uint64_t i = a;
assert(a != 0);
i --;
/* could optimise this with uint8* access, depends on endianness */
/* first whole bytes */
while( (i&(~(uint64_t)0xff)) ) {
i >>= 8;
x += 8;
}
/* now details */
while(i) {
i >>= 1;
x ++;
}
assert( x>=0 && x<=63);
assert( ((uint64_t)1<<x) >= a);
assert( x==0 || /* <<x-1 without negative number analyzer complaints: */ (((uint64_t)1<<x)>>1) < a);
return x;
}
int udb_exp_offset(uint64_t o)
{
/* this means measuring the number of 0 bits on the right */
/* so, if exp zero bits then (o&(2**x-1))==0 */
int x = 0;
uint64_t i = o;
assert(o != 0);
/* first whole bytes */
while( (i&(uint64_t)0xff) == 0) {
i >>= 8;
x += 8;
}
/* now details */
while( (i&(uint64_t)0x1) == 0) {
i >>= 1;
x ++;
}
assert( o % ((uint64_t)1<<x) == 0);
assert( o % ((uint64_t)1<<(x+1)) != 0);
return x;
}
void udb_alloc_init_new(udb_alloc_d* a)
{
assert(UDB_HEADER_SIZE % UDB_ALLOC_CHUNK_MINSIZE == 0);
memset(a, 0, sizeof(*a));
/* set new allocations after header, as if allocated in a sequence
* of minsize allocations */
a->nextgrow = UDB_HEADER_SIZE;
}
/** fsck the file size, false if failed and file is useless */
static int
fsck_fsize(udb_base* udb, udb_alloc* alloc)
{
off_t realsize;
log_msg(LOG_WARNING, "udb-fsck %s: file size wrong", udb->fname);
realsize = lseek(udb->fd, (off_t)0, SEEK_END);
if(realsize == (off_t)-1) {
log_msg(LOG_ERR, "lseek(%s): %s", udb->fname, strerror(errno));
return 0;
}
udb->glob_data->fsize = (uint64_t)realsize;
if(!udb_base_remap(udb, alloc, (uint64_t)realsize))
return 0;
udb->glob_data->dirty_alloc = udb_dirty_clean;
log_msg(LOG_WARNING, "udb-fsck %s: file size fixed (sync)", udb->fname);
udb_base_sync(udb, 1);
return 1;
}
/** regenerate freelist add a new free chunk, return next todo */
static udb_void
regen_free(void* base, udb_void c, int exp, udb_alloc_d* regen)
{
udb_free_chunk_d* cp = UDB_FREE_CHUNK(c);
uint64_t esz = (uint64_t)1<<exp;
if(exp < UDB_ALLOC_CHUNK_MINEXP || exp > UDB_ALLOC_CHUNKS_MAX) {
return 0;
}
cp->type = udb_chunk_type_free;
cp->flags = 0;
chunk_set_last(base, c, exp, (uint8_t)exp);
cp->prev = 0;
cp->next = regen->free[exp-UDB_ALLOC_CHUNK_MINEXP];
if(cp->next)
UDB_FREE_CHUNK(cp->next)->prev = c;
regen->stat_free += esz;
return c + esz;
}
/** regenerate xl chunk, return next todo */
static udb_void
regen_xl(void* base, udb_void c, udb_alloc_d* regen)
{
udb_xl_chunk_d* cp = UDB_XL_CHUNK(c);
uint64_t xlsz = cp->size;
if( (xlsz&(UDB_ALLOC_CHUNK_SIZE-1)) != 0) {
return 0;
}
if( (c&(UDB_ALLOC_CHUNK_SIZE-1)) != 0) {
return 0;
}
/* fixup end-size and end-expmarker */
regen->stat_alloc += xlsz;
return c + xlsz;
}
/** regenerate data chunk, return next todo */
static udb_void
regen_data(void* base, udb_void c, int exp, udb_alloc_d* regen)
{
uint64_t esz = (uint64_t)1<<exp;
if(exp < UDB_ALLOC_CHUNK_MINEXP || exp > UDB_ALLOC_CHUNKS_MAX) {
return 0;
}
chunk_set_last(base, c, exp, (uint8_t)exp);
regen->stat_alloc += esz;
return c + esz;
}
/** regenerate a relptr structure inside a data segment */
static void
regen_relptr_func(void* base, udb_rel_ptr* rp, void* arg)
{
udb_void* a = (udb_void*)arg;
/* ignore 0 pointers */
if(!rp->data)
return;
/* edit relptrs that point to oldmoved to point to newmoved. */
if(rp->data == a[0])
rp->data = a[1];
/* regenerate relptr lists, add this item to the relptr list for
* the data that it points to */
udb_rel_ptr_link(base, rp, rp->data);
}
/** regenerate the relptrs store in this data segment */
static void
regen_its_ptrs(void* base, udb_base* udb, udb_chunk_d* atp,
void* data, uint64_t dsz, udb_void rb_old, udb_void rb_new)
{
udb_void arg[2];
arg[0] = rb_old; arg[1] = rb_new;
/* walk through the structs here and put them on their respective
* relptr lists */
(*udb->walkfunc)(base, udb->walkarg, atp->type, data, dsz,
®en_relptr_func, arg);
}
/** regenerate relptrlists in the file */
static void
regen_ptrlist(void* base, udb_base* udb, udb_alloc* alloc,
udb_void rb_old, udb_void rb_new)
{
udb_void at = alloc->udb->glob_data->hsize;
/* clear all ptrlist start pointers in the file. */
while(at < alloc->disk->nextgrow) {
int exp = (int)UDB_CHUNK(at)->exp;
udb_chunk_type tp = (udb_chunk_type)UDB_CHUNK(at)->type;
if(exp == UDB_EXP_XL) {
UDB_XL_CHUNK(at)->ptrlist = 0;
at += UDB_XL_CHUNK(at)->size;
} else if(tp == udb_chunk_type_free) {
at += (uint64_t)1<<exp;
} else { /* data chunk */
UDB_CHUNK(at)->ptrlist = 0;
at += (uint64_t)1<<exp;
}
}
/* walk through all relptr structs and put on the right list. */
at = alloc->udb->glob_data->hsize;
while(at < alloc->disk->nextgrow) {
udb_chunk_d* atp = UDB_CHUNK(at);
int exp = (int)atp->exp;
udb_chunk_type tp = (udb_chunk_type)atp->type;
uint64_t sz = ((exp == UDB_EXP_XL)?UDB_XL_CHUNK(at)->size:
(uint64_t)1<<exp);
if(exp == UDB_EXP_XL) {
assert(at != rb_old); /* should have been freed */
regen_its_ptrs(base, udb, atp,
((char*)atp)+sizeof(udb_xl_chunk_d),
sz-sizeof(udb_xl_chunk_d) - sizeof(uint64_t)*2,
rb_old, rb_new);
at += sz;
} else if(tp == udb_chunk_type_free) {
at += sz;
} else { /* data chunk */
assert(at != rb_old); /* should have been freed */
regen_its_ptrs(base, udb, atp,
((char*)atp)+sizeof(udb_chunk_d),
sz-sizeof(udb_chunk_d)-1, rb_old, rb_new);
at += sz;
}
}
}
/** mark free elements from ex XL chunk space and later fixups pick that up */
static void
rb_mark_free_segs(void* base, udb_void s, uint64_t m)
{
udb_void q = s + m - UDB_ALLOC_CHUNK_SIZE;
/* because of header and alignment we know s >= UDB_ALLOC_CHUNK_SIZE*/
assert(s >= UDB_ALLOC_CHUNK_SIZE);
while(q >= s) {
UDB_CHUNK(q)->exp = UDB_ALLOC_CHUNKS_MAX;
UDB_CHUNK(q)->type = udb_chunk_type_free;
q -= UDB_ALLOC_CHUNK_SIZE;
}
}
/** fsck rollback or rollforward XL move results */
static int
fsck_rb_xl(void* base, udb_base* udb, udb_void rb_old, udb_void rb_new,
uint64_t rb_size, uint64_t rb_seg)
{
if(rb_old <= rb_new)
return 0; /* XL move one way */
if( (rb_size&(UDB_ALLOC_CHUNK_SIZE-1)) != 0)
return 0; /* not aligned */
if( (rb_old&(UDB_ALLOC_CHUNK_SIZE-1)) != 0)
return 0; /* not aligned */
if( (rb_new&(UDB_ALLOC_CHUNK_SIZE-1)) != 0)
return 0; /* not aligned */
if(rb_new + rb_size <= rb_old) {
/* not overlapping: resume copy */
memcpy(UDB_CHUNK(rb_new), UDB_CHUNK(rb_old), rb_size);
/* and free up old piece(s) */
rb_mark_free_segs(base, rb_old, rb_size);
} else {
/* overlapping, see what segment we stopped at
* and continue there. */
move_xl_segment(base, udb, rb_old, rb_new, rb_size, rb_seg);
/* free up old piece(s); from the end of the moved segment,
* until the end of the old segment */
rb_mark_free_segs(base, rb_new+rb_size, (rb_old+rb_size)-
(rb_new+rb_size));
}
/* do not call fix_ptrs, regenptrs does the job */
return 1;
}
/** fsck rollback or rollforward move results */
static int
fsck_rb(void* base, udb_void rb_old, udb_void rb_new, uint64_t rb_size,
udb_void* make_free)
{
if( (rb_size&(rb_size-1)) != 0)
return 0; /* not powerof2 */
if( (rb_old&(rb_size-1)) != 0)
return 0; /* not aligned */
if( (rb_new&(rb_size-1)) != 0)
return 0; /* not aligned */
/* resume copy */
memcpy(UDB_CHUNK(rb_new), UDB_CHUNK(rb_old), rb_size);
/* do not call fix_ptrs, regenptrs does the job */
/* make sure udb_old is freed */
*make_free = rb_old;
return 1;
}
/** fsck the file and salvage, false if failed and file is useless */
static int
fsck_file(udb_base* udb, udb_alloc* alloc, int moved)
{
void* base = udb->base;
udb_alloc_d regen;
udb_void at = udb->glob_data->hsize;
udb_void rb_old = udb->glob_data->rb_old;
udb_void rb_new = udb->glob_data->rb_new;
udb_void rb_seg = udb->glob_data->rb_seg;
udb_void make_free = 0;
uint64_t rb_size = udb->glob_data->rb_size;
log_msg(LOG_WARNING, "udb-fsck %s: salvaging", udb->fname);
/* walk through the file, use the exp values to see what can be
* salvaged */
if(moved && rb_old && rb_new && rb_size) {
if(rb_old+rb_size <= alloc->disk->nextgrow
&& rb_new+rb_size <= alloc->disk->nextgrow) {
/* we can use the move information to fix up the
* duplicate element (or partially moved element) */
if(rb_size > 1024*1024) {
/* XL chunk */
if(!fsck_rb_xl(base, udb, rb_old, rb_new,
rb_size, rb_seg))
return 0;
} else {
if(!fsck_rb(base, rb_old, rb_new, rb_size,
&make_free))
return 0;
}
}
}
/* rebuild freelists */
/* recalculate stats in alloc (except 'stat_data') */
/* possibly new end 'nextgrow' value */
memset(®en, 0, sizeof(regen));
regen.nextgrow = alloc->disk->nextgrow;
while(at < regen.nextgrow) {
/* figure out this chunk */
int exp = (int)UDB_CHUNK(at)->exp;
udb_chunk_type tp = (udb_chunk_type)UDB_CHUNK(at)->type;
/* consistency check possible here with end-exp */
if(tp == udb_chunk_type_free || at == make_free) {
at = regen_free(base, at, exp, ®en);
if(!at) return 0;
} else if(exp == UDB_EXP_XL) {
/* allocated data of XL size */
at = regen_xl(base, at, ®en);
if(!at) return 0;
} else if(exp >= UDB_ALLOC_CHUNK_MINEXP
&& exp <= UDB_ALLOC_CHUNKS_MAX) {
/* allocated data */
at = regen_data(base, at, exp, ®en);
if(!at) return 0;
} else {
/* garbage; this must be EOF then */
regen.nextgrow = at;
break;
}
}
*alloc->disk = regen;
/* rebuild relptr lists */
regen_ptrlist(base, udb, alloc, rb_old, rb_new);
log_msg(LOG_WARNING, "udb-fsck %s: salvaged successfully (sync)",
udb->fname);
udb->glob_data->rb_old = 0;
udb->glob_data->rb_new = 0;
udb->glob_data->rb_size = 0;
udb->glob_data->dirty_alloc = udb_dirty_clean;
udb_base_sync(udb, 1);
return 1;
}
udb_alloc* udb_alloc_create(udb_base* udb, udb_alloc_d* disk)
{
udb_alloc* alloc = (udb_alloc*)xalloc_zero(sizeof(*alloc));
if(!alloc)
return NULL;
alloc->udb = udb;
alloc->disk = disk;
/* see if committed but uncompleted actions need to be done */
/* preserves the alloc state */
if(udb->glob_data->dirty_alloc != udb_dirty_clean) {
if(udb->glob_data->dirty_alloc == udb_dirty_fsize) {
if(fsck_fsize(udb, alloc))
return alloc;
} else if(udb->glob_data->dirty_alloc == udb_dirty_fl) {
if(fsck_file(udb, alloc, 0))
return alloc;
} else if(udb->glob_data->dirty_alloc == udb_dirty_compact) {
if(fsck_file(udb, alloc, 1))
return alloc;
}
log_msg(LOG_ERR, "error: file allocation dirty (%d)",
(int)udb->glob_data->dirty_alloc);
free(alloc);
return NULL;
}
return alloc;
}
void udb_alloc_delete(udb_alloc* alloc)
{
if(!alloc) return;
free(alloc);
}
/** unlink this element from its freelist */
static void
udb_alloc_unlink_fl(void* base, udb_alloc* alloc, udb_void chunk, int exp)
{
udb_free_chunk_d* fp = UDB_FREE_CHUNK(chunk);
assert(chunk);