Skip to contentSkip to conversation

master - filter: use bcache for filter reads

PATCH David Teigland 1 participant 1 message May 10, 2018

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=57bb46c5e7f8d6af1737af5ed1acae8abc37cded
Commit:        57bb46c5e7f8d6af1737af5ed1acae8abc37cded
Parent:        39ce38eb8868e367fa796ddb12b1020647d14c63
Author:        David Teigland <[email protected]>
AuthorDate:    Thu May 3 17:12:07 2018 -0500
Committer:     David Teigland <[email protected]>
CommitterDate: Thu May 10 16:03:19 2018 -0500

filter: use bcache for filter reads

Filters are still applied before any device reading or the label scan, but any filter checks that want to read the device are skipped and the device is flagged.

After bcache is populated, but before lvm looks for devices (i.e. before label scan), the filters are reapplied to the devices that were flagged above. The filters will then find the data they need in bcache.

---
 lib/cache/lvmcache.c             |    2 +-
 lib/cache/lvmetad.c              |    2 +-
 lib/commands/toolcontext.c       |    2 +-
 lib/commands/toolcontext.h       |    1 +
 lib/device/dev-cache.c           |   70 +++++++++++++++++++++++++++++-----
 lib/device/dev-io.c              |    4 +-
 lib/device/dev-luks.c            |   13 ++----
 lib/device/dev-md.c              |   67 +++++++++++++++++++++++++--------
 lib/device/dev-swap.c            |   16 ++-----
 lib/device/dev-type.c            |   18 +++------
 lib/device/dev-type.h            |    7 ++-
 lib/device/device.h              |    2 +
 lib/filters/filter-composite.c   |    9 +++-
 lib/filters/filter-md.c          |   39 ++++++++++++++++--
 lib/filters/filter-partitioned.c |   12 +++++-
 lib/filters/filter-persistent.c  |   78 ++++++++++++++++++++++++++++++-------
 lib/filters/filter-signature.c   |   12 +++---
 lib/filters/filter-usable.c      |    9 ----
 lib/filters/filter.h             |    2 +-
 lib/label/label.c                |   58 +++++++++++++++++++++++-----
 lib/label/label.h                |    2 +-
 lib/metadata/metadata-exported.h |    2 +-
 lib/metadata/metadata.c          |   19 ++-------
 tools/lvmdiskscan.c              |    5 +-
 tools/pvck.c                     |   48 +++++++++++++++++------
 tools/pvcreate.c                 |    3 +
 tools/pvscan.c                   |   54 +++++++++++++++++++++++---
 tools/vgcreate.c                 |    3 +
 tools/vgextend.c                 |    3 +
 29 files changed, 408 insertions(+), 154 deletions(-)
lib/cache/lvmcache.c +1 -1
index 745e219..290d733 100644
@@ -1320,7 +1320,7 @@ int lvmcache_label_rescan_vg(struct cmd_context *cmd, const char *vgname, const
 	/* FIXME: should we also rescan unused_duplicate_devs for devs
 	   being rescanned here and then repeat resolving the duplicates? */
 
-	label_scan_devs(cmd, &devs);
+	label_scan_devs(cmd, cmd->filter, &devs);
 
 	if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, vgid))) {
 		log_warn("VG info not found after rescan of %s", vgname);
lib/cache/lvmetad.c +1 -1
index f21829c..235f72b 100644
@@ -1878,7 +1878,7 @@ static struct volume_group *_lvmetad_pvscan_vg(struct cmd_context *cmd, struct v
 	 */
 	log_debug_lvmetad("Rescan VG %s scanning data from devs in previous metadata.", vg->name);
 
-	label_scan_devs(cmd, &pvs_scan);
+	label_scan_devs(cmd, cmd->full_filter, &pvs_scan);
 
 	/*
 	 * Check if any pvs_scan entries are no longer PVs.
lib/commands/toolcontext.c +1 -1
index 4fdd62f..e9f9357 100644
@@ -1144,7 +1144,7 @@ static struct dev_filter *_init_lvmetad_filter_chain(struct cmd_context *cmd)
 	/* md component filter. Optional, non-critical. */
 	if (find_config_tree_bool(cmd, devices_md_component_detection_CFG, NULL)) {
 		init_md_filtering(1);
-		if ((filters[nr_filt] = md_filter_create(cmd->dev_types)))
+		if ((filters[nr_filt] = md_filter_create(cmd, cmd->dev_types)))
 			nr_filt++;
 	}
 
lib/commands/toolcontext.h +1 -0
index 34222ee..83c8e82 100644
@@ -171,6 +171,7 @@ struct cmd_context {
 	unsigned pvscan_cache_single:1;
 	unsigned can_use_one_scan:1;
 	unsigned is_clvmd:1;
+	unsigned use_full_md_check:1;
 
 	/*
 	 * Filtering.
lib/device/dev-cache.c +60 -10

111 lines not on this page · raw message

lib/device/dev-io.c +2 -2
index 39d5d30..cce5a94 100644
@@ -358,11 +358,11 @@ static int _dev_get_size_dev(struct device *dev, uint64_t *size)
 	dev->size = *size;
 	dev->size_seqno = _dev_size_seqno;
 
+	log_very_verbose("%s: size is %" PRIu64 " sectors", name, *size);
+
 	if (do_close && !dev_close(dev))
 		log_sys_error("close", name);
 
-	log_very_verbose("%s: size is %" PRIu64 " sectors", name, *size);
-
 	return 1;
 }
 
lib/device/dev-luks.c +4 -9
index 8513e14..25a8b2c 100644
@@ -18,27 +18,22 @@
 #define LUKS_SIGNATURE "LUKS\xba\xbe"
 #define LUKS_SIGNATURE_SIZE 6
 
-int dev_is_luks(struct device *dev, uint64_t *offset_found)
+int dev_is_luks(struct device *dev, uint64_t *offset_found, int full)
 {
 	char buf[LUKS_SIGNATURE_SIZE];
 	int ret = -1;
 
-	if (!dev_open_readonly(dev)) {
-		stack;
-		return -1;
-	}
+	if (!scan_bcache)
+		return -EAGAIN;
 
 	if (offset_found)
 		*offset_found = 0;
 
-	if (!dev_read(dev, 0, LUKS_SIGNATURE_SIZE, DEV_IO_SIGNATURES, buf))
+	if (!dev_read_bytes(dev, 0, LUKS_SIGNATURE_SIZE, buf))
 		goto_out;
 
 	ret = memcmp(buf, LUKS_SIGNATURE, LUKS_SIGNATURE_SIZE) ? 0 : 1;
 
 out:
-	if (!dev_close(dev))
-		stack;
-
 	return ret;
 }
lib/device/dev-md.c +51 -16

116 lines not on this page · raw message

lib/device/dev-swap.c +5 -11
index a7ff10b..3bfb72b 100644
@@ -35,19 +35,17 @@ static int _swap_detect_signature(const char *buf)
 	return 0;
 }
 
-int dev_is_swap(struct device *dev, uint64_t *offset_found)
+int dev_is_swap(struct device *dev, uint64_t *offset_found, int full)
 {
 	char buf[10];
 	uint64_t size;
 	unsigned page;
 	int ret = 0;
 
-	if (!dev_get_size(dev, &size)) {
-		stack;
-		return -1;
-	}
+	if (!scan_bcache)
+		return -EAGAIN;
 
-	if (!dev_open_readonly(dev)) {
+	if (!dev_get_size(dev, &size)) {
 		stack;
 		return -1;
 	}
@@ -60,8 +58,7 @@ int dev_is_swap(struct device *dev, uint64_t *offset_found)
 			continue;
 		if (size < (page >> SECTOR_SHIFT))
 			break;
-		if (!dev_read(dev, page - SIGNATURE_SIZE,
-			      SIGNATURE_SIZE, DEV_IO_SIGNATURES, buf)) {
+		if (!dev_read_bytes(dev, page - SIGNATURE_SIZE, SIGNATURE_SIZE, buf)) {
 			ret = -1;
 			break;
 		}
@@ -73,9 +70,6 @@ int dev_is_swap(struct device *dev, uint64_t *offset_found)
 		}
 	}
 
-	if (!dev_close(dev))
-		stack;
-
 	return ret;
 }
 
lib/device/dev-type.c +6 -12
index 9923943..8d4a403 100644
@@ -365,7 +365,7 @@ static int _has_partition_table(struct device *dev)
 		uint16_t magic;
 	} __attribute__((packed)) buf; /* sizeof() == SECTOR_SIZE */
 
-	if (!dev_read(dev, UINT64_C(0), sizeof(buf), DEV_IO_SIGNATURES, &buf))
+	if (!dev_read_bytes(dev, UINT64_C(0), sizeof(buf), &buf))
 		return_0;
 
 	/* FIXME Check for other types of partition table too */
@@ -434,6 +434,9 @@ static int _native_dev_is_partitioned(struct dev_types *dt, struct device *dev)
 {
 	int r;
 
+	if (!scan_bcache)
+		return -EAGAIN;
+
 	if (!_is_partitionable(dt, dev))
 		return 0;
 
@@ -441,17 +444,8 @@ static int _native_dev_is_partitioned(struct dev_types *dt, struct device *dev)
 	if ((MAJOR(dev->dev) == dt->dasd_major) && dasd_is_cdl_formatted(dev))
 		return 1;
 
-	if (!dev_open_readonly_quiet(dev)) {
-		log_debug_devs("%s: failed to open device, considering device "
-			       "is partitioned", dev_name(dev));
-		return 1;
-	}
-
 	r = _has_partition_table(dev);
 
-	if (!dev_close(dev))
-		stack;
-
 	return r;
 }
 
@@ -750,12 +744,12 @@ out:
 
 static int _wipe_signature(struct device *dev, const char *type, const char *name,
 			   int wipe_len, int yes, force_t force, int *wiped,
-			   int (*signature_detection_fn)(struct device *dev, uint64_t *offset_found))
+			   int (*signature_detection_fn)(struct device *dev, uint64_t *offset_found, int full))
 {
 	int wipe;
 	uint64_t offset_found;
 
-	wipe = signature_detection_fn(dev, &offset_found);
+	wipe = signature_detection_fn(dev, &offset_found, 1);
 	if (wipe == -1) {
 		log_error("Fatal error while trying to detect %s on %s.",
 			  type, name);
lib/device/dev-type.h +4 -3
index e48e22b..843e254 100644
@@ -17,6 +17,7 @@
 
 #include "device.h"
 #include "display.h"
+#include "label.h"
 
 #define NUMBER_OF_MAJORS 4096
 
@@ -56,9 +57,9 @@ const char *dev_subsystem_name(struct dev_types *dt, struct device *dev);
 int major_is_scsi_device(struct dev_types *dt, int major);
 
 /* Signature/superblock recognition with position returned where found. */
-int dev_is_md(struct device *dev, uint64_t *sb);
-int dev_is_swap(struct device *dev, uint64_t *signature);
-int dev_is_luks(struct device *dev, uint64_t *signature);
+int dev_is_md(struct device *dev, uint64_t *sb, int full);
+int dev_is_swap(struct device *dev, uint64_t *signature, int full);
+int dev_is_luks(struct device *dev, uint64_t *signature, int full);
 int dasd_is_cdl_formatted(struct device *dev);
 int udev_dev_is_mpath_component(struct device *dev);
 
lib/device/device.h +2 -0
index 36d1e3e..fd8cbb7 100644
@@ -33,6 +33,8 @@
 #define DEV_NOT_O_NOATIME	0x00000400	/* Don't use O_NOATIME */
 #define DEV_IN_BCACHE		0x00000800      /* dev fd is open and used in bcache */
 #define DEV_BCACHE_EXCL		0x00001000      /* bcache_fd should be open EXCL */
+#define DEV_FILTER_AFTER_SCAN	0x00002000	/* apply filter after bcache has data */
+#define DEV_FILTER_OUT_SCAN	0x00004000	/* filtered out during label scan */
 
 /*
  * Support for external device info.
lib/filters/filter-composite.c +7 -2
index c635896..f15ff12 100644
@@ -15,14 +15,19 @@
 
 #include "lib.h"
 #include "filter.h"
+#include "device.h"
 
 static int _and_p(struct dev_filter *f, struct device *dev)
 {
 	struct dev_filter **filters;
+	int ret;
 
-	for (filters = (struct dev_filter **) f->private; *filters; ++filters)
-		if (!(*filters)->passes_filter(*filters, dev))
+	for (filters = (struct dev_filter **) f->private; *filters; ++filters) {
+		ret = (*filters)->passes_filter(*filters, dev);
+
+		if (!ret)
 			return 0;	/* No 'stack': a filter, not an error. */
+	}
 
 	return 1;
 }
lib/filters/filter-md.c +34 -5
index 7fac50a..bb8a7cf 100644
@@ -20,15 +20,21 @@
 
 #define MSG_SKIPPING "%s: Skipping md component device"
 
-static int _ignore_md(struct dev_filter *f __attribute__((unused)),
-		      struct device *dev)
+static int _ignore_md(struct device *dev, int full)
 {
 	int ret;
 	
 	if (!md_filtering())
 		return 1;
 	
-	ret = dev_is_md(dev, NULL);
+	ret = dev_is_md(dev, NULL, full);
+
+	if (ret == -EAGAIN) {
+		/* let pass, call again after scan */
+		dev->flags |= DEV_FILTER_AFTER_SCAN;
+		log_debug_devs("filter md deferred %s", dev_name(dev));
+		return 1;
+	}
 
 	if (ret == 1) {
 		if (dev->ext.src == DEV_EXT_NONE)
@@ -48,6 +54,18 @@ static int _ignore_md(struct dev_filter *f __attribute__((unused)),
 	return 1;
 }
 
+static int _ignore_md_lite(struct dev_filter *f __attribute__((unused)),
+		           struct device *dev)
+{
+	return _ignore_md(dev, 0);
+}
+
+static int _ignore_md_full(struct dev_filter *f __attribute__((unused)),
+		           struct device *dev)
+{
+	return _ignore_md(dev, 1);
+}
+
 static void _destroy(struct dev_filter *f)
 {
 	if (f->use_count)
@@ -56,7 +74,7 @@ static void _destroy(struct dev_filter *f)
 	dm_free(f);
 }
 
-struct dev_filter *md_filter_create(struct dev_types *dt)
+struct dev_filter *md_filter_create(struct cmd_context *cmd, struct dev_types *dt)
 {
 	struct dev_filter *f;
 
@@ -65,7 +83,18 @@ struct dev_filter *md_filter_create(struct dev_types *dt)
 		return NULL;
 	}
 
-	f->passes_filter = _ignore_md;
+	/*
+	 * FIXME: for commands that want a full md check (pvcreate, vgcreate,
+	 * vgextend), we do an extra read at the end of every device that the
+	 * filter looks at.  This isn't necessary; we only need to do the full
+	 * md check on the PVs that these commands are trying to use.
+	 */
+
+	if (cmd->use_full_md_check)
+		f->passes_filter = _ignore_md_full;
+	else
+		f->passes_filter = _ignore_md_lite;
+
 	f->destroy = _destroy;
 	f->use_count = 0;
 	f->private = dt;
lib/filters/filter-partitioned.c +11 -1
index f23fae6..5e1c4e8 100644
@@ -21,8 +21,18 @@
 static int _passes_partitioned_filter(struct dev_filter *f, struct device *dev)
 {
 	struct dev_types *dt = (struct dev_types *) f->private;
+	int ret;
 
-	if (dev_is_partitioned(dt, dev)) {
+	ret = dev_is_partitioned(dt, dev);
+
+	if (ret == -EAGAIN) {
+		/* let pass, call again after scan */
+		log_debug_devs("filter partitioned deferred %s", dev_name(dev));
+		dev->flags |= DEV_FILTER_AFTER_SCAN;
+		return 1;
+	}
+
+	if (ret) {
 		if (dev->ext.src == DEV_EXT_NONE)
 			log_debug_devs(MSG_SKIPPING, dev_name(dev));
 		else
lib/filters/filter-persistent.c +63 -15

104 lines not on this page · raw message

lib/filters/filter-signature.c +6 -6
index b426476..23b01e7 100644
@@ -26,14 +26,16 @@ static int _ignore_signature(struct dev_filter *f __attribute__((unused)),
 	char buf[BUFSIZE];
 	int ret = 0;
 
-	if (!dev_open_readonly(dev)) {
-		stack;
-		return -1;
+	if (!scan_bcache) {
+		/* let pass, call again after scan */
+		log_debug_devs("filter signature deferred %s", dev_name(dev));
+		dev->flags |= DEV_FILTER_AFTER_SCAN;
+		return 1;
 	}
 
 	memset(buf, 0, BUFSIZE);
 
-	if (!dev_read(dev, 0, BUFSIZE, DEV_IO_SIGNATURES, buf)) {
+	if (!dev_read_bytes(dev, 0, BUFSIZE, buf)) {
 		log_debug_devs("%s: Skipping: error in signature detection",
 			       dev_name(dev));
 		ret = 0;
@@ -54,8 +56,6 @@ static int _ignore_signature(struct dev_filter *f __attribute__((unused)),
 	ret = 1;
 
 out:
-	dev_close(dev);
-
 	return ret;
 }
 
lib/filters/filter-usable.c +0 -9
index 4ee2e9d..2de2a0f 100644
@@ -27,12 +27,6 @@ static int _native_check_pv_min_size(struct device *dev)
 	uint64_t size;
 	int ret = 0;
 
-	/* Check it's accessible */
-	if (!dev_open_readonly_quiet(dev)) {
-		log_debug_devs("%s: Skipping: open failed", dev_name(dev));
-		return 0;
-	}
-
 	/* Check it's not too small */
 	if (!dev_get_size(dev, &size)) {
 		log_debug_devs("%s: Skipping: dev_get_size failed", dev_name(dev));
@@ -47,9 +41,6 @@ static int _native_check_pv_min_size(struct device *dev)
 
 	ret = 1;
 out:
-	if (!dev_close(dev))
-		stack;
-
 	return ret;
 }
 
lib/filters/filter.h +1 -1
index 2f809dd..6245827 100644
@@ -23,7 +23,7 @@
 struct dev_filter *composite_filter_create(int n, int use_dev_ext_info, struct dev_filter **filters);
 
 struct dev_filter *lvm_type_filter_create(struct dev_types *dt);
-struct dev_filter *md_filter_create(struct dev_types *dt);
+struct dev_filter *md_filter_create(struct cmd_context *cmd, struct dev_types *dt);
 struct dev_filter *fwraid_filter_create(struct dev_types *dt);
 struct dev_filter *mpath_filter_create(struct dev_types *dt);
 struct dev_filter *partitioned_filter_create(struct dev_types *dt);
lib/label/label.c +47 -11

148 lines not on this page · raw message

lib/label/label.h +1 -1
index bccf777..3483321 100644
@@ -103,7 +103,7 @@ void label_destroy(struct label *label);
 extern struct bcache *scan_bcache;
 
 int label_scan(struct cmd_context *cmd);
-int label_scan_devs(struct cmd_context *cmd, struct dm_list *devs);
+int label_scan_devs(struct cmd_context *cmd, struct dev_filter *f, struct dm_list *devs);
 int label_scan_devs_excl(struct dm_list *devs);
 void label_scan_invalidate(struct device *dev);
 void label_scan_invalidate_lv(struct cmd_context *cmd, struct logical_volume *lv);
lib/metadata/metadata-exported.h +1 -1
index 120dbb0..97184ed 100644
@@ -723,7 +723,7 @@ int pv_resize_single(struct cmd_context *cmd,
 			     const uint64_t new_size,
 			     int yes);
 
-int pv_analyze(struct cmd_context *cmd, const char *pv_name,
+int pv_analyze(struct cmd_context *cmd, struct device *dev,
 	       uint64_t label_sector);
 
 /* FIXME: move internal to library */
lib/metadata/metadata.c +4 -15
index 16fd6fa..b8f1b97 100644
@@ -5059,30 +5059,19 @@ static int _analyze_mda(struct metadata_area *mda, void *baton)
  *  0 - fail
  *  1 - success
  */
-int pv_analyze(struct cmd_context *cmd, const char *pv_name,
+int pv_analyze(struct cmd_context *cmd, struct device *dev,
 	       uint64_t label_sector)
 {
 	struct label *label;
-	struct device *dev;
 	struct lvmcache_info *info;
 
-	dev = dev_cache_get(pv_name, cmd->filter);
-	if (!dev) {
-		log_error("Device %s %s.", pv_name, dev_cache_filtered_reason(pv_name));
-		return 0;
-	}
-
-	/*
-	 * First, scan for LVM labels.
-	 */
-	if (!label_read(dev, &label, label_sector)) {
-		log_error("Could not find LVM label on %s",
-			  pv_name);
+	if (!(label = lvmcache_get_dev_label(dev))) {
+		log_error("Could not find LVM label on %s", dev_name(dev));
 		return 0;
 	}
 
 	log_print("Found label on %s, sector %"PRIu64", type=%.8s",
-		  pv_name, label->sector, label->type);
+		  dev_name(dev), label->sector, label->type);
 
 	/*
 	 * Next, loop through metadata areas
tools/lvmdiskscan.c +3 -2
index 7e2fc88..772694c 100644
@@ -97,6 +97,9 @@ int lvmdiskscan(struct cmd_context *cmd, int argc __attribute__((unused)),
 	if (arg_is_set(cmd, lvmpartition_ARG))
 		log_warn("WARNING: only considering LVM devices");
 
+	/* Call before using dev_iter which uses filters which want bcache data. */
+	label_scan(cmd);
+
 	max_len = _get_max_dev_name_len(cmd->full_filter);
 
 	if (!(iter = dev_iter_create(cmd->full_filter, 0))) {
@@ -104,8 +107,6 @@ int lvmdiskscan(struct cmd_context *cmd, int argc __attribute__((unused)),
 		return ECMD_FAILED;
 	}
 
-	label_scan(cmd);
-
 	for (dev = dev_iter_get(iter); dev; dev = dev_iter_get(iter)) {
 		if (lvmcache_has_dev_info(dev)) {
 			if (!dev_get_size(dev, &size)) {
tools/pvck.c +36 -12

63 lines not on this page · raw message

tools/pvcreate.c +3 -0
index 73605a8..0af9d49 100644
@@ -147,6 +147,9 @@ int pvcreate(struct cmd_context *cmd, int argc, char **argv)
 	pp.pv_count = argc;
 	pp.pv_names = argv;
 
+	/* Check for old md signatures at the end of devices. */
+	cmd->use_full_md_check = 1;
+
 	/*
 	 * Needed to change the set of orphan PVs.
 	 * (disable afterward to prevent process_each_pv from doing
tools/pvscan.c +47 -7

94 lines not on this page · raw message

tools/vgcreate.c +3 -0
index 0c4c428..e9f9073 100644
@@ -65,6 +65,9 @@ int vgcreate(struct cmd_context *cmd, int argc, char **argv)
 		return_ECMD_FAILED;
 	cmd->lockd_gl_disable = 1;
 
+	/* Check for old md signatures at the end of devices. */
+	cmd->use_full_md_check = 1;
+
 	/*
 	 * Check if the VG name already exists.  This should be done before
 	 * creating PVs on any of the devices.
tools/vgextend.c +3 -0
index 35168e9..5287a36 100644
@@ -154,6 +154,9 @@ int vgextend(struct cmd_context *cmd, int argc, char **argv)
 	/* pvcreate within vgextend cannot be forced. */
 	pp->force = 0;
 
+	/* Check for old md signatures at the end of devices. */
+	cmd->use_full_md_check = 1;
+
 	/*
 	 * Needed to change the set of orphan PVs.
 	 * (disable afterward to prevent process_each_pv from doing