2004-11-18 23:02:21 +03:00
/*
* Copyright ( C ) 2004 Luca Berra
2008-09-19 09:19:09 +04:00
* Copyright ( C ) 2004 - 2008 Red Hat , Inc . All rights reserved .
2004-11-18 23:02:21 +03:00
*
* This file is part of LVM2 .
*
* 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 ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2004-11-18 23:02:21 +03:00
*/
# include "lib.h"
2013-06-12 14:08:56 +04:00
# include "dev-type.h"
2004-11-28 01:07:41 +03:00
# include "xlate.h"
2014-09-03 17:47:52 +04:00
# ifdef UDEV_SYNC_SUPPORT
# include <libudev.h> /* for MD detection using udev db records */
2015-01-29 18:44:34 +03:00
# include "dev-ext-udev-constants.h"
2014-09-03 17:47:52 +04:00
# endif
2004-11-18 23:02:21 +03:00
2013-11-13 17:56:29 +04:00
# ifdef __linux__
2007-10-24 04:30:30 +04:00
2004-11-18 23:02:21 +03:00
/* Lifted from <linux/raid/md_p.h> because of difficulty including it */
# define MD_SB_MAGIC 0xa92b4efc
2007-10-24 15:24:24 +04:00
# define MD_RESERVED_BYTES (64 * 1024ULL)
2004-11-18 23:02:21 +03:00
# define MD_RESERVED_SECTORS (MD_RESERVED_BYTES / 512)
2017-07-19 17:17:30 +03:00
# define MD_NEW_SIZE_SECTORS(x) (((x) & ~(MD_RESERVED_SECTORS - 1)) \
2004-11-18 23:02:21 +03:00
- MD_RESERVED_SECTORS )
2011-07-08 19:53:59 +04:00
# define MD_MAX_SYSFS_SIZE 64
2004-11-18 23:02:21 +03:00
2007-10-24 04:30:30 +04:00
static int _dev_has_md_magic ( struct device * dev , uint64_t sb_offset )
{
uint32_t md_magic ;
/* Version 1 is little endian; version 0.90.0 is machine endian */
2017-12-05 02:18:56 +03:00
if ( dev_read ( dev , sb_offset , sizeof ( uint32_t ) , DEV_IO_SIGNATURES , & md_magic ) & &
2016-02-23 15:16:55 +03:00
( ( md_magic = = MD_SB_MAGIC ) | |
( ( MD_SB_MAGIC ! = xlate32 ( MD_SB_MAGIC ) ) & & ( md_magic = = xlate32 ( MD_SB_MAGIC ) ) ) ) )
2007-10-24 04:30:30 +04:00
return 1 ;
return 0 ;
}
2007-10-24 15:24:24 +04:00
/*
* Calculate the position of the superblock .
* It is always aligned to a 4 K boundary and
* depending on minor_version , it can be :
* 0 : At least 8 K , but less than 12 K , from end of device
* 1 : At start of device
* 2 : 4 K from start of device .
*/
2008-06-23 18:54:50 +04:00
typedef enum {
MD_MINOR_VERSION_MIN ,
MD_MINOR_V0 = MD_MINOR_VERSION_MIN ,
MD_MINOR_V1 ,
MD_MINOR_V2 ,
MD_MINOR_VERSION_MAX = MD_MINOR_V2
} md_minor_version_t ;
static uint64_t _v1_sb_offset ( uint64_t size , md_minor_version_t minor_version )
2007-10-24 04:51:05 +04:00
{
2014-05-24 01:53:04 +04:00
uint64_t sb_offset ;
2007-10-24 04:51:05 +04:00
switch ( minor_version ) {
2008-06-23 18:54:50 +04:00
case MD_MINOR_V0 :
2007-10-24 15:24:24 +04:00
sb_offset = ( size - 8 * 2 ) & ~ ( 4 * 2 - 1ULL ) ;
2007-10-24 04:51:05 +04:00
break ;
2008-06-23 18:54:50 +04:00
case MD_MINOR_V1 :
2007-10-24 04:51:05 +04:00
sb_offset = 0 ;
break ;
2008-06-23 18:54:50 +04:00
case MD_MINOR_V2 :
2007-10-24 04:51:05 +04:00
sb_offset = 4 * 2 ;
break ;
2014-05-24 01:53:04 +04:00
default :
log_warn ( INTERNAL_ERROR " WARNING: Unknown minor version %d. " ,
minor_version ) ;
return 0 ;
2007-10-24 04:51:05 +04:00
}
sb_offset < < = SECTOR_SHIFT ;
return sb_offset ;
}
2014-09-03 17:47:52 +04:00
# ifdef UDEV_SYNC_SUPPORT
static int _udev_dev_is_md ( struct device * dev )
{
const char * value ;
struct dev_ext * ext ;
if ( ! ( ext = dev_ext_get ( dev ) ) )
return_0 ;
2015-01-29 18:44:34 +03:00
if ( ! ( value = udev_device_get_property_value ( ( struct udev_device * ) ext - > handle , DEV_EXT_UDEV_BLKID_TYPE ) ) )
2014-09-03 17:47:52 +04:00
return 0 ;
2015-01-29 18:44:34 +03:00
return ! strcmp ( value , DEV_EXT_UDEV_BLKID_TYPE_SW_RAID ) ;
2014-09-03 17:47:52 +04:00
}
# else
static int _udev_dev_is_md ( struct device * dev )
{
return 0 ;
}
# endif
2004-11-18 23:02:21 +03:00
/*
* Returns - 1 on error
*/
2014-09-03 17:47:52 +04:00
static int _native_dev_is_md ( struct device * dev , uint64_t * offset_found )
2004-11-18 23:02:21 +03:00
{
2007-10-24 04:51:05 +04:00
int ret = 1 ;
2008-06-23 18:54:50 +04:00
md_minor_version_t minor ;
2004-11-18 23:02:21 +03:00
uint64_t size , sb_offset ;
if ( ! dev_get_size ( dev , & size ) ) {
stack ;
return - 1 ;
}
if ( size < MD_RESERVED_SECTORS * 2 )
return 0 ;
2011-05-28 13:48:14 +04:00
if ( ! dev_open_readonly ( dev ) ) {
2004-11-18 23:02:21 +03:00
stack ;
return - 1 ;
}
2007-10-24 04:51:05 +04:00
/* Check if it is an md component device. */
/* Version 0.90.0 */
2004-11-18 23:02:21 +03:00
sb_offset = MD_NEW_SIZE_SECTORS ( size ) < < SECTOR_SHIFT ;
2007-10-24 04:51:05 +04:00
if ( _dev_has_md_magic ( dev , sb_offset ) )
goto out ;
2004-11-18 23:02:21 +03:00
2008-06-23 18:54:50 +04:00
minor = MD_MINOR_VERSION_MIN ;
2007-10-24 04:51:05 +04:00
/* Version 1, try v1.0 -> v1.2 */
do {
sb_offset = _v1_sb_offset ( size , minor ) ;
if ( _dev_has_md_magic ( dev , sb_offset ) )
goto out ;
2008-06-23 18:54:50 +04:00
} while ( + + minor < = MD_MINOR_VERSION_MAX ) ;
2007-10-24 04:51:05 +04:00
ret = 0 ;
2004-11-18 23:02:21 +03:00
2007-10-24 04:51:05 +04:00
out :
2004-11-18 23:02:21 +03:00
if ( ! dev_close ( dev ) )
stack ;
2013-11-06 18:09:29 +04:00
if ( ret & & offset_found )
* offset_found = sb_offset ;
2007-10-24 04:51:05 +04:00
2004-11-18 23:02:21 +03:00
return ret ;
}
2014-09-03 17:47:52 +04:00
int dev_is_md ( struct device * dev , uint64_t * offset_found )
{
/*
* If non - native device status source is selected , use it
* only if offset_found is not requested as this
* information is not in udev db .
*/
if ( ( dev - > ext . src = = DEV_EXT_NONE ) | | offset_found )
return _native_dev_is_md ( dev , offset_found ) ;
if ( dev - > ext . src = = DEV_EXT_UDEV )
return _udev_dev_is_md ( dev ) ;
log_error ( INTERNAL_ERROR " Missing hook for MD device recognition "
" using external device info source %s " , dev_ext_name ( dev ) ) ;
return - 1 ;
}
2009-07-06 23:04:24 +04:00
static int _md_sysfs_attribute_snprintf ( char * path , size_t size ,
2013-06-12 14:08:56 +04:00
struct dev_types * dt ,
2009-08-01 21:11:02 +04:00
struct device * blkdev ,
2009-07-06 23:04:24 +04:00
const char * attribute )
2008-09-19 09:19:09 +04:00
{
2013-06-12 13:38:48 +04:00
const char * sysfs_dir = dm_sysfs_dir ( ) ;
2008-09-19 09:19:09 +04:00
struct stat info ;
2009-08-01 21:11:02 +04:00
dev_t dev = blkdev - > dev ;
2009-07-06 23:04:24 +04:00
int ret = - 1 ;
2008-09-19 09:19:09 +04:00
2009-08-01 21:11:02 +04:00
if ( ! sysfs_dir | | ! * sysfs_dir )
2009-07-06 23:04:24 +04:00
return ret ;
2008-09-19 09:19:09 +04:00
2013-06-12 14:08:56 +04:00
if ( MAJOR ( dev ) = = dt - > blkext_major ) {
2009-08-19 19:34:33 +04:00
/* lookup parent MD device from blkext partition */
2013-06-12 16:33:28 +04:00
if ( ! dev_get_primary_dev ( dt , blkdev , & dev ) )
2009-08-19 19:34:33 +04:00
return ret ;
2009-08-01 21:11:02 +04:00
}
2008-09-19 09:19:09 +04:00
2013-06-12 14:08:56 +04:00
if ( MAJOR ( dev ) ! = dt - > md_major )
2009-08-19 19:34:33 +04:00
return ret ;
2009-07-20 22:33:16 +04:00
ret = dm_snprintf ( path , size , " %s/dev/block/%d:%d/md/%s " , sysfs_dir ,
2009-08-01 21:11:02 +04:00
( int ) MAJOR ( dev ) , ( int ) MINOR ( dev ) , attribute ) ;
2009-07-06 23:04:24 +04:00
if ( ret < 0 ) {
log_error ( " dm_snprintf md %s failed " , attribute ) ;
return ret ;
2008-09-19 09:19:09 +04:00
}
2009-08-01 21:14:52 +04:00
if ( stat ( path , & info ) = = - 1 ) {
if ( errno ! = ENOENT ) {
log_sys_error ( " stat " , path ) ;
return ret ;
}
2009-07-06 23:04:24 +04:00
/* old sysfs structure */
ret = dm_snprintf ( path , size , " %s/block/md%d/md/%s " ,
2009-08-01 21:11:02 +04:00
sysfs_dir , ( int ) MINOR ( dev ) , attribute ) ;
2009-07-06 23:04:24 +04:00
if ( ret < 0 ) {
log_error ( " dm_snprintf old md %s failed " , attribute ) ;
return ret ;
}
2008-09-19 09:19:09 +04:00
}
2009-07-06 23:04:24 +04:00
return ret ;
}
2013-06-12 14:08:56 +04:00
static int _md_sysfs_attribute_scanf ( struct dev_types * dt ,
2009-07-06 23:04:24 +04:00
struct device * dev ,
const char * attribute_name ,
const char * attribute_fmt ,
void * attribute_value )
{
2011-07-08 19:53:59 +04:00
char path [ PATH_MAX + 1 ] , buffer [ MD_MAX_SYSFS_SIZE ] ;
2009-07-06 23:04:24 +04:00
FILE * fp ;
int ret = 0 ;
2013-06-12 14:08:56 +04:00
if ( _md_sysfs_attribute_snprintf ( path , PATH_MAX , dt ,
dev , attribute_name ) < 0 )
2009-07-06 23:04:24 +04:00
return ret ;
2008-09-19 09:19:09 +04:00
if ( ! ( fp = fopen ( path , " r " ) ) ) {
log_sys_error ( " fopen " , path ) ;
2009-07-06 23:04:24 +04:00
return ret ;
2008-09-19 09:19:09 +04:00
}
if ( ! fgets ( buffer , sizeof ( buffer ) , fp ) ) {
log_sys_error ( " fgets " , path ) ;
goto out ;
}
2009-07-06 23:04:24 +04:00
if ( ( ret = sscanf ( buffer , attribute_fmt , attribute_value ) ) ! = 1 ) {
log_error ( " %s sysfs attr %s not in expected format: %s " ,
dev_name ( dev ) , attribute_name , buffer ) ;
2008-09-19 09:19:09 +04:00
goto out ;
}
out :
if ( fclose ( fp ) )
log_sys_error ( " fclose " , path ) ;
2009-07-06 23:04:24 +04:00
return ret ;
}
/*
* Retrieve chunk size from md device using sysfs .
*/
2017-10-18 17:57:46 +03:00
static unsigned long _dev_md_chunk_size ( struct dev_types * dt , struct device * dev )
2009-07-06 23:04:24 +04:00
{
const char * attribute = " chunk_size " ;
unsigned long chunk_size_bytes = 0UL ;
2013-06-12 14:08:56 +04:00
if ( _md_sysfs_attribute_scanf ( dt , dev , attribute ,
2009-07-06 23:04:24 +04:00
" %lu " , & chunk_size_bytes ) ! = 1 )
return 0 ;
log_very_verbose ( " Device %s %s is %lu bytes. " ,
dev_name ( dev ) , attribute , chunk_size_bytes ) ;
2008-10-03 18:22:18 +04:00
return chunk_size_bytes > > SECTOR_SHIFT ;
2008-09-19 09:19:09 +04:00
}
2009-07-06 23:04:24 +04:00
/*
* Retrieve level from md device using sysfs .
*/
2017-10-18 17:57:46 +03:00
static int _dev_md_level ( struct dev_types * dt , struct device * dev )
2009-07-06 23:04:24 +04:00
{
2011-07-08 19:53:59 +04:00
char level_string [ MD_MAX_SYSFS_SIZE ] ;
2009-07-06 23:04:24 +04:00
const char * attribute = " level " ;
int level = - 1 ;
2013-06-12 14:08:56 +04:00
if ( _md_sysfs_attribute_scanf ( dt , dev , attribute ,
2011-07-08 19:53:59 +04:00
" %s " , & level_string ) ! = 1 )
2009-07-06 23:04:24 +04:00
return - 1 ;
2011-07-08 19:53:59 +04:00
log_very_verbose ( " Device %s %s is %s. " ,
dev_name ( dev ) , attribute , level_string ) ;
/* We only care about raid - ignore linear/faulty/multipath etc. */
if ( sscanf ( level_string , " raid%d " , & level ) ! = 1 )
return - 1 ;
2009-07-06 23:04:24 +04:00
return level ;
}
/*
* Retrieve raid_disks from md device using sysfs .
*/
2017-10-18 17:57:46 +03:00
static int _dev_md_raid_disks ( struct dev_types * dt , struct device * dev )
2009-07-06 23:04:24 +04:00
{
const char * attribute = " raid_disks " ;
int raid_disks = 0 ;
2013-06-12 14:08:56 +04:00
if ( _md_sysfs_attribute_scanf ( dt , dev , attribute ,
2009-07-06 23:04:24 +04:00
" %d " , & raid_disks ) ! = 1 )
return 0 ;
log_very_verbose ( " Device %s %s is %d. " ,
dev_name ( dev ) , attribute , raid_disks ) ;
return raid_disks ;
}
/*
* Calculate stripe width of md device using its sysfs files .
*/
2013-06-12 14:08:56 +04:00
unsigned long dev_md_stripe_width ( struct dev_types * dt , struct device * dev )
2009-07-06 23:04:24 +04:00
{
unsigned long chunk_size_sectors = 0UL ;
unsigned long stripe_width_sectors = 0UL ;
int level , raid_disks , data_disks ;
2017-10-18 17:57:46 +03:00
chunk_size_sectors = _dev_md_chunk_size ( dt , dev ) ;
2009-07-06 23:04:24 +04:00
if ( ! chunk_size_sectors )
return 0 ;
2017-10-18 17:57:46 +03:00
level = _dev_md_level ( dt , dev ) ;
2009-07-06 23:04:24 +04:00
if ( level < 0 )
return 0 ;
2017-10-18 17:57:46 +03:00
raid_disks = _dev_md_raid_disks ( dt , dev ) ;
2009-07-06 23:04:24 +04:00
if ( ! raid_disks )
return 0 ;
/* The raid level governs the number of data disks. */
switch ( level ) {
case 0 :
/* striped md does not have any parity disks */
data_disks = raid_disks ;
break ;
case 1 :
case 10 :
/* mirrored md effectively has 1 data disk */
data_disks = 1 ;
break ;
case 4 :
case 5 :
/* both raid 4 and 5 have a single parity disk */
data_disks = raid_disks - 1 ;
break ;
case 6 :
/* raid 6 has 2 parity disks */
data_disks = raid_disks - 2 ;
break ;
default :
log_error ( " Device %s has an unknown md raid level: %d " ,
dev_name ( dev ) , level ) ;
return 0 ;
}
stripe_width_sectors = chunk_size_sectors * data_disks ;
log_very_verbose ( " Device %s stripe-width is %lu bytes. " ,
dev_name ( dev ) ,
stripe_width_sectors < < SECTOR_SHIFT ) ;
return stripe_width_sectors ;
}
2007-10-24 04:30:30 +04:00
# else
2010-07-09 19:34:40 +04:00
int dev_is_md ( struct device * dev __attribute__ ( ( unused ) ) ,
uint64_t * sb __attribute__ ( ( unused ) ) )
2007-10-24 04:30:30 +04:00
{
return 0 ;
}
2013-06-12 14:08:56 +04:00
unsigned long dev_md_stripe_width ( struct dev_types * dt __attribute__ ( ( unused ) ) ,
struct device * dev __attribute__ ( ( unused ) ) )
2008-09-19 09:19:09 +04:00
{
return 0UL ;
}
2007-10-24 04:30:30 +04:00
# endif