LVM2/lib/metadata lv_manip.c
CVSROOT: /cvs/lvm2
Module name: LVM2
Changes by: wysochanski at sourceware.org 2007-08-06 20:35:48
Modified files:
lib/metadata : lv_manip.c
Log message:
Add support for renaming mirrored LVs. Patch by Jun'ichi Nomura <[email protected]>
lib/metadata/lv_manip.c +134 -8
@@ -1461,6 +1461,126 @@ }/* +* Minimal LV renaming function. +* Metadata transaction should be made by caller. +* Assumes new_name is allocated from cmd->mem pool. +*/ +static int _rename_single_lv (struct logical_volume *lv,char *new_name) +{ +struct volume_group *vg = lv->vg; + +if (find_lv_in_vg (vg, new_name)) { +log_error ("Logical volume \"%s\" already exists in " +"volume group \"%s\"" , new_name, vg->name); + return_0; + } + +if (lv->status &LOCKED ) { +log_error ("Cannot rename locked LV %s" , lv->name); + return_0; + } + + lv->name = new_name; + +return 1 ; +} + +/* +* Returns a pointer to LV name suffix. +* Returns NULL if the LV doesn't have suffix. +*/ +static char *sub_lv_name_suffix (const char *lvname) +{ +char *s; + +if ((s =strstr (lvname,"_mimage" ))) +return s; + +if ((s =strstr (lvname,"_mlog" ))) +return s; + +return NULL ; +} + +/* +* Rename sub LV. +* Returns 0 on failure, 1 on success. +* If a new name for the sub LV cannot be determined, 1 is returned. +* 'lv_main_old' and 'lv_main_new' are old and new names of the main LV. +*/ +static int _rename_sub_lv (struct cmd_context *cmd, +struct logical_volume *lv, +char *lv_main_old,char *lv_main_new) +{ +char *suffix, *new_name; +size_t l; + +/* Rename only if the lv has known suffix */ +if (!(suffix =sub_lv_name_suffix (lv->name))) +return 1 ; + +/* Make sure that lv->name is exactly a lv_main_old + suffix */ + l = suffix - lv->name; +if (strlen (lv_main_old) != l ||strncmp (lv->name, lv_main_old, l)) +return 1 ; + +/* +* Compose a new name for sub lv: +* e.g. new name is "lvol1_mlog" +* if the sub LV is "lvol0_mlog" and +* a new name for main LV is "lvol1" +*/ + l =strlen (lv_main_new) +strlen (suffix) +1 ; + new_name =dm_pool_alloc (cmd->mem, l); +if (!new_name) { +log_error ("Failed to allocate space for new name" ); + return_0; + } +if (!dm_snprintf (new_name, l,"%s%s" , lv_main_new, suffix)) { +log_error ("Failed to create new name" ); + return_0; + } + +/* Rename it */ +return _rename_single_lv (lv, new_name); +} + +/* Callback for _for_each_sub_lv */ +static int _rename_cb (struct cmd_context *cmd,struct logical_volume *lv, +void *data) +{ +char **names = (char **) data; + +return _rename_sub_lv (cmd, lv, names[0 ], names[1 ]); +} + +/* +* Loop down sub LVs and call "func" for each. +* "func" is responsible to log necessary information on failure. +*/ +static int _for_each_sub_lv (struct cmd_context *cmd,struct logical_volume *lv, +int (*func)(struct cmd_context *cmd, +struct logical_volume *lv, +void *data), +void *data) +{ +struct lv_segment *seg; +int s; + + list_iterate_items(seg, &lv->segments) { +if (seg->log_lv && !func (cmd, seg->log_lv, data)) + return_0; +for (s =0 ; s < seg->area_count; s++) +if (seg_type (seg, s) ==AREA_LV && + !func (cmd,seg_lv (seg, s), data)) + return_0; + } + +return 1 ; +} + + +/* * Core of LV renaming routine. * VG must be locked by caller. * Returns 0 on failure, 1 on success.
@@ -1469,6 +1589,13 @@ const char *new_name) {struct volume_group *vg = lv->vg; +const char *names[2 ]; + +/* rename is not allowed on sub LVs */ +if ((lv->status &MIRROR_LOG ) || (lv->status &MIRROR_IMAGE )) { +log_error ("Cannot rename hidden LV \"%s\"." , lv->name); +return 0 ; + }if (find_lv_in_vg (vg, new_name)) { log_error("Logical volume \"%s\" already exists in "
@@ -1481,17 +1608,16 @@return 0 ; } -if ((lv->status &MIRRORED ) || - (lv->status &MIRROR_LOG ) || - (lv->status &MIRROR_IMAGE )) { -log_error ("Mirrored LV, \"%s\" cannot be renamed: %s" , - lv->name,strerror (ENOSYS )); -return 0 ; - } -if (!archive (vg)) return_0; +/* rename sub LVs */ + names[0 ] = lv->name; + names[1 ] = new_name; +if (!_for_each_sub_lv (cmd, lv, _rename_cb, (void *) names)) +return 0 ; + +/* rename main LV */ if (!(lv->name =dm_pool_strdup (cmd->mem, new_name))) {log_error ("Failed to allocate space for new name" );return 0 ;
Quoted 1 line · wysochanski at sourceware.org
Add support for renaming mirrored LVs.
A patch missing accompanying discussion and analysis.
Did you test this in a cluster?
That's a pre-requisite before this code forms part of any release.
Quoted 1 line
+static int
_rename_single_lv(struct logical_volume *lv, char *new_name)
Should not be _ there. (Error repeated throughout patch.) You only need that when it does not follow an explicit log message.
Quoted 1 line
+static char *
sub_lv_name_suffix(const char *lvname)
Remove space after *.
Add _ prefix (static).
[And I don't quite understand the need for this function, and don't want to see _mimage etc. hard-coded there.]
Quoted 1 line
+ * Returns 0 on failure, 1 on success.
Superfluous comment - it's the default.
Quoted 1 line
+ * If a new name for the sub LV cannot be determined, 1 is returned.
How can that happen? Why return success if it does?
Quoted 1 line
+ * '
lv_main_old' and 'lv_main_new' are old and new names of the main LV.
So 'sub_lv' is new terminology you're introducing to describe any LV that is used by any segment of another 'main' LV ?
Currently we have 'toplevel' and 'visible'.
I don't think we need to add 'main'.
I'll ponder what value comes from introducing 'sub_lv'.
And 'lv' suggests struct logical_volume * - but these are const char * so just call then lv_name_old and lv_name_new.
lib/metadata/lv_manip.c line 1516
Quoted 3 lines
lib/metadata/lv_manip.c +2
-0+static int _rename_sub_lv (struct cmd_context *cmd, + size_t l;
Use a more-descriptive name.
lib/metadata/lv_manip.c line 1525
Quoted 7 lines
lib/metadata/lv_manip.c +7
-0+/* Rename only if the lv has known suffix */ +if (!(suffix =sub_lv_name_suffix (lv->name))) +return 1 ; +/* Make sure that lv->name is exactly a lv_main_old + suffix */ + l = suffix - lv->name; +if (strlen (lv_main_old) != l ||strncmp (lv->name, lv_main_old, l)) +return 1 ;
The logic here seems a bit twisted. In what circumstances would those tests fail and why treat this as success? And if you are going to validate the names, then do it properly e.g. why proceed if you find a mirror log with a suffix 'mimage'?
Today, they should never fail unless there's been a coding bug (or someone's edited the metadata making it invalid). Any checks should be in vg_validate. It's inconsistent to, in one place, assume 'mimage' was put there by the tools (there's no check on what follows 'mimage') and is a valid name, yet in another place if an invalid name is encountered, return success.
These are internal LVs with naming under the complete control of LVM2. If the names became wrong somehow, then issue a log_error by all means but then correct the names!
Or maybe this is to support future enhancements? But we haven't yet discussed how to name internal LVs when they are stacked.
One simple way out perhaps?
Check if it begins with lv_name_old: if so, replace that part of the string with lv_name_new; if not, log_error and make the entire rename fail. (Add complete name validation to vg_validate, if you wish.)
lib/metadata/lv_manip.c line 1596
Quoted 1 line
lib/metadata/lv_manip.c +1
-0+log_error ("Cannot rename hidden LV \"%s\"." , lv->name);
New terminology again - please resist and discuss first! ('Internal LV' has been used already. We might be able to improve on that, but we should try to avoid using different terminology in different places.)
lib/metadata/lv_manip.c line 1616
Quoted 3 lines
lib/metadata/lv_manip.c +3
-0+/* rename sub LVs */ + names[0 ] = lv->name; + names[1 ] = new_name;
How do I work out what '0' and '1' mean there? Please avoid hiding things like that. Use a struct there (or enum).
Alasdair
Signature · 1 line
agk at redhat.com
Quoted 1 line · Alasdair G Kergon
Should not be _ there. (Error repeated throughout patch.)
was referring to occurences of 'return_0' instead of 'return 0'
Alasdair
Signature · 1 line
agk at redhat.com
Dave,
thanks a lot for rapidly applying fixes for Alasdair's comments.
Alasdair,
as for the comments about extra checks in _rename_sub_lv(), they were to allow future enhancements like stacked LV, having LVs with independent names as sub LVs, not only the internal ones. But, for now, the checks are not necessary unless metadata is broken. Also we can seek for other solutions like marking independently named LV as you mentioned.
So, I'm ok with this:
Quoted 4 lines
One simple way out perhaps?
Check if it begins withlv_name_old: if so, replace that part of the string withlv_name_new; if not,log_errorand make the entire rename fail. (Add complete name validation tovg_validate, if you wish.)
A patch is attached.
Thanks,
Signature · 8 lines
Jun'ichi Nomura, NEC Corporation of America -------------- next part -------------- A non-text attachment was scrubbed... Name: lvm2-lvrename-simplify-sublv-name-check.patch Type: text/x-patch Size: 1675 bytes Desc: not available URL: <http://listman.redhat.com/archives/lvm-devel/attachments/20070807/07c00a58/attachment.bin>