Skip to contentSkip to conversation

main - lvresize: restructure code

PATCH David Teigland 1 participant 1 message Sep 13, 2022

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=18722dfdf4d3e6f172d0b2af8bbdc4a154ea1dc0
Commit:        18722dfdf4d3e6f172d0b2af8bbdc4a154ea1dc0
Parent:        55e9494e5fe3e3d2790fca6330b848a577e0b1c0
Author:        David Teigland <[email protected]>
AuthorDate:    Fri Sep 9 16:07:07 2022 -0500
Committer:     David Teigland <[email protected]>
CommitterDate: Fri Sep 9 16:18:55 2022 -0500

lvresize: restructure code

Rewrite top level resize function to prepare for adding new fs resizing.

---
 lib/commands/cmd_enum.h          |  19 +
 lib/commands/toolcontext.h       |   2 +
 lib/locking/lvmlockd.c           |   6 +-
 lib/metadata/lv_manip.c          | 942 +++++++++++++++++++++++----------------
 lib/metadata/metadata-exported.h |  39 +-
 lib/metadata/metadata.c          |  12 +
 lib/metadata/pool_manip.c        |   3 +
 test/shell/lvextend-thin-full.sh |  11 +-
 test/shell/lvresize-full.sh      |  13 +-
 tools/command-lines.in           |  20 +-
 tools/lvextend.c                 |   2 +-
 tools/lvmcmdline.c               |  13 +
 tools/lvreduce.c                 |   2 +-
 tools/lvresize.c                 | 346 ++++++++------
 tools/tools.h                    |  16 +-
 15 files changed, 889 insertions(+), 557 deletions(-)
lib/commands/cmd_enum.h new file +19 -0
new file mode 100644
index 000000000..939af81c3
@@ -0,0 +1,19 @@
+#ifndef _CMD_ENUM_H
+#define _CMD_ENUM_H
+
+/*
+ * tools/cmds.h is generated by the Makefile.  For each command definition
+ * in command-lines.in, cmds.h contains:
+ * cmd(foo_CMD, foo)
+ *
+ * This header adds each of the foo_CMD's into an enum, so there's
+ * a unique integer identifier for each command definition.
+ */
+
+enum {
+#define cmd(a, b) a ,
+#include "../tools/cmds.h"
+#undef cmd
+};
+
+#endif
lib/commands/toolcontext.h +2 -0
index 7a4979b33..eb2d1922b 100644
@@ -18,6 +18,7 @@
 
 #include "lib/device/dev-cache.h"
 #include "lib/device/dev-type.h"
+#include "lib/commands/cmd_enum.h"
 
 #include <limits.h>
 
@@ -94,6 +95,7 @@ struct cmd_context {
 	const char *name; /* needed before cmd->command is set */
 	struct command_name *cname;
 	struct command *command;
+	int command_enum; /* duplicate from command->command_enum for lib code */
 	char **argv;
 	struct arg_values *opt_arg_values;
 	struct dm_list arg_value_groups;
lib/locking/lvmlockd.c +5 -1
index f0183db3d..2ef0900d4 100644
@@ -577,8 +577,9 @@ static int _extend_sanlock_lv(struct cmd_context *cmd, struct volume_group *vg,
 		  (uint32_t)(new_size_sectors * SECTOR_SIZE));
 
 	lp.size = new_size_sectors;
+	lp.pvh = &vg->pvs;
 
-	if (!lv_resize(lv, &lp, &vg->pvs)) {
+	if (!lv_resize(cmd, lv, &lp)) {
 		log_error("Extend sanlock LV %s to size %s failed.",
 			  display_lvname(lv), display_size(cmd, lp.size));
 		return 0;
@@ -2733,6 +2734,9 @@ int lockd_lv_resize(struct cmd_context *cmd, struct logical_volume *lv,
 	if (!_lvmlockd_connected)
 		return 0;
 
+	if (lv_is_lockd_sanlock_lv(lv))
+		return 1;
+
 	/*
 	 * A special case for gfs2 where we want to allow lvextend
 	 * of an LV that has an existing shared lock, which is normally
lib/metadata/lv_manip.c +565 -377

1,145 lines not on this page · raw message

lib/metadata/metadata-exported.h +18 -21
index deaf3f30c..048b7553f 100644
@@ -297,6 +297,9 @@
 int lv_layout_and_role(struct dm_pool *mem, const struct logical_volume *lv,
 		       struct dm_list **layout, struct dm_list **role);
 
+int lv_is_linear(struct logical_volume *lv);
+int lv_is_striped(struct logical_volume *lv);
+
 /* Ordered list - see lv_manip.c */
 typedef enum {
 	AREA_UNASSIGNED,
@@ -661,42 +664,33 @@ struct pvcreate_params {
 };
 
 struct lvresize_params {
-	int argc;
-	char **argv;
-
-	const char *vg_name; /* only-used when VG is not yet opened (in /tools) */
-	const char *lv_name;
-
-	const struct segment_type *segtype;
-
-	uint64_t poolmetadata_size;
-	sign_t poolmetadata_sign;
-
-	/* Per LV applied parameters */
-
 	enum {
 		LV_ANY = 0,
 		LV_REDUCE = 1,
 		LV_EXTEND = 2
 	} resize;
-
-	int use_policies;
-
 	alloc_policy_t alloc;
 	int yes;
 	int force;
 	int nosync;
 	int nofsck;
 	int resizefs;
+	int use_policies;
 
+	const struct segment_type *segtype;
 	unsigned mirrors;
 	uint32_t stripes;
 	uint64_t stripe_size;
 
-	uint32_t extents;
 	uint64_t size;
+	uint32_t extents;
 	sign_t sign;
-	percent_type_t percent;
+	percent_type_t percent; /* the type of percentage, not a value */
+	uint32_t percent_value; /* 0 - 100 */
+	uint64_t poolmetadata_size;
+	sign_t poolmetadata_sign;
+	uint32_t policy_percent_main;
+	uint32_t policy_percent_meta;
 
 	int approx_alloc;
 	int extents_are_pes;	/* Is 'extents' counting PEs or LEs? */
@@ -705,6 +699,8 @@ struct lvresize_params {
 	const char *lockopt;
 	char *lockd_lv_refresh_path; /* set during resize to use for refresh at the end */
 	char *lockd_lv_refresh_uuid; /* set during resize to use for refresh at the end */
+
+	struct dm_list *pvh;	/* list of pvs to use */
 };
 
 void pvcreate_params_set_defaults(struct pvcreate_params *pp);
@@ -745,9 +741,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 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);
+int lv_extend_policy_calculate_percent(struct logical_volume *lv,
+                                       uint32_t *amount, uint32_t *meta_amount);
 
 struct volume_group *vg_read(struct cmd_context *cmd, const char *vg_name, const char *vgid,
 			     uint32_t read_flags, uint32_t lockd_state,
lib/metadata/metadata.c +12 -0
index 1cda1888f..95f25eef8 100644
@@ -5302,3 +5302,15 @@ int get_visible_lvs_using_pv(struct cmd_context *cmd, struct volume_group *vg, s
 	return 1;
 }
 
+int lv_is_linear(struct logical_volume *lv)
+{
+	struct lv_segment *seg = first_seg(lv);
+	return segtype_is_linear(seg->segtype);
+}
+
+int lv_is_striped(struct logical_volume *lv)
+{
+	struct lv_segment *seg = first_seg(lv);
+	return segtype_is_striped(seg->segtype);
+}
+
lib/metadata/pool_manip.c +3 -0
index bb4b5f115..a6bfc2deb 100644
@@ -737,6 +737,7 @@ int handle_pool_metadata_spare(struct volume_group *vg, uint32_t extents,
 		extents = MAX_SIZE;
 
 	if (!lv) {
+		log_debug("Adding new pool metadata spare %u extents.", extents);
 		if (!_alloc_pool_metadata_spare(vg, extents, pvh))
 			return_0;
 
@@ -746,6 +747,8 @@ int handle_pool_metadata_spare(struct volume_group *vg, uint32_t extents,
 	seg = last_seg(lv);
 	seg_mirrors = lv_mirror_count(lv);
 
+	log_debug("Extending pool metadata spare from %u to %u extents.",
+		  lv->le_count, extents);
 	/* Check spare LV is big enough and preserve segtype */
 	if ((lv->le_count < extents) && seg &&
 	    !lv_extend(lv, seg->segtype,
test/shell/lvextend-thin-full.sh +5 -6
index 263ab2fb9..69d15e3b0 100644
@@ -41,16 +41,15 @@ lvcreate -L2M -n $lv1 $vg
 "$LVM_TEST_THIN_RESTORE_CMD" -i data -o "$DM_DEV_DIR/mapper/$vg-$lv1"
 lvconvert -y --thinpool $vg/pool --poolmetadata $vg/$lv1
 
-# Cannot resize if set to 0%
-not lvextend --use-policies --config 'activation{thin_pool_autoextend_percent = 0}' $vg/pool 2>&1 | tee err
-grep "0%" err
-
-# Locally active LV is needed
+# active thin pool is needed to use policy
 not lvextend --use-policies $vg/pool 2>&1 | tee err
-grep "locally" err
 
 lvchange -ay $vg
 
+# Cannot resize if set to 0%
+not lvextend --use-policies --config 'activation{thin_pool_autoextend_percent = 0}' $vg/pool 2>&1 | tee err
+grep "0%" err
+
 # Creation of new LV is not allowed when thinpool is over threshold
 not lvcreate -V10 $vg/pool
 
test/shell/lvresize-full.sh +9 -4
index dcf93a7b2..1ad9f32b1 100644
@@ -37,15 +37,19 @@ lvs -a $vg
 "$MKFS" "$lvdev"
 
 # this should resolve to resize to same actual size
-lvreduce -r -f -l-100%FREE $vg/$lv1
+not lvreduce -l-100%FREE $vg/$lv1
+not lvreduce -r -f -l-100%FREE $vg/$lv1
 "$FSCK" -n "$lvdev"
 
 # size should remain the same
-lvextend -r -f -l+100%FREE $vg/$lv1
+# lvresize fails with same result with or without -r
+not lvextend -l+100%FREE $vg/$lv1
+not lvextend -r -f -l+100%FREE $vg/$lv1
 "$FSCK" -n "$lvdev"
 
 #lvchange -an $vg/$lv1
-lvresize -r -f -l+100%FREE $vg/$lv1
+not lvresize -l+100%FREE $vg/$lv1
+not lvresize -r -f -l+100%FREE $vg/$lv1
 "$FSCK" -n "$lvdev"
 
 # Check there is really file system resize happening
@@ -55,7 +59,8 @@ lvresize -r -f -l+100%FREE $vg/$lv1
 grep "20000 blocks" out
 
 SIZE=$(get lv_field $vg/$lv1 size)
-lvresize -r -f -l-100%FREE $vg/$lv1
+not lvresize -l-100%FREE $vg/$lv1
+not lvresize -r -f -l-100%FREE $vg/$lv1
 test "$SIZE" = "$(get lv_field $vg/$lv1 size)"
 
 "$FSCK" -n "$lvdev" | tee out
tools/command-lines.in +10 -10
index 6e9e7bdea..0cb9425c1 100644
@@ -1382,7 +1382,7 @@ OO: --alloc Alloc, --autobackup Bool, --force, --mirrors Number,
 --stripes Number, --stripesize SizeKB, --poolmetadatasize PSizeMB,
 --type SegType
 OP: PV ...
-ID: lvextend_by_size
+ID: lvextend_size
 DESC: Extend an LV by a specified size.
 
 lvextend LV PV ...
@@ -1390,25 +1390,25 @@ OO: --alloc Alloc, --autobackup Bool, --force, --mirrors Number,
 --nofsck, --nosync, --noudevsync,
 --reportformat ReportFmt, --resizefs, --stripes Number, --stripesize SizeKB,
 --type SegType
-ID: lvextend_by_pv
+ID: lvextend_pv
 DESC: Extend an LV by specified PV extents.
 
-lvextend --poolmetadatasize PSizeMB LV_thinpool
+lvextend --poolmetadatasize PSizeMB LV_thinpool_linear
 OO: --alloc Alloc, --autobackup Bool, --force, --mirrors Number,
 --nofsck, --nosync, --noudevsync,
 --reportformat ReportFmt, --stripes Number, --stripesize SizeKB,
 --type SegType
 OP: PV ...
-ID: lvextend_pool_metadata_by_size
+ID: lvextend_pool_metadata
 DESC: Extend a pool metadata SubLV by a specified size.
 
-lvextend --usepolicies LV_thinpool_snapshot
+lvextend --usepolicies LV_snapshot_thinpool_vdopool
 OO: --alloc Alloc, --autobackup Bool, --force, --mirrors Number,
 --nofsck, --nosync, --noudevsync,
 --reportformat ReportFmt, --resizefs,
 --type SegType
 OP: PV ...
-ID: lvextend_by_policy
+ID: lvextend_policy
 DESC: Extend an LV according to a predefined policy.
 
 ---
@@ -1456,7 +1456,7 @@ DESC: Remove the devices file entry for the given PVID.
 lvreduce --size NSizeMB LV
 OO: --autobackup Bool, --force, --nofsck, --noudevsync,
 --reportformat ReportFmt, --resizefs
-ID: lvreduce_general
+ID: lvreduce_size
 
 ---
 
@@ -1487,7 +1487,7 @@ OO: --alloc Alloc, --autobackup Bool, --force,
 --stripes Number, --stripesize SizeKB, --poolmetadatasize PSizeMB,
 --type SegType
 OP: PV ...
-ID: lvresize_by_size
+ID: lvresize_size
 DESC: Resize an LV by a specified size.
 
 lvresize LV PV ...
@@ -1495,7 +1495,7 @@ OO: --alloc Alloc, --autobackup Bool, --force,
 --nofsck, --nosync, --noudevsync,
 --reportformat ReportFmt, --resizefs, --stripes Number, --stripesize SizeKB,
 --type SegType
-ID: lvresize_by_pv
+ID: lvresize_pv
 DESC: Resize an LV by specified PV extents.
 
 lvresize --poolmetadatasize PSizeMB LV_thinpool
@@ -1504,7 +1504,7 @@ OO: --alloc Alloc, --autobackup Bool, --force,
 --reportformat ReportFmt, --stripes Number, --stripesize SizeKB,
 --type SegType
 OP: PV ...
-ID: lvresize_pool_metadata_by_size
+ID: lvresize_pool_metadata
 DESC: Resize a pool metadata SubLV by a specified size.
 
 ---
tools/lvextend.c +1 -1
index 865bb965e..e915a95cf 100644
@@ -17,5 +17,5 @@
 
 int lvextend(struct cmd_context *cmd, int argc, char **argv)
 {
-	return lvresize(cmd, argc, argv);
+	return lvresize_cmd(cmd, argc, argv);
 }
tools/lvmcmdline.c +13 -0
index ba3ca220b..a5bb6a5c5 100644
@@ -165,6 +165,16 @@ static const struct command_function _command_functions[CMD_COUNT] = {
 
 	{ pvscan_display_CMD, pvscan_display_cmd },
 	{ pvscan_cache_CMD, pvscan_cache_cmd },
+
+	/* lvextend/lvreduce/lvresize */
+	{ lvextend_policy_CMD,		lvextend_policy_cmd },
+	{ lvextend_pool_metadata_CMD,	lvresize_cmd },
+	{ lvresize_pool_metadata_CMD,	lvresize_cmd },
+	{ lvextend_pv_CMD,		lvresize_cmd },
+	{ lvresize_pv_CMD,		lvresize_cmd },
+	{ lvextend_size_CMD,		lvresize_cmd },
+	{ lvreduce_size_CMD,		lvresize_cmd },
+	{ lvresize_size_CMD,		lvresize_cmd },
 };
 
 
@@ -3153,6 +3163,9 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
 	if (!(cmd->command = _find_command(cmd, cmd->name, &argc, argv)))
 		return EINVALID_CMD_LINE;
 
+	/* avoid this by letting lib code use cmd->command */
+	cmd->command_enum = cmd->command->command_enum;
+
 	/*
 	 * If option --foo is set which is listed in IO (ignore option) in
 	 * command-lines.in, then unset foo.  Commands won't usually use an
tools/lvreduce.c +1 -1
index 7b5d1583a..721d7f1b9 100644
@@ -17,5 +17,5 @@
 
 int lvreduce(struct cmd_context *cmd, int argc, char **argv)
 {
-	return lvresize(cmd, argc, argv);
+	return lvresize_cmd(cmd, argc, argv);
 }
tools/lvresize.c +217 -129

405 lines not on this page · raw message

tools/tools.h +9 -7
index 2636c22da..36da3bc7e 100644
@@ -45,19 +45,18 @@
 #include "lib/notify/lvmnotify.h"
 #include "lib/label/hints.h"
 
+/*
+ * cmd_enum.h uses the generated cmds.h to create the enum with an ID
+ * for each command definition in command-lines.in.
+ */
+#include "lib/commands/cmd_enum.h"
+
 #include <ctype.h>
 #include <sys/types.h>
 
 #define CMD_LEN 256
 #define MAX_ARGS 64
 
-/* define the enums for each unique ID in command defs in command-lines.in */
-enum {
-#define cmd(a, b) a ,
-#include "cmds.h"
-#undef cmd
-};
-
 /* define the enums for the values accepted by command line --options, foo_VAL */
 enum {
 #define val(a, b, c, d) a ,
@@ -296,4 +295,7 @@ int lvconvert_cachevol_attach_single(struct cmd_context *cmd,
                                      struct logical_volume *lv,
                                      struct processing_handle *handle);
 
+int lvresize_cmd(struct cmd_context *cmd, int argc, char **argv);
+int lvextend_policy_cmd(struct cmd_context *cmd, int argc, char **argv);
+
 #endif