Skip to contentSkip to conversation

master - cache: Store metadata size and checksum.

PATCH Alasdair Kergon 1 participant 1 message Mar 18, 2015

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=6407d184d1d4ce491035fe4d9e3ebbe6aefaa394
Commit:        6407d184d1d4ce491035fe4d9e3ebbe6aefaa394
Parent:        19c3851d9cd251bdb7eccd47385dd37818161bf7
Author:        Alasdair G Kergon <[email protected]>
AuthorDate:    Wed Mar 18 23:43:02 2015 +0000
Committer:     Alasdair G Kergon <[email protected]>
CommitterDate: Wed Mar 18 23:43:02 2015 +0000

cache: Store metadata size and checksum.

Refactor the recent metadata-reading optimisation patches.

Remove the recently-added cache fields from struct labeller and struct format_instance.

Instead, introduce struct lvmcache_vgsummary to wrap the VG information that lvmcache holds and add the metadata size and checksum to it.

Allow this VG summary information to be looked up by metadata size + checksum. Adjust the debug log messages to make it clear when this shortcut has been successful.

(This changes the optimisation slightly, and might be extendable further.)

Add struct cached_vg_fmtdata to format-specific vg_read calls to preserve state alongside the VG across separate calls and indicate if the details supplied match, avoiding the need to read and process the VG metadata again.

---
 WHATS_NEW                        |    3 +-
 lib/cache/lvmcache.c             |   78 ++++++++++++++++++++---
 lib/cache/lvmcache.h             |   13 +++-
 lib/cache/lvmetad.c              |    4 +-
 lib/config/config.c              |    6 +-
 lib/format1/format1.c            |    2 +
 lib/format_pool/format_pool.c    |    2 +
 lib/format_text/archiver.c       |    2 +-
 lib/format_text/format-text.c    |  127 ++++++++++++++++++++------------------
 lib/format_text/import-export.h  |   26 ++++----
 lib/format_text/import.c         |   76 ++++++++++++++---------
 lib/format_text/import_vsn1.c    |   23 +++----
 lib/format_text/layout.h         |   12 +---
 lib/format_text/text_label.c     |   12 +---
 lib/label/label.c                |   24 ++++---
 lib/label/label.h                |    8 ---
 lib/metadata/metadata-exported.h |    5 --
 lib/metadata/metadata.c          |   39 +++++++++---
 lib/metadata/metadata.h          |    7 ++-
 19 files changed, 288 insertions(+), 181 deletions(-)
WHATS_NEW +2 -1
index b182925..c9242fe 100644
@@ -1,5 +1,6 @@
 Version 2.02.118 - 
 =================================
+  Store metadata size + checksum in lvmcache and add struct lvmcache_vgsummary.
   Remove inaccessible clustered PVs from 'pvs -a'.
   Don't invalidate cached orphan information while global lock is held.
   Avoid rescan of all devices when requested pvscan for removed device.
@@ -9,7 +10,7 @@ Version 2.02.118 -
   Add After=iscsi-shutdown.service to blk-availability.service systemd unit.
   Disallow vgconvert from changing metadata format when lvmetad is used.
   Don't do a full read of VG when creating a new VG with an existing name.
-  Reduce number of VG metadata parsing when looking for vgname on a PV.
+  Reduce amount of VG metadata parsing when looking for vgname on a PV.
   Avoid reparsing same metadata when reading same metadata from multiple PVs.
   Save extra device open/close when scanning device for size.
   Fix seg_monitor field to report status also for mirrors and thick snapshots.
lib/cache/lvmcache.c +68 -10

131 lines not on this page · raw message

lib/cache/lvmcache.h +11 -2
index 0a7d898..6c4c927 100644
@@ -39,6 +39,15 @@ struct disk_locn;
 
 struct lvmcache_vginfo;
 
+struct lvmcache_vgsummary {
+	const char *vgname;
+	struct id vgid;
+	uint64_t vgstatus;
+	char *creation_host;
+	uint32_t mda_checksum;
+	size_t mda_size;
+};
+
 int lvmcache_init(void);
 void lvmcache_allow_reads_with_lvmetad(void);
 
@@ -58,8 +67,7 @@ void lvmcache_del(struct lvmcache_info *info);
 
 /* Update things */
 int lvmcache_update_vgname_and_id(struct lvmcache_info *info,
-				  const char *vgname, const char *vgid,
-				  uint32_t vgstatus, const char *hostname);
+				  struct lvmcache_vgsummary *vgsummary);
 int lvmcache_update_vg(struct volume_group *vg, unsigned precommitted);
 
 void lvmcache_lock_vgname(const char *vgname, int read_only);
@@ -68,6 +76,7 @@ int lvmcache_verify_lock_order(const char *vgname);
 
 /* Queries */
 const struct format_type *lvmcache_fmt_from_vgname(struct cmd_context *cmd, const char *vgname, const char *vgid, unsigned revalidate_labels);
+int lvmcache_lookup_mda(struct lvmcache_vgsummary *vgsummary);
 
 /* Decrement and test if there are still vg holders in vginfo. */
 int lvmcache_vginfo_holders_dec_and_test_for_zero(struct lvmcache_vginfo *vginfo);
lib/cache/lvmetad.c +2 -2
index d878c17..7bb6701 100644
@@ -897,7 +897,7 @@ static int _lvmetad_pvscan_single(struct metadata_area *mda, void *baton)
 	struct _lvmetad_pvscan_baton *b = baton;
 	struct volume_group *this;
 
-	this = mda_is_ignored(mda) ? NULL : mda->ops->vg_read(b->fid, "", mda, 1);
+	this = mda_is_ignored(mda) ? NULL : mda->ops->vg_read(b->fid, "", mda, NULL, NULL, 1);
 
 	/* FIXME Also ensure contents match etc. */
 	if (!b->vg || this->seqno > b->vg->seqno)
@@ -960,7 +960,7 @@ int lvmetad_pvscan_single(struct cmd_context *cmd, struct device *dev,
 	 * can scan further devices.
 	 */
 	if (!baton.vg && !(baton.fid->fmt->features & FMT_MDAS))
-		baton.vg = ((struct metadata_area *) dm_list_first(&baton.fid->metadata_areas_in_use))->ops->vg_read(baton.fid, lvmcache_vgname_from_info(info), NULL, 1);
+		baton.vg = ((struct metadata_area *) dm_list_first(&baton.fid->metadata_areas_in_use))->ops->vg_read(baton.fid, lvmcache_vgname_from_info(info), NULL, NULL, NULL, 1);
 
 	if (!baton.vg)
 		lvmcache_fmt(info)->ops->destroy_instance(baton.fid);
lib/config/config.c +3 -3
index b635654..58eab51 100644
@@ -482,14 +482,14 @@ int override_config_tree_from_profile(struct cmd_context *cmd,
 }
 
 /*
- * When skip_parse is set, the checksum of buffer is only matched
+ * When checksum_only is set, the checksum of buffer is only matched
  * and function avoids parsing of mda into config tree which
  * remains unmodified and should not be used.
  */
 int config_file_read_fd(struct dm_config_tree *cft, struct device *dev,
 			off_t offset, size_t size, off_t offset2, size_t size2,
 			checksum_fn_t checksum_fn, uint32_t checksum,
-			int skip_parse)
+			int checksum_only)
 {
 	char *fb, *fe;
 	int r = 0;
@@ -538,7 +538,7 @@ int config_file_read_fd(struct dm_config_tree *cft, struct device *dev,
 		goto out;
 	}
 
-	if (!skip_parse) {
+	if (!checksum_only) {
 		fe = fb + size + size2;
 		if (!dm_config_parse(cft, fb, fe))
 			goto_out;
lib/format1/format1.c +2 -0
index 19df8aa..1b86ef5 100644
@@ -180,6 +180,8 @@ out:
 static struct volume_group *_format1_vg_read(struct format_instance *fid,
 				     const char *vg_name,
 				     struct metadata_area *mda __attribute__((unused)),
+				     struct cached_vg_fmtdata **vg_fmtdata __attribute__((unused)),
+				     unsigned *use_previous_vg __attribute__((unused)),
 				     int single_device __attribute__((unused)))
 {
 	struct volume_group *vg;
lib/format_pool/format_pool.c +2 -0
index 503005a..2a8819d 100644
@@ -101,6 +101,8 @@ static int _check_usp(const char *vgname, struct user_subpool *usp, int sp_count
 static struct volume_group *_pool_vg_read(struct format_instance *fid,
 					  const char *vg_name,
 					  struct metadata_area *mda __attribute__((unused)),
+					  struct cached_vg_fmtdata **vg_fmtdata __attribute__((unused)),
+					  unsigned *use_previous_vg __attribute__((unused)),
 					  int single_device __attribute__((unused)))
 {
 	struct volume_group *vg;
lib/format_text/archiver.c +1 -1
index a2f40f2..e3d3d57 100644
@@ -308,7 +308,7 @@ struct volume_group *backup_read_vg(struct cmd_context *cmd,
 	}
 
 	dm_list_iterate_items(mda, &tf->metadata_areas_in_use) {
-		if (!(vg = mda->ops->vg_read(tf, vg_name, mda, 0)))
+		if (!(vg = mda->ops->vg_read(tf, vg_name, mda, NULL, NULL, 0)))
 			stack;
 		break;
 	}
lib/format_text/format-text.c +68 -59

266 lines not on this page · raw message

lib/format_text/import-export.h +14 -12
index 4b2756d..1ee647b 100644
@@ -18,6 +18,7 @@
 
 #include "config.h"
 #include "metadata.h"
+#include "lvmcache.h"
 
 #include <stdio.h>
 
@@ -49,10 +50,9 @@ struct text_vg_version_ops {
 					 unsigned use_cached_pvs);
 	void (*read_desc) (struct dm_pool * mem, const struct dm_config_tree *cf,
 			   time_t *when, char **desc);
-	const char *(*read_vgname) (const struct format_type *fmt,
-				    const struct dm_config_tree *cft,
-				    struct id *vgid, uint64_t *vgstatus,
-				    char **creation_host);
+	int (*read_vgname) (const struct format_type *fmt,
+			    const struct dm_config_tree *cft,
+			    struct lvmcache_vgsummary *vgsummary);
 };
 
 struct text_vg_version_ops *text_vg_vsn1_init(void);
@@ -70,6 +70,8 @@ struct volume_group *text_vg_import_file(struct format_instance *fid,
 					 time_t *when, char **desc);
 struct volume_group *text_vg_import_fd(struct format_instance *fid,
 				       const char *file,
+				       struct cached_vg_fmtdata **vg_fmtdata,
+				       unsigned *use_previous_vg,
 				       int single_device,
 				       struct device *dev,
 				       off_t offset, uint32_t size,
@@ -77,13 +79,13 @@ struct volume_group *text_vg_import_fd(struct format_instance *fid,
 				       checksum_fn_t checksum_fn,
 				       uint32_t checksum,
 				       time_t *when, char **desc);
-const char *text_vgname_import(const struct format_type *fmt,
-			       struct device *dev,
-			       off_t offset, uint32_t size,
-			       off_t offset2, uint32_t size2,
-			       checksum_fn_t checksum_fn, uint32_t checksum,
-			       const char *vgname,
-			       struct id *vgid, uint64_t *vgstatus,
-			       char **creation_host);
+
+int text_vgname_import(const struct format_type *fmt,
+		       struct device *dev,
+		       off_t offset, uint32_t size,
+		       off_t offset2, uint32_t size2,
+		       checksum_fn_t checksum_fn,
+		       int checksum_only,
+		       struct lvmcache_vgsummary *vgsummary);
 
 #endif
lib/format_text/import.c +46 -30

146 lines not on this page · raw message

lib/format_text/import_vsn1.c +10 -13
index 1569aaa..5e8e049 100644
@@ -941,19 +941,16 @@ static void _read_desc(struct dm_pool *mem,
 	*when = u;
 }
 
-static const char *_read_vgname(const struct format_type *fmt,
-				const struct dm_config_tree *cft, struct id *vgid,
-				uint64_t *vgstatus, char **creation_host)
+static int _read_vgname(const struct format_type *fmt, const struct dm_config_tree *cft, 
+			struct lvmcache_vgsummary *vgsummary)
 {
 	const struct dm_config_node *vgn;
 	struct dm_pool *mem = fmt->cmd->mem;
-	char *vgname;
 	int old_suppress;
 
 	old_suppress = log_suppress(2);
-	*creation_host = dm_pool_strdup(mem,
-					dm_config_find_str_allow_empty(cft->root,
-							"creation_host", ""));
+	vgsummary->creation_host =
+	    dm_pool_strdup(mem, dm_config_find_str_allow_empty(cft->root, "creation_host", ""));
 	log_suppress(old_suppress);
 
 	/* skip any top-level values */
@@ -964,23 +961,23 @@ static const char *_read_vgname(const struct format_type *fmt,
 		return 0;
 	}
 
-	if (!(vgname = dm_pool_strdup(mem, vgn->key)))
+	if (!(vgsummary->vgname = dm_pool_strdup(mem, vgn->key)))
 		return_0;
 
 	vgn = vgn->child;
 
-	if (!_read_id(vgid, vgn, "id")) {
-		log_error("Couldn't read uuid for volume group %s.", vgname);
+	if (!_read_id(&vgsummary->vgid, vgn, "id")) {
+		log_error("Couldn't read uuid for volume group %s.", vgsummary->vgname);
 		return 0;
 	}
 
-	if (!_read_flag_config(vgn, vgstatus, VG_FLAGS)) {
+	if (!_read_flag_config(vgn, &vgsummary->vgstatus, VG_FLAGS)) {
 		log_error("Couldn't find status flags for volume group %s.",
-			  vgname);
+			  vgsummary->vgname);
 		return 0;
 	}
 
-	return vgname;
+	return 1;
 }
 
 static struct text_vg_version_ops _vsn1_ops = {
lib/format_text/layout.h +4 -8
index 5f6b549..64fc0e1 100644
@@ -18,6 +18,7 @@
 
 #include "config.h"
 #include "metadata.h"
+#include "lvmcache.h"
 #include "uuid.h"
 
 /* disk_locn and data_area_list are defined in format-text.h */
@@ -97,13 +98,8 @@ struct mda_context {
 #define LVM2_LABEL "LVM2 001"
 #define MDA_SIZE_MIN (8 * (unsigned) lvm_getpagesize())
 
-
-const char *vgname_from_mda(const struct format_type *fmt,
-			    struct mda_header *mdah,
-			    struct device_area *dev_area,
-			    uint32_t *mda_checksum, size_t *mda_size,
-			    const char *vgname, struct id *vgid,
-			    uint64_t *vgstatus, char **creation_host,
-			    uint64_t *mda_free_sectors);
+int vgname_from_mda(const struct format_type *fmt, struct mda_header *mdah,
+		    struct device_area *dev_area, struct lvmcache_vgsummary *vgsummary,
+		    uint64_t *mda_free_sectors);
 
 #endif
lib/format_text/text_label.c +4 -8
index 3a4835c..599be41 100644
@@ -319,7 +319,7 @@ static int _update_mda(struct metadata_area *mda, void *baton)
 	const struct format_type *fmt = p->label->labeller->fmt;
 	struct mda_context *mdac = (struct mda_context *) mda->metadata_locn;
 	struct mda_header *mdah;
-	struct labeller *l = p->label->labeller;
+	struct lvmcache_vgsummary vgsummary = { 0 };
 
 	/*
 	 * Using the labeller struct to preserve info about
@@ -350,13 +350,9 @@ static int _update_mda(struct metadata_area *mda, void *baton)
 		return 1;
 	}
 
-	if ((l->vgname = vgname_from_mda(fmt, mdah, &mdac->area,
-					 &l->mda_checksum, &l->mda_size, l->vgname,
-					 &l->vgid, &l->vgstatus, &l->creation_host,
-					 &mdac->free_sectors)) &&
-	    !lvmcache_update_vgname_and_id(p->info, l->vgname,
-					   (char *) &l->vgid, l->vgstatus,
-					   l->creation_host)) {
+	if (vgname_from_mda(fmt, mdah, &mdac->area, &vgsummary,
+			     &mdac->free_sectors) &&
+	    !lvmcache_update_vgname_and_id(p->info, &vgsummary)) {
 		if (!dev_close(mdac->area.dev))
 			stack;
 		return_0;
lib/label/label.c +14 -10
index ce59da8..4510400 100644
@@ -97,6 +97,17 @@ struct labeller *label_get_handler(const char *name)
 	return NULL;
 }
 
+static void _update_lvmcache_orphan(struct lvmcache_info *info)
+{
+	struct lvmcache_vgsummary vgsummary_orphan = {
+		.vgname = lvmcache_fmt(info)->orphan_vg_name,
+	};
+
+        memcpy(&vgsummary_orphan.vgid, lvmcache_fmt(info)->orphan_vg_name, strlen(lvmcache_fmt(info)->orphan_vg_name));
+
+	lvmcache_update_vgname_and_id(info, &vgsummary_orphan);
+}
+
 static struct labeller *_find_labeller(struct device *dev, char *buf,
 				       uint64_t *label_sector,
 				       uint64_t scan_sector)
@@ -173,9 +184,7 @@ static struct labeller *_find_labeller(struct device *dev, char *buf,
       out:
 	if (!found) {
 		if ((info = lvmcache_info_from_pvid(dev->pvid, 0)))
-			lvmcache_update_vgname_and_id(info, lvmcache_fmt(info)->orphan_vg_name,
-						      lvmcache_fmt(info)->orphan_vg_name,
-						      0, NULL);
+			_update_lvmcache_orphan(info);
 		log_very_verbose("%s: No label detected", dev_name(dev));
 	}
 
@@ -271,9 +280,7 @@ int label_read(struct device *dev, struct label **result,
 		stack;
 
 		if ((info = lvmcache_info_from_pvid(dev->pvid, 0)))
-			lvmcache_update_vgname_and_id(info, lvmcache_fmt(info)->orphan_vg_name,
-						      lvmcache_fmt(info)->orphan_vg_name,
-						      0, NULL);
+			_update_lvmcache_orphan(info);
 
 		return r;
 	}
@@ -348,10 +355,7 @@ int label_verify(struct device *dev)
 
 	if (!dev_open_readonly(dev)) {
 		if ((info = lvmcache_info_from_pvid(dev->pvid, 0)))
-			lvmcache_update_vgname_and_id(info, lvmcache_fmt(info)->orphan_vg_name,
-						      lvmcache_fmt(info)->orphan_vg_name,
-						      0, NULL);
-
+			_update_lvmcache_orphan(info);
 		return_0;
 	}
 
lib/label/label.h +0 -8
index 831977f..253e1e8 100644
@@ -89,14 +89,6 @@ struct label_ops {
 struct labeller {
 	struct label_ops *ops;
 	const struct format_type *fmt;
-
-	/* Caching info */
-	const char *vgname;
-	struct id vgid;
-	uint64_t vgstatus;
-	char *creation_host;
-	uint32_t mda_checksum;
-	size_t mda_size;
 };
 
 int label_init(void);
lib/metadata/metadata-exported.h +0 -5
index 414216a..1555c87 100644
@@ -324,11 +324,6 @@ struct format_instance {
 	struct dm_list metadata_areas_ignored;
 	struct dm_hash_table *metadata_areas_index;
 
-	/* Remember last vg to avoid parsing same mda content for multiple PVs */
-	struct volume_group *vg;
-	uint32_t mda_checksum;
-	size_t mda_size;
-
 	void *private;
 };
 
lib/metadata/metadata.c +31 -8
index ed91948..33ce370 100644
@@ -3248,6 +3248,8 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,
 	char uuid[64] __attribute__((aligned(8)));
 	unsigned seqno = 0;
 	int reappeared = 0;
+	struct cached_vg_fmtdata *vg_fmtdata = NULL;	/* Additional format-specific data about the vg */
+	unsigned use_previous_vg;
 
 	if (is_orphan_vg(vgname)) {
 		if (use_precommitted) {
@@ -3334,12 +3336,20 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,
 	/* Ensure contents of all metadata areas match - else do recovery */
 	inconsistent_mda_count=0;
 	dm_list_iterate_items(mda, &fid->metadata_areas_in_use) {
+		use_previous_vg = 0;
 
 		if ((use_precommitted &&
-		     !(vg = mda->ops->vg_read_precommit(fid, vgname, mda))) ||
+		     !(vg = mda->ops->vg_read_precommit(fid, vgname, mda, &vg_fmtdata, &use_previous_vg)) && !use_previous_vg) ||
 		    (!use_precommitted &&
-		     !(vg = mda->ops->vg_read(fid, vgname, mda, 0)))) {
+		     !(vg = mda->ops->vg_read(fid, vgname, mda, &vg_fmtdata, &use_previous_vg, 0)) && !use_previous_vg)) {
 			inconsistent = 1;
+			vg_fmtdata = NULL;
+			continue;
+		}
+
+		/* Use previous VG because checksum matches */
+		if (!vg) {
+			vg = correct_vg;
 			continue;
 		}
 
@@ -3366,9 +3376,10 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,
 			}
 		}
 
-		if (vg != correct_vg)
-			/* NOTE: tied to fid->vg logic in text_vg_import_fd() */
+		if (vg != correct_vg) {
 			release_vg(vg);
+			vg_fmtdata = NULL;
+		}
 	}
 	fid->ref_count--;
 
@@ -3484,6 +3495,7 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,
 		 * but we failed to do so (so there's a dangling fid now).
 		 */
 		_destroy_fid(&fid);
+		vg_fmtdata = NULL;
 
 		inconsistent = 0;
 
@@ -3514,14 +3526,23 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,
 		/* Ensure contents of all metadata areas match - else recover */
 		inconsistent_mda_count=0;
 		dm_list_iterate_items(mda, &fid->metadata_areas_in_use) {
+			use_previous_vg = 0;
+
 			if ((use_precommitted &&
-			     !(vg = mda->ops->vg_read_precommit(fid, vgname,
-								mda))) ||
+			     !(vg = mda->ops->vg_read_precommit(fid, vgname, mda, &vg_fmtdata, &use_previous_vg)) && !use_previous_vg) ||
 			    (!use_precommitted &&
-			     !(vg = mda->ops->vg_read(fid, vgname, mda, 0)))) {
+			     !(vg = mda->ops->vg_read(fid, vgname, mda, &vg_fmtdata, &use_previous_vg, 0)) && !use_previous_vg)) {
 				inconsistent = 1;
+				vg_fmtdata = NULL;
 				continue;
 			}
+
+			/* Use previous VG because checksum matches */
+			if (!vg) {
+				vg = correct_vg;
+				continue;
+			}
+
 			if (!correct_vg) {
 				correct_vg = vg;
 				if (!_update_pv_list(cmd->mem, &all_pvs, correct_vg)) {
@@ -3564,8 +3585,10 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,
 				}
 			}
 
-			if (vg != correct_vg)
+			if (vg != correct_vg) {
 				release_vg(vg);
+				vg_fmtdata = NULL;
+			}
 		}
 		fid->ref_count--;
 
lib/metadata/metadata.h +6 -1
index 75abf63..031d19e 100644
@@ -72,6 +72,7 @@ struct dm_config_tree;
 struct metadata_area;
 struct alloc_handle;
 struct lvmcache_info;
+struct cached_vg_fmtdata;
 
 /* Per-format per-metadata area operations */
 struct metadata_area_ops {
@@ -79,10 +80,14 @@ struct metadata_area_ops {
 	struct volume_group *(*vg_read) (struct format_instance * fi,
 					 const char *vg_name,
 					 struct metadata_area * mda,
+					 struct cached_vg_fmtdata **vg_fmtdata,
+					 unsigned *use_previous_vg,
 					 int single_device);
 	struct volume_group *(*vg_read_precommit) (struct format_instance * fi,
 					 const char *vg_name,
-					 struct metadata_area * mda);
+					 struct metadata_area * mda,
+					 struct cached_vg_fmtdata **vg_fmtdata,
+					 unsigned *use_previous_vg);
 	/*
 	 * Write out complete VG metadata.  You must ensure internal
 	 * consistency before calling. eg. PEs can't refer to PVs not