Skip to contentSkip to conversation

master - lvresize: separate validation from action

PATCH Alasdair Kergon 1 participant 1 message Jul 6, 2013

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=7c6526aae277c049aa4a6e41b7c2b30d366c9852
Commit:        7c6526aae277c049aa4a6e41b7c2b30d366c9852
Parent:        a64239f225463d418dd4ccb931ceda88535fc6e2
Author:        Alasdair G Kergon <[email protected]>
AuthorDate:    Sat Jul 6 03:28:21 2013 +0100
Committer:     Alasdair G Kergon <[email protected]>
CommitterDate: Sat Jul 6 03:28:21 2013 +0100

lvresize: separate validation from action

Start separating the validation from the action in the basic lvresize code moved to the library.
Remove incorrect use of command line error codes from lvresize library functions. Move errors.h to tools directory to reinforce this, exporting public versions of the error codes in lvm2cmd.h for dmeventd plugins to use.

---
 WHATS_NEW                                          |    2 +
 daemons/dmeventd/plugins/mirror/dmeventd_mirror.c  |    5 +-
 daemons/dmeventd/plugins/raid/dmeventd_raid.c      |    5 +-
 .../dmeventd/plugins/snapshot/dmeventd_snapshot.c  |    3 +-
 daemons/dmeventd/plugins/thin/dmeventd_thin.c      |    3 +-
 include/.symlinks.in                               |    1 -
 lib/commands/errors.h                              |   26 --
 lib/metadata/lv_manip.c                            |  265 ++++++++++++--------
 lib/metadata/metadata-exported.h                   |   10 +-
 lib/metadata/pv_manip.c                            |   29 ++-
 liblvm/lvm_lv.c                                    |   14 +-
 liblvm/lvm_pv.c                                    |    4 +-
 tools/errors.h                                     |   26 ++
 tools/lvm2cmd.h                                    |    6 +
 tools/lvresize.c                                   |   28 ++-
 tools/pvcreate.c                                   |    3 +-
 tools/pvremove.c                                   |    6 +-
 17 files changed, 261 insertions(+), 175 deletions(-)
WHATS_NEW +2 -0
index b174cb3..e389a36 100644
@@ -1,5 +1,7 @@
 Version 2.02.99 - 
 ===================================
+  Define LVM2_* command errors in lvm2cmd.h and use in dmeventd plugins.
+  Move errors.h to tools dir.
   Add man page entries for profile configuration and related options.
   Improve error loging when user tries to interrupt commands.
   Rename _swap_lv to _swap_lv_identifiers and move to allow an additional user.
daemons/dmeventd/plugins/mirror/dmeventd_mirror.c +2 -3
index e59feb4..864f0e2 100644
@@ -15,7 +15,6 @@
 #include "lib.h"
 
 #include "lvm2cmd.h"
-#include "errors.h"
 #include "libdevmapper-event.h"
 #include "dmeventd_lvm.h"
 #include "defaults.h"
@@ -145,9 +144,9 @@ static int _remove_failed_devices(const char *device)
 	r = dmeventd_lvm2_run(cmd_str);
 
 	syslog(LOG_INFO, "Repair of mirrored device %s %s.", device,
-	       (r == ECMD_PROCESSED) ? "finished successfully" : "failed");
+	       (r == LVM2_COMMAND_SUCCEEDED) ? "finished successfully" : "failed");
 
-	return (r == ECMD_PROCESSED) ? 0 : -1;
+	return (r == LVM2_COMMAND_SUCCEEDED) ? 0 : -1;
 }
 
 void process_event(struct dm_task *dmt,
daemons/dmeventd/plugins/raid/dmeventd_raid.c +2 -3
index a3ecdc1..9da2e7c 100644
@@ -15,7 +15,6 @@
 #include "lib.h"
 
 #include "lvm2cmd.h"
-#include "errors.h"
 #include "libdevmapper-event.h"
 #include "dmeventd_lvm.h"
 
@@ -41,10 +40,10 @@ static int run_repair(const char *device)
 
 	r = dmeventd_lvm2_run(cmd_str);
 
-	if (r != ECMD_PROCESSED)
+	if (r != LVM2_COMMAND_SUCCEEDED)
 		syslog(LOG_INFO, "Repair of RAID device %s failed.", device);
 
-	return (r == ECMD_PROCESSED) ? 0 : -1;
+	return (r == LVM2_COMMAND_SUCCEEDED) ? 0 : -1;
 }
 
 static int _process_raid_event(char *params, const char *device)
daemons/dmeventd/plugins/snapshot/dmeventd_snapshot.c +1 -2
index 98dd1b4..c4b69e2 100644
@@ -15,7 +15,6 @@
 #include "lib.h"
 
 #include "lvm2cmd.h"
-#include "errors.h"
 #include "libdevmapper-event.h"
 #include "dmeventd_lvm.h"
 
@@ -82,7 +81,7 @@ static int _run(const char *cmd, ...)
 
 static int _extend(const char *cmd)
 {
-	return dmeventd_lvm2_run(cmd) == ECMD_PROCESSED;
+	return dmeventd_lvm2_run(cmd) == LVM2_COMMAND_SUCCEEDED;
 }
 
 static void _umount(const char *device, int major, int minor)
daemons/dmeventd/plugins/thin/dmeventd_thin.c +1 -2
index f32424ad..8b173f1 100644
@@ -15,7 +15,6 @@
 #include "lib.h"
 
 #include "lvm2cmd.h"
-#include "errors.h"
 #include "libdevmapper-event.h"
 #include "dmeventd_lvm.h"
 
@@ -147,7 +146,7 @@ static int _extend(struct dso_state *state)
 #if THIN_DEBUG
 	syslog(LOG_INFO, "dmeventd executes: %s.\n", state->cmd_str);
 #endif
-	return (dmeventd_lvm2_run(state->cmd_str) == ECMD_PROCESSED);
+	return (dmeventd_lvm2_run(state->cmd_str) == LVM2_COMMAND_SUCCEEDED);
 }
 
 static int _run(const char *cmd, ...)
include/.symlinks.in +0 -1
index facadbe..7fdc045 100644
@@ -6,7 +6,6 @@
 @top_srcdir@/lib/activate/targets.h
 @top_srcdir@/lib/cache/lvmcache.h
 @top_srcdir@/lib/cache/lvmetad.h
- at top_srcdir@/lib/commands/errors.h
 @top_srcdir@/lib/commands/toolcontext.h
 @top_srcdir@/lib/config/config.h
 @top_srcdir@/lib/config/config_settings.h
lib/commands/errors.h deleted +0 -26
deleted file mode 100644
index a644fc7..0000000
@@ -1,26 +0,0 @@
-/*
- * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.  
- * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
- *
- * This file is part of LVM2.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License v.2.1.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#ifndef _LVM_ERRORS_H
-#define _LVM_ERRORS_H
-
-#define ECMD_PROCESSED		1
-#define ENO_SUCH_CMD		2
-#define EINVALID_CMD_LINE	3
-#define ECMD_FAILED		5
-
-/* FIXME Also returned by cmdlib. */
-
-#endif
lib/metadata/lv_manip.c +163 -102

537 lines not on this page · raw message

lib/metadata/metadata-exported.h +6 -4
index ee3ae66..412d29c 100644
@@ -26,7 +26,6 @@
 #include "vg.h"
 #include "lv.h"
 #include "lvm-percent.h"
-#include "errors.h"
 
 #define MAX_STRIPES 128U
 #define SECTOR_SHIFT 9L
@@ -466,6 +465,7 @@ struct lvresize_params {
 	int argc;
 	char **argv;
 
+	/* FIXME Deal with meaningless 'ac' */
 	/* Arg counts & values */
 	unsigned ac_policy;
 	unsigned ac_stripes;
@@ -474,7 +474,7 @@ struct lvresize_params {
 	uint32_t ac_mirrors_value;
 	unsigned ac_stripesize;
 	uint64_t ac_stripesize_value;
-	unsigned ac_alloc;
+	alloc_policy_t ac_alloc;
 	unsigned ac_no_sync;
 	unsigned ac_force;
 
@@ -532,8 +532,10 @@ int vgs_are_compatible(struct cmd_context *cmd,
 		       struct volume_group *vg_to);
 uint32_t vg_lock_newname(struct cmd_context *cmd, const char *vgname);
 
-int lv_resize(struct cmd_context *cmd, struct volume_group *vg,
-		     struct lvresize_params *lp, struct dm_list *pvh);
+int lv_resize_prepare(struct cmd_context *cmd, struct logical_volume *lv,
+		      struct lvresize_params *lp, struct dm_list *pvh);
+int lv_resize(struct cmd_context *cmd, struct logical_volume *lv,
+	      struct lvresize_params *lp, struct dm_list *pvh);
 
 /*
  * Return a handle to VG metadata.
lib/metadata/pv_manip.c +15 -14
index 5ab2d24..3a3ac09 100644
@@ -22,7 +22,7 @@
 #include "lvmetad.h"
 #include "display.h"
 #include "label.h"
-#include "../../tools/tools.h"
+#include "archiver.h"
 
 static struct pv_segment *_alloc_pv_segment(struct dm_pool *mem,
 					    struct physical_volume *pv,
@@ -739,15 +739,15 @@ bad:
 }
 
 int pvremove_single(struct cmd_context *cmd, const char *pv_name,
-			   void *handle __attribute__((unused)), unsigned force_count,
-			   unsigned prompt)
+		    void *handle __attribute__((unused)), unsigned force_count,
+		    unsigned prompt)
 {
 	struct device *dev;
-	int ret = ECMD_FAILED;
+	int r = 0;
 
 	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE, NULL)) {
 		log_error("Can't get lock for orphan PVs");
-		return ECMD_FAILED;
+		return 0;
 	}
 
 	if (!pvremove_check(cmd, pv_name, force_count, prompt))
@@ -778,30 +778,31 @@ int pvremove_single(struct cmd_context *cmd, const char *pv_name,
 	log_print_unless_silent("Labels on physical volume \"%s\" successfully wiped",
 				pv_name);
 
-	ret = ECMD_PROCESSED;
+	r = 1;
 
 out:
 	unlock_vg(cmd, VG_ORPHANS);
 
-	return ret;
+	return r;
 }
 
 int pvcreate_locked(struct cmd_context *cmd, const char *pv_name,
 		struct pvcreate_params *pp)
 {
-	int ret = ECMD_PROCESSED;
+	int r = 0;
 
 	if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE, NULL)) {
 		log_error("Can't get lock for orphan PVs");
-		return ECMD_FAILED;
+		return 0;
 	}
 
-	if (!(pvcreate_single(cmd, pv_name, pp, 1))) {
-		stack;
-		ret = ECMD_FAILED;
-	}
+	if (!(pvcreate_single(cmd, pv_name, pp, 1)))
+		goto_out;
+
+	r = 1;
 
+out:
 	unlock_vg(cmd, VG_ORPHANS);
 
-	return ret;
+	return r;
 }
liblvm/lvm_lv.c +11 -3
index 15bd813..4171baa 100644
@@ -22,6 +22,8 @@
 #include "lvm_misc.h"
 #include "lvm2app.h"
 
+/* FIXME Improve all the log messages to include context. Which VG/LV as a minimum? */
+
 struct lvm_lv_create_params
 {
 	uint32_t magic;
@@ -229,6 +231,7 @@ int lvm_lv_activate(lv_t lv)
 		log_verbose("Activating logical volume \"%s\" "
 			    "exclusively", lv->name);
 		if (!activate_lv_excl(lv->vg->cmd, lv)) {
+			/* FIXME Improve msg */
 			log_error("Activate exclusive failed.");
 			return -1;
 		}
@@ -236,6 +239,7 @@ int lvm_lv_activate(lv_t lv)
 		log_verbose("Activating logical volume \"%s\"",
 			    lv->name);
 		if (!activate_lv(lv->vg->cmd, lv)) {
+			/* FIXME Improve msg */
 			log_error("Activate failed.");
 			return -1;
 		}
@@ -320,7 +324,8 @@ lv_t lvm_lv_from_uuid(vg_t vg, const char *uuid)
 int lvm_lv_rename(lv_t lv, const char *new_name)
 {
 	if (!lv_rename(lv->vg->cmd, lv, new_name)) {
-		log_verbose("LV Rename failed.");
+		/* FIXME Improve msg */
+		log_error("LV rename failed.");
 		return -1;
 	}
 	return 0;
@@ -339,8 +344,11 @@ int lvm_lv_resize(const lv_t lv, uint64_t new_size)
 	lp.ac_force = 1;	/* Assume the user has a good backup? */
 	lp.sizeargs = 1;
 
-	if (ECMD_PROCESSED != lv_resize(lv->vg->cmd, lv->vg, &lp, &lv->vg->pvs)) {
-		log_verbose("LV Re-size failed.");
+	if (!lv_resize_prepare(lv->vg->cmd, lv, &lp, &lv->vg->pvs) ||
+	    !lv_resize(lv->vg->cmd, lv, &lp, &lv->vg->pvs)) {
+		/* FIXME Improve msg */
+		log_error("LV resize failed.");
+		/* FIXME Define consistent symbolic return codes */
 		return -1;
 	}
 
liblvm/lvm_pv.c +2 -2
index 622ef98..5d8a66b 100644
@@ -73,7 +73,7 @@ int lvm_pv_remove(lvm_t libh, const char *pv_name)
 {
 	struct cmd_context *cmd = (struct cmd_context *)libh;
 
-	if (pvremove_single(cmd, pv_name, NULL, 0, 0) != 1)
+	if (!pvremove_single(cmd, pv_name, NULL, 0, 0))
 		return -1;
 
 	return 0;
@@ -235,7 +235,7 @@ int lvm_pv_create(lvm_t libh, const char *pv_name, uint64_t size)
 
 	pp.size = size_sectors;
 
-	if (pvcreate_locked(cmd, pv_name, &pp) != ECMD_PROCESSED)
+	if (!pvcreate_locked(cmd, pv_name, &pp))
 		return -1;
 
 	return 0;
tools/errors.h new file +26 -0
new file mode 100644
index 0000000..a644fc7
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.  
+ * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License v.2.1.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _LVM_ERRORS_H
+#define _LVM_ERRORS_H
+
+#define ECMD_PROCESSED		1
+#define ENO_SUCH_CMD		2
+#define EINVALID_CMD_LINE	3
+#define ECMD_FAILED		5
+
+/* FIXME Also returned by cmdlib. */
+
+#endif
tools/lvm2cmd.h +6 -0
index bf93787..6df959d 100644
@@ -36,6 +36,12 @@ typedef void (*lvm2_log_fn_t) (int level, const char *file, int line,
 #define LVM2_LOG_VERY_VERBOSE	6
 #define LVM2_LOG_DEBUG		7
 
+/* Return values */
+#define LVM2_COMMAND_SUCCEEDED	1	/* ECMD_PROCESSED */
+#define LVM2_NO_SUCH_COMMAND	2	/* ENO_SUCH_CMD */
+#define LVM2_INVALID_PARAMETERS	3	/* EINVALID_CMD_LINE */
+#define LVM2_PROCESSING_FAILED	5	/* ECMD_FAILED */
+
 /*
  * Define external function to replace the built-in logging function.
  * It receives output line-by-line.
tools/lvresize.c +21 -7
index 7c73341..c5f01da 100644
@@ -156,7 +156,7 @@ static int _lvresize_params(struct cmd_context *cmd, int argc, char **argv,
 	}
 
 	lp->ac_no_sync = arg_count(cmd, nosync_ARG);
-	lp->ac_alloc = arg_uint_value(cmd, alloc_ARG, 0);
+	lp->ac_alloc = (alloc_policy_t) arg_uint_value(cmd, alloc_ARG, 0);
 
 	lp->ac_type = arg_str_value(cmd, type_ARG, NULL);
 	lp->ac_force = arg_count(cmd, force_ARG);
@@ -168,8 +168,9 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
 {
 	struct lvresize_params lp = { 0 };
 	struct volume_group *vg;
-	int r;
 	struct dm_list *pvh = NULL;
+	struct lv_list *lvl;
+	int r = ECMD_FAILED;
 
 	if (!_lvresize_params(cmd, argc, argv, &lp))
 		return EINVALID_CMD_LINE;
@@ -181,15 +182,28 @@ int lvresize(struct cmd_context *cmd, int argc, char **argv)
 		return_ECMD_FAILED;
 	}
 
-	/* How does this list get cleaned up? */
+        /* Does LV exist? */
+        if (!(lvl = find_lv_in_vg(vg, lp.lv_name))) {
+                log_error("Logical volume %s not found in volume group %s",
+                          lp.lv_name, lp.vg_name);
+		goto out;
+        }
+
 	if (!(pvh = lp.argc ? create_pv_list(cmd->mem, vg, lp.argc,
-						     lp.argv, 1) : &vg->pvs)) {
-		return_ECMD_FAILED;
+					     lp.argv, 1) : &vg->pvs))
+		goto_out;
+
+	if (!lv_resize_prepare(cmd, lvl->lv, &lp, pvh)) {
+		r = EINVALID_CMD_LINE;
+		goto_out;
 	}
 
-	if (!(r = lv_resize(cmd, vg, &lp, pvh)))
-		stack;
+	if (!lv_resize(cmd, lvl->lv, &lp, pvh))
+		goto_out;
+
+	r = ECMD_PROCESSED;
 
+out:
 	unlock_and_release_vg(cmd, vg, lp.vg_name);
 
 	return r;
tools/pvcreate.c +1 -2
index b18e400..38642f6 100644
@@ -113,9 +113,8 @@ int pvcreate(struct cmd_context *cmd, int argc, char **argv)
 
 		dm_unescape_colons_and_at_signs(argv[i], NULL, NULL);
 
-		if (ECMD_PROCESSED != pvcreate_locked(cmd, argv[i], &pp)) {
+		if (!pvcreate_locked(cmd, argv[i], &pp))
 			ret = ECMD_FAILED;
-		}
 	}
 
 	return ret;
tools/pvremove.c +2 -4
index a99a9bd..45c675e 100644
@@ -16,7 +16,6 @@
 #include "tools.h"
 #include "metadata.h"
 
-
 int pvremove(struct cmd_context *cmd, int argc, char **argv)
 {
 	int i, r;
@@ -34,9 +33,8 @@ int pvremove(struct cmd_context *cmd, int argc, char **argv)
 
 	for (i = 0; i < argc; i++) {
 		dm_unescape_colons_and_at_signs(argv[i], NULL, NULL);
-		r = pvremove_single(cmd, argv[i], NULL, force_count, prompt);
-		if (r > ret)
-			ret = r;
+		if (!pvremove_single(cmd, argv[i], NULL, force_count, prompt))
+			ret = ECMD_FAILED;
 	}
 
 	return ret;