-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
pag.cpp
2529 lines (2139 loc) · 69.7 KB
/
pag.cpp
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
/*
* PROGRAM: JRD Access Method
* MODULE: pag.cpp
* DESCRIPTION: Page level ods manager
*
* The contents of this file are subject to the Interbase Public
* License Version 1.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.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete ports:
* - EPSON, DELTA, IMP, NCR3000, M88K
* - HP9000 s300 and Apollo
*
* 2002.10.27 Sean Leyne - Completed removal of obsolete "DG_X86" port
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "UNIXWARE" port
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "Ultrix" port
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "Ultrix/MIPS" port
*
* 2002.10.28 Sean Leyne - Completed removal of obsolete "DGUX" port
* 2002.10.28 Sean Leyne - Code cleanup, removed obsolete "MPEXL" port
* 2002.10.28 Sean Leyne - Code cleanup, removed obsolete "DecOSF" port
* 2002.10.28 Sean Leyne - Code cleanup, removed obsolete "SGI" port
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
*/
/*
* Modified by: Patrick J. P. Griffin
* Date: 11/29/2000
* Problem: Bug 116733 Too many generators corrupt database.
* DPM_gen_id was not calculating page and offset correctly.
* Change: Caculate pgc_gpg, number of generators per page,
* for use in DPM_gen_id.
*/
/* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
*
* 2001.08.07 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, second attempt
*
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "MAC" and "MAC_CP" defines
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "Apollo" port
*
*/
#include "firebird.h"
#include "../jrd/common.h"
#include <stdio.h>
#include <string.h>
#ifdef WIN_NT
#include <process.h>
#endif
#include "../common/config/config.h"
#include "../common/utils_proto.h"
#include "../jrd/fil.h"
#include "../jrd/jrd.h"
#include "../jrd/pag.h"
#include "../jrd/ods.h"
#include "../jrd/os/pio.h"
#include "../jrd/os/path_utils.h"
#include "../jrd/ibase.h"
#include "../jrd/iberr.h"
#include "../jrd/gdsassert.h"
#include "../jrd/lck.h"
#include "../jrd/sdw.h"
#include "../jrd/cch.h"
#include "../jrd/tra.h"
#ifdef VIO_DEBUG
#include "../jrd/vio_debug.h"
#endif
#include "../jrd/cch_proto.h"
#include "../jrd/dpm_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/met_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/ods_proto.h"
#include "../jrd/pag_proto.h"
#include "../jrd/os/pio_proto.h"
#include "../jrd/thread_proto.h"
#include "../jrd/isc_f_proto.h"
#include "../jrd/TempSpace.h"
using namespace Jrd;
using namespace Ods;
static int blocking_ast_attachment(void*);
static void find_clump_space(SLONG, WIN*, pag**, USHORT, USHORT, const UCHAR*,
USHORT);
static bool find_type(SLONG, WIN*, pag**, USHORT, USHORT, UCHAR**,
const UCHAR**);
inline void err_post_if_database_is_readonly(const Database* dbb)
{
if (dbb->dbb_flags & DBB_read_only)
ERR_post(isc_read_only_database, 0);
}
// Class definitions (obsolete platforms are commented out)
// Class constant name consists of OS platform and CPU architecture.
//
// For ports created before Firebird 2.0 release 64-bit and 32-bit
// sub-architectures of the same CPU should use different classes.
// For 64-bit ports first created after or as a part of Firebird 2.0
// release CPU architecture may be the same for both variants.
static const int CLASS_UNKNOWN = 0;
//static const CLASS_APOLLO_68K = 1; // Apollo 68K, Dn 10K
static const int CLASS_SOLARIS_SPARC = 2; // Sun 68k, Sun Sparc, HP 9000/300, MAC AUX, IMP, DELTA, NeXT, UNIXWARE, DG_X86
static const int CLASS_SOLARIS_I386 = 3; // Sun 386i
//static const CLASS_VMS_VAX = 4; // VMS/VAX
//static const CLASS_ULTRIX_VAX = 5; // Ultrix/VAX
//static const CLASS_ULTRIX_MIPS = 6; // Ultrix/MIPS
static const int CLASS_HPUX_PA = 7; // HP-UX on PA-RISC (was: HP 900/800 (precision))
static const int CLASS_NETWARE_I386 = 8; // NetWare
//static const CLASS_MAC_OS = 9; // MAC-OS
static const int CLASS_AIX_PPC = 10; // AIX on PowerPC platform (was: IBM RS/6000)
//static const CLASS_DG_AVIION = 11; // DG AViiON
//static const CLASS_MPE_XL = 12; // MPE/XL
static const int CLASS_IRIX_MIPS = 13; // Silicon Graphics/IRIS
//static const int CLASS_CRAY = 14; // Cray
static const int CLASS_TRU64_ALPHA = 15; // Tru64 Unix running on Alpha (was: Dec OSF/1)
static const int CLASS_WINDOWS_I386 = 16; // NT -- post 4.0 (delivered 4.0 as class 8)
//static const CLASS_OS2 = 17; // OS/2
//static const CLASS_WIN16 = 18; // Windows 16 bit
static const int CLASS_LINUX_I386 = 19; // LINUX on Intel series
static const int CLASS_LINUX_SPARC = 20; // LINUX on sparc systems
static const int CLASS_FREEBSD_I386 = 21; // FreeBSD/i386
static const int CLASS_NETBSD_I386 = 22; // NetBSD/i386
static const int CLASS_DARWIN_PPC = 23; // Darwin/PowerPC
static const int CLASS_LINUX_AMD64 = 24; // LINUX on AMD64 systems
static const int CLASS_FREEBSD_AMD64 = 25;// FreeBSD/amd64
static const int CLASS_WINDOWS_AMD64 = 26;// Windows/amd64
static const int CLASS_LINUX_PPC = 27; // LINUX/PowerPC
static const int CLASS_DARWIN_I386 = 28; //Darwin/Intel
static const int CLASS_LINUX_MIPSEL = 29; // LINUX/MIPSEL
static const int CLASS_LINUX_MIPS = 30; // LINUX/MIPS
static const int CLASS_DARWIN_X64 = 31; // Darwin/x64
static const int CLASS_SOLARIS_AMD64 = 32; // Solaris/amd64
static const int CLASS_LINUX_ARM = 33; // LINUX/ARM
static const int CLASS_MAX10 = CLASS_LINUX_AMD64; // This should not be changed, no new ports with ODS10
static const int CLASS_MAX = CLASS_LINUX_ARM;
// ARCHITECTURE COMPATIBILITY CLASSES
// For ODS10 and earlier things which normally define ODS compatibility are:
// 1) endianness (big-endian/little-endian)
// 2) alignment (32-bit or 64-bit), matters for record formats
// 3) pointer size (32-bit or 64-bit), also matters for record formats
//
// For ODS11 pointers are not stored in database and alignment is always 64-bit.
// So the only thing which normally matters for ODS11 is endiannes, but if
// endianness is wrong we are going to notice it during ODS version check,
// before architecture compatibility is tested. But we distinguish them here too,
// for consistency.
enum ArchitectureType {
archUnknown, // Unknown architecture, allow opening database only if CLASS matches exactly
archIntel86, // Little-endian platform with 32-bit pointers and 32-bit alignment (ODS10)
archLittleEndian, // Any little-endian platform with standard layout of data
archBigEndian // Any big-endian platform with standard layout of data
};
// Note that Sparc, HP and PowerPC disk structures should be compatible in theory,
// but in practice alignment on these platforms varies and actually depends on the
// compiler used to produce the build. Yes, some 32-bit RISC builds use 64-bit alignment.
// This is why we declare all such builds "Unknown" for ODS10.
static const ArchitectureType archMatrix10[CLASS_MAX10 + 1] =
{
archUnknown, // CLASS_UNKNOWN
archUnknown, // CLASS_APOLLO_68K
archUnknown, // CLASS_SOLARIS_SPARC
archIntel86, // CLASS_SOLARIS_I386
archUnknown, // CLASS_VMS_VAX
archUnknown, // CLASS_ULTRIX_VAX
archUnknown, // CLASS_ULTRIX_MIPS
archUnknown, // CLASS_HPUX_PA
archUnknown, // CLASS_NETWARE_I386
archUnknown, // CLASS_MAC_OS
archUnknown, // CLASS_AIX_PPC
archUnknown, // CLASS_DG_AVIION
archUnknown, // CLASS_MPE_XL
archUnknown, // CLASS_IRIX_MIPS
archUnknown, // CLASS_CRAY
archUnknown, // CLASS_TRU64_ALPHA
archIntel86, // CLASS_WINDOWS_I386
archUnknown, // CLASS_OS2
archUnknown, // CLASS_WIN16
archIntel86, // CLASS_LINUX_I386
archUnknown, // CLASS_LINUX_SPARC
archIntel86, // CLASS_FREEBSD_I386
archIntel86, // CLASS_NETBSD_I386
archUnknown, // CLASS_DARWIN_PPC
archUnknown // CLASS_LINUX_AMD64
};
static const ArchitectureType archMatrix[CLASS_MAX + 1] =
{
archUnknown, // CLASS_UNKNOWN
archUnknown, // CLASS_APOLLO_68K
archBigEndian, // CLASS_SOLARIS_SPARC
archLittleEndian, // CLASS_SOLARIS_I386
archUnknown, // CLASS_VMS_VAX
archUnknown, // CLASS_ULTRIX_VAX
archUnknown, // CLASS_ULTRIX_MIPS
archBigEndian, // CLASS_HPUX_PA
archUnknown, // CLASS_NETWARE_I386
archUnknown, // CLASS_MAC_OS
archBigEndian, // CLASS_AIX_PPC
archUnknown, // CLASS_DG_AVIION
archUnknown, // CLASS_MPE_XL
archBigEndian, // CLASS_IRIX_MIPS
archUnknown, // CLASS_CRAY
archBigEndian, // CLASS_TRU64_ALPHA
archLittleEndian, // CLASS_WINDOWS_I386
archUnknown, // CLASS_OS2
archUnknown, // CLASS_WIN16
archLittleEndian, // CLASS_LINUX_I386
archBigEndian, // CLASS_LINUX_SPARC
archLittleEndian, // CLASS_FREEBSD_I386
archLittleEndian, // CLASS_NETBSD_I386
archBigEndian, // CLASS_DARWIN_PPC
archLittleEndian, // CLASS_LINUX_AMD64
archLittleEndian, // CLASS_FREEBSD_AMD64
archLittleEndian, // CLASS_WINDOWS_AMD64
archBigEndian, // CLASS_LINUX_PPC
archLittleEndian, // CLASS_DARWIN_I386
archLittleEndian, // CLASS_LINUX_MIPSEL
archBigEndian, // CLASS_LINUX_MIPS
archLittleEndian, // CLASS_DARWIN_X64
archLittleEndian, // CLASS_SOLARIS_AMD64
archLittleEndian // CLASS_LINUX_ARM
};
#ifdef sun
#ifdef i386
const SSHORT CLASS = CLASS_SOLARIS_I386;
#else
const SSHORT CLASS = CLASS_SOLARIS_SPARC;
#endif
#endif
#ifdef HPUX
const SSHORT CLASS = CLASS_HPUX_PA;
#endif
#ifdef AIX
const SSHORT CLASS = CLASS_AIX_PPC;
#endif
#ifdef AIX_PPC
const SSHORT CLASS = CLASS_AIX_PPC;
#endif
#ifdef WIN_NT
#if defined(I386)
const SSHORT CLASS = CLASS_WINDOWS_I386;
#elif defined(AMD64)
const SSHORT CLASS = CLASS_WINDOWS_AMD64;
#else
#error no support on other hardware for Windows
#endif
#endif // WIN_NT
#ifdef LINUX
#if defined(i386) || defined(i586)
const SSHORT CLASS = CLASS_LINUX_I386;
#elif defined(sparc)
const SSHORT CLASS = CLASS_LINUX_SPARC;
#elif defined(AMD64)
const SSHORT CLASS = CLASS_LINUX_AMD64;
#elif defined(ARM)
const SSHORT CLASS = CLASS_LINUX_ARM;
#elif defined(PPC)
const SSHORT CLASS = CLASS_LINUX_PPC;
#elif defined(MIPSEL)
const SSHORT CLASS = CLASS_LINUX_MIPSEL;
#elif defined(MIPS)
const SSHORT CLASS = CLASS_LINUX_MIPS;
#else
#error no support on other hardware for Linux
#endif
#endif // LINUX
#ifdef FREEBSD
#if defined(i386)
const SSHORT CLASS = CLASS_FREEBSD_I386;
#elif defined(AMD64)
const SSHORT CLASS = CLASS_FREEBSD_AMD64;
#else
#error no support on other hardware for FreeBSD
#endif
#endif
#ifdef NETBSD
const SSHORT CLASS = CLASS_NETBSD_I386;
#endif
#ifdef DARWIN
#if defined(i386)
const SSHORT CLASS = CLASS_DARWIN_I386;
#elif defined(DARWIN64)
const SSHORT CLASS = CLASS_DARWIN_X64;
#elif defined(powerpc)
const SSHORT CLASS = CLASS_DARWIN_PPC;
#endif
#endif // DARWIN
static const char* const SCRATCH = "fb_table_";
static const int MIN_EXTEND_BYTES = 128 * 1024; // 128KB
// CVC: Since nobody checks the result from this function (strange!), I changed
// bool to void as the return type but left the result returned as comment.
void PAG_add_clump(
SLONG page_num,
USHORT type,
USHORT len, const UCHAR* entry, USHORT mode, USHORT must_write)
{
/***********************************************
*
* P A G _ a d d _ c l u m p
*
***********************************************
*
* Functional description
* Adds a clump to log/header page.
* mode
* 0 - add CLUMP_ADD
* 1 - replace CLUMP_REPLACE
* 2 - replace only! CLUMP_REPLACE_ONLY
* returns
* true - modified page
* false - nothing done => nobody checks this function's result.
*
**************************************/
thread_db* tdbb = JRD_get_thread_data();
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
err_post_if_database_is_readonly(dbb);
pag* page;
header_page* header = 0;
log_info_page* logp = 0;
USHORT* end_addr;
WIN window(DB_PAGE_SPACE, page_num);
if (page_num == HEADER_PAGE) {
page = CCH_FETCH(tdbb, &window, LCK_write, pag_header);
header = (header_page*) page;
end_addr = &header->hdr_end;
}
else {
page = CCH_FETCH(tdbb, &window, LCK_write, pag_log);
logp = (log_info_page*) page;
end_addr = &logp->log_end;
}
UCHAR* entry_p;
const UCHAR* clump_end;
while (mode != CLUMP_ADD) {
const bool found =
find_type(page_num, &window, &page, LCK_write, type,
&entry_p, &clump_end);
/* If we did'nt find it and it is REPLACE_ONLY, return */
if ((!found) && (mode == CLUMP_REPLACE_ONLY)) {
CCH_RELEASE(tdbb, &window);
return; //false;
}
/* If not found, just go and add the entry */
if (!found)
break;
/* if same size, overwrite it */
if (entry_p[1] == len) {
entry_p += 2;
if (len)
{
if (must_write)
CCH_MARK_MUST_WRITE(tdbb, &window);
else
CCH_MARK(tdbb, &window);
memcpy(entry_p, entry, len);
}
CCH_RELEASE(tdbb, &window);
return; // true;
}
/* delete the entry
* Page is marked must write because of precedence problems. Later
* on we may allocate a new page and set up a precedence relationship.
* This may be the lower precedence page and so it cannot be dirty
*/
CCH_MARK_MUST_WRITE(tdbb, &window);
*end_addr -= (2 + entry_p[1]);
const UCHAR* r = entry_p + 2 + entry_p[1];
USHORT l = clump_end - r + 1;
if (l)
memmove(entry_p, r, l);
CCH_RELEASE(tdbb, &window);
/* refetch the page */
window.win_page = page_num;
if (page_num == HEADER_PAGE) {
page = CCH_FETCH(tdbb, &window, LCK_write, pag_header);
header = (header_page*) page;
end_addr = &header->hdr_end;
}
else {
page = CCH_FETCH(tdbb, &window, LCK_write, pag_log);
logp = (log_info_page*) page;
end_addr = &logp->log_end;
}
break;
}
/* Add the entry */
find_clump_space(page_num, &window, &page, type, len, entry, must_write);
CCH_RELEASE(tdbb, &window);
return; // true;
}
USHORT PAG_add_file(const TEXT* file_name, SLONG start)
{
/**************************************
*
* P A G _ a d d _ f i l e
*
**************************************
*
* Functional description
* Add a file to the current database. Return the sequence
* number for the new file.
*
**************************************/
thread_db* tdbb = JRD_get_thread_data();
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
err_post_if_database_is_readonly(dbb);
/* Find current last file */
PageSpace* pageSpace = dbb->dbb_page_manager.findPageSpace(DB_PAGE_SPACE);
jrd_file* file = pageSpace->file;
while (file->fil_next) {
file = file->fil_next;
}
// Verify database file path against DatabaseAccess entry of firebird.conf
if (!JRD_verify_database_access(file_name)) {
ERR_post(isc_conf_access_denied,
isc_arg_string, "additional database file",
isc_arg_string, ERR_cstring(file_name),
isc_arg_end);
}
/* Create the file. If the sequence number comes back zero, it didn't
work, so punt */
const USHORT sequence = PIO_add_file(dbb, pageSpace->file, file_name, start);
if (!sequence)
return 0;
/* Create header page for new file */
jrd_file* next = file->fil_next;
if (dbb->dbb_flags & (DBB_force_write | DBB_no_fs_cache))
{
PIO_force_write(next,
dbb->dbb_flags & DBB_force_write,
dbb->dbb_flags & DBB_no_fs_cache);
}
WIN window(DB_PAGE_SPACE, next->fil_min_page);
header_page* header = (header_page*) CCH_fake(tdbb, &window, 1);
header->hdr_header.pag_type = pag_header;
header->hdr_sequence = sequence;
header->hdr_page_size = dbb->dbb_page_size;
header->hdr_data[0] = HDR_end;
header->hdr_end = HDR_SIZE;
next->fil_sequence = sequence;
#ifdef SUPPORT_RAW_DEVICES
/* The following lines (taken from PAG_format_header) are needed to identify
this file in raw_devices_validate_database as a valid database attachment. */
*(ISC_TIMESTAMP*) header->hdr_creation_date = Firebird::TimeStamp::getCurrentTimeStamp().value();
// should we include milliseconds or not?
//Firebird::TimeStamp::round_time(header->hdr_creation_date->timestamp_time, 0);
header->hdr_ods_version = ODS_VERSION | ODS_FIREBIRD_FLAG;
header->hdr_implementation = CLASS;
header->hdr_ods_minor = ODS_CURRENT;
header->hdr_ods_minor_original = ODS_CURRENT;
if (dbb->dbb_flags & DBB_DB_SQL_dialect_3)
header->hdr_flags |= hdr_SQL_dialect_3;
#endif
header->hdr_header.pag_checksum = CCH_checksum(window.win_bdb);
PIO_write(pageSpace->file, window.win_bdb, window.win_buffer,
tdbb->tdbb_status_vector);
CCH_RELEASE(tdbb, &window);
next->fil_fudge = 1;
/* Update the previous header page to point to new file */
file->fil_fudge = 0;
window.win_page = file->fil_min_page;
header = (header_page*) CCH_FETCH(tdbb, &window, LCK_write, pag_header);
if (!file->fil_min_page)
CCH_MARK_MUST_WRITE(tdbb, &window);
else
CCH_MARK(tdbb, &window);
--start;
if (file->fil_min_page) {
PAG_add_header_entry(header, HDR_file, strlen(file_name),
reinterpret_cast<const UCHAR*>(file_name));
PAG_add_header_entry(header, HDR_last_page, sizeof(SLONG),
(UCHAR *) & start);
}
else {
PAG_add_clump(HEADER_PAGE, HDR_file, strlen(file_name),
reinterpret_cast<const UCHAR*>(file_name), CLUMP_REPLACE, 1);
PAG_add_clump(HEADER_PAGE, HDR_last_page, sizeof(SLONG),
(UCHAR *) & start, CLUMP_REPLACE, 1);
}
header->hdr_header.pag_checksum = CCH_checksum(window.win_bdb);
PIO_write(pageSpace->file, window.win_bdb, window.win_buffer,
tdbb->tdbb_status_vector);
CCH_RELEASE(tdbb, &window);
if (file->fil_min_page)
file->fil_fudge = 1;
return sequence;
}
int PAG_add_header_entry(header_page* header, USHORT type, USHORT len, const UCHAR* entry)
{
/***********************************************
*
* P A G _ a d d _ h e a d e r _ e n t r y
*
***********************************************
*
* Functional description
* Add an entry to header page.
* This will be used mainly for the shadow header page and adding
* secondary files.
* Will not follow to hdr_next_page
* Will not journal changes made to page. => obsolete
* RETURNS
* TRUE - modified page
* FALSE - nothing done
* CVC: Nobody checks the result of this function!
*
**************************************/
thread_db* tdbb = JRD_get_thread_data();
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
err_post_if_database_is_readonly(dbb);
UCHAR* p;
for (p = header->hdr_data; ((*p != HDR_end) && (*p != type));
p += 2 + p[1]);
if (*p != HDR_end)
return FALSE;
// We are at HDR_end, add the entry
const int free_space = dbb->dbb_page_size - header->hdr_end;
if (free_space > (2 + len)) {
fb_assert(type <= MAX_UCHAR);
fb_assert(len <= MAX_UCHAR);
*p++ = static_cast<UCHAR>(type);
*p++ = static_cast<UCHAR>(len);
if (len) {
if (entry) {
memcpy(p, entry, len);
}
else {
memset(p, 0, len);
}
p += len;
}
*p = HDR_end;
header->hdr_end = p - (UCHAR *) header;
return TRUE;
}
BUGCHECK(251);
return FALSE; /* Added to remove compiler warning */
}
void PAG_attach_temp_pages(thread_db* tdbb, USHORT pageSpaceID)
{
/***********************************************
*
* P A G _ a t t a c h _ t e m p _ p a g e s
*
***********************************************
*
* Functional description
* Attach a temporary page space
*
**************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
PageSpace* pageSpaceTemp = dbb->dbb_page_manager.addPageSpace(pageSpaceID);
if (!pageSpaceTemp->file)
{
Firebird::PathName file_name = TempFile::create(SCRATCH);
pageSpaceTemp->file = PIO_create(dbb, file_name, true, true, false);
PAG_format_pip(tdbb, *pageSpaceTemp);
}
}
int PAG_replace_entry_first(header_page* header, USHORT type, USHORT len, const UCHAR* entry)
{
/***********************************************
*
* P A G _ r e p l a c e _ e n t r y _ f i r s t
*
***********************************************
*
* Functional description
* Replace an entry in the header page so it will become first entry
* This will be used mainly for the clumplets used for backup purposes
* because they are needed to be read without page lock
* Will not follow to hdr_next_page
* Will not journal changes made to page. => obsolete
* RETURNS
* TRUE - modified page
* FALSE - nothing done
*
**************************************/
thread_db* tdbb = JRD_get_thread_data();
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
err_post_if_database_is_readonly(dbb);
UCHAR* p = header->hdr_data;
while ((*p != HDR_end) && (*p != type)) {
p += 2 + p[1];
}
// Remove item if found it somewhere
if (*p != HDR_end) {
UCHAR l = p[1] + 2;
memmove(p, p + l,
header->hdr_end - (p - (UCHAR*) header) - l + 1); // to preserve HDR_end
header->hdr_end -= l;
}
if (!entry) {
return FALSE; // We were asked just to remove item. We finished.
}
// Check if we got enough space
if (dbb->dbb_page_size - header->hdr_end <= len + 2) {
BUGCHECK(251);
}
// Actually add the item
memmove(header->hdr_data + len + 2, header->hdr_data, header->hdr_end - HDR_SIZE + 1);
header->hdr_data[0] = type;
header->hdr_data[1] = len;
memcpy(header->hdr_data + 2, entry, len);
header->hdr_end += len + 2;
return TRUE;
}
PAG PAG_allocate(WIN * window)
{
/**************************************
*
* P A G _ a l l o c a t e
*
**************************************
*
* Functional description
* Allocate a page and fake a read with a write lock. This is
* the universal sequence when allocating pages.
*
**************************************/
thread_db* tdbb = JRD_get_thread_data();
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
PageManager& pageMgr = dbb->dbb_page_manager;
PageSpace* pageSpace = pageMgr.findPageSpace(
window->win_page.getPageSpaceID());
fb_assert(pageSpace);
// Not sure if this can be moved inside the loop. Maybe some data members
// should persist across iterations?
WIN pip_window(pageSpace->pageSpaceID, -1);
// CVC: Not sure of the initial value. Notice bytes and bit are used after the loop.
UCHAR* bytes = 0;
UCHAR bit = 0;
pag* new_page = 0; // NULL before the search for a new page.
// Starting from ODS 11.1 we store in pip_header.reserved number of pages
// allocated from this pointer page. There is intention to create dedicated
// field at page_inv_page for this purpose in ODS 12.
const bool isODS11_x = (dbb->dbb_ods_version == ODS_VERSION11 &&
dbb->dbb_minor_version >= 1);
/* Find an allocation page with something on it */
SLONG relative_bit = -1;
SLONG sequence;
SLONG pipMin;
for (sequence = pageSpace->pipHighWater;; sequence++) {
pip_window.win_page = (sequence == 0) ?
pageSpace->ppFirst : sequence * dbb->dbb_page_manager.pagesPerPIP - 1;
page_inv_page* pip_page =
(page_inv_page*) CCH_FETCH(tdbb, &pip_window, LCK_write, pag_pages);
pipMin = MAX_SLONG;
const UCHAR* end = (UCHAR*) pip_page + dbb->dbb_page_size;
for (bytes = &pip_page->pip_bits[pip_page->pip_min >> 3]; bytes < end;
bytes++)
{
if (*bytes != 0) {
/* 'byte' is not zero, so it describes at least one free page. */
bit = 1;
for (SLONG i = 0; i < 8; i++, bit <<= 1) {
if (bit & *bytes) {
relative_bit = ((bytes - pip_page->pip_bits) << 3) + i;
pipMin = MIN(pipMin, relative_bit);
const SLONG pageNum = relative_bit + sequence * pageMgr.pagesPerPIP;
window->win_page = pageNum;
new_page = CCH_fake(tdbb, window, 0); /* don't wait on latch */
if (new_page)
{
if (isODS11_x)
{
USHORT next_init_pages = 1;
// ensure there are space on disk for faked page
if (relative_bit + 1 > pip_page->pip_header.reserved)
{
fb_assert(relative_bit == pip_page->pip_header.reserved);
USHORT init_pages = 1;
if (!(dbb->dbb_flags & DBB_no_reserve))
{
const int minExtendPages = MIN_EXTEND_BYTES / dbb->dbb_page_size;
init_pages = sequence ? 64 : MIN(pip_page->pip_header.reserved / 16, 64);
// don't touch pages belongs to the next PIP
init_pages =
MIN(init_pages, pageMgr.pagesPerPIP - pip_page->pip_header.reserved);
if (init_pages < minExtendPages)
init_pages = 1;
next_init_pages = init_pages;
}
ISC_STATUS_ARRAY status;
const ULONG start =
sequence * pageMgr.pagesPerPIP + pip_page->pip_header.reserved;
init_pages = PIO_init_data(dbb, pageSpace->file, status, start, init_pages);
if (init_pages) {
pip_page->pip_header.reserved += init_pages;
}
else
{
// PIO_init_data returns zero - perhaps it is not supported, no space
// left on disk or IO error occured. Try to write one page and handle
// IO errors if any
CCH_must_write(window);
try {
CCH_RELEASE(tdbb, window);
pip_page->pip_header.reserved = relative_bit + 1;
}
catch (Firebird::status_exception)
{
// forget about this page as if we never tried to fake it
CCH_forget_page(tdbb, window);
// normally all page buffers now released by CCH_unwind
// only exception is when TDBB_no_cache_unwind flag is set
if (tdbb->tdbb_flags & TDBB_no_cache_unwind)
CCH_RELEASE(tdbb, &pip_window);
throw;
}
new_page = CCH_fake(tdbb, window, 1);
}
fb_assert(new_page);
}
if (!(dbb->dbb_flags & DBB_no_reserve))
{
const ULONG initialized =
sequence * pageMgr.pagesPerPIP + pip_page->pip_header.reserved;
// At this point we ensure database has at least "initialized" pages
// allocated. To avoid file growth by few pages when all this space
// will be used, extend file up to initialized + next_init_pages now
pageSpace->extend(tdbb, initialized + next_init_pages);
}
}
break; /* Found a page and successfully fake-ed it */
}
}
}
}
if (new_page)
break; /* Found a page and successfully fake-ed it */
}
if (new_page)
break; /* Found a page and successfully fake-ed it */
CCH_RELEASE(tdbb, &pip_window);
}
pageSpace->pipHighWater = sequence;
CCH_MARK(tdbb, &pip_window);
*bytes &= ~bit;
page_inv_page* pip_page = (page_inv_page*) pip_window.win_buffer;
if (pipMin == relative_bit)
pipMin++;
pip_page->pip_min = pipMin;
if (relative_bit != pageMgr.pagesPerPIP - 1) {
CCH_RELEASE(tdbb, &pip_window);
CCH_precedence(tdbb, window, pip_window.win_page);
#ifdef VIO_DEBUG
if (debug_flag > DEBUG_WRITES_INFO)
printf("\tPAG_allocate: allocated page %"SLONGFORMAT"\n",
window->win_page);
#endif
return new_page;
}
/* We've allocated the last page on the space management page. Rather
than returning it, format it as a page inventory page, and recurse. */
page_inv_page* new_pip_page = (page_inv_page*) new_page;
new_pip_page->pip_header.pag_type = pag_pages;
const UCHAR* end = (UCHAR *) new_pip_page + dbb->dbb_page_size;
memset(new_pip_page->pip_bits, 0xff, end - new_pip_page->pip_bits);
CCH_must_write(window);
CCH_RELEASE(tdbb, window);
CCH_must_write(&pip_window);
CCH_RELEASE(tdbb, &pip_window);
return PAG_allocate(window);
}
SLONG PAG_attachment_id(thread_db* tdbb)
{
/******************************************
*
* P A G _ a t t a c h m e n t _ i d
*
******************************************
*
* Functional description
* Get attachment id. If don't have one, get one. As a side
* effect, get a lock on it as well.
*
******************************************/
SET_TDBB(tdbb);
Database* dbb = tdbb->getDatabase();
Attachment* attachment = tdbb->getAttachment();
WIN window(DB_PAGE_SPACE, -1);
/* If we've been here before just return the id */
if (attachment->att_id_lock)
return attachment->att_attachment_id;
/* Get new attachment id */
if (dbb->dbb_flags & DBB_read_only) {
attachment->att_attachment_id = dbb->dbb_attachment_id + fb_utils::genReadOnlyId();
}
else {
window.win_page = HEADER_PAGE_NUMBER;
header_page* header = (header_page*) CCH_FETCH(tdbb, &window, LCK_write, pag_header);
CCH_MARK(tdbb, &window);
attachment->att_attachment_id = ++header->hdr_attachment_id;
CCH_RELEASE(tdbb, &window);
}
/* Take out lock on attachment id */
Lock* lock = FB_NEW_RPT(*dbb->dbb_permanent, sizeof(SLONG)) Lock();
attachment->att_id_lock = lock;
lock->lck_type = LCK_attachment;
lock->lck_owner_handle = LCK_get_owner_handle(tdbb, lock->lck_type);
lock->lck_parent = dbb->dbb_lock;
lock->lck_length = sizeof(SLONG);
lock->lck_key.lck_long = attachment->att_attachment_id;
lock->lck_dbb = dbb;
lock->lck_ast = blocking_ast_attachment;
lock->lck_object = attachment;
LCK_lock(tdbb, lock, LCK_EX, LCK_WAIT);
return attachment->att_attachment_id;
}
int PAG_delete_clump_entry(SLONG page_num, USHORT type)
{
/***********************************************
*
* P A G _ d e l e t e _ c l u m p _ e n t r y
*
***********************************************
*
* Functional description
* Gets rid on the entry 'type' from page.
*
**************************************/
thread_db* tdbb = JRD_get_thread_data();
Database* dbb = tdbb->getDatabase();
CHECK_DBB(dbb);
err_post_if_database_is_readonly(dbb);
WIN window(DB_PAGE_SPACE, page_num);
pag* page;
if (page_num == HEADER_PAGE)
page = CCH_FETCH(tdbb, &window, LCK_write, pag_header);
else
page = CCH_FETCH(tdbb, &window, LCK_write, pag_log);
UCHAR* entry_p;
const UCHAR* clump_end;
if (!find_type
(page_num, &window, &page, LCK_write, type, &entry_p, &clump_end))