2002-04-08 20:04:50 +04:00
/*
2008-01-30 17:00:02 +03:00
* Copyright ( C ) 2002 - 2004 Sistina Software , Inc . All rights reserved .
2007-08-21 00:55:30 +04:00
* Copyright ( C ) 2004 - 2006 Red Hat , Inc . All rights reserved .
2002-04-08 20:04:50 +04:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
2002-04-08 20:04:50 +04:00
*
2004-03-30 23:35:44 +04:00
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2004-03-30 23:35:44 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2002-04-08 20:04:50 +04:00
*/
2002-11-18 17:01:16 +03:00
# include "lib.h"
2002-04-08 20:04:50 +04:00
# include "locking_types.h"
# include "defaults.h"
2002-11-18 17:01:16 +03:00
# include "sharedlib.h"
2006-05-16 20:48:31 +04:00
# include "toolcontext.h"
2011-02-04 22:18:16 +03:00
# include "activate.h"
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
static void * _locking_lib = NULL ;
2004-05-19 01:57:24 +04:00
static void ( * _reset_fn ) ( void ) = NULL ;
2002-11-18 17:01:16 +03:00
static void ( * _end_fn ) ( void ) = NULL ;
static int ( * _lock_fn ) ( struct cmd_context * cmd , const char * resource ,
2007-08-22 18:38:18 +04:00
uint32_t flags ) = NULL ;
2011-08-30 18:55:15 +04:00
static int ( * _init_fn ) ( int type , struct dm_config_tree * cft ,
2004-03-26 23:17:11 +03:00
uint32_t * flags ) = NULL ;
2009-05-20 16:58:03 +04:00
static int ( * _lock_query_fn ) ( const char * resource , int * mode ) = NULL ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
static int _lock_resource ( struct cmd_context * cmd , const char * resource ,
2014-09-22 17:50:07 +04:00
uint32_t flags , const struct logical_volume * lv __attribute__ ( ( unused ) ) )
2002-04-08 20:04:50 +04:00
{
2011-02-04 22:18:16 +03:00
if ( ! _lock_fn )
2002-04-24 22:20:51 +04:00
return 0 ;
2011-02-04 22:18:16 +03:00
if ( ! strcmp ( resource , VG_SYNC_NAMES ) ) {
/* Hide this lock request from external locking */
fs_unlock ( ) ;
return 1 ;
}
return _lock_fn ( cmd , resource , flags ) ;
2002-04-08 20:04:50 +04:00
}
2002-11-18 17:01:16 +03:00
static void _fin_external_locking ( void )
2002-04-08 20:04:50 +04:00
{
2002-11-18 17:01:16 +03:00
if ( _end_fn )
_end_fn ( ) ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
dlclose ( _locking_lib ) ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
_locking_lib = NULL ;
_init_fn = NULL ;
_end_fn = NULL ;
_lock_fn = NULL ;
2004-05-19 01:57:24 +04:00
_reset_fn = NULL ;
}
static void _reset_external_locking ( void )
{
if ( _reset_fn )
_reset_fn ( ) ;
2002-04-08 20:04:50 +04:00
}
2011-08-09 15:44:57 +04:00
int init_external_locking ( struct locking_type * locking , struct cmd_context * cmd ,
int suppress_messages )
2002-04-08 20:04:50 +04:00
{
2002-11-18 17:01:16 +03:00
const char * libname ;
2002-04-08 20:04:50 +04:00
2002-11-18 17:01:16 +03:00
if ( _locking_lib ) {
2011-08-09 15:44:57 +04:00
log_error_suppress ( suppress_messages , " External locking already initialised " ) ;
2002-04-24 22:20:51 +04:00
return 1 ;
2002-04-08 20:04:50 +04:00
}
2002-11-18 17:01:16 +03:00
locking - > lock_resource = _lock_resource ;
locking - > fin_locking = _fin_external_locking ;
2004-05-19 01:57:24 +04:00
locking - > reset_locking = _reset_external_locking ;
2004-03-26 23:17:11 +03:00
locking - > flags = 0 ;
2002-04-08 20:04:50 +04:00
2013-06-25 14:29:54 +04:00
if ( ! ( libname = find_config_tree_str ( cmd , global_locking_library_CFG , NULL ) ) )
2014-03-06 12:41:11 +04:00
return_0 ;
2002-04-24 22:20:51 +04:00
2008-01-30 16:19:47 +03:00
if ( ! ( _locking_lib = load_shared_library ( cmd , libname , " locking " , 1 ) ) )
return_0 ;
2002-04-08 20:04:50 +04:00
/* Get the functions we need */
2004-03-26 23:17:11 +03:00
if ( ! ( _init_fn = dlsym ( _locking_lib , " locking_init " ) ) | |
2002-11-18 17:01:16 +03:00
! ( _lock_fn = dlsym ( _locking_lib , " lock_resource " ) ) | |
2004-05-19 01:57:24 +04:00
! ( _reset_fn = dlsym ( _locking_lib , " reset_locking " ) ) | |
2004-03-26 23:17:11 +03:00
! ( _end_fn = dlsym ( _locking_lib , " locking_end " ) ) ) {
2011-08-09 15:44:57 +04:00
log_error_suppress ( suppress_messages , " Shared library %s does "
" not contain locking functions " , libname ) ;
2002-11-18 17:01:16 +03:00
dlclose ( _locking_lib ) ;
_locking_lib = NULL ;
2002-04-24 22:20:51 +04:00
return 0 ;
2002-04-08 20:04:50 +04:00
}
2009-05-21 07:04:52 +04:00
if ( ! ( _lock_query_fn = dlsym ( _locking_lib , " query_resource " ) ) )
2011-08-09 15:44:57 +04:00
log_warn_suppress ( suppress_messages , " WARNING: %s: _query_resource() "
" missing: Using inferior activation method. " , libname ) ;
2009-05-20 16:58:03 +04:00
2002-11-18 17:01:16 +03:00
log_verbose ( " Loaded external locking library %s " , libname ) ;
2006-05-16 20:48:31 +04:00
return _init_fn ( 2 , cmd - > cft , & locking - > flags ) ;
2002-04-08 20:04:50 +04:00
}