diff --git a/lib/Makefile.in b/lib/Makefile.in index e5e3edb11..03f466a32 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -22,7 +22,8 @@ VPATH = @srcdir@ SOURCES=\ config/config.c \ - dev-mgr/dev-manager.c \ + datastruct/hash.c \ + dev-mgr/dev-cache.c \ device/device.c \ display/display.c \ display/metadata.c \ diff --git a/lib/datastruct/hash.c b/lib/datastruct/hash.c index 7c5ae5732..c750e3b7a 100644 --- a/lib/datastruct/hash.c +++ b/lib/datastruct/hash.c @@ -5,8 +5,8 @@ */ -#include "hash.h" #include "dbg_malloc.h" +#include "hash.h" #include "log.h" struct hash_node { @@ -68,7 +68,7 @@ static unsigned _hash(const char *str) return h; } -hash_table_t create_hash_table(unsigned size_hint) +struct hash_table *create_hash_table(unsigned size_hint) { size_t len; unsigned new_size = 16u; @@ -92,7 +92,7 @@ hash_table_t create_hash_table(unsigned size_hint) goto bad; } memset(hc->slots, 0, len); - return (hash_table_t) hc; + return (struct hash_table) hc; bad: dbg_free(hc->slots); diff --git a/lib/dev-mgr/dev-cache.h b/lib/dev-mgr/dev-cache.h index e0f5efee8..074bee0d4 100644 --- a/lib/dev-mgr/dev-cache.h +++ b/lib/dev-mgr/dev-cache.h @@ -7,6 +7,8 @@ #ifndef _LVM_DEV_CACHE_H #define _LVM_DEV_CACHE_H +#include + /* * All devices in LVM will be represented by one of these. * pointer comparisons are valid. @@ -17,7 +19,7 @@ struct device { }; struct dev_filter { - int (*passes_filter)(struct dev_cache_filter *f, struct device *dev); + int (*passes_filter)(struct dev_filter *f, struct device *dev); void *private; }; @@ -45,7 +47,7 @@ struct device *dev_iter_get(struct dev_iter *iter); * All io should use these routines, rather than opening the devices * by hand. You do not have to call an open routine. */ -uint64_t dev_get_size(struct device *dev); /* in 512 byte sectors */ +__uint64_t dev_get_size(struct device *dev); /* in 512 byte sectors */ ssize_t dev_read(struct device *dev, size_t offset, size_t len, void *buffer); ssize_t dev_write(struct device *dev, size_t offset, size_t len, void *buffer); diff --git a/lib/device/device.c b/lib/device/device.c index 061435183..87ad84c4c 100644 --- a/lib/device/device.c +++ b/lib/device/device.c @@ -30,7 +30,7 @@ #include "dbg_malloc.h" #include "log.h" -#include "dev-manager.h" +#include "dev-cache.h" #include "metadata.h" #include "device.h" diff --git a/lib/metadata/metadata.h b/lib/metadata/metadata.h index 33ed0411b..fedfca18f 100644 --- a/lib/metadata/metadata.h +++ b/lib/metadata/metadata.h @@ -3,19 +3,47 @@ * * This file is released under the GPL. * - * This is the in core representation of a volume group and it's + * This is the in core representation of a volume group and its * associated physical and logical volumes. */ #ifndef _LVM_METADATA_H #define _LVM_METADATA_H +#include #include "dev-cache.h" #define ID_LEN 32 + +/* Various flags */ +/* Note that the bits no longer necessarily correspond to LVM1 disk format */ + +#define STATUS_ACTIVE 0x01 /* PV VG LV */ + +#define STATUS_EXPORTED 0x02 /* VG */ +#define STATUS_EXTENDABLE 0x04 /* VG */ + +#define STATUS_ALLOCATED 0x02 /* PV */ + +#define STATUS_SPINDOWN 0x02 /* LV */ +#define STATUS_BADBLOCK_ON 0x04 /* LV */ +#define STATUS_ALLOC_STRICT 0x08 /* LV */ +#define STATUS_ALLOC_CONTIGUOUS 0x10 /* LV */ + +#define ACCESS_READ 0x01 /* LV VG */ +#define ACCESS_WRITE 0x02 /* LV VG */ + +#define ACCESS_SNAPSHOT 0x04 /* LV */ +#define ACCESS_SNAPSHOT_ORG 0x08 /* LV */ + +#define ACCESS_CLUSTERED 0x04 /* VG */ +#define ACCESS_SHARED 0x08 /* VG */ + + + struct id { - uint8_t chars[ID_LEN]; + __uint8_t chars[ID_LEN]; }; struct logical_volume; @@ -25,19 +53,19 @@ struct physical_volume { struct device *dev; char *vg_name; - uint32_t status; - uint64_t size; + __uint32_t status; + __uint64_t size; /* physical extents */ - uint64_t pe_size; - uint64_t pe_start; - uint32_t pe_count; - uint32_t pe_allocated; + __uint64_t pe_size; + __uint64_t pe_start; + __uint32_t pe_count; + __uint32_t pe_allocated; }; struct pe_specifier { struct physical_volume *pv; - uint32_t pe; + __uint32_t pe; }; struct logical_volume { @@ -45,12 +73,12 @@ struct logical_volume { struct id *id; char *name; - uint32_t access; - uint32_t status; - uint32_t open; + __uint32_t access; + __uint32_t status; + __uint32_t open; - uint64_t size; - uint32_t le_count; + __uint64_t size; + __uint32_t le_count; /* le -> pe mapping array */ struct pe_specifier *map; @@ -60,76 +88,62 @@ struct volume_group { struct id *id; char *name; - uint64_t extent_size; - uint32_t extent_count; - uint32_t free_count; + __uint64_t extent_size; + __uint32_t extent_count; + __uint32_t free_count; /* physical volumes */ - uint32_t pv_count; + __uint32_t pv_count; struct physical_volume **pv; /* logical volumes */ - uint32_t lv_count; + __uint32_t lv_count; struct logical_volume **lv; }; + /* ownership of returned objects passes */ struct io_space { struct str_list *(*get_vgs)(struct io_space *is); struct dev_list *(*get_pvs)(struct io_space *is); - struct physical_volume *pv_read(struct io_space *is, + struct physical_volume *(*pv_read)(struct io_space *is, struct device *dev); - int pv_write(struct io_space *is, struct physical_volume *pv); + int (*pv_write)(struct io_space *is, struct physical_volume *pv); struct volume_group *(*vg_read)(struct io_space *is, const char *vg_name); int (*vg_write)(struct io_space *is, struct volume_group *vg); - void (*destructor)(struct io_space *is); + void (*destroy)(struct io_space *is); struct dev_filter *filter; void *private; }; -struct io_space *create_text_format(struct device_manager *mgr, +/* FIXME: Move to other files */ +struct io_space *create_text_format(struct dev_filter *filter, const char *text_file); +struct io_space *create_lvm_v1_format(struct dev_filter *filter); -inline struct volume_group *read_vg(struct io_space *f) -{ - struct dev_list *pvs = f->get_pvs(); - return f->read_vg(pvs); -} - -inline int write_vg(struct io_object *f, struct volume_group *vg) -{ - return f->write_vg(vg); -} - - - -inline int write_backup(struct io_format *orig, struct io_format *text) +inline int write_backup(struct io_space *orig, struct io_space *text) { } - int id_eq(struct id *op1, struct id *op2); -struct volume_group *create_vg(); -int destroy_vg(struct volume_group *vg); +struct volume_group *vg_create(); +struct physical_volume *pv_create(); -int add_pv(struct volume_group *vg, struct physical_volume *pv); -struct physical_volume *find_pv(struct volume_group *vg, +int vg_destroy(struct volume_group *vg); + +int pv_add(struct volume_group *vg, struct physical_volume *pv); +struct physical_volume *pv_find(struct volume_group *vg, struct physical_volume *pv); -int add_lv(struct volume_group *vg, struct logical_volume *lv); -struct logical_volume *find_lv(struct volume_group *vg, +int lv_add(struct volume_group *vg, struct logical_volume *lv); +struct logical_volume *lv_find(struct volume_group *vg, struct logical_volume *lv); -struct io_handler { - struct volume_group *read_vg(); - int write_vg(struct volume_group *vg); -}; - #endif diff --git a/lib/mm/dbg_malloc.h b/lib/mm/dbg_malloc.h index 68de7168e..49ca4d4b5 100644 --- a/lib/mm/dbg_malloc.h +++ b/lib/mm/dbg_malloc.h @@ -21,6 +21,8 @@ #ifndef _LVM_DBG_MALLOC_H #define _LVM_DBG_MALLOC_H +#include + #ifdef DEBUG_MEM void *malloc_aux(size_t s, const char *file, int line); void free_aux(void *p); diff --git a/tools/Makefile.in b/tools/Makefile.in index 44604f572..7c51ca1a5 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -22,8 +22,9 @@ VPATH = @srcdir@ SOURCES=\ lvm.c\ - lvactivate.c\ - pvdisplay.c + pvcreate.c\ + pvdisplay.c\ + lvactivate.c TARGETS=\ lvm diff --git a/tools/lvactivate.c b/tools/lvactivate.c index 7ed3e98bb..30786ffea 100644 --- a/tools/lvactivate.c +++ b/tools/lvactivate.c @@ -24,38 +24,38 @@ int lvactivate(int argc, char **argv) { int p; - struct dev_mgr *dm; + struct io_space *ios; struct device *pv_dev; - char *lv; + char *lv_name; char *pv_name; - pv_t *pv = NULL; - lv_disk_t *lvs = NULL; + struct physical_volume *pv = NULL; + struct logical_volume *lv = NULL; if (argc < 2) { log_error("please enter logical volume & physical volume(s)"); return LVM_EINVALID_CMD_LINE; } - lv = argv[0]; + lv_name = argv[0]; argc--; argv++; - dm = active_dev_mgr(); + ios = active_ios(); while (argc--) { pv_name = argv[argc]; - if (!(pv_dev = dev_by_name(dm, pv_name))) { + if (!(pv_dev = dev_cache_get(pv_name))) { log_error("device \"%s\" not found", pv_name); return -1; } - if (!(pv = pv_read(dm, pv_name))) { + if (!(pv = pv_read(ios, pv_dev))) { return -1; } - if (pv->pe_allocated) { + if (pv->status & STATUS_ALLOCATED) { if (!(pv->pe = pv_read_pe(pv_name, pv))) goto pvdisplay_device_out; if (!(lvs = pv_read_lvs(pv))) { diff --git a/tools/lvm.c b/tools/lvm.c index e52d77d41..13cdc7149 100644 --- a/tools/lvm.c +++ b/tools/lvm.c @@ -59,6 +59,7 @@ static int _num_commands; static struct command *_commands; static struct dev_filter *_filter; +static struct io_space *_ios; static struct config_file *_cf; static int _interactive; @@ -211,10 +212,10 @@ int string_arg(struct arg *a) int permission_arg(struct arg *a) { if ((!strcmp(a->value, "rw")) || (!strcmp(a->value, "wr"))) - a->i_value = LV_READ | LV_WRITE; + a->i_value = ACCESS_READ | ACCESS_WRITE; else if (!strcmp(a->value, "r")) - a->i_value = LV_READ; + a->i_value = ACCESS_READ; else return 0; @@ -447,8 +448,8 @@ static int process_common_commands(struct command *com) } if (arg_count(version_ARG)) { - /* FIXME: Add driver version */ - log_error("%s: %s", com->name, lvm_version); + /* FIXME: Add driver and software version */ + log_error("%s: ", com->name); return LVM_ECMD_PROCESSED; } @@ -548,6 +549,14 @@ struct config_file *active_config_file(void) { return _cf; } +struct dev_filter *active_filter(void) { + return _filter; +} + +struct io_space *active_ios(void) { + return _ios; +} + static void __init_log(struct config_file *cf) { const char *log_file = find_config_str(cf->root, "log/file", '/', 0); @@ -601,6 +610,10 @@ static int init(void) goto out; } + if (!(_ios = create_lvm_v1_format(_filter))) { + goto out; + } + ret = 1; out: @@ -619,6 +632,7 @@ static void __fin_commands(void) static void fin(void) { + _ios->destroy(_ios); config_filter_destroy(_filter); dev_cache_exit(); destroy_config_file(_cf); diff --git a/tools/pvcreate.c b/tools/pvcreate.c index b38073e35..810e1bfe9 100644 --- a/tools/pvcreate.c +++ b/tools/pvcreate.c @@ -45,42 +45,36 @@ int pvcreate(int argc, char **argv) void pvcreate_single(const char *pv_name) { int size; - pv_t *pv = NULL; - pv_t *pv_new; + struct physical_volume *pv = NULL; - struct dev_mgr *dm; + struct io_space *ios; struct device *pv_dev; - dm = active_dev_mgr(); + ios = active_ios(); - if (!(pv_dev = dev_by_name(dm, pv_name))) { + if (!(pv_dev = dev_cache_get(pv_name))) { log_error("Device %s not found", pv_name); return; } - if ((size = device_get_size(pv_name)) < 0) { + if ((size = dev_get_size(pv_dev)) < 0) { log_error("Unable to get size of %s", pv_name); return; } - if (arg_count(force_ARG) < 1 && !partition_type_is_lvm(dm, pv_dev)) { + if (arg_count(force_ARG) < 1 && !partition_type_is_lvm(ios, pv_dev)) { return; } - if (!(pv = pv_read(dm, pv_name))) { + pv = ios->pv_read(ios, pv_dev); + + if (pv && (pv->status & STATUS_EXPORTED)) { + log_error ("Physical volume %s belongs to exported volume" + " group %s", pv_name, pv->vg_name); return; } -/**** - FIXME: Check attributes - - EXPORTED - pv->vg_name[strlen(pv->vg_name) - strlen(EXPORTED)] = 0; - log_error ("physical volume \"%s\" belongs to exported volume group \"%s\"", - pv_name, pv->vg_name); -*****/ - - if (pv->vg_name[0]) { + if (pv && pv->vg_name[0]) { if (arg_count(force_ARG) < 2) { log_error("Can't initialize physical volume %s of " "volume group %s without -ff", pv_name, @@ -99,18 +93,20 @@ void pvcreate_single(const char *pv_name) } } -/*** - if (pv) { - if (pv_check_active((char *) pv->vg_name, (char *) pv_name) == - TRUE) { - log_error - ("can't force create on active physical volume \"%s\"", - pv_name); + if (pv && (pv->status & STATUS_ACTIVE)) { + log_error("Can't create on active physical volume %s", + pv_name); + return; + } + + + if (!pv) { + if (!(pv = pv_create())) return; - } - -***/ + /* FIXME: Set up initial size & PEs here */ + } + if (arg_count(force_ARG)) { /* FIXME: Change to log_print */ @@ -127,20 +123,13 @@ void pvcreate_single(const char *pv_name) log_verbose("setting up physical volume for %s with %u sectors", pv_name, size); -/*** FIXME: New metadata creation fn reqd - if (!(pv_new = pv_setup_for_create(pv_name, size)) < 0) { - log_error("Failed to set up physical volume %s", pv_name); - return; - } -***/ log_verbose("writing physical volume data to disk %s", pv_name); -/*** FIXME: New metadata write fn reqd - if (!(pv_write(pv_name, pv_new)) == 0) { + if (!(pv_write(ios, pv))) { log_error("Failed to create physical volume %s", pv_name); + return; } -***/ printf("physical volume %s successfully created\n", pv_name);