[Sheepdog] [PATCH] Revert "sheepdog: use qemu socket helper functions"

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Mon May 10 08:55:43 CEST 2010


This reverts commit ca3f41435015a90c1b4de9bdf64f197ede52fc84.

qemu socket helper functions don't retry when EINTR error occurs, so
we cannot use them.

Conflicts:

    block/sheepdog.c

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 Makefile         |    2 +-
 block/sheepdog.c |   95 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 93 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index e9eb2d2..1637736 100644
--- a/Makefile
+++ b/Makefile
@@ -103,7 +103,7 @@ qobject-obj-y += qerror.o
 # block-obj-y is code used by both qemu system emulation and qemu-img
 
 block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
-block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-sockets.o
+block-obj-y += nbd.o block.o aio.o aes.o osdep.o
 block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 block-obj-$(CONFIG_POSIX) += compatfd.o
diff --git a/block/sheepdog.c b/block/sheepdog.c
index 6c0dfe2..ced4bc5 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -8,8 +8,10 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
+#include <netdb.h>
+#include <netinet/tcp.h>
+
 #include "qemu-common.h"
-#include "qemu_socket.h"
 #include "block_int.h"
 
 #define SD_PROTO_VER 0x01
@@ -487,10 +489,75 @@ static int do_send_recv(int sockfd, struct iovec *iov, int len, int offset,
 
 static int connect_to_sdog(const char *addr)
 {
+	char buf[64];
+	char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+	char name[256], *p;
+	int fd, ret;
+	struct addrinfo hints, *res, *res0;
+	int port = 0;
+
 	if (!addr)
 		addr = SD_DEFAULT_ADDR;
 
-	return inet_connect(addr, SOCK_STREAM);
+	strcpy(name, addr);
+
+	p = name;
+	while (*p) {
+		if (*p == ':') {
+			*p++ = '\0';
+			break;
+		} else
+			p++;
+	}
+
+	if (*p == '\0') {
+		eprintf("cannot find a port number, %s\n", name);
+		return -1;
+	}
+	port = strtol(p, NULL, 10);
+	if (port == 0) {
+		eprintf("invalid port number, %s\n", p);
+		return -1;
+	}
+
+	memset(&hints, 0, sizeof(hints));
+	snprintf(buf, sizeof(buf), "%d", port);
+
+	hints.ai_socktype = SOCK_STREAM;
+
+	ret = getaddrinfo(name, buf, &hints, &res0);
+	if (ret) {
+		eprintf("unable to get address info %s, %m\n", name);
+		return -1;
+	}
+
+	for (res = res0; res; res = res->ai_next) {
+		ret = getnameinfo(res->ai_addr, res->ai_addrlen,
+				  hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
+				  NI_NUMERICHOST | NI_NUMERICSERV);
+		if (ret)
+			continue;
+
+		fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+		if (fd < 0)
+			continue;
+
+reconnect:
+		ret = connect(fd, res->ai_addr, res->ai_addrlen);
+		if (ret < 0) {
+			if (errno == EINTR)
+				goto reconnect;
+			break;
+		}
+
+		dprintf("connected to %s:%d\n", name, port);
+		goto success;
+	}
+	fd = -1;
+	eprintf("failed connect to %s:%d\n", name, port);
+success:
+	freeaddrinfo(res0);
+	return fd;
 }
 
 static int do_readv_writev(int sockfd, struct iovec *iov, int len,
@@ -690,6 +757,23 @@ static int aio_flush_request(void *opaque)
 	return nr_outstanding_aio_req((struct bdrv_sd_state *)opaque);
 }
 
+static int set_nonblocking(int fd)
+{
+	int ret;
+
+	ret = fcntl(fd, F_GETFL);
+	if (ret < 0) {
+		eprintf("can't fcntl (F_GETFL), %m\n");
+		close(fd);
+	} else {
+		ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
+		if (ret < 0)
+			eprintf("can't fcntl (O_NONBLOCK), %m\n");
+	}
+
+	return ret;
+}
+
 static int set_nodelay(int fd)
 {
 	int ret, opt;
@@ -709,7 +793,12 @@ static int get_sheep_fd(struct bdrv_sd_state *s)
 		return -1;
 	}
 
-	socket_set_nonblock(fd);
+	ret = set_nonblocking(fd);
+	if (ret) {
+		eprintf("%m\n");
+		close(fd);
+		return -1;
+	}
 
 	ret = set_nodelay(fd);
 	if (ret) {
-- 
1.5.6.5




More information about the sheepdog mailing list