Skip to contentSkip to conversation

Amended vg_read patches.

PATCH New Petr Rockai 4 participants 10 messages Jan 15, 2009 – Jan 16, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Amended vg_read patches.

Hi,

I have tried to address most issues that have been voiced here. Since changes in the first patches required rebasing several other patches, I am sending all affected patches at once. (Ie. I am omitting patches that did not change.)

I did not change the uint32_t for failure to a typedef yet, I would do that in a separate patch on top, which I will send as soon as these are re-reviewed.

Yours,
Petr.

#1 lore raw

PATCH 1/6 Provide _vg_lock_and_read, a new implementation of lock+vg_read_internal.

Thu Jan 15 10:36:27 CET 2009 Petr Rockai <[email protected]>
* Provide _vg_lock_and_read, a new implementation of lock+vg_read_internal.

This is an internal function, to be later used by vg_read and vg_read_for_update. Not to be confused with the public function vg_lock_and_read, which will be removed later.

lib/metadata/metadata.c +157 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata.c new-temp.23590/lib/metadata/metadata.c
@@ -2377,7 +2377,37 @@ int pv_analyze(struct cmd_context *cmd, 
 	return 1;
 }
 
+static uint32_t _vg_check_status(const struct volume_group *vg, uint32_t status)
+{
+	uint32_t failure = 0;
 
+	if ((status & CLUSTERED) &&
+	    (vg_is_clustered(vg)) && !locking_is_clustered() &&
+	    !lockingfailed()) {
+		log_error("Skipping clustered volume group %s", vg->name);
+		failure |= FAILED_CLUSTERED;
+	}
+
+	if ((status & EXPORTED_VG) &&
+	    (vg->status & EXPORTED_VG)) {
+		log_error("Volume group %s is exported", vg->name);
+		failure |= FAILED_EXPORTED;
+	}
+
+	if ((status & LVM_WRITE) &&
+	    !(vg->status & LVM_WRITE)) {
+		log_error("Volume group %s is read-only", vg->name);
+		failure |= FAILED_READ_ONLY;
+	}
+
+	if ((status & RESIZEABLE_VG) &&
+	    !(vg->status & RESIZEABLE_VG)) {
+		log_error("Volume group %s is not resizeable.", vg->name);
+		failure |= FAILED_RESIZEABLE;
+	}
+
+	return failure;
+}
 
 /**
  * vg_check_status - check volume group status flags and log error
@@ -2462,6 +2492,133 @@ vg_t *vg_lock_and_read(struct cmd_contex
 }
 
 /*
+ * Create a (vg_t) volume group handle from a struct volume_group pointer and a
+ * possible failure code. Zero failure code = success.
+ */
+static vg_t *_vg_make_handle(struct cmd_context *cmd,
+			     struct volume_group *vg,
+			     uint32_t failure)
+{
+	if (!vg && !(vg = dm_pool_zalloc(cmd->mem, sizeof(*vg)))) {
+		log_error("Error allocating vg handle .");
+		return_NULL;
+	}
+	vg->read_failed = failure;
+	return (vg_t *) vg;
+}
+
+static vg_t *_recover_vg(struct cmd_context *cmd, const char *lock,
+			 const char *vg_name, const char *vgid,
+			 uint32_t lock_flags)
+{
+	int consistent = 1;
+	struct volume_group *vg;
+
+	lock_flags &= ~LCK_TYPE_MASK;
+	lock_flags |= LCK_WRITE;
+
+	unlock_vg(cmd, lock);
+
+	dev_close_all();
+
+	if (!lock_vol(cmd, lock, lock_flags))
+		return_NULL;
+
+	if (!(vg = vg_read_internal(cmd, vg_name, vgid, &consistent)))
+		return_NULL;
+	if (!consistent)
+		return_NULL;
+
+	return (vg_t *)vg;
+}
+
+/*
+ * _vg_lock_and_read - consolidate vg locking, reading, and status flag checking
+ * This is an internal function.
+ *
+ * misc_flags:
+ *  ALLOW_INCONSISTENT: disable autocorrection
+ *
+ * Setting ALLOW_INCONSISTENT might give you inconsistent metadata. You will
+ * *still* get FAILED_INCONSISTENT in case the metadata has *really* been
+ * inconsistent. However, you still get the latest version of metadata in VG.
+ *
+ * Returns:
+ * Use vg_read_error(vg) to determine the result. Nonzero vg_read_error(vg)
+ * means there were problems reading the volume group.
+ * Zero value means that the VG is open and appropriate locks are held.
+ */
+static vg_t *_vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
+			       const char *vgid,
+			       uint32_t lock_flags, uint32_t status_flags,
+			       uint32_t misc_flags)
+{
+	struct volume_group *vg = 0;
+	const char *lock;
+ 	int consistent = 1;
+	int consistent_in;
+	uint32_t failure = 0;
+
+	if (misc_flags & ALLOW_INCONSISTENT || !(lock_flags & LCK_WRITE))
+		consistent = 0;
+
+	if (!validate_name(vg_name) && !is_orphan_vg(vg_name)) {
+		log_error("Volume group name %s has invalid characters",
+			  vg_name);
+		return NULL;
+	}
+
+	lock = (misc_flags & ORPHAN_LOCK ? VG_ORPHANS : vg_name);
+	if (!(misc_flags & DISABLE_LOCK) && !lock_vol(cmd, lock, lock_flags)) {
+		log_error("Can't get lock for %s", vg_name);
+		return _vg_make_handle(cmd, vg, FAILED_LOCKING);
+	}
+
+	if (misc_flags & ORPHAN_LOCK)
+		status_flags &= ~LVM_WRITE;
+
+	if (misc_flags & EXISTENCE_CHECK)
+		consistent = 0;
+
+	consistent_in = consistent;
+
+	/* If consistent == 1, we get NULL here if correction fails. */
+	if (!(vg = vg_read_internal(cmd, vg_name, vgid, &consistent))) {
+		if (consistent_in && !consistent) {
+			log_error("Volume group \"%s\" inconsistent.", vg_name);
+			failure |= FAILED_INCONSISTENT;
+			goto_bad;
+		}
+		if (!(misc_flags & EXISTENCE_CHECK))
+			log_error("Volume group \"%s\" not found", vg_name);
+		failure |= FAILED_NOTFOUND | (misc_flags & EXISTENCE_CHECK);
+		goto_bad;
+	}
+
+	/* consistent == 0 when VG is not found, but failed == FAILED_NOTFOUND */
+	if (!consistent && !failure) {
+		if (!(vg = _recover_vg(cmd, lock, vg_name, vgid, lock_flags))) {
+			log_error("Recovery of volume group \"%s\" failed.",
+				  vg_name);
+			failure |= FAILED_INCONSISTENT;
+			goto_bad;
+		}
+	}
+
+	failure |= _vg_check_status(vg, status_flags);
+	if (failure)
+		goto_bad;
+
+	return _vg_make_handle(cmd, vg, failure);
+
+ bad:
+	if (failure != (FAILED_NOTFOUND | EXISTENCE_CHECK) &&
+	    !(misc_flags & KEEP_LOCK) && !(misc_flags & DISABLE_LOCK))
+			unlock_vg(cmd, lock);
+	return _vg_make_handle(cmd, vg, failure);
+}
+
+/*
  * Gets/Sets for external LVM library
  */
 struct id pv_id(const pv_t *pv)
lib/metadata/metadata-exported.h +40 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata-exported.h new-temp.23590/lib/metadata/metadata-exported.h
@@ -104,6 +104,43 @@ struct pv_segment;
 #define MIRROR_BY_LV		0x00000002U	/* mirror using whole mimage LVs */
 #define MIRROR_SKIP_INIT_SYNC	0x00000010U	/* skip initial sync */
 
+/* vg_read and vg_read_for_update flags */
+#define ALLOW_INCONSISTENT 0x1
+#define ALLOW_EXPORTED     0x2
+#define REQUIRE_RESIZEABLE 0x4
+#define EXISTENCE_CHECK    0x8
+
+/* FIXME these should be removed when we get better internal locking logic that
+   does not rely on caller to provide locking details. */
+#define NONBLOCKING_LOCK 0x100 /* Give up instead of waiting for a lock;
+                                  required when acquiring a second VG lock. */
+#define KEEP_LOCK        0x200 /* Do not unlock upon read failure. */
+
+/* FIXME the following flags should be removed in favour of better APIs
+   (vg_reread and vg_read_orphan?) */
+#define DISABLE_LOCK     0x400 /* Disable locking altogether. DANGEROUS. Only
+				  use when you already hold appropriate lock
+				  for this VG. */
+#define ORPHAN_LOCK      0x800 /* Use when you vg_read(_for_update) the orphan
+				  VG. */
+
+/* A meta-flag, useful with toollib for_each_* functions. */
+#define READ_FOR_UPDATE 0x1000
+
+/* vg's "read_failed" field */
+#define FAILED_INCONSISTENT 0x1
+#define FAILED_LOCKING      0x2
+#define FAILED_NOTFOUND     0x4
+/* #define EXISTENCE_CHECK  0x8 -- left out for EXISTENCE_CHECK above, as it can
+   be or'd into failure flags */
+
+#define FAILED_READ_ONLY   0x10
+#define FAILED_EXPORTED    0x20
+#define FAILED_RESIZEABLE  0x40
+#define FAILED_CLUSTERED   0x80
+
+#define FAILED_ALLOCATION 0x100
+
 /* Ordered list - see lv_manip.c */
 typedef enum {
 	ALLOC_INVALID,
@@ -196,6 +233,9 @@ struct volume_group {
 	char *name;
 	char *system_id;
 
+	uint32_t read_failed; /* 0 = no, everything OK. Otherwise uses FAILED_*
+				 flags as defined above. */
+
 	uint32_t status;
 	alloc_policy_t alloc;
 
#2 lore raw

PATCH 2/6 Replace implementation of vg_check_status with a call to _vg_check_status.

Thu Jan 15 10:38:04 CET 2009 Petr Rockai <[email protected]>
* Replace implementation of vg_check_status with a call to _vg_check_status.

lib/metadata/metadata.c +1 -25
diff -rN -u -p old-temp.23590/lib/metadata/metadata.c new-temp.23590/lib/metadata/metadata.c
@@ -2420,31 +2420,7 @@ static uint32_t _vg_check_status(const s
  */
 int vg_check_status(const struct volume_group *vg, uint32_t status)
 {
-	if ((status & CLUSTERED) &&
-	    (vg_is_clustered(vg)) && !locking_is_clustered() &&
-	    !lockingfailed()) {
-		log_error("Skipping clustered volume group %s", vg->name);
-		return 0;
-	}
-
-	if ((status & EXPORTED_VG) &&
-	    (vg->status & EXPORTED_VG)) {
-		log_error("Volume group %s is exported", vg->name);
-		return 0;
-	}
-
-	if ((status & LVM_WRITE) &&
-	    !(vg->status & LVM_WRITE)) {
-		log_error("Volume group %s is read-only", vg->name);
-		return 0;
-	}
-	if ((status & RESIZEABLE_VG) &&
-	    !(vg->status & RESIZEABLE_VG)) {
-		log_error("Volume group %s is not resizeable.", vg->name);
-		return 0;
-	}
-
-	return 1;
+	return !_vg_check_status(vg, status);
 }
 
 /*
#3 lore raw

PATCH 3/6 Implement vg_read and vg_read_for_update.

Thu Jan 15 10:55:15 CET 2009 Petr Rockai <[email protected]>
* Implement vg_read and vg_read_for_update.

lib/metadata/metadata.c +68 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata.c new-temp.23590/lib/metadata/metadata.c
@@ -2595,6 +2595,74 @@ static vg_t *_vg_lock_and_read(struct cm
 }
 
 /*
+ * vg_read: High-level volume group metadata read function.
+ *
+ * On failure, an error-indicating handle is returned. Users should use
+ * "vg_read_error" to check whether handle indicates an error or it points to
+ * valid volume group data. Moreover, vg_read_error will provide more details
+ * about the kind of failure:
+ *
+ *  - metadata inconsistent and automatic correction failed: FAILED_INCONSISTENT
+ *  - VG is read-only: FAILED_READ_ONLY
+ *  - VG is EXPORTED, unless flags has ALLOW_EXPORTED: FAILED_EXPORTED
+ *  - VG is not RESIZEABLE, unless flags has ALLOW_NONRESIZEABLE:
+ *    FAILED_RESIZEABLE
+ *  - locking failed: FAILED_LOCKING
+ *
+ * On failures, all locks are released, unless KEEP_LOCK has been supplied.
+ *
+ * By default, volume groups are opened read-only. To open a VG for updates,
+ * please use vg_read_for_update (see below).
+ *
+ * Checking for VG existence:
+ *
+ * If EXISTENCE_CHECK is set in flags, if the VG exists, a non-NULL struct
+ * volume_group will be returned every time, but if it has INCONSISTENT_VG set,
+ * the other fields will be uninitialized. You *have to* check for
+ * INCONSISTENT_VG if passing EXISTENCE_CHECK. You also *must not* use it if it
+ * has INCONSISTENT_VG set.
+ *
+ * FIXME: We want vg_read to attempt automatic recovery after acquiring a
+ * temporary write lock -- if that fails, we bail out as usual, with failed &
+ * FAILED_INCONSISTENT. If it works, we are good to go. Code that's been in
+ * toollib just set lock type to LCK_WRITE and called vg_read_internal with
+ * *consistent = 1.
+ */
+vg_t *vg_read(struct cmd_context *cmd, const char *vg_name,
+	      const char *vgid, uint32_t flags)
+{
+	uint32_t status = CLUSTERED;
+	uint32_t lock = LCK_VG_READ;
+
+	if (flags & READ_FOR_UPDATE) {
+		status |= EXPORTED_VG | LVM_WRITE;
+		lock = LCK_VG_WRITE;
+	}
+
+	if (flags & ALLOW_EXPORTED)
+		status &= ~EXPORTED_VG;
+
+	if (flags & REQUIRE_RESIZEABLE)
+		status |= RESIZEABLE_VG;
+
+	if (flags & NONBLOCKING_LOCK)
+		lock |= LCK_NONBLOCK;
+
+	return _vg_lock_and_read(cmd, vg_name, vgid, lock, status, flags);
+}
+
+/*
+ * A high-level volume group metadata reading function. Open a volume group for
+ * later update (this means the user code can change the metadata and later
+ * request the new metadata to be written and committed).
+ */
+vg_t *vg_read_for_update(struct cmd_context *cmd, const char *vg_name,
+			 const char *vgid, uint32_t flags)
+{
+	return vg_read(cmd, vg_name, vgid, flags | READ_FOR_UPDATE);
+}
+
+/*
  * Gets/Sets for external LVM library
  */
 struct id pv_id(const pv_t *pv)
lib/metadata/metadata-exported.h +6 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata-exported.h new-temp.23590/lib/metadata/metadata-exported.h
@@ -393,6 +393,12 @@ vg_t *vg_lock_and_read(struct cmd_contex
 		       uint32_t lock_flags, uint32_t status_flags,
 		       uint32_t misc_flags);
 
+/* Loading volume group metadata. */
+vg_t *vg_read(struct cmd_context *cmd, const char *vg_name,
+              const char *vgid, uint32_t flags);
+vg_t *vg_read_for_update(struct cmd_context *cmd, const char *vg_name,
+                         const char *vgid, uint32_t flags);
+
 /* pe_start and pe_end relate to any existing data so that new metadata
 * areas can avoid overlap */
 pv_t *pv_create(const struct cmd_context *cmd,
#4 lore raw

PATCH 4/6 Add vg_read_error and vg_might_exist.

Thu Jan 15 10:55:43 CET 2009 Petr Rockai <[email protected]>
* Add vg_read_error and vg_might_exist.

lib/metadata/metadata.c +34 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata.c new-temp.23590/lib/metadata/metadata.c
@@ -2663,6 +2663,40 @@ vg_t *vg_read_for_update(struct cmd_cont
 }
 
 /*
+ * Check whether a vg_read (or vg_read_for_update) operation resulted in an
+ * error, or completed successfully. If EXISTENCE_CHECK has been given to
+ * vg_read, we do not consider nonexistence of the volume group an error. The
+ * caller is responsible for using vg_might_exist:
+ * - !vg_read_error(vg) && vg_might_exist(vg) -> vg is a valid handle
+ * - vg_read_error(vg) && vg_might_exist(vg) -> vg is not valid, but the VG in
+ *   question probably exists, it just cannot be opened (consult vg_read_error
+ *   return code for reason).
+ * - !vg_read_error(vg) && !vg_might_exist(vg) -> the VG does not exist
+ * - vg_read_error(vg) && !vg_might_exist(vg) -> cannot happen
+ */
+uint32_t vg_read_error(vg_t *vg)
+{
+	if (!vg)
+		return FAILED_ALLOCATION;
+	if (vg->read_failed & EXISTENCE_CHECK)
+		return vg->read_failed & ~(EXISTENCE_CHECK | FAILED_NOTFOUND);
+	return vg->read_failed;
+}
+
+/*
+ * Returns true if the volume group already exists. If unsure, it will return
+ * true (it might exist, but we are not sure, as the read failed for some other
+ * reason).
+ */
+uint32_t vg_might_exist(vg_t *vg) {
+	if (!vg)
+		return 1;
+	if (vg->read_failed == (FAILED_NOTFOUND | EXISTENCE_CHECK))
+		return 0;
+	return 1;
+}
+
+/*
  * Gets/Sets for external LVM library
  */
 struct id pv_id(const pv_t *pv)
lib/metadata/metadata-exported.h +4 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata-exported.h new-temp.23590/lib/metadata/metadata-exported.h
@@ -399,6 +399,10 @@ vg_t *vg_read(struct cmd_context *cmd, c
 vg_t *vg_read_for_update(struct cmd_context *cmd, const char *vg_name,
                          const char *vgid, uint32_t flags);
 
+/* Queries on a (possibly error-indicating) VG handle. */
+uint32_t vg_read_error(vg_t *vg);
+uint32_t vg_might_exist(vg_t *vg);
+
 /* pe_start and pe_end relate to any existing data so that new metadata
 * areas can avoid overlap */
 pv_t *pv_create(const struct cmd_context *cmd,
#5 lore raw

PATCH 5/6 Convert vgsplit to use vg_read_for_update.

Thu Jan 15 11:02:12 CET 2009 Petr Rockai <[email protected]>
* Convert vgsplit to use vg_read_for_update.

tools/vgsplit.c +19 -20
diff -rN -u -p old-temp.23590/tools/vgsplit.c new-temp.23590/tools/vgsplit.c
@@ -289,7 +289,6 @@ int vgsplit(struct cmd_context *cmd, int
 	struct volume_group *vg_to, *vg_from;
 	int opt;
 	int existing_vg;
-	int consistent;
 	const char *lv_name;
 
 	if ((arg_count(cmd, name_ARG) + argc) < 3) {
@@ -320,21 +319,20 @@ int vgsplit(struct cmd_context *cmd, int
 	}
 
 	log_verbose("Checking for volume group \"%s\"", vg_name_from);
-	if (!(vg_from = vg_lock_and_read(cmd, vg_name_from, NULL, LCK_VG_WRITE,
-				       CLUSTERED | EXPORTED_VG |
-				       RESIZEABLE_VG | LVM_WRITE,
-				       CORRECT_INCONSISTENT | FAIL_INCONSISTENT)))
-		 return ECMD_FAILED;
+	vg_from = vg_read_for_update(cmd, vg_name_from, NULL,
+				     REQUIRE_RESIZEABLE);
+	if (vg_read_error(vg_from))
+		return ECMD_FAILED;
 
 	log_verbose("Checking for new volume group \"%s\"", vg_name_to);
-	if (!lock_vol(cmd, vg_name_to, LCK_VG_WRITE | LCK_NONBLOCK)) {
-		log_error("Can't get lock for %s", vg_name_to);
-		unlock_vg(cmd, vg_name_from);
-		return ECMD_FAILED;
-	}
+	vg_to = vg_read_for_update(cmd, vg_name_to, NULL, REQUIRE_RESIZEABLE |
+				   NONBLOCKING_LOCK | KEEP_LOCK |
+				   EXISTENCE_CHECK);
+
+	if (vg_read_error(vg_to))
+		goto_bad;
 
-	consistent = 0;
-	if ((vg_to = vg_read_internal(cmd, vg_name_to, NULL, &consistent))) {
+	if (vg_might_exist(vg_to)) {
 		existing_vg = 1;
 		if (new_vg_option_specified(cmd)) {
 			log_error("Volume group \"%s\" exists, but new VG "
@@ -449,13 +447,14 @@ int vgsplit(struct cmd_context *cmd, int
 	/*
 	 * Finally, remove the EXPORTED flag from the new VG and write it out.
 	 */
-	consistent = 1;
-	if (!test_mode() &&
-	    (!(vg_to = vg_read_internal(cmd, vg_name_to, NULL, &consistent)) ||
-	     !consistent)) {
-		log_error("Volume group \"%s\" became inconsistent: please "
-			  "fix manually", vg_name_to);
-		goto_bad;
+	if (!test_mode()) {
+		vg_to = vg_read_for_update(cmd, vg_name_to, NULL,
+					   DISABLE_LOCK | ALLOW_EXPORTED);
+		if (vg_read_error(vg_to)) {
+			log_error("Volume group \"%s\" became inconsistent: "
+				  "please fix manually", vg_name_to);
+			goto_bad;
+		}
 	}
 
 	vg_to->status &= ~EXPORTED_VG;
#6 lore raw

PATCH 6/6 Remove now-unused vg_lock_and_read.

Thu Jan 15 10:52:08 CET 2009 Petr Rockai <[email protected]>
* Remove now-unused vg_lock_and_read.

lib/metadata/metadata.c +0 -44
diff -rN -u -p old-temp.23590/lib/metadata/metadata.c new-temp.23590/lib/metadata/metadata.c
@@ -2424,50 +2424,6 @@ int vg_check_status(const struct volume_
 }
 
 /*
- * vg_lock_and_read - consolidate vg locking, reading, and status flag checking
- *
- * Returns:
- * NULL - failure
- * non-NULL - success; volume group handle
- */
-vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
-		       const char *vgid,
-		       uint32_t lock_flags, uint32_t status_flags,
-		       uint32_t misc_flags)
-{
-	struct volume_group *vg;
-	int consistent = 1;
-
-	if (!(misc_flags & CORRECT_INCONSISTENT))
-		consistent = 0;
-
-	if (!validate_name(vg_name)) {
-		log_error("Volume group name %s has invalid characters",
-			  vg_name);
-		return NULL;
-	}
-
-	if (!lock_vol(cmd, vg_name, lock_flags)) {
-		log_error("Can't get lock for %s", vg_name);
-		return NULL;
-	}
-
-	if (!(vg = vg_read_internal(cmd, vg_name, vgid, &consistent)) ||
-	    ((misc_flags & FAIL_INCONSISTENT) && !consistent)) {
-		log_error("Volume group \"%s\" not found", vg_name);
-		unlock_vg(cmd, vg_name);
-		return NULL;
-	}
-
-	if (!vg_check_status(vg, status_flags)) {
-		unlock_vg(cmd, vg_name);
-		return NULL;
-	}
-
-	return vg;
-}
-
-/*
  * Create a (vg_t) volume group handle from a struct volume_group pointer and a
  * possible failure code. Zero failure code = success.
  */
lib/metadata/metadata-exported.h +0 -4
diff -rN -u -p old-temp.23590/lib/metadata/metadata-exported.h new-temp.23590/lib/metadata/metadata-exported.h
@@ -95,10 +95,6 @@ struct pv_segment;
 #define FMT_UNLIMITED_STRIPESIZE 0x00000100U	/* Unlimited stripe size? */
 #define FMT_RESTRICTED_READAHEAD 0x00000200U	/* Readahead restricted to 2-120? */
 
-/* LVM2 external library flags */
-#define CORRECT_INCONSISTENT    0x00000001U /* Correct inconsistent metadata */
-#define FAIL_INCONSISTENT       0x00000002U /* Fail if metadata inconsistent */
-
 /* Mirror conversion type flags */
 #define MIRROR_BY_SEG		0x00000001U	/* segment-by-segment mirror */
 #define MIRROR_BY_LV		0x00000002U	/* mirror using whole mimage LVs */
#7 lore raw

PATCH 4/6 Add vg_read_error and vg_might_exist.

In reply to Petr Rockai
Quoted 51 lines · Petr Rockai

Thu Jan 15 10:55:43 CET 2009 Petr Rockai <[email protected]>
* Add vg_read_error and vg_might_exist.
diff -rN -u -p old-temp.23590/lib/metadata/metadata.c new-temp.23590/lib/metadata/metadata.c

lib/metadata/metadata.c +34 -0
@@ -2663,6 +2663,40 @@ vg_t *vg_read_for_update(struct cmd_cont
 }
 
 /*
+ * Check whether a vg_read (or vg_read_for_update) operation resulted in an
+ * error, or completed successfully. If EXISTENCE_CHECK has been given to
+ * vg_read, we do not consider nonexistence of the volume group an error. The
+ * caller is responsible for using vg_might_exist:
+ * - !vg_read_error(vg) && vg_might_exist(vg) -> vg is a valid handle
+ * - vg_read_error(vg) && vg_might_exist(vg) -> vg is not valid, but the VG in
+ *   question probably exists, it just cannot be opened (consult vg_read_error
+ *   return code for reason).
+ * - !vg_read_error(vg) && !vg_might_exist(vg) -> the VG does not exist
+ * - vg_read_error(vg) && !vg_might_exist(vg) -> cannot happen
+ */
+uint32_t vg_read_error(vg_t *vg)
+{
+	if (!vg)
+		return FAILED_ALLOCATION;
+	if (vg->read_failed & EXISTENCE_CHECK)
+		return vg->read_failed & ~(EXISTENCE_CHECK | FAILED_NOTFOUND);
+	return vg->read_failed;
+}
+
+/*
+ * Returns true if the volume group already exists. If unsure, it will return
+ * true (it might exist, but we are not sure, as the read failed for some other
+ * reason).
+ */
+uint32_t vg_might_exist(vg_t *vg) {
+	if (!vg)
+		return 1;
+	if (vg->read_failed == (FAILED_NOTFOUND | EXISTENCE_CHECK))
+		return 0;
+	return 1;
+}
+
+/*
  * Gets/Sets for external LVM library
  */
 struct id pv_id(const pv_t *pv)

diff -rN -u -p old-temp.23590/lib/metadata/metadata-exported.h new-temp.23590/lib/metadata/metadata-exported.h

lib/metadata/metadata-exported.h vg_t *vg_read(struct cmd_context *cmd, c · line 408

Quoted 10 lines
lib/metadata/metadata-exported.h +4 -0
@@ -399,6 +399,10 @@ vg_t *vg_read(struct cmd_context *cmd, c
 vg_t *vg_read_for_update(struct cmd_context *cmd, const char *vg_name,
                          const char *vgid, uint32_t flags);
 
+/* Queries on a (possibly error-indicating) VG handle. */
+uint32_t vg_read_error(vg_t *vg);
+uint32_t vg_might_exist(vg_t *vg);
+
 /* pe_start and pe_end relate to any existing data so that new metadata
 * areas can avoid overlap */
 pv_t *pv_create(const struct cmd_context *cmd,

This second hunk doesn't apply, probably because this patch is out of sequence from the original sequence of 11 (comes after the vg_read_for_update patch in this sequence).

#8 lore raw

PATCH 1/6 Provide _vg_lock_and_read, a new implementation of lock+vg_read_internal.

In reply to Petr Rockai

The version with un-regressed _vg_check_status. A better fix for the problem will be added on top of the stack later. For reference:

agk_ | CLUSTERED: if the VG is clustered and we are not looking at clustered
     | VGs then we must not look at them
agk_ | All we are permitted to do is check the clustered flag
agk_ | We may not check the exported flag - it is meaningless
agk_ | and may give the wrong answer
agk_ | and same for everything else - it's considered an "undefined" state

Thu Jan 15 10:36:27 CET 2009 Petr Rockai <[email protected]>
* Provide _vg_lock_and_read, a new implementation of lock+vg_read_internal.

This is an internal function, to be later used by vg_read and vg_read_for_update. Not to be confused with the public function vg_lock_and_read, which will be removed later.

lib/metadata/metadata.c +157 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata.c new-temp.23590/lib/metadata/metadata.c
@@ -2377,7 +2377,37 @@ int pv_analyze(struct cmd_context *cmd, 
 	return 1;
 }
 
+static uint32_t _vg_check_status(const struct volume_group *vg, uint32_t status)
+{
+	uint32_t failure = 0;
 
+	if ((status & CLUSTERED) &&
+	    (vg_is_clustered(vg)) && !locking_is_clustered() &&
+	    !lockingfailed()) {
+		log_error("Skipping clustered volume group %s", vg->name);
+		return FAILED_CLUSTERED;
+	}
+
+	if ((status & EXPORTED_VG) &&
+	    (vg->status & EXPORTED_VG)) {
+		log_error("Volume group %s is exported", vg->name);
+		failure |= FAILED_EXPORTED;
+	}
+
+	if ((status & LVM_WRITE) &&
+	    !(vg->status & LVM_WRITE)) {
+		log_error("Volume group %s is read-only", vg->name);
+		failure |= FAILED_READ_ONLY;
+	}
+
+	if ((status & RESIZEABLE_VG) &&
+	    !(vg->status & RESIZEABLE_VG)) {
+		log_error("Volume group %s is not resizeable.", vg->name);
+		failure |= FAILED_RESIZEABLE;
+	}
+
+	return failure;
+}
 
 /**
  * vg_check_status - check volume group status flags and log error
@@ -2462,6 +2492,133 @@ vg_t *vg_lock_and_read(struct cmd_contex
 }
 
 /*
+ * Create a (vg_t) volume group handle from a struct volume_group pointer and a
+ * possible failure code. Zero failure code = success.
+ */
+static vg_t *_vg_make_handle(struct cmd_context *cmd,
+			     struct volume_group *vg,
+			     uint32_t failure)
+{
+	if (!vg && !(vg = dm_pool_zalloc(cmd->mem, sizeof(*vg)))) {
+		log_error("Error allocating vg handle .");
+		return_NULL;
+	}
+	vg->read_failed = failure;
+	return (vg_t *) vg;
+}
+
+static vg_t *_recover_vg(struct cmd_context *cmd, const char *lock,
+			 const char *vg_name, const char *vgid,
+			 uint32_t lock_flags)
+{
+	int consistent = 1;
+	struct volume_group *vg;
+
+	lock_flags &= ~LCK_TYPE_MASK;
+	lock_flags |= LCK_WRITE;
+
+	unlock_vg(cmd, lock);
+
+	dev_close_all();
+
+	if (!lock_vol(cmd, lock, lock_flags))
+		return_NULL;
+
+	if (!(vg = vg_read_internal(cmd, vg_name, vgid, &consistent)))
+		return_NULL;
+	if (!consistent)
+		return_NULL;
+
+	return (vg_t *)vg;
+}
+
+/*
+ * _vg_lock_and_read - consolidate vg locking, reading, and status flag checking
+ * This is an internal function.
+ *
+ * misc_flags:
+ *  ALLOW_INCONSISTENT: disable autocorrection
+ *
+ * Setting ALLOW_INCONSISTENT might give you inconsistent metadata. You will
+ * *still* get FAILED_INCONSISTENT in case the metadata has *really* been
+ * inconsistent. However, you still get the latest version of metadata in VG.
+ *
+ * Returns:
+ * Use vg_read_error(vg) to determine the result. Nonzero vg_read_error(vg)
+ * means there were problems reading the volume group.
+ * Zero value means that the VG is open and appropriate locks are held.
+ */
+static vg_t *_vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
+			       const char *vgid,
+			       uint32_t lock_flags, uint32_t status_flags,
+			       uint32_t misc_flags)
+{
+	struct volume_group *vg = 0;
+	const char *lock;
+ 	int consistent = 1;
+	int consistent_in;
+	uint32_t failure = 0;
+
+	if (misc_flags & ALLOW_INCONSISTENT || !(lock_flags & LCK_WRITE))
+		consistent = 0;
+
+	if (!validate_name(vg_name) && !is_orphan_vg(vg_name)) {
+		log_error("Volume group name %s has invalid characters",
+			  vg_name);
+		return NULL;
+	}
+
+	lock = (misc_flags & ORPHAN_LOCK ? VG_ORPHANS : vg_name);
+	if (!(misc_flags & DISABLE_LOCK) && !lock_vol(cmd, lock, lock_flags)) {
+		log_error("Can't get lock for %s", vg_name);
+		return _vg_make_handle(cmd, vg, FAILED_LOCKING);
+	}
+
+	if (misc_flags & ORPHAN_LOCK)
+		status_flags &= ~LVM_WRITE;
+
+	if (misc_flags & EXISTENCE_CHECK)
+		consistent = 0;
+
+	consistent_in = consistent;
+
+	/* If consistent == 1, we get NULL here if correction fails. */
+	if (!(vg = vg_read_internal(cmd, vg_name, vgid, &consistent))) {
+		if (consistent_in && !consistent) {
+			log_error("Volume group \"%s\" inconsistent.", vg_name);
+			failure |= FAILED_INCONSISTENT;
+			goto_bad;
+		}
+		if (!(misc_flags & EXISTENCE_CHECK))
+			log_error("Volume group \"%s\" not found", vg_name);
+		failure |= FAILED_NOTFOUND | (misc_flags & EXISTENCE_CHECK);
+		goto_bad;
+	}
+
+	/* consistent == 0 when VG is not found, but failed == FAILED_NOTFOUND */
+	if (!consistent && !failure) {
+		if (!(vg = _recover_vg(cmd, lock, vg_name, vgid, lock_flags))) {
+			log_error("Recovery of volume group \"%s\" failed.",
+				  vg_name);
+			failure |= FAILED_INCONSISTENT;
+			goto_bad;
+		}
+	}
+
+	failure |= _vg_check_status(vg, status_flags);
+	if (failure)
+		goto_bad;
+
+	return _vg_make_handle(cmd, vg, failure);
+
+ bad:
+	if (failure != (FAILED_NOTFOUND | EXISTENCE_CHECK) &&
+	    !(misc_flags & KEEP_LOCK) && !(misc_flags & DISABLE_LOCK))
+			unlock_vg(cmd, lock);
+	return _vg_make_handle(cmd, vg, failure);
+}
+
+/*
  * Gets/Sets for external LVM library
  */
 struct id pv_id(const pv_t *pv)
lib/metadata/metadata-exported.h +40 -0
diff -rN -u -p old-temp.23590/lib/metadata/metadata-exported.h new-temp.23590/lib/metadata/metadata-exported.h
@@ -104,6 +104,43 @@ struct pv_segment;
 #define MIRROR_BY_LV		0x00000002U	/* mirror using whole mimage LVs */
 #define MIRROR_SKIP_INIT_SYNC	0x00000010U	/* skip initial sync */
 
+/* vg_read and vg_read_for_update flags */
+#define ALLOW_INCONSISTENT 0x1
+#define ALLOW_EXPORTED     0x2
+#define REQUIRE_RESIZEABLE 0x4
+#define EXISTENCE_CHECK    0x8
+
+/* FIXME these should be removed when we get better internal locking logic that
+   does not rely on caller to provide locking details. */
+#define NONBLOCKING_LOCK 0x100 /* Give up instead of waiting for a lock;
+                                  required when acquiring a second VG lock. */
+#define KEEP_LOCK        0x200 /* Do not unlock upon read failure. */
+
+/* FIXME the following flags should be removed in favour of better APIs
+   (vg_reread and vg_read_orphan?) */
+#define DISABLE_LOCK     0x400 /* Disable locking altogether. DANGEROUS. Only
+				  use when you already hold appropriate lock
+				  for this VG. */
+#define ORPHAN_LOCK      0x800 /* Use when you vg_read(_for_update) the orphan
+				  VG. */
+
+/* A meta-flag, useful with toollib for_each_* functions. */
+#define READ_FOR_UPDATE 0x1000
+
+/* vg's "read_failed" field */
+#define FAILED_INCONSISTENT 0x1
+#define FAILED_LOCKING      0x2
+#define FAILED_NOTFOUND     0x4
+/* #define EXISTENCE_CHECK  0x8 -- left out for EXISTENCE_CHECK above, as it can
+   be or'd into failure flags */
+
+#define FAILED_READ_ONLY   0x10
+#define FAILED_EXPORTED    0x20
+#define FAILED_RESIZEABLE  0x40
+#define FAILED_CLUSTERED   0x80
+
+#define FAILED_ALLOCATION 0x100
+
 /* Ordered list - see lv_manip.c */
 typedef enum {
 	ALLOC_INVALID,
@@ -196,6 +233,9 @@ struct volume_group {
 	char *name;
 	char *system_id;
 
+	uint32_t read_failed; /* 0 = no, everything OK. Otherwise uses FAILED_*
+				 flags as defined above. */
+
 	uint32_t status;
 	alloc_policy_t alloc;
 
Quoted 1 line

--
lvm-devel mailing list
lvm-devel at redhat.com
https://www.redhat.com/mailman/listinfo/lvm-devel

Signature · 6 lines
Peter Rockai | me()mornfall!net | prockai()redhat!com
 http://blog.mornfall.net | http://web.mornfall.net

"In My Egotistical Opinion, most people's C programs should be
 indented six feet downward and covered with dirt."
     -- Blair P. Houghton on the subject of C program indentation
#9 lore raw

PATCH 1/1 Amended vg_read patches.

In reply to Petr Rockai
Quoted 3 lines · Petr Rockai

I have tried to address most issues that have been voiced here. Since changes in the first patches required rebasing several other patches, I am sending all affected patches at once. (Ie. I am omitting patches that did not change.)

I think it would easier in future if revised patches like this were posted as follow-ups to the original, so the discussion stays together on the same thread.

Alasdair

Signature · 1 line
agk at redhat.com