pg.ddx.io  pgbackrest/pgbackrest GitHub issues and pull requests  
help / color / mirror / Atom feed
From: fmbiete (@fmbiete) <noreply+fmbiete@github.com>
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
Subject: [pgbackrest/pgbackrest] PR #2887: Direct I/O for Posix/CIFS storage.
Date: Sun, 25 Oct 2026 14:59:49 +0000
Message-ID: <gh-pgbackrest-pgbackrest-2887@github.com> (raw)

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.patchdiff --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")) {

view thread (20+ messages)  latest in thread

Message-ID: <gh-pgbackrest-pgbackrest-2887@github.com>
Permalink:  ../gh-pgbackrest-pgbackrest-2887@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