-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathunix.cpp
1334 lines (1113 loc) · 33.9 KB
/
unix.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: unix.cpp
* DESCRIPTION: UNIX (BSD) specific physical IO
*
* 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): ______________________________________.
* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
*
* 2002.10.27 Sean Leyne - Completed removal of "DELTA" port
*
*/
#include "firebird.h"
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <sys/file.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_AIO_H
#include <aio.h>
#endif
#ifdef HAVE_LINUX_FALLOC_H
#include <linux/falloc.h>
#endif
#ifdef SUPPORT_RAW_DEVICES
#include <sys/ioctl.h>
#ifdef LINUX
#include <linux/fs.h>
#endif
#endif //SUPPORT_RAW_DEVICES
#include "../jrd/jrd.h"
#include "../jrd/os/pio.h"
#include "../jrd/ods.h"
#include "../jrd/lck.h"
#include "../jrd/cch.h"
#include "iberror.h"
#include "../jrd/cch_proto.h"
#include "../jrd/err_proto.h"
#include "../yvalve/gds_proto.h"
#include "../common/isc_proto.h"
#include "../common/isc_f_proto.h"
#include "../common/os/isc_i_proto.h"
#include "../jrd/lck_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/ods_proto.h"
#include "../jrd/os/pio_proto.h"
#include "../common/classes/init.h"
#include "../common/os/os_utils.h"
using namespace Jrd;
using namespace Firebird;
// Some operating systems have problems with use of write/read with
// big (>2Gb) files. On the other hand, pwrite/pread works fine for them.
// Therefore:
#if defined SOLARIS
#define BROKEN_IO_64
#endif
// which will force use of pread/pwrite even for CS.
#define IO_RETRY 20
#ifdef O_SYNC
#define SYNC O_SYNC
#endif
// Changed to not redfine SYNC if O_SYNC already exists
// they seem to be the same values anyway. MOD 13-07-2001
#if (!(defined SYNC) && (defined O_FSYNC))
#define SYNC O_FSYNC
#endif
#ifdef O_DSYNC
#undef SYNC
#define SYNC O_DSYNC
#endif
#ifndef SYNC
#define SYNC 0
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#if !defined(O_DIRECT) && defined(LSB_BUILD)
#define O_DIRECT 00040000
#endif
// please undefine FCNTL_BROKEN for operating systems,
// that can successfully change BOTH O_DIRECT and O_SYNC using fcntl()
static const mode_t MASK = 0660;
#define FCNTL_BROKEN
static jrd_file* seek_file(jrd_file*, BufferDesc*, FB_UINT64*, FbStatusVector*);
static jrd_file* setup_file(Database*, const PathName&, const int, const bool, const bool, const bool);
static void lockDatabaseFile(int& desc, const bool shareMode, const bool temporary,
const char* fileName, ISC_STATUS operation);
static bool unix_error(const TEXT*, const jrd_file*, ISC_STATUS, FbStatusVector* = NULL);
static bool block_size_error(const jrd_file*, off_t, FbStatusVector* = NULL);
#if !(defined HAVE_PREAD && defined HAVE_PWRITE)
static SLONG pread(int, SCHAR*, SLONG, SLONG);
static SLONG pwrite(int, SCHAR*, SLONG, SLONG);
#endif
#ifdef SUPPORT_RAW_DEVICES
static bool raw_devices_validate_database (int, const PathName&);
static int raw_devices_unlink_database (const PathName&);
#endif
static int openFile(const Firebird::PathName&, const bool, const bool, const bool);
static void maybeCloseFile(int&);
int PIO_add_file(thread_db* tdbb, jrd_file* main_file, const PathName& file_name, SLONG start)
{
/**************************************
*
* P I O _ a d d _ f i l e
*
**************************************
*
* Functional description
* Add a file to an existing database. Return the sequence
* number of the new file. If anything goes wrong, return a
* sequence of 0.
* NOTE: This routine does not lock any mutexes on
* its own behalf. It is assumed that mutexes will
* have been locked before entry.
*
**************************************/
jrd_file* new_file = PIO_create(tdbb, file_name, false, false);
if (!new_file)
return 0;
new_file->fil_min_page = start;
USHORT sequence = 1;
jrd_file* file;
for (file = main_file; file->fil_next; file = file->fil_next)
++sequence;
file->fil_max_page = start - 1;
file->fil_next = new_file;
return sequence;
}
void PIO_close(jrd_file* main_file)
{
/**************************************
*
* P I O _ c l o s e
*
**************************************
*
* Functional description
* NOTE: This routine does not lock any mutexes on
* its own behalf. It is assumed that mutexes will
* have been locked before entry.
*
**************************************/
for (jrd_file* file = main_file; file; file = file->fil_next)
{
WriteLockGuard writeGuard(file->fil_desc_lock, FB_FUNCTION);
if (file->fil_desc && file->fil_desc != -1)
{
close(file->fil_desc);
file->fil_desc = -1;
}
}
}
jrd_file* PIO_create(thread_db* tdbb, const PathName& file_name,
const bool overwrite, const bool temporary)
{
/**************************************
*
* P I O _ c r e a t e
*
**************************************
*
* Functional description
* Create a new database file.
* NOTE: This routine does not lock any mutexes on
* its own behalf. It is assumed that mutexes will
* have been locked before entry.
*
**************************************/
#ifdef SUPERSERVER_V2
const int flag = SYNC | O_RDWR | O_CREAT | (overwrite ? O_TRUNC : O_EXCL) | O_BINARY;
#else
#ifdef SUPPORT_RAW_DEVICES
const int flag = O_RDWR |
(PIO_on_raw_device(file_name) ? 0 : O_CREAT) |
(overwrite ? O_TRUNC : O_EXCL) |
O_BINARY;
#else
const int flag = O_RDWR | O_CREAT | (overwrite ? O_TRUNC : O_EXCL) | O_BINARY;
#endif
#endif
Database* const dbb = tdbb->getDatabase();
int desc = os_utils::open(file_name.c_str(), flag, 0666);
if (desc == -1)
{
ERR_post(Arg::Gds(isc_io_error) << Arg::Str("open O_CREAT") << Arg::Str(file_name) <<
Arg::Gds(isc_io_create_err) << Arg::Unix(errno));
}
const bool shareMode = dbb->dbb_config->getServerMode() != MODE_SUPER;
lockDatabaseFile(desc, shareMode, temporary, file_name.c_str(), isc_io_create_err);
#ifdef HAVE_FCHMOD
if (fchmod(desc, MASK) < 0)
#else
if (chmod(file_name.c_str(), MASK) < 0)
#endif
{
int chmodError = errno;
// ignore possible errors in these calls - even if they have failed
// we cannot help much with former recovery
close(desc);
unlink(file_name.c_str());
ERR_post(Arg::Gds(isc_io_error) << Arg::Str("chmod") << Arg::Str(file_name) <<
Arg::Gds(isc_io_create_err) << Arg::Unix(chmodError));
}
if (temporary
#ifdef SUPPORT_RAW_DEVICES
&& (!PIO_on_raw_device(file_name))
#endif
)
{
int rc = unlink(file_name.c_str());
// it's no use throwing an error if unlink() failed for temp file in release build
#ifdef DEV_BUILD
if (rc < 0)
{
ERR_post(Arg::Gds(isc_io_error) << Arg::Str("unlink") << Arg::Str(file_name) <<
Arg::Gds(isc_io_create_err) << Arg::Unix(errno));
}
#endif
}
// os_utils::posix_fadvise(desc, 0, 0, POSIX_FADV_RANDOM);
// File open succeeded. Now expand the file name.
PathName expanded_name(file_name);
ISC_expand_filename(expanded_name, false);
return setup_file(dbb, expanded_name, desc, false, shareMode, !(flag & O_CREAT));
}
bool PIO_expand(const TEXT* file_name, USHORT file_length, TEXT* expanded_name, FB_SIZE_T len_expanded)
{
/**************************************
*
* P I O _ e x p a n d
*
**************************************
*
* Functional description
* Fully expand a file name. If the file doesn't exist, do something
* intelligent.
*
**************************************/
return ISC_expand_filename(file_name, file_length, expanded_name, len_expanded, false);
}
void PIO_extend(thread_db* tdbb, jrd_file* main_file, const ULONG extPages, const USHORT pageSize)
{
/**************************************
*
* P I O _ e x t e n d
*
**************************************
*
* Functional description
* Extend file by extPages pages of pageSize size.
*
**************************************/
#if defined(HAVE_LINUX_FALLOC_H) && defined(HAVE_FALLOCATE)
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
ULONG leftPages = extPages;
for (jrd_file* file = main_file; file && leftPages; file = file->fil_next)
{
const ULONG filePages = PIO_get_number_of_pages(file, pageSize);
const ULONG fileMaxPages = (file->fil_max_page == MAX_ULONG) ?
MAX_ULONG : file->fil_max_page - file->fil_min_page + 1;
if (filePages < fileMaxPages)
{
if (file->fil_flags & FIL_no_fast_extend)
return;
const ULONG extendBy = MIN(fileMaxPages - filePages + file->fil_fudge, leftPages);
int r;
for (r = 0; r < IO_RETRY; r++)
{
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
int err = fallocate(file->fil_desc, 0, filePages * pageSize, extendBy * pageSize);
if (err == 0)
break;
err = errno;
if (SYSCALL_INTERRUPTED(err))
continue;
if (err != EOPNOTSUPP && err != ENOSYS)
unix_error("fallocate", file, isc_io_write_err);
file->fil_flags |= FIL_no_fast_extend;
return;
}
if (r == IO_RETRY)
{
#ifdef DEV_BUILD
fprintf(stderr, "PIO_extend: retry count exceeded\n");
fflush(stderr);
#endif
unix_error("fallocate_retry", file, isc_io_write_err);
}
leftPages -= extendBy;
}
}
#else
main_file->fil_flags |= FIL_no_fast_extend;
#endif // fallocate present
// not implemented
return;
}
void PIO_flush(thread_db* tdbb, jrd_file* main_file)
{
/**************************************
*
* P I O _ f l u s h
*
**************************************
*
* Functional description
* Flush the operating system cache back to good, solid oxide.
*
**************************************/
// Since all SUPERSERVER_V2 database and shadow I/O is synchronous, this is a no-op.
#ifndef SUPERSERVER_V2
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
MutexLockGuard guard(main_file->fil_mutex, FB_FUNCTION);
for (jrd_file* file = main_file; file; file = file->fil_next)
{
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
if (file->fil_desc != -1)
{
// This really should be an error
fsync(file->fil_desc);
}
}
#endif
}
#ifdef SOLARIS
// minimize #ifdefs inside PIO_force_write()
#define O_DIRECT 0
#endif
void PIO_force_write(jrd_file* file, const bool forcedWrites, const bool notUseFSCache)
{
/**************************************
*
* P I O _ f o r c e _ w r i t e ( G E N E R I C )
*
**************************************
*
* Functional description
* Set (or clear) force write, if possible, for the database.
*
**************************************/
// Since all SUPERSERVER_V2 database and shadow I/O is synchronous, this is a no-op.
#ifndef SUPERSERVER_V2
const bool oldForce = (file->fil_flags & FIL_force_write) != 0;
const bool oldNotUseCache = (file->fil_flags & FIL_no_fs_cache) != 0;
if (forcedWrites != oldForce || notUseFSCache != oldNotUseCache)
{
const int control = (forcedWrites ? SYNC : 0) | (notUseFSCache ? O_DIRECT : 0);
WriteLockGuard writeGuard(file->fil_desc_lock, FB_FUNCTION);
#ifndef FCNTL_BROKEN
if (fcntl(file->fil_desc, F_SETFL, control) == -1)
{
unix_error("fcntl() SYNC/DIRECT", file, isc_io_access_err);
}
#else //FCNTL_BROKEN
maybeCloseFile(file->fil_desc);
file->fil_desc = openFile(file->fil_string, forcedWrites,
notUseFSCache, file->fil_flags & FIL_readonly);
if (file->fil_desc == -1)
{
unix_error("re open() for SYNC/DIRECT", file, isc_io_open_err);
}
lockDatabaseFile(file->fil_desc, file->fil_flags & FIL_sh_write, false,
file->fil_string, isc_io_open_err);
#endif //FCNTL_BROKEN
#ifdef SOLARIS
if (notUseFSCache != oldNotUseCache &&
directio(file->fil_desc, notUseFSCache ? DIRECTIO_ON : DIRECTIO_OFF) != 0)
{
unix_error("directio()", file, isc_io_access_err);
}
#endif
file->fil_flags &= ~(FIL_force_write | FIL_no_fs_cache);
file->fil_flags |= (forcedWrites ? FIL_force_write : 0) |
(notUseFSCache ? FIL_no_fs_cache : 0);
}
#endif
}
ULONG PIO_get_number_of_pages(const jrd_file* file, const USHORT pagesize)
{
/**************************************
*
* P I O _ g e t _ n u m b e r _ o f _ p a g e s
*
**************************************
*
* Functional description
* Compute number of pages in file, based only on file size.
*
**************************************/
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
if (file->fil_desc == -1)
unix_error("fstat", file, isc_io_access_err);
struct STAT statistics;
if (os_utils::fstat(file->fil_desc, &statistics))
unix_error("fstat", file, isc_io_access_err);
FB_UINT64 length = statistics.st_size;
#ifdef SUPPORT_RAW_DEVICES
if (S_ISCHR(statistics.st_mode) || S_ISBLK(statistics.st_mode))
{
// This place is highly OS-dependent
// Looks like any OS needs own ioctl() to determine raw device size
#undef HAS_RAW_SIZE
#ifdef LINUX
#ifdef BLKGETSIZE64
if (ioctl(file->fil_desc, BLKGETSIZE64, &length) != 0)
#endif /*BLKGETSIZE64*/
{
unsigned long sectorCount;
if (ioctl(file->fil_desc, BLKGETSIZE, §orCount) != 0)
unix_error("ioctl(BLKGETSIZE)", file, isc_io_access_err);
unsigned int sectorSize;
if (ioctl(file->fil_desc, BLKSSZGET, §orSize) != 0)
unix_error("ioctl(BLKSSZGET)", file, isc_io_access_err);
length = sectorCount;
length *= sectorSize;
}
#define HAS_RAW_SIZE
#endif /*LINUX*/
#ifndef HAS_RAW_SIZE
error: Raw device support for your OS is missing. Fix it or turn off raw device support.
#endif
#undef HAS_RAW_SIZE
}
#endif /*SUPPORT_RAW_DEVICES*/
return length / pagesize;
}
void PIO_header(thread_db* tdbb, UCHAR* address, int length)
{
/**************************************
*
* P I O _ h e a d e r
*
**************************************
*
* Functional description
* Read the page header.
*
**************************************/
Database* const dbb = tdbb->getDatabase();
int i;
SINT64 bytes;
PageSpace* pageSpace = dbb->dbb_page_manager.findPageSpace(DB_PAGE_SPACE);
jrd_file* file = pageSpace->file;
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
if (file->fil_desc == -1)
unix_error("PIO_header", file, isc_io_read_err);
for (i = 0; i < IO_RETRY; i++)
{
if ((bytes = os_utils::pread(file->fil_desc, address, length, 0)) == length)
break;
if (bytes < 0 && !SYSCALL_INTERRUPTED(errno))
unix_error("read", file, isc_io_read_err);
if (bytes >= 0)
block_size_error(file, bytes);
}
if (i == IO_RETRY)
{
if (bytes == 0)
{
#ifdef DEV_BUILD
fprintf(stderr, "PIO_header: an empty page read!\n");
fflush(stderr);
#endif
}
else
{
#ifdef DEV_BUILD
fprintf(stderr, "PIO_header: retry count exceeded\n");
fflush(stderr);
#endif
unix_error("read_retry", file, isc_io_read_err);
}
}
}
// we need a class here only to return memory on shutdown and avoid
// false memory leak reports
static Firebird::InitInstance<ZeroBuffer> zeros;
USHORT PIO_init_data(thread_db* tdbb, jrd_file* main_file, FbStatusVector* status_vector,
ULONG startPage, USHORT initPages)
{
/**************************************
*
* P I O _ i n i t _ d a t a
*
**************************************
*
* Functional description
* Initialize tail of file with zeros
*
**************************************/
const char* const zero_buff = zeros().getBuffer();
const size_t zero_buff_size = zeros().getSize();
Database* const dbb = tdbb->getDatabase();
// Fake buffer, used in seek_file. Page space ID have no matter there
// as we already know file to work with
BufferDesc bdb(dbb->dbb_bcb);
bdb.bdb_page = PageNumber(0, startPage);
FB_UINT64 offset;
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
jrd_file* file = seek_file(main_file, &bdb, &offset, status_vector);
if (!file)
return 0;
if (file->fil_min_page + 8 > startPage)
return 0;
USHORT leftPages = initPages;
const ULONG initBy = MIN(file->fil_max_page - startPage, leftPages);
if (initBy < leftPages)
leftPages = initBy;
for (ULONG i = startPage; i < startPage + initBy; )
{
bdb.bdb_page = PageNumber(0, i);
USHORT write_pages = zero_buff_size / dbb->dbb_page_size;
if (write_pages > leftPages)
write_pages = leftPages;
SLONG to_write = write_pages * dbb->dbb_page_size;
SINT64 written;
for (int r = 0; r < IO_RETRY; r++)
{
if (!(file = seek_file(file, &bdb, &offset, status_vector)))
return false;
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
if ((written = os_utils::pwrite(file->fil_desc, zero_buff, to_write, LSEEK_OFFSET_CAST offset)) == to_write)
break;
if (written < 0 && !SYSCALL_INTERRUPTED(errno))
return unix_error("write", file, isc_io_write_err, status_vector);
}
leftPages -= write_pages;
i += write_pages;
}
return (initPages - leftPages);
}
jrd_file* PIO_open(thread_db* tdbb,
const PathName& string,
const PathName& file_name)
{
/**************************************
*
* P I O _ o p e n
*
**************************************
*
* Functional description
* Open a database file.
*
**************************************/
Database* const dbb = tdbb->getDatabase();
bool readOnly = false;
const PathName& expandedName(string.hasData() ? string : file_name);
const PathName& originalName(file_name.hasData() ? file_name : string);
int desc = openFile(expandedName, false, false, false);
if (desc == -1)
{
// Try opening the database file in ReadOnly mode. The database file could
// be on a RO medium (CD-ROM etc.). If this fileopen fails, return error.
desc = openFile(expandedName, false, false, true);
if (desc == -1)
{
ERR_post(Arg::Gds(isc_io_error) << Arg::Str("open") << Arg::Str(originalName) <<
Arg::Gds(isc_io_open_err) << Arg::Unix(errno));
}
readOnly = true;
}
else if (geteuid() == 0)
{
// root has too many rights - therefore artificially check for readonly file
struct STAT st;
if (os_utils::fstat(desc, &st) == 0)
{
readOnly = ((st.st_mode & 0222) == 0); // nobody has write permissions
}
}
if (readOnly)
{
// If this is the primary file, set Database flag to indicate that it is
// being opened ReadOnly. This flag will be used later to compare with
// the Header Page flag setting to make sure that the database is set ReadOnly.
PageSpace* pageSpace = dbb->dbb_page_manager.findPageSpace(DB_PAGE_SPACE);
if (!pageSpace->file)
dbb->dbb_flags |= DBB_being_opened_read_only;
}
const bool shareMode = dbb->dbb_config->getServerMode() != MODE_SUPER;
lockDatabaseFile(desc, shareMode, false, expandedName.c_str(), isc_io_open_err);
// os_utils::posix_fadvise(desc, 0, 0, POSIX_FADV_RANDOM);
bool raw = false;
#ifdef SUPPORT_RAW_DEVICES
// At this point the file has successfully been opened in either RW or RO
// mode. Check if it is a special file (i.e. raw block device) and if a
// valid database is on it. If not, return an error.
if (PIO_on_raw_device(expandedName))
{
raw = true;
if (!raw_devices_validate_database(desc, expandedName))
{
maybeCloseFile(desc);
ERR_post(Arg::Gds(isc_io_error) << Arg::Str("open") << Arg::Str(originalName) <<
Arg::Gds(isc_io_open_err) << Arg::Unix(ENOENT));
}
}
#endif // SUPPORT_RAW_DEVICES
return setup_file(dbb, expandedName, desc, readOnly, shareMode, raw);
}
bool PIO_read(thread_db* tdbb, jrd_file* file, BufferDesc* bdb, Ods::pag* page, FbStatusVector* status_vector)
{
/**************************************
*
* P I O _ r e a d
*
**************************************
*
* Functional description
* Read a data page. Oh wow.
*
**************************************/
int i;
SINT64 bytes;
FB_UINT64 offset;
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
if (file->fil_desc == -1)
return unix_error("read", file, isc_io_read_err, status_vector);
Database* const dbb = tdbb->getDatabase();
const SLONG size = dbb->dbb_page_size;
for (i = 0; i < IO_RETRY; i++)
{
if (!(file = seek_file(file, bdb, &offset, status_vector)))
return false;
if ((bytes = os_utils::pread(file->fil_desc, page, size, LSEEK_OFFSET_CAST offset)) == size)
{
// os_utils::posix_fadvise(file->desc, offset, size, POSIX_FADV_NOREUSE);
return true;
}
// pread() returned error
if (bytes < 0 && !SYSCALL_INTERRUPTED(errno))
return unix_error("read", file, isc_io_read_err, status_vector);
// pread() returned not enough bytes
if (bytes >= 0)
{
if (!block_size_error(file, offset + bytes, status_vector))
return false;
}
}
return unix_error("read_retry", file, isc_io_read_err, status_vector);
}
bool PIO_write(thread_db* tdbb, jrd_file* file, BufferDesc* bdb, Ods::pag* page, FbStatusVector* status_vector)
{
/**************************************
*
* P I O _ w r i t e
*
**************************************
*
* Functional description
* Write a data page. Oh wow.
*
**************************************/
int i;
SINT64 bytes;
FB_UINT64 offset;
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
if (file->fil_desc == -1)
return unix_error("write", file, isc_io_write_err, status_vector);
Database* const dbb = tdbb->getDatabase();
const SLONG size = dbb->dbb_page_size;
for (i = 0; i < IO_RETRY; i++)
{
if (!(file = seek_file(file, bdb, &offset, status_vector)))
return false;
if ((bytes = os_utils::pwrite(file->fil_desc, page, size, LSEEK_OFFSET_CAST offset)) == size)
{
// os_utils::posix_fadvise(file->desc, offset, size, POSIX_FADV_DONTNEED);
return true;
}
if (bytes < 0 && !SYSCALL_INTERRUPTED(errno))
return unix_error("write", file, isc_io_write_err, status_vector);
}
return unix_error("write_retry", file, isc_io_write_err, status_vector);
}
static jrd_file* seek_file(jrd_file* file, BufferDesc* bdb, FB_UINT64* offset,
FbStatusVector* status_vector)
{
/**************************************
*
* s e e k _ f i l e
*
**************************************
*
* Functional description
* Given a buffer descriptor block, find the appropriate
* file block and seek to the proper page in that file.
*
**************************************/
BufferControl* bcb = bdb->bdb_bcb;
Database* dbb = bcb->bcb_database;
ULONG page = bdb->bdb_page.getPageNum();
for (;; file = file->fil_next)
{
if (!file) {
CORRUPT(158); // msg 158 database file not available
}
else if (page >= file->fil_min_page && page <= file->fil_max_page)
break;
}
ReadLockGuard readGuard(file->fil_desc_lock, FB_FUNCTION);
if (file->fil_desc == -1)
{
unix_error("lseek", file, isc_io_access_err, status_vector);
return 0;
}
page -= file->fil_min_page - file->fil_fudge;
FB_UINT64 lseek_offset = page;
lseek_offset *= dbb->dbb_page_size;
if (lseek_offset != (FB_UINT64) LSEEK_OFFSET_CAST lseek_offset)
{
unix_error("lseek", file, isc_io_32bit_exceeded_err, status_vector);
return 0;
}
*offset = lseek_offset;
return file;
}
static int openFile(const PathName& name, const bool forcedWrites,
const bool notUseFSCache, const bool readOnly)
{
/**************************************
*
* o p e n F i l e
*
**************************************
*
* Functional description
* Open a file with appropriate flags.
*
**************************************/
int flag = O_BINARY | (readOnly ? O_RDONLY : O_RDWR);
#ifdef SUPERSERVER_V2
flag |= SYNC;
// what to do with O_DIRECT here ?
#else
if (forcedWrites)
flag |= SYNC;
if (notUseFSCache)
flag |= O_DIRECT;
#endif
return os_utils::open(name.c_str(), flag);
}
static void maybeCloseFile(int& desc)
{
/**************************************
*
* m a y b e C l o s e F i l e
*
**************************************
*
* Functional description
* If the file is open, close it.
*
**************************************/
if (desc >= 0)
{
close(desc);
desc = -1;
}
}
static jrd_file* setup_file(Database* dbb,
const PathName& file_name,
const int desc,
const bool readOnly,
const bool shareMode,
const bool onRawDev)
{
/**************************************
*
* s e t u p _ f i l e
*
**************************************
*
* Functional description
* Set up file and lock blocks for a file.
*
**************************************/
jrd_file* file = NULL;
try
{
file = FB_NEW_RPT(*dbb->dbb_permanent, file_name.length() + 1) jrd_file();
file->fil_desc = desc;
file->fil_max_page = MAX_ULONG;
strcpy(file->fil_string, file_name.c_str());
if (readOnly)
file->fil_flags |= FIL_readonly;
if (shareMode)
file->fil_flags |= FIL_sh_write;
if (onRawDev)
file->fil_flags |= FIL_raw_device;
}
catch (const Exception&)
{
close(desc);
delete file;
throw;
}
fb_assert(file);
return file;
}
static void lockDatabaseFile(int& desc, const bool share, const bool temporary,
const char* fileName, ISC_STATUS operation)
{
bool shared = (!temporary) && share;
bool busy = false;
do