ia64/xen-unstable

changeset 19795:4f779d41b0ba

blktap: re-enable blktap1 if blktap2 is disabled

This patch re-enables a useful blktap1 for users who disable blktap2.
Itremoves tapdisk and blktapctrl from blktap2, both of which cause
problems with blktap operation. In addition, this patch modifies xend
to check for blktap2 installation. If the blktap2 driver isn't
running (hopefully because the dom0 kernel option wasn't selected) we
fall back to blktap.

Signed-off-by: Dutch Meyer <dmeyer@cs.ubc.ca>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 18 10:29:58 2009 +0100 (2009-06-18)
parents 94bf7d4854eb
children 1c01814f9a25
files tools/blktap2/Makefile tools/blktap2/daemon/Makefile tools/blktap2/daemon/lib/Makefile tools/blktap2/daemon/lib/xs_api.c tools/blktap2/daemon/lib/xs_api.h tools/blktap2/daemon/tapdisk-channel.c tools/blktap2/daemon/tapdisk-daemon.c tools/blktap2/daemon/tapdisk-dispatch-common.c tools/blktap2/daemon/tapdisk-dispatch.h tools/blktap2/drivers/Makefile tools/blktap2/drivers/tapdisk.c tools/python/xen/xend/server/BlktapController.py
line diff
     1.1 --- a/tools/blktap2/Makefile	Thu Jun 18 10:27:21 2009 +0100
     1.2 +++ b/tools/blktap2/Makefile	Thu Jun 18 10:29:58 2009 +0100
     1.3 @@ -9,7 +9,6 @@ SUBDIRS-y += include
     1.4  SUBDIRS-y += lvm
     1.5  SUBDIRS-y += vhd
     1.6  SUBDIRS-y += drivers
     1.7 -SUBDIRS-y += daemon
     1.8  
     1.9  clean:
    1.10  	rm -rf *.a *.so *.o *.rpm $(LIB) *~ $(DEPS) TAGS
     2.1 --- a/tools/blktap2/daemon/Makefile	Thu Jun 18 10:27:21 2009 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,54 +0,0 @@
     2.4 -XEN_ROOT=../../../
     2.5 -BLKTAP_ROOT := ..
     2.6 -include $(XEN_ROOT)/tools/Rules.mk
     2.7 -
     2.8 -SUBDIRS-y    :=
     2.9 -SUBDIRS-y    += lib
    2.10 -
    2.11 -IBIN          = blktapctrl
    2.12 -INST_DIR      = $(SBINDIR)
    2.13 -
    2.14 -LIBS         := -lxenstore
    2.15 -LIBS         += -Llib
    2.16 -LIBS         += -lblktap
    2.17 -LIBS         += -lxenctrl
    2.18 -
    2.19 -ifneq ($(USE_SYSTEM_LIBRARIES),y)
    2.20 -INCLUDES     += -I $(XEN_LIBXC) -I $(XEN_XENSTORE)
    2.21 -LIBS         += -L $(XEN_LIBXC) -L $(XEN_XENSTORE)
    2.22 -endif
    2.23 -
    2.24 -OBJS         := tapdisk-dispatch-common.o
    2.25 -OBJS         += tapdisk-channel.o
    2.26 -
    2.27 -CFLAGS       += -Werror
    2.28 -CFLAGS       += -Wno-unused
    2.29 -CFLAGS       += -fno-strict-aliasing -fPIC
    2.30 -CFLAGS       += -Ilib -I../include -I../drivers -I../../include $(INCLUDES)
    2.31 -CFLAGS       += -D_GNU_SOURCE
    2.32 -CFLAGS       += -g
    2.33 -
    2.34 -# Get gcc to generate the dependencies for us.
    2.35 -CFLAGS       += -Wp,-MD,.$(@F).d
    2.36 -DEPS          = .*.d
    2.37 -
    2.38 -all: subdirs-all $(IBIN)
    2.39 -
    2.40 -LIBS_DEPENDS := lib/libblktap.a lib/libblktap.so
    2.41 -$(LIBS_DEPENDS):subdirs-all
    2.42 -
    2.43 -blktapctrl: tapdisk-daemon.c $(OBJS) $(LIBS_DEPENDS)
    2.44 -	$(CC) $(CFLAGS) -o blktapctrl tapdisk-daemon.c $(LDFLAGS) $(LIBS) $(OBJS)
    2.45 -
    2.46 -install: all
    2.47 -	$(MAKE) subdirs-install
    2.48 -	$(INSTALL_DIR) -p $(DESTDIR)$(INST_DIR)
    2.49 -	$(INSTALL_PROG) $(IBIN) $(DESTDIR)$(INST_DIR)
    2.50 -
    2.51 -clean: subdirs-clean
    2.52 -	rm -rf *.o *~ $(IBIN) $(DEPS) xen TAGS
    2.53 -
    2.54 -.PHONY: all clean install blktapctrl
    2.55 -
    2.56 --include $(DEPS)
    2.57 -
     3.1 --- a/tools/blktap2/daemon/lib/Makefile	Thu Jun 18 10:27:21 2009 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,69 +0,0 @@
     3.4 -XEN_ROOT=../../../../
     3.5 -BLKTAP_ROOT := ../../
     3.6 -include $(XEN_ROOT)/tools/Rules.mk
     3.7 -
     3.8 -MAJOR    = 3.1
     3.9 -MINOR    = 0
    3.10 -SONAME   = libblktap.so.$(MAJOR)
    3.11 -
    3.12 -BLKTAP_INSTALL_DIR = /usr/sbin
    3.13 -
    3.14 -LIBS     := -lxenstore
    3.15 -
    3.16 -ifneq ($(USE_SYSTEM_LIBRARIES),y)
    3.17 -INCLUDES += -I $(XEN_LIBXC) -I $(XEN_XENSTORE)
    3.18 -LIBS     += -L$(XEN_XENSTORE)
    3.19 -endif
    3.20 -
    3.21 -SRCS     :=
    3.22 -SRCS     += xs_api.c
    3.23 -CFLAGS   += -Werror
    3.24 -CFLAGS   += -Wno-unused
    3.25 -CFLAGS   += -fno-strict-aliasing -fPIC
    3.26 -# get asprintf():
    3.27 -CFLAGS   += -D _GNU_SOURCE
    3.28 -CFLAGS   += -g
    3.29 -CFLAGS   += -I../../include -I../../../include/ $(INCLUDES) 
    3.30 -
    3.31 -
    3.32 -# Get gcc to generate the dependencies for us.
    3.33 -CFLAGS  += -Wp,-MD,.$(@F).d
    3.34 -DEPS     = .*.d
    3.35 -
    3.36 -OBJS     = $(patsubst %.c,%.o,$(SRCS))
    3.37 -IBINS   :=
    3.38 -
    3.39 -LIB      = libblktap.a libblktap.so.$(MAJOR).$(MINOR)
    3.40 -
    3.41 -.PHONY: all
    3.42 -all: build
    3.43 -
    3.44 -.PHONY: build
    3.45 -build: libblktap.a
    3.46 -
    3.47 -.PHONY: libblktap
    3.48 -libblktap: libblktap.a
    3.49 -
    3.50 -install: all
    3.51 -	$(INSTALL_DIR) -p $(DESTDIR)$(LIBDIR)
    3.52 -	$(INSTALL_DATA) $(LIB) $(DESTDIR)$(LIBDIR)
    3.53 -	ln -sf libblktap.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)/libblktap.so.$(MAJOR)
    3.54 -	ln -sf libblktap.so.$(MAJOR) $(DESTDIR)$(LIBDIR)/libblktap.so
    3.55 -
    3.56 -clean:
    3.57 -	rm -rf *.a *.so* *.o *.rpm $(LIB) *~ $(DEPS) xen TAGS
    3.58 -
    3.59 -libblktap.a: $(OBJS) 
    3.60 -	$(CC) $(CFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,$(SONAME) $(SHLIB_CFLAGS) \
    3.61 -	      $(LDFLAGS) -o libblktap.so.$(MAJOR).$(MINOR) $^ $(LIBS)
    3.62 -	ln -sf libblktap.so.$(MAJOR).$(MINOR) libblktap.so.$(MAJOR)
    3.63 -	ln -sf libblktap.so.$(MAJOR) libblktap.so
    3.64 -	$(AR) rc $@ libblktap.so
    3.65 -
    3.66 -.PHONY: TAGS all build clean install libblktap
    3.67 -
    3.68 -TAGS:
    3.69 -	etags -t $(SRCS) *.h
    3.70 -
    3.71 --include $(DEPS)
    3.72 -
     4.1 --- a/tools/blktap2/daemon/lib/xs_api.c	Thu Jun 18 10:27:21 2009 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,323 +0,0 @@
     4.4 -/*
     4.5 - * xs_api.c
     4.6 - * 
     4.7 - * blocktap interface functions to xenstore
     4.8 - *
     4.9 - * (c) 2005 Andrew Warfield and Julian Chesterfield
    4.10 - *
    4.11 - *
    4.12 - * This program is free software; you can redistribute it and/or
    4.13 - * modify it under the terms of the GNU General Public License version 2
    4.14 - * as published by the Free Software Foundation; or, when distributed
    4.15 - * separately from the Linux kernel or incorporated into other
    4.16 - * software packages, subject to the following license:
    4.17 - *
    4.18 - * Permission is hereby granted, free of charge, to any person obtaining a copy
    4.19 - * of this source file (the "Software"), to deal in the Software without
    4.20 - * restriction, including without limitation the rights to use, copy, modify,
    4.21 - * merge, publish, distribute, sublicense, and/or sell copies of the Software,
    4.22 - * and to permit persons to whom the Software is furnished to do so, subject to
    4.23 - * the following conditions:
    4.24 - *
    4.25 - * The above copyright notice and this permission notice shall be included in
    4.26 - * all copies or substantial portions of the Software.
    4.27 - *
    4.28 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    4.29 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    4.30 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    4.31 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    4.32 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    4.33 - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    4.34 - * IN THE SOFTWARE.
    4.35 - *
    4.36 - */
    4.37 -
    4.38 -#include <time.h>
    4.39 -#include <stdio.h>
    4.40 -#include <errno.h>
    4.41 -#include <stdlib.h>
    4.42 -#include <string.h>
    4.43 -#include <stdarg.h>
    4.44 -#include <xs.h>
    4.45 -
    4.46 -#include "xs_api.h"
    4.47 -#include "blktaplib.h"
    4.48 -
    4.49 -#define DOMNAME "Domain-0"
    4.50 -#define BASE_DEV_VAL 2048
    4.51 -
    4.52 -static LIST_HEAD(watches);
    4.53 -
    4.54 -int
    4.55 -xs_gather(struct xs_handle *xs, const char *dir, ...)
    4.56 -{
    4.57 -	va_list ap;
    4.58 -	const char *name;
    4.59 -	char *path, **e;
    4.60 -	int ret = 0, num,i;
    4.61 -	unsigned int len;
    4.62 -	xs_transaction_t xth;
    4.63 -
    4.64 -again:
    4.65 -	if ((xth = xs_transaction_start(xs)) == XBT_NULL) {
    4.66 -		DPRINTF("unable to start xs trasanction\n");
    4.67 -		ret = ENOMEM;
    4.68 -		return ret;
    4.69 -	}
    4.70 -
    4.71 -	va_start(ap, dir);
    4.72 -	while ((ret == 0) && (name = va_arg(ap, char *)) != NULL) {
    4.73 -		char *p;
    4.74 -		const char *fmt = va_arg(ap, char *);
    4.75 -		void *result = va_arg(ap, void *);
    4.76 -		
    4.77 -		if (asprintf(&path, "%s/%s", dir, name) == -1) {
    4.78 -			EPRINTF("allocation error in xs_gather!\n");
    4.79 -			ret = ENOMEM;
    4.80 -			break;
    4.81 -		}
    4.82 -
    4.83 -		p = xs_read(xs, xth, path, &len);
    4.84 -		free(path);
    4.85 -
    4.86 -		if (!p) {
    4.87 -			ret = ENOENT;
    4.88 -			break;
    4.89 -		}
    4.90 -
    4.91 -		if (fmt) {
    4.92 -			if (sscanf(p, fmt, result) == 0)
    4.93 -				ret = EINVAL;
    4.94 -			free(p);
    4.95 -		} else
    4.96 -			*(char **)result = p;
    4.97 -	}
    4.98 -
    4.99 -	va_end(ap);
   4.100 -
   4.101 -	if (!xs_transaction_end(xs, xth, ret)) {
   4.102 -		if (ret == 0 && errno == EAGAIN)
   4.103 -			goto again;
   4.104 -		else
   4.105 -			ret = errno;
   4.106 -	}
   4.107 -
   4.108 -	return ret;
   4.109 -}
   4.110 -
   4.111 -/* Single printf and write: returns -errno or 0. */
   4.112 -int
   4.113 -xs_printf(struct xs_handle *h, const char *dir,
   4.114 -	  const char *node, const char *fmt, ...)
   4.115 -{
   4.116 -	int ret;
   4.117 -	va_list ap;
   4.118 -	char *buf, *path;
   4.119 -
   4.120 -	va_start(ap, fmt);
   4.121 -	ret = vasprintf(&buf, fmt, ap);
   4.122 -	va_end(ap);
   4.123 -
   4.124 -	if (ret == -1)
   4.125 -		return 0;
   4.126 -
   4.127 -	ret = asprintf(&path, "%s/%s", dir, node);
   4.128 -	if (ret == -1) {
   4.129 -		free(buf);
   4.130 -		return 0;
   4.131 -	}
   4.132 -
   4.133 -	ret = xs_write(h, XBT_NULL, path, buf, strlen(buf)+1);
   4.134 -
   4.135 -	free(buf);
   4.136 -	free(path);
   4.137 -
   4.138 -	return ret;
   4.139 -}
   4.140 -
   4.141 -int
   4.142 -xs_exists(struct xs_handle *h, const char *path)
   4.143 -{
   4.144 -	char **d;
   4.145 -	unsigned int num;
   4.146 -	xs_transaction_t xth;
   4.147 -
   4.148 -	if ((xth = xs_transaction_start(h)) == XBT_NULL) {
   4.149 -		EPRINTF("unable to start xs trasanction\n");
   4.150 -		return 0;
   4.151 -	}
   4.152 -
   4.153 -	d = xs_directory(h, xth, path, &num);
   4.154 -	xs_transaction_end(h, xth, 0);
   4.155 -	if (!d)
   4.156 -		return 0;
   4.157 -
   4.158 -	free(d);
   4.159 -	return 1;
   4.160 -}
   4.161 -
   4.162 -
   4.163 -
   4.164 -/**
   4.165 - * This assumes that the domain name we are looking for is unique. 
   4.166 - * Name parameter Domain-0 
   4.167 - */
   4.168 -char *
   4.169 -get_dom_domid(struct xs_handle *h)
   4.170 -{
   4.171 -	int i;
   4.172 -	xs_transaction_t xth;
   4.173 -	unsigned int num, len;
   4.174 -	char *val, *path, *domid, **e;
   4.175 -
   4.176 -	e     = NULL;
   4.177 -	domid = NULL;
   4.178 -
   4.179 -	if ((xth = xs_transaction_start(h)) == XBT_NULL) {
   4.180 -		EPRINTF("unable to start xs trasanction\n");
   4.181 -		return NULL;
   4.182 -	}
   4.183 -
   4.184 -	e = xs_directory(h, xth, "/local/domain", &num);
   4.185 -	if (e == NULL)
   4.186 -		goto done;
   4.187 -
   4.188 -	for (i = 0; (i < num) && (domid == NULL); i++) {
   4.189 -		if (asprintf(&path, "/local/domain/%s/name", e[i]) == -1)
   4.190 -			break;
   4.191 -
   4.192 -		val = xs_read(h, xth, path, &len);
   4.193 -		free(path);
   4.194 -		if (val == NULL)
   4.195 -			continue;
   4.196 -
   4.197 -		if (strcmp(val, DOMNAME) == 0) {
   4.198 -			/* match! */
   4.199 -			if (asprintf(&path, 
   4.200 -				     "/local/domain/%s/domid", e[i]) == -1) {
   4.201 -				free(val);
   4.202 -				break;
   4.203 -			}
   4.204 -			domid = xs_read(h, xth, path, &len);
   4.205 -			free(path);
   4.206 -		}
   4.207 -		free(val);
   4.208 -	}
   4.209 -
   4.210 - done:
   4.211 -	xs_transaction_end(h, xth, 0);
   4.212 -	free(e);
   4.213 -	return domid;
   4.214 -}
   4.215 -
   4.216 -/*
   4.217 - * a little paranoia: we don't just trust token
   4.218 - */
   4.219 -static struct xenbus_watch *find_watch(const char *token)
   4.220 -{
   4.221 -	int ret;
   4.222 -	long nonce;
   4.223 -	unsigned long addr;
   4.224 -	struct xenbus_watch *i, *cmp;
   4.225 -
   4.226 -	ret = sscanf(token, "%lX:%lX", &addr, &nonce);
   4.227 -	if (ret != 2) {
   4.228 -		EPRINTF("invalid watch token %s\n", token);
   4.229 -		return NULL;
   4.230 -	}
   4.231 -
   4.232 -	cmp = (struct xenbus_watch *)addr;
   4.233 -	list_for_each_entry(i, &watches, list)
   4.234 -		if (i == cmp && i->nonce == nonce)
   4.235 -			return i;
   4.236 -
   4.237 -	return NULL;
   4.238 -}
   4.239 -
   4.240 -/*
   4.241 - * Register callback to watch this node;
   4.242 - * like xs_watch, return 0 on failure
   4.243 - */
   4.244 -int register_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch)
   4.245 -{
   4.246 -	/* Pointer in ascii is the token. */
   4.247 -	char token[(sizeof(watch) + sizeof(long)) * 2 + 2];
   4.248 -
   4.249 -	/* 1-second granularity should suffice here */
   4.250 -	watch->nonce = time(NULL);
   4.251 -
   4.252 -	sprintf(token, "%lX:%lX", (long)watch, watch->nonce);
   4.253 -	if (find_watch(token)) {
   4.254 -		EPRINTF("watch collision!\n");
   4.255 -		return -EINVAL;
   4.256 -	}
   4.257 -
   4.258 -	if (!xs_watch(h, watch->node, token)) {
   4.259 -		EPRINTF("unable to set watch!\n");
   4.260 -		return -EINVAL;
   4.261 -	}
   4.262 -
   4.263 -	list_add(&watch->list, &watches);
   4.264 -
   4.265 -	return 0;
   4.266 -}
   4.267 -
   4.268 -int unregister_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch)
   4.269 -{
   4.270 -	char token[(sizeof(watch) + sizeof(long)) * 2 + 2];
   4.271 -
   4.272 -	sprintf(token, "%lX:%lX", (long)watch, watch->nonce);
   4.273 -	if (!find_watch(token)) {
   4.274 -		EPRINTF("no such watch!\n");
   4.275 -		return -EINVAL;
   4.276 -	}
   4.277 -
   4.278 -	if (!xs_unwatch(h, watch->node, token))
   4.279 -		EPRINTF("XENBUS Failed to release watch %s\n", watch->node);
   4.280 -
   4.281 -	list_del(&watch->list);
   4.282 -
   4.283 -	return 0;
   4.284 -}
   4.285 -
   4.286 -/*
   4.287 - * re-register callbacks to all watches
   4.288 - */
   4.289 -void reregister_xenbus_watches(struct xs_handle *h)
   4.290 -{
   4.291 -	struct xenbus_watch *watch;
   4.292 -	char token[(sizeof(watch) + sizeof(long)) * 2 + 2];
   4.293 -
   4.294 -	list_for_each_entry(watch, &watches, list) {
   4.295 -		sprintf(token, "%lX:%lX", (long)watch, watch->nonce);
   4.296 -		xs_watch(h, watch->node, token);
   4.297 -	}
   4.298 -}
   4.299 -
   4.300 -/*
   4.301 - * based on watch_thread() 
   4.302 - */
   4.303 -int xs_fire_next_watch(struct xs_handle *h)
   4.304 -{
   4.305 -	unsigned int num;
   4.306 -	struct xenbus_watch *w;
   4.307 -	char **res, *token, *node = NULL;
   4.308 -
   4.309 -	res = xs_read_watch(h, &num);
   4.310 -	if (res == NULL) 
   4.311 -		return -EAGAIN; /* in O_NONBLOCK, read_watch returns 0... */
   4.312 -
   4.313 -	node  = res[XS_WATCH_PATH];
   4.314 -	token = res[XS_WATCH_TOKEN];
   4.315 -	DPRINTF("got watch %s on %s\n", token, node);
   4.316 -
   4.317 -	w = find_watch(token);
   4.318 -	if (w) 
   4.319 -		w->callback(h, w, node);
   4.320 -
   4.321 -	DPRINTF("handled watch %s on %s\n", token, node);
   4.322 -
   4.323 -	free(res);
   4.324 -
   4.325 -	return 1;
   4.326 -}
     5.1 --- a/tools/blktap2/daemon/lib/xs_api.h	Thu Jun 18 10:27:21 2009 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,62 +0,0 @@
     5.4 -/*
     5.5 - * xs_api.h
     5.6 - *
     5.7 - * (c) 2005 Andrew Warfield and Julian Chesterfield
     5.8 - *
     5.9 - *
    5.10 - * This program is free software; you can redistribute it and/or
    5.11 - * modify it under the terms of the GNU General Public License version 2
    5.12 - * as published by the Free Software Foundation; or, when distributed
    5.13 - * separately from the Linux kernel or incorporated into other
    5.14 - * software packages, subject to the following license:
    5.15 - *
    5.16 - * Permission is hereby granted, free of charge, to any person obtaining a copy
    5.17 - * of this source file (the "Software"), to deal in the Software without
    5.18 - * restriction, including without limitation the rights to use, copy, modify,
    5.19 - * merge, publish, distribute, sublicense, and/or sell copies of the Software,
    5.20 - * and to permit persons to whom the Software is furnished to do so, subject to
    5.21 - * the following conditions:
    5.22 - *
    5.23 - * The above copyright notice and this permission notice shall be included in
    5.24 - * all copies or substantial portions of the Software.
    5.25 - *
    5.26 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    5.27 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    5.28 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    5.29 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    5.30 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    5.31 - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    5.32 - * IN THE SOFTWARE.
    5.33 - */
    5.34 -
    5.35 -#ifndef _XS_API_H_
    5.36 -#define _XS_API_H_
    5.37 -
    5.38 -#include <xs.h>
    5.39 -
    5.40 -#include "list.h"
    5.41 -
    5.42 -struct xenbus_watch
    5.43 -{
    5.44 -        struct list_head  list;
    5.45 -        char             *node;
    5.46 -	void             *data;
    5.47 -	long              nonce;
    5.48 -        void (*callback) (struct xs_handle *h, 
    5.49 -			  struct xenbus_watch *, 
    5.50 -			  const  char *node);
    5.51 -};
    5.52 -
    5.53 -int xs_gather(struct xs_handle *xs, const char *dir, ...);
    5.54 -int xs_printf(struct xs_handle *h, const char *dir, const char *node, 
    5.55 -	      const char *fmt, ...) __attribute__((format(printf, 4, 5)));
    5.56 -int xs_exists(struct xs_handle *h, const char *path);
    5.57 -char *get_dom_domid(struct xs_handle *h);
    5.58 -int convert_dev_name_to_num(char *name);
    5.59 -
    5.60 -int register_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch);
    5.61 -int unregister_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch);
    5.62 -void reregister_xenbus_watches(struct xs_handle *h);
    5.63 -int xs_fire_next_watch(struct xs_handle *h);
    5.64 -
    5.65 -#endif
     6.1 --- a/tools/blktap2/daemon/tapdisk-channel.c	Thu Jun 18 10:27:21 2009 +0100
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,1367 +0,0 @@
     6.4 -/* Copyright (c) 2008, XenSource Inc.
     6.5 - * All rights reserved.
     6.6 - *
     6.7 - * Redistribution and use in source and binary forms, with or without
     6.8 - * modification, are permitted provided that the following conditions are met:
     6.9 - *     * Redistributions of source code must retain the above copyright
    6.10 - *       notice, this list of conditions and the following disclaimer.
    6.11 - *     * Redistributions in binary form must reproduce the above copyright
    6.12 - *       notice, this list of conditions and the following disclaimer in the
    6.13 - *       documentation and/or other materials provided with the distribution.
    6.14 - *     * Neither the name of XenSource Inc. nor the names of its contributors
    6.15 - *       may be used to endorse or promote products derived from this software
    6.16 - *       without specific prior written permission.
    6.17 - *
    6.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    6.19 - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    6.20 - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    6.21 - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    6.22 - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    6.23 - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    6.24 - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    6.25 - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    6.26 - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    6.27 - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    6.28 - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    6.29 -*/
    6.30 -#include <stdio.h>
    6.31 -#include <errno.h>
    6.32 -#include <fcntl.h>
    6.33 -#include <stdlib.h>
    6.34 -#include <unistd.h>
    6.35 -#include <string.h>
    6.36 -#include <stdarg.h>
    6.37 -#include <sys/wait.h>
    6.38 -#include <sys/ioctl.h>
    6.39 -#include <sys/resource.h>
    6.40 -
    6.41 -#include <xs.h>
    6.42 -#include "disktypes.h"
    6.43 -#include "tapdisk-dispatch.h"
    6.44 -
    6.45 -#define TAPDISK_CHANNEL_IDLE          1
    6.46 -#define TAPDISK_CHANNEL_WAIT_PID      2
    6.47 -#define TAPDISK_CHANNEL_WAIT_OPEN     3
    6.48 -#define TAPDISK_CHANNEL_WAIT_PAUSE    4
    6.49 -#define TAPDISK_CHANNEL_WAIT_RESUME   5
    6.50 -#define TAPDISK_CHANNEL_WAIT_CLOSE    6
    6.51 -#define TAPDISK_CHANNEL_CLOSED        7
    6.52 -
    6.53 -static void tapdisk_channel_error(tapdisk_channel_t *,
    6.54 -				  const char *fmt, ...)
    6.55 -  __attribute__((format(printf, 2, 3)));
    6.56 -static void tapdisk_channel_fatal(tapdisk_channel_t *,
    6.57 -				  const char *fmt, ...)
    6.58 -  __attribute__((format(printf, 2, 3)));
    6.59 -static int tapdisk_channel_parse_params(tapdisk_channel_t *);
    6.60 -static void tapdisk_channel_pause_event(struct xs_handle *,
    6.61 -					struct xenbus_watch *,
    6.62 -					const char *);
    6.63 -
    6.64 -static int
    6.65 -tapdisk_channel_check_uuid(tapdisk_channel_t *channel)
    6.66 -{
    6.67 -	uint32_t uuid;
    6.68 -	char *uuid_str;
    6.69 -
    6.70 -	uuid_str = xs_read(channel->xsh, XBT_NULL, channel->uuid_str, NULL);
    6.71 -	if (!uuid_str)
    6.72 -		return -errno;
    6.73 -
    6.74 -	uuid = strtoul(uuid_str, NULL, 10);
    6.75 -	free(uuid_str);
    6.76 -
    6.77 -	if (uuid != channel->cookie)
    6.78 -		return -EINVAL;
    6.79 -
    6.80 -	return 0;
    6.81 -}
    6.82 -
    6.83 -static inline int
    6.84 -tapdisk_channel_validate_watch(tapdisk_channel_t *channel, const char *path)
    6.85 -{
    6.86 -	int err, len;
    6.87 -
    6.88 -	len = strsep_len(path, '/', 7);
    6.89 -	if (len < 0)
    6.90 -		return -EINVAL;
    6.91 -
    6.92 -	err = tapdisk_channel_check_uuid(channel);
    6.93 -	if (err)
    6.94 -		return err;
    6.95 -
    6.96 -	if (!xs_exists(channel->xsh, path))
    6.97 -		return -ENOENT;
    6.98 -
    6.99 -	return 0;
   6.100 -}
   6.101 -
   6.102 -static inline int
   6.103 -tapdisk_channel_validate_message(tapdisk_channel_t *channel,
   6.104 -				 tapdisk_message_t *message)
   6.105 -{
   6.106 -	switch (message->type) {
   6.107 -	case TAPDISK_MESSAGE_PID_RSP:
   6.108 -		if (channel->state != TAPDISK_CHANNEL_WAIT_PID)
   6.109 -			return -EINVAL;
   6.110 -		break;
   6.111 -
   6.112 -	case TAPDISK_MESSAGE_OPEN_RSP:
   6.113 -		if (channel->state != TAPDISK_CHANNEL_WAIT_OPEN)
   6.114 -			return -EINVAL;
   6.115 -		break;
   6.116 -
   6.117 -	case TAPDISK_MESSAGE_PAUSE_RSP:
   6.118 -		if (channel->state != TAPDISK_CHANNEL_WAIT_PAUSE)
   6.119 -			return -EINVAL;
   6.120 -		break;
   6.121 -
   6.122 -	case TAPDISK_MESSAGE_RESUME_RSP:
   6.123 -		if (channel->state != TAPDISK_CHANNEL_WAIT_RESUME)
   6.124 -			return -EINVAL;
   6.125 -		break;
   6.126 -
   6.127 -	case TAPDISK_MESSAGE_CLOSE_RSP:
   6.128 -		if (channel->state != TAPDISK_CHANNEL_WAIT_CLOSE)
   6.129 -			return -EINVAL;
   6.130 -		break;
   6.131 -
   6.132 -	case TAPDISK_MESSAGE_RUNTIME_ERROR:
   6.133 -		/*
   6.134 -		 * runtime errors can be received at any time
   6.135 -		 * and should not affect the state machine
   6.136 -		 */
   6.137 -		return 0;
   6.138 -	}
   6.139 -
   6.140 -	channel->state = TAPDISK_CHANNEL_IDLE;
   6.141 -	return 0;
   6.142 -}
   6.143 -
   6.144 -static int
   6.145 -tapdisk_channel_send_message(tapdisk_channel_t *channel,
   6.146 -			     tapdisk_message_t *message, int timeout)
   6.147 -{
   6.148 -	fd_set writefds;
   6.149 -	struct timeval tv;
   6.150 -	int ret, len, offset;
   6.151 -
   6.152 -	tv.tv_sec  = timeout;
   6.153 -	tv.tv_usec = 0;
   6.154 -	offset     = 0;
   6.155 -	len        = sizeof(tapdisk_message_t);
   6.156 -
   6.157 -	DPRINTF("%s: sending '%s' message to %d:%d\n",
   6.158 -		channel->path, tapdisk_message_name(message->type),
   6.159 -		channel->channel_id, channel->cookie);
   6.160 -
   6.161 -	if (channel->state != TAPDISK_CHANNEL_IDLE &&
   6.162 -	    message->type  != TAPDISK_MESSAGE_CLOSE)
   6.163 -		EPRINTF("%s: writing message to non-idle channel (%d)\n",
   6.164 -			channel->path, channel->state);
   6.165 -
   6.166 -	while (offset < len) {
   6.167 -		FD_ZERO(&writefds);
   6.168 -		FD_SET(channel->write_fd, &writefds);
   6.169 -
   6.170 -		/* we don't bother reinitializing tv. at worst, it will wait a
   6.171 -		 * bit more time than expected. */
   6.172 -
   6.173 -		ret = select(channel->write_fd + 1,
   6.174 -			     NULL, &writefds, NULL, &tv);
   6.175 -		if (ret == -1)
   6.176 -			break;
   6.177 -		else if (FD_ISSET(channel->write_fd, &writefds)) {
   6.178 -			ret = write(channel->write_fd,
   6.179 -				    message + offset, len - offset);
   6.180 -			if (ret <= 0)
   6.181 -				break;
   6.182 -			offset += ret;
   6.183 -		} else
   6.184 -			break;
   6.185 -	}
   6.186 -
   6.187 -	if (offset != len) {
   6.188 -		EPRINTF("%s: error writing '%s' message to %d:%d\n",
   6.189 -			channel->path, tapdisk_message_name(message->type),
   6.190 -			channel->channel_id, channel->cookie);
   6.191 -		return -EIO;
   6.192 -	}
   6.193 -
   6.194 -	switch (message->type) {
   6.195 -	case TAPDISK_MESSAGE_PID:
   6.196 -		channel->state = TAPDISK_CHANNEL_WAIT_PID;
   6.197 -		break;
   6.198 -
   6.199 -	case TAPDISK_MESSAGE_OPEN:
   6.200 -		channel->state = TAPDISK_CHANNEL_WAIT_OPEN;
   6.201 -		break;
   6.202 -
   6.203 -	case TAPDISK_MESSAGE_PAUSE:
   6.204 -		channel->state = TAPDISK_CHANNEL_WAIT_PAUSE;
   6.205 -		break;
   6.206 -
   6.207 -	case TAPDISK_MESSAGE_RESUME:
   6.208 -		channel->state = TAPDISK_CHANNEL_WAIT_RESUME;
   6.209 -		break;
   6.210 -
   6.211 -	case TAPDISK_MESSAGE_CLOSE:
   6.212 -		channel->state = TAPDISK_CHANNEL_WAIT_CLOSE;
   6.213 -		break;
   6.214 -
   6.215 -	default:
   6.216 -		EPRINTF("%s: unrecognized message type %d\n",
   6.217 -			channel->path, message->type);
   6.218 -	}
   6.219 -
   6.220 -	return 0;
   6.221 -}
   6.222 -
   6.223 -static void
   6.224 -__tapdisk_channel_error(tapdisk_channel_t *channel,
   6.225 -			const char *fmt, va_list ap)
   6.226 -{
   6.227 -	int err;
   6.228 -	char *dir, *buf, *message;
   6.229 -
   6.230 -	err = vasprintf(&buf, fmt, ap);
   6.231 -	if (err == -1) {
   6.232 -		EPRINTF("failed to allocate error message\n");
   6.233 -		buf = NULL;
   6.234 -	}
   6.235 -
   6.236 -	if (buf)
   6.237 -		message = buf;
   6.238 -	else
   6.239 -		message = "tapdisk error";
   6.240 -
   6.241 -	EPRINTF("%s: %s\n", channel->path, message);
   6.242 -
   6.243 -	err = asprintf(&dir, "%s/tapdisk-error", channel->path);
   6.244 -	if (err == -1) {
   6.245 -		EPRINTF("%s: failed to write %s\n", __func__, message);
   6.246 -		dir = NULL;
   6.247 -		goto out;
   6.248 -	}
   6.249 -
   6.250 -	xs_write(channel->xsh, XBT_NULL, dir, message, strlen(message));
   6.251 -
   6.252 -out:
   6.253 -	free(dir);
   6.254 -	free(buf);
   6.255 -}
   6.256 -
   6.257 -static void
   6.258 -tapdisk_channel_error(tapdisk_channel_t *channel, const char *fmt, ...)
   6.259 -{
   6.260 -	va_list ap;
   6.261 -
   6.262 -	va_start(ap, fmt);
   6.263 -	__tapdisk_channel_error(channel, fmt, ap);
   6.264 -	va_end(ap);
   6.265 -}
   6.266 -
   6.267 -static void
   6.268 -tapdisk_channel_fatal(tapdisk_channel_t *channel, const char *fmt, ...)
   6.269 -{
   6.270 -	va_list ap;
   6.271 -
   6.272 -	va_start(ap, fmt);
   6.273 -	__tapdisk_channel_error(channel, fmt, ap);
   6.274 -	va_end(ap);
   6.275 -
   6.276 -	tapdisk_channel_close(channel);
   6.277 -}
   6.278 -
   6.279 -static int
   6.280 -tapdisk_channel_connect_backdev(tapdisk_channel_t *channel)
   6.281 -{
   6.282 -	int err, major, minor;
   6.283 -	char *s, *path, *devname;
   6.284 -
   6.285 -	s       = NULL;
   6.286 -	path    = NULL;
   6.287 -	devname = NULL;
   6.288 -
   6.289 -	err = ioctl(channel->blktap_fd,
   6.290 -		    BLKTAP_IOCTL_BACKDEV_SETUP, channel->minor);
   6.291 -	if (err) {
   6.292 -		err = -errno;
   6.293 -		goto fail;
   6.294 -	}
   6.295 -
   6.296 -	err = asprintf(&path, "%s/backdev-node", channel->path);
   6.297 -	if (err == -1) {
   6.298 -		path = NULL;
   6.299 -		err  = -ENOMEM;
   6.300 -		goto fail;
   6.301 -	}
   6.302 -
   6.303 -	s = xs_read(channel->xsh, XBT_NULL, path, NULL);
   6.304 -	if (!s) {
   6.305 -		err = -errno;
   6.306 -		goto fail;
   6.307 -	}
   6.308 -
   6.309 -	err = sscanf(s, "%d:%d", &major, &minor);
   6.310 -	if (err != 2) {
   6.311 -		err = -EINVAL;
   6.312 -		goto fail;
   6.313 -	}
   6.314 -
   6.315 -	err = asprintf(&devname,"%s/%s%d",
   6.316 -		       BLKTAP_DEV_DIR, BACKDEV_NAME, minor);
   6.317 -	if (err == -1) {
   6.318 -		devname = NULL;
   6.319 -		err = -ENOMEM;
   6.320 -		goto fail;
   6.321 -	}
   6.322 -
   6.323 -	err = make_blktap_device(devname, major, minor, S_IFBLK | 0600);
   6.324 -	if (err)
   6.325 -		goto fail;
   6.326 -
   6.327 -	free(path);
   6.328 -	err = asprintf(&path, "%s/backdev-path", channel->path);
   6.329 -	if (err == -1) {
   6.330 -		path = NULL;
   6.331 -		err  = -ENOMEM;
   6.332 -		goto fail;
   6.333 -	}
   6.334 -
   6.335 -	err = xs_write(channel->xsh, XBT_NULL, path, devname, strlen(devname));
   6.336 -	if (err == 0) {
   6.337 -		err = -errno;
   6.338 -		goto fail;
   6.339 -	}
   6.340 -
   6.341 -	err = 0;
   6.342 - out:
   6.343 -	free(devname);
   6.344 -	free(path);
   6.345 -	free(s);
   6.346 -	return err;
   6.347 -
   6.348 - fail:
   6.349 -	EPRINTF("backdev setup failed [%d]\n", err);
   6.350 -	goto out;
   6.351 -}
   6.352 -
   6.353 -static int
   6.354 -tapdisk_channel_complete_connection(tapdisk_channel_t *channel)
   6.355 -{
   6.356 -	int err;
   6.357 -	char *path;
   6.358 -
   6.359 -	if (!xs_printf(channel->xsh, channel->path,
   6.360 -		       "sectors", "%llu", channel->image.size)) {
   6.361 -		EPRINTF("ERROR: Failed writing sectors");
   6.362 -		return -errno;
   6.363 -	}
   6.364 -
   6.365 -	if (!xs_printf(channel->xsh, channel->path,
   6.366 -		       "sector-size", "%lu", channel->image.secsize)) {
   6.367 -		EPRINTF("ERROR: Failed writing sector-size");
   6.368 -		return -errno;
   6.369 -	}
   6.370 -
   6.371 -	if (!xs_printf(channel->xsh, channel->path,
   6.372 -		       "info", "%u", channel->image.info)) {
   6.373 -		EPRINTF("ERROR: Failed writing info");
   6.374 -		return -errno;
   6.375 -	}
   6.376 -
   6.377 -	err = tapdisk_channel_connect_backdev(channel);
   6.378 -	if (err)
   6.379 -		goto clean;
   6.380 -
   6.381 -	channel->connected = 1;
   6.382 -	return 0;
   6.383 -
   6.384 - clean:
   6.385 -	if (asprintf(&path, "%s/info", channel->path) == -1)
   6.386 -		return err;
   6.387 -
   6.388 -	if (!xs_rm(channel->xsh, XBT_NULL, path))
   6.389 -		goto clean_out;
   6.390 -
   6.391 -	free(path);
   6.392 -	if (asprintf(&path, "%s/sector-size", channel->path) == -1)
   6.393 -		return err;
   6.394 -
   6.395 -	if (!xs_rm(channel->xsh, XBT_NULL, path))
   6.396 -		goto clean_out;
   6.397 -
   6.398 -	free(path);
   6.399 -	if (asprintf(&path, "%s/sectors", channel->path) == -1)
   6.400 -		return err;
   6.401 -
   6.402 -	xs_rm(channel->xsh, XBT_NULL, path);
   6.403 -
   6.404 - clean_out:
   6.405 -	free(path);
   6.406 -	return err;
   6.407 -}
   6.408 -
   6.409 -static int
   6.410 -tapdisk_channel_send_open_request(tapdisk_channel_t *channel)
   6.411 -{
   6.412 -	int len;
   6.413 -	tapdisk_message_t message;
   6.414 -
   6.415 -	memset(&message, 0, sizeof(tapdisk_message_t));
   6.416 -
   6.417 -	len = strlen(channel->vdi_path);
   6.418 -
   6.419 -	message.type              = TAPDISK_MESSAGE_OPEN;
   6.420 -	message.cookie            = channel->cookie;
   6.421 -	message.drivertype        = channel->drivertype;
   6.422 -	message.u.params.storage  = channel->storage;
   6.423 -	message.u.params.devnum   = channel->minor;
   6.424 -	message.u.params.domid    = channel->domid;
   6.425 -	message.u.params.path_len = len;
   6.426 -	strncpy(message.u.params.path, channel->vdi_path, len);
   6.427 -
   6.428 -	if (channel->mode == 'r')
   6.429 -		message.u.params.flags |= TAPDISK_MESSAGE_FLAG_RDONLY;
   6.430 -	if (channel->shared)
   6.431 -		message.u.params.flags |= TAPDISK_MESSAGE_FLAG_SHARED;
   6.432 -
   6.433 -	/* TODO: clean this up */
   6.434 -	if (xs_exists(channel->xsh, "/local/domain/0/tapdisk/add-cache"))
   6.435 -		message.u.params.flags |= TAPDISK_MESSAGE_FLAG_ADD_CACHE;
   6.436 -	if (xs_exists(channel->xsh, "/local/domain/0/tapdisk/log-dirty"))
   6.437 -		message.u.params.flags |= TAPDISK_MESSAGE_FLAG_LOG_DIRTY;
   6.438 -
   6.439 -	return tapdisk_channel_send_message(channel, &message, 2);
   6.440 -}
   6.441 -
   6.442 -static int
   6.443 -tapdisk_channel_receive_open_response(tapdisk_channel_t *channel,
   6.444 -				      tapdisk_message_t *message)
   6.445 -{
   6.446 -	int err;
   6.447 -
   6.448 -	channel->image.size    = message->u.image.sectors;
   6.449 -	channel->image.secsize = message->u.image.sector_size;
   6.450 -	channel->image.info    = message->u.image.info;
   6.451 -
   6.452 -	err = tapdisk_channel_complete_connection(channel);
   6.453 -	if (err)
   6.454 -		goto fail;
   6.455 -
   6.456 -	/* did we receive a pause request before the connection completed? */
   6.457 -	if (channel->pause_needed) {
   6.458 -		DPRINTF("%s: deferred pause request\n", channel->path);
   6.459 -		tapdisk_channel_pause_event(channel->xsh,
   6.460 -					    &channel->pause_watch,
   6.461 -					    channel->pause_str);
   6.462 -		channel->pause_needed = 0;
   6.463 -	}
   6.464 -
   6.465 -	return 0;
   6.466 -
   6.467 -fail:
   6.468 -	tapdisk_channel_fatal(channel,
   6.469 -			      "failure completing connection: %d", err);
   6.470 -	return err;
   6.471 -}
   6.472 -
   6.473 -static int
   6.474 -tapdisk_channel_send_shutdown_request(tapdisk_channel_t *channel)
   6.475 -{
   6.476 -	tapdisk_message_t message;
   6.477 -
   6.478 -	memset(&message, 0, sizeof(tapdisk_message_t));
   6.479 -
   6.480 -	message.type       = TAPDISK_MESSAGE_CLOSE;
   6.481 -	message.drivertype = channel->drivertype;
   6.482 -	message.cookie     = channel->cookie;
   6.483 -
   6.484 -	return tapdisk_channel_send_message(channel, &message, 2);
   6.485 -}
   6.486 -
   6.487 -static int
   6.488 -tapdisk_channel_receive_shutdown_response(tapdisk_channel_t *channel,
   6.489 -					  tapdisk_message_t *message)
   6.490 -{
   6.491 -	channel->open  = 0;
   6.492 -	channel->state = TAPDISK_CHANNEL_CLOSED;
   6.493 -	tapdisk_channel_close(channel);
   6.494 -	return 0;
   6.495 -}
   6.496 -
   6.497 -static int
   6.498 -tapdisk_channel_receive_runtime_error(tapdisk_channel_t *channel,
   6.499 -				      tapdisk_message_t *message)
   6.500 -{
   6.501 -	tapdisk_channel_error(channel,
   6.502 -			      "runtime error: %s", message->u.string.text);
   6.503 -	return 0;
   6.504 -}
   6.505 -
   6.506 -static int
   6.507 -tapdisk_channel_send_pid_request(tapdisk_channel_t *channel)
   6.508 -{
   6.509 -	int err;
   6.510 -	tapdisk_message_t message;
   6.511 -
   6.512 -	memset(&message, 0, sizeof(tapdisk_message_t));
   6.513 -
   6.514 -	message.type       = TAPDISK_MESSAGE_PID;
   6.515 -	message.drivertype = channel->drivertype;
   6.516 -	message.cookie     = channel->cookie;
   6.517 -
   6.518 -	err = tapdisk_channel_send_message(channel, &message, 2);
   6.519 -
   6.520 -	if (!err)
   6.521 -		channel->open = 1;
   6.522 -
   6.523 -	return err;
   6.524 -}
   6.525 -
   6.526 -static int
   6.527 -tapdisk_channel_receive_pid_response(tapdisk_channel_t *channel,
   6.528 -				     tapdisk_message_t *message)
   6.529 -{
   6.530 -	int err;
   6.531 -
   6.532 -	channel->tapdisk_pid = message->u.tapdisk_pid;
   6.533 -
   6.534 -	DPRINTF("%s: tapdisk pid: %d\n", channel->path, channel->tapdisk_pid);
   6.535 -
   6.536 -	err = setpriority(PRIO_PROCESS, channel->tapdisk_pid, PRIO_SPECIAL_IO);
   6.537 -	if (err) {
   6.538 -		tapdisk_channel_fatal(channel,
   6.539 -				      "setting tapdisk priority: %d", err);
   6.540 -		return err;
   6.541 -	}
   6.542 -
   6.543 -	err = tapdisk_channel_send_open_request(channel);
   6.544 -	if (err) {
   6.545 -		tapdisk_channel_fatal(channel,
   6.546 -				      "sending open request: %d", err);
   6.547 -		return err;
   6.548 -	}
   6.549 -
   6.550 -	return 0;
   6.551 -}
   6.552 -
   6.553 -static int
   6.554 -tapdisk_channel_send_pause_request(tapdisk_channel_t *channel)
   6.555 -{
   6.556 -	tapdisk_message_t message;
   6.557 -
   6.558 -	memset(&message, 0, sizeof(tapdisk_message_t));
   6.559 -
   6.560 -	DPRINTF("pausing %s\n", channel->path);
   6.561 -
   6.562 -	message.type       = TAPDISK_MESSAGE_PAUSE;
   6.563 -	message.drivertype = channel->drivertype;
   6.564 -	message.cookie     = channel->cookie;
   6.565 -
   6.566 -	return tapdisk_channel_send_message(channel, &message, 2);
   6.567 -}
   6.568 -
   6.569 -static int
   6.570 -tapdisk_channel_receive_pause_response(tapdisk_channel_t *channel,
   6.571 -				       tapdisk_message_t *message)
   6.572 -{
   6.573 -	int err;
   6.574 -
   6.575 -	if (!xs_write(channel->xsh, XBT_NULL,
   6.576 -		      channel->pause_done_str, "", strlen(""))) {
   6.577 -		err = -errno;
   6.578 -		goto fail;
   6.579 -	}
   6.580 -
   6.581 -	return 0;
   6.582 -
   6.583 -fail:
   6.584 -	tapdisk_channel_fatal(channel,
   6.585 -			      "failure receiving pause response: %d\n", err);
   6.586 -	return err;
   6.587 -}
   6.588 -
   6.589 -static int
   6.590 -tapdisk_channel_send_resume_request(tapdisk_channel_t *channel)
   6.591 -{
   6.592 -	int len;
   6.593 -	tapdisk_message_t message;
   6.594 -
   6.595 -	memset(&message, 0, sizeof(tapdisk_message_t));
   6.596 -
   6.597 -	len = strlen(channel->vdi_path);
   6.598 -
   6.599 -	DPRINTF("resuming %s\n", channel->path);
   6.600 -
   6.601 -	message.type              = TAPDISK_MESSAGE_RESUME;
   6.602 -	message.drivertype        = channel->drivertype;
   6.603 -	message.cookie            = channel->cookie;
   6.604 -	message.u.params.path_len = len;
   6.605 -	strncpy(message.u.params.path, channel->vdi_path, len);
   6.606 -
   6.607 -	return tapdisk_channel_send_message(channel, &message, 2);
   6.608 -}
   6.609 -
   6.610 -static int
   6.611 -tapdisk_channel_receive_resume_response(tapdisk_channel_t *channel,
   6.612 -					tapdisk_message_t *message)
   6.613 -{
   6.614 -	int err;
   6.615 -
   6.616 -	if (!xs_rm(channel->xsh, XBT_NULL, channel->pause_done_str)) {
   6.617 -		err = -errno;
   6.618 -		goto fail;
   6.619 -	}
   6.620 -
   6.621 -	return 0;
   6.622 -
   6.623 -fail:
   6.624 -	tapdisk_channel_fatal(channel,
   6.625 -			      "failure receiving pause response: %d", err);
   6.626 -	return err;
   6.627 -}
   6.628 -
   6.629 -static void
   6.630 -tapdisk_channel_shutdown_event(struct xs_handle *xsh,
   6.631 -			       struct xenbus_watch *watch, const char *path)
   6.632 -{
   6.633 -	int err;
   6.634 -	tapdisk_channel_t *channel;
   6.635 -
   6.636 -	channel = watch->data;
   6.637 -
   6.638 -	DPRINTF("%s: got watch on %s\n", channel->path, path);
   6.639 -
   6.640 -	if (!xs_exists(channel->xsh, channel->path)) {
   6.641 -		tapdisk_channel_close(channel);
   6.642 -		return;
   6.643 -	}
   6.644 -
   6.645 -	err = tapdisk_channel_validate_watch(channel, path);
   6.646 -	if (err) {
   6.647 -		if (err == -EINVAL)
   6.648 -			tapdisk_channel_fatal(channel, "bad shutdown watch");
   6.649 -		return;
   6.650 -	}
   6.651 -
   6.652 -	tapdisk_channel_send_shutdown_request(channel);
   6.653 -}
   6.654 -
   6.655 -static void
   6.656 -tapdisk_channel_pause_event(struct xs_handle *xsh,
   6.657 -			    struct xenbus_watch *watch, const char *path)
   6.658 -{
   6.659 -	int err, paused;
   6.660 -	tapdisk_channel_t *channel;
   6.661 -
   6.662 -	channel = watch->data;
   6.663 -
   6.664 -	DPRINTF("%s: got watch on %s\n", channel->path, path);
   6.665 -
   6.666 -	if (!xs_exists(channel->xsh, channel->path)) {
   6.667 -		tapdisk_channel_close(channel);
   6.668 -		return;
   6.669 -	}
   6.670 -
   6.671 -	/* NB: The VBD is essentially considered ready since the
   6.672 -	 * backend hotplug event ocurred, which is just after
   6.673 -	 * start-tapdisk, not after watch registration. We start
   6.674 -	 * testing xenstore keys with the very first shot, but defer
   6.675 -	 * until after connection completion. */
   6.676 -
   6.677 -	err = tapdisk_channel_validate_watch(channel, path);
   6.678 -	if (err) {
   6.679 -		if (err == -EINVAL)
   6.680 -			tapdisk_channel_fatal(channel, "bad pause watch");
   6.681 -
   6.682 -		if (err != -ENOENT)
   6.683 -			return;
   6.684 -
   6.685 -		err = 0;
   6.686 -	}
   6.687 -
   6.688 -	paused  = xs_exists(xsh, channel->pause_done_str);
   6.689 -
   6.690 -	if (xs_exists(xsh, channel->pause_str)) {
   6.691 -		/*
   6.692 -		 * Duplicate requests are a protocol validation, but
   6.693 -		 * impossible to identify if watch registration and an
   6.694 -		 * actual pause request may fire separately in close
   6.695 -		 * succession. Warn, but do not signal an error.
   6.696 -		 */
   6.697 -		int pausing = channel->state == TAPDISK_CHANNEL_WAIT_PAUSE;
   6.698 -		if (pausing || paused) {
   6.699 -			DPRINTF("Ignoring pause event for %s vbd %s\n",
   6.700 -				pausing ? "pausing" : "paused", channel->path);
   6.701 -			goto out;
   6.702 -		}
   6.703 -
   6.704 -		/* defer if tapdisk is not ready yet */
   6.705 -		if (!channel->connected) {
   6.706 -			DPRINTF("%s: deferring pause request\n", path);
   6.707 -			channel->pause_needed = 1;
   6.708 -			goto out;
   6.709 -		}
   6.710 -
   6.711 -		err = tapdisk_channel_send_pause_request(channel);
   6.712 -
   6.713 -	} else if (xs_exists(xsh, channel->pause_done_str)) {
   6.714 -		free(channel->params);
   6.715 -		channel->params   = NULL;
   6.716 -		channel->vdi_path = NULL;
   6.717 -
   6.718 -		err = xs_gather(channel->xsh, channel->path,
   6.719 -				"params", NULL, &channel->params, NULL);
   6.720 -		if (err) {
   6.721 -			EPRINTF("failure re-reading params: %d\n", err);
   6.722 -			channel->params = NULL;
   6.723 -			goto out;
   6.724 -		}
   6.725 -
   6.726 -		err = tapdisk_channel_parse_params(channel);
   6.727 -		if (err)
   6.728 -			goto out;
   6.729 -
   6.730 -		err = tapdisk_channel_send_resume_request(channel);
   6.731 -		if (err)
   6.732 -			goto out;
   6.733 -	}
   6.734 -
   6.735 -	err = 0;
   6.736 -
   6.737 -out:
   6.738 -	if (err)
   6.739 -		tapdisk_channel_error(channel, "pause event failed: %d", err);
   6.740 -}
   6.741 -
   6.742 -static int
   6.743 -tapdisk_channel_open_control_socket(char *devname)
   6.744 -{
   6.745 -	int err, fd;
   6.746 -	fd_set socks;
   6.747 -	struct timeval timeout;
   6.748 -
   6.749 -	err = mkdir(BLKTAP_CTRL_DIR, 0755);
   6.750 -	if (err == -1 && errno != EEXIST) {
   6.751 -		EPRINTF("Failure creating %s directory: %d\n",
   6.752 -			BLKTAP_CTRL_DIR, errno);
   6.753 -		return -errno;
   6.754 -	}
   6.755 -
   6.756 -	err = mkfifo(devname, S_IRWXU | S_IRWXG | S_IRWXO);
   6.757 -	if (err) {
   6.758 -		if (errno == EEXIST) {
   6.759 -			/*
   6.760 -			 * Remove fifo since it may have data from
   6.761 -			 * it's previous use --- earlier invocation
   6.762 -			 * of tapdisk may not have read all messages.
   6.763 -			 */
   6.764 -			err = unlink(devname);
   6.765 -			if (err) {
   6.766 -				EPRINTF("ERROR: unlink(%s) failed (%d)\n",
   6.767 -					devname, errno);
   6.768 -				return -errno;
   6.769 -			}
   6.770 -
   6.771 -			err = mkfifo(devname, S_IRWXU | S_IRWXG | S_IRWXO);
   6.772 -		}
   6.773 -
   6.774 -		if (err) {
   6.775 -			EPRINTF("ERROR: pipe failed (%d)\n", errno);
   6.776 -			return -errno;
   6.777 -		}
   6.778 -	}
   6.779 -
   6.780 -	fd = open(devname, O_RDWR | O_NONBLOCK);
   6.781 -	if (fd == -1) {
   6.782 -		EPRINTF("Failed to open %s\n", devname);
   6.783 -		return -errno;
   6.784 -	}
   6.785 -
   6.786 -	return fd;
   6.787 -}
   6.788 -
   6.789 -static int
   6.790 -tapdisk_channel_get_device_number(tapdisk_channel_t *channel)
   6.791 -{
   6.792 -	char *devname;
   6.793 -	domid_translate_t tr;
   6.794 -	int major, minor, err;
   6.795 -
   6.796 -	tr.domid = channel->domid;
   6.797 -        tr.busid = channel->busid;
   6.798 -
   6.799 -	minor = ioctl(channel->blktap_fd, BLKTAP_IOCTL_NEWINTF, tr);
   6.800 -	if (minor <= 0 || minor > MAX_TAP_DEV) {
   6.801 -		EPRINTF("invalid dev id: %d\n", minor);
   6.802 -		return -EINVAL;
   6.803 -	}
   6.804 -
   6.805 -	major = ioctl(channel->blktap_fd, BLKTAP_IOCTL_MAJOR, minor);
   6.806 -	if (major < 0) {
   6.807 -		EPRINTF("invalid major id: %d\n", major);
   6.808 -		return -EINVAL;
   6.809 -	}
   6.810 -
   6.811 -	err = asprintf(&devname, "%s/%s%d",
   6.812 -		       BLKTAP_DEV_DIR, BLKTAP_DEV_NAME, minor);
   6.813 -	if (err == -1) {
   6.814 -		EPRINTF("get_new_dev: malloc failed\n");
   6.815 -		return -ENOMEM;
   6.816 -	}
   6.817 -
   6.818 -	err = make_blktap_device(devname, major, minor, S_IFCHR | 0600);
   6.819 -	free(devname);
   6.820 -
   6.821 -	if (err)
   6.822 -		return err;
   6.823 -
   6.824 -	DPRINTF("Received device id %d and major %d, "
   6.825 -		"sent domid %d and be_id %d\n",
   6.826 -		minor, major, tr.domid, tr.busid);
   6.827 -
   6.828 -	channel->major = major;
   6.829 -	channel->minor = minor;
   6.830 -
   6.831 -	return 0;
   6.832 -}
   6.833 -
   6.834 -static int
   6.835 -tapdisk_channel_start_process(tapdisk_channel_t *channel,
   6.836 -			      char *write_dev, char *read_dev)
   6.837 -{
   6.838 -	pid_t child;
   6.839 -	char *argv[] = { "tapdisk", write_dev, read_dev, NULL };
   6.840 -
   6.841 -	if ((child = fork()) == -1)
   6.842 -		return -errno;
   6.843 -
   6.844 -	if (!child) {
   6.845 -		int i;
   6.846 -		for (i = 0 ; i < sysconf(_SC_OPEN_MAX) ; i++)
   6.847 -			if (i != STDIN_FILENO &&
   6.848 -			    i != STDOUT_FILENO &&
   6.849 -			    i != STDERR_FILENO)
   6.850 -				close(i);
   6.851 -
   6.852 -		execvp("tapdisk", argv);
   6.853 -		_exit(1);
   6.854 -	} else {
   6.855 -		pid_t got;
   6.856 -		do {
   6.857 -			got = waitpid(child, NULL, 0);
   6.858 -		} while (got != child);
   6.859 -	}
   6.860 -	return 0;
   6.861 -}
   6.862 -
   6.863 -static int
   6.864 -tapdisk_channel_launch_tapdisk(tapdisk_channel_t *channel)
   6.865 -{
   6.866 -	int err;
   6.867 -	char *read_dev, *write_dev;
   6.868 -
   6.869 -	read_dev          = NULL;
   6.870 -	write_dev         = NULL;
   6.871 -	channel->read_fd  = -1;
   6.872 -	channel->write_fd = -1;
   6.873 -
   6.874 -	err = tapdisk_channel_get_device_number(channel);
   6.875 -	if (err)
   6.876 -		return err;
   6.877 -
   6.878 -	err = asprintf(&write_dev,
   6.879 -		       "%s/tapctrlwrite%d", BLKTAP_CTRL_DIR, channel->minor);
   6.880 -	if (err == -1) {
   6.881 -		err = -ENOMEM;
   6.882 -		write_dev = NULL;
   6.883 -		goto fail;
   6.884 -	}
   6.885 -
   6.886 -	err = asprintf(&read_dev,
   6.887 -		       "%s/tapctrlread%d", BLKTAP_CTRL_DIR, channel->minor);
   6.888 -	if (err == -1) {
   6.889 -		err = -ENOMEM;
   6.890 -		read_dev = NULL;
   6.891 -		goto fail;
   6.892 -	}
   6.893 -
   6.894 -	channel->write_fd = tapdisk_channel_open_control_socket(write_dev);
   6.895 -	if (channel->write_fd < 0) {
   6.896 -		err = channel->write_fd;
   6.897 -		channel->write_fd = -1;
   6.898 -		goto fail;
   6.899 -	}
   6.900 -
   6.901 -	channel->read_fd = tapdisk_channel_open_control_socket(read_dev);
   6.902 -	if (channel->read_fd < 0) {
   6.903 -		err = channel->read_fd;
   6.904 -		channel->read_fd = -1;
   6.905 -		goto fail;
   6.906 -	}
   6.907 -
   6.908 -	err = tapdisk_channel_start_process(channel, write_dev, read_dev);
   6.909 -	if (err)
   6.910 -		goto fail;
   6.911 -
   6.912 -	channel->open       = 1;
   6.913 -	channel->channel_id = channel->write_fd;
   6.914 -
   6.915 -	free(read_dev);
   6.916 -	free(write_dev);
   6.917 -
   6.918 -	DPRINTF("process launched, channel = %d:%d\n",
   6.919 -		channel->channel_id, channel->cookie);
   6.920 -
   6.921 -	return tapdisk_channel_send_pid_request(channel);
   6.922 -
   6.923 -fail:
   6.924 -	free(read_dev);
   6.925 -	free(write_dev);
   6.926 -	if (channel->read_fd != -1)
   6.927 -		close(channel->read_fd);
   6.928 -	if (channel->write_fd != -1)
   6.929 -		close(channel->write_fd);
   6.930 -	return err;
   6.931 -}
   6.932 -
   6.933 -static int
   6.934 -tapdisk_channel_connect(tapdisk_channel_t *channel)
   6.935 -{
   6.936 -	int err;
   6.937 -
   6.938 -	tapdisk_daemon_find_channel(channel);
   6.939 -
   6.940 -	if (!channel->tapdisk_pid)
   6.941 -		return tapdisk_channel_launch_tapdisk(channel);
   6.942 -
   6.943 -	DPRINTF("%s: process exists: %d, channel = %d:%d\n",
   6.944 -		channel->path, channel->tapdisk_pid,
   6.945 -		channel->channel_id, channel->cookie);
   6.946 -
   6.947 -	err = tapdisk_channel_get_device_number(channel);
   6.948 -	if (err)
   6.949 -		return err;
   6.950 -
   6.951 -	return tapdisk_channel_send_pid_request(channel);
   6.952 -}
   6.953 -
   6.954 -static int
   6.955 -tapdisk_channel_init(tapdisk_channel_t *channel)
   6.956 -{
   6.957 -	int err;
   6.958 -
   6.959 -	channel->uuid_str          = NULL;
   6.960 -	channel->pause_str         = NULL;
   6.961 -	channel->pause_done_str    = NULL;
   6.962 -	channel->shutdown_str      = NULL;
   6.963 -	channel->share_tapdisk_str = NULL;
   6.964 -
   6.965 -	err = asprintf(&channel->uuid_str,
   6.966 -		       "%s/tapdisk-uuid", channel->path);
   6.967 -	if (err == -1) {
   6.968 -		channel->uuid_str = NULL;
   6.969 -		goto fail;
   6.970 -	}
   6.971 -
   6.972 -	err = asprintf(&channel->pause_str, "%s/pause", channel->path);
   6.973 -	if (err == -1) {
   6.974 -		channel->pause_str = NULL;
   6.975 -		goto fail;
   6.976 -	}
   6.977 -
   6.978 -	err = asprintf(&channel->pause_done_str,
   6.979 -		       "%s/pause-done", channel->path);
   6.980 -	if (err == -1) {
   6.981 -		channel->pause_done_str = NULL;
   6.982 -		goto fail;
   6.983 -	}
   6.984 -
   6.985 -	err = asprintf(&channel->shutdown_str,
   6.986 -		       "%s/shutdown-tapdisk", channel->path);
   6.987 -	if (err == -1) {
   6.988 -		channel->shutdown_str = NULL;
   6.989 -		goto fail;
   6.990 -	}
   6.991 -
   6.992 -	channel->share_tapdisk_str = "/local/domain/0/tapdisk/share-tapdisks";
   6.993 -
   6.994 -	return 0;
   6.995 -
   6.996 -fail:
   6.997 -	free(channel->uuid_str);
   6.998 -	free(channel->pause_str);
   6.999 -	free(channel->pause_done_str);
  6.1000 -	free(channel->shutdown_str);
  6.1001 -	channel->uuid_str          = NULL;
  6.1002 -	channel->pause_str         = NULL;
  6.1003 -	channel->pause_done_str    = NULL;
  6.1004 -	channel->shutdown_str      = NULL;
  6.1005 -	channel->share_tapdisk_str = NULL;
  6.1006 -	return -ENOMEM;
  6.1007 -}
  6.1008 -
  6.1009 -static int
  6.1010 -tapdisk_channel_set_watches(tapdisk_channel_t *channel)
  6.1011 -{
  6.1012 -	int err;
  6.1013 -
  6.1014 -	/* watch for pause events */
  6.1015 -	channel->pause_watch.node            = channel->pause_str;
  6.1016 -	channel->pause_watch.callback        = tapdisk_channel_pause_event;
  6.1017 -	channel->pause_watch.data            = channel;
  6.1018 -	err = register_xenbus_watch(channel->xsh, &channel->pause_watch);
  6.1019 -	if (err) {
  6.1020 -		channel->pause_watch.node    = NULL;
  6.1021 -		goto fail;
  6.1022 -	}
  6.1023 -
  6.1024 -	/* watch for shutdown events */
  6.1025 -	channel->shutdown_watch.node         = channel->shutdown_str;
  6.1026 -	channel->shutdown_watch.callback     = tapdisk_channel_shutdown_event;
  6.1027 -	channel->shutdown_watch.data         = channel;
  6.1028 -	err = register_xenbus_watch(channel->xsh, &channel->shutdown_watch);
  6.1029 -	if (err) {
  6.1030 -		channel->shutdown_watch.node = NULL;
  6.1031 -		goto fail;
  6.1032 -	}
  6.1033 -
  6.1034 -	return 0;
  6.1035 -
  6.1036 -fail:
  6.1037 -	if (channel->pause_watch.node) {
  6.1038 -		unregister_xenbus_watch(channel->xsh, &channel->pause_watch);
  6.1039 -		channel->pause_watch.node    = NULL;
  6.1040 -	}
  6.1041 -	if (channel->shutdown_watch.node) {
  6.1042 -		unregister_xenbus_watch(channel->xsh, &channel->shutdown_watch);
  6.1043 -		channel->shutdown_watch.node = NULL;
  6.1044 -	}
  6.1045 -	return err;
  6.1046 -}
  6.1047 -
  6.1048 -static void
  6.1049 -tapdisk_channel_get_storage_type(tapdisk_channel_t *channel)
  6.1050 -{
  6.1051 -	int err, type;
  6.1052 -	unsigned int len;
  6.1053 -	char *path, *stype;
  6.1054 -
  6.1055 -	channel->storage = TAPDISK_STORAGE_TYPE_DEFAULT;
  6.1056 -
  6.1057 -	err = asprintf(&path, "%s/sm-data/storage-type", channel->path);
  6.1058 -	if (err == -1)
  6.1059 -		return;
  6.1060 -
  6.1061 -	stype = xs_read(channel->xsh, XBT_NULL, path, &len);
  6.1062 -	if (!stype)
  6.1063 -		goto out;
  6.1064 -	else if (!strcmp(stype, "nfs"))
  6.1065 -		channel->storage = TAPDISK_STORAGE_TYPE_NFS;
  6.1066 -	else if (!strcmp(stype, "ext"))
  6.1067 -		channel->storage = TAPDISK_STORAGE_TYPE_EXT;
  6.1068 -	else if (!strcmp(stype, "lvm"))
  6.1069 -		channel->storage = TAPDISK_STORAGE_TYPE_LVM;
  6.1070 -
  6.1071 -out:
  6.1072 -	free(path);
  6.1073 -	free(stype);
  6.1074 -}
  6.1075 -
  6.1076 -static int
  6.1077 -tapdisk_channel_get_busid(tapdisk_channel_t *channel)
  6.1078 -{
  6.1079 -	int len, end;
  6.1080 -	const char *ptr;
  6.1081 -	char *tptr, num[10];
  6.1082 -
  6.1083 -	len = strsep_len(channel->path, '/', 6);
  6.1084 -	end = strlen(channel->path);
  6.1085 -	if(len < 0 || end < 0) {
  6.1086 -		EPRINTF("invalid path: %s\n", channel->path);
  6.1087 -		return -EINVAL;
  6.1088 -	}
  6.1089 -	
  6.1090 -	ptr = channel->path + len + 1;
  6.1091 -	strncpy(num, ptr, end - len);
  6.1092 -	tptr = num + (end - (len + 1));
  6.1093 -	*tptr = '\0';
  6.1094 -
  6.1095 -	channel->busid = atoi(num);
  6.1096 -	return 0;
  6.1097 -}
  6.1098 -
  6.1099 -static int
  6.1100 -tapdisk_channel_parse_params(tapdisk_channel_t *channel)
  6.1101 -{
  6.1102 -	int i, size, err;
  6.1103 -	unsigned int len;
  6.1104 -	char *ptr, *path, handle[10];
  6.1105 -	char *vdi_type;
  6.1106 -	char *vtype;
  6.1107 -
  6.1108 -	path = channel->params;
  6.1109 -	size = sizeof(dtypes) / sizeof(disk_info_t *);
  6.1110 -
  6.1111 -	if (strlen(path) + 1 >= TAPDISK_MESSAGE_MAX_PATH_LENGTH)
  6.1112 -		goto fail;
  6.1113 -
  6.1114 -	ptr = strchr(path, ':');
  6.1115 -	if (!ptr)
  6.1116 -		goto fail;
  6.1117 -
  6.1118 -	channel->vdi_path = ptr + 1;
  6.1119 -	memcpy(handle, path, (ptr - path));
  6.1120 -	ptr  = handle + (ptr - path);
  6.1121 -	*ptr = '\0';
  6.1122 -
  6.1123 -	err = asprintf(&vdi_type, "%s/sm-data/vdi-type", channel->path);
  6.1124 -	if (err == -1)
  6.1125 -		goto fail;
  6.1126 -
  6.1127 -	if (xs_exists(channel->xsh, vdi_type)) {
  6.1128 -		vtype = xs_read(channel->xsh, XBT_NULL, vdi_type, &len);
  6.1129 -		free(vdi_type);
  6.1130 -		if (!vtype)
  6.1131 -			goto fail;
  6.1132 -		if (len >= sizeof(handle) - 1) {
  6.1133 -			free(vtype);
  6.1134 -			goto fail;
  6.1135 -		}
  6.1136 -		sprintf(handle, "%s", vtype);
  6.1137 -		free(vtype);
  6.1138 -	}
  6.1139 -
  6.1140 -	for (i = 0; i < size; i++) {
  6.1141 -		if (strncmp(handle, dtypes[i]->handle, (ptr - path)))
  6.1142 -			continue;
  6.1143 -
  6.1144 -		if (dtypes[i]->idnum == -1)
  6.1145 -			goto fail;
  6.1146 -
  6.1147 -		channel->drivertype = dtypes[i]->idnum;
  6.1148 -		return 0;
  6.1149 -	}
  6.1150 -
  6.1151 -fail:
  6.1152 -	EPRINTF("%s: invalid blktap params: %s\n",
  6.1153 -		channel->path, channel->params);
  6.1154 -	channel->vdi_path = NULL;
  6.1155 -	return -EINVAL;
  6.1156 -}
  6.1157 -
  6.1158 -static int
  6.1159 -tapdisk_channel_gather_info(tapdisk_channel_t *channel)
  6.1160 -{
  6.1161 -	int err;
  6.1162 -
  6.1163 -	err = xs_gather(channel->xsh, channel->path,
  6.1164 -			"frontend", NULL, &channel->frontpath,
  6.1165 -			"frontend-id", "%li", &channel->domid,
  6.1166 -			"params", NULL, &channel->params,
  6.1167 -			"mode", "%c", &channel->mode, NULL);
  6.1168 -	if (err) {
  6.1169 -		EPRINTF("could not find device info: %d\n", err);
  6.1170 -		return err;
  6.1171 -	}
  6.1172 -
  6.1173 -	err = tapdisk_channel_parse_params(channel);
  6.1174 -	if (err)
  6.1175 -		return err;
  6.1176 -
  6.1177 -	err = tapdisk_channel_get_busid(channel);
  6.1178 -	if (err)
  6.1179 -		return err;
  6.1180 -
  6.1181 -	tapdisk_channel_get_storage_type(channel);
  6.1182 -
  6.1183 -	return 0;
  6.1184 -}
  6.1185 -
  6.1186 -static int
  6.1187 -tapdisk_channel_verify_start_request(tapdisk_channel_t *channel)
  6.1188 -{
  6.1189 -	char *path;
  6.1190 -	unsigned int err;
  6.1191 -
  6.1192 -	err = asprintf(&path, "%s/start-tapdisk", channel->path);
  6.1193 -	if (err == -1)
  6.1194 -		goto mem_fail;
  6.1195 -
  6.1196 -	if (!xs_exists(channel->xsh, path))
  6.1197 -		goto fail;
  6.1198 -
  6.1199 -	free(path);
  6.1200 -	err = asprintf(&path, "%s/shutdown-request", channel->path);
  6.1201 -	if (err == -1)
  6.1202 -		goto mem_fail;
  6.1203 -
  6.1204 -	if (xs_exists(channel->xsh, path))
  6.1205 -		goto fail;
  6.1206 -
  6.1207 -	if (xs_exists(channel->xsh, channel->shutdown_str))
  6.1208 -		goto fail;
  6.1209 -
  6.1210 -	free(path);
  6.1211 -	err = asprintf(&path, "%s/shutdown-done", channel->path);
  6.1212 -	if (err == -1)
  6.1213 -		goto mem_fail;
  6.1214 -
  6.1215 -	if (xs_exists(channel->xsh, path))
  6.1216 -		goto fail;
  6.1217 -
  6.1218 -	free(path);
  6.1219 -
  6.1220 -	return 0;
  6.1221 -
  6.1222 -fail:
  6.1223 -	free(path);
  6.1224 -	EPRINTF("%s:%s: invalid start request\n", __func__, channel->path);
  6.1225 -	return -EINVAL;
  6.1226 -
  6.1227 -mem_fail:
  6.1228 -	EPRINTF("%s:%s: out of memory\n", __func__, channel->path);
  6.1229 -	return -ENOMEM;
  6.1230 -}
  6.1231 -
  6.1232 -void
  6.1233 -tapdisk_channel_close(tapdisk_channel_t *channel)
  6.1234 -{
  6.1235 -	if (channel->channel_id)
  6.1236 -		DPRINTF("%s: closing channel %d:%d\n",
  6.1237 -			channel->path, channel->channel_id, channel->cookie);
  6.1238 -
  6.1239 -	if (channel->open)
  6.1240 -		tapdisk_channel_send_shutdown_request(channel);
  6.1241 -
  6.1242 -	if (channel->pause_watch.node) {
  6.1243 -		unregister_xenbus_watch(channel->xsh, &channel->pause_watch);
  6.1244 -		channel->pause_watch.node = NULL;
  6.1245 -	}
  6.1246 -
  6.1247 -	if (channel->shutdown_watch.node) {
  6.1248 -		unregister_xenbus_watch(channel->xsh, &channel->shutdown_watch);
  6.1249 -		channel->shutdown_watch.node = NULL;
  6.1250 -	}
  6.1251 -
  6.1252 -	tapdisk_daemon_close_channel(channel);
  6.1253 -
  6.1254 -	free(channel->params);
  6.1255 -	free(channel->frontpath);
  6.1256 -	free(channel->shutdown_str);
  6.1257 -	free(channel->pause_done_str);
  6.1258 -	free(channel->pause_str);
  6.1259 -	free(channel->uuid_str);
  6.1260 -	free(channel->path);
  6.1261 -	free(channel);
  6.1262 -}
  6.1263 -
  6.1264 -int
  6.1265 -tapdisk_channel_open(tapdisk_channel_t **_channel,
  6.1266 -		     char *path, struct xs_handle *xsh,
  6.1267 -		     int blktap_fd, uint16_t cookie)
  6.1268 -{
  6.1269 -	int err;
  6.1270 -	char *msg;
  6.1271 -	tapdisk_channel_t *channel;
  6.1272 -
  6.1273 -	msg       = NULL;
  6.1274 -	*_channel = NULL;
  6.1275 -
  6.1276 -	channel = calloc(1, sizeof(tapdisk_channel_t));
  6.1277 -	if (!channel)
  6.1278 -		return -ENOMEM;
  6.1279 -
  6.1280 -	channel->xsh       = xsh;
  6.1281 -	channel->blktap_fd = blktap_fd;
  6.1282 -	channel->cookie    = cookie;
  6.1283 -	channel->state     = TAPDISK_CHANNEL_IDLE;
  6.1284 -
  6.1285 -	INIT_LIST_HEAD(&channel->list);
  6.1286 -
  6.1287 -	channel->path = strdup(path);
  6.1288 -	if (!channel->path) {
  6.1289 -		err = -ENOMEM;
  6.1290 -		goto fail;
  6.1291 -	}
  6.1292 -
  6.1293 -	err = tapdisk_channel_init(channel);
  6.1294 -	if (err) {
  6.1295 -		msg = "allocating device";
  6.1296 -		goto fail;
  6.1297 -	}
  6.1298 -
  6.1299 -	err = tapdisk_channel_check_uuid(channel);
  6.1300 -	if (err) {
  6.1301 -		msg = "checking uuid";
  6.1302 -		goto fail;
  6.1303 -	}
  6.1304 -
  6.1305 -	err = tapdisk_channel_gather_info(channel);
  6.1306 -	if (err) {
  6.1307 -		msg = "gathering parameters";
  6.1308 -		goto fail;
  6.1309 -	}
  6.1310 -
  6.1311 -	err = tapdisk_channel_verify_start_request(channel);
  6.1312 -	if (err) {
  6.1313 -		msg = "invalid start request";
  6.1314 -		goto fail;
  6.1315 -	}
  6.1316 -
  6.1317 -	err = tapdisk_channel_set_watches(channel);
  6.1318 -	if (err) {
  6.1319 -		msg = "registering xenstore watches";
  6.1320 -		goto fail;
  6.1321 -	}
  6.1322 -
  6.1323 -	err = tapdisk_channel_connect(channel);
  6.1324 -	if (err) {
  6.1325 -		msg = "connecting to tapdisk";
  6.1326 -		goto fail;
  6.1327 -	}
  6.1328 -
  6.1329 -	*_channel = channel;
  6.1330 -	return 0;
  6.1331 -
  6.1332 -fail:
  6.1333 -	tapdisk_channel_fatal(channel, "%s: %d", (msg ? : "failure"), err);
  6.1334 -	return err;
  6.1335 -}
  6.1336 -
  6.1337 -int
  6.1338 -tapdisk_channel_receive_message(tapdisk_channel_t *c, tapdisk_message_t *m)
  6.1339 -{
  6.1340 -	int err;
  6.1341 -
  6.1342 -	err = tapdisk_channel_validate_message(c, m);
  6.1343 -	if (err)
  6.1344 -		goto fail;
  6.1345 -
  6.1346 -	switch (m->type) {
  6.1347 -	case TAPDISK_MESSAGE_PID_RSP:
  6.1348 -		return tapdisk_channel_receive_pid_response(c, m);
  6.1349 -
  6.1350 -	case TAPDISK_MESSAGE_OPEN_RSP:
  6.1351 -		return tapdisk_channel_receive_open_response(c, m);
  6.1352 -
  6.1353 -	case TAPDISK_MESSAGE_PAUSE_RSP:
  6.1354 -		return tapdisk_channel_receive_pause_response(c, m);
  6.1355 -
  6.1356 -	case TAPDISK_MESSAGE_RESUME_RSP:
  6.1357 -		return tapdisk_channel_receive_resume_response(c, m);
  6.1358 -
  6.1359 -	case TAPDISK_MESSAGE_CLOSE_RSP:
  6.1360 -		return tapdisk_channel_receive_shutdown_response(c, m);
  6.1361 -
  6.1362 -	case TAPDISK_MESSAGE_RUNTIME_ERROR:
  6.1363 -		return tapdisk_channel_receive_runtime_error(c, m);
  6.1364 -	}
  6.1365 -
  6.1366 -fail:
  6.1367 -	tapdisk_channel_fatal(c, "received unexpected message %s in state %d",
  6.1368 -			      tapdisk_message_name(m->type), c->state);
  6.1369 -	return -EINVAL;
  6.1370 -}
     7.1 --- a/tools/blktap2/daemon/tapdisk-daemon.c	Thu Jun 18 10:27:21 2009 +0100
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,599 +0,0 @@
     7.4 -/* Copyright (c) 2008, XenSource Inc.
     7.5 - * All rights reserved.
     7.6 - *
     7.7 - * Redistribution and use in source and binary forms, with or without
     7.8 - * modification, are permitted provided that the following conditions are met:
     7.9 - *     * Redistributions of source code must retain the above copyright
    7.10 - *       notice, this list of conditions and the following disclaimer.
    7.11 - *     * Redistributions in binary form must reproduce the above copyright
    7.12 - *       notice, this list of conditions and the following disclaimer in the
    7.13 - *       documentation and/or other materials provided with the distribution.
    7.14 - *     * Neither the name of XenSource Inc. nor the names of its contributors
    7.15 - *       may be used to endorse or promote products derived from this software
    7.16 - *       without specific prior written permission.
    7.17 - *
    7.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    7.19 - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    7.20 - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    7.21 - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    7.22 - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    7.23 - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    7.24 - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    7.25 - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    7.26 - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    7.27 - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    7.28 - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    7.29 -*/
    7.30 -#include <stdio.h>
    7.31 -#include <errno.h>
    7.32 -#include <fcntl.h>
    7.33 -#include <unistd.h>
    7.34 -#include <stdlib.h>
    7.35 -#include <string.h>
    7.36 -#include <sys/ioctl.h>
    7.37 -#include <sys/resource.h>
    7.38 -
    7.39 -#include <xs.h>
    7.40 -#include "disktypes.h"
    7.41 -#include "tapdisk-dispatch.h"
    7.42 -
    7.43 -#define TAPDISK_DAEMON_DOMID_WATCH   "domid-watch"
    7.44 -#define TAPDISK_DAEMON_PIDFILE       "/var/run/blktapctrl.pid"
    7.45 -
    7.46 -typedef struct tapdisk_daemon {
    7.47 -	char                         *node;
    7.48 -	int                           blktap_fd;
    7.49 -	uint16_t                      cookie;
    7.50 -
    7.51 -	struct xs_handle             *xsh;
    7.52 -	struct list_head              channels;
    7.53 -	struct xenbus_watch           watch;
    7.54 -} tapdisk_daemon_t;
    7.55 -
    7.56 -static tapdisk_daemon_t tapdisk_daemon;
    7.57 -
    7.58 -#define tapdisk_daemon_for_each_channel(c, tmp) \
    7.59 -	list_for_each_entry_safe(c, tmp, &tapdisk_daemon.channels, list)
    7.60 -
    7.61 -#define MAX(a, b) ((a) >= (b) ? (a) : (b))
    7.62 -
    7.63 -static void
    7.64 -tapdisk_daemon_print_drivers(void)
    7.65 -{
    7.66 -	int i, size;
    7.67 -
    7.68 -	DPRINTF("blktap-daemon: v1.0.2\n");
    7.69 -
    7.70 -	size = sizeof(dtypes) / sizeof(disk_info_t *);
    7.71 -	for (i = 0; i < size; i++)
    7.72 -		DPRINTF("Found driver: [%s]\n", dtypes[i]->name);
    7.73 -}
    7.74 -
    7.75 -static int
    7.76 -tapdisk_daemon_write_pidfile(long pid)
    7.77 -{
    7.78 -	char buf[100];
    7.79 -	int len, fd, flags, err;
    7.80 -
    7.81 -	fd = open(TAPDISK_DAEMON_PIDFILE, O_RDWR | O_CREAT, 0600);
    7.82 -	if (fd == -1) {
    7.83 -		EPRINTF("Opening pid file failed (%d)\n", errno);
    7.84 -		return -errno;
    7.85 -	}
    7.86 -
    7.87 -	/* We exit silently if daemon already running */
    7.88 -	err = lockf(fd, F_TLOCK, 0);
    7.89 -	if (err == -1)
    7.90 -		exit(0);
    7.91 -
    7.92 -	/* Set FD_CLOEXEC, so that tapdisk doesn't get this file descriptor */
    7.93 -	flags = fcntl(fd, F_GETFD);
    7.94 -	if (flags == -1) {
    7.95 -		EPRINTF("F_GETFD failed (%d)\n", errno);
    7.96 -		return -errno;
    7.97 -	}
    7.98 -
    7.99 -	flags |= FD_CLOEXEC;
   7.100 -	err = fcntl(fd, F_SETFD, flags);
   7.101 -	if (err == -1) {
   7.102 -		EPRINTF("F_SETFD failed (%d)\n", errno);
   7.103 -		return -errno;
   7.104 -	}
   7.105 -
   7.106 -	len = sprintf(buf, "%ld\n", pid);
   7.107 -	err = write(fd, buf, len);
   7.108 -	if (err != len) {
   7.109 -		EPRINTF("Writing pid file failed (%d)\n", errno);
   7.110 -		return -errno;
   7.111 -	}
   7.112 -
   7.113 -	return 0;
   7.114 -}
   7.115 -
   7.116 -static int
   7.117 -tapdisk_daemon_init(void)
   7.118 -{
   7.119 -	char *devname;
   7.120 -	int i, err, blktap_major;
   7.121 -
   7.122 -	memset(&tapdisk_daemon, 0, sizeof(tapdisk_daemon_t));
   7.123 -
   7.124 -	err = asprintf(&devname, "%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME);
   7.125 -	if (err == -1) {
   7.126 -		devname = NULL;
   7.127 -		err = -ENOMEM;
   7.128 -		goto fail;
   7.129 -	}
   7.130 -
   7.131 -	err = xc_find_device_number("blktap0");
   7.132 -	if (err < 0)
   7.133 -		goto fail;
   7.134 -
   7.135 -	blktap_major = major(err);
   7.136 -	err = make_blktap_device(devname, blktap_major, 0, S_IFCHR | 0600);
   7.137 -	if (err)
   7.138 -		goto fail;
   7.139 -
   7.140 -	tapdisk_daemon.blktap_fd = open(devname, O_RDWR);
   7.141 -	if (tapdisk_daemon.blktap_fd == -1) {
   7.142 -		err = -errno;
   7.143 -		EPRINTF("blktap0 open failed\n");
   7.144 -		goto fail;
   7.145 -	}
   7.146 -
   7.147 -	for (i = 0; i < 2; i++) {
   7.148 -		tapdisk_daemon.xsh = xs_daemon_open();
   7.149 -		if (!tapdisk_daemon.xsh) {
   7.150 -			EPRINTF("xs_daemon_open failed -- is xenstore running?\n");
   7.151 -			sleep(2);
   7.152 -		} else
   7.153 -			break;
   7.154 -	}
   7.155 -
   7.156 -	if (!tapdisk_daemon.xsh) {
   7.157 -		err = -ENOSYS;
   7.158 -		goto fail;
   7.159 -	}
   7.160 -
   7.161 -	INIT_LIST_HEAD(&tapdisk_daemon.channels);
   7.162 -
   7.163 -	free(devname);
   7.164 -	return 0;
   7.165 -
   7.166 -fail:
   7.167 -	if (tapdisk_daemon.blktap_fd > 0)
   7.168 -		close(tapdisk_daemon.blktap_fd);
   7.169 -	free(devname);
   7.170 -	memset(&tapdisk_daemon, 0, sizeof(tapdisk_daemon_t));
   7.171 -	EPRINTF("%s: %d\n", __func__, err);
   7.172 -
   7.173 -	return err;
   7.174 -}
   7.175 -
   7.176 -static int
   7.177 -tapdisk_daemon_set_node(void)
   7.178 -{
   7.179 -	int err;
   7.180 -	char *domid;
   7.181 -
   7.182 -	domid = get_dom_domid(tapdisk_daemon.xsh);
   7.183 -	if (!domid)
   7.184 -		return -EAGAIN;
   7.185 -
   7.186 -	err = asprintf(&tapdisk_daemon.node,
   7.187 -		       "/local/domain/%s/backend/tap", domid);
   7.188 -	if (err == -1) {
   7.189 -		tapdisk_daemon.node = NULL;
   7.190 -		err = -ENOMEM;
   7.191 -		goto out;
   7.192 -	}
   7.193 -
   7.194 -	err = 0;
   7.195 -
   7.196 -out:
   7.197 -	free(domid);
   7.198 -	return err;
   7.199 -}
   7.200 -
   7.201 -static int
   7.202 -tapdisk_daemon_get_domid(void)
   7.203 -{
   7.204 -	int err;
   7.205 -	unsigned int num;
   7.206 -	char **res, *node, *token, *domid;
   7.207 -
   7.208 -	res = xs_read_watch(tapdisk_daemon.xsh, &num);
   7.209 -	if (!res)
   7.210 -		return -EAGAIN;
   7.211 -
   7.212 -	err   = 0;
   7.213 -	node  = res[XS_WATCH_PATH];
   7.214 -	token = res[XS_WATCH_TOKEN];
   7.215 -
   7.216 -	if (strcmp(token, TAPDISK_DAEMON_DOMID_WATCH)) {
   7.217 -		err = -EINVAL;
   7.218 -		goto out;
   7.219 -	}
   7.220 -
   7.221 -	err = tapdisk_daemon_set_node();
   7.222 -
   7.223 -out:
   7.224 -	free(res);
   7.225 -	return err;
   7.226 -}
   7.227 -
   7.228 -static int
   7.229 -tapdisk_daemon_wait_for_domid(void)
   7.230 -{
   7.231 -	int err;
   7.232 -	char *domid;
   7.233 -	fd_set readfds;
   7.234 -
   7.235 -	err = tapdisk_daemon_set_node();
   7.236 -	if (!err)
   7.237 -		return 0;
   7.238 -
   7.239 -	if (!xs_watch(tapdisk_daemon.xsh, "/local/domain",
   7.240 -		      TAPDISK_DAEMON_DOMID_WATCH)) {
   7.241 -		EPRINTF("unable to set domain id watch\n");
   7.242 -		return -EINVAL;
   7.243 -	}
   7.244 -
   7.245 -	do {
   7.246 -		FD_ZERO(&readfds);
   7.247 -		FD_SET(xs_fileno(tapdisk_daemon.xsh), &readfds);
   7.248 -
   7.249 -		select(xs_fileno(tapdisk_daemon.xsh) + 1,
   7.250 -		       &readfds, NULL, NULL, NULL);
   7.251 -
   7.252 -		if (FD_ISSET(xs_fileno(tapdisk_daemon.xsh), &readfds))
   7.253 -			err = tapdisk_daemon_get_domid();
   7.254 -		else
   7.255 -			err = -EAGAIN;
   7.256 -	} while (err == -EAGAIN);
   7.257 -
   7.258 -	xs_unwatch(tapdisk_daemon.xsh,
   7.259 -		   "/local/domain", TAPDISK_DAEMON_DOMID_WATCH);
   7.260 -	return err;
   7.261 -}
   7.262 -
   7.263 -static inline int
   7.264 -tapdisk_daemon_new_vbd_event(const char *node)
   7.265 -{
   7.266 -	return (!strcmp(node, "start-tapdisk"));
   7.267 -}
   7.268 -
   7.269 -static int
   7.270 -tapdisk_daemon_write_uuid(char *path, uint32_t uuid)
   7.271 -{
   7.272 -	int err;
   7.273 -	char *cpath, uuid_str[12];
   7.274 -
   7.275 -	snprintf(uuid_str, sizeof(uuid_str), "%u", uuid);
   7.276 -
   7.277 -	err = asprintf(&cpath, "%s/tapdisk-uuid", path);
   7.278 -	if (err == -1)
   7.279 -		return -ENOMEM;
   7.280 -
   7.281 -	err = xs_write(tapdisk_daemon.xsh, XBT_NULL,
   7.282 -		       cpath, uuid_str, strlen(uuid_str));
   7.283 -	free(cpath);
   7.284 -
   7.285 -	return (err ? 0 : -errno);
   7.286 -}
   7.287 -
   7.288 -static void
   7.289 -tapdisk_daemon_probe(struct xs_handle *xsh,
   7.290 -		     struct xenbus_watch *watch, const char *path)
   7.291 -{
   7.292 -	char *cpath;
   7.293 -	int len, err;
   7.294 -	uint32_t cookie;
   7.295 -	const char *node;
   7.296 -	tapdisk_channel_t *channel;
   7.297 -
   7.298 -	len = strsep_len(path, '/', 7);
   7.299 -	if (len < 0)
   7.300 -		return;
   7.301 -
   7.302 -	node = path + len + 1;
   7.303 -
   7.304 -	if (!tapdisk_daemon_new_vbd_event(node))
   7.305 -		return;
   7.306 -
   7.307 -	if (!xs_exists(xsh, path))
   7.308 -		return;
   7.309 -
   7.310 -	cpath = strdup(path);
   7.311 -	if (!cpath) {
   7.312 -		EPRINTF("failed to allocate control path for %s\n", path);
   7.313 -		return;
   7.314 -	}
   7.315 -	cpath[len] = '\0';
   7.316 -
   7.317 -	cookie = tapdisk_daemon.cookie++;
   7.318 -	err    = tapdisk_daemon_write_uuid(cpath, cookie);
   7.319 -	if (err)
   7.320 -		goto out;
   7.321 -
   7.322 -	DPRINTF("%s: got watch on %s, uuid = %u\n", __func__, path, cookie);
   7.323 -
   7.324 -	err = tapdisk_channel_open(&channel, cpath,
   7.325 -				   tapdisk_daemon.xsh,
   7.326 -				   tapdisk_daemon.blktap_fd,
   7.327 -				   cookie);
   7.328 -	if (!err)
   7.329 -		list_add(&channel->list, &tapdisk_daemon.channels);
   7.330 -	else
   7.331 -		EPRINTF("failed to open tapdisk channel for %s: %d\n",
   7.332 -			path, err);
   7.333 -
   7.334 -out:
   7.335 -	free(cpath);
   7.336 -}
   7.337 -
   7.338 -static int
   7.339 -tapdisk_daemon_start(void)
   7.340 -{
   7.341 -	int err;
   7.342 -
   7.343 -	err = tapdisk_daemon_wait_for_domid();
   7.344 -	if (err)
   7.345 -		return err;
   7.346 -
   7.347 -	tapdisk_daemon.watch.node     = tapdisk_daemon.node;
   7.348 -	tapdisk_daemon.watch.callback = tapdisk_daemon_probe;
   7.349 -
   7.350 -	err = register_xenbus_watch(tapdisk_daemon.xsh, &tapdisk_daemon.watch);
   7.351 -	if (err)
   7.352 -		goto fail;
   7.353 -
   7.354 -	ioctl(tapdisk_daemon.blktap_fd,
   7.355 -	      BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_INTERPOSE);
   7.356 -	ioctl(tapdisk_daemon.blktap_fd, BLKTAP_IOCTL_SENDPID, getpid());
   7.357 -
   7.358 -	return 0;
   7.359 -
   7.360 -fail:
   7.361 -	free(tapdisk_daemon.node);
   7.362 -	tapdisk_daemon.node       = NULL;
   7.363 -	tapdisk_daemon.watch.node = NULL;
   7.364 -	EPRINTF("%s: %d\n", __func__, err);
   7.365 -	return err;
   7.366 -}
   7.367 -
   7.368 -static int
   7.369 -tapdisk_daemon_stop(void)
   7.370 -{
   7.371 -	unregister_xenbus_watch(tapdisk_daemon.xsh, &tapdisk_daemon.watch);
   7.372 -
   7.373 -	ioctl(tapdisk_daemon.blktap_fd,
   7.374 -	      BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_PASSTHROUGH);
   7.375 -	close(tapdisk_daemon.blktap_fd);
   7.376 -
   7.377 -	return 0;
   7.378 -}
   7.379 -
   7.380 -static void
   7.381 -tapdisk_daemon_free(void)
   7.382 -{
   7.383 -	free(tapdisk_daemon.node);
   7.384 -	xs_daemon_close(tapdisk_daemon.xsh);
   7.385 -	memset(&tapdisk_daemon, 0, sizeof(tapdisk_daemon_t));
   7.386 -}
   7.387 -
   7.388 -static int
   7.389 -tapdisk_daemon_read_message(int fd, tapdisk_message_t *message, int timeout)
   7.390 -{
   7.391 -	fd_set readfds;
   7.392 -	struct timeval tv;
   7.393 -	int ret, len, offset;
   7.394 -
   7.395 -	tv.tv_sec  = timeout;
   7.396 -	tv.tv_usec = 0;
   7.397 -	offset     = 0;
   7.398 -	len        = sizeof(tapdisk_message_t);
   7.399 -
   7.400 -	memset(message, 0, sizeof(tapdisk_message_t));
   7.401 -
   7.402 -	while (offset < len) {
   7.403 -		FD_ZERO(&readfds);
   7.404 -		FD_SET(fd, &readfds);
   7.405 -
   7.406 -		/* we don't bother reinitializing tv. at worst, it will wait a
   7.407 -		 * bit more time than expected. */
   7.408 -
   7.409 -		ret = select(fd + 1, &readfds, NULL, NULL, &tv);
   7.410 -		if (ret == -1)
   7.411 -			break;
   7.412 -		else if (FD_ISSET(fd, &readfds)) {
   7.413 -			ret = read(fd, message + offset, len - offset);
   7.414 -			if (ret <= 0)
   7.415 -				break;
   7.416 -			offset += ret;
   7.417 -		} else
   7.418 -			break;
   7.419 -	}
   7.420 -
   7.421 -	return (offset == len ? 0 : -EIO);
   7.422 -}
   7.423 -
   7.424 -static int
   7.425 -tapdisk_daemon_receive_message(int fd)
   7.426 -{
   7.427 -	int err;
   7.428 -	tapdisk_message_t m;
   7.429 -	tapdisk_channel_t *c, *tmp;
   7.430 -
   7.431 -	err = tapdisk_daemon_read_message(fd, &m, 2);
   7.432 -	if (err) {
   7.433 -		EPRINTF("failed reading message on %d: %d\n", fd, err);
   7.434 -		return err;
   7.435 -	}
   7.436 -
   7.437 -	tapdisk_daemon_for_each_channel(c, tmp)
   7.438 -		if (c->cookie == m.cookie && c->read_fd == fd) {
   7.439 -			DPRINTF("got '%s' message from %d:%d\n",
   7.440 -				tapdisk_message_name(m.type),
   7.441 -				c->channel_id, c->cookie);
   7.442 -
   7.443 -			return tapdisk_channel_receive_message(c, &m);
   7.444 -		}
   7.445 -
   7.446 -	EPRINTF("unrecognized message on %d: '%s' (uuid = %u)\n",
   7.447 -		fd, tapdisk_message_name(m.type), m.cookie);
   7.448 -
   7.449 -	return -EINVAL;
   7.450 -}
   7.451 -
   7.452 -static int
   7.453 -tapdisk_daemon_set_fds(fd_set *readfds)
   7.454 -{
   7.455 -	int max, fd;
   7.456 -	tapdisk_channel_t *channel, *tmp;
   7.457 -
   7.458 -	max = xs_fileno(tapdisk_daemon.xsh);
   7.459 -
   7.460 -	FD_ZERO(readfds);
   7.461 -	FD_SET(max, readfds);
   7.462 -
   7.463 -	tapdisk_daemon_for_each_channel(channel, tmp) {
   7.464 -		fd  = channel->read_fd;
   7.465 -		max = MAX(fd, max);
   7.466 -		FD_SET(fd, readfds);
   7.467 -	}
   7.468 -
   7.469 -	return max;
   7.470 -}
   7.471 -
   7.472 -static int
   7.473 -tapdisk_daemon_check_fds(fd_set *readfds)
   7.474 -{
   7.475 -	int err;
   7.476 -	tapdisk_channel_t *channel, *tmp;
   7.477 -
   7.478 -	if (FD_ISSET(xs_fileno(tapdisk_daemon.xsh), readfds))
   7.479 -		xs_fire_next_watch(tapdisk_daemon.xsh);
   7.480 -
   7.481 -	tapdisk_daemon_for_each_channel(channel, tmp)
   7.482 -		if (FD_ISSET(channel->read_fd, readfds))
   7.483 -			return tapdisk_daemon_receive_message(channel->read_fd);
   7.484 -
   7.485 -	return 0;
   7.486 -}
   7.487 -
   7.488 -static int
   7.489 -tapdisk_daemon_run(void)
   7.490 -{
   7.491 -	int err, max;
   7.492 -	fd_set readfds;
   7.493 -
   7.494 -	while (1) {
   7.495 -		max = tapdisk_daemon_set_fds(&readfds);
   7.496 -
   7.497 -		err = select(max + 1, &readfds, NULL, NULL, NULL);
   7.498 -		if (err < 0)
   7.499 -			continue;
   7.500 -
   7.501 -		err = tapdisk_daemon_check_fds(&readfds);
   7.502 -	}
   7.503 -
   7.504 -	return err;
   7.505 -}
   7.506 -
   7.507 -void
   7.508 -tapdisk_daemon_find_channel(tapdisk_channel_t *channel)
   7.509 -{
   7.510 -	tapdisk_channel_t *c, *tmp;
   7.511 -
   7.512 -	channel->read_fd     = 0;
   7.513 -	channel->write_fd    = 0;
   7.514 -	channel->tapdisk_pid = 0;
   7.515 -
   7.516 -	/* do we want multiple vbds per tapdisk? */
   7.517 -	if (!xs_exists(tapdisk_daemon.xsh, channel->share_tapdisk_str)) {
   7.518 -		channel->shared = 0;
   7.519 -		return;
   7.520 -	}
   7.521 -
   7.522 -	channel->shared = 1;
   7.523 -
   7.524 -	/* check if we already have a process started */
   7.525 -	tapdisk_daemon_for_each_channel(c, tmp)
   7.526 -		if (c->drivertype == channel->drivertype) {
   7.527 -			channel->write_fd    = c->write_fd;
   7.528 -			channel->read_fd     = c->read_fd;
   7.529 -			channel->channel_id  = c->channel_id;
   7.530 -			channel->tapdisk_pid = c->tapdisk_pid;
   7.531 -			return;
   7.532 -		}
   7.533 -}
   7.534 -
   7.535 -void
   7.536 -tapdisk_daemon_close_channel(tapdisk_channel_t *channel)
   7.537 -{
   7.538 -	tapdisk_channel_t *c, *tmp;
   7.539 -
   7.540 -	list_del(&channel->list);
   7.541 -
   7.542 -	tapdisk_daemon_for_each_channel(c, tmp)
   7.543 -		if (c->channel_id == channel->channel_id)
   7.544 -			return;
   7.545 -
   7.546 -	close(channel->read_fd);
   7.547 -	close(channel->write_fd);
   7.548 -}
   7.549 -
   7.550 -int
   7.551 -main(int argc, char *argv[])
   7.552 -{
   7.553 -	int err;
   7.554 -	char buf[128];
   7.555 -
   7.556 -	if (daemon(0, 0)) {
   7.557 -	  EPRINTF("daemon() failed (%d)\n", errno);
   7.558 -	  return -errno;
   7.559 -	}
   7.560 -
   7.561 -#define CORE_DUMP
   7.562 -#if defined(CORE_DUMP)
   7.563 -	{
   7.564 -		/* set up core-dumps*/
   7.565 -		struct rlimit rlim;
   7.566 -		rlim.rlim_cur = RLIM_INFINITY;
   7.567 -		rlim.rlim_max = RLIM_INFINITY;
   7.568 -		if (setrlimit(RLIMIT_CORE, &rlim) < 0)
   7.569 -			EPRINTF("setrlimit failed: %d\n", errno);
   7.570 -	}
   7.571 -#endif
   7.572 -
   7.573 -	snprintf(buf, sizeof(buf), "BLKTAP-DAEMON[%d]", getpid());
   7.574 -	openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
   7.575 -
   7.576 -	err = tapdisk_daemon_write_pidfile(getpid());
   7.577 -	if (err)
   7.578 -		goto out;
   7.579 -
   7.580 -	tapdisk_daemon_print_drivers();
   7.581 -
   7.582 -	err = tapdisk_daemon_init();
   7.583 -	if (err)
   7.584 -		goto out;
   7.585 -
   7.586 -	err = tapdisk_daemon_start();
   7.587 -	if (err)
   7.588 -		goto out;
   7.589 -
   7.590 -	tapdisk_daemon_run();
   7.591 -
   7.592 -	tapdisk_daemon_stop();
   7.593 -	tapdisk_daemon_free();
   7.594 -
   7.595 -	err = 0;
   7.596 -
   7.597 -out:
   7.598 -	if (err)
   7.599 -		EPRINTF("failed to start %s: %d\n", argv[0], err);
   7.600 -	closelog();
   7.601 -	return err;
   7.602 -}
     8.1 --- a/tools/blktap2/daemon/tapdisk-dispatch-common.c	Thu Jun 18 10:27:21 2009 +0100
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,94 +0,0 @@
     8.4 -/*
     8.5 - * (c) 2005 Andrew Warfield and Julian Chesterfield
     8.6 - *
     8.7 - * This program is free software; you can redistribute it and/or
     8.8 - * modify it under the terms of the GNU General Public License version 2
     8.9 - * as published by the Free Software Foundation; or, when distributed
    8.10 - * separately from the Linux kernel or incorporated into other
    8.11 - * software packages, subject to the following license:
    8.12 - *
    8.13 - * Permission is hereby granted, free of charge, to any person obtaining a copy
    8.14 - * of this source file (the "Software"), to deal in the Software without
    8.15 - * restriction, including without limitation the rights to use, copy, modify,
    8.16 - * merge, publish, distribute, sublicense, and/or sell copies of the Software,
    8.17 - * and to permit persons to whom the Software is furnished to do so, subject to
    8.18 - * the following conditions:
    8.19 - *
    8.20 - * The above copyright notice and this permission notice shall be included in
    8.21 - * all copies or substantial portions of the Software.
    8.22 - *
    8.23 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    8.24 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    8.25 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    8.26 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    8.27 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    8.28 - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    8.29 - * IN THE SOFTWARE.
    8.30 - */
    8.31 -#include <errno.h>
    8.32 -#include <fcntl.h>
    8.33 -#include <unistd.h>
    8.34 -#include <sys/stat.h>
    8.35 -#include <sys/types.h>
    8.36 -
    8.37 -#include "tapdisk-dispatch.h"
    8.38 -
    8.39 -int
    8.40 -strsep_len(const char *str, char c, unsigned int len)
    8.41 -{
    8.42 -	unsigned int i;
    8.43 -	
    8.44 -	for (i = 0; str[i]; i++)
    8.45 -		if (str[i] == c) {
    8.46 -			if (len == 0)
    8.47 -				return i;
    8.48 -			len--;
    8.49 -		}
    8.50 -
    8.51 -	return (len == 0) ? i : -ERANGE;
    8.52 -}
    8.53 -
    8.54 -int
    8.55 -make_blktap_device(char *devname, int major, int minor, int perm)
    8.56 -{
    8.57 -	int err;
    8.58 -
    8.59 -	err = unlink(devname);
    8.60 -	if (err && errno != ENOENT) {
    8.61 -		EPRINTF("unlink %s failed: %d\n", devname, errno);
    8.62 -		return -errno;
    8.63 -	}
    8.64 -
    8.65 -	/* Need to create device */
    8.66 -	err = mkdir(BLKTAP_DEV_DIR, 0755);
    8.67 -	if (err && errno != EEXIST) {
    8.68 -		EPRINTF("Failed to create %s directory\n", BLKTAP_DEV_DIR);
    8.69 -		return -errno;
    8.70 -	}
    8.71 -
    8.72 -	err = mknod(devname, perm, makedev(major, minor));
    8.73 -	if (err) {
    8.74 -		int ret = -errno;
    8.75 -		struct stat st;
    8.76 -
    8.77 -		EPRINTF("mknod %s failed: %d\n", devname, -errno);
    8.78 -
    8.79 -		err = lstat(devname, &st);
    8.80 -		if (err) {
    8.81 -			DPRINTF("lstat %s failed: %d\n", devname, -errno);
    8.82 -			err = access(devname, F_OK);
    8.83 -			if (err)
    8.84 -				DPRINTF("access %s failed: %d\n", devname, -errno);
    8.85 -			else
    8.86 -				DPRINTF("access %s succeeded\n", devname);
    8.87 -		} else
    8.88 -			DPRINTF("lstat %s: %u:%u\n", devname,
    8.89 -				(unsigned int)st.st_rdev >> 8,
    8.90 -				(unsigned int)st.st_rdev & 0xff);
    8.91 -
    8.92 -		return ret;
    8.93 -	}
    8.94 -
    8.95 -	DPRINTF("Created %s device\n", devname);
    8.96 -	return 0;
    8.97 -}
     9.1 --- a/tools/blktap2/daemon/tapdisk-dispatch.h	Thu Jun 18 10:27:21 2009 +0100
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,95 +0,0 @@
     9.4 -/* Copyright (c) 2008, XenSource Inc.
     9.5 - * All rights reserved.
     9.6 - *
     9.7 - * Redistribution and use in source and binary forms, with or without
     9.8 - * modification, are permitted provided that the following conditions are met:
     9.9 - *     * Redistributions of source code must retain the above copyright
    9.10 - *       notice, this list of conditions and the following disclaimer.
    9.11 - *     * Redistributions in binary form must reproduce the above copyright
    9.12 - *       notice, this list of conditions and the following disclaimer in the
    9.13 - *       documentation and/or other materials provided with the distribution.
    9.14 - *     * Neither the name of XenSource Inc. nor the names of its contributors
    9.15 - *       may be used to endorse or promote products derived from this software
    9.16 - *       without specific prior written permission.
    9.17 - *
    9.18 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    9.19 - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    9.20 - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    9.21 - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    9.22 - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    9.23 - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    9.24 - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    9.25 - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    9.26 - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    9.27 - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    9.28 - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    9.29 -*/
    9.30 -#ifndef _TAPDISK_DISPATCH_H_
    9.31 -#define _TAPDISK_DISPATCH_H_
    9.32 -
    9.33 -#include "xs_api.h"
    9.34 -#include "blktaplib.h"
    9.35 -#include "tapdisk-message.h"
    9.36 -
    9.37 -struct tapdisk_channel {
    9.38 -	int                       state;
    9.39 -
    9.40 -	int                       read_fd;
    9.41 -	int                       write_fd;
    9.42 -	int                       blktap_fd;
    9.43 -	int                       channel_id;
    9.44 -
    9.45 -	char                      mode;
    9.46 -	char                      shared;
    9.47 -	char                      open;
    9.48 -	unsigned int              domid;
    9.49 -	unsigned int              busid;
    9.50 -	unsigned int              major;
    9.51 -	unsigned int              minor;
    9.52 -	unsigned int              storage;
    9.53 -	unsigned int              drivertype;
    9.54 -	uint16_t                  cookie;
    9.55 -	pid_t                     tapdisk_pid;
    9.56 -
    9.57 -	/*
    9.58 -	 * special accounting needed to handle pause
    9.59 -	 * requests received before tapdisk process is ready
    9.60 -	 */
    9.61 -	char                      connected;
    9.62 -	char                      pause_needed;
    9.63 -
    9.64 -	char                     *path;
    9.65 -	char                     *frontpath;
    9.66 -	char                     *params;
    9.67 -	char                     *vdi_path;
    9.68 -	char                     *uuid_str;
    9.69 -	char                     *pause_str;
    9.70 -	char                     *pause_done_str;
    9.71 -	char                     *shutdown_str;
    9.72 -	char                     *share_tapdisk_str;
    9.73 -
    9.74 -	image_t                   image;
    9.75 -
    9.76 -	struct list_head          list;
    9.77 -	struct xenbus_watch       pause_watch;
    9.78 -	struct xenbus_watch       shutdown_watch;
    9.79 -
    9.80 -	struct xs_handle         *xsh;
    9.81 -};
    9.82 -
    9.83 -typedef struct tapdisk_channel tapdisk_channel_t;
    9.84 -
    9.85 -int strsep_len(const char *str, char c, unsigned int len);
    9.86 -int make_blktap_device(char *devname, int major, int minor, int perm);
    9.87 -
    9.88 -int tapdisk_channel_open(tapdisk_channel_t **,
    9.89 -			 char *node, struct xs_handle *,
    9.90 -			 int blktap_fd, uint16_t cookie);
    9.91 -void tapdisk_channel_close(tapdisk_channel_t *);
    9.92 -
    9.93 -void tapdisk_daemon_find_channel(tapdisk_channel_t *);
    9.94 -void tapdisk_daemon_close_channel(tapdisk_channel_t *);
    9.95 -
    9.96 -int tapdisk_channel_receive_message(tapdisk_channel_t *, tapdisk_message_t *);
    9.97 -
    9.98 -#endif
    10.1 --- a/tools/blktap2/drivers/Makefile	Thu Jun 18 10:27:21 2009 +0100
    10.2 +++ b/tools/blktap2/drivers/Makefile	Thu Jun 18 10:29:58 2009 +0100
    10.3 @@ -4,7 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
    10.4  
    10.5  LIBVHDDIR  = $(BLKTAP_ROOT)/vhd/lib
    10.6  
    10.7 -IBIN       = tapdisk tapdisk2 td-util tapdisk-client tapdisk-stream tapdisk-diff
    10.8 +IBIN       = tapdisk2 td-util tapdisk-client tapdisk-stream tapdisk-diff
    10.9  QCOW_UTIL  = img2qcow qcow-create qcow2raw
   10.10  LOCK_UTIL  = lock-util
   10.11  INST_DIR   = $(SBINDIR)
   10.12 @@ -34,11 +34,11 @@ endif
   10.13  
   10.14  LDFLAGS_img := $(CRYPT_LIB) -lpthread -lz
   10.15  
   10.16 -tapdisk tapdisk2 td-util tapdisk-stream tapdisk-diff $(QCOW_UTIL): LIBS += -L$(LIBVHDDIR) -lvhd -luuid
   10.17 +tapdisk2 td-util tapdisk-stream tapdisk-diff $(QCOW_UTIL): LIBS += -L$(LIBVHDDIR) -lvhd -luuid
   10.18  
   10.19  LIBAIO_DIR = $(XEN_ROOT)/tools/libaio/src
   10.20 -tapdisk tapdisk2 tapdisk-stream tapdisk-diff $(QCOW_UTIL): AIOLIBS := $(LIBAIO_DIR)/libaio.a
   10.21 -tapdisk tapdisk-client tapdisk-stream tapdisk-diff $(QCOW_UTIL): CFLAGS  += -I$(LIBAIO_DIR) -I$(XEN_LIBXC)
   10.22 +tapdisk2 tapdisk-stream tapdisk-diff $(QCOW_UTIL): AIOLIBS := $(LIBAIO_DIR)/libaio.a
   10.23 +tapdisk-client tapdisk-stream tapdisk-diff $(QCOW_UTIL): CFLAGS  += -I$(LIBAIO_DIR) -I$(XEN_LIBXC)
   10.24  
   10.25  ifeq ($(VHD_STATIC),y)
   10.26  td-util: CFLAGS += -static
   10.27 @@ -71,8 +71,6 @@ BLK-OBJS-y  += aes.o
   10.28  
   10.29  all: $(IBIN) lock-util qcow-util
   10.30  
   10.31 -tapdisk: $(TAP-OBJS-y) $(BLK-OBJS-y) $(MISC-OBJS-y) tapdisk.c
   10.32 -	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(AIOLIBS)  $(LDFLAGS_img)
   10.33  
   10.34  tapdisk2: $(TAP-OBJS-y) $(BLK-OBJS-y) $(MISC-OBJS-y) tapdisk2.c
   10.35  	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS) $(AIOLIBS) $(LDFLAGS_img)
    11.1 --- a/tools/blktap2/drivers/tapdisk.c	Thu Jun 18 10:27:21 2009 +0100
    11.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.3 @@ -1,66 +0,0 @@
    11.4 -/*
    11.5 - * Copyright (c) 2008, XenSource Inc.
    11.6 - * All rights reserved.
    11.7 - *
    11.8 - * Redistribution and use in source and binary forms, with or without
    11.9 - * modification, are permitted provided that the following conditions are met:
   11.10 - *     * Redistributions of source code must retain the above copyright
   11.11 - *       notice, this list of conditions and the following disclaimer.
   11.12 - *     * Redistributions in binary form must reproduce the above copyright
   11.13 - *       notice, this list of conditions and the following disclaimer in the
   11.14 - *       documentation and/or other materials provided with the distribution.
   11.15 - *     * Neither the name of XenSource Inc. nor the names of its contributors
   11.16 - *       may be used to endorse or promote products derived from this software
   11.17 - *       without specific prior written permission.
   11.18 - *
   11.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   11.20 - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   11.21 - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   11.22 - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
   11.23 - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   11.24 - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   11.25 - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   11.26 - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   11.27 - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   11.28 - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   11.29 - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   11.30 - */
   11.31 -#include <stdio.h>
   11.32 -#include <errno.h>
   11.33 -#include <stdlib.h>
   11.34 -#include <unistd.h>
   11.35 -
   11.36 -#include "tapdisk-utils.h"
   11.37 -#include "tapdisk-server.h"
   11.38 -
   11.39 -static void
   11.40 -usage(void)
   11.41 -{
   11.42 -	fprintf(stderr, "blktap-utils: v2.0.0\n");
   11.43 -	fprintf(stderr, "usage: tapdisk <READ fifo> <WRITE fifo>\n");
   11.44 -	exit(EINVAL);
   11.45 -}
   11.46 -
   11.47 -int
   11.48 -main(int argc, char *argv[])
   11.49 -{
   11.50 -	int err;
   11.51 -
   11.52 -	if (argc != 3)
   11.53 -		usage();
   11.54 -
   11.55 -	daemon(0, 0);
   11.56 -	tapdisk_start_logging("TAPDISK");
   11.57 -
   11.58 -	err = tapdisk_server_initialize(argv[1], argv[2]);
   11.59 -	if (err) {
   11.60 -		EPRINTF("failed to initialize tapdisk server: %d\n", err);
   11.61 -		goto out;
   11.62 -	}
   11.63 -
   11.64 -	err = tapdisk_server_run();
   11.65 -
   11.66 -out:
   11.67 -	tapdisk_stop_logging();
   11.68 -	return err;
   11.69 -}
    12.1 --- a/tools/python/xen/xend/server/BlktapController.py	Thu Jun 18 10:27:21 2009 +0100
    12.2 +++ b/tools/python/xen/xend/server/BlktapController.py	Thu Jun 18 10:29:58 2009 +0100
    12.3 @@ -126,9 +126,19 @@ class BlktapController(BlkifController):
    12.4          except:
    12.5              (typ, params, file) = string.split(uname, ':', 2)
    12.6              subtyp = 'tapdisk'
    12.7 +
    12.8 +        #check for blktap2 installation.
    12.9 +        blktap2_installed=0;
   12.10 +        (rc,stdout, stderr) = doexec("cat /proc/devices");
   12.11 +        out = stdout.read();
   12.12 +        stdout.close();
   12.13 +        stderr.close();
   12.14 +        if( out.find("blktap2") >= 0 ):
   12.15 +            blktap2_installed=1;
   12.16 +           
   12.17          if typ in ('tap'):
   12.18              if subtyp in ('tapdisk'):                                          
   12.19 -                if params in ('ioemu', 'qcow2', 'vmdk', 'sync'):
   12.20 +                if params in ('ioemu', 'qcow2', 'vmdk', 'sync') or not blktap2_installed:
   12.21                      log.warn('WARNING: using deprecated blktap module');
   12.22                      return BlkifController.createDevice(self, config);
   12.23