pg.ddx.io pgbackrest/pgbackrest GitHub issues and pull requests
help / color / mirror / Atom feed[pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
20+ messages / 4 participants
[nested] [flat]
* [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-10-25 14:59 "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
0 siblings, 0 replies; 20+ messages in thread
From: fmbiete (@fmbiete) @ 2026-10-25 14:59 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
New repository type: posix-dio
Identical to posix, but opening files with O_DIRECT and reading/writting aligned.
If O_DIRECT is not supported it will fallback to existing posix behavior.
New pg-instance flag: pg-direct-io (y/n)
If enabled it will use O_DIRECT to read PostgreSQL files
inline patch 1: build/config.yaml, doc/xml/reference.xml, src/config/config.auto.h and 19 more (+690/-85 lines) download inline-1.patch
diff --git a/build/config.yaml b/build/config.yaml
index e20b77be03..7a49d400d6 100644
--- a/build/config.yaml
+++ b/build/config.yaml
@@ -1407,6 +1407,19 @@ option:
db-path: {}
db?-path: {}
+ pg-direct-io:
+ section: stanza
+ group: pg
+ type: boolean
+ default: false
+ command:
+ backup: {}
+ restore: {}
+ deprecate:
+ pg-directio: {}
+ pg?-direct-io: {}
+ pg?-directio: {}
+
# PostgreSQL host options
#---------------------------------------------------------------------------------------------------------------------------------
pg-local:
@@ -1685,6 +1698,7 @@ option:
- cifs
- gcs
- posix
+ - posix-dio
- s3
- sftp: HAVE_LIBSSH2
command:
@@ -1793,6 +1807,7 @@ option:
default: false
list:
- posix
+ - posix-dio
repo-target-time:
section: global
diff --git a/doc/xml/reference.xml b/doc/xml/reference.xml
index 5659cb7b3c..6ebefed1fa 100644
--- a/doc/xml/reference.xml
+++ b/doc/xml/reference.xml
@@ -1424,6 +1424,7 @@
<allow-item id="cifs">Like <id>posix</id>, but disables links and directory fsyncs</allow-item>
<allow-item id="gcs">Google Cloud Storage</allow-item>
<allow-item id="posix">Posix-compliant file systems</allow-item>
+ <allow-item id="posix-dio">Posix-compliant file systems using direct I/O</allow-item>
<allow-item id="s3">AWS Simple Storage Service</allow-item>
<allow-item id="sftp">Secure File Transfer Protocol</allow-item>
</allow-list>
@@ -1964,6 +1965,16 @@
<example>/conf/pgbackrest</example>
</config-key>
+ <config-key id="pg-direct-io" name="PostgreSQL Direct I/O">
+ <summary>Use direct I/O for PostgreSQL data directory.</summary>
+
+ <text>
+ <p>Enables direct I/O (<id>O_DIRECT</id>) for reading and writing files in the <postgres/> data directory during backup and restore operations.</p>
+ </text>
+
+ <example>y</example>
+ </config-key>
+
<config-key id="pg-host" name="PostgreSQL Host">
<summary><postgres/> host for operating remotely.</summary>
diff --git a/src/config/config.auto.h b/src/config/config.auto.h
index 07ea853524..5c9438c1b4 100644
--- a/src/config/config.auto.h
+++ b/src/config/config.auto.h
@@ -146,7 +146,7 @@ Option constants
#define CFGOPT_VERBOSE "verbose"
#define CFGOPT_VERSION "version"
-#define CFG_OPTION_TOTAL 202
+#define CFG_OPTION_TOTAL 203
/***********************************************************************************************************************************
Option value constants
@@ -384,6 +384,8 @@ Option value constants
#define CFGOPTVAL_REPO_TYPE_GCS_Z "gcs"
#define CFGOPTVAL_REPO_TYPE_POSIX STRID5("posix", 0x184cdf00)
#define CFGOPTVAL_REPO_TYPE_POSIX_Z "posix"
+#define CFGOPTVAL_REPO_TYPE_POSIX_DIO STRID5("posix-dio", 0xf493784cdf00)
+#define CFGOPTVAL_REPO_TYPE_POSIX_DIO_Z "posix-dio"
#define CFGOPTVAL_REPO_TYPE_S3 STRID6("s3", 0x7d31)
#define CFGOPTVAL_REPO_TYPE_S3_Z "s3"
#define CFGOPTVAL_REPO_TYPE_SFTP STRID5("sftp", 0x850d30)
@@ -546,6 +548,7 @@ typedef enum
cfgOptPageHeaderCheck,
cfgOptPg,
cfgOptPgDatabase,
+ cfgOptPgDirectIo,
cfgOptPgHost,
cfgOptPgHostCaFile,
cfgOptPgHostCaPath,
diff --git a/src/config/load.c b/src/config/load.c
index 667351cc82..142f785e0e 100644
--- a/src/config/load.c
+++ b/src/config/load.c
@@ -25,6 +25,7 @@ Configuration Load
#include "storage/cifs/storage.h"
#include "storage/helper.h"
#include "storage/posix/storage.h"
+#include "storage/posixDio/storage.h"
#include "storage/sftp/storage.h"
/***********************************************************************************************************************************
@@ -98,10 +99,11 @@ cfgLoadUpdateOption(void)
{
for (unsigned int optionIdx = 0; optionIdx < cfgOptionGroupIdxTotal(cfgOptGrpRepo); optionIdx++)
{
- // If the repo is local and either posix or cifs
+ // If the repo is local and posix, cifs, or posix-dio
if (!cfgOptionIdxTest(cfgOptRepoHost, optionIdx) &&
(cfgOptionIdxStrId(cfgOptRepoType, optionIdx) == STORAGE_POSIX_TYPE ||
- cfgOptionIdxStrId(cfgOptRepoType, optionIdx) == STORAGE_CIFS_TYPE))
+ cfgOptionIdxStrId(cfgOptRepoType, optionIdx) == STORAGE_CIFS_TYPE ||
+ cfgOptionIdxStrId(cfgOptRepoType, optionIdx) == STORAGE_POSIX_DIO_TYPE))
{
// Ensure a local repo does not have the same path as another local repo of the same type
for (unsigned int repoIdx = 0; repoIdx < cfgOptionGroupIdxTotal(cfgOptGrpRepo); repoIdx++)
diff --git a/src/config/parse.auto.c.inc b/src/config/parse.auto.c.inc
index b736d687ad..513c1500a5 100644
--- a/src/config/parse.auto.c.inc
+++ b/src/config/parse.auto.c.inc
@@ -117,6 +117,7 @@ static const StringPubConst parseRuleValueStr[] =
PARSE_RULE_STRPUB("pgbackrest"), // val/str
PARSE_RULE_STRPUB("pod-id"), // val/str
PARSE_RULE_STRPUB("posix"), // val/str
+ PARSE_RULE_STRPUB("posix-dio"), // val/str
PARSE_RULE_STRPUB("postgres"), // val/str
PARSE_RULE_STRPUB("prefer"), // val/str
PARSE_RULE_STRPUB("preserve"), // val/str
@@ -259,6 +260,7 @@ typedef enum
parseRuleValStrQT_pgbackrest_QT, // val/str/enum
parseRuleValStrQT_pod_DS_id_QT, // val/str/enum
parseRuleValStrQT_posix_QT, // val/str/enum
+ parseRuleValStrQT_posix_DS_dio_QT, // val/str/enum
parseRuleValStrQT_postgres_QT, // val/str/enum
parseRuleValStrQT_prefer_QT, // val/str/enum
parseRuleValStrQT_preserve_QT, // val/str/enum
@@ -3820,6 +3822,46 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
), // opt/pg-database
), // opt/pg-database
// -----------------------------------------------------------------------------------------------------------------------------
+ PARSE_RULE_OPTION // opt/pg-direct-io
+ ( // opt/pg-direct-io
+ PARSE_RULE_OPTION_NAME("pg-direct-io"), // opt/pg-direct-io
+ PARSE_RULE_OPTION_TYPE(Boolean), // opt/pg-direct-io
+ PARSE_RULE_OPTION_NEGATE(true), // opt/pg-direct-io
+ PARSE_RULE_OPTION_RESET(true), // opt/pg-direct-io
+ PARSE_RULE_OPTION_REQUIRED(true), // opt/pg-direct-io
+ PARSE_RULE_OPTION_SECTION(Stanza), // opt/pg-direct-io
+ PARSE_RULE_OPTION_GROUP_ID(Pg), // opt/pg-direct-io
+ // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND_ROLE_MAIN_VALID_LIST // opt/pg-direct-io
+ ( // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND(Backup) // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND(Restore) // opt/pg-direct-io
+ ), // opt/pg-direct-io
+ // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND_ROLE_LOCAL_VALID_LIST // opt/pg-direct-io
+ ( // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND(Backup) // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND(Restore) // opt/pg-direct-io
+ ), // opt/pg-direct-io
+ // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND_ROLE_REMOTE_VALID_LIST // opt/pg-direct-io
+ ( // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND(Backup) // opt/pg-direct-io
+ PARSE_RULE_OPTION_COMMAND(Restore) // opt/pg-direct-io
+ ), // opt/pg-direct-io
+ // opt/pg-direct-io
+ PARSE_RULE_OPTIONAL // opt/pg-direct-io
+ ( // opt/pg-direct-io
+ PARSE_RULE_OPTIONAL_GROUP // opt/pg-direct-io
+ ( // opt/pg-direct-io
+ PARSE_RULE_OPTIONAL_DEFAULT // opt/pg-direct-io
+ ( // opt/pg-direct-io
+ PARSE_RULE_VAL_BOOL_FALSE, // opt/pg-direct-io
+ ), // opt/pg-direct-io
+ ), // opt/pg-direct-io
+ ), // opt/pg-direct-io
+ ), // opt/pg-direct-io
+ // -----------------------------------------------------------------------------------------------------------------------------
PARSE_RULE_OPTION // opt/pg-host
( // opt/pg-host
PARSE_RULE_OPTION_NAME("pg-host"), // opt/pg-host
@@ -10445,6 +10487,7 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
PARSE_RULE_OPTIONAL_DEPEND_DEFAULT(PARSE_RULE_VAL_BOOL_FALSE), // opt/repo-symlink
PARSE_RULE_VAL_OPT(RepoType), // opt/repo-symlink
PARSE_RULE_VAL_STRID(posix), // opt/repo-symlink
+ PARSE_RULE_VAL_STRID(posix_DS_dio), // opt/repo-symlink
), // opt/repo-symlink
// opt/repo-symlink
PARSE_RULE_OPTIONAL_DEFAULT // opt/repo-symlink
@@ -10562,6 +10605,7 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
PARSE_RULE_VAL_STRID(cifs), // opt/repo-type
PARSE_RULE_VAL_STRID(gcs), // opt/repo-type
PARSE_RULE_VAL_STRID(posix), // opt/repo-type
+ PARSE_RULE_VAL_STRID(posix_DS_dio), // opt/repo-type
PARSE_RULE_VAL_STRID(s3), // opt/repo-type
PARSE_RULE_VAL_STRID(sftp), // opt/repo-type
#ifndef HAVE_LIBSSH2 // opt/repo-type
@@ -11881,7 +11925,7 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
/***********************************************************************************************************************************
Option deprecations
***********************************************************************************************************************************/
-#define CFG_OPTION_DEPRECATE_TOTAL 39
+#define CFG_OPTION_DEPRECATE_TOTAL 41
static const ParseRuleOptionDeprecate parseRuleOptionDeprecate[CFG_OPTION_DEPRECATE_TOTAL] =
{
@@ -11983,6 +12027,19 @@ static const ParseRuleOptionDeprecate parseRuleOptionDeprecate[CFG_OPTION_DEPREC
.unindexed = true, // opt-deprecate/repo-hardlink
}, // opt-deprecate/repo-hardlink
// -----------------------------------------------------------------------------------------------------------------------------
+ { // opt-deprecate/pg-direct-io
+ .name = "pg-direct-io", // opt-deprecate/pg-direct-io
+ .id = cfgOptPgDirectIo, // opt-deprecate/pg-direct-io
+ .indexed = true, // opt-deprecate/pg-direct-io
+ }, // opt-deprecate/pg-direct-io
+ // -----------------------------------------------------------------------------------------------------------------------------
+ { // opt-deprecate/pg-direct-io
+ .name = "pg-directio", // opt-deprecate/pg-direct-io
+ .id = cfgOptPgDirectIo, // opt-deprecate/pg-direct-io
+ .indexed = true, // opt-deprecate/pg-direct-io
+ .unindexed = true, // opt-deprecate/pg-direct-io
+ }, // opt-deprecate/pg-direct-io
+ // -----------------------------------------------------------------------------------------------------------------------------
{ // opt-deprecate/repo-storage-ca-file
.name = "repo-azure-ca-file", // opt-deprecate/repo-storage-ca-file
.id = cfgOptRepoStorageCaFile, // opt-deprecate/repo-storage-ca-file
@@ -12194,6 +12251,7 @@ static const uint8_t optionResolveOrder[] =
cfgOptOutput, // opt-resolve-order
cfgOptPageHeaderCheck, // opt-resolve-order
cfgOptPg, // opt-resolve-order
+ cfgOptPgDirectIo, // opt-resolve-order
cfgOptPgLocal, // opt-resolve-order
cfgOptPgPath, // opt-resolve-order
cfgOptPgPort, // opt-resolve-order
diff --git a/src/main.c b/src/main.c
index a8e438b150..ab43ccf4c4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -46,6 +46,7 @@ Main
#include "storage/cifs/helper.h"
#include "storage/gcs/helper.h"
#include "storage/helper.h"
+#include "storage/posixDio/helper.h"
#include "storage/s3/helper.h"
#include "storage/sftp/helper.h"
#include "version.h"
@@ -63,6 +64,7 @@ main(int argListSize, const char *argList[])
STORAGE_AZURE_HELPER,
STORAGE_CIFS_HELPER,
STORAGE_GCS_HELPER,
+ STORAGE_POSIX_DIO_HELPER,
STORAGE_S3_HELPER,
#ifdef HAVE_LIBSSH2
STORAGE_SFTP_HELPER,
diff --git a/src/meson.build b/src/meson.build
index 3d01cff811..83332f3a73 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -228,6 +228,8 @@ src_pgbackrest = [
'storage/posix/read.c',
'storage/posix/storage.c',
'storage/posix/write.c',
+ 'storage/posixDio/helper.c',
+ 'storage/posixDio/storage.c',
'storage/read.c',
'storage/readMulti.c',
'storage/remote/protocol.c',
diff --git a/src/storage/cifs/storage.c b/src/storage/cifs/storage.c
index c0f4b39335..f76811e13e 100644
--- a/src/storage/cifs/storage.c
+++ b/src/storage/cifs/storage.c
@@ -24,5 +24,7 @@ storageCifsNew(
FUNCTION_LOG_END();
FUNCTION_LOG_RETURN(
- STORAGE, storagePosixNewInternal(STORAGE_CIFS_TYPE, path, modeFile, modePath, write, pathExpressionFunction, false, false));
+ STORAGE,
+ storagePosixNewInternal(
+ STORAGE_CIFS_TYPE, path, modeFile, modePath, write, pathExpressionFunction, false, false, false));
}
diff --git a/src/storage/helper.c b/src/storage/helper.c
index 7d41100175..f57b9663c4 100644
--- a/src/storage/helper.c
+++ b/src/storage/helper.c
@@ -13,6 +13,7 @@ Storage Helper
#include "protocol/helper.h"
#include "storage/helper.h"
#include "storage/posix/storage.h"
+#include "storage/posix/storage.intern.h"
#include "storage/remote/storage.h"
/***********************************************************************************************************************************
@@ -198,7 +199,18 @@ storagePgGet(const unsigned int pgIdx, const bool write)
}
// Use Posix storage
else
- result = storagePosixNewP(cfgOptionIdxStr(cfgOptPgPath, pgIdx), .write = write);
+ {
+ const bool directIo = cfgOptionValid(cfgOptPgDirectIo) && cfgOptionIdxBool(cfgOptPgDirectIo, pgIdx);
+
+ if (directIo)
+ {
+ result = storagePosixNewInternal(
+ STORAGE_POSIX_TYPE, cfgOptionIdxStr(cfgOptPgPath, pgIdx), STORAGE_MODE_FILE_DEFAULT,
+ STORAGE_MODE_PATH_DEFAULT, write, NULL, true, true, true);
+ }
+ else
+ result = storagePosixNewP(cfgOptionIdxStr(cfgOptPgPath, pgIdx), .write = write);
+ }
FUNCTION_TEST_RETURN(STORAGE, result);
}
diff --git a/src/storage/posix/read.c b/src/storage/posix/read.c
index 51cd1120bf..d82fcb212f 100644
--- a/src/storage/posix/read.c
+++ b/src/storage/posix/read.c
@@ -4,13 +4,16 @@ Posix Storage Read
#include <build.h>
#include <fcntl.h>
+#include <stdlib.h>
#include <unistd.h>
#include "common/debug.h"
+#include "common/io/io.h"
#include "common/io/read.h"
#include "common/log.h"
#include "common/type/object.h"
#include "storage/posix/read.h"
+#include "storage/posixDio/storage.h"
#include "storage/read.h"
/***********************************************************************************************************************************
@@ -18,16 +21,22 @@ Object types
***********************************************************************************************************************************/
struct StorageReadPosix
{
- const StorageReadInterface *interface; // Interface
- StoragePosix *storage; // Storage that created this object
+ const StorageReadInterface *interface; // Interface
+ StoragePosix *storage; // Storage that created this object
- const String *name; // File name
- uint64_t offset; // Read offset
- const Variant *limit; // Read limit (NULL for no limit)
+ const String *name; // File name
+ uint64_t offset; // Read offset
+ const Variant *limit; // Read limit (NULL for no limit)
- int fd; // File descriptor
- uint64_t current; // Current bytes read from file
+ int fd; // File descriptor
+ uint64_t current; // Current bytes read from file
bool eof;
+
+ bool directIo; // Use direct I/O
+ uint8_t *dioBuffer; // Aligned bounce buffer for direct I/O
+ size_t dioBufferSize; // Size of aligned buffer
+ size_t dioBufferUsed; // Valid bytes currently in buffer
+ size_t dioBufferPos; // Current read position in buffer
};
/***********************************************************************************************************************************
@@ -44,6 +53,12 @@ storageReadPosixFreeResource(THIS_VOID)
ASSERT(this != NULL);
+ if (this->dioBuffer != NULL)
+ {
+ free(this->dioBuffer);
+ this->dioBuffer = NULL;
+ }
+
if (this->fd != -1)
THROW_ON_SYS_ERROR_FMT(close(this->fd) == -1, FileCloseError, STORAGE_ERROR_READ_CLOSE, strZ(this->name));
@@ -66,13 +81,30 @@ storageReadPosixOpen(THIS_VOID)
ASSERT(this->fd == -1);
// Open the file
- this->fd = open(strZ(this->name), O_RDONLY, 0);
+ int flags = O_RDONLY;
+
+#ifdef O_DIRECT
+ if (this->directIo)
+ flags |= O_DIRECT;
+#endif
+
+ this->fd = open(strZ(this->name), flags, 0);
// Handle errors except missing, which is reported to the caller via the result
if (this->fd == -1)
{
- if (errno != ENOENT) // {vm_covered}
- THROW_SYS_ERROR_FMT(FileOpenError, STORAGE_ERROR_READ_OPEN, strZ(this->name)); // {vm_covered}
+ if (errno != ENOENT)
+ {
+#ifdef O_DIRECT
+ if (this->directIo && errno == EINVAL)
+ {
+ THROW_SYS_ERROR_FMT(
+ FeatureNotSupportedError, "unable to open '%s' with direct I/O: filesystem does not support direct I/O",
+ strZ(this->name));
+ }
+#endif
+ THROW_SYS_ERROR_FMT(FileOpenError, STORAGE_ERROR_READ_OPEN, strZ(this->name));
+ }
}
// Else success
else
@@ -80,8 +112,53 @@ storageReadPosixOpen(THIS_VOID)
// Set free callback to ensure the file descriptor is freed
memContextCallbackSet(objMemContext(this), storageReadPosixFreeResource, this);
- // Seek to offset
- if (this->offset != 0)
+ if (this->directIo)
+ {
+ this->dioBufferSize = ((ioBufferSize() + STORAGE_POSIX_DIO_ALIGN - 1) / STORAGE_POSIX_DIO_ALIGN) *
+ STORAGE_POSIX_DIO_ALIGN;
+
+ if (this->dioBufferSize < STORAGE_POSIX_DIO_ALIGN)
+ this->dioBufferSize = STORAGE_POSIX_DIO_ALIGN;
+
+ if (posix_memalign((void **)&this->dioBuffer, STORAGE_POSIX_DIO_ALIGN, this->dioBufferSize) != 0)
+ THROW_SYS_ERROR_FMT(MemoryError, "unable to allocate direct I/O buffer for '%s'", strZ(this->name));
+
+ // Initialize buffer tracking metrics to empty
+ this->dioBufferUsed = 0;
+ this->dioBufferPos = 0;
+
+ // Seek to offset
+ if (this->offset != 0)
+ {
+ const uint64_t alignedOffset = (this->offset / STORAGE_POSIX_DIO_ALIGN) * STORAGE_POSIX_DIO_ALIGN;
+ const size_t discard = (size_t)(this->offset - alignedOffset);
+
+ THROW_ON_SYS_ERROR_FMT(
+ lseek(this->fd, (off_t)alignedOffset, SEEK_SET) == -1, FileOpenError, STORAGE_ERROR_READ_SEEK,
+ alignedOffset, strZ(this->name));
+
+ if (discard > 0)
+ {
+ const ssize_t bytes = read(this->fd, this->dioBuffer, STORAGE_POSIX_DIO_ALIGN);
+
+ if (bytes == -1)
+ THROW_SYS_ERROR_FMT(FileReadError, "unable to read '%s'", strZ(this->name));
+
+ this->dioBufferUsed = (size_t)bytes;
+
+ if ((size_t)bytes <= discard)
+ {
+ this->dioBufferPos = (size_t)bytes;
+ this->eof = true;
+ }
+ else
+ {
+ this->dioBufferPos = discard;
+ }
+ }
+ }
+ }
+ else if (this->offset != 0)
{
THROW_ON_SYS_ERROR_FMT(
lseek(this->fd, (off_t)this->offset, SEEK_SET) == -1, FileOpenError, STORAGE_ERROR_READ_SEEK, this->offset,
@@ -103,7 +180,7 @@ storageReadPosix(THIS_VOID, Buffer *const buffer, const bool block)
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(STORAGE_READ_POSIX, this);
FUNCTION_LOG_PARAM(BUFFER, buffer);
- (void)block; // Blocking is handled by the caller
+ (void)block; // Blocking is handled by the caller
FUNCTION_LOG_END();
ASSERT(this != NULL && this->fd != -1);
@@ -114,31 +191,91 @@ storageReadPosix(THIS_VOID, Buffer *const buffer, const bool block)
if (!this->eof)
{
- // Rather than enable/disable limit checking just use a big number when there is no limit. We can feel pretty confident that
- // no files will be > UINT64_MAX in size.
const uint64_t limit = this->limit == NULL ? UINT64_MAX : varUInt64(this->limit);
- // Determine expected bytes to read. If remaining size in the buffer would exceed the limit then reduce the expected read.
- size_t expectedBytes = bufRemains(buffer);
+ if (!this->directIo)
+ {
+ size_t expectedBytes = bufRemains(buffer);
- if (this->current + expectedBytes > limit)
- expectedBytes = (size_t)(limit - this->current);
+ if (this->current + expectedBytes > limit)
+ expectedBytes = (size_t)(limit - this->current);
- // Read from file
- actualBytes = read(this->fd, bufRemainsPtr(buffer), expectedBytes);
+ actualBytes = read(this->fd, bufRemainsPtr(buffer), expectedBytes);
- // Error occurred during read
- if (actualBytes == -1)
- THROW_SYS_ERROR_FMT(FileReadError, "unable to read '%s'", strZ(this->name));
+ if (actualBytes == -1)
+ THROW_SYS_ERROR_FMT(FileReadError, "unable to read '%s'", strZ(this->name));
- // Update amount of buffer used
- bufUsedInc(buffer, (size_t)actualBytes);
- this->current += (uint64_t)actualBytes;
+ bufUsedInc(buffer, (size_t)actualBytes);
+ this->current += (uint64_t)actualBytes;
- // If less data than expected was read or the limit has been reached then EOF. The file may not actually be EOF but we are
- // not concerned with files that are growing. Just read up to the point where the file is being extended.
- if ((size_t)actualBytes != expectedBytes || this->current == limit)
- this->eof = true;
+ if ((size_t)actualBytes != expectedBytes || this->current == limit)
+ this->eof = true;
+ }
+ else
+ {
+ // The destination buffer could be full or size limit hit right from the start
+ while (bufRemains(buffer) > 0 && this->current < limit)
+ {
+ // 1. If there is data in the cache buffer, drain it out systematically
+ if (this->dioBufferPos < this->dioBufferUsed)
+ {
+ const size_t bytesAvailable = this->dioBufferUsed - this->dioBufferPos;
+ size_t bytesToCopy = bufRemains(buffer);
+
+ if (this->current + bytesToCopy > limit)
+ bytesToCopy = (size_t)(limit - this->current);
+
+ if (bytesToCopy > bytesAvailable)
+ bytesToCopy = bytesAvailable;
+
+ memcpy(bufRemainsPtr(buffer), this->dioBuffer + this->dioBufferPos, bytesToCopy);
+ bufUsedInc(buffer, bytesToCopy);
+ this->dioBufferPos += bytesToCopy;
+ this->current += (uint64_t)bytesToCopy;
+ actualBytes += (ssize_t)bytesToCopy;
+
+ if (this->current == limit)
+ this->eof = true;
+
+ continue;
+ }
+
+ // If our destination buffer is now filled up, exit the loop
+ // immediately BEFORE executing another read operation on disk.
+ if (bufRemains(buffer) == 0)
+ break;
+
+ // If the underlying file already hit EOF and our cache buffer is completely dry, exit
+ if (this->eof)
+ break;
+
+ // Cache buffer is completely empty. Fetch the next aligned block.
+ this->dioBufferPos = 0;
+ this->dioBufferUsed = 0;
+
+ const ssize_t bytesRead = read(this->fd, this->dioBuffer, this->dioBufferSize);
+
+ if (bytesRead == -1)
+ THROW_SYS_ERROR_FMT(FileReadError, "unable to read '%s'", strZ(this->name));
+
+ if (bytesRead == 0)
+ {
+ this->eof = true;
+ break;
+ }
+
+ this->dioBufferUsed = (size_t)bytesRead;
+
+ // If less data than the buffer configuration size was returned, the file on disk is finished.
+ // We mark eof = true, but do NOT break, allowing the next loop iteration to copy the data.
+ if ((size_t)bytesRead < this->dioBufferSize)
+ this->eof = true;
+ }
+
+ // If the limit or physical file end is reached, ensure eof state matches
+ if (this->current == limit)
+ this->eof = true;
+ }
}
FUNCTION_LOG_RETURN(SIZE, (size_t)actualBytes);
@@ -210,27 +347,30 @@ static const StorageReadInterface storageReadPosixInterface =
};
FN_EXTERN StorageReadPosix *
-storageReadPosixNew(StoragePosix *const storage, const String *const name, const uint64_t offset, const Variant *const limit)
+storageReadPosixNew(
+ StoragePosix *const storage, const String *const name, const uint64_t offset, const Variant *const limit,
+ const bool directIo)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(STORAGE_POSIX, storage);
FUNCTION_LOG_PARAM(STRING, name);
FUNCTION_LOG_PARAM(UINT64, offset);
FUNCTION_LOG_PARAM(VARIANT, limit);
+ FUNCTION_LOG_PARAM(BOOL, directIo);
FUNCTION_LOG_END();
ASSERT(name != NULL);
OBJ_NEW_BEGIN(StorageReadPosix, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
- *this = (StorageReadPosix)
- {
+ *this = (StorageReadPosix){
.interface = &storageReadPosixInterface,
.storage = storage,
.name = strDup(name),
.offset = offset,
.limit = varDup(limit),
.fd = -1,
+ .directIo = directIo,
};
}
OBJ_NEW_END();
diff --git a/src/storage/posix/read.h b/src/storage/posix/read.h
index afd5b706b8..feb2d440e0 100644
--- a/src/storage/posix/read.h
+++ b/src/storage/posix/read.h
@@ -15,7 +15,8 @@ typedef struct StorageReadPosix StorageReadPosix;
/***********************************************************************************************************************************
Constructors
***********************************************************************************************************************************/
-FN_EXTERN StorageReadPosix *storageReadPosixNew(StoragePosix *storage, const String *name, uint64_t offset, const Variant *limit);
+FN_EXTERN StorageReadPosix *storageReadPosixNew(
+ StoragePosix *storage, const String *name, uint64_t offset, const Variant *limit, bool directIo);
/***********************************************************************************************************************************
Macros for function logging
diff --git a/src/storage/posix/storage.c b/src/storage/posix/storage.c
index b9a535cef8..62be1b4412 100644
--- a/src/storage/posix/storage.c
+++ b/src/storage/posix/storage.c
@@ -19,6 +19,7 @@ Posix Storage
#include "storage/posix/read.h"
#include "storage/posix/storage.intern.h"
#include "storage/posix/write.h"
+#include "storage/posixDio/storage.h"
/***********************************************************************************************************************************
Define PATH_MAX if it is not defined
@@ -33,6 +34,7 @@ Object type
struct StoragePosix
{
STORAGE_COMMON_MEMBER;
+ bool directIo;
};
/**********************************************************************************************************************************/
@@ -358,7 +360,7 @@ storagePosixNewRead(THIS_VOID, const String *const file, const StorageInterfaceN
ASSERT(file != NULL);
ASSERT(param.versionId == NULL);
- FUNCTION_LOG_RETURN(STORAGE_READ_POSIX, storageReadPosixNew(this, file, param.offset, param.limit));
+ FUNCTION_LOG_RETURN(STORAGE_READ_POSIX, storageReadPosixNew(this, file, param.offset, param.limit, this->directIo));
}
/**********************************************************************************************************************************/
@@ -389,7 +391,8 @@ storagePosixNewWrite(THIS_VOID, const String *const file, const StorageInterface
STORAGE_WRITE_POSIX,
storageWritePosixNew(
this, file, param.modeFile, param.modePath, param.user, param.group, param.timeModified, param.createPath,
- param.syncFile, this->interface.pathSync != NULL ? param.syncPath : false, param.atomic, param.truncate));
+ param.syncFile, this->interface.pathSync != NULL ? param.syncPath : false, param.atomic, param.truncate,
+ this->directIo));
}
/**********************************************************************************************************************************/
@@ -592,7 +595,7 @@ static const StorageInterface storageInterfacePosix =
FN_EXTERN Storage *
storagePosixNewInternal(
const StringId type, const String *const path, const mode_t modeFile, const mode_t modePath, const bool write,
- StoragePathExpressionCallback pathExpressionFunction, const bool pathSync, const bool symLink)
+ StoragePathExpressionCallback pathExpressionFunction, const bool pathSync, const bool symLink, const bool directIo)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(STRING_ID, type);
@@ -603,6 +606,7 @@ storagePosixNewInternal(
FUNCTION_LOG_PARAM(FUNCTIONP, pathExpressionFunction);
FUNCTION_LOG_PARAM(BOOL, pathSync);
FUNCTION_LOG_PARAM(BOOL, symLink);
+ FUNCTION_LOG_PARAM(BOOL, directIo);
FUNCTION_LOG_END();
ASSERT(type != 0);
@@ -619,6 +623,7 @@ storagePosixNewInternal(
*this = (StoragePosix)
{
.interface = storageInterfacePosix,
+ .directIo = directIo,
};
// Disable path sync when not supported
@@ -626,7 +631,7 @@ storagePosixNewInternal(
this->interface.pathSync = NULL;
// If this is a posix driver then add link features
- if (type == STORAGE_POSIX_TYPE)
+ if (type == STORAGE_POSIX_TYPE || type == STORAGE_POSIX_DIO_TYPE)
{
this->interface.feature |=
1 << storageFeatureHardLink | (unsigned int)symLink << storageFeatureSymLink | 1 << storageFeaturePathSync |
@@ -656,5 +661,5 @@ storagePosixNew(const String *const path, const StoragePosixNewParam param)
storagePosixNewInternal(
STORAGE_POSIX_TYPE, path, param.modeFile == 0 ? STORAGE_MODE_FILE_DEFAULT : param.modeFile,
param.modePath == 0 ? STORAGE_MODE_PATH_DEFAULT : param.modePath, param.write, param.pathExpressionFunction, true,
- !param.noSymLink));
+ !param.noSymLink, false));
}
diff --git a/src/storage/posix/storage.intern.h b/src/storage/posix/storage.intern.h
index cb235f5aa1..8024e7b505 100644
--- a/src/storage/posix/storage.intern.h
+++ b/src/storage/posix/storage.intern.h
@@ -16,7 +16,7 @@ Constructors
***********************************************************************************************************************************/
FN_EXTERN Storage *storagePosixNewInternal(
StringId type, const String *path, mode_t modeFile, mode_t modePath, bool write,
- StoragePathExpressionCallback pathExpressionFunction, bool pathSync, bool symLink);
+ StoragePathExpressionCallback pathExpressionFunction, bool pathSync, bool symLink, bool directIo);
/***********************************************************************************************************************************
Macros for function logging
diff --git a/src/storage/posix/write.c b/src/storage/posix/write.c
index d988aca892..2009f0d382 100644
--- a/src/storage/posix/write.c
+++ b/src/storage/posix/write.c
@@ -5,15 +5,18 @@ Posix Storage File write
#include <fcntl.h>
#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
#include <utime.h>
#include "common/debug.h"
+#include "common/io/io.h"
#include "common/io/write.h"
#include "common/log.h"
#include "common/type/object.h"
#include "common/user.h"
#include "storage/posix/write.h"
+#include "storage/posixDio/storage.h"
#include "storage/write.h"
/***********************************************************************************************************************************
@@ -21,24 +24,30 @@ Object type
***********************************************************************************************************************************/
struct StorageWritePosix
{
- const StorageWriteInterface *interface; // Interface
- StoragePosix *storage; // Storage that created this object
-
- const String *name; // File name
- mode_t modeFile; // File mode
- mode_t modePath; // Path mode
- bool createPath; // Create path if missing
- bool atomic; // Atomic write
- bool truncate; // Truncate existing file
- bool syncPath; // Sync path after write
- bool syncFile; // Sync file after write
- const String *user; // User owner
- const String *group; // Group owner
- time_t timeModified; // Modified time
+ const StorageWriteInterface *interface; // Interface
+ StoragePosix *storage; // Storage that created this object
+
+ const String *name; // File name
+ mode_t modeFile; // File mode
+ mode_t modePath; // Path mode
+ bool createPath; // Create path if missing
+ bool atomic; // Atomic write
+ bool truncate; // Truncate existing file
+ bool syncPath; // Sync path after write
+ bool syncFile; // Sync file after write
+ const String *user; // User owner
+ const String *group; // Group owner
+ time_t timeModified; // Modified time
const String *nameTmp;
const String *path;
- int fd; // File descriptor
+ int fd; // File descriptor
+
+ bool directIo; // Use direct I/O
+ uint8_t *dioBuffer; // Aligned bounce buffer for direct I/O
+ size_t dioBufferSize; // Size of aligned buffer
+ size_t dioBufferUsed; // Valid bytes currently in buffer
+ uint64_t totalWritten; // Total unpadded bytes written
};
/***********************************************************************************************************************************
@@ -46,7 +55,7 @@ File open constants
Since open is called more than once use constants to make sure these parameters are always the same
***********************************************************************************************************************************/
-#define FILE_OPEN_PURPOSE "write"
+#define FILE_OPEN_PURPOSE "write"
/***********************************************************************************************************************************
Close file descriptor
@@ -62,7 +71,14 @@ storageWritePosixFreeResource(THIS_VOID)
ASSERT(this != NULL);
- THROW_ON_SYS_ERROR_FMT(close(this->fd) == -1, FileCloseError, STORAGE_ERROR_WRITE_CLOSE, strZ(this->nameTmp));
+ if (this->dioBuffer != NULL)
+ {
+ free(this->dioBuffer);
+ this->dioBuffer = NULL;
+ }
+
+ if (this->fd != -1)
+ THROW_ON_SYS_ERROR_FMT(close(this->fd) == -1, FileCloseError, STORAGE_ERROR_WRITE_CLOSE, strZ(this->nameTmp));
FUNCTION_LOG_RETURN_VOID();
}
@@ -95,12 +111,17 @@ storageWritePosixOpen(THIS_VOID)
ASSERT(this->fd == -1);
// Open the file
- const int flags = O_CREAT | O_WRONLY | (this->truncate ? O_TRUNC : 0);
+ int flags = O_CREAT | O_WRONLY | (this->truncate ? O_TRUNC : 0);
+
+#ifdef O_DIRECT
+ if (this->directIo)
+ flags |= O_DIRECT;
+#endif
this->fd = open(strZ(this->nameTmp), flags, this->modeFile);
// Attempt to create the path if it is missing
- if (this->fd == -1 && errno == ENOENT && this->createPath) // {vm_covered}
+ if (this->fd == -1 && errno == ENOENT && this->createPath) // {vm_covered}
{
// Create the path
storageInterfacePathCreateP(this->storage, this->path, false, false, this->modePath);
@@ -112,15 +133,37 @@ storageWritePosixOpen(THIS_VOID)
// Handle errors
if (this->fd == -1)
{
- if (errno == ENOENT) // {vm_covered}
+ if (errno == ENOENT) // {vm_covered}
THROW_FMT(FileMissingError, STORAGE_ERROR_WRITE_MISSING, strZ(this->name));
else
- THROW_SYS_ERROR_FMT(FileOpenError, STORAGE_ERROR_WRITE_OPEN, strZ(this->name)); // {vm_covered}
+ {
+#ifdef O_DIRECT
+ if (this->directIo && errno == EINVAL)
+ {
+ THROW_SYS_ERROR_FMT(
+ FeatureNotSupportedError, "unable to open '%s' with direct I/O: filesystem does not support direct I/O",
+ strZ(this->nameTmp));
+ }
+#endif
+ THROW_SYS_ERROR_FMT(FileOpenError, STORAGE_ERROR_WRITE_OPEN, strZ(this->name)); // {vm_covered}
+ }
}
// Set free callback to ensure the file descriptor is freed
memContextCallbackSet(objMemContext(this), storageWritePosixFreeResource, this);
+ if (this->directIo)
+ {
+ this->dioBufferSize = ((ioBufferSize() + STORAGE_POSIX_DIO_ALIGN - 1) / STORAGE_POSIX_DIO_ALIGN) *
+ STORAGE_POSIX_DIO_ALIGN;
+
+ if (this->dioBufferSize < STORAGE_POSIX_DIO_ALIGN)
+ this->dioBufferSize = STORAGE_POSIX_DIO_ALIGN;
+
+ if (posix_memalign((void **)&this->dioBuffer, STORAGE_POSIX_DIO_ALIGN, this->dioBufferSize) != 0)
+ THROW_SYS_ERROR_FMT(MemoryError, "unable to allocate direct I/O buffer for '%s'", strZ(this->nameTmp));
+ }
+
// Update user/group owner
if (this->user != NULL || this->group != NULL)
{
@@ -175,9 +218,49 @@ storageWritePosix(THIS_VOID, const Buffer *const buffer)
ASSERT(buffer != NULL);
ASSERT(this->fd != -1);
- // Write the data
- if (write(this->fd, bufPtrConst(buffer), bufUsed(buffer)) != (ssize_t)bufUsed(buffer))
- THROW_SYS_ERROR_FMT(FileWriteError, "unable to write '%s'", strZ(this->nameTmp));
+ if (!this->directIo)
+ {
+ // Write the data
+ if (write(this->fd, bufPtrConst(buffer), bufUsed(buffer)) != (ssize_t)bufUsed(buffer))
+ THROW_SYS_ERROR_FMT(FileWriteError, "unable to write '%s'", strZ(this->nameTmp));
+ }
+ else
+ {
+ size_t bufferPos = 0;
+ const size_t bufferRemaining = bufUsed(buffer);
+ const uint8_t *const bufferPtr = bufPtrConst(buffer);
+
+ while (bufferPos < bufferRemaining)
+ {
+ size_t copyBytes = bufferRemaining - bufferPos;
+ size_t spaceAvailable = this->dioBufferSize - this->dioBufferUsed;
+
+ if (copyBytes > spaceAvailable)
+ copyBytes = spaceAvailable;
+
+ memcpy(this->dioBuffer + this->dioBufferUsed, bufferPtr + bufferPos, copyBytes);
+ this->dioBufferUsed += copyBytes;
+ bufferPos += copyBytes;
+
+ // If dioBuffer has full blocks to write
+ if (this->dioBufferUsed >= this->dioBufferSize)
+ {
+ const size_t bytesToWrite = (this->dioBufferUsed / STORAGE_POSIX_DIO_ALIGN) * STORAGE_POSIX_DIO_ALIGN;
+
+ if (write(this->fd, this->dioBuffer, bytesToWrite) != (ssize_t)bytesToWrite)
+ THROW_SYS_ERROR_FMT(FileWriteError, "unable to write '%s'", strZ(this->nameTmp));
+
+ this->totalWritten += bytesToWrite;
+
+ const size_t remainder = this->dioBufferUsed - bytesToWrite;
+
+ if (remainder > 0)
+ memmove(this->dioBuffer, this->dioBuffer + bytesToWrite, remainder);
+
+ this->dioBufferUsed = remainder;
+ }
+ }
+ }
FUNCTION_LOG_RETURN_VOID();
}
@@ -198,8 +281,27 @@ storageWritePosixSeek(THIS_VOID, const uint64_t position)
ASSERT(this != NULL);
ASSERT(this->fd != -1);
+ if (this->directIo)
+ {
+ if (this->dioBufferUsed > 0)
+ {
+ const size_t writeSize = ((this->dioBufferUsed + STORAGE_POSIX_DIO_ALIGN - 1) / STORAGE_POSIX_DIO_ALIGN) *
+ STORAGE_POSIX_DIO_ALIGN;
+ memset(this->dioBuffer + this->dioBufferUsed, 0, writeSize - this->dioBufferUsed);
+
+ if (write(this->fd, this->dioBuffer, writeSize) != (ssize_t)writeSize)
+ THROW_SYS_ERROR_FMT(FileWriteError, "unable to write '%s'", strZ(this->nameTmp));
+
+ this->totalWritten += this->dioBufferUsed;
+ this->dioBufferUsed = 0;
+ }
+
+ this->totalWritten = position;
+ }
+
THROW_ON_SYS_ERROR_FMT(
- lseek(this->fd, (off_t)position, SEEK_SET) == -1, FileWriteError, STORAGE_ERROR_WRITE_SEEK, position, strZ(this->nameTmp));
+ lseek(this->fd, (off_t)position, SEEK_SET) == -1, FileWriteError, STORAGE_ERROR_WRITE_SEEK, position,
+ strZ(this->nameTmp));
FUNCTION_LOG_RETURN_VOID();
}
@@ -221,6 +323,29 @@ storageWritePosixClose(THIS_VOID)
// Close if the file has not already been closed
if (this->fd != -1)
{
+ if (this->directIo && this->dioBufferUsed > 0)
+ {
+ const size_t writeSize = ((this->dioBufferUsed + STORAGE_POSIX_DIO_ALIGN - 1) / STORAGE_POSIX_DIO_ALIGN) *
+ STORAGE_POSIX_DIO_ALIGN;
+ memset(this->dioBuffer + this->dioBufferUsed, 0, writeSize - this->dioBufferUsed);
+
+ if (write(this->fd, this->dioBuffer, writeSize) != (ssize_t)writeSize)
+ THROW_SYS_ERROR_FMT(FileWriteError, "unable to write '%s'", strZ(this->nameTmp));
+
+ this->totalWritten += this->dioBufferUsed;
+ this->dioBufferUsed = 0;
+
+ THROW_ON_SYS_ERROR_FMT(
+ ftruncate(this->fd, (off_t)this->totalWritten) == -1, FileWriteError, "unable to truncate '%s'",
+ strZ(this->nameTmp));
+ }
+
+ if (this->dioBuffer != NULL)
+ {
+ free(this->dioBuffer);
+ this->dioBuffer = NULL;
+ }
+
// Sync the file
if (this->syncFile)
THROW_ON_SYS_ERROR_FMT(fsync(this->fd) == -1, FileSyncError, STORAGE_ERROR_WRITE_SYNC, strZ(this->nameTmp));
@@ -286,7 +411,7 @@ FN_EXTERN StorageWritePosix *
storageWritePosixNew(
StoragePosix *const storage, const String *const name, const mode_t modeFile, const mode_t modePath, const String *const user,
const String *const group, const time_t timeModified, const bool createPath, const bool syncFile, const bool syncPath,
- const bool atomic, const bool truncate)
+ const bool atomic, const bool truncate, const bool directIo)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(STORAGE_POSIX, storage);
@@ -301,6 +426,7 @@ storageWritePosixNew(
FUNCTION_LOG_PARAM(BOOL, syncPath);
FUNCTION_LOG_PARAM(BOOL, atomic);
FUNCTION_LOG_PARAM(BOOL, truncate);
+ FUNCTION_LOG_PARAM(BOOL, directIo);
FUNCTION_LOG_END();
ASSERT(storage != NULL);
@@ -310,8 +436,7 @@ storageWritePosixNew(
OBJ_NEW_BEGIN(StorageWritePosix, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
- *this = (StorageWritePosix)
- {
+ *this = (StorageWritePosix){
.interface = &storageWritePosixInterface,
.storage = storage,
.name = strDup(name),
@@ -328,6 +453,7 @@ storageWritePosixNew(
.nameTmp = atomic ? strNewFmt("%s." STORAGE_FILE_TEMP_EXT, strZ(name)) : strDup(name),
.path = strPath(name),
.fd = -1,
+ .directIo = directIo,
};
}
OBJ_NEW_END();
diff --git a/src/storage/posix/write.h b/src/storage/posix/write.h
index a4fc6d2b01..94a10c88cc 100644
--- a/src/storage/posix/write.h
+++ b/src/storage/posix/write.h
@@ -17,7 +17,7 @@ Constructors
***********************************************************************************************************************************/
FN_EXTERN StorageWritePosix *storageWritePosixNew(
StoragePosix *storage, const String *name, mode_t modeFile, mode_t modePath, const String *user, const String *group,
- time_t timeModified, bool createPath, bool syncFile, bool syncPath, bool atomic, bool truncate);
+ time_t timeModified, bool createPath, bool syncFile, bool syncPath, bool atomic, bool truncate, bool directIo);
/***********************************************************************************************************************************
Macros for function logging
diff --git a/src/storage/posixDio/helper.c b/src/storage/posixDio/helper.c
new file mode 100644
index 0000000000..a264b08feb
--- /dev/null
+++ b/src/storage/posixDio/helper.c
@@ -0,0 +1,28 @@
+/***********************************************************************************************************************************
+Posix Direct I/O Storage Helper
+***********************************************************************************************************************************/
+#include <build.h>
+
+#include "common/debug.h"
+#include "common/log.h"
+#include "config/config.h"
+#include "storage/posixDio/helper.h"
+
+/**********************************************************************************************************************************/
+FN_EXTERN Storage *
+storagePosixDioHelper(const unsigned int repoIdx, const bool write, StoragePathExpressionCallback pathExpressionCallback)
+{
+ FUNCTION_LOG_BEGIN(logLevelDebug);
+ FUNCTION_LOG_PARAM(UINT, repoIdx);
+ FUNCTION_LOG_PARAM(BOOL, write);
+ FUNCTION_LOG_PARAM_P(VOID, pathExpressionCallback);
+ FUNCTION_LOG_END();
+
+ ASSERT(cfgOptionIdxStrId(cfgOptRepoType, repoIdx) == STORAGE_POSIX_DIO_TYPE);
+
+ Storage *const result = storagePosixDioNew(
+ cfgOptionIdxStr(cfgOptRepoPath, repoIdx), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, write,
+ pathExpressionCallback);
+
+ FUNCTION_LOG_RETURN(STORAGE, result);
+}
diff --git a/src/storage/posixDio/helper.h b/src/storage/posixDio/helper.h
new file mode 100644
index 0000000000..dbb7db6150
--- /dev/null
+++ b/src/storage/posixDio/helper.h
@@ -0,0 +1,19 @@
+/***********************************************************************************************************************************
+Posix Direct I/O Storage Helper
+***********************************************************************************************************************************/
+#ifndef STORAGE_POSIX_DIO_STORAGE_HELPER_H
+#define STORAGE_POSIX_DIO_STORAGE_HELPER_H
+
+#include "storage/posixDio/storage.h"
+
+/***********************************************************************************************************************************
+Functions
+***********************************************************************************************************************************/
+FN_EXTERN Storage *storagePosixDioHelper(unsigned int repoIdx, bool write, StoragePathExpressionCallback pathExpressionCallback);
+
+/***********************************************************************************************************************************
+Storage helper for StorageHelper array passed to storageHelperInit()
+***********************************************************************************************************************************/
+#define STORAGE_POSIX_DIO_HELPER {.type = STORAGE_POSIX_DIO_TYPE, .helper = storagePosixDioHelper}
+
+#endif
diff --git a/src/storage/posixDio/storage.c b/src/storage/posixDio/storage.c
new file mode 100644
index 0000000000..3f40947e2e
--- /dev/null
+++ b/src/storage/posixDio/storage.c
@@ -0,0 +1,30 @@
+/***********************************************************************************************************************************
+Posix Direct I/O Storage
+***********************************************************************************************************************************/
+#include <build.h>
+
+#include "common/debug.h"
+#include "common/log.h"
+#include "common/regExp.h"
+#include "storage/posix/storage.intern.h"
+#include "storage/posixDio/storage.h"
+
+/**********************************************************************************************************************************/
+FN_EXTERN Storage *
+storagePosixDioNew(
+ const String *const path, const mode_t modeFile, const mode_t modePath, const bool write,
+ StoragePathExpressionCallback pathExpressionFunction)
+{
+ FUNCTION_LOG_BEGIN(logLevelDebug);
+ FUNCTION_LOG_PARAM(STRING, path);
+ FUNCTION_LOG_PARAM(MODE, modeFile);
+ FUNCTION_LOG_PARAM(MODE, modePath);
+ FUNCTION_LOG_PARAM(BOOL, write);
+ FUNCTION_LOG_PARAM(FUNCTIONP, pathExpressionFunction);
+ FUNCTION_LOG_END();
+
+ FUNCTION_LOG_RETURN(
+ STORAGE,
+ storagePosixNewInternal(
+ STORAGE_POSIX_DIO_TYPE, path, modeFile, modePath, write, pathExpressionFunction, true, true, true));
+}
diff --git a/src/storage/posixDio/storage.h b/src/storage/posixDio/storage.h
new file mode 100644
index 0000000000..b12ffb9870
--- /dev/null
+++ b/src/storage/posixDio/storage.h
@@ -0,0 +1,31 @@
+/***********************************************************************************************************************************
+Posix Direct I/O Storage
+***********************************************************************************************************************************/
+#ifndef STORAGE_POSIX_DIO_STORAGE_H
+#define STORAGE_POSIX_DIO_STORAGE_H
+
+#include "storage/storage.h"
+
+/***********************************************************************************************************************************
+Storage type
+***********************************************************************************************************************************/
+#define STORAGE_POSIX_DIO_TYPE STRID5("posix-dio", 0xf493784cdf00)
+
+/***********************************************************************************************************************************
+Direct I/O alignment requirement in bytes. Buffers and transfer sizes must be a multiple of this value.
+***********************************************************************************************************************************/
+#define STORAGE_POSIX_DIO_ALIGN 4096
+
+#ifndef O_DIRECT
+#ifdef __O_DIRECT
+#define O_DIRECT __O_DIRECT
+#endif
+#endif
+
+/***********************************************************************************************************************************
+Constructors
+***********************************************************************************************************************************/
+FN_EXTERN Storage *storagePosixDioNew(
+ const String *path, mode_t modeFile, mode_t modePath, bool write, StoragePathExpressionCallback pathExpressionFunction);
+
+#endif
diff --git a/test/define.yaml b/test/define.yaml
index b18e56235a..0bca09adf7 100644
--- a/test/define.yaml
+++ b/test/define.yaml
@@ -551,7 +551,7 @@ unit:
# --------------------------------------------------------------------------------------------------------------------------------
- name: storage/posix
- total: 24
+ total: 25
coverage:
- storage/cifs/helper
@@ -559,6 +559,8 @@ unit:
- storage/posix/read
- storage/posix/storage
- storage/posix/write
+ - storage/posixDio/helper
+ - storage/posixDio/storage
- storage/helper
- storage/iterator
- storage/list
diff --git a/test/src/harness/storageHelper.c b/test/src/harness/storageHelper.c
index fbb3a47871..861bdb31e6 100644
--- a/test/src/harness/storageHelper.c
+++ b/test/src/harness/storageHelper.c
@@ -204,13 +204,14 @@ static const StorageReadInterface hrnStorageReadTestInterface =
static HrnStorageReadTest *
hrnStorageReadTestNew(
StoragePosix *const storage, const String *name, const uint64_t offset, const Variant *const limit,
- const String *const versionId)
+ const String *const versionId, const bool directio)
{
FUNCTION_HARNESS_BEGIN();
FUNCTION_HARNESS_PARAM(STRING, name);
FUNCTION_HARNESS_PARAM(UINT64, offset);
FUNCTION_HARNESS_PARAM(VARIANT, limit);
FUNCTION_HARNESS_PARAM(STRING, versionId);
+ FUNCTION_HARNESS_PARAM(BOOL, directio);
FUNCTION_HARNESS_END();
ASSERT(name != NULL);
@@ -224,7 +225,7 @@ hrnStorageReadTestNew(
*this = (HrnStorageReadTest)
{
.interface = &hrnStorageReadTestInterface,
- .driver = storageReadPosixNew(storage, name, offset, limit),
+ .driver = storageReadPosixNew(storage, name, offset, limit, directio),
};
}
OBJ_NEW_END();
@@ -314,7 +315,7 @@ static HrnStorageWriteTest *
hrnStorageWriteTestNew(
Storage *const storagePosix, const String *const name, const mode_t modeFile, const mode_t modePath,
const String *const user, const String *const group, time_t timeModified, const bool createPath, const bool syncFile,
- const bool syncPath, const bool atomic, const bool truncate)
+ const bool syncPath, const bool atomic, const bool truncate, const bool directio)
{
FUNCTION_HARNESS_BEGIN();
FUNCTION_HARNESS_PARAM(STORAGE_POSIX, storagePosix);
@@ -329,6 +330,7 @@ hrnStorageWriteTestNew(
FUNCTION_HARNESS_PARAM(BOOL, syncPath);
FUNCTION_HARNESS_PARAM(BOOL, atomic);
FUNCTION_HARNESS_PARAM(BOOL, truncate);
+ FUNCTION_HARNESS_PARAM(BOOL, directio);
FUNCTION_HARNESS_END();
ASSERT(storagePosix != NULL);
@@ -349,10 +351,10 @@ hrnStorageWriteTestNew(
.interface = &hrnStorageWriteTestInterface,
.base = storageWritePosixNew(
storageDriver(storagePosix), name, modeFile, modePath, user, group, timeModified, createPath, false, false,
- false, truncate),
+ false, truncate, directio),
.version = storageWritePosixNew(
storageDriver(storagePosix), hrnStorageTestVersionFind(storagePosix, name), modeFile, modePath, user, group,
- timeModified, createPath, false, false, false, truncate),
+ timeModified, createPath, false, false, false, truncate, directio),
};
}
OBJ_NEW_END();
@@ -492,7 +494,7 @@ hrnStorageTestNewRead(THIS_VOID, const String *file, const StorageInterfaceNewRe
FUNCTION_HARNESS_RETURN(
HRN_STORAGE_READ_TEST,
- hrnStorageReadTestNew(storageDriver(this->storagePosix), file, param.offset, param.limit, param.versionId));
+ hrnStorageReadTestNew(storageDriver(this->storagePosix), file, param.offset, param.limit, param.versionId, false));
}
static void *
@@ -523,7 +525,7 @@ hrnStorageTestNewWrite(THIS_VOID, const String *const file, const StorageInterfa
HRN_STORAGE_WRITE_TEST,
hrnStorageWriteTestNew(
this->storagePosix, file, param.modeFile, param.modePath, param.user, param.group, param.timeModified,
- param.createPath, param.syncFile, param.syncPath, param.atomic, param.truncate));
+ param.createPath, param.syncFile, param.syncPath, param.atomic, param.truncate, false));
}
static void
diff --git a/test/src/module/storage/posixTest.c b/test/src/module/storage/posixTest.c
index 3de8f939b5..ccb7f80778 100644
--- a/test/src/module/storage/posixTest.c
+++ b/test/src/module/storage/posixTest.c
@@ -1,8 +1,10 @@
/***********************************************************************************************************************************
-Test Posix/CIFS Storage
+Test Posix/CIFS/PosixDio Storage
***********************************************************************************************************************************/
#include "common/io/io.h"
#include "common/time.h"
+#include "storage/posixDio/helper.h"
+#include "storage/posixDio/storage.h"
#include "storage/read.h"
#include "storage/write.h"
@@ -1337,6 +1339,20 @@ testRun(void)
TEST_RESULT_VOID(storageReadFree(storageNewReadP(storageTest, fileName)), "free file");
TEST_RESULT_VOID(storageReadMove(NULL, memContextTop()), "move null file");
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("direct I/O read round-trip");
+
+ Storage *const storageDioRead = storagePosixDioNew(
+ TEST_PATH_STR, STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL);
+
+ const Buffer *dioContent = BUFSTRDEF("DIRECT_IO_DATA");
+ TEST_RESULT_VOID(storagePutP(storageNewWriteP(storageDioRead, STRDEF("dio-read.file")), dioContent), "write dio file");
+
+ Buffer *dioRead = storageGetP(storageNewReadP(storageDioRead, STRDEF("dio-read.file")));
+ TEST_RESULT_BOOL(bufEq(dioRead, dioContent), true, "check direct I/O file contents");
+
+ storageRemoveP(storageDioRead, STRDEF("dio-read.file"), .errorOnMissing = true);
}
// *****************************************************************************************************************************
@@ -1497,6 +1513,24 @@ testRun(void)
TEST_STORAGE_GET(storageTest, "no-truncate", "ABC");
TEST_RESULT_UINT(storageInfoP(storageTest, STRDEF("no-truncate")).mode, 0600, "check mode");
TEST_RESULT_INT(storageInfoP(storageTest, STRDEF("no-truncate")).timeModified, 77777, "check time");
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("direct I/O write round-trip");
+
+ Storage *const storageDioWrite = storagePosixDioNew(
+ TEST_PATH_STR, STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL);
+
+ const Buffer *dioWriteContent = BUFSTRDEF("DIRECT_IO_WRITE_DATA");
+
+ TEST_ASSIGN(file, storageNewWriteP(storageDioWrite, STRDEF("dio-write.file")), "new direct I/O write file");
+ TEST_RESULT_VOID(ioWriteOpen(storageWriteIo(file)), "open direct I/O file");
+ TEST_RESULT_VOID(ioWrite(storageWriteIo(file), dioWriteContent), "write direct I/O data");
+ TEST_RESULT_VOID(ioWriteClose(storageWriteIo(file)), "close direct I/O file");
+
+ Buffer *dioVerify = storageGetP(storageNewReadP(storageDioWrite, STRDEF("dio-write.file")));
+ TEST_RESULT_BOOL(bufEq(dioVerify, dioWriteContent), true, "check direct I/O write file contents");
+
+ storageRemoveP(storageDioWrite, STRDEF("dio-write.file"), .errorOnMissing = true);
}
// *****************************************************************************************************************************
@@ -1685,12 +1719,34 @@ testRun(void)
TEST_RESULT_STR_Z(storage->path, TEST_PATH "/db", "check pg write storage path");
TEST_RESULT_BOOL(storage->write, true, "check pg write storage write");
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("storagePg() - pg-direct-io enabled");
+
+ // Reset pg storage caches so they are recreated with new config
+ storageHelper.storagePg = NULL;
+ storageHelper.storagePgWrite = NULL;
+
+ argList = strLstNew();
+ hrnCfgArgRawZ(argList, cfgOptStanza, "db");
+ hrnCfgArgRawBool(argList, cfgOptArchiveAsync, true);
+ hrnCfgArgRawZ(argList, cfgOptSpoolPath, TEST_PATH);
+ hrnCfgArgKeyRawZ(argList, cfgOptPgPath, 1, TEST_PATH "/db");
+ hrnCfgArgKeyRawZ(argList, cfgOptPgPath, 2, TEST_PATH "/db2");
+ hrnCfgArgRawBool(argList, cfgOptPgDirectIo, true);
+ HRN_CFG_LOAD(cfgCmdBackup, argList);
+
+ TEST_ASSIGN(storage, storagePg(), "new pg storage with direct I/O");
+ TEST_RESULT_STR_Z(storage->path, TEST_PATH "/db", "check pg storage path");
+ TEST_RESULT_UINT(storageType(storage), STORAGE_POSIX_TYPE, "check pg storage type is posix with direct I/O");
+
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("storageSpool - helper fails because stanza is required");
// Change the stanza to NULL, stanzaInit flag to false and make sure helper fails because stanza is required
storageHelper.storageSpool = NULL;
storageHelper.storageSpoolWrite = NULL;
+ storageHelper.storagePg = NULL;
+ storageHelper.storagePgWrite = NULL;
storageHelper.stanzaInit = false;
storageHelper.stanza = NULL;
@@ -1777,6 +1833,64 @@ testRun(void)
"assertion '!param.errorOnMissing || storageFeature(this, storageFeatureFileRemoveMissing)' failed");
}
+ // *****************************************************************************************************************************
+ if (testBegin("storageRepoGet() and StorageDriverPosixDio"))
+ {
+ // Load configuration
+ StringList *argList = strLstNew();
+ hrnCfgArgRawZ(argList, cfgOptStanza, "test");
+ hrnCfgArgRawZ(argList, cfgOptPgPath, "/path/to/pg");
+ hrnCfgArgRawZ(argList, cfgOptRepoType, "posix-dio");
+ hrnCfgArgRawZ(argList, cfgOptRepoPath, TEST_PATH);
+ HRN_CFG_LOAD(cfgCmdArchiveGet, argList);
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("error on invalid storage type");
+
+ static const StorageHelper storageHelperListError[] = {{.type = STORAGE_POSIX_TYPE}, STORAGE_END_HELPER};
+ storageHelperInit(storageHelperListError);
+
+ TEST_ERROR(storageRepoGet(0, true), AssertError, "invalid storage type");
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("storage configuration");
+
+ // Set storage helper
+ static const StorageHelper storageHelperList[] = {STORAGE_POSIX_DIO_HELPER, STORAGE_END_HELPER};
+ storageHelperInit(storageHelperList);
+
+ const Storage *storage = NULL;
+ TEST_ASSIGN(storage, storageRepoGet(0, true), "get posix-dio repo storage");
+ TEST_RESULT_UINT(storageType(storage), STORAGE_POSIX_DIO_TYPE, "check storage type");
+ TEST_RESULT_BOOL(storageFeature(storage, storageFeaturePath), true, "check path feature");
+ TEST_RESULT_BOOL(storageFeature(storage, storageFeatureHardLink), true, "check hard link feature");
+ TEST_RESULT_BOOL(storageFeature(storage, storageFeatureSymLink), true, "check sym link feature");
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("write object path sync enabled");
+
+ // Create a FileWrite object and ensure path sync is enabled (unlike CIFS)
+ StorageWrite *file = NULL;
+ TEST_ASSIGN(file, storageNewWriteP(storage, STRDEF("somefile"), .noSyncPath = false), "new file write");
+
+ TEST_RESULT_BOOL(((StorageWritePosix *)file->driver)->syncPath, true, "path sync is enabled");
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("basic write/read round-trip without direct I/O");
+
+ // Use standard posix storage to write a reference file for comparison
+ Storage *const storagePosixRef = storagePosixNewP(TEST_PATH_STR, .write = true);
+ TEST_RESULT_VOID(
+ storagePutP(storageNewWriteP(storagePosixRef, STRDEF("dio-repo.file")), BUFSTRDEF("POSIX_DIO_TEST")),
+ "write reference file");
+
+ // Read back via posix-dio storage and verify
+ Buffer *dioRepoRead = storageGetP(storageNewReadP(storage, STRDEF("dio-repo.file")));
+ TEST_RESULT_STR_Z(strNewBuf(dioRepoRead), "POSIX_DIO_TEST", "check posix-dio read file contents");
+
+ storageRemoveP(storagePosixRef, STRDEF("dio-repo.file"), .errorOnMissing = true);
+ }
+
// *****************************************************************************************************************************
if (testBegin("HrnStorageTest"))
{
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: DirectIO
@ 2026-10-26 17:28 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-10-26 17:28 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
Thanks for the PR. Before getting into the code I'm a bit worried about performance.
Right now we depend on the kernel to read ahead for us. Writes go into the page cache so they return immediately and writeback happens in the background. With `O_DIRECT` each `read()`/`write()` is a synchronous round trip to the device (`buffer-size`, so 1MiB by default) and nothing is in flight while we compress/encrypt/checksum. So per process throughput goes from roughly max(io, cpu) to io + cpu. `process-max` will hide some of that but not all of it, and it gets worse as storage latency goes up (EBS, SAN, etc.).
A few other things I noticed on the performance side:
- Reads with an offset/limit always fetch a full aligned buffer from disk. Nothing is cached so repeated reads of the same region always go to disk. Block incremental restore does a lot of this, and the checksum re-read after a block delta restore expects the pages to still be in cache.
- On Linux an `O_DIRECT` read of a range with dirty pages in the page cache forces writeback of those pages first. Since PostgreSQL writes through the page cache, a backup with `pg-direct-io` will be flushing PostgreSQL's dirty pages as it reads, outside of checkpoints.
- `O_DIRECT` writes still need `fsync` for durability so there are no savings there.
- The bounce buffer `memcpy` replaces the kernel copy so there are no CPU savings either.
Do you have any benchmarks? It would also help to know what problem you are trying to solve. If it is cache eviction (backup pushing the PostgreSQL working set out of the page cache) then `posix_fadvise(POSIX_FADV_DONTNEED)` on ranges we have already read (and after `fsync` for writes) might get most of the benefit without losing readahead/writeback.
There also looks to be a correctness problem in block incremental restore. `storageWritePosixSeek()` sets `totalWritten = position` and `storageWritePosixClose()` does `ftruncate(totalWritten)`. Delta restore opens the file with `.noTruncate` and seeks to each changed block, so with `pg-direct-io` the file will be truncated at the end of the last changed block. I haven't run it but that's how the code reads.
If we do move forward with this:
- I don't think a new driver is needed. `posix-dio` is just `posix` with a flag set, so an option like `repo-io=default/direct` (and something similar for pg) would be simpler. Also, a new option doesn't need deprecations.
- There are a lot of formatting changes to existing code (comment alignment, struct initializers) and some existing comments and `{vm_covered}` markers have been removed. That makes the diff hard to review -- please revert those.
- This needs a probe in `meson.build`. We build with `_POSIX_C_SOURCE` rather than `_GNU_SOURCE` so glibc doesn't expose `O_DIRECT`, which I'm guessing is why there's a `#define O_DIRECT __O_DIRECT` (that's glibc internal). Better to probe for `O_DIRECT`, define `HAVE_O_DIRECT`, and only allow the option when it's available (like `sftp: HAVE_LIBSSH2` in `config.yaml`).
- The PR description states it falls back to posix when `O_DIRECT` isn't supported, but the code throws `FeatureNotSupportedError`. On platforms without `O_DIRECT` (e.g. macOS) it silently uses buffered I/O but still goes through the bounce buffer.
- The 4096 alignment is hard-coded. That works for most devices but ideally it would come from the device (`statx()` with `STATX_DIOALIGN` on Linux >= 6.1).
Overall I'm just struggling to see the advantage of `O_DIRECT` over `posix_fadvise(POSIX_FADV_DONTNEED)` for our specific use case, especially given the apparent performance hit. AIO solves a lot of that at the cost of complexity but that's not what we have here.
Overall I think the `fadvise` patch had more promise but it was not moving forward and I did not have time to take it over. This patch in particular looked useful to me: https://github.com/pgbackrest/pgbackrest/compare/main...fbernier:pgbackrest:fadvise?expand=1.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: DirectIO
@ 2026-10-31 13:29 "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: fmbiete (@fmbiete) @ 2026-10-31 13:29 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
Use DirectIO to avoid polluting buffer cache with reads or writes from pgbackrest.
Calling fadvise POSIX_FADV_DONTNEED after the read purges the cached pages for that file, it doesn't avoid caching them in the first place. That means we don't avoid polluting the cache and we could evict PostgreSQL cached pages.
Example (test01.c) - you can see it has fully cached the read file and fadvise evicts the pages.
```
Creating 1GB file via O_DIRECT...
[BEFORE READ] Cached pages: 0 / 262144 (Size: 0.00 MB)
Reading file sequentially...
Read 1073741824 bytes. Execution time: 2019.85 ms
[AFTER READ ] Cached pages: 262144 / 262144 (Size: 1024.00 MB)
Calling posix_fadvise(POSIX_FADV_DONTNEED) to evict cache...
[AFTER EVICTION] Cached pages: 0 / 262144 (Size: 0.00 MB)
```
Calling fadvise before reading with POSIX_FADV_NOREUSE should be the way to go, but the Linux implementation is not what we would expect, or we don't see the benefit with 1 file test.
Example (test02.c) - kernel 7.2
```
Creating 1GB file via O_DIRECT...
[BEFORE READ] Cached pages: 0 / 262144 (Size: 0.00 MB)
Calling posix_fadvise(POSIX_FADV_NOREUSE)...
Reading file sequentially...
Read 1073741824 bytes. Execution time: 2018.80 ms
[AFTER READ ] Cached pages: 262144 / 262144 (Size: 1024.00 MB)
Calling posix_fadvise(POSIX_FADV_DONTNEED) to evict cache...
[AFTER EVICTION] Cached pages: 0 / 262144 (Size: 0.00 MB)
```
Using DirectIO avoids the page pollution, of course it is slower than cached IO: 1.27x in my NVME. Would that be a deal breaker for pgbackrest? I don't think so, and it avoids the above issues.
```
Creating 1GB file via O_DIRECT...
[BEFORE READ] Cached pages: 0 / 262144 (Size: 0.00 MB)
Opening file with O_DIRECT...
Reading file sequentially via O_DIRECT...
Read 1073741824 bytes. Execution time: 2555.02 ms
[AFTER READ ] Cached pages: 0 / 262144 (Size: 0.00 MB)
```
If you think this is adequate I could do the changes you commented and avoid using vscode auto-formatter on-save, because code-format doesn't seem to fix the breaks.
[test01.c](https://github.com/user-attachments/files/34217940/test01.c)
[test02.c](https://github.com/user-attachments/files/34217941/test02.c)
[test03.c](https://github.com/user-attachments/files/34217942/test03.c)
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: DirectIO
@ 2026-11-03 11:20 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-03 11:20 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
> Calling fadvise POSIX_FADV_DONTNEED after the read purges the cached pages for that file, it doesn't avoid caching them in the first place.
Sure, but if we do this by buffer rather than for the entire file it would work better. I also think a better way to go might be RWF_DONTCACHE with preadv2()/pwritev2().
> If you think this is adequate I could do the changes you commented and avoid using vscode auto-formatter on-save, because code-format doesn't seem to fix the breaks.
Unfortunately uncrustify won't fix everything -- especially comment formatting. In general it is best to avoid reformatting lines that are not part of the patch.
I think DIO, even though slower, would make a good option but this patch needs to be drastically simpler and right now it is hard to evaluate due to the whitespace changes and unneeded complexity. Please follow my comments and let's see what the patch looks like afterwards.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: DirectIO
@ 2026-11-04 14:58 "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: fmbiete (@fmbiete) @ 2026-11-04 14:58 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
I have applied the requested changes
```
Add configurable direct I/O for repositories and PostgreSQL
Add repo-io-mode and pg-io-mode options to enable direct I/O for POSIX
and CIFS repositories and PostgreSQL data directories when O_DIRECT is
available. Probe O_DIRECT and Linux statx(STATX_DIOALIGN), using device
alignment when reported and a 4096-byte fallback otherwise.
Stage reads and writes through aligned buffers, preserve requested read
offsets and limits, and truncate padded writes to their logical size.
Fall back to buffered I/O when opening with O_DIRECT returns EINVAL.
Flush pending output before seeking, including seeks to the current
flushed offset.
Add direct-I/O coverage for round trips, offsets, limits, EOF, alignment,
seek, and I/O errors.
```
If O_DIRECT is slow we could add io_uring, but I don't consider it terribly slow.
> Sure, but if we do this by buffer rather than for the entire file it would work better. I also think a better way to go might be RWF_DONTCACHE with preadv2()/pwritev2().
buffer to buffer we would purge all the cached pages (postgresql side); the cache is global, not particular to a process. Running fadvise in a loop is also extremely slow, it also has to write/sync any dirty page.
RWF_DONTCACHE is interesting, we could add this without flag and auto-discovery, but it would not work for any current RHEL versions.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: DirectIO
@ 2026-11-04 15:19 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-04 15:19 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
> I have applied the requested changes
This looks a lot more like what I was expecting, thank you!
> If O_DIRECT is slow we could add io_uring
I briefly looked at io_uring and was put off a bit by the complexity thinking we should stick with fadvise() but it might not look like such a big diff if DIO goes in.
> RWF_DONTCACHE is interesting, we could add this without flag and auto-discovery, but it would not work for any current RHEL versions.
Yeah, this would be best effort and fail-back to default. But it seems to be the best possible option for distros where it works, and those will only grow.
I won't be able to look at this immediately but the goal would be to get it into the January release as a beta feature, as long as there are no good reasons to keep it out.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: DirectIO
@ 2026-11-13 14:46 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-13 14:46 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
I made a first pass through this and overall it looks good but as you can see I made significant changes. Mostly they were organizational but there are a few large changes:
1) The last block that would need to be truncated if written with direct IO is written with buffered I/O (see 76823bc7b42f for details/reasons). In practice, especially with bundled backups, this should cause minimal buffered I/O.
2) statx has been removed so alignment > 4096 fails back to buffered I/O. These larger alignments should be rare so I don't think this is a very big deal but let me know if you disagree. I'm fine with bringing it back but I wanted to limit the scope of this patch to what is most useful.
Also I committed the I/O write fix separately in 5929c36f2af6ac. This does not express as a bug in the current code (like it did in the PR) because of how it is used but I felt it deserved a separate commit for clarity.
This still needs more review and I will probably commit the _GNU_SOURCE changes separately, but overall I think the patch is in decent shape.
Let me know what you think.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887 (approved): Direct I/O for Posix/CIFS storage.
@ 2026-11-16 14:44 "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: hunleyd (@hunleyd) @ 2026-11-16 14:44 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: DirectIO
@ 2026-11-17 14:19 "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: fmbiete (@fmbiete) @ 2026-11-17 14:19 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
I think I'm ok with those changes.
The last bit as buffered IO is a tread-off, simple, less rase cases prone and it should be a few bytes per file at worse, and since it's at the end the read-ahead of the kernel should not load additional pages.
The aligment should work, but I have some doubts about those kubernetes-operators... I hope they don't backup to any overlay filesystem. But if we identify EINVAL in the first write as fallback to buffered and don't error, it should be ok.
Let me know if I can help you with something.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887 (approved): Direct I/O for Posix/CIFS storage.
@ 2026-11-17 14:34 "pgstef (@pgstef)" <noreply+pgstef@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: pgstef (@pgstef) @ 2026-11-17 14:34 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
For what it's worth, the current state of this PR looks good to me!
The performance trade-offs of `O_DIRECT` (no readahead or writeback caching, synchronous I/O, and forced writeback of dirty pages on Linux) are documented.
And if I read it correctly, the alignment fix in `db0ccfb79` produces the same aligned address regardless of the requested alignment, stays within the `size + align - 1` allocation, and keeps the pointer derived from the original allocation.
So overall, looks good to me! 👍
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-17 17:32 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-17 17:32 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele renamed this.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-17 17:34 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-17 17:34 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele renamed this.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-17 17:40 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-17 17:40 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@fmbiete
> I think I'm ok with those changes.
I'm still fine with adding alignment via statx(). I just wanted to keep the initial commit simple. Feel free to submit statx() as a followup if you like.
> The last bit as buffered IO is a tread-off
I really debated this but I think it is the right way to go. My thinking is we can default to use preadv2()/pwritev2() for the last block when it is present and avoid buffering entirely. I have a note to add that support but you are welcome to take that on if you like.
> but I have some doubts about those kubernetes-operators
I'm not sure how well this is going to work on k8s (or containers in general) based on my reading but I'm hoping preadv2()/pwritev2() also gives us a path forward there.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-17 17:49 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-17 17:49 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@pgstef
> And if I read it correctly, the alignment fix in db0ccfb79 produces the same aligned address regardless of the requested alignment
Correct. This fix was mostly to make Fil-C happy, since it can't track pointers through a uintptr_t.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-18 14:01 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-18 14:01 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele assigned @dwsteele.
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-18 14:01 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-18 14:01 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele set milestone to "2.61.0".
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-18 14:01 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-18 14:01 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele applied label "module [core]".
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-18 14:01 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-18 14:01 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele applied label "enhancement".
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-18 14:04 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-18 14:04 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele merged this (commit ab2d4fd801db).
^ permalink raw [nested|flat] 20+ messages in thread
* Re: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
@ 2026-11-18 14:04 "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
18 siblings, 0 replies; 20+ messages in thread
From: dwsteele (@dwsteele) @ 2026-11-18 14:04 UTC (permalink / raw)
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
@dwsteele closed this.
^ permalink raw [nested|flat] 20+ messages in thread
end of thread, other threads:[~2026-11-18 14:04 UTC | newest]
Thread overview: 20+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-25 14:59 [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage. "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
2026-10-26 17:28 ` Re: [pgbackrest/pgbackrest] PR #2887: DirectIO "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-10-31 13:29 ` Re: [pgbackrest/pgbackrest] PR #2887: DirectIO "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
2026-11-03 11:20 ` Re: [pgbackrest/pgbackrest] PR #2887: DirectIO "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-04 14:58 ` Re: [pgbackrest/pgbackrest] PR #2887: DirectIO "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
2026-11-04 15:19 ` Re: [pgbackrest/pgbackrest] PR #2887: DirectIO "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-13 14:46 ` Re: [pgbackrest/pgbackrest] PR #2887: DirectIO "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-16 14:44 ` Re: [pgbackrest/pgbackrest] PR #2887 (approved): Direct I/O for Posix/CIFS storage. "hunleyd (@hunleyd)" <noreply+hunleyd@github.com>
2026-11-17 14:19 ` Re: [pgbackrest/pgbackrest] PR #2887: DirectIO "fmbiete (@fmbiete)" <noreply+fmbiete@github.com>
2026-11-17 14:34 ` Re: [pgbackrest/pgbackrest] PR #2887 (approved): Direct I/O for Posix/CIFS storage. "pgstef (@pgstef)" <noreply+pgstef@github.com>
2026-11-17 17:32 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-17 17:34 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-17 17:40 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-17 17:49 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-18 14:01 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-18 14:01 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-18 14:01 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-18 14:01 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-18 14:04 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
2026-11-18 14:04 ` "dwsteele (@dwsteele)" <noreply+dwsteele@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox