/*------------------------------------------------------------------------- * * aio_types.h * AIO related types that are useful to include separately, to reduce the * "include burden". * * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * src/include/storage/aio_types.h * *------------------------------------------------------------------------- */ #ifndef AIO_TYPES_H #define AIO_TYPES_H #include "storage/block.h" #include "storage/relfilelocator.h" typedef struct PgAioHandle PgAioHandle; typedef struct PgAioHandleCallbacks PgAioHandleCallbacks; typedef struct PgAioTargetInfo PgAioTargetInfo; /* * A reference to an IO that can be used to wait for the IO (using * pgaio_wref_wait()) to complete. * * These can be passed across process boundaries. */ typedef struct PgAioWaitRef { /* internal ID identifying the specific PgAioHandle */ uint32 aio_index; /* * IO handles are reused. To detect if a handle was reused, and thereby * avoid unnecessarily waiting for a newer IO, each time the handle is * reused a generation number is increased. * * To avoid requiring alignment sufficient for an int64, split the * generation into two. */ uint32 generation_upper; uint32 generation_lower; } PgAioWaitRef; /* * Information identifying what the IO is being performed on. * * This needs sufficient information to * * a) Reopen the file for the IO if the IO is executed in a context that * cannot use the FD provided initially (e.g. because the IO is executed in * a worker process). * * b) Describe the object the IO is performed on in log / error messages. */ typedef union PgAioTargetData { struct { RelFileLocator rlocator; /* physical relation identifier */ BlockNumber blockNum; /* blknum relative to begin of reln */ BlockNumber nblocks; ForkNumber forkNum:8; /* don't waste 4 byte for four values */ bool is_temp:1; /* proc can be inferred by owning AIO */ bool skip_fsync:1; /* [PG_CUSTOM: CFS] * Set by the CFS submit paths only: the pages of this IO were * delivered verified by the CFS CRC chain (header, payload and * original-image CRCs), so the cluster page-checksum in * PageIsVerified() is redundant and may be skipped (gated by * the cfs_skip_page_checksum GUC at submit time). */ bool cfs_skip_checksum:1; } smgr; /* [PG_CUSTOM: CFS] * Target data for a CFS span readv: the worker reopens the .cfp * segment by identity (no cross-process fd), the completion callback * fetches the span plan by the shared arena slot id. 'generation' * is the relation map generation the plan's offsets belong to: a * compaction bump between planning and reopening means the reopened * file no longer matches the plan, so the reopen fails the IO * transiently instead of reading wrong bytes. The ID space is * never persisted. CFS relations are never temp. * * ALIASING CONTRACT: the leading fields (rlocator, blockNum, * nblocks) deliberately mirror the smgr member's layout, and the * smgr member's trailing bitfield unit (is_temp, skip_fsync, * cfs_skip_checksum) falls into this struct's alignment padding * before 'generation'. The planning backend zeroes the whole * union before filling cfs fields, so the buffer completion's * report callback (buffer_readv_report), which reads the union * through its smgr lens, sees the right relation, block range, * fork 0 (CFS serves the main fork) and is_temp false. The CFS * span submit path likewise writes cfs_skip_checksum through the * smgr lens AFTER filling the cfs fields; the zeroed padding * keeps that write confined to dead bytes of this struct. The * asserts below turn this coupling into a checked contract; * reorder either struct only together with them. */ struct { RelFileLocator rlocator; /* physical relation identifier */ BlockNumber blockNum; /* first block, for identity reports */ BlockNumber nblocks; /* blocks served by the span plan */ uint64 generation; /* relation map generation of the plan */ uint32 plan_slot; /* scratch arena slot with the plan */ uint32 segment; /* .cfp segment index */ ForkNumber forkNum:8; } cfs; } PgAioTargetData; /* * [PG_CUSTOM: CFS] * Checked half of the aliasing contract above: the shared prefix must * stay byte-identical, and every smgr byte past it (the * forkNum/is_temp/skip_fsync/cfs_skip_checksum bitfield unit) must * land inside this struct's zeroed padding, never on a live cfs * field. */ StaticAssertDecl(offsetof(PgAioTargetData, smgr.blockNum) == offsetof(PgAioTargetData, cfs.blockNum) && offsetof(PgAioTargetData, smgr.nblocks) == offsetof(PgAioTargetData, cfs.nblocks) && offsetof(PgAioTargetData, cfs.generation) >= sizeof(((PgAioTargetData *) 0)->smgr), "smgr/cfs aliasing contract of PgAioTargetData is broken"); /* * The status of an AIO operation. */ typedef enum PgAioResultStatus { PGAIO_RS_UNKNOWN, /* not yet completed / uninitialized */ PGAIO_RS_OK, PGAIO_RS_PARTIAL, /* did not fully succeed, no warning/error */ PGAIO_RS_WARNING, /* [partially] succeeded, with a warning */ PGAIO_RS_ERROR, /* failed entirely */ } PgAioResultStatus; /* * Result of IO operation, visible only to the initiator of IO. * * We need to be careful about the size of PgAioResult, as it is embedded in * every PgAioHandle, as well as every PgAioReturn. Currently we assume we can * fit it into one 8 byte value, restricting the space for per-callback error * data to PGAIO_RESULT_ERROR_BITS. */ #define PGAIO_RESULT_ID_BITS 6 #define PGAIO_RESULT_STATUS_BITS 3 #define PGAIO_RESULT_ERROR_BITS 23 typedef struct PgAioResult { /* * This is of type PgAioHandleCallbackID, but can't use a bitfield of an * enum, because some compilers treat enums as signed. */ uint32 id:PGAIO_RESULT_ID_BITS; /* of type PgAioResultStatus, see above */ uint32 status:PGAIO_RESULT_STATUS_BITS; /* meaning defined by callback->report */ uint32 error_data:PGAIO_RESULT_ERROR_BITS; int32 result; } PgAioResult; StaticAssertDecl(PGAIO_RESULT_ID_BITS + PGAIO_RESULT_STATUS_BITS + PGAIO_RESULT_ERROR_BITS == 32, "PgAioResult bits divided up incorrectly"); StaticAssertDecl(sizeof(PgAioResult) == 8, "PgAioResult has unexpected size"); /* * Combination of PgAioResult with minimal metadata about the IO. * * Contains sufficient information to be able, in case the IO [partially] * fails, to log/raise an error under control of the IO issuing code. */ typedef struct PgAioReturn { PgAioResult result; PgAioTargetData target_data; } PgAioReturn; #endif /* AIO_TYPES_H */