2001-11-21 15:47:42 +03:00
/*
2004-03-30 23:08:57 +04:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2012-01-09 16:26:14 +04:00
* Copyright ( C ) 2004 - 2012 Red Hat , Inc . All rights reserved .
2001-11-21 15:47:42 +03:00
*
2004-03-30 23:08:57 +04:00
* This file is part of the device - mapper userspace tools .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2001-11-21 15:47:42 +03:00
*/
2008-11-03 21:59:59 +03:00
# include "dmlib.h"
2003-01-22 00:27:36 +03:00
# include "libdm-targets.h"
# include "libdm-common.h"
2003-11-11 00:06:16 +03:00
2001-11-21 15:47:42 +03:00
# include <fcntl.h>
2003-07-02 01:20:58 +04:00
# include <dirent.h>
2003-11-11 00:06:16 +03:00
# include <sys/ioctl.h>
2009-02-12 23:42:07 +03:00
# include <sys/utsname.h>
2003-07-02 01:20:58 +04:00
# include <limits.h>
2003-11-12 20:30:32 +03:00
# ifdef linux
2004-07-01 19:14:29 +04:00
# include "kdev_t.h"
2003-11-12 20:30:32 +03:00
# include <linux / limits.h>
# else
# define MAJOR(x) major((x))
# define MINOR(x) minor((x))
# define MKDEV(x,y) makedev((x),(y))
# endif
2001-11-21 15:47:42 +03:00
2008-11-01 05:19:19 +03:00
# include "dm-ioctl.h"
2005-01-27 19:16:54 +03:00
2003-01-22 00:27:36 +03:00
/*
* Ensure build compatibility .
* The hard - coded versions here are the highest present
* in the _cmd_data arrays .
*/
2011-08-09 21:56:47 +04:00
# if !((DM_VERSION_MAJOR == 4 && DM_VERSION_MINOR >= 6))
2003-01-22 00:27:36 +03:00
# error The version of dm-ioctl.h included is incompatible.
# endif
2005-01-06 21:22:44 +03:00
/* FIXME This should be exported in device-mapper.h */
# define DM_NAME "device-mapper"
# define PROC_MISC " / proc / misc"
# define PROC_DEVICES " / proc / devices"
# define MISC_NAME "misc"
2005-10-16 18:33:22 +04:00
# define NUMBER_OF_MAJORS 4096
2010-08-18 17:11:56 +04:00
/*
* Static minor number assigned since kernel version 2.6 .36 .
* The original definition is in kernel ' s include / linux / miscdevice . h .
* This number is also visible in modules . devname exported by depmod
* utility ( support included in module - init - tools version > = 3.12 ) .
*/
# define MAPPER_CTRL_MINOR 236
# define MISC_MAJOR 10
2003-01-22 00:27:36 +03:00
/* dm major version no for running kernel */
2006-04-03 19:56:02 +04:00
static unsigned _dm_version = DM_VERSION_MAJOR ;
static unsigned _dm_version_minor = 0 ;
static unsigned _dm_version_patchlevel = 0 ;
2003-01-22 00:27:36 +03:00
static int _log_suppress = 0 ;
2001-11-21 15:47:42 +03:00
2011-06-09 19:07:40 +04:00
static int _kernel_major = 0 ;
static int _kernel_minor = 0 ;
static int _kernel_release = 0 ;
2010-06-01 20:08:13 +04:00
2009-02-12 23:42:07 +03:00
/*
* If the kernel dm driver only supports one major number
* we store it in _dm_device_major . Otherwise we indicate
* which major numbers have been claimed by device - mapper
* in _dm_bitset .
*/
static unsigned _dm_multiple_major_support = 1 ;
2005-10-17 02:57:20 +04:00
static dm_bitset_t _dm_bitset = NULL ;
2009-02-12 23:42:07 +03:00
static uint32_t _dm_device_major = 0 ;
2003-07-02 01:20:58 +04:00
static int _control_fd = - 1 ;
static int _version_checked = 0 ;
static int _version_ok = 1 ;
2005-09-20 22:04:28 +04:00
static unsigned _ioctl_buffer_double_factor = 0 ;
2003-07-02 01:20:58 +04:00
2003-01-22 00:27:36 +03:00
const int _dm_compat = 0 ;
/* *INDENT-OFF* */
2003-04-30 02:52:11 +04:00
static struct cmd_data _cmd_data_v4 [ ] = {
{ " create " , DM_DEV_CREATE , { 4 , 0 , 0 } } ,
2003-07-02 01:20:58 +04:00
{ " reload " , DM_TABLE_LOAD , { 4 , 0 , 0 } } ,
2003-04-30 02:52:11 +04:00
{ " remove " , DM_DEV_REMOVE , { 4 , 0 , 0 } } ,
{ " remove_all " , DM_REMOVE_ALL , { 4 , 0 , 0 } } ,
{ " suspend " , DM_DEV_SUSPEND , { 4 , 0 , 0 } } ,
{ " resume " , DM_DEV_SUSPEND , { 4 , 0 , 0 } } ,
{ " info " , DM_DEV_STATUS , { 4 , 0 , 0 } } ,
2003-07-02 01:20:58 +04:00
{ " deps " , DM_TABLE_DEPS , { 4 , 0 , 0 } } ,
2003-04-30 02:52:11 +04:00
{ " rename " , DM_DEV_RENAME , { 4 , 0 , 0 } } ,
{ " version " , DM_VERSION , { 4 , 0 , 0 } } ,
2003-07-02 01:20:58 +04:00
{ " status " , DM_TABLE_STATUS , { 4 , 0 , 0 } } ,
{ " table " , DM_TABLE_STATUS , { 4 , 0 , 0 } } ,
{ " waitevent " , DM_DEV_WAIT , { 4 , 0 , 0 } } ,
{ " names " , DM_LIST_DEVICES , { 4 , 0 , 0 } } ,
{ " clear " , DM_TABLE_CLEAR , { 4 , 0 , 0 } } ,
2003-11-13 16:14:28 +03:00
{ " mknodes " , DM_DEV_STATUS , { 4 , 0 , 0 } } ,
2004-03-31 22:54:17 +04:00
# ifdef DM_LIST_VERSIONS
2004-01-23 17:37:47 +03:00
{ " versions " , DM_LIST_VERSIONS , { 4 , 1 , 0 } } ,
2004-03-31 22:54:17 +04:00
# endif
2004-06-09 00:34:40 +04:00
# ifdef DM_TARGET_MSG
{ " message " , DM_TARGET_MSG , { 4 , 2 , 0 } } ,
# endif
2006-02-21 02:55:58 +03:00
# ifdef DM_DEV_SET_GEOMETRY
{ " setgeometry " , DM_DEV_SET_GEOMETRY , { 4 , 6 , 0 } } ,
# endif
2002-03-20 17:34:15 +03:00
} ;
2003-01-22 00:27:36 +03:00
/* *INDENT-ON* */
2003-04-09 01:20:31 +04:00
# define ALIGNMENT 8
2002-03-20 17:34:15 +03:00
2003-04-24 20:08:18 +04:00
/* FIXME Rejig library to record & use errno instead */
# ifndef DM_EXISTS_FLAG
# define DM_EXISTS_FLAG 0x00000004
# endif
2011-02-19 02:09:55 +03:00
static char * _align ( char * ptr , unsigned int a )
2002-05-03 15:55:58 +04:00
{
2003-01-22 00:27:36 +03:00
register unsigned long agn = - - a ;
2011-02-19 02:09:55 +03:00
return ( char * ) ( ( ( unsigned long ) ptr + agn ) & ~ agn ) ;
2003-01-22 00:27:36 +03:00
}
2010-08-03 17:06:35 +04:00
static int _uname ( void )
2010-06-01 20:08:13 +04:00
{
static int _uts_set = 0 ;
struct utsname _uts ;
2011-06-09 19:07:40 +04:00
int parts ;
2010-06-01 20:08:13 +04:00
if ( _uts_set )
return 1 ;
if ( uname ( & _uts ) ) {
log_error ( " uname failed: %s " , strerror ( errno ) ) ;
return 0 ;
}
2011-06-09 19:07:40 +04:00
parts = sscanf ( _uts . release , " %d.%d.%d " ,
2010-06-01 20:08:13 +04:00
& _kernel_major ,
& _kernel_minor ,
2011-06-09 19:07:40 +04:00
& _kernel_release ) ;
/* Kernels with a major number of 2 always had 3 parts. */
if ( parts < 1 | | ( _kernel_major < 3 & & parts < 3 ) ) {
2010-06-01 20:08:13 +04:00
log_error ( " Could not determine kernel version used. " ) ;
return 0 ;
}
_uts_set = 1 ;
return 1 ;
}
2005-05-16 19:15:34 +04:00
# ifdef DM_IOCTLS
2005-10-16 18:33:22 +04:00
/*
* Set number to NULL to populate _dm_bitset - otherwise first
* match is returned .
*/
2005-01-06 21:22:44 +03:00
static int _get_proc_number ( const char * file , const char * name ,
uint32_t * number )
{
FILE * fl ;
char nm [ 256 ] ;
2011-11-08 23:02:21 +04:00
char * line = NULL ;
2011-11-08 21:32:10 +04:00
size_t len ;
2005-10-16 18:33:22 +04:00
uint32_t num ;
2005-01-06 21:22:44 +03:00
if ( ! ( fl = fopen ( file , " r " ) ) ) {
2007-07-28 14:23:02 +04:00
log_sys_error ( " fopen " , file ) ;
2005-01-06 21:22:44 +03:00
return 0 ;
}
2011-11-08 21:32:10 +04:00
while ( getline ( & line , & len , fl ) ! = - 1 ) {
if ( sscanf ( line , " %d %255s \n " , & num , & nm [ 0 ] ) = = 2 ) {
2005-01-06 21:22:44 +03:00
if ( ! strcmp ( name , nm ) ) {
2005-10-16 18:33:22 +04:00
if ( number ) {
* number = num ;
2007-01-25 17:16:20 +03:00
if ( fclose ( fl ) )
2007-07-28 14:23:02 +04:00
log_sys_error ( " fclose " , file ) ;
2011-11-08 23:02:21 +04:00
free ( line ) ;
2005-10-16 18:33:22 +04:00
return 1 ;
}
2005-10-17 02:57:20 +04:00
dm_bit_set ( _dm_bitset , num ) ;
2005-01-06 21:22:44 +03:00
}
2011-11-08 21:32:10 +04:00
}
2005-01-06 21:22:44 +03:00
}
2007-01-25 17:16:20 +03:00
if ( fclose ( fl ) )
2007-07-28 14:23:02 +04:00
log_sys_error ( " fclose " , file ) ;
2011-11-08 23:02:21 +04:00
free ( line ) ;
2005-01-06 21:22:44 +03:00
2005-10-16 18:33:22 +04:00
if ( number ) {
log_error ( " %s: No entry for %s found " , file , name ) ;
return 0 ;
}
return 1 ;
2005-01-06 21:22:44 +03:00
}
static int _control_device_number ( uint32_t * major , uint32_t * minor )
{
if ( ! _get_proc_number ( PROC_DEVICES , MISC_NAME , major ) | |
! _get_proc_number ( PROC_MISC , DM_NAME , minor ) ) {
* major = 0 ;
return 0 ;
}
return 1 ;
}
/*
2011-03-26 02:50:35 +03:00
* Returns 1 if it exists on returning ; 0 if it doesn ' t ; - 1 if it ' s wrong .
2005-01-06 21:22:44 +03:00
*/
static int _control_exists ( const char * control , uint32_t major , uint32_t minor )
{
struct stat buf ;
if ( stat ( control , & buf ) < 0 ) {
if ( errno ! = ENOENT )
2007-07-28 14:23:02 +04:00
log_sys_error ( " stat " , control ) ;
2005-01-06 21:22:44 +03:00
return 0 ;
}
if ( ! S_ISCHR ( buf . st_mode ) ) {
log_verbose ( " %s: Wrong inode type " , control ) ;
if ( ! unlink ( control ) )
return 0 ;
2007-07-28 14:23:02 +04:00
log_sys_error ( " unlink " , control ) ;
2005-01-06 21:22:44 +03:00
return - 1 ;
}
2012-08-16 22:06:07 +04:00
if ( major & & buf . st_rdev ! = MKDEV ( ( dev_t ) major , minor ) ) {
2005-01-06 21:22:44 +03:00
log_verbose ( " %s: Wrong device number: (%u, %u) instead of "
" (%u, %u) " , control ,
MAJOR ( buf . st_mode ) , MINOR ( buf . st_mode ) ,
major , minor ) ;
if ( ! unlink ( control ) )
return 0 ;
2007-07-28 14:23:02 +04:00
log_sys_error ( " unlink " , control ) ;
2005-01-06 21:22:44 +03:00
return - 1 ;
}
return 1 ;
}
static int _create_control ( const char * control , uint32_t major , uint32_t minor )
{
int ret ;
mode_t old_umask ;
2011-03-26 02:50:35 +03:00
/*
* Return if the control already exists with intended major / minor
* or there ' s an error unlinking an apparently incorrect one .
*/
ret = _control_exists ( control , major , minor ) ;
if ( ret = = - 1 )
return 0 ; /* Failed to unlink existing incorrect node */
if ( ret )
return 1 ; /* Already exists and correct */
2005-01-06 21:22:44 +03:00
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( dm_dir ( ) , S_IFDIR ) ;
2009-09-25 15:58:00 +04:00
old_umask = umask ( DM_DEV_DIR_UMASK ) ;
2007-07-28 14:48:36 +04:00
ret = dm_create_dir ( dm_dir ( ) ) ;
2005-01-06 21:22:44 +03:00
umask ( old_umask ) ;
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
2005-01-06 21:22:44 +03:00
if ( ! ret )
return 0 ;
log_verbose ( " Creating device %s (%u, %u) " , control , major , minor ) ;
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( control , S_IFCHR ) ;
2011-01-04 17:43:53 +03:00
old_umask = umask ( DM_CONTROL_NODE_UMASK ) ;
2005-01-06 21:22:44 +03:00
if ( mknod ( control , S_IFCHR | S_IRUSR | S_IWUSR ,
2012-08-16 22:06:07 +04:00
MKDEV ( ( dev_t ) major , minor ) ) < 0 ) {
2007-07-28 14:23:02 +04:00
log_sys_error ( " mknod " , control ) ;
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
2005-01-06 21:22:44 +03:00
return 0 ;
}
2011-01-04 17:43:53 +03:00
umask ( old_umask ) ;
2010-12-13 13:43:56 +03:00
( void ) dm_prepare_selinux_context ( NULL , 0 ) ;
2005-01-06 21:22:44 +03:00
return 1 ;
}
2005-05-16 19:15:34 +04:00
# endif
2005-01-06 21:22:44 +03:00
2009-02-12 23:42:07 +03:00
/*
* FIXME Update bitset in long - running process if dm claims new major numbers .
*/
2005-10-16 18:33:22 +04:00
static int _create_dm_bitset ( void )
{
# ifdef DM_IOCTLS
2009-02-12 23:42:07 +03:00
if ( _dm_bitset | | _dm_device_major )
2005-10-16 18:33:22 +04:00
return 1 ;
2010-06-01 20:08:13 +04:00
if ( ! _uname ( ) )
2009-02-12 23:42:07 +03:00
return 0 ;
/*
* 2.6 kernels are limited to one major number .
* Assume 2.4 kernels are patched not to .
* FIXME Check _dm_version and _dm_version_minor if 2.6 changes this .
*/
2010-06-01 20:08:13 +04:00
if ( KERNEL_VERSION ( _kernel_major , _kernel_minor , _kernel_release ) > =
KERNEL_VERSION ( 2 , 6 , 0 ) )
2009-02-12 23:42:07 +03:00
_dm_multiple_major_support = 0 ;
if ( ! _dm_multiple_major_support ) {
if ( ! _get_proc_number ( PROC_DEVICES , DM_NAME , & _dm_device_major ) )
return 0 ;
return 1 ;
}
/* Multiple major numbers supported */
2005-10-17 02:57:20 +04:00
if ( ! ( _dm_bitset = dm_bitset_create ( NULL , NUMBER_OF_MAJORS ) ) )
2005-10-16 18:33:22 +04:00
return 0 ;
if ( ! _get_proc_number ( PROC_DEVICES , DM_NAME , NULL ) ) {
2005-10-17 02:57:20 +04:00
dm_bitset_destroy ( _dm_bitset ) ;
2005-10-16 18:33:22 +04:00
_dm_bitset = NULL ;
return 0 ;
}
return 1 ;
# else
return 0 ;
# endif
}
int dm_is_dm_major ( uint32_t major )
{
if ( ! _create_dm_bitset ( ) )
return 0 ;
2009-02-12 23:42:07 +03:00
if ( _dm_multiple_major_support )
return dm_bit ( _dm_bitset , major ) ? 1 : 0 ;
else
return ( major = = _dm_device_major ) ? 1 : 0 ;
2005-10-16 18:33:22 +04:00
}
2010-08-18 17:11:56 +04:00
static void _close_control_fd ( void )
{
if ( _control_fd ! = - 1 ) {
if ( close ( _control_fd ) < 0 )
log_sys_error ( " close " , " _control_fd " ) ;
_control_fd = - 1 ;
}
}
2011-03-26 02:50:35 +03:00
static int _open_and_assign_control_fd ( const char * control )
2010-06-01 20:08:13 +04:00
{
if ( ( _control_fd = open ( control , O_RDWR ) ) < 0 ) {
2011-03-18 15:17:57 +03:00
log_sys_error ( " open " , control ) ;
2010-06-01 20:08:13 +04:00
return 0 ;
}
return 1 ;
}
2003-11-11 00:06:16 +03:00
static int _open_control ( void )
{
2005-01-27 19:16:54 +03:00
# ifdef DM_IOCTLS
2003-11-11 00:06:16 +03:00
char control [ PATH_MAX ] ;
2011-03-26 02:50:35 +03:00
uint32_t major = MISC_MAJOR ;
uint32_t minor = MAPPER_CTRL_MINOR ;
2003-11-11 00:06:16 +03:00
if ( _control_fd ! = - 1 )
return 1 ;
2010-06-01 20:08:13 +04:00
if ( ! _uname ( ) )
return 0 ;
2005-01-06 21:22:44 +03:00
2010-06-01 20:08:13 +04:00
snprintf ( control , sizeof ( control ) , " %s/%s " , dm_dir ( ) , DM_CONTROL_NODE ) ;
2005-01-06 21:22:44 +03:00
2010-06-01 20:08:13 +04:00
/*
2011-03-26 02:50:35 +03:00
* Prior to 2.6 .36 the minor number should be looked up in / proc .
2010-06-01 20:08:13 +04:00
*/
2011-03-26 02:50:35 +03:00
if ( ( KERNEL_VERSION ( _kernel_major , _kernel_minor , _kernel_release ) <
KERNEL_VERSION ( 2 , 6 , 36 ) ) & &
! _control_device_number ( & major , & minor ) )
goto_bad ;
2010-06-01 20:08:13 +04:00
2010-08-18 17:11:56 +04:00
/*
2011-03-26 02:50:35 +03:00
* Create the node with correct major and minor if not already done .
* Udev may already have created / dev / mapper / control
* from the modules . devname file generated by depmod .
2010-08-18 17:11:56 +04:00
*/
2011-03-26 02:50:35 +03:00
if ( ! _create_control ( control , major , minor ) )
goto_bad ;
2003-11-11 00:06:16 +03:00
2010-08-18 17:11:56 +04:00
/*
2011-03-26 02:50:35 +03:00
* As of 2.6 .36 kernels , the open can trigger autoloading dm - mod .
2010-08-18 17:11:56 +04:00
*/
2011-03-26 02:50:35 +03:00
if ( ! _open_and_assign_control_fd ( control ) )
goto_bad ;
2005-10-16 18:33:22 +04:00
if ( ! _create_dm_bitset ( ) ) {
log_error ( " Failed to set up list of device-mapper major numbers " ) ;
return 0 ;
}
2003-11-11 00:06:16 +03:00
return 1 ;
2005-01-06 21:22:44 +03:00
2011-03-26 02:50:35 +03:00
bad :
2005-01-06 21:22:44 +03:00
log_error ( " Failure to communicate with kernel device-mapper driver. " ) ;
2011-03-26 02:50:35 +03:00
if ( ! geteuid ( ) )
log_error ( " Check that device-mapper is available in the kernel. " ) ;
2005-01-06 21:22:44 +03:00
return 0 ;
2005-01-27 19:16:54 +03:00
# else
return 1 ;
# endif
2003-11-11 00:06:16 +03:00
}
2010-04-07 19:57:20 +04:00
static void _dm_zfree_string ( char * string )
{
if ( string ) {
memset ( string , 0 , strlen ( string ) ) ;
dm_free ( string ) ;
}
}
static void _dm_zfree_dmi ( struct dm_ioctl * dmi )
{
if ( dmi ) {
memset ( dmi , 0 , dmi - > data_size ) ;
dm_free ( dmi ) ;
}
}
2003-11-11 00:06:16 +03:00
void dm_task_destroy ( struct dm_task * dmt )
{
struct target * t , * n ;
for ( t = dmt - > head ; t ; t = n ) {
n = t - > next ;
2010-04-07 19:57:20 +04:00
_dm_zfree_string ( t - > params ) ;
2005-10-17 02:57:20 +04:00
dm_free ( t - > type ) ;
dm_free ( t ) ;
2003-11-11 00:06:16 +03:00
}
2010-04-07 19:57:20 +04:00
_dm_zfree_dmi ( dmt - > dmi . v4 ) ;
2011-01-28 13:16:04 +03:00
dm_free ( dmt - > dev_name ) ;
2012-02-15 15:27:01 +04:00
dm_free ( dmt - > mangled_dev_name ) ;
2011-01-28 13:16:04 +03:00
dm_free ( dmt - > newname ) ;
dm_free ( dmt - > message ) ;
2011-08-12 00:49:33 +04:00
dm_free ( dmt - > geometry ) ;
2011-01-28 13:16:04 +03:00
dm_free ( dmt - > uuid ) ;
2012-10-10 19:00:41 +04:00
dm_free ( dmt - > mangled_uuid ) ;
2005-10-17 02:57:20 +04:00
dm_free ( dmt ) ;
2003-11-11 00:06:16 +03:00
}
/*
* Protocol Version 4 functions .
*/
2001-11-21 15:47:42 +03:00
2002-03-07 23:56:10 +03:00
int dm_task_get_driver_version ( struct dm_task * dmt , char * version , size_t size )
2002-01-15 18:21:57 +03:00
{
2006-04-03 19:56:02 +04:00
unsigned * v ;
2002-06-17 19:50:17 +04:00
2003-04-30 02:52:11 +04:00
if ( ! dmt - > dmi . v4 ) {
2012-03-01 14:07:38 +04:00
if ( version )
version [ 0 ] = ' \0 ' ;
2002-01-15 18:21:57 +03:00
return 0 ;
2003-01-22 00:27:36 +03:00
}
2002-01-15 18:21:57 +03:00
2003-04-30 02:52:11 +04:00
v = dmt - > dmi . v4 - > version ;
2006-04-03 19:56:02 +04:00
_dm_version_minor = v [ 1 ] ;
_dm_version_patchlevel = v [ 2 ] ;
2012-03-01 14:07:38 +04:00
if ( version & &
( snprintf ( version , size , " %u.%u.%u " , v [ 0 ] , v [ 1 ] , v [ 2 ] ) < 0 ) ) {
log_error ( " Buffer for version is to short. " ) ;
if ( size > 0 )
2012-03-01 14:46:39 +04:00
version [ 0 ] = ' \0 ' ;
2012-03-01 14:07:38 +04:00
return 0 ;
}
2006-04-03 19:56:02 +04:00
2002-01-15 18:21:57 +03:00
return 1 ;
}
2003-01-22 00:27:36 +03:00
static int _check_version ( char * version , size_t size , int log_suppress )
{
struct dm_task * task ;
int r ;
if ( ! ( task = dm_task_create ( DM_DEVICE_VERSION ) ) ) {
log_error ( " Failed to get device-mapper version " ) ;
version [ 0 ] = ' \0 ' ;
return 0 ;
}
if ( log_suppress )
_log_suppress = 1 ;
r = dm_task_run ( task ) ;
2012-03-01 14:07:38 +04:00
if ( ! dm_task_get_driver_version ( task , version , size ) )
stack ;
2003-01-22 00:27:36 +03:00
dm_task_destroy ( task ) ;
_log_suppress = 0 ;
return r ;
}
/*
* Find out device - mapper ' s major version number the first time
* this is called and whether or not we support it .
*/
int dm_check_version ( void )
{
char libversion [ 64 ] , dmversion [ 64 ] ;
const char * compat = " " ;
2003-07-02 01:20:58 +04:00
if ( _version_checked )
return _version_ok ;
2003-01-22 00:27:36 +03:00
2003-07-02 01:20:58 +04:00
_version_checked = 1 ;
2003-01-22 00:27:36 +03:00
if ( _check_version ( dmversion , sizeof ( dmversion ) , _dm_compat ) )
return 1 ;
2011-03-20 05:00:52 +03:00
if ( ! _dm_compat )
goto_bad ;
2003-01-22 00:27:36 +03:00
2006-04-03 19:56:02 +04:00
log_verbose ( " device-mapper ioctl protocol version %u failed. "
2003-01-22 00:27:36 +03:00
" Trying protocol version 1. " , _dm_version ) ;
_dm_version = 1 ;
if ( _check_version ( dmversion , sizeof ( dmversion ) , 0 ) ) {
log_verbose ( " Using device-mapper ioctl protocol version 1 " ) ;
return 1 ;
}
compat = " (compat) " ;
dm_get_library_version ( libversion , sizeof ( libversion ) ) ;
log_error ( " Incompatible libdevmapper %s%s and kernel driver %s " ,
libversion , compat , dmversion ) ;
bad :
2003-07-02 01:20:58 +04:00
_version_ok = 0 ;
2003-01-22 00:27:36 +03:00
return 0 ;
}
2009-07-31 19:53:11 +04:00
int dm_cookie_supported ( void )
{
return ( dm_check_version ( ) & &
2011-08-19 21:02:48 +04:00
_dm_version > = 4 & &
_dm_version_minor > = 15 ) ;
2009-07-31 19:53:11 +04:00
}
2011-02-21 19:26:23 +03:00
static int dm_inactive_supported ( void )
{
int inactive_supported = 0 ;
if ( dm_check_version ( ) & & _dm_version > = 4 ) {
if ( _dm_version_minor > = 16 )
inactive_supported = 1 ; /* upstream */
else if ( _dm_version_minor = = 11 & &
( _dm_version_patchlevel > = 6 & &
_dm_version_patchlevel < = 40 ) ) {
inactive_supported = 1 ; /* RHEL 5.7 */
}
}
return inactive_supported ;
}
2002-05-10 19:25:38 +04:00
void * dm_get_next_target ( struct dm_task * dmt , void * next ,
uint64_t * start , uint64_t * length ,
char * * target_type , char * * params )
2002-05-03 15:55:58 +04:00
{
2002-05-10 19:25:38 +04:00
struct target * t = ( struct target * ) next ;
2002-05-03 15:55:58 +04:00
2002-05-10 19:25:38 +04:00
if ( ! t )
t = dmt - > head ;
2002-05-03 15:55:58 +04:00
2012-02-08 15:25:09 +04:00
if ( ! t ) {
* start = 0 ;
* length = 0 ;
* target_type = 0 ;
* params = 0 ;
2002-05-10 19:25:38 +04:00
return NULL ;
2012-02-08 15:25:09 +04:00
}
2002-05-03 15:55:58 +04:00
* start = t - > start ;
* length = t - > length ;
* target_type = t - > type ;
* params = t - > params ;
2002-05-10 19:25:38 +04:00
2002-05-03 15:55:58 +04:00
return t - > next ;
}
/* Unmarshall the target info returned from a status call */
2002-05-10 19:25:38 +04:00
static int _unmarshal_status ( struct dm_task * dmt , struct dm_ioctl * dmi )
2002-05-03 15:55:58 +04:00
{
2003-04-28 15:55:58 +04:00
char * outbuf = ( char * ) dmi + dmi - > data_start ;
2002-05-03 15:55:58 +04:00
char * outptr = outbuf ;
2003-01-22 00:27:36 +03:00
uint32_t i ;
2003-02-20 16:30:03 +03:00
struct dm_target_spec * spec ;
2002-05-03 15:55:58 +04:00
2002-05-10 19:25:38 +04:00
for ( i = 0 ; i < dmi - > target_count ; i + + ) {
2003-02-20 16:30:03 +03:00
spec = ( struct dm_target_spec * ) outptr ;
2003-01-22 00:27:36 +03:00
if ( ! dm_task_add_target ( dmt , spec - > sector_start ,
spec - > length ,
2003-02-20 16:30:03 +03:00
spec - > target_type ,
2004-06-09 00:34:40 +04:00
outptr + sizeof ( * spec ) ) ) {
2002-05-10 19:25:38 +04:00
return 0 ;
2004-06-09 00:34:40 +04:00
}
2002-05-10 19:25:38 +04:00
2003-04-09 01:20:31 +04:00
outptr = outbuf + spec - > next ;
2002-05-03 15:55:58 +04:00
}
2002-05-10 19:25:38 +04:00
2002-05-03 15:55:58 +04:00
return 1 ;
}
2003-11-11 00:06:16 +03:00
int dm_format_dev ( char * buf , int bufsize , uint32_t dev_major ,
uint32_t dev_minor )
2003-07-02 01:20:58 +04:00
{
int r ;
if ( bufsize < 8 )
return 0 ;
2006-01-31 17:50:38 +03:00
r = snprintf ( buf , ( size_t ) bufsize , " %u:%u " , dev_major , dev_minor ) ;
2003-07-02 01:20:58 +04:00
if ( r < 0 | | r > bufsize - 1 )
return 0 ;
return 1 ;
}
2001-11-21 15:47:42 +03:00
int dm_task_get_info ( struct dm_task * dmt , struct dm_info * info )
{
2003-04-30 02:52:11 +04:00
if ( ! dmt - > dmi . v4 )
2001-11-21 15:47:42 +03:00
return 0 ;
2002-01-15 02:07:32 +03:00
memset ( info , 0 , sizeof ( * info ) ) ;
2003-04-30 02:52:11 +04:00
info - > exists = dmt - > dmi . v4 - > flags & DM_EXISTS_FLAG ? 1 : 0 ;
2002-01-15 02:07:32 +03:00
if ( ! info - > exists )
return 1 ;
2003-04-30 02:52:11 +04:00
info - > suspended = dmt - > dmi . v4 - > flags & DM_SUSPEND_FLAG ? 1 : 0 ;
info - > read_only = dmt - > dmi . v4 - > flags & DM_READONLY_FLAG ? 1 : 0 ;
2003-07-02 01:20:58 +04:00
info - > live_table = dmt - > dmi . v4 - > flags & DM_ACTIVE_PRESENT_FLAG ? 1 : 0 ;
info - > inactive_table = dmt - > dmi . v4 - > flags & DM_INACTIVE_PRESENT_FLAG ?
1 : 0 ;
2003-04-30 02:52:11 +04:00
info - > target_count = dmt - > dmi . v4 - > target_count ;
info - > open_count = dmt - > dmi . v4 - > open_count ;
info - > event_nr = dmt - > dmi . v4 - > event_nr ;
info - > major = MAJOR ( dmt - > dmi . v4 - > dev ) ;
info - > minor = MINOR ( dmt - > dmi . v4 - > dev ) ;
2002-01-15 02:07:32 +03:00
2001-11-21 15:47:42 +03:00
return 1 ;
}
2007-11-30 17:59:57 +03:00
uint32_t dm_task_get_read_ahead ( const struct dm_task * dmt , uint32_t * read_ahead )
2011-08-19 21:02:48 +04:00
{
2007-12-05 19:24:41 +03:00
const char * dev_name ;
* read_ahead = 0 ;
2011-08-19 21:02:48 +04:00
if ( ! dmt - > dmi . v4 | | ! ( dmt - > dmi . v4 - > flags & DM_EXISTS_FLAG ) )
2007-12-05 19:24:41 +03:00
return 0 ;
if ( * dmt - > dmi . v4 - > name )
dev_name = dmt - > dmi . v4 - > name ;
2012-02-15 15:33:53 +04:00
else if ( ! ( dev_name = DEV_NAME ( dmt ) ) ) {
2007-12-05 19:24:41 +03:00
log_error ( " Get read ahead request failed: device name unrecorded. " ) ;
return 0 ;
}
2012-01-09 16:26:14 +04:00
return get_dev_node_read_ahead ( dev_name , MAJOR ( dmt - > dmi . v4 - > dev ) ,
MINOR ( dmt - > dmi . v4 - > dev ) , read_ahead ) ;
2007-11-27 23:57:05 +03:00
}
2002-03-06 17:38:25 +03:00
struct dm_deps * dm_task_get_deps ( struct dm_task * dmt )
{
2010-08-03 17:16:21 +04:00
return ( struct dm_deps * ) ( ( ( char * ) dmt - > dmi . v4 ) +
2003-04-30 02:52:11 +04:00
dmt - > dmi . v4 - > data_start ) ;
2002-03-06 17:38:25 +03:00
}
2003-07-02 01:20:58 +04:00
struct dm_names * dm_task_get_names ( struct dm_task * dmt )
{
2010-08-03 17:16:21 +04:00
return ( struct dm_names * ) ( ( ( char * ) dmt - > dmi . v4 ) +
2003-07-02 01:20:58 +04:00
dmt - > dmi . v4 - > data_start ) ;
}
2004-01-23 17:37:47 +03:00
struct dm_versions * dm_task_get_versions ( struct dm_task * dmt )
{
2010-08-03 17:16:21 +04:00
return ( struct dm_versions * ) ( ( ( char * ) dmt - > dmi . v4 ) +
2004-01-23 17:37:47 +03:00
dmt - > dmi . v4 - > data_start ) ;
}
2002-01-03 13:39:21 +03:00
int dm_task_set_ro ( struct dm_task * dmt )
{
dmt - > read_only = 1 ;
return 1 ;
}
2007-11-27 23:57:05 +03:00
int dm_task_set_read_ahead ( struct dm_task * dmt , uint32_t read_ahead ,
uint32_t read_ahead_flags )
{
dmt - > read_ahead = read_ahead ;
dmt - > read_ahead_flags = read_ahead_flags ;
return 1 ;
}
2005-11-22 21:43:12 +03:00
int dm_task_suppress_identical_reload ( struct dm_task * dmt )
{
dmt - > suppress_identical_reload = 1 ;
return 1 ;
}
2011-02-04 22:33:53 +03:00
int dm_task_set_add_node ( struct dm_task * dmt , dm_add_node_t add_node )
{
switch ( add_node ) {
case DM_ADD_NODE_ON_RESUME :
case DM_ADD_NODE_ON_CREATE :
dmt - > add_node = add_node ;
return 1 ;
default :
log_error ( " Unknown add node parameter " ) ;
return 0 ;
}
}
2010-10-15 05:10:27 +04:00
int dm_task_set_newuuid ( struct dm_task * dmt , const char * newuuid )
{
2012-10-10 19:01:13 +04:00
dm_string_mangling_t mangling_mode = dm_get_name_mangling_mode ( ) ;
char mangled_uuid [ DM_UUID_LEN ] ;
int r = 0 ;
2010-10-15 05:10:27 +04:00
if ( strlen ( newuuid ) > = DM_UUID_LEN ) {
log_error ( " Uuid \" %s \" too long " , newuuid ) ;
return 0 ;
}
2012-10-10 19:01:13 +04:00
if ( ! check_multiple_mangled_string_allowed ( newuuid , " new UUID " , mangling_mode ) )
return_0 ;
if ( mangling_mode ! = DM_STRING_MANGLING_NONE & &
( r = mangle_string ( newuuid , " new UUID " , strlen ( newuuid ) , mangled_uuid ,
sizeof ( mangled_uuid ) , mangling_mode ) ) < 0 ) {
log_error ( " Failed to mangle new device UUID \" %s \" " , newuuid ) ;
return 0 ;
}
if ( r ) {
log_debug ( " New device uuid mangled [%s]: %s --> %s " ,
mangling_mode = = DM_STRING_MANGLING_AUTO ? " auto " : " hex " ,
newuuid , mangled_uuid ) ;
newuuid = mangled_uuid ;
}
2010-10-15 05:10:27 +04:00
if ( ! ( dmt - > newname = dm_strdup ( newuuid ) ) ) {
log_error ( " dm_task_set_newuuid: strdup(%s) failed " , newuuid ) ;
return 0 ;
}
dmt - > new_uuid = 1 ;
return 1 ;
}
2004-06-09 00:34:40 +04:00
int dm_task_set_message ( struct dm_task * dmt , const char * message )
{
2005-10-17 02:57:20 +04:00
if ( ! ( dmt - > message = dm_strdup ( message ) ) ) {
2010-05-13 17:31:30 +04:00
log_error ( " dm_task_set_message: strdup failed " ) ;
2004-06-09 00:34:40 +04:00
return 0 ;
}
return 1 ;
}
int dm_task_set_sector ( struct dm_task * dmt , uint64_t sector )
{
dmt - > sector = sector ;
return 1 ;
}
2012-02-08 16:59:19 +04:00
int dm_task_set_geometry ( struct dm_task * dmt , const char * cylinders , const char * heads ,
const char * sectors , const char * start )
2006-02-21 02:55:58 +03:00
{
2012-02-08 16:59:19 +04:00
if ( dm_asprintf ( & ( dmt - > geometry ) , " %s %s %s %s " ,
cylinders , heads , sectors , start ) < 0 ) {
2006-02-21 02:55:58 +03:00
log_error ( " dm_task_set_geometry: sprintf failed " ) ;
return 0 ;
}
2006-04-03 19:56:02 +04:00
return 1 ;
2006-02-21 02:55:58 +03:00
}
2006-10-12 19:42:25 +04:00
int dm_task_no_flush ( struct dm_task * dmt )
{
2006-10-12 21:29:05 +04:00
dmt - > no_flush = 1 ;
2006-10-12 19:42:25 +04:00
return 1 ;
}
2005-01-13 01:10:14 +03:00
int dm_task_no_open_count ( struct dm_task * dmt )
{
dmt - > no_open_count = 1 ;
return 1 ;
}
2005-10-05 00:12:32 +04:00
int dm_task_skip_lockfs ( struct dm_task * dmt )
{
dmt - > skip_lockfs = 1 ;
return 1 ;
}
2011-02-04 19:08:11 +03:00
int dm_task_secure_data ( struct dm_task * dmt )
{
dmt - > secure_data = 1 ;
return 1 ;
}
2011-09-22 21:09:48 +04:00
int dm_task_retry_remove ( struct dm_task * dmt )
{
dmt - > retry_remove = 1 ;
return 1 ;
}
2009-11-06 03:43:08 +03:00
int dm_task_query_inactive_table ( struct dm_task * dmt )
{
dmt - > query_inactive_table = 1 ;
return 1 ;
}
2003-04-29 15:34:23 +04:00
int dm_task_set_event_nr ( struct dm_task * dmt , uint32_t event_nr )
{
dmt - > event_nr = event_nr ;
return 1 ;
}
2002-05-10 19:25:38 +04:00
struct target * create_target ( uint64_t start , uint64_t len , const char * type ,
const char * params )
2001-11-21 15:47:42 +03:00
{
2011-11-18 23:34:02 +04:00
struct target * t ;
if ( strlen ( type ) > = DM_MAX_TYPE_NAME ) {
log_error ( " Target type name %s is too long. " , type ) ;
return NULL ;
}
2001-11-21 15:47:42 +03:00
2011-11-18 23:34:02 +04:00
if ( ! ( t = dm_zalloc ( sizeof ( * t ) ) ) ) {
2006-01-31 17:50:38 +03:00
log_error ( " create_target: malloc(% " PRIsize_t " ) failed " ,
sizeof ( * t ) ) ;
2001-11-21 15:47:42 +03:00
return NULL ;
2001-12-05 19:41:52 +03:00
}
2005-10-17 02:57:20 +04:00
if ( ! ( t - > params = dm_strdup ( params ) ) ) {
2002-01-18 22:37:26 +03:00
log_error ( " create_target: strdup(params) failed " ) ;
2001-11-21 15:47:42 +03:00
goto bad ;
}
2005-10-17 02:57:20 +04:00
if ( ! ( t - > type = dm_strdup ( type ) ) ) {
2002-01-18 22:37:26 +03:00
log_error ( " create_target: strdup(type) failed " ) ;
2001-11-21 15:47:42 +03:00
goto bad ;
}
t - > start = start ;
t - > length = len ;
return t ;
2002-03-07 23:56:10 +03:00
bad :
2010-04-07 19:57:20 +04:00
_dm_zfree_string ( t - > params ) ;
2005-10-17 02:57:20 +04:00
dm_free ( t - > type ) ;
dm_free ( t ) ;
2001-11-21 15:47:42 +03:00
return NULL ;
}
2011-02-19 02:09:55 +03:00
static char * _add_target ( struct target * t , char * out , char * end )
2001-11-21 15:47:42 +03:00
{
2011-02-19 02:09:55 +03:00
char * out_sp = out ;
2001-11-21 15:47:42 +03:00
struct dm_target_spec sp ;
2003-01-22 00:27:36 +03:00
size_t sp_size = sizeof ( struct dm_target_spec ) ;
2012-02-15 16:17:34 +04:00
unsigned int backslash_count = 0 ;
2001-11-21 15:47:42 +03:00
int len ;
2012-02-15 16:17:34 +04:00
char * pt ;
2001-11-21 15:47:42 +03:00
2011-11-18 23:34:02 +04:00
if ( strlen ( t - > type ) > = sizeof ( sp . target_type ) ) {
log_error ( " Target type name %s is too long. " , t - > type ) ;
return NULL ;
}
2001-11-21 15:47:42 +03:00
sp . status = 0 ;
sp . sector_start = t - > start ;
sp . length = t - > length ;
2011-11-18 23:34:02 +04:00
strncpy ( sp . target_type , t - > type , sizeof ( sp . target_type ) - 1 ) ;
sp . target_type [ sizeof ( sp . target_type ) - 1 ] = ' \0 ' ;
2001-11-21 15:47:42 +03:00
2011-11-18 23:34:02 +04:00
out + = sp_size ;
2012-02-15 16:17:34 +04:00
pt = t - > params ;
while ( * pt )
if ( * pt + + = = ' \\ ' )
backslash_count + + ;
len = strlen ( t - > params ) + backslash_count ;
2001-11-21 15:47:42 +03:00
2011-11-18 23:34:02 +04:00
if ( ( out > = end ) | | ( out + len + 1 ) > = end ) {
log_error ( " Ran out of memory building ioctl parameter " ) ;
return NULL ;
}
2001-11-21 15:47:42 +03:00
2012-02-15 16:17:34 +04:00
if ( backslash_count ) {
/* replace "\" with "\\" */
pt = t - > params ;
do {
if ( * pt = = ' \\ ' )
* out + + = ' \\ ' ;
* out + + = * pt + + ;
} while ( * pt ) ;
* out + + = ' \0 ' ;
}
else {
strcpy ( out , t - > params ) ;
out + = len + 1 ;
}
2001-11-21 15:47:42 +03:00
/* align next block */
out = _align ( out , ALIGNMENT ) ;
sp . next = out - out_sp ;
2003-01-22 00:27:36 +03:00
memcpy ( out_sp , & sp , sp_size ) ;
2001-11-21 15:47:42 +03:00
return out ;
}
2006-04-03 19:56:02 +04:00
static int _lookup_dev_name ( uint64_t dev , char * buf , size_t len )
{
struct dm_names * names ;
unsigned next = 0 ;
struct dm_task * dmt ;
int r = 0 ;
if ( ! ( dmt = dm_task_create ( DM_DEVICE_LIST ) ) )
return 0 ;
if ( ! dm_task_run ( dmt ) )
goto out ;
if ( ! ( names = dm_task_get_names ( dmt ) ) )
goto out ;
if ( ! names - > dev )
goto out ;
do {
2011-02-19 02:09:55 +03:00
names = ( struct dm_names * ) ( ( char * ) names + next ) ;
2006-04-03 19:56:02 +04:00
if ( names - > dev = = dev ) {
strncpy ( buf , names - > name , len ) ;
r = 1 ;
break ;
}
next = names - > next ;
} while ( next ) ;
out :
dm_task_destroy ( dmt ) ;
return r ;
}
2005-09-19 18:29:17 +04:00
static struct dm_ioctl * _flatten ( struct dm_task * dmt , unsigned repeat_count )
2001-11-21 15:47:42 +03:00
{
2002-03-06 17:38:25 +03:00
const size_t min_size = 16 * 1024 ;
2003-01-22 00:27:36 +03:00
const int ( * version ) [ 3 ] ;
2002-03-06 17:38:25 +03:00
2001-11-21 15:47:42 +03:00
struct dm_ioctl * dmi ;
struct target * t ;
2004-06-09 00:34:40 +04:00
struct dm_target_msg * tmsg ;
2001-11-21 15:47:42 +03:00
size_t len = sizeof ( struct dm_ioctl ) ;
2011-02-19 02:09:55 +03:00
char * b , * e ;
2001-11-21 15:47:42 +03:00
int count = 0 ;
for ( t = dmt - > head ; t ; t = t - > next ) {
len + = sizeof ( struct dm_target_spec ) ;
len + = strlen ( t - > params ) + 1 + ALIGNMENT ;
count + + ;
}
2004-06-09 00:34:40 +04:00
if ( count & & ( dmt - > sector | | dmt - > message ) ) {
log_error ( " targets and message are incompatible " ) ;
return NULL ;
}
2002-01-11 02:29:16 +03:00
if ( count & & dmt - > newname ) {
2010-10-15 05:10:27 +04:00
log_error ( " targets and rename are incompatible " ) ;
2002-01-11 02:29:16 +03:00
return NULL ;
}
2006-02-21 02:55:58 +03:00
if ( count & & dmt - > geometry ) {
log_error ( " targets and geometry are incompatible " ) ;
return NULL ;
}
2004-06-09 00:34:40 +04:00
if ( dmt - > newname & & ( dmt - > sector | | dmt - > message ) ) {
2010-10-15 05:10:27 +04:00
log_error ( " message and rename are incompatible " ) ;
2004-06-09 00:34:40 +04:00
return NULL ;
}
2006-02-21 02:55:58 +03:00
if ( dmt - > newname & & dmt - > geometry ) {
2010-10-15 05:10:27 +04:00
log_error ( " geometry and rename are incompatible " ) ;
2006-02-21 02:55:58 +03:00
return NULL ;
}
if ( dmt - > geometry & & ( dmt - > sector | | dmt - > message ) ) {
log_error ( " geometry and message are incompatible " ) ;
return NULL ;
}
2004-06-09 00:34:40 +04:00
if ( dmt - > sector & & ! dmt - > message ) {
log_error ( " message is required with sector " ) ;
return NULL ;
}
2002-01-11 02:29:16 +03:00
if ( dmt - > newname )
len + = strlen ( dmt - > newname ) + 1 ;
2004-06-09 00:34:40 +04:00
if ( dmt - > message )
len + = sizeof ( struct dm_target_msg ) + strlen ( dmt - > message ) + 1 ;
2006-02-21 02:55:58 +03:00
if ( dmt - > geometry )
len + = strlen ( dmt - > geometry ) + 1 ;
2002-03-06 17:38:25 +03:00
/*
* Give len a minimum size so that we have space to store
* dependencies or status information .
*/
if ( len < min_size )
len = min_size ;
2005-09-19 18:29:17 +04:00
/* Increase buffer size if repeating because buffer was too small */
while ( repeat_count - - )
len * = 2 ;
2005-10-17 02:57:20 +04:00
if ( ! ( dmi = dm_malloc ( len ) ) )
2001-11-21 15:47:42 +03:00
return NULL ;
2002-01-08 01:01:50 +03:00
memset ( dmi , 0 , len ) ;
2003-04-30 02:52:11 +04:00
version = & _cmd_data_v4 [ dmt - > type ] . version ;
2003-01-22 00:27:36 +03:00
dmi - > version [ 0 ] = ( * version ) [ 0 ] ;
dmi - > version [ 1 ] = ( * version ) [ 1 ] ;
dmi - > version [ 2 ] = ( * version ) [ 2 ] ;
2001-11-21 15:47:42 +03:00
dmi - > data_size = len ;
2003-04-28 15:55:58 +04:00
dmi - > data_start = sizeof ( struct dm_ioctl ) ;
2002-01-15 18:21:57 +03:00
2002-02-01 20:39:20 +03:00
if ( dmt - > minor > = 0 ) {
2003-04-09 01:20:31 +04:00
if ( dmt - > major < = 0 ) {
2005-11-09 01:50:11 +03:00
log_error ( " Missing major number for persistent device. " ) ;
goto bad ;
2003-04-09 01:20:31 +04:00
}
2009-02-12 23:42:07 +03:00
2009-06-18 00:55:24 +04:00
if ( ! _dm_multiple_major_support & & dmt - > allow_default_major_fallback & &
dmt - > major ! = _dm_device_major ) {
2009-02-12 23:42:07 +03:00
log_verbose ( " Overriding major number of % " PRIu32
" with % " PRIu32 " for persistent device. " ,
dmt - > major , _dm_device_major ) ;
dmt - > major = _dm_device_major ;
}
2002-01-15 18:21:57 +03:00
dmi - > flags | = DM_PERSISTENT_DEV_FLAG ;
2012-08-16 22:06:07 +04:00
dmi - > dev = MKDEV ( ( dev_t ) dmt - > major , dmt - > minor ) ;
2002-01-15 02:07:32 +03:00
}
2001-11-21 15:47:42 +03:00
2006-04-03 19:56:02 +04:00
/* Does driver support device number referencing? */
2012-10-10 19:02:19 +04:00
if ( _dm_version_minor < 3 & & ! DEV_NAME ( dmt ) & & ! DEV_UUID ( dmt ) & & dmi - > dev ) {
2006-04-03 19:56:02 +04:00
if ( ! _lookup_dev_name ( dmi - > dev , dmi - > name , sizeof ( dmi - > name ) ) ) {
log_error ( " Unable to find name for device (% " PRIu32
" :% " PRIu32 " ) " , dmt - > major , dmt - > minor ) ;
goto bad ;
}
log_verbose ( " device (% " PRIu32 " :% " PRIu32 " ) is %s "
" for compatibility with old kernel " ,
dmt - > major , dmt - > minor , dmi - > name ) ;
}
2007-12-04 01:48:36 +03:00
/* FIXME Until resume ioctl supplies name, use dev_name for readahead */
2012-02-15 15:33:53 +04:00
if ( DEV_NAME ( dmt ) & & ( dmt - > type ! = DM_DEVICE_RESUME | | dmt - > minor < 0 | |
2007-12-04 01:48:36 +03:00
dmt - > major < 0 ) )
2012-02-15 15:33:53 +04:00
strncpy ( dmi - > name , DEV_NAME ( dmt ) , sizeof ( dmi - > name ) ) ;
2006-04-03 19:56:02 +04:00
2012-10-10 19:02:19 +04:00
if ( DEV_UUID ( dmt ) )
strncpy ( dmi - > uuid , DEV_UUID ( dmt ) , sizeof ( dmi - > uuid ) ) ;
2002-03-12 01:44:36 +03:00
2006-04-03 19:56:02 +04:00
if ( dmt - > type = = DM_DEVICE_SUSPEND )
dmi - > flags | = DM_SUSPEND_FLAG ;
2006-10-12 21:29:05 +04:00
if ( dmt - > no_flush )
2006-10-12 19:42:25 +04:00
dmi - > flags | = DM_NOFLUSH_FLAG ;
2006-04-03 19:56:02 +04:00
if ( dmt - > read_only )
dmi - > flags | = DM_READONLY_FLAG ;
if ( dmt - > skip_lockfs )
dmi - > flags | = DM_SKIP_LOCKFS_FLAG ;
2011-02-05 00:26:33 +03:00
if ( dmt - > secure_data ) {
if ( _dm_version_minor < 20 )
2011-03-06 00:17:19 +03:00
log_verbose ( " Secure data flag unsupported by kernel. "
" Buffers will not be wiped after use. " ) ;
2011-02-04 19:08:11 +03:00
dmi - > flags | = DM_SECURE_DATA_FLAG ;
2011-02-05 00:26:33 +03:00
}
2009-11-06 03:43:08 +03:00
if ( dmt - > query_inactive_table ) {
2011-02-21 19:26:23 +03:00
if ( ! dm_inactive_supported ( ) )
2009-11-06 03:43:08 +03:00
log_warn ( " WARNING: Inactive table query unsupported "
" by kernel. It will use live table. " ) ;
dmi - > flags | = DM_QUERY_INACTIVE_TABLE_FLAG ;
}
2010-10-15 05:10:27 +04:00
if ( dmt - > new_uuid ) {
if ( _dm_version_minor < 19 ) {
log_error ( " WARNING: Setting UUID unsupported by "
" kernel. Aborting operation. " ) ;
goto bad ;
}
2010-10-25 15:44:20 +04:00
dmi - > flags | = DM_UUID_FLAG ;
2010-10-15 05:10:27 +04:00
}
2006-04-03 19:56:02 +04:00
2001-11-21 15:47:42 +03:00
dmi - > target_count = count ;
2003-04-29 15:34:23 +04:00
dmi - > event_nr = dmt - > event_nr ;
2001-11-21 15:47:42 +03:00
2011-02-19 02:09:55 +03:00
b = ( char * ) ( dmi + 1 ) ;
e = ( char * ) dmi + len ;
2001-11-21 15:47:42 +03:00
for ( t = dmt - > head ; t ; t = t - > next )
2011-11-18 23:34:02 +04:00
if ( ! ( b = _add_target ( t , b , e ) ) )
goto_bad ;
2001-11-21 15:47:42 +03:00
2002-01-11 02:29:16 +03:00
if ( dmt - > newname )
strcpy ( b , dmt - > newname ) ;
2004-06-09 00:34:40 +04:00
if ( dmt - > message ) {
tmsg = ( struct dm_target_msg * ) b ;
tmsg - > sector = dmt - > sector ;
strcpy ( tmsg - > message , dmt - > message ) ;
}
2006-02-21 02:55:58 +03:00
if ( dmt - > geometry )
strcpy ( b , dmt - > geometry ) ;
2001-11-21 15:47:42 +03:00
return dmi ;
2002-03-07 23:56:10 +03:00
bad :
2010-04-07 19:57:20 +04:00
_dm_zfree_dmi ( dmi ) ;
2001-11-21 15:47:42 +03:00
return NULL ;
}
2004-03-30 18:31:58 +04:00
static int _process_mapper_dir ( struct dm_task * dmt )
{
struct dirent * dirent ;
DIR * d ;
const char * dir ;
int r = 1 ;
dir = dm_dir ( ) ;
if ( ! ( d = opendir ( dir ) ) ) {
2007-07-28 14:23:02 +04:00
log_sys_error ( " opendir " , dir ) ;
2004-03-30 18:31:58 +04:00
return 0 ;
}
while ( ( dirent = readdir ( d ) ) ) {
if ( ! strcmp ( dirent - > d_name , " . " ) | |
! strcmp ( dirent - > d_name , " .. " ) | |
! strcmp ( dirent - > d_name , " control " ) )
continue ;
2010-12-01 01:32:44 +03:00
if ( ! dm_task_set_name ( dmt , dirent - > d_name ) ) {
r = 0 ;
stack ;
continue ; /* try next name */
}
if ( ! dm_task_run ( dmt ) ) {
r = 0 ;
stack ; /* keep going */
}
2004-03-30 18:31:58 +04:00
}
2007-01-25 17:16:20 +03:00
if ( closedir ( d ) )
2007-07-28 14:23:02 +04:00
log_sys_error ( " closedir " , dir ) ;
2004-03-30 18:31:58 +04:00
return r ;
}
static int _process_all_v4 ( struct dm_task * dmt )
{
struct dm_task * task ;
struct dm_names * names ;
unsigned next = 0 ;
int r = 1 ;
if ( ! ( task = dm_task_create ( DM_DEVICE_LIST ) ) )
return 0 ;
if ( ! dm_task_run ( task ) ) {
r = 0 ;
goto out ;
}
if ( ! ( names = dm_task_get_names ( task ) ) ) {
r = 0 ;
goto out ;
}
if ( ! names - > dev )
goto out ;
do {
2011-02-19 02:09:55 +03:00
names = ( struct dm_names * ) ( ( char * ) names + next ) ;
2004-03-30 18:31:58 +04:00
if ( ! dm_task_set_name ( dmt , names - > name ) ) {
r = 0 ;
goto out ;
}
if ( ! dm_task_run ( dmt ) )
r = 0 ;
next = names - > next ;
} while ( next ) ;
out :
dm_task_destroy ( task ) ;
return r ;
}
static int _mknodes_v4 ( struct dm_task * dmt )
{
( void ) _process_mapper_dir ( dmt ) ;
return _process_all_v4 ( dmt ) ;
}
2009-08-06 19:02:01 +04:00
/*
* If an operation that uses a cookie fails , decrement the
* semaphore instead of udev .
*/
static int _udev_complete ( struct dm_task * dmt )
{
2010-03-23 17:38:37 +03:00
uint16_t base ;
2009-10-22 16:55:47 +04:00
2010-03-23 17:38:37 +03:00
if ( dmt - > cookie_set & &
( base = dmt - > event_nr & ~ DM_UDEV_FLAGS_MASK ) )
2009-10-22 16:55:47 +04:00
/* strip flags from the cookie and use cookie magic instead */
2010-03-23 17:38:37 +03:00
return dm_udev_complete ( base | ( DM_COOKIE_MAGIC < <
DM_UDEV_FLAGS_SHIFT ) ) ;
2009-08-06 19:02:01 +04:00
return 1 ;
}
2010-03-23 17:38:37 +03:00
static int _check_uevent_generated ( struct dm_ioctl * dmi )
{
if ( ! dm_check_version ( ) | |
_dm_version < 4 | |
_dm_version_minor < 17 )
/* can't check, assume uevent is generated */
return 1 ;
return dmi - > flags & DM_UEVENT_GENERATED_FLAG ;
}
2003-07-02 01:20:58 +04:00
static int _create_and_load_v4 ( struct dm_task * dmt )
2001-11-21 15:47:42 +03:00
{
2003-07-02 01:20:58 +04:00
struct dm_task * task ;
int r ;
2010-05-04 01:06:53 +04:00
uint32_t cookie ;
2001-11-21 15:47:42 +03:00
2003-07-02 01:20:58 +04:00
/* Use new task struct to create the device */
if ( ! ( task = dm_task_create ( DM_DEVICE_CREATE ) ) ) {
2009-08-06 19:02:01 +04:00
_udev_complete ( dmt ) ;
2011-10-20 14:38:04 +04:00
return_0 ;
2003-07-02 01:20:58 +04:00
}
2003-01-22 00:27:36 +03:00
2003-07-02 01:20:58 +04:00
/* Copy across relevant fields */
2011-10-20 14:38:04 +04:00
if ( dmt - > dev_name & & ! dm_task_set_name ( task , dmt - > dev_name ) )
goto_bad ;
2001-11-21 15:47:42 +03:00
2011-10-20 14:38:04 +04:00
if ( dmt - > uuid & & ! dm_task_set_uuid ( task , dmt - > uuid ) )
goto_bad ;
2001-11-21 20:08:37 +03:00
2003-07-02 01:20:58 +04:00
task - > major = dmt - > major ;
task - > minor = dmt - > minor ;
2006-02-03 17:23:22 +03:00
task - > uid = dmt - > uid ;
task - > gid = dmt - > gid ;
task - > mode = dmt - > mode ;
2009-10-26 17:29:33 +03:00
/* FIXME: Just for udev_check in dm_task_run. Can we avoid this? */
2009-11-13 15:43:21 +03:00
task - > event_nr = dmt - > event_nr & DM_UDEV_FLAGS_MASK ;
2009-10-26 17:29:33 +03:00
task - > cookie_set = dmt - > cookie_set ;
2011-03-02 02:27:06 +03:00
task - > add_node = dmt - > add_node ;
2003-07-02 01:20:58 +04:00
2011-10-20 14:38:04 +04:00
if ( ! dm_task_run ( task ) )
goto_bad ;
2003-07-02 01:20:58 +04:00
dm_task_destroy ( task ) ;
/* Next load the table */
if ( ! ( task = dm_task_create ( DM_DEVICE_RELOAD ) ) ) {
2011-10-20 14:38:04 +04:00
stack ;
2009-08-06 19:02:01 +04:00
_udev_complete ( dmt ) ;
2010-07-28 14:30:28 +04:00
goto revert ;
2003-07-02 01:20:58 +04:00
}
/* Copy across relevant fields */
if ( dmt - > dev_name & & ! dm_task_set_name ( task , dmt - > dev_name ) ) {
2011-10-20 14:38:04 +04:00
stack ;
2003-07-02 01:20:58 +04:00
dm_task_destroy ( task ) ;
2009-08-06 19:02:01 +04:00
_udev_complete ( dmt ) ;
2010-07-28 14:30:28 +04:00
goto revert ;
2001-11-21 15:47:42 +03:00
}
2003-07-02 01:20:58 +04:00
task - > read_only = dmt - > read_only ;
task - > head = dmt - > head ;
task - > tail = dmt - > tail ;
2011-02-04 19:08:11 +03:00
task - > secure_data = dmt - > secure_data ;
2003-07-02 01:20:58 +04:00
r = dm_task_run ( task ) ;
task - > head = NULL ;
task - > tail = NULL ;
dm_task_destroy ( task ) ;
2011-10-20 14:38:04 +04:00
2009-08-06 19:02:01 +04:00
if ( ! r ) {
2011-10-20 14:38:04 +04:00
stack ;
2009-08-06 19:02:01 +04:00
_udev_complete ( dmt ) ;
2006-03-30 19:15:47 +04:00
goto revert ;
2009-08-06 19:02:01 +04:00
}
2003-07-02 01:20:58 +04:00
/* Use the original structure last so the info will be correct */
dmt - > type = DM_DEVICE_RESUME ;
2005-10-17 02:57:20 +04:00
dm_free ( dmt - > uuid ) ;
2004-06-20 17:50:42 +04:00
dmt - > uuid = NULL ;
2012-10-10 19:00:41 +04:00
dm_free ( dmt - > mangled_uuid ) ;
dmt - > mangled_uuid = NULL ;
2003-07-02 01:20:58 +04:00
2011-10-20 14:38:04 +04:00
if ( dm_task_run ( dmt ) )
return 1 ;
2006-03-30 19:15:47 +04:00
revert :
2011-10-20 14:38:04 +04:00
dmt - > type = DM_DEVICE_REMOVE ;
2006-03-30 19:15:47 +04:00
dm_free ( dmt - > uuid ) ;
dmt - > uuid = NULL ;
2012-10-10 19:00:41 +04:00
dm_free ( dmt - > mangled_uuid ) ;
dmt - > mangled_uuid = NULL ;
2010-05-04 01:06:53 +04:00
/*
* Also udev - synchronize " remove " dm task that is a part of this revert !
* But only if the original dm task was supposed to be synchronized .
*/
if ( dmt - > cookie_set ) {
cookie = ( dmt - > event_nr & ~ DM_UDEV_FLAGS_MASK ) |
( DM_COOKIE_MAGIC < < DM_UDEV_FLAGS_SHIFT ) ;
2010-12-01 01:40:19 +03:00
if ( ! dm_task_set_cookie ( dmt , & cookie ,
( dmt - > event_nr & DM_UDEV_FLAGS_MASK ) > >
DM_UDEV_FLAGS_SHIFT ) )
2011-08-19 21:02:48 +04:00
stack ; /* keep going */
2010-05-04 01:06:53 +04:00
}
2006-03-30 19:15:47 +04:00
if ( ! dm_task_run ( dmt ) )
log_error ( " Failed to revert device creation. " ) ;
2011-10-20 14:38:04 +04:00
return 0 ;
bad :
dm_task_destroy ( task ) ;
_udev_complete ( dmt ) ;
return 0 ;
2003-07-02 01:20:58 +04:00
}
2008-09-18 22:34:53 +04:00
uint64_t dm_task_get_existing_table_size ( struct dm_task * dmt )
{
return dmt - > existing_table_size ;
}
2005-11-22 21:43:12 +03:00
static int _reload_with_suppression_v4 ( struct dm_task * dmt )
{
struct dm_task * task ;
struct target * t1 , * t2 ;
2011-03-09 01:43:19 +03:00
size_t len ;
2005-11-22 21:43:12 +03:00
int r ;
/* New task to get existing table information */
if ( ! ( task = dm_task_create ( DM_DEVICE_TABLE ) ) ) {
log_error ( " Failed to create device-mapper task struct " ) ;
return 0 ;
}
/* Copy across relevant fields */
if ( dmt - > dev_name & & ! dm_task_set_name ( task , dmt - > dev_name ) ) {
dm_task_destroy ( task ) ;
return 0 ;
}
if ( dmt - > uuid & & ! dm_task_set_uuid ( task , dmt - > uuid ) ) {
dm_task_destroy ( task ) ;
return 0 ;
}
task - > major = dmt - > major ;
task - > minor = dmt - > minor ;
r = dm_task_run ( task ) ;
if ( ! r ) {
dm_task_destroy ( task ) ;
return r ;
}
2008-09-18 22:34:53 +04:00
/* Store existing table size */
t2 = task - > head ;
while ( t2 & & t2 - > next )
t2 = t2 - > next ;
dmt - > existing_table_size = t2 ? t2 - > start + t2 - > length : 0 ;
2011-07-25 03:59:03 +04:00
if ( ( ( task - > dmi . v4 - > flags & DM_READONLY_FLAG ) ? 1 : 0 ) ! = dmt - > read_only )
2006-06-15 02:00:03 +04:00
goto no_match ;
2005-11-22 21:43:12 +03:00
t1 = dmt - > head ;
t2 = task - > head ;
while ( t1 & & t2 ) {
2011-03-09 01:43:19 +03:00
len = strlen ( t2 - > params ) ;
while ( len - - > 0 & & t2 - > params [ len ] = = ' ' )
t2 - > params [ len ] = ' \0 ' ;
2005-11-22 21:43:12 +03:00
if ( ( t1 - > start ! = t2 - > start ) | |
( t1 - > length ! = t2 - > length ) | |
( strcmp ( t1 - > type , t2 - > type ) ) | |
2006-06-15 02:00:03 +04:00
( strcmp ( t1 - > params , t2 - > params ) ) )
goto no_match ;
2005-11-22 21:43:12 +03:00
t1 = t1 - > next ;
t2 = t2 - > next ;
}
2006-06-15 02:00:03 +04:00
if ( ! t1 & & ! t2 ) {
2005-11-22 21:43:12 +03:00
dmt - > dmi . v4 = task - > dmi . v4 ;
task - > dmi . v4 = NULL ;
dm_task_destroy ( task ) ;
return 1 ;
}
2006-06-15 02:00:03 +04:00
no_match :
2005-11-22 21:43:12 +03:00
dm_task_destroy ( task ) ;
/* Now do the original reload */
dmt - > suppress_identical_reload = 0 ;
r = dm_task_run ( dmt ) ;
return r ;
}
2011-07-02 05:17:09 +04:00
static int _check_children_not_suspended_v4 ( struct dm_task * dmt , uint64_t device )
2011-07-01 18:09:19 +04:00
{
2011-07-02 05:17:09 +04:00
struct dm_task * task ;
struct dm_info info ;
struct dm_deps * deps ;
int r = 0 ;
uint32_t i ;
/* Find dependencies */
if ( ! ( task = dm_task_create ( DM_DEVICE_DEPS ) ) )
return 0 ;
/* Copy across or set relevant fields */
if ( device ) {
task - > major = MAJOR ( device ) ;
task - > minor = MINOR ( device ) ;
} else {
if ( dmt - > dev_name & & ! dm_task_set_name ( task , dmt - > dev_name ) )
goto out ;
if ( dmt - > uuid & & ! dm_task_set_uuid ( task , dmt - > uuid ) )
goto out ;
task - > major = dmt - > major ;
task - > minor = dmt - > minor ;
}
task - > uid = dmt - > uid ;
task - > gid = dmt - > gid ;
task - > mode = dmt - > mode ;
/* FIXME: Just for udev_check in dm_task_run. Can we avoid this? */
task - > event_nr = dmt - > event_nr & DM_UDEV_FLAGS_MASK ;
task - > cookie_set = dmt - > cookie_set ;
task - > add_node = dmt - > add_node ;
if ( ! ( r = dm_task_run ( task ) ) )
goto out ;
if ( ! dm_task_get_info ( task , & info ) | | ! info . exists )
goto out ;
2011-07-01 18:09:19 +04:00
/*
2011-07-02 05:17:09 +04:00
* Warn if any of the devices this device depends upon are already
* suspended : I / O could become trapped between the two devices .
2011-07-01 18:09:19 +04:00
*/
2011-07-02 05:17:09 +04:00
if ( info . suspended ) {
if ( ! device )
log_debug ( " Attempting to suspend a device that is already suspended "
" (%u:%u) " , info . major , info . minor ) ;
else
log_error ( INTERNAL_ERROR " Attempt to suspend device %s%s%s%.0d%s%.0d%s%s "
" that uses already-suspended device (%u:%u) " ,
2012-10-10 19:02:19 +04:00
DEV_NAME ( dmt ) ? : " " , DEV_UUID ( dmt ) ? : " " ,
2011-07-02 05:17:09 +04:00
dmt - > major > 0 ? " ( " : " " ,
dmt - > major > 0 ? dmt - > major : 0 ,
dmt - > major > 0 ? " : " : " " ,
dmt - > minor > 0 ? dmt - > minor : 0 ,
dmt - > major > 0 & & dmt - > minor = = 0 ? " 0 " : " " ,
dmt - > major > 0 ? " ) " : " " ,
info . major , info . minor ) ;
/* No need for further recursion */
r = 1 ;
goto out ;
}
2011-08-19 21:02:48 +04:00
if ( ! ( deps = dm_task_get_deps ( task ) ) )
goto out ;
2011-07-02 05:17:09 +04:00
for ( i = 0 ; i < deps - > count ; i + + ) {
/* Only recurse with dm devices */
if ( MAJOR ( deps - > device [ i ] ) ! = _dm_device_major )
continue ;
if ( ! _check_children_not_suspended_v4 ( task , deps - > device [ i ] ) )
goto out ;
}
r = 1 ;
out :
dm_task_destroy ( task ) ;
return r ;
}
static int _suspend_with_validation_v4 ( struct dm_task * dmt )
{
/* Avoid recursion */
2011-07-01 18:09:19 +04:00
dmt - > enable_checks = 0 ;
2011-07-02 05:17:09 +04:00
/*
* Ensure we can ' t leave any I / O trapped between suspended devices .
*/
if ( ! _check_children_not_suspended_v4 ( dmt , 0 ) )
return 0 ;
/* Finally, perform the original suspend. */
2011-07-01 18:09:19 +04:00
return dm_task_run ( dmt ) ;
}
2010-05-13 17:31:30 +04:00
static const char * _sanitise_message ( char * message )
{
const char * sanitised_message = message ? : " " ;
/* FIXME: Check for whitespace variations. */
/* This traps what cryptsetup sends us. */
if ( message & & ! strncasecmp ( message , " key set " , 7 ) )
sanitised_message = " key set " ;
return sanitised_message ;
}
2012-10-10 19:01:42 +04:00
static int _do_dm_ioctl_unmangle_string ( char * str , const char * str_name ,
char * buf , size_t buf_size ,
dm_string_mangling_t mode )
2012-02-15 16:01:28 +04:00
{
int r ;
2012-03-05 16:40:34 +04:00
if ( mode = = DM_STRING_MANGLING_NONE )
2012-03-05 16:43:03 +04:00
return 1 ;
2012-03-05 16:40:34 +04:00
2012-10-10 19:01:42 +04:00
if ( ! check_multiple_mangled_string_allowed ( str , str_name , mode ) )
2012-03-05 16:48:12 +04:00
return_0 ;
2012-10-10 19:01:42 +04:00
if ( ( r = unmangle_string ( str , str_name , strlen ( str ) , buf , buf_size , mode ) ) < 0 ) {
log_debug ( " _do_dm_ioctl_unmangle_string: failed to "
" unmangle %s \" %s \" " , str_name , str ) ;
2012-03-05 16:43:03 +04:00
return 0 ;
} else if ( r )
2012-10-10 19:01:42 +04:00
memcpy ( str , buf , strlen ( buf ) + 1 ) ;
2012-03-05 16:43:03 +04:00
return 1 ;
2012-02-15 16:01:28 +04:00
}
2012-03-05 16:43:03 +04:00
static int _dm_ioctl_unmangle_names ( int type , struct dm_ioctl * dmi )
2012-02-15 16:01:28 +04:00
{
2012-10-10 19:01:42 +04:00
char buf [ DM_NAME_LEN ] ;
2012-02-15 16:01:28 +04:00
struct dm_names * names ;
unsigned next = 0 ;
char * name ;
2012-03-05 16:43:03 +04:00
int r = 1 ;
2012-02-15 16:01:28 +04:00
if ( ( name = dmi - > name ) )
2012-10-10 19:01:42 +04:00
r = _do_dm_ioctl_unmangle_string ( name , " name " , buf , sizeof ( buf ) ,
dm_get_name_mangling_mode ( ) ) ;
2012-02-15 16:01:28 +04:00
if ( type = = DM_DEVICE_LIST & &
( ( names = ( ( struct dm_names * ) ( ( char * ) dmi + dmi - > data_start ) ) ) ) & &
names - > dev ) {
do {
names = ( struct dm_names * ) ( ( char * ) names + next ) ;
2012-10-10 19:01:42 +04:00
r = _do_dm_ioctl_unmangle_string ( names - > name , " name " ,
buf , sizeof ( buf ) ,
dm_get_name_mangling_mode ( ) ) ;
2012-02-15 16:01:28 +04:00
next = names - > next ;
} while ( next ) ;
}
2012-03-05 16:43:03 +04:00
return r ;
2012-02-15 16:01:28 +04:00
}
2012-10-10 19:01:42 +04:00
static int _dm_ioctl_unmangle_uuids ( int type , struct dm_ioctl * dmi )
{
char buf [ DM_UUID_LEN ] ;
char * uuid = dmi - > uuid ;
if ( uuid )
return _do_dm_ioctl_unmangle_string ( uuid , " UUID " , buf , sizeof ( buf ) ,
dm_get_name_mangling_mode ( ) ) ;
return 1 ;
}
2005-09-20 22:04:28 +04:00
static struct dm_ioctl * _do_dm_ioctl ( struct dm_task * dmt , unsigned command ,
2011-09-22 21:09:48 +04:00
unsigned buffer_repeat_count ,
unsigned retry_repeat_count ,
int * retryable )
2003-07-02 01:20:58 +04:00
{
2005-09-19 18:29:17 +04:00
struct dm_ioctl * dmi ;
2010-03-23 17:38:37 +03:00
int ioctl_with_uevent ;
2003-07-02 01:20:58 +04:00
2011-09-22 21:09:48 +04:00
dmi = _flatten ( dmt , buffer_repeat_count ) ;
2003-07-02 01:20:58 +04:00
if ( ! dmi ) {
2005-11-09 01:50:11 +03:00
log_error ( " Couldn't create ioctl argument. " ) ;
2005-09-19 18:29:17 +04:00
return NULL ;
2003-07-02 01:20:58 +04:00
}
2002-06-19 17:07:05 +04:00
if ( dmt - > type = = DM_DEVICE_TABLE )
dmi - > flags | = DM_STATUS_TABLE_FLAG ;
2003-04-24 20:08:18 +04:00
dmi - > flags | = DM_EXISTS_FLAG ; /* FIXME */
2005-01-13 01:10:14 +03:00
if ( dmt - > no_open_count )
dmi - > flags | = DM_SKIP_BDGET_FLAG ;
2010-03-23 17:38:37 +03:00
ioctl_with_uevent = dmt - > type = = DM_DEVICE_RESUME | |
dmt - > type = = DM_DEVICE_REMOVE | |
dmt - > type = = DM_DEVICE_RENAME ;
2010-04-28 17:37:36 +04:00
if ( ioctl_with_uevent & & dm_cookie_supported ( ) ) {
2009-10-26 17:29:33 +03:00
/*
2010-04-28 17:37:36 +04:00
* Always mark events coming from libdevmapper as
* " primary sourced " . This is needed to distinguish
* any spurious events so we can act appropriately .
* This needs to be applied even when udev_sync is
* not used because udev flags could be used alone .
*/
dmi - > event_nr | = DM_UDEV_PRIMARY_SOURCE_FLAG < <
2009-10-26 17:29:33 +03:00
DM_UDEV_FLAGS_SHIFT ;
2010-04-28 17:37:36 +04:00
/*
* Prevent udev vs . libdevmapper race when processing nodes
* and symlinks . This can happen when the udev rules are
* installed and udev synchronisation code is enabled in
* libdevmapper but the software using libdevmapper does not
* make use of it ( by not calling dm_task_set_cookie before ) .
* We need to instruct the udev rules not to be applied at
* all in this situation so we can gracefully fallback to
* libdevmapper ' s node and symlink creation code .
*/
if ( ! dmt - > cookie_set & & dm_udev_get_sync_support ( ) ) {
2010-05-04 02:08:38 +04:00
log_debug ( " Cookie value is not set while trying to call %s "
2010-04-28 17:37:36 +04:00
" ioctl. Please, consider using libdevmapper's udev "
" synchronisation interface or disable it explicitly "
2010-05-04 02:08:38 +04:00
" by calling dm_udev_set_sync_support(0). " ,
dmt - > type = = DM_DEVICE_RESUME ? " DM_DEVICE_RESUME " :
dmt - > type = = DM_DEVICE_REMOVE ? " DM_DEVICE_REMOVE " :
" DM_DEVICE_RENAME " ) ;
2010-04-28 17:37:36 +04:00
log_debug ( " Switching off device-mapper and all subsystem related "
" udev rules. Falling back to libdevmapper node creation. " ) ;
/*
* Disable general dm and subsystem rules but keep
* dm disk rules if not flagged out explicitly before .
* We need / dev / disk content for the software that expects it .
*/
dmi - > event_nr | = ( DM_UDEV_DISABLE_DM_RULES_FLAG |
DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG ) < <
DM_UDEV_FLAGS_SHIFT ;
}
2009-10-26 17:29:33 +03:00
}
2010-10-15 05:10:27 +04:00
log_debug ( " dm %s %s%s %s%s%s %s%.0d%s%.0d%s "
2011-09-22 21:09:48 +04:00
" %s%c%c%s%s%s%s%s%s %.0 " PRIu64 " %s [%u] (*%u) " ,
2005-09-20 22:04:28 +04:00
_cmd_data_v4 [ dmt - > type ] . name ,
2010-10-15 05:10:27 +04:00
dmt - > new_uuid ? " UUID " : " " ,
2005-09-20 22:04:28 +04:00
dmi - > name , dmi - > uuid , dmt - > newname ? " " : " " ,
dmt - > newname ? dmt - > newname : " " ,
2005-10-18 17:07:41 +04:00
dmt - > major > 0 ? " ( " : " " ,
2005-10-26 22:32:57 +04:00
dmt - > major > 0 ? dmt - > major : 0 ,
2005-10-18 17:07:41 +04:00
dmt - > major > 0 ? " : " : " " ,
2005-10-26 22:32:57 +04:00
dmt - > minor > 0 ? dmt - > minor : 0 ,
2005-11-09 01:50:11 +03:00
dmt - > major > 0 & & dmt - > minor = = 0 ? " 0 " : " " ,
2005-10-18 17:07:41 +04:00
dmt - > major > 0 ? " ) " : " " ,
2005-01-13 01:10:14 +03:00
dmt - > no_open_count ? ' N ' : ' O ' ,
2006-10-12 21:29:05 +04:00
dmt - > no_flush ? ' N ' : ' F ' ,
2011-06-29 20:08:33 +04:00
dmt - > read_only ? " R " : " " ,
2006-08-09 01:22:31 +04:00
dmt - > skip_lockfs ? " S " : " " ,
2011-09-22 21:09:48 +04:00
dmt - > retry_remove ? " T " : " " ,
2011-02-04 19:08:11 +03:00
dmt - > secure_data ? " W " : " " ,
2009-11-06 03:43:08 +03:00
dmt - > query_inactive_table ? " I " : " " ,
2011-07-01 18:09:19 +04:00
dmt - > enable_checks ? " C " : " " ,
2010-05-13 17:31:30 +04:00
dmt - > sector , _sanitise_message ( dmt - > message ) ,
2011-09-22 21:09:48 +04:00
dmi - > data_size , retry_repeat_count ) ;
2005-01-27 19:16:54 +03:00
# ifdef DM_IOCTLS
2011-10-19 20:36:01 +04:00
if ( ioctl ( _control_fd , command , dmi ) < 0 & &
dmt - > expected_errno ! = errno ) {
2003-11-13 16:14:28 +03:00
if ( errno = = ENXIO & & ( ( dmt - > type = = DM_DEVICE_INFO ) | |
2005-10-26 22:32:57 +04:00
( dmt - > type = = DM_DEVICE_MKNODES ) | |
( dmt - > type = = DM_DEVICE_STATUS ) ) )
2003-04-24 20:08:18 +04:00
dmi - > flags & = ~ DM_EXISTS_FLAG ; /* FIXME */
2005-01-27 19:16:54 +03:00
else {
if ( _log_suppress )
2005-12-02 02:11:41 +03:00
log_verbose ( " device-mapper: %s ioctl "
" failed: %s " ,
_cmd_data_v4 [ dmt - > type ] . name ,
strerror ( errno ) ) ;
2005-01-27 19:16:54 +03:00
else
2011-11-30 06:02:10 +04:00
log_error ( " device-mapper: %s ioctl on %s "
2005-12-02 02:11:41 +03:00
" failed: %s " ,
2011-08-18 23:43:08 +04:00
_cmd_data_v4 [ dmt - > type ] . name ,
2011-11-30 06:02:10 +04:00
dmi - > name , strerror ( errno ) ) ;
2011-09-22 21:09:48 +04:00
2011-09-23 21:16:28 +04:00
/*
* It ' s sometimes worth retrying after EBUSY in case
* it ' s a transient failure caused by an asynchronous
* process quickly scanning the device .
*/
2011-09-22 21:09:48 +04:00
* retryable = errno = = EBUSY ;
2012-03-05 16:43:03 +04:00
goto error ;
2003-04-24 20:08:18 +04:00
}
2001-11-21 15:47:42 +03:00
}
2010-03-23 17:38:37 +03:00
2011-03-02 03:29:57 +03:00
if ( ioctl_with_uevent & & dm_udev_get_sync_support ( ) & &
! _check_uevent_generated ( dmi ) ) {
2011-01-31 14:54:55 +03:00
log_debug ( " Uevent not generated! Calling udev_complete "
" internally to avoid process lock-up. " ) ;
2010-03-23 17:38:37 +03:00
_udev_complete ( dmt ) ;
2011-01-31 14:54:55 +03:00
}
2010-03-23 17:38:37 +03:00
2012-03-05 16:43:03 +04:00
if ( ! _dm_ioctl_unmangle_names ( dmt - > type , dmi ) )
goto error ;
2012-02-15 16:01:28 +04:00
2012-10-12 13:53:04 +04:00
if ( dmt - > type ! = DM_DEVICE_REMOVE & &
! _dm_ioctl_unmangle_uuids ( dmt - > type , dmi ) )
2012-10-10 19:01:42 +04:00
goto error ;
2005-01-27 19:16:54 +03:00
# else /* Userspace alternative for testing */
# endif
2005-09-19 18:29:17 +04:00
return dmi ;
2012-03-05 16:43:03 +04:00
error :
_dm_zfree_dmi ( dmi ) ;
return NULL ;
2005-09-19 18:29:17 +04:00
}
2001-11-21 15:47:42 +03:00
2005-12-02 02:11:41 +03:00
void dm_task_update_nodes ( void )
{
update_devs ( ) ;
}
2011-09-22 21:09:48 +04:00
# define DM_IOCTL_RETRIES 25
# define DM_RETRY_USLEEP_DELAY 200000
2005-09-19 18:29:17 +04:00
int dm_task_run ( struct dm_task * dmt )
{
struct dm_ioctl * dmi ;
unsigned command ;
2009-10-26 17:29:33 +03:00
int check_udev ;
2011-06-28 01:43:58 +04:00
int rely_on_udev ;
2011-06-13 07:32:45 +04:00
int suspended_counter ;
2011-09-22 21:09:48 +04:00
unsigned ioctl_retry = 1 ;
2011-09-22 21:59:58 +04:00
int retryable = 0 ;
2012-02-15 15:33:53 +04:00
const char * dev_name = DEV_NAME ( dmt ) ;
2012-10-10 19:02:19 +04:00
const char * dev_uuid = DEV_UUID ( dmt ) ;
2005-09-19 18:29:17 +04:00
if ( ( unsigned ) dmt - > type > =
( sizeof ( _cmd_data_v4 ) / sizeof ( * _cmd_data_v4 ) ) ) {
2010-03-25 21:22:04 +03:00
log_error ( INTERNAL_ERROR " unknown device-mapper task %d " ,
2005-09-19 18:29:17 +04:00
dmt - > type ) ;
return 0 ;
}
command = _cmd_data_v4 [ dmt - > type ] . cmd ;
/* Old-style creation had a table supplied */
if ( dmt - > type = = DM_DEVICE_CREATE & & dmt - > head )
return _create_and_load_v4 ( dmt ) ;
2012-02-15 15:33:53 +04:00
if ( dmt - > type = = DM_DEVICE_MKNODES & & ! dev_name & &
2012-10-10 19:02:19 +04:00
! dev_uuid & & dmt - > major < = 0 )
2005-09-19 18:29:17 +04:00
return _mknodes_v4 ( dmt ) ;
2005-11-22 21:43:12 +03:00
if ( ( dmt - > type = = DM_DEVICE_RELOAD ) & & dmt - > suppress_identical_reload )
return _reload_with_suppression_v4 ( dmt ) ;
2011-07-01 18:09:19 +04:00
if ( ( dmt - > type = = DM_DEVICE_SUSPEND ) & & dmt - > enable_checks )
return _suspend_with_validation_v4 ( dmt ) ;
2009-08-06 19:02:01 +04:00
if ( ! _open_control ( ) ) {
_udev_complete ( dmt ) ;
2011-10-20 14:38:04 +04:00
return_0 ;
2009-08-06 19:02:01 +04:00
}
2005-09-19 18:29:17 +04:00
2011-06-13 07:32:45 +04:00
if ( ( suspended_counter = dm_get_suspended_counter ( ) ) & &
2011-06-29 15:36:37 +04:00
dmt - > type = = DM_DEVICE_RELOAD )
2011-06-29 12:54:13 +04:00
log_error ( INTERNAL_ERROR " Performing unsafe table load while %d device(s) "
2011-06-13 07:32:45 +04:00
" are known to be suspended: "
2011-06-13 07:53:02 +04:00
" %s%s%s %s%.0d%s%.0d%s%s " ,
2011-06-13 07:32:45 +04:00
suspended_counter ,
2012-02-15 15:33:53 +04:00
dev_name ? : " " ,
2012-10-10 19:02:19 +04:00
dev_uuid ? " UUID " : " " ,
dev_uuid ? : " " ,
2011-08-19 21:02:48 +04:00
dmt - > major > 0 ? " ( " : " " ,
dmt - > major > 0 ? dmt - > major : 0 ,
dmt - > major > 0 ? " : " : " " ,
dmt - > minor > 0 ? dmt - > minor : 0 ,
dmt - > major > 0 & & dmt - > minor = = 0 ? " 0 " : " " ,
dmt - > major > 0 ? " ) " : " " ) ;
2011-06-13 07:32:45 +04:00
2009-08-03 22:01:45 +04:00
/* FIXME Detect and warn if cookie set but should not be. */
2005-09-19 18:29:17 +04:00
repeat_ioctl :
2011-09-22 21:09:48 +04:00
if ( ! ( dmi = _do_dm_ioctl ( dmt , command , _ioctl_buffer_double_factor ,
ioctl_retry , & retryable ) ) ) {
2011-09-23 21:16:28 +04:00
/*
* Async udev rules that scan devices commonly cause transient
* failures . Normally you ' d expect the user to have made sure
* nothing was using the device before issuing REMOVE , so it ' s
* worth retrying in case the failure is indeed transient .
*/
2011-09-22 21:09:48 +04:00
if ( retryable & & dmt - > type = = DM_DEVICE_REMOVE & &
dmt - > retry_remove & & + + ioctl_retry < = DM_IOCTL_RETRIES ) {
usleep ( DM_RETRY_USLEEP_DELAY ) ;
goto repeat_ioctl ;
}
2009-08-06 19:02:01 +04:00
_udev_complete ( dmt ) ;
2005-09-19 18:29:17 +04:00
return 0 ;
2009-08-03 22:01:45 +04:00
}
2005-09-19 18:29:17 +04:00
if ( dmi - > flags & DM_BUFFER_FULL_FLAG ) {
switch ( dmt - > type ) {
case DM_DEVICE_LIST_VERSIONS :
case DM_DEVICE_LIST :
case DM_DEVICE_DEPS :
case DM_DEVICE_STATUS :
case DM_DEVICE_TABLE :
case DM_DEVICE_WAITEVENT :
2005-09-20 22:04:28 +04:00
_ioctl_buffer_double_factor + + ;
2010-04-07 19:57:20 +04:00
_dm_zfree_dmi ( dmi ) ;
2005-09-19 18:29:17 +04:00
goto repeat_ioctl ;
default :
2007-06-28 21:27:02 +04:00
log_error ( " WARNING: libdevmapper buffer too small for data " ) ;
2005-09-19 18:29:17 +04:00
}
}
2005-08-08 22:40:17 +04:00
2011-06-28 01:43:58 +04:00
/*
* Are we expecting a udev operation to occur that we need to check for ?
*/
2009-10-26 17:29:33 +03:00
check_udev = dmt - > cookie_set & &
! ( dmt - > event_nr > > DM_UDEV_FLAGS_SHIFT &
DM_UDEV_DISABLE_DM_RULES_FLAG ) ;
2011-06-28 01:43:58 +04:00
rely_on_udev = dmt - > cookie_set ? ( dmt - > event_nr > > DM_UDEV_FLAGS_SHIFT &
DM_UDEV_DISABLE_LIBRARY_FALLBACK ) : 0 ;
2010-02-15 19:21:33 +03:00
2001-11-21 15:47:42 +03:00
switch ( dmt - > type ) {
case DM_DEVICE_CREATE :
2011-03-02 11:41:55 +03:00
if ( ( dmt - > add_node = = DM_ADD_NODE_ON_CREATE ) & &
2012-02-15 15:33:53 +04:00
dev_name & & * dev_name & & ! rely_on_udev )
add_dev_node ( dev_name , MAJOR ( dmi - > dev ) ,
2008-05-21 20:14:46 +04:00
MINOR ( dmi - > dev ) , dmt - > uid , dmt - > gid ,
2011-06-28 01:43:58 +04:00
dmt - > mode , check_udev , rely_on_udev ) ;
2001-11-21 15:47:42 +03:00
break ;
case DM_DEVICE_REMOVE :
2004-10-01 23:11:37 +04:00
/* FIXME Kernel needs to fill in dmi->name */
2012-02-15 15:33:53 +04:00
if ( dev_name & & ! rely_on_udev )
rm_dev_node ( dev_name , check_udev , rely_on_udev ) ;
2001-11-21 15:47:42 +03:00
break ;
2002-04-11 16:45:18 +04:00
case DM_DEVICE_RENAME :
2004-10-01 23:11:37 +04:00
/* FIXME Kernel needs to fill in dmi->name */
2012-02-15 15:33:53 +04:00
if ( ! dmt - > new_uuid & & dev_name )
rename_dev_node ( dev_name , dmt - > newname ,
2011-06-28 01:43:58 +04:00
check_udev , rely_on_udev ) ;
2002-04-11 16:45:18 +04:00
break ;
2002-05-03 15:55:58 +04:00
2007-11-30 17:59:57 +03:00
case DM_DEVICE_RESUME :
2011-02-04 22:33:53 +03:00
if ( ( dmt - > add_node = = DM_ADD_NODE_ON_RESUME ) & &
2012-02-15 15:33:53 +04:00
dev_name & & * dev_name )
add_dev_node ( dev_name , MAJOR ( dmi - > dev ) ,
2011-02-04 22:33:53 +03:00
MINOR ( dmi - > dev ) , dmt - > uid , dmt - > gid ,
2011-06-28 01:43:58 +04:00
dmt - > mode , check_udev , rely_on_udev ) ;
2007-12-04 01:48:36 +03:00
/* FIXME Kernel needs to fill in dmi->name */
2012-02-15 15:33:53 +04:00
set_dev_node_read_ahead ( dev_name ,
2012-01-09 16:26:14 +04:00
MAJOR ( dmi - > dev ) , MINOR ( dmi - > dev ) ,
dmt - > read_ahead , dmt - > read_ahead_flags ) ;
2007-11-30 17:59:57 +03:00
break ;
2003-11-13 16:14:28 +03:00
case DM_DEVICE_MKNODES :
if ( dmi - > flags & DM_EXISTS_FLAG )
2004-10-01 23:11:37 +04:00
add_dev_node ( dmi - > name , MAJOR ( dmi - > dev ) ,
2009-10-26 17:29:33 +03:00
MINOR ( dmi - > dev ) , dmt - > uid ,
2011-06-28 01:43:58 +04:00
dmt - > gid , dmt - > mode , 0 , rely_on_udev ) ;
2012-02-15 15:33:53 +04:00
else if ( dev_name )
rm_dev_node ( dev_name , 0 , rely_on_udev ) ;
2003-11-13 16:14:28 +03:00
break ;
2002-05-03 15:55:58 +04:00
case DM_DEVICE_STATUS :
case DM_DEVICE_TABLE :
2003-04-28 15:55:58 +04:00
case DM_DEVICE_WAITEVENT :
2002-05-10 19:25:38 +04:00
if ( ! _unmarshal_status ( dmt , dmi ) )
goto bad ;
break ;
2001-11-21 15:47:42 +03:00
}
2006-04-03 19:56:02 +04:00
/* Was structure reused? */
2010-04-07 19:57:20 +04:00
_dm_zfree_dmi ( dmt - > dmi . v4 ) ;
2003-04-30 02:52:11 +04:00
dmt - > dmi . v4 = dmi ;
2001-11-21 15:47:42 +03:00
return 1 ;
2002-03-07 23:56:10 +03:00
bad :
2010-04-07 19:57:20 +04:00
_dm_zfree_dmi ( dmi ) ;
2001-11-21 15:47:42 +03:00
return 0 ;
}
2003-07-02 01:20:58 +04:00
void dm_lib_release ( void )
{
2010-08-18 17:11:56 +04:00
_close_control_fd ( ) ;
2003-07-02 01:20:58 +04:00
update_devs ( ) ;
}
2009-04-10 13:56:58 +04:00
void dm_pools_check_leaks ( void ) ;
2003-07-02 01:20:58 +04:00
void dm_lib_exit ( void )
{
2011-06-24 23:33:41 +04:00
int suspended_counter ;
static unsigned _exited = 0 ;
if ( _exited + + )
return ;
if ( ( suspended_counter = dm_get_suspended_counter ( ) ) )
log_error ( " libdevmapper exiting with %d device(s) still suspended. " , suspended_counter ) ;
2005-05-16 19:15:34 +04:00
dm_lib_release ( ) ;
2010-12-13 13:43:56 +03:00
selinux_release ( ) ;
2005-10-16 18:33:22 +04:00
if ( _dm_bitset )
2005-10-17 02:57:20 +04:00
dm_bitset_destroy ( _dm_bitset ) ;
2005-10-16 18:33:22 +04:00
_dm_bitset = NULL ;
2009-04-10 13:56:58 +04:00
dm_pools_check_leaks ( ) ;
2005-10-17 02:57:20 +04:00
dm_dump_memory ( ) ;
2003-07-02 01:20:58 +04:00
_version_ok = 1 ;
_version_checked = 0 ;
}