Skip to contentSkip to conversation

Add vg/lv tag addition/deletion to lvm2app

PATCH New Dave Wysochanski 2 participants 10 messages Feb 16, 2010 – Feb 17, 2010

lore
Share & tools
lore
#0 lore raw

cover Add vg/lv tag addition/deletion to lvm2app

This patchset adds lv/vg tag addition/deletion to lvm2app. pv tags will be handled later when the pv refactoring patches are done. Partially fixes rhbz 562909.

Patchset is fairly straightforward. When adding a tag, we need to copy the tag memory before adding it to the list (see patch #4). The copy could be placed inside lvm2app or in the internal library. I chose the internal library to minmize code in lvm2app. Another finer point is what to return when the 'get_tags' lvm2app function is called but there's no tags - a NULL pointer or an empty list. For now I've chosen a NULL pointer to avoid allocation of memory. There is some inconsistency here in lvm2app but the behavior is documented in the header for all functions that return lists.

#1 lore raw

PATCH 1/7 Refactor _vgchange_tag() to vg_change_tag() library function.

Pull out common code to be called from tools as well as lvm2app. Leave archive() at tool level so we can use from vgcreate as well as vgchange. Should be no functional change.

---
 lib/metadata/metadata-exported.h |    1 +
 lib/metadata/metadata.c          |   23 +++++++++++++++++++++++
 tools/vgchange.c                 |   20 ++------------------
 3 files changed, 26 insertions(+), 18 deletions(-)
lib/metadata/metadata-exported.h +1 -0
index 164ce36..fae9551 100644
@@ -463,6 +463,7 @@ int vg_rename(struct cmd_context *cmd, struct volume_group *vg,
 int vg_extend(struct volume_group *vg, int pv_count, char **pv_names,
 	      struct pvcreate_params *pp);
 int vg_reduce(struct volume_group *vg, char *pv_name);
+int vg_change_tag(struct volume_group *vg, const char *tag, int add_tag);
 int vg_set_extent_size(struct volume_group *vg, uint32_t new_extent_size);
 int vg_set_max_lv(struct volume_group *vg, uint32_t max_lv);
 int vg_set_max_pv(struct volume_group *vg, uint32_t max_pv);
lib/metadata/metadata.c +23 -0
index b3b3778..52bbc46 100644
@@ -665,6 +665,29 @@ int vg_reduce(struct volume_group *vg, char *pv_name)
 	return 0;
 }
 
+int vg_change_tag(struct volume_group *vg, const char *tag, int add_tag)
+{
+	if (!(vg->fid->fmt->features & FMT_TAGS)) {
+		log_error("Volume group %s does not support tags", vg->name);
+		return 0;
+	}
+
+	if (add_tag) {
+		if (!str_list_add(vg->vgmem, &vg->tags, tag)) {
+			log_error("Failed to add tag %s to volume group %s",
+				  tag, vg->name);
+			return 0;
+		}
+	} else {
+		if (!str_list_del(&vg->tags, tag)) {
+			log_error("Failed to remove tag %s from volume group "
+				  "%s", tag, vg->name);
+			return 0;
+		}
+	}
+	return 1;
+}
+
 const char *strip_dir(const char *vg_name, const char *dev_dir)
 {
 	size_t len = strlen(dev_dir);
tools/vgchange.c +2 -18
index b4f5d00..8f4439e 100644
@@ -447,29 +447,13 @@ static int _vgchange_tag(struct cmd_context *cmd, struct volume_group *vg,
 		return ECMD_FAILED;
 	}
 
-	if (!(vg->fid->fmt->features & FMT_TAGS)) {
-		log_error("Volume group %s does not support tags", vg->name);
-		return ECMD_FAILED;
-	}
-
 	if (!archive(vg)) {
 		stack;
 		return ECMD_FAILED;
 	}
 
-	if ((arg == addtag_ARG)) {
-		if (!str_list_add(cmd->mem, &vg->tags, tag)) {
-			log_error("Failed to add tag %s to volume group %s",
-				  tag, vg->name);
-			return ECMD_FAILED;
-		}
-	} else {
-		if (!str_list_del(&vg->tags, tag)) {
-			log_error("Failed to remove tag %s from volume group "
-				  "%s", tag, vg->name);
-			return ECMD_FAILED;
-		}
-	}
+	if (!vg_change_tag(vg, tag, arg == addtag_ARG))
+		return ECMD_FAILED;
 
 	if (!vg_write(vg) || !vg_commit(vg)) {
 		stack;
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 2/7 Refactor vgcreate to call new vg_change_tag() function.

---
 tools/vgcreate.c |   10 +---------
 1 files changed, 1 insertions(+), 9 deletions(-)
tools/vgcreate.c +1 -9
index 5113b1f..75440c2 100644
@@ -94,16 +94,8 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv)
 			goto bad;
 		}
 
-		if (!(vg->fid->fmt->features & FMT_TAGS)) {
-			log_error("Volume group format does not support tags");
+		if (!vg_change_tag(vg, tag, 1))
 			goto bad;
-		}
-
-		if (!str_list_add(cmd->mem, &vg->tags, tag)) {
-			log_error("Failed to add tag %s to volume group %s",
-				  tag, vp_new.vg_name);
-			goto bad;
-		}
 	}
 
 	if (vg_is_clustered(vg)) {
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 3/7 Refactor lvchange_tag() to call lv_change_tag() library function.

Similar refactoring to vgchange - pull out common parts and put into library function for reuse. Should be no functional change.

---
 lib/metadata/metadata-exported.h |    2 ++
 lib/metadata/metadata.c          |   24 ++++++++++++++++++++++++
 tools/lvchange.c                 |   19 +------------------
 3 files changed, 27 insertions(+), 18 deletions(-)
lib/metadata/metadata-exported.h +2 -0
index fae9551..9540285 100644
@@ -493,6 +493,8 @@ struct logical_volume *lv_create_empty(const char *name,
 int set_lv(struct cmd_context *cmd, struct logical_volume *lv,
            uint64_t sectors, int value);
 
+int lv_change_tag(struct logical_volume *lv, const char *tag, int add_tag);
+
 /* Reduce the size of an LV by extents */
 int lv_reduce(struct logical_volume *lv, uint32_t extents);
 
lib/metadata/metadata.c +24 -0
index 52bbc46..2b8a1be 100644
@@ -665,6 +665,30 @@ int vg_reduce(struct volume_group *vg, char *pv_name)
 	return 0;
 }
 
+int lv_change_tag(struct logical_volume *lv, const char *tag, int add_tag)
+{
+	if (!(lv->vg->fid->fmt->features & FMT_TAGS)) {
+		log_error("Logical volume %s/%s does not support tags",
+			  lv->vg->name, lv->name);
+		return 0;
+	}
+
+	if (add_tag) {
+		if (!str_list_add(lv->vg->vgmem, &lv->tags, tag)) {
+			log_error("Failed to add tag %s to %s/%s",
+				  tag, lv->vg->name, lv->name);
+			return 0;
+		}
+	} else {
+		if (!str_list_del(&lv->tags, tag)) {
+			log_error("Failed to remove tag %s from %s/%s",
+				  tag, lv->vg->name, lv->name);
+			return 0;
+		}
+	}
+	return 1;
+}
+
 int vg_change_tag(struct volume_group *vg, const char *tag, int add_tag)
 {
 	if (!(vg->fid->fmt->features & FMT_TAGS)) {
tools/lvchange.c +1 -18
index 5118e51..862a709 100644
@@ -500,25 +500,8 @@ static int lvchange_tag(struct cmd_context *cmd, struct logical_volume *lv,
 		return 0;
 	}
 
-	if (!(lv->vg->fid->fmt->features & FMT_TAGS)) {
-		log_error("Logical volume %s/%s does not support tags",
-			  lv->vg->name, lv->name);
+	if (!lv_change_tag(lv, tag, arg == addtag_ARG))
 		return 0;
-	}
-
-	if ((arg == addtag_ARG)) {
-		if (!str_list_add(cmd->mem, &lv->tags, tag)) {
-			log_error("Failed to add tag %s to %s/%s",
-				  tag, lv->vg->name, lv->name);
-			return 0;
-		}
-	} else {
-		if (!str_list_del(&lv->tags, tag)) {
-			log_error("Failed to remove tag %s from %s/%s",
-				  tag, lv->vg->name, lv->name);
-			return 0;
-		}
-	}
 
 	log_very_verbose("Updating logical volume \"%s\" on disk(s)", lv->name);
 
Signature · 1 line
1.6.0.6
#4 lore raw

PATCH 4/7 Add _tag_copy() to allocate memory and copy a tag in {lv|vg}_change_tag()

We need to allocate memory for the tag and copy the tag value before we add it to the list of tags. We could put this inside lvm2app since the tools keep their memory around until vg_write/vg_commit is called. Call _tag_copy() from {vg|lv}_change_tag(). We need to copy the tag passed in by the caller to ensure the memory until the {vg|lv} handle is released.

---
 lib/metadata/metadata.c |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)
lib/metadata/metadata.c +20 -2
index 2b8a1be..e0d63c3 100644
@@ -665,8 +665,22 @@ int vg_reduce(struct volume_group *vg, char *pv_name)
 	return 0;
 }
 
+static char *_tag_copy(struct dm_pool *p, const char *tag)
+{
+	char *tag_new;
+
+	/* FIXME: verify tag length */
+	if (!(tag_new = dm_pool_alloc(p, strlen(tag)))) {
+		log_error("Failed to alloc memory for tag %s.", tag);
+		return NULL;
+	}
+	strcpy(tag_new, tag);
+	return tag_new;
+}
+
 int lv_change_tag(struct logical_volume *lv, const char *tag, int add_tag)
 {
+	char *tag_new;
 	if (!(lv->vg->fid->fmt->features & FMT_TAGS)) {
 		log_error("Logical volume %s/%s does not support tags",
 			  lv->vg->name, lv->name);
@@ -674,7 +688,8 @@ int lv_change_tag(struct logical_volume *lv, const char *tag, int add_tag)
 	}
 
 	if (add_tag) {
-		if (!str_list_add(lv->vg->vgmem, &lv->tags, tag)) {
+		tag_new = _tag_copy(lv->vg->vgmem, tag);
+		if (!str_list_add(lv->vg->vgmem, &lv->tags, tag_new)) {
 			log_error("Failed to add tag %s to %s/%s",
 				  tag, lv->vg->name, lv->name);
 			return 0;
@@ -691,13 +706,16 @@ int lv_change_tag(struct logical_volume *lv, const char *tag, int add_tag)
 
 int vg_change_tag(struct volume_group *vg, const char *tag, int add_tag)
 {
+	char *tag_new;
+
 	if (!(vg->fid->fmt->features & FMT_TAGS)) {
 		log_error("Volume group %s does not support tags", vg->name);
 		return 0;
 	}
 
 	if (add_tag) {
-		if (!str_list_add(vg->vgmem, &vg->tags, tag)) {
+		tag_new = _tag_copy(vg->vgmem, tag);
+		if (!str_list_add(vg->vgmem, &vg->tags, tag_new)) {
 			log_error("Failed to add tag %s to volume group %s",
 				  tag, vg->name);
 			return 0;
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH 5/7 Add lvm_vg_get_tags(), lvm_vg_add_tag(), and lvm_vg_remove_tag().

Add lvm2app functions to manage VG tags.
For lvm_vg_get_tags(), we return a list of tags, similar to other functions that return lists. NULL is returned if there is no tags.

---
 liblvm/.exported_symbols |    3 ++
 liblvm/lvm2app.h         |   49 +++++++++++++++++++++++++++++++++++++++-
 liblvm/lvm_vg.c          |   55 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 2 deletions(-)
liblvm/.exported_symbols +3 -0
index c184a88..7431248 100644
@@ -18,6 +18,7 @@ lvm_vg_get_extent_size
 lvm_vg_get_extent_count
 lvm_vg_get_free_extent_count
 lvm_vg_get_pv_count
+lvm_vg_get_tags
 lvm_lv_activate
 lvm_lv_deactivate
 lvm_lv_get_uuid
@@ -33,6 +34,8 @@ lvm_vg_write
 lvm_vg_open
 lvm_vg_close
 lvm_vg_remove
+lvm_vg_add_tag
+lvm_vg_remove_tag
 lvm_scan
 lvm_errno
 lvm_errmsg
liblvm/lvm2app.h +47 -2

69 lines not on this page · raw message

liblvm/lvm_vg.c +55 -0

67 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#6 lore raw

PATCH 6/7 Add lvm_lv_get_tags(), lvm_lv_add_tag(), and lvm_lv_remove_tag().

Add lvm2app functions to manage VG tags.
For lvm_lv_get_tags(), we return a list of tags, similar to other functions that return lists. NULL is returned if there are no tags.

---
 liblvm/.exported_symbols |    3 ++
 liblvm/lvm2app.h         |   46 ++++++++++++++++++++++++++++++++++++
 liblvm/lvm_lv.c          |   58 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+), 0 deletions(-)
liblvm/.exported_symbols +3 -0
index 7431248..9b61d45 100644
@@ -26,6 +26,9 @@ lvm_lv_get_name
 lvm_lv_get_size
 lvm_lv_is_active
 lvm_lv_is_suspended
+lvm_lv_add_tag
+lvm_lv_remove_tag
+lvm_lv_get_tags
 lvm_vg_create
 lvm_vg_extend
 lvm_vg_reduce
liblvm/lvm2app.h +46 -0

52 lines not on this page · raw message

liblvm/lvm_lv.c +58 -0

70 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#7 lore raw

PATCH 7/7 Update lvm2app interactive unit test for vg/lv tags.

Simple test of vg/lv tag addition/deletion.

---
 test/api/test.c |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 99 insertions(+), 6 deletions(-)
test/api/test.c +99 -6
index 6e222da..98f77e4 100644
@@ -89,6 +89,14 @@ static void _show_help(void)
 	       "Issue a lvm_config_reload() API to reload LVM config\n");
 	printf("'config_override' device: "
 	       "Issue a lvm_config_override() with accept device filter\n");
+	printf("'vg_get_tags vgname': "
+	       "List the tags of a VG\n");
+	printf("'lv_get_tags vgname lvname': "
+	       "List the tags of a LV\n");
+	printf("'vg_{add|remove}_tag vgname tag': "
+	       "Add/remove a tag from a VG\n");
+	printf("'lv_{add|remove}_tag vgname lvname tag': "
+	       "Add/remove a tag from a LV\n");
 	printf("'quit': exit the program\n");
 }
 
@@ -468,13 +476,11 @@ static void _list_vg_names(lvm_t libh)
 {
 	struct dm_list *list;
 	struct lvm_str_list *strl;
-	const char *tmp;
 
 	list = lvm_list_vg_names(libh);
 	printf("VG names:\n");
 	dm_list_iterate_items(strl, list) {
-		tmp = strl->str;
-		printf("%s\n", tmp);
+		printf("%s\n", strl->str);
 	}
 }
 
@@ -482,17 +488,91 @@ static void _list_vg_ids(lvm_t libh)
 {
 	struct dm_list *list;
 	struct lvm_str_list *strl;
-	const char *tmp;
 
 	list = lvm_list_vg_uuids(libh);
 	printf("VG uuids:\n");
 	dm_list_iterate_items(strl, list) {
-		tmp = strl->str;
-		printf("%s\n", tmp);
+		printf("%s\n", strl->str);
 	}
 }
 
+static void _display_tags(struct dm_list *list)
+{
+	struct lvm_str_list *strl;
+	if (!list || dm_list_empty(list)) {
+		printf("No tags exist\n");
+		return;
+	}
+	dm_list_iterate_items(strl, list) {
+		printf("%s\n", strl->str);
+	}
+}
+
+static void _vg_get_tags(char **argv, int argc)
+{
+	vg_t vg;
+
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	printf("VG tags:\n");
+	_display_tags(lvm_vg_get_tags(vg));
+}
+
+static void _vg_tag(char **argv, int argc, int add)
+{
+	vg_t vg;
+
+	if (argc < 3) {
+		printf("Please enter vgname, tag\n");
+		return;
+	}
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	if (add && lvm_vg_add_tag(vg, argv[2]))
+		printf("Error ");
+	else if (!add && lvm_vg_remove_tag(vg, argv[2])){
+		printf("Error ");
+	} else {
+		printf("Success ");
+	}
+	printf("%s tag %s to VG %s\n",
+	       add ? "adding":"removing", argv[2], argv[1]);
+}
+
+static void _lv_get_tags(char **argv, int argc)
+{
+	lv_t lv;
+
+	if (argc < 3) {
+		printf("Please enter vgname, lvname\n");
+		return;
+	}
+	if (!(lv = _lookup_lv_by_name(argv[2])))
+		return;
+	printf("LV tags:\n");
+	_display_tags(lvm_lv_get_tags(lv));
+}
 
+static void _lv_tag(char **argv, int argc, int add)
+{
+	lv_t lv;
+
+	if (argc < 3) {
+		printf("Please enter vgname, lvname\n");
+		return;
+	}
+	if (!(lv = _lookup_lv_by_name(argv[2])))
+		return;
+	if (add && lvm_lv_add_tag(lv, argv[3]))
+		printf("Error ");
+	else if (!add && lvm_lv_remove_tag(lv, argv[3])){
+		printf("Error ");
+	} else {
+		printf("Success ");
+	}
+	printf("%s tag %s to LV %s\n",
+	       add ? "adding":"removing", argv[3], argv[2]);
+}
 static void _lvs_in_vg(char **argv, int argc)
 {
 	struct dm_list *lvs;
@@ -549,6 +629,7 @@ static void _lv_activate(char **argv, int argc)
 	printf("activating LV %s in VG %s\n",
 		argv[2], argv[1]);
 }
+
 static void _vg_remove_lv(char **argv, int argc)
 {
 	lv_t lv;
@@ -670,6 +751,18 @@ static int lvmapi_test_shell(lvm_t libh)
 			_scan_vgs(libh);
 		} else if (!strcmp(argv[0], "vg_create_lv_linear")) {
 			_vg_create_lv_linear(argv, argc);
+		} else if (!strcmp(argv[0], "vg_add_tag")) {
+			_vg_tag(argv, argc, 1);
+		} else if (!strcmp(argv[0], "vg_remove_tag")) {
+			_vg_tag(argv, argc, 0);
+		} else if (!strcmp(argv[0], "vg_get_tags")) {
+			_vg_get_tags(argv, argc);
+		} else if (!strcmp(argv[0], "lv_add_tag")) {
+			_lv_tag(argv, argc, 1);
+		} else if (!strcmp(argv[0], "lv_remove_tag")) {
+			_lv_tag(argv, argc, 0);
+		} else if (!strcmp(argv[0], "lv_get_tags")) {
+			_lv_get_tags(argv, argc);
 		} else {
 			printf ("Unrecognized command %s\n", argv[0]);
 		}
Signature · 1 line
1.6.0.6
#8 lore raw

PATCH 7/7 Update lvm2app interactive unit test for vg/lv tags.

test/api/test.c static void _list_vg_ids(lvm_t libh) · line 495

Quoted 11 lines · Dave Wysochanski

Simple test of vg/lv tag addition/deletion.

---

test/api/test.c +1 -2
 	list = lvm_list_vg_uuids(libh);
 	printf("VG uuids:\n");
 	dm_list_iterate_items(strl, list) {
-		tmp = strl->str;
-		printf("%s\n", tmp);
+		printf("%s\n", strl->str);

Simplier: puts(strl->str);

adds trailing '\n' automaticaly.

Zdenek

#9 lore raw

PATCH 4/7 Add _tag_copy() to allocate memory and copy a tag in {lv|vg}_change_tag()

In reply to Dave Wysochanski
Quoted 16 lines · Dave Wysochanski

We need to allocate memory for the tag and copy the tag value before we add it to the list of tags. We could put this inside lvm2app since the tools keep their memory around until vg_write/vg_commit is called. Call _tag_copy() from {vg|lv}_change_tag(). We need to copy the tag passed in by the caller to ensure the memory until the {vg|lv} handle is released.

---
 lib/metadata/metadata.c |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)
lib/metadata/metadata.c +0 -0
index 2b8a1be..e0d63c3 100644

lib/metadata/metadata.c int vg_reduce(struct volume_group *vg, char *pv_name) · line 673

Quoted 9 lines
lib/metadata/metadata.c +6 -0
@@ -665,8 +665,22 @@ int vg_reduce(struct volume_group *vg, char *pv_name)
 	return 0;
 }
 
+static char *_tag_copy(struct dm_pool *p, const char *tag)
+{
+	char *tag_new;
+
+	/* FIXME: verify tag length */
+	if (!(tag_new = dm_pool_alloc(p, strlen(tag)))) {

strlen(tag) + 1
(for '\0')

But better way is to use directly dm_pool_strdup() - and avoid whole _tag_copy() function.

Quoted 7 lines
lib/metadata/metadata.c +7 -0
+		log_error("Failed to alloc memory for tag %s.", tag);
+		return NULL;
+	}
+	strcpy(tag_new, tag);
+	return tag_new;
+}
+