From 8961b1d503de120a1f220f099fc1fa8080a92e5e Mon Sep 17 00:00:00 2001 From: Petr Rockai Date: Tue, 14 Dec 2010 23:20:58 +0000 Subject: Add getters for copy_percent and snap_percent to the lvm2app API. --- lib/report/properties.c | 17 +++++++++++++++-- liblvm/lvm2app.h | 30 ++++++++++++++++++++++++++++-- liblvm/lvm_base.c | 2 +- liblvm/lvm_lv.c | 2 +- liblvm/lvm_misc.c | 4 ++-- liblvm/lvm_misc.h | 1 + liblvm/lvm_pv.c | 2 +- liblvm/lvm_vg.c | 2 +- test/api/Makefile.in | 2 +- test/api/percent.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/api/percent.sh | 10 ++++++++++ 11 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 test/api/percent.c create mode 100644 test/api/percent.sh diff --git a/lib/report/properties.c b/lib/report/properties.c index edbaecdd..0b4929aa 100644 --- a/lib/report/properties.c +++ b/lib/report/properties.c @@ -16,6 +16,7 @@ #include "libdevmapper.h" #include "properties.h" +#include "activate.h" #include "lvm-logging.h" #include "lvm-types.h" #include "metadata.h" @@ -85,6 +86,18 @@ static int _not_implemented_set(void *obj, struct lvm_property_type *prop) return 0; } +static percent_t _copy_percent(const struct logical_volume *lv) { + percent_t perc; + lv_mirror_percent(lv->vg->cmd, (struct logical_volume *) lv, 0, &perc, NULL); + return perc; +} + +static percent_t _snap_percent(const struct logical_volume *lv) { + percent_t perc; + lv_snapshot_percent(lv, &perc); + return perc; +} + /* PV */ GET_PV_STR_PROPERTY_FN(pv_fmt, pv_fmt_dup(pv)) #define _pv_fmt_set _not_implemented_set @@ -148,9 +161,9 @@ GET_LV_STR_PROPERTY_FN(origin, lv_origin_dup(lv->vg->vgmem, lv)) #define _origin_set _not_implemented_set GET_LV_NUM_PROPERTY_FN(origin_size, lv_origin_size(lv)) #define _origin_size_set _not_implemented_set -#define _snap_percent_get _not_implemented_get +GET_LV_NUM_PROPERTY_FN(snap_percent, _snap_percent(lv)) #define _snap_percent_set _not_implemented_set -#define _copy_percent_get _not_implemented_get +GET_LV_NUM_PROPERTY_FN(copy_percent, _copy_percent(lv)) #define _copy_percent_set _not_implemented_set GET_LV_STR_PROPERTY_FN(move_pv, lv_move_pv_dup(lv->vg->vgmem, lv)) #define _move_pv_set _not_implemented_set diff --git a/liblvm/lvm2app.h b/liblvm/lvm2app.h index 1fff08ad..317911d9 100644 --- a/liblvm/lvm2app.h +++ b/liblvm/lvm2app.h @@ -1446,8 +1446,10 @@ uint64_t lvm_pv_get_free(const pv_t pv); * Name of property to query. See pvs man page for full list of properties * that may be queried. * - * The memory allocated for a string property value is tied to the vg_t - * handle and will be released when lvm_vg_close() is called. + * The memory allocated for a string property value is tied to the vg_t handle + * and will be released when lvm_vg_close() is called. For "percent" values + * (those obtained for copy_percent and snap_percent properties), please see + * percent_range_t and lvm_percent_to_float(). * * Example: * lvm_property_value value; @@ -1578,6 +1580,30 @@ pv_t lvm_pv_from_uuid(vg_t vg, const char *uuid); */ int lvm_pv_resize(const pv_t pv, uint64_t new_size); +#ifndef _LVM_PERCENT_H + +/** + * This type defines a couple of special percent values. The PERCENT_0 and + * PERCENT_100 constants designate *exact* percentages: values are never + * rounded to either of these two. + */ +typedef enum { + PERCENT_0 = 0, + PERCENT_1 = 1000000, + PERCENT_100 = 100 * PERCENT_1, + PERCENT_INVALID = -1 +} percent_range_t; + +typedef int32_t percent_t; + +#endif + +/** + * Convert a (fixed-point) value obtained from the percent-denominated + * *_get_property functions into a floating-point value. + */ +float lvm_percent_to_float(percent_t v); + #ifdef __cplusplus } #endif diff --git a/liblvm/lvm_base.c b/liblvm/lvm_base.c index 7ee7c362..f8e10046 100644 --- a/liblvm/lvm_base.c +++ b/liblvm/lvm_base.c @@ -13,11 +13,11 @@ */ #include "lib.h" -#include "lvm2app.h" #include "toolcontext.h" #include "locking.h" #include "lvm-version.h" #include "metadata-exported.h" +#include "lvm2app.h" const char *lvm_library_get_version(void) { diff --git a/liblvm/lvm_lv.c b/liblvm/lvm_lv.c index 85d536cf..b77f78cd 100644 --- a/liblvm/lvm_lv.c +++ b/liblvm/lvm_lv.c @@ -13,7 +13,6 @@ */ #include "lib.h" -#include "lvm2app.h" #include "metadata.h" #include "lvm-string.h" #include "defaults.h" @@ -21,6 +20,7 @@ #include "locking.h" #include "activate.h" #include "lvm_misc.h" +#include "lvm2app.h" static int _lv_check_handle(const lv_t lv, const int vg_writeable) { diff --git a/liblvm/lvm_misc.c b/liblvm/lvm_misc.c index dc349b55..62fef612 100644 --- a/liblvm/lvm_misc.c +++ b/liblvm/lvm_misc.c @@ -12,10 +12,10 @@ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "lvm2app.h" -#include "lvm_misc.h" #include "lib.h" #include "properties.h" +#include "lvm_misc.h" +#include "lvm2app.h" struct dm_list *tag_list_copy(struct dm_pool *p, struct dm_list *tag_list) { diff --git a/liblvm/lvm_misc.h b/liblvm/lvm_misc.h index f5a39fa5..a0324b8c 100644 --- a/liblvm/lvm_misc.h +++ b/liblvm/lvm_misc.h @@ -15,6 +15,7 @@ #define _LVM2APP_MISC_H #include "libdevmapper.h" +#include "lvm2app.h" struct dm_list *tag_list_copy(struct dm_pool *p, struct dm_list *tag_list); struct lvm_property_value get_property(const pv_t pv, const vg_t vg, diff --git a/liblvm/lvm_pv.c b/liblvm/lvm_pv.c index 5f2014d1..4fe2d3ed 100644 --- a/liblvm/lvm_pv.c +++ b/liblvm/lvm_pv.c @@ -13,10 +13,10 @@ */ #include "lib.h" -#include "lvm2app.h" #include "metadata.h" #include "lvm-string.h" #include "lvm_misc.h" +#include "lvm2app.h" const char *lvm_pv_get_uuid(const pv_t pv) { diff --git a/liblvm/lvm_vg.c b/liblvm/lvm_vg.c index 28185197..f087ba43 100644 --- a/liblvm/lvm_vg.c +++ b/liblvm/lvm_vg.c @@ -13,13 +13,13 @@ */ #include "lib.h" -#include "lvm2app.h" #include "toolcontext.h" #include "metadata.h" #include "archiver.h" #include "locking.h" #include "lvmcache.h" #include "lvm_misc.h" +#include "lvm2app.h" int lvm_vg_add_tag(vg_t vg, const char *tag) { diff --git a/test/api/Makefile.in b/test/api/Makefile.in index 6d0dafdc..b3fb7510 100644 --- a/test/api/Makefile.in +++ b/test/api/Makefile.in @@ -24,7 +24,7 @@ test_SOURCES = test.c wrapper_SOURCES = test.c INCLUDES += -I../../include -UNIT = vgtest.t +UNIT = vgtest.t percent.t LVMLIBS = @LVM2APP_LIB@ -ldevmapper DEPLIBS = $(top_builddir)/liblvm/liblvm2app.so $(top_builddir)/libdm/libdevmapper.so diff --git a/test/api/percent.c b/test/api/percent.c new file mode 100644 index 00000000..28d8144b --- /dev/null +++ b/test/api/percent.c @@ -0,0 +1,45 @@ +#include "lvm2app.h" + +#define assert(x) do { if (!(x)) goto bad; } while (0) + +int main(int argc, char *argv[]) +{ + lvm_t handle = lvm_init(NULL); + assert(handle); + + vg_t vg = lvm_vg_open(handle, argv[1], "r", 0); + assert(vg); + + lv_t lv = lvm_lv_from_name(vg, "snap"); + assert(lv); + + struct lvm_property_value v = lvm_lv_get_property(lv, "snap_percent"); + assert(v.is_valid); + assert(v.value.integer == PERCENT_0); + + lv = lvm_lv_from_name(vg, "mirr"); + assert(lv); + + v = lvm_lv_get_property(lv, "copy_percent"); + assert(v.is_valid); + assert(v.value.integer == PERCENT_100); + + lv = lvm_lv_from_name(vg, "snap2"); + assert(lv); + + v = lvm_lv_get_property(lv, "snap_percent"); + assert(v.is_valid); + assert(v.value.integer == 50 * PERCENT_1); + + lvm_vg_close(vg); + return 0; + +bad: + if (handle && lvm_errno(handle)) + fprintf(stderr, "LVM Error: %s\n", lvm_errmsg(handle)); + if (vg) + lvm_vg_close(vg); + if (handle) + lvm_quit(handle); + return 1; +} diff --git a/test/api/percent.sh b/test/api/percent.sh new file mode 100644 index 00000000..46964e36 --- /dev/null +++ b/test/api/percent.sh @@ -0,0 +1,10 @@ +. ./test-utils.sh +aux prepare_devs 2 +vgcreate -c n -s 4k $vg $devs +lvcreate -n foo $vg -l 5 +lvcreate -s -n snap $vg/foo -l 2 -c 4k +lvcreate -s -n snap2 $vg/foo -l 6 -c 4k +dd if=/dev/urandom of=$DM_DEV_DIR/$vg/snap2 count=1 bs=1024 +lvcreate -m 1 -n mirr $vg -l 1 --mirrorlog core +lvs +apitest percent $vg -- cgit