@@ -60,7 +60,6 @@ typedef struct TdePrincipalKeySharedState
60
60
LWLockPadded * Locks ;
61
61
dshash_table_handle hashHandle ;
62
62
void * rawDsaArea ; /* DSA area pointer */
63
-
64
63
} TdePrincipalKeySharedState ;
65
64
66
65
typedef struct TdePrincipalKeylocalState
@@ -84,10 +83,6 @@ static dshash_parameters principal_key_dsh_params = {
84
83
static TdePrincipalKeylocalState principalKeyLocalState ;
85
84
86
85
static void principal_key_info_attach_shmem (void );
87
- static Size initialize_shared_state (void * start_address );
88
- static void initialize_objects_in_dsa_area (dsa_area * dsa , void * raw_dsa_area );
89
- static Size required_shared_mem_size (void );
90
- static void shared_memory_shutdown (int code , Datum arg );
91
86
static void clear_principal_key_cache (Oid databaseId );
92
87
static inline dshash_table * get_principal_key_hash (void );
93
88
static TDEPrincipalKey * get_principal_key_from_cache (Oid dbOid );
@@ -111,32 +106,6 @@ PG_FUNCTION_INFO_V1(pg_tde_set_server_key_using_global_key_provider);
111
106
112
107
static void pg_tde_set_principal_key_internal (Oid providerOid , Oid dbOid , const char * principal_key_name , const char * provider_name , bool ensure_new_key );
113
108
114
- static const TDEShmemSetupRoutine principal_key_info_shmem_routine = {
115
- .init_shared_state = initialize_shared_state ,
116
- .init_dsa_area_objects = initialize_objects_in_dsa_area ,
117
- .required_shared_mem_size = required_shared_mem_size ,
118
- .shmem_kill = shared_memory_shutdown
119
- };
120
-
121
- void
122
- InitializePrincipalKeyInfo (void )
123
- {
124
- ereport (LOG , errmsg ("Initializing TDE principal key info" ));
125
- RegisterShmemRequest (& principal_key_info_shmem_routine );
126
- }
127
-
128
- /*
129
- * Lock to guard internal/principal key. Usually, this lock has to be held until
130
- * the caller fetches an internal_key or rotates the principal.
131
- */
132
- LWLock *
133
- tde_lwlock_enc_keys (void )
134
- {
135
- Assert (principalKeyLocalState .sharedPrincipalKeyState );
136
-
137
- return & principalKeyLocalState .sharedPrincipalKeyState -> Locks [TDE_LWLOCK_ENC_KEY ].lock ;
138
- }
139
-
140
109
/*
141
110
* Request some pages so we can fit the DSA header, empty hash table plus some
142
111
* extra. Additional memory to grow the hash map will be allocated as needed
@@ -147,50 +116,84 @@ tde_lwlock_enc_keys(void)
147
116
*/
148
117
#define CACHE_DSA_INITIAL_SIZE (4096 * 64)
149
118
150
- static Size
151
- required_shared_mem_size (void )
119
+ Size
120
+ PrincipalKeyShmemSize (void )
152
121
{
153
122
Size sz = CACHE_DSA_INITIAL_SIZE ;
154
123
155
124
sz = add_size (sz , sizeof (TdePrincipalKeySharedState ));
156
125
return MAXALIGN (sz );
157
126
}
158
127
159
- /*
160
- * Initialize the shared area for Principal key info.
161
- * This includes locks and cache area for principal key info
162
- */
163
-
164
- static Size
165
- initialize_shared_state (void * start_address )
128
+ void
129
+ PrincipalKeyShmemInit (void )
166
130
{
167
- TdePrincipalKeySharedState * sharedState = (TdePrincipalKeySharedState * ) start_address ;
131
+ bool found ;
132
+ char * free_start ;
133
+ Size required_shmem_size = PrincipalKeyShmemSize ();
168
134
169
- ereport ( LOG , errmsg ( "initializing shared state for principal key" ) );
135
+ LWLockAcquire ( AddinShmemInitLock , LW_EXCLUSIVE );
170
136
171
- sharedState -> Locks = GetNamedLWLockTranche (TDE_TRANCHE_NAME );
137
+ /* Create or attach to the shared memory state */
138
+ ereport (NOTICE , errmsg ("PrincipalKeyShmemInit: requested %ld bytes" , required_shmem_size ));
139
+ free_start = ShmemInitStruct ("pg_tde" , required_shmem_size , & found );
172
140
173
- principalKeyLocalState .sharedPrincipalKeyState = sharedState ;
174
- principalKeyLocalState .sharedHash = NULL ;
141
+ if (!found )
142
+ {
143
+ TdePrincipalKeySharedState * sharedState ;
144
+ Size sz ;
145
+ Size dsa_area_size ;
146
+ dsa_area * dsa ;
147
+ dshash_table * dsh ;
175
148
176
- return sizeof (TdePrincipalKeySharedState );
177
- }
149
+ /* Now place shared state structure */
150
+ sharedState = (TdePrincipalKeySharedState * ) free_start ;
151
+ sz = MAXALIGN (sizeof (TdePrincipalKeySharedState ));
152
+ free_start += sz ;
153
+ Assert (sz <= required_shmem_size );
178
154
179
- static void
180
- initialize_objects_in_dsa_area (dsa_area * dsa , void * raw_dsa_area )
181
- {
182
- dshash_table * dsh ;
183
- TdePrincipalKeySharedState * sharedState = principalKeyLocalState .sharedPrincipalKeyState ;
155
+ /* Create DSA area */
156
+ dsa_area_size = required_shmem_size - sz ;
157
+ Assert (dsa_area_size > 0 );
158
+
159
+ ereport (LOG , errmsg ("creating DSA area of size %lu" , dsa_area_size ));
160
+
161
+ dsa = dsa_create_in_place (free_start ,
162
+ dsa_area_size ,
163
+ LWLockNewTrancheId (), 0 );
164
+ dsa_pin (dsa );
165
+
166
+ /* Limit area size during population to get a nice error */
167
+ dsa_set_size_limit (dsa , dsa_area_size );
168
+
169
+ principal_key_dsh_params .tranche_id = LWLockNewTrancheId ();
170
+ dsh = dshash_create (dsa , & principal_key_dsh_params , NULL );
171
+
172
+ dsa_set_size_limit (dsa , -1 );
184
173
185
- ereport (LOG , errmsg ("initializing dsa area objects for principal key" ));
174
+ sharedState -> Locks = GetNamedLWLockTranche (TDE_TRANCHE_NAME );
175
+ sharedState -> hashHandle = dshash_get_hash_table_handle (dsh );
176
+ sharedState -> rawDsaArea = free_start ;
186
177
187
- Assert (sharedState != NULL );
178
+ principalKeyLocalState .sharedPrincipalKeyState = sharedState ;
179
+ principalKeyLocalState .sharedHash = NULL ;
188
180
189
- sharedState -> rawDsaArea = raw_dsa_area ;
190
- principal_key_dsh_params .tranche_id = LWLockNewTrancheId ();
191
- dsh = dshash_create (dsa , & principal_key_dsh_params , NULL );
192
- sharedState -> hashHandle = dshash_get_hash_table_handle (dsh );
193
- dshash_detach (dsh );
181
+ dshash_detach (dsh );
182
+ }
183
+
184
+ LWLockRelease (AddinShmemInitLock );
185
+ }
186
+
187
+ /*
188
+ * Lock to guard internal/principal key. Usually, this lock has to be held until
189
+ * the caller fetches an internal_key or rotates the principal.
190
+ */
191
+ LWLock *
192
+ tde_lwlock_enc_keys (void )
193
+ {
194
+ Assert (principalKeyLocalState .sharedPrincipalKeyState );
195
+
196
+ return & principalKeyLocalState .sharedPrincipalKeyState -> Locks [TDE_LWLOCK_ENC_KEY ].lock ;
194
197
}
195
198
196
199
/*
@@ -217,12 +220,6 @@ principal_key_info_attach_shmem(void)
217
220
MemoryContextSwitchTo (oldcontext );
218
221
}
219
222
220
- static void
221
- shared_memory_shutdown (int code , Datum arg )
222
- {
223
- principalKeyLocalState .sharedPrincipalKeyState = NULL ;
224
- }
225
-
226
223
void
227
224
set_principal_key_with_keyring (const char * key_name , const char * provider_name ,
228
225
Oid providerOid , Oid dbOid , bool ensure_new_key )
0 commit comments