From 1bd72d90a4e9dac6c96618a4a69ce8c5d7fd0b02 Mon Sep 17 00:00:00 2001 From: Dave Wysochanski Date: Mon, 27 Jul 2009 17:43:39 +0000 Subject: [PATCH] Add vg_reduce to metadata.c and metadata-exported.h This function behaves a little bit different than vg_reduce_single, because it allowes to remove even the latest pv. This has been done to be consistent to lvm_vg_create, which creates an empty vg. removed_pvs has been added to the volume_group struct. vg_reduce adds remove pvs to this list to be able to commit the changes for the pvs in lvm_vg_comm in liblvm2app. Initialize removed_pvs list in format-specific volume_group constructors. Ideally, we should have a base constructor here that initializes the general non-format specific members of struct volume_group. But until then, there are multiple places to initialize these members. Maybe a better patch would be a base constructor patch for struct volume_group. That is more work though. Signed-off-by: Dave Wysochanski Signed-off-by: Thomas Woerner Author: Dave Wysochanski --- lib/format1/format1.c | 1 + lib/format_pool/format_pool.c | 1 + lib/format_text/import_vsn1.c | 1 + lib/metadata/metadata-exported.h | 7 +++++ lib/metadata/metadata.c | 52 ++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+) diff --git a/lib/format1/format1.c b/lib/format1/format1.c index c8f95191c..aa1483af1 100644 --- a/lib/format1/format1.c +++ b/lib/format1/format1.c @@ -132,6 +132,7 @@ static struct volume_group *_build_vg(struct format_instance *fid, dm_list_init(&vg->pvs); dm_list_init(&vg->lvs); dm_list_init(&vg->tags); + dm_list_init(&vg->removed_pvs); if (!_check_vgs(pvs)) goto_bad; diff --git a/lib/format_pool/format_pool.c b/lib/format_pool/format_pool.c index 3f31bba3b..6f7e4b432 100644 --- a/lib/format_pool/format_pool.c +++ b/lib/format_pool/format_pool.c @@ -124,6 +124,7 @@ static struct volume_group *_build_vg_from_pds(struct format_instance dm_list_init(&vg->pvs); dm_list_init(&vg->lvs); dm_list_init(&vg->tags); + dm_list_init(&vg->removed_pvs); if (!import_pool_vg(vg, smem, pds)) return_NULL; diff --git a/lib/format_text/import_vsn1.c b/lib/format_text/import_vsn1.c index 629310a07..3c416757b 100644 --- a/lib/format_text/import_vsn1.c +++ b/lib/format_text/import_vsn1.c @@ -753,6 +753,7 @@ static struct volume_group *_read_vg(struct format_instance *fid, dm_list_init(&vg->lvs); dm_list_init(&vg->tags); + dm_list_init(&vg->removed_pvs); /* Optional tags */ if ((cn = find_config_node(vgn, "tags")) && diff --git a/lib/metadata/metadata-exported.h b/lib/metadata/metadata-exported.h index 142381b9c..bc946da65 100644 --- a/lib/metadata/metadata-exported.h +++ b/lib/metadata/metadata-exported.h @@ -242,6 +242,12 @@ struct volume_group { * FIXME: Split these out. */ + /* + * List of removed physical volumes by pvreduce. + * They have to get cleared on vg_commit. + */ + struct dm_list removed_pvs; + /* * Store result of the last vg_read(). * 0 for success else appropriate FAILURE_* bits set. @@ -437,6 +443,7 @@ int vg_remove_single(vg_t *vg); int vg_rename(struct cmd_context *cmd, struct volume_group *vg, const char *new_name); int vg_extend(struct volume_group *vg, int pv_count, char **pv_names); +int vg_reduce(struct volume_group *vg, char *pv_name); int vg_set_extent_size(vg_t *vg, uint32_t new_extent_size); int vg_set_max_lv(vg_t *vg, uint32_t max_lv); int vg_set_max_pv(vg_t *vg, uint32_t max_pv); diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c index 838add833..03e773015 100644 --- a/lib/metadata/metadata.c +++ b/lib/metadata/metadata.c @@ -532,6 +532,54 @@ int vg_extend(struct volume_group *vg, int pv_count, char **pv_names) return 0; } +/* FIXME: use this inside vgreduce_single? */ +int vg_reduce(struct volume_group *vg, char *pv_name) +{ + struct physical_volume *pv; + struct pv_list *pvl; + + if (_vg_bad_status_bits(vg, RESIZEABLE_VG)) + return 0; + + if (!archive(vg)) + goto bad; + + /* remove each pv */ + if (!(pvl = find_pv_in_vg(vg, pv_name))) { + log_error("Physical volume %s not in volume group %s.", + pv_name, vg->name); + goto bad; + } + + pv = pvl->pv; + + if (pv_pe_alloc_count(pv)) { + log_error("Physical volume %s still in use.", + pv_name); + goto bad; + } + + if (!dev_get_size(pv_dev(pv), &pv->size)) { + log_error("%s: Couldn't get size.", pv_name); + goto bad; + } + + vg->pv_count--; + vg->free_count -= pv_pe_count(pv) - pv_pe_alloc_count(pv); + vg->extent_count -= pv_pe_count(pv); + + /* add pv to the remove_pvs list */ + dm_list_del(&pvl->list); + dm_list_add(&vg->removed_pvs, &pvl->list); + + return 1; + + bad: + log_error("Unable to remove physical volume '%s' from " + "volume group '%s'.", pv_name, vg->name); + return 0; +} + const char *strip_dir(const char *vg_name, const char *dev_dir) { size_t len = strlen(dev_dir); @@ -660,6 +708,9 @@ vg_t *vg_create(struct cmd_context *cmd, const char *vg_name) dm_list_init(&vg->tags); + /* initialize removed_pvs list */ + dm_list_init(&vg->removed_pvs); + if (!(vg->fid = cmd->fmt->ops->create_instance(cmd->fmt, vg_name, NULL, NULL))) { log_error("Failed to create format instance"); @@ -2165,6 +2216,7 @@ static struct volume_group *_vg_read_orphans(struct cmd_context *cmd, dm_list_init(&vg->pvs); dm_list_init(&vg->lvs); dm_list_init(&vg->tags); + dm_list_init(&vg->removed_pvs); vg->vgmem = mem; vg->cmd = cmd; if (!(vg->name = dm_pool_strdup(mem, orphan_vgname))) {