take2: liblvm initialization, attribute, and object handling
cover take2: liblvm initialization, attribute, and object handling
This is an updated set of liblvm patches. Changes since the last time:
- lvm_vg_open(): added write mode with an attempt at allowing vgid as well as vgnames. This clearly does not work in all cases currently but is a start.
- lvm_vg_get_attr_list(): moved into lvm_vg_open() - does make sense and solves the memory problem with the report handle
- adds struct lib_context, containing struct cmd_context as well as errno.
- lvm_errno() and lvm_strerrno()
PATCH 1/21 Add lvm_create, lvm_destroy, lvm_reload_config() APIs.
Add lvm2.c file.
Update test code.
Call init_locking() with locking_type = -1 to read config file value.
--- lib/Makefile.in | 3 +- lib/lvm2.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/lvm2.h | 44 +++++++++++++++++++++++-------------- test/api/test.c | 13 +++++------ 4 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 lib/lvm2.c
lib/Makefile.in +2 -1
@@ -86,7 +86,8 @@ SOURCES =\ report/report.c \ striped/striped.c \ uuid/uuid.c \ - zero/zero.c + zero/zero.c \ + lvm2.c ifeq ("@LVM1@", "internal") SOURCES +=\
lib/lvm2.c new file +63 -0
63 lines not on this page · raw message
lib/lvm2.h +27 -17
63 lines not on this page · raw message
test/api/test.c +6 -7
@@ -48,7 +48,7 @@ static int lvm_split(char *str, int *argc, char **argv, int max)return *argc; } -static int lvmapi_test_shell (void *h) +static int lvmapi_test_shell (lvm_handle_t libh) {int argc, i;char *input =NULL , *args[MAX_ARGS ], **argv;
@@ -99,18 +99,17 @@ static int lvmapi_test_shell(void *h)int main (int argc,char *argv[]) { -void *h; +lvm_handle_t libh; - h =lvm2_create (); -if (!h) { + libh =lvm_create (NULL ); +if (!libh) {printf ("Unable to open lvm library instance\n" );return 1 ; } -lvmapi_test_shell (h); +lvmapi_test_shell (libh); -if (h) -lvm2_destroy (h); +lvm_destroy (libh);return 0 ; }
Signature · 1 line
1.6.0.5
PATCH 2/21 Move vg_t, lv_t, and pv_t from metadata-exported.h into lvm2.h
--- lib/lvm2.h | 9 +++++++++ lib/metadata/metadata-exported.h | 8 +------- 2 files changed, 10 insertions(+), 7 deletions(-)
lib/lvm2.h +9 -0
@@ -60,4 +60,13 @@ void lvm_destroy(lvm_handle_t libh);*/ int lvm_reload_config (lvm_handle_t libh); +struct volume_group ; +typedef struct volume_group vg_t ; + +struct physical_volume ; +typedef struct physical_volume pv_t ; + +struct logical_volume ; +typedef struct logical_volume lv_t ; +#endif
lib/metadata/metadata-exported.h +1 -7
@@ -22,13 +22,7 @@#define _LVM_METADATA_EXPORTED_H#include "uuid.h" - -struct physical_volume ; -typedef struct physical_volume pv_t ; -struct volume_group ; -typedef struct volume_group vg_t ; - -struct logical_volume ; +#include "lvm2.h" struct lv_segment ;struct pv_segment ;
Signature · 1 line
1.6.0.5
PATCH 3/21 Add lvm_pv_name, lvm_vg_name, and lvm_lv_name accessors.
RFC - liblvm could provide accessor functions such as these along with the generic attribute functions. These specific accessor functions will make application code simpler and more readable and may be desired by application programmers over the generic attribute functions for single attributes.
--- lib/lvm2.h | 7 +++++++ lib/metadata/metadata.c | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 0 deletions(-)
lib/lvm2.h +7 -0
@@ -69,4 +69,11 @@ typedef struct physical_volume pv_t;struct logical_volume ;typedef struct logical_volume lv_t ; +/* +* PV, VG, and LV object attribute functions. +*/ +const char *lvm_pv_name (const pv_t *pv); +const char *lvm_vg_name (const vg_t *vg); +const char *lvm_lv_name (const lv_t *lv); +#endif
lib/metadata/metadata.c +15 -0
@@ -2673,6 +2673,21 @@ vg_t *vg_read_for_update(struct cmd_context *cmd, const char *vg_name,return vg_read (cmd, vg_name, vgid, flags |READ_FOR_UPDATE ); } +const char *lvm_pv_name (const pv_t *pv) +{ +return dev_name (pv->dev); +} + +const char *lvm_vg_name (const vg_t *vg) +{ +return vg->name; +} + +const char *lvm_lv_name (const lv_t *lv) +{ +return lv->name; +} +/* * Test the validity of a VG handle returned by vg_read() or vg_read_for_update(). *
Signature · 1 line
1.6.0.5
PATCH 4/21 Add lvm_vg_open().
RFC - lvm_vg_open(). The new vg_read() function is a step in the right direction but still contains a lot of new options / flags that will be new to people and we can hopefully simplify. lvm_vg_open() was modelled after 'open' which gives us a potential model that others are familiar with. Can we use the modes and flags of 'open' in a similar way or do we need to introduce new flags/concepts to people? For example, can we use a some of the open flags such as O_CREAT and O_EXCL to implement READ_CHECK_EXISTENCE?
--- lib/lvm2.h | 2 ++ lib/metadata/metadata.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 0 deletions(-)
lib/lvm2.h +2 -0
@@ -16,6 +16,7 @@#define _LIB_LVM2_H#include <stdint.h> +#include <fcntl.h> /* * lvm_handle_t
@@ -76,4 +77,5 @@ const char *lvm_pv_name(const pv_t *pv);const char *lvm_vg_name (const vg_t *vg);const char *lvm_lv_name (const lv_t *lv); +vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode);#endif
lib/metadata/metadata.c +14 -0
@@ -2472,6 +2472,20 @@ vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name, }/* +* Open / read a VG. +* FIXME: Only read access allowed. +*/ +vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode) +{ +if ((mode &O_ACCMODE ) !=O_RDONLY ) { +log_error ("Invalid access mode 0x%x for lvm_vg_read()\n" , + mode); +return NULL ; + } +return vg_read ((struct cmd_context *)libh, vg_name,NULL ,0 ); +} + +/* * Create a (vg_t) volume group handle from a struct volume_group pointer and a * possible failure code or zero for success. */
Signature · 1 line
1.6.0.5
PATCH 5/21 Add lvm_vg_close().
For now we cannot free the handle but just check to see if we should unlock it.
--- lib/lvm2.h | 1 + lib/metadata/metadata.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 0 deletions(-)
lib/lvm2.h +1 -0
@@ -78,4 +78,5 @@ const char *lvm_vg_name(const vg_t *vg);const char *lvm_lv_name (const lv_t *lv);vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode); +void lvm_vg_close (vg_t *vg);#endif
lib/metadata/metadata.c +11 -0
@@ -2486,6 +2486,17 @@ vg_t *lvm_vg_open(lvm_handle_t libh, const char *vg_name, mode_t mode) }/* +* Close a VG handle. +* If lock is still held, unlock the VG. +* FIXME: We cannot free the memory as it's tied to the cmd->mem pool. +*/ +void lvm_vg_close (vg_t *vg) +{ +if (vgname_is_locked (vg->name)) +unlock_vg (vg->cmd, vg->name); +} + +/* * Create a (vg_t) volume group handle from a struct volume_group pointer and a * possible failure code or zero for success. */
Signature · 1 line
1.6.0.5
PATCH 6/21 Add lvm_vg_get_attr_list() libLVM API to return a list of VG attribute names.
This API returns a list of attribute names for a VG. A later API will allow the values of the attributes to be queried. The API implementation relies on adding an exported call to libdevmapper, dm_report_get_field_ids(), which is the interface into dm's reporting infrastructure to retrieve the attribute/field names/ids.
Add supporting libdm function, dm_report_get_field_ids.
--- lib/lvm2.h | 3 +++ lib/report/report.c | 28 ++++++++++++++++++++++++++++ libdm/.exported_symbols | 1 + libdm/libdevmapper.h | 12 ++++++++++++ libdm/libdm-report.c | 27 +++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 0 deletions(-)
lib/lvm2.h +3 -0
@@ -17,6 +17,7 @@#include <stdint.h> #include <fcntl.h> +#include "libdevmapper.h" /* * lvm_handle_t
@@ -77,6 +78,8 @@ const char *lvm_pv_name(const pv_t *pv);const char *lvm_vg_name (const vg_t *vg);const char *lvm_lv_name (const lv_t *lv); +int lvm_vg_get_attr_list (vg_t *vg,struct dm_list *list); +vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode);void lvm_vg_close (vg_t *vg);#endif
lib/report/report.c +28 -0
@@ -1189,3 +1189,31 @@ int report_object(void *handle, struct volume_group *vg,return dm_report_object (handle, &obj); } + +/* +* Get a list of attributes for a vg +*/ +int lvm_vg_get_attr_list (vg_t *vg,struct dm_list *list) +{ +void *rh; +report_type_t report_type =VGS ; + +dm_list_init (list); + +/* +* Create a report so we can return the headings. +*/ +if (!(rh =report_init (vg->cmd,"all" ,"" , &report_type, +" " ,1 ,1 ,1 ,0 ,0 ,0 ))) { + stack; +return 0 ; + } + +if (!dm_report_get_field_ids (rh, report_type, list)) { +dm_report_free (rh); +return 0 ; + } + +dm_report_free (rh); +return 1 ; +}
libdm/.exported_symbols +1 -0
@@ -132,6 +132,7 @@ dm_report_field_int32 dm_report_field_uint32 dm_report_field_uint64 dm_report_field_set_value +dm_report_get_field_ids dm_report_set_output_field_name_prefix dm_regex_create dm_regex_match
libdm/libdevmapper.h +12 -0
@@ -924,6 +924,17 @@ struct dm_report_field_type {const char *desc;/* description of the field */ }; +struct dm_report_field_ids_type { +struct dm_list list; +const char *id; +}; + +/* +* Returns a list of all field ids for a specific report type. +*/ +int dm_report_get_field_ids (struct dm_report *rh,uint32_t type, +struct dm_list *list); +/* * dm_report_init output_flags */
@@ -947,6 +958,7 @@ int dm_report_object(struct dm_report *rh, void *object);int dm_report_output (struct dm_report *rh);void dm_report_free (struct dm_report *rh); +/* * Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX */
libdm/libdm-report.c +27 -0
@@ -1076,3 +1076,30 @@ int dm_report_output(struct dm_report *rh)else return _output_as_columns (rh); } + +/* +* Return a list of field IDs (names) for a specified report type. +*/ +int dm_report_get_field_ids (struct dm_report *rh,uint32_t type, +struct dm_list *list) +{ +uint32_t i; +struct dm_report_field_ids_type *field_id; + +for (i =0 ; rh->fields[i].report_fn; i++) { +if (rh->fields[i].type != type) +continue ; + +if (!(field_id =dm_pool_zalloc (rh->mem,sizeof (*field_id)))) { +log_error ("dm_report_get_field_ids: struct " +"dm_report_field_ids_type allocation failed" ); +return 0 ; + } +if (!(field_id->id =dm_pool_strdup (rh->mem, rh->fields[i].id))) { +log_error ("dm_report_get_field_ids: dm_pool_strdup failed" ); +return 0 ; + } +dm_list_add (list, &field_id->list); + } +return 1 ; +}
Signature · 1 line
1.6.0.5
PATCH 7/21 Add lvm_vg_get_attr_value() libLVM API to query to value of a VG attribute.
This API may be used independently to query the value of a VG attribute, provided the name of the attribute is known. If the name of the attribute(s) are unknown, the previous API, vg_get_attr_list(), may be used.
--- lib/lvm2.h | 2 ++ lib/report/report.c | 28 ++++++++++++++++++++++++++++ libdm/.exported_symbols | 1 + libdm/libdevmapper.h | 12 ++++++++++++ libdm/libdm-report.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 0 deletions(-)
lib/lvm2.h +2 -0
@@ -79,6 +79,8 @@ const char *lvm_vg_name(const vg_t *vg);const char *lvm_lv_name (const lv_t *lv);int lvm_vg_get_attr_list (vg_t *vg,struct dm_list *list); +int lvm_vg_get_attr_value (vg_t *vg,const char *attr_name, +struct dm_report_field_value_type *value);vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode);void lvm_vg_close (vg_t *vg);
lib/report/report.c +28 -0
@@ -1217,3 +1217,31 @@ int lvm_vg_get_attr_list(vg_t *vg, struct dm_list *list)dm_report_free (rh);return 1 ; } + +/* +* Get the value of the VG attribute 'attr_name'. +*/ +int lvm_vg_get_attr_value (vg_t *vg,const char *attr_name, +struct dm_report_field_value_type *value) +{ +void *rh; +report_type_t report_type =VGS ; + +if (!(rh =report_init (vg->cmd, attr_name,"" , &report_type, +" " ,1 ,1 ,1 ,0 ,0 ,0 ))) { + stack; +return 0 ; + } + +if (!report_object (rh, vg,NULL ,NULL ,NULL ,NULL )) { + stack; +return 0 ; + } + +if (!dm_report_get_field_value (rh, value)) { + stack; +return 0 ; + } +dm_report_free (rh); +return 1 ; +}
libdm/.exported_symbols +1 -0
@@ -133,6 +133,7 @@ dm_report_field_uint32 dm_report_field_uint64 dm_report_field_set_value dm_report_get_field_ids +dm_report_get_field_value dm_report_set_output_field_name_prefix dm_regex_create dm_regex_match
libdm/libdevmapper.h +12 -0
@@ -927,6 +927,15 @@ struct dm_report_field_type {struct dm_report_field_ids_type {struct dm_list list;const char *id; +uint32_t type;/* FIXME: unnecessary ?*/ +}; + +struct dm_report_field_value_type { +unsigned is_string; +union { +char *s_val; +uint64_t n_val; + } u; };/*
@@ -935,6 +944,9 @@ struct dm_report_field_ids_type {int dm_report_get_field_ids (struct dm_report *rh,uint32_t type,struct dm_list *list); +int dm_report_get_field_value (struct dm_report *rh, +struct dm_report_field_value_type *value); +/* * dm_report_init output_flags */
libdm/libdm-report.c +29 -0
@@ -1099,7 +1099,36 @@ int dm_report_get_field_ids(struct dm_report *rh, uint32_t type,log_error ("dm_report_get_field_ids: dm_pool_strdup failed" );return 0 ; } + field_id->type = type;dm_list_add (list, &field_id->list); }return 1 ; } + + +int dm_report_get_field_value (struct dm_report *rh, +struct dm_report_field_value_type *value) +{ +struct dm_report_field *field; +struct row *row; + + dm_list_iterate_items(row, &rh->rows) { +if ((field =dm_list_item (dm_list_first (&row->fields), + struct dm_report_field))) { +if ((field->props->flags &DM_REPORT_FIELD_TYPE_MASK ) == +DM_REPORT_FIELD_TYPE_STRING ) { + value->is_string =1 ; + value->u.s_val =dm_pool_strdup (rh->mem, + field->sort_value); + }else { + value->is_string =0 ; + value->u.n_val = *(const uint64_t *)field->sort_value; + } +dm_list_del (&field->list); + } + + } + +return 1 ; + +}
Signature · 1 line
1.6.0.5
PATCH 8/21 Add lvm_lvs_in_vg() API.
Returns list of only user-displayable LVs.
Add exported lvm_lv_list structure along with the API. Note that this struct is very similar to struct lv_list.
TODO: merge struct lv_list and struct lvm_lv_list
--- lib/lvm2.h | 7 +++++++ lib/metadata/metadata.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 0 deletions(-)
lib/lvm2.h +7 -0
@@ -84,4 +84,11 @@ int lvm_vg_get_attr_value(vg_t *vg, const char *attr_name,vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode);void lvm_vg_close (vg_t *vg); + +struct lvm_lv_list { +struct dm_list list; +lv_t *lv; +}; +struct dm_list *lvm_lvs_in_vg (vg_t *vg); +#endif
lib/metadata/metadata.c +29 -0
@@ -2497,6 +2497,35 @@ void lvm_vg_close(vg_t *vg) }/* +* Returns a list of all top-level, user-displayable LVs in a VG. +*/ +struct dm_list *lvm_lvs_in_vg (vg_t *vg) +{ +struct dm_list *l; +struct lvm_lv_list *ll; +struct lv_list *lvl; +/* FIXME: lv_list should be replaced by lvm_lv_list */ +struct dm_pool *mem = vg->cmd->mem; + +/* FIXME: Return code for memory allocation failure */ +if (!(l =dm_pool_alloc (mem,sizeof (struct dm_list )))) { +log_error ("Error allocating LV list." ); +return NULL ; + } +dm_list_init (l); + dm_list_iterate_items(lvl, &vg->lvs) { +if (!lv_is_displayable (lvl->lv)) +continue ; +if (!(ll =dm_pool_alloc (mem,sizeof (*ll)))) +log_error ("Error allocating LV list member" ); + ll->lv = lvl->lv; +dm_list_add (l, &ll->list); + } + +return l; +} + +/* * Create a (vg_t) volume group handle from a struct volume_group pointer and a * possible failure code or zero for success. */
Signature · 1 line
1.6.0.5
PATCH 9/21 Add lvm_pvs_in_vg().
NOTE: struct lvm_pv_list should not be necessary but I did not like exposing struct pv_list and attempts to clean up struct pv_list to contain only struct dm_list and pv_t members were unsuccessful.
--- lib/lvm2.h | 10 ++++++++++ lib/metadata/metadata.c | 5 +++++ 2 files changed, 15 insertions(+), 0 deletions(-)
lib/lvm2.h +10 -0
@@ -91,4 +91,14 @@ struct lvm_lv_list { };struct dm_list *lvm_lvs_in_vg (vg_t *vg); +/* +* FIXME: struct lvm_pv_list is not equivalent to struct pv_list, which +* is used in lvm_pvs_in_vg(). Ideally we should not need a new structure. +*/ +struct lvm_pv_list { +struct dm_list list; +pv_t *pv; +}; +struct dm_list *lvm_pvs_in_vg (vg_t *vg); +#endif
lib/metadata/metadata.c +5 -0
@@ -2496,6 +2496,11 @@ void lvm_vg_close(vg_t *vg)unlock_vg (vg->cmd, vg->name); } +struct dm_list *lvm_pvs_in_vg (vg_t *vg) +{ +return (&vg->pvs); +} +/* * Returns a list of all top-level, user-displayable LVs in a VG. */
Signature · 1 line
1.6.0.5
PATCH 10/21 Add lvm_lv_get_attr_list() and lvm_lv_get_attr_value().
--- lib/lvm2.h | 3 ++ lib/report/report.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 0 deletions(-)
lib/lvm2.h +3 -0
@@ -91,6 +91,9 @@ struct lvm_lv_list { };struct dm_list *lvm_lvs_in_vg (vg_t *vg); +int lvm_lv_get_attr_list (lv_t *lv,struct dm_list *list); +int lvm_lv_get_attr_value (lv_t *lv,const char *attr_name, +struct dm_report_field_value_type *value);/* * FIXME: struct lvm_pv_list is not equivalent to struct pv_list, which * is used in lvm_pvs_in_vg(). Ideally we should not need a new structure.
lib/report/report.c +57 -0
60 lines not on this page · raw message
Signature · 1 line
1.6.0.5
PATCH 11/21 First cut at adding pv_obj_* APIs.
Impelemnt pv_obj_get(), pv_obj_get_list(), pv_obj_get_attr_list() and pv_obj_get_attr_value().
This is a first attempt at adding pv_obj_* APIs as outlined by
http://fedoraproject.org/wiki/LVM/liblvm/api
Why add another structure when we could use struct physical_volume and other internal structs? At this point it is not clear but would provide us with the ability to more easily allocate and free handles, and would allow places to store things like the access mode and API errors without polluting the internal structures.
--- lib/lvm2.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/lvm2.h | 36 ++++++++++++++++++ 2 files changed, 157 insertions(+), 0 deletions(-)
lib/lvm2.c +121 -0
@@ -61,3 +61,124 @@ int lvm_reload_config(lvm_handle_t libh) {return refresh_toolcontext ((struct cmd_context *)libh); } + + +struct lvm_pv_obj { +char mode; +struct physical_volume *pv; +}; + + +/* +* Gets object for a physical volume by volume path +* Retuns lvm_pv_obj_t on success, otherwide NULL. errno is used to indicate +* the error. +* mode: "r" or "w" +*/ +lvm_pv_obj_t *lvm_pv_obj_get (lvm_handle_t libh,const char * phys_vol, +const char *mode) +{ +struct cmd_context *cmd = (struct cmd_context *)libh; +lvm_pv_obj_t *pv_obj; +struct physical_volume *pv; +struct dm_list mdas; +uint64_t label_sector; + +dm_list_init (&mdas); + pv =pv_read (cmd, phys_vol, &mdas, &label_sector,0 ); +if (!pv) +return NULL ; + + pv_obj =dm_pool_alloc (cmd->mem,sizeof (*pv_obj)); +if (!pv_obj) +return NULL ; + + pv_obj->pv = pv; + pv_obj->mode = mode[0 ]; + +return pv_obj; +} + + +lvm_pv_obj_list_t *lvm_pv_obj_list (lvm_handle_t libh) +{ +struct dm_list *pvs; +struct pv_list *pvl; +lvm_pv_obj_list_t *head =NULL , *elem; +struct cmd_context *cmd = (struct cmd_context *)libh; + + pvs =get_pvs (cmd); + +if (dm_list_empty (pvs)) +return NULL ; + + dm_list_iterate_items(pvl, pvs) { + elem =dm_pool_alloc (cmd->mem,sizeof (*elem)); +if (!elem) +return NULL ; + elem->obj =lvm_pv_obj_get (libh,lvm_pv_name (pvl->pv),"r" ); +if (!head) + head = elem; +dm_list_add (&head->list, &elem->list); + } +return head; +} + + +/* +* Get a list of attributes for a vg +*/ +int lvm_pv_obj_get_attr_list (lvm_pv_obj_t *obj,struct dm_list *list) +{ +void *rh; +report_type_t report_type =PVS ; + +dm_list_init (list); + +/* +* Create a report so we can return the headings. +*/ +if (!(rh =report_init (obj->pv->fmt->cmd,"all" ,"" , &report_type, +" " ,1 ,1 ,1 ,0 ,0 ,0 ))) { + stack; +return 0 ; + } + +if (!dm_report_get_field_ids (rh, report_type, list)) { +dm_report_free (rh); +return 0 ; + } + +dm_report_free (rh); +return 1 ; +} + +/* +* Get the value of the VG attribute 'attr_name'. +*/ +int lvm_pv_obj_get_attr_value (lvm_pv_obj_t *obj,const char *attr_name, +struct dm_report_field_value_type *value) +{ +void *rh; +report_type_t report_type =PVS ; + +if (!(rh =report_init (obj->pv->fmt->cmd, attr_name,"" , &report_type, +" " ,1 ,1 ,1 ,0 ,0 ,0 ))) { + stack; +return 0 ; + } + +if (!report_object (rh,NULL ,NULL , obj->pv,NULL ,NULL )) { + stack; +return 0 ; + } + +if (!dm_report_get_field_value (rh, value)) { + stack; +return 0 ; + } +dm_report_free (rh); +return 1 ; +} + +
lib/lvm2.h +36 -0
@@ -104,4 +104,40 @@ struct lvm_pv_list { };struct dm_list *lvm_pvs_in_vg (vg_t *vg); +/* +* The lvm physical volume object. +*/ +struct lvm_pv_obj ; +typedef struct lvm_pv_obj lvm_pv_obj_t ;/* with mode inside (r/rw) */ + +/* +* The lvm physical volume object list. +*/ +typedef struct lvm_pv_obj_list { +struct dm_list list; +lvm_pv_obj_t *obj; +}lvm_pv_obj_list_t ; + +/* +* Gets object for a physical volume by volume path +* Retuns lvm_pv_obj_t on success, otherwide NULL. errno is used to indicate +* the error. +* mode: "r" or "rw" +*/ +lvm_pv_obj_t *lvm_pv_obj_get (lvm_handle_t libh,const char * phys_vol, +const char *mode); + + +/* +* Returns list of physical volumes. +* Retuns lvm_pv_obj_list_t on success, otherwide NULL. errno is used to +* indicate the error. The objects are read only. +*/ +lvm_pv_obj_list_t *lvm_pv_obj_list (lvm_handle_t libh); + +int lvm_pv_obj_get_attr_list (lvm_pv_obj_t *obj,struct dm_list *list); +int lvm_pv_obj_get_attr_value (lvm_pv_obj_t *obj,const char *attr_name, +struct dm_report_field_value_type *value); + +#endif
Signature · 1 line
1.6.0.5
PATCH 12/21 Add lvm-types.h to lvm2.h file.
--- lib/lvm2.h | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
lib/lvm2.h +1 -0
@@ -18,6 +18,7 @@#include <stdint.h> #include <fcntl.h> #include "libdevmapper.h" +#include "lvm-types.h" /* needed for str_list */ /* * lvm_handle_t
Signature · 1 line
1.6.0.5
PATCH 13/21 Add test code.
Test lvm_pv_list, lvm_lv_get_attr_list, lvm_lv_get_attr_value(). pv_obj_get(), pv_obj_get_attr_list(), pv_obj_get_attr_value, vg_get_attr_list and vg_get_attr_value, lv_get_attr_list and lv_get_attr_value,lvs_in_vg(), pvs_in_vg()lvm_vg_open, lvm_vg_close().
- modularize code more
- don't ignore orphan vg names (e.g. #orphan_lvm2) but allow comments
- when vg_open called, store vg, lvs, and pvs inside hashes for later lookup
- allow lv attr value display by vgname/lvname
--- test/api/test.c | 487 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 480 insertions(+), 7 deletions(-)
test/api/test.c +480 -7
@@ -29,7 +29,7 @@ static int lvm_split(char *str, int *argc, char **argv, int max)while (*b &&isspace (*b)) b++; -if ((!*b) || (*b =='#' )) +if ((!*b) || ((*argc ==0 )&&(*b =='#' )))break ; e = b;
@@ -48,11 +48,457 @@ static int lvm_split(char *str, int *argc, char **argv, int max)return *argc; } -static int lvmapi_test_shell (lvm_handle_t libh) +static void _show_help (void ) { -int argc, i; +printf ("'pvs_in_vg vgname': " +"Issue lvm_pvs_in_vg() API; must follow vg_open\n" ); +printf ("'pv_obj_get pvname': " +"Issue a lvm_pv_obj_get() API call on PV 'pvname'\n" ); +printf ("'pv_obj_get_attr_list': " +"Issue lvm_pv_obj_get_attr_list() API; must follow pv_obj_get\n" ); +printf ("'pv_obj_get_attr_value [attr_name]':" +"Issue lvm_pv_obj_get_attr_value() API; must follow pv_obj_get\n" ); +printf ("'vgs_open': " +"List the VGs that are currently open\n" ); +printf ("'vgs': " +"List all VGs known to the system\n" ); +printf ("'vg_open vgname': " +"Issue a lvm_vg_open() API call on VG 'vgname'\n" ); +printf ("'vg_close vgname': " +"Issue a lvm_vg_close() API call on VG 'vgname'\n" ); +printf ("'vg_get_attr_list vgname':" +"Issue lvm_vg_get_attr_list() API; must follow vg_open\n" ); +printf ("'vg_get_attr_value vgname [attr_name]':" +"Issue lvm_vg_get_attr_value() API; must follow vg_open\n" ); +printf ("'lvs_in_vg vgname': " +"Issue lvm_lvs_in_vg() API; must follow vg_open\n" ); +printf ("'lv_get_attr_list vgname/lvname': " +"Issue lvm_lv_get_attr_list() API; must follow vg_open\n" ); +printf ("'lv_get_attr_value vgname/lvname [attr_name]': " +"Issue lvm_lv_get_attr_value() API; must follow vg_open\n" ); +printf ("'quit': exit the program\n" ); +} + +static struct dm_hash_table *_vgname_hash =NULL ; +static struct dm_hash_table *_pvname_hash =NULL ; +static struct dm_hash_table *_lvname_hash =NULL ; + +static void _hash_destroy_single (struct dm_hash_table **htable) +{ +if (htable && *htable) { +dm_hash_destroy (*htable); + *htable =NULL ; + } +} + +static void _hash_destroy (void ) +{ +_hash_destroy_single (&_vgname_hash); +_hash_destroy_single (&_pvname_hash); +_hash_destroy_single (&_lvname_hash); +} + +static int _hash_create (void ) +{ +if (!(_vgname_hash =dm_hash_create (128 ))) +return 0 ; +if (!(_pvname_hash =dm_hash_create (128 ))) { +_hash_destroy_single (&_vgname_hash); +return 0 ; + } +if (!(_lvname_hash =dm_hash_create (128 ))) { +_hash_destroy_single (&_vgname_hash); +_hash_destroy_single (&_pvname_hash); +return 0 ; + } +return 1 ; +} + +static vg_t *_lookup_vg_by_name (char **argv,int argc) +{ +vg_t *vg; + +if (argc <2 ) { +printf ("Please enter vg_name\n" ); +return NULL ; + } +if (!(vg =dm_hash_lookup (_vgname_hash, argv[1 ]))) { +printf ("Can't find %s in open VGs - run vg_open first\n" , + argv[1 ]); +return NULL ; + } +if (vg_read_error (vg)) { +printf ("Error on VG - try vg_close/vg_open\n" ); +return NULL ; + } +return vg; +} + +static pv_t *_lookup_pv_by_name (char **argv,int argc) +{ +pv_t *pv; + +if (argc <2 ) { +printf ("Please enter pv_name\n" ); +return NULL ; + } +if (!(pv =dm_hash_lookup (_pvname_hash, argv[1 ]))) { +printf ("Can't find %s in open VGs - run vg_open first\n" , + argv[1 ]); +return NULL ; + } +return pv; +} + +static lv_t *_lookup_lv_by_name (char *lv_name) +{ +lv_t *lv; + +if (!(lv =dm_hash_lookup (_lvname_hash, lv_name))) { +printf ("Can't find %s in open VGs - run vg_open first\n" , + lv_name); +return NULL ; + } +return lv; +} + +static void _print_attr (const char *name, +struct dm_report_field_value_type value) +{ +if (value.is_string) +printf ("%s = %s\n" , name, value.u.s_val); +else +printf ("%s = %llu\n" , name, (unsigned long long )value.u.n_val); +} + +static void get_and_print_vg_attr (vg_t *vg,const char *name) +{ +struct dm_report_field_value_type value; + +if (!lvm_vg_get_attr_value (vg, name, &value)) { +printf ("Error reading vg attribute %s value \n" , + name); +return ; + } +_print_attr (name, value); +} + +static void get_and_print_lv_attr (lv_t *lv,const char *name) +{ +struct dm_report_field_value_type value; + +if (!lvm_lv_get_attr_value (lv, name, &value)) { +printf ("Error reading lv attribute %s value \n" , + name); +return ; + } +_print_attr (name, value); +} + +static void get_and_print_pv_attr (lvm_pv_obj_t *pv,const char *name) +{ +struct dm_report_field_value_type value; + +if (!lvm_pv_obj_get_attr_value (pv, name, &value)) { +printf ("Error reading pv attribute %s value \n" , + name); +return ; + } +_print_attr (name, value); +} + +static void _vg_open (char **argv,int argc,lvm_handle_t libh) +{ +vg_t *vg; +struct dm_list *lvs; +struct lvm_lv_list *lvl; +struct dm_list *pvs; +struct lvm_pv_list *pvl; + +if (argc <2 ) { +printf ("Please enter vg_name\n" ); +return ; + } +if ((vg =dm_hash_lookup (_vgname_hash, argv[1 ]))) { +printf ("VG already open\n" ); +return ; + } + vg =lvm_vg_open (libh, argv[1 ],O_RDONLY ); +if (vg_read_error (vg)) { +printf ("Error opening vg %s\n" , argv[1 ]); +return ; + } + +printf ("Success opening vg %s\n" , argv[1 ]); +dm_hash_insert (_vgname_hash,lvm_vg_name (vg), vg); + +/* +* Add the LVs and PVs into the hashes for lookups +*/ + lvs =lvm_lvs_in_vg (vg); + dm_list_iterate_items(lvl, lvs) { +/* Concatenate VG name with LV name */ +dm_hash_insert (_lvname_hash,lvm_lv_name (lvl->lv), lvl->lv); + } + pvs =lvm_pvs_in_vg (vg); + dm_list_iterate_items(pvl, pvs) { +dm_hash_insert (_pvname_hash,lvm_pv_name (pvl->pv), pvl->pv); + } +} + +static void _vg_close (char **argv,int argc) +{ +vg_t *vg; + +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; +if (argc <2 ) { +printf ("Please enter vg_name\n" ); +return ; + } +if (!(vg =dm_hash_lookup (_vgname_hash, argv[1 ]))) { +printf ("Can't find %s in open VGs - run vg_open first\n" , + argv[1 ]); +return ; + } +dm_hash_remove (_vgname_hash,lvm_vg_name (vg)); +lvm_vg_close (vg); +} + +static void _show_one_vg (vg_t *vg) +{ +/* FIXME: store / display actual open mode */ +printf ("%s: mode = READ\n" ,lvm_vg_name (vg)); +} +static void _list_open_vgs (void ) +{ +dm_hash_iter (_vgname_hash, (dm_hash_iterate_fn ) _show_one_vg); +} + +static void _list_all_vgs (lvm_handle_t libh) +{ +struct dm_list *vgnames; +struct str_list *vgname; + +/* FIXME: properly export str_list and API function */ + vgnames =get_vgnames (libh,0 ); + dm_list_iterate_items(vgname, vgnames) { +printf ("%s\n" , vgname->str); + } +} + +static void _vg_get_attr_list (char **argv,int argc) +{ +struct dm_report_field_ids_type *field; +struct dm_list fields; +vg_t *vg; + +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; +if (!lvm_vg_get_attr_list (vg, &fields)) { +printf ("Error reading vg attribute list\n" ); +return ; + } +printf ("VG attribute names:\n" ); + dm_list_iterate_items(field, &fields) { +printf ("%s\n" , field->id); + } +} + +static void _vg_get_attr_value (char **argv,int argc) +{ +struct dm_list fields; +struct dm_report_field_ids_type *field; +int i; +vg_t *vg; + +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; +if (argc >2 ) { +for (i=2 ; i<argc; i++) +get_and_print_vg_attr (vg, argv[i]); +return ; + } +if (!lvm_vg_get_attr_list (vg, &fields)) { +printf ("Error reading vg attribute list\n" ); +return ; + } + dm_list_iterate_items(field, &fields) { +get_and_print_vg_attr (vg, field->id); + } +} + +static void _pv_obj_get (char **argv,int argc,lvm_pv_obj_t **pvp, +lvm_handle_t libh) +{ +if (argc <2 ) { +printf ("Please enter pv_name\n" ); +return ; + } +if (!(*pvp =lvm_pv_obj_get (libh, argv[1 ],"r" ))) +printf ("Error opening PV %s\n" , argv[1 ]); +else +printf ("Success opening PV %s\n" , argv[1 ]); +} + +static void _pv_obj_get_attr_list (lvm_pv_obj_t *pv) +{ +struct dm_report_field_ids_type *field; +struct dm_list fields; + +if (!pv) { +printf ("Run pv_obj_get first\n" ); +return ; + } +if (!lvm_pv_obj_get_attr_list (pv, &fields)) { +printf ("Error reading pv attribute list\n" ); +return ; + } +printf ("PV attribute names:\n" ); + dm_list_iterate_items(field, &fields) { +printf ("%s\n" , field->id); + } +} + +static void _pv_obj_get_attr_value (char **argv,int argc, +lvm_pv_obj_t *pv) +{ +struct dm_report_field_ids_type *field; +struct dm_list fields; +int i; + +if (!pv) { +printf ("Run pv_obj_get first\n" ); +return ; + } +if (argc >1 ) { +for (i=1 ; i<argc; i++) +get_and_print_pv_attr (pv, argv[i]); +return ; + } +if (!lvm_pv_obj_get_attr_list (pv, &fields)) { +printf ("Error reading pv attribute list\n" ); +return ; + } + dm_list_iterate_items(field, &fields) { +get_and_print_pv_attr (pv, field->id); + } +} + +static void _lvs_in_vg (char **argv,int argc) +{ +struct dm_list *lvs; +struct lvm_lv_list *lvl; +vg_t *vg; + +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; + lvs =lvm_lvs_in_vg (vg); +if (dm_list_empty (lvs)) { +printf ("No LVs in VG %s\n" ,lvm_vg_name (vg)); +return ; + } +printf ("LVs in VG %s:\n" ,lvm_vg_name (vg)); + dm_list_iterate_items(lvl, lvs) { +printf ("%s/%s\n" ,lvm_vg_name (vg),lvm_lv_name (lvl->lv)); + } +} + +static void _pvs_in_vg (char **argv,int argc) +{ +struct dm_list *pvs; +struct lvm_pv_list *pvl; +vg_t *vg; + +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; + pvs =lvm_pvs_in_vg (vg); +if (dm_list_empty (pvs)) { +printf ("No PVs in VG %s\n" ,lvm_vg_name (vg)); +return ; + } +printf ("PVs in VG %s:\n" ,lvm_vg_name (vg)); + dm_list_iterate_items(pvl, pvs) { +printf ("%s\n" ,lvm_pv_name (pvl->pv)); + } +} + +static void _lv_get_attr_list (char **argv,int argc) +{ +struct dm_list fields; +struct dm_report_field_ids_type *field; +vg_t *vg; +char *slash; +lv_t *lv; + +/* +* cmdline should be: vgname/lvname +*/ + slash =strchr (argv[1 ],'/' ); +if (argc <2 || !slash) { +printf ("Invalid commandline - please enter vgname/lvname" ); + } + slash[0 ] ='\0' ; + +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; +if (!strlen (slash+1 ) || !(lv =_lookup_lv_by_name (slash+1 ))) +return ; +if (!lvm_lv_get_attr_list (lv, &fields)) { +printf ("Error reading lv attribute list\n" ); +return ; + } +printf ("LV attribute names:\n" ); + dm_list_iterate_items(field, &fields) { +printf ("%s\n" , field->id); + } +} + +static void _lv_get_attr_value (char **argv,int argc) +{ +struct dm_list fields; +struct dm_report_field_ids_type *field; +int i; +vg_t *vg; +char *slash; +lv_t *lv; + +/* +* cmdline should be: vgname/lvname +*/ + slash =strchr (argv[1 ],'/' ); +if (argc <2 || !slash) { +printf ("Invalid commandline - please enter vgname/lvname" ); + } + slash[0 ] ='\0' ; + +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; +if (!strlen (slash+1 ) || !(lv =_lookup_lv_by_name (slash+1 ))) +return ; +if (!lvm_lv_get_attr_list (lv, &fields)) { +printf ("Error reading lv attribute list\n" ); +return ; + } +if (argc >2 ) { +for (i=2 ; i<argc; i++) +get_and_print_lv_attr (lv, argv[i]); +return ; + } + dm_list_iterate_items(field, &fields) { +get_and_print_lv_attr (lv, field->id); + } +} + + +int lvmapi_test_shell (lvm_handle_t libh) +{ +int argc;char *input =NULL , *args[MAX_ARGS ], **argv; +lvm_pv_obj_t *pv =NULL ; +_hash_create (); + argc=0 ;while (1 ) {free (input); input =readline ("lvm> " );
@@ -86,13 +532,40 @@ static int lvmapi_test_shell(lvm_handle_t libh)printf ("Exiting.\n" );break ; }else if (!strcmp (argv[0 ],"?" ) || !strcmp (argv[0 ],"help" )) { -printf ("No commands defined\n" ); - }else if (!strcmp (argv[0 ],"scan" )) { -for (i=1 ; i < argc; i++) -printf ("Scan a path!\n" ); +_show_help (); + }else if (!strcmp (argv[0 ],"vg_open" )) { +_vg_open (argv, argc, libh); + }else if (!strcmp (argv[0 ],"vg_close" )) { +_vg_close (argv, argc); + }else if (!strcmp (argv[0 ],"vgs_open" )) { +_list_open_vgs (); + }else if (!strcmp (argv[0 ],"vgs" )) { +_list_all_vgs (libh); + }else if (!strcmp (argv[0 ],"vg_get_attr_list" )) { +_vg_get_attr_list (argv, argc); + }else if (!strcmp (argv[0 ],"vg_get_attr_value" )) { +_vg_get_attr_value (argv, argc); + }else if (!strcmp (argv[0 ],"pvs_in_vg" )) { +_pvs_in_vg (argv, argc); + }else if (!strcmp (argv[0 ],"pv_obj_get" )) { +_pv_obj_get (argv, argc, &pv, libh); + }else if (!strcmp (argv[0 ],"pv_obj_get_attr_list" )) { +_pv_obj_get_attr_list (pv); + }else if (!strcmp (argv[0 ],"pv_obj_get_attr_value" )) { +_pv_obj_get_attr_value (argv, argc, pv); + }else if (!strcmp (argv[0 ],"lvs_in_vg" )) { +_lvs_in_vg (argv, argc); + }else if (!strcmp (argv[0 ],"lv_get_attr_list" )) { +_lv_get_attr_list (argv, argc); + }else if (!strcmp (argv[0 ],"lv_get_attr_value" )) { +_lv_get_attr_value (argv, argc); + }else { +printf ("Unrecognized command %s\n" , argv[0 ]); } } +dm_hash_iter (_vgname_hash, (dm_hash_iterate_fn ) lvm_vg_close); +_hash_destroy ();free (input);return 0 ; }
Signature · 1 line
1.6.0.5
PATCH 14/21 Add lvm_errno(), lvm_strerror, lvm_vg_mode.
Create struct lib_context to store lvm_errno field as well as cmd_context. Add 'mode' field to struct volume_group.
Create error APIs - lvm_errno, lvm_strerror.
Update lvm_vg_open to set lvm_errno if invalid mode given (mode != O_RDONLY). Move lvm_vg_open, lvm_vg_close to lvm2.c (b/c lib_context defined there). Update test code to use lvm_strerror().
--- lib/lvm2.c | 97 ++++++++++++++++++++++++++++++++------ lib/lvm2.h | 13 +++++ lib/metadata/metadata-exported.h | 1 + lib/metadata/metadata.c | 25 ---------- test/api/test.c | 20 ++++++-- 5 files changed, 111 insertions(+), 45 deletions(-)
lib/lvm2.c +83 -14
@@ -12,6 +12,8 @@* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include <errno.h> +#include <string.h> #include "lvm2.h" #include "lib.h" #include "toolcontext.h"
@@ -19,10 +21,70 @@#include "metadata-exported.h" #include "report.h" +struct lib_context { +struct cmd_context *cmd; +int lvm_errno; +}; + +mode_t lvm_vg_mode (vg_t *vg) +{ +return vg->mode; +} + +int lvm_errno (lvm_handle_t libh) +{ +return libh->lvm_errno; +} + +char *lvm_strerror (lvm_handle_t libh) +{ +return strerror (libh->lvm_errno); +} + +/* +* Open / read a VG. +* FIXME: Only read access allowed. +*/ +vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode) +{ +vg_t *vg; + +if ((mode &O_ACCMODE ) !=O_RDONLY ) { +log_error ("Invalid access mode 0x%x for lvm_vg_read()\n" , + mode); + libh->lvm_errno =EPERM ; +return NULL ; + } + vg =vg_read (libh->cmd, vg_name,NULL ,0 ); + +if (vg_read_error (vg)) { +/* FIXME: fill in vg->errno */ +return NULL ; + } + vg->mode = mode; +return vg; +} + +/* +* Close a VG handle. +* If lock is still held, unlock the VG. +* FIXME: We cannot free the memory as it's tied to the cmd->mem pool. +*/ +void lvm_vg_close (vg_t *vg) +{ +if (vgname_is_locked (vg->name)) +unlock_vg (vg->cmd, vg->name); +}lvm_handle_t lvm_create (const char *system_dir) {struct cmd_context *cmd; +struct lib_context *libh; + +if (!(libh =dm_malloc (sizeof (*libh)))) { +log_error ("Failed to allocate library context" ); +return NULL ; + }/* To be done: * logging bound to handle
@@ -32,27 +94,34 @@ lvm_handle_t lvm_create(const char *system_dir) cmd =create_toolcontext (1 , system_dir);/* initilize remaining */ -if (cmd) { -/* initilization from lvm_run_command */ -init_error_message_produced (0 ); -sigint_clear (); - -/* FIXME: locking_type config option needed? */ -/* initialize locking */ -if (!init_locking (-1 , cmd)) { -printf ("Locking initialisation failed." ); -lvm_destroy ((lvm_handle_t ) cmd); -return NULL ; - } +if (!cmd) { +dm_free (libh); +return NULL ; + } + + libh->cmd = cmd; + +/* initilization from lvm_run_command */ +init_error_message_produced (0 ); +sigint_clear (); + +/* FIXME: locking_type config option needed? */ +/* initialize locking */ +if (!init_locking (-1 , cmd)) { +printf ("Locking initialisation failed." ); +lvm_destroy (libh); +dm_free (libh); +return NULL ; } -return (lvm_handle_t ) cmd; +return libh; }void lvm_destroy (lvm_handle_t libh) { -destroy_toolcontext ((struct cmd_context *)libh); +destroy_toolcontext (libh->cmd); +dm_free (libh);/* no error handling here */ }
lib/lvm2.h +13 -0
@@ -83,6 +83,16 @@ int lvm_vg_get_attr_list(vg_t *vg, struct dm_list *list);int lvm_vg_get_attr_value (vg_t *vg,const char *attr_name,struct dm_report_field_value_type *value); +/* +* Open volume group 'vg_name' for reading or writing. +* +* Returns: +* NULL - failure; use liblvm error APIs for specifics +* non-NULL - success; VG handle (vg_t *) returned may be used in future calls. +* +* liblvm error code explanations: +* EPERM: Invalid access mode given. +*/ vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode);void lvm_vg_close (vg_t *vg);
@@ -141,4 +151,7 @@ int lvm_pv_obj_get_attr_value(lvm_pv_obj_t *obj, const char *attr_name, struct dm_report_field_value_type *value); +int lvm_errno (lvm_handle_t libh); +char *lvm_strerror (lvm_handle_t libh); +mode_t lvm_vg_mode (vg_t *vg);#endif
lib/metadata/metadata-exported.h +1 -0
@@ -212,6 +212,7 @@ struct volume_group {struct id id;char *name;char *system_id; +mode_t mode;uint32_t status;alloc_policy_t alloc;
lib/metadata/metadata.c +0 -25
@@ -2471,31 +2471,6 @@ vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,return vg; } -/* -* Open / read a VG. -* FIXME: Only read access allowed. -*/ -vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode) -{ -if ((mode &O_ACCMODE ) !=O_RDONLY ) { -log_error ("Invalid access mode 0x%x for lvm_vg_read()\n" , - mode); -return NULL ; - } -return vg_read ((struct cmd_context *)libh, vg_name,NULL ,0 ); -} - -/* -* Close a VG handle. -* If lock is still held, unlock the VG. -* FIXME: We cannot free the memory as it's tied to the cmd->mem pool. -*/ -void lvm_vg_close (vg_t *vg) -{ -if (vgname_is_locked (vg->name)) -unlock_vg (vg->cmd, vg->name); -} -struct dm_list *lvm_pvs_in_vg (vg_t *vg) {return (&vg->pvs);
test/api/test.c +14 -6
@@ -62,7 +62,7 @@ static void _show_help(void) "List the VGs that are currently open\n");printf ("'vgs': " "List all VGs known to the system\n" ); -printf ("'vg_open vgname': " +printf ("'vg_open vgname ['r' | 'w']': " "Issue a lvm_vg_open() API call on VG 'vgname'\n" );printf ("'vg_close vgname': " "Issue a lvm_vg_close() API call on VG 'vgname'\n" );
@@ -214,6 +214,7 @@ static void _vg_open(char **argv, int argc, lvm_handle_t libh)struct lvm_lv_list *lvl;struct dm_list *pvs;struct lvm_pv_list *pvl; +mode_t mode;if (argc <2 ) {printf ("Please enter vg_name\n" );
@@ -223,9 +224,14 @@ static void _vg_open(char **argv, int argc, lvm_handle_t libh)printf ("VG already open\n" );return ; } - vg =lvm_vg_open (libh, argv[1 ],O_RDONLY ); -if (vg_read_error (vg)) { -printf ("Error opening vg %s\n" , argv[1 ]); +if ((argc ==3 ) && (argv[2 ][0 ] =='w' )) { + mode =O_RDWR ; + }else + mode =O_RDONLY ; + vg =lvm_vg_open (libh, argv[1 ], mode); +if (!vg) { +printf ("Error opening %s: %s\n" , argv[1 ], +lvm_strerror (libh));return ; }
@@ -267,8 +273,10 @@ static void _vg_close(char **argv, int argc)static void _show_one_vg (vg_t *vg) { -/* FIXME: store / display actual open mode */ -printf ("%s: mode = READ\n" ,lvm_vg_name (vg)); +printf ("%s: mode = %s\n" ,lvm_vg_name (vg), +lvm_vg_mode (vg) ==O_RDWR ?"READ/WRITE" : +lvm_vg_mode (vg) ==O_RDONLY ?"READ" : +"UNKNOWN" ); }static void _list_open_vgs (void ) {
Signature · 1 line
1.6.0.5
PATCH 15/21 Add write mode to vg_open(), calling the new vg_read API with READ_FOR_UPDATE.
--- lib/lvm2.c | 9 ++++++--- 1 files changed, 6 insertions(+), 3 deletions(-)
lib/lvm2.c +6 -3
@@ -49,14 +49,17 @@ vg_t *lvm_vg_open(lvm_handle_t libh, const char *vg_name, mode_t mode) {vg_t *vg; -if ((mode &O_ACCMODE ) !=O_RDONLY ) { +if ((mode &O_ACCMODE ) ==O_RDONLY ) { + vg =vg_read (libh->cmd, vg_name,NULL ,0 ); + }else if ((mode &O_ACCMODE ) ==O_RDWR ) { + vg =vg_read (libh->cmd, vg_name,NULL ,READ_FOR_UPDATE ); + }else {log_error ("Invalid access mode 0x%x for lvm_vg_read()\n" , mode); libh->lvm_errno =EPERM ;return NULL ; } - vg =vg_read (libh->cmd, vg_name,NULL ,0 ); - +if (vg_read_error (vg)) {/* FIXME: fill in vg->errno */ return NULL ;
Signature · 1 line
1.6.0.5
PATCH 16/21 Add lvm_vg_uuid with test code.
--- lib/lvm2.h | 1 + lib/metadata/metadata.c | 14 ++++++++++++++ test/api/test.c | 3 ++- 3 files changed, 17 insertions(+), 1 deletions(-)
lib/lvm2.h +1 -0
@@ -78,6 +78,7 @@ typedef struct logical_volume lv_t;const char *lvm_pv_name (const pv_t *pv);const char *lvm_vg_name (const vg_t *vg);const char *lvm_lv_name (const lv_t *lv); +const char *lvm_vg_uuid (vg_t *vg);int lvm_vg_get_attr_list (vg_t *vg,struct dm_list *list); int lvm_vg_get_attr_value(vg_t *vg, const char *attr_name,
lib/metadata/metadata.c +14 -0
@@ -2717,6 +2717,20 @@ const char *lvm_vg_name(const vg_t *vg)return vg->name; } +/* FIXME: signedness + null termination problem - export proper uuid type */ +const char *lvm_vg_uuid (vg_t *vg) +{ +char *value; + +/* FIXME: leak - this is really bad */ +if (!(value =dm_pool_zalloc (vg->cmd->mem,sizeof (vg->id.uuid)+1 ))) +return NULL ; +strncpy (value, vg->id.uuid,sizeof (vg->id.uuid)); + value[sizeof (vg->id.uuid)] ='\0' ; + +return value; +} +const char *lvm_lv_name (const lv_t *lv) {return lv->name;
test/api/test.c +2 -1
@@ -273,7 +273,8 @@ static void _vg_close(char **argv, int argc)static void _show_one_vg (vg_t *vg) { -printf ("%s: mode = %s\n" ,lvm_vg_name (vg), +printf ("%s: uuid = %s, mode = %s\n" ,lvm_vg_name (vg), +lvm_vg_uuid (vg),lvm_vg_mode (vg) ==O_RDWR ?"READ/WRITE" :lvm_vg_mode (vg) ==O_RDONLY ?"READ" :"UNKNOWN" );
Signature · 1 line
1.6.0.5
PATCH 17/21 Update lvm_vg_open to take either a name or uuid.
We attempt to verify whether the string given is a uuid or a name. Do some uuid validation on it, if that passes, try to look it up in the lvmcache. If we can find the uuid in the cache, we return the vgname from the cache entry and use that vgname in our vg_read() call. Note that the vgname lock was not obtained at the time of reading the cache, but presumably vg_read will fail if the vgname,vgid pair given does not match the disk?
--- lib/lvm2.c | 73 ++++++++++++++++++++++++++++++++++++++++------ lib/lvm2.h | 5 +++ lib/metadata/metadata.c | 2 +- test/api/test.c | 46 ++++++++++++++++++----------- 4 files changed, 98 insertions(+), 28 deletions(-)
lib/lvm2.c +63 -10
@@ -21,6 +21,12 @@#include "metadata-exported.h" #include "report.h" +/* FIXME: these need to be #included */ +extern const char *vgname_from_vgid (struct dm_pool *mem,const char *vgid); +extern int vgname_is_locked (const char *vgname); + + +struct lib_context {struct cmd_context *cmd;int lvm_errno;
@@ -41,31 +47,78 @@ char *lvm_strerror(lvm_handle_t libh)return strerror (libh->lvm_errno); } +struct dm_list *lvm_get_vgids (lvm_handle_t libh) +{ +return get_vgids (libh->cmd,0 ); +} +/* * Open / read a VG. -* FIXME: Only read access allowed. +* Parameters +* - name: either a vgname or a vg uuid */ -vg_t *lvm_vg_open (lvm_handle_t libh,const char *vg_name,mode_t mode) +vg_t *lvm_vg_open (lvm_handle_t libh,const char *name_or_uuid,mode_t mode) { -vg_t *vg; - +vg_t *vg =NULL ; +struct id id; +int found_id =0 ; +uint32_t flags; +const char *vg_name; +if ((mode &O_ACCMODE ) ==O_RDONLY ) { - vg =vg_read (libh->cmd, vg_name,NULL ,0 ); + flags =0 ; }else if ((mode &O_ACCMODE ) ==O_RDWR ) { - vg =vg_read (libh->cmd, vg_name,NULL ,READ_FOR_UPDATE ); + flags =READ_FOR_UPDATE ; }else {log_error ("Invalid access mode 0x%x for lvm_vg_read()\n" , mode); libh->lvm_errno =EPERM ;return NULL ; } - + +/* +* First assume we've been passed a UUID in 'name_or_uuid'. +* If the string passes the uuid validation in id_read_format(), and +* lvmcache's vgname_from_vgid() returns a vgname based on the string +* being a uuid, try to read the vg with the string as uuid. +* If any of these checks fail, or the vg_read() fails, we'll re-try +* the vg_read() later assuming the string was a vgname. +*/ +log_suppress (2 ); + found_id =id_read_format (&id, name_or_uuid); +log_suppress (0 ); + +if (found_id && (vg_name =vgname_from_vgid (libh->cmd->mem, name_or_uuid))) { +/* +* Try the read with the uuid - if it succeeds, great. +*/ + vg =vg_read (libh->cmd, vg_name, name_or_uuid, flags); + } +if (vg_read_error (vg)) { -/* FIXME: fill in vg->errno */ -return NULL ; +/* +* Something went wrong trying the uuid. +* Attempt the read assuming we have a vgname. +*/ + vg =vg_read (libh->cmd, name_or_uuid,NULL , flags); +if (vg_read_error (vg)) +return NULL ; } + vg->mode = mode; -return vg; + +/* +* Something went wrong trying the uuid. +* Attempt the read assuming we have a vgname. +*/ + vg =vg_read (libh->cmd, name_or_uuid,NULL , flags); +if (!vg_read_error (vg)) { + vg->mode = mode; +return vg; + } + +/* FIXME: set errno */ +return NULL ; }/*
lib/lvm2.h +5 -0
@@ -155,4 +155,9 @@ int lvm_pv_obj_get_attr_value(lvm_pv_obj_t *obj, const char *attr_name,int lvm_errno (lvm_handle_t libh);char *lvm_strerror (lvm_handle_t libh);mode_t lvm_vg_mode (vg_t *vg); + +struct dm_list *lvm_get_vgids (lvm_handle_t libh); +/* FIXME: some of these might be ok as is, some might need cleanup */ +extern const char *vgname_from_vgid (struct dm_pool *mem,const char *vgid); +extern uint32_t vg_read_error (vg_t *vg_handle);#endif
lib/metadata/metadata.c +1 -1
@@ -2574,7 +2574,7 @@ static vg_t *_vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,if (misc_flags &READ_ALLOW_INCONSISTENT || !(lock_flags &LCK_WRITE )) consistent =0 ; -if (!validate_name (vg_name) && !is_orphan_vg (vg_name)) { +if (vg_name && !validate_name (vg_name) && !is_orphan_vg (vg_name)) {log_error ("Volume group name %s has invalid characters" , vg_name);return NULL ;
test/api/test.c +29 -17
128 lines not on this page · raw message
Signature · 1 line
1.6.0.5
PATCH 18/21 Update lvmcache_update_vg to use vgid as well as vgname.
With duplicate vgnames in a system, vg metadata could get exported to the wrong lvmcache_vginfo structure. Use the vg_uuid as well as the name to lookup the vginfo structure before we store the metadata.
--- lib/cache/lvmcache.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
lib/cache/lvmcache.c +1 -1
@@ -1068,7 +1068,7 @@ int lvmcache_update_vg(struct volume_group *vg, unsigned precommitted)/* store text representation of vg to cache */ if (vg->cmd->current_settings.cache_vgmetadata && - (vginfo =vginfo_from_vgname (vg->name,NULL ))) + (vginfo =vginfo_from_vgname (vg->name, (char *)&vg->id)))_store_metadata (vginfo, vg, precommitted);return 1 ;
Signature · 1 line
1.6.0.5
PATCH 19/21 Update lvm_vg_open() to read and store the list of vg attributes.
The original lvm_vg_get_attr_list() code used rh->mem, then called dm_report_free(rh) which released the memory for the attributes. For obvious reasons this won't work.
This patch is basically the original lvm_vg_get_attr_list() code moved into lvm_vg_open(). We read all vg attributes and store them on a list inside struct volume_group. The list remains valid and the report handle open until vg_close is called where we call dm_report_free().
--- lib/lvm2.c | 27 +++++++++++++++++++-------- lib/metadata/metadata-exported.h | 6 ++++++ 2 files changed, 25 insertions(+), 8 deletions(-)
lib/lvm2.c +19 -8
@@ -64,6 +64,7 @@ vg_t *lvm_vg_open(lvm_handle_t libh, const char *name_or_uuid, mode_t mode)int found_id =0 ;uint32_t flags;const char *vg_name; +report_type_t report_type =VGS ;if ((mode &O_ACCMODE ) ==O_RDONLY ) { flags =0 ;
@@ -108,17 +109,26 @@ vg_t *lvm_vg_open(lvm_handle_t libh, const char *name_or_uuid, mode_t mode) vg->mode = mode;/* -* Something went wrong trying the uuid. -* Attempt the read assuming we have a vgname. +* Fill in all attributes */ - vg =vg_read (libh->cmd, name_or_uuid,NULL , flags); -if (!vg_read_error (vg)) { - vg->mode = mode; -return vg; +dm_list_init (&vg->attrs); + +/* +* Create a report so we can return the field ids / attribute names +*/ +if (!(vg->rh =report_init (vg->cmd,"all" ,"" , &report_type, +" " ,1 ,1 ,1 ,0 ,0 ,0 ))) { + stack; +/* FIXME: set errno */ +return NULL ; } -/* FIXME: set errno */ -return NULL ; +if (!dm_report_get_field_ids (vg->rh, report_type, &vg->attrs)) { +dm_report_free (vg->rh); +/* FIXME: set errno */ +return NULL ; + } +return vg; }/*
@@ -128,6 +138,7 @@ vg_t *lvm_vg_open(lvm_handle_t libh, const char *name_or_uuid, mode_t mode)*/ void lvm_vg_close (vg_t *vg) { +dm_report_free (vg->rh);if (vgname_is_locked (vg->name))unlock_vg (vg->cmd, vg->name); }
lib/metadata/metadata-exported.h +6 -0
@@ -261,6 +261,12 @@ struct volume_group {* 0 for success else appropriate FAILURE_* bits set. */ uint32_t read_status; + +/* +* Store all fields of a VG report here, the vg attributes. +*/ +struct dm_list attrs; +void *rh; };/* There will be one area for each stripe */
Signature · 1 line
1.6.0.5
PATCH 20/21 Update test code to use new lvm_get_vg_attr_list(), list from lvm_vg_open().
The updated lvm_vg_open() stores the list of vg attributes at open time.
--- test/api/test.c | 84 +++++++++++++++++++++++++++++------------------------- 1 files changed, 45 insertions(+), 39 deletions(-)
test/api/test.c +45 -39
@@ -218,6 +218,35 @@ static void get_and_print_pv_attr(lvm_pv_obj_t *pv, const char *name)_print_attr (name, value); } +static void _show_one_vg (vg_t *vg) +{ +/* FIXME: uuid is not null terminated */ +printf ("%s (%s): mode = %s\n" ,lvm_vg_name (vg), +lvm_vg_uuid (vg), +lvm_vg_mode (vg) ==O_RDWR ?"READ/WRITE" : +lvm_vg_mode (vg) ==O_RDONLY ?"READ" : +"UNKNOWN" ); +} +static void _list_open_vgs (void ) +{ +dm_hash_iter (_vgid_hash, (dm_hash_iterate_fn ) _show_one_vg); +dm_hash_iter (_vgname_hash, (dm_hash_iterate_fn ) _show_one_vg); +} + +static void _list_all_vgs (lvm_handle_t libh) +{ +struct dm_list *vgids; +struct str_list *vgid; +const char *vgname; + +/* FIXME: properly export str_list and API function */ + vgids =lvm_get_vgids (libh); + dm_list_iterate_items(vgid, vgids) { + vgname =vgname_from_vgid (NULL , vgid->str); +printf ("%s, %s\n" , vgname, vgid->str); + } +} +static void _vg_open (char **argv,int argc,lvm_handle_t libh) {vg_t *vg;
@@ -273,62 +302,39 @@ static void _vg_close(char **argv, int argc)printf ("Please enter vg_name\n" );return ; } -if (!(vg =_lookup_vg_by_name (argv, argc))) -return ; -dm_hash_remove (_vgid_hash,lvm_vg_uuid (vg)); -dm_hash_remove (_vgname_hash,lvm_vg_name (vg)); -lvm_vg_close (vg); -} - -static void _show_one_vg (vg_t *vg) -{ -/* FIXME: uuid is not null terminated */ -printf ("%s (%s): mode = %s\n" ,lvm_vg_name (vg), -lvm_vg_uuid (vg), -lvm_vg_mode (vg) ==O_RDWR ?"READ/WRITE" : -lvm_vg_mode (vg) ==O_RDONLY ?"READ" : -"UNKNOWN" ); -} -static void _list_open_vgs (void ) -{ -dm_hash_iter (_vgid_hash, (dm_hash_iterate_fn ) _show_one_vg); -} - -void _list_all_vgs (lvm_handle_t libh) -{ -struct dm_list *vgids; -struct str_list *vgid; -const char *vgname; - -/* FIXME: properly export str_list and API function */ - vgids =lvm_get_vgids (libh); - dm_list_iterate_items(vgid, vgids) { - vgname =vgname_from_vgid (NULL , vgid->str); -printf ("%s, %s\n" , vgname, vgid->str); +while ((vg =dm_hash_lookup (_vgname_hash, argv[1 ]))) { +dm_hash_remove (_vgid_hash,lvm_vg_uuid (vg)); +dm_hash_remove (_vgname_hash,lvm_vg_name (vg)); +lvm_vg_close (vg); } -} +while ((vg =dm_hash_lookup (_vgid_hash, argv[1 ]))) { +dm_hash_remove (_vgid_hash,lvm_vg_uuid (vg)); +dm_hash_remove (_vgname_hash,lvm_vg_name (vg)); +lvm_vg_close (vg); + } +}static void _vg_get_attr_list (char **argv,int argc) {struct dm_report_field_ids_type *field; -struct dm_list fields; +struct dm_list *fields;vg_t *vg;if (!(vg =_lookup_vg_by_name (argv, argc)))return ; -if (!lvm_vg_get_attr_list (vg, &fields)) { +if (!(fields =lvm_vg_get_attr_list (vg))) {printf ("Error reading vg attribute list\n" );return ; }printf ("VG attribute names:\n" ); - dm_list_iterate_items(field, &fields) { + dm_list_iterate_items(field, fields) {printf ("%s\n" , field->id); } }static void _vg_get_attr_value (char **argv,int argc) { -struct dm_list fields; +struct dm_list *fields;struct dm_report_field_ids_type *field;int i;vg_t *vg;
@@ -340,11 +346,11 @@ static void _vg_get_attr_value(char **argv, int argc)get_and_print_vg_attr (vg, argv[i]);return ; } -if (!lvm_vg_get_attr_list (vg, &fields)) { +if (!(fields =lvm_vg_get_attr_list (vg))) {printf ("Error reading vg attribute list\n" );return ; } - dm_list_iterate_items(field, &fields) { + dm_list_iterate_items(field, fields) {get_and_print_vg_attr (vg, field->id); } }
Signature · 1 line
1.6.0.5
PATCH 21/21 Update lvm_vg_get_attr_list() to use attributes obtained during vg_open().
The original lvm_vg_get_attr_list() code used rh->mem, then called dm_report_free(rh) which released the memory for the attributes. This patch relies on the change to vg_open(), which reads all vg attributes and stores them on a list inside struct volume_group. The list remains valid and the report handle open until vg_close is called.
TODO: Fix lvm_vg_get_attr_value() to use attribute values obtained during vg_open. Probably need to define an lvm_attribute structure.
--- lib/lvm2.h | 2 +- lib/report/report.c | 27 +++++---------------------- 2 files changed, 6 insertions(+), 23 deletions(-)
lib/lvm2.h +1 -1
@@ -80,7 +80,7 @@ const char *lvm_vg_name(const vg_t *vg);const char *lvm_lv_name (const lv_t *lv);const char *lvm_vg_uuid (vg_t *vg); -int lvm_vg_get_attr_list (vg_t *vg,struct dm_list *list); +struct dm_list *lvm_vg_get_attr_list (vg_t *vg);int lvm_vg_get_attr_value (vg_t *vg,const char *attr_name,struct dm_report_field_value_type *value);
lib/report/report.c +5 -22
@@ -1193,29 +1193,9 @@ int report_object(void *handle, struct volume_group *vg,/* * Get a list of attributes for a vg */ -int lvm_vg_get_attr_list (vg_t *vg,struct dm_list *list) +struct dm_list *lvm_vg_get_attr_list (vg_t *vg) { -void *rh; -report_type_t report_type =VGS ; - -dm_list_init (list); - -/* -* Create a report so we can return the headings. -*/ -if (!(rh =report_init (vg->cmd,"all" ,"" , &report_type, -" " ,1 ,1 ,1 ,0 ,0 ,0 ))) { - stack; -return 0 ; - } - -if (!dm_report_get_field_ids (rh, report_type, list)) { -dm_report_free (rh); -return 0 ; - } - -dm_report_free (rh); -return 1 ; +return &vg->attrs; }/*
@@ -1224,6 +1204,9 @@ int lvm_vg_get_attr_list(vg_t *vg, struct dm_list *list)int lvm_vg_get_attr_value (vg_t *vg,const char *attr_name,struct dm_report_field_value_type *value) { +/* +* FIXME: this should be returning values filled in during vg_open +*/ void *rh;report_type_t report_type =VGS ;
Signature · 1 line
1.6.0.5