Skip to contentSkip to conversation

master - device: Introduce dev_read_callback

PATCH Alasdair Kergon 1 participant 1 message Jan 6, 2018

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=5e7d3ad749d3de735ba213ad64637182afec2586
Commit:        5e7d3ad749d3de735ba213ad64637182afec2586
Parent:        946f07af3e8f97b0e48752ce1cd8de87d3456540
Author:        Alasdair G Kergon <[email protected]>
AuthorDate:    Sat Jan 6 00:33:09 2018 +0000
Committer:     Alasdair G Kergon <[email protected]>
CommitterDate: Sat Jan 6 02:40:12 2018 +0000

device: Introduce dev_read_callback

If it obtains the data, it passes it into the supplied callback function and returns 1. Otherwise the callback receives failed = 1.

Updated config_file_read_fd to use this and similarly return the data via a callback fn of its own.

---
 WHATS_NEW                |    1 +
 lib/config/config.c      |   72 ++++++++++++++++++++++++++--------------------
 lib/config/config.h      |    4 ++-
 lib/device/dev-io.c      |   27 +++++++++++++++++
 lib/device/device.h      |    8 ++++-
 lib/format_text/import.c |   39 +++++++++++++-----------
 6 files changed, 100 insertions(+), 51 deletions(-)
WHATS_NEW +1 -0
index fa79068..345c472 100644
@@ -1,5 +1,6 @@
 Version 2.02.178 - 
 =====================================
+  Refactor metadata reading code to use callback functions.
   Move memory allocation for the key dev_reads into the device layer.
 
 Version 2.02.177 - 18th December 2017
lib/config/config.c +41 -31

142 lines not on this page · raw message

lib/config/config.h +3 -1
index 5c8844a..9901003 100644
@@ -242,7 +242,9 @@ struct dm_config_tree *config_open(config_source_t source, const char *filename,
 int config_file_read_fd(struct dm_pool *mem, struct dm_config_tree *cft, struct device *dev, dev_io_reason_t reason,
 			off_t offset, size_t size, off_t offset2, size_t size2,
 			checksum_fn_t checksum_fn, uint32_t checksum,
-			int skip_parse, int no_dup_node_check);
+			int skip_parse, int no_dup_node_check,
+			lvm_callback_fn_t config_file_read_fd_callback, void *config_file_read_fd_context);
+
 int config_file_read(struct dm_pool *mem, struct dm_config_tree *cft);
 struct dm_config_tree *config_file_open_and_read(const char *config_file, config_source_t source,
 						 struct cmd_context *cmd);
lib/device/dev-io.c +27 -0
index 72d6768..22a0d4c 100644
@@ -763,6 +763,33 @@ char *dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t
 	return buf;
 }
 
+/* Callback fn is responsible for dm_free */
+int dev_read_callback(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason,
+		      lvm_callback_fn_t dev_read_callback_fn, void *callback_context)
+{
+	char *buf;
+	int r = 0;
+
+	if (!(buf = dm_malloc(len))) {
+		log_error("Buffer allocation failed for device read.");
+		goto out;
+	}
+
+	if (!_dev_read(dev, offset, len, reason, buf)) {
+		log_error("Read from %s failed", dev_name(dev));
+		dm_free(buf);
+		goto out;
+	}
+
+	r = 1;
+
+out:
+	if (dev_read_callback_fn)
+		dev_read_callback_fn(!r, callback_context, buf);
+
+	return r;
+}
+
 /* Read into supplied retbuf */
 int dev_read_buf(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, void *retbuf)
 {
lib/device/device.h +7 -1
index 04117d2..4349222 100644
@@ -33,7 +33,9 @@
 #define DEV_NOT_O_NOATIME	0x00000400	/* Don't use O_NOATIME */
 
 /*
- * Standard format for callback functions
+ * Standard format for callback functions.
+ * When provided, callback functions are called exactly once.
+ * If failed is set, data cannot be accessed.
  */
 typedef void (*lvm_callback_fn_t)(int failed, void *context, void *data);
 
@@ -155,6 +157,10 @@ char *dev_read(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t
 char *dev_read_circular(struct device *dev, uint64_t offset, size_t len,
 			uint64_t offset2, size_t len2, dev_io_reason_t reason);
 
+/* Passes the data to dev_read_callback_fn */
+int dev_read_callback(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason,
+		      lvm_callback_fn_t dev_read_callback_fn, void *callback_context);
+
 /* Read data and copy it into a supplied private buffer. */
 /* Only use for tiny reads or on unimportant code paths. */
 int dev_read_buf(struct device *dev, uint64_t offset, size_t len, dev_io_reason_t reason, void *retbuf);
lib/format_text/import.c +21 -18
index 64ef2be..a295b78 100644
@@ -46,6 +46,11 @@ static void _import_vgsummary(int failed, void *context, void *data)
 	struct import_vgsummary_params *ivsp = context;
 	struct text_vg_version_ops **vsn;
 
+	if (failed) {
+		ivsp->ret = 0;
+		goto_out;
+	}
+
 	if (ivsp->checksum_only)
 		/* Checksum matches already-cached content - no need to reparse. */
 		goto out;
@@ -100,23 +105,20 @@ int text_vgsummary_import(const struct format_type *fmt,
 	ivsp->vgsummary = vgsummary;
 	ivsp->ret = 1;
 
-	if (!dev && !config_file_read(fmt->cmd->mem, ivsp->cft)) {
-		log_error("Couldn't read volume group metadata.");
-		config_destroy(ivsp->cft);
-		return 0;
-	}
-
-	if (dev && !config_file_read_fd(fmt->cmd->mem, ivsp->cft, dev, reason, offset, size,
+	if (!dev) {
+		if (!config_file_read(fmt->cmd->mem, ivsp->cft)) {
+			log_error("Couldn't read volume group metadata.");
+			ivsp->ret = 0;
+		}
+		_import_vgsummary(!ivsp->ret, ivsp, NULL);
+	} else if (!config_file_read_fd(fmt->cmd->mem, ivsp->cft, dev, reason, offset, size,
 					offset2, size2, checksum_fn,
 					vgsummary->mda_checksum,
-					checksum_only, 1)) {
+					checksum_only, 1, &_import_vgsummary, ivsp)) {
 		log_error("Couldn't read volume group metadata.");
-		config_destroy(ivsp->cft);
 		return 0;
 	}
 
-	_import_vgsummary(0, ivsp, NULL);
-
 	return ivsp->ret;
 }
 
@@ -229,14 +231,15 @@ struct volume_group *text_vg_import_fd(struct format_instance *fid,
 		return_NULL;
 	}
 
-	if (dev && !config_file_read_fd(fid->mem, ivp->cft, dev, MDA_CONTENT_REASON(primary_mda), offset, size,
+	if (dev) {
+		if (!config_file_read_fd(fid->mem, ivp->cft, dev, MDA_CONTENT_REASON(primary_mda), offset, size,
 					offset2, size2, checksum_fn, checksum,
-					ivp->skip_parse, 1)) {
-		config_destroy(ivp->cft);
-		return_NULL;
-	}
-
-	_import_vg(0, ivp, NULL);
+					ivp->skip_parse, 1, &_import_vg, ivp)) {
+			config_destroy(ivp->cft);
+			return_NULL;
+		}
+	} else
+		_import_vg(0, ivp, NULL);
 
 	return ivp->vg;
 }