Skip to content

Commit

Permalink
PG-799: Modify code for server smgr api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dutow committed Dec 2, 2024
1 parent e0978a8 commit adf1d30
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
1 change: 1 addition & 0 deletions pg_tde.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
shared_preload_libraries = 'pg_tde'
smgr_chain = 'pg_tde,md'
51 changes: 24 additions & 27 deletions src/smgr/pg_tde_smgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ tde_smgr_get_key(SMgrRelation reln, RelFileLocator* old_locator, bool can_create

static void
tde_mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
const void **buffers, BlockNumber nblocks, bool skipFsync)
const void **buffers, BlockNumber nblocks, bool skipFsync, SmgrChainIndex chain_index)
{
TDESMgrRelation tdereln = (TDESMgrRelation) reln;
RelKeyData *rkd = &tdereln->relKey;

if (!tde_is_encryption_required(tdereln, forknum))
{
mdwritev(reln, forknum, blocknum, buffers, nblocks, skipFsync);
smgr_writev_internal(reln, forknum, blocknum, buffers, nblocks, skipFsync, chain_index+1);
}
else
{
Expand All @@ -132,8 +132,8 @@ tde_mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
AesEncrypt(rkd->internal_key.key, iv, ((char **) buffers)[i], BLCKSZ, local_buffers[i], &out_len);
}

mdwritev(reln, forknum, blocknum,
local_buffers, nblocks, skipFsync);
smgr_writev_internal(reln, forknum, blocknum,
local_buffers, nblocks, skipFsync, chain_index+1);

pfree(local_blocks);
pfree(local_buffers);
Expand All @@ -142,14 +142,14 @@ tde_mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,

static void
tde_mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
const void *buffer, bool skipFsync)
const void *buffer, bool skipFsync, SmgrChainIndex chain_index)
{
TDESMgrRelation tdereln = (TDESMgrRelation) reln;
RelKeyData *rkd = &tdereln->relKey;

if (!tde_is_encryption_required(tdereln, forknum))
{
mdextend(reln, forknum, blocknum, buffer, skipFsync);
smgr_extend_internal(reln, forknum, blocknum, buffer, skipFsync, chain_index+1);
}
else
{
Expand All @@ -165,21 +165,21 @@ tde_mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,

AesEncrypt(rkd->internal_key.key, iv, ((char *) buffer), BLCKSZ, local_blocks_aligned, &out_len);

mdextend(reln, forknum, blocknum, local_blocks_aligned, skipFsync);
smgr_extend_internal(reln, forknum, blocknum, local_blocks_aligned, skipFsync, chain_index+1);

pfree(local_blocks);
}
}

static void
tde_mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
void **buffers, BlockNumber nblocks)
void **buffers, BlockNumber nblocks, SmgrChainIndex chain_index)
{
int out_len = BLCKSZ;
TDESMgrRelation tdereln = (TDESMgrRelation) reln;
RelKeyData *rkd = &tdereln->relKey;

mdreadv(reln, forknum, blocknum, buffers, nblocks);
smgr_readv_internal(reln, forknum, blocknum, buffers, nblocks, chain_index+1);

if (!tde_is_encryption_required(tdereln, forknum))
return;
Expand Down Expand Up @@ -224,7 +224,7 @@ tde_mdreadv(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
}

static void
tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool isRedo)
tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool isRedo, SmgrChainIndex chain_index)
{
TDESMgrRelation tdereln = (TDESMgrRelation) reln;

Expand All @@ -234,7 +234,7 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
*/
/* so we create the key here by loading it */

mdcreate(relold, reln, forknum, isRedo);
smgr_create_internal(relold, reln, forknum, isRedo, chain_index+1);

/*
* Later calls then decide to encrypt or not based on the existence of the
Expand All @@ -257,7 +257,7 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
* mdopen() -- Initialize newly-opened relation.
*/
static void
tde_mdopen(SMgrRelation reln)
tde_mdopen(SMgrRelation reln, SmgrChainIndex chain_index)
{
TDESMgrRelation tdereln = (TDESMgrRelation) reln;
RelKeyData *key = tde_smgr_get_key(reln, NULL, false);
Expand All @@ -271,38 +271,35 @@ tde_mdopen(SMgrRelation reln)
{
tdereln->encrypted_relation = false;
}
mdopen(reln);
smgr_open_internal(reln, chain_index + 1);
}

static SMgrId tde_smgr_id;
static const struct f_smgr tde_smgr = {
.name = "tde",
.smgr_init = mdinit,
.name = "pg_tde",
.smgr_init = NULL,
.smgr_shutdown = NULL,
.smgr_open = tde_mdopen,
.smgr_close = mdclose,
.smgr_close = NULL,
.smgr_create = tde_mdcreate,
.smgr_exists = mdexists,
.smgr_unlink = mdunlink,
.smgr_exists = NULL,
.smgr_unlink = NULL,
.smgr_extend = tde_mdextend,
.smgr_zeroextend = mdzeroextend,
.smgr_zeroextend = NULL,
.smgr_prefetch = mdprefetch,
.smgr_readv = tde_mdreadv,
.smgr_writev = tde_mdwritev,
.smgr_writeback = mdwriteback,
.smgr_nblocks = mdnblocks,
.smgr_truncate = mdtruncate,
.smgr_immedsync = mdimmedsync,
.smgr_registersync = mdregistersync,
.smgr_writeback = NULL,
.smgr_nblocks = NULL,
.smgr_truncate = NULL,
.smgr_immedsync = NULL,
.smgr_registersync = NULL,
};

void
RegisterStorageMgr(void)
{
tde_smgr_id = smgr_register(&tde_smgr, sizeof(TDESMgrRelationData));

/* TODO: figure out how this part should work in a real extension */
storage_manager_id = tde_smgr_id;
}

#else
Expand Down
1 change: 1 addition & 0 deletions t/008_tde_heap.pl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# UPDATE postgresql.conf to include/load pg_tde library
open my $conf, '>>', "$pgdata/postgresql.conf";
print $conf "shared_preload_libraries = 'pg_tde'\n";
print $conf "smgr_chain = 'pg_tde,md'\n";
close $conf;

# Start server
Expand Down

0 comments on commit adf1d30

Please sign in to comment.