Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions contrib/pg_tde/src/access/pg_tde_xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tdeheap_rmgr_redo(XLogReaderState *record)
{
uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;

if (info == XLOG_TDE_ADD_RELATION_KEY)
if (info == XLOG_TDE_CREATE_RELATION_KEY)
{
XLogRelKey *xlrec = (XLogRelKey *) XLogRecGetData(record);

Expand All @@ -62,7 +62,7 @@ tdeheap_rmgr_redo(XLogReaderState *record)
{
XLogRelKey *xlrec = (XLogRelKey *) XLogRecGetData(record);

tde_smgr_delete_key_redo(&xlrec->rlocator);
tde_smgr_delete_leftover_key_redo(&xlrec->rlocator);
}
else if (info == XLOG_TDE_ROTATE_PRINCIPAL_KEY)
{
Expand Down Expand Up @@ -99,7 +99,7 @@ tdeheap_rmgr_desc(StringInfo buf, XLogReaderState *record)
{
uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;

if (info == XLOG_TDE_ADD_RELATION_KEY)
if (info == XLOG_TDE_CREATE_RELATION_KEY)
{
XLogRelKey *xlrec = (XLogRelKey *) XLogRecGetData(record);

Expand Down Expand Up @@ -148,8 +148,8 @@ tdeheap_rmgr_identify(uint8 info)
{
switch (info & ~XLR_INFO_MASK)
{
case XLOG_TDE_ADD_RELATION_KEY:
return "ADD_RELATION_KEY";
case XLOG_TDE_CREATE_RELATION_KEY:
return "CREATE_RELATION_KEY";
case XLOG_TDE_ADD_PRINCIPAL_KEY:
return "ADD_PRINCIPAL_KEY";
case XLOG_TDE_ROTATE_PRINCIPAL_KEY:
Expand Down
2 changes: 1 addition & 1 deletion contrib/pg_tde/src/include/access/pg_tde_xlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef FRONTEND

/* TDE XLOG record types */
#define XLOG_TDE_ADD_RELATION_KEY 0x00
#define XLOG_TDE_CREATE_RELATION_KEY 0x00
#define XLOG_TDE_ADD_PRINCIPAL_KEY 0x10
#define XLOG_TDE_ROTATE_PRINCIPAL_KEY 0x20
#define XLOG_TDE_WRITE_KEY_PROVIDER 0x30
Expand Down
2 changes: 1 addition & 1 deletion contrib/pg_tde/src/include/smgr/pg_tde_smgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

extern void RegisterStorageMgr(void);
extern void tde_smgr_create_key_redo(const RelFileLocator *rlocator);
extern void tde_smgr_delete_key_redo(const RelFileLocator *rlocator);
extern void tde_smgr_delete_leftover_key_redo(const RelFileLocator *rlocator);
extern bool tde_smgr_rel_is_encrypted(SMgrRelation reln);

#endif /* PG_TDE_SMGR_H */
76 changes: 41 additions & 35 deletions contrib/pg_tde/src/smgr/pg_tde_smgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,29 @@ static SMgrId OurSMgrId = MaxSMgrId;
static void tde_smgr_save_temp_key(const RelFileLocator *newrlocator, const InternalKey *key);
static InternalKey *tde_smgr_get_temp_key(const RelFileLocator *rel);
static bool tde_smgr_has_temp_key(const RelFileLocator *rel);
static void tde_smgr_remove_temp_key(const RelFileLocator *rel);
static void tde_smgr_delete_temp_key(const RelFileLocator *rel);
static void CalcBlockIv(ForkNumber forknum, BlockNumber bn, const unsigned char *base_iv, unsigned char *iv);

static void
tde_smgr_log_create_key(const RelFileLocator *rlocator)
{
XLogRelKey xlrec = {.rlocator = *rlocator};

XLogBeginInsert();
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_CREATE_RELATION_KEY);
}

static void
tde_smgr_log_delete_leftover_key(const RelFileLocator *rlocator)
{
XLogRelKey xlrec = {.rlocator = *rlocator};

XLogBeginInsert();
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_DELETE_RELATION_KEY);
}

static InternalKey *
tde_smgr_create_key(const RelFileLocatorBackend *smgr_rlocator)
{
Expand All @@ -80,23 +100,14 @@ tde_smgr_create_key(const RelFileLocatorBackend *smgr_rlocator)
if (RelFileLocatorBackendIsTemp(*smgr_rlocator))
tde_smgr_save_temp_key(&smgr_rlocator->locator, key);
else
{
pg_tde_save_smgr_key(smgr_rlocator->locator, key);
tde_smgr_log_create_key(&smgr_rlocator->locator);
}

return key;
}

static void
tde_smgr_log_create_key(const RelFileLocatorBackend *smgr_rlocator)
{
XLogRelKey xlrec = {
.rlocator = smgr_rlocator->locator,
};

XLogBeginInsert();
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_ADD_RELATION_KEY);
}

void
tde_smgr_create_key_redo(const RelFileLocator *rlocator)
{
Expand All @@ -113,19 +124,24 @@ tde_smgr_create_key_redo(const RelFileLocator *rlocator)
static void
tde_smgr_delete_key(const RelFileLocatorBackend *smgr_rlocator)
{
XLogRelKey xlrec = {
.rlocator = smgr_rlocator->locator,
};

pg_tde_free_key_map_entry(smgr_rlocator->locator);
if (RelFileLocatorBackendIsTemp(*smgr_rlocator))
tde_smgr_delete_temp_key(&smgr_rlocator->locator);
else
pg_tde_free_key_map_entry(smgr_rlocator->locator);
}

XLogBeginInsert();
XLogRegisterData((char *) &xlrec, sizeof(xlrec));
XLogInsert(RM_TDERMGR_ID, XLOG_TDE_DELETE_RELATION_KEY);
static void
tde_smgr_delete_leftover_key(const RelFileLocatorBackend *smgr_rlocator)
{
if (!RelFileLocatorBackendIsTemp(*smgr_rlocator))
{
pg_tde_free_key_map_entry(smgr_rlocator->locator);
tde_smgr_log_delete_leftover_key(&smgr_rlocator->locator);
}
}

void
tde_smgr_delete_key_redo(const RelFileLocator *rlocator)
tde_smgr_delete_leftover_key_redo(const RelFileLocator *rlocator)
{
pg_tde_free_key_map_entry(*rlocator);
}
Expand All @@ -148,15 +164,6 @@ tde_smgr_get_key(const RelFileLocatorBackend *smgr_rlocator)
return pg_tde_get_smgr_key(smgr_rlocator->locator);
}

static void
tde_smgr_remove_key(const RelFileLocatorBackend *smgr_rlocator)
{
if (RelFileLocatorBackendIsTemp(*smgr_rlocator))
tde_smgr_remove_temp_key(&smgr_rlocator->locator);
else
pg_tde_free_key_map_entry(smgr_rlocator->locator);
}

static bool
tde_smgr_should_encrypt(const RelFileLocatorBackend *smgr_rlocator, RelFileLocator *old_locator)
{
Expand Down Expand Up @@ -264,7 +271,7 @@ tde_mdunlink(RelFileLocatorBackend rlocator, ForkNumber forknum, bool isRedo)
if (forknum == MAIN_FORKNUM || forknum == InvalidForkNumber)
{
if (tde_smgr_is_encrypted(&rlocator))
tde_smgr_remove_key(&rlocator);
tde_smgr_delete_key(&rlocator);
}
}

Expand Down Expand Up @@ -384,7 +391,7 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
* If we're in redo, a separate WAL record will make sure the key is
* removed.
*/
tde_smgr_delete_key(&reln->smgr_rlocator);
tde_smgr_delete_leftover_key(&reln->smgr_rlocator);

if (!tde_smgr_should_encrypt(&reln->smgr_rlocator, &relold))
{
Expand All @@ -393,7 +400,6 @@ tde_mdcreate(RelFileLocator relold, SMgrRelation reln, ForkNumber forknum, bool
}

key = tde_smgr_create_key(&reln->smgr_rlocator);
tde_smgr_log_create_key(&reln->smgr_rlocator);

tdereln->encryption_status = RELATION_KEY_AVAILABLE;
tdereln->relKey = *key;
Expand Down Expand Up @@ -508,7 +514,7 @@ tde_smgr_has_temp_key(const RelFileLocator *rel)
}

static void
tde_smgr_remove_temp_key(const RelFileLocator *rel)
tde_smgr_delete_temp_key(const RelFileLocator *rel)
{
Assert(TempRelKeys);
hash_search(TempRelKeys, rel, HASH_REMOVE, NULL);
Expand Down
Loading