Refactor vgremove and move orphan lock inside library, v2
cover Refactor vgremove and move orphan lock inside library, v2
The following 5 patches refactor vgremove and move the orphan lock inside a few library calls. After these patches we will have vgcreate, vgremove, and vgextend library calls. The last few patches also reorders VG orphan locks and VG name locks consistently throughout the code. Now VG orphan locks are obtained second. Previously in most cases they were obtained first, but in at least one case (vgreduce) they were second.
Passes testsuite and applies on:
commit 7e7274b002956a377d1f02c97d2ba96f4da1de59
Author: Alasdair Kergon <[email protected]> Date: Fri Jul 10 09:59:37 2009 +0000
Add dm_log_with_errno and dm_log_with_errno_init, deprecating the old Change plog to use dm_log_with_errno unless deprecated dm_log_init was
used.
Rename plog macro to LOG_LINE and use in dm_dump_memory_debug.
PATCH 1/5 Remove unnecessary parameters from vg_remove_single().
Use vg_t instead of struct volume_group.
Should be no functional change.
--- lib/metadata/metadata-exported.h | 4 +--- lib/metadata/metadata.c | 27 ++++++++++++--------------- tools/vgremove.c | 3 +-- 3 files changed, 14 insertions(+), 20 deletions(-)
lib/metadata/metadata-exported.h +1 -3
@@ -425,9 +425,7 @@ uint32_t pv_list_extents_free(const struct dm_list *pvh);vg_t *vg_create (struct cmd_context *cmd,const char *vg_name);int vg_remove (struct volume_group *vg); -int vg_remove_single (struct cmd_context *cmd,const char *vg_name, -struct volume_group *vg, -force_t force); +int vg_remove_single (vg_t *vg,force_t force);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);
lib/metadata/metadata.c +12 -15
@@ -359,10 +359,7 @@ static int remove_lvs_in_vg(struct cmd_context *cmd,return 1 ; } -/* FIXME: remove redundant vg_name */ -int vg_remove_single (struct cmd_context *cmd,const char *vg_name, -struct volume_group *vg, -force_t force __attribute((unused))) +int vg_remove_single (vg_t *vg,force_t force) {struct physical_volume *pv;struct pv_list *pvl;
@@ -371,7 +368,7 @@ int vg_remove_single(struct cmd_context *cmd, const char *vg_name,if (vg_read_error (vg) ||vg_missing_pv_count (vg)) {log_error ("Volume group \"%s\" not found, is inconsistent " -"or has PVs missing." , vg_name); +"or has PVs missing." , vg ? vg->name :"" );log_error ("Consider vgreduce --removemissing if metadata " "is inconsistent." );return 0 ;
@@ -387,11 +384,11 @@ int vg_remove_single(struct cmd_context *cmd, const char *vg_name, (yes_no_prompt("Do you really want to remove volume " "group \"%s\" containing %u " "logical volumes? [y/n]: ", - vg_name, lv_count) == 'n')) { + vg->name, lv_count) == 'n')) {log_print ("Volume group \"%s\" not removed" , vg_name);return 0 ; } -if (!remove_lvs_in_vg (cmd, vg, force)) +if (!remove_lvs_in_vg (vg->cmd, vg, force))return 0 ; }
@@ -399,7 +396,7 @@ int vg_remove_single(struct cmd_context *cmd, const char *vg_name,if (lv_count) {log_error ("Volume group \"%s\" still contains %u " -"logical volume(s)" , vg_name, lv_count); +"logical volume(s)" , vg->name, lv_count);return 0 ; }
@@ -407,7 +404,7 @@ int vg_remove_single(struct cmd_context *cmd, const char *vg_name,return 0 ;if (!vg_remove (vg)) { -log_error ("vg_remove %s failed" , vg_name); +log_error ("vg_remove %s failed" , vg->name);return 0 ; }
@@ -415,7 +412,7 @@ int vg_remove_single(struct cmd_context *cmd, const char *vg_name, dm_list_iterate_items(pvl, &vg->pvs) { pv = pvl->pv;log_verbose ("Removing physical volume \"%s\" from " -"volume group \"%s\"" ,pv_dev_name (pv), vg_name); +"volume group \"%s\"" ,pv_dev_name (pv), vg->name); pv->vg_name = vg->fid->fmt->orphan_vg_name; pv->status =ALLOCATABLE_PV ;
@@ -426,20 +423,20 @@ int vg_remove_single(struct cmd_context *cmd, const char *vg_name, }/* FIXME Write to same sector label was read from */ -if (!pv_write (cmd, pv,NULL ,INT64_C (-1 ))) { +if (!pv_write (vg->cmd, pv,NULL ,INT64_C (-1 ))) {log_error ("Failed to remove physical volume \"%s\"" " from volume group \"%s\"" , -pv_dev_name (pv), vg_name); +pv_dev_name (pv), vg->name); ret =0 ; } } -backup_remove (cmd, vg_name); +backup_remove (vg->cmd, vg->name);if (ret) -log_print ("Volume group \"%s\" successfully removed" , vg_name); +log_print ("Volume group \"%s\" successfully removed" , vg->name);else -log_error ("Volume group \"%s\" not properly removed" , vg_name); +log_error ("Volume group \"%s\" not properly removed" , vg->name);return ret; }
tools/vgremove.c +1 -2
@@ -19,8 +19,7 @@ static int vgremove_single(struct cmd_context *cmd, const char *vg_name,struct volume_group *vg, void *handle __attribute((unused))) { -if (!vg_remove_single (cmd, vg_name, vg, -arg_count (cmd, force_ARG))) +if (!vg_remove_single (vg,arg_count (cmd, force_ARG)))return ECMD_FAILED ;return ECMD_PROCESSED ;
Signature · 1 line
1.6.0.6
PATCH 2/5 Remove force parameter from vg_remove_single, now the liblvm function.
Move check for active LVs outside of library function. The vgremove liblvm function function will fail if there are active LVs. It will be the application's responsibility to check this condition and remove the LVs individually before calling vgremove. Note also that we've duplicated the EXPORTED_VG check in vgremove_single (tools) and vg_remove_single (library). Duplication seemed the only option here since we don't want to do the automatic removal of LVs (in the tools) if the vg is exported, and we still need to protect the library call from removal if the vg is exported.
We still need to deal with the ORPHAN lock but vg_remove_single is now very close to our liblvm function.
TODO: Refactor lvremove in a similar way.
--- lib/metadata/metadata-exported.h | 6 +++++- lib/metadata/metadata.c | 23 ++++------------------- tools/vgremove.c | 24 +++++++++++++++++++++++- 3 files changed, 32 insertions(+), 21 deletions(-)
lib/metadata/metadata-exported.h +5 -1
@@ -425,7 +425,7 @@ uint32_t pv_list_extents_free(const struct dm_list *pvh);vg_t *vg_create (struct cmd_context *cmd,const char *vg_name);int vg_remove (struct volume_group *vg); -int vg_remove_single (vg_t *vg,force_t force); +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);
@@ -436,6 +436,10 @@ int vg_set_alloc_policy(vg_t *vg, alloc_policy_t alloc);int vg_split_mdas (struct cmd_context *cmd,struct volume_group *vg_from,struct volume_group *vg_to); +/* FIXME: refactor / unexport when lvremove liblvm refactoring dones */ +int remove_lvs_in_vg (struct cmd_context *cmd, +struct volume_group *vg, +force_t force);/* * vg_release() must be called on every struct volume_group allocated * by vg_create() or vg_read_internal() to free it when no longer required.
lib/metadata/metadata.c +4 -19
@@ -343,9 +343,9 @@ int vg_rename(struct cmd_context *cmd, struct volume_group *vg,return 1 ; } -static int remove_lvs_in_vg (struct cmd_context *cmd, -struct volume_group *vg, -force_t force) +int remove_lvs_in_vg (struct cmd_context *cmd, +struct volume_group *vg, +force_t force) {struct dm_list *lst;struct lv_list *lvl;
@@ -359,7 +359,7 @@ static int remove_lvs_in_vg(struct cmd_context *cmd,return 1 ; } -int vg_remove_single (vg_t *vg,force_t force) +int vg_remove_single (vg_t *vg) {struct physical_volume *pv;struct pv_list *pvl;
@@ -380,21 +380,6 @@ int vg_remove_single(vg_t *vg, force_t force) lv_count =vg_visible_lvs (vg);if (lv_count) { -if ((force ==PROMPT ) && - (yes_no_prompt ("Do you really want to remove volume " -"group \"%s\" containing %u " -"logical volumes? [y/n]: " , - vg->name, lv_count) =='n' )) { -log_print ("Volume group \"%s\" not removed" , vg_name); -return 0 ; - } -if (!remove_lvs_in_vg (vg->cmd, vg, force)) -return 0 ; - } - - lv_count =vg_visible_lvs (vg); - -if (lv_count) {log_error ("Volume group \"%s\" still contains %u " "logical volume(s)" , vg->name, lv_count);return 0 ;
tools/vgremove.c +23 -1
@@ -19,7 +19,29 @@ static int vgremove_single(struct cmd_context *cmd, const char *vg_name,struct volume_group *vg, void *handle __attribute((unused))) { -if (!vg_remove_single (vg,arg_count (cmd, force_ARG))) +unsigned lv_count; +force_t force; + +if (!vg_check_status (vg,EXPORTED_VG )) +return ECMD_FAILED ; + + lv_count =vg_visible_lvs (vg); + + force =arg_count (cmd, force_ARG); +if (lv_count) { +if ((force ==PROMPT ) && + (yes_no_prompt ("Do you really want to remove volume " +"group \"%s\" containing %u " +"logical volumes? [y/n]: " , + vg_name, lv_count) =='n' )) { +log_print ("Volume group \"%s\" not removed" , vg_name); +return ECMD_FAILED ; + } +if (!remove_lvs_in_vg (cmd, vg, force)) +return ECMD_FAILED ; + } + +if (!vg_remove_single (vg))return ECMD_FAILED ;return ECMD_PROCESSED ;
Signature · 1 line
1.6.0.6
PATCH 3/5 Move orphan lock inside vg_remove_single.
Move the vg orphan lock inside vg_remove_single, now a complete liblvm function. Note that this changes the order of the locks - originally VG_ORPHAN was obtained first, then the vgname lock. With the current policy of non-blocking second locks, this could mean we get a failure obtaining the orphan lock. In the case of a vg with lvs being removed, this could result in the lvs being removed but not the vg. Such a scenario could have happened prior though with a different failure. Other tools were examined for side-effects, and no major problems were noted.
--- lib/metadata/metadata.c | 7 +++++++ tools/vgremove.c | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-)
lib/metadata/metadata.c +7 -0
@@ -388,8 +388,14 @@ int vg_remove_single(vg_t *vg)if (!archive (vg))return 0 ; +if (!lock_vol (vg->cmd,VG_ORPHANS ,LCK_VG_WRITE )) { +log_error ("Can't get lock for orphan PVs" ); +return 0 ; + } +if (!vg_remove (vg)) {log_error ("vg_remove %s failed" , vg->name); +unlock_vg (vg->cmd,VG_ORPHANS );return 0 ; }
@@ -423,6 +429,7 @@ int vg_remove_single(vg_t *vg)else log_error ("Volume group \"%s\" not properly removed" , vg->name); +unlock_vg (vg->cmd,VG_ORPHANS );return ret; }
tools/vgremove.c +0 -7
@@ -56,16 +56,9 @@ int vgremove(struct cmd_context *cmd, int argc, char **argv)return EINVALID_CMD_LINE ; } -if (!lock_vol (cmd,VG_ORPHANS ,LCK_VG_WRITE )) { -log_error ("Can't get lock for orphan PVs" ); -return ECMD_FAILED ; - } - ret =process_each_vg (cmd, argc, argv,READ_FOR_UPDATE ,NULL , &vgremove_single); -unlock_vg (cmd,VG_ORPHANS ); -return ret; }
Signature · 1 line
1.6.0.6
PATCH 4/5 Move orphan lock obtain/release inside vg_extend().
With this change we now have vgcreate/vgextend liblvm functions. Note that this changes the lock order of the following functions as the orphan lock is now obtained first. With our policy of non-blocking second locks, this should not be a problem.
--- lib/metadata/metadata.c | 11 +++++++++-- tools/vgcreate.c | 8 -------- tools/vgextend.c | 7 ------- 3 files changed, 9 insertions(+), 17 deletions(-)
lib/metadata/metadata.c +9 -2
@@ -437,6 +437,12 @@ int vg_extend(struct volume_group *vg, int pv_count, char **pv_names) {int i;struct physical_volume *pv; +struct cmd_context *cmd = vg->cmd; + +if (!lock_vol (cmd,VG_ORPHANS ,LCK_VG_WRITE )) { +log_error ("Can't get lock for orphan PVs" ); +return 0 ; + }/* attach each pv */ for (i =0 ; i < pv_count; i++) {
@@ -445,18 +451,19 @@ int vg_extend(struct volume_group *vg, int pv_count, char **pv_names) "physical volume", pv_names[i]); goto bad; } -if (!add_pv_to_vg (vg, pv_names[i], pv)) goto bad; }/* FIXME Decide whether to initialise and add new mdahs to format instance */ +unlock_vg (cmd,VG_ORPHANS );return 1 ; - + bad:log_error ("Unable to add physical volume '%s' to " "volume group '%s'." , pv_names[i], vg->name); +unlock_vg (cmd,VG_ORPHANS );return 0 ; }
tools/vgcreate.c +0 -8
@@ -46,12 +46,6 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv)if (validate_vg_create_params (cmd, &vp_new))return EINVALID_CMD_LINE ; -/* FIXME: orphan lock needs tied to vg handle or inside library call */ -if (!lock_vol (cmd,VG_ORPHANS ,LCK_VG_WRITE )) { -log_error ("Can't get lock for orphan PVs" ); -return ECMD_FAILED ; - } -/* Create the new VG */ vg =vg_create (cmd, vp_new.vg_name);if (vg_read_error (vg))
@@ -113,7 +107,6 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv) }unlock_vg (cmd, vp_new.vg_name); -unlock_vg (cmd,VG_ORPHANS );backup (vg);
@@ -126,6 +119,5 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv) bad:vg_release (vg);unlock_vg (cmd, vp_new.vg_name); -unlock_vg (cmd,VG_ORPHANS );return ECMD_FAILED ; }
tools/vgextend.c +0 -7
@@ -36,17 +36,11 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv) argc--; argv++; -if (!lock_vol (cmd,VG_ORPHANS ,LCK_VG_WRITE )) { -log_error ("Can't get lock for orphan PVs" ); -return ECMD_FAILED ; - } -log_verbose ("Checking for volume group \"%s\"" , vg_name); vg =vg_read_for_update (cmd, vg_name,NULL ,READ_REQUIRE_RESIZEABLE );if (vg_read_error (vg)) {vg_release (vg); -unlock_vg (cmd,VG_ORPHANS );return ECMD_FAILED ; }/********** FIXME
@@ -77,6 +71,5 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv) error:unlock_and_release_vg (cmd, vg, vg_name); -unlock_vg (cmd,VG_ORPHANS );return r; }
Signature · 1 line
1.6.0.6
PATCH 5/5 Change lock ordering of vgcfgrestore to be consistent with other tools.
Orphan lock is now obtained second and released first.
--- tools/vgcfgrestore.c | 12 ++++++------ 1 files changed, 6 insertions(+), 6 deletions(-)
tools/vgcfgrestore.c +6 -6
@@ -43,17 +43,17 @@ int vgcfgrestore(struct cmd_context *cmd, int argc, char **argv)return ECMD_PROCESSED ; } -if (!lock_vol (cmd,VG_ORPHANS ,LCK_VG_WRITE )) { -log_error ("Unable to lock orphans" ); -return ECMD_FAILED ; - } -if (!lock_vol (cmd, vg_name,LCK_VG_WRITE )) {log_error ("Unable to lock volume group %s" , vg_name);unlock_vg (cmd,VG_ORPHANS );return ECMD_FAILED ; } +if (!lock_vol (cmd,VG_ORPHANS ,LCK_VG_WRITE )) { +log_error ("Unable to lock orphans" ); +return ECMD_FAILED ; + } + if (!(arg_count(cmd, file_ARG) ? backup_restore_from_file(cmd, vg_name, arg_str_value(cmd, file_ARG, "")) :
@@ -66,7 +66,7 @@ int vgcfgrestore(struct cmd_context *cmd, int argc, char **argv)log_print ("Restored volume group %s" , vg_name); -unlock_vg (cmd, vg_name);unlock_vg (cmd,VG_ORPHANS ); +unlock_vg (cmd, vg_name);return ECMD_PROCESSED ; }
Signature · 1 line
1.6.0.6