-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathfile.c
9035 lines (7644 loc) · 296 KB
/
file.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 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* This module is intended for all file related bdb stuff e.g. opening,
* closing, creating database, renaming databases etc. This is all stuff that
* I've refactored out of bdb.c to try to make that monster a little more
* manageable.
*/
/* To get POSIX standard readdir_r() */
#ifdef _SUN_SOURCE
#ifndef _POSIX_PTHREAD_SEMANTICS
#define _POSIX_PTHREAD_SEMANTICS
#endif
#endif
#include "flibc.h"
#include <alloca.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#ifdef __sun
/* for PTHREAD_STACK_MIN on Solaris */
#define __EXTENSIONS__
#endif
#include <limits.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <unistd.h>
#include <stddef.h>
#include <ctype.h>
#include <dirent.h>
#include <signal.h>
#include <assert.h>
#include <poll.h>
#include <libgen.h>
#include <str0.h>
#include <build/db.h>
#include <epochlib.h>
#include <net.h>
#include <cheapstack.h>
#include "bdb_int.h"
#include "locks.h"
#include "sys_wrap.h"
#include <time.h>
#include <ctrace.h>
#include <list.h>
#include "intern_strings.h"
#include <list.h>
#include "debug_switches.h"
#include "endian_core.h"
#include "bdb_osqltrn.h"
#include "printformats.h"
#include "util.h"
#include <bb_oscompat.h>
#include <logmsg.h>
#include <build/db_int.h>
#include "dbinc/db_swap.h"
#include <dbinc/db_am.h>
#include "dbinc/log.h"
#include "dbinc/txn.h"
#include <rep_qstat.h>
#include <bdb_queuedb.h>
#include <schema_lk.h>
#include <tohex.h>
#include <timer_util.h>
#include <disttxn.h>
#include <phys_rep.h>
#include <phys_rep_lsn.h>
#include <log_trigger.h>
extern int gbl_bdblock_debug;
extern int gbl_keycompr;
extern int gbl_early;
extern int gbl_exit;
extern int gbl_fullrecovery;
extern int gbl_import_mode;
extern char *gbl_myhostname;
extern struct interned_string *gbl_myhostname_interned;
extern size_t gbl_blobmem_cap;
extern int gbl_backup_logfiles;
struct timeval last_timer_pstack;
extern int gbl_modsnap_asof;
extern int get_commit_lsn_map_switch_value();
#define FILENAMELEN 100
#define USE_GENID_IN_QUEUEDB_FILE_NAME() \
(gbl_queuedb_genid_filename || (gbl_queuedb_file_threshold > 0))
extern int is_db_roomsync();
extern int get_schema_change_in_progress(const char *func, int line);
int gbl_debug_children_lock = 0;
int gbl_queuedb_genid_filename = 1;
int gbl_queuedb_file_threshold = 0;
int gbl_queuedb_file_interval = 60000;
static const char NEW_PREFIX[] = "new.";
static pthread_once_t ONCE_LOCK = PTHREAD_ONCE_INIT;
static int notclosingdta_trace = 0;
int rep_caught_up(bdb_state_type *bdb_state);
static int bdb_del_file(bdb_state_type *bdb_state, DB_TXN *tid, char *filename,
int *bdberr);
static int bdb_free_int(bdb_state_type *bdb_state, bdb_state_type *replace,
int *bdberr);
static int bdb_close_only_int(bdb_state_type *bdb_state, DB_TXN *tid,
int *bdberr);
enum {
BDB_CLOSE_FLAGS_FLUSH = 1,
};
static int bdb_close_only_flags(bdb_state_type *, DB_TXN *, int *bdberr, int flags);
int bdb_rename_file(bdb_state_type *bdb_state, DB_TXN *tid, char *oldfile,
char *newfile, int *bdberr);
static int bdb_reopen_int(bdb_state_type *bdb_state);
static int open_dbs(bdb_state_type *, int, int, int, DB_TXN **, uint32_t, int *);
static int close_dbs(bdb_state_type *bdb_state);
static int close_dbs_txn(bdb_state_type *bdb_state, DB_TXN *tid);
static int close_dbs_flush(bdb_state_type *bdb_state);
static int bdb_watchdog_test_io_dir(bdb_state_type *bdb_state, char *dir);
int __txn_commit_map_delete_logfile_txns(DB_ENV *, int);
void berkdb_set_recovery(DB_ENV *dbenv);
void watchdog_set_alarm(int seconds);
void watchdog_cancel_alarm(void);
const char *get_sc_to_name(const char *name);
extern void *lwm_printer_thd(void *p);
unsigned int sc_get_logical_redo_lwm();
/* Sorry - reaching into berkeley "internals" here. This should
* probably be an environment method. */
extern int __db_find_recovery_start_if_enabled(DB_ENV *dbenv, DB_LSN *lsn);
extern void *master_lease_thread(void *arg);
extern void *coherency_lease_thread(void *arg);
extern int bulk_import_tmpdb_should_ignore_table(const char *table);
extern int bulk_import_tmpdb_should_ignore_btree(const char *filename);
LISTC_T(struct checkpoint_list) ckp_lst;
static pthread_mutex_t ckp_lst_mtx;
int ckp_lst_ready = 0;
extern int gbl_set_seqnum_trace;
void bdb_checkpoint_list_init()
{
listc_init(&ckp_lst, offsetof(struct checkpoint_list, lnk));
Pthread_mutex_init(&ckp_lst_mtx, NULL);
ckp_lst_ready = 1;
}
int bdb_checkpoint_list_push(DB_LSN lsn, DB_LSN ckp_lsn, int32_t timestamp, int push_top)
{
struct checkpoint_list *ckp = NULL;
if (!ckp_lst_ready)
return 0;
ckp = malloc(sizeof(struct checkpoint_list));
if (!ckp)
return ENOMEM;
ckp->lsn = lsn;
ckp->ckp_lsn = ckp_lsn;
ckp->timestamp = timestamp;
Pthread_mutex_lock(&ckp_lst_mtx);
if (push_top) {
listc_atl(&ckp_lst, ckp);
} else {
listc_abl(&ckp_lst, ckp);
}
Pthread_mutex_unlock(&ckp_lst_mtx);
return 0;
}
int bdb_recovery_timestamp_fulfills_log_age_requirement(int32_t recovery_timestamp)
{
const bdb_state_type *bdb_state = thedb->bdb_env;
return (time(NULL) - recovery_timestamp) >= bdb_state->attr->min_keep_logs_age;
}
static int bdb_need_log_to_fulfill_log_age_requirement(int filenum)
{
struct checkpoint_list *ckp = NULL;
if (!ckp_lst_ready)
return 1;
Pthread_mutex_lock(&ckp_lst_mtx);
LISTC_FOR_EACH(&ckp_lst, ckp, lnk)
{
/* find the first checkpoint which references a file that's larger than
* the logfile that we're considering deleting */
if (ckp->ckp_lsn.file > filenum) {
const int ckp_in_newer_logfile_can_be_oldest_ckp = bdb_recovery_timestamp_fulfills_log_age_requirement(ckp->timestamp);
const int need_logfile = !ckp_in_newer_logfile_can_be_oldest_ckp;
Pthread_mutex_unlock(&ckp_lst_mtx);
return need_logfile;
}
}
Pthread_mutex_unlock(&ckp_lst_mtx);
return 1;
}
void bdb_delete_logfile_pglogs(bdb_state_type *bdb_state, int filenum,
int flags);
void bdb_delete_timestamp_lsn(bdb_state_type *bdb_state, int32_t timestamp);
extern pthread_mutex_t bdb_gbl_recoverable_lsn_mutex;
extern DB_LSN bdb_gbl_recoverable_lsn;
extern int32_t bdb_gbl_recoverable_timestamp;
void set_repinfo_master_host(bdb_state_type *bdb_state, char *master,
const char *func, uint32_t line)
{
if (bdb_state->parent)
bdb_state = bdb_state->parent;
if (bdb_state->attr->set_repinfo_master_trace) {
logmsg(LOGMSG_USER, "Setting repinfo master to %s from %s line %u\n",
master, func, line);
}
bdb_state->repinfo->master_host_interned = intern_ptr(master);
bdb_state->repinfo->master_host = bdb_state->repinfo->master_host_interned->str;
if (master == db_eid_invalid) {
/* whoismaster_rtn (new_master_callback) will be called when master is available */
thedb_set_master(db_eid_invalid);
}
}
static void bdb_checkpoint_list_delete_log(int filenum)
{
struct checkpoint_list *ckp = NULL;
if (!ckp_lst_ready)
return;
Pthread_mutex_lock(&ckp_lst_mtx);
while ((ckp = listc_rtl(&ckp_lst)) != NULL) {
if (ckp->ckp_lsn.file <= filenum) {
free(ckp);
} else {
/* push back to the front of the list */
listc_atl(&ckp_lst, ckp);
break;
}
}
/* update bdb_gbl_recoverable_lsn*/
assert(ckp != NULL);
bdb_gbl_recoverable_lsn = ckp->lsn;
bdb_gbl_recoverable_timestamp = ckp->timestamp;
Pthread_mutex_unlock(&ckp_lst_mtx);
}
static void bdb_snapshot_asof_delete_log(bdb_state_type *bdb_state, int filenum,
time_t timestamp)
{
bdb_checkpoint_list_delete_log(filenum);
bdb_delete_logfile_pglogs(bdb_state, filenum, 0);
bdb_delete_timestamp_lsn(bdb_state, timestamp);
}
static void bdb_modsnap_delete_log(bdb_state_type *bdb_state, int filenum,
time_t timestamp)
{
bdb_checkpoint_list_delete_log(filenum);
bdb_delete_timestamp_lsn(bdb_state, timestamp);
}
void bdb_checkpoint_list_get_ckplsn_before_lsn(DB_LSN lsn, DB_LSN *lsnout)
{
struct checkpoint_list *ckp = NULL;
if (!ckp_lst_ready)
return;
Pthread_mutex_lock(&ckp_lst_mtx);
LISTC_FOR_EACH_REVERSE(&ckp_lst, ckp, lnk)
{
if (log_compare(&ckp->lsn, &lsn) <= 0) {
*lsnout = ckp->ckp_lsn;
Pthread_mutex_unlock(&ckp_lst_mtx);
return;
}
}
Pthread_mutex_unlock(&ckp_lst_mtx);
logmsg(LOGMSG_ERROR, "%s: Failed to find checkpoint LSN before %"PRIu32":%"PRIu32".\n",
__func__, lsn.file, lsn.offset);
abort();
}
void bdb_checkpoint_list_get_ckp_before_timestamp(int timestamp, DB_LSN *lsnout)
{
struct checkpoint_list *ckp = NULL;
if (!ckp_lst_ready)
return;
Pthread_mutex_lock(&ckp_lst_mtx);
LISTC_FOR_EACH_REVERSE(&ckp_lst, ckp, lnk)
{
if (ckp->timestamp < timestamp) {
*lsnout = ckp->lsn;
Pthread_mutex_unlock(&ckp_lst_mtx);
return;
}
}
Pthread_mutex_unlock(&ckp_lst_mtx);
}
static void set_some_flags(bdb_state_type *bdb_state, DB *dbp, char *name)
{
if (bdb_state->attr->checksums) {
if (dbp->set_flags(dbp, DB_CHKSUM) != 0) {
logmsg(LOGMSG_ERROR, "error enabling checksums\n");
}
}
if (bdb_state->dbenv->crypto_handle) {
print(bdb_state, "enabling crypto for %s\n", name);
if (dbp->set_flags(dbp, DB_ENCRYPT) != 0) {
logmsg(LOGMSG_ERROR, "error enabling crypto\n");
}
}
if (bdb_state->attr->little_endian_btrees)
dbp->set_lorder(dbp, 1234 /*little endian*/);
else
dbp->set_lorder(dbp, 4321 /*big endian*/);
}
void bdb_set_recovery(bdb_state_type *bdb_state)
{
berkdb_set_recovery(bdb_state->dbenv);
}
/* Given a dtanum (0==main record, 1+==blobs) say if it is striped or not */
static int is_datafile_striped(bdb_state_type *bdb_state, int dtanum)
{
if (bdb_state->bdbtype != BDBTYPE_TABLE) {
return 0;
} else if (bdb_state->attr->dtastripe > 0) {
if (0 == dtanum)
return 1;
else if (bdb_state->attr->blobstripe > 0)
return 1;
else
return 0;
} else {
return 0;
}
}
int bdb_get_datafile_num_files(bdb_state_type *bdb_state, int dtanum)
{
if (is_datafile_striped(bdb_state, dtanum))
return bdb_state->attr->dtastripe;
else
return 1;
}
/* copies the new prefix that is to be used to the provided buffer
* this prefix is used for temporary tables during schema change, the bdb layer
* needs to pick it so that it can be unprepended when a temp table gets
* upgraded to an actual table */
int bdb_get_new_prefix(char *buf, size_t buflen, int *bdberr)
{
if (!buf || !bdberr) {
logmsg(LOGMSG_ERROR, "bdb_get_new_prefix: NULL argument\n");
if (bdberr)
*bdberr = BDBERR_BADARGS;
return -1;
}
if (sizeof(NEW_PREFIX) - 1 >= buflen) {
*bdberr = BDBERR_BUFSMALL;
return -1;
}
strncpy0(buf, NEW_PREFIX, buflen);
*bdberr = BDBERR_NOERROR;
return 0;
}
/* remove the prefix new. from the beginning of a string (usually a tablename).
* if the string didn't start with new. then the origional string is
* returned and bdberr is set to BDBERR_BADARGS
* NOTE: the returned pointer will point to either the begining or some internal
* part of the parameter string */
const char *bdb_unprepend_new_prefix(const char *tablename, int *bdberr)
{
/* if the input didn't start with new. report it by setting bdberr, note
* that this may not be an error */
if (strncmp(tablename, NEW_PREFIX, sizeof(NEW_PREFIX) - 1)) {
*bdberr = BDBERR_BADARGS;
return tablename;
}
*bdberr = BDBERR_NOERROR;
/* we want to remove the new. prefix from the tablename */
return tablename + sizeof(NEW_PREFIX) - 1;
}
/* this removes a new.SOMETHING. prefix from a tables name (if it exists)
* no files are renamed */
void bdb_remove_prefix(bdb_state_type *bdb_state)
{
if (bdb_state->origname) {
free(bdb_state->name);
bdb_state->name = strdup(bdb_state->origname);
return;
}
int bdberr;
const char *new_name = bdb_unprepend_new_prefix(bdb_state->name, &bdberr);
if (bdberr == BDBERR_NOERROR) {
char *old_name = bdb_state->name;
bdb_state->name = strdup(new_name);
free(old_name); /* must do this after new name is duped bc new name
* points inside old_name */
}
}
struct bdb_file_version_num_type {
unsigned long long version_num;
};
enum { BDB_FILE_VERSION_NUM_TYPE_LEN = 8 };
BB_COMPILE_TIME_ASSERT(bdb_file_version_num_type,
sizeof(struct bdb_file_version_num_type) ==
BDB_FILE_VERSION_NUM_TYPE_LEN);
static uint8_t *bdb_file_version_num_put(
const struct bdb_file_version_num_type *p_file_version_num_type,
uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf ||
BDB_FILE_VERSION_NUM_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf =
buf_put(&(p_file_version_num_type->version_num),
sizeof(p_file_version_num_type->version_num), p_buf, p_buf_end);
return p_buf;
}
static const uint8_t *bdb_file_version_num_get(
struct bdb_file_version_num_type *p_file_version_num_type,
const uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf ||
BDB_FILE_VERSION_NUM_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf =
buf_get(&(p_file_version_num_type->version_num),
sizeof(p_file_version_num_type->version_num), p_buf, p_buf_end);
return p_buf;
}
const char *bdb_get_tmpdir(bdb_state_type *bdb_state)
{
if (bdb_state->parent)
bdb_state = bdb_state->parent;
return bdb_state->tmpdir;
}
void bdb_replace_cached_data_version(bdb_state_type *target, bdb_state_type *new)
{
target->dtavers[0] = new->dtavers[0];
}
void bdb_replace_cached_blob_version(bdb_state_type *target, int targetnum, bdb_state_type *new, int newnum)
{
target->dtavers[targetnum + 1] = new->dtavers[newnum + 1];
}
void bdb_replace_cached_index_version(bdb_state_type *target, int targetnum, bdb_state_type *new, int newnum)
{
target->ixvers[targetnum] = new->ixvers[newnum];
}
/* takes information about a file and constructs its filename, returns bytes
* written not including NUL */
static int form_file_name_ex(
bdb_state_type *bdb_state, /* db to open */
int is_data_file, /* see FILE_VERSIONS_FILE_TYPE_* */
int file_num, /* ixnum or dtanum */
int add_prefix, /* whether to add XXX. or directory to
* the front of the filename */
int isstriped, /* whether this dta is striped (ignored
* for index files*/
int stripenum, /* only used if isstriped is true
* (ignored for index files*/
unsigned long long version_num, /* if == 0 old style names are used
* otherwise new style */
char *outbuf, /* filename is returned here */
size_t buflen)
{
int offset, bdberr;
/*we want to remove the new.SOMETHING. prefix if it's there*/
const char *tablename;
if (bdb_state->origname) {
tablename = bdb_state->origname;
} else {
tablename = bdb_unprepend_new_prefix(bdb_state->name, &bdberr);
}
size_t orig_buflen = buflen;
/* start building the file name */
if (add_prefix) {
offset = snprintf(outbuf, buflen, "XXX.");
outbuf += offset;
buflen -= offset;
}
offset = snprintf(outbuf, buflen, "%s", tablename);
outbuf += offset;
buflen -= offset;
/*append the version number for this table db*/
if (version_num) {
unsigned long long pr_vers;
uint8_t *p_buf, *p_buf_end;
struct bdb_file_version_num_type p_file_version_num_type;
char *file_ext = "index";
if (is_data_file) {
if (file_num)
file_ext = "blob";
else
file_ext = "data";
}
p_file_version_num_type.version_num = version_num;
p_buf = (uint8_t *)&pr_vers;
p_buf_end = p_buf + sizeof(pr_vers);
bdb_file_version_num_put(&(p_file_version_num_type), p_buf, p_buf_end);
offset = snprintf(outbuf, buflen, "_%016llx.%s", pr_vers, file_ext);
outbuf += offset;
buflen -= offset;
} else /*if we have no version number*/
{
char *file_ext = "ix";
if (is_data_file)
file_ext = "dta";
/*add the tablename and the suffix*/
offset = snprintf(outbuf, buflen, ".%s", file_ext);
outbuf += offset;
buflen -= offset;
/* if this is an index or a blob blob, add its number */
if (file_num > 0 || !is_data_file) {
offset = snprintf(outbuf, buflen, "%d", file_num);
outbuf += offset;
buflen -= offset;
}
}
/*if datafile and striped add the stripe number*/
if (is_data_file && isstriped) {
offset = snprintf(outbuf, buflen, "s%d", stripenum);
outbuf += offset;
buflen -= offset;
}
if (is_data_file)
bdb_state->dtavers[file_num] = version_num;
else
bdb_state->ixvers[file_num] = version_num;
return orig_buflen - buflen;
}
int bdb_form_file_name(bdb_state_type *bdb_state, int is_data_file, int filenum,
int stripenum, unsigned long long version_num,
char *outbuf, size_t buflen)
{
int isstriped = 0;
if (is_data_file) {
if (filenum == 0) {
/* data */
if (bdb_state->bdbtype == BDBTYPE_TABLE &&
bdb_state->attr->dtastripe > 0)
isstriped = 1;
} else {
/* blob */
if (bdb_state->attr->blobstripe > 0)
isstriped = 1;
}
}
return form_file_name_ex(bdb_state, is_data_file, filenum, 0, isstriped,
stripenum, version_num, outbuf, buflen);
}
/*figures out what the version number is (if any) and calls form_file_name_ex*/
static int form_file_name(bdb_state_type *bdb_state, DB_TXN *tid,
int is_data_file, int file_num, int isstriped,
int stripenum, char *outbuf, size_t buflen)
{
unsigned long long version_num;
int rc, bdberr;
tran_type tran = {0};
tran.tid = tid;
if (is_data_file)
rc = bdb_get_file_version_data(bdb_state, &tran, file_num, &version_num,
&bdberr);
else
rc = bdb_get_file_version_index(bdb_state, &tran, file_num,
&version_num, &bdberr);
if (rc || bdberr != BDBERR_NOERROR) {
if (bdberr == BDBERR_DBEMPTY) {
/*fprintf( stderr, "Not using version numbers (or db is empty) for
* "*/
/*"table: %s with type: %d, and num: %d, defaulting to old "*/
/*"name scheme\n", tablename, is_data_file, file_num );*/
} else if (bdberr == BDBERR_FETCH_DTA) {
/*fprintf( stderr, "No version number found for table: %s with type:
* "*/
/*"%s, and num: %d, defaulting to old name scheme\n",*/
/*bdb_state->name, (is_data_file)?"data":"idx", file_num );*/
} else
logmsg(LOGMSG_ERROR,
"Version number lookup failed with bdberr: %d for "
"table: %s with type: %s, and num: %d, defaulting to old "
"name scheme\n",
bdberr, bdb_state->name, (is_data_file) ? "data" : "idx",
file_num);
}
return form_file_name_ex(bdb_state, is_data_file, file_num,
1 /*add_prefix*/, isstriped, stripenum,
version_num, outbuf, buflen);
}
/* calls form_file_name for a datafile */
static int form_datafile_name(bdb_state_type *bdb_state, DB_TXN *tid,
int dtanum, int stripenum, char *outbuf,
size_t buflen)
{
/*find out whether this db is striped*/
int isstriped = 0;
if (dtanum > 0) {
if (bdb_state->attr->blobstripe > 0)
isstriped = 1;
} else if (bdb_state->bdbtype == BDBTYPE_TABLE &&
bdb_state->attr->dtastripe > 0)
isstriped = 1;
return form_file_name(bdb_state, tid, 1 /*is_data_file*/, dtanum, isstriped,
stripenum, outbuf, buflen);
}
/* calls form_file_name for an indexfile */
static int form_indexfile_name(bdb_state_type *bdb_state, DB_TXN *tid,
int ixnum, char *outbuf, size_t buflen)
{
return form_file_name(bdb_state, tid, 0 /*is_data_file*/, ixnum,
0 /*isstriped*/, 0 /*stripenum*/, outbuf, buflen);
}
static int should_stop_looking_for_queuedb_files(bdb_state_type *bdb_state,
tran_type *tran, int file_num,
unsigned long long *file_version)
{
unsigned long long local_file_version = 0;
int bdberr = 0;
if (bdb_get_file_version_qdb(bdb_state, tran, file_num,
&local_file_version, &bdberr) != 0) {
/*
** NOTE: For queuedb, all files after the first one are optional
** and may not actually exist.
*/
if (file_version != NULL) *file_version = 0;
if (file_num > 0) {
logmsg(LOGMSG_DEBUG,
"%s: queuedb %s file %d version not found, stopping...\n",
__func__, bdb_state->name, file_num);
return 1;
}
}
if (file_version != NULL) *file_version = local_file_version;
bdb_state->qvers[file_num] = local_file_version;
return 0;
}
static void form_queuedb_name_int(bdb_state_type *bdb_state, char *name,
size_t len, unsigned long long file_version)
{
if (file_version != 0) {
/* Flip to form name on big-endian */
#if defined(_SUN_SOURCE)
file_version = flibc_llflip(file_version);
#endif
snprintf0(name, len, "XXX.%s_%016llx.queuedb", bdb_state->name,
file_version);
} else {
snprintf0(name, len, "XXX.%s.queuedb", bdb_state->name);
}
}
static int form_queuedb_name(bdb_state_type *bdb_state, tran_type *tran,
int file_num, int create, char *name, size_t len)
{
unsigned long long ver;
int rc, bdberr;
if (create && USE_GENID_IN_QUEUEDB_FILE_NAME()) {
/* Always flip before writing to llmeta */
ver = flibc_llflip(bdb_get_cmp_context(bdb_state));
rc = bdb_new_file_version_qdb(bdb_state, tran, file_num, ver, &bdberr);
if (rc || bdberr != BDBERR_NOERROR) {
return -1;
}
}
/* NOTE: This point is reached even if we (just) successfully
** set the file version above. */
if (bdb_get_file_version_qdb(bdb_state, tran, file_num, &ver,
&bdberr) == 0) {
/* success, do nothing yet. */
bdb_state->qvers[file_num] = ver;
} else {
/* no version -AND- do fallback to versionless */
ver = 0;
}
form_queuedb_name_int(bdb_state, name, len, ver);
return 0;
}
int bdb_bulk_import_copy_cmd_add_tmpdir_filenames(
bdb_state_type *bdb_state, unsigned long long src_data_genid,
unsigned long long dst_data_genid,
const unsigned long long *p_src_index_genids,
const unsigned long long *p_dst_index_genids, size_t num_index_genids,
const unsigned long long *p_src_blob_genids,
const unsigned long long *p_dst_blob_genids, size_t num_blob_genids,
char *outbuf, size_t buflen, int *bdberr)
{
int dtanum;
int ixnum;
int len;
int offset = 0;
if (!bdb_state || !p_src_index_genids || !p_dst_index_genids ||
!p_src_blob_genids || !p_dst_blob_genids || !outbuf || !bdberr ||
bdb_state->numdtafiles - 1 /*1st data file isn't blob*/
!= num_blob_genids ||
bdb_state->numix != num_index_genids) {
logmsg(LOGMSG_ERROR, "%s: null or invalid argument\n", __func__);
if (bdberr)
*bdberr = BDBERR_BADARGS;
return -1;
}
/* add -dsttmpdir */
len = snprintf(outbuf + offset, buflen - offset, " -dsttmpdir %s",
bdb_state->parent->tmpdir);
offset += len;
if (len < 0 || offset >= buflen) {
logmsg(LOGMSG_ERROR,
"%s: adding -dsttmpdir arg failed or string was too "
"long for buffer this string len: %d total string len: %d\n",
__func__, len, offset);
*bdberr = BDBERR_BUFSMALL;
return -1;
}
/* add data files */
for (dtanum = 0; dtanum < bdb_state->numdtafiles; dtanum++) {
int strnum;
int num_stripes = bdb_get_datafile_num_files(bdb_state, dtanum);
int isstriped = 0;
unsigned long long src_version_num;
unsigned long long dst_version_num;
/*find out if this datafile is striped and get the right version num*/
if (dtanum > 0) {
src_version_num = p_src_blob_genids[dtanum - 1];
dst_version_num = p_dst_blob_genids[dtanum - 1];
if (bdb_state->attr->blobstripe > 0)
isstriped = 1;
} else {
src_version_num = src_data_genid;
dst_version_num = dst_data_genid;
if (bdb_state->bdbtype == BDBTYPE_TABLE &&
bdb_state->attr->dtastripe > 0)
isstriped = 1;
}
/* for each stripe add a -file param */
for (strnum = 0; strnum < num_stripes; ++strnum) {
/* add -file */
len = snprintf(outbuf + offset, buflen - offset, " -file ");
offset += len;
if (len < 0 || offset >= buflen) {
logmsg(LOGMSG_ERROR,
"%s: adding data -file arg failed or string was"
" too long for buffer this string len: %d total string "
"len: %d\n",
__func__, len, offset);
*bdberr = BDBERR_BUFSMALL;
return -1;
}
/* add src filename */
len = form_file_name_ex(bdb_state, 1 /*is_data_file*/, dtanum,
0 /*add_prefix*/, isstriped, strnum,
src_version_num, outbuf + offset,
buflen - offset);
offset += len + 1 /*include the space we're about to add*/;
if (len < 0 || offset >= buflen) {
logmsg(LOGMSG_ERROR,
"%s: adding src data filename failed or string "
"was too long for buffer this string len: %d total "
"string len: %d\n",
__func__, len, offset);
*bdberr = BDBERR_BUFSMALL;
return -1;
}
/* add a space. this removes the NUL, it will be re added by the
* form_file_name_ex() below */
outbuf[offset - 1] = ' ';
/* add dst filename */
len = form_file_name_ex(bdb_state, 1 /*is_data_file*/, dtanum,
0 /*add_prefix*/, isstriped, strnum,
dst_version_num, outbuf + offset,
buflen - offset);
offset += len;
if (len < 0 || offset >= buflen) {
logmsg(LOGMSG_ERROR,
"%s: adding dst data filename failed or string "
"was too long for buffer this string len: %d total "
"string len: %d\n",
__func__, len, offset);
*bdberr = BDBERR_BUFSMALL;
return -1;
}
}
}
/* for each index add a -file param */
for (ixnum = 0; ixnum < bdb_state->numix; ixnum++) {
/* add -file */
len = snprintf(outbuf + offset, buflen - offset, " -file ");
offset += len;
if (len < 0 || offset >= buflen) {
logmsg(LOGMSG_ERROR,
"%s: adding index -file arg failed or string was "
"too long for buffer this string len: %d total string len: "
"%d\n",
__func__, len, offset);
*bdberr = BDBERR_BUFSMALL;
return -1;
}
/* add src filename */
len = form_file_name_ex(bdb_state, 0 /*is_data_file*/, ixnum,
0 /*add_prefix*/, 0 /*isstriped*/, 0 /*strnum*/,
p_src_index_genids[ixnum], outbuf + offset,
buflen - offset);
offset += len + 1 /*include the space we're about to add*/;
if (len < 0 || offset >= buflen) {
logmsg(LOGMSG_ERROR, "%s: adding src index filename failed or string "
"was too long for buffer this string len: %d total string "
"len: %d\n",
__func__, len, offset);
*bdberr = BDBERR_BUFSMALL;
return -1;
}
/* add a space. this removes the NUL, it will be re added by the
* form_file_name_ex() below */
outbuf[offset - 1] = ' ';
/* add dst filename */
len = form_file_name_ex(bdb_state, 0 /*is_data_file*/, ixnum,
0 /*add_prefix*/, 0 /*isstriped*/, 0 /*strnum*/,
p_dst_index_genids[ixnum], outbuf + offset,
buflen - offset);
offset += len;
if (len < 0 || offset >= buflen) {
logmsg(LOGMSG_ERROR, "%s: adding dst index filename failed or string "
"was too long for buffer this string len: %d total string "
"len: %d\n",
__func__, len, offset);
*bdberr = BDBERR_BUFSMALL;
return -1;
}
}
*bdberr = BDBERR_NOERROR;
return 0;
}
/*moves an entire table over to the new versioning database*/
int bdb_rename_file_versioning_table(bdb_state_type *bdb_state,
tran_type *input_trans, char *newtblname,
int *bdberr)
{
int dtanum, ixnum, retries = 0;
unsigned long long version_num;
unsigned long long new_version_num;
char oldname[80], newname[80];
tran_type *tran;
char *orig_name;
orig_name = bdb_state->name;
retry:
if (++retries >= gbl_maxretries) {
logmsg(LOGMSG_ERROR, "%s: giving up after %d retries\n", __func__,
retries);
return -1;
}
/*if the user didn't give us a transaction, create our own*/
if (!input_trans) {
tran = bdb_tran_begin(bdb_state, NULL, bdberr);
if (!tran) {
if (*bdberr == BDBERR_DEADLOCK)
goto retry;
logmsg(LOGMSG_ERROR, "%s: failed to get transaction\n", __func__);
return -1;
}
} else
tran = input_trans;
/* set table version num, this is used to test if the table is using file
* versions */
bdb_state->name = newtblname;
new_version_num = bdb_get_cmp_context(bdb_state);
if (bdb_new_file_version_table(bdb_state, tran, new_version_num, bdberr) ||
*bdberr != BDBERR_NOERROR)
goto backout;
/* get existing name version */
bdb_state->name = orig_name;
if (bdb_get_file_version_table(bdb_state, tran, &version_num, bdberr) &&
*bdberr != BDBERR_NOERROR) {
logmsg(LOGMSG_ERROR, "failed to retrieve existing table version\n");
*bdberr = BDBERR_MISC;
goto backout;
}