2001-09-13 22:30:05 +04:00
/*
* dm - linear . c
*
2001-09-26 18:32:07 +04:00
* Copyright ( C ) 2001 Sistina Software ( UK ) Limited .
2001-09-13 22:30:05 +04:00
*
2001-09-26 18:32:07 +04:00
* This file is released under the GPL .
2001-09-13 22:30:05 +04:00
*/
# include <linux/config.h>
# include <linux/module.h>
# include <linux/init.h>
# include <linux/slab.h>
# include <linux/fs.h>
2001-09-14 13:45:35 +04:00
# include <linux/blkdev.h>
2001-09-13 22:30:05 +04:00
# include <linux/device-mapper.h>
2001-09-14 13:45:35 +04:00
# include "dm.h"
2001-09-13 22:30:05 +04:00
/*
* linear : maps a linear range of a device .
*/
struct linear_c {
long delta ; /* FIXME: we need a signed offset type */
2001-09-26 18:32:07 +04:00
struct dm_dev * dev ;
2001-09-13 22:30:05 +04:00
} ;
/*
* construct a linear mapping .
* < dev_path > < offset >
*/
2001-09-26 18:32:07 +04:00
static int linear_ctr ( struct dm_table * t , offset_t b , offset_t l ,
2001-10-17 15:34:50 +04:00
char * args , void * * context )
2001-09-13 22:30:05 +04:00
{
struct linear_c * lc ;
unsigned int start ;
2001-09-26 18:32:07 +04:00
int r = - EINVAL ;
2001-10-17 15:34:50 +04:00
char * tok ;
char * path ;
char * p = args ;
2001-09-13 22:30:05 +04:00
2001-10-17 15:34:50 +04:00
* context = " No device path given " ;
path = next_token ( & p ) ;
if ( ! path )
2001-09-26 18:32:07 +04:00
goto bad ;
2001-10-17 15:34:50 +04:00
* context = " No initial offset given " ;
tok = next_token ( & p ) ;
if ( ! tok )
2001-09-26 18:32:07 +04:00
goto bad ;
2001-10-17 15:34:50 +04:00
start = simple_strtoul ( tok , NULL , 10 ) ;
2001-09-13 22:30:05 +04:00
2001-10-17 15:34:50 +04:00
* context = " Cannot allocate linear context private structure " ;
lc = kmalloc ( sizeof ( lc ) , GFP_KERNEL ) ;
if ( lc = = NULL )
2001-09-26 18:32:07 +04:00
goto bad ;
2001-10-17 15:34:50 +04:00
* context = " Cannot get target device " ;
r = dm_table_get_device ( t , path , & lc - > dev ) ;
if ( r )
goto bad_free ;
2001-09-13 22:30:05 +04:00
lc - > delta = ( int ) start - ( int ) b ;
2001-09-26 18:32:07 +04:00
* context = lc ;
return 0 ;
2001-09-13 22:30:05 +04:00
2001-10-17 15:34:50 +04:00
bad_free :
2001-09-26 18:32:07 +04:00
kfree ( lc ) ;
2001-10-17 15:34:50 +04:00
bad :
2001-09-26 18:32:07 +04:00
return r ;
2001-09-13 22:30:05 +04:00
}
static void linear_dtr ( struct dm_table * t , void * c )
{
struct linear_c * lc = ( struct linear_c * ) c ;
2001-09-26 23:48:20 +04:00
dm_table_put_device ( t , lc - > dev ) ;
2001-09-13 22:30:05 +04:00
kfree ( c ) ;
}
2001-09-14 20:22:02 +04:00
static int linear_map ( struct buffer_head * bh , int rw , void * context )
2001-09-13 22:30:05 +04:00
{
struct linear_c * lc = ( struct linear_c * ) context ;
2001-09-26 23:48:20 +04:00
bh - > b_rdev = lc - > dev - > dev ;
2001-09-13 22:30:05 +04:00
bh - > b_rsector = bh - > b_rsector + lc - > delta ;
return 1 ;
}
2001-10-17 15:34:50 +04:00
/*
* Debugging use only .
*/
static char * linear_print ( void * context )
{
struct linear_c * lc = ( struct linear_c * ) context ;
static char buf [ 256 ] ;
sprintf ( buf , " %lu " , lc - > delta ) ;
return buf ;
}
2001-09-13 22:30:05 +04:00
static struct target_type linear_target = {
name : " linear " ,
2001-09-14 00:10:14 +04:00
module : THIS_MODULE ,
2001-09-13 22:30:05 +04:00
ctr : linear_ctr ,
dtr : linear_dtr ,
map : linear_map ,
2001-10-17 15:34:50 +04:00
print : linear_print ,
2001-09-13 22:30:05 +04:00
} ;
static int __init linear_init ( void )
{
2001-09-26 18:32:07 +04:00
int r = dm_register_target ( & linear_target ) ;
2001-09-13 22:30:05 +04:00
2001-09-26 18:32:07 +04:00
if ( r < 0 )
printk ( KERN_ERR
" Device mapper: Linear: register failed %d \n " , r ) ;
2001-09-13 22:30:05 +04:00
2001-09-26 18:32:07 +04:00
return r ;
2001-09-13 22:30:05 +04:00
}
static void __exit linear_exit ( void )
{
2001-09-26 18:32:07 +04:00
int r = dm_unregister_target ( & linear_target ) ;
2001-09-13 22:30:05 +04:00
2001-09-26 18:32:07 +04:00
if ( r < 0 )
printk ( KERN_ERR
" Device mapper: Linear: unregister failed %d \n " , r ) ;
2001-09-13 22:30:05 +04:00
}
module_init ( linear_init ) ;
module_exit ( linear_exit ) ;
MODULE_AUTHOR ( " Joe Thornber <thornber@uk.sistina.com> " ) ;
MODULE_DESCRIPTION ( " Device Mapper: Linear mapping " ) ;
2001-09-14 13:45:35 +04:00
MODULE_LICENSE ( " GPL " ) ;
2001-09-13 22:30:05 +04:00