|
|
@ -113,11 +113,11 @@ typedef struct RelKeyCache |
|
|
|
RelKeyCache *tde_rel_key_cache = NULL; |
|
|
|
RelKeyCache *tde_rel_key_cache = NULL; |
|
|
|
|
|
|
|
|
|
|
|
static int32 pg_tde_process_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, off_t *offset, bool should_delete); |
|
|
|
static int32 pg_tde_process_map_entry(const RelFileLocator *rlocator, uint32 key_type, char *db_map_path, off_t *offset, bool should_delete); |
|
|
|
static RelKeyData* pg_tde_read_keydata(char *db_keydata_path, int32 key_index, TDEPrincipalKey *principal_key); |
|
|
|
static RelKeyData *pg_tde_read_keydata(char *db_keydata_path, int32 key_index, TDEPrincipalKey *principal_key); |
|
|
|
static int pg_tde_open_file_basic(char *tde_filename, int fileFlags, bool ignore_missing); |
|
|
|
static int pg_tde_open_file_basic(char *tde_filename, int fileFlags, bool ignore_missing); |
|
|
|
static int pg_tde_file_header_read(char *tde_filename, int fd, TDEFileHeader *fheader, bool *is_new_file, off_t *bytes_read); |
|
|
|
static int pg_tde_file_header_read(char *tde_filename, int fd, TDEFileHeader *fheader, bool *is_new_file, off_t *bytes_read); |
|
|
|
static bool pg_tde_read_one_map_entry(int fd, const RelFileLocator *rlocator, int flags, TDEMapEntry *map_entry, off_t *offset); |
|
|
|
static bool pg_tde_read_one_map_entry(int fd, const RelFileLocator *rlocator, int flags, TDEMapEntry *map_entry, off_t *offset); |
|
|
|
static RelKeyData* pg_tde_read_one_keydata(int keydata_fd, int32 key_index, TDEPrincipalKey *principal_key); |
|
|
|
static RelKeyData *pg_tde_read_one_keydata(int keydata_fd, int32 key_index, TDEPrincipalKey *principal_key); |
|
|
|
static int pg_tde_open_file(char *tde_filename, TDEPrincipalKeyInfo *principal_key_info, bool should_fill_info, int fileFlags, bool *is_new_file, off_t *offset); |
|
|
|
static int pg_tde_open_file(char *tde_filename, TDEPrincipalKeyInfo *principal_key_info, bool should_fill_info, int fileFlags, bool *is_new_file, off_t *offset); |
|
|
|
static RelKeyData *pg_tde_get_key_from_cache(RelFileNumber rel_number, uint32 key_type); |
|
|
|
static RelKeyData *pg_tde_get_key_from_cache(RelFileNumber rel_number, uint32 key_type); |
|
|
|
|
|
|
|
|
|
|
@ -218,7 +218,7 @@ tde_sprint_key(InternalKey *k) |
|
|
|
int i; |
|
|
|
int i; |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(k->key); i++) |
|
|
|
for (i = 0; i < sizeof(k->key); i++) |
|
|
|
sprintf(buf+i, "%02X", k->key[i]); |
|
|
|
sprintf(buf + i, "%02X", k->key[i]); |
|
|
|
|
|
|
|
|
|
|
|
return buf; |
|
|
|
return buf; |
|
|
|
} |
|
|
|
} |
|
|
@ -231,6 +231,7 @@ RelKeyData * |
|
|
|
tde_create_rel_key(RelFileNumber rel_num, InternalKey *key, TDEPrincipalKeyInfo *principal_key_info) |
|
|
|
tde_create_rel_key(RelFileNumber rel_num, InternalKey *key, TDEPrincipalKeyInfo *principal_key_info) |
|
|
|
{ |
|
|
|
{ |
|
|
|
RelKeyData rel_key_data; |
|
|
|
RelKeyData rel_key_data; |
|
|
|
|
|
|
|
|
|
|
|
memcpy(&rel_key_data.principal_key_id, &principal_key_info->keyId, sizeof(TDEPrincipalKeyId)); |
|
|
|
memcpy(&rel_key_data.principal_key_id, &principal_key_info->keyId, sizeof(TDEPrincipalKeyId)); |
|
|
|
memcpy(&rel_key_data.internal_key, key, sizeof(InternalKey)); |
|
|
|
memcpy(&rel_key_data.internal_key, key, sizeof(InternalKey)); |
|
|
|
rel_key_data.internal_key.ctx = NULL; |
|
|
|
rel_key_data.internal_key.ctx = NULL; |
|
|
@ -238,6 +239,7 @@ tde_create_rel_key(RelFileNumber rel_num, InternalKey *key, TDEPrincipalKeyInfo |
|
|
|
/* Add to the decrypted key to cache */ |
|
|
|
/* Add to the decrypted key to cache */ |
|
|
|
return pg_tde_put_key_into_cache(rel_num, &rel_key_data); |
|
|
|
return pg_tde_put_key_into_cache(rel_num, &rel_key_data); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Encrypts a given key and returns the encrypted one. |
|
|
|
* Encrypts a given key and returns the encrypted one. |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -373,15 +375,18 @@ pg_tde_write_map_entry(const RelFileLocator *rlocator, uint32 entry_type, char * |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Read until we find an empty slot. Otherwise, read until end. This seems |
|
|
|
* Read until we find an empty slot. Otherwise, read until end. This seems |
|
|
|
* to be less frequent than vacuum. So let's keep this function here rather |
|
|
|
* to be less frequent than vacuum. So let's keep this function here |
|
|
|
* than overloading the vacuum process. |
|
|
|
* rather than overloading the vacuum process. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
while(1) |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
{ |
|
|
|
prev_pos = curr_pos; |
|
|
|
prev_pos = curr_pos; |
|
|
|
found = pg_tde_read_one_map_entry(map_fd, NULL, MAP_ENTRY_EMPTY, &map_entry, &curr_pos); |
|
|
|
found = pg_tde_read_one_map_entry(map_fd, NULL, MAP_ENTRY_EMPTY, &map_entry, &curr_pos); |
|
|
|
|
|
|
|
|
|
|
|
/* We either reach EOF or found an empty slot in the middle of the file */ |
|
|
|
/*
|
|
|
|
|
|
|
|
* We either reach EOF or found an empty slot in the middle of the |
|
|
|
|
|
|
|
* file |
|
|
|
|
|
|
|
*/ |
|
|
|
if (prev_pos == curr_pos || found) |
|
|
|
if (prev_pos == curr_pos || found) |
|
|
|
break; |
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
@ -389,7 +394,10 @@ pg_tde_write_map_entry(const RelFileLocator *rlocator, uint32 entry_type, char * |
|
|
|
key_index++; |
|
|
|
key_index++; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Write the given entry at the location pointed by prev_pos; i.e. the free entry */ |
|
|
|
/*
|
|
|
|
|
|
|
|
* Write the given entry at the location pointed by prev_pos; i.e. the |
|
|
|
|
|
|
|
* free entry |
|
|
|
|
|
|
|
*/ |
|
|
|
curr_pos = prev_pos; |
|
|
|
curr_pos = prev_pos; |
|
|
|
pg_tde_write_one_map_entry(map_fd, rlocator, entry_type, key_index, &map_entry, &prev_pos); |
|
|
|
pg_tde_write_one_map_entry(map_fd, rlocator, entry_type, key_index, &map_entry, &prev_pos); |
|
|
|
|
|
|
|
|
|
|
@ -425,6 +433,7 @@ pg_tde_write_one_map_entry(int fd, const RelFileLocator *rlocator, uint32 flags, |
|
|
|
if (bytes_written != MAP_ENTRY_SIZE) |
|
|
|
if (bytes_written != MAP_ENTRY_SIZE) |
|
|
|
{ |
|
|
|
{ |
|
|
|
char db_map_path[MAXPGPATH] = {0}; |
|
|
|
char db_map_path[MAXPGPATH] = {0}; |
|
|
|
|
|
|
|
|
|
|
|
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, NULL); |
|
|
|
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, NULL); |
|
|
|
ereport(FATAL, |
|
|
|
ereport(FATAL, |
|
|
|
(errcode_for_file_access(), |
|
|
|
(errcode_for_file_access(), |
|
|
@ -434,6 +443,7 @@ pg_tde_write_one_map_entry(int fd, const RelFileLocator *rlocator, uint32 flags, |
|
|
|
if (pg_fsync(fd) != 0) |
|
|
|
if (pg_fsync(fd) != 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
char db_map_path[MAXPGPATH] = {0}; |
|
|
|
char db_map_path[MAXPGPATH] = {0}; |
|
|
|
|
|
|
|
|
|
|
|
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, NULL); |
|
|
|
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, NULL); |
|
|
|
ereport(data_sync_elevel(ERROR), |
|
|
|
ereport(data_sync_elevel(ERROR), |
|
|
|
(errcode_for_file_access(), |
|
|
|
(errcode_for_file_access(), |
|
|
@ -528,7 +538,8 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, RelKeyData *enc_rel_k |
|
|
|
* Deletes a map entry by setting marking it as unused. We don't have to delete |
|
|
|
* Deletes a map entry by setting marking it as unused. We don't have to delete |
|
|
|
* the actual key data as valid key data entries are identify by valid map entries. |
|
|
|
* the actual key data as valid key data entries are identify by valid map entries. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void pg_tde_delete_key_map_entry(const RelFileLocator *rlocator, uint32 key_type) |
|
|
|
void |
|
|
|
|
|
|
|
pg_tde_delete_key_map_entry(const RelFileLocator *rlocator, uint32 key_type) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int32 key_index = 0; |
|
|
|
int32 key_index = 0; |
|
|
|
off_t offset = 0; |
|
|
|
off_t offset = 0; |
|
|
@ -575,7 +586,8 @@ void pg_tde_delete_key_map_entry(const RelFileLocator *rlocator, uint32 key_type |
|
|
|
*
|
|
|
|
*
|
|
|
|
* A caller should hold an EXCLUSIVE tde_lwlock_enc_keys lock. |
|
|
|
* A caller should hold an EXCLUSIVE tde_lwlock_enc_keys lock. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void pg_tde_free_key_map_entry(const RelFileLocator *rlocator, uint32 key_type, off_t offset) |
|
|
|
void |
|
|
|
|
|
|
|
pg_tde_free_key_map_entry(const RelFileLocator *rlocator, uint32 key_type, off_t offset) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int32 key_index = 0; |
|
|
|
int32 key_index = 0; |
|
|
|
char db_map_path[MAXPGPATH] = {0}; |
|
|
|
char db_map_path[MAXPGPATH] = {0}; |
|
|
@ -621,7 +633,10 @@ void pg_tde_free_key_map_entry(const RelFileLocator *rlocator, uint32 key_type, |
|
|
|
static File |
|
|
|
static File |
|
|
|
keyrotation_init_file(TDEPrincipalKeyInfo *new_principal_key_info, char *rotated_filename, char *filename, bool *is_new_file, off_t *curr_pos) |
|
|
|
keyrotation_init_file(TDEPrincipalKeyInfo *new_principal_key_info, char *rotated_filename, char *filename, bool *is_new_file, off_t *curr_pos) |
|
|
|
{ |
|
|
|
{ |
|
|
|
/* Set the new filenames for the key rotation process - temporary at the moment */ |
|
|
|
/*
|
|
|
|
|
|
|
|
* Set the new filenames for the key rotation process - temporary at the |
|
|
|
|
|
|
|
* moment |
|
|
|
|
|
|
|
*/ |
|
|
|
snprintf(rotated_filename, MAXPGPATH, "%s.r", filename); |
|
|
|
snprintf(rotated_filename, MAXPGPATH, "%s.r", filename); |
|
|
|
|
|
|
|
|
|
|
|
/* Create file, truncate if the rotate file already exits */ |
|
|
|
/* Create file, truncate if the rotate file already exits */ |
|
|
@ -677,11 +692,17 @@ pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_p |
|
|
|
principal_key->keyInfo.tablespaceId, |
|
|
|
principal_key->keyInfo.tablespaceId, |
|
|
|
db_map_path, db_keydata_path); |
|
|
|
db_map_path, db_keydata_path); |
|
|
|
|
|
|
|
|
|
|
|
/* Let's update the pathnames in the local variable for ease of use/readability */ |
|
|
|
/*
|
|
|
|
|
|
|
|
* Let's update the pathnames in the local variable for ease of |
|
|
|
|
|
|
|
* use/readability |
|
|
|
|
|
|
|
*/ |
|
|
|
strncpy(m_path[OLD_PRINCIPAL_KEY], db_map_path, MAXPGPATH); |
|
|
|
strncpy(m_path[OLD_PRINCIPAL_KEY], db_map_path, MAXPGPATH); |
|
|
|
strncpy(k_path[OLD_PRINCIPAL_KEY], db_keydata_path, MAXPGPATH); |
|
|
|
strncpy(k_path[OLD_PRINCIPAL_KEY], db_keydata_path, MAXPGPATH); |
|
|
|
|
|
|
|
|
|
|
|
/* Open both files in read only mode. We don't need to track the current position of the keydata file. We always use the key index */ |
|
|
|
/*
|
|
|
|
|
|
|
|
* Open both files in read only mode. We don't need to track the current |
|
|
|
|
|
|
|
* position of the keydata file. We always use the key index |
|
|
|
|
|
|
|
*/ |
|
|
|
m_fd[OLD_PRINCIPAL_KEY] = pg_tde_open_file(m_path[OLD_PRINCIPAL_KEY], &principal_key->keyInfo, false, O_RDONLY, &is_new_file, &curr_pos[OLD_PRINCIPAL_KEY]); |
|
|
|
m_fd[OLD_PRINCIPAL_KEY] = pg_tde_open_file(m_path[OLD_PRINCIPAL_KEY], &principal_key->keyInfo, false, O_RDONLY, &is_new_file, &curr_pos[OLD_PRINCIPAL_KEY]); |
|
|
|
k_fd[OLD_PRINCIPAL_KEY] = pg_tde_open_file(k_path[OLD_PRINCIPAL_KEY], &principal_key->keyInfo, false, O_RDONLY, &is_new_file, &read_pos_tmp); |
|
|
|
k_fd[OLD_PRINCIPAL_KEY] = pg_tde_open_file(k_path[OLD_PRINCIPAL_KEY], &principal_key->keyInfo, false, O_RDONLY, &is_new_file, &read_pos_tmp); |
|
|
|
|
|
|
|
|
|
|
@ -689,9 +710,10 @@ pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_p |
|
|
|
k_fd[NEW_PRINCIPAL_KEY] = keyrotation_init_file(&new_principal_key->keyInfo, k_path[NEW_PRINCIPAL_KEY], k_path[OLD_PRINCIPAL_KEY], &is_new_file, &read_pos_tmp); |
|
|
|
k_fd[NEW_PRINCIPAL_KEY] = keyrotation_init_file(&new_principal_key->keyInfo, k_path[NEW_PRINCIPAL_KEY], k_path[OLD_PRINCIPAL_KEY], &is_new_file, &read_pos_tmp); |
|
|
|
|
|
|
|
|
|
|
|
/* Read all entries until EOF */ |
|
|
|
/* Read all entries until EOF */ |
|
|
|
for(key_index[OLD_PRINCIPAL_KEY] = 0; ; key_index[OLD_PRINCIPAL_KEY]++) |
|
|
|
for (key_index[OLD_PRINCIPAL_KEY] = 0;; key_index[OLD_PRINCIPAL_KEY]++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
TDEMapEntry read_map_entry, write_map_entry; |
|
|
|
TDEMapEntry read_map_entry, |
|
|
|
|
|
|
|
write_map_entry; |
|
|
|
RelFileLocator rloc; |
|
|
|
RelFileLocator rloc; |
|
|
|
|
|
|
|
|
|
|
|
prev_pos[OLD_PRINCIPAL_KEY] = curr_pos[OLD_PRINCIPAL_KEY]; |
|
|
|
prev_pos[OLD_PRINCIPAL_KEY] = curr_pos[OLD_PRINCIPAL_KEY]; |
|
|
@ -705,10 +727,13 @@ pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_p |
|
|
|
if (found == false) |
|
|
|
if (found == false) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
/* Set the relNumber of rlocator. Ignore the tablespace Oid since we only place our files under the default. */ |
|
|
|
/*
|
|
|
|
|
|
|
|
* Set the relNumber of rlocator. Ignore the tablespace Oid since we |
|
|
|
|
|
|
|
* only place our files under the default. |
|
|
|
|
|
|
|
*/ |
|
|
|
rloc.relNumber = read_map_entry.relNumber; |
|
|
|
rloc.relNumber = read_map_entry.relNumber; |
|
|
|
rloc.dbOid = principal_key->keyInfo.databaseId; |
|
|
|
rloc.dbOid = principal_key->keyInfo.databaseId; |
|
|
|
rloc.spcOid = DEFAULTTABLESPACE_OID; /* TODO. Revisit*/ |
|
|
|
rloc.spcOid = DEFAULTTABLESPACE_OID; /* TODO. Revisit */ |
|
|
|
|
|
|
|
|
|
|
|
/* Let's get the decrypted key and re-encrypt it with the new key. */ |
|
|
|
/* Let's get the decrypted key and re-encrypt it with the new key. */ |
|
|
|
enc_rel_key_data[OLD_PRINCIPAL_KEY] = pg_tde_read_one_keydata(k_fd[OLD_PRINCIPAL_KEY], key_index[OLD_PRINCIPAL_KEY], principal_key); |
|
|
|
enc_rel_key_data[OLD_PRINCIPAL_KEY] = pg_tde_read_one_keydata(k_fd[OLD_PRINCIPAL_KEY], key_index[OLD_PRINCIPAL_KEY], principal_key); |
|
|
@ -955,12 +980,11 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator, uint32 key_type, bool n |
|
|
|
* |
|
|
|
* |
|
|
|
* We should hold the lock until the internal key is loaded to be sure the |
|
|
|
* We should hold the lock until the internal key is loaded to be sure the |
|
|
|
* retrieved key was encrypted with the obtained principal key. Otherwise, |
|
|
|
* retrieved key was encrypted with the obtained principal key. Otherwise, |
|
|
|
* the next may happen:
|
|
|
|
* the next may happen: - GetPrincipalKey returns key "PKey_1". - Some |
|
|
|
* - GetPrincipalKey returns key "PKey_1". |
|
|
|
* other process rotates the Principal key and re-encrypt an Internal key |
|
|
|
* - Some other process rotates the Principal key and re-encrypt an |
|
|
|
* with "PKey_2". - We read the Internal key and decrypt it with "PKey_1" |
|
|
|
* Internal key with "PKey_2". |
|
|
|
* (that's what we've got). As the result we return an invalid Internal |
|
|
|
* - We read the Internal key and decrypt it with "PKey_1" (that's what
|
|
|
|
* key. |
|
|
|
* we've got). As the result we return an invalid Internal key. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
LWLockAcquire(lock_pk, LW_SHARED); |
|
|
|
LWLockAcquire(lock_pk, LW_SHARED); |
|
|
|
principal_key = GetPrincipalKey(rlocator->dbOid, rlocator->spcOid, LW_SHARED); |
|
|
|
principal_key = GetPrincipalKey(rlocator->dbOid, rlocator->spcOid, LW_SHARED); |
|
|
@ -1031,8 +1055,8 @@ pg_tde_process_map_entry(const RelFileLocator *rlocator, uint32 key_type, char * |
|
|
|
Assert(offset); |
|
|
|
Assert(offset); |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Open and validate file for basic correctness. DO NOT create it. |
|
|
|
* Open and validate file for basic correctness. DO NOT create it. The |
|
|
|
* The file should pre-exist otherwise we should never be here. |
|
|
|
* file should pre-exist otherwise we should never be here. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
map_fd = pg_tde_open_file(db_map_path, NULL, false, O_RDWR, &is_new_file, &curr_pos); |
|
|
|
map_fd = pg_tde_open_file(db_map_path, NULL, false, O_RDWR, &is_new_file, &curr_pos); |
|
|
|
|
|
|
|
|
|
|
@ -1061,10 +1085,10 @@ pg_tde_process_map_entry(const RelFileLocator *rlocator, uint32 key_type, char * |
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Read until we find an empty slot. Otherwise, read until end. This seems |
|
|
|
* Read until we find an empty slot. Otherwise, read until end. This seems |
|
|
|
* to be less frequent than vacuum. So let's keep this function here rather |
|
|
|
* to be less frequent than vacuum. So let's keep this function here |
|
|
|
* than overloading the vacuum process. |
|
|
|
* rather than overloading the vacuum process. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
while(1) |
|
|
|
while (1) |
|
|
|
{ |
|
|
|
{ |
|
|
|
prev_pos = curr_pos; |
|
|
|
prev_pos = curr_pos; |
|
|
|
found = pg_tde_read_one_map_entry(map_fd, rlocator, key_type, &map_entry, &curr_pos); |
|
|
|
found = pg_tde_read_one_map_entry(map_fd, rlocator, key_type, &map_entry, &curr_pos); |
|
|
@ -1300,6 +1324,7 @@ pg_tde_read_one_keydata(int keydata_fd, int32 key_index, TDEPrincipalKey *princi |
|
|
|
if ((read_pos + INTERNAL_KEY_DAT_LEN) > lseek(keydata_fd, 0, SEEK_END)) |
|
|
|
if ((read_pos + INTERNAL_KEY_DAT_LEN) > lseek(keydata_fd, 0, SEEK_END)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
char db_keydata_path[MAXPGPATH] = {0}; |
|
|
|
char db_keydata_path[MAXPGPATH] = {0}; |
|
|
|
|
|
|
|
|
|
|
|
pg_tde_set_db_file_paths(principal_key->keyInfo.databaseId, principal_key->keyInfo.tablespaceId, NULL, db_keydata_path); |
|
|
|
pg_tde_set_db_file_paths(principal_key->keyInfo.databaseId, principal_key->keyInfo.tablespaceId, NULL, db_keydata_path); |
|
|
|
ereport(FATAL, |
|
|
|
ereport(FATAL, |
|
|
|
(errcode(ERRCODE_NO_DATA_FOUND), |
|
|
|
(errcode(ERRCODE_NO_DATA_FOUND), |
|
|
@ -1313,6 +1338,7 @@ pg_tde_read_one_keydata(int keydata_fd, int32 key_index, TDEPrincipalKey *princi |
|
|
|
if (pg_pread(keydata_fd, &(enc_rel_key_data->internal_key), INTERNAL_KEY_DAT_LEN, read_pos) != INTERNAL_KEY_DAT_LEN) |
|
|
|
if (pg_pread(keydata_fd, &(enc_rel_key_data->internal_key), INTERNAL_KEY_DAT_LEN, read_pos) != INTERNAL_KEY_DAT_LEN) |
|
|
|
{ |
|
|
|
{ |
|
|
|
char db_keydata_path[MAXPGPATH] = {0}; |
|
|
|
char db_keydata_path[MAXPGPATH] = {0}; |
|
|
|
|
|
|
|
|
|
|
|
pg_tde_set_db_file_paths(principal_key->keyInfo.databaseId, principal_key->keyInfo.tablespaceId, NULL, db_keydata_path); |
|
|
|
pg_tde_set_db_file_paths(principal_key->keyInfo.databaseId, principal_key->keyInfo.tablespaceId, NULL, db_keydata_path); |
|
|
|
ereport(FATAL, |
|
|
|
ereport(FATAL, |
|
|
|
(errcode_for_file_access(), |
|
|
|
(errcode_for_file_access(), |
|
|
@ -1356,7 +1382,10 @@ pg_tde_get_principal_key_info(Oid dbOid, Oid spcOid) |
|
|
|
|
|
|
|
|
|
|
|
close(fd); |
|
|
|
close(fd); |
|
|
|
|
|
|
|
|
|
|
|
/* It's not a new file. So we can memcpy the principal key info from the header */ |
|
|
|
/*
|
|
|
|
|
|
|
|
* It's not a new file. So we can memcpy the principal key info from the |
|
|
|
|
|
|
|
* header |
|
|
|
|
|
|
|
*/ |
|
|
|
if (!is_new_file) |
|
|
|
if (!is_new_file) |
|
|
|
{ |
|
|
|
{ |
|
|
|
size_t sz = sizeof(TDEPrincipalKeyInfo); |
|
|
|
size_t sz = sizeof(TDEPrincipalKeyInfo); |
|
|
@ -1427,7 +1456,7 @@ pg_tde_get_key_from_cache(RelFileNumber rel_number, uint32 key_type) |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < tde_rel_key_cache->len; i++) |
|
|
|
for (int i = 0; i < tde_rel_key_cache->len; i++) |
|
|
|
{ |
|
|
|
{ |
|
|
|
rec = tde_rel_key_cache->data+i; |
|
|
|
rec = tde_rel_key_cache->data + i; |
|
|
|
if (rec != NULL && |
|
|
|
if (rec != NULL && |
|
|
|
(rel_number == InvalidOid || (rec->rel_number == rel_number)) && |
|
|
|
(rel_number == InvalidOid || (rec->rel_number == rel_number)) && |
|
|
|
rec->key.internal_key.rel_type & key_type) |
|
|
|
rec->key.internal_key.rel_type & key_type) |
|
|
@ -1454,9 +1483,9 @@ pg_tde_put_key_into_cache(RelFileNumber rel_num, RelKeyData *key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
#ifndef _SC_PAGESIZE |
|
|
|
#ifndef _SC_PAGESIZE |
|
|
|
pageSize = getpagesize(); |
|
|
|
pageSize = getpagesize(); |
|
|
|
#else |
|
|
|
#else |
|
|
|
pageSize = sysconf(_SC_PAGESIZE); |
|
|
|
pageSize = sysconf(_SC_PAGESIZE); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (tde_rel_key_cache == NULL) |
|
|
|
if (tde_rel_key_cache == NULL) |
|
|
@ -1479,17 +1508,18 @@ pg_tde_put_key_into_cache(RelFileNumber rel_num, RelKeyData *key) |
|
|
|
tde_rel_key_cache->cap = pageSize / sizeof(RelKeyCacheRec); |
|
|
|
tde_rel_key_cache->cap = pageSize / sizeof(RelKeyCacheRec); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Add another mem page if there is no more room left for another key. We
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Add another mem page if there is no more room left for another key. We |
|
|
|
* allocate `current_memory_size` + 1 page and copy data there. |
|
|
|
* allocate `current_memory_size` + 1 page and copy data there. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
if (tde_rel_key_cache->len+1 >
|
|
|
|
if (tde_rel_key_cache->len + 1 > |
|
|
|
(tde_rel_key_cache->cap * sizeof(RelKeyCacheRec)) / sizeof(RelKeyCacheRec)) |
|
|
|
(tde_rel_key_cache->cap * sizeof(RelKeyCacheRec)) / sizeof(RelKeyCacheRec)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
size_t size; |
|
|
|
size_t size; |
|
|
|
size_t old_size; |
|
|
|
size_t old_size; |
|
|
|
RelKeyCacheRec *chachePage; |
|
|
|
RelKeyCacheRec *chachePage; |
|
|
|
|
|
|
|
|
|
|
|
size = TYPEALIGN(pageSize, (tde_rel_key_cache->cap+1) * sizeof(RelKeyCacheRec)); |
|
|
|
size = TYPEALIGN(pageSize, (tde_rel_key_cache->cap + 1) * sizeof(RelKeyCacheRec)); |
|
|
|
old_size = TYPEALIGN(pageSize, (tde_rel_key_cache->cap) * sizeof(RelKeyCacheRec)); |
|
|
|
old_size = TYPEALIGN(pageSize, (tde_rel_key_cache->cap) * sizeof(RelKeyCacheRec)); |
|
|
|
|
|
|
|
|
|
|
|
#ifndef FRONTEND |
|
|
|
#ifndef FRONTEND |
|
|
|