blktap2: a completely rewritten blktap implementation

Benefits to blktap2 over the old version of blktap:

* Isolation from xenstore - Blktap devices are now created directly on
   the linux dom0 command line, rather than being spawned in response
   to XenStore events.  This is handy for debugging, makes blktap
   generally easier to work with, and is a step toward a generic
   user-level block device implementation that is not Xen-specific.

* Improved tapdisk infrastructure: simpler request forwarding, new
   request scheduler, request merging, more efficient use of AIO.

* Improved tapdisk error handling and memory management.  No
   allocations on the block data path, IO retry logic to protect
   guests
   transient block device failures.  This has been tested and is known
   to work on weird environments such as NFS soft mounts.

* Pause and snapshot of live virtual disks (see xmsnap script).

* VHD support.  The VHD code in this release has been rigorously
   tested, and represents a very mature implementation of the VHD
   image
   format.

* No more duplication of mechanism with blkback.  The blktap kernel
   module has changed dramatically from the original blktap.  Blkback
   is now always used to talk to Xen guests, blktap just presents a
   Linux gendisk that blkback can export.  This is done while
   preserving the zero-copy data path from domU to physical device.

These patches deprecate the old blktap code, which can hopefully be
removed from the tree completely at some point in the future.

Signed-off-by: Jake Wires <jake.wires@citrix.com>
Signed-off-by: Dutch Meyer <dmeyer@cs.ubc.ca>
This commit is contained in:
Keir Fraser committed 2009-05-26 11:52:31 +01:00
1 parent 6009f4ddb2
commit bd5573a630
108 files changed
+35869 -5

No files matched your search

+13 -1
View File
@@ -103,7 +103,19 @@
^stubdom/lwip/
^stubdom/ioemu/
^tools/.*/build/lib.*/.*\.py$
^tools/blktap/Makefile\.smh$
^tools/blktap2/daemon/blktapctrl$
^tools/blktap2/drivers/img2qcow$
^tools/blktap2/drivers/lock-util$
^tools/blktap2/drivers/qcow-create$
^tools/blktap2/drivers/qcow2raw$
^tools/blktap2/drivers/tapdisk$
^tools/blktap2/drivers/tapdisk-client$
^tools/blktap2/drivers/tapdisk-diff$
^tools/blktap2/drivers/tapdisk-stream$
^tools/blktap2/drivers/tapdisk2$
^tools/blktap2/drivers/td-util$
^tools/blktap2/vhd/vhd-update$
^tools/blktap2/vhd/vhd-util$
^tools/blktap/drivers/blktapctrl$
^tools/blktap/drivers/img2qcow$
^tools/blktap/drivers/qcow-create$
+1
View File
@@ -22,6 +22,7 @@ SUBDIRS-$(VTPM_TOOLS) += vtpm
SUBDIRS-y += xenstat
SUBDIRS-$(CONFIG_Linux) += libaio
SUBDIRS-$(CONFIG_Linux) += blktap
SUBDIRS-$(CONFIG_Linux) += blktap2
SUBDIRS-y += libfsimage
SUBDIRS-$(LIBXENAPI_BINDINGS) += libxen
SUBDIRS-$(CONFIG_Linux) += fs-back
+34
View File
@@ -0,0 +1,34 @@
XEN_ROOT = ../..
include $(XEN_ROOT)/tools/Rules.mk
CFLAGS += $(CFLAGS_libxenctrl)
LDFLAGS += $(LDFLAGS_libxenctrl)
SUBDIRS-y :=
SUBDIRS-y += include
SUBDIRS-y += lvm
SUBDIRS-y += vhd
SUBDIRS-y += drivers
SUBDIRS-y += daemon
.PHONY: all
all: build
.PHONY: build
build:
@set -e; for subdir in $(SUBDIRS-y); do \
$(MAKE) -C $$subdir all; \
done
.PHONY: install
install:
@set -e; for subdir in $(SUBDIRS-y); do \
$(MAKE) -C $$subdir install; \
done
.PHONY: clean
clean:
rm -rf *.a *.so *.o *.rpm $(LIB) *~ $(DEPS) TAGS
@set -e; for subdir in $(SUBDIRS-y); do \
$(MAKE) -C $$subdir clean; \
done
+122
View File
@@ -0,0 +1,122 @@
Blktap Userspace Tools + Library
================================
Andrew Warfield and Julian Chesterfield
16th June 2006
{firstname.lastname}@cl.cam.ac.uk
The blktap userspace toolkit provides a user-level disk I/O
interface. The blktap mechanism involves a kernel driver that acts
similarly to the existing Xen/Linux blkback driver, and a set of
associated user-level libraries. Using these tools, blktap allows
virtual block devices presented to VMs to be implemented in userspace
and to be backed by raw partitions, files, network, etc.
The key benefit of blktap is that it makes it easy and fast to write
arbitrary block backends, and that these user-level backends actually
perform very well. Specifically:
- Metadata disk formats such as Copy-on-Write, encrypted disks, sparse
formats and other compression features can be easily implemented.
- Accessing file-based images from userspace avoids problems related
to flushing dirty pages which are present in the Linux loopback
driver. (Specifically, doing a large number of writes to an
NFS-backed image don't result in the OOM killer going berserk.)
- Per-disk handler processes enable easier userspace policing of block
resources, and process-granularity QoS techniques (disk scheduling
and related tools) may be trivially applied to block devices.
- It's very easy to take advantage of userspace facilities such as
networking libraries, compression utilities, peer-to-peer
file-sharing systems and so on to build more complex block backends.
- Crashes are contained -- incremental development/debugging is very
fast.
How it works (in one paragraph):
Working in conjunction with the kernel blktap driver, all disk I/O
requests from VMs are passed to the userspace deamon (using a shared
memory interface) through a character device. Each active disk is
mapped to an individual device node, allowing per-disk processes to
implement individual block devices where desired. The userspace
drivers are implemented using asynchronous (Linux libaio),
O_DIRECT-based calls to preserve the unbuffered, batched and
asynchronous request dispatch achieved with the existing blkback
code. We provide a simple, asynchronous virtual disk interface that
makes it quite easy to add new disk implementations.
As of June 2006 the current supported disk formats are:
- Raw Images (both on partitions and in image files)
- File-backed Qcow disks
- Standalone sparse Qcow disks
- Fast shareable RAM disk between VMs (requires some form of cluster-based
filesystem support e.g. OCFS2 in the guest kernel)
- Some VMDK images - your mileage may vary
Raw and QCow images have asynchronous backends and so should perform
fairly well. VMDK is based directly on the qemu vmdk driver, which is
synchronous (a.k.a. slow).
Build and Installation Instructions
===================================
Make to configure the blktap backend driver in your dom0 kernel. It
will cooperate fine with the existing backend driver, so you can
experiment with tap disks without breaking existing VM configs.
To build the tools separately, "make && make install" in
tools/blktap.
Using the Tools
===============
Prepare the image for booting. For qcow files use the qcow utilities
installed earlier. e.g. qcow-create generates a blank standalone image
or a file-backed CoW image. img2qcow takes an existing image or
partition and creates a sparse, standalone qcow-based file.
The userspace disk agent is configured to start automatically via xend
(alternatively you can start it manually => 'blktapctrl')
Customise the VM config file to use the 'tap' handler, followed by the
driver type. e.g. for a raw image such as a file or partition:
disk = ['tap:aio:<FILENAME>,sda1,w']
e.g. for a qcow image:
disk = ['tap:qcow:<FILENAME>,sda1,w']
Mounting images in Dom0 using the blktap driver
===============================================
Tap (and blkback) disks are also mountable in Dom0 without requiring an
active VM to attach. You will need to build a xenlinux Dom0 kernel that
includes the blkfront driver (e.g. the default 'make world' or
'make kernels' build. Simply use the xm command-line tool to activate
the backend disks, and blkfront will generate a virtual block device that
can be accessed in the same way as a loop device or partition:
e.g. for a raw image file <FILENAME> that would normally be mounted using
the loopback driver (such as 'mount -o loop <FILENAME> /mnt/disk'), do the
following:
xm block-attach 0 tap:aio:<FILENAME> /dev/xvda1 w 0
mount /dev/xvda1 /mnt/disk <--- don't use loop driver
In this way, you can use any of the userspace device-type drivers built
with the blktap userspace toolkit to open and mount disks such as qcow
or vmdk images:
xm block-attach 0 tap:qcow:<FILENAME> /dev/xvda1 w 0
mount /dev/xvda1 /mnt/disk
+55
View File
@@ -0,0 +1,55 @@
XEN_ROOT=../../../
BLKTAP_ROOT := ..
include $(XEN_ROOT)/tools/Rules.mk
IBIN = blktapctrl
INST_DIR = $(SBINDIR)
LIBDIR = lib
LIBS := -lxenstore
LIBS += -Llib
LIBS += -lblktap
LIBS += -lxenctrl
ifneq ($(USE_SYSTEM_LIBRARIES),y)
INCLUDES += -I $(XEN_LIBXC) -I $(XEN_XENSTORE)
LIBS += -L $(XEN_LIBXC) -L $(XEN_XENSTORE)
endif
OBJS := tapdisk-dispatch-common.o
OBJS += tapdisk-channel.o
CFLAGS += -Werror
CFLAGS += -Wno-unused
CFLAGS += -fno-strict-aliasing -fPIC
CFLAGS += -Ilib -I../include -I../drivers -I../../include $(INCLUDES)
CFLAGS += -D_GNU_SOURCE
CFLAGS += -g
# Get gcc to generate the dependencies for us.
CFLAGS += -Wp,-MD,.$(@F).d
DEPS = .*.d
all: libblktap $(IBIN)
blktapctrl: tapdisk-daemon.c $(OBJS)
$(CC) $(CFLAGS) -o blktapctrl tapdisk-daemon.c $(LIBS) $(OBJS)
libblktap:
@set -e
$(MAKE) -C $(LIBDIR) all
install: all
$(MAKE) -C $(LIBDIR) install
$(INSTALL_DIR) -p $(DESTDIR)$(INST_DIR)
$(INSTALL_PROG) $(IBIN) $(DESTDIR)$(INST_DIR)
clean:
$(MAKE) -C $(LIBDIR) clean
rm -rf *.o *~ $(IBIN) $(DEPS) xen TAGS
.PHONY: all clean install blktapctrl libblktap
-include $(DEPS)
+69
View File
@@ -0,0 +1,69 @@
XEN_ROOT=../../../../
BLKTAP_ROOT := ../../
include $(XEN_ROOT)/tools/Rules.mk
MAJOR = 3.1
MINOR = 0
SONAME = libblktap.so.$(MAJOR)
BLKTAP_INSTALL_DIR = /usr/sbin
LIBS := -lxenstore
ifneq ($(USE_SYSTEM_LIBRARIES),y)
INCLUDES += -I $(XEN_LIBXC) -I $(XEN_XENSTORE)
LIBS += -L$(XEN_XENSTORE)
endif
SRCS :=
SRCS += xs_api.c
CFLAGS += -Werror
CFLAGS += -Wno-unused
CFLAGS += -fno-strict-aliasing -fPIC
# get asprintf():
CFLAGS += -D _GNU_SOURCE
CFLAGS += -g
CFLAGS += -I../../include -I../../../include/ $(INCLUDES)
# Get gcc to generate the dependencies for us.
CFLAGS += -Wp,-MD,.$(@F).d
DEPS = .*.d
OBJS = $(patsubst %.c,%.o,$(SRCS))
IBINS :=
LIB = libblktap.a libblktap.so.$(MAJOR).$(MINOR)
.PHONY: all
all: build
.PHONY: build
build: libblktap.a
.PHONY: libblktap
libblktap: libblktap.a
install: all
$(INSTALL_DIR) -p $(DESTDIR)$(LIBDIR)
$(INSTALL_DATA) $(LIB) $(DESTDIR)$(LIBDIR)
ln -sf libblktap.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)/libblktap.so.$(MAJOR)
ln -sf libblktap.so.$(MAJOR) $(DESTDIR)$(LIBDIR)/libblktap.so
clean:
rm -rf *.a *.so* *.o *.rpm $(LIB) *~ $(DEPS) xen TAGS
libblktap.a: $(OBJS)
$(CC) $(CFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,$(SONAME) $(SHLIB_CFLAGS) \
-o libblktap.so.$(MAJOR).$(MINOR) $^ $(LIBS)
ln -sf libblktap.so.$(MAJOR).$(MINOR) libblktap.so.$(MAJOR)
ln -sf libblktap.so.$(MAJOR) libblktap.so
$(AR) rc $@ libblktap.so
.PHONY: TAGS all build clean install libblktap
TAGS:
etags -t $(SRCS) *.h
-include $(DEPS)
+323
View File
@@ -0,0 +1,323 @@
/*
* xs_api.c
*
* blocktap interface functions to xenstore
*
* (c) 2005 Andrew Warfield and Julian Chesterfield
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <xs.h>
#include "xs_api.h"
#include "blktaplib.h"
#define DOMNAME "Domain-0"
#define BASE_DEV_VAL 2048
static LIST_HEAD(watches);
int
xs_gather(struct xs_handle *xs, const char *dir, ...)
{
va_list ap;
const char *name;
char *path, **e;
int ret = 0, num,i;
unsigned int len;
xs_transaction_t xth;
again:
if ((xth = xs_transaction_start(xs)) == XBT_NULL) {
DPRINTF("unable to start xs trasanction\n");
ret = ENOMEM;
return ret;
}
va_start(ap, dir);
while ((ret == 0) && (name = va_arg(ap, char *)) != NULL) {
char *p;
const char *fmt = va_arg(ap, char *);
void *result = va_arg(ap, void *);
if (asprintf(&path, "%s/%s", dir, name) == -1) {
EPRINTF("allocation error in xs_gather!\n");
ret = ENOMEM;
break;
}
p = xs_read(xs, xth, path, &len);
free(path);
if (!p) {
ret = ENOENT;
break;
}
if (fmt) {
if (sscanf(p, fmt, result) == 0)
ret = EINVAL;
free(p);
} else
*(char **)result = p;
}
va_end(ap);
if (!xs_transaction_end(xs, xth, ret)) {
if (ret == 0 && errno == EAGAIN)
goto again;
else
ret = errno;
}
return ret;
}
/* Single printf and write: returns -errno or 0. */
int
xs_printf(struct xs_handle *h, const char *dir,
const char *node, const char *fmt, ...)
{
int ret;
va_list ap;
char *buf, *path;
va_start(ap, fmt);
ret = vasprintf(&buf, fmt, ap);
va_end(ap);
if (ret == -1)
return 0;
ret = asprintf(&path, "%s/%s", dir, node);
if (ret == -1) {
free(buf);
return 0;
}
ret = xs_write(h, XBT_NULL, path, buf, strlen(buf)+1);
free(buf);
free(path);
return ret;
}
int
xs_exists(struct xs_handle *h, const char *path)
{
char **d;
unsigned int num;
xs_transaction_t xth;
if ((xth = xs_transaction_start(h)) == XBT_NULL) {
EPRINTF("unable to start xs trasanction\n");
return 0;
}
d = xs_directory(h, xth, path, &num);
xs_transaction_end(h, xth, 0);
if (!d)
return 0;
free(d);
return 1;
}
/**
* This assumes that the domain name we are looking for is unique.
* Name parameter Domain-0
*/
char *
get_dom_domid(struct xs_handle *h)
{
int i;
xs_transaction_t xth;
unsigned int num, len;
char *val, *path, *domid, **e;
e = NULL;
domid = NULL;
if ((xth = xs_transaction_start(h)) == XBT_NULL) {
EPRINTF("unable to start xs trasanction\n");
return NULL;
}
e = xs_directory(h, xth, "/local/domain", &num);
if (e == NULL)
goto done;
for (i = 0; (i < num) && (domid == NULL); i++) {
if (asprintf(&path, "/local/domain/%s/name", e[i]) == -1)
break;
val = xs_read(h, xth, path, &len);
free(path);
if (val == NULL)
continue;
if (strcmp(val, DOMNAME) == 0) {
/* match! */
if (asprintf(&path,
"/local/domain/%s/domid", e[i]) == -1) {
free(val);
break;
}
domid = xs_read(h, xth, path, &len);
free(path);
}
free(val);
}
done:
xs_transaction_end(h, xth, 0);
free(e);
return domid;
}
/*
* a little paranoia: we don't just trust token
*/
static struct xenbus_watch *find_watch(const char *token)
{
int ret;
long nonce;
unsigned long addr;
struct xenbus_watch *i, *cmp;
ret = sscanf(token, "%lX:%lX", &addr, &nonce);
if (ret != 2) {
EPRINTF("invalid watch token %s\n", token);
return NULL;
}
cmp = (struct xenbus_watch *)addr;
list_for_each_entry(i, &watches, list)
if (i == cmp && i->nonce == nonce)
return i;
return NULL;
}
/*
* Register callback to watch this node;
* like xs_watch, return 0 on failure
*/
int register_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch)
{
/* Pointer in ascii is the token. */
char token[(sizeof(watch) + sizeof(long)) * 2 + 2];
/* 1-second granularity should suffice here */
watch->nonce = time(NULL);
sprintf(token, "%lX:%lX", (long)watch, watch->nonce);
if (find_watch(token)) {
EPRINTF("watch collision!\n");
return -EINVAL;
}
if (!xs_watch(h, watch->node, token)) {
EPRINTF("unable to set watch!\n");
return -EINVAL;
}
list_add(&watch->list, &watches);
return 0;
}
int unregister_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch)
{
char token[(sizeof(watch) + sizeof(long)) * 2 + 2];
sprintf(token, "%lX:%lX", (long)watch, watch->nonce);
if (!find_watch(token)) {
EPRINTF("no such watch!\n");
return -EINVAL;
}
if (!xs_unwatch(h, watch->node, token))
EPRINTF("XENBUS Failed to release watch %s\n", watch->node);
list_del(&watch->list);
return 0;
}
/*
* re-register callbacks to all watches
*/
void reregister_xenbus_watches(struct xs_handle *h)
{
struct xenbus_watch *watch;
char token[(sizeof(watch) + sizeof(long)) * 2 + 2];
list_for_each_entry(watch, &watches, list) {
sprintf(token, "%lX:%lX", (long)watch, watch->nonce);
xs_watch(h, watch->node, token);
}
}
/*
* based on watch_thread()
*/
int xs_fire_next_watch(struct xs_handle *h)
{
unsigned int num;
struct xenbus_watch *w;
char **res, *token, *node = NULL;
res = xs_read_watch(h, &num);
if (res == NULL)
return -EAGAIN; /* in O_NONBLOCK, read_watch returns 0... */
node = res[XS_WATCH_PATH];
token = res[XS_WATCH_TOKEN];
DPRINTF("got watch %s on %s\n", token, node);
w = find_watch(token);
if (w)
w->callback(h, w, node);
DPRINTF("handled watch %s on %s\n", token, node);
free(res);
return 1;
}
+62
View File
@@ -0,0 +1,62 @@
/*
* xs_api.h
*
* (c) 2005 Andrew Warfield and Julian Chesterfield
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef _XS_API_H_
#define _XS_API_H_
#include <xs.h>
#include "list.h"
struct xenbus_watch
{
struct list_head list;
char *node;
void *data;
long nonce;
void (*callback) (struct xs_handle *h,
struct xenbus_watch *,
const char *node);
};
int xs_gather(struct xs_handle *xs, const char *dir, ...);
int xs_printf(struct xs_handle *h, const char *dir, const char *node,
const char *fmt, ...) __attribute__((format(printf, 4, 5)));
int xs_exists(struct xs_handle *h, const char *path);
char *get_dom_domid(struct xs_handle *h);
int convert_dev_name_to_num(char *name);
int register_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch);
int unregister_xenbus_watch(struct xs_handle *h, struct xenbus_watch *watch);
void reregister_xenbus_watches(struct xs_handle *h);
int xs_fire_next_watch(struct xs_handle *h);
#endif
File diff suppressed because it is too large. Load diff
+599
View File
@@ -0,0 +1,599 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <xs.h>
#include "disktypes.h"
#include "tapdisk-dispatch.h"
#define TAPDISK_DAEMON_DOMID_WATCH "domid-watch"
#define TAPDISK_DAEMON_PIDFILE "/var/run/blktapctrl.pid"
typedef struct tapdisk_daemon {
char *node;
int blktap_fd;
uint16_t cookie;
struct xs_handle *xsh;
struct list_head channels;
struct xenbus_watch watch;
} tapdisk_daemon_t;
static tapdisk_daemon_t tapdisk_daemon;
#define tapdisk_daemon_for_each_channel(c, tmp) \
list_for_each_entry_safe(c, tmp, &tapdisk_daemon.channels, list)
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
static void
tapdisk_daemon_print_drivers(void)
{
int i, size;
DPRINTF("blktap-daemon: v1.0.2\n");
size = sizeof(dtypes) / sizeof(disk_info_t *);
for (i = 0; i < size; i++)
DPRINTF("Found driver: [%s]\n", dtypes[i]->name);
}
static int
tapdisk_daemon_write_pidfile(long pid)
{
char buf[100];
int len, fd, flags, err;
fd = open(TAPDISK_DAEMON_PIDFILE, O_RDWR | O_CREAT, 0600);
if (fd == -1) {
EPRINTF("Opening pid file failed (%d)\n", errno);
return -errno;
}
/* We exit silently if daemon already running */
err = lockf(fd, F_TLOCK, 0);
if (err == -1)
exit(0);
/* Set FD_CLOEXEC, so that tapdisk doesn't get this file descriptor */
flags = fcntl(fd, F_GETFD);
if (flags == -1) {
EPRINTF("F_GETFD failed (%d)\n", errno);
return -errno;
}
flags |= FD_CLOEXEC;
err = fcntl(fd, F_SETFD, flags);
if (err == -1) {
EPRINTF("F_SETFD failed (%d)\n", errno);
return -errno;
}
len = sprintf(buf, "%ld\n", pid);
err = write(fd, buf, len);
if (err != len) {
EPRINTF("Writing pid file failed (%d)\n", errno);
return -errno;
}
return 0;
}
static int
tapdisk_daemon_init(void)
{
char *devname;
int i, err, blktap_major;
memset(&tapdisk_daemon, 0, sizeof(tapdisk_daemon_t));
err = asprintf(&devname, "%s/%s0", BLKTAP_DEV_DIR, BLKTAP_DEV_NAME);
if (err == -1) {
devname = NULL;
err = -ENOMEM;
goto fail;
}
err = xc_find_device_number("blktap0");
if (err < 0)
goto fail;
blktap_major = major(err);
err = make_blktap_device(devname, blktap_major, 0, S_IFCHR | 0600);
if (err)
goto fail;
tapdisk_daemon.blktap_fd = open(devname, O_RDWR);
if (tapdisk_daemon.blktap_fd == -1) {
err = -errno;
EPRINTF("blktap0 open failed\n");
goto fail;
}
for (i = 0; i < 2; i++) {
tapdisk_daemon.xsh = xs_daemon_open();
if (!tapdisk_daemon.xsh) {
EPRINTF("xs_daemon_open failed -- is xenstore running?\n");
sleep(2);
} else
break;
}
if (!tapdisk_daemon.xsh) {
err = -ENOSYS;
goto fail;
}
INIT_LIST_HEAD(&tapdisk_daemon.channels);
free(devname);
return 0;
fail:
if (tapdisk_daemon.blktap_fd > 0)
close(tapdisk_daemon.blktap_fd);
free(devname);
memset(&tapdisk_daemon, 0, sizeof(tapdisk_daemon_t));
EPRINTF("%s: %d\n", __func__, err);
return err;
}
static int
tapdisk_daemon_set_node(void)
{
int err;
char *domid;
domid = get_dom_domid(tapdisk_daemon.xsh);
if (!domid)
return -EAGAIN;
err = asprintf(&tapdisk_daemon.node,
"/local/domain/%s/backend/tap", domid);
if (err == -1) {
tapdisk_daemon.node = NULL;
err = -ENOMEM;
goto out;
}
err = 0;
out:
free(domid);
return err;
}
static int
tapdisk_daemon_get_domid(void)
{
int err;
unsigned int num;
char **res, *node, *token, *domid;
res = xs_read_watch(tapdisk_daemon.xsh, &num);
if (!res)
return -EAGAIN;
err = 0;
node = res[XS_WATCH_PATH];
token = res[XS_WATCH_TOKEN];
if (strcmp(token, TAPDISK_DAEMON_DOMID_WATCH)) {
err = -EINVAL;
goto out;
}
err = tapdisk_daemon_set_node();
out:
free(res);
return err;
}
static int
tapdisk_daemon_wait_for_domid(void)
{
int err;
char *domid;
fd_set readfds;
err = tapdisk_daemon_set_node();
if (!err)
return 0;
if (!xs_watch(tapdisk_daemon.xsh, "/local/domain",
TAPDISK_DAEMON_DOMID_WATCH)) {
EPRINTF("unable to set domain id watch\n");
return -EINVAL;
}
do {
FD_ZERO(&readfds);
FD_SET(xs_fileno(tapdisk_daemon.xsh), &readfds);
select(xs_fileno(tapdisk_daemon.xsh) + 1,
&readfds, NULL, NULL, NULL);
if (FD_ISSET(xs_fileno(tapdisk_daemon.xsh), &readfds))
err = tapdisk_daemon_get_domid();
else
err = -EAGAIN;
} while (err == -EAGAIN);
xs_unwatch(tapdisk_daemon.xsh,
"/local/domain", TAPDISK_DAEMON_DOMID_WATCH);
return err;
}
static inline int
tapdisk_daemon_new_vbd_event(const char *node)
{
return (!strcmp(node, "start-tapdisk"));
}
static int
tapdisk_daemon_write_uuid(char *path, uint32_t uuid)
{
int err;
char *cpath, uuid_str[12];
snprintf(uuid_str, sizeof(uuid_str), "%u", uuid);
err = asprintf(&cpath, "%s/tapdisk-uuid", path);
if (err == -1)
return -ENOMEM;
err = xs_write(tapdisk_daemon.xsh, XBT_NULL,
cpath, uuid_str, strlen(uuid_str));
free(cpath);
return (err ? 0 : -errno);
}
static void
tapdisk_daemon_probe(struct xs_handle *xsh,
struct xenbus_watch *watch, const char *path)
{
char *cpath;
int len, err;
uint32_t cookie;
const char *node;
tapdisk_channel_t *channel;
len = strsep_len(path, '/', 7);
if (len < 0)
return;
node = path + len + 1;
if (!tapdisk_daemon_new_vbd_event(node))
return;
if (!xs_exists(xsh, path))
return;
cpath = strdup(path);
if (!cpath) {
EPRINTF("failed to allocate control path for %s\n", path);
return;
}
cpath[len] = '\0';
cookie = tapdisk_daemon.cookie++;
err = tapdisk_daemon_write_uuid(cpath, cookie);
if (err)
goto out;
DPRINTF("%s: got watch on %s, uuid = %u\n", __func__, path, cookie);
err = tapdisk_channel_open(&channel, cpath,
tapdisk_daemon.xsh,
tapdisk_daemon.blktap_fd,
cookie);
if (!err)
list_add(&channel->list, &tapdisk_daemon.channels);
else
EPRINTF("failed to open tapdisk channel for %s: %d\n",
path, err);
out:
free(cpath);
}
static int
tapdisk_daemon_start(void)
{
int err;
err = tapdisk_daemon_wait_for_domid();
if (err)
return err;
tapdisk_daemon.watch.node = tapdisk_daemon.node;
tapdisk_daemon.watch.callback = tapdisk_daemon_probe;
err = register_xenbus_watch(tapdisk_daemon.xsh, &tapdisk_daemon.watch);
if (err)
goto fail;
ioctl(tapdisk_daemon.blktap_fd,
BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_INTERPOSE);
ioctl(tapdisk_daemon.blktap_fd, BLKTAP_IOCTL_SENDPID, getpid());
return 0;
fail:
free(tapdisk_daemon.node);
tapdisk_daemon.node = NULL;
tapdisk_daemon.watch.node = NULL;
EPRINTF("%s: %d\n", __func__, err);
return err;
}
static int
tapdisk_daemon_stop(void)
{
unregister_xenbus_watch(tapdisk_daemon.xsh, &tapdisk_daemon.watch);
ioctl(tapdisk_daemon.blktap_fd,
BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_PASSTHROUGH);
close(tapdisk_daemon.blktap_fd);
return 0;
}
static void
tapdisk_daemon_free(void)
{
free(tapdisk_daemon.node);
xs_daemon_close(tapdisk_daemon.xsh);
memset(&tapdisk_daemon, 0, sizeof(tapdisk_daemon_t));
}
static int
tapdisk_daemon_read_message(int fd, tapdisk_message_t *message, int timeout)
{
fd_set readfds;
struct timeval tv;
int ret, len, offset;
tv.tv_sec = timeout;
tv.tv_usec = 0;
offset = 0;
len = sizeof(tapdisk_message_t);
memset(message, 0, sizeof(tapdisk_message_t));
while (offset < len) {
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
/* we don't bother reinitializing tv. at worst, it will wait a
* bit more time than expected. */
ret = select(fd + 1, &readfds, NULL, NULL, &tv);
if (ret == -1)
break;
else if (FD_ISSET(fd, &readfds)) {
ret = read(fd, message + offset, len - offset);
if (ret <= 0)
break;
offset += ret;
} else
break;
}
return (offset == len ? 0 : -EIO);
}
static int
tapdisk_daemon_receive_message(int fd)
{
int err;
tapdisk_message_t m;
tapdisk_channel_t *c, *tmp;
err = tapdisk_daemon_read_message(fd, &m, 2);
if (err) {
EPRINTF("failed reading message on %d: %d\n", fd, err);
return err;
}
tapdisk_daemon_for_each_channel(c, tmp)
if (c->cookie == m.cookie && c->read_fd == fd) {
DPRINTF("got '%s' message from %d:%d\n",
tapdisk_message_name(m.type),
c->channel_id, c->cookie);
return tapdisk_channel_receive_message(c, &m);
}
EPRINTF("unrecognized message on %d: '%s' (uuid = %u)\n",
fd, tapdisk_message_name(m.type), m.cookie);
return -EINVAL;
}
static int
tapdisk_daemon_set_fds(fd_set *readfds)
{
int max, fd;
tapdisk_channel_t *channel, *tmp;
max = xs_fileno(tapdisk_daemon.xsh);
FD_ZERO(readfds);
FD_SET(max, readfds);
tapdisk_daemon_for_each_channel(channel, tmp) {
fd = channel->read_fd;
max = MAX(fd, max);
FD_SET(fd, readfds);
}
return max;
}
static int
tapdisk_daemon_check_fds(fd_set *readfds)
{
int err;
tapdisk_channel_t *channel, *tmp;
if (FD_ISSET(xs_fileno(tapdisk_daemon.xsh), readfds))
xs_fire_next_watch(tapdisk_daemon.xsh);
tapdisk_daemon_for_each_channel(channel, tmp)
if (FD_ISSET(channel->read_fd, readfds))
return tapdisk_daemon_receive_message(channel->read_fd);
return 0;
}
static int
tapdisk_daemon_run(void)
{
int err, max;
fd_set readfds;
while (1) {
max = tapdisk_daemon_set_fds(&readfds);
err = select(max + 1, &readfds, NULL, NULL, NULL);
if (err < 0)
continue;
err = tapdisk_daemon_check_fds(&readfds);
}
return err;
}
void
tapdisk_daemon_find_channel(tapdisk_channel_t *channel)
{
tapdisk_channel_t *c, *tmp;
channel->read_fd = 0;
channel->write_fd = 0;
channel->tapdisk_pid = 0;
/* do we want multiple vbds per tapdisk? */
if (!xs_exists(tapdisk_daemon.xsh, channel->share_tapdisk_str)) {
channel->shared = 0;
return;
}
channel->shared = 1;
/* check if we already have a process started */
tapdisk_daemon_for_each_channel(c, tmp)
if (c->drivertype == channel->drivertype) {
channel->write_fd = c->write_fd;
channel->read_fd = c->read_fd;
channel->channel_id = c->channel_id;
channel->tapdisk_pid = c->tapdisk_pid;
return;
}
}
void
tapdisk_daemon_close_channel(tapdisk_channel_t *channel)
{
tapdisk_channel_t *c, *tmp;
list_del(&channel->list);
tapdisk_daemon_for_each_channel(c, tmp)
if (c->channel_id == channel->channel_id)
return;
close(channel->read_fd);
close(channel->write_fd);
}
int
main(int argc, char *argv[])
{
int err;
char buf[128];
if (daemon(0, 0)) {
EPRINTF("daemon() failed (%d)\n", errno);
return -errno;
}
#define CORE_DUMP
#if defined(CORE_DUMP)
#include <sys/resource.h>
{
/* set up core-dumps*/
struct rlimit rlim;
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &rlim) < 0)
EPRINTF("setrlimit failed: %d\n", errno);
}
#endif
snprintf(buf, sizeof(buf), "BLKTAP-DAEMON[%d]", getpid());
openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
err = tapdisk_daemon_write_pidfile(getpid());
if (err)
goto out;
tapdisk_daemon_print_drivers();
err = tapdisk_daemon_init();
if (err)
goto out;
err = tapdisk_daemon_start();
if (err)
goto out;
tapdisk_daemon_run();
tapdisk_daemon_stop();
tapdisk_daemon_free();
err = 0;
out:
if (err)
EPRINTF("failed to start %s: %d\n", argv[0], err);
closelog();
return err;
}
@@ -0,0 +1,94 @@
/*
* (c) 2005 Andrew Warfield and Julian Chesterfield
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "tapdisk-dispatch.h"
int
strsep_len(const char *str, char c, unsigned int len)
{
unsigned int i;
for (i = 0; str[i]; i++)
if (str[i] == c) {
if (len == 0)
return i;
len--;
}
return (len == 0) ? i : -ERANGE;
}
int
make_blktap_device(char *devname, int major, int minor, int perm)
{
int err;
err = unlink(devname);
if (err && errno != ENOENT) {
EPRINTF("unlink %s failed: %d\n", devname, errno);
return -errno;
}
/* Need to create device */
err = mkdir(BLKTAP_DEV_DIR, 0755);
if (err && errno != EEXIST) {
EPRINTF("Failed to create %s directory\n", BLKTAP_DEV_DIR);
return -errno;
}
err = mknod(devname, perm, makedev(major, minor));
if (err) {
int ret = -errno;
struct stat st;
EPRINTF("mknod %s failed: %d\n", devname, -errno);
err = lstat(devname, &st);
if (err) {
DPRINTF("lstat %s failed: %d\n", devname, -errno);
err = access(devname, F_OK);
if (err)
DPRINTF("access %s failed: %d\n", devname, -errno);
else
DPRINTF("access %s succeeded\n", devname);
} else
DPRINTF("lstat %s: %u:%u\n", devname,
(unsigned int)st.st_rdev >> 8,
(unsigned int)st.st_rdev & 0xff);
return ret;
}
DPRINTF("Created %s device\n", devname);
return 0;
}
+95
View File
@@ -0,0 +1,95 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_DISPATCH_H_
#define _TAPDISK_DISPATCH_H_
#include "xs_api.h"
#include "blktaplib.h"
#include "tapdisk-message.h"
struct tapdisk_channel {
int state;
int read_fd;
int write_fd;
int blktap_fd;
int channel_id;
char mode;
char shared;
char open;
unsigned int domid;
unsigned int busid;
unsigned int major;
unsigned int minor;
unsigned int storage;
unsigned int drivertype;
uint16_t cookie;
pid_t tapdisk_pid;
/*
* special accounting needed to handle pause
* requests received before tapdisk process is ready
*/
char connected;
char pause_needed;
char *path;
char *frontpath;
char *params;
char *vdi_path;
char *uuid_str;
char *pause_str;
char *pause_done_str;
char *shutdown_str;
char *share_tapdisk_str;
image_t image;
struct list_head list;
struct xenbus_watch pause_watch;
struct xenbus_watch shutdown_watch;
struct xs_handle *xsh;
};
typedef struct tapdisk_channel tapdisk_channel_t;
int strsep_len(const char *str, char c, unsigned int len);
int make_blktap_device(char *devname, int major, int minor, int perm);
int tapdisk_channel_open(tapdisk_channel_t **,
char *node, struct xs_handle *,
int blktap_fd, uint16_t cookie);
void tapdisk_channel_close(tapdisk_channel_t *);
void tapdisk_daemon_find_channel(tapdisk_channel_t *);
void tapdisk_daemon_close_channel(tapdisk_channel_t *);
int tapdisk_channel_receive_message(tapdisk_channel_t *, tapdisk_message_t *);
#endif
+105
View File
@@ -0,0 +1,105 @@
XEN_ROOT=../../../
BLKTAP_ROOT= ..
include $(XEN_ROOT)/tools/Rules.mk
LIBVHDDIR = $(BLKTAP_ROOT)/vhd/lib
IBIN = tapdisk tapdisk2 td-util tapdisk-client tapdisk-stream tapdisk-diff
QCOW_UTIL = img2qcow qcow-create qcow2raw
LOCK_UTIL = lock-util
INST_DIR = $(SBINDIR)
CFLAGS += -Werror -g -O0
CFLAGS += -Wno-unused
CFLAGS += -fno-strict-aliasing
CFLAGS += -I../lib -I../../libxc
CFLAGS += -I../include -I../../include
CFLAGS += -I $(LIBAIO_DIR)
CFLAGS += -D_GNU_SOURCE
CFLAGS += -DUSE_NFS_LOCKS
ifeq ($(CONFIG_X86_64),y)
CFLAGS += -fPIC
endif
LIBS += -lrt -lz
ifeq ($(shell . ./check_gcrypt $(CC)),yes)
CFLAGS += -DUSE_GCRYPT
CRYPT_LIB += -lgcrypt
else
CRYPT_LIB += -lcrypto
$(warning === libgcrypt not installed: falling back to libcrypto ===)
endif
LDFLAGS_img := $(CRYPT_LIB) -lpthread -lz
tapdisk tapdisk2 td-util tapdisk-stream tapdisk-diff $(QCOW_UTIL): LIBS += -L$(LIBVHDDIR) -lvhd -luuid
LIBAIO_DIR = $(XEN_ROOT)/tools/libaio/src
tapdisk tapdisk2 tapdisk-stream tapdisk-diff $(QCOW_UTIL): AIOLIBS := $(LIBAIO_DIR)/libaio.a
tapdisk tapdisk-client tapdisk-stream tapdisk-diff $(QCOW_UTIL): CFLAGS += -I$(LIBAIO_DIR) -I$(XEN_LIBXC)
ifeq ($(VHD_STATIC),y)
td-util: CFLAGS += -static
endif
TAP-OBJS-y := scheduler.o
TAP-OBJS-y += tapdisk-ipc.o
TAP-OBJS-y += tapdisk-vbd.o
TAP-OBJS-y += tapdisk-image.o
TAP-OBJS-y += tapdisk-driver.o
TAP-OBJS-y += tapdisk-interface.o
TAP-OBJS-y += tapdisk-server.o
TAP-OBJS-y += tapdisk-queue.o
TAP-OBJS-y += tapdisk-filter.o
TAP-OBJS-y += tapdisk-log.o
TAP-OBJS-y += tapdisk-utils.o
TAP-OBJS-y += io-optimize.o
TAP-OBJS-y += lock.o
TAP-OBJS-$(CONFIG_Linux) += blk_linux.o
MISC-OBJS-y := atomicio.o
BLK-OBJS-y := block-aio.o
BLK-OBJS-y += block-ram.o
BLK-OBJS-y += block-cache.o
BLK-OBJS-y += block-vhd.o
BLK-OBJS-y += block-log.o
BLK-OBJS-y += block-qcow.o
BLK-OBJS-y += aes.o
all: $(IBIN) lock-util qcow-util
tapdisk: $(TAP-OBJS-y) $(BLK-OBJS-y) $(MISC-OBJS-y) tapdisk.c
$(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(AIOLIBS) $(LDFLAGS_img)
tapdisk2: $(TAP-OBJS-y) $(BLK-OBJS-y) $(MISC-OBJS-y) tapdisk2.c
$(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(AIOLIBS) $(LDFLAGS_img)
tapdisk-client: tapdisk-client.o
$(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(LDFLAGS_img)
tapdisk-stream tapdisk-diff: %: %.o $(TAP-OBJS-y) $(BLK-OBJS-y)
$(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(AIOLIBS) $(LDFLAGS_img)
td-util: td.o tapdisk-utils.o tapdisk-log.o
$(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(LDFLAGS_img)
lock-util: lock.c
$(CC) $(CFLAGS) -DUTIL -o lock-util lock.c $(LIBS)
.PHONY: qcow-util
qcow-util: img2qcow qcow2raw qcow-create
img2qcow qcow2raw qcow-create: %: %.o $(TAP-OBJS-y) $(BLK-OBJS-y)
$(CC) $(CFLAGS) -o $@ $^ $(LIBS) $(AIOLIBS) $(LDFLAGS_img)
install: all
$(INSTALL_DIR) -p $(DESTDIR)$(INST_DIR)
$(INSTALL_PROG) $(IBIN) $(LOCK_UTIL) $(QCOW_UTIL) $(DESTDIR)$(INST_DIR)
clean:
rm -rf *.o *~ xen TAGS $(IBIN) $(LIB) $(LOCK_UTIL) $(QCOW_UTIL)
.PHONY: clean install
File diff suppressed because it is too large. Load diff
+28
View File
@@ -0,0 +1,28 @@
#ifndef QEMU_AES_H
#define QEMU_AES_H
#include <stdint.h>
#define AES_MAXNR 14
#define AES_BLOCK_SIZE 16
struct aes_key_st {
uint32_t rd_key[4 *(AES_MAXNR + 1)];
int rounds;
};
typedef struct aes_key_st AES_KEY;
int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
AES_KEY *key);
int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
AES_KEY *key);
void AES_encrypt(const unsigned char *in, unsigned char *out,
const AES_KEY *key);
void AES_decrypt(const unsigned char *in, unsigned char *out,
const AES_KEY *key);
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
const unsigned long length, const AES_KEY *key,
unsigned char *ivec, const int enc);
#endif
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
* Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <errno.h>
#include "atomicio.h"
/*
* ensure all of data on socket comes through. f==read || f==vwrite
*/
size_t
atomicio(f, fd, _s, n)
ssize_t (*f) (int, void *, size_t);
int fd;
void *_s;
size_t n;
{
char *s = _s;
size_t pos = 0;
ssize_t res;
while (n > pos) {
res = (f) (fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
return 0;
case 0:
errno = EPIPE;
return pos;
default:
pos += (size_t)res;
}
}
return (pos);
}
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
int blk_getimagesize(int fd, uint64_t *size);
int blk_getsectorsize(int fd, uint64_t *sector_size);
+43
View File
@@ -0,0 +1,43 @@
#include <inttypes.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include "tapdisk.h"
#include "blk.h"
int blk_getimagesize(int fd, uint64_t *size)
{
int rc;
*size = 0;
rc = ioctl(fd, BLKGETSIZE, size);
if (rc) {
DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
return -EINVAL;
}
return 0;
}
int blk_getsectorsize(int fd, uint64_t *sector_size)
{
#if defined(BLKSSZGET)
int rc;
*sector_size = DEFAULT_SECTOR_SIZE;
rc = ioctl(fd, BLKSSZGET, sector_size);
if (rc) {
DPRINTF("ERR: BLKSSZGET failed. Falling back to use default sector size");
*sector_size = DEFAULT_SECTOR_SIZE;
}
if (*sector_size != DEFAULT_SECTOR_SIZE)
DPRINTF("Note: sector size is %"PRIu64" (not %u)\n",
*sector_size, DEFAULT_SECTOR_SIZE);
#else
*sector_size = DEFAULT_SECTOR_SIZE;
#endif
return 0;
}
+66
View File
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _BLKTAP_2_H_
#define _BLKTAP_2_H_
#define MISC_MAJOR_NUMBER 10
#define BLKTAP2_MAX_MESSAGE_LEN 256
#define BLKTAP2_RING_MESSAGE_PAUSE 1
#define BLKTAP2_RING_MESSAGE_RESUME 2
#define BLKTAP2_RING_MESSAGE_CLOSE 3
#define BLKTAP2_IOCTL_KICK_FE 1
#define BLKTAP2_IOCTL_ALLOC_TAP 200
#define BLKTAP2_IOCTL_FREE_TAP 201
#define BLKTAP2_IOCTL_CREATE_DEVICE 202
#define BLKTAP2_IOCTL_SET_PARAMS 203
#define BLKTAP2_IOCTL_PAUSE 204
#define BLKTAP2_IOCTL_REOPEN 205
#define BLKTAP2_IOCTL_RESUME 206
#define BLKTAP2_CONTROL_NAME "blktap-control"
#define BLKTAP2_DIRECTORY "/dev/xen/blktap-2"
#define BLKTAP2_CONTROL_DEVICE BLKTAP2_DIRECTORY"/control"
#define BLKTAP2_RING_DEVICE BLKTAP2_DIRECTORY"/blktap"
#define BLKTAP2_IO_DEVICE BLKTAP2_DIRECTORY"/tapdev"
struct blktap2_handle {
unsigned int ring;
unsigned int device;
unsigned int minor;
};
struct blktap2_params {
char name[BLKTAP2_MAX_MESSAGE_LEN];
unsigned long long capacity;
unsigned long sector_size;
};
#endif
+272
View File
@@ -0,0 +1,272 @@
/*
* Copyright (c) 2007, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <libaio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include "tapdisk.h"
#include "tapdisk-driver.h"
#include "tapdisk-interface.h"
#define MAX_AIO_REQS TAPDISK_DATA_REQUESTS
struct tdaio_state;
struct aio_request {
td_request_t treq;
struct tiocb tiocb;
struct tdaio_state *state;
};
struct tdaio_state {
int fd;
td_driver_t *driver;
int aio_free_count;
struct aio_request aio_requests[MAX_AIO_REQS];
struct aio_request *aio_free_list[MAX_AIO_REQS];
};
/*Get Image size, secsize*/
static int tdaio_get_image_info(int fd, td_disk_info_t *info)
{
int ret;
long size;
unsigned long total_size;
struct statvfs statBuf;
struct stat stat;
ret = fstat(fd, &stat);
if (ret != 0) {
DPRINTF("ERROR: fstat failed, Couldn't stat image");
return -EINVAL;
}
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
info->size = 0;
if (ioctl(fd,BLKGETSIZE,&info->size)!=0) {
DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
return -EINVAL;
}
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)(info->size << SECTOR_SHIFT),
(long long unsigned)info->size);
/*Get the sector size*/
#if defined(BLKSSZGET)
{
int arg;
info->sector_size = DEFAULT_SECTOR_SIZE;
ioctl(fd, BLKSSZGET, &info->sector_size);
if (info->sector_size != DEFAULT_SECTOR_SIZE)
DPRINTF("Note: sector size is %ld (not %d)\n",
info->sector_size, DEFAULT_SECTOR_SIZE);
}
#else
info->sector_size = DEFAULT_SECTOR_SIZE;
#endif
} else {
/*Local file? try fstat instead*/
info->size = (stat.st_size >> SECTOR_SHIFT);
info->sector_size = DEFAULT_SECTOR_SIZE;
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)(info->size << SECTOR_SHIFT),
(long long unsigned)info->size);
}
if (info->size == 0) {
info->size =((uint64_t) 16836057);
info->sector_size = DEFAULT_SECTOR_SIZE;
}
info->info = 0;
return 0;
}
/* Open the disk file and initialize aio state. */
int tdaio_open(td_driver_t *driver, const char *name, td_flag_t flags)
{
int i, fd, ret, o_flags;
struct tdaio_state *prv;
ret = 0;
prv = (struct tdaio_state *)driver->data;
DPRINTF("block-aio open('%s')", name);
memset(prv, 0, sizeof(struct tdaio_state));
prv->aio_free_count = MAX_AIO_REQS;
for (i = 0; i < MAX_AIO_REQS; i++)
prv->aio_free_list[i] = &prv->aio_requests[i];
/* Open the file */
o_flags = O_DIRECT | O_LARGEFILE |
((flags & TD_OPEN_RDONLY) ? O_RDONLY : O_RDWR);
fd = open(name, o_flags);
if ( (fd == -1) && (errno == EINVAL) ) {
/* Maybe O_DIRECT isn't supported. */
o_flags &= ~O_DIRECT;
fd = open(name, o_flags);
if (fd != -1) DPRINTF("WARNING: Accessing image without"
"O_DIRECT! (%s)\n", name);
} else if (fd != -1) DPRINTF("open(%s) with O_DIRECT\n", name);
if (fd == -1) {
DPRINTF("Unable to open [%s] (%d)!\n", name, 0 - errno);
ret = 0 - errno;
goto done;
}
ret = tdaio_get_image_info(fd, &driver->info);
if (ret) {
close(fd);
goto done;
}
prv->fd = fd;
done:
return ret;
}
void tdaio_complete(void *arg, struct tiocb *tiocb, int err)
{
struct aio_request *aio = (struct aio_request *)arg;
struct tdaio_state *prv = aio->state;
td_complete_request(aio->treq, err);
prv->aio_free_list[prv->aio_free_count++] = aio;
}
void tdaio_queue_read(td_driver_t *driver, td_request_t treq)
{
int size;
uint64_t offset;
struct aio_request *aio;
struct tdaio_state *prv;
prv = (struct tdaio_state *)driver->data;
size = treq.secs * driver->info.sector_size;
offset = treq.sec * (uint64_t)driver->info.sector_size;
if (prv->aio_free_count == 0)
goto fail;
aio = prv->aio_free_list[--prv->aio_free_count];
aio->treq = treq;
aio->state = prv;
td_prep_read(&aio->tiocb, prv->fd, treq.buf,
size, offset, tdaio_complete, aio);
td_queue_tiocb(driver, &aio->tiocb);
return;
fail:
td_complete_request(treq, -EBUSY);
}
void tdaio_queue_write(td_driver_t *driver, td_request_t treq)
{
int size;
uint64_t offset;
struct aio_request *aio;
struct tdaio_state *prv;
prv = (struct tdaio_state *)driver->data;
size = treq.secs * driver->info.sector_size;
offset = treq.sec * (uint64_t)driver->info.sector_size;
if (prv->aio_free_count == 0)
goto fail;
aio = prv->aio_free_list[--prv->aio_free_count];
aio->treq = treq;
aio->state = prv;
td_prep_write(&aio->tiocb, prv->fd, treq.buf,
size, offset, tdaio_complete, aio);
td_queue_tiocb(driver, &aio->tiocb);
return;
fail:
td_complete_request(treq, -EBUSY);
}
int tdaio_close(td_driver_t *driver)
{
struct tdaio_state *prv = (struct tdaio_state *)driver->data;
close(prv->fd);
return 0;
}
int tdaio_get_parent_id(td_driver_t *driver, td_disk_id_t *id)
{
return TD_NO_PARENT;
}
int tdaio_validate_parent(td_driver_t *driver,
td_driver_t *pdriver, td_flag_t flags)
{
return -EINVAL;
}
struct tap_disk tapdisk_aio = {
.disk_type = "tapdisk_aio",
.flags = 0,
.private_data_size = sizeof(struct tdaio_state),
.td_open = tdaio_open,
.td_close = tdaio_close,
.td_queue_read = tdaio_queue_read,
.td_queue_write = tdaio_queue_write,
.td_get_parent_id = tdaio_get_parent_id,
.td_validate_parent = tdaio_validate_parent,
.td_debug = NULL,
};
+787
View File
@@ -0,0 +1,787 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include "tapdisk.h"
#include "tapdisk-utils.h"
#include "tapdisk-driver.h"
#include "tapdisk-server.h"
#include "tapdisk-interface.h"
#ifdef DEBUG
#define DBG(_f, _a...) tlog_write(TLOG_DBG, _f, ##_a)
#else
#define DBG(_f, _a...) ((void)0)
#endif
#define WARN(_f, _a...) tlog_write(TLOG_WARN, _f, ##_a)
#define RADIX_TREE_PAGE_SHIFT 12 /* 4K pages */
#define RADIX_TREE_PAGE_SIZE (1 << RADIX_TREE_PAGE_SHIFT)
#define RADIX_TREE_NODE_SHIFT 9 /* 512B nodes */
#define RADIX_TREE_NODE_SIZE (1 << RADIX_TREE_NODE_SHIFT)
#define RADIX_TREE_NODE_MASK (RADIX_TREE_NODE_SIZE - 1)
#define BLOCK_CACHE_NODES_PER_PAGE (1 << (RADIX_TREE_PAGE_SHIFT - RADIX_TREE_NODE_SHIFT))
#define BLOCK_CACHE_MAX_SIZE (10 << 20) /* 100MB cache */
#define BLOCK_CACHE_REQUESTS (TAPDISK_DATA_REQUESTS << 3)
#define BLOCK_CACHE_PAGE_IDLETIME 60
typedef struct radix_tree radix_tree_t;
typedef struct radix_tree_node radix_tree_node_t;
typedef struct radix_tree_link radix_tree_link_t;
typedef struct radix_tree_leaf radix_tree_leaf_t;
typedef struct radix_tree_page radix_tree_page_t;
typedef struct block_cache block_cache_t;
typedef struct block_cache_request block_cache_request_t;
typedef struct block_cache_stats block_cache_stats_t;
struct radix_tree_page {
char *buf;
size_t size;
uint64_t sec;
radix_tree_link_t *owners[BLOCK_CACHE_NODES_PER_PAGE];
};
struct radix_tree_leaf {
radix_tree_page_t *page;
char *buf;
};
struct radix_tree_link {
uint32_t time;
union {
radix_tree_node_t *next;
radix_tree_leaf_t leaf;
} u;
};
struct radix_tree_node {
int height;
radix_tree_link_t links[RADIX_TREE_NODE_SIZE];
};
struct radix_tree {
int height;
uint64_t size;
uint32_t nodes;
radix_tree_node_t *root;
block_cache_t *cache;
};
struct block_cache_request {
int err;
char *buf;
uint64_t secs;
td_request_t treq;
block_cache_t *cache;
};
struct block_cache_stats {
uint64_t reads;
uint64_t hits;
uint64_t misses;
uint64_t prunes;
};
struct block_cache {
int ptype;
char *name;
uint64_t sectors;
block_cache_request_t requests[BLOCK_CACHE_REQUESTS];
block_cache_request_t *request_free_list[BLOCK_CACHE_REQUESTS];
int requests_free;
event_id_t timeout_id;
radix_tree_t tree;
block_cache_stats_t stats;
};
static inline uint64_t
radix_tree_calculate_size(int height)
{
return (uint64_t)RADIX_TREE_NODE_SIZE <<
(height * RADIX_TREE_NODE_SHIFT);
}
static inline int
radix_tree_calculate_height(uint64_t sectors)
{
int height;
uint64_t tree_size;
height = 1; /* always allocate root node */
tree_size = radix_tree_calculate_size(height);
while (sectors > tree_size)
tree_size = radix_tree_calculate_size(++height);
return height;
}
static inline int
radix_tree_index(radix_tree_node_t *node, uint64_t sector)
{
return ((sector >> (node->height * RADIX_TREE_NODE_SHIFT)) &
RADIX_TREE_NODE_MASK);
}
static inline int
radix_tree_node_contains_leaves(radix_tree_t *tree, radix_tree_node_t *node)
{
return (node->height == 0);
}
static inline int
radix_tree_node_is_root(radix_tree_t *tree, radix_tree_node_t *node)
{
return (node->height == tree->height);
}
static inline uint64_t
radix_tree_size(radix_tree_t *tree)
{
return tree->size + tree->nodes * sizeof(radix_tree_node_t);
}
static inline void
radix_tree_clear_link(radix_tree_link_t *link)
{
if (link)
memset(link, 0, sizeof(radix_tree_link_t));
}
static inline radix_tree_node_t *
radix_tree_allocate_node(radix_tree_t *tree, int height)
{
radix_tree_node_t *node;
node = calloc(1, sizeof(radix_tree_node_t));
if (!node)
return NULL;
node->height = height;
tree->nodes++;
return node;
}
static inline radix_tree_node_t *
radix_tree_allocate_child_node(radix_tree_t *tree, radix_tree_node_t *parent)
{
return radix_tree_allocate_node(tree, parent->height - 1);
}
void
radix_tree_free_node(radix_tree_t *tree, radix_tree_node_t *node)
{
if (!node)
return;
free(node);
tree->nodes--;
}
static inline radix_tree_page_t *
radix_tree_allocate_page(radix_tree_t *tree,
char *buf, uint64_t sec, size_t size)
{
radix_tree_page_t *page;
page = calloc(1, sizeof(radix_tree_page_t));
if (!page)
return NULL;
page->buf = buf;
page->sec = sec;
page->size = size;
tree->size += size;
return page;
}
static inline void
radix_tree_free_page(radix_tree_t *tree, radix_tree_page_t *page)
{
int i;
for (i = 0; i < page->size >> RADIX_TREE_NODE_SHIFT; i++)
DBG("%s: ejecting sector 0x%llx\n",
tree->cache->name, page->sec + i);
tree->cache->stats.prunes += (page->size >> RADIX_TREE_NODE_SHIFT);
tree->size -= page->size;
free(page->buf);
free(page);
}
/*
* remove a leaf and the shared radix_tree_page_t containing its buffer.
* leaves are deleted, nodes are not; gc will reap the nodes later.
*/
static void
radix_tree_remove_page(radix_tree_t *tree, radix_tree_page_t *page)
{
int i;
if (!page)
return;
for (i = 0; i < BLOCK_CACHE_NODES_PER_PAGE; i++)
radix_tree_clear_link(page->owners[i]);
radix_tree_free_page(tree, page);
}
static void
radix_tree_insert_leaf(radix_tree_t *tree, radix_tree_link_t *link,
radix_tree_page_t *page, off_t off)
{
int i;
if (off + RADIX_TREE_NODE_SIZE > page->size)
return;
for (i = 0; i < BLOCK_CACHE_NODES_PER_PAGE; i++) {
if (page->owners[i])
continue;
page->owners[i] = link;
link->u.leaf.page = page;
link->u.leaf.buf = page->buf + off;
break;
}
}
static char *
radix_tree_find_leaf(radix_tree_t *tree, uint64_t sector)
{
int idx;
struct timeval now;
radix_tree_link_t *link;
radix_tree_node_t *node;
node = tree->root;
gettimeofday(&now, NULL);
do {
idx = radix_tree_index(node, sector);
link = node->links + idx;
link->time = now.tv_sec;
if (radix_tree_node_contains_leaves(tree, node))
return link->u.leaf.buf;
if (!link->u.next)
return NULL;
node = link->u.next;
} while (1);
}
static char *
radix_tree_add_leaf(radix_tree_t *tree, uint64_t sector,
radix_tree_page_t *page, off_t off)
{
int idx;
struct timeval now;
radix_tree_link_t *link;
radix_tree_node_t *node;
node = tree->root;
gettimeofday(&now, NULL);
do {
idx = radix_tree_index(node, sector);
link = node->links + idx;
link->time = now.tv_sec;
if (radix_tree_node_contains_leaves(tree, node)) {
radix_tree_remove_page(tree, link->u.leaf.page);
radix_tree_insert_leaf(tree, link, page, off);
return link->u.leaf.buf;
}
if (!link->u.next) {
link->u.next = radix_tree_allocate_child_node(tree,
node);
if (!link->u.next)
return NULL;
}
node = link->u.next;
} while (1);
}
static int
radix_tree_add_leaves(radix_tree_t *tree, char *buf,
uint64_t sector, uint64_t sectors)
{
int i;
radix_tree_page_t *page;
page = radix_tree_allocate_page(tree, buf, sector,
sectors << RADIX_TREE_NODE_SHIFT);
if (!page)
return -ENOMEM;
for (i = 0; i < sectors; i++)
if (!radix_tree_add_leaf(tree, sector + i,
page, (i << RADIX_TREE_NODE_SHIFT)))
goto fail;
return 0;
fail:
page->buf = NULL;
radix_tree_remove_page(tree, page);
return -ENOMEM;
}
static void
radix_tree_delete_branch(radix_tree_t *tree, radix_tree_node_t *node)
{
int i;
radix_tree_link_t *link;
if (!node)
return;
for (i = 0; i < RADIX_TREE_NODE_SIZE; i++) {
link = node->links + i;
if (radix_tree_node_contains_leaves(tree, node))
radix_tree_remove_page(tree, link->u.leaf.page);
else
radix_tree_delete_branch(tree, link->u.next);
radix_tree_clear_link(link);
}
radix_tree_free_node(tree, node);
}
static inline void
radix_tree_destroy(radix_tree_t *tree)
{
radix_tree_delete_branch(tree, tree->root);
tree->root = NULL;
}
/*
* returns 1 if @node is empty after pruning, 0 otherwise
*/
static int
radix_tree_prune_branch(radix_tree_t *tree,
radix_tree_node_t *node, uint32_t now)
{
int i, empty;
radix_tree_link_t *link;
empty = 1;
if (!node)
return empty;
for (i = 0; i < RADIX_TREE_NODE_SIZE; i++) {
link = node->links + i;
if (now - link->time < BLOCK_CACHE_PAGE_IDLETIME) {
if (radix_tree_node_contains_leaves(tree, node)) {
empty = 0;
continue;
}
if (radix_tree_prune_branch(tree, link->u.next, now))
radix_tree_clear_link(link);
else
empty = 0;
continue;
}
if (radix_tree_node_contains_leaves(tree, node))
radix_tree_remove_page(tree, link->u.leaf.page);
else
radix_tree_delete_branch(tree, link->u.next);
radix_tree_clear_link(link);
}
if (empty && !radix_tree_node_is_root(tree, node))
radix_tree_free_node(tree, node);
return empty;
}
/*
* walk tree and free any node that has been idle for too long
*/
static void
radix_tree_prune(radix_tree_t *tree)
{
struct timeval now;
if (!tree->root)
return;
DPRINTF("tree %s has %"PRIu64" bytes\n",
tree->cache->name, tree->size);
gettimeofday(&now, NULL);
radix_tree_prune_branch(tree, tree->root, now.tv_sec);
DPRINTF("tree %s now has %"PRIu64" bytes\n",
tree->cache->name, tree->size);
}
static inline int
radix_tree_initialize(radix_tree_t *tree, uint64_t sectors)
{
tree->height = radix_tree_calculate_height(sectors);
tree->root = radix_tree_allocate_node(tree, tree->height);
if (!tree->root)
return -ENOMEM;
return 0;
}
static inline void
radix_tree_free(radix_tree_t *tree)
{
radix_tree_destroy(tree);
}
static void
block_cache_prune_event(event_id_t id, char mode, void *private)
{
radix_tree_t *tree;
block_cache_t *cache;
cache = (block_cache_t *)private;
tree = &cache->tree;
radix_tree_prune(tree);
}
static inline block_cache_request_t *
block_cache_get_request(block_cache_t *cache)
{
if (!cache->requests_free)
return NULL;
return cache->request_free_list[--cache->requests_free];
}
static inline void
block_cache_put_request(block_cache_t *cache, block_cache_request_t *breq)
{
memset(breq, 0, sizeof(block_cache_request_t));
cache->request_free_list[cache->requests_free++] = breq;
}
static int
block_cache_open(td_driver_t *driver, const char *name, td_flag_t flags)
{
int i, err;
radix_tree_t *tree;
block_cache_t *cache;
if (!td_flag_test(flags, TD_OPEN_RDONLY))
return -EINVAL;
if (driver->info.sector_size != RADIX_TREE_NODE_SIZE)
return -EINVAL;
cache = (block_cache_t *)driver->data;
err = tapdisk_namedup(&cache->name, (char *)name);
if (err)
return -ENOMEM;
cache->sectors = driver->info.size;
tree = &cache->tree;
err = radix_tree_initialize(tree, cache->sectors);
if (err)
goto fail;
tree->cache = cache;
cache->requests_free = BLOCK_CACHE_REQUESTS;
for (i = 0; i < BLOCK_CACHE_REQUESTS; i++)
cache->request_free_list[i] = cache->requests + i;
cache->timeout_id = tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT,
-1, /* dummy fd */
BLOCK_CACHE_PAGE_IDLETIME << 1,
block_cache_prune_event,
cache);
if (cache->timeout_id < 0)
goto fail;
DPRINTF("opening cache for %s, sectors: %"PRIu64", "
"tree: %p, height: %d\n",
cache->name, cache->sectors, tree, tree->height);
if (mlockall(MCL_CURRENT | MCL_FUTURE))
DPRINTF("mlockall failed: %d\n", -errno);
return 0;
fail:
free(cache->name);
radix_tree_free(&cache->tree);
return err;
}
static int
block_cache_close(td_driver_t *driver)
{
radix_tree_t *tree;
block_cache_t *cache;
cache = (block_cache_t *)driver->data;
tree = &cache->tree;
DPRINTF("closing cache for %s\n", cache->name);
tapdisk_server_unregister_event(cache->timeout_id);
radix_tree_free(tree);
free(cache->name);
return 0;
}
static inline uint64_t
block_cache_hash(block_cache_t *cache, char *buf)
{
int i, n;
uint64_t cksm, *data;
return 0;
cksm = 0;
data = (uint64_t *)buf;
n = RADIX_TREE_NODE_SIZE / sizeof(uint64_t);
for (i = 0; i < n; i++)
cksm += data[i];
return ~cksm;
}
static void
block_cache_hit(block_cache_t *cache, td_request_t treq, char *iov[])
{
int i;
off_t off;
cache->stats.hits += treq.secs;
for (i = 0; i < treq.secs; i++) {
DBG("%s: block cache hit: sec 0x%08llx, hash: 0x%08llx\n",
cache->name, treq.sec + i, block_cache_hash(cache, iov[i]));
off = i << RADIX_TREE_NODE_SHIFT;
memcpy(treq.buf + off, iov[i], RADIX_TREE_NODE_SIZE);
}
td_complete_request(treq, 0);
}
static void
block_cache_populate_cache(td_request_t clone, int err)
{
int i;
radix_tree_t *tree;
block_cache_t *cache;
block_cache_request_t *breq;
breq = (block_cache_request_t *)clone.cb_data;
cache = breq->cache;
tree = &cache->tree;
breq->secs -= clone.secs;
breq->err = (breq->err ? breq->err : err);
if (breq->secs)
return;
if (breq->err) {
free(breq->buf);
goto out;
}
for (i = 0; i < breq->treq.secs; i++) {
off_t off = i << RADIX_TREE_NODE_SHIFT;
DBG("%s: populating sec 0x%08llx\n",
cache->name, breq->treq.sec + i);
memcpy(breq->treq.buf + off,
breq->buf + off, RADIX_TREE_NODE_SIZE);
}
if (radix_tree_add_leaves(tree, breq->buf,
breq->treq.sec, breq->treq.secs))
free(breq->buf);
out:
td_complete_request(breq->treq, breq->err);
block_cache_put_request(cache, breq);
}
static void
block_cache_miss(block_cache_t *cache, td_request_t treq)
{
char *buf;
size_t size;
td_request_t clone;
radix_tree_t *tree;
block_cache_request_t *breq;
DBG("%s: block cache miss: sec 0x%08llx\n", cache->name, treq.sec);
clone = treq;
tree = &cache->tree;
size = treq.secs << RADIX_TREE_NODE_SHIFT;
cache->stats.misses += treq.secs;
if (radix_tree_size(tree) + size >= BLOCK_CACHE_MAX_SIZE)
goto out;
breq = block_cache_get_request(cache);
if (!breq)
goto out;
if (posix_memalign((void **)&buf, RADIX_TREE_NODE_SIZE, size)) {
block_cache_put_request(cache, breq);
goto out;
}
breq->treq = treq;
breq->secs = treq.secs;
breq->err = 0;
breq->buf = buf;
breq->cache = cache;
clone.buf = buf;
clone.cb = block_cache_populate_cache;
clone.cb_data = breq;
out:
td_forward_request(clone);
}
static void
block_cache_queue_read(td_driver_t *driver, td_request_t treq)
{
int i;
radix_tree_t *tree;
block_cache_t *cache;
char *iov[BLOCK_CACHE_NODES_PER_PAGE];
cache = (block_cache_t *)driver->data;
tree = &cache->tree;
cache->stats.reads += treq.secs;
if (treq.secs > BLOCK_CACHE_NODES_PER_PAGE)
return td_forward_request(treq);
for (i = 0; i < treq.secs; i++) {
iov[i] = radix_tree_find_leaf(tree, treq.sec + i);
if (!iov[i])
return block_cache_miss(cache, treq);
}
return block_cache_hit(cache, treq, iov);
}
static void
block_cache_queue_write(td_driver_t *driver, td_request_t treq)
{
td_complete_request(treq, -EPERM);
}
static int
block_cache_get_parent_id(td_driver_t *driver, td_disk_id_t *id)
{
return -EINVAL;
}
static int
block_cache_validate_parent(td_driver_t *driver,
td_driver_t *pdriver, td_flag_t flags)
{
block_cache_t *cache;
if (!td_flag_test(pdriver->state, TD_DRIVER_RDONLY))
return -EINVAL;
cache = (block_cache_t *)driver->data;
if (strcmp(driver->name, pdriver->name))
return -EINVAL;
return 0;
}
static void
block_cache_debug(td_driver_t *driver)
{
block_cache_t *cache;
block_cache_stats_t *stats;
cache = (block_cache_t *)driver->data;
stats = &cache->stats;
WARN("BLOCK CACHE %s\n", cache->name);
WARN("reads: %"PRIu64", hits: %"PRIu64", misses: %"PRIu64", prunes: %"PRIu64"\n",
stats->reads, stats->hits, stats->misses, stats->prunes);
}
struct tap_disk tapdisk_block_cache = {
.disk_type = "tapdisk_block_cache",
.flags = 0,
.private_data_size = sizeof(block_cache_t),
.td_open = block_cache_open,
.td_close = block_cache_close,
.td_queue_read = block_cache_queue_read,
.td_queue_write = block_cache_queue_write,
.td_get_parent_id = block_cache_get_parent_id,
.td_validate_parent = block_cache_validate_parent,
.td_debug = block_cache_debug,
};
+688
View File
@@ -0,0 +1,688 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Driver to sit on top of another disk and log writes, in order
* to synchronize two distinct disks
*
* On receipt of a control request it can export a list of dirty
* sectors in the following format:
* struct writerange {
* u64 sector;
* u32 count;
* }
* terminated by { 0, 0 }
*/
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "log.h"
#include "tapdisk.h"
#include "tapdisk-server.h"
#include "tapdisk-driver.h"
#include "tapdisk-interface.h"
#define MAX_CONNECTIONS 1
typedef struct poll_fd {
int fd;
event_id_t id;
} poll_fd_t;
struct tdlog_state {
uint64_t size;
void* writelog;
char* ctlpath;
poll_fd_t ctl;
int connected;
poll_fd_t connections[MAX_CONNECTIONS];
char* shmpath;
void* shm;
log_sring_t* sring;
log_back_ring_t bring;
};
#define BDPRINTF(_f, _a...) syslog (LOG_DEBUG, "log: " _f "\n", ## _a)
#define BWPRINTF(_f, _a...) syslog (LOG_WARNING, "log: " _f "\n", ## _a)
static void ctl_accept(event_id_t, char, void *);
static void ctl_request(event_id_t, char, void *);
/* -- write log -- */
/* large flat bitmaps don't scale particularly well either in size or scan
* time, but they'll do for now */
#define BITS_PER_LONG (sizeof(unsigned long) * 8)
#define BITS_TO_LONGS(bits) (((bits)+BITS_PER_LONG-1)/BITS_PER_LONG)
#define BITMAP_ENTRY(_nr, _bmap) ((unsigned long*)(_bmap))[(_nr)/BITS_PER_LONG]
#define BITMAP_SHIFT(_nr) ((_nr) % BITS_PER_LONG)
static inline int test_bit(int nr, void* bmap)
{
return (BITMAP_ENTRY(nr, bmap) >> BITMAP_SHIFT(nr)) & 1;
}
static inline void clear_bit(int nr, void* bmap)
{
BITMAP_ENTRY(nr, bmap) &= ~(1UL << BITMAP_SHIFT(nr));
}
static inline void set_bit(int nr, void* bmap)
{
BITMAP_ENTRY(nr, bmap) |= (1UL << BITMAP_SHIFT(nr));
}
static inline int bitmap_size(uint64_t sz)
{
return sz >> 3;
}
static int writelog_create(struct tdlog_state *s)
{
uint64_t bmsize;
bmsize = bitmap_size(s->size);
BDPRINTF("allocating %"PRIu64" bytes for dirty bitmap", bmsize);
if (!(s->writelog = calloc(bmsize, 1))) {
BWPRINTF("could not allocate dirty bitmap of size %"PRIu64, bmsize);
return -1;
}
return 0;
}
static int writelog_free(struct tdlog_state *s)
{
if (s->writelog)
free(s->writelog);
return 0;
}
static int writelog_set(struct tdlog_state* s, uint64_t sector, int count)
{
int i;
for (i = 0; i < count; i++)
set_bit(sector + i, s->writelog);
return 0;
}
/* if end is 0, clear to end of disk */
int writelog_clear(struct tdlog_state* s, uint64_t start, uint64_t end)
{
if (!end)
end = s->size;
/* clear to word boundaries */
while (BITMAP_SHIFT(start))
clear_bit(start++, s->writelog);
while (BITMAP_SHIFT(end))
clear_bit(end--, s->writelog);
memset(s->writelog + start / BITS_PER_LONG, 0, (end - start) >> 3);
return 0;
}
/* returns last block exported (may not be end of disk if shm region
* overflows) */
static uint64_t writelog_export(struct tdlog_state* s)
{
struct disk_range* range = s->shm;
uint64_t i = 0;
BDPRINTF("sector count: %"PRIu64, s->size);
for (i = 0; i < s->size; i++) {
if (test_bit(i, s->writelog)) {
/* range start */
range->sector = i;
range->count = 1;
/* find end */
for (i++; i < s->size && test_bit(i, s->writelog); i++)
range->count++;
BDPRINTF("export: dirty extent %"PRIu64":%u",
range->sector, range->count);
range++;
/* out of space in shared memory region */
if ((void*)range >= bmend(s->shm)) {
BDPRINTF("out of space in shm region at sector %"PRIu64, i);
return i;
}
/* undo forloop increment */
i--;
}
}
/* NULL-terminate range list */
range->sector = 0;
range->count = 0;
return i;
}
/* -- communication channel -- */
/* remove FS special characters in up to len bytes of path */
static inline void path_escape(char* path, size_t len) {
int i;
for (i = 0; i < len && path[i]; i++)
if (strchr(":/", path[i]))
path[i] = '_';
}
static char* ctl_makepath(const char* name, const char* ext)
{
char* res;
char *file;
file = strrchr(name, '/');
if (!file) {
BWPRINTF("invalid name %s\n", name);
return NULL;
}
if (asprintf(&res, BLKTAP_CTRL_DIR "/log_%s.%s", file, ext) < 0) {
BWPRINTF("could not allocate path");
return NULL;
}
path_escape(res + strlen(BLKTAP_CTRL_DIR) + 5, strlen(file));
return res;
}
static int shmem_open(struct tdlog_state* s, const char* name)
{
int i, l, fd;
/* device name -> path */
if (asprintf(&s->shmpath, "/log_%s.wlog", name) < 0) {
BWPRINTF("could not allocate shm path");
return -1;
}
path_escape(s->shmpath + 5, strlen(name));
if ((fd = shm_open(s->shmpath, O_CREAT|O_RDWR, 0750)) < 0) {
BWPRINTF("could not open shared memory file %s: %s", s->shmpath,
strerror(errno));
goto err;
}
if (ftruncate(fd, SHMSIZE) < 0) {
BWPRINTF("error truncating shmem to size %u", SHMSIZE);
close(fd);
goto err;
}
s->shm = mmap(NULL, SHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (s->shm == MAP_FAILED) {
BWPRINTF("could not mmap write log shm: %s", strerror(errno));
goto err;
}
return 0;
err:
s->shm = NULL;
free(s->shmpath);
s->shmpath = NULL;
return -1;
}
static int shmem_close(struct tdlog_state* s)
{
if (s->shm) {
munmap(s->shm, SHMSIZE);
s->shm = NULL;
}
if (s->shmpath) {
shm_unlink(s->shmpath);
s->shmpath = NULL;
}
return 0;
}
/* control socket */
static int ctl_open(struct tdlog_state* s, const char* name)
{
struct sockaddr_un saddr;
if (!(s->ctlpath = ctl_makepath(name, "ctl")))
return -1;
if ((s->ctl.fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
BWPRINTF("error opening control socket: %s", strerror(errno));
goto err;
}
memset(&saddr, 0, sizeof(saddr));
saddr.sun_family = AF_UNIX;
memcpy(saddr.sun_path, s->ctlpath, strlen(s->ctlpath));
if (unlink(s->ctlpath) && errno != ENOENT) {
BWPRINTF("error unlinking old socket path %s: %s", s->ctlpath,
strerror(errno));
goto err_sock;
}
if (bind(s->ctl.fd, &saddr, sizeof(saddr)) < 0) {
BWPRINTF("error binding control socket to %s: %s", s->ctlpath,
strerror(errno));
goto err_sock;
}
if (listen(s->ctl.fd, 1) < 0) {
BWPRINTF("error listening on control socket: %s", strerror(errno));
goto err_sock;
}
s->ctl.id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
s->ctl.fd, 0, ctl_accept, s);
if (s->ctl.id < 0) {
BWPRINTF("error register event handler: %s", strerror(s->ctl.id));
goto err_sock;
}
return 0;
err_sock:
close(s->ctl.fd);
s->ctl.fd = -1;
err:
free(s->ctlpath);
s->ctlpath = NULL;
return -1;
}
static int ctl_close(struct tdlog_state* s)
{
while (s->connected) {
tapdisk_server_unregister_event(s->connections[s->connected].id);
close(s->connections[s->connected].fd);
s->connections[s->connected].fd = -1;
s->connections[s->connected].id = 0;
s->connected--;
}
if (s->ctl.fd >= 0) {
tapdisk_server_unregister_event(s->ctl.id);
close(s->ctl.fd);
s->ctl.fd = -1;
s->ctl.id = 0;
}
if (s->ctlpath) {
unlink(s->ctlpath);
free(s->ctlpath);
s->ctlpath = NULL;
}
/* XXX this must be fixed once requests are actually in flight */
/* could just drain the existing ring here first */
if (s->sring) {
SHARED_RING_INIT(s->sring);
BACK_RING_INIT(&s->bring, s->sring, SRINGSIZE);
}
return 0;
}
/* walk list of open sockets, close matching fd */
static int ctl_close_sock(struct tdlog_state* s, int fd)
{
int i;
for (i = 0; i <= s->connected; i++) {
if (s->connections[i].fd == fd) {
tapdisk_server_unregister_event(s->connections[i].id);
close(s->connections[i].fd);
s->connections[i].fd = -1;
s->connections[i].id = 0;
s->connected--;
return 0;
}
}
BWPRINTF("requested to close unknown socket %d", fd);
return -1;
}
static void ctl_accept(event_id_t id, char mode, void *private)
{
struct tdlog_state* s = (struct tdlog_state *)private;
int fd;
event_id_t cid;
if ((fd = accept(s->ctl.fd, NULL, NULL)) < 0) {
BWPRINTF("error accepting control connection: %s", strerror(errno));
return;
}
if (s->connected) {
BWPRINTF("control session in progress, closing new connection");
close(fd);
return;
}
cid = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
fd, 0, ctl_request, s);
if (cid < 0) {
BWPRINTF("error registering connection event handler: %s", strerror(cid));
close(fd);
return;
}
s->connections[s->connected].fd = fd;
s->connections[s->connected].id = cid;
s->connected++;
}
/* response format: 4 bytes shmsize, 0-terminated path */
static int ctl_get_shmpath(struct tdlog_state* s, int fd)
{
char msg[CTLRSPLEN_SHMP + 1];
uint32_t sz;
int rc;
BDPRINTF("ctl: sending shared memory parameters (size: %u, path: %s)",
SHMSIZE, s->shmpath);
/* TMP: sanity-check shm */
sz = 0xdeadbeef;
memcpy(s->shm, &sz, sizeof(sz));
sz = SHMSIZE;
memcpy(msg, &sz, sizeof(sz));
snprintf(msg + sizeof(sz), sizeof(msg) - sizeof(sz), "%s", s->shmpath);
if ((rc = write(fd, msg, CTLRSPLEN_SHMP)) < 0) {
BWPRINTF("error writing shmpath: %s", strerror(errno));
return -1;
}
return 0;
}
static int ctl_peek_writes(struct tdlog_state* s, int fd)
{
int rc;
BDPRINTF("ctl: peeking bitmap");
writelog_export(s);
if ((rc = write(fd, "done", CTLRSPLEN_PEEK)) < 0) {
BWPRINTF("error writing peek ack: %s", strerror(errno));
return -1;
}
return 0;
}
static int ctl_clear_writes(struct tdlog_state* s, int fd)
{
int rc;
BDPRINTF("ctl: clearing bitmap");
writelog_clear(s, 0, 0);
if ((rc = write(fd, "done", CTLRSPLEN_CLEAR)) < 0) {
BWPRINTF("error writing clear ack: %s", strerror(errno));
return -1;
}
return 0;
}
/* get dirty bitmap and clear it atomically */
static int ctl_get_writes(struct tdlog_state* s, int fd)
{
int rc;
BDPRINTF("ctl: getting bitmap");
writelog_export(s);
writelog_clear(s, 0, 0);
if ((rc = write(fd, "done", CTLRSPLEN_GET)) < 0) {
BWPRINTF("error writing get ack: %s", strerror(errno));
return -1;
}
return 0;
}
/* get requests from ring */
static int ctl_kick(struct tdlog_state* s, int fd)
{
RING_IDX reqstart, reqend;
log_request_t req;
/* XXX testing */
RING_IDX rspstart, rspend;
log_response_t rsp;
struct log_ctlmsg msg;
int rc;
reqstart = s->bring.req_cons;
reqend = s->sring->req_prod;
BDPRINTF("ctl: ring kicked (start = %u, end = %u)", reqstart, reqend);
while (reqstart != reqend) {
/* XXX actually submit these! */
memcpy(&req, RING_GET_REQUEST(&s->bring, reqstart), sizeof(req));
BDPRINTF("ctl: read request %"PRIu64":%u", req.sector, req.count);
s->bring.req_cons = ++reqstart;
rsp.sector = req.sector;
rsp.count = req.count;
memcpy(RING_GET_RESPONSE(&s->bring, s->bring.rsp_prod_pvt), &rsp,
sizeof(rsp));
s->bring.rsp_prod_pvt++;
}
RING_PUSH_RESPONSES(&s->bring);
memset(&msg, 0, sizeof(msg));
memcpy(msg.msg, LOGCMD_KICK, 4);
if ((rc = write(fd, &msg, sizeof(msg))) < 0) {
BWPRINTF("error sending notify: %s", strerror(errno));
return -1;
} else if (rc < sizeof(msg)) {
BWPRINTF("short notify write (%d/%zd)", rc, sizeof(msg));
return -1;
}
return 0;
}
static int ctl_do_request(struct tdlog_state* s, int fd, struct log_ctlmsg* msg)
{
if (!strncmp(msg->msg, LOGCMD_SHMP, 4)) {
return ctl_get_shmpath(s, fd);
} else if (!strncmp(msg->msg, LOGCMD_PEEK, 4)) {
return ctl_peek_writes(s, fd);
} else if (!strncmp(msg->msg, LOGCMD_CLEAR, 4)) {
return ctl_clear_writes(s, fd);
} else if (!strncmp(msg->msg, LOGCMD_GET, 4)) {
return ctl_get_writes(s, fd);
} else if (!strncmp(msg->msg, LOGCMD_KICK, 4)) {
return ctl_kick(s, fd);
}
BWPRINTF("unknown control request %.4s", msg->msg);
return -1;
}
static inline int ctl_find_connection(struct tdlog_state *s, event_id_t id)
{
int i;
for (i = 0; i < s->connected; i++)
if (s->connections[i].id == id)
return s->connections[i].fd;
BWPRINTF("unrecognized event callback id %d", id);
return -1;
}
static void ctl_request(event_id_t id, char mode, void *private)
{
struct tdlog_state* s = (struct tdlog_state*)private;
struct log_ctlmsg msg;
int rc, i, fd = -1;
fd = ctl_find_connection(s, id);
if (fd == -1)
return;
if ((rc = read(fd, &msg, sizeof(msg))) < 0) {
BWPRINTF("error reading from ctl socket %d, closing: %s", fd,
strerror(errno));
ctl_close_sock(s, fd);
return;
} else if (rc == 0) {
BDPRINTF("ctl_request: EOF, closing socket");
ctl_close_sock(s, fd);
return;
} else if (rc < sizeof(msg)) {
BWPRINTF("short request received (%d/%zd bytes), ignoring", rc,
sizeof(msg));
return;
}
ctl_do_request(s, fd, &msg);
}
/* -- interface -- */
static int tdlog_close(td_driver_t*);
static int tdlog_open(td_driver_t* driver, const char* name, td_flag_t flags)
{
struct tdlog_state* s = (struct tdlog_state*)driver->data;
int rc;
memset(s, 0, sizeof(*s));
s->size = driver->info.size;
if ((rc = writelog_create(s))) {
tdlog_close(driver);
return rc;
}
if ((rc = shmem_open(s, name))) {
tdlog_close(driver);
return rc;
}
if ((rc = ctl_open(s, name))) {
tdlog_close(driver);
return rc;
}
s->sring = (log_sring_t*)sringstart(s->shm);
SHARED_RING_INIT(s->sring);
BACK_RING_INIT(&s->bring, s->sring, SRINGSIZE);
BDPRINTF("opened ctl socket");
return 0;
}
static int tdlog_close(td_driver_t* driver)
{
struct tdlog_state* s = (struct tdlog_state*)driver->data;
ctl_close(s);
shmem_close(s);
writelog_free(s);
return 0;
}
static void tdlog_queue_read(td_driver_t* driver, td_request_t treq)
{
td_forward_request(treq);
}
static void tdlog_queue_write(td_driver_t* driver, td_request_t treq)
{
struct tdlog_state* s = (struct tdlog_state*)driver->data;
int rc;
writelog_set(s, treq.sec, treq.secs);
td_forward_request(treq);
}
static int tdlog_get_parent_id(td_driver_t* driver, td_disk_id_t* id)
{
return -EINVAL;
}
static int tdlog_validate_parent(td_driver_t *driver,
td_driver_t *parent, td_flag_t flags)
{
return 0;
}
struct tap_disk tapdisk_log = {
.disk_type = "tapdisk_log",
.private_data_size = sizeof(struct tdlog_state),
.flags = 0,
.td_open = tdlog_open,
.td_close = tdlog_close,
.td_queue_read = tdlog_queue_read,
.td_queue_write = tdlog_queue_write,
.td_get_parent_id = tdlog_get_parent_id,
.td_validate_parent = tdlog_validate_parent,
};
File diff suppressed because it is too large. Load diff
+269
View File
@@ -0,0 +1,269 @@
/*
* Copyright (c) 2007, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <string.h>
#include "tapdisk.h"
#include "tapdisk-driver.h"
#include "tapdisk-interface.h"
char *img;
long int disksector_size;
long int disksize;
long int diskinfo;
static int connections = 0;
struct tdram_state {
int fd;
};
/*Get Image size, secsize*/
static int get_image_info(int fd, td_disk_info_t *info)
{
int ret;
long size;
unsigned long total_size;
struct statvfs statBuf;
struct stat stat;
ret = fstat(fd, &stat);
if (ret != 0) {
DPRINTF("ERROR: fstat failed, Couldn't stat image");
return -EINVAL;
}
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
info->size = 0;
if (ioctl(fd,BLKGETSIZE,&info->size)!=0) {
DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
return -EINVAL;
}
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)(info->size << SECTOR_SHIFT),
(long long unsigned)info->size);
/*Get the sector size*/
#if defined(BLKSSZGET)
{
int arg;
info->sector_size = DEFAULT_SECTOR_SIZE;
ioctl(fd, BLKSSZGET, &info->sector_size);
if (info->sector_size != DEFAULT_SECTOR_SIZE)
DPRINTF("Note: sector size is %ld (not %d)\n",
info->sector_size, DEFAULT_SECTOR_SIZE);
}
#else
info->sector_size = DEFAULT_SECTOR_SIZE;
#endif
} else {
/*Local file? try fstat instead*/
info->size = (stat.st_size >> SECTOR_SHIFT);
info->sector_size = DEFAULT_SECTOR_SIZE;
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)(info->size << SECTOR_SHIFT),
(long long unsigned)info->size);
}
if (info->size == 0) {
info->size =((uint64_t) MAX_RAMDISK_SIZE);
info->sector_size = DEFAULT_SECTOR_SIZE;
}
info->info = 0;
/*Store variables locally*/
disksector_size = info->sector_size;
disksize = info->size;
diskinfo = info->info;
DPRINTF("Image sector_size: \n\t[%lu]\n",
info->sector_size);
return 0;
}
/* Open the disk file and initialize ram state. */
int tdram_open (td_driver_t *driver, const char *name, td_flag_t flags)
{
char *p;
uint64_t size;
int i, fd, ret = 0, count = 0, o_flags;
struct tdram_state *prv = (struct tdram_state *)driver->data;
connections++;
if (connections > 1) {
driver->info.sector_size = disksector_size;
driver->info.size = disksize;
driver->info.info = diskinfo;
DPRINTF("Image already open, returning parameters:\n");
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)(driver->info.size << SECTOR_SHIFT),
(long long unsigned)driver->info.size);
DPRINTF("Image sector_size: \n\t[%lu]\n",
driver->info.sector_size);
prv->fd = -1;
goto done;
}
/* Open the file */
o_flags = O_DIRECT | O_LARGEFILE |
((flags == TD_OPEN_RDONLY) ? O_RDONLY : O_RDWR);
fd = open(name, o_flags);
if ((fd == -1) && (errno == EINVAL)) {
/* Maybe O_DIRECT isn't supported. */
o_flags &= ~O_DIRECT;
fd = open(name, o_flags);
if (fd != -1) DPRINTF("WARNING: Accessing image without"
"O_DIRECT! (%s)\n", name);
} else if (fd != -1) DPRINTF("open(%s) with O_DIRECT\n", name);
if (fd == -1) {
DPRINTF("Unable to open [%s]!\n",name);
ret = 0 - errno;
goto done;
}
prv->fd = fd;
ret = get_image_info(fd, &driver->info);
size = MAX_RAMDISK_SIZE;
if (driver->info.size > size) {
DPRINTF("Disk exceeds limit, must be less than [%d]MB",
(MAX_RAMDISK_SIZE<<SECTOR_SHIFT)>>20);
return -ENOMEM;
}
/*Read the image into memory*/
if (posix_memalign((void **)&img,
DEFAULT_SECTOR_SIZE,
driver->info.size << SECTOR_SHIFT)) {
DPRINTF("Mem malloc failed\n");
return -errno;
}
p = img;
DPRINTF("Reading %llu bytes.......",
(long long unsigned)driver->info.size << SECTOR_SHIFT);
for (i = 0; i < driver->info.size; i++) {
ret = read(prv->fd, p, driver->info.sector_size);
if (ret != driver->info.sector_size) {
DPRINTF("ret = %d, errno = %d\n", ret, errno);
ret = 0 - errno;
break;
} else {
count += ret;
p = img + count;
}
}
DPRINTF("[%d]\n",count);
if (count != driver->info.size << SECTOR_SHIFT) {
ret = -1;
} else {
ret = 0;
}
done:
return ret;
}
void tdram_queue_read(td_driver_t *driver, td_request_t treq)
{
struct tdram_state *prv = (struct tdram_state *)driver->data;
int size = treq.secs * driver->info.sector_size;
uint64_t offset = treq.sec * (uint64_t)driver->info.sector_size;
memcpy(treq.buf, img + offset, size);
td_complete_request(treq, 0);
}
void tdram_queue_write(td_driver_t *driver, td_request_t treq)
{
struct tdram_state *prv = (struct tdram_state *)driver->data;
int size = treq.secs * driver->info.sector_size;
uint64_t offset = treq.sec * (uint64_t)driver->info.sector_size;
/* We assume that write access is controlled
* at a higher level for multiple disks */
memcpy(img + offset, treq.buf, size);
td_complete_request(treq, 0);
}
int tdram_close(td_driver_t *driver)
{
struct tdram_state *prv = (struct tdram_state *)driver->data;
connections--;
return 0;
}
int tdram_get_parent_id(td_driver_t *driver, td_disk_id_t *id)
{
return TD_NO_PARENT;
}
int tdram_validate_parent(td_driver_t *driver,
td_driver_t *pdriver, td_flag_t flags)
{
return -EINVAL;
}
struct tap_disk tapdisk_ram = {
.disk_type = "tapdisk_ram",
.flags = 0,
.private_data_size = sizeof(struct tdram_state),
.td_open = tdram_open,
.td_close = tdram_close,
.td_queue_read = tdram_queue_read,
.td_queue_write = tdram_queue_write,
.td_get_parent_id = tdram_get_parent_id,
.td_validate_parent = tdram_validate_parent,
.td_debug = NULL,
};
File diff suppressed because it is too large. Load diff
+214
View File
@@ -0,0 +1,214 @@
#ifndef BSWAP_H
#define BSWAP_H
//#include "config-host.h"
#include <inttypes.h>
#if defined(__NetBSD__)
#include <sys/endian.h>
#include <sys/types.h>
#elif defined(__OpenBSD__)
#include <machine/endian.h>
#define bswap_16(x) swap16(x)
#define bswap_32(x) swap32(x)
#define bswap_64(x) swap64(x)
#else
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#else
#define bswap_16(x) \
({ \
uint16_t __x = (x); \
((uint16_t)( \
(((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
(((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
})
#define bswap_32(x) \
({ \
uint32_t __x = (x); \
((uint32_t)( \
(((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
(((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
(((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
(((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
})
#define bswap_64(x) \
({ \
uint64_t __x = (x); \
((uint64_t)( \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
(uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
})
#endif /* !HAVE_BYTESWAP_H */
static inline uint16_t bswap16(uint16_t x)
{
return bswap_16(x);
}
static inline uint32_t bswap32(uint32_t x)
{
return bswap_32(x);
}
static inline uint64_t bswap64(uint64_t x)
{
return bswap_64(x);
}
static inline void bswap16s(uint16_t *s)
{
*s = bswap16(*s);
}
static inline void bswap32s(uint32_t *s)
{
*s = bswap32(*s);
}
static inline void bswap64s(uint64_t *s)
{
*s = bswap64(*s);
}
#endif
#if defined(WORDS_BIGENDIAN)
#define be_bswap(v, size) (v)
#define le_bswap(v, size) bswap ## size(v)
#define be_bswaps(v, size)
#define le_bswaps(p, size) *p = bswap ## size(*p);
#else
#define le_bswap(v, size) (v)
#define be_bswap(v, size) bswap ## size(v)
#define le_bswaps(v, size)
#define be_bswaps(p, size) *p = bswap ## size(*p);
#endif
#define CPU_CONVERT(endian, size, type)\
static inline type endian ## size ## _to_cpu(type v)\
{\
return endian ## _bswap(v, size);\
}\
\
static inline type cpu_to_ ## endian ## size(type v)\
{\
return endian ## _bswap(v, size);\
}\
\
static inline void endian ## size ## _to_cpus(type *p)\
{\
endian ## _bswaps(p, size)\
}\
\
static inline void cpu_to_ ## endian ## size ## s(type *p)\
{\
endian ## _bswaps(p, size)\
}\
\
static inline type endian ## size ## _to_cpup(const type *p)\
{\
return endian ## size ## _to_cpu(*p);\
}\
\
static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
{\
*p = cpu_to_ ## endian ## size(v);\
}
CPU_CONVERT(be, 16, uint16_t)
CPU_CONVERT(be, 32, uint32_t)
CPU_CONVERT(be, 64, uint64_t)
CPU_CONVERT(le, 16, uint16_t)
CPU_CONVERT(le, 32, uint32_t)
CPU_CONVERT(le, 64, uint64_t)
/* unaligned versions (optimized for frequent unaligned accesses)*/
#if defined(__i386__) || defined(__powerpc__)
#define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
#define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
#define le16_to_cpupu(p) le16_to_cpup(p)
#define le32_to_cpupu(p) le32_to_cpup(p)
#define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
#define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
#else
static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
{
uint8_t *p1 = (uint8_t *)p;
p1[0] = v;
p1[1] = v >> 8;
}
static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
{
uint8_t *p1 = (uint8_t *)p;
p1[0] = v;
p1[1] = v >> 8;
p1[2] = v >> 16;
p1[3] = v >> 24;
}
static inline uint16_t le16_to_cpupu(const uint16_t *p)
{
const uint8_t *p1 = (const uint8_t *)p;
return p1[0] | (p1[1] << 8);
}
static inline uint32_t le32_to_cpupu(const uint32_t *p)
{
const uint8_t *p1 = (const uint8_t *)p;
return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
}
static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
{
uint8_t *p1 = (uint8_t *)p;
p1[0] = v >> 8;
p1[1] = v;
}
static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
{
uint8_t *p1 = (uint8_t *)p;
p1[0] = v >> 24;
p1[1] = v >> 16;
p1[2] = v >> 8;
p1[3] = v;
}
#endif
#ifdef WORDS_BIGENDIAN
#define cpu_to_32wu cpu_to_be32wu
#else
#define cpu_to_32wu cpu_to_le32wu
#endif
#undef le_bswap
#undef be_bswap
#undef le_bswaps
#undef be_bswaps
#endif /* BSWAP_H */
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
cat > .gcrypt.c << EOF
#include <gcrypt.h>
int main(void) { return 0; }
EOF
if $1 -o .gcrypt .gcrypt.c -lgcrypt 2>/dev/null ; then
echo "yes"
else
echo "no"
fi
rm -f .gcrypt*
+184
View File
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2007, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __DISKTYPES_H__
#define __DISKTYPES_H__
typedef struct disk_info {
int idnum;
char name[50]; /* e.g. "RAMDISK" */
char handle[10]; /* xend handle, e.g. 'ram' */
int single_handler; /* is there a single controller for all */
/* instances of disk type? */
#ifdef TAPDISK
struct tap_disk *drv;
#endif
} disk_info_t;
extern struct tap_disk tapdisk_aio;
/* extern struct tap_disk tapdisk_sync; */
/* extern struct tap_disk tapdisk_vmdk; */
/* extern struct tap_disk tapdisk_vhdsync; */
extern struct tap_disk tapdisk_vhd;
extern struct tap_disk tapdisk_ram;
extern struct tap_disk tapdisk_qcow;
extern struct tap_disk tapdisk_block_cache;
extern struct tap_disk tapdisk_log;
#define MAX_DISK_TYPES 20
#define DISK_TYPE_AIO 0
#define DISK_TYPE_SYNC 1
#define DISK_TYPE_VMDK 2
#define DISK_TYPE_VHDSYNC 3
#define DISK_TYPE_VHD 4
#define DISK_TYPE_RAM 5
#define DISK_TYPE_QCOW 6
#define DISK_TYPE_BLOCK_CACHE 7
#define DISK_TYPE_LOG 9
/*Define Individual Disk Parameters here */
static disk_info_t null_disk = {
-1,
"null disk",
"null",
0,
#ifdef TAPDISK
0,
#endif
};
static disk_info_t aio_disk = {
DISK_TYPE_AIO,
"raw image (aio)",
"aio",
0,
#ifdef TAPDISK
&tapdisk_aio,
#endif
};
/*
static disk_info_t sync_disk = {
DISK_TYPE_SYNC,
"raw image (sync)",
"sync",
0,
#ifdef TAPDISK
&tapdisk_sync,
#endif
};
static disk_info_t vmdk_disk = {
DISK_TYPE_VMDK,
"vmware image (vmdk)",
"vmdk",
1,
#ifdef TAPDISK
&tapdisk_vmdk,
#endif
};
static disk_info_t vhdsync_disk = {
DISK_TYPE_VHDSYNC,
"virtual server image (vhd) - synchronous",
"vhdsync",
1,
#ifdef TAPDISK
&tapdisk_vhdsync,
#endif
};
*/
static disk_info_t vhd_disk = {
DISK_TYPE_VHD,
"virtual server image (vhd)",
"vhd",
0,
#ifdef TAPDISK
&tapdisk_vhd,
#endif
};
static disk_info_t ram_disk = {
DISK_TYPE_RAM,
"ramdisk image (ram)",
"ram",
1,
#ifdef TAPDISK
&tapdisk_ram,
#endif
};
static disk_info_t qcow_disk = {
DISK_TYPE_QCOW,
"qcow disk (qcow)",
"qcow",
0,
#ifdef TAPDISK
&tapdisk_qcow,
#endif
};
static disk_info_t block_cache_disk = {
DISK_TYPE_BLOCK_CACHE,
"block cache image (bc)",
"bc",
1,
#ifdef TAPDISK
&tapdisk_block_cache,
#endif
};
static disk_info_t log_disk = {
DISK_TYPE_LOG,
"write logger (log)",
"log",
0,
#ifdef TAPDISK
&tapdisk_log,
#endif
};
/*Main disk info array */
static disk_info_t *dtypes[] = {
&aio_disk,
&null_disk, /* &sync_disk, */
&null_disk, /* &vmdk_disk, */
&null_disk, /* &vhdsync_disk, */
&vhd_disk,
&ram_disk,
&qcow_disk,
&block_cache_disk,
&null_disk,
&log_disk,
};
#endif
+318
View File
@@ -0,0 +1,318 @@
/* img2qcow.c
*
* Generates a qcow format disk and fills it from an existing image.
*
* (c) 2006 Julian Chesterfield and Andrew Warfield
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <zlib.h>
#include <inttypes.h>
#include <libaio.h>
#include <openssl/md5.h>
#include "bswap.h"
#include "aes.h"
#include "tapdisk.h"
#include "tapdisk-server.h"
#include "tapdisk-driver.h"
#include "tapdisk-interface.h"
#include "qcow.h"
#include "blk.h"
#if 1
#define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
#else
#define DFPRINTF(_f, _a...) ((void)0)
#endif
/* *BSD has no O_LARGEFILE */
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#define TAPDISK 1
#define BLOCK_PROCESSSZ 4096
#define QCOW_VBD 0
#define PROGRESS_QUANT 2
static int running = 1, complete = 0;
static int returned_events = 0, submit_events = 0;
static uint32_t read_idx = 0;
td_driver_t *ddqcow;
td_vbd_t* qcow_vbd;
static uint64_t prev = 0, written = 0;
static char output[(100/PROGRESS_QUANT) + 5];
extern tapdisk_server_t server;
static void print_bytes(void *ptr, int length)
{
int i,k;
unsigned char *p = ptr;
DFPRINTF("Buf dump, length %d:\n",length);
for (k = 0; k < length; k++) {
DFPRINTF("%x",*p);
*p++;
if(k % 16 == 0) DFPRINTF("\n");
else if(k % 2 == 0) DFPRINTF(" ");
}
DFPRINTF("\n");
return;
}
static void debug_output(uint64_t progress, uint64_t size)
{
//Output progress every PROGRESS_QUANT
uint64_t blocks = size/(100/PROGRESS_QUANT);
if (progress/blocks > prev) {
memcpy(output+prev+1,"=>",2);
prev++;
DFPRINTF("\r%s %"PRIi64"%%",
output, (int64_t)((prev-1)*PROGRESS_QUANT));
}
return;
}
static int get_image_info(td_disk_info_t *driver, int fd)
{
int ret;
long size;
unsigned long total_size;
struct statvfs statBuf;
struct stat stat;
uint64_t sector_size=DEFAULT_SECTOR_SIZE;
ret = fstat(fd, &stat);
if (ret != 0) {
DFPRINTF("ERROR: fstat failed, Couldn't stat image");
return -EINVAL;
}
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
if (blk_getimagesize(fd, &driver->size) != 0)
return -EINVAL;
DFPRINTF("Image size: \n\tpre sector_shift [%"PRIu64"]\n\tpost "
"sector_shift [%"PRIu64"]\n",
(uint64_t)(driver->size << SECTOR_SHIFT),
(uint64_t)driver->size);
/*Get the sector size*/
if (!blk_getsectorsize(fd, &sector_size))
driver->sector_size = sector_size;
} else {
/*Local file? try fstat instead*/
driver->size = (stat.st_size >> SECTOR_SHIFT);
driver->sector_size = DEFAULT_SECTOR_SIZE;
DFPRINTF("Image size: [%"PRIu64"]\n",
(uint64_t)driver->size);
}
return 0;
}
void send_responses(td_request_t treq, int err)
{
if (err < 0) {
DFPRINTF("AIO FAILURE: res [%d]!\n",err);
return;
}
returned_events++;
free(treq.buf);
}
int main(int argc, const char *argv[])
{
int ret = -1, fd, len, err;
struct timeval timeout;
uint64_t i;
char *buf;
td_request_t treq;
td_disk_info_t info;
td_vbd_request_t* vreq;
if (argc != 3) {
fprintf(stderr, "Qcow-utils: v1.0.0\n");
fprintf(stderr, "usage: %s <QCOW FILENAME> <SRC IMAGE>\n",
argv[0]);
exit(-1);
}
/*Open image*/
fd = open(argv[2], O_RDONLY | O_LARGEFILE);
if (fd == -1) {
DFPRINTF("Unable to open [%s], (err %d)!\n",argv[2],0 - errno);
exit(-1);
}
get_image_info(&info, fd);
/*Create qcow file*/
ret = qcow_create(argv[1],info.size<<SECTOR_SHIFT,NULL,0);
if (ret < 0) {
DFPRINTF("Unable to create QCOW file\n");
exit(-1);
} else DFPRINTF("Qcow file created: size %"PRIu64" sectors\n",
(uint64_t)info.size);
/* Open Qcow image*/
err = tapdisk_server_initialize(NULL, NULL);
if( err ) {
DPRINTF("qcow2raw Couldn't initialize server instance.\n");
return err;
}
err=tapdisk_vbd_initialize(-1,-1, QCOW_VBD);
if( err ) {
DPRINTF("qcow2raw Couldn't initialize qcow vbd.\n");
return err;
}
qcow_vbd = tapdisk_server_get_vbd(QCOW_VBD);
if (!qcow_vbd) {
err = -ENODEV;
DPRINTF("qcow2raw Couldn't create qcow vbd.\n");
return err;
}
err = tapdisk_vbd_open_vdi(qcow_vbd, argv[1], DISK_TYPE_QCOW,
TAPDISK_STORAGE_TYPE_DEFAULT,
0);
if( err ) {
DPRINTF("qcow2raw Couldn't open qcow file.\n");
return err;
}
ddqcow=(tapdisk_vbd_first_image(qcow_vbd))->driver;
/*Initialise the output string*/
memset(output,0x20,(100/PROGRESS_QUANT)+5);
output[0] = '[';
output[(100/PROGRESS_QUANT)+2] = ']';
output[(100/PROGRESS_QUANT)+3] = '\0';
DFPRINTF("%s",output);
i = 0;
while (running) {
if (!complete) {
/*Read sector from image*/
if (lseek(fd, i*512, SEEK_SET) == (off_t)-1) {
DFPRINTF("Unable to access file offset %"PRIu64"\n",
(uint64_t)i*512);
exit(-1);
}
if( (ret = posix_memalign((void **)&buf,
BLOCK_PROCESSSZ,
BLOCK_PROCESSSZ)) != 0) {
DFPRINTF("Unable to read memalign buf (%d)\n",ret);
exit(-1);
}
/*We attempt to read 4k sized blocks*/
len = read(fd, buf, BLOCK_PROCESSSZ);
if (len < 512) {
DFPRINTF("Unable to read sector %"PRIu64"\n",
(uint64_t) (i));
complete = 1;
continue;
}
len = (len >> 9);
treq.op = TD_OP_WRITE;
treq.buf = buf;
treq.sec = i;
treq.secs = len;
treq.image = 0;
treq.cb = send_responses;
treq.cb_data = buf;
treq.id = 0;
treq.sidx = 0;
vreq = calloc(1, sizeof(td_vbd_request_t));
treq.private = vreq;
vreq->submitting = 1;
INIT_LIST_HEAD(&vreq->next);
tapdisk_vbd_move_request(treq.private,
&qcow_vbd->pending_requests);
ddqcow->ops->td_queue_write(ddqcow,treq);
--vreq->submitting;
submit_events++;
i += len;
if (i == info.size)
complete = 1;
tapdisk_submit_all_tiocbs(&server.aio_queue);
debug_output(i,info.size);
}
while(returned_events != submit_events) {
ret = scheduler_wait_for_events(&server.scheduler);
if (ret < 0) {
DFPRINTF("server wait returned %d\n", ret);
sleep(2);
}
}
if (complete && (returned_events == submit_events))
running = 0;
}
memcpy(output+prev+1,"=",1);
DFPRINTF("\r%s 100%%\nTRANSFER COMPLETE\n\n", output);
ddqcow->ops->td_close(ddqcow);
free(ddqcow->data);
return 0;
}
+664
View File
@@ -0,0 +1,664 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include "io-optimize.h"
#include "tapdisk-log.h"
#if (!defined(TEST) && defined(DEBUG))
#define DBG(ctx, f, a...) tlog_write(TLOG_DBG, f, ##a)
#elif defined(TEST)
#define DBG(ctx, f, a...) printf(f, ##a)
#else
#define DBG(ctx, f, a...) ((void)0)
#endif
static void print_merged_iocbs(struct opioctx *ctx,
struct iocb **iocbs, int num_iocbs);
void
opio_free(struct opioctx *ctx)
{
free(ctx->opios);
free(ctx->free_opios);
free(ctx->iocb_queue);
free(ctx->event_queue);
}
int
opio_init(struct opioctx *ctx, int num_iocbs)
{
int i;
memset(ctx, 0, sizeof(struct opioctx));
ctx->num_opios = num_iocbs;
ctx->free_opio_cnt = num_iocbs;
ctx->opios = calloc(1, sizeof(struct opio) * num_iocbs);
ctx->free_opios = calloc(1, sizeof(struct opio *) * num_iocbs);
ctx->iocb_queue = calloc(1, sizeof(struct iocb *) * num_iocbs);
ctx->event_queue = calloc(1, sizeof(struct io_event) * num_iocbs);
if (!ctx->opios || !ctx->free_opios ||
!ctx->iocb_queue || !ctx->event_queue)
goto fail;
for (i = 0; i < num_iocbs; i++)
ctx->free_opios[i] = &ctx->opios[i];
return 0;
fail:
opio_free(ctx);
return -ENOMEM;
}
static inline struct opio *
alloc_opio(struct opioctx *ctx)
{
if (ctx->free_opio_cnt <= 0)
return NULL;
return ctx->free_opios[--ctx->free_opio_cnt];
}
static inline void
free_opio(struct opioctx *ctx, struct opio *op)
{
memset(op, 0, sizeof(struct opio));
ctx->free_opios[ctx->free_opio_cnt++] = op;
}
static inline void
restore_iocb(struct opio *op)
{
struct iocb *io = op->iocb;
io->data = op->data;
io->u.c.buf = op->buf;
io->u.c.nbytes = op->nbytes;
}
static inline int
iocb_optimized(struct opioctx *ctx, struct iocb *io)
{
unsigned long iop = (unsigned long)io->data;
unsigned long start = (unsigned long)ctx->opios;
unsigned long end = start + (ctx->num_opios * sizeof(struct opio));
return (iop >= start && iop < end);
}
static inline int
contiguous_sectors(struct iocb *l, struct iocb *r)
{
return (l->u.c.offset + l->u.c.nbytes == r->u.c.offset);
}
static inline int
contiguous_buffers(struct iocb *l, struct iocb *r)
{
return (l->u.c.buf + l->u.c.nbytes == r->u.c.buf);
}
static inline int
contiguous_iocbs(struct iocb *l, struct iocb *r)
{
return ((l->aio_fildes == r->aio_fildes) &&
contiguous_sectors(l, r) &&
contiguous_buffers(l, r));
}
static inline void
init_opio_list(struct opio *op)
{
op->list.head = op->list.tail = op;
}
static struct opio *
opio_iocb_init(struct opioctx *ctx, struct iocb *io)
{
struct opio *op;
op = alloc_opio(ctx);
if (!op)
return NULL;
op->buf = io->u.c.buf;
op->nbytes = io->u.c.nbytes;
op->offset = io->u.c.offset;
op->data = io->data;
op->iocb = io;
io->data = op;
init_opio_list(op);
return op;
}
static inline struct opio *
opio_get(struct opioctx *ctx, struct iocb *io)
{
if (iocb_optimized(ctx, io))
return (struct opio *)io->data;
else
return opio_iocb_init(ctx, io);
}
static int
merge_tail(struct opioctx *ctx, struct iocb *head, struct iocb *io)
{
struct opio *ophead, *opio;
ophead = opio_get(ctx, head);
if (!ophead)
return -ENOMEM;
opio = opio_get(ctx, io);
if (!opio)
return -ENOMEM;
opio->head = ophead;
head->u.c.nbytes += io->u.c.nbytes;
ophead->list.tail = ophead->list.tail->next = opio;
return 0;
}
static int
merge(struct opioctx *ctx, struct iocb *head, struct iocb *io)
{
if (head->aio_lio_opcode != io->aio_lio_opcode)
return -EINVAL;
if (!contiguous_iocbs(head, io))
return -EINVAL;
return merge_tail(ctx, head, io);
}
int
io_merge(struct opioctx *ctx, struct iocb **queue, int num)
{
int i, on_queue;
struct iocb *io, **q;
struct opio *ophead;
if (!num)
return 0;
on_queue = 0;
q = ctx->iocb_queue;
memcpy(q, queue, num * sizeof(struct iocb *));
for (i = 1; i < num; i++) {
io = q[i];
if (merge(ctx, queue[on_queue], io) != 0)
queue[++on_queue] = io;
}
#if (defined(TEST) || defined(DEBUG))
print_merged_iocbs(ctx, queue, on_queue + 1);
#endif
return ++on_queue;
}
static int
expand_iocb(struct opioctx *ctx, struct iocb **queue, struct iocb *io)
{
int idx;
struct opio *op, *next;
idx = 0;
op = (struct opio *)io->data;
while (op) {
next = op->next;
restore_iocb(op);
queue[idx++] = op->iocb;
free_opio(ctx, op);
op = next;
}
return idx;
}
int
io_expand_iocbs(struct opioctx *ctx, struct iocb **queue, int idx, int num)
{
int i, on_queue;
struct iocb *io, **q;
if (!num)
return 0;
on_queue = 0;
q = ctx->iocb_queue;
memcpy(q, queue, num * sizeof(struct iocb *));
for (i = idx; i < num; i++) {
io = q[i];
if (!iocb_optimized(ctx, io))
queue[on_queue++] = io;
else
on_queue += expand_iocb(ctx, queue + on_queue, io);
}
return on_queue;
}
static int
expand_event(struct opioctx *ctx,
struct io_event *event, struct io_event *queue, int idx)
{
int err;
struct iocb *io;
struct io_event *ep;
struct opio *ophead, *op, *next;
io = event->obj;
ophead = (struct opio *)io->data;
op = ophead;
if (event->res == io->u.c.nbytes)
err = 0;
else if ((int)event->res < 0)
err = (int)event->res;
else
err = -EIO;
while (op) {
next = op->next;
ep = &queue[idx++];
ep->obj = op->iocb;
ep->res = (err ? err : op->nbytes);
restore_iocb(op);
free_opio(ctx, op);
op = next;
}
return idx;
}
int
io_split(struct opioctx *ctx, struct io_event *events, int num)
{
int on_queue;
struct iocb *io;
struct io_event *ep, *q;
if (!num)
return 0;
on_queue = 0;
q = ctx->event_queue;
memcpy(q, events, num * sizeof(struct io_event));
for (ep = q; num-- > 0; ep++) {
io = ep->obj;
if (!iocb_optimized(ctx, io))
events[on_queue++] = *ep;
else
on_queue = expand_event(ctx, ep, events, on_queue);
}
return on_queue;
}
/******************************************************************************
debug print functions
******************************************************************************/
static inline void
__print_iocb(struct opioctx *ctx, struct iocb *io, char *prefix)
{
char *type;
type = (io->aio_lio_opcode == IO_CMD_PREAD ? "read" : "write");
DBG(ctx, "%soff: %08llx, nbytes: %04lx, buf: %p, type: %s, data: %08lx,"
" optimized: %d\n", prefix, io->u.c.offset, io->u.c.nbytes,
io->u.c.buf, type, (unsigned long)io->data,
iocb_optimized(ctx, io));
}
static char *null_prefix = "";
#define print_iocb(ctx, io) __print_iocb(ctx, io, null_prefix)
static void
print_iocbs(struct opioctx *ctx, struct iocb **iocbs, int num_iocbs)
{
int i;
char pref[10];
struct iocb *io;
DBG(ctx, "iocbs:\n");
for (i = 0; i < num_iocbs; i++) {
io = iocbs[i];
snprintf(pref, 10, "%d: ", i);
__print_iocb(ctx, io, pref);
}
}
static void
print_optimized_iocbs(struct opioctx *ctx, struct opio *op, int *cnt)
{
char pref[10];
while (op) {
snprintf(pref, 10, " %d: ", (*cnt)++);
__print_iocb(ctx, op->iocb, pref);
op = op->next;
}
}
static void
print_merged_iocbs(struct opioctx *ctx, struct iocb **iocbs, int num_iocbs)
{
int i, cnt;
char pref[10];
struct iocb *io;
struct opio *op;
DBG(ctx, "merged iocbs:\n");
for (i = 0, cnt = 0; i < num_iocbs; i++) {
io = iocbs[i];
snprintf(pref, 10, "%d: ", cnt++);
__print_iocb(ctx, io, pref);
if (iocb_optimized(ctx, io)) {
op = (struct opio *)io->data;
print_optimized_iocbs(ctx, op->next, &cnt);
}
}
}
static void
print_events(struct opioctx *ctx, struct io_event *events, int num_events)
{
int i;
struct iocb *io;
for (i = 0; i < num_events; i++) {
io = events[i].obj;
print_iocb(ctx, io);
}
}
/******************************************************************************
end debug print functions
******************************************************************************/
#if defined(TEST)
#define hmask 0x80000000UL
#define smask 0x40000000UL
#define make_data(idx, is_head, sparse) \
(void *)((idx) | ((is_head) ? hmask : 0) | ((sparse) ? smask : 0))
#define data_idx(data) (int)((unsigned long)(data) & (0x0fffffff))
#define data_is_head(data) (((unsigned long)(data) & hmask) ? 1 : 0)
#define data_is_sparse(data) (((unsigned long)(data) & smask) ? 1 : 0)
static void
usage(void)
{
fprintf(stderr, "usage: io_optimize [-n num_runs] "
"[-i num_iocbs] [-s num_secs] [-r random_seed]\n");
exit(-1);
}
static int xalloc_cnt, xfree_cnt;
static inline char *
xalloc(int size)
{
char *buf = malloc(size);
if (!buf) {
fprintf(stderr, "xalloc failed\n");
exit(ENOMEM);
}
xalloc_cnt++;
return buf;
}
static inline void
xfree(void *buf)
{
free(buf);
xfree_cnt++;
}
static void
randomize_iocbs(struct iocb **iocbs, int num_iocbs, int num_secs)
{
int i, j;
i = 0;
while (i < num_iocbs) {
char *buf;
short type;
int segs, sparse_mem;
uint64_t offset, nbytes;
type = (random() % 10 < 5 ? IO_CMD_PREAD : IO_CMD_PWRITE);
offset = ((random() % num_secs) << 9);
if (random() % 10 < 4) {
segs = 1;
nbytes = (((random() % 7) + 1) << 9);
} else {
segs = (random() % 10) + 1;
nbytes = 4096;
}
if (i + segs > num_iocbs)
segs = (num_iocbs - i);
sparse_mem = (random() % 10 < 2 ? 1 : 0);
if (sparse_mem)
buf = xalloc(nbytes);
else
buf = xalloc(segs * nbytes);
for (j = 0; j < segs; j++) {
struct iocb *io = iocbs[i + j];
io->aio_lio_opcode = type;
io->u.c.nbytes = nbytes;
io->u.c.offset = offset;
io->u.c.buf = buf;
offset += nbytes;
io->data = make_data(i + j, (j == 0), sparse_mem);
if (j + 1 < segs && sparse_mem)
buf = xalloc(nbytes);
else
buf += nbytes;
}
i += segs;
}
}
static int
simulate_io(struct iocb **iocbs, struct io_event *events, int num_iocbs)
{
int i, done;
struct iocb *io;
struct io_event *ep;
if (num_iocbs > 1)
done = (random() % (num_iocbs - 1)) + 1;
else
done = num_iocbs;
for (i = 0; i < done; i++) {
io = iocbs[i];
ep = &events[i];
ep->obj = io;
ep->res = (random() % 10 < 8 ? io->u.c.nbytes : 0);
}
return done;
}
static inline void
process_events(struct opioctx *ctx,
struct iocb *iocb_list, struct io_event *events, int num)
{
int i;
struct iocb *io;
for (i = 0; i < num; i++) {
io = events[i].obj;
print_iocb(ctx, io);
if (data_idx(io->data) != (io - iocb_list)) {
printf("corrupt data! data_idx = %d, io = %d\n",
data_idx(io->data), (io - iocb_list));
exit(-1);
}
if (data_is_head(io->data) || data_is_sparse(io->data))
xfree(io->u.c.buf);
memset(io, 0, sizeof(struct iocb));
}
}
static inline void
init_optest(struct iocb *iocb_list,
struct iocb **iocbs, struct io_event *events, int num)
{
int i;
memset(iocb_list, 0, num * sizeof(struct iocb));
memset(events, 0, num * sizeof(struct io_event));
for (i = 0; i < num; i++)
iocbs[i] = &iocb_list[i];
}
int
main(int argc, char **argv)
{
uint64_t num_secs;
struct opioctx ctx;
struct io_event *events;
int i, c, num_runs, num_iocbs, seed;
struct iocb *iocb_list, **iocbs, **ioqueue;
num_runs = 1;
num_iocbs = 300;
seed = time(NULL);
num_secs = ((4ULL << 20) >> 9); /* 4GB disk */
while ((c = getopt(argc, argv, "n:i:s:r:h")) != -1) {
switch (c) {
case 'n':
num_runs = atoi(optarg);
break;
case 'i':
num_iocbs = atoi(optarg);
break;
case 's':
num_secs = strtoull(optarg, NULL, 10);
break;
case 'r':
seed = atoi(optarg);
break;
case 'h':
usage();
case '?':
fprintf(stderr, "Unrecognized option: -%c\n", optopt);
usage();
}
}
printf("Running %d tests with %d iocbs on %llu sectors, seed = %d\n",
num_runs, num_iocbs, num_secs, seed);
srand(seed);
iocb_list = malloc(num_iocbs * sizeof(struct iocb));
iocbs = malloc(num_iocbs * sizeof(struct iocb *));
events = malloc(num_iocbs * sizeof(struct io_event));
if (!iocb_list || !iocbs || !events || opio_init(&ctx, num_iocbs)) {
fprintf(stderr, "initialization failed\n");
exit(ENOMEM);
}
for (i = 0; i < num_runs; i++) {
int op_rem, op_done, num_split, num_events, num_done;
ioqueue = iocbs;
init_optest(iocb_list, ioqueue, events, num_iocbs);
randomize_iocbs(ioqueue, num_iocbs, num_secs);
print_iocbs(&ctx, ioqueue, num_iocbs);
op_done = 0;
num_done = 0;
op_rem = io_merge(&ctx, ioqueue, num_iocbs);
print_iocbs(&ctx, ioqueue, op_rem);
print_merged_iocbs(&ctx, ioqueue, op_rem);
while (num_done < num_iocbs) {
DBG(&ctx, "optimized remaining: %d\n", op_rem);
DBG(&ctx, "simulating\n");
num_events = simulate_io(ioqueue + op_done, events, op_rem);
print_events(&ctx, events, num_events);
DBG(&ctx, "splitting %d\n", num_events);
num_split = io_split(&ctx, events, num_events);
print_events(&ctx, events, num_split);
DBG(&ctx, "processing %d\n", num_split);
process_events(&ctx, iocb_list, events, num_split);
op_rem -= num_events;
op_done += num_events;
num_done += num_split;
}
DBG(&ctx, "run %d: processed: %d, xallocs: %d, xfrees: %d\n",
i, num_done, xalloc_cnt, xfree_cnt);
if (xalloc_cnt != xfree_cnt)
exit(-1);
xalloc_cnt = xfree_cnt = 0;
}
free(iocbs);
free(events);
free(iocb_list);
opio_free(&ctx);
return 0;
}
#endif
+68
View File
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __IO_OPTIMIZE_H__
#define __IO_OPTIMIZE_H__
#include <libaio.h>
struct opio;
struct opio_list {
struct opio *head;
struct opio *tail;
};
struct opio {
char *buf;
unsigned long nbytes;
long long offset;
void *data;
struct iocb *iocb;
struct io_event event;
struct opio *head;
struct opio *next;
struct opio_list list;
};
struct opioctx {
int num_opios;
int free_opio_cnt;
struct opio *opios;
struct opio **free_opios;
struct iocb **iocb_queue;
struct io_event *event_queue;
};
int opio_init(struct opioctx *ctx, int num_iocbs);
void opio_free(struct opioctx *ctx);
int io_merge(struct opioctx *ctx, struct iocb **queue, int num);
int io_split(struct opioctx *ctx, struct io_event *events, int num);
int io_expand_iocbs(struct opioctx *ctx, struct iocb **queue, int idx, int num);
#endif
+1000
View File
@@ -0,0 +1,1000 @@
/*
* Copyright (c) 2007, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This module implements a "dot locking" style advisory file locking algorithm.
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <dirent.h>
#include <limits.h>
#include "lock.h"
#define unlikely(x) __builtin_expect(!!(x), 0)
/* format: xenlk.hostname.uuid.<xf><rw>*/
#define LF_POSTFIX ".xenlk"
#define LFXL_FORMAT LF_POSTFIX ".%s.%s.x%s"
#define LFFL_FORMAT LF_POSTFIX ".%s.%s.f%s"
#define RETRY_MAX 16
#if defined(LOGS)
#define LOG(format, args...) printf("%d: ", __LINE__); printf(format, ## args)
#else
#define LOG(format, args...)
#endif
/* random wait - up to .5 seconds */
#define XSLEEP usleep(random() & 0x7ffff)
typedef int (*eval_func)(char *name, int readonly);
static char *create_lockfn(char *fn_to_lock)
{
char *lockfn;
/* allocate string to hold constructed lock file */
lockfn = malloc(strlen(fn_to_lock) + strlen(LF_POSTFIX) + 1);
if (unlikely(!lockfn)) {
return 0;
}
/* append postfix to file to lock */
strcpy(lockfn, fn_to_lock);
strcat(lockfn, LF_POSTFIX);
return lockfn;
}
static char *create_lockfn_link(char *fn_to_lock, char *format,
char *uuid, int readonly)
{
char hostname[128];
char *lockfn_link;
char *ptr;
/* get hostname */
if (unlikely(gethostname(hostname, sizeof(hostname)) == -1)) {
return 0;
}
/* allocate string to hold constructed lock file link */
lockfn_link = malloc(strlen(fn_to_lock) + strlen(LF_POSTFIX) +
strlen(hostname) + strlen(uuid) + 8);
if (unlikely(!lockfn_link)) {
return 0;
}
/* construct lock file link with specific format */
strcpy(lockfn_link, fn_to_lock);
ptr = lockfn_link + strlen(lockfn_link);
sprintf(ptr, format, hostname, uuid, readonly ? "r" : "w");
return lockfn_link;
}
static int NFSnormalizedStatTime(char *fn, struct stat *statnow, int *reterrno)
{
int result = LOCK_OK;
int uniq;
char *buf;
int fd;
int pid = (int)getpid();
int clstat;
*reterrno = 0;
/* create file to normalize time */
srandom((int)time(0) ^ pid);
uniq = random() % 0xffffff;
buf = malloc(strlen(fn) + 24);
if (unlikely(!buf)) { result = LOCK_ENOMEM; goto finish; }
strcpy(buf, fn);
sprintf(buf + strlen(buf), ".xen%08d.tmp", uniq);
fd = open(buf, O_WRONLY | O_CREAT, 0644);
if (fd == -1) { *reterrno = errno; result = LOCK_EOPEN; goto finish; }
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
if (lstat(buf, statnow) == -1) {
unlink(buf);
*reterrno = errno;
result = LOCK_ESTAT;
goto finish;
}
unlink(buf);
finish:
return result;
}
static int writer_eval(char *name, int readonly)
{
return name[strlen(name)-1] == 'w';
}
static int reader_eval(char *name, int readonly)
{
return name[strlen(name)-1] == 'r' && !readonly;
}
static int lock_holder(char *fn, char *lockfn, char *lockfn_link,
int force, int readonly, int *stole, eval_func eval,
int *elt, int *ioerror)
{
int status = 0;
int ustat;
DIR *pd = 0;
struct dirent *dptr;
char *ptr;
char *dirname = malloc(strlen(lockfn));
char *uname = malloc(strlen(lockfn_link) + 8);
int elt_established = 0;
int fd;
char tmpbuf[4096];
*stole = 0;
*ioerror = 0;
*elt = 0;
if (!dirname) goto finish;
if (!uname) goto finish;
/* get directory */
ptr = strrchr(lockfn, '/');
if (!ptr) {
strcpy(dirname, ".");
} else {
int numbytes = ptr - lockfn;
strncpy(dirname, lockfn, numbytes);
dirname[numbytes] = '\0';
}
pd = opendir(dirname);
if (!pd) {
*ioerror = errno ? errno : EIO;
goto finish;
}
/*
* scan through directory entries and use eval function
* if we have a match (i.e. reader or writer lock) but
* note that if we are forcing, we will remove any and
* all locks that appear for target of our lock, regardless
* if it a reader/writer owns the lock.
*/
errno = 0;
dptr = readdir(pd);
if (!dptr) {
*ioerror = EIO;
}
while (dptr) {
char *p1 = strrchr(fn, '/');
char *p2 = strrchr(lockfn, '/');
char *p3 = strrchr(lockfn_link, '/');
if (p1) p1+=1;
if (p2) p2+=1;
if (p3) p3+=1;
if (strcmp(dptr->d_name, p1 ? p1 : fn) &&
strcmp(dptr->d_name, p2 ? p2 : lockfn) &&
strcmp(dptr->d_name, p3 ? p3 : lockfn_link) &&
!strncmp(dptr->d_name, p1 ? p1 : fn, strlen(p1?p1:fn))) {
strcpy(uname, dirname);
strcat(uname, "/");
strcat(uname, dptr->d_name);
if (!elt_established) {
/* read final lock file and extract lease time */
fd = open(uname, O_RDONLY, 0644);
memset(tmpbuf, 0, sizeof(tmpbuf));
if (read(fd, tmpbuf, sizeof(tmpbuf)) < 0) {
*ioerror = errno;
status = 1;
close(fd);
goto finish;
}
close(fd);
ptr = strrchr(tmpbuf, '.');
if (ptr) {
*elt = atoi(ptr+1);
elt_established = 1;
}
}
if (force) {
ustat = unlink(uname);
if (ustat == -1) {
LOG("failed to unlink %s\n", uname);
}
*stole = 1;
*elt = 0;
} else {
if ((*eval)(dptr->d_name, readonly)) {
closedir(pd);
status = 1;
goto finish;
}
}
}
dptr = readdir(pd);
if (!dptr & errno) {
*ioerror = EIO;
}
}
closedir(pd);
finish:
free(dirname);
free(uname);
/* if IO error, force a taken status */
return (*ioerror) ? 1 : status;
}
int lock(char *fn_to_lock, char *uuid, int force, int readonly, int *lease_time, int *retstatus)
{
char *lockfn = 0;
char *lockfn_xlink = 0;
char *lockfn_flink = 0;
char *buf = 0;
int fd;
int status = 0;
struct stat stat1, stat2;
int retry_attempts = 0;
int clstat;
int tmpstat;
int stealx = 0;
int stealw = 0;
int stealr = 0;
int established_lease_time = 0;
char tmpbuf[4096];
int ioerr;
if (!fn_to_lock || !uuid) {
*retstatus = LOCK_EBADPARM;
return EINVAL;
}
*retstatus = 0;
/* seed random with time/pid combo */
srandom((int)time(0) ^ getpid());
/* build lock file strings */
lockfn = create_lockfn(fn_to_lock);
if (unlikely(!lockfn)) { status = ENOMEM; *retstatus = LOCK_ENOMEM; goto finish; }
lockfn_xlink = create_lockfn_link(fn_to_lock, LFXL_FORMAT,
uuid, readonly);
if (unlikely(!lockfn_xlink)) { status = ENOMEM; *retstatus = LOCK_ENOMEM; goto finish; }
lockfn_flink = create_lockfn_link(fn_to_lock, LFFL_FORMAT, uuid,
readonly);
if (unlikely(!lockfn_flink)) { status = ENOMEM; *retstatus = LOCK_ENOMEM; goto finish; }
try_again:
if (retry_attempts++ > RETRY_MAX) {
if (*retstatus == LOCK_EXLOCK_OPEN) {
struct stat statnow, stat_exlock;
int diff;
if (lstat(lockfn, &stat_exlock) == -1) {
goto finish;
}
if (NFSnormalizedStatTime(fn_to_lock, &statnow, &ioerr)) {
goto finish;
}
diff = (int)statnow.st_mtime - (int)stat_exlock.st_mtime;
if (diff > DEFAULT_LEASE_TIME_SECS) {
unlink(lockfn);
retry_attempts = 0;
goto try_again;
}
}
goto finish;
}
/* try to open exlusive lockfile */
fd = open(lockfn, O_WRONLY | O_CREAT | O_EXCL, 0644);
if (fd == -1) {
LOG("Initial lockfile creation failed %s force=%d, errno=%d\n",
lockfn, force, errno);
if (errno == EIO) {
*retstatus = LOCK_EXLOCK_OPEN;
status = EIO;
goto finish;
}
/* already owned? (hostname & uuid match, skip time bits) */
errno = 0;
fd = open(lockfn, O_RDWR, 0644);
if (fd != -1) {
buf = malloc(strlen(lockfn_xlink)+1);
if (!buf) {
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
*retstatus = LOCK_ENOMEM;
status = ENOMEM;
goto finish;
}
if (read(fd, buf, strlen(lockfn_xlink)) !=
(strlen(lockfn_xlink))) {
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
free(buf);
goto force_lock;
}
if (!strncmp(buf, lockfn_xlink, strlen(lockfn_xlink)-1)) {
LOG("lock owned by us, reasserting\n");
/* our lock, reassert by rewriting below */
if (lseek(fd, 0, SEEK_SET) == -1) {
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
goto force_lock;
}
free(buf);
goto skip;
}
free(buf);
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
}
force_lock:
if (errno == EIO) {
*retstatus = LOCK_EXLOCK_OPEN;
status = EIO;
goto finish;
}
if (force) {
/* remove lock file, we are forcing lock, try again */
status = unlink(lockfn);
if (unlikely(status == -1)) {
if (errno == EIO) {
*retstatus = LOCK_EXLOCK_OPEN;
status = EIO;
goto finish;
}
LOG("force removal of %s lockfile failed, "
"errno=%d, trying again\n", lockfn, errno);
}
stealx = 1;
}
XSLEEP;
*retstatus = LOCK_EXLOCK_OPEN;
goto try_again;
}
LOG("lockfile created %s\n", lockfn);
skip:
/*
* write into the temporary xlock
*/
if (write(fd, lockfn_xlink, strlen(lockfn_xlink)) !=
strlen(lockfn_xlink)) {
if (errno == EIO) {
*retstatus = LOCK_EXLOCK_WRITE;
status = EIO;
goto finish;
}
status = errno;
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
XSLEEP;
*retstatus = LOCK_EXLOCK_WRITE;
if (unlink(lockfn) == -1) {
LOG("removal of %s lockfile failed, "
"errno=%d, trying again\n", lockfn, errno);
}
goto try_again;
}
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
while (retry_attempts++ < RETRY_MAX) {
tmpstat = link(lockfn, lockfn_xlink);
LOG("linking %s and %s\n", lockfn, lockfn_xlink);
if ((tmpstat == -1) && (errno != EEXIST)) {
LOG("link status is %d, errno=%d\n", tmpstat, errno);
}
if ((lstat(lockfn, &stat1) == -1) ||
(lstat(lockfn_xlink, &stat2) == -1)) {
/* try again, cleanup first */
tmpstat = unlink(lockfn);
if (unlikely(tmpstat == -1)) {
LOG("error removing lock file %s", lockfn);
}
tmpstat = unlink(lockfn_xlink);
if (unlikely(tmpstat == -1)) {
LOG("error removing linked lock file %s",
lockfn_xlink);
}
XSLEEP;
status = LOCK_ESTAT;
goto finish;
}
/* compare inodes */
if (stat1.st_ino == stat2.st_ino) {
/* success, inodes are the same */
/* should we check that st_nlink's are also 2?? */
*retstatus = LOCK_OK;
status = 0;
tmpstat = unlink(lockfn_xlink);
if (unlikely(tmpstat == -1)) {
LOG("error removing linked lock file %s",
lockfn_xlink);
}
goto finish;
} else {
status = errno;
/* try again, cleanup first */
tmpstat = unlink(lockfn);
if (unlikely(tmpstat == -1)) {
LOG("error removing lock file %s", lockfn);
}
tmpstat = unlink(lockfn_xlink);
if (unlikely(tmpstat == -1)) {
LOG("error removing linked lock file %s",
lockfn_xlink);
}
XSLEEP;
*retstatus = LOCK_EINODE;
goto try_again;
}
}
finish:
if (!*retstatus) {
/* we have exclusive lock */
status = 0;
/* fast check, see if we own a final lock and are reasserting */
if (!lstat(lockfn_flink, &stat1)) {
char *ptr;
/* set the return value to notice this is a reassert */
*retstatus = 1;
/* read existing lock file and extract
established lease time */
fd = open(lockfn_flink, O_RDONLY, 0644);
memset(tmpbuf, 0, sizeof(tmpbuf));
if (read(fd, tmpbuf, sizeof(tmpbuf)) < 0) {
if (errno == EIO) {
close(fd);
*retstatus = LOCK_EINODE;
status = EIO;
goto skip_scan;
}
}
close(fd);
ptr = strrchr(tmpbuf, '.');
if (ptr) {
*lease_time = atoi(ptr+1);
} else {
*lease_time = 10; /* wkchack */
}
goto skip_scan;
} else {
if (errno == EIO) {
*retstatus = LOCK_EINODE;
status = EIO;
goto skip_scan;
}
}
/* we allow exclusive writer, or multiple readers */
if (lock_holder(fn_to_lock, lockfn, lockfn_flink, force,
readonly, &stealw, writer_eval,
&established_lease_time, &ioerr)) {
if (ioerr) {
*retstatus = LOCK_EREAD;
status = ioerr;
goto skip_scan;
}
*retstatus = LOCK_EHELD_WR;
} else if (lock_holder(fn_to_lock, lockfn, lockfn_flink, force,
readonly, &stealr, reader_eval,
&established_lease_time, &ioerr)) {
if (ioerr) {
*retstatus = LOCK_EREAD;
status = ioerr;
goto skip_scan;
}
*retstatus = LOCK_EHELD_RD;
}
if (established_lease_time) *lease_time =
established_lease_time;
}
skip_scan:
if (*retstatus >= 0) {
/* update file, changes last modify time */
fd = open(lockfn_flink, O_WRONLY | O_CREAT, 0644);
if (fd == -1) {
*retstatus = LOCK_EOPEN;
status = errno;
} else {
char tmpbuf[32];
int failed_write;
memset(tmpbuf, 0, sizeof(tmpbuf));
sprintf(tmpbuf, ".%d", *lease_time);
failed_write = write(fd, lockfn_flink,
strlen(lockfn_flink)) !=
strlen(lockfn_flink);
if (failed_write) status = errno;
failed_write |= write(fd, tmpbuf, strlen(tmpbuf)) !=
strlen(tmpbuf);
if (failed_write) status = errno;
if (failed_write) {
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
XSLEEP;
*retstatus = LOCK_EUPDATE;
goto try_again;
}
}
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
}
if (!*retstatus && force && (stealx || stealw || stealr)) {
struct timeval timeout;
/* enforce quiet time on steal */
timeout.tv_sec = *lease_time;
timeout.tv_usec = 0;
select(0, 0, 0, 0, &timeout);
}
/* remove exclusive lock, final read/write locks will hold */
tmpstat = unlink(lockfn);
if (unlikely(tmpstat == -1)) {
LOG("error removing exclusive lock file %s",
lockfn);
}
free(lockfn);
free(lockfn_xlink);
free(lockfn_flink);
/* set lease time to -1 if error, so no one is apt to use it */
if (*retstatus < 0) *lease_time = -1;
LOG("returning status %d, errno=%d\n", status, errno);
return status;
}
int unlock(char *fn_to_unlock, char *uuid, int readonly, int *status)
{
char *lockfn_link = 0;
int reterrno = 0;
if (!fn_to_unlock || !uuid) {
*status = LOCK_EBADPARM;
return 0;
}
lockfn_link = create_lockfn_link(fn_to_unlock, LFFL_FORMAT, uuid,
readonly);
if (unlikely(!lockfn_link)) { *status = LOCK_ENOMEM; goto finish; }
if (unlink(lockfn_link) == -1) {
LOG("error removing linked lock file %s", lockfn_link);
reterrno = errno;
*status = LOCK_ENOLOCK;
goto finish;
}
*status = LOCK_OK;
finish:
free(lockfn_link);
return reterrno;
}
int lock_delta(char *fn, int *ret_lease, int *max_lease)
{
int reterrno = 0;
DIR *pd = 0;
struct dirent *dptr;
char *ptr;
int result = INT_MAX;
struct stat statbuf, statnow;
char *dirname = malloc(strlen(fn));
char *uname = malloc(strlen(fn) + 8);
int elt_established = 0;
char *dotptr;
char tmpbuf[4096];
int fd;
if (!fn || !dirname || !uname) {
*ret_lease = LOCK_EBADPARM;
*max_lease = -1;
return 0;
}
if (NFSnormalizedStatTime(fn, &statnow, &reterrno)) {
result = LOCK_ESTAT;
goto finish;
}
/* get directory */
ptr = strrchr(fn, '/');
if (!ptr) {
strcpy(dirname, ".");
ptr = fn;
} else {
int numbytes = ptr - fn;
strncpy(dirname, fn, numbytes);
ptr += 1;
}
pd = opendir(dirname);
if (!pd) { reterrno = errno; goto finish; }
dptr = readdir(pd);
while (dptr) {
if (strcmp(dptr->d_name, ptr) &&
!strncmp(dptr->d_name, ptr, strlen(ptr))) {
char *fpath = malloc(strlen(dptr->d_name) +
strlen(dirname) + 2);
if (!fpath) {
closedir(pd);
result = LOCK_ENOMEM;
goto finish;
}
strcpy(fpath, dirname);
strcat(fpath, "/");
strcat(fpath, dptr->d_name);
if (lstat(fpath, &statbuf) != -1) {
int diff = (int)statnow.st_mtime -
(int)statbuf.st_mtime;
/* adjust diff if someone updated the lock
between now and when we created the "now"
file
*/
diff = (diff < 0) ? 0 : diff;
result = diff < result ? diff : result;
} else {
closedir(pd);
reterrno = errno;
goto finish;
}
if (!elt_established) {
/* read final lock file and extract lease time */
fd = open(fpath, O_RDONLY, 0644);
memset(tmpbuf, 0, sizeof(tmpbuf));
if (read(fd, tmpbuf, sizeof(tmpbuf)) < 0) {
/* error on read? */
}
close(fd);
dotptr = strrchr(tmpbuf, '.');
if (dotptr) {
*max_lease = atoi(dotptr+1);
elt_established = 1;
}
}
free(fpath);
}
dptr = readdir(pd);
}
closedir(pd);
finish:
free(dirname);
free(uname);
/* returns smallest lock time, or error */
if (result == INT_MAX) result = LOCK_ENOLOCK;
/* set lease time to -1 if error, so no one is apt to use it */
if ((result < 0) || reterrno) *max_lease = -1;
*ret_lease = result;
return reterrno;
}
#if defined(TEST)
/*
* the following is for sanity testing.
*/
static void usage(char *prg)
{
printf("usage %s\n"
" dtr <filename>]\n"
" p <filename> [num iterations]\n"
" u <filename> [0|1] [<uniqid>]\n"
" l <filename> [0|1] [0|1] [<uniqid>] [<leasetime>]\n", prg);
printf(" p : perf test lock take and reassert\n");
printf(" d : delta lock time\n");
printf(" t : test the file (after random locks)\n");
printf(" r : random lock tests (must ^C)\n");
printf(" u : unlock, readonly? uniqID (default is PID)\n");
printf(" l : lock, readonly? force?, uniqID (default is PID), lease time\n");
}
static void test_file(char *fn)
{
FILE *fptr;
int prev_count = 0;
int count, pid, time;
fptr = fopen(fn, "r");
if (!fptr) {
LOG("ERROR on file %s open, errno=%d\n", fn, errno);
return;
}
while (!feof(fptr)) {
fscanf(fptr, "%d %d %d\n", &count, &pid, &time);
if (prev_count != count) {
LOG("ERROR: prev_count=%d, count=%d, pid=%d, time=%d\n",
prev_count, count, pid, time);
}
prev_count = count + 1;
}
}
static void random_locks(char *fn)
{
int pid = getpid();
int status;
char *filebuf = malloc(256);
int count = 0;
int dummy;
int clstat;
char uuid[12];
int readonly;
int lease = DEFAULT_LEASE_TIME_SECS;
int err;
/* this will never return, kill to exit */
srandom((int)time(0) ^ pid);
LOG("pid: %d using file %s\n", pid, fn);
sprintf(uuid, "%08d", pid);
while (1) {
XSLEEP;
readonly = random() & 1;
sysstatus = lock(fn, uuid, 0, readonly, &lease, status);
if (status == LOCK_OK) {
/* got lock, open, read, modify write close file */
int fd = open(fn, O_RDWR, 0644);
if (fd == -1) {
LOG("pid: %d ERROR on file %s open, errno=%d\n",
pid, fn, errno);
} else {
if (!readonly) {
/* ugly code to read data in test format */
/* format is "%d %d %d" 'count pid time' */
struct stat statbuf;
int bytes;
status = stat(fn, &statbuf);
if (status != -1) {
if (statbuf.st_size > 256) {
lseek(fd, -256, SEEK_END);
}
memset(filebuf, 0, 256);
bytes = read(fd, filebuf, 256);
if (bytes) {
int bw = bytes-2;
while (bw && filebuf[bw]!='\n')
bw--;
if (!bw) bw = -1;
sscanf(&filebuf[bw+1],
"%d %d %d",
&count, &dummy, &dummy);
count += 1;
}
lseek(fd, 0, SEEK_END);
sprintf(filebuf, "%d %d %d\n",
count, pid, (int)time(0));
write(fd, filebuf, strlen(filebuf));
} else {
LOG("pid: %d ERROR on file %s stat, "
"errno=%d\n", pid, fn, errno);
}
}
clstat = close(fd);
if (unlikely(clstat == -1)) {
LOG("fail on close\n");
}
}
XSLEEP;
err = unlock(fn, uuid, readonly, &status);
LOG("unlock status is %d (err=%d)\n", status, err);
}
}
}
static void perf_lock(char *fn, int loops)
{
int sysstatus;
char buf[9];
int start = loops;
int lease = DEFAULT_LEASE_TIME_SECS;
sprintf(buf, "%08d", getpid());
while (loops--) {
sysstatus = lock(fn, buf, 0, 0, &lease, &status);
if (status < 0) {
printf("failed to get lock at iteration %d errno=%d\n",
start - loops, errno);
return;
}
}
unlock(fn, buf, 0, &status);
}
int main(int argc, char *argv[])
{
int status;
char *ptr;
char uuid[12];
int force;
int readonly;
int max_lease, cur_lease;
int intstatus;
int lease = DEFAULT_LEASE_TIME_SECS;
if (argc < 3) {
usage(argv[0]);
return 0;
}
sprintf(uuid, "%08d", getpid());
ptr = uuid;
if (!strcmp(argv[1],"d")) {
status = lock_delta(argv[2], &cur_lease, &max_lease);
printf("lock delta for %s is %d seconds, max lease is %d\n",
argv[2], cur_lease, max_lease);
} else if (!strcmp(argv[1],"t")) {
test_file(argv[2]);
} else if (!strcmp(argv[1],"r")) {
random_locks(argv[2]);
} else if (!strcmp(argv[1],"p")) {
perf_lock(argv[2], argc < 3 ? 100000 : atoi(argv[3]));
} else if (!strcmp(argv[1],"l")) {
if (argc < 4) force = 0; else force = atoi(argv[3]);
if (argc < 5) readonly = 0; else readonly = atoi(argv[4]);
if (argc >= 6) ptr = argv[5];
if (argc == 7) lease = atoi(argv[6]);
status = lock(argv[2], ptr, readonly, force, &lease, &intstatus);
printf("lock status = %d\n", status);
} else if (!strcmp(argv[1],"u") ) {
if (argc < 5) readonly = 0; else readonly = atoi(argv[3]);
if (argc == 5) ptr = argv[4];
status = unlock(argv[2], ptr, readonly, &intstatus);
printf("unlock status = %d\n", intstatus);
} else {
usage(argv[0]);
}
return status;
}
#elif defined(UTIL)
/*
* the following is used for non-libary, standalone
* program utility as a shell program
*/
static void usage(char *prg)
{
printf("usage %s\n"
" delta <filename>\n"
" unlock <filename> <r|w> <uniqid>\n"
" lock <filename> <r|w> <0|1> <uniqid> <leasetime>\n", prg);
printf(" delta : get time since lock last refreshed\n");
printf(" returns delta time and max lease time in seconds\n");
printf(" unlock: unlock request filename, r|w, uniqID\n");
printf(" returns status (success is 0)\n");
printf(" lock : lock request filename, r|w, force?, uniqID, lease time request\n");
printf(" returns status (success is 0) and established lease time in seconds\n");
}
int main(int argc, char *argv[])
{
int status = 0;
int dlock;
char *ptr;
int force;
int readonly;
int cur_lease, max_lease, intstatus;
int lease = DEFAULT_LEASE_TIME_SECS;
if (argc < 3) {
if (argc == 2 && !strcmp(argv[1], "-h")) {
usage(argv[0]);
} else {
printf("%d\n", LOCK_EUSAGE);
}
return 0;
}
if (!strcmp(argv[1],"delta") && (argc == 3)) {
status = lock_delta(argv[2], &cur_lease, &max_lease);
printf("%d %d\n", cur_lease, max_lease);
} else if (!strcmp(argv[1],"lock") && (argc == 7)) {
readonly = (strcmp(argv[3], "r") == 0) ? 1 : 0;
force = atoi(argv[4]);
ptr = argv[5];
lease = atoi(argv[6]);
status = lock(argv[2], ptr, force, readonly, &lease, &intstatus);
printf("%d %d\n", intstatus, lease);
} else if (!strcmp(argv[1],"unlock") && (argc == 5)) {
readonly = (strcmp(argv[3], "r") == 0) ? 1 : 0;
ptr = argv[4];
status = unlock(argv[2], ptr, readonly, &intstatus);
printf("%d\n", intstatus);
} else {
printf("%d\n", LOCK_EUSAGE);
}
/* this is either 0 or a system defined errno */
return status;
}
#endif
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2007, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define DEFAULT_LEASE_TIME_SECS 30
int lock(char *fn_to_lock, char *uuid, int force, int readonly, int *lease_time, int *retstat);
int unlock(char *fn_to_unlock, char *uuid, int readonly, int *retstat);
int lock_delta(char *fn_to_check, int *cur_lease_time, int *max_lease_time);
typedef enum {
LOCK_OK = 0,
LOCK_EBADPARM = -1,
LOCK_ENOMEM = -2,
LOCK_ESTAT = -3,
LOCK_EHELD_WR = -4,
LOCK_EHELD_RD = -5,
LOCK_EOPEN = -6,
LOCK_EXLOCK_OPEN = -7,
LOCK_EXLOCK_WRITE= -8,
LOCK_EINODE = -9,
LOCK_EUPDATE = -10,
LOCK_EREAD = -11,
LOCK_EREMOVE = -12,
LOCK_ENOLOCK = -13,
LOCK_EUSAGE = -14,
} lock_error;
+123
View File
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* log.h: API for writelog communication */
#ifndef __LOG_H__
#define __LOG_H__ 1
#include <inttypes.h>
#include <xen/io/ring.h>
/* for wmb et al */
#include <xenctrl.h>
#define LOGCMD_SHMP "shmp"
#define LOGCMD_PEEK "peek"
#define LOGCMD_CLEAR "clrw"
#define LOGCMD_GET "getw"
#define LOGCMD_KICK "kick"
#define CTLRSPLEN_SHMP 256
#define CTLRSPLEN_PEEK 4
#define CTLRSPLEN_CLEAR 4
#define CTLRSPLEN_GET 4
#define CTLRSPLEN_KICK 0
/* shmregion is arbitrarily capped at 8 megs for a minimum of
* 64 MB of data per read (if there are no contiguous regions)
* In the off-chance that there is more dirty data, multiple
* reads must be done */
#define SHMSIZE (8 * 1024 * 1024)
#define SRINGSIZE 4096
/* The shared memory region is split up into 3 subregions:
* The first half is reserved for the dirty bitmap log.
* The second half begins with 1 page for read request descriptors,
* followed by a big area for supplying read data.
*/
static inline void* bmstart(void* shm)
{
return shm;
}
static inline void* bmend(void* shm)
{
return shm + SHMSIZE/2;
}
static inline void* sringstart(void* shm)
{
return bmend(shm);
}
static inline void* sdatastart(void* shm)
{
return sringstart(shm) + SRINGSIZE;
}
static inline void* sdataend(void* shm)
{
return shm + SHMSIZE;
}
/* format for messages between log client and server */
struct log_ctlmsg {
char msg[4];
char params[16];
};
/* extent descriptor */
struct disk_range {
uint64_t sector;
uint32_t count;
};
/* dirty write logging space. This is an extent ring at the front,
* full of disk_ranges plus a pointer into the data area */
/* I think I'd rather have the header in front of each data section to
* avoid having two separate spaces that can run out, but then I'd either
* lose page alignment on the data blocks or spend an entire page on the
* header */
struct log_extent {
uint64_t sector;
uint32_t count;
uint32_t offset; /* offset from start of data area to start of extent */
};
/* struct above should be 16 bytes, or 256 extents/page */
typedef struct log_extent log_request_t;
typedef struct log_extent log_response_t;
DEFINE_RING_TYPES(log, log_request_t, log_response_t);
#define LOG_HEADER_PAGES 4
#endif
+191
View File
@@ -0,0 +1,191 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __TAP_PROFILE_H__
#define __TAP_PROFILE_H__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <inttypes.h>
//#define PROFILING
//#define LOGGING
#define TAPPROF_IN 1
#define TAPPROF_OUT 2
struct profile_times {
char *fn_name;
uint64_t in, out_sum, cnt;
};
struct profile_info {
FILE *log;
int size;
char *name;
unsigned long long seq;
struct profile_times *pt;
};
#ifdef PROFILING
static inline void
tp_open(struct profile_info *prof, char *tap_name, char *log_name, int size)
{
memset(prof, 0, sizeof(struct profile_info));
#ifdef LOGGING
prof->log = fopen(log_name, "w");
#endif
prof->size = size;
prof->name = strdup(tap_name);
prof->pt = malloc(sizeof(struct profile_times) * prof->size);
if (prof->pt)
memset(prof->pt, 0, sizeof(struct profile_times) * prof->size);
}
static inline void
tp_close(struct profile_info *prof)
{
int i;
struct profile_times *pt;
for (i = 0; i < prof->size; i++) {
pt = &prof->pt[i];
if (pt->fn_name) {
syslog(LOG_DEBUG, "%s: %s: cnt: %llu, avg time: %llu\n",
prof->name, pt->fn_name, pt->cnt,
((pt->cnt) ? (pt->out_sum / pt->cnt) : 0));
free(pt->fn_name);
}
}
#ifdef LOGGING
if (prof->log)
fclose(prof->log);
#endif
free(prof->name);
if (prof->pt)
free(prof->pt);
}
static inline u64
tp_get_id(struct profile_info *prof)
{
return prof->seq++;
}
static inline int
tp_fn_id(struct profile_info *prof, const char *name)
{
int i;
struct profile_times *pt;
for (i = 0; i < prof->size; i++) {
pt = &prof->pt[i];
if (!pt->fn_name)
return i;
if (!strcmp(pt->fn_name, name))
return i;
}
return prof->size - 1;
}
static inline void
__tp_in(struct profile_info *prof, const char *func)
{
long long _time;
int idx = tp_fn_id(prof, func);
struct profile_times *pt = &prof->pt[idx];
if (!pt->fn_name)
pt->fn_name = strdup(func);
asm volatile(".byte 0x0f, 0x31" : "=A" (_time));
pt->in = _time;
}
#define tp_in(prof) __tp_in(prof, __func__)
static inline void
__tp_out(struct profile_info *prof, const char *func)
{
long long _time;
int idx = tp_fn_id(prof, func);
struct profile_times *pt = &prof->pt[idx];
if (!pt->fn_name || !pt->in)
return;
asm volatile(".byte 0x0f, 0x31" : "=A" (_time));
pt->cnt++;
pt->out_sum += (_time - pt->in);
pt->in = 0;
}
#define tp_out(prof) __tp_out(prof, __func__)
static inline void
__tp_log(struct profile_info *prof, u64 id, const char *func, int direction)
{
long long _time;
asm volatile(".byte 0x0f, 0x31" : "=A" (_time));
if (direction == TAPPROF_IN)
__tp_in(prof, func);
else
__tp_out(prof, func);
#ifdef LOGGING
if (prof->log)
fprintf(prof->log, "%s: %s: %llu, %lld\n", func,
((direction == TAPPROF_IN) ? "in" : "out"), id, _time);
#endif
}
#define tp_log(prof, id, direction) __tp_log(prof, id, __func__, direction)
#else
#define tp_open(prof, tname, lname, size) ((void)0)
#define tp_close(prof) ((void)0)
#define tp_in(prof) ((void)0)
#define tp_out(prof) ((void)0)
#define tp_log(prof, sec, direction) ((void)0)
#endif
#endif
+121
View File
@@ -0,0 +1,121 @@
/* qcow-create.c
*
* Generates a qcow format disk.
*
* (c) 2006 Andrew Warfield and Julian Chesterfield
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include "tapdisk.h"
#include "qcow.h"
#if 1
#define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
#else
#define DFPRINTF(_f, _a...) ((void)0)
#endif
#define MAX_NAME_LEN 1000
void help(void)
{
fprintf(stderr, "Qcow-utils: v1.0.0\n");
fprintf(stderr,
"usage: qcow-create [-h help] [-r reserve] <SIZE(MB)> <FILENAME> "
"[<BACKING_FILENAME>]\n");
exit(-1);
}
int main(int argc, char *argv[])
{
int ret = -1, c, backed = 0;
int sparse = 1;
uint64_t size;
char filename[MAX_NAME_LEN], bfilename[MAX_NAME_LEN];
for(;;) {
c = getopt(argc, argv, "hr");
if (c == -1)
break;
switch(c) {
case 'h':
help();
exit(0);
break;
case 'r':
sparse = 0;
break;
default:
fprintf(stderr, "Unknown option\n");
help();
}
}
printf("Optind %d, argc %d\n", optind, argc);
if ( !(optind == (argc - 2) || optind == (argc - 3)) )
help();
size = atoi(argv[optind++]);
size = size << 20;
if (snprintf(filename, MAX_NAME_LEN, "%s",argv[optind++]) >=
MAX_NAME_LEN) {
fprintf(stderr,"Device name too long\n");
exit(-1);
}
if (optind != argc) {
/*Backing file argument*/
backed = 1;
if (snprintf(bfilename, MAX_NAME_LEN, "%s",argv[optind++]) >=
MAX_NAME_LEN) {
fprintf(stderr,"Device name too long\n");
exit(-1);
}
}
DFPRINTF("Creating file size %"PRIu64", name %s\n",(uint64_t)size, filename);
if (!backed)
ret = qcow_create(filename,size,NULL,sparse);
else
ret = qcow_create(filename,size,bfilename,sparse);
if (ret < 0)
DPRINTF("Unable to create QCOW file\n");
else
DPRINTF("QCOW file successfully created\n");
return 0;
}
+131
View File
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _QCOW_H_
#define _QCOW_H_
#include "aes.h"
/**************************************************************/
/* QEMU COW block driver with compression and encryption support */
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
#define XEN_MAGIC (('X' << 24) | ('E' << 16) | ('N' << 8) | 0xfb)
#define QCOW_VERSION 1
#define QCOW_CRYPT_NONE 0x00
#define QCOW_CRYPT_AES 0x01
#define QCOW_OFLAG_COMPRESSED (1LL << 63)
#define SPARSE_FILE 0x01
#define EXTHDR_L1_BIG_ENDIAN 0x02
#ifndef O_BINARY
#define O_BINARY 0
#endif
#define ROUNDUP(l, s) \
({ \
(uint64_t)( \
(l + (s - 1)) - ((l + (s - 1)) % s)); \
})
typedef struct QCowHeader {
uint32_t magic;
uint32_t version;
uint64_t backing_file_offset;
uint32_t backing_file_size;
uint32_t mtime;
uint64_t size; /* in bytes */
uint8_t cluster_bits;
uint8_t l2_bits;
uint32_t crypt_method;
uint64_t l1_table_offset;
} QCowHeader;
/*Extended header for Xen enhancements*/
typedef struct QCowHeader_ext {
uint32_t xmagic;
uint32_t cksum;
uint32_t min_cluster_alloc;
uint32_t flags;
} QCowHeader_ext;
uint32_t gen_cksum(char *ptr, int len);
int get_filesize(char *filename, uint64_t *size, struct stat *st);
int qtruncate(int fd, off_t length, int sparse);
#define L2_CACHE_SIZE 16 /*Fixed allocation in Qemu*/
struct tdqcow_state {
int fd; /*Main Qcow file descriptor */
uint64_t fd_end; /*Store a local record of file length */
char *name; /*Record of the filename*/
uint32_t backing_file_size;
uint64_t backing_file_offset;
uint8_t extended; /*File contains extended header*/
int encrypted; /*File contents are encrypted or plain*/
int cluster_bits; /*Determines length of cluster as
*indicated by file hdr*/
int cluster_size; /*Length of cluster*/
int cluster_sectors; /*Number of sectors per cluster*/
int cluster_alloc; /*Blktap fix for allocating full
*extents*/
int min_cluster_alloc; /*Blktap historical extent alloc*/
int sparse; /*Indicates whether to preserve sparseness*/
int l2_bits; /*Size of L2 table entry*/
int l2_size; /*Full table size*/
int l1_size; /*L1 table size*/
uint64_t cluster_offset_mask;
uint64_t l1_table_offset; /*L1 table offset from beginning of
*file*/
uint64_t *l1_table; /*L1 table entries*/
uint64_t *l2_cache; /*We maintain a cache of size
*L2_CACHE_SIZE of most read entries*/
uint64_t l2_cache_offsets[L2_CACHE_SIZE]; /*L2 cache entries*/
uint32_t l2_cache_counts[L2_CACHE_SIZE]; /*Cache access record*/
uint8_t *cluster_cache;
uint8_t *cluster_data;
uint64_t cluster_cache_offset; /**/
uint32_t crypt_method; /*current crypt method, 0 if no
*key yet */
uint32_t crypt_method_header; /**/
AES_KEY aes_encrypt_key; /*AES key*/
AES_KEY aes_decrypt_key; /*AES key*/
/* libaio state */
int aio_free_count;
int max_aio_reqs;
struct qcow_request *aio_requests;
struct qcow_request **aio_free_list;
};
int qcow_create(const char *filename, uint64_t total_size,
const char *backing_file, int sparse);
#endif //_QCOW_H_
+449
View File
@@ -0,0 +1,449 @@
/* qcow2raw.c
*
* Generates raw image data from an existing qcow image
*
* (c) 2006 Julian Chesterfield and Andrew Warfield
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation; or, when distributed
* separately from the Linux kernel or incorporated into other
* software packages, subject to the following license:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include "bswap.h"
#include "aes.h"
#include "blk.h"
#include "tapdisk.h"
#include "tapdisk-server.h"
#include "tapdisk-driver.h"
#include "tapdisk-interface.h"
#include "qcow.h"
#if 1
#define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
#else
#define DFPRINTF(_f, _a...) ((void)0)
#endif
/* *BSD has no O_LARGEFILE */
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#define TAPDISK 1
#define BLOCK_PROCESSSZ 4096
#define QCOW_VBD 0
#define AIO_VBD 1
#define WINDOW 32
#define PROGRESS_QUANT 2
static int running = 1, complete = 0;
static int returned_read_events = 0, returned_write_events = 0;
static int submit_events = 0;
static uint32_t read_idx = 0;
td_driver_t *ddqcow, *ddaio;
td_vbd_t* qcow_vbd, *aio_vbd;
static uint64_t prev = 0, written = 0;
static char output[(100/PROGRESS_QUANT) + 5];
extern tapdisk_server_t server;
struct request_info {
void* buf;
uint64_t logical_sec;
int pending;
};
static void print_bytes(void *ptr, int length)
{
int i,k;
unsigned char *p = ptr;
DFPRINTF("Buf dump, length %d:\n",length);
for (k = 0; k < length; k++) {
DFPRINTF("%x",*p);
*p++;
if (k % 16 == 0) DFPRINTF("\n");
else if (k % 2 == 0) DFPRINTF(" ");
}
DFPRINTF("\n");
return;
}
void
queue_event(event_id_t id, char mode, void *private)
{
tapdisk_complete_tiocbs(&server.aio_queue);
}
static void debug_output(uint64_t progress, uint64_t size)
{
//Output progress every PROGRESS_QUANT
uint64_t blocks = size/(100/PROGRESS_QUANT);
if (progress/blocks > prev) {
memcpy(output+prev+1,"=>",2);
prev++;
DFPRINTF("\r%s %"PRIu64"%%",
output, (uint64_t)((prev-1)*PROGRESS_QUANT));
}
return;
}
static void send_write_responses(td_request_t treq, int err)
{
struct request_info* req;
if (err < 0) {
DFPRINTF("AIO FAILURE: res [%d]!\n",err);
return;
}
returned_write_events+=treq.secs;
written += treq.secs;
req= (struct request_info*)treq.cb_data;
//Wait for whole request to complete.
req->pending-=treq.secs;
if(req->pending)
return;
//Whole request has completed, we can free buffers.
free(req->buf);
free(req);
debug_output(written, ddaio->info.size);
return;
}
static void send_read_responses(td_request_t treq, int err)
{
int ret;
struct request_info* req;
td_vbd_request_t* vreq;
if (err < 0) {
DFPRINTF("AIO FAILURE: res [%d]!\n",err);
return;
}
returned_read_events+=treq.secs;
req= (struct request_info*)treq.cb_data;
//do nothing until all fragments complete.
req->pending-=treq.secs;
if(req->pending)
return;
//This read is done.
tapdisk_vbd_complete_vbd_request(qcow_vbd, treq.private);
treq.op = TD_OP_WRITE;
treq.buf = req->buf;
treq.sec = req->logical_sec;
treq.secs = BLOCK_PROCESSSZ>>9;
treq.image = tapdisk_vbd_first_image(aio_vbd);
treq.cb = send_write_responses;
treq.id = 0;
treq.sidx = 0;
req->pending = BLOCK_PROCESSSZ>>9;
treq.cb_data = req;
vreq = calloc(1, sizeof(td_vbd_request_t));
treq.private = vreq;
//Put it in the VBD's queue, so we don't lose
//track of it.
vreq->submitting = 1;
INIT_LIST_HEAD(&vreq->next);
tapdisk_vbd_move_request(treq.private,
&aio_vbd->pending_requests);
ddaio->ops->td_queue_write(ddaio,treq);
--vreq->submitting;
tapdisk_submit_all_tiocbs(&server.aio_queue);
return;
}
int main(int argc, const char *argv[])
{
int ret = -1, fd, len,input;
uint64_t size;
struct timeval timeout;
uint64_t i;
char *buf;
struct stat finfo;
td_request_t treq;
td_vbd_request_t* vreq;
struct request_info* req;
int err;
if (argc != 3) {
fprintf(stderr, "Qcow-utils: v1.0.0\n");
fprintf(stderr, "usage: %s <Dest File descriptor> "
"<Qcow SRC IMAGE>\n",
argv[0]);
exit(-1);
}
err = tapdisk_server_initialize(NULL, NULL);
if( err ) {
DPRINTF("qcow2raw Couldn't initialize server instance.\n");
return err;
}
err=tapdisk_vbd_initialize(-1,-1, QCOW_VBD);
if( err ) {
DPRINTF("qcow2raw Couldn't initialize qcow vbd.\n");
return err;
}
qcow_vbd = tapdisk_server_get_vbd(QCOW_VBD);
if (!qcow_vbd) {
err = -ENODEV;
DPRINTF("qcow2raw Couldn't create qcow vbd.\n");
return err;
}
err = tapdisk_vbd_open_vdi(qcow_vbd, argv[2], DISK_TYPE_QCOW,
TAPDISK_STORAGE_TYPE_DEFAULT,
TD_OPEN_RDONLY);
if( err ) {
DPRINTF("qcow2raw Couldn't open qcow file.\n");
return err;
}
ddqcow=(tapdisk_vbd_first_image(qcow_vbd))->driver;
/*Setup aio destination file*/
ret = stat(argv[1],&finfo);
if (ret == -1) {
/*Check errno*/
switch(errno) {
case ENOENT:
/*File doesn't exist, create*/
fd = open(argv[1],
O_RDWR | O_LARGEFILE | O_CREAT, 0644);
if (fd < 0) {
DFPRINTF("ERROR creating file [%s] "
"(errno %d)\n",
argv[1], 0 - errno);
exit(-1);
}
if (ftruncate(fd, (off_t)ddqcow->info.size<<9) < 0) {
DFPRINTF("Unable to create file "
"[%s] of size %"PRIu64" (errno %d). "
"Exiting...\n",
argv[1],
(uint64_t)ddqcow->info.size<<9,
0 - errno);
close(fd);
exit(-1);
}
close(fd);
break;
case ENXIO:
DFPRINTF("ERROR Device [%s] does not exist\n",argv[1]);
exit(-1);
default:
DFPRINTF("An error occurred opening Device [%s] "
"(errno %d)\n",
argv[1], 0 - errno);
exit(-1);
}
} else {
fprintf(stderr, "WARNING: All existing data in "
"%s will be overwritten.\nDo you wish to continue? "
"(y or n) ",
argv[1]);
if (getchar() != 'y') {
DFPRINTF("Exiting...\n");
exit(-1);
}
/*TODO - Test the existing file or device for adequate space*/
fd = open(argv[1], O_RDWR | O_LARGEFILE);
if (fd < 0) {
DFPRINTF("ERROR: opening file [%s] (errno %d)\n",
argv[1], 0 - errno);
exit(-1);
}
if (S_ISBLK(finfo.st_mode)) {
if (blk_getimagesize(fd, &size) != 0) {
close(fd);
return -1;
}
if (size < ddqcow->info.size<<9) {
DFPRINTF("ERROR: Not enough space on device "
"%s (%"PRIu64" bytes available, "
"%"PRIu64" bytes required\n",
argv[1], size,
(uint64_t)ddqcow->info.size<<9);
close(fd);
exit(-1);
}
} else {
if (ftruncate(fd, (off_t)ddqcow->info.size<<9) < 0) {
DFPRINTF("Unable to create file "
"[%s] of size %"PRIu64" (errno %d). "
"Exiting...\n",
argv[1],
(uint64_t)ddqcow->info.size<<9,
0 - errno);
close(fd);
exit(-1);
} else DFPRINTF("File [%s] truncated to length %"PRIu64" "
"(%"PRIu64")\n",
argv[1],
(uint64_t)ddqcow->info.size<<9,
(uint64_t)ddqcow->info.size);
}
close(fd);
}
//Now the output file should be there, reopen it as an aio VBD
err=tapdisk_vbd_initialize(-1,-1, AIO_VBD);
if( err ) {
DPRINTF("qcow2raw Couldn't initialize aio vbd.\n");
return err;
}
aio_vbd = tapdisk_server_get_vbd(AIO_VBD);
if (!aio_vbd) {
err = -ENODEV;
DPRINTF("qcow2raw Couldn't create aio vbd.\n");
return err;
}
err = tapdisk_vbd_open_vdi(aio_vbd, argv[1], DISK_TYPE_AIO,
TAPDISK_STORAGE_TYPE_DEFAULT,
0);
if( err ) {
DPRINTF("qcow2raw Couldn't open aio file.\n");
return err;
}
ddaio=(tapdisk_vbd_first_image(aio_vbd))->driver;
/*Initialise the output string*/
memset(output,0x20,(100/PROGRESS_QUANT)+5);
output[0] = '[';
output[(100/PROGRESS_QUANT)+2] = ']';
output[(100/PROGRESS_QUANT)+3] = '\0';
DFPRINTF("%s",output);
i = 0;
while (running) {
timeout.tv_sec = 0;
if (!complete) {
/*Read Pages from qcow image*/
if ( (ret = posix_memalign((void **)&buf,
BLOCK_PROCESSSZ,
BLOCK_PROCESSSZ))
!= 0) {
DFPRINTF("Unable to alloc memory (%d)\n",ret);
exit(-1);
}
/*Attempt to read 4k sized blocks*/
submit_events+=BLOCK_PROCESSSZ>>9;
//Set up the read request
treq.op = TD_OP_READ;
treq.buf = buf;
treq.sec = i;
treq.secs = BLOCK_PROCESSSZ>>9;
treq.image = tapdisk_vbd_first_image(qcow_vbd);
treq.cb = send_read_responses;
treq.id = 0;
treq.sidx = 0;
req = calloc(1, sizeof(struct request_info));
req->buf = buf;
req->logical_sec = i;
req->pending = BLOCK_PROCESSSZ>>9;
treq.cb_data = req;
vreq = calloc(1, sizeof(td_vbd_request_t));
treq.private = vreq;
//Put it in the VBD's queue, so we don't lose
//track of it.
vreq->submitting = 1;
INIT_LIST_HEAD(&vreq->next);
tapdisk_vbd_move_request(treq.private,
&qcow_vbd->pending_requests);
ddqcow->ops->td_queue_read(ddqcow, treq);
--vreq->submitting;
i += BLOCK_PROCESSSZ>>9;
if (i >= ddqcow->info.size)
complete = 1;
tapdisk_submit_all_tiocbs(&server.aio_queue);
}
while(returned_write_events != submit_events) {
ret = scheduler_wait_for_events(&server.scheduler);
if (ret < 0) {
DFPRINTF("server wait returned %d\n", ret);
sleep(2);
}
}
if (complete && (returned_write_events == submit_events))
running = 0;
}
memcpy(output+prev+1,"=",1);
DFPRINTF("\r%s 100%%\nTRANSFER COMPLETE\n\n", output);
ddqcow->ops->td_close(ddqcow);
ddaio->ops->td_close(ddaio);
free(ddqcow->data);
free(ddaio->data);
return 0;
}
+265
View File
@@ -0,0 +1,265 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include "scheduler.h"
#include "tapdisk-log.h"
#define DBG(_f, _a...) tlog_write(TLOG_DBG, _f, ##_a)
#define SCHEDULER_MAX_TIMEOUT 600
#define SCHEDULER_POLL_FD (SCHEDULER_POLL_READ_FD | \
SCHEDULER_POLL_WRITE_FD | \
SCHEDULER_POLL_EXCEPT_FD)
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define scheduler_for_each_event(s, event, tmp) \
list_for_each_entry_safe(event, tmp, &(s)->events, next)
typedef struct event {
char mode;
event_id_t id;
int fd;
int timeout;
int deadline;
event_cb_t cb;
void *private;
struct list_head next;
} event_t;
static void
scheduler_prepare_events(scheduler_t *s)
{
int diff;
struct timeval now;
event_t *event, *tmp;
FD_ZERO(&s->read_fds);
FD_ZERO(&s->write_fds);
FD_ZERO(&s->except_fds);
s->max_fd = 0;
s->timeout = SCHEDULER_MAX_TIMEOUT;
gettimeofday(&now, NULL);
scheduler_for_each_event(s, event, tmp) {
if (event->mode & SCHEDULER_POLL_READ_FD) {
FD_SET(event->fd, &s->read_fds);
s->max_fd = MAX(event->fd, s->max_fd);
}
if (event->mode & SCHEDULER_POLL_WRITE_FD) {
FD_SET(event->fd, &s->write_fds);
s->max_fd = MAX(event->fd, s->max_fd);
}
if (event->mode & SCHEDULER_POLL_EXCEPT_FD) {
FD_SET(event->fd, &s->except_fds);
s->max_fd = MAX(event->fd, s->max_fd);
}
if (event->mode & SCHEDULER_POLL_TIMEOUT) {
diff = event->deadline - now.tv_sec;
if (diff > 0)
s->timeout = MIN(s->timeout, diff);
else
s->timeout = 0;
}
}
s->timeout = MIN(s->timeout, s->max_timeout);
}
static void
scheduler_event_callback(event_t *event, char mode)
{
if (event->mode & SCHEDULER_POLL_TIMEOUT) {
struct timeval now;
gettimeofday(&now, NULL);
event->deadline = now.tv_sec + event->timeout;
}
event->cb(event->id, mode, event->private);
}
static void
scheduler_run_events(scheduler_t *s)
{
struct timeval now;
event_t *event, *tmp;
gettimeofday(&now, NULL);
again:
s->restart = 0;
scheduler_for_each_event(s, event, tmp) {
if ((event->mode & SCHEDULER_POLL_READ_FD) &&
FD_ISSET(event->fd, &s->read_fds)) {
FD_CLR(event->fd, &s->read_fds);
scheduler_event_callback(event, SCHEDULER_POLL_READ_FD);
goto next;
}
if ((event->mode & SCHEDULER_POLL_WRITE_FD) &&
FD_ISSET(event->fd, &s->write_fds)) {
FD_CLR(event->fd, &s->write_fds);
scheduler_event_callback(event, SCHEDULER_POLL_WRITE_FD);
goto next;
}
if ((event->mode & SCHEDULER_POLL_EXCEPT_FD) &&
FD_ISSET(event->fd, &s->except_fds)) {
FD_CLR(event->fd, &s->except_fds);
scheduler_event_callback(event, SCHEDULER_POLL_EXCEPT_FD);
goto next;
}
if ((event->mode & SCHEDULER_POLL_TIMEOUT) &&
(event->deadline <= now.tv_sec))
scheduler_event_callback(event, SCHEDULER_POLL_TIMEOUT);
next:
if (s->restart)
goto again;
}
}
int
scheduler_register_event(scheduler_t *s, char mode, int fd,
int timeout, event_cb_t cb, void *private)
{
event_t *event;
struct timeval now;
if (!cb)
return -EINVAL;
if (!(mode & SCHEDULER_POLL_TIMEOUT) && !(mode & SCHEDULER_POLL_FD))
return -EINVAL;
event = calloc(1, sizeof(event_t));
if (!event)
return -ENOMEM;
gettimeofday(&now, NULL);
INIT_LIST_HEAD(&event->next);
event->mode = mode;
event->fd = fd;
event->timeout = timeout;
event->deadline = now.tv_sec + timeout;
event->cb = cb;
event->private = private;
event->id = s->uuid++;
if (!s->uuid)
s->uuid++;
list_add_tail(&event->next, &s->events);
return event->id;
}
void
scheduler_unregister_event(scheduler_t *s, event_id_t id)
{
event_t *event, *tmp;
if (!id)
return;
scheduler_for_each_event(s, event, tmp)
if (event->id == id) {
list_del(&event->next);
free(event);
s->restart = 1;
break;
}
}
void
scheduler_set_max_timeout(scheduler_t *s, int timeout)
{
if (timeout >= 0)
s->max_timeout = MIN(s->max_timeout, timeout);
}
int
scheduler_wait_for_events(scheduler_t *s)
{
int ret;
struct timeval tv;
scheduler_prepare_events(s);
tv.tv_sec = s->timeout;
tv.tv_usec = 0;
DBG("timeout: %d, max_timeout: %d\n",
s->timeout, s->max_timeout);
ret = select(s->max_fd + 1, &s->read_fds,
&s->write_fds, &s->except_fds, &tv);
s->restart = 0;
s->timeout = SCHEDULER_MAX_TIMEOUT;
s->max_timeout = SCHEDULER_MAX_TIMEOUT;
if (ret < 0)
return ret;
scheduler_run_events(s);
return ret;
}
void
scheduler_initialize(scheduler_t *s)
{
memset(s, 0, sizeof(scheduler_t));
s->uuid = 1;
FD_ZERO(&s->read_fds);
FD_ZERO(&s->write_fds);
FD_ZERO(&s->except_fds);
INIT_LIST_HEAD(&s->events);
}
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SCHEDULER_H_
#define _SCHEDULER_H_
#include <sys/select.h>
#include "list.h"
#define SCHEDULER_POLL_READ_FD 0x1
#define SCHEDULER_POLL_WRITE_FD 0x2
#define SCHEDULER_POLL_EXCEPT_FD 0x4
#define SCHEDULER_POLL_TIMEOUT 0x8
typedef int event_id_t;
typedef void (*event_cb_t) (event_id_t id, char mode, void *private);
typedef struct scheduler {
fd_set read_fds;
fd_set write_fds;
fd_set except_fds;
struct list_head events;
int uuid;
int max_fd;
int timeout;
int restart;
int max_timeout;
} scheduler_t;
void scheduler_initialize(scheduler_t *);
event_id_t scheduler_register_event(scheduler_t *, char mode,
int fd, int timeout,
event_cb_t cb, void *private);
void scheduler_unregister_event(scheduler_t *, event_id_t);
void scheduler_set_max_timeout(scheduler_t *, int);
int scheduler_wait_for_events(scheduler_t *);
#endif
+496
View File
@@ -0,0 +1,496 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* client harness for tapdisk log */
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include "log.h"
#define BDPRINTF(_f, _a...) fprintf (stderr, "log: " _f "\n", ## _a)
#define BWPRINTF(_f, _a...) fprintf (stderr, "log: " _f "\n", ## _a)
struct writelog {
char* shmpath;
uint32_t shmsize;
void* shm;
/* next unprocessed item in the writelog */
void* cur;
unsigned int inflight;
/* pointer to start and end of free data space for requests */
void* dhd;
void* dtl;
log_sring_t* sring;
log_front_ring_t fring;
};
/* bytes free on the data ring */
static inline unsigned int dring_avail(struct writelog* wl)
{
/* one byte reserved to distinguish empty from full */
if (wl->dhd == wl->dtl)
return sdataend(wl->shm) - sdatastart(wl->shm) - 1;
if (wl->dhd < wl->dtl)
return wl->dtl - wl->dhd - 1;
return (sdataend(wl->shm) - wl->dhd) + (wl->dtl - sdatastart(wl->shm)) - 1;
}
/* advance ring pointer by len bytes */
static inline void* dring_advance(struct writelog* wl, void* start, size_t len)
{
void* next;
int dsz = sdataend(wl->shm) - sdatastart(wl->shm);
next = start + (len % dsz);
if (next > sdataend(wl->shm))
next -= dsz;
return next;
}
static void usage(void)
{
fprintf(stderr, "usage: tapdisk-client <sock>\n");
}
/* returns socket file descriptor */
static int tdctl_open(const char* sockpath)
{
struct sockaddr_un saddr;
int fd;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
BWPRINTF("error creating socket: %s", strerror(errno));
return -1;
}
memset(&saddr, 0, sizeof(saddr));
saddr.sun_family = AF_UNIX;
memcpy(saddr.sun_path, sockpath, strlen(sockpath));
if (connect(fd, &saddr, sizeof(saddr)) < 0) {
BWPRINTF("error connecting to socket %s: %s", sockpath, strerror(errno));
close(fd);
return -1;
}
return fd;
}
static int ctl_talk(int fd, struct log_ctlmsg* msg, char* rsp, int rsplen)
{
int rc;
if ((rc = write(fd, msg, sizeof(*msg))) < 0) {
BWPRINTF("error sending ctl request: %s", strerror(errno));
return -1;
} else if (rc < sizeof(*msg)) {
BWPRINTF("short ctl write (%d/%zd bytes)", rc, sizeof(*msg));
return -1;
}
if (!rsplen)
return 0;
if ((rc = read(fd, rsp, rsplen)) < 0) {
BWPRINTF("error reading ctl response: %s", strerror(errno));
return -1;
} else if (rc < rsplen) {
BWPRINTF("short ctl read (%d/%d bytes)", rc, rsplen);
return -1;
}
return 0;
}
static int ctl_get_shmem(int fd, struct writelog* wl)
{
struct log_ctlmsg req;
char rsp[CTLRSPLEN_SHMP + 1];
int rc;
memset(&req, 0, sizeof(req));
memset(rsp, 0, sizeof(rsp));
memcpy(req.msg, LOGCMD_SHMP, 4);
if ((rc = ctl_talk(fd, &req, rsp, CTLRSPLEN_SHMP)) < 0) {
BWPRINTF("error getting shared memory parameters");
return -1;
}
memcpy(&wl->shmsize, rsp, sizeof(wl->shmsize));
wl->shmpath = strdup(rsp + sizeof(wl->shmsize));
BDPRINTF("shared memory parameters: size: %u, path: %s",
wl->shmsize, wl->shmpath);
return 0;
}
static void ctlmsg_init(struct log_ctlmsg* msg, const char* cmd)
{
memset(msg, 0, sizeof(*msg));
memcpy(msg->msg, cmd, 4);
}
static int ctl_get_writes(int fd)
{
struct log_ctlmsg req;
char rsp[CTLRSPLEN_GET];
int rc;
ctlmsg_init(&req, LOGCMD_GET);
if ((rc = ctl_talk(fd, &req, rsp, CTLRSPLEN_GET)) < 0) {
BWPRINTF("error getting writes");
return -1;
}
return 0;
}
static int ctl_peek_writes(int fd)
{
struct log_ctlmsg req;
char rsp[CTLRSPLEN_PEEK];
int rc;
ctlmsg_init(&req, LOGCMD_PEEK);
if ((rc = ctl_talk(fd, &req, rsp, CTLRSPLEN_PEEK)) < 0) {
BWPRINTF("error peeking writes");
return -1;
}
return 0;
}
/* submit pending requests */
static int ctl_kick(int fd)
{
struct log_ctlmsg req;
int rc;
ctlmsg_init(&req, LOGCMD_KICK);
if ((rc = ctl_talk(fd, &req, NULL, 0)) < 0) {
BWPRINTF("error kicking ring");
return -1;
}
return 0;
}
static int ctl_clear_writes(int fd)
{
struct log_ctlmsg req;
char rsp[CTLRSPLEN_CLEAR];
int rc;
ctlmsg_init(&req, LOGCMD_CLEAR);
if ((rc = ctl_talk(fd, &req, rsp, CTLRSPLEN_CLEAR)) < 0) {
BWPRINTF("error clearing writes");
return -1;
}
return 0;
}
static int writelog_map(struct writelog* wl)
{
int fd;
void* shm;
if ((fd = shm_open(wl->shmpath, O_RDWR, 0750)) < 0) {
BWPRINTF("could not open shared memory at %s: %s", wl->shmpath,
strerror(errno));
return -1;
}
wl->shm = mmap(NULL, wl->shmsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (wl->shm == MAP_FAILED) {
BWPRINTF("could not mmap write log shm: %s", strerror(errno));
return -1;
}
wl->cur = wl->shm;
wl->inflight = 0;
wl->dhd = wl->dtl = sdatastart(wl->shm);
BDPRINTF("shm cookie: 0x%x, data size: %u", *((uint32_t*)wl->shm),
dring_avail(wl));
wl->sring = sringstart(wl->shm);
/* need some thought about what to do on reconnect */
FRONT_RING_INIT(&wl->fring, wl->sring, SRINGSIZE);
return 0;
}
static int writelog_dump(struct writelog* wl)
{
struct disk_range* range = wl->shm;
for (range = wl->shm; (void*)range < bmend(wl->shm); range++) {
if (!range->count)
break;
BDPRINTF("dirty extent: %"PRIu64":%u",
range->sector, range->count);
}
return 0;
}
/* walk dirty map and enqueue read requests.
* returns: 0 when entire bitmap has been enqueued,
* 1 when the ring is full
* -1 on error
*/
static int writelog_enqueue_requests(struct writelog* wl)
{
struct disk_range* range = wl->shm;
log_request_t* req;
for (range = wl->cur; (void*)range < bmend(wl->shm); range++) {
if (!range->count)
break;
if (RING_FULL(&wl->fring))
break;
/* insert range into request stream */
/* 1. get next request slot from ring */
/* 2. ensure enough shm space is available */
BDPRINTF("enqueueing dirty extent: %"PRIu64":%u (ring space: %d/%d)",
range->sector, range->count, RING_FREE_REQUESTS(&wl->fring),
RING_SIZE(&wl->fring));
req = RING_GET_REQUEST(&wl->fring, wl->fring.req_prod_pvt);
req->sector = range->sector;
req->count = range->count;
/* ... */
req->offset = 0;
wl->fring.req_prod_pvt++;
wl->inflight++;
}
wl->cur = range;
if (range->count)
return 1;
return 0;
}
static int writelog_dequeue_responses(struct writelog* wl)
{
RING_IDX rstart, rend;
log_response_t rsp;
rstart = wl->fring.rsp_cons;
rend = wl->sring->rsp_prod;
BDPRINTF("ring kicked (start = %u, end = %u)", rstart, rend);
while (rstart != rend) {
memcpy(&rsp, RING_GET_RESPONSE(&wl->fring, rstart), sizeof(rsp));
BDPRINTF("ctl: read response %"PRIu64":%u", rsp.sector, rsp.count);
wl->fring.rsp_cons = ++rstart;
wl->inflight--;
}
return 0;
}
static int writelog_free(struct writelog* wl)
{
if (wl->shmpath) {
free(wl->shmpath);
wl->shmpath = NULL;
}
if (wl->shm) {
munmap(wl->shm, wl->shmsize);
wl->shm = NULL;
}
return 0;
}
int get_writes(struct writelog* wl, int fd, int peek)
{
int rc;
if (peek)
rc = ctl_peek_writes(fd);
else
rc = ctl_get_writes(fd);
if (rc < 0)
return rc;
wl->cur = wl->shm;
return 0;
}
int await_responses(struct writelog* wl, int fd)
{
struct log_ctlmsg msg;
int rc;
/* sit on socket waiting for kick */
if ((rc = read(fd, &msg, sizeof(msg))) < 0) {
BWPRINTF("error reading from control socket: %s", strerror(errno));
return -1;
} else if (!rc) {
BWPRINTF("EOF on control socket");
return -1;
} else if (rc < sizeof(msg)) {
BWPRINTF("short reply (%d/%d bytes)", rc, (int) sizeof(msg));
return -1;
}
if (strncmp(msg.msg, LOGCMD_KICK, 4)) {
BWPRINTF("Unknown message received: %.4s", msg.msg);
return -1;
}
if (writelog_dequeue_responses(wl) < 0)
return -1;
return 0;
}
/* read_loop:
* 1. extract dirty bitmap
* 2. feed as much as possible onto ring
* 3. kick
* 4. as responses come back, feed more of the dirty bitmap
* into the ring
* 5. when entire bitmap has been queued, go to 1?
*/
int read_loop(struct writelog* wl, int fd)
{
int rc;
if (get_writes(wl, fd, 1) < 0)
return -1;
writelog_dump(wl);
do {
rc = writelog_enqueue_requests(wl);
if (RING_FREE_REQUESTS(&wl->fring) < RING_SIZE(&wl->fring))
RING_PUSH_REQUESTS(&wl->fring);
if (ctl_kick(fd) < 0)
return -1;
/* collect responses */
if (wl->inflight && await_responses(wl, fd) < 0)
return -1;
} while (rc > 0);
return rc;
}
int main(int argc, char* argv[])
{
int fd;
struct writelog wl;
char cmd;
if (argc < 2) {
usage();
return 1;
}
if (argc < 3)
cmd = 'p';
else
cmd = argv[2][0];
fd = tdctl_open(argv[1]);
if (ctl_get_shmem(fd, &wl) < 0)
return 1;
if (writelog_map(&wl) < 0) {
BWPRINTF("Error mapping write log: %s", strerror(errno));
return 1;
}
switch (cmd) {
case 'p':
if (get_writes(&wl, fd, 1) < 0)
return 1;
writelog_dump(&wl);
break;
case 'c':
if (ctl_clear_writes(fd) < 0)
return 1;
break;
case 'g':
if (get_writes(&wl, fd, 0) < 0)
return 1;
writelog_dump(&wl);
break;
case 'r':
if (read_loop(&wl, fd) < 0)
return 1;
break;
default:
usage();
return 1;
}
writelog_free(&wl);
close(fd);
return 0;
}
+797
View File
@@ -0,0 +1,797 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "list.h"
#include "scheduler.h"
#include "tapdisk-vbd.h"
#include "tapdisk-server.h"
#include "libvhd.h"
#define POLL_READ 0
#define POLL_WRITE 1
#define SPB_SHIFT (VHD_BLOCK_SHIFT - SECTOR_SHIFT)
struct tapdisk_stream_poll {
int pipe[2];
int set;
};
struct tapdisk_stream_request {
uint64_t sec;
uint32_t secs;
uint64_t seqno;
blkif_request_t blkif_req;
struct list_head next;
};
struct tapdisk_stream {
td_vbd_t *vbd;
unsigned int id;
int err;
uint64_t cur;
uint64_t start;
uint64_t end;
uint64_t started;
uint64_t completed;
struct tapdisk_stream_poll poll;
event_id_t enqueue_event_id;
struct list_head free_list;
struct list_head pending_list;
struct list_head completed_list;
struct tapdisk_stream_request requests[MAX_REQUESTS];
};
static unsigned int tapdisk_stream_count;
static void tapdisk_stream_close_image(struct tapdisk_stream *);
static char *program;
static struct tapdisk_stream stream1, stream2;
static vhd_context_t vhd1;
static void
usage(FILE *stream)
{
printf("usage: %s <-n type:/path/to/image> <-m type:/path/to/image>\n",
program);
}
static int
open_vhd(const char *path, vhd_context_t *vhd)
{
int err;
err = vhd_open(vhd, path, VHD_OPEN_RDONLY);
if (err) {
printf("error opening %s: %d\n", path, err);
return err;
}
err = vhd_get_bat(vhd);
if (err)
{
printf("error reading BAT for %s: %d\n", path, err);
vhd_close(vhd);
return err;
}
return 0;
}
static inline void
tapdisk_stream_poll_initialize(struct tapdisk_stream_poll *p)
{
p->set = 0;
p->pipe[POLL_READ] = p->pipe[POLL_WRITE] = -1;
}
static int
tapdisk_stream_poll_open(struct tapdisk_stream_poll *p)
{
int err;
tapdisk_stream_poll_initialize(p);
err = pipe(p->pipe);
if (err)
return -errno;
err = fcntl(p->pipe[POLL_READ], F_SETFL, O_NONBLOCK);
if (err)
goto out;
err = fcntl(p->pipe[POLL_WRITE], F_SETFL, O_NONBLOCK);
if (err)
goto out;
return 0;
out:
close(p->pipe[POLL_READ]);
close(p->pipe[POLL_WRITE]);
tapdisk_stream_poll_initialize(p);
return -errno;
}
static void
tapdisk_stream_poll_close(struct tapdisk_stream_poll *p)
{
if (p->pipe[POLL_READ] != -1)
close(p->pipe[POLL_READ]);
if (p->pipe[POLL_WRITE] != -1)
close(p->pipe[POLL_WRITE]);
tapdisk_stream_poll_initialize(p);
}
static inline void
tapdisk_stream_poll_clear(struct tapdisk_stream_poll *p)
{
int dummy;
read(p->pipe[POLL_READ], &dummy, sizeof(dummy));
p->set = 0;
}
static inline void
tapdisk_stream_poll_set(struct tapdisk_stream_poll *p)
{
int dummy = 0;
if (!p->set) {
write(p->pipe[POLL_WRITE], &dummy, sizeof(dummy));
p->set = 1;
}
}
static inline int
tapdisk_stream_stop(struct tapdisk_stream *s)
{
return ((s->cur == s->end || s->err) &&
list_empty(&s->pending_list) &&
list_empty(&s->completed_list));
}
static inline void
tapdisk_stream_initialize_request(struct tapdisk_stream_request *req)
{
memset(req, 0, sizeof(*req));
INIT_LIST_HEAD(&req->next);
}
static inline int
tapdisk_stream_request_idx(struct tapdisk_stream *s,
struct tapdisk_stream_request *req)
{
return (req - s->requests);
}
static inline struct tapdisk_stream_request *
tapdisk_stream_get_request(struct tapdisk_stream *s)
{
struct tapdisk_stream_request *req;
if (list_empty(&s->free_list))
return NULL;
req = list_entry(s->free_list.next,
struct tapdisk_stream_request, next);
list_del_init(&req->next);
tapdisk_stream_initialize_request(req);
return req;
}
static inline void
tapdisk_stream_queue_completed(struct tapdisk_stream *s,
struct tapdisk_stream_request *sreq)
{
struct tapdisk_stream_request *itr;
list_for_each_entry(itr, &s->completed_list, next)
if (sreq->seqno < itr->seqno) {
list_add_tail(&sreq->next, &itr->next);
return;
}
list_add_tail(&sreq->next, &s->completed_list);
}
static int
tapdisk_result_compare(struct tapdisk_stream_request *sreq1,
struct tapdisk_stream_request *sreq2)
{
unsigned long idx1, idx2;
char *buf1, *buf2;
int result;
assert(sreq1->seqno == sreq2->seqno);
assert(sreq1->secs == sreq2->secs);
idx1 = (unsigned long)tapdisk_stream_request_idx(&stream1,
sreq1);
idx2 = (unsigned long)tapdisk_stream_request_idx(&stream2,
sreq2);
buf1 = (char *)MMAP_VADDR(stream1.vbd->ring.vstart, idx1, 0);
buf2 = (char *)MMAP_VADDR(stream2.vbd->ring.vstart, idx2, 0);
result = memcmp(buf1, buf2, sreq1->secs << SECTOR_SHIFT);
return result;
}
static int
tapdisk_stream_process_data(void)
{
struct tapdisk_stream_request *sreq1, *sreq2, *tmp1, *tmp2;
int advance_both;
int result = 0;
sreq1 = list_entry(stream1.completed_list.next,
struct tapdisk_stream_request, next);
sreq2 = list_entry(stream2.completed_list.next,
struct tapdisk_stream_request, next);
tmp1 = list_entry(sreq1->next.next,
struct tapdisk_stream_request, next);
tmp2 = list_entry(sreq2->next.next,
struct tapdisk_stream_request, next);
while (result == 0 &&
&sreq1->next != &stream1.completed_list &&
&sreq2->next != &stream2.completed_list) {
//printf("checking: %llu|%llu\n", sreq1->seqno, sreq2->seqno);
advance_both = 1;
if (sreq1->seqno < sreq2->seqno) {
advance_both = 0;
goto advance1;
}
if (sreq1->seqno > sreq2->seqno)
goto advance2;
result = tapdisk_result_compare(sreq1, sreq2);
stream1.completed++;
stream2.completed++;
list_del_init(&sreq1->next);
list_add_tail(&sreq1->next, &stream1.free_list);
list_del_init(&sreq2->next);
list_add_tail(&sreq2->next, &stream2.free_list);
advance1:
sreq1 = tmp1;
tmp1 = list_entry(tmp1->next.next,
struct tapdisk_stream_request, next);
if (!advance_both)
continue;
advance2:
sreq2 = tmp2;
tmp2 = list_entry(tmp2->next.next,
struct tapdisk_stream_request, next);
}
return result;
}
static void
tapdisk_stream_dequeue(void *arg, blkif_response_t *rsp)
{
struct tapdisk_stream *s = (struct tapdisk_stream *)arg;
struct tapdisk_stream_request *sreq = s->requests + rsp->id;
list_del_init(&sreq->next);
if (rsp->status == BLKIF_RSP_OKAY)
tapdisk_stream_queue_completed(s, sreq);
else {
s->err = EIO;
list_add_tail(&sreq->next, &s->free_list);
fprintf(stderr, "error reading sector 0x%"PRIx64"\n", sreq->sec);
}
if (tapdisk_stream_process_data()) {
fprintf(stderr, "mismatch at sector 0x%"PRIx64"\n",
sreq->sec);
stream1.err = EINVAL;
stream2.err = EINVAL;
}
tapdisk_stream_poll_set(&stream1.poll);
tapdisk_stream_poll_set(&stream2.poll);
}
static inline int
tapdisk_stream_enqueue_copy(struct tapdisk_stream *s,
struct tapdisk_stream_request *r)
{
td_vbd_t *vbd;
blkif_request_t *breq;
td_vbd_request_t *vreq;
struct tapdisk_stream_request *sreq;
int idx;
vbd = stream2.vbd;
sreq = tapdisk_stream_get_request(s);
if (!sreq)
return 1;
idx = tapdisk_stream_request_idx(s, sreq);
sreq->sec = r->sec;
sreq->secs = r->secs;
sreq->seqno = r->seqno;
breq = &sreq->blkif_req;
breq->id = idx;
breq->nr_segments = r->blkif_req.nr_segments;
breq->sector_number = r->blkif_req.sector_number;
breq->operation = BLKIF_OP_READ;
for (int i = 0; i < r->blkif_req.nr_segments; i++) {
struct blkif_request_segment *seg = breq->seg + i;
seg->first_sect = r->blkif_req.seg[i].first_sect;
seg->last_sect = r->blkif_req.seg[i].last_sect;
}
s->cur += sreq->secs;
vreq = vbd->request_list + idx;
assert(list_empty(&vreq->next));
assert(vreq->secs_pending == 0);
memcpy(&vreq->req, breq, sizeof(*breq));
vbd->received++;
vreq->vbd = vbd;
tapdisk_vbd_move_request(vreq, &vbd->new_requests);
list_add_tail(&sreq->next, &s->pending_list);
return 0;
}
static void
tapdisk_stream_enqueue1(void)
{
td_vbd_t *vbd;
int i, idx, psize, blk;
struct tapdisk_stream *s = &stream1;
vbd = s->vbd;
psize = getpagesize();
while (s->cur < s->end && !s->err) {
blkif_request_t *breq;
td_vbd_request_t *vreq;
struct tapdisk_stream_request *sreq;
/* skip any blocks that are not present in this image */
blk = s->cur >> SPB_SHIFT;
while (s->cur < s->end && vhd1.bat.bat[blk] == DD_BLK_UNUSED) {
//printf("skipping block %d\n", blk);
blk++;
s->cur = blk << SPB_SHIFT;
}
if (s->cur >= s->end)
break;
sreq = tapdisk_stream_get_request(s);
if (!sreq)
break;
idx = tapdisk_stream_request_idx(s, sreq);
sreq->sec = s->cur;
sreq->secs = 0;
sreq->seqno = s->started++;
breq = &sreq->blkif_req;
breq->id = idx;
breq->nr_segments = 0;
breq->sector_number = sreq->sec;
breq->operation = BLKIF_OP_READ;
for (i = 0; i < BLKIF_MAX_SEGMENTS_PER_REQUEST; i++) {
uint32_t secs;
struct blkif_request_segment *seg = breq->seg + i;
secs = MIN(s->end - s->cur, psize >> SECTOR_SHIFT);
secs = MIN(((blk + 1) << SPB_SHIFT) - s->cur, secs);
if (!secs)
break;
sreq->secs += secs;
s->cur += secs;
seg->first_sect = 0;
seg->last_sect = secs - 1;
breq->nr_segments++;
}
vreq = vbd->request_list + idx;
assert(list_empty(&vreq->next));
assert(vreq->secs_pending == 0);
memcpy(&vreq->req, breq, sizeof(*breq));
vbd->received++;
vreq->vbd = vbd;
tapdisk_vbd_move_request(vreq, &vbd->new_requests);
list_add_tail(&sreq->next, &s->pending_list);
}
tapdisk_vbd_issue_requests(vbd);
}
static void
tapdisk_stream_enqueue2(void)
{
td_vbd_t *vbd;
int i, blk;
struct tapdisk_stream_request *itr;
struct tapdisk_stream *s = &stream2;
vbd = s->vbd;
/* issue the same requests that we issued on stream1 */
list_for_each_entry(itr, &stream1.completed_list, next) {
if (itr->sec < s->cur)
continue;
if (tapdisk_stream_enqueue_copy(s, itr))
goto done;
}
list_for_each_entry(itr, &stream1.pending_list, next) {
if (itr->sec < s->cur)
continue;
if (tapdisk_stream_enqueue_copy(s, itr))
goto done;
}
stream2.cur = stream1.cur;
done:
tapdisk_vbd_issue_requests(vbd);
}
static inline int
tapdisk_diff_done(void)
{
return (tapdisk_stream_stop(&stream1) && tapdisk_stream_stop(&stream2));
}
static void
tapdisk_diff_stop(void)
{
tapdisk_stream_close_image(&stream1);
tapdisk_stream_close_image(&stream2);
}
static void
tapdisk_stream_enqueue(event_id_t id, char mode, void *arg)
{
struct tapdisk_stream *s = (struct tapdisk_stream *)arg;
tapdisk_stream_poll_clear(&s->poll);
if (tapdisk_diff_done()) {
tapdisk_diff_stop();
return;
}
if (s == &stream1)
tapdisk_stream_enqueue1();
else if (s == &stream2)
tapdisk_stream_enqueue2();
else
assert(0);
if (tapdisk_diff_done()) {
// we have to check again for the case when stream1 had no
// blocks at all
tapdisk_diff_stop();
return;
}
}
static int
tapdisk_stream_open_image(struct tapdisk_stream *s, const char *path, int type)
{
int err;
image_t image;
s->id = tapdisk_stream_count++;
err = tapdisk_vbd_initialize(-1, -1, s->id);
if (err)
goto out;
s->vbd = tapdisk_server_get_vbd(s->id);
if (!s->vbd) {
err = ENODEV;
goto out;
}
tapdisk_vbd_set_callback(s->vbd, tapdisk_stream_dequeue, s);
err = tapdisk_vbd_open_vdi(s->vbd, path, type,
TAPDISK_STORAGE_TYPE_DEFAULT,
TD_OPEN_RDONLY);
if (err)
goto out;
s->vbd->reopened = 1;
err = tapdisk_vbd_get_image_info(s->vbd, &image);
if (err) {
fprintf(stderr, "failed getting image size: %d\n", err);
return err;
}
s->start = 0;
s->cur = s->start;
s->end = image.size;
err = 0;
out:
if (err)
fprintf(stderr, "failed to open image %s: %d\n", path, err);
return err;
}
static void
tapdisk_stream_close_image(struct tapdisk_stream *s)
{
td_vbd_t *vbd;
vbd = tapdisk_server_get_vbd(s->id);
if (vbd) {
tapdisk_vbd_close_vdi(vbd);
tapdisk_server_remove_vbd(vbd);
free((void *)vbd->ring.vstart);
free(vbd->name);
free(vbd);
s->vbd = NULL;
}
}
static int
tapdisk_stream_initialize_requests(struct tapdisk_stream *s)
{
size_t size;
td_ring_t *ring;
int err, i, psize;
ring = &s->vbd->ring;
psize = getpagesize();
size = psize * BLKTAP_MMAP_REGION_SIZE;
/* sneaky -- set up ring->vstart so tapdisk_vbd will use our buffers */
err = posix_memalign((void **)&ring->vstart, psize, size);
if (err) {
fprintf(stderr, "failed to allocate buffers: %d\n", err);
ring->vstart = 0;
return err;
}
for (i = 0; i < MAX_REQUESTS; i++) {
struct tapdisk_stream_request *req = s->requests + i;
tapdisk_stream_initialize_request(req);
list_add_tail(&req->next, &s->free_list);
}
return 0;
}
static int
tapdisk_stream_register_enqueue_event(struct tapdisk_stream *s)
{
int err;
struct tapdisk_stream_poll *p = &s->poll;
err = tapdisk_stream_poll_open(p);
if (err)
goto out;
err = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
p->pipe[POLL_READ], 0,
tapdisk_stream_enqueue, s);
if (err < 0)
goto out;
s->enqueue_event_id = err;
err = 0;
out:
if (err)
fprintf(stderr, "failed to register event: %d\n", err);
return err;
}
static void
tapdisk_stream_unregister_enqueue_event(struct tapdisk_stream *s)
{
if (s->enqueue_event_id) {
tapdisk_server_unregister_event(s->enqueue_event_id);
s->enqueue_event_id = 0;
}
tapdisk_stream_poll_close(&s->poll);
}
static inline void
tapdisk_stream_initialize(struct tapdisk_stream *s)
{
memset(s, 0, sizeof(*s));
INIT_LIST_HEAD(&s->free_list);
INIT_LIST_HEAD(&s->pending_list);
INIT_LIST_HEAD(&s->completed_list);
}
static int
tapdisk_stream_open(struct tapdisk_stream *s, const char *arg)
{
int err, type;
char *path;
err = tapdisk_parse_disk_type(arg, &path, &type);
if (err)
return err;
tapdisk_stream_initialize(s);
err = tapdisk_stream_open_image(s, path, type);
if (err)
return err;
err = tapdisk_stream_initialize_requests(s);
if (err)
return err;
err = tapdisk_stream_register_enqueue_event(s);
if (err)
return err;
tapdisk_stream_enqueue(s->enqueue_event_id,
SCHEDULER_POLL_READ_FD, s);
return 0;
}
static void
tapdisk_stream_release(struct tapdisk_stream *s)
{
tapdisk_stream_close_image(s);
tapdisk_stream_unregister_enqueue_event(s);
}
static int
tapdisk_stream_run(struct tapdisk_stream *s)
{
tapdisk_stream_enqueue(s->enqueue_event_id, SCHEDULER_POLL_READ_FD, s);
tapdisk_server_run();
return s->err;
}
int
main(int argc, char *argv[])
{
int c, err, type1;
const char *arg1 = NULL, *arg2 = NULL;
char *path1;
err = 0;
program = basename(argv[0]);
while ((c = getopt(argc, argv, "n:m:h")) != -1) {
switch (c) {
case 'n':
arg1 = optarg;
break;
case 'm':
arg2 = optarg;
break;
case 'h':
usage(stdout);
return 0;
default:
goto fail_usage;
}
}
if (!arg1 || !arg2)
goto fail_usage;
err = tapdisk_parse_disk_type(arg1, &path1, &type1);
if (err)
return err;
if (type1 != DISK_TYPE_VHD) {
printf("error: first VDI is not VHD\n");
return EINVAL;
}
err = open_vhd(path1, &vhd1);
if (err)
return err;
tapdisk_start_logging("tapdisk-diff");
err = tapdisk_server_initialize(NULL, NULL);
if (err)
goto out;
err = tapdisk_stream_open(&stream1, arg1);
if (err) {
fprintf(stderr, "Failed to open %s: %s\n",
arg1, strerror(-err));
goto out;
}
err = tapdisk_stream_open(&stream2, arg2);
if (err) {
fprintf(stderr, "Failed to open %s: %s\n",
arg2, strerror(-err));
goto out1;
}
if (stream1.end != stream2.end) {
fprintf(stderr, "Image sizes differ: %"PRIu64" != %"PRIu64"\n",
stream1.end, stream2.end);
err = EINVAL;
goto out2;
}
tapdisk_server_run();
out2:
tapdisk_stream_release(&stream2);
out1:
tapdisk_stream_release(&stream1);
out:
vhd_close(&vhd1);
tapdisk_stop_logging();
return err ? : stream1.err;
fail_usage:
usage(stderr);
return 1;
}
+100
View File
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include "tapdisk-driver.h"
#include "tapdisk-server.h"
td_driver_t *
tapdisk_driver_allocate(int type, char *name, td_flag_t flags, int storage)
{
int err;
td_driver_t *driver;
struct tap_disk *ops;
ops = tapdisk_server_find_driver_interface(type);
if (!ops)
return NULL;
driver = calloc(1, sizeof(td_driver_t));
if (!driver)
return NULL;
err = tapdisk_namedup(&driver->name, name);
if (err)
goto fail;
driver->ops = ops;
driver->type = type;
driver->storage = storage;
driver->data = calloc(1, ops->private_data_size);
if (!driver->data)
goto fail;
if (td_flag_test(flags, TD_OPEN_RDONLY))
td_flag_set(driver->state, TD_DRIVER_RDONLY);
return driver;
fail:
free(driver->name);
free(driver->data);
free(driver);
return NULL;
}
void
tapdisk_driver_free(td_driver_t *driver)
{
if (!driver)
return;
if (driver->refcnt)
return;
if (td_flag_test(driver->state, TD_DRIVER_OPEN))
EPRINTF("freeing open driver %s (state 0x%08x)\n",
driver->name, driver->state);
free(driver->name);
free(driver->data);
free(driver);
}
void
tapdisk_driver_queue_tiocb(td_driver_t *driver, struct tiocb *tiocb)
{
tapdisk_server_queue_tiocb(tiocb);
}
void
tapdisk_driver_debug(td_driver_t *driver)
{
if (driver->ops->td_debug)
driver->ops->td_debug(driver);
}
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_DRIVER_H_
#define _TAPDISK_DRIVER_H_
#include "tapdisk.h"
#include "scheduler.h"
#include "tapdisk-queue.h"
#define TD_DRIVER_OPEN 0x0001
#define TD_DRIVER_RDONLY 0x0002
struct td_driver_handle {
int type;
char *name;
int storage;
int refcnt;
td_flag_t state;
td_disk_info_t info;
void *data;
struct tap_disk *ops;
struct list_head next;
};
td_driver_t *tapdisk_driver_allocate(int, char *, td_flag_t, int);
void tapdisk_driver_free(td_driver_t *);
void tapdisk_driver_queue_tiocb(td_driver_t *, struct tiocb *);
void tapdisk_driver_debug(td_driver_t *);
#endif
+271
View File
@@ -0,0 +1,271 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <unistd.h>
#include <libaio.h>
#include <syslog.h>
#include <sys/time.h>
#include "tapdisk-log.h"
#include "tapdisk-filter.h"
#define RSEED 7
#define PRE_CHECK 0
#define POST_CHECK 1
#define WRITE_INTEGRITY "buffer integrity failure after write"
#define READ_INTEGRITY "disk integrity failure after read"
#define DBG(f, a...) tlog_write(TLOG_WARN, f, ##a)
/*
* simulate IO errors by knocking request size to zero before
* submitting and restoring original size before returning
*/
static inline void
inject_fault(struct tfilter *filter, struct iocb *io)
{
struct fiocb *fio;
if (!filter->ffree)
return;
fio = filter->flist[--filter->ffree];
fio->bytes = io->u.c.nbytes;
fio->data = io->data;
io->u.c.nbytes = 0;
io->data = fio;
}
static inline int
fault_injected(struct tfilter *filter, struct iocb *io)
{
unsigned long iop = (unsigned long)io->data;
unsigned long start = (unsigned long)filter->fiocbs;
unsigned long end = start + (filter->iocbs * sizeof(struct fiocb));
return (iop >= start && iop < end);
}
static inline void
recover_fault(struct tfilter *filter, struct iocb *io)
{
struct fiocb *fio = (struct fiocb *)io->data;
io->u.c.nbytes = fio->bytes;
io->data = fio->data;
memset(fio, 0, sizeof(struct fiocb));
filter->flist[filter->ffree++] = fio;
}
static inline uint64_t
chksum(char *buf)
{
int i, num = 512 >> 3;
uint64_t *p = (uint64_t *)buf;
uint64_t sum = 0;
for (i = 0; i < num; i++)
sum += p[i];
return sum;
}
static inline void
check_hash(struct tfilter *filter, uint64_t sec, char *buf, char *type)
{
uint64_t sum;
struct dhash *hash;
hash = filter->dhash + sec;
if (!hash->time.tv_sec)
return;
sum = chksum(buf);
if (hash->hash != chksum(buf)) {
struct timeval now;
gettimeofday(&now, NULL);
DBG("%s: hash table: 0x%020" PRIx64 " at %012lu.%06lu, "
"from disk: 0x%020" PRIx64 " at %012lu.%06lu\n",
type, hash->hash, hash->time.tv_sec,
hash->time.tv_usec, sum, now.tv_sec, now.tv_usec);
}
}
static inline void
insert_hash(struct tfilter *filter, uint64_t sec, char *buf)
{
struct dhash *hash;
hash = filter->dhash + sec;
hash->hash = chksum(buf);
gettimeofday(&hash->time, NULL);
}
static void
check_sector(struct tfilter *filter, int type, int rw, uint64_t sec, char *buf)
{
struct dhash *hash;
if (sec >= filter->secs)
return;
hash = filter->dhash + sec;
if (rw) {
if (type == PRE_CHECK)
insert_hash(filter, sec, buf);
else
check_hash(filter, sec, buf, WRITE_INTEGRITY);
} else if (type == POST_CHECK) {
check_hash(filter, sec, buf, READ_INTEGRITY);
insert_hash(filter, sec, buf);
}
}
static void
check_data(struct tfilter *filter, int type, struct iocb *io)
{
int rw;
uint64_t i, sec;
rw = (io->aio_lio_opcode == IO_CMD_PWRITE);
for (i = 0; i < io->u.c.nbytes; i += 512) {
char *buf = io->u.c.buf + i;
uint64_t sec = (io->u.c.offset + i) >> 9;
check_sector(filter, type, rw, sec, buf);
}
}
struct tfilter *
tapdisk_init_tfilter(int mode, int iocbs, uint64_t secs)
{
int i;
struct tfilter *filter = NULL;
if (!mode)
return NULL;
filter = calloc(1, sizeof(struct tfilter));
if (!filter)
goto fail;
filter->mode = mode;
filter->secs = secs;
filter->iocbs = iocbs;
if (filter->mode & TD_INJECT_FAULTS) {
filter->fiocbs = calloc(iocbs, sizeof(struct fiocb));
filter->flist = calloc(iocbs, sizeof(struct fiocb *));
if (!filter->fiocbs || !filter->flist)
filter->mode &= ~TD_INJECT_FAULTS;
else {
srand(RSEED);
filter->ffree = iocbs;
for (i = 0; i < iocbs; i++)
filter->flist[i] = filter->fiocbs + i;
}
}
if (filter->mode & TD_CHECK_INTEGRITY) {
filter->dhash = calloc(secs, sizeof(struct dhash));
if (!filter->dhash)
filter->mode &= ~TD_CHECK_INTEGRITY;
}
syslog(LOG_WARNING, "WARNING: "
"FILTERING IN MODE 0x%04x\n", filter->mode);
return filter;
fail:
tapdisk_free_tfilter(filter);
return NULL;
}
void
tapdisk_free_tfilter(struct tfilter *filter)
{
if (!filter)
return;
free(filter->dhash);
free(filter->flist);
free(filter->fiocbs);
free(filter);
}
void
tapdisk_filter_iocbs(struct tfilter *filter, struct iocb **iocbs, int num)
{
int i;
if (!filter)
return;
for (i = 0; i < num; i++) {
struct iocb *io = iocbs[i];
if (filter->mode & TD_INJECT_FAULTS) {
if ((random() % 100) <= TD_FAULT_RATE) {
inject_fault(filter, io);
continue;
}
}
if (filter->mode & TD_CHECK_INTEGRITY)
check_data(filter, PRE_CHECK, io);
}
}
void
tapdisk_filter_events(struct tfilter *filter, struct io_event *events, int num)
{
int i;
if (!filter)
return;
for (i = 0; i < num; i++) {
struct iocb *io = events[i].obj;
if (filter->mode & TD_INJECT_FAULTS) {
if (fault_injected(filter, io)) {
recover_fault(filter, io);
continue;
}
}
if (filter->mode & TD_CHECK_INTEGRITY)
check_data(filter, POST_CHECK, io);
}
}
+67
View File
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TAPDISK_FILTER_H
#define TAPDISK_FILTER_H
#include <libaio.h>
#include <inttypes.h>
#include <time.h>
#define TD_INJECT_FAULTS 0x00001 /* simulate random IO failures */
#define TD_CHECK_INTEGRITY 0x00002 /* check data integrity */
#define TD_FAULT_RATE 5
struct dhash {
uint64_t hash;
struct timeval time;
};
struct fiocb {
size_t bytes;
void *data;
};
struct tfilter {
int mode;
uint64_t secs;
int iocbs;
struct dhash *dhash;
int ffree;
struct fiocb *fiocbs;
struct fiocb **flist;
};
struct tfilter *tapdisk_init_tfilter(int mode, int iocbs, uint64_t secs);
void tapdisk_free_tfilter(struct tfilter *);
void tapdisk_filter_iocbs(struct tfilter *, struct iocb **, int);
void tapdisk_filter_events(struct tfilter *, struct io_event *, int);
#endif
+160
View File
@@ -0,0 +1,160 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include "tapdisk-image.h"
#include "tapdisk-driver.h"
#include "tapdisk-server.h"
#define ERR(_err, _f, _a...) tlog_error(_err, _f, ##_a)
td_image_t *
tapdisk_image_allocate(char *file, int type, int storage,
td_flag_t flags, void *private)
{
int err;
td_image_t *image;
image = calloc(1, sizeof(td_image_t));
if (!image)
return NULL;
err = tapdisk_namedup(&image->name, file);
if (err) {
free(image);
return NULL;
}
image->type = type;
image->flags = flags;
image->storage = storage;
image->private = private;
INIT_LIST_HEAD(&image->next);
return image;
}
void
tapdisk_image_free(td_image_t *image)
{
if (!image)
return;
list_del(&image->next);
free(image->name);
tapdisk_driver_free(image->driver);
free(image);
}
int
tapdisk_image_check_td_request(td_image_t *image, td_request_t treq)
{
int rdonly;
td_driver_t *driver;
td_disk_info_t *info;
driver = image->driver;
if (!driver)
return -ENODEV;
info = &driver->info;
rdonly = td_flag_test(image->flags, TD_OPEN_RDONLY);
if (treq.op != TD_OP_READ && treq.op != TD_OP_WRITE)
goto fail;
if (treq.op == TD_OP_WRITE && rdonly)
goto fail;
if (treq.secs <= 0 || treq.sec + treq.secs > info->size)
goto fail;
return 0;
fail:
ERR(-EINVAL, "bad td request on %s (%s, %"PRIu64"): %d at %"PRIu64,
image->name, (rdonly ? "ro" : "rw"), info->size, treq.op,
treq.sec + treq.secs);
return -EINVAL;
}
int
tapdisk_image_check_ring_request(td_image_t *image, blkif_request_t *req)
{
td_driver_t *driver;
td_disk_info_t *info;
int i, psize, rdonly;
uint64_t nsects, total;
driver = image->driver;
if (!driver)
return -ENODEV;
nsects = 0;
total = 0;
info = &driver->info;
rdonly = td_flag_test(image->flags, TD_OPEN_RDONLY);
if (req->operation != BLKIF_OP_READ &&
req->operation != BLKIF_OP_WRITE)
goto fail;
if (req->operation == BLKIF_OP_WRITE && rdonly)
goto fail;
if (!req->nr_segments || req->nr_segments > MAX_SEGMENTS_PER_REQ)
goto fail;
total = 0;
psize = getpagesize();
for (i = 0; i < req->nr_segments; i++) {
nsects = req->seg[i].last_sect - req->seg[i].first_sect + 1;
if (req->seg[i].last_sect >= psize >> 9 || nsects <= 0)
goto fail;
total += nsects;
}
if (req->sector_number + nsects > info->size)
goto fail;
return 0;
fail:
ERR(-EINVAL, "bad request on %s (%s, %"PRIu64"): id: %"PRIu64": %d at %"PRIu64,
image->name, (rdonly ? "ro" : "rw"), info->size, req->id,
req->operation, req->sector_number + total);
return -EINVAL;
}
+55
View File
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_IMAGE_H_
#define _TAPDISK_IMAGE_H_
#include "tapdisk.h"
#include <xen/io/blkif.h>
struct td_image_handle {
int type;
char *name;
td_flag_t flags;
int storage;
td_driver_t *driver;
td_disk_info_t info;
void *private;
struct list_head next;
};
td_image_t *tapdisk_image_allocate(char *, int, int, td_flag_t, void *);
void tapdisk_image_free(td_image_t *);
int tapdisk_image_check_td_request(td_image_t *, td_request_t);
int tapdisk_image_check_ring_request(td_image_t *, blkif_request_t *);
#endif
+250
View File
@@ -0,0 +1,250 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include "tapdisk.h"
#include "tapdisk-vbd.h"
#include "tapdisk-image.h"
#include "tapdisk-driver.h"
#include "tapdisk-server.h"
#include "tapdisk-interface.h"
int
td_load(td_image_t *image)
{
int err;
td_image_t *shared;
td_driver_t *driver;
shared = tapdisk_server_get_shared_image(image);
if (!shared)
return -ENODEV;
driver = shared->driver;
if (!driver)
return -EBADF;
driver->refcnt++;
image->driver = driver;
image->info = driver->info;
DPRINTF("loaded shared image %s (%d users, state: 0x%08x, type: %d)\n",
driver->name, driver->refcnt, driver->state, driver->type);
return 0;
}
int
td_open(td_image_t *image)
{
int err;
td_driver_t *driver;
driver = image->driver;
if (!driver) {
driver = tapdisk_driver_allocate(image->type,
image->name,
image->flags,
image->storage);
if (!driver)
return -ENOMEM;
}
if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
err = driver->ops->td_open(driver, image->name, image->flags);
if (err) {
if (!image->driver)
tapdisk_driver_free(driver);
return err;
}
td_flag_set(driver->state, TD_DRIVER_OPEN);
DPRINTF("opened image %s (%d users, state: 0x%08x, type: %d)\n",
driver->name, driver->refcnt + 1,
driver->state, driver->type);
}
image->driver = driver;
image->info = driver->info;
driver->refcnt++;
return 0;
}
int
td_close(td_image_t *image)
{
td_driver_t *driver;
driver = image->driver;
if (!driver)
return -ENODEV;
driver->refcnt--;
if (!driver->refcnt && td_flag_test(driver->state, TD_DRIVER_OPEN)) {
driver->ops->td_close(driver);
td_flag_clear(driver->state, TD_DRIVER_OPEN);
}
DPRINTF("closed image %s (%d users, state: 0x%08x, type: %d)\n",
driver->name, driver->refcnt, driver->state, driver->type);
return 0;
}
int
td_get_parent_id(td_image_t *image, td_disk_id_t *id)
{
td_driver_t *driver;
driver = image->driver;
if (!driver)
return -ENODEV;
if (!td_flag_test(driver->state, TD_DRIVER_OPEN))
return -EBADF;
return driver->ops->td_get_parent_id(driver, id);
}
int
td_validate_parent(td_image_t *image, td_image_t *parent)
{
td_driver_t *driver, *pdriver;
driver = image->driver;
pdriver = parent->driver;
if (!driver || !pdriver)
return -ENODEV;
if (!td_flag_test(driver->state, TD_DRIVER_OPEN) ||
!td_flag_test(pdriver->state, TD_DRIVER_OPEN))
return -EBADF;
return 0;
return driver->ops->td_validate_parent(driver, pdriver, 0);
}
void
td_queue_write(td_image_t *image, td_request_t treq)
{
int err;
td_driver_t *driver;
driver = image->driver;
if (!driver) {
err = -ENODEV;
goto fail;
}
if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
err = -EBADF;
goto fail;
}
err = tapdisk_image_check_td_request(image, treq);
if (err)
goto fail;
driver->ops->td_queue_write(driver, treq);
return;
fail:
td_complete_request(treq, err);
}
void
td_queue_read(td_image_t *image, td_request_t treq)
{
int err;
td_driver_t *driver;
driver = image->driver;
if (!driver) {
err = -ENODEV;
goto fail;
}
if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
err = -EBADF;
goto fail;
}
err = tapdisk_image_check_td_request(image, treq);
if (err)
goto fail;
driver->ops->td_queue_read(driver, treq);
return;
fail:
td_complete_request(treq, err);
}
void
td_forward_request(td_request_t treq)
{
tapdisk_vbd_forward_request(treq);
}
void
td_complete_request(td_request_t treq, int res)
{
treq.cb(treq, res);
}
void
td_queue_tiocb(td_driver_t *driver, struct tiocb *tiocb)
{
tapdisk_driver_queue_tiocb(driver, tiocb);
}
void
td_prep_read(struct tiocb *tiocb, int fd, char *buf, size_t bytes,
long long offset, td_queue_callback_t cb, void *arg)
{
tapdisk_prep_tiocb(tiocb, fd, 0, buf, bytes, offset, cb, arg);
}
void
td_prep_write(struct tiocb *tiocb, int fd, char *buf, size_t bytes,
long long offset, td_queue_callback_t cb, void *arg)
{
tapdisk_prep_tiocb(tiocb, fd, 1, buf, bytes, offset, cb, arg);
}
void
td_debug(td_image_t *image)
{
td_driver_t *driver;
driver = image->driver;
if (!driver || !td_flag_test(driver->state, TD_DRIVER_OPEN))
return;
tapdisk_driver_debug(driver);
}
+53
View File
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_INTERFACE_H_
#define _TAPDISK_INTERFACE_H_
#include "tapdisk.h"
#include "tapdisk-queue.h"
int td_open(td_image_t *);
int td_load(td_image_t *);
int td_close(td_image_t *);
int td_get_parent_id(td_image_t *, td_disk_id_t *);
int td_validate_parent(td_image_t *, td_image_t *);
void td_queue_write(td_image_t *, td_request_t);
void td_queue_read(td_image_t *, td_request_t);
void td_forward_request(td_request_t);
void td_complete_request(td_request_t, int);
void td_debug(td_image_t *);
void td_queue_tiocb(td_driver_t *, struct tiocb *);
void td_prep_read(struct tiocb *, int, char *, size_t,
long long, td_queue_callback_t, void *);
void td_prep_write(struct tiocb *, int, char *, size_t,
long long, td_queue_callback_t, void *);
#endif
+279
View File
@@ -0,0 +1,279 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "tapdisk.h"
#include "tapdisk-ipc.h"
#include "tapdisk-vbd.h"
#include "tapdisk-server.h"
static int
tapdisk_ipc_write_message(int fd, tapdisk_message_t *message, int timeout)
{
fd_set writefds;
int ret, len, offset;
struct timeval tv, *t;
t = NULL;
offset = 0;
len = sizeof(tapdisk_message_t);
if (timeout) {
tv.tv_sec = timeout;
tv.tv_usec = 0;
t = &tv;
}
DPRINTF("sending '%s' message (uuid = %u)\n",
tapdisk_message_name(message->type), message->cookie);
while (offset < len) {
FD_ZERO(&writefds);
FD_SET(fd, &writefds);
/* we don't bother reinitializing tv. at worst, it will wait a
* bit more time than expected. */
ret = select(fd + 1, NULL, &writefds, NULL, t);
if (ret == -1)
break;
else if (FD_ISSET(fd, &writefds)) {
ret = write(fd, message + offset, len - offset);
if (ret <= 0)
break;
offset += ret;
} else
break;
}
if (offset != len) {
EPRINTF("failure writing message\n");
return -EIO;
}
return 0;
}
int
tapdisk_ipc_write(td_ipc_t *ipc, int type)
{
tapdisk_message_t message;
if (ipc->wfd == -1)
return 0;
memset(&message, 0, sizeof(tapdisk_message_t));
message.type = type;
message.cookie = ipc->uuid;
return tapdisk_ipc_write_message(ipc->wfd, &message, 2);
}
int
tapdisk_ipc_write_error(td_ipc_t *ipc, const char *text)
{
tapdisk_message_t message;
memset(&message, 0, sizeof(message));
message.type = TAPDISK_MESSAGE_RUNTIME_ERROR;
message.cookie = ipc->uuid;
snprintf(message.u.string.text, sizeof(message.u.string.text), "%s", text);
return tapdisk_ipc_write_message(ipc->wfd, &message, 2);
}
static int
tapdisk_ipc_read_message(int fd, tapdisk_message_t *message, int timeout)
{
fd_set readfds;
int ret, len, offset;
struct timeval tv, *t;
t = NULL;
offset = 0;
len = sizeof(tapdisk_message_t);
if (timeout) {
tv.tv_sec = timeout;
tv.tv_usec = 0;
t = &tv;
}
memset(message, 0, sizeof(tapdisk_message_t));
while (offset < len) {
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
/* we don't bother reinitializing tv. at worst, it will wait a
* bit more time than expected. */
ret = select(fd + 1, &readfds, NULL, NULL, t);
if (ret == -1)
break;
else if (FD_ISSET(fd, &readfds)) {
ret = read(fd, message + offset, len - offset);
if (ret <= 0)
break;
offset += ret;
} else
break;
}
if (offset != len) {
EPRINTF("failure reading message\n");
return -EIO;
}
DPRINTF("received '%s' message (uuid = %u)\n",
tapdisk_message_name(message->type), message->cookie);
return 0;
}
int
tapdisk_ipc_read(td_ipc_t *ipc)
{
int err;
td_vbd_t *vbd;
td_uuid_t uuid;
tapdisk_message_t message;
err = tapdisk_ipc_read_message(ipc->rfd, &message, 2);
if (err) {
tapdisk_server_check_state();
return err;
}
uuid = message.cookie;
vbd = tapdisk_server_get_vbd(uuid);
if (!vbd && message.type != TAPDISK_MESSAGE_PID) {
EPRINTF("received message for non-existing vbd: %u\n", uuid);
err = -EINVAL;
goto fail;
}
switch (message.type) {
case TAPDISK_MESSAGE_PID:
err = tapdisk_vbd_initialize(ipc->rfd, ipc->wfd, uuid);
memset(&message, 0, sizeof(tapdisk_message_t));
message.cookie = uuid;
if (!err) {
message.type = TAPDISK_MESSAGE_PID_RSP;
message.u.tapdisk_pid = getpid();
} else
message.type = TAPDISK_MESSAGE_ERROR;
return tapdisk_ipc_write_message(ipc->wfd, &message, 0);
case TAPDISK_MESSAGE_OPEN:
{
image_t image;
char *devname;
td_flag_t flags;
flags = 0;
if (message.u.params.flags & TAPDISK_MESSAGE_FLAG_RDONLY)
flags |= TD_OPEN_RDONLY;
if (message.u.params.flags & TAPDISK_MESSAGE_FLAG_SHARED)
flags |= TD_OPEN_SHAREABLE;
if (message.u.params.flags & TAPDISK_MESSAGE_FLAG_ADD_CACHE)
flags |= TD_OPEN_ADD_CACHE;
if (message.u.params.flags & TAPDISK_MESSAGE_FLAG_VHD_INDEX)
flags |= TD_OPEN_VHD_INDEX;
if (message.u.params.flags & TAPDISK_MESSAGE_FLAG_LOG_DIRTY)
flags |= TD_OPEN_LOG_DIRTY;
err = asprintf(&devname, "%s/%s%d",
BLKTAP_DEV_DIR, BLKTAP_DEV_NAME,
message.u.params.devnum);
if (err == -1)
goto fail;
err = tapdisk_vbd_open(vbd,
message.u.params.path,
message.drivertype,
message.u.params.storage,
devname, flags);
free(devname);
if (err)
goto fail;
err = tapdisk_vbd_get_image_info(vbd, &image);
if (err)
goto fail;
memset(&message, 0, sizeof(tapdisk_message_t));
message.cookie = uuid;
message.u.image.sectors = image.size;
message.u.image.sector_size = image.secsize;
message.u.image.info = image.info;
message.type = TAPDISK_MESSAGE_OPEN_RSP;
return tapdisk_ipc_write_message(ipc->wfd, &message, 0);
}
case TAPDISK_MESSAGE_PAUSE:
tapdisk_vbd_pause(vbd);
return 0; /* response written asynchronously */
case TAPDISK_MESSAGE_RESUME:
tapdisk_vbd_resume(vbd,
message.u.params.path,
message.drivertype);
return 0; /* response written asynchronously */
case TAPDISK_MESSAGE_CLOSE:
tapdisk_vbd_close(vbd);
return 0; /* response written asynchronously */
case TAPDISK_MESSAGE_EXIT:
return 0;
}
err = -EINVAL;
EPRINTF("received unrecognized message %s, uuid = %d\n",
tapdisk_message_name(message.type), uuid);
fail:
memset(&message, 0, sizeof(tapdisk_message_t));
message.cookie = uuid;
message.type = TAPDISK_MESSAGE_ERROR;
tapdisk_ipc_write_message(ipc->wfd, &message, 2);
tapdisk_server_check_state();
return -err;
}
+43
View File
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_IPC_H_
#define _TAPDISK_IPC_H_
#include "tapdisk-message.h"
typedef struct td_ipc_handle {
int rfd;
int wfd;
td_uuid_t uuid;
} td_ipc_t;
int tapdisk_ipc_read(td_ipc_t *ipc);
int tapdisk_ipc_write(td_ipc_t *ipc, int type);
int tapdisk_ipc_write_error(td_ipc_t *ipc, const char *message);
#endif
+255
View File
@@ -0,0 +1,255 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <syslog.h>
#include <inttypes.h>
#include <sys/time.h>
#include "tapdisk-log.h"
#define MAX_ENTRY_LEN 512
#define MAX_ERROR_MESSAGES 16
struct error {
int cnt;
int err;
char *func;
char msg[MAX_ENTRY_LEN];
};
struct ehandle {
int cnt;
int dropped;
struct error errors[MAX_ERROR_MESSAGES];
};
struct tlog {
char *p;
int size;
uint64_t cnt;
char *buf;
int level;
char *file;
int append;
};
static struct ehandle tapdisk_err;
static struct tlog tapdisk_log;
void
open_tlog(char *file, size_t bytes, int level, int append)
{
tapdisk_log.size = ((bytes + 511) & (~511));
if (asprintf(&tapdisk_log.file, "%s.%d", file, getpid()) == -1)
return;
if (posix_memalign((void **)&tapdisk_log.buf, 512, tapdisk_log.size)) {
free(tapdisk_log.file);
tapdisk_log.buf = NULL;
return;
}
memset(tapdisk_log.buf, 0, tapdisk_log.size);
tapdisk_log.p = tapdisk_log.buf;
tapdisk_log.level = level;
tapdisk_log.append = append;
}
void
close_tlog(void)
{
if (!tapdisk_log.buf)
return;
if (tapdisk_log.append)
tlog_flush();
free(tapdisk_log.buf);
free(tapdisk_log.file);
memset(&tapdisk_log, 0, sizeof(struct tlog));
}
void
__tlog_write(int level, const char *func, const char *fmt, ...)
{
char *buf;
va_list ap;
struct timeval t;
int ret, len, avail;
if (!tapdisk_log.buf)
return;
if (level > tapdisk_log.level)
return;
avail = tapdisk_log.size - (tapdisk_log.p - tapdisk_log.buf);
if (avail < MAX_ENTRY_LEN) {
if (tapdisk_log.append)
tlog_flush();
tapdisk_log.p = tapdisk_log.buf;
}
buf = tapdisk_log.p;
gettimeofday(&t, NULL);
len = snprintf(buf, MAX_ENTRY_LEN - 1, "%08"PRIu64":%010ld.%06ld:"
"%s ", tapdisk_log.cnt, t.tv_sec, t.tv_usec, func);
va_start(ap, fmt);
ret = vsnprintf(buf + len, MAX_ENTRY_LEN - (len + 1), fmt, ap);
va_end(ap);
len = (ret < MAX_ENTRY_LEN - (len + 1) ?
len + ret : MAX_ENTRY_LEN - 1);
buf[len] = '\0';
tapdisk_log.cnt++;
tapdisk_log.p += len;
}
void
__tlog_error(int err, const char *func, const char *fmt, ...)
{
va_list ap;
int i, len, ret;
struct error *e;
struct timeval t;
err = (err > 0 ? err : -err);
for (i = 0; i < tapdisk_err.cnt; i++) {
e = &tapdisk_err.errors[i];
if (e->err == err && e->func == func) {
e->cnt++;
return;
}
}
if (tapdisk_err.cnt >= MAX_ERROR_MESSAGES) {
tapdisk_err.dropped++;
return;
}
gettimeofday(&t, NULL);
e = &tapdisk_err.errors[tapdisk_err.cnt];
len = snprintf(e->msg, MAX_ENTRY_LEN - 1, "%010ld.%06ld:%s ",
t.tv_sec, t.tv_usec, func);
va_start(ap, fmt);
ret = vsnprintf(e->msg + len, MAX_ENTRY_LEN - (len + 1), fmt, ap);
va_end(ap);
len = (ret < MAX_ENTRY_LEN - (len + 1) ?
len + ret : MAX_ENTRY_LEN - 1);
e->msg[len] = '\0';
e->cnt++;
e->err = err;
e->func = (char *)func;
tapdisk_err.cnt++;
}
void
tlog_print_errors(void)
{
int i;
struct error *e;
for (i = 0; i < tapdisk_err.cnt; i++) {
e = &tapdisk_err.errors[i];
syslog(LOG_INFO, "TAPDISK ERROR: errno %d at %s (cnt = %d): "
"%s\n", e->err, e->func, e->cnt, e->msg);
}
if (tapdisk_err.dropped)
syslog(LOG_INFO, "TAPDISK ERROR: %d other error messages "
"dropped\n", tapdisk_err.dropped);
}
void
tlog_flush_errors(void)
{
int i;
struct error *e;
for (i = 0; i < tapdisk_err.cnt; i++) {
e = &tapdisk_err.errors[i];
tlog_write(TLOG_WARN, "TAPDISK ERROR: errno %d at %s "
"(cnt = %d): %s\n", e->err, e->func, e->cnt,
e->msg);
}
if (tapdisk_err.dropped)
tlog_write(TLOG_WARN, "TAPDISK ERROR: %d other error messages "
"dropped\n", tapdisk_err.dropped);
}
void
tlog_flush(void)
{
int fd, flags;
size_t size, wsize;
if (!tapdisk_log.buf)
return;
flags = O_CREAT | O_WRONLY | O_DIRECT | O_NONBLOCK;
if (!tapdisk_log.append)
flags |= O_TRUNC;
fd = open(tapdisk_log.file, flags, 0644);
if (fd == -1)
return;
if (tapdisk_log.append)
if (lseek64(fd, 0, SEEK_END) == (loff_t)-1)
goto out;
tlog_flush_errors();
size = tapdisk_log.p - tapdisk_log.buf;
wsize = ((size + 511) & (~511));
memset(tapdisk_log.buf + size, '\n', wsize - size);
write(fd, tapdisk_log.buf, wsize);
tapdisk_log.p = tapdisk_log.buf;
out:
close(fd);
}
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_LOG_H_
#define _TAPDISK_LOG_H_
#define TLOG_WARN 0
#define TLOG_INFO 1
#define TLOG_DBG 2
void open_tlog(char *file, size_t bytes, int level, int append);
void close_tlog(void);
void tlog_flush(void);
void tlog_print_errors(void);
void __tlog_write(int level, const char *func, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
void __tlog_error(int err, const char *func, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
#define tlog_write(_level, _f, _a...) \
__tlog_write(_level, __func__, _f, ##_a)
#define tlog_error(_err, _f, _a...) \
__tlog_error(_err, __func__, _f, ##_a)
#endif
+441
View File
@@ -0,0 +1,441 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <libaio.h>
#include "tapdisk.h"
#include "tapdisk-log.h"
#include "tapdisk-queue.h"
#include "tapdisk-filter.h"
#include "atomicio.h"
#define WARN(_f, _a...) tlog_write(TLOG_WARN, _f, ##_a)
#define DBG(_f, _a...) tlog_write(TLOG_DBG, _f, ##_a)
#define ERR(_err, _f, _a...) tlog_error(_err, _f, ##_a)
/*
* We used a kernel patch to return an fd associated with the AIO context
* so that we can concurrently poll on synchronous and async descriptors.
* This is signalled by passing 1 as the io context to io_setup.
*/
#define REQUEST_ASYNC_FD 1
static inline void
queue_tiocb(struct tqueue *queue, struct tiocb *tiocb)
{
struct iocb *iocb = &tiocb->iocb;
if (queue->queued) {
struct tiocb *prev = (struct tiocb *)
queue->iocbs[queue->queued - 1]->data;
prev->next = tiocb;
}
queue->iocbs[queue->queued++] = iocb;
}
static inline int
deferred_tiocbs(struct tqueue *queue)
{
return (queue->deferred.head != NULL);
}
static inline void
defer_tiocb(struct tqueue *queue, struct tiocb *tiocb)
{
struct tlist *list = &queue->deferred;
if (!list->head)
list->head = list->tail = tiocb;
else
list->tail = list->tail->next = tiocb;
queue->tiocbs_deferred++;
queue->deferrals++;
}
static inline void
queue_deferred_tiocb(struct tqueue *queue)
{
struct tlist *list = &queue->deferred;
if (list->head) {
struct tiocb *tiocb = list->head;
list->head = tiocb->next;
if (!list->head)
list->tail = NULL;
queue_tiocb(queue, tiocb);
queue->tiocbs_deferred--;
}
}
static inline void
queue_deferred_tiocbs(struct tqueue *queue)
{
while (!tapdisk_queue_full(queue) && deferred_tiocbs(queue))
queue_deferred_tiocb(queue);
}
/*
* td_complete may queue more tiocbs
*/
static void
complete_tiocb(struct tqueue *queue, struct tiocb *tiocb, unsigned long res)
{
int err;
struct iocb *iocb = &tiocb->iocb;
if (res == iocb->u.c.nbytes)
err = 0;
else if ((int)res < 0)
err = (int)res;
else
err = -EIO;
tiocb->cb(tiocb->arg, tiocb, err);
}
static int
cancel_tiocbs(struct tqueue *queue, int err)
{
int queued;
struct tiocb *tiocb;
if (!queue->queued)
return 0;
/*
* td_complete may queue more tiocbs, which
* will overwrite the contents of queue->iocbs.
* use a private linked list to keep track
* of the tiocbs we're cancelling.
*/
tiocb = (struct tiocb *)queue->iocbs[0]->data;
queued = queue->queued;
queue->queued = 0;
for (; tiocb != NULL; tiocb = tiocb->next)
complete_tiocb(queue, tiocb, err);
return queued;
}
static int
fail_tiocbs(struct tqueue *queue, int succeeded, int total, int err)
{
ERR(err, "io_submit error: %d of %d failed",
total - succeeded, total);
/* take any non-submitted, merged iocbs
* off of the queue, split them, and fail them */
queue->queued = io_expand_iocbs(&queue->opioctx,
queue->iocbs, succeeded, total);
return cancel_tiocbs(queue, err);
}
static inline ssize_t
iocb_rw(struct iocb *iocb)
{
int fd = iocb->aio_fildes;
char *buf = iocb->u.c.buf;
long long off = iocb->u.c.offset;
size_t size = iocb->u.c.nbytes;
ssize_t (*func)(int, void *, size_t) =
(iocb->aio_lio_opcode == IO_CMD_PWRITE ? vwrite : read);
if (lseek64(fd, off, SEEK_SET) == (off64_t)-1)
return -errno;
if (atomicio(func, fd, buf, size) != size)
return -errno;
return size;
}
static int
io_synchronous_rw(struct tqueue *queue)
{
int i, merged, split;
struct iocb *iocb;
struct tiocb *tiocb;
struct io_event *ep;
if (!queue->queued)
return 0;
tapdisk_filter_iocbs(queue->filter, queue->iocbs, queue->queued);
merged = io_merge(&queue->opioctx, queue->iocbs, queue->queued);
queue->queued = 0;
for (i = 0; i < merged; i++) {
ep = queue->aio_events + i;
iocb = queue->iocbs[i];
ep->obj = iocb;
ep->res = iocb_rw(iocb);
}
split = io_split(&queue->opioctx, queue->aio_events, merged);
tapdisk_filter_events(queue->filter, queue->aio_events, split);
for (i = split, ep = queue->aio_events; i-- > 0; ep++) {
iocb = ep->obj;
tiocb = (struct tiocb *)iocb->data;
complete_tiocb(queue, tiocb, ep->res);
}
queue_deferred_tiocbs(queue);
return split;
}
int
tapdisk_init_queue(struct tqueue *queue, int size,
int sync, struct tfilter *filter)
{
int i, err;
memset(queue, 0, sizeof(struct tqueue));
queue->size = size;
queue->sync = sync;
queue->filter = filter;
if (sync) {
/* set up a pipe so we can return
* a poll fd that won't fire. */
if (pipe(queue->dummy_pipe))
return -errno;
queue->poll_fd = queue->dummy_pipe[0];
} else {
queue->aio_ctx = (io_context_t)REQUEST_ASYNC_FD;
queue->poll_fd = io_setup(size, &queue->aio_ctx);
if (queue->poll_fd < 0) {
if (queue->poll_fd == -EAGAIN)
DPRINTF("Couldn't setup AIO context. If you "
"are trying to concurrently use a "
"large number of blktap-based disks, "
"you may need to increase the "
"system-wide aio request limit. "
"(e.g. 'echo 1048576 > /proc/sys/fs/"
"aio-max-nr')\n");
else
DPRINTF("Couldn't get fd for AIO poll "
"support. This is probably because "
"your kernel does not have the "
"aio-poll patch applied.\n");
return queue->poll_fd;
}
}
err = -ENOMEM;
queue->iocbs = calloc(size, sizeof(struct iocb *));
queue->aio_events = calloc(size, sizeof(struct io_event));
if (!queue->iocbs || !queue->aio_events)
goto fail;
err = opio_init(&queue->opioctx, size);
if (err)
goto fail;
return 0;
fail:
tapdisk_free_queue(queue);
return err;
}
void
tapdisk_free_queue(struct tqueue *queue)
{
if (queue->sync) {
close(queue->dummy_pipe[0]);
close(queue->dummy_pipe[1]);
} else
io_destroy(queue->aio_ctx);
free(queue->iocbs);
free(queue->aio_events);
opio_free(&queue->opioctx);
}
void
tapdisk_debug_queue(struct tqueue *queue)
{
struct tiocb *tiocb = queue->deferred.head;
WARN("TAPDISK QUEUE:\n");
WARN("size: %d, sync: %d, queued: %d, iocbs_pending: %d, "
"tiocbs_pending: %d, tiocbs_deferred: %d, deferrals: %"PRIx64"\n",
queue->size, queue->sync, queue->queued, queue->iocbs_pending,
queue->tiocbs_pending, queue->tiocbs_deferred, queue->deferrals);
if (tiocb) {
WARN("deferred:\n");
for (; tiocb != NULL; tiocb = tiocb->next) {
struct iocb *io = &tiocb->iocb;
WARN("%s of %lu bytes at %lld\n",
(io->aio_lio_opcode == IO_CMD_PWRITE ?
"write" : "read"),
io->u.c.nbytes, io->u.c.offset);
}
}
}
void
tapdisk_prep_tiocb(struct tiocb *tiocb, int fd, int rw, char *buf, size_t size,
long long offset, td_queue_callback_t cb, void *arg)
{
struct iocb *iocb = &tiocb->iocb;
if (rw)
io_prep_pwrite(iocb, fd, buf, size, offset);
else
io_prep_pread(iocb, fd, buf, size, offset);
iocb->data = tiocb;
tiocb->cb = cb;
tiocb->arg = arg;
tiocb->next = NULL;
}
void
tapdisk_queue_tiocb(struct tqueue *queue, struct tiocb *tiocb)
{
if (!tapdisk_queue_full(queue))
queue_tiocb(queue, tiocb);
else
defer_tiocb(queue, tiocb);
}
/*
* fail_tiocbs may queue more tiocbs
*/
int
tapdisk_submit_tiocbs(struct tqueue *queue)
{
int merged, submitted, err = 0;
if (!queue->queued)
return 0;
if (queue->sync)
return io_synchronous_rw(queue);
tapdisk_filter_iocbs(queue->filter, queue->iocbs, queue->queued);
merged = io_merge(&queue->opioctx, queue->iocbs, queue->queued);
submitted = io_submit(queue->aio_ctx, merged, queue->iocbs);
DBG("queued: %d, merged: %d, submitted: %d\n",
queue->queued, merged, submitted);
if (submitted < 0) {
err = submitted;
submitted = 0;
} else if (submitted < merged)
err = -EIO;
queue->iocbs_pending += submitted;
queue->tiocbs_pending += queue->queued;
queue->queued = 0;
if (err)
queue->tiocbs_pending -=
fail_tiocbs(queue, submitted, merged, err);
return submitted;
}
int
tapdisk_submit_all_tiocbs(struct tqueue *queue)
{
int submitted = 0;
do {
submitted += tapdisk_submit_tiocbs(queue);
} while (!tapdisk_queue_empty(queue));
return submitted;
}
int
tapdisk_complete_tiocbs(struct tqueue *queue)
{
int i, ret, split;
struct iocb *iocb;
struct tiocb *tiocb;
struct io_event *ep;
ret = io_getevents(queue->aio_ctx, 0,
queue->size, queue->aio_events, NULL);
split = io_split(&queue->opioctx, queue->aio_events, ret);
tapdisk_filter_events(queue->filter, queue->aio_events, split);
DBG("events: %d, tiocbs: %d\n", ret, split);
queue->iocbs_pending -= ret;
queue->tiocbs_pending -= split;
for (i = split, ep = queue->aio_events; i-- > 0; ep++) {
iocb = ep->obj;
tiocb = (struct tiocb *)iocb->data;
complete_tiocb(queue, tiocb, ep->res);
}
queue_deferred_tiocbs(queue);
return split;
}
/*
* cancel_tiocbs may queue more tiocbs
*/
int
tapdisk_cancel_tiocbs(struct tqueue *queue)
{
return cancel_tiocbs(queue, -EIO);
}
int
tapdisk_cancel_all_tiocbs(struct tqueue *queue)
{
int cancelled = 0;
do {
cancelled += tapdisk_cancel_tiocbs(queue);
} while (!tapdisk_queue_empty(queue));
return cancelled;
}
+113
View File
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TAPDISK_QUEUE_H
#define TAPDISK_QUEUE_H
#include <libaio.h>
#include "io-optimize.h"
struct tiocb;
struct tfilter;
typedef void (*td_queue_callback_t)(void *arg, struct tiocb *, int err);
struct tiocb {
td_queue_callback_t cb;
void *arg;
struct iocb iocb;
struct tiocb *next;
};
struct tlist {
struct tiocb *head;
struct tiocb *tail;
};
struct tqueue {
int size;
int sync;
int poll_fd;
io_context_t aio_ctx;
struct opioctx opioctx;
int dummy_pipe[2];
int queued;
struct iocb **iocbs;
struct io_event *aio_events;
/* number of iocbs pending in the aio layer */
int iocbs_pending;
/* number of tiocbs pending in the queue --
* this is likely to be larger than iocbs_pending
* due to request coalescing */
int tiocbs_pending;
/* iocbs may be deferred if the aio ring is full.
* tapdisk_queue_complete will ensure deferred
* iocbs are queued as slots become available. */
struct tlist deferred;
int tiocbs_deferred;
/* optional tapdisk filter */
struct tfilter *filter;
uint64_t deferrals;
};
/*
* Interface for request producer (i.e., tapdisk)
* NB: the following functions may cause additional tiocbs to be queued:
* - tapdisk_submit_tiocbs
* - tapdisk_cancel_tiocbs
* - tapdisk_complete_tiocbs
* The *_all_tiocbs variants will handle the first two cases;
* be sure to call submit after calling complete in the third case.
*/
#define tapdisk_queue_count(q) ((q)->queued)
#define tapdisk_queue_empty(q) ((q)->queued == 0)
#define tapdisk_queue_full(q) \
(((q)->tiocbs_pending + (q)->queued) >= (q)->size)
int tapdisk_init_queue(struct tqueue *, int size, int sync, struct tfilter *);
void tapdisk_free_queue(struct tqueue *);
void tapdisk_debug_queue(struct tqueue *);
void tapdisk_queue_tiocb(struct tqueue *, struct tiocb *);
int tapdisk_submit_tiocbs(struct tqueue *);
int tapdisk_submit_all_tiocbs(struct tqueue *);
int tapdisk_complete_tiocbs(struct tqueue *);
int tapdisk_cancel_tiocbs(struct tqueue *);
int tapdisk_cancel_all_tiocbs(struct tqueue *);
void tapdisk_prep_tiocb(struct tiocb *, int, int, char *, size_t,
long long, td_queue_callback_t, void *);
#endif
+439
View File
@@ -0,0 +1,439 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include "tapdisk-ring.h"
static int
tapdisk_uring_create_ctlfd(td_uring_t *ring)
{
int fd, err;
struct sockaddr_un saddr;
if (strnlen(ring->ctlfd_path, sizeof(saddr.sun_family)) >=
sizeof(saddr.sun_family))
return -ENAMETOOLONG;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
return -errno;
memset(&saddr, 0, sizeof(struct sockaddr_un));
saddr.sun_family = AF_UNIX;
memcpy(saddr.sun_path, ring->ctlfd_path, strlen(ring->ctlfd_path));
err = unlink(ring->ctlfd_path);
if (err == -1 && errno != ENOENT) {
err = -errno;
goto fail;
}
err = bind(fd, &saddr, sizeof(struct sockaddr_un));
if (err == -1) {
err = -errno;
goto fail;
}
err = listen(fd, 1);
if (err == -1) {
err = -errno;
goto fail;
}
ring->ctlfd = fd;
return 0;
fail:
close(fd);
return err;
}
static void
tapdisk_uring_destroy_ctlfd(td_uring_t *ring)
{
if (ring->ctlfd) {
close(ring->ctlfd);
ring->ctlfd = 0;
}
if (ring->ctlfd_path) {
unlink(ring->ctlfd_path);
free(ring->ctlfd_path);
ring->ctlfd_path = NULL;
}
}
static int
tapdisk_uring_connect_ctlfd(td_uring_t *ring)
{
int fd, err;
struct sockaddr_un saddr;
if (strnlen(ring->ctlfd_path, sizeof(saddr.sun_path)) >=
sizeof(saddr.sun_path))
return -ENAMETOOLONG;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
return -errno;
memset(&saddr, 0, sizeof(struct sockaddr_un));
saddr.sun_family = AF_UNIX;
memcpy(saddr.sun_path, ring->ctlfd_path, strlen(ring->ctlfd_path));
err = connect(fd, &saddr, sizeof(saddr));
if (err == -1) {
err = -errno;
goto fail;
}
ring->ctlfd = fd;
return 0;
fail:
close(fd);
return err;
}
static void
tapdisk_uring_disconnect_ctlfd(td_uring_t *ring)
{
if (ring->ctlfd)
close(ring->ctlfd);
free(ring->ctlfd_path);
ring->ctlfd_path = NULL;
}
static int
tapdisk_uring_create_shmem(td_uring_t *ring)
{
int fd, err;
fd = shm_open(ring->shmem_path, O_CREAT | O_RDWR, 0750);
if (fd == -1)
return -errno;
err = ftruncate(fd, ring->shmem_size);
if (err == -1) {
err = -errno;
goto out;
}
ring->shmem = mmap(NULL, ring->shmem_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ring->shmem == MAP_FAILED) {
ring->shmem = NULL;
err = -errno;
goto out;
}
err = 0;
out:
close(fd);
return err;
}
static void
tapdisk_uring_destroy_shmem(td_uring_t *ring)
{
if (ring->shmem) {
munmap(ring->shmem, ring->shmem_size);
ring->shmem = NULL;
}
if (ring->shmem_path) {
shm_unlink(ring->shmem_path);
free(ring->shmem_path);
ring->shmem_path = NULL;
}
}
static int
tapdisk_uring_connect_shmem(td_uring_t *ring)
{
int fd, err;
td_uring_header_t header, *p;
fd = shm_open(ring->shmem_path, O_RDWR);
if (fd == -1)
return -errno;
p = mmap(NULL, sizeof(td_uring_header_t),
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
err = -errno;
goto out;
}
memcpy(&header, p, sizeof(td_uring_header_t));
munmap(p, sizeof(td_uring_header_t));
if (memcmp(header.cookie,
TAPDISK_URING_COOKIE, sizeof(header.cookie))) {
err = -EINVAL;
goto out;
}
if (header.version != TD_URING_CURRENT_VERSION) {
err = -EINVAL;
goto out;
}
ring->ring_size = header.ring_size;
ring->data_size = header.data_size;
ring->shmem_size = header.shmem_size;
ring->shmem = mmap(NULL, ring->shmem_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (ring->shmem == MAP_FAILED) {
rint->shmem = NULL;
err = -errno;
goto out;
}
err = 0;
out:
close(fd);
return err;
}
static void
tapdisk_uring_disconnect_shmem(td_uring_t *ring)
{
if (ring->shmem)
munmap(ring->shmem, ring->shmem_size);
free(ring->shmem_path);
ring->shmem_path = NULL;
}
int
tapdisk_uring_create(td_uring_t *ring, const char *location,
uint32_t ring_size, uint32_t data_size)
{
int fd, err;
memset(ring, 0, sizeof(td_uring_t));
ring->ring_size = ring_size;
ring->data_size = data_size;
ring->shmem_size = ring_size + data_size + sizeof(td_uring_header_t);
err = asprintf(&ring->shmem_path, "%s.shm", location);
if (err == -1) {
ring->shmem_path = NULL;
err = -errno;
goto fail;
}
err = asprintf(&ring->ctlfd_path, "%s.cfd", location);
if (err == -1) {
ring->ctlfd_path = NULL;
err = -errno;
goto fail;
}
err = tapdisk_uring_create_ctlfd(ring);
if (err)
goto fail;
err = tapdisk_uring_create_shmem(ring);
if (err)
goto fail;
ring->ring_area = (unsigned long)ring->shmem + sizeof(td_uring_header_t);
ring->data_area = (unsigned long)ring->ring_area + ring->ring_size;
return 0;
fail:
tapdisk_uring_destroy(ring);
return err;
}
int
tapdisk_uring_destroy(td_uring_t *ring)
{
tapdisk_uring_destroy_shmem(ring);
tapdisk_uring_destroy_ctlfd(ring);
return 0;
}
int
tapdisk_uring_connect(td_uring_t *ring, const char *location)
{
int fd, err;
memset(ring, 0, sizeof(td_uring_t));
err = asprintf(&ring->shmem_path, "%s.shm", location);
if (err == -1) {
ring->shmem_path = NULL;
err = -errno;
goto fail;
}
err = asprintf(&ring->ctlfd_path, "%s.cfd", location);
if (err == -1) {
ring->ctlfd_path = NULL;
err = -errno;
goto fail;
}
err = tapdisk_uring_connect_ctlfd(ring);
if (err)
goto fail;
err = tapdisk_uring_connect_shmem(ring);
if (err)
goto fail;
err = 0;
fail:
}
int
tapdisk_uring_disconnect(td_uring_t *ring)
{
tapdisk_uring_disconnect_shmem(ring);
tapdisk_uring_disconnect_ctlfd(ring);
return 0;
}
static int
tapdisk_ring_read_message(int fd, td_uring_message_t *message, int timeout)
{
fd_set readfds;
int ret, len, offset;
struct timeval tv, *t;
t = NULL;
offset = 0;
len = sizeof(td_uring_message_t);
if (timeout) {
tv.tv_sec = timeout;
tv.tv_usec = 0;
t = &tv;
}
while (offset < len) {
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
/* we don't bother reinitializing tv. at worst, it will wait a
* bit more time than expected. */
ret = select(fd + 1, &readfds, NULL, NULL, t);
if (ret == -1)
break;
else if (FD_ISSET(fd, &readfds)) {
ret = read(fd, message + offset, len - offset);
if (ret <= 0)
break;
offset += ret;
} else
break;
}
if (offset != len)
return -EIO;
return 0;
}
static int
tapdisk_ring_write_message(int fd, td_uring_message_t *message, int timeout)
{
fd_set writefds;
int ret, len, offset;
struct timeval tv, *t;
t = NULL;
offset = 0;
len = sizeof(td_uring_message_t);
if (timeout) {
tv.tv_sec = timeout;
tv.tv_usec = 0;
t = &tv;
}
while (offset < len) {
FD_ZERO(&writefds);
FD_SET(fd, &writefds);
/* we don't bother reinitializing tv. at worst, it will wait a
* bit more time than expected. */
ret = select(fd + 1, NULL, &writefds, NULL, t);
if (ret == -1)
break;
else if (FD_ISSET(fd, &writefds)) {
ret = write(fd, message + offset, len - offset);
if (ret <= 0)
break;
offset += ret;
} else
break;
}
if (offset != len)
return -EIO;
return 0;
}
int
tapdisk_uring_poll(td_uring_t *ring)
{
int err;
td_uring_message_t message;
err = tapdisk_uring_read_message(ring->ctlfd, &message, 1);
if (err)
return err;
if (message.type != TAPDISK_URING_MESSAGE_KICK)
return -EINVAL;
return 0;
}
int
tapdisk_uring_kick(td_uring_t *ring)
{
td_uring_message_t message;
memset(&message, 0, sizeof(td_uring_message_t));
message.type = TAPDISK_URING_MESSAGE_KICK;
return tapdisk_uring_write_message(ring->ctlfd, &message, 1);
}
+87
View File
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_RING_H_
#define _TAPDISK_RING_H_
#include <inttypes.h>
#include <xenctrl.h>
#include <xen/io/ring.h>
typedef struct td_uring td_uring_t;
typedef struct td_uring_header td_uring_header_t;
typedef struct td_uring_request td_uring_request_t;
typedef struct td_uring_response td_uring_response_t;
struct td_uring {
int ctlfd;
char *shmem_path;
char *ctlfd_path;
void *shmem;
void *ring_area;
void *data_area;
};
struct td_uring_header {
char cookie[8];
uint32_t version;
uint32_t shmem_size;
uint32_t ring_size;
uint32_t data_size;
char reserved[4064];
};
struct td_uring_request {
uint8_t op;
uint64_t id;
uint64_t sec;
uint32_t secs;
uint32_t offset;
};
struct td_uring_response {
uint8_t op;
uint64_t id;
uint8_t status;
};
DEFINE_RING_TYPES(td_uring, td_uring_request_t, td_uring_response_t);
int tapdisk_uring_create(td_uring_t *, const char *location,
uint32_t ring_size, uint32_t data_size);
int tapdisk_uring_destroy(td_uring_t *);
int tapdisk_uring_connect(td_uring_t *, const char *location);
int tapdisk_uring_disconnect(td_uring_t *);
int tapdisk_uring_poll(td_uring_t *);
int tapdisk_uring_kick(td_uring_t *);
#endif
+415
View File
@@ -0,0 +1,415 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#define TAPDISK
#include "tapdisk-utils.h"
#include "tapdisk-server.h"
#include "tapdisk-driver.h"
#include "tapdisk-interface.h"
#define DBG(_level, _f, _a...) tlog_write(_level, _f, ##_a)
#define ERR(_err, _f, _a...) tlog_error(_err, _f, ##_a)
tapdisk_server_t server;
#define tapdisk_server_for_each_vbd(vbd, tmp) \
list_for_each_entry_safe(vbd, tmp, &server.vbds, next)
struct tap_disk *
tapdisk_server_find_driver_interface(int type)
{
int n;
n = sizeof(dtypes) / sizeof(struct disk_info_t *);
if (type > n)
return NULL;
return dtypes[type]->drv;
}
td_image_t *
tapdisk_server_get_shared_image(td_image_t *image)
{
td_vbd_t *vbd, *tmpv;
td_image_t *img, *tmpi;
if (!td_flag_test(image->flags, TD_OPEN_SHAREABLE))
return NULL;
tapdisk_server_for_each_vbd(vbd, tmpv)
tapdisk_vbd_for_each_image(vbd, img, tmpi)
if (img->type == image->type &&
!strcmp(img->name, image->name))
return img;
return NULL;
}
td_vbd_t *
tapdisk_server_get_vbd(uint16_t uuid)
{
td_vbd_t *vbd, *tmp;
tapdisk_server_for_each_vbd(vbd, tmp)
if (vbd->uuid == uuid)
return vbd;
return NULL;
}
void
tapdisk_server_add_vbd(td_vbd_t *vbd)
{
list_add_tail(&vbd->next, &server.vbds);
}
void
tapdisk_server_remove_vbd(td_vbd_t *vbd)
{
list_del(&vbd->next);
INIT_LIST_HEAD(&vbd->next);
tapdisk_server_check_state();
}
void
tapdisk_server_queue_tiocb(struct tiocb *tiocb)
{
tapdisk_queue_tiocb(&server.aio_queue, tiocb);
}
void
tapdisk_server_debug(void)
{
td_vbd_t *vbd, *tmp;
tapdisk_debug_queue(&server.aio_queue);
tapdisk_server_for_each_vbd(vbd, tmp)
tapdisk_vbd_debug(vbd);
tlog_flush();
}
void
tapdisk_server_check_state(void)
{
if (list_empty(&server.vbds))
server.run = 0;
}
event_id_t
tapdisk_server_register_event(char mode, int fd,
int timeout, event_cb_t cb, void *data)
{
return scheduler_register_event(&server.scheduler,
mode, fd, timeout, cb, data);
}
void
tapdisk_server_unregister_event(event_id_t event)
{
return scheduler_unregister_event(&server.scheduler, event);
}
void
tapdisk_server_set_max_timeout(int seconds)
{
scheduler_set_max_timeout(&server.scheduler, seconds);
}
static void
tapdisk_server_assert_locks(void)
{
}
static void
tapdisk_server_set_retry_timeout(void)
{
td_vbd_t *vbd, *tmp;
tapdisk_server_for_each_vbd(vbd, tmp)
if (tapdisk_vbd_retry_needed(vbd)) {
tapdisk_server_set_max_timeout(TD_VBD_RETRY_INTERVAL);
return;
}
}
static void
tapdisk_server_check_progress(void)
{
struct timeval now;
td_vbd_t *vbd, *tmp;
gettimeofday(&now, NULL);
tapdisk_server_for_each_vbd(vbd, tmp)
tapdisk_vbd_check_progress(vbd);
}
static void
tapdisk_server_submit_tiocbs(void)
{
tapdisk_submit_all_tiocbs(&server.aio_queue);
}
static void
tapdisk_server_kick_responses(void)
{
int n;
td_vbd_t *vbd, *tmp;
tapdisk_server_for_each_vbd(vbd, tmp)
tapdisk_vbd_kick(vbd);
}
static void
tapdisk_server_check_vbds(void)
{
td_vbd_t *vbd, *tmp;
tapdisk_server_for_each_vbd(vbd, tmp)
tapdisk_vbd_check_state(vbd);
}
static void
tapdisk_server_stop_vbds(void)
{
td_vbd_t *vbd, *tmp;
tapdisk_server_for_each_vbd(vbd, tmp)
tapdisk_vbd_kill_queue(vbd);
}
static void
tapdisk_server_send_error(const char *message)
{
td_vbd_t *vbd, *tmp;
tapdisk_server_for_each_vbd(vbd, tmp)
tapdisk_ipc_write_error(&vbd->ipc, message);
}
static void
tapdisk_server_read_ipc_message(event_id_t id, char mode, void *private)
{
tapdisk_ipc_read(&server.ipc);
}
static void
tapdisk_server_aio_queue_event(event_id_t id, char mode, void *private)
{
tapdisk_complete_tiocbs(&server.aio_queue);
}
static void
tapdisk_server_free_aio_queue(void)
{
tapdisk_server_unregister_event(server.aio_queue_event_id);
tapdisk_free_queue(&server.aio_queue);
}
static int
tapdisk_server_initialize_aio_queue(void)
{
int err;
event_id_t id;
err = tapdisk_init_queue(&server.aio_queue,
TAPDISK_TIOCBS, 0, NULL);
if (err)
return err;
id = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
server.aio_queue.poll_fd, 0,
tapdisk_server_aio_queue_event,
NULL);
if (id < 0) {
tapdisk_free_queue(&server.aio_queue);
return id;
}
server.aio_queue_event_id = id;
return 0;
}
static void
tapdisk_server_close(void)
{
tapdisk_server_free_aio_queue();
if (server.control_event)
scheduler_unregister_event(&server.scheduler, server.control_event);
if (server.ipc.rfd != -1)
close(server.ipc.rfd);
if (server.ipc.wfd != -1)
close(server.ipc.wfd);
}
static void
__tapdisk_server_run(void)
{
int ret;
while (server.run) {
tapdisk_server_assert_locks();
tapdisk_server_set_retry_timeout();
tapdisk_server_check_progress();
ret = scheduler_wait_for_events(&server.scheduler);
if (ret < 0)
DBG(TLOG_WARN, "server wait returned %d\n", ret);
tapdisk_server_check_vbds();
tapdisk_server_submit_tiocbs();
tapdisk_server_kick_responses();
}
}
static void
tapdisk_server_signal_handler(int signal)
{
td_vbd_t *vbd, *tmp;
static int xfsz_error_sent = 0;
switch (signal) {
case SIGBUS:
case SIGINT:
tapdisk_server_for_each_vbd(vbd, tmp)
tapdisk_vbd_close(vbd);
break;
case SIGXFSZ:
ERR(EFBIG, "received SIGXFSZ");
tapdisk_server_stop_vbds();
if (xfsz_error_sent)
break;
tapdisk_server_send_error("received SIGXFSZ, closing queues");
xfsz_error_sent = 1;
break;
case SIGUSR1:
tapdisk_server_debug();
break;
}
}
int
tapdisk_server_initialize(const char *read, const char *write)
{
int err;
event_id_t event_id;
event_id = 0;
memset(&server, 0, sizeof(tapdisk_server_t));
server.ipc.rfd = server.ipc.wfd = -1;
INIT_LIST_HEAD(&server.vbds);
if (read) {
server.ipc.rfd = open(read, O_RDWR | O_NONBLOCK);
if (server.ipc.rfd < 0) {
err = -errno;
EPRINTF("FD open failed %s: %d\n", read, err);
goto fail;
}
}
if (write) {
server.ipc.wfd = open(write, O_RDWR | O_NONBLOCK);
if (server.ipc.wfd < 0) {
err = -errno;
EPRINTF("FD open failed %s, %d\n", write, err);
goto fail;
}
}
scheduler_initialize(&server.scheduler);
if (read) {
event_id = scheduler_register_event(&server.scheduler,
SCHEDULER_POLL_READ_FD,
server.ipc.rfd, 0,
tapdisk_server_read_ipc_message,
NULL);
if (event_id < 0) {
err = event_id;
goto fail;
}
}
err = tapdisk_server_initialize_aio_queue();
if (err)
goto fail;
server.control_event = event_id;
server.run = 1;
return 0;
fail:
if (server.ipc.rfd > 0)
close(server.ipc.rfd);
if (server.ipc.wfd > 0)
close(server.ipc.wfd);
if (event_id > 0)
scheduler_unregister_event(&server.scheduler,
server.control_event);
return err;
}
int
tapdisk_server_run()
{
int err;
err = tapdisk_set_resource_limits();
if (err)
return err;
signal(SIGBUS, tapdisk_server_signal_handler);
signal(SIGINT, tapdisk_server_signal_handler);
signal(SIGUSR1, tapdisk_server_signal_handler);
signal(SIGXFSZ, tapdisk_server_signal_handler);
__tapdisk_server_run();
tapdisk_server_close();
return 0;
}
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_SERVER_H_
#define _TAPDISK_SERVER_H_
#include "tapdisk-vbd.h"
#include "tapdisk-queue.h"
struct tap_disk *tapdisk_server_find_driver_interface(int);
td_image_t *tapdisk_server_get_shared_image(td_image_t *);
td_vbd_t *tapdisk_server_get_vbd(td_uuid_t);
void tapdisk_server_add_vbd(td_vbd_t *);
void tapdisk_server_remove_vbd(td_vbd_t *);
void tapdisk_server_queue_tiocb(struct tiocb *);
void tapdisk_server_check_state(void);
event_id_t tapdisk_server_register_event(char, int, int, event_cb_t, void *);
void tapdisk_server_unregister_event(event_id_t);
void tapdisk_server_set_max_timeout(int);
int tapdisk_server_initialize(const char *, const char *);
int tapdisk_server_run(void);
#define TAPDISK_TIOCBS (TAPDISK_DATA_REQUESTS + 50)
typedef struct tapdisk_server {
int run;
td_ipc_t ipc;
struct list_head vbds;
scheduler_t scheduler;
event_id_t control_event;
struct tqueue aio_queue;
event_id_t aio_queue_event_id;
} tapdisk_server_t;
#endif
+600
View File
@@ -0,0 +1,600 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "list.h"
#include "scheduler.h"
#include "tapdisk-vbd.h"
#include "tapdisk-server.h"
#define POLL_READ 0
#define POLL_WRITE 1
#define MIN(a, b) ((a) < (b) ? (a) : (b))
struct tapdisk_stream_poll {
int pipe[2];
int set;
};
struct tapdisk_stream_request {
uint64_t sec;
uint32_t secs;
uint64_t seqno;
blkif_request_t blkif_req;
struct list_head next;
};
struct tapdisk_stream {
td_vbd_t *vbd;
unsigned int id;
int in_fd;
int out_fd;
int err;
uint64_t cur;
uint64_t start;
uint64_t end;
uint64_t started;
uint64_t completed;
struct tapdisk_stream_poll poll;
event_id_t enqueue_event_id;
struct list_head free_list;
struct list_head pending_list;
struct list_head completed_list;
struct tapdisk_stream_request requests[MAX_REQUESTS];
};
static unsigned int tapdisk_stream_count;
static void tapdisk_stream_close_image(struct tapdisk_stream *);
static void
usage(const char *app, int err)
{
printf("usage: %s <-n type:/path/to/image> "
"[-c sector count] [-s skip sectors]\n", app);
exit(err);
}
static inline void
tapdisk_stream_poll_initialize(struct tapdisk_stream_poll *p)
{
p->set = 0;
p->pipe[POLL_READ] = p->pipe[POLL_WRITE] = -1;
}
static int
tapdisk_stream_poll_open(struct tapdisk_stream_poll *p)
{
int err;
tapdisk_stream_poll_initialize(p);
err = pipe(p->pipe);
if (err)
return -errno;
err = fcntl(p->pipe[POLL_READ], F_SETFL, O_NONBLOCK);
if (err)
goto out;
err = fcntl(p->pipe[POLL_WRITE], F_SETFL, O_NONBLOCK);
if (err)
goto out;
return 0;
out:
close(p->pipe[POLL_READ]);
close(p->pipe[POLL_WRITE]);
tapdisk_stream_poll_initialize(p);
return -errno;
}
static void
tapdisk_stream_poll_close(struct tapdisk_stream_poll *p)
{
if (p->pipe[POLL_READ] != -1)
close(p->pipe[POLL_READ]);
if (p->pipe[POLL_WRITE] != -1)
close(p->pipe[POLL_WRITE]);
tapdisk_stream_poll_initialize(p);
}
static inline void
tapdisk_stream_poll_clear(struct tapdisk_stream_poll *p)
{
int dummy;
read(p->pipe[POLL_READ], &dummy, sizeof(dummy));
p->set = 0;
}
static inline void
tapdisk_stream_poll_set(struct tapdisk_stream_poll *p)
{
int dummy = 0;
if (!p->set) {
write(p->pipe[POLL_WRITE], &dummy, sizeof(dummy));
p->set = 1;
}
}
static inline int
tapdisk_stream_stop(struct tapdisk_stream *s)
{
return (list_empty(&s->pending_list) && (s->cur == s->end || s->err));
}
static inline void
tapdisk_stream_initialize_request(struct tapdisk_stream_request *req)
{
memset(req, 0, sizeof(*req));
INIT_LIST_HEAD(&req->next);
}
static inline int
tapdisk_stream_request_idx(struct tapdisk_stream *s,
struct tapdisk_stream_request *req)
{
return (req - s->requests);
}
static inline struct tapdisk_stream_request *
tapdisk_stream_get_request(struct tapdisk_stream *s)
{
struct tapdisk_stream_request *req;
if (list_empty(&s->free_list))
return NULL;
req = list_entry(s->free_list.next,
struct tapdisk_stream_request, next);
list_del_init(&req->next);
tapdisk_stream_initialize_request(req);
return req;
}
static void
tapdisk_stream_print_request(struct tapdisk_stream *s,
struct tapdisk_stream_request *sreq)
{
unsigned long idx = (unsigned long)tapdisk_stream_request_idx(s, sreq);
char *buf = (char *)MMAP_VADDR(s->vbd->ring.vstart, idx, 0);
write(s->out_fd, buf, sreq->secs << SECTOR_SHIFT);
}
static void
tapdisk_stream_write_data(struct tapdisk_stream *s)
{
struct tapdisk_stream_request *sreq, *tmp;
list_for_each_entry_safe(sreq, tmp, &s->completed_list, next) {
if (sreq->seqno != s->completed)
break;
s->completed++;
tapdisk_stream_print_request(s, sreq);
list_del_init(&sreq->next);
list_add_tail(&sreq->next, &s->free_list);
}
}
static inline void
tapdisk_stream_queue_completed(struct tapdisk_stream *s,
struct tapdisk_stream_request *sreq)
{
struct tapdisk_stream_request *itr;
list_for_each_entry(itr, &s->completed_list, next)
if (sreq->seqno < itr->seqno) {
list_add_tail(&sreq->next, &itr->next);
return;
}
list_add_tail(&sreq->next, &s->completed_list);
}
static void
tapdisk_stream_dequeue(void *arg, blkif_response_t *rsp)
{
struct tapdisk_stream *s = (struct tapdisk_stream *)arg;
struct tapdisk_stream_request *sreq = s->requests + rsp->id;
list_del_init(&sreq->next);
if (rsp->status == BLKIF_RSP_OKAY)
tapdisk_stream_queue_completed(s, sreq);
else {
s->err = EIO;
list_add_tail(&sreq->next, &s->free_list);
fprintf(stderr, "error reading sector 0x%"PRIu64"\n", sreq->sec);
}
tapdisk_stream_write_data(s);
tapdisk_stream_poll_set(&s->poll);
}
static void
tapdisk_stream_enqueue(event_id_t id, char mode, void *arg)
{
td_vbd_t *vbd;
int i, idx, psize;
struct tapdisk_stream *s = (struct tapdisk_stream *)arg;
vbd = s->vbd;
tapdisk_stream_poll_clear(&s->poll);
if (tapdisk_stream_stop(s)) {
tapdisk_stream_close_image(s);
return;
}
psize = getpagesize();
while (s->cur < s->end && !s->err) {
blkif_request_t *breq;
td_vbd_request_t *vreq;
struct tapdisk_stream_request *sreq;
sreq = tapdisk_stream_get_request(s);
if (!sreq)
break;
idx = tapdisk_stream_request_idx(s, sreq);
sreq->sec = s->cur;
sreq->secs = 0;
sreq->seqno = s->started++;
breq = &sreq->blkif_req;
breq->id = idx;
breq->nr_segments = 0;
breq->sector_number = sreq->sec;
breq->operation = BLKIF_OP_READ;
for (i = 0; i < BLKIF_MAX_SEGMENTS_PER_REQUEST; i++) {
uint32_t secs = MIN(s->end - s->cur, psize >> SECTOR_SHIFT);
struct blkif_request_segment *seg = breq->seg + i;
if (!secs)
break;
sreq->secs += secs;
s->cur += secs;
seg->first_sect = 0;
seg->last_sect = secs - 1;
breq->nr_segments++;
}
vreq = vbd->request_list + idx;
assert(list_empty(&vreq->next));
assert(vreq->secs_pending == 0);
memcpy(&vreq->req, breq, sizeof(*breq));
vbd->received++;
vreq->vbd = vbd;
tapdisk_vbd_move_request(vreq, &vbd->new_requests);
list_add_tail(&sreq->next, &s->pending_list);
}
tapdisk_vbd_issue_requests(vbd);
}
static int
tapdisk_stream_open_image(struct tapdisk_stream *s, const char *path, int type)
{
int err;
s->id = tapdisk_stream_count++;
err = tapdisk_server_initialize(NULL, NULL);
if (err)
goto out;
err = tapdisk_vbd_initialize(-1, -1, s->id);
if (err)
goto out;
s->vbd = tapdisk_server_get_vbd(s->id);
if (!s->vbd) {
err = ENODEV;
goto out;
}
tapdisk_vbd_set_callback(s->vbd, tapdisk_stream_dequeue, s);
err = tapdisk_vbd_open_vdi(s->vbd, path, type,
TAPDISK_STORAGE_TYPE_DEFAULT,
TD_OPEN_RDONLY);
if (err)
goto out;
s->vbd->reopened = 1;
err = 0;
out:
if (err)
fprintf(stderr, "failed to open %s: %d\n", path, err);
return err;
}
static void
tapdisk_stream_close_image(struct tapdisk_stream *s)
{
td_vbd_t *vbd;
vbd = tapdisk_server_get_vbd(s->id);
if (vbd) {
tapdisk_vbd_close_vdi(vbd);
tapdisk_server_remove_vbd(vbd);
free((void *)vbd->ring.vstart);
free(vbd->name);
free(vbd);
s->vbd = NULL;
}
}
static int
tapdisk_stream_set_position(struct tapdisk_stream *s,
uint64_t count, uint64_t skip)
{
int err;
image_t image;
err = tapdisk_vbd_get_image_info(s->vbd, &image);
if (err) {
fprintf(stderr, "failed getting image size: %d\n", err);
return err;
}
if (count == (uint64_t)-1)
count = image.size - skip;
if (count + skip > image.size) {
fprintf(stderr, "0x%"PRIx64" past end of image 0x%"PRIx64"\n",
(uint64_t) (count + skip), (uint64_t) image.size);
return -EINVAL;
}
s->start = skip;
s->cur = s->start;
s->end = s->start + count;
return 0;
}
static int
tapdisk_stream_initialize_requests(struct tapdisk_stream *s)
{
size_t size;
td_ring_t *ring;
int err, i, psize;
ring = &s->vbd->ring;
psize = getpagesize();
size = psize * BLKTAP_MMAP_REGION_SIZE;
/* sneaky -- set up ring->vstart so tapdisk_vbd will use our buffers */
err = posix_memalign((void **)&ring->vstart, psize, size);
if (err) {
fprintf(stderr, "failed to allocate buffers: %d\n", err);
ring->vstart = 0;
return err;
}
for (i = 0; i < MAX_REQUESTS; i++) {
struct tapdisk_stream_request *req = s->requests + i;
tapdisk_stream_initialize_request(req);
list_add_tail(&req->next, &s->free_list);
}
return 0;
}
static int
tapdisk_stream_register_enqueue_event(struct tapdisk_stream *s)
{
int err;
struct tapdisk_stream_poll *p = &s->poll;
err = tapdisk_stream_poll_open(p);
if (err)
goto out;
err = tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
p->pipe[POLL_READ], 0,
tapdisk_stream_enqueue, s);
if (err < 0)
goto out;
s->enqueue_event_id = err;
err = 0;
out:
if (err)
fprintf(stderr, "failed to register event: %d\n", err);
return err;
}
static void
tapdisk_stream_unregister_enqueue_event(struct tapdisk_stream *s)
{
if (s->enqueue_event_id) {
tapdisk_server_unregister_event(s->enqueue_event_id);
s->enqueue_event_id = 0;
}
tapdisk_stream_poll_close(&s->poll);
}
static inline void
tapdisk_stream_initialize(struct tapdisk_stream *s)
{
memset(s, 0, sizeof(*s));
s->in_fd = s->out_fd = -1;
INIT_LIST_HEAD(&s->free_list);
INIT_LIST_HEAD(&s->pending_list);
INIT_LIST_HEAD(&s->completed_list);
}
static int
tapdisk_stream_open_fds(struct tapdisk_stream *s)
{
s->out_fd = dup(STDOUT_FILENO);
if (s->out_fd == -1) {
fprintf(stderr, "failed to open output: %d\n", errno);
return errno;
}
return 0;
}
static int
tapdisk_stream_open(struct tapdisk_stream *s, const char *path,
int type, uint64_t count, uint64_t skip)
{
int err;
tapdisk_stream_initialize(s);
err = tapdisk_stream_open_fds(s);
if (err)
return err;
err = tapdisk_stream_open_image(s, path, type);
if (err)
return err;
err = tapdisk_stream_set_position(s, count, skip);
if (err)
return err;
err = tapdisk_stream_initialize_requests(s);
if (err)
return err;
err = tapdisk_stream_register_enqueue_event(s);
if (err)
return err;
return 0;
}
static void
tapdisk_stream_release(struct tapdisk_stream *s)
{
close(s->out_fd);
tapdisk_stream_close_image(s);
tapdisk_stream_unregister_enqueue_event(s);
}
static int
tapdisk_stream_run(struct tapdisk_stream *s)
{
tapdisk_stream_enqueue(s->enqueue_event_id, SCHEDULER_POLL_READ_FD, s);
tapdisk_server_run();
return s->err;
}
int
main(int argc, char *argv[])
{
int c, err, type;
char *params, *path;
uint64_t count, skip;
struct tapdisk_stream stream;
err = 0;
skip = 0;
count = (uint64_t)-1;
params = NULL;
while ((c = getopt(argc, argv, "n:c:s:h")) != -1) {
switch (c) {
case 'n':
params = optarg;
break;
case 'c':
count = strtoull(optarg, NULL, 10);
break;
case 's':
skip = strtoull(optarg, NULL, 10);
break;
default:
err = EINVAL;
case 'h':
usage(argv[0], err);
}
}
if (!params)
usage(argv[0], EINVAL);
err = tapdisk_parse_disk_type(params, &path, &type);
if (err) {
fprintf(stderr, "invalid argument %s: %d\n", params, err);
return err;
}
tapdisk_start_logging("tapdisk-stream");
err = tapdisk_stream_open(&stream, path, type, count, skip);
if (err)
goto out;
err = tapdisk_stream_run(&stream);
if (err)
goto out;
err = 0;
out:
tapdisk_stream_release(&stream);
tapdisk_stop_logging();
return err;
}
+199
View File
@@ -0,0 +1,199 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/fs.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include "tapdisk.h"
#include "disktypes.h"
#include "blktaplib.h"
#include "tapdisk-log.h"
#include "tapdisk-utils.h"
void
tapdisk_start_logging(const char *name)
{
static char buf[128];
snprintf(buf, sizeof(buf), "%s[%d]", name, getpid());
openlog(buf, LOG_CONS | LOG_ODELAY, LOG_DAEMON);
open_tlog("/tmp/tapdisk.log", (64 << 10), TLOG_WARN, 0);
}
void
tapdisk_stop_logging(void)
{
closelog();
close_tlog();
}
int
tapdisk_set_resource_limits(void)
{
int err;
struct rlimit rlim;
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;
err = setrlimit(RLIMIT_MEMLOCK, &rlim);
if (err == -1) {
EPRINTF("RLIMIT_MEMLOCK failed: %d\n", errno);
return -errno;
}
err = mlockall(MCL_CURRENT | MCL_FUTURE);
if (err == -1) {
EPRINTF("mlockall failed: %d\n", errno);
return -errno;
}
#define CORE_DUMP
#if defined(CORE_DUMP)
err = setrlimit(RLIMIT_CORE, &rlim);
if (err == -1)
EPRINTF("RLIMIT_CORE failed: %d\n", errno);
#endif
return 0;
}
int
tapdisk_namedup(char **dup, const char *name)
{
*dup = NULL;
if (strnlen(name, MAX_NAME_LEN) >= MAX_NAME_LEN)
return -ENAMETOOLONG;
*dup = strdup(name);
if (!*dup)
return -ENOMEM;
return 0;
}
int
tapdisk_parse_disk_type(const char *params, char **_path, int *_type)
{
int i, err, size, handle_len;
char *ptr, *path, handle[10];
if (strlen(params) + 1 >= MAX_NAME_LEN)
return -ENAMETOOLONG;
ptr = strchr(params, ':');
if (!ptr)
return -EINVAL;
path = ptr + 1;
handle_len = ptr - params;
if (handle_len > sizeof(handle))
return -ENAMETOOLONG;
memcpy(handle, params, handle_len);
handle[handle_len] = '\0';
size = sizeof(dtypes) / sizeof(disk_info_t *);
for (i = 0; i < size; i++) {
if (strncmp(handle, dtypes[i]->handle, handle_len))
continue;
if (dtypes[i]->idnum == -1)
return -ENODEV;
*_type = dtypes[i]->idnum;
*_path = path;
return 0;
}
return -ENODEV;
}
/*Get Image size, secsize*/
int
tapdisk_get_image_size(int fd, uint64_t *_sectors, uint32_t *_sector_size)
{
int ret;
struct stat stat;
uint64_t sectors;
uint32_t sector_size;
sectors = 0;
sector_size = 0;
*_sectors = 0;
*_sector_size = 0;
if (fstat(fd, &stat)) {
DPRINTF("ERROR: fstat failed, Couldn't stat image");
return -EINVAL;
}
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
if (ioctl(fd, BLKGETSIZE, &sectors)) {
DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
return -EINVAL;
}
/*Get the sector size*/
#if defined(BLKSSZGET)
{
int arg;
sector_size = DEFAULT_SECTOR_SIZE;
ioctl(fd, BLKSSZGET, &sector_size);
if (sector_size != DEFAULT_SECTOR_SIZE)
DPRINTF("Note: sector size is %u (not %d)\n",
sector_size, DEFAULT_SECTOR_SIZE);
}
#else
sector_size = DEFAULT_SECTOR_SIZE;
#endif
} else {
/*Local file? try fstat instead*/
sectors = (stat.st_size >> SECTOR_SHIFT);
sector_size = DEFAULT_SECTOR_SIZE;
}
if (sectors == 0) {
sectors = 16836057ULL;
sector_size = DEFAULT_SECTOR_SIZE;
}
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_UTILS_H_
#define _TAPDISK_UTILS_H_
#include <inttypes.h>
#define MAX_NAME_LEN 1000
void tapdisk_start_logging(const char *);
void tapdisk_stop_logging(void);
int tapdisk_set_resource_limits(void);
int tapdisk_namedup(char **, const char *);
int tapdisk_parse_disk_type(const char *, char **, int *);
int tapdisk_get_image_size(int, uint64_t *, uint32_t *);
#endif
File diff suppressed because it is too large. Load diff
+193
View File
@@ -0,0 +1,193 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_VBD_H_
#define _TAPDISK_VBD_H_
#include <sys/time.h>
#include <xenctrl.h>
#include <xen/io/blkif.h>
#include "tapdisk.h"
#include "scheduler.h"
#include "tapdisk-ipc.h"
#include "tapdisk-image.h"
#define TD_VBD_MAX_RETRIES 100
#define TD_VBD_RETRY_INTERVAL 1
#define TD_VBD_DEAD 0x0001
#define TD_VBD_CLOSED 0x0002
#define TD_VBD_QUIESCE_REQUESTED 0x0004
#define TD_VBD_QUIESCED 0x0008
#define TD_VBD_PAUSE_REQUESTED 0x0010
#define TD_VBD_PAUSED 0x0020
#define TD_VBD_SHUTDOWN_REQUESTED 0x0040
#define TD_VBD_LOCKING 0x0080
#define TD_VBD_RETRY_NEEDED 0x0100
#define TD_VBD_LOG_DROPPED 0x0200
typedef struct td_ring td_ring_t;
typedef struct td_vbd_request td_vbd_request_t;
typedef struct td_vbd_handle td_vbd_t;
typedef void (*td_vbd_cb_t) (void *, blkif_response_t *);
struct td_ring {
int fd;
char *mem;
blkif_sring_t *sring;
blkif_back_ring_t fe_ring;
unsigned long vstart;
};
struct td_vbd_request {
blkif_request_t req;
int16_t status;
int error;
int blocked; /* blocked on a dependency */
int submitting;
int secs_pending;
int num_retries;
struct timeval last_try;
td_vbd_t *vbd;
struct list_head next;
};
struct td_vbd_handle {
char *name;
td_uuid_t uuid;
int type;
int storage;
uint8_t reopened;
uint8_t reactivated;
td_flag_t flags;
td_flag_t state;
td_ipc_t ipc;
struct list_head images;
struct list_head new_requests;
struct list_head pending_requests;
struct list_head failed_requests;
struct list_head completed_requests;
td_vbd_request_t request_list[MAX_REQUESTS];
td_ring_t ring;
event_id_t ring_event_id;
td_vbd_cb_t callback;
void *argument;
struct list_head next;
struct timeval ts;
uint64_t received;
uint64_t returned;
uint64_t kicked;
uint64_t secs_pending;
uint64_t retries;
uint64_t errors;
};
#define tapdisk_vbd_for_each_request(vreq, tmp, list) \
list_for_each_entry_safe((vreq), (tmp), (list), next)
#define tapdisk_vbd_for_each_image(vbd, image, tmp) \
list_for_each_entry_safe((image), (tmp), &(vbd)->images, next)
static inline void
tapdisk_vbd_move_request(td_vbd_request_t *vreq, struct list_head *dest)
{
list_del(&vreq->next);
INIT_LIST_HEAD(&vreq->next);
list_add_tail(&vreq->next, dest);
}
static inline void
tapdisk_vbd_add_image(td_vbd_t *vbd, td_image_t *image)
{
list_add_tail(&image->next, &vbd->images);
}
static inline int
tapdisk_vbd_is_last_image(td_vbd_t *vbd, td_image_t *image)
{
return list_is_last(&image->next, &vbd->images);
}
td_image_t *
tapdisk_vbd_first_image(td_vbd_t *vbd);
static inline td_image_t *
tapdisk_vbd_last_image(td_vbd_t *vbd)
{
return list_entry(vbd->images.prev, td_image_t, next);
}
static inline td_image_t *
tapdisk_vbd_next_image(td_image_t *image)
{
return list_entry(image->next.next, td_image_t, next);
}
int tapdisk_vbd_initialize(int, int, td_uuid_t);
void tapdisk_vbd_set_callback(td_vbd_t *, td_vbd_cb_t, void *);
int tapdisk_vbd_open(td_vbd_t *, const char *, uint16_t,
uint16_t, const char *, td_flag_t);
int tapdisk_vbd_close(td_vbd_t *);
int tapdisk_vbd_open_vdi(td_vbd_t *, const char *,
uint16_t, uint16_t, td_flag_t);
void tapdisk_vbd_close_vdi(td_vbd_t *);
void tapdisk_vbd_forward_request(td_request_t);
int tapdisk_vbd_get_image_info(td_vbd_t *, image_t *);
int tapdisk_vbd_queue_ready(td_vbd_t *);
int tapdisk_vbd_retry_needed(td_vbd_t *);
int tapdisk_vbd_quiesce_queue(td_vbd_t *);
int tapdisk_vbd_start_queue(td_vbd_t *);
int tapdisk_vbd_issue_requests(td_vbd_t *);
int tapdisk_vbd_kill_queue(td_vbd_t *);
int tapdisk_vbd_pause(td_vbd_t *);
int tapdisk_vbd_resume(td_vbd_t *, const char *, uint16_t);
int tapdisk_vbd_kick(td_vbd_t *);
void tapdisk_vbd_check_state(td_vbd_t *);
void tapdisk_vbd_check_progress(td_vbd_t *);
void tapdisk_vbd_debug(td_vbd_t *);
void tapdisk_vbd_complete_vbd_request(td_vbd_t *, td_vbd_request_t *);
#endif
+66
View File
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "tapdisk-utils.h"
#include "tapdisk-server.h"
static void
usage(void)
{
fprintf(stderr, "blktap-utils: v2.0.0\n");
fprintf(stderr, "usage: tapdisk <READ fifo> <WRITE fifo>\n");
exit(EINVAL);
}
int
main(int argc, char *argv[])
{
int err;
if (argc != 3)
usage();
daemon(0, 0);
tapdisk_start_logging("TAPDISK");
err = tapdisk_server_initialize(argv[1], argv[2]);
if (err) {
EPRINTF("failed to initialize tapdisk server: %d\n", err);
goto out;
}
err = tapdisk_server_run();
out:
tapdisk_stop_logging();
return err;
}
+158
View File
@@ -0,0 +1,158 @@
/*
* Copyright (c) 2007, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Some notes on the tap_disk interface:
*
* tap_disk aims to provide a generic interface to easily implement new
* types of image accessors. The structure-of-function-calls is similar
* to disk interfaces used in qemu/denali/etc, with the significant
* difference being the expectation of asynchronous rather than synchronous
* I/O. The asynchronous interface is intended to allow lots of requests to
* be pipelined through a disk, without the disk requiring any of its own
* threads of control. As such, a batch of requests is delivered to the disk
* using:
*
* td_queue_[read,write]()
*
* and passing in a completion callback, which the disk is responsible for
* tracking. Disks should transform these requests as necessary and return
* the resulting iocbs to tapdisk using td_prep_[read,write]() and
* td_queue_tiocb().
*
* NOTE: tapdisk uses the number of sectors submitted per request as a
* ref count. Plugins must use the callback function to communicate the
* completion -- or error -- of every sector submitted to them.
*
* td_get_parent_id returns:
* 0 if parent id successfully retrieved
* TD_NO_PARENT if no parent exists
* -errno on error
*/
#ifndef _TAPDISK_H_
#define _TAPDISK_H_
#include <time.h>
#include <stdint.h>
#include "list.h"
#include "blktaplib.h"
#include "disktypes.h"
#include "tapdisk-log.h"
#include "tapdisk-utils.h"
#define MAX_SEGMENTS_PER_REQ 11
#define SECTOR_SHIFT 9
#define DEFAULT_SECTOR_SIZE 512
#define TAPDISK_DATA_REQUESTS (MAX_REQUESTS * MAX_SEGMENTS_PER_REQ)
//#define BLK_NOT_ALLOCATED (-99)
#define TD_NO_PARENT 1
#define MAX_RAMDISK_SIZE 1024000 /*500MB disk limit*/
#define TD_OP_READ 0
#define TD_OP_WRITE 1
#define TD_OPEN_QUIET 0x00001
#define TD_OPEN_QUERY 0x00002
#define TD_OPEN_RDONLY 0x00004
#define TD_OPEN_STRICT 0x00008
#define TD_OPEN_SHAREABLE 0x00010
#define TD_OPEN_ADD_CACHE 0x00020
#define TD_OPEN_VHD_INDEX 0x00040
#define TD_OPEN_LOG_DIRTY 0x00080
#define TD_CREATE_SPARSE 0x00001
#define TD_CREATE_MULTITYPE 0x00002
#define td_flag_set(word, flag) ((word) |= (flag))
#define td_flag_clear(word, flag) ((word) &= ~(flag))
#define td_flag_test(word, flag) ((word) & (flag))
typedef uint16_t td_uuid_t;
typedef uint32_t td_flag_t;
typedef uint64_t td_sector_t;
typedef struct td_disk_id td_disk_id_t;
typedef struct td_disk_info td_disk_info_t;
typedef struct td_request td_request_t;
typedef struct td_driver_handle td_driver_t;
typedef struct td_image_handle td_image_t;
/*
* Prototype of the callback to activate as requests complete.
*/
typedef void (*td_callback_t)(td_request_t, int);
struct td_disk_id {
char *name;
int drivertype;
};
struct td_disk_info {
td_sector_t size;
long sector_size;
uint32_t info;
};
struct td_request {
int op;
char *buf;
td_sector_t sec;
int secs;
uint8_t blocked; /* blocked on a dependency */
td_image_t *image;
td_callback_t cb;
void *cb_data;
uint64_t id;
int sidx;
void *private;
};
/*
* Structure describing the interface to a virtual disk implementation.
* See note at the top of this file describing this interface.
*/
struct tap_disk {
const char *disk_type;
td_flag_t flags;
int private_data_size;
int (*td_open) (td_driver_t *, const char *, td_flag_t);
int (*td_close) (td_driver_t *);
int (*td_get_parent_id) (td_driver_t *, td_disk_id_t *);
int (*td_validate_parent) (td_driver_t *, td_driver_t *, td_flag_t);
void (*td_queue_read) (td_driver_t *, td_request_t);
void (*td_queue_write) (td_driver_t *, td_request_t);
void (*td_debug) (td_driver_t *);
};
#endif
+436
View File
@@ -0,0 +1,436 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "tapdisk.h"
#include "blktap2.h"
#include "tapdisk-vbd.h"
#include "tapdisk-utils.h"
#include "tapdisk-server.h"
#define TAPDISK2_VBD 0
#define cprintf(_err, _f, _a...) \
do { \
if (child_out) { \
fprintf(child_out, "%d: " _f, _err, ##_a); \
fflush(child_out); \
} \
} while (0)
#define CHILD_ERR(_err, _f, _a...) \
do { \
EPRINTF(_f, ##_a); \
cprintf(_err, _f, ##_a); \
} while (0)
static int channel[2];
static FILE *child_out;
static struct blktap2_handle handle;
static int
tapdisk2_prepare_directory(void)
{
int err;
char *ptr, *name, *start;
err = access(BLKTAP2_DIRECTORY, W_OK | R_OK);
if (!err)
return 0;
name = strdup(BLKTAP2_DIRECTORY);
if (!name)
return -ENOMEM;
start = name;
for (;;) {
ptr = strchr(start + 1, '/');
if (ptr)
*ptr = '\0';
err = mkdir(name, 0755);
if (err && errno != EEXIST) {
err = -errno;
CHILD_ERR(err, "failed to create directory %s: %d\n",
name, err);
break;
}
if (!ptr)
break;
else {
*ptr = '/';
start = ptr + 1;
}
}
free(name);
return err;
}
static int
tapdisk2_make_device(char *devname, int major, int minor, int perm)
{
int err;
struct stat st;
err = tapdisk2_prepare_directory();
if (err)
return err;
if (!access(devname, F_OK))
if (unlink(devname)) {
CHILD_ERR(errno, "error unlinking %s: %d\n",
devname, errno);
return -errno;
}
err = mknod(devname, perm, makedev(major, minor));
if (err) {
CHILD_ERR(errno, "mknod %s failed: %d\n", devname, -errno);
return -errno;
}
DPRINTF("Created %s device\n", devname);
return 0;
}
static int
tapdisk2_check_environment(void)
{
FILE *f;
int err, minor;
char name[256];
if (!access(BLKTAP2_CONTROL_DEVICE, R_OK | W_OK))
return 0;
memset(name, 0, sizeof(name));
f = fopen("/proc/misc", "r");
if (!f) {
CHILD_ERR(errno, "failed to open /proc/misc: %d\n", errno);
return -errno;
}
while (fscanf(f, "%d %256s", &minor, name) == 2)
if (!strcmp(name, BLKTAP2_CONTROL_NAME)) {
err = tapdisk2_make_device(BLKTAP2_CONTROL_DEVICE,
MISC_MAJOR_NUMBER,
minor, S_IFCHR | 0600);
goto out;
}
err = -ENOSYS;
CHILD_ERR(err, "didn't find %s in /proc/misc\n", BLKTAP2_CONTROL_NAME);
out:
fclose(f);
return err;
}
static void
tapdisk2_free_device(void)
{
int fd, err;
fd = open(BLKTAP2_CONTROL_DEVICE, O_RDONLY);
if (fd == -1) {
CHILD_ERR(errno, "failed to open control device: %d\n", errno);
return;
}
err = ioctl(fd, BLKTAP2_IOCTL_FREE_TAP, handle.minor);
close(fd);
}
static int
tapdisk2_prepare_device(void)
{
char *name;
int fd, err;
fd = open(BLKTAP2_CONTROL_DEVICE, O_RDONLY);
if (fd == -1) {
CHILD_ERR(errno, "failed to open control device: %d\n", errno);
return -errno;
}
err = ioctl(fd, BLKTAP2_IOCTL_ALLOC_TAP, &handle);
close(fd);
if (err == -1) {
CHILD_ERR(errno, "failed to allocate new device: %d\n", errno);
return -errno;
}
err = asprintf(&name, "%s%d", BLKTAP2_RING_DEVICE, handle.minor);
if (err == -1) {
err = -ENOMEM;
goto fail;
}
err = tapdisk2_make_device(name, handle.ring,
handle.minor, S_IFCHR | 0600);
free(name);
if (err) {
CHILD_ERR(err, "creating ring device for %d failed: %d\n",
handle.minor, err);
goto fail;
}
err = asprintf(&name, "%s%d", BLKTAP2_IO_DEVICE, handle.minor);
if (err == -1) {
err = -ENOMEM;
goto fail;
}
err = tapdisk2_make_device(name, handle.device,
handle.minor, S_IFBLK | 0600);
free(name);
if (err) {
CHILD_ERR(err, "creating IO device for %d failed: %d\n",
handle.minor, err);
goto fail;
}
DPRINTF("new interface: ring: %u, device: %u, minor: %u\n",
handle.ring, handle.device, handle.minor);
return 0;
fail:
tapdisk2_free_device();
return err;
}
static int
tapdisk2_open_device(int type, const char *path, const char *name)
{
int err;
td_vbd_t *vbd;
image_t image;
char *devname;
struct blktap2_params params;
err = tapdisk_vbd_initialize(-1, -1, TAPDISK2_VBD);
if (err)
return err;
vbd = tapdisk_server_get_vbd(TAPDISK2_VBD);
if (!vbd) {
err = -ENODEV;
CHILD_ERR(err, "couldn't find vbd\n");
return err;
}
err = asprintf(&devname, "%s%d", BLKTAP2_RING_DEVICE, handle.minor);
if (err == -1) {
err = -ENOMEM;
CHILD_ERR(err, "couldn't allocate ring\n");
return err;
}
err = tapdisk_vbd_open(vbd, path, type,
TAPDISK_STORAGE_TYPE_DEFAULT,
devname, 0);
free(devname);
if (err) {
CHILD_ERR(err, "vbd open failed: %d\n", err);
return err;
}
memset(&params, 0, sizeof(params));
tapdisk_vbd_get_image_info(vbd, &image);
params.capacity = image.size;
params.sector_size = image.secsize;
snprintf(params.name, sizeof(params.name) - 1, "%s", name);
err = ioctl(vbd->ring.fd, BLKTAP2_IOCTL_CREATE_DEVICE, &params);
if (err) {
err = -errno;
CHILD_ERR(err, "create device failed: %d\n", err);
return err;
}
return 0;
}
static int
tapdisk2_set_child_fds(void)
{
int i, err;
err = dup2(channel[1], STDOUT_FILENO);
if (err == -1) {
CHILD_ERR(errno, "failed duping pipe: %d\n", errno);
return errno;
}
child_out = fdopen(STDOUT_FILENO, "w");
if (!child_out) {
CHILD_ERR(errno, "failed setting child_out: %d\n", errno);
return errno;
}
for (i = 0; i < sysconf(_SC_OPEN_MAX); i++)
if (i != STDOUT_FILENO)
close(i);
return 0;
}
static int
tapdisk2_create_device(const char *params)
{
char *path;
int err, type;
chdir("/");
tapdisk_start_logging("tapdisk2");
err = tapdisk2_set_child_fds();
if (err)
goto out;
err = tapdisk2_check_environment();
if (err)
goto out;
err = tapdisk_parse_disk_type(params, &path, &type);
if (err)
goto out;
err = tapdisk2_prepare_device();
if (err)
goto out;
err = tapdisk_server_initialize(NULL, NULL);
if (err)
goto fail;
err = tapdisk2_open_device(type, path, params);
if (err)
goto fail;
cprintf(0, "%s%d\n", BLKTAP2_IO_DEVICE, handle.minor);
close(STDOUT_FILENO);
err = tapdisk_server_run();
if (err)
goto fail;
err = 0;
out:
tapdisk_stop_logging();
return err;
fail:
tapdisk2_free_device();
goto out;
}
static int
tapdisk2_wait_for_device(void)
{
int err;
char msg[1024];
FILE *parent_in;
close(channel[1]);
parent_in = fdopen(channel[0], "r");
if (!parent_in) {
printf("failed to connect to child: %d\n", errno);
return errno;
}
memset(msg, 0, sizeof(msg));
if (fscanf(parent_in, "%d: %1023[^\n]", &err, msg) != 2) {
printf("unrecognized child response\n");
return EINVAL;
}
printf("%s\n", msg);
return (err >= 0 ? err : -err);
}
static void
usage(const char *app, int err)
{
fprintf(stderr, "usage: %s <-n file>\n", app);
exit(err);
}
int
main(int argc, char *argv[])
{
int c;
char *params;
params = NULL;
while ((c = getopt(argc, argv, "n:h")) != -1) {
switch (c) {
case 'n':
params = optarg;
break;
case 'h':
usage(argv[0], 0);
default:
usage(argv[0], EINVAL);
}
}
if (!params || optind != argc)
usage(argv[0], EINVAL);
if (pipe(channel) == -1) {
printf("pipe failed: %d\n", errno);
return errno;
}
switch (fork()) {
case -1:
printf("fork failed: %d\n", errno);
return errno;
case 0:
return tapdisk2_create_device(params);
default:
return tapdisk2_wait_for_device();
}
}
+691
View File
@@ -0,0 +1,691 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>
#include <string.h>
#include "libvhd.h"
#include "vhd-util.h"
#include "tapdisk-utils.h"
#if 1
#define DFPRINTF(_f, _a...) fprintf ( stdout, _f , ## _a )
#else
#define DFPRINTF(_f, _a...) ((void)0)
#endif
typedef enum {
TD_FIELD_HIDDEN = 0,
TD_FIELD_INVALID = 1
} td_field_t;
struct vdi_field {
char *name;
td_field_t id;
};
static struct vdi_field td_vdi_fields[TD_FIELD_INVALID] = {
{ .id = TD_FIELD_HIDDEN, .name = "hidden" }
};
typedef enum {
TD_CMD_CREATE = 0,
TD_CMD_SNAPSHOT,
/* TD_CMD_COALESCE, */
TD_CMD_QUERY,
/* TD_CMD_RESIZE, */
TD_CMD_SET,
/* TD_CMD_REPAIR, */
/* TD_CMD_FILL, */
/* TD_CMD_READ, */
TD_CMD_INVALID,
} td_command_t;
struct command {
td_command_t id;
char *name;
int needs_type;
};
struct command commands[TD_CMD_INVALID] = {
{ .id = TD_CMD_CREATE, .name = "create", .needs_type = 1 },
{ .id = TD_CMD_SNAPSHOT, .name = "snapshot", .needs_type = 1 },
/* { .id = TD_CMD_COALESCE, .name = "coalesce", .needs_type = 1 }, */
{ .id = TD_CMD_QUERY, .name = "query", .needs_type = 1 },
/* { .id = TD_CMD_RESIZE, .name = "resize", .needs_type = 1 }, */
{ .id = TD_CMD_SET, .name = "set", .needs_type = 1 },
/* { .id = TD_CMD_REPAIR, .name = "repair", .needs_type = 1 }, */
/* { .id = TD_CMD_FILL, .name = "fill", .needs_type = 1 }, */
/* { .id = TD_CMD_READ, .name = "read", .needs_type = 1 }, */
};
typedef enum {
TD_TYPE_VHD = 0,
TD_TYPE_AIO,
TD_TYPE_INVALID,
} td_disk_t;
const char *td_disk_types[TD_TYPE_INVALID] = {
"vhd",
"aio",
};
#define print_commands() \
do { \
int i; \
fprintf(stderr, "COMMAND := { "); \
fprintf(stderr, "%s", commands[0].name); \
for (i = 1; i < TD_CMD_INVALID; i++) \
fprintf(stderr, " | %s", commands[i].name); \
fprintf(stderr, " }\n"); \
} while (0)
#define print_disk_types() \
do { \
int i; \
fprintf(stderr, "TYPE := { "); \
fprintf(stderr, "%s", td_disk_types[0]); \
for (i = 1; i < TD_TYPE_INVALID; i++) \
fprintf(stderr, " | %s", td_disk_types[i]); \
fprintf(stderr, " }\n"); \
} while (0);
#define print_field_names() \
do { \
int i; \
fprintf(stderr, "FIELD := { "); \
fprintf(stderr, "%s", td_vdi_fields[0].name); \
for (i = 1; i < TD_FIELD_INVALID; i++) \
fprintf(stderr, " | %s", td_vdi_fields[i].name); \
fprintf(stderr, " }\n"); \
} while (0)
void
help(void)
{
fprintf(stderr, "Tapdisk Utilities: v1.0.0\n");
fprintf(stderr, "usage: td-util COMMAND [TYPE] [OPTIONS]\n");
print_commands();
print_disk_types();
exit(-1);
}
struct command *
get_command(char *command)
{
int i;
for (i = 0; i < TD_CMD_INVALID; i++)
if (!strcmp(command, commands[i].name))
return &commands[i];
return NULL;
}
struct vdi_field *
get_field(char *field)
{
int i;
for (i = 0; i < TD_FIELD_INVALID; i++)
if (!strcmp(field, td_vdi_fields[i].name))
return &td_vdi_fields[i];
return NULL;
}
int
get_driver_type(char *type)
{
int i;
if (strnlen(type, 25) >= 25)
return -ENAMETOOLONG;
for (i = 0; i < TD_TYPE_INVALID; i++)
if (!strcmp(type, td_disk_types[i]))
return i;
return -TD_TYPE_INVALID;
}
int
td_create(int type, int argc, char *argv[])
{
ssize_t mb;
uint64_t size;
char *name, *buf;
int c, i, fd, sparse = 1, fixedsize = 0;
while ((c = getopt(argc, argv, "hrb")) != -1) {
switch(c) {
case 'r':
sparse = 0;
break;
case 'b':
fixedsize = 1;
break;
default:
fprintf(stderr, "Unknown option %c\n", (char)c);
case 'h':
goto usage;
}
}
if (optind != (argc - 2))
goto usage;
mb = 1 << 20;
size = atoi(argv[optind++]);
size = size << 20;
name = argv[optind];
if (strnlen(name, MAX_NAME_LEN) == MAX_NAME_LEN) {
fprintf(stderr, "Device name too long\n");
return ENAMETOOLONG;
}
if (type == TD_TYPE_VHD) {
int cargc = 0;
char sbuf[32], *cargv[10];
size >>= 20;
memset(cargv, 0, sizeof(cargv));
snprintf(sbuf, sizeof(sbuf) - 1, "%"PRIu64, size);
cargv[cargc++] = "create";
cargv[cargc++] = "-n";
cargv[cargc++] = name;
cargv[cargc++] = "-s";
cargv[cargc++] = sbuf;
if (!sparse)
cargv[cargc++] = "-r";
if (fixedsize)
cargv[cargc++] = "-b";
return vhd_util_create(cargc, cargv);
}
/* generic create */
if (sparse) {
fprintf(stderr, "Cannot create sparse %s image\n",
td_disk_types[type]);
return EINVAL;
}
buf = calloc(1, mb);
if (!buf)
return ENOMEM;
fd = open(name, O_WRONLY | O_DIRECT | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
free(buf);
return errno;
}
size >>= 20;
for (i = 0; i < size; i++)
if (write(fd, buf, mb) != mb) {
close(fd);
unlink(name);
free(buf);
return EIO;
}
close(fd);
free(buf);
return 0;
usage:
fprintf(stderr, "usage: td-util create %s [-h help] [-r reserve] "
"[-b file_is_fixed_size] <SIZE(MB)> <FILENAME>\n",
td_disk_types[type]);
return EINVAL;
}
int
td_snapshot(int type, int argc, char *argv[])
{
char *cargv[10];
int c, err, cargc;
struct stat stats;
char *name, *backing, *limit = NULL;
int fixedsize = 0, rawparent = 0;
if (type != TD_TYPE_VHD) {
fprintf(stderr, "Cannot create snapshot of %s image type\n",
td_disk_types[type]);
return EINVAL;
}
while ((c = getopt(argc, argv, "hbml:")) != -1) {
switch(c) {
case 'b':
fixedsize = 1;
break;
case 'm':
rawparent = 1;
break;
case 'l':
limit = optarg;
break;
case 'h':
err = 0;
goto usage;
default:
err = EINVAL;
goto usage;
}
}
if (optind != (argc - 2)) {
err = EINVAL;
goto usage;
}
name = argv[optind++];
backing = argv[optind++];
if (strnlen(name, MAX_NAME_LEN) == MAX_NAME_LEN ||
strnlen(backing, MAX_NAME_LEN) == MAX_NAME_LEN) {
fprintf(stderr, "Device name too long\n");
return ENAMETOOLONG;
}
if (stat(backing, &stats) == -1) {
fprintf(stderr, "File %s not found\n", backing);
return errno;
}
cargc = 0;
memset(cargv, 0, sizeof(cargv));
cargv[cargc++] = "snapshot";
cargv[cargc++] = "-n";
cargv[cargc++] = name;
cargv[cargc++] = "-p";
cargv[cargc++] = backing;
if (fixedsize)
cargv[cargc++] = "-b";
if (rawparent)
cargv[cargc++] = "-m";
if (limit) {
cargv[cargc++] = "-l";
cargv[cargc++] = limit;
}
return vhd_util_snapshot(cargc, cargv);
usage:
fprintf(stderr, "usage: td-util snapshot %s [-h help] [-m parent_raw] "
"[-b file_is_fixed_size] [-l snapshot depth limit] "
"<FILENAME> <BACKING_FILENAME>\n", td_disk_types[type]);
return err;
}
int
td_coalesce(int type, int argc, char *argv[])
{
int c, ret, cargc;
char *name, *pname, *cargv[3];
if (type != TD_TYPE_VHD) {
fprintf(stderr, "Cannot create snapshot of %s image type\n",
td_disk_types[type]);
return EINVAL;
}
while ((c = getopt(argc, argv, "h")) != -1) {
switch(c) {
default:
fprintf(stderr, "Unknown option %c\n", (char)c);
case 'h':
goto usage;
}
}
if (optind != (argc - 1))
goto usage;
name = argv[optind++];
if (strnlen(name, MAX_NAME_LEN) == MAX_NAME_LEN) {
fprintf(stderr, "Device name too long\n");
return ENAMETOOLONG;
}
cargc = 0;
memset(cargv, 0, sizeof(cargv));
cargv[cargc++] = "coalesce";
cargv[cargc++] = "-n";
cargv[cargc++] = name;
ret = vhd_util_coalesce(cargc, cargv);
if (ret)
printf("coalesce failed: %d\n", ret);
return ret;
usage:
fprintf(stderr, "usage: td-util coalesce %s [-h help] "
"<FILENAME>\n", td_disk_types[type]);
return EINVAL;
}
int
td_query(int type, int argc, char *argv[])
{
char *name;
int c, size = 0, parent = 0, fields = 0, depth = 0, err = 0;
while ((c = getopt(argc, argv, "hvpfd")) != -1) {
switch(c) {
case 'v':
size = 1;
break;
case 'p':
parent = 1;
break;
case 'f':
fields = 1;
break;
case 'd':
depth = 1;
break;
case 'h':
err = 0;
goto usage;
default:
err = EINVAL;
goto usage;
}
}
if (optind != (argc - 1)) {
err = EINVAL;
goto usage;
}
name = argv[optind++];
if (strnlen(name, MAX_NAME_LEN) == MAX_NAME_LEN) {
fprintf(stderr, "Device name too long\n");
return ENAMETOOLONG;
}
if (type == TD_TYPE_VHD) {
vhd_context_t vhd;
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY);
if (err) {
printf("failed opening %s: %d\n", name, err);
return err;
}
if (size)
printf("%"PRIu64"\n", vhd.footer.curr_size >> 20);
if (parent) {
if (vhd.footer.type != HD_TYPE_DIFF)
printf("%s has no parent\n", name);
else {
char *pname;
err = vhd_parent_locator_get(&vhd, &pname);
if (err)
printf("failed getting parent: %d\n",
err);
else {
printf("%s\n", pname);
free(pname);
}
}
}
if (fields) {
int ret, hidden;
ret = vhd_hidden(&vhd, &hidden);
if (ret) {
printf("failed checking 'hidden' field: %d\n",
ret);
err = (err ? : ret);
} else
printf("%s: %d\n",
td_vdi_fields[TD_FIELD_HIDDEN].name,
hidden);
}
if (depth) {
int ret, length;
ret = vhd_chain_depth(&vhd, &length);
if (ret)
printf("error checking chain depth: %d\n", ret);
else
printf("chain depth: %d\n", length);
err = (err ? : ret);
}
vhd_close(&vhd);
} else if (type == TD_TYPE_AIO) {
if (size) {
int fd;
uint64_t secs;
uint32_t ssize;
fd = open(name, O_RDONLY | O_LARGEFILE);
if (fd == -1) {
printf("failed opening %s: %d\n", name, errno);
return -errno;
}
err = tapdisk_get_image_size(fd, &secs, &ssize);
close(fd);
if (err) {
printf("failed getting size for %s: %d\n:",
name, err);
return err;
}
printf("%"PRIu64"\n", secs >> 11);
}
if (parent)
printf("%s has no parent\n", name);
if (fields) {
int i;
for (i = 0; i < TD_FIELD_INVALID; i++)
printf("%s: 0\n", td_vdi_fields[i].name);
}
}
return err;
usage:
fprintf(stderr, "usage: td-util query %s [-h help] [-v virtsize] "
"[-p parent] [-f fields] <FILENAME>\n", td_disk_types[type]);
return err;
}
int
td_set_field(int type, int argc, char *argv[])
{
int ret, i, c, cargc;
struct vdi_field *field;
char *name, *value, *cargv[7];
if (type != TD_TYPE_VHD) {
fprintf(stderr, "Cannot set fields of %s images\n",
td_disk_types[type]);
return EINVAL;
}
while ((c = getopt(argc, argv, "h")) != -1) {
switch(c) {
default:
fprintf(stderr, "Unknown option %c\n", (char)c);
case 'h':
goto usage;
}
}
if (optind != (argc - 3))
goto usage;
name = argv[optind++];
field = get_field(argv[optind]);
if (!field || field->id != TD_FIELD_HIDDEN) {
fprintf(stderr, "Invalid field %s\n", argv[optind]);
goto usage;
}
value = argv[++optind];
cargc = 0;
memset(cargv, 0, sizeof(cargv));
cargv[cargc++] = "set";
cargv[cargc++] = "-n";
cargv[cargc++] = name;
cargv[cargc++] = "-f";
cargv[cargc++] = field->name;
cargv[cargc++] = "-v";
cargv[cargc++] = value;
return vhd_util_set_field(cargc, cargv);
usage:
fprintf(stderr, "usage: td-util set %s [-h help] "
"<FILENAME> <FIELD> <VALUE>\n", td_disk_types[type]);
print_field_names();
return EINVAL;
}
int
main(int argc, char *argv[])
{
char **cargv;
struct command *cmd;
int cargc, i, type = -1, ret = 0;
#ifdef CORE_DUMP
struct rlimit rlim;
rlim.rlim_cur = RLIM_INFINITY;
rlim.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &rlim) < 0)
fprintf(stderr, "setrlimit failed: %d\n", errno);
#endif
if (argc < 2)
help();
cargc = argc - 1;
cmd = get_command(argv[1]);
if (!cmd) {
fprintf(stderr, "invalid COMMAND %s\n", argv[1]);
help();
}
if (cmd->needs_type) {
if (argc < 3) {
fprintf(stderr, "td-util %s requires a TYPE\n",
cmd->name);
print_disk_types();
exit(-1);
}
type = get_driver_type(argv[2]);
if (type < 0) {
fprintf(stderr, "invalid TYPE '%s'.\n", argv[2]);
print_disk_types();
exit(-1);
}
--cargc;
}
cargv = malloc(sizeof(char *) * cargc);
if (!cargv)
exit(ENOMEM);
cargv[0] = cmd->name;
for (i = 1; i < cargc; i++)
cargv[i] = argv[i + (argc - cargc)];
switch(cmd->id) {
case TD_CMD_CREATE:
ret = td_create(type, cargc, cargv);
break;
case TD_CMD_SNAPSHOT:
ret = td_snapshot(type, cargc, cargv);
break;
/*
case TD_CMD_COALESCE:
ret = td_coalesce(type, cargc, cargv);
break;
*/
case TD_CMD_QUERY:
ret = td_query(type, cargc, cargv);
break;
/*
case TD_CMD_RESIZE:
ret = td_resize(type, cargc, cargv);
break;
*/
case TD_CMD_SET:
ret = td_set_field(type, cargc, cargv);
break;
/*
case TD_CMD_REPAIR:
ret = td_repair(type, cargc, cargv);
break;
case TD_CMD_FILL:
ret = td_fill(type, cargc, cargv);
break;
case TD_CMD_READ:
ret = td_read(type, cargc, cargv);
break;
*/
default:
case TD_CMD_INVALID:
ret = EINVAL;
break;
}
free(cargv);
return (ret >= 0 ? ret : -ret);
}
+78
View File
@@ -0,0 +1,78 @@
#!/bin/bash
usage () { echo "USAGE: xmsnap <VM ID> <Backing File>"; }
#
# Check Usage
#
if [ -n "$1" ]
then
vmid=$1
else
usage
exit 1
fi
if [ -n "$2" ]
then
target=$2
else
usage
exit 1
fi
if [ -e "$target" ]
then
echo "Creating snapshot of file $target for VM $vmid."
else
usage
echo "File $target not found."
exit 1
fi
#
# Find the snapshot name
#
directory=`dirname "$target"`
target=`basename "$target"`
let maxidx=0
if [ -e $directory/${target}.snap1 ]
then
for idx in $(ls $directory/${target}.snap*)
do
let idx=${idx#$directory/${target}.snap}
if [ "$idx" -gt "$maxidx" ]
then
let maxidx=$idx
fi
done
fi
snap=${target}.snap`expr $maxidx + 1`
#
# Pause VM
#
xm pause $vmid
if [ "$?" -ne "0" ]; then
exit 1
fi
#
# Snap and reposition the files
#
mv $directory/$target $directory/$snap
if [ "$?" -ne "0" ]; then
exit 1
fi
qcow-create 0 $directory/$target $directory/$snap
#
# Unpause
#
xm unpause $vmid
exit
+14
View File
@@ -0,0 +1,14 @@
XEN_ROOT := ../../../
include $(XEN_ROOT)/tools/Rules.mk
.PHONY: all
all:
.PHONY: install
install:
$(INSTALL_DIR) -p $(DESTDIR)$(INCLUDEDIR)
.PHONY: clean
clean:
@:
+33
View File
@@ -0,0 +1,33 @@
/* $OpenBSD: atomicio.h,v 1.6 2005/05/24 17:32:43 avsm Exp $ */
/*
* Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Ensure all of data on socket comes through. f==read || f==vwrite
*/
size_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t);
#define vwrite (ssize_t (*)(int, void *, size_t))write
+249
View File
@@ -0,0 +1,249 @@
/* blktaplib.h
*
* Blktap library userspace code.
*
* Copyright (c) 2007, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __BLKTAPLIB_H__
#define __BLKTAPLIB_H__
#include <syslog.h>
#include <xenctrl.h>
#include <xen/io/blkif.h>
#if 1
#define DPRINTF(_f, _a...) syslog(LOG_INFO, _f, ##_a)
#else
#define DPRINTF(_f, _a...) ((void)0)
#endif
#define EPRINTF(_f, _a...) syslog(LOG_ERR, "tap-err:%s: " _f, __func__, ##_a)
#define BLK_RING_SIZE __RING_SIZE((blkif_sring_t *)0, XC_PAGE_SIZE)
/* size of the extra VMA area to map in attached pages. */
#define BLKTAP_VMA_PAGES BLK_RING_SIZE
/* blktap IOCTLs: These must correspond with the blktap driver ioctls */
#define BLKTAP_IOCTL_KICK_FE 1
#define BLKTAP_IOCTL_KICK_BE 2
#define BLKTAP_IOCTL_SETMODE 3
#define BLKTAP_IOCTL_SENDPID 4
#define BLKTAP_IOCTL_NEWINTF 5
#define BLKTAP_IOCTL_MINOR 6
#define BLKTAP_IOCTL_MAJOR 7
#define BLKTAP_QUERY_ALLOC_REQS 8
#define BLKTAP_IOCTL_FREEINTF 9
#define BLKTAP_IOCTL_PRINT_IDXS 100
#define BLKTAP_IOCTL_BACKDEV_SETUP 200
#define PRIO_SPECIAL_IO -9999
/* blktap switching modes: (Set with BLKTAP_IOCTL_SETMODE) */
#define BLKTAP_MODE_PASSTHROUGH 0x00000000 /* default */
#define BLKTAP_MODE_INTERCEPT_FE 0x00000001
#define BLKTAP_MODE_INTERCEPT_BE 0x00000002
#define BLKTAP_MODE_INTERPOSE \
(BLKTAP_MODE_INTERCEPT_FE | BLKTAP_MODE_INTERCEPT_BE)
static inline int BLKTAP_MODE_VALID(unsigned long arg)
{
return (
( arg == BLKTAP_MODE_PASSTHROUGH ) ||
( arg == BLKTAP_MODE_INTERCEPT_FE ) ||
( arg == BLKTAP_MODE_INTERPOSE ) );
}
#define MAX_REQUESTS BLK_RING_SIZE
#define BLKTAP_IOCTL_KICK 1
#define MAX_PENDING_REQS BLK_RING_SIZE
#define BLKTAP_DEV_DIR "/dev/xen"
#define BLKTAP_DEV_NAME "blktap"
#define BACKDEV_NAME "backdev"
#define BLKTAP_DEV_MINOR 0
#define BLKTAP_CTRL_DIR "/var/run/tap"
extern int blktap_major;
#define BLKTAP_RING_PAGES 1 /* Front */
#define BLKTAP_MMAP_REGION_SIZE (BLKTAP_RING_PAGES + MMAP_PAGES)
struct blkif;
struct blkif_info;
typedef struct {
blkif_request_t req;
int submitting;
int secs_pending;
int16_t status;
int num_retries;
struct timeval last_try;
} pending_req_t;
typedef struct blkif {
domid_t domid;
long int handle;
long int pdev;
long int readonly;
enum { DISCONNECTED, DISCONNECTING, CONNECTED } state;
struct blkif_ops *ops;
struct blkif *hash_next;
void *prv; /* device-specific data */
struct blkif_info *info; /*Image parameter passing */
pending_req_t pending_list[MAX_REQUESTS];
int devnum;
int fds[2];
int be_id;
char *backend_path;
int major;
int minor;
pid_t tappid;
int drivertype;
uint16_t cookie;
int err;
} blkif_t;
typedef struct blkif_info {
char *params;
int readonly;
int storage;
} blkif_info_t;
typedef struct tapdev_info {
int fd;
char *mem;
blkif_sring_t *sring;
blkif_back_ring_t fe_ring;
unsigned long vstart;
blkif_t *blkif;
} tapdev_info_t;
typedef struct domid_translate {
unsigned short domid;
unsigned short busid;
} domid_translate_t ;
typedef struct image {
unsigned long long size;
unsigned long secsize;
unsigned int info;
} image_t;
typedef struct msg_hdr {
uint16_t type;
uint16_t len;
uint16_t drivertype;
uint16_t cookie;
} msg_hdr_t;
typedef struct msg_params {
uint8_t readonly;
int path_off;
int path_len;
int storage;
} msg_params_t;
typedef struct msg_newdev {
uint8_t devnum;
uint16_t domid;
} msg_newdev_t;
typedef struct msg_pid {
pid_t pid;
} msg_pid_t;
typedef struct msg_cp {
int cp_uuid_off;
int cp_uuid_len;
int cp_drivertype;
} msg_cp_t;
typedef struct msg_lock {
int ro;
int enforce;
int uuid_off;
int uuid_len;
} msg_lock_t;
#define READ 0
#define WRITE 1
/*Control Messages between manager and tapdev*/
#define CTLMSG_PARAMS 1
#define CTLMSG_IMG 2
#define CTLMSG_IMG_FAIL 3
#define CTLMSG_NEWDEV 4
#define CTLMSG_NEWDEV_RSP 5
#define CTLMSG_NEWDEV_FAIL 6
#define CTLMSG_CLOSE 7
#define CTLMSG_CLOSE_RSP 8
#define CTLMSG_PID 9
#define CTLMSG_PID_RSP 10
#define CTLMSG_CHECKPOINT 11
#define CTLMSG_CHECKPOINT_RSP 12
#define CTLMSG_LOCK 13
#define CTLMSG_LOCK_RSP 14
#define CTLMSG_PAUSE 15
#define CTLMSG_PAUSE_RSP 16
#define CTLMSG_RESUME 17
#define CTLMSG_RESUME_RSP 18
#define TAPDISK_STORAGE_TYPE_NFS 1
#define TAPDISK_STORAGE_TYPE_EXT 2
#define TAPDISK_STORAGE_TYPE_LVM 3
#define TAPDISK_STORAGE_TYPE_DEFAULT TAPDISK_STORAGE_TYPE_EXT
/* Abitrary values, must match the underlying driver... */
#define MAX_TAP_DEV 256
/* Accessing attached data page mappings */
#define MMAP_PAGES \
(MAX_PENDING_REQS * BLKIF_MAX_SEGMENTS_PER_REQUEST)
#define MMAP_VADDR(_vstart,_req,_seg) \
((_vstart) + \
((_req) * BLKIF_MAX_SEGMENTS_PER_REQUEST * getpagesize()) + \
((_seg) * getpagesize()))
/* Defines that are only used by library clients */
#ifndef __COMPILING_BLKTAP_LIB
static char *blkif_op_name[] = {
[BLKIF_OP_READ] = "READ",
[BLKIF_OP_WRITE] = "WRITE",
};
#endif /* __COMPILING_BLKTAP_LIB */
#endif /* __BLKTAPLIB_H__ */
+68
View File
@@ -0,0 +1,68 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VHD_JOURNAL_H_
#define _VHD_JOURNAL_H_
#include <inttypes.h>
#include "libvhd.h"
#define VHD_JOURNAL_METADATA 0x01
#define VHD_JOURNAL_DATA 0x02
#define VHD_JOURNAL_HEADER_COOKIE "vjournal"
#define VHD_JOURNAL_ENTRY_COOKIE 0xaaaa12344321aaaa
typedef struct vhd_journal_header {
char cookie[8];
uuid_t uuid;
uint64_t vhd_footer_offset;
uint32_t journal_data_entries;
uint32_t journal_metadata_entries;
uint64_t journal_data_offset;
uint64_t journal_metadata_offset;
uint64_t journal_eof;
char pad[448];
} vhd_journal_header_t;
typedef struct vhd_journal {
char *jname;
int jfd;
int is_block; /* is jfd a block device */
vhd_journal_header_t header;
vhd_context_t vhd;
} vhd_journal_t;
int vhd_journal_create(vhd_journal_t *, const char *file, const char *jfile);
int vhd_journal_open(vhd_journal_t *, const char *file, const char *jfile);
int vhd_journal_add_block(vhd_journal_t *, uint32_t block, char mode);
int vhd_journal_commit(vhd_journal_t *);
int vhd_journal_revert(vhd_journal_t *);
int vhd_journal_close(vhd_journal_t *);
int vhd_journal_remove(vhd_journal_t *);
#endif
+308
View File
@@ -0,0 +1,308 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VHD_LIB_H_
#define _VHD_LIB_H_
#include <string.h>
#include <endian.h>
#include <byteswap.h>
#include <uuid/uuid.h>
#include "vhd.h"
#if BYTE_ORDER == LITTLE_ENDIAN
#define BE16_IN(foo) (*(foo)) = bswap_16(*(foo))
#define BE32_IN(foo) (*(foo)) = bswap_32(*(foo))
#define BE64_IN(foo) (*(foo)) = bswap_64(*(foo))
#define BE16_OUT(foo) (*(foo)) = bswap_16(*(foo))
#define BE32_OUT(foo) (*(foo)) = bswap_32(*(foo))
#define BE64_OUT(foo) (*(foo)) = bswap_64(*(foo))
#else
#define BE16_IN(foo)
#define BE32_IN(foo)
#define BE64_IN(foo)
#define BE32_OUT(foo)
#define BE32_OUT(foo)
#define BE64_OUT(foo)
#endif
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define VHD_MAX_NAME_LEN 1024
#define VHD_BLOCK_SHIFT 21
#define VHD_BLOCK_SIZE (1ULL << VHD_BLOCK_SHIFT)
#define UTF_16 "UTF-16"
#define UTF_16LE "UTF-16LE"
#define UTF_16BE "UTF-16BE"
#define VHD_OPEN_RDONLY 0x00001
#define VHD_OPEN_RDWR 0x00002
#define VHD_OPEN_FAST 0x00004
#define VHD_OPEN_STRICT 0x00008
#define VHD_OPEN_IGNORE_DISABLED 0x00010
#define VHD_FLAG_CREAT_PARENT_RAW 0x00001
#define vhd_flag_set(word, flag) ((word) |= (flag))
#define vhd_flag_clear(word, flag) ((word) &= ~(flag))
#define vhd_flag_test(word, flag) ((word) & (flag))
#define ENABLE_FAILURE_TESTING
#define FAIL_REPARENT_BEGIN 0
#define FAIL_REPARENT_LOCATOR 1
#define FAIL_REPARENT_END 2
#define FAIL_RESIZE_BEGIN 3
#define FAIL_RESIZE_DATA_MOVED 4
#define FAIL_RESIZE_METADATA_MOVED 5
#define FAIL_RESIZE_END 6
#define NUM_FAIL_TESTS 7
#ifdef ENABLE_FAILURE_TESTING
#define TEST_FAIL_AT(point) \
if (TEST_FAIL[point]) { \
printf("Failing at %s\n", ENV_VAR_FAIL[point]); exit(EINVAL); }
#define TEST_FAIL_EXTERN_VARS \
extern const char* ENV_VAR_FAIL[]; \
extern int TEST_FAIL[];
#else
#define TEST_FAIL_AT(point)
#define TEST_FAIL_EXTERN_VARS
#endif // ENABLE_FAILURE_TESTING
static const char VHD_POISON_COOKIE[] = "v_poison";
typedef struct hd_ftr vhd_footer_t;
typedef struct dd_hdr vhd_header_t;
typedef struct vhd_bat vhd_bat_t;
typedef struct vhd_batmap vhd_batmap_t;
typedef struct dd_batmap_hdr vhd_batmap_header_t;
typedef struct prt_loc vhd_parent_locator_t;
typedef struct vhd_context vhd_context_t;
typedef uint32_t vhd_flag_creat_t;
struct vhd_bat {
uint32_t spb;
uint32_t entries;
uint32_t *bat;
};
struct vhd_batmap {
vhd_batmap_header_t header;
char *map;
};
struct vhd_context {
int fd;
char *file;
int oflags;
int is_block;
uint32_t spb;
uint32_t bm_secs;
vhd_header_t header;
vhd_footer_t footer;
vhd_bat_t bat;
vhd_batmap_t batmap;
};
static inline uint32_t
secs_round_up(uint64_t bytes)
{
return ((bytes + (VHD_SECTOR_SIZE - 1)) >> VHD_SECTOR_SHIFT);
}
static inline uint32_t
secs_round_up_no_zero(uint64_t bytes)
{
return (secs_round_up(bytes) ? : 1);
}
static inline uint64_t
vhd_sectors_to_bytes(uint64_t sectors)
{
return sectors << VHD_SECTOR_SHIFT;
}
static inline uint64_t
vhd_bytes_padded(uint64_t bytes)
{
return vhd_sectors_to_bytes(secs_round_up_no_zero(bytes));
}
static inline int
vhd_type_dynamic(vhd_context_t *ctx)
{
return (ctx->footer.type == HD_TYPE_DYNAMIC ||
ctx->footer.type == HD_TYPE_DIFF);
}
static inline int
vhd_creator_tapdisk(vhd_context_t *ctx)
{
return !strncmp(ctx->footer.crtr_app, "tap", 3);
}
static inline int
vhd_disabled(vhd_context_t *ctx)
{
return (!memcmp(ctx->footer.cookie,
VHD_POISON_COOKIE, sizeof(ctx->footer.cookie)));
}
static inline size_t
vhd_parent_locator_size(vhd_parent_locator_t *loc)
{
/*
* MICROSOFT_COMPAT
* data_space *should* be in sectors,
* but sometimes we find it in bytes
*/
if (loc->data_space < 512)
return vhd_sectors_to_bytes(loc->data_space);
else if (loc->data_space % 512 == 0)
return loc->data_space;
else
return 0;
}
static inline int
vhd_parent_raw(vhd_context_t *ctx)
{
return uuid_is_null(ctx->header.prt_uuid);
}
void libvhd_set_log_level(int);
int vhd_test_file_fixed(const char *, int *);
uint32_t vhd_time(time_t time);
size_t vhd_time_to_string(uint32_t timestamp, char *target);
uint32_t vhd_chs(uint64_t size);
uint32_t vhd_checksum_footer(vhd_footer_t *);
uint32_t vhd_checksum_header(vhd_header_t *);
uint32_t vhd_checksum_batmap(vhd_batmap_t *);
void vhd_footer_in(vhd_footer_t *);
void vhd_footer_out(vhd_footer_t *);
void vhd_header_in(vhd_header_t *);
void vhd_header_out(vhd_header_t *);
void vhd_bat_in(vhd_bat_t *);
void vhd_bat_out(vhd_bat_t *);
void vhd_batmap_header_in(vhd_batmap_t *);
void vhd_batmap_header_out(vhd_batmap_t *);
int vhd_validate_footer(vhd_footer_t *footer);
int vhd_validate_header(vhd_header_t *header);
int vhd_validate_batmap_header(vhd_batmap_t *batmap);
int vhd_validate_batmap(vhd_batmap_t *batmap);
int vhd_validate_platform_code(uint32_t code);
int vhd_open(vhd_context_t *, const char *file, int flags);
void vhd_close(vhd_context_t *);
int vhd_create(const char *name, uint64_t bytes, int type, vhd_flag_creat_t);
/* vhd_snapshot: the bytes parameter is optional and can be 0 if the snapshot
* is to have the same size as the (first non-empty) parent */
int vhd_snapshot(const char *snapshot, uint64_t bytes, const char *parent,
vhd_flag_creat_t);
int vhd_hidden(vhd_context_t *, int *);
int vhd_chain_depth(vhd_context_t *, int *);
off64_t vhd_position(vhd_context_t *);
int vhd_seek(vhd_context_t *, off64_t, int);
int vhd_read(vhd_context_t *, void *, size_t);
int vhd_write(vhd_context_t *, void *, size_t);
int vhd_offset(vhd_context_t *, uint32_t, uint32_t *);
int vhd_end_of_headers(vhd_context_t *ctx, off64_t *off);
int vhd_end_of_data(vhd_context_t *ctx, off64_t *off);
int vhd_batmap_header_offset(vhd_context_t *ctx, off64_t *off);
int vhd_get_header(vhd_context_t *);
int vhd_get_footer(vhd_context_t *);
int vhd_get_bat(vhd_context_t *);
int vhd_get_batmap(vhd_context_t *);
void vhd_put_header(vhd_context_t *);
void vhd_put_footer(vhd_context_t *);
void vhd_put_bat(vhd_context_t *);
void vhd_put_batmap(vhd_context_t *);
int vhd_has_batmap(vhd_context_t *);
int vhd_batmap_test(vhd_context_t *, vhd_batmap_t *, uint32_t);
void vhd_batmap_set(vhd_context_t *, vhd_batmap_t *, uint32_t);
void vhd_batmap_clear(vhd_context_t *, vhd_batmap_t *, uint32_t);
int vhd_get_phys_size(vhd_context_t *, off64_t *);
int vhd_set_phys_size(vhd_context_t *, off64_t);
int vhd_bitmap_test(vhd_context_t *, char *, uint32_t);
void vhd_bitmap_set(vhd_context_t *, char *, uint32_t);
void vhd_bitmap_clear(vhd_context_t *, char *, uint32_t);
int vhd_parent_locator_count(vhd_context_t *);
int vhd_parent_locator_get(vhd_context_t *, char **);
int vhd_parent_locator_read(vhd_context_t *, vhd_parent_locator_t *, char **);
int vhd_find_parent(vhd_context_t *, const char *, char **);
int vhd_parent_locator_write_at(vhd_context_t *, const char *,
off64_t, uint32_t, size_t,
vhd_parent_locator_t *);
int vhd_header_decode_parent(vhd_context_t *, vhd_header_t *, char **);
int vhd_change_parent(vhd_context_t *, char *parent_path, int raw);
int vhd_read_footer(vhd_context_t *, vhd_footer_t *);
int vhd_read_footer_at(vhd_context_t *, vhd_footer_t *, off64_t);
int vhd_read_footer_strict(vhd_context_t *, vhd_footer_t *);
int vhd_read_header(vhd_context_t *, vhd_header_t *);
int vhd_read_header_at(vhd_context_t *, vhd_header_t *, off64_t);
int vhd_read_bat(vhd_context_t *, vhd_bat_t *);
int vhd_read_batmap(vhd_context_t *, vhd_batmap_t *);
int vhd_read_bitmap(vhd_context_t *, uint32_t block, char **bufp);
int vhd_read_block(vhd_context_t *, uint32_t block, char **bufp);
int vhd_write_footer(vhd_context_t *, vhd_footer_t *);
int vhd_write_footer_at(vhd_context_t *, vhd_footer_t *, off64_t);
int vhd_write_header(vhd_context_t *, vhd_header_t *);
int vhd_write_header_at(vhd_context_t *, vhd_header_t *, off64_t);
int vhd_write_bat(vhd_context_t *, vhd_bat_t *);
int vhd_write_batmap(vhd_context_t *, vhd_batmap_t *);
int vhd_write_bitmap(vhd_context_t *, uint32_t block, char *bitmap);
int vhd_write_block(vhd_context_t *, uint32_t block, char *data);
int vhd_io_read(vhd_context_t *, char *, uint64_t, uint32_t);
int vhd_io_write(vhd_context_t *, char *, uint64_t, uint32_t);
#endif
+93
View File
@@ -0,0 +1,93 @@
/*
* list.h
*
* This is a subset of linux's list.h intended to be used in user-space.
*
*/
#ifndef __LIST_H__
#define __LIST_H__
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
#endif /* __LIST_H__ */
+71
View File
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _LVM_UTIL_H_
#define _LVM_UTIL_H_
#include <inttypes.h>
#define MAX_NAME_SIZE 256
#define LVM_SEG_TYPE_LINEAR 1
#define LVM_SEG_TYPE_UNKNOWN 2
struct lv_segment {
uint8_t type;
char device[MAX_NAME_SIZE];
uint64_t pe_start;
uint64_t pe_size;
};
struct lv {
char name[MAX_NAME_SIZE];
uint64_t size;
uint32_t segments;
struct lv_segment first_segment;
};
struct pv {
char name[MAX_NAME_SIZE];
uint64_t start;
};
struct vg {
char name[MAX_NAME_SIZE];
uint64_t extent_size;
int pv_cnt;
struct pv *pvs;
int lv_cnt;
struct lv *lvs;
};
int lvm_scan_vg(const char *vg_name, struct vg *vg);
void lvm_free_vg(struct vg *vg);
#endif
+43
View File
@@ -0,0 +1,43 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _RELATIVE_PATH_H_
#define _RELATIVE_PATH_H_
#include <syslog.h>
#define DELIMITER '/'
#define MAX_NAME_LEN 1000
#define EPRINTF(_f, _a...) syslog(LOG_ERR, "tap-err:%s: " _f, __func__, ##_a)
/*
* returns a relative path from @src to @dest
* result should be freed
*/
char *relative_path_to(char *src, char *dest, int *err);
#endif
+141
View File
@@ -0,0 +1,141 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _TAPDISK_MESSAGE_H_
#define _TAPDISK_MESSAGE_H_
#include <inttypes.h>
#define TAPDISK_MESSAGE_MAX_PATH_LENGTH 256
#define TAPDISK_MESSAGE_STRING_LENGTH 256
#define TAPDISK_MESSAGE_FLAG_SHARED 0x01
#define TAPDISK_MESSAGE_FLAG_RDONLY 0x02
#define TAPDISK_MESSAGE_FLAG_ADD_CACHE 0x04
#define TAPDISK_MESSAGE_FLAG_VHD_INDEX 0x08
#define TAPDISK_MESSAGE_FLAG_LOG_DIRTY 0x10
typedef struct tapdisk_message tapdisk_message_t;
typedef uint8_t tapdisk_message_flag_t;
typedef struct tapdisk_message_image tapdisk_message_image_t;
typedef struct tapdisk_message_params tapdisk_message_params_t;
typedef struct tapdisk_message_string tapdisk_message_string_t;
struct tapdisk_message_params {
tapdisk_message_flag_t flags;
uint8_t storage;
uint32_t devnum;
uint32_t domid;
uint16_t path_len;
char path[TAPDISK_MESSAGE_MAX_PATH_LENGTH];
};
struct tapdisk_message_image {
uint64_t sectors;
uint32_t sector_size;
uint32_t info;
};
struct tapdisk_message_string {
char text[TAPDISK_MESSAGE_STRING_LENGTH];
};
struct tapdisk_message {
uint16_t type;
uint16_t cookie;
uint16_t drivertype;
union {
pid_t tapdisk_pid;
tapdisk_message_image_t image;
tapdisk_message_params_t params;
tapdisk_message_string_t string;
} u;
};
enum tapdisk_message_id {
TAPDISK_MESSAGE_ERROR = 1,
TAPDISK_MESSAGE_RUNTIME_ERROR,
TAPDISK_MESSAGE_PID,
TAPDISK_MESSAGE_PID_RSP,
TAPDISK_MESSAGE_OPEN,
TAPDISK_MESSAGE_OPEN_RSP,
TAPDISK_MESSAGE_PAUSE,
TAPDISK_MESSAGE_PAUSE_RSP,
TAPDISK_MESSAGE_RESUME,
TAPDISK_MESSAGE_RESUME_RSP,
TAPDISK_MESSAGE_CLOSE,
TAPDISK_MESSAGE_CLOSE_RSP,
TAPDISK_MESSAGE_EXIT,
};
static inline char *
tapdisk_message_name(enum tapdisk_message_id id)
{
switch (id) {
case TAPDISK_MESSAGE_ERROR:
return "error";
case TAPDISK_MESSAGE_PID:
return "pid";
case TAPDISK_MESSAGE_PID_RSP:
return "pid response";
case TAPDISK_MESSAGE_OPEN:
return "open";
case TAPDISK_MESSAGE_OPEN_RSP:
return "open response";
case TAPDISK_MESSAGE_PAUSE:
return "pause";
case TAPDISK_MESSAGE_PAUSE_RSP:
return "pause response";
case TAPDISK_MESSAGE_RESUME:
return "resume";
case TAPDISK_MESSAGE_RESUME_RSP:
return "resume response";
case TAPDISK_MESSAGE_CLOSE:
return "close";
case TAPDISK_MESSAGE_CLOSE_RSP:
return "close response";
case TAPDISK_MESSAGE_EXIT:
return "exit";
default:
return "unknown";
}
}
#endif
+44
View File
@@ -0,0 +1,44 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VHD_UTIL_H_
#define _VHD_UTIL_H_
int vhd_util_create(int argc, char **argv);
int vhd_util_snapshot(int argc, char **argv);
int vhd_util_query(int argc, char **argv);
int vhd_util_read(int argc, char **argv);
int vhd_util_set_field(int argc, char **argv);
int vhd_util_repair(int argc, char **argv);
int vhd_util_fill(int argc, char **argv);
int vhd_util_resize(int argc, char **argv);
int vhd_util_coalesce(int argc, char **argv);
int vhd_util_modify(int argc, char **argv);
int vhd_util_scan(int argc, char **argv);
int vhd_util_check(int argc, char **argv);
int vhd_util_revert(int argc, char **argv);
#endif
+221
View File
@@ -0,0 +1,221 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __VHD_H__
#define __VHD_H__
#include <asm/types.h>
#include <uuid/uuid.h>
#include <inttypes.h>
typedef uint32_t u32;
typedef uint64_t u64;
#define DEBUG 1
/* ---------------------------------------------------------------------- */
/* General definitions. */
/* ---------------------------------------------------------------------- */
#define VHD_SECTOR_SIZE 512
#define VHD_SECTOR_SHIFT 9
/* ---------------------------------------------------------------------- */
/* This is the generic disk footer, used by all disks. */
/* ---------------------------------------------------------------------- */
struct hd_ftr {
char cookie[8]; /* Identifies original creator of the disk */
u32 features; /* Feature Support -- see below */
u32 ff_version; /* (major,minor) version of disk file */
u64 data_offset; /* Abs. offset from SOF to next structure */
u32 timestamp; /* Creation time. secs since 1/1/2000GMT */
char crtr_app[4]; /* Creator application */
u32 crtr_ver; /* Creator version (major,minor) */
u32 crtr_os; /* Creator host OS */
u64 orig_size; /* Size at creation (bytes) */
u64 curr_size; /* Current size of disk (bytes) */
u32 geometry; /* Disk geometry */
u32 type; /* Disk type */
u32 checksum; /* 1's comp sum of this struct. */
uuid_t uuid; /* Unique disk ID, used for naming parents */
char saved; /* one-bit -- is this disk/VM in a saved state? */
char hidden; /* tapdisk-specific field: is this vdi hidden? */
char reserved[426]; /* padding */
};
/* VHD cookie string. */
static const char HD_COOKIE[9] = "conectix";
/* Feature fields in hd_ftr */
#define HD_NO_FEATURES 0x00000000
#define HD_TEMPORARY 0x00000001 /* disk can be deleted on shutdown */
#define HD_RESERVED 0x00000002 /* NOTE: must always be set */
/* Version field in hd_ftr */
#define HD_FF_VERSION 0x00010000
/* Known creator OS type fields in hd_ftr.crtr_os */
#define HD_CR_OS_WINDOWS 0x5769326B /* (Wi2k) */
#define HD_CR_OS_MACINTOSH 0x4D616320 /* (Mac ) */
/*
* version 0.1: little endian bitmaps
* version 1.1: big endian bitmaps; batmap
* version 1.2: libvhd
* version 1.3: batmap version bump to 1.2
*/
#define VHD_VERSION(major, minor) (((major) << 16) | ((minor) & 0x0000FFFF))
#define VHD_CURRENT_VERSION VHD_VERSION(1, 3)
/* Disk geometry accessor macros. */
/* Geometry is a triple of (cylinders (2 bytes), tracks (1 byte), and
* secotrs-per-track (1 byte))
*/
#define GEOM_GET_CYLS(_g) (((_g) >> 16) & 0xffff)
#define GEOM_GET_HEADS(_g) (((_g) >> 8) & 0xff)
#define GEOM_GET_SPT(_g) ((_g) & 0xff)
#define GEOM_ENCODE(_c, _h, _s) (((_c) << 16) | ((_h) << 8) | (_s))
/* type field in hd_ftr */
#define HD_TYPE_NONE 0
#define HD_TYPE_FIXED 2 /* fixed-allocation disk */
#define HD_TYPE_DYNAMIC 3 /* dynamic disk */
#define HD_TYPE_DIFF 4 /* differencing disk */
/* String table for hd.type */
static const char *HD_TYPE_STR[7] = {
"None", /* 0 */
"Reserved (deprecated)", /* 1 */
"Fixed hard disk", /* 2 */
"Dynamic hard disk", /* 3 */
"Differencing hard disk", /* 4 */
"Reserved (deprecated)", /* 5 */
"Reserved (deprecated)" /* 6 */
};
#define HD_TYPE_MAX 6
struct prt_loc {
u32 code; /* Platform code -- see defines below. */
u32 data_space; /* Number of 512-byte sectors to store locator */
u32 data_len; /* Actual length of parent locator in bytes */
u32 res; /* Must be zero */
u64 data_offset; /* Absolute offset of locator data (bytes) */
};
/* Platform Codes */
#define PLAT_CODE_NONE 0x0
#define PLAT_CODE_WI2R 0x57693272 /* deprecated */
#define PLAT_CODE_WI2K 0x5769326B /* deprecated */
#define PLAT_CODE_W2RU 0x57327275 /* Windows relative path (UTF-16) */
#define PLAT_CODE_W2KU 0x57326B75 /* Windows absolute path (UTF-16) */
#define PLAT_CODE_MAC 0x4D616320 /* MacOS alias stored as a blob. */
#define PLAT_CODE_MACX 0x4D616358 /* File URL (UTF-8), see RFC 2396. */
/* ---------------------------------------------------------------------- */
/* This is the dynamic disk header. */
/* ---------------------------------------------------------------------- */
struct dd_hdr {
char cookie[8]; /* Should contain "cxsparse" */
u64 data_offset; /* Byte offset of next record. (Unused) 0xffs */
u64 table_offset; /* Absolute offset to the BAT. */
u32 hdr_ver; /* Version of the dd_hdr (major,minor) */
u32 max_bat_size; /* Maximum number of entries in the BAT */
u32 block_size; /* Block size in bytes. Must be power of 2. */
u32 checksum; /* Header checksum. 1's comp of all fields. */
uuid_t prt_uuid; /* ID of the parent disk. */
u32 prt_ts; /* Modification time of the parent disk */
u32 res1; /* Reserved. */
char prt_name[512]; /* Parent unicode name. */
struct prt_loc loc[8]; /* Parent locator entries. */
char res2[256]; /* Reserved. */
};
/* VHD cookie string. */
static const char DD_COOKIE[9] = "cxsparse";
/* Version field in hd_ftr */
#define DD_VERSION 0x00010000
/* Default blocksize is 2 meg. */
#define DD_BLOCKSIZE_DEFAULT 0x00200000
#define DD_BLK_UNUSED 0xFFFFFFFF
struct dd_batmap_hdr {
char cookie[8]; /* should contain "tdbatmap" */
u64 batmap_offset; /* byte offset to batmap */
u32 batmap_size; /* batmap size in sectors */
u32 batmap_version; /* version of batmap */
u32 checksum; /* batmap checksum -- 1's complement of batmap */
};
static const char VHD_BATMAP_COOKIE[9] = "tdbatmap";
/*
* version 1.1: signed char checksum
*/
#define VHD_BATMAP_VERSION(major, minor) (((major) << 16) | ((minor) & 0x0000FFFF))
#define VHD_BATMAP_CURRENT_VERSION VHD_BATMAP_VERSION(1, 2)
/* Layout of a dynamic disk:
*
* +-------------------------------------------------+
* | Mirror image of HD footer (hd_ftr) (512 bytes) |
* +-------------------------------------------------+
* | Sparse drive header (dd_hdr) (1024 bytes) |
* +-------------------------------------------------+
* | BAT (Block allocation table) |
* | - Array of absolute sector offsets into the |
* | file (u32). |
* | - Rounded up to a sector boundary. |
* | - Unused entries are marked as 0xFFFFFFFF |
* | - max entries in dd_hdr->max_bat_size |
* +-------------------------------------------------+
* | Data Block 0 |
* | Bitmap (padded to 512 byte sector boundary) |
* | - each bit indicates whether the associated |
* | sector within this block is used. |
* | Data |
* | - power-of-two multiple of sectors. |
* | - default 2MB (4096 * 512) |
* | - Any entries with zero in bitmap should be |
* | zero on disk |
* +-------------------------------------------------+
* | Data Block 1 |
* +-------------------------------------------------+
* | ... |
* +-------------------------------------------------+
* | Data Block n |
* +-------------------------------------------------+
* | HD Footer (511 bytes) |
* +-------------------------------------------------+
*/
#endif
+38
View File
@@ -0,0 +1,38 @@
XEN_ROOT = ../../../
BLKTAP_ROOT := ../
include $(XEN_ROOT)/tools/Rules.mk
ifeq ($(LVM_UTIL_TEST),y)
TEST := lvm-util
endif
CFLAGS += -Werror
CFLAGS += -Wno-unused
CFLAGS += -I../include
CFLAGS += -D_GNU_SOURCE
ifeq ($(CONFIG_X86_64),y)
CFLAGS += -fPIC
endif
# Get gcc to generate the dependencies for us.
CFLAGS += -Wp,-MD,.$(@F).d
DEPS = .*.d
LVM-OBJS := lvm-util.o
all: build
build: $(TEST) $(LVM-OBJS)
install: all
lvm-util: lvm-util.o
$(CC) $(CFLAGS) -DLVM_UTIL -o lvm-util lvm-util.c
clean:
rm -rf *.o *~ $(DEPS) $(IBIN)
.PHONY: all build clean install lvm-util
-include $(DEPS)
+349
View File
@@ -0,0 +1,349 @@
/*
* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "lvm-util.h"
#define _NAME "%255s"
static char line[1024];
static inline int
lvm_read_line(FILE *scan)
{
memset(line, 0, sizeof(line));
return (fscanf(scan, "%1023[^\n]", line) != 1);
}
static inline int
lvm_next_line(FILE *scan)
{
return (fscanf(scan, "%1023[\n]", line) != 1);
}
static int
lvm_copy_name(char *dst, const char *src, size_t size)
{
if (strnlen(src, size) == size)
return -ENAMETOOLONG;
strcpy(dst, src);
return 0;
}
static int
lvm_parse_pv(struct vg *vg, const char *name, int pvs, uint64_t start)
{
int i, err;
struct pv *pv;
pv = NULL;
if (!vg->pvs) {
vg->pvs = calloc(pvs, sizeof(struct pv));
if (!vg->pvs)
return -ENOMEM;
}
for (i = 0; i < pvs; i++) {
pv = vg->pvs + i;
if (!pv->name[0])
break;
if (!strcmp(pv->name, name))
return -EEXIST;
}
if (!pv)
return -ENOENT;
if (i == pvs)
return -ENOMEM;
err = lvm_copy_name(pv->name, name, sizeof(pv->name) - 1);
if (err)
return err;
pv->start = start;
return 0;
}
static int
lvm_open_vg(const char *vgname, struct vg *vg)
{
FILE *scan;
int i, err, pvs, lvs;
char *cmd, pvname[256];
uint64_t size, pv_start;
memset(vg, 0, sizeof(*vg));
err = asprintf(&cmd, "/usr/sbin/vgs %s --noheadings --nosuffix --units=b "
"--options=vg_name,vg_extent_size,lv_count,pv_count,"
"pv_name,pe_start --unbuffered 2> /dev/null", vgname);
if (err == -1)
return -ENOMEM;
errno = 0;
scan = popen(cmd, "r");
if (!scan) {
err = (errno ? -errno : ENOMEM);
goto out;
}
for (;;) {
if (lvm_read_line(scan))
break;
err = -EINVAL;
if (sscanf(line, _NAME" %"SCNu64" %d %d "_NAME" %"SCNu64,
vg->name, &size, &lvs, &pvs, pvname, &pv_start) != 6)
goto out;
if (strcmp(vg->name, vgname))
goto out;
err = lvm_parse_pv(vg, pvname, pvs, pv_start);
if (err)
goto out;
if (lvm_next_line(scan))
break;
}
err = -EINVAL;
if (strcmp(vg->name, vgname))
goto out;
for (i = 0; i < pvs; i++)
if (!vg->pvs[i].name[0])
goto out;
err = -ENOMEM;
vg->lvs = calloc(lvs, sizeof(struct lv));
if (!vg->lvs)
goto out;
err = 0;
vg->lv_cnt = lvs;
vg->pv_cnt = pvs;
vg->extent_size = size;
out:
if (scan)
pclose(scan);
if (err)
lvm_free_vg(vg);
free(cmd);
return err;
}
static int
lvm_parse_lv_devices(struct vg *vg, struct lv_segment *seg, char *devices)
{
int i;
uint64_t start, pe_start;
for (i = 0; i < strlen(devices); i++)
if (strchr(",()", devices[i]))
devices[i] = ' ';
if (sscanf(devices, _NAME" %"SCNu64, seg->device, &start) != 2)
return -EINVAL;
pe_start = -1;
for (i = 0; i < vg->pv_cnt; i++)
if (!strcmp(vg->pvs[i].name, seg->device)) {
pe_start = vg->pvs[i].start;
break;
}
if (pe_start == -1)
return -EINVAL;
seg->pe_start = (start * vg->extent_size) + pe_start;
return 0;
}
static int
lvm_scan_lvs(struct vg *vg)
{
char *cmd;
FILE *scan;
int i, err;
err = asprintf(&cmd, "/usr/sbin/lvs %s --noheadings --nosuffix --units=b "
"--options=lv_name,lv_size,segtype,seg_count,seg_start,"
"seg_size,devices --unbuffered 2> /dev/null", vg->name);
if (err == -1)
return -ENOMEM;
errno = 0;
scan = popen(cmd, "r");
if (!scan) {
err = (errno ? -errno : -ENOMEM);
goto out;
}
for (i = 0;;) {
int segs;
struct lv *lv;
struct lv_segment seg;
uint64_t size, seg_start;
char type[32], name[256], dev[256], devices[1024];
if (i >= vg->lv_cnt)
break;
if (lvm_read_line(scan)) {
vg->lv_cnt = i;
break;
}
err = -EINVAL;
lv = vg->lvs + i;
if (sscanf(line, _NAME" %"SCNu64" %31s %u %"SCNu64" %"SCNu64" %1023s",
name, &size, type, &segs, &seg_start,
&seg.pe_size, devices) != 7)
goto out;
if (seg_start)
goto next;
if (!strcmp(type, "linear"))
seg.type = LVM_SEG_TYPE_LINEAR;
else
seg.type = LVM_SEG_TYPE_UNKNOWN;
if (lvm_parse_lv_devices(vg, &seg, devices))
goto out;
i++;
lv->size = size;
lv->segments = segs;
lv->first_segment = seg;
err = lvm_copy_name(lv->name, name, sizeof(lv->name) - 1);
if (err)
goto out;
err = -EINVAL;
next:
if (lvm_next_line(scan))
goto out;
}
err = 0;
out:
if (scan)
pclose(scan);
free(cmd);
return err;
}
void
lvm_free_vg(struct vg *vg)
{
free(vg->lvs);
free(vg->pvs);
memset(vg, 0, sizeof(*vg));
}
int
lvm_scan_vg(const char *vg_name, struct vg *vg)
{
int err;
memset(vg, 0, sizeof(*vg));
err = lvm_open_vg(vg_name, vg);
if (err)
return err;
err = lvm_scan_lvs(vg);
if (err) {
lvm_free_vg(vg);
return err;
}
return 0;
}
#ifdef LVM_UTIL
static int
usage(void)
{
printf("usage: lvm-util <vgname>\n");
exit(EINVAL);
}
int
main(int argc, char **argv)
{
int i, err;
struct vg vg;
struct pv *pv;
struct lv *lv;
struct lv_segment *seg;
if (argc != 2)
usage();
err = lvm_scan_vg(argv[1], &vg);
if (err) {
printf("scan failed: %d\n", err);
return (err >= 0 ? err : -err);
}
printf("vg %s: extent_size: %"PRIu64", pvs: %d, lvs: %d\n",
vg.name, vg.extent_size, vg.pv_cnt, vg.lv_cnt);
for (i = 0; i < vg.pv_cnt; i++) {
pv = vg.pvs + i;
printf("pv %s: start %"PRIu64"\n", pv->name, pv->start);
}
for (i = 0; i < vg.lv_cnt; i++) {
lv = vg.lvs + i;
seg = &lv->first_segment;
printf("lv %s: size: %"PRIu64", segments: %u, type: %u, "
"dev: %s, pe_start: %"PRIu64", pe_size: %"PRIu64"\n",
lv->name, lv->size, lv->segments, seg->type,
seg->device, seg->pe_start, seg->pe_size);
}
lvm_free_vg(&vg);
return 0;
}
#endif
+55
View File
@@ -0,0 +1,55 @@
XEN_ROOT=../../../
BLKTAP_ROOT := ../
include $(XEN_ROOT)/tools/Rules.mk
IBIN = vhd-util vhd-update
INST_DIR = $(SBINDIR)
LIBDIR = lib
CFLAGS += -Werror
CFLAGS += -Wno-unused
CFLAGS += -I../include
CFLAGS += -D_GNU_SOURCE
ifeq ($(CONFIG_X86_64),y)
CFLAGS += -fPIC
endif
ifeq ($(VHD_STATIC),y)
CFLAGS += -static
endif
LIBS := -L$(LIBDIR) -lvhd
LIBS += -luuid
# Get gcc to generate the dependencies for us.
CFLAGS += -Wp,-MD,.$(@F).d
DEPS = .*.d
all: build
build: libvhd $(IBIN)
libvhd:
@set -e
$(MAKE) -C $(LIBDIR) all
vhd-util: vhd-util.o
$(CC) $(CFLAGS) -o vhd-util vhd-util.o $(LIBS)
vhd-update: vhd-update.o
$(CC) $(CFLAGS) -o vhd-update vhd-update.o $(LIBS)
install: all
$(MAKE) -C $(LIBDIR) install
$(INSTALL_DIR) -p $(DESTDIR)$(INST_DIR)
$(INSTALL_PROG) $(IBIN) $(DESTDIR)$(INST_DIR)
clean:
$(MAKE) -C $(LIBDIR) clean
rm -rf *.o *~ $(DEPS) $(IBIN)
.PHONY: all build clean install libvhd vhd-util vhd-update
-include $(DEPS)
+73
View File
@@ -0,0 +1,73 @@
XEN_ROOT=../../../../
BLKTAP_ROOT := ../../
include $(XEN_ROOT)/tools/Rules.mk
LIBVHD-MAJOR = 1.0
LIBVHD-MINOR = 0
LIBVHD-SONAME = libvhd.so.$(LIBVHD-MAJOR)
LVM-UTIL-OBJ := $(BLKTAP_ROOT)lvm/lvm-util.o
LIBVHD-BUILD := libvhd.a
INST-DIR = $(LIBDIR)
CFLAGS += -Werror
CFLAGS += -Wno-unused
CFLAGS += -I../../include
CFLAGS += -D_GNU_SOURCE
CFLAGS += -fPIC
CFLAGS += -g
LIBS := -luuid
# Get gcc to generate the dependencies for us.
CFLAGS += -Wp,-MD,.$(@F).d
DEPS = .*.d
LIB-SRCS := libvhd.c
LIB-SRCS += libvhd-journal.c
LIB-SRCS += vhd-util-coalesce.c
LIB-SRCS += vhd-util-create.c
LIB-SRCS += vhd-util-fill.c
LIB-SRCS += vhd-util-modify.c
LIB-SRCS += vhd-util-query.c
LIB-SRCS += vhd-util-read.c
LIB-SRCS += vhd-util-repair.c
LIB-SRCS += vhd-util-resize.c
LIB-SRCS += vhd-util-revert.c
LIB-SRCS += vhd-util-set-field.c
LIB-SRCS += vhd-util-snapshot.c
LIB-SRCS += vhd-util-scan.c
LIB-SRCS += vhd-util-check.c
LIB-SRCS += relative-path.c
LIB-SRCS += atomicio.c
LIB-OBJS = $(patsubst %.c,%.o,$(LIB-SRCS))
LIB-OBJS += $(LVM-UTIL-OBJ)
LIBVHD = libvhd.a libvhd.so.$(LIBVHD-MAJOR).$(LIBVHD-MINOR)
all: build
build: $(LIBVHD-BUILD)
libvhd.a: $(LIB-OBJS)
$(CC) $(CFLAGS) -Wl,$(SONAME_LDFLAG),$(LIBVHD-SONAME) $(SHLIB_CFLAGS) \
-o libvhd.so.$(LIBVHD-MAJOR).$(LIBVHD-MINOR) $(LIBS) $^
ln -sf libvhd.so.$(LIBVHD-MAJOR).$(LIBVHD-MINOR) libvhd.so.$(LIBVHD-MAJOR)
ln -sf libvhd.so.$(LIBVHD-MAJOR) libvhd.so
$(AR) rc $@ $^
install: all
$(INSTALL_DIR) -p $(DESTDIR)$(INST-DIR)
$(INSTALL_DATA) $(LIBVHD) $(DESTDIR)$(INST-DIR)
ln -sf libvhd.so.$(LIBVHD-MAJOR).$(LIBVHD-MINOR) $(DESTDIR)$(INST-DIR)/libvhd.so.$(LIBVHD-MAJOR)
ln -sf libvhd.so.$(LIBVHD-MAJOR) $(DESTDIR)$(INST-DIR)/libvhd.so
clean:
rm -rf *.a *.so* *.o *~ $(DEPS) $(LIBVHD)
.PHONY: all build clean install libvhd
-include $(DEPS)
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
* Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <errno.h>
#include "atomicio.h"
/*
* ensure all of data on socket comes through. f==read || f==vwrite
*/
size_t
atomicio(f, fd, _s, n)
ssize_t (*f) (int, void *, size_t);
int fd;
void *_s;
size_t n;
{
char *s = _s;
size_t pos = 0;
ssize_t res;
while (n > pos) {
res = (f) (fd, s + pos, n - pos);
switch (res) {
case -1:
if (errno == EINTR || errno == EAGAIN)
continue;
return 0;
case 0:
errno = EPIPE;
return pos;
default:
pos += (size_t)res;
}
}
return (pos);
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+299
View File
@@ -0,0 +1,299 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "relative-path.h"
#define sfree(ptr) \
do { \
free(ptr); \
ptr = NULL; \
} while (0)
/*
* count number of tokens between DELIMETER characters
*/
static int
count_nodes(char *path)
{
int i;
char *tmp;
if (!path)
return 0;
for (i = 0, tmp = path; *tmp != '\0'; tmp++)
if (*tmp == DELIMITER)
i++;
return i;
}
/*
* return copy of next node in @path, or NULL
* @path is moved to the end of the next node
* @err is set to -errno on failure
* copy should be freed
*/
static char *
next_node(char **path, int *err)
{
int ret;
char *tmp, *start;
if (!path || !*path) {
*err = -EINVAL;
return NULL;
}
*err = 0;
start = *path;
for (tmp = *path; *tmp != '\0'; tmp++)
if (*tmp == DELIMITER) {
int size;
char *node;
size = tmp - start + 1;
node = malloc(size);
if (!node) {
*err = -ENOMEM;
return NULL;
}
ret = snprintf(node, size, "%s", start);
if (ret < 0) {
free(node);
*err = -EINVAL;
return NULL;
}
*path = tmp;
return node;
}
return NULL;
}
/*
* count number of nodes in common betwee @to and @from
* returns number of common nodes, or -errno on failure
*/
static int
count_common_nodes(char *to, char *from)
{
int err, common;
char *to_node, *from_node;
if (!to || !from)
return -EINVAL;
err = 0;
common = 0;
to_node = NULL;
from_node = NULL;
do {
to_node = next_node(&to, &err);
if (err || !to_node)
break;
from_node = next_node(&from, &err);
if (err || !from_node)
break;
if (strncmp(to_node, from_node, MAX_NAME_LEN))
break;
++to;
++from;
++common;
sfree(to_node);
sfree(from_node);
} while (1);
sfree(to_node);
sfree(from_node);
if (err)
return err;
return common;
}
/*
* construct path of @count '../', './' if @count is zero, or NULL on error
* result should be freed
*/
static char *
up_nodes(int count)
{
char *path, *tmp;
int i, ret, len, size;
if (!count)
return strdup("./");
len = strlen("../");
size = len * count;
if (size >= MAX_NAME_LEN)
return NULL;
path = malloc(size + 1);
if (!path)
return NULL;
tmp = path;
for (i = 0; i < count; i++) {
ret = sprintf(tmp, "../");
if (ret < 0 || ret != len) {
free(path);
return NULL;
}
tmp += ret;
}
return path;
}
/*
* return pointer to @offset'th node of path or NULL on error
*/
static char *
node_offset(char *from, int offset)
{
char *path;
if (!from || !offset)
return NULL;
for (path = from; *path != '\0'; path++) {
if (*path == DELIMITER)
if (--offset == 0)
return path + 1;
}
return NULL;
}
/*
* return a relative path from @from to @to
* result should be freed
*/
char *
relative_path_to(char *from, char *to, int *err)
{
int from_nodes, common;
char *to_absolute, *from_absolute;
char *up, *common_target_path, *relative_path;
*err = 0;
up = NULL;
to_absolute = NULL;
from_absolute = NULL;
relative_path = NULL;
if (strnlen(to, MAX_NAME_LEN) == MAX_NAME_LEN ||
strnlen(from, MAX_NAME_LEN) == MAX_NAME_LEN) {
EPRINTF("invalid input; max path length is %d\n",
MAX_NAME_LEN);
*err = -ENAMETOOLONG;
return NULL;
}
to_absolute = realpath(to, NULL);
if (!to_absolute) {
EPRINTF("failed to get absolute path of %s\n", to);
*err = -errno;
goto out;
}
from_absolute = realpath(from, NULL);
if (!from_absolute) {
EPRINTF("failed to get absolute path of %s\n", from);
*err = -errno;
goto out;
}
if (strnlen(to_absolute, MAX_NAME_LEN) == MAX_NAME_LEN ||
strnlen(from_absolute, MAX_NAME_LEN) == MAX_NAME_LEN) {
EPRINTF("invalid input; max path length is %d\n",
MAX_NAME_LEN);
*err = -ENAMETOOLONG;
goto out;
}
/* count nodes in source path */
from_nodes = count_nodes(from_absolute);
/* count nodes in common */
common = count_common_nodes(to_absolute + 1, from_absolute + 1);
if (common < 0) {
EPRINTF("failed to count common nodes of %s and %s: %d\n",
to_absolute, from_absolute, common);
*err = common;
goto out;
}
/* move up to common node */
up = up_nodes(from_nodes - common - 1);
if (!up) {
EPRINTF("failed to allocate relative path for %s: %d\n",
from_absolute, -ENOMEM);
*err = -ENOMEM;
goto out;
}
/* get path from common node to target */
common_target_path = node_offset(to_absolute, common + 1);
if (!common_target_path) {
EPRINTF("failed to find common target path to %s: %d\n",
to_absolute, -EINVAL);
*err = -EINVAL;
goto out;
}
/* get relative path */
if (asprintf(&relative_path, "%s%s", up, common_target_path) == -1) {
EPRINTF("failed to construct final path %s%s: %d\n",
up, common_target_path, -ENOMEM);
relative_path = NULL;
*err = -ENOMEM;
goto out;
}
out:
sfree(up);
sfree(to_absolute);
sfree(from_absolute);
return relative_path;
}
+977
View File
@@ -0,0 +1,977 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <inttypes.h>
#include <sys/stat.h>
#include "libvhd.h"
#include "vhd-util.h"
// allow the VHD timestamp to be at most this many seconds into the future to
// account for time skew with NFS servers
#define TIMESTAMP_MAX_SLACK 1800
static int
vhd_util_check_zeros(void *buf, size_t size)
{
int i;
char *p;
p = buf;
for (i = 0; i < size; i++)
if (p[i])
return i;
return 0;
}
static int
vhd_util_check_footer_opened(vhd_footer_t *footer)
{
int i, n;
uint32_t *buf;
buf = (uint32_t *)footer;
n = sizeof(*footer) / sizeof(uint32_t);
for (i = 0; i < n; i++)
if (buf[i] != 0xc7c7c7c7)
return 0;
return 1;
}
static char *
vhd_util_check_validate_footer(vhd_footer_t *footer)
{
int size;
uint32_t checksum, now;
size = sizeof(footer->cookie);
if (memcmp(footer->cookie, HD_COOKIE, size))
return "invalid cookie";
checksum = vhd_checksum_footer(footer);
if (checksum != footer->checksum) {
if (footer->hidden &&
!strncmp(footer->crtr_app, "tap", 3) &&
(footer->crtr_ver == VHD_VERSION(0, 1) ||
footer->crtr_ver == VHD_VERSION(1, 1))) {
char tmp = footer->hidden;
footer->hidden = 0;
checksum = vhd_checksum_footer(footer);
footer->hidden = tmp;
if (checksum == footer->checksum)
goto ok;
}
return "invalid checksum";
}
ok:
if (!(footer->features & HD_RESERVED))
return "invalid 'reserved' feature";
if (footer->features & ~(HD_TEMPORARY | HD_RESERVED))
return "invalid extra features";
if (footer->ff_version != HD_FF_VERSION)
return "invalid file format version";
if (footer->type != HD_TYPE_DYNAMIC &&
footer->type != HD_TYPE_DIFF &&
footer->data_offset != ~(0ULL))
return "invalid data offset";
now = vhd_time(time(NULL));
if (footer->timestamp > now + TIMESTAMP_MAX_SLACK)
return "creation time in future";
if (!strncmp(footer->crtr_app, "tap", 3) &&
footer->crtr_ver > VHD_CURRENT_VERSION)
return "unsupported tap creator version";
if (vhd_chs(footer->curr_size) < footer->geometry)
return "geometry too large";
if (footer->type != HD_TYPE_FIXED &&
footer->type != HD_TYPE_DYNAMIC &&
footer->type != HD_TYPE_DIFF)
return "invalid type";
if (footer->saved && footer->saved != 1)
return "invalid 'saved' state";
if (footer->hidden && footer->hidden != 1)
return "invalid 'hidden' state";
if (vhd_util_check_zeros(footer->reserved,
sizeof(footer->reserved)))
return "invalid 'reserved' bits";
return NULL;
}
static char *
vhd_util_check_validate_header(int fd, vhd_header_t *header)
{
off64_t eof;
int i, cnt, size;
uint32_t checksum;
size = sizeof(header->cookie);
if (memcmp(header->cookie, DD_COOKIE, size))
return "invalid cookie";
checksum = vhd_checksum_header(header);
if (checksum != header->checksum)
return "invalid checksum";
if (header->hdr_ver != 0x00010000)
return "invalid header version";
if (header->data_offset != ~(0ULL))
return "invalid data offset";
eof = lseek64(fd, 0, SEEK_END);
if (eof == (off64_t)-1)
return "error finding eof";
if (header->table_offset <= 0 ||
header->table_offset % 512 ||
(header->table_offset +
(header->max_bat_size * sizeof(uint32_t)) >
eof - sizeof(vhd_footer_t)))
return "invalid table offset";
for (cnt = 0, i = 0; i < sizeof(header->block_size) * 8; i++)
if ((header->block_size >> i) & 1)
cnt++;
if (cnt != 1)
return "invalid block size";
if (header->res1)
return "invalid reserved bits";
if (vhd_util_check_zeros(header->res2, sizeof(header->res2)))
return "invalid reserved bits";
return NULL;
}
static char *
vhd_util_check_validate_differencing_header(vhd_context_t *vhd)
{
vhd_header_t *header;
header = &vhd->header;
if (vhd->footer.type == HD_TYPE_DIFF) {
char *parent;
uint32_t now;
now = vhd_time(time(NULL));
if (header->prt_ts > now + TIMESTAMP_MAX_SLACK)
return "parent creation time in future";
if (vhd_header_decode_parent(vhd, header, &parent))
return "invalid parent name";
free(parent);
} else {
if (vhd_util_check_zeros(header->prt_name,
sizeof(header->prt_name)))
return "invalid non-null parent name";
if (vhd_util_check_zeros(header->loc, sizeof(header->loc)))
return "invalid non-null parent locators";
if (!uuid_is_null(header->prt_uuid))
return "invalid non-null parent uuid";
if (header->prt_ts)
return "invalid non-zero parent timestamp";
}
return NULL;
}
static char *
vhd_util_check_validate_batmap(vhd_context_t *vhd, vhd_batmap_t *batmap)
{
int size;
off64_t eof;
uint32_t checksum;
size = sizeof(batmap->header.cookie);
if (memcmp(batmap->header.cookie, VHD_BATMAP_COOKIE, size))
return "invalid cookie";
if (batmap->header.batmap_version > VHD_BATMAP_CURRENT_VERSION)
return "unsupported batmap version";
checksum = vhd_checksum_batmap(batmap);
if (checksum != batmap->header.checksum)
return "invalid checksum";
if (!batmap->header.batmap_size)
return "invalid size zero";
eof = lseek64(vhd->fd, 0, SEEK_END);
if (eof == (off64_t)-1)
return "error finding eof";
if (!batmap->header.batmap_offset ||
batmap->header.batmap_offset % 512)
return "invalid batmap offset";
if ((batmap->header.batmap_offset +
vhd_sectors_to_bytes(batmap->header.batmap_size)) >
eof - sizeof(vhd_footer_t))
return "invalid batmap size";
return NULL;
}
static char *
vhd_util_check_validate_parent_locator(vhd_context_t *vhd,
vhd_parent_locator_t *loc)
{
off64_t eof;
if (vhd_validate_platform_code(loc->code))
return "invalid platform code";
if (loc->code == PLAT_CODE_NONE) {
if (vhd_util_check_zeros(loc, sizeof(*loc)))
return "non-zero locator";
return NULL;
}
if (!loc->data_offset)
return "invalid data offset";
if (!loc->data_space)
return "invalid data space";
if (!loc->data_len)
return "invalid data length";
eof = lseek64(vhd->fd, 0, SEEK_END);
if (eof == (off64_t)-1)
return "error finding eof";
if (loc->data_offset + vhd_parent_locator_size(loc) >
eof - sizeof(vhd_footer_t))
return "invalid size";
if (loc->res)
return "invalid reserved bits";
return NULL;
}
static char *
vhd_util_check_validate_parent(vhd_context_t *vhd, const char *ppath)
{
char *msg;
vhd_context_t parent;
msg = NULL;
if (vhd_parent_raw(vhd))
return msg;
if (vhd_open(&parent, ppath,
VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED))
return "error opening parent";
if (uuid_compare(vhd->header.prt_uuid, parent.footer.uuid)) {
msg = "invalid parent uuid";
goto out;
}
out:
vhd_close(&parent);
return msg;
}
static int
vhd_util_check_footer(int fd, vhd_footer_t *footer, int ignore)
{
size_t size;
int err, opened;
char *msg, *buf;
off64_t eof, off;
vhd_footer_t primary, backup;
memset(&primary, 0, sizeof(primary));
memset(&backup, 0, sizeof(backup));
err = posix_memalign((void **)&buf, VHD_SECTOR_SIZE, sizeof(primary));
if (err) {
printf("error allocating buffer: %d\n", err);
return -err;
}
memset(buf, 0, sizeof(primary));
eof = lseek64(fd, 0, SEEK_END);
if (eof == (off64_t)-1) {
err = -errno;
printf("error calculating end of file: %d\n", err);
goto out;
}
size = ((eof % 512) ? 511 : 512);
eof = lseek64(fd, eof - size, SEEK_SET);
if (eof == (off64_t)-1) {
err = -errno;
printf("error calculating end of file: %d\n", err);
goto out;
}
err = read(fd, buf, 512);
if (err != size) {
err = (errno ? -errno : -EIO);
printf("error reading primary footer: %d\n", err);
goto out;
}
memcpy(&primary, buf, sizeof(primary));
opened = vhd_util_check_footer_opened(&primary);
vhd_footer_in(&primary);
msg = vhd_util_check_validate_footer(&primary);
if (msg) {
if (opened && ignore)
goto check_backup;
err = -EINVAL;
printf("primary footer invalid: %s\n", msg);
goto out;
}
if (primary.type == HD_TYPE_FIXED) {
err = 0;
goto out;
}
check_backup:
off = lseek64(fd, 0, SEEK_SET);
if (off == (off64_t)-1) {
err = -errno;
printf("error seeking to backup footer: %d\n", err);
goto out;
}
size = 512;
memset(buf, 0, sizeof(primary));
err = read(fd, buf, size);
if (err != size) {
err = (errno ? -errno : -EIO);
printf("error reading backup footer: %d\n", err);
goto out;
}
memcpy(&backup, buf, sizeof(backup));
vhd_footer_in(&backup);
msg = vhd_util_check_validate_footer(&backup);
if (msg) {
err = -EINVAL;
printf("backup footer invalid: %s\n", msg);
goto out;
}
if (memcmp(&primary, &backup, sizeof(primary))) {
if (opened && ignore) {
memcpy(&primary, &backup, sizeof(primary));
goto ok;
}
if (backup.hidden &&
!strncmp(backup.crtr_app, "tap", 3) &&
(backup.crtr_ver == VHD_VERSION(0, 1) ||
backup.crtr_ver == VHD_VERSION(1, 1))) {
char cmp, tmp = backup.hidden;
backup.hidden = 0;
cmp = memcmp(&primary, &backup, sizeof(primary));
backup.hidden = tmp;
if (!cmp)
goto ok;
}
err = -EINVAL;
printf("primary and backup footers do not match\n");
goto out;
}
ok:
err = 0;
memcpy(footer, &primary, sizeof(primary));
out:
free(buf);
return err;
}
static int
vhd_util_check_header(int fd, vhd_footer_t *footer)
{
int err;
off64_t off;
char *msg, *buf;
vhd_header_t header;
err = posix_memalign((void **)&buf, VHD_SECTOR_SIZE, sizeof(header));
if (err) {
printf("error allocating header: %d\n", err);
return err;
}
off = footer->data_offset;
off = lseek64(fd, off, SEEK_SET);
if (off == (off64_t)-1) {
err = -errno;
printf("error seeking to header: %d\n", err);
goto out;
}
err = read(fd, buf, sizeof(header));
if (err != sizeof(header)) {
err = (errno ? -errno : -EIO);
printf("error reading header: %d\n", err);
goto out;
}
memcpy(&header, buf, sizeof(header));
vhd_header_in(&header);
msg = vhd_util_check_validate_header(fd, &header);
if (msg) {
err = -EINVAL;
printf("header is invalid: %s\n", msg);
goto out;
}
err = 0;
out:
free(buf);
return err;
}
static int
vhd_util_check_differencing_header(vhd_context_t *vhd)
{
char *msg;
msg = vhd_util_check_validate_differencing_header(vhd);
if (msg) {
printf("differencing header is invalid: %s\n", msg);
return -EINVAL;
}
return 0;
}
static int
vhd_util_check_bat(vhd_context_t *vhd)
{
off64_t eof, eoh;
int i, j, err, block_size;
err = vhd_seek(vhd, 0, SEEK_END);
if (err) {
printf("error calculating eof: %d\n", err);
return err;
}
eof = vhd_position(vhd);
if (eof == (off64_t)-1) {
printf("error calculating eof: %d\n", -errno);
return -errno;
}
/* adjust eof for vhds with short footers */
if (eof % 512) {
if (eof % 512 != 511) {
printf("invalid file size: 0x%"PRIx64"\n", eof);
return -EINVAL;
}
eof++;
}
err = vhd_get_bat(vhd);
if (err) {
printf("error reading bat: %d\n", err);
return err;
}
err = vhd_end_of_headers(vhd, &eoh);
if (err) {
printf("error calculating end of metadata: %d\n", err);
return err;
}
eof -= sizeof(vhd_footer_t);
eof >>= VHD_SECTOR_SHIFT;
eoh >>= VHD_SECTOR_SHIFT;
block_size = vhd->spb + vhd->bm_secs;
for (i = 0; i < vhd->header.max_bat_size; i++) {
uint32_t off = vhd->bat.bat[i];
if (off == DD_BLK_UNUSED)
continue;
if (off < eoh) {
printf("block %d (offset 0x%x) clobbers headers\n",
i, off);
return -EINVAL;
}
if (off + block_size > eof) {
printf("block %d (offset 0x%x) clobbers footer\n",
i, off);
return -EINVAL;
}
for (j = 0; j < vhd->header.max_bat_size; j++) {
uint32_t joff = vhd->bat.bat[j];
if (i == j)
continue;
if (joff == DD_BLK_UNUSED)
continue;
if (off == joff)
err = -EINVAL;
if (off > joff && off < joff + block_size)
err = -EINVAL;
if (off + block_size > joff &&
off + block_size < joff + block_size)
err = -EINVAL;
if (err) {
printf("block %d (offset 0x%x) clobbers "
"block %d (offset 0x%x)\n",
i, off, j, joff);
return err;
}
}
}
return 0;
}
static int
vhd_util_check_batmap(vhd_context_t *vhd)
{
char *msg;
int i, err;
err = vhd_get_bat(vhd);
if (err) {
printf("error reading bat: %d\n", err);
return err;
}
err = vhd_get_batmap(vhd);
if (err) {
printf("error reading batmap: %d\n", err);
return err;
}
msg = vhd_util_check_validate_batmap(vhd, &vhd->batmap);
if (msg) {
printf("batmap is invalid: %s\n", msg);
return -EINVAL;
}
for (i = 0; i < vhd->header.max_bat_size; i++) {
if (!vhd_batmap_test(vhd, &vhd->batmap, i))
continue;
if (vhd->bat.bat[i] == DD_BLK_UNUSED) {
printf("batmap shows unallocated block %d full\n", i);
return -EINVAL;
}
}
return 0;
}
static int
vhd_util_check_parent_locators(vhd_context_t *vhd)
{
int i, n, err;
vhd_parent_locator_t *loc;
char *msg, *file, *ppath, *location, *pname;
int mac, macx, w2ku, w2ru, wi2r, wi2k, found;
mac = 0;
macx = 0;
w2ku = 0;
w2ru = 0;
wi2r = 0;
wi2k = 0;
found = 0;
pname = NULL;
ppath = NULL;
location = NULL;
err = vhd_header_decode_parent(vhd, &vhd->header, &pname);
if (err) {
printf("error decoding parent name: %d\n", err);
return err;
}
n = sizeof(vhd->header.loc) / sizeof(vhd->header.loc[0]);
for (i = 0; i < n; i++) {
ppath = NULL;
location = NULL;
loc = vhd->header.loc + i;
msg = vhd_util_check_validate_parent_locator(vhd, loc);
if (msg) {
err = -EINVAL;
printf("invalid parent locator %d: %s\n", i, msg);
goto out;
}
if (loc->code == PLAT_CODE_NONE)
continue;
switch (loc->code) {
case PLAT_CODE_MACX:
if (macx++)
goto dup;
break;
case PLAT_CODE_MAC:
if (mac++)
goto dup;
break;
case PLAT_CODE_W2KU:
if (w2ku++)
goto dup;
break;
case PLAT_CODE_W2RU:
if (w2ru++)
goto dup;
break;
case PLAT_CODE_WI2R:
if (wi2r++)
goto dup;
break;
case PLAT_CODE_WI2K:
if (wi2k++)
goto dup;
break;
default:
err = -EINVAL;
printf("invalid platform code for locator %d\n", i);
goto out;
}
if (loc->code != PLAT_CODE_MACX &&
loc->code != PLAT_CODE_W2RU &&
loc->code != PLAT_CODE_W2KU)
continue;
err = vhd_parent_locator_read(vhd, loc, &ppath);
if (err) {
printf("error reading parent locator %d: %d\n", i, err);
goto out;
}
file = basename(ppath);
if (strcmp(pname, file)) {
err = -EINVAL;
printf("parent locator %d name (%s) does not match "
"header name (%s)\n", i, file, pname);
goto out;
}
err = vhd_find_parent(vhd, ppath, &location);
if (err) {
printf("error resolving %s: %d\n", ppath, err);
goto out;
}
err = access(location, R_OK);
if (err && loc->code == PLAT_CODE_MACX) {
err = -errno;
printf("parent locator %d points to missing file %s "
"(resolved to %s)\n", i, ppath, location);
goto out;
}
msg = vhd_util_check_validate_parent(vhd, location);
if (msg) {
err = -EINVAL;
printf("invalid parent %s: %s\n", location, msg);
goto out;
}
found++;
free(ppath);
free(location);
ppath = NULL;
location = NULL;
continue;
dup:
printf("duplicate platform code in locator %d: 0x%x\n",
i, loc->code);
err = -EINVAL;
goto out;
}
if (!found) {
err = -EINVAL;
printf("could not find parent %s\n", pname);
goto out;
}
err = 0;
out:
free(pname);
free(ppath);
free(location);
return err;
}
static void
vhd_util_dump_headers(const char *name)
{
char *argv[] = { "read", "-p", "-n", (char *)name };
int argc = sizeof(argv) / sizeof(argv[0]);
printf("%s appears invalid; dumping metadata\n", name);
vhd_util_read(argc, argv);
}
static int
vhd_util_check_vhd(const char *name, int ignore)
{
int fd, err;
vhd_context_t vhd;
struct stat stats;
vhd_footer_t footer;
fd = -1;
memset(&vhd, 0, sizeof(vhd));
err = stat(name, &stats);
if (err == -1) {
printf("cannot stat %s: %d\n", name, errno);
return -errno;
}
if (!S_ISREG(stats.st_mode) && !S_ISBLK(stats.st_mode)) {
printf("%s is not a regular file or block device\n", name);
return -EINVAL;
}
fd = open(name, O_RDONLY | O_DIRECT | O_LARGEFILE);
if (fd == -1) {
printf("error opening %s\n", name);
return -errno;
}
err = vhd_util_check_footer(fd, &footer, ignore);
if (err)
goto out;
if (footer.type != HD_TYPE_DYNAMIC && footer.type != HD_TYPE_DIFF)
goto out;
err = vhd_util_check_header(fd, &footer);
if (err)
goto out;
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED);
if (err)
goto out;
err = vhd_util_check_differencing_header(&vhd);
if (err)
goto out;
err = vhd_util_check_bat(&vhd);
if (err)
goto out;
if (vhd_has_batmap(&vhd)) {
err = vhd_util_check_batmap(&vhd);
if (err)
goto out;
}
if (vhd.footer.type == HD_TYPE_DIFF) {
err = vhd_util_check_parent_locators(&vhd);
if (err)
goto out;
}
err = 0;
printf("%s is valid\n", name);
out:
if (err)
vhd_util_dump_headers(name);
if (fd != -1)
close(fd);
vhd_close(&vhd);
return err;
}
static int
vhd_util_check_parents(const char *name, int ignore)
{
int err;
vhd_context_t vhd;
char *cur, *parent;
cur = (char *)name;
for (;;) {
err = vhd_open(&vhd, cur,
VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED);
if (err)
goto out;
if (vhd.footer.type != HD_TYPE_DIFF || vhd_parent_raw(&vhd)) {
vhd_close(&vhd);
goto out;
}
err = vhd_parent_locator_get(&vhd, &parent);
vhd_close(&vhd);
if (err) {
printf("error getting parent: %d\n", err);
goto out;
}
if (cur != name)
free(cur);
cur = parent;
err = vhd_util_check_vhd(cur, ignore);
if (err)
goto out;
}
out:
if (err)
printf("error checking parents: %d\n", err);
if (cur != name)
free(cur);
return err;
}
int
vhd_util_check(int argc, char **argv)
{
char *name;
vhd_context_t vhd;
int c, err, ignore, parents;
if (!argc || !argv) {
err = -EINVAL;
goto usage;
}
ignore = 0;
parents = 0;
name = NULL;
optind = 0;
while ((c = getopt(argc, argv, "n:iph")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 'i':
ignore = 1;
break;
case 'p':
parents = 1;
break;
case 'h':
err = 0;
goto usage;
default:
err = -EINVAL;
goto usage;
}
}
if (!name || optind != argc) {
err = -EINVAL;
goto usage;
}
err = vhd_util_check_vhd(name, ignore);
if (err)
goto out;
if (parents)
err = vhd_util_check_parents(name, ignore);
out:
return err;
usage:
printf("options: -n <file> [-i ignore missing primary footers] "
"[-p check parents] [-h help]\n");
return err;
}
+218
View File
@@ -0,0 +1,218 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "libvhd.h"
static int
__raw_io_write(int fd, char* buf, uint64_t sec, uint32_t secs)
{
off64_t off;
size_t ret;
errno = 0;
off = lseek64(fd, vhd_sectors_to_bytes(sec), SEEK_SET);
if (off == (off64_t)-1) {
printf("raw parent: seek(0x%08"PRIx64") failed: %d\n",
vhd_sectors_to_bytes(sec), -errno);
return -errno;
}
ret = write(fd, buf, vhd_sectors_to_bytes(secs));
if (ret == vhd_sectors_to_bytes(secs))
return 0;
printf("raw parent: write of 0x%"PRIx64" returned %zd, errno: %d\n",
vhd_sectors_to_bytes(secs), ret, -errno);
return (errno ? -errno : -EIO);
}
/*
* Use 'parent' if the parent is VHD, and 'parent_fd' if the parent is raw
*/
static int
vhd_util_coalesce_block(vhd_context_t *vhd, vhd_context_t *parent,
int parent_fd, uint64_t block)
{
int i, err;
char *buf, *map;
uint64_t sec, secs;
buf = NULL;
map = NULL;
sec = block * vhd->spb;
if (vhd->bat.bat[block] == DD_BLK_UNUSED)
return 0;
err = posix_memalign((void **)&buf, 4096, vhd->header.block_size);
if (err)
return -err;
err = vhd_io_read(vhd, buf, sec, vhd->spb);
if (err)
goto done;
if (vhd_has_batmap(vhd) && vhd_batmap_test(vhd, &vhd->batmap, block)) {
if (parent->file)
err = vhd_io_write(parent, buf, sec, vhd->spb);
else
err = __raw_io_write(parent_fd, buf, sec, vhd->spb);
goto done;
}
err = vhd_read_bitmap(vhd, block, &map);
if (err)
goto done;
for (i = 0; i < vhd->spb; i++) {
if (!vhd_bitmap_test(vhd, map, i))
continue;
for (secs = 0; i + secs < vhd->spb; secs++)
if (!vhd_bitmap_test(vhd, map, i + secs))
break;
if (parent->file)
err = vhd_io_write(parent,
buf + vhd_sectors_to_bytes(i),
sec + i, secs);
else
err = __raw_io_write(parent_fd,
buf + vhd_sectors_to_bytes(i),
sec + i, secs);
if (err)
goto done;
i += secs;
}
err = 0;
done:
free(buf);
free(map);
return err;
}
int
vhd_util_coalesce(int argc, char **argv)
{
int err, c;
uint64_t i;
char *name, *pname;
vhd_context_t vhd, parent;
int parent_fd = -1;
name = NULL;
pname = NULL;
parent.file = NULL;
if (!argc || !argv)
goto usage;
optind = 0;
while ((c = getopt(argc, argv, "n:h")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 'h':
default:
goto usage;
}
}
if (!name || optind != argc)
goto usage;
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY);
if (err) {
printf("error opening %s: %d\n", name, err);
return err;
}
err = vhd_parent_locator_get(&vhd, &pname);
if (err) {
printf("error finding %s parent: %d\n", name, err);
vhd_close(&vhd);
return err;
}
if (vhd_parent_raw(&vhd)) {
parent_fd = open(pname, O_RDWR | O_DIRECT | O_LARGEFILE, 0644);
if (parent_fd == -1) {
err = -errno;
printf("failed to open parent %s: %d\n", pname, err);
vhd_close(&vhd);
return err;
}
} else {
err = vhd_open(&parent, pname, VHD_OPEN_RDWR);
if (err) {
printf("error opening %s: %d\n", pname, err);
free(pname);
vhd_close(&vhd);
return err;
}
}
err = vhd_get_bat(&vhd);
if (err)
goto done;
if (vhd_has_batmap(&vhd)) {
err = vhd_get_batmap(&vhd);
if (err)
goto done;
}
for (i = 0; i < vhd.bat.entries; i++) {
err = vhd_util_coalesce_block(&vhd, &parent, parent_fd, i);
if (err)
goto done;
}
err = 0;
done:
free(pname);
vhd_close(&vhd);
if (parent.file)
vhd_close(&parent);
else
close(parent_fd);
return err;
usage:
printf("options: <-n name> [-h help]\n");
return -EINVAL;
}
+80
View File
@@ -0,0 +1,80 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "libvhd.h"
int
vhd_util_create(int argc, char **argv)
{
char *name;
uint64_t size;
int c, sparse, err;
vhd_flag_creat_t flags;
err = -EINVAL;
size = 0;
sparse = 1;
name = NULL;
flags = 0;
if (!argc || !argv)
goto usage;
optind = 0;
while ((c = getopt(argc, argv, "n:s:rh")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 's':
err = 0;
size = strtoull(optarg, NULL, 10);
break;
case 'r':
sparse = 0;
break;
case 'h':
default:
goto usage;
}
}
if (err || !name || optind != argc)
goto usage;
return vhd_create(name, size << 20,
(sparse ? HD_TYPE_DYNAMIC : HD_TYPE_FIXED),
flags);
usage:
printf("options: <-n name> <-s size (MB)> [-r reserve] [-h help]\n");
return -EINVAL;
}
+105
View File
@@ -0,0 +1,105 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "libvhd.h"
int
vhd_util_fill(int argc, char **argv)
{
int err, c;
char *buf, *name;
vhd_context_t vhd;
uint64_t i, sec, secs;
buf = NULL;
name = NULL;
if (!argc || !argv)
goto usage;
optind = 0;
while ((c = getopt(argc, argv, "n:h")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 'h':
default:
goto usage;
}
}
if (!name || optind != argc)
goto usage;
err = vhd_open(&vhd, name, VHD_OPEN_RDWR);
if (err) {
printf("error opening %s: %d\n", name, err);
return err;
}
err = vhd_get_bat(&vhd);
if (err)
goto done;
err = posix_memalign((void **)&buf, 4096, vhd.header.block_size);
if (err) {
err = -err;
goto done;
}
sec = 0;
secs = vhd.header.block_size >> VHD_SECTOR_SHIFT;
for (i = 0; i < vhd.header.max_bat_size; i++) {
err = vhd_io_read(&vhd, buf, sec, secs);
if (err)
goto done;
err = vhd_io_write(&vhd, buf, sec, secs);
if (err)
goto done;
sec += secs;
}
err = 0;
done:
free(buf);
vhd_close(&vhd);
return err;
usage:
printf("options: <-n name> [-h help]\n");
return -EINVAL;
}
+132
View File
@@ -0,0 +1,132 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Altering operations:
*
* 1. Change the parent pointer to another file.
* 2. Change the size of the file containing the VHD image. This does NOT
* affect the VHD disk capacity, only the physical size of the file containing
* the VHD. Naturally, it is not possible to set the file size to be less than
* the what VHD utilizes.
* The operation doesn't actually change the file size, but it writes the
* footer in the right location such that resizing the file (manually, as a
* separate step) will produce the correct results. If the new file size is
* greater than the current file size, the file must first be expanded and then
* altered with this operation. If the new size is smaller than the current
* size, the VHD must first be altered with this operation and then the file
* must be shrunk. Failing to resize the file will result in a corrupted VHD.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "libvhd.h"
TEST_FAIL_EXTERN_VARS;
int
vhd_util_modify(int argc, char **argv)
{
char *name;
vhd_context_t vhd;
int err, c, size, parent, parent_raw;
off64_t newsize = 0;
char *newparent = NULL;
name = NULL;
size = 0;
parent = 0;
parent_raw = 0;
optind = 0;
while ((c = getopt(argc, argv, "n:s:p:mh")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 's':
size = 1;
errno = 0;
newsize = strtoll(optarg, NULL, 10);
if (errno) {
fprintf(stderr, "Invalid size '%s'\n", optarg);
goto usage;
}
break;
case 'p':
parent = 1;
newparent = optarg;
break;
case 'm':
parent_raw = 1;
break;
case 'h':
default:
goto usage;
}
}
if (!name || optind != argc)
goto usage;
err = vhd_open(&vhd, name, VHD_OPEN_RDWR);
if (err) {
printf("error opening %s: %d\n", name, err);
return err;
}
if (size) {
err = vhd_set_phys_size(&vhd, newsize);
if (err)
printf("failed to set physical size to %"PRIu64":"
" %d\n", newsize, err);
}
if (parent) {
TEST_FAIL_AT(FAIL_REPARENT_BEGIN);
err = vhd_change_parent(&vhd, newparent, parent_raw);
if (err) {
printf("failed to set parent to '%s': %d\n",
newparent, err);
goto done;
}
TEST_FAIL_AT(FAIL_REPARENT_END);
}
done:
vhd_close(&vhd);
return err;
usage:
printf("*** Dangerous operations, use with care ***\n");
printf("options: <-n name> [-p NEW_PARENT set parent [-m raw]] "
"[-s NEW_SIZE set size] [-h help]\n");
return -EINVAL;
}
+159
View File
@@ -0,0 +1,159 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "libvhd.h"
int
vhd_util_query(int argc, char **argv)
{
char *name;
vhd_context_t vhd;
off64_t currsize;
int ret, err, c, size, physize, parent, fields, depth;
name = NULL;
size = 0;
physize = 0;
parent = 0;
fields = 0;
depth = 0;
if (!argc || !argv) {
err = -EINVAL;
goto usage;
}
optind = 0;
while ((c = getopt(argc, argv, "n:vspfdh")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 'v':
size = 1;
break;
case 's':
physize = 1;
break;
case 'p':
parent = 1;
break;
case 'f':
fields = 1;
break;
case 'd':
depth = 1;
break;
case 'h':
err = 0;
goto usage;
default:
err = -EINVAL;
goto usage;
}
}
if (!name || optind != argc) {
err = -EINVAL;
goto usage;
}
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED);
if (err) {
printf("error opening %s: %d\n", name, err);
return err;
}
if (size)
printf("%"PRIu64"\n", vhd.footer.curr_size >> 20);
if (physize) {
err = vhd_get_phys_size(&vhd, &currsize);
if (err)
printf("failed to get physical size: %d\n", err);
else
printf("%"PRIu64"\n", currsize);
}
if (parent) {
ret = 0;
if (vhd.footer.type != HD_TYPE_DIFF)
printf("%s has no parent\n", name);
else {
char *pname;
ret = vhd_parent_locator_get(&vhd, &pname);
if (ret)
printf("query failed\n");
else {
printf("%s\n", pname);
free(pname);
}
}
err = (err ? : ret);
}
if (fields) {
int hidden;
ret = vhd_hidden(&vhd, &hidden);
if (ret)
printf("error checking 'hidden' field: %d\n", ret);
else
printf("hidden: %d\n", hidden);
err = (err ? : ret);
}
if (depth) {
int length;
ret = vhd_chain_depth(&vhd, &length);
if (ret)
printf("error checking chain depth: %d\n", ret);
else
printf("chain depth: %d\n", length);
err = (err ? : ret);
}
vhd_close(&vhd);
return err;
usage:
printf("options: <-n name> [-v print virtual size (in MB)] "
"[-s print physical utilization (bytes)] [-p print parent] "
"[-f print fields] [-d print chain depth] [-h help]\n");
return err;
}
+742
View File
@@ -0,0 +1,742 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include "libvhd.h"
#include "vhd-util.h"
#define nsize 15
static char nbuf[nsize];
static inline char *
__xconv(uint64_t num)
{
snprintf(nbuf, nsize, "%#" PRIx64 , num);
return nbuf;
}
static inline char *
__dconv(uint64_t num)
{
snprintf(nbuf, nsize, "%" PRIu64, num);
return nbuf;
}
#define conv(hex, num) \
(hex ? __xconv((uint64_t)num) : __dconv((uint64_t)num))
static void
vhd_print_header(vhd_context_t *vhd, vhd_header_t *h, int hex)
{
int err;
uint32_t cksm;
char uuid[37], time_str[26], cookie[9], out[512], *name;
printf("VHD Header Summary:\n-------------------\n");
snprintf(cookie, 9, "%s", h->cookie);
printf("Cookie : %s\n", cookie);
printf("Data offset (unusd) : %s\n", conv(hex, h->data_offset));
printf("Table offset : %s\n", conv(hex, h->table_offset));
printf("Header version : 0x%08x\n", h->hdr_ver);
printf("Max BAT size : %s\n", conv(hex, h->max_bat_size));
printf("Block size : %s ", conv(hex, h->block_size));
printf("(%s MB)\n", conv(hex, h->block_size >> 20));
err = vhd_header_decode_parent(vhd, h, &name);
printf("Parent name : %s\n",
(err ? "failed to read name" : name));
free(name);
uuid_unparse(h->prt_uuid, uuid);
printf("Parent UUID : %s\n", uuid);
vhd_time_to_string(h->prt_ts, time_str);
printf("Parent timestamp : %s\n", time_str);
cksm = vhd_checksum_header(h);
printf("Checksum : 0x%x|0x%x (%s)\n", h->checksum, cksm,
h->checksum == cksm ? "Good!" : "Bad!");
printf("\n");
}
static void
vhd_print_footer(vhd_footer_t *f, int hex)
{
uint64_t c, h, s;
uint32_t ff_maj, ff_min, cr_maj, cr_min, cksm, cksm_save;
char time_str[26], creator[5], uuid[37], cookie[9];
printf("VHD Footer Summary:\n-------------------\n");
snprintf(cookie, 9, "%s", f->cookie);
printf("Cookie : %s\n", cookie);
printf("Features : (0x%08x) %s%s\n", f->features,
(f->features & HD_TEMPORARY) ? "<TEMP>" : "",
(f->features & HD_RESERVED) ? "<RESV>" : "");
ff_maj = f->ff_version >> 16;
ff_min = f->ff_version & 0xffff;
printf("File format version : Major: %d, Minor: %d\n",
ff_maj, ff_min);
printf("Data offset : %s\n", conv(hex, f->data_offset));
vhd_time_to_string(f->timestamp, time_str);
printf("Timestamp : %s\n", time_str);
memcpy(creator, f->crtr_app, 4);
creator[4] = '\0';
printf("Creator Application : '%s'\n", creator);
cr_maj = f->crtr_ver >> 16;
cr_min = f->crtr_ver & 0xffff;
printf("Creator version : Major: %d, Minor: %d\n",
cr_maj, cr_min);
printf("Creator OS : %s\n",
((f->crtr_os == HD_CR_OS_WINDOWS) ? "Windows" :
((f->crtr_os == HD_CR_OS_MACINTOSH) ? "Macintosh" :
"Unknown!")));
printf("Original disk size : %s MB ", conv(hex, f->orig_size >> 20));
printf("(%s Bytes)\n", conv(hex, f->orig_size));
printf("Current disk size : %s MB ", conv(hex, f->curr_size >> 20));
printf("(%s Bytes)\n", conv(hex, f->curr_size));
c = f->geometry >> 16;
h = (f->geometry & 0x0000FF00) >> 8;
s = f->geometry & 0x000000FF;
printf("Geometry : Cyl: %s, ", conv(hex, c));
printf("Hds: %s, ", conv(hex, h));
printf("Sctrs: %s\n", conv(hex, s));
printf(" : = %s MB ", conv(hex, (c * h * s) >> 11));
printf("(%s Bytes)\n", conv(hex, c * h * s << 9));
printf("Disk type : %s\n",
f->type <= HD_TYPE_MAX ?
HD_TYPE_STR[f->type] : "Unknown type!\n");
cksm = vhd_checksum_footer(f);
printf("Checksum : 0x%x|0x%x (%s)\n", f->checksum, cksm,
f->checksum == cksm ? "Good!" : "Bad!");
uuid_unparse(f->uuid, uuid);
printf("UUID : %s\n", uuid);
printf("Saved state : %s\n", f->saved == 0 ? "No" : "Yes");
printf("Hidden : %d\n", f->hidden);
printf("\n");
}
static inline char *
code_name(uint32_t code)
{
switch(code) {
case PLAT_CODE_NONE:
return "PLAT_CODE_NONE";
case PLAT_CODE_WI2R:
return "PLAT_CODE_WI2R";
case PLAT_CODE_WI2K:
return "PLAT_CODE_WI2K";
case PLAT_CODE_W2RU:
return "PLAT_CODE_W2RU";
case PLAT_CODE_W2KU:
return "PLAT_CODE_W2KU";
case PLAT_CODE_MAC:
return "PLAT_CODE_MAC";
case PLAT_CODE_MACX:
return "PLAT_CODE_MACX";
default:
return "UNKOWN";
}
}
static void
vhd_print_parent(vhd_context_t *vhd, vhd_parent_locator_t *loc)
{
int err;
char *buf;
err = vhd_parent_locator_read(vhd, loc, &buf);
if (err) {
printf("failed to read parent name\n");
return;
}
printf(" decoded name : %s\n", buf);
}
static void
vhd_print_parent_locators(vhd_context_t *vhd, int hex)
{
int i, n;
vhd_parent_locator_t *loc;
printf("VHD Parent Locators:\n--------------------\n");
n = sizeof(vhd->header.loc) / sizeof(struct prt_loc);
for (i = 0; i < n; i++) {
loc = &vhd->header.loc[i];
if (loc->code == PLAT_CODE_NONE)
continue;
printf("locator: : %d\n", i);
printf(" code : %s\n",
code_name(loc->code));
printf(" data_space : %s\n",
conv(hex, loc->data_space));
printf(" data_length : %s\n",
conv(hex, loc->data_len));
printf(" data_offset : %s\n",
conv(hex, loc->data_offset));
vhd_print_parent(vhd, loc);
printf("\n");
}
}
static void
vhd_print_batmap_header(vhd_batmap_t *batmap, int hex)
{
uint32_t cksm;
printf("VHD Batmap Summary:\n-------------------\n");
printf("Batmap offset : %s\n",
conv(hex, batmap->header.batmap_offset));
printf("Batmap size (secs) : %s\n",
conv(hex, batmap->header.batmap_size));
printf("Batmap version : 0x%08x\n",
batmap->header.batmap_version);
cksm = vhd_checksum_batmap(batmap);
printf("Checksum : 0x%x|0x%x (%s)\n",
batmap->header.checksum, cksm,
(batmap->header.checksum == cksm ? "Good!" : "Bad!"));
printf("\n");
}
static inline int
check_block_range(vhd_context_t *vhd, uint64_t block, int hex)
{
if (block > vhd->header.max_bat_size) {
fprintf(stderr, "block %s past end of file\n",
conv(hex, block));
return -ERANGE;
}
return 0;
}
static int
vhd_print_headers(vhd_context_t *vhd, int hex)
{
int err;
vhd_print_footer(&vhd->footer, hex);
if (vhd_type_dynamic(vhd)) {
vhd_print_header(vhd, &vhd->header, hex);
if (vhd->footer.type == HD_TYPE_DIFF)
vhd_print_parent_locators(vhd, hex);
if (vhd_has_batmap(vhd)) {
err = vhd_get_batmap(vhd);
if (err) {
printf("failed to get batmap header\n");
return err;
}
vhd_print_batmap_header(&vhd->batmap, hex);
}
}
return 0;
}
static int
vhd_dump_headers(const char *name, int hex)
{
vhd_context_t vhd;
libvhd_set_log_level(1);
memset(&vhd, 0, sizeof(vhd));
printf("\n%s appears invalid; dumping headers\n\n", name);
vhd.fd = open(name, O_DIRECT | O_LARGEFILE | O_RDONLY);
if (vhd.fd == -1)
return -errno;
vhd.file = strdup(name);
vhd_read_footer(&vhd, &vhd.footer);
vhd_read_header(&vhd, &vhd.header);
vhd_print_footer(&vhd.footer, hex);
vhd_print_header(&vhd, &vhd.header, hex);
close(vhd.fd);
free(vhd.file);
return 0;
}
static int
vhd_print_logical_to_physical(vhd_context_t *vhd,
uint64_t sector, int count, int hex)
{
int i;
uint32_t blk, lsec;
uint64_t cur, offset;
if (vhd_sectors_to_bytes(sector + count) > vhd->footer.curr_size) {
fprintf(stderr, "sector %s past end of file\n",
conv(hex, sector + count));
return -ERANGE;
}
for (i = 0; i < count; i++) {
cur = sector + i;
blk = cur / vhd->spb;
lsec = cur % vhd->spb;
offset = vhd->bat.bat[blk];
if (offset != DD_BLK_UNUSED) {
offset += lsec + 1;
offset = vhd_sectors_to_bytes(offset);
}
printf("logical sector %s: ", conv(hex, cur));
printf("block number: %s, ", conv(hex, blk));
printf("sector offset: %s, ", conv(hex, lsec));
printf("file offset: %s\n", (offset == DD_BLK_UNUSED ?
"not allocated" : conv(hex, offset)));
}
return 0;
}
static int
vhd_print_bat(vhd_context_t *vhd, uint64_t block, int count, int hex)
{
int i;
uint64_t cur, offset;
if (check_block_range(vhd, block + count, hex))
return -ERANGE;
for (i = 0; i < count; i++) {
cur = block + i;
offset = vhd->bat.bat[cur];
printf("block: %s: ", conv(hex, cur));
printf("offset: %s\n",
(offset == DD_BLK_UNUSED ? "not allocated" :
conv(hex, vhd_sectors_to_bytes(offset))));
}
return 0;
}
static inline void
write_full(int fd, void* buf, size_t count)
{
ssize_t num_written = 0;
if (!buf) return;
while(count > 0) {
num_written = write(fd, buf, count);
if (num_written == -1) {
if (errno == EINTR)
continue;
else
return;
}
count -= num_written;
buf += num_written;
}
}
static int
vhd_print_bitmap(vhd_context_t *vhd, uint64_t block, int count, int hex)
{
char *buf;
int i, err;
uint64_t cur;
if (check_block_range(vhd, block + count, hex))
return -ERANGE;
for (i = 0; i < count; i++) {
cur = block + i;
if (vhd->bat.bat[cur] == DD_BLK_UNUSED) {
printf("block %s not allocated\n", conv(hex, cur));
continue;
}
err = vhd_read_bitmap(vhd, cur, &buf);
if (err)
goto out;
write_full(STDOUT_FILENO, buf,
vhd_sectors_to_bytes(vhd->bm_secs));
free(buf);
}
err = 0;
out:
return err;
}
static int
vhd_test_bitmap(vhd_context_t *vhd, uint64_t sector, int count, int hex)
{
char *buf;
uint64_t cur;
int i, err, bit;
uint32_t blk, bm_blk, sec;
if (vhd_sectors_to_bytes(sector + count) > vhd->footer.curr_size) {
printf("sector %s past end of file\n", conv(hex, sector));
return -ERANGE;
}
bm_blk = -1;
buf = NULL;
for (i = 0; i < count; i++) {
cur = sector + i;
blk = cur / vhd->spb;
sec = cur % vhd->spb;
if (blk != bm_blk) {
bm_blk = blk;
free(buf);
buf = NULL;
if (vhd->bat.bat[blk] != DD_BLK_UNUSED) {
err = vhd_read_bitmap(vhd, blk, &buf);
if (err)
goto out;
}
}
if (vhd->bat.bat[blk] == DD_BLK_UNUSED)
bit = 0;
else
bit = vhd_bitmap_test(vhd, buf, blk);
print:
printf("block %s: ", conv(hex, blk));
printf("sec: %s: %d\n", conv(hex, sec), bit);
}
err = 0;
out:
free(buf);
return err;
}
static int
vhd_print_batmap(vhd_context_t *vhd)
{
int err;
size_t size;
err = vhd_get_batmap(vhd);
if (err) {
printf("failed to read batmap: %d\n", err);
return err;
}
size = vhd_sectors_to_bytes(vhd->batmap.header.batmap_size);
write_full(STDOUT_FILENO, vhd->batmap.map, size);
return 0;
}
static int
vhd_test_batmap(vhd_context_t *vhd, uint64_t block, int count, int hex)
{
int i, err;
uint64_t cur;
if (check_block_range(vhd, block + count, hex))
return -ERANGE;
err = vhd_get_batmap(vhd);
if (err) {
fprintf(stderr, "failed to get batmap\n");
return err;
}
for (i = 0; i < count; i++) {
cur = block + i;
fprintf(stderr, "batmap for block %s: %d\n", conv(hex, cur),
vhd_batmap_test(vhd, &vhd->batmap, cur));
}
return 0;
}
static int
vhd_print_data(vhd_context_t *vhd, uint64_t block, int count, int hex)
{
char *buf;
int i, err;
uint64_t cur;
err = 0;
if (check_block_range(vhd, block + count, hex))
return -ERANGE;
for (i = 0; i < count; i++) {
cur = block + i;
if (vhd->bat.bat[cur] == DD_BLK_UNUSED) {
printf("block %s not allocated\n", conv(hex, cur));
continue;
}
err = vhd_read_block(vhd, cur, &buf);
if (err)
break;
write_full(STDOUT_FILENO, buf, vhd->header.block_size);
free(buf);
}
return err;
}
static int
vhd_read_data(vhd_context_t *vhd, uint64_t sec, int count, int hex)
{
char *buf;
uint64_t cur;
int err, max, secs;
if (vhd_sectors_to_bytes(sec + count) > vhd->footer.curr_size)
return -ERANGE;
max = MIN(vhd_sectors_to_bytes(count), VHD_BLOCK_SIZE);
err = posix_memalign((void **)&buf, VHD_SECTOR_SIZE, max);
if (err)
return -err;
cur = sec;
while (count) {
secs = MIN((max >> VHD_SECTOR_SHIFT), count);
err = vhd_io_read(vhd, buf, cur, secs);
if (err)
break;
write_full(STDOUT_FILENO, buf, vhd_sectors_to_bytes(secs));
cur += secs;
count -= secs;
}
free(buf);
return err;
}
int
vhd_util_read(int argc, char **argv)
{
char *name;
vhd_context_t vhd;
int c, err, headers, hex;
uint64_t bat, bitmap, tbitmap, batmap, tbatmap, data, lsec, count, read;
err = 0;
hex = 0;
headers = 0;
count = 1;
bat = -1;
bitmap = -1;
tbitmap = -1;
batmap = -1;
tbatmap = -1;
data = -1;
lsec = -1;
read = -1;
name = NULL;
if (!argc || !argv)
goto usage;
optind = 0;
while ((c = getopt(argc, argv, "n:pt:b:m:i:aj:d:c:r:xh")) != -1) {
switch(c) {
case 'n':
name = optarg;
break;
case 'p':
headers = 1;
break;
case 't':
lsec = strtoul(optarg, NULL, 10);
break;
case 'b':
bat = strtoull(optarg, NULL, 10);
break;
case 'm':
bitmap = strtoull(optarg, NULL, 10);
break;
case 'i':
tbitmap = strtoul(optarg, NULL, 10);
break;
case 'a':
batmap = 1;
break;
case 'j':
tbatmap = strtoull(optarg, NULL, 10);
break;
case 'd':
data = strtoull(optarg, NULL, 10);
break;
case 'r':
read = strtoull(optarg, NULL, 10);
break;
case 'c':
count = strtoul(optarg, NULL, 10);
break;
case 'x':
hex = 1;
break;
case 'h':
default:
goto usage;
}
}
if (!name || optind != argc)
goto usage;
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED);
if (err) {
printf("Failed to open %s: %d\n", name, err);
vhd_dump_headers(name, hex);
return err;
}
err = vhd_get_bat(&vhd);
if (err) {
printf("Failed to get bat for %s: %d\n", name, err);
goto out;
}
if (headers)
vhd_print_headers(&vhd, hex);
if (lsec != -1) {
err = vhd_print_logical_to_physical(&vhd, lsec, count, hex);
if (err)
goto out;
}
if (bat != -1) {
err = vhd_print_bat(&vhd, bat, count, hex);
if (err)
goto out;
}
if (bitmap != -1) {
err = vhd_print_bitmap(&vhd, bitmap, count, hex);
if (err)
goto out;
}
if (tbitmap != -1) {
err = vhd_test_bitmap(&vhd, tbitmap, count, hex);
if (err)
goto out;
}
if (batmap != -1) {
err = vhd_print_batmap(&vhd);
if (err)
goto out;
}
if (tbatmap != -1) {
err = vhd_test_batmap(&vhd, tbatmap, count, hex);
if (err)
goto out;
}
if (data != -1) {
err = vhd_print_data(&vhd, data, count, hex);
if (err)
goto out;
}
if (read != -1) {
err = vhd_read_data(&vhd, read, count, hex);
if (err)
goto out;
}
err = 0;
out:
vhd_close(&vhd);
return err;
usage:
printf("options:\n"
"-h help\n"
"-n name\n"
"-p print VHD headers\n"
"-t sec translate logical sector to VHD location\n"
"-b blk print bat entry\n"
"-m blk print bitmap\n"
"-i sec test bitmap for logical sector\n"
"-a print batmap\n"
"-j blk test batmap for block\n"
"-d blk print data\n"
"-c num num units\n"
"-r sec read num sectors at sec\n"
"-x print in hex\n");
return EINVAL;
}
+84
View File
@@ -0,0 +1,84 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "libvhd.h"
int
vhd_util_repair(int argc, char **argv)
{
char *name;
int err, c;
off64_t eof;
vhd_context_t vhd;
name = NULL;
if (!argc || !argv)
goto usage;
optind = 0;
while ((c = getopt(argc, argv, "n:h")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 'h':
default:
goto usage;
}
}
if (!name || optind != argc)
goto usage;
err = vhd_open(&vhd, name, VHD_OPEN_RDWR);
if (err) {
printf("error opening %s: %d\n", name, err);
return err;
}
err = vhd_end_of_data(&vhd, &eof);
if (err) {
printf("error finding end of data: %d\n", err);
goto done;
}
err = vhd_write_footer_at(&vhd, &vhd.footer, eof);
done:
vhd_close(&vhd);
return err;
usage:
printf("options: <-n name> [-h help]\n");
return -EINVAL;
}
File diff suppressed because it is too large. Load diff
+106
View File
@@ -0,0 +1,106 @@
/* Copyright (c) 2008, XenSource Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of XenSource Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Altering operations:
*
* 1. Change the parent pointer to another file.
* 2. Change the size of the file containing the VHD image. This does NOT
* affect the VHD disk capacity, only the physical size of the file containing
* the VHD. Naturally, it is not possible to set the file size to be less than
* the what VHD utilizes.
* The operation doesn't actually change the file size, but it writes the
* footer in the right location such that resizing the file (manually, as a
* separate step) will produce the correct results. If the new file size is
* greater than the current file size, the file must first be expanded and then
* altered with this operation. If the new size is smaller than the current
* size, the VHD must first be altered with this operation and then the file
* must be shrunk. Failing to resize the file will result in a corrupted VHD.
*/
#include <errno.h>
//#include <fcntl.h>
#include <stdio.h>
//#include <stdlib.h>
#include <unistd.h>
#include "libvhd.h"
#include "libvhd-journal.h"
int
vhd_util_revert(int argc, char **argv)
{
char *name, *jname;
vhd_journal_t journal;
int c, err;
name = NULL;
jname = NULL;
optind = 0;
while ((c = getopt(argc, argv, "n:j:h")) != -1) {
switch (c) {
case 'n':
name = optarg;
break;
case 'j':
jname = optarg;
break;
case 'h':
default:
goto usage;
}
}
if (!name || !jname || argc != optind)
goto usage;
libvhd_set_log_level(1);
err = vhd_journal_open(&journal, name, jname);
if (err) {
printf("opening journal failed: %d\n", err);
return err;
}
err = vhd_journal_revert(&journal);
if (err) {
printf("reverting journal failed: %d\n", err);
vhd_journal_close(&journal);
return err;
}
err = vhd_journal_remove(&journal);
if (err) {
printf("removing journal failed: %d\n", err);
vhd_journal_close(&journal);
return err;
}
return 0;
usage:
printf("options: <-n name> <-j journal> [-h help]\n");
return -EINVAL;
}
File diff suppressed because it is too large. Load diff
Loaded 100 of 108 files, more files were not shown because too many files have changed in this diff. Show more
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.