All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jim Rees <rees@umich.edu>
To: Benny Halevy <bhalevy@panasas.com>
Cc: linux-nfs@vger.kernel.org, peter honeyman <honey@citi.umich.edu>
Subject: [PATCH 3/5] disk signature fixes
Date: Tue, 30 Nov 2010 14:14:03 -0500	[thread overview]
Message-ID: <87fc19de9a71a12abc58bbad83298cabec55cc91.1291142529.git.rees@umich.edu> (raw)
In-Reply-To: <cover.1291142529.git.rees@umich.edu>

re-write decode_blk_signature to keep fd open
pretty print disk signature
remove unneeded syslog info messages

Signed-off-by: Jim Rees <rees@umich.edu>
---
 utils/blkmapd/device-process.c |  105 +++++++++++++++++++++++-----------------
 1 files changed, 60 insertions(+), 45 deletions(-)

diff --git a/utils/blkmapd/device-process.c b/utils/blkmapd/device-process.c
index a543769..4482bd5 100644
--- a/utils/blkmapd/device-process.c
+++ b/utils/blkmapd/device-process.c
@@ -47,6 +47,21 @@
 #include <linux/kdev_t.h>
 #include "device-discovery.h"
 
+static char *pretty_sig(char *sig, int siglen)
+{
+	static char rs[100];
+	unsigned int i;
+
+	if (siglen <= 4) {
+		memcpy(&i, sig, sizeof i);
+		sprintf(rs, "0x%0x", i);
+	} else {
+		memcpy(rs, sig, siglen);
+		rs[siglen] = '\0';
+	}
+	return rs;
+}
+
 uint32_t *blk_overflow(uint32_t * p, uint32_t * end, size_t nbytes)
 {
 	uint32_t *q = p + ((nbytes + 3) >> 2);
@@ -55,10 +70,10 @@ uint32_t *blk_overflow(uint32_t * p, uint32_t * end, size_t nbytes)
 	return p;
 }
 
-static int decode_blk_signature(uint32_t **pp, uint32_t *end,
+static int decode_blk_signature(uint32_t ** pp, uint32_t * end,
 				struct bl_sig *sig)
 {
-	int i, tmp;
+	int i, siglen;
 	uint32_t *p = *pp;
 
 	BLK_READBUF(p, end, 4);
@@ -73,19 +88,21 @@ static int decode_blk_signature(uint32_t **pp, uint32_t *end,
 		goto out_err;
 	}
 	for (i = 0; i < sig->si_num_comps; i++) {
+		struct bl_sig_comp *comp = &sig->si_comps[i];
+
 		BLK_READBUF(p, end, 12);
-		READ64(sig->si_comps[i].bs_offset);
-		READ32(tmp);
-		sig->si_comps[i].bs_length = tmp;
-		BLK_READBUF(p, end, tmp);
+		READ64(comp->bs_offset);
+		READ32(siglen);
+		comp->bs_length = siglen;
+		BLK_READBUF(p, end, siglen);
 		/* Note we rely here on fact that sig is used immediately
 		 * for mapping, then thrown away.
 		 */
-		sig->si_comps[i].bs_string = (char *)p;
+		comp->bs_string = (char *)p;
 		BL_LOG_INFO("%s: si_comps[%d]: bs_length %d, bs_string %s\n",
-			   __func__, i, sig->si_comps[i].bs_length,
-			   sig->si_comps[i].bs_string);
-		p += ((tmp + 3) >> 2);
+			    __func__, i, siglen,
+			    pretty_sig(comp->bs_string, siglen));
+		p += ((siglen + 3) >> 2);
 	}
 	*pp = p;
 	return 0;
@@ -93,50 +110,45 @@ static int decode_blk_signature(uint32_t **pp, uint32_t *end,
 	return -EIO;
 }
 
-/* Read signature from device
- * return 0: read successfully
- * return -1: error
+/*
+ * Read signature from device and compare to sig_comp
+ * return: 0=match, 1=no match, -1=error
  */
-int
-read_cmp_blk_sig(const char *dev_name, struct bl_sig_comp *comp,
-		 int64_t bs_offset)
+static int
+read_cmp_blk_sig(struct bl_disk *disk, int fd, struct bl_sig_comp *comp)
 {
-	int fd, ret = -1;
+	const char *dev_name = disk->valid_path->full_path;
+	int ret = -1;
+	ssize_t siglen = comp->bs_length;
+	int64_t bs_offset = comp->bs_offset;
 	char *sig = NULL;
 
-	fd = open(dev_name, O_RDONLY | O_LARGEFILE);
-	if (fd < 0) {
-		BL_LOG_ERR("%s could not be opened for read\n", dev_name);
-		goto error;
-	}
-
-	sig = (char *)malloc(comp->bs_length);
+	sig = (char *)malloc(siglen);
 	if (!sig) {
 		BL_LOG_ERR("%s: Out of memory\n", __func__);
-		goto error;
+		goto out;
 	}
 
+	if (bs_offset < 0)
+		bs_offset += (((int64_t) disk->size) << 9);
 	if (lseek64(fd, bs_offset, SEEK_SET) == -1) {
 		BL_LOG_ERR("File %s lseek error\n", dev_name);
-		goto error;
+		goto out;
 	}
 
-	if (read(fd, sig, comp->bs_length) != comp->bs_length) {
+	if (read(fd, sig, siglen) != siglen) {
 		BL_LOG_ERR("File %s read error\n", dev_name);
-		goto error;
+		goto out;
 	}
 
-	BL_LOG_INFO
-	    ("%s: %s sig: %s, bs_string: %s, bs_length: %d, bs_offset: %lld\n",
-	     __func__, dev_name, sig, comp->bs_string, comp->bs_length,
-	     (long long)bs_offset);
-	ret = memcmp(sig, comp->bs_string, comp->bs_length);
+	ret = memcmp(sig, comp->bs_string, siglen);
+	if (!ret)
+		BL_LOG_INFO("%s: %s sig %s at %lld\n", __func__, dev_name,
+			    pretty_sig(sig, siglen), (long long)bs_offset);
 
- error:
+ out:
 	if (sig)
 		free(sig);
-	if (fd >= 0)
-		close(fd);
 	return ret;
 }
 
@@ -146,22 +158,25 @@ read_cmp_blk_sig(const char *dev_name, struct bl_sig_comp *comp,
  */
 static int verify_sig(struct bl_disk *disk, struct bl_sig *sig)
 {
+	const char *dev_name = disk->valid_path->full_path;
 	struct bl_sig_comp *comp;
-	int i, ret;
-	int64_t bs_offset;
+	int fd, i, ret;
+
+	fd = open(dev_name, O_RDONLY | O_LARGEFILE);
+	if (fd < 0) {
+		BL_LOG_ERR("%s could not be opened for read\n", dev_name);
+		return 0;
+	}
 
 	for (i = 0; i < sig->si_num_comps; i++) {
 		comp = &sig->si_comps[i];
-		bs_offset = comp->bs_offset;
-		if (bs_offset < 0)
-			bs_offset += (((int64_t) disk->size) << 9);
-		BL_LOG_INFO("%s: bs_offset: %lld\n",
-			   __func__, (long long) bs_offset);
-		ret = read_cmp_blk_sig(disk->valid_path->full_path,
-				       comp, bs_offset);
+		ret = read_cmp_blk_sig(disk, fd, comp);
 		if (ret)
 			return 0;
 	}
+
+	if (fd >= 0)
+		close(fd);
 	return 1;
 }
 
-- 
1.7.1


  parent reply	other threads:[~2010-11-30 19:14 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-30 19:13 [PATCH 0/5] nfs-utils: various changes Jim Rees
2010-11-30 19:13 ` [PATCH 1/5] add blkmapd and spnfsd to list of build targets to ignore Jim Rees
2010-12-02 14:05   ` Benny Halevy
2010-11-30 19:13 ` [PATCH 2/5] Remove blkmapd config file, which is no longer used Jim Rees
2010-12-02 14:06   ` Benny Halevy
2010-11-30 19:14 ` Jim Rees [this message]
2010-11-30 19:14 ` [PATCH 4/5] various minor cleanups Jim Rees
2010-12-02 13:59   ` Benny Halevy
2010-12-02 14:11     ` Benny Halevy
2010-12-02 14:40       ` [PATCH] SQUASHME: decorate truncated signatures with "..." Benny Halevy
2010-12-02 14:41       ` [PATCH 4/5] various minor cleanups Jim Rees
2010-12-02 14:43         ` Benny Halevy
2010-12-02 16:10           ` Jim Rees
2010-12-02 14:35     ` [PATCH] SQUASHME: blkmapd: fix pretty_sig short sig endianess agnosticity Benny Halevy
2010-12-02 16:24       ` Jim Rees
2010-12-02 16:30         ` Benny Halevy
2010-12-02 16:58           ` Jim Rees
2010-11-30 19:14 ` [PATCH 5/5] device mapping fixes Jim Rees

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fc19de9a71a12abc58bbad83298cabec55cc91.1291142529.git.rees@umich.edu \
    --to=rees@umich.edu \
    --cc=bhalevy@panasas.com \
    --cc=honey@citi.umich.edu \
    --cc=linux-nfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.