Improve cache management
Changelog:
- add get_serial() function to the policy.h interface, which provides a way to get a serial/commit number for both direct and pserver. This removes part of the dependency of database.* on semanage_store, which we need to get rid of, to support the policy server.
- remove policy_serial variable from the handle, I don't see it being used. It can be added back when necessary. I am not sure the serial at connect time is very important.
- On cache(), check not only if cache exists, but also if it needs resync (expired). Do this by storing the serial on every cache(), and comparing the stored serial against the current one. Stop calling drop_cache() on exiting a read-only dbase call, so the caller can benefit from the cache as long as it's valid. Also, don't call drop_cache() on successful commit, since it will be invalidated automatically by the serial increase.
- Some other simplifications - assume the modified flag implies cached, and don't check both on flush(). Set modified =0 in policydb_detach().
libsemanage/src/database_activedb.c +6 -4
@@ -41,7 +41,7 @@ static int dbase_activedb_cache(unsigned int i =0 ;/* Already cached */ -if (dbase_llist_is_cached (&dbase->llist)) +if (!dbase_llist_needs_resync (handle, &dbase->llist))return STATUS_SUCCESS ;dbase_llist_cache_init (&dbase->llist);
@@ -57,8 +57,11 @@ static int dbase_activedb_cache( rtable->free (records[i]); } +/* Update cache serial */ +if (dbase_llist_set_serial (handle, &dbase->llist) <0 ) + goto err; +free (records); -dbase_llist_set_cached (&dbase->llist,1 );return STATUS_SUCCESS ; err:
@@ -81,8 +84,7 @@ static int dbase_activedb_flush(unsigned int i;/* Not cached, or not modified - flush is not necessary */ -if (!dbase_llist_is_cached (&dbase->llist) || - !dbase_llist_is_modified (&dbase->llist)) +if (!dbase_llist_is_modified (&dbase->llist))return STATUS_SUCCESS ;/* Fetch list */
libsemanage/src/database.c +3 -4
@@ -49,12 +49,11 @@ static inline int exit_ro( semanage_handle_t* handle, dbase_config_t* dconfig) { -int commit_num =semanage_get_commit_number (handle); +int commit_num = handle->funcs->get_serial (handle); -if (!handle->is_in_transaction) { +if (!handle->is_in_transaction)semanage_release_active_lock (handle); - dconfig->dtable->drop_cache (dconfig->dbase); - } +return commit_num; }
libsemanage/src/database_file.c +6 -4
@@ -73,7 +73,7 @@ static int dbase_file_cache(char * fname =NULL ;/* Already cached */ -if (dbase_llist_is_cached (&dbase->llist)) +if (!dbase_llist_needs_resync (handle, &dbase->llist))return STATUS_SUCCESS ;dbase_llist_cache_init (&dbase->llist);
@@ -115,11 +115,14 @@ static int dbase_file_cache( }while (pstatus !=STATUS_NODATA ); +/* Update cache serial */ +if (dbase_llist_set_serial (handle, &dbase->llist) <0 ) + goto err; + rtable->free (process_record);parse_close (parse_info);parse_release (parse_info);free (fname); -dbase_llist_set_cached (&dbase->llist,1 );return STATUS_SUCCESS ; err:
@@ -142,8 +145,7 @@ static int dbase_file_flush(char * fname =NULL ;FILE * str =NULL ; -if (!dbase_llist_is_cached (&dbase->llist) || - !dbase_llist_is_modified (&dbase->llist)) +if (!dbase_llist_is_modified (&dbase->llist))return STATUS_SUCCESS ;if (construct_filename (handle, dbase, &fname) <0 )
libsemanage/src/database.h +3 -1
@@ -181,7 +181,9 @@ typedef struct dbase_table {/* Cache the database (if supported). * This function must be invoked before using -* any of the database functions above */ +* any of the database functions above. It may be invoked +* multiple times, and will update the cache if a commit +* occured between invocations */ int (*cache) (struct semanage_handle * handle,dbase_t * dbase);
libsemanage/src/database_join.c +5 -4
@@ -55,7 +55,7 @@ static int dbase_join_cache(unsigned int rcount1 =0 , rcount2 =0 , i =0 , j =0 ;/* Already cached */ -if (dbase_llist_is_cached (&dbase->llist)) +if (!dbase_llist_needs_resync (handle, &dbase->llist))return STATUS_SUCCESS ;dbase_llist_cache_init (&dbase->llist);
@@ -135,7 +135,9 @@ static int dbase_join_cache( record =NULL ; } -dbase_llist_set_cached (&dbase->llist,1 ); +/* Update cache serial */ +if (dbase_llist_set_serial (handle, &dbase->llist) <0 ) + goto err;for (i=0 ; i < rcount1; i++) rtable1->free (records1[i]);
@@ -179,8 +181,7 @@ static int dbase_join_flush(record2_t * record2 =NULL ;/* No effect of flush */ -if (!dbase_llist_is_cached (&dbase->llist) || - !dbase_llist_is_modified (&dbase->llist)) +if (!dbase_llist_is_modified (&dbase->llist))return STATUS_SUCCESS ;/* Then clear all records from the cache.
libsemanage/src/database_llist.c +40 -7
79 lines not on this page · raw message
libsemanage/src/database_llist.h +8 -11
@@ -25,7 +25,7 @@ typedef struct dbase_llist {cache_entry_t * cache_tail;unsigned int cache_sz; -int cached; +int cache_serial;int modified; } dbase_llist_t;
@@ -37,7 +37,7 @@ static inline void dbase_llist_cache_ini dbase->cache =NULL ; dbase->cache_tail =NULL ; dbase->cache_sz =0 ; - dbase->cached =0 ; + dbase->cache_serial =-1 ; dbase->modified =0 ; }
@@ -56,16 +56,13 @@ extern int dbase_llist_cache_prepend(dbase_llist_t * dbase, const record_t* data); -static inline int dbase_llist_is_cached ( -dbase_llist_t * dbase) { -return dbase->cached; -} +extern int dbase_llist_needs_resync ( +semanage_handle_t * handle, +dbase_llist_t * dbase); -static inline void dbase_llist_set_cached ( -dbase_llist_t * dbase, -int status) { - dbase->cached = status; -} +extern int dbase_llist_set_serial ( +semanage_handle_t * handle, +dbase_llist_t * dbase); static inline void dbase_llist_set_modified( dbase_llist_t* dbase,
libsemanage/src/database_policydb.c +60 -19
133 lines not on this page · raw message
libsemanage/src/direct_api.c +1 -0
@@ -61,6 +61,7 @@ static int semanage_direct_list(semanage semanage_module_info_t **modinfo, int *num_modules);static struct semanage_policy_table direct_funcs = { + .get_serial = semanage_get_commit_number, .destroy = semanage_direct_destroy, .disconnect = semanage_direct_disconnect, .begin_trans = semanage_direct_begintrans,
libsemanage/src/handle.h +0 -1
@@ -34,7 +34,6 @@struct semanage_handle {int con_id;/* Connection ID */ -int policy_serial;/* Policy serial number at connect time */ /* Error handling */ int msg_level;
libsemanage/src/policy_components.c +0 -4
@@ -211,10 +211,6 @@ int semanage_commit_components( goto err; } -/* Drop cache, because we're leaving transaction soon */ -for (i=0 ; i <CCOUNT ; i++) - components[i]->dtable->drop_cache (components[i]->dbase); -return STATUS_SUCCESS ; err:
libsemanage/src/policy.h +4 -0
@@ -30,6 +30,10 @@ struct semanage_handle;/* Backend dependent portion */ struct semanage_policy_table { +/* Returns the current policy serial/commit number +* A negative number is returned in case of failre */ +int (*get_serial)(struct semanage_handle *); +/* Destroy a connection */ void (*destroy)(struct semanage_handle *);
This is a bugfix for the last patch ...
clear must set/update the serial number.
libsemanage/src/database_llist.c +8 -1
@@ -327,7 +327,14 @@ int dbase_llist_clear( semanage_handle_t* handle, dbase_llist_t* dbase) { -if (dbase->cache_serial >=0 ) { +int old_serial = dbase->cache_serial; + +if (dbase_llist_set_serial (handle, dbase) <0 ) { +ERR (handle,"could not set serial of cleared dbase" ); +return STATUS_ERR ; + } + +if (old_serial >=0 ) {cache_entry_t *prev, *ptr = dbase->cache;while (ptr !=NULL ) { prev = ptr;