2002-01-14 13:00:56 +03:00
/*
* Copyright ( C ) 2002 Sistina Software ( UK ) Limited .
*
* This file is released under the LGPL .
*/
# include "lvm1_label.h"
# include "dbg_malloc.h"
# include "disk-rep.h"
# include "log.h"
# include "label.h"
2002-01-16 21:10:08 +03:00
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
2002-01-14 13:00:56 +03:00
static void _not_supported ( const char * op )
{
log_err ( " The '%s' operation is not supported for the lvm1 labeller. " ,
op ) ;
}
static int _can_handle ( struct labeller * l , struct device * dev )
{
2002-01-16 21:10:08 +03:00
struct pv_disk pvd ;
int r ;
2002-04-24 22:20:51 +04:00
if ( ! dev_open ( dev , O_RDONLY ) ) {
stack ;
return 0 ;
}
2002-01-16 21:10:08 +03:00
r = read_pvd ( dev , & pvd ) ;
2002-04-24 22:20:51 +04:00
if ( ! dev_close ( dev ) )
stack ;
2002-01-14 13:00:56 +03:00
2002-01-16 21:10:08 +03:00
return r ;
2002-01-14 13:00:56 +03:00
}
2002-04-24 22:20:51 +04:00
static int _write ( struct labeller * l , struct device * dev , struct label * label )
2002-01-14 13:00:56 +03:00
{
_not_supported ( " write " ) ;
return 0 ;
}
static int _remove ( struct labeller * l , struct device * dev )
{
_not_supported ( " remove " ) ;
return 0 ;
}
2002-01-16 21:10:08 +03:00
static struct label * _to_label ( struct pv_disk * pvd )
2002-01-14 13:00:56 +03:00
{
struct label * l ;
2002-01-15 13:24:48 +03:00
struct lvm_label_info * info ;
2002-01-14 13:00:56 +03:00
if ( ! ( l = dbg_malloc ( sizeof ( * l ) ) ) ) {
log_err ( " Couldn't allocate label. " ) ;
return NULL ;
}
2002-01-16 21:10:08 +03:00
if ( ! ( info = ( struct lvm_label_info * ) dbg_strdup ( pvd - > vg_name ) ) ) {
2002-01-15 13:24:48 +03:00
dbg_free ( l ) ;
return NULL ;
}
2002-01-16 21:10:08 +03:00
memcpy ( & l - > id , & pvd - > pv_uuid , sizeof ( l - > id ) ) ;
2002-01-15 13:24:48 +03:00
strcpy ( l - > volume_type , " lvm " ) ;
2002-01-14 13:00:56 +03:00
l - > version [ 0 ] = 1 ;
l - > version [ 0 ] = 0 ;
l - > version [ 0 ] = 0 ;
2002-01-15 13:24:48 +03:00
l - > extra_info = info ;
2002-01-14 13:00:56 +03:00
return l ;
}
2002-01-16 21:10:08 +03:00
static int _read ( struct labeller * l , struct device * dev , struct label * * label )
2002-01-14 13:00:56 +03:00
{
2002-01-16 21:10:08 +03:00
struct pv_disk pvd ;
2002-01-14 13:00:56 +03:00
int r = 0 ;
2002-04-24 22:20:51 +04:00
if ( ! dev_open ( dev , O_RDONLY ) ) {
stack ;
return 0 ;
}
2002-01-16 21:10:08 +03:00
r = read_pvd ( dev , & pvd ) ;
2002-04-24 22:20:51 +04:00
if ( ! dev_close ( dev ) )
stack ;
2002-01-16 21:10:08 +03:00
if ( ! r ) {
2002-01-16 16:09:26 +03:00
stack ;
2002-01-14 13:00:56 +03:00
return 0 ;
}
/*
* Convert the disk_list into a label structure .
*/
2002-01-16 21:10:08 +03:00
if ( ! ( * label = _to_label ( & pvd ) ) ) {
2002-01-14 13:00:56 +03:00
stack ;
2002-01-16 21:10:08 +03:00
return 0 ;
}
2002-01-14 13:00:56 +03:00
2002-01-16 21:10:08 +03:00
return 1 ;
2002-01-14 13:00:56 +03:00
}
2002-01-15 20:37:23 +03:00
static void _destroy_label ( struct labeller * l , struct label * label )
2002-01-15 13:24:48 +03:00
{
dbg_free ( label - > extra_info ) ;
dbg_free ( label ) ;
}
2002-01-14 13:00:56 +03:00
static void _destroy ( struct labeller * l )
{
2002-01-16 00:28:04 +03:00
dbg_free ( l ) ;
2002-01-14 13:00:56 +03:00
}
struct label_ops _lvm1_ops = {
2002-04-24 22:20:51 +04:00
can_handle : _can_handle ,
write : _write ,
remove : _remove ,
read : _read ,
verify : _can_handle ,
destroy_label : _destroy_label ,
destroy : _destroy
2002-01-14 13:00:56 +03:00
} ;
struct labeller * lvm1_labeller_create ( void )
{
struct labeller * l ;
if ( ! ( l = dbg_malloc ( sizeof ( * l ) ) ) ) {
log_err ( " Couldn't allocate labeller object. " ) ;
return NULL ;
}
l - > ops = & _lvm1_ops ;
2002-01-16 21:10:08 +03:00
l - > private = NULL ;
2002-01-14 13:00:56 +03:00
return l ;
}