2005-04-17 02:20:36 +04:00
/*
* Copyright ( C ) 2001 - 2003 Sistina Software ( UK ) Limited .
*
* This file is released under the GPL .
*/
# include "dm.h"
# include <linux/module.h>
# include <linux/init.h>
# include <linux/blkdev.h>
# include <linux/bio.h>
2017-04-12 23:37:44 +03:00
# include <linux/dax.h>
2005-04-17 02:20:36 +04:00
# include <linux/slab.h>
2008-10-21 20:44:59 +04:00
# include <linux/device-mapper.h>
2005-04-17 02:20:36 +04:00
2006-06-26 11:27:35 +04:00
# define DM_MSG_PREFIX "linear"
2005-04-17 02:20:36 +04:00
/*
* Linear : maps a linear range of a device .
*/
struct linear_c {
struct dm_dev * dev ;
sector_t start ;
} ;
/*
* Construct a linear mapping : < dev_path > < offset >
*/
static int linear_ctr ( struct dm_target * ti , unsigned int argc , char * * argv )
{
struct linear_c * lc ;
2006-03-27 13:17:48 +04:00
unsigned long long tmp ;
dm: reject trailing characters in sccanf input
Device mapper uses sscanf to convert arguments to numbers. The problem is that
the way we use it ignores additional unmatched characters in the scanned string.
For example, this `if (sscanf(string, "%d", &number) == 1)' will match a number,
but also it will match number with some garbage appended, like "123abc".
As a result, device mapper accepts garbage after some numbers. For example
the command `dmsetup create vg1-new --table "0 16384 linear 254:1bla 34816bla"'
will pass without an error.
This patch fixes all sscanf uses in device mapper. It appends "%c" with
a pointer to a dummy character variable to every sscanf statement.
The construct `if (sscanf(string, "%d%c", &number, &dummy) == 1)' succeeds
only if string is a null-terminated number (optionally preceded by some
whitespace characters). If there is some character appended after the number,
sscanf matches "%c", writes the character to the dummy variable and returns 2.
We check the return value for 1 and consequently reject numbers with some
garbage appended.
Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Acked-by: Mike Snitzer <snitzer@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
2012-03-28 21:41:26 +04:00
char dummy ;
2015-07-31 16:20:36 +03:00
int ret ;
2005-04-17 02:20:36 +04:00
if ( argc ! = 2 ) {
2006-06-26 11:27:35 +04:00
ti - > error = " Invalid argument count " ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
lc = kmalloc ( sizeof ( * lc ) , GFP_KERNEL ) ;
if ( lc = = NULL ) {
2015-10-27 22:38:58 +03:00
ti - > error = " Cannot allocate linear context " ;
2005-04-17 02:20:36 +04:00
return - ENOMEM ;
}
2015-07-31 16:20:36 +03:00
ret = - EINVAL ;
2018-11-08 00:24:55 +03:00
if ( sscanf ( argv [ 1 ] , " %llu%c " , & tmp , & dummy ) ! = 1 | | tmp ! = ( sector_t ) tmp ) {
2015-10-27 22:38:58 +03:00
ti - > error = " Invalid device sector " ;
2005-04-17 02:20:36 +04:00
goto bad ;
}
2006-03-27 13:17:48 +04:00
lc - > start = tmp ;
2005-04-17 02:20:36 +04:00
2015-07-31 16:20:36 +03:00
ret = dm_get_device ( ti , argv [ 0 ] , dm_table_get_mode ( ti - > table ) , & lc - > dev ) ;
if ( ret ) {
2015-10-27 22:38:58 +03:00
ti - > error = " Device lookup failed " ;
2005-04-17 02:20:36 +04:00
goto bad ;
}
2013-03-02 02:45:47 +04:00
ti - > num_flush_bios = 1 ;
ti - > num_discard_bios = 1 ;
2018-03-13 12:23:45 +03:00
ti - > num_secure_erase_bios = 1 ;
2013-03-02 02:45:47 +04:00
ti - > num_write_same_bios = 1 ;
2017-04-05 20:21:05 +03:00
ti - > num_write_zeroes_bios = 1 ;
2005-04-17 02:20:36 +04:00
ti - > private = lc ;
return 0 ;
bad :
kfree ( lc ) ;
2015-07-31 16:20:36 +03:00
return ret ;
2005-04-17 02:20:36 +04:00
}
static void linear_dtr ( struct dm_target * ti )
{
struct linear_c * lc = ( struct linear_c * ) ti - > private ;
dm_put_device ( ti , lc - > dev ) ;
kfree ( lc ) ;
}
2008-07-21 15:00:38 +04:00
static sector_t linear_map_sector ( struct dm_target * ti , sector_t bi_sector )
2005-04-17 02:20:36 +04:00
{
2008-07-21 15:00:38 +04:00
struct linear_c * lc = ti - > private ;
2010-08-12 07:14:11 +04:00
return lc - > start + dm_target_offset ( ti , bi_sector ) ;
2008-07-21 15:00:38 +04:00
}
static void linear_map_bio ( struct dm_target * ti , struct bio * bio )
{
struct linear_c * lc = ti - > private ;
2005-04-17 02:20:36 +04:00
2017-08-23 20:10:32 +03:00
bio_set_dev ( bio , lc - > dev - > bdev ) ;
2019-10-27 17:05:48 +03:00
if ( bio_sectors ( bio ) | | op_is_zone_mgmt ( bio_op ( bio ) ) )
2013-10-12 02:44:27 +04:00
bio - > bi_iter . bi_sector =
linear_map_sector ( ti , bio - > bi_iter . bi_sector ) ;
2008-07-21 15:00:38 +04:00
}
2012-12-22 00:23:41 +04:00
static int linear_map ( struct dm_target * ti , struct bio * bio )
2008-07-21 15:00:38 +04:00
{
linear_map_bio ( ti , bio ) ;
2005-04-17 02:20:36 +04:00
2006-12-08 13:41:06 +03:00
return DM_MAPIO_REMAPPED ;
2005-04-17 02:20:36 +04:00
}
2013-03-02 02:45:44 +04:00
static void linear_status ( struct dm_target * ti , status_type_t type ,
unsigned status_flags , char * result , unsigned maxlen )
2005-04-17 02:20:36 +04:00
{
struct linear_c * lc = ( struct linear_c * ) ti - > private ;
2021-07-13 03:49:03 +03:00
size_t sz = 0 ;
2005-04-17 02:20:36 +04:00
switch ( type ) {
case STATUSTYPE_INFO :
result [ 0 ] = ' \0 ' ;
break ;
case STATUSTYPE_TABLE :
2021-07-13 03:49:03 +03:00
DMEMIT ( " %s %llu " , lc - > dev - > name , ( unsigned long long ) lc - > start ) ;
break ;
case STATUSTYPE_IMA :
DMEMIT_TARGET_NAME_VERSION ( ti - > type ) ;
DMEMIT ( " ,device_name=%s,start=%llu; " , lc - > dev - > name ,
( unsigned long long ) lc - > start ) ;
2005-04-17 02:20:36 +04:00
break ;
}
}
2018-04-03 23:54:10 +03:00
static int linear_prepare_ioctl ( struct dm_target * ti , struct block_device * * bdev )
2006-10-03 12:15:18 +04:00
{
struct linear_c * lc = ( struct linear_c * ) ti - > private ;
2012-01-12 19:01:29 +04:00
struct dm_dev * dev = lc - > dev ;
2015-10-15 15:10:50 +03:00
* bdev = dev - > bdev ;
2012-01-12 19:01:29 +04:00
/*
* Only pass ioctls through if the device sizes match exactly .
*/
2021-10-18 13:11:05 +03:00
if ( lc - > start | | ti - > len ! = bdev_nr_sectors ( dev - > bdev ) )
2015-10-15 15:10:50 +03:00
return 1 ;
return 0 ;
2006-10-03 12:15:18 +04:00
}
2018-10-12 13:08:49 +03:00
# ifdef CONFIG_BLK_DEV_ZONED
2019-11-11 05:39:30 +03:00
static int linear_report_zones ( struct dm_target * ti ,
struct dm_report_zones_args * args , unsigned int nr_zones )
2018-10-12 13:08:49 +03:00
{
2019-11-11 05:39:30 +03:00
struct linear_c * lc = ti - > private ;
2018-10-12 13:08:49 +03:00
2021-05-26 00:24:57 +03:00
return dm_report_zones ( lc - > dev - > bdev , lc - > start ,
linear_map_sector ( ti , args - > next_sector ) ,
args , nr_zones ) ;
2018-10-12 13:08:49 +03:00
}
2021-02-11 01:38:30 +03:00
# else
# define linear_report_zones NULL
2018-10-12 13:08:49 +03:00
# endif
2009-06-22 13:12:33 +04:00
static int linear_iterate_devices ( struct dm_target * ti ,
iterate_devices_callout_fn fn , void * data )
{
struct linear_c * lc = ti - > private ;
2009-07-23 23:30:42 +04:00
return fn ( ti , lc - > dev , lc - > start , ti - > len , data ) ;
2009-06-22 13:12:33 +04:00
}
2021-11-29 13:21:36 +03:00
# if IS_ENABLED(CONFIG_FS_DAX)
2021-11-29 13:21:43 +03:00
static struct dax_device * linear_dax_pgoff ( struct dm_target * ti , pgoff_t * pgoff )
{
struct linear_c * lc = ti - > private ;
sector_t sector = linear_map_sector ( ti , * pgoff < < PAGE_SECTORS_SHIFT ) ;
* pgoff = ( get_start_sect ( lc - > dev - > bdev ) + sector ) > > PAGE_SECTORS_SHIFT ;
return lc - > dev - > dax_dev ;
}
2017-04-12 23:37:44 +03:00
static long linear_dax_direct_access ( struct dm_target * ti , pgoff_t pgoff ,
long nr_pages , void * * kaddr , pfn_t * pfn )
2016-06-23 02:54:54 +03:00
{
2021-11-29 13:21:43 +03:00
struct dax_device * dax_dev = linear_dax_pgoff ( ti , & pgoff ) ;
2017-04-12 23:37:44 +03:00
return dax_direct_access ( dax_dev , pgoff , nr_pages , kaddr , pfn ) ;
2016-06-23 02:54:54 +03:00
}
2020-02-28 19:34:54 +03:00
static int linear_dax_zero_page_range ( struct dm_target * ti , pgoff_t pgoff ,
size_t nr_pages )
{
2021-11-29 13:21:43 +03:00
struct dax_device * dax_dev = linear_dax_pgoff ( ti , & pgoff ) ;
2020-02-28 19:34:54 +03:00
return dax_zero_page_range ( dax_dev , pgoff , nr_pages ) ;
}
2018-03-30 03:22:13 +03:00
# else
# define linear_dax_direct_access NULL
2020-02-28 19:34:54 +03:00
# define linear_dax_zero_page_range NULL
2018-03-30 03:22:13 +03:00
# endif
2005-04-17 02:20:36 +04:00
static struct target_type linear_target = {
. name = " linear " ,
2017-05-09 02:40:50 +03:00
. version = { 1 , 4 , 0 } ,
2020-09-23 23:06:52 +03:00
. features = DM_TARGET_PASSES_INTEGRITY | DM_TARGET_NOWAIT |
2021-02-01 08:10:19 +03:00
DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO ,
2018-10-12 13:08:49 +03:00
. report_zones = linear_report_zones ,
2005-04-17 02:20:36 +04:00
. module = THIS_MODULE ,
. ctr = linear_ctr ,
. dtr = linear_dtr ,
. map = linear_map ,
. status = linear_status ,
2015-10-15 15:10:50 +03:00
. prepare_ioctl = linear_prepare_ioctl ,
2009-06-22 13:12:33 +04:00
. iterate_devices = linear_iterate_devices ,
2017-04-12 23:37:44 +03:00
. direct_access = linear_dax_direct_access ,
2020-02-28 19:34:54 +03:00
. dax_zero_page_range = linear_dax_zero_page_range ,
2005-04-17 02:20:36 +04:00
} ;
int __init dm_linear_init ( void )
{
int r = dm_register_target ( & linear_target ) ;
if ( r < 0 )
2006-06-26 11:27:35 +04:00
DMERR ( " register failed %d " , r ) ;
2005-04-17 02:20:36 +04:00
return r ;
}
void dm_linear_exit ( void )
{
2009-01-06 06:04:58 +03:00
dm_unregister_target ( & linear_target ) ;
2005-04-17 02:20:36 +04:00
}