Skip to content

Commit 581c04d

Browse files
author
Artyom Ivanov
committed
refactor(pag): Use fallocate as the primary method for extending a file
- This should eliminate double writes of pages when file is extended. - If fallocate is not supported by filesystem fallback to the old method of writing zeroes to the file.
1 parent 45f0a5a commit 581c04d

File tree

6 files changed

+152
-116
lines changed

6 files changed

+152
-116
lines changed

src/jrd/nbak.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,12 @@ bool BackupManager::extendDatabase(thread_db* tdbb)
389389
if (maxAllocPage >= maxPage)
390390
return true;
391391

392-
if (!pgSpace->extend(tdbb, maxPage, true))
393-
return false;
394-
392+
const auto extension_result = pgSpace->extend(tdbb, maxPage, true);
395393
maxAllocPage = pgSpace->maxAlloc();
394+
if (extension_result.success && maxAllocPage > maxPage)
395+
return true;
396+
397+
// Fast file extension not succeeded for some reason, try file extension via direct file writes.
396398
while (maxAllocPage < maxPage)
397399
{
398400
const USHORT ret = PIO_init_data(tdbb, pgSpace->file, tdbb->tdbb_status_vector,

src/jrd/os/pio_proto.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ void PIO_close(Jrd::jrd_file*);
4040
Jrd::jrd_file* PIO_create(Jrd::thread_db*, const Firebird::PathName&,
4141
const bool, const bool);
4242
bool PIO_expand(const TEXT*, USHORT, TEXT*, FB_SIZE_T);
43-
void PIO_extend(Jrd::thread_db*, Jrd::jrd_file*, const ULONG, const USHORT);
43+
bool PIO_fast_extension_is_supported(const Jrd::jrd_file& file) noexcept;
44+
bool PIO_extend(Jrd::thread_db* tdbb, Jrd::jrd_file* file, ULONG extPages, USHORT pageSize);
4445
void PIO_flush(Jrd::thread_db*, Jrd::jrd_file*);
4546
void PIO_force_write(Jrd::jrd_file*, const bool);
4647
ULONG PIO_get_number_of_pages(const Jrd::jrd_file*, const USHORT);
4748
bool PIO_header(Jrd::thread_db*, UCHAR*, unsigned);
48-
USHORT PIO_init_data(Jrd::thread_db*, Jrd::jrd_file*, Jrd::FbStatusVector*, ULONG, USHORT);
49+
USHORT PIO_init_data(Jrd::thread_db* tdbb, Jrd::jrd_file* file, Jrd::FbStatusVector* status_vector, ULONG startPage, USHORT initPages);
4950
Jrd::jrd_file* PIO_open(Jrd::thread_db*, const Firebird::PathName&,
5051
const Firebird::PathName&);
5152
bool PIO_read(Jrd::thread_db*, Jrd::jrd_file*, Jrd::BufferDesc*, Ods::pag*, Jrd::FbStatusVector*);

src/jrd/os/posix/unix.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,19 @@ bool PIO_expand(const TEXT* file_name, USHORT file_length, TEXT* expanded_name,
299299
}
300300

301301

302-
void PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USHORT pageSize)
302+
bool PIO_fast_extension_is_supported(const Jrd::jrd_file& file) noexcept
303+
{
304+
#if defined(HAVE_LINUX_FALLOC_H) && defined(HAVE_FALLOCATE)
305+
return file.fil_flags & FIL_no_fast_extend
306+
? false
307+
: true;
308+
#else
309+
return false;
310+
#endif
311+
}
312+
313+
314+
bool PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USHORT pageSize)
303315
{
304316
/**************************************
305317
*
@@ -317,8 +329,8 @@ void PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USH
317329

318330
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
319331

320-
if (file->fil_flags & FIL_no_fast_extend)
321-
return;
332+
if (!PIO_fast_extension_is_supported(*file))
333+
return false;
322334

323335
const ULONG filePages = PIO_get_number_of_pages(file, pageSize);
324336
const ULONG extendBy = MIN(MAX_ULONG - filePages, extPages);
@@ -341,7 +353,7 @@ void PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USH
341353
unix_error("fallocate", file, isc_io_write_err);
342354

343355
file->fil_flags |= FIL_no_fast_extend;
344-
return;
356+
return false;
345357
}
346358

347359
if (r == IO_RETRY)
@@ -352,12 +364,12 @@ void PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USH
352364
#endif
353365
unix_error("fallocate_retry", file, isc_io_write_err);
354366
}
367+
368+
return true;
355369
#else
356370
file->fil_flags |= FIL_no_fast_extend;
371+
return false;
357372
#endif // fallocate present
358-
359-
// not implemented
360-
return;
361373
}
362374

363375

src/jrd/os/win32/winnt.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,13 @@ bool PIO_expand(const TEXT* file_name, USHORT file_length, TEXT* expanded_name,
215215
}
216216

217217

218-
void PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USHORT pageSize)
218+
bool PIO_fast_extension_is_supported(const Jrd::jrd_file& file) noexcept
219+
{
220+
return true;
221+
}
222+
223+
224+
bool PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USHORT pageSize)
219225
{
220226
/**************************************
221227
*
@@ -238,7 +244,7 @@ void PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USH
238244

239245
// if file have no extend lock it is better to not extend file than corrupt it
240246
if (!file->fil_ext_lock)
241-
return;
247+
return false;
242248

243249
EngineCheckout cout(tdbb, FB_FUNCTION, EngineCheckout::UNNECESSARY);
244250
FileExtendLockGuard extLock(file->fil_ext_lock, true);
@@ -257,6 +263,8 @@ void PIO_extend(thread_db* tdbb, jrd_file* file, const ULONG extPages, const USH
257263

258264
if (!SetEndOfFile(hFile))
259265
nt_error("SetEndOfFile", file, isc_io_write_err, NULL);
266+
267+
return true;
260268
}
261269

262270

0 commit comments

Comments
 (0)