Browse files

Merge pull request #8 from andreil/trunk-ring3

Cleanup & fixes after code-reviewing the NBD additions
  • Loading branch information...
2 parents d38b32a + 5772631 commit a53f25c119f0b7641460e52ca9dc90f75facd33f @andreil andreil committed Jun 25, 2012
View
7 control/tap-ctl-unpause.c
@@ -40,7 +40,8 @@
#include "tap-ctl.h"
int
-tap_ctl_unpause(const int id, const int minor, const char *params, int flags, char *secondary)
+tap_ctl_unpause(const int id, const int minor, const char *params, int flags,
+ char *secondary)
{
int err;
tapdisk_message_t message;
@@ -55,8 +56,8 @@ tap_ctl_unpause(const int id, const int minor, const char *params, int flags, ch
sizeof(message.u.params.path) - 1);
if (secondary) {
err = snprintf(message.u.params.secondary,
- sizeof(message.u.params.secondary) - 1, "%s",
- secondary);
+ sizeof(message.u.params.secondary) - 1, "%s",
+ secondary);
if (err >= sizeof(message.u.params.secondary)) {
EPRINTF("secondary image name too long\n");
return ENAMETOOLONG;
View
3 control/tap-ctl.c
@@ -609,7 +609,8 @@ tap_cli_pause(int argc, char **argv)
static void
tap_cli_unpause_usage(FILE *stream)
{
- fprintf(stream, "usage: unpause <-p pid> <-m minor> [-a args] [-2 secondary]\n");
+ fprintf(stream, "usage: unpause <-p pid> <-m minor> [-a args] "
+ "[-2 secondary]\n");
}
int
View
1 drivers/Makefile.am
@@ -82,7 +82,6 @@ libtapdisk_la_SOURCES += block-valve.h
libtapdisk_la_SOURCES += block-vindex.c
libtapdisk_la_SOURCES += block-lcache.c
libtapdisk_la_SOURCES += block-llcache.c
-libtapdisk_la_SOURCES += block-export.c
libtapdisk_la_SOURCES += block-nbd.c
libtapdisk_la_LIBADD = ../vhd/lib/libvhd.la
View
354 drivers/block-export.c
@@ -1,354 +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 <sys/types.h>
-#include <netdb.h>
-#include <arpa/inet.h>
-#include <netinet/tcp.h>
-#include <netinet/in.h>
-#include "tapdisk.h"
-#include "tapdisk-server.h"
-#include "tapdisk-driver.h"
-#include "tapdisk-interface.h"
-
-#define INFO(_f, _a...) tlog_syslog(TLOG_INFO, "export: " _f, ##_a)
-#define ERROR(_f, _a...) tlog_syslog(TLOG_WARN, "export: " _f, ##_a)
-
-struct vdi_chunk_format {
- uint64_t offset;
- uint32_t length;
- char payload[0];
-} __attribute__((__packed__));
-
-struct vdi_chunk {
- unsigned long bytes_to_send;
- unsigned long bytes_sent;
- int send_terminator;
- struct vdi_chunk_format chunk;
-};
-
-struct tdexport_data {
- /* Socket details */
- int socket;
- struct sockaddr_in *remote;
- char *peer_ip;
-
- /* HTTP import protocol details */
- char *session_ref;
- char *vdi_ref;
-};
-
-int tdexport_connect_import_session(struct tdexport_data *prv)
-{
- int sock;
- int opt = 1;
- int rc;
- char *msg;
-
- sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock < 0) {
- ERROR("Could not create socket: %s\n", strerror(errno));
- return -1;
- }
-
- rc = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&opt, sizeof(opt));
- if (rc < 0) {
- ERROR("Could not set TCP_NODELAY: %s\n", strerror(errno));
- return -1;
- }
-
- prv->remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *));
- if (!prv->remote) {
- ERROR("struct sockaddr_in malloc failure\n");
- close(sock);
- return -1;
- }
- prv->remote->sin_family = AF_INET;
- rc = inet_pton(AF_INET, prv->peer_ip, &(prv->remote->sin_addr.s_addr));
- if (rc < 0) {
- ERROR("Could not create inaddr: %s\n", strerror(errno));
- free(prv->remote);
- prv->remote = NULL;
- close(sock);
- return -1;
- }
- else if (rc == 0) {
- ERROR("inet_pton parse error\n");
- free(prv->remote);
- prv->remote = NULL;
- close(sock);
- return -1;
- }
- prv->remote->sin_port = htons(80);
-
- if (connect(sock, (struct sockaddr *)prv->remote, sizeof(struct sockaddr)) < 0) {
- ERROR("Could not connect to peer: %s\n", strerror(errno));
- close(sock);
- return -1;
- }
-
-#define IMPORT_URL_FMT "PUT /import_raw_vdi?session_id=%s&vdi=%s&chunked=true&o_direct=true HTTP/1.0\r\n\r\n"
- msg = malloc(strlen(IMPORT_URL_FMT) +
- strlen(prv->session_ref) +
- strlen(prv->vdi_ref) +1 );
- if (!msg) {
- ERROR("HTTP PUT message malloc failure\n");
- close(sock);
- return -1;
- }
- sprintf(msg, IMPORT_URL_FMT, prv->session_ref, prv->vdi_ref);
- INFO("Using query: %s\n", msg);
-
- rc = send(sock, msg, strlen(msg), 0);
- if (rc == -1) {
- ERROR("Error sending PUT request: %s\n", strerror(errno));
- close(sock);
- free(msg);
- return -1;
- }
- free(msg);
-
- /* Wait for HTTP/1.1 200 OK and the double \r\n */
-#define RECV_BUFFER_SIZE 4096
- msg = malloc(RECV_BUFFER_SIZE);
- if (!msg) {
- ERROR("Receive buffer malloc failure\n");
- close(sock);
- return -1;
- }
- for (;;) {
- rc = recv(sock, msg, RECV_BUFFER_SIZE, 0);
- if ( rc < 0) {
- ERROR("Error receiving from peer: %s", strerror(errno));
- free(msg);
- close(sock);
- return -1;
- }
- if (strstr(msg, "HTTP/1.1 200 OK")) {
- INFO("Received from peer: HTTP/1.1 200 OK\n");
- }
- if (strstr(msg, "\r\n\r\n")) {
- break;
- }
- }
- free(msg);
-
- prv->socket = sock;
- return sock;
-}
-
-int tdexport_send_chunk(struct tdexport_data *prv, const char *buffer, uint32_t length, uint64_t offset)
-{
- struct vdi_chunk_format chunk;
- ssize_t rc;
- uint32_t bytes_to_send;
- uint32_t bytes_sent;
-
- INFO("Sending chunk of size %u at offset %"PRIu64"\n", length, offset);
- if (prv->socket == 0) {
- ERROR("Cannot send chunk without an open socket\n");
- return -1;
- }
-
- chunk.length = length;
- chunk.offset = offset;
- rc = send(prv->socket, &chunk, sizeof(chunk), 0);
- if (rc < 0) {
- ERROR("Error sending chunk header: %s\n", strerror(errno));
- return -1;
- }
- if (rc != sizeof(chunk)) {
- ERROR("Send of %d bytes of chunk header does not match expected %d\n", (int)rc, (int)sizeof(chunk));
- }
-
- bytes_to_send = length;
- bytes_sent = 0;
-
- while(bytes_to_send) {
- rc = send(prv->socket, buffer + bytes_sent, bytes_to_send, 0);
- if (rc < 0) {
- ERROR("Error sending chunk payload: %s\n", strerror(errno));
- return -1;
- }
- if ((uint32_t)rc > bytes_to_send)
- rc = bytes_to_send;
- bytes_sent += (uint32_t)rc;
- bytes_to_send -= (uint32_t)rc;
- }
-
- return 0;
-}
-
-int tdexport_recv_result(struct tdexport_data *prv)
-{
- uint32_t result;
- ssize_t rc;
-
- if (prv->socket == 0) {
- ERROR("Cannot receive result an open socket\n");
- return -1;
- }
- rc = recv(prv->socket, &result, sizeof(result), 0);
- if (rc < 0) {
- ERROR("Error receiving result: %s\n", strerror(errno));
- return -1;
- }
- if (result != 0) {
- ERROR("Received non-zero result: %d (mirror is out-of-sync)", result);
- return -1;
- }
-
- INFO("Received successful result; mirror is synchronised OK\n");
-
- return 0;
-}
-
-int tdexport_send_terminator_chunk(struct tdexport_data *prv)
-{
- struct vdi_chunk_format zero;
- ssize_t rc;
-
- if (prv->socket == 0) {
- ERROR("Cannot send terminator chunk without an open socket\n");
- return -1;
- }
-
- zero.offset = 0ULL;
- zero.length = 0UL;
-
- rc = send(prv->socket, &zero, sizeof(zero), 0);
- if (rc < 0) {
- ERROR("Error sending chunk terminator: %s\n", strerror(errno));
- return -1;
- }
- if (rc != sizeof(zero)) {
- ERROR("Send of %d bytes of terminator does not match expected %u\n", (int)rc, (int)sizeof(zero));
- return -1;
- }
- INFO("Terminator chunk written successfully\n");
- return 0;
-}
-
-/* -- interface -- */
-
-static int tdexport_close(td_driver_t*);
-
-static int tdexport_open(td_driver_t* driver, const char* name, td_flag_t flags)
-{
- struct tdexport_data *prv;
- char peer_ip[256];
- char session_ref[256];
- char vdi_ref[256];
- int rc;
-
- driver->info.sector_size = 512;
- driver->info.info = 0;
-
- prv = (struct tdexport_data *)driver->data;
- memset(prv, 0, sizeof(struct tdexport_data));
-
- INFO("Opening export to %s\n", name);
-
- rc = sscanf(name, "%255[^/]/%255[^/]/%255[^\n]", peer_ip, session_ref, vdi_ref);
- if (rc != 3) {
- ERROR("Could not parse export URL");
- return -1;
- }
- prv->peer_ip = malloc(strlen(peer_ip) + 1);
- prv->session_ref = malloc(strlen(session_ref) + 1);
- prv->vdi_ref = malloc(strlen(vdi_ref) + 1);
- if (!prv->peer_ip || !prv->session_ref || !prv->vdi_ref) {
- ERROR("Failure to malloc for URL parts");
- return -1;
- }
- strcpy(prv->peer_ip, peer_ip);
- strcpy(prv->session_ref, session_ref);
- strcpy(prv->vdi_ref, vdi_ref);
-
- INFO("Export peer=%s session=%s VDI=%s\n", prv->peer_ip, prv->session_ref, prv->vdi_ref);
- if (tdexport_connect_import_session(prv) < 0) {
- return -1;
- }
-
- return 0;
-}
-
-static int tdexport_close(td_driver_t* driver)
-{
- struct tdexport_data *prv;
-
- prv = (struct tdexport_data *)driver->data;
- if (prv->socket) {
- tdexport_send_terminator_chunk(prv);
- tdexport_recv_result(prv);
- close(prv->socket);
- prv->socket = 0;
- }
-
- if (prv->peer_ip) {
- free(prv->peer_ip);
- prv->peer_ip = NULL;
- }
- if (prv->session_ref) {
- free(prv->session_ref);
- prv->session_ref = NULL;
- }
- if (prv->vdi_ref) {
- free(prv->vdi_ref);
- prv->vdi_ref = NULL;
- }
-
- return 0;
-}
-
-static void tdexport_queue_read(td_driver_t* driver, td_request_t treq)
-{
- int size = treq.secs * driver->info.sector_size;
- uint64_t offset = treq.sec * (uint64_t)driver->info.sector_size;
- INFO("READ %"PRIu64" (%d)\n", offset, size);
- td_forward_request(treq);
- //td_complete_request(treq, 0);
-}
-
-static void tdexport_queue_write(td_driver_t* driver, td_request_t treq)
-{
- struct tdexport_data *prv = (struct tdexport_data *)driver->data;
- int size = treq.secs * driver->info.sector_size;
- uint64_t offset = treq.sec * (uint64_t)driver->info.sector_size;
-
- //memcpy(img + offset, treq.buf, size);
-
- INFO("WRITE 0x%"PRIu64" (%u)\n", offset, size);
-
- tdexport_send_chunk(prv, treq.buf, size, offset);
-
- td_complete_request(treq, 0);
-}
-
-static int tdexport_get_parent_id(td_driver_t* driver, td_disk_id_t* id)
-{
- return -EINVAL;
-}
-
-static int tdexport_validate_parent(td_driver_t *driver,
- td_driver_t *parent, td_flag_t flags)
-{
- return 0;
-}
-
-struct tap_disk tapdisk_export = {
- .disk_type = "tapdisk_export",
- .private_data_size = sizeof(struct tdexport_data),
- .flags = 0,
- .td_open = tdexport_open,
- .td_close = tdexport_close,
- .td_queue_read = tdexport_queue_read,
- .td_queue_write = tdexport_queue_write,
- .td_get_parent_id = tdexport_get_parent_id,
- .td_validate_parent = tdexport_validate_parent,
-};
View
663 drivers/block-nbd.c
@@ -1,3 +1,32 @@
+/*
+ * Copyright (c) 2012, Citrix Systems, 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 <fcntl.h>
@@ -28,108 +57,112 @@
#define N_PASSED_FDS 10
#define TAPDISK_NBDCLIENT_MAX_PATH_LEN 256
-#define TAPDISK_NBDCLIENT_LISTENING_SOCK_PATH "/var/run/blktap-control/nbdclient"
+#define TAPDISK_NBDCLIENT_LISTEN_SOCK_PATH "/var/run/blktap-control/nbdclient"
#define MAX_NBD_REQS TAPDISK_DATA_REQUESTS
#define NBD_TIMEOUT 30
-/* We'll only ever have one nbdclient fd receiver per tapdisk process,
- so let's just store it here globally. We'll also keep track of the
- passed fds here too. */
+/*
+ * We'll only ever have one nbdclient fd receiver per tapdisk process, so let's
+ * just store it here globally. We'll also keep track of the passed fds here
+ * too.
+ */
-struct td_fdreceiver *fdreceiver=NULL;
+struct td_fdreceiver *fdreceiver = NULL;
struct tdnbd_passed_fd {
- char id[40];
- struct timeval t;
- int fd;
+ char id[40];
+ struct timeval t;
+ int fd;
} passed_fds[N_PASSED_FDS];
-
struct nbd_queued_io {
- char *buffer;
- int len;
- int so_far;
+ char *buffer;
+ int len;
+ int so_far;
};
struct td_nbd_request {
- td_request_t treq;
- struct nbd_request nreq;
- int timeout_event;
- int fake;
- struct nbd_queued_io header; /* points to the request struct above */
- struct nbd_queued_io body; /* in or out, depending on whether type is read or write */
- struct list_head queue;
+ td_request_t treq;
+ struct nbd_request nreq;
+ int timeout_event;
+ int fake;
+ struct nbd_queued_io header;
+ struct nbd_queued_io body; /* in or out, depending on whether
+ type is read or write. */
+ struct list_head queue;
};
struct tdnbd_data
{
- int writer_event_id;
- struct list_head sent_reqs;
- struct list_head pending_reqs;
- struct list_head free_reqs;
- struct td_nbd_request requests[MAX_NBD_REQS];
- int nr_free_count;
-
- int reader_event_id;
- struct nbd_reply current_reply;
- struct nbd_queued_io cur_reply_qio;
- struct td_nbd_request *current_reply_request;
-
- int socket;
- struct sockaddr_in *remote;
- char *peer_ip;
- int port;
- char *name;
-
- int flags;
- int closed;
+ int writer_event_id;
+ struct list_head sent_reqs;
+ struct list_head pending_reqs;
+ struct list_head free_reqs;
+ struct td_nbd_request requests[MAX_NBD_REQS];
+ int nr_free_count;
+
+ int reader_event_id;
+ struct nbd_reply current_reply;
+ struct nbd_queued_io cur_reply_qio;
+ struct td_nbd_request *curr_reply_req;
+
+ int socket;
+ struct sockaddr_in *remote;
+ char *peer_ip;
+ int port;
+ char *name;
+
+ int flags;
+ int closed;
};
-int global_id=0;
-
-void disable_write_queue(struct tdnbd_data *prv);
+int global_id = 0;
+static void disable_write_queue(struct tdnbd_data *prv);
/* -- fdreceiver bits and pieces -- */
-void
+static void
tdnbd_stash_passed_fd(int fd, char *msg, void *data)
{
- int free_index=-1;
+ int free_index = -1;
int i;
- for(i=0; i<N_PASSED_FDS; i++) {
- if(passed_fds[i].fd == -1) {
- free_index=i;
+ for (i = 0; i < N_PASSED_FDS; i++)
+ if (passed_fds[i].fd == -1) {
+ free_index = i;
break;
}
- }
- if(free_index==-1) {
- ERROR("Error - more than %d fds passed! cannot stash another.",N_PASSED_FDS);
+ if (free_index == -1) {
+ ERROR("Error - more than %d fds passed! cannot stash another",
+ N_PASSED_FDS);
close(fd);
return;
}
- passed_fds[free_index].fd=fd;
- strncpy(passed_fds[free_index].id, msg, sizeof(passed_fds[free_index].id));
+ passed_fds[free_index].fd = fd;
+ strncpy(passed_fds[free_index].id, msg,
+ sizeof(passed_fds[free_index].id));
gettimeofday(&passed_fds[free_index].t, NULL);
}
-int tdnbd_retreive_passed_fd(const char *name)
+static int
+tdnbd_retreive_passed_fd(const char *name)
{
int fd, i;
- for(i=0; i<N_PASSED_FDS; i++) {
- if(strncmp(name, passed_fds[i].id,sizeof(passed_fds[i].id))==0) {
- fd=passed_fds[i].fd;
+ for (i = 0; i < N_PASSED_FDS; i++) {
+ if (strncmp(name, passed_fds[i].id,
+ sizeof(passed_fds[i].id)) == 0) {
+ fd = passed_fds[i].fd;
passed_fds[i].fd = -1;
return fd;
}
}
- ERROR("Couldn't find the fd named: %s",name);
+ ERROR("Couldn't find the fd named: %s", name);
return -1;
}
@@ -141,83 +174,88 @@ tdnbd_fdreceiver_start()
int i;
/* initialise the passed fds list */
- for(i=0; i<N_PASSED_FDS; i++) {
+ for (i = 0; i < N_PASSED_FDS; i++)
passed_fds[i].fd = -1;
- }
snprintf(fdreceiver_path, TAPDISK_NBDCLIENT_MAX_PATH_LEN,
- "%s%d", TAPDISK_NBDCLIENT_LISTENING_SOCK_PATH, getpid());
+ "%s%d", TAPDISK_NBDCLIENT_LISTEN_SOCK_PATH, getpid());
- fdreceiver=td_fdreceiver_start(fdreceiver_path,
- tdnbd_stash_passed_fd, NULL);
+ fdreceiver = td_fdreceiver_start(fdreceiver_path,
+ tdnbd_stash_passed_fd, NULL);
}
+void
+tdnbd_fdreceiver_stop()
+{
+ if (fdreceiver)
+ td_fdreceiver_stop(fdreceiver);
+}
-void __cancel_req(int i, struct td_nbd_request *pos, int e)
+static void
+__cancel_req(int i, struct td_nbd_request *pos, int e)
{
char handle[9];
- memcpy(handle,pos->nreq.handle,8);
- handle[8]=0;
- INFO("Entry %d: handle='%s' type=%d -- reporting errno: %d",i,handle,ntohl(pos->nreq.type), e);
-
- if(pos->timeout_event >= 0) {
+ memcpy(handle, pos->nreq.handle, 8);
+ handle[8] = 0;
+ INFO("Entry %d: handle='%s' type=%d -- reporting errno: %d",
+ i, handle, ntohl(pos->nreq.type), e);
+
+ if (pos->timeout_event >= 0) {
tapdisk_server_unregister_event(pos->timeout_event);
pos->timeout_event = -1;
}
-
+
td_complete_request(pos->treq, e);
}
-void tdnbd_disable(struct tdnbd_data *prv, int e)
+static void
+tdnbd_disable(struct tdnbd_data *prv, int e)
{
struct td_nbd_request *pos, *q;
- int i=0;
-
+ int i = 0;
+
INFO("NBD client full-disable");
tapdisk_server_unregister_event(prv->writer_event_id);
tapdisk_server_unregister_event(prv->reader_event_id);
- list_for_each_entry_safe(pos, q, &prv->sent_reqs, queue) {
- __cancel_req(i++,pos,e);
- }
+ list_for_each_entry_safe(pos, q, &prv->sent_reqs, queue)
+ __cancel_req(i++, pos, e);
+
+ list_for_each_entry_safe(pos, q, &prv->pending_reqs, queue)
+ __cancel_req(i++, pos, e);
- list_for_each_entry_safe(pos, q, &prv->pending_reqs, queue) {
- __cancel_req(i++,pos,e);
- }
-
INFO("Setting closed");
prv->closed = 3;
}
/* NBD writer queue */
-
/* Return code: how much is left to write, or a negative error code */
-int tdnbd_write_some(int fd, struct nbd_queued_io *data)
+static int
+tdnbd_write_some(int fd, struct nbd_queued_io *data)
{
int left = data->len - data->so_far;
int rc;
char *code;
- while(left > 0) {
+ while (left > 0) {
rc = send(fd, data->buffer + data->so_far, left, 0);
- if(rc == -1) {
-
- if((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
+ if (rc == -1) {
+ if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
return left;
- }
code = strerror(errno);
- ERROR("Bad return code %d from send (%s)",rc,(code==0 ? "unknown" : code));
+ ERROR("Bad return code %d from send (%s)", rc,
+ (code == 0 ? "unknown" : code));
return rc;
}
- if(rc == 0) {
- ERROR("Server shutdown prematurely in write_some");
- return -1;
+ if (rc == 0) {
+ ERROR("Server shutdown prematurely in write_some");
+ return -1;
}
left -= rc;
@@ -227,29 +265,30 @@ int tdnbd_write_some(int fd, struct nbd_queued_io *data)
return left;
}
-int tdnbd_read_some(int fd, struct nbd_queued_io *data)
+static int
+tdnbd_read_some(int fd, struct nbd_queued_io *data)
{
int left = data->len - data->so_far;
int rc;
char *code;
- while(left > 0) {
+ while (left > 0) {
rc = recv(fd, data->buffer + data->so_far, left, 0);
- if(rc == -1) {
+ if (rc == -1) {
- if((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
+ if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
return left;
- }
code = strerror(errno);
- ERROR("Bad return code %d from send (%s)",rc,(code==0 ? "unknown" : code));
+ ERROR("Bad return code %d from send (%s)", rc,
+ (code == 0 ? "unknown" : code));
return rc;
}
- if(rc == 0) {
- ERROR("Server shutdown prematurely in read_some");
- return -1;
+ if (rc == 0) {
+ ERROR("Server shutdown prematurely in read_some");
+ return -1;
}
data->so_far += rc;
@@ -259,34 +298,38 @@ int tdnbd_read_some(int fd, struct nbd_queued_io *data)
return left;
}
-void tdnbd_timeout_cb(event_id_t eb, char mode, void *data)
+static void
+tdnbd_timeout_cb(event_id_t eb, char mode, void *data)
{
- struct tdnbd_data *prv=data;
-
- ERROR("Timeout!: %d",eb);
-
+ struct tdnbd_data *prv = data;
+ ERROR("Timeout!: %d", eb);
tdnbd_disable(prv, ETIMEDOUT);
}
-void tdnbd_writer_cb(event_id_t eb, char mode, void *data)
+static void
+tdnbd_writer_cb(event_id_t eb, char mode, void *data)
{
struct td_nbd_request *pos, *q;
-
- struct tdnbd_data *prv=data;
+ struct tdnbd_data *prv = data;
list_for_each_entry_safe(pos, q, &prv->pending_reqs, queue) {
- if(tdnbd_write_some(prv->socket, &pos->header)>0)
+ if (tdnbd_write_some(prv->socket, &pos->header) > 0)
return;
- if(ntohl(pos->nreq.type)==NBD_CMD_WRITE) {
- if(tdnbd_write_some(prv->socket, &pos->body)>0)
+
+ if (ntohl(pos->nreq.type) == NBD_CMD_WRITE) {
+ if (tdnbd_write_some(prv->socket, &pos->body) > 0)
return;
}
- if(ntohl(pos->nreq.type)==NBD_CMD_DISC) {
+
+ if (ntohl(pos->nreq.type) == NBD_CMD_DISC) {
INFO("sent close request");
- /* We don't expect a response from a DISC, so move the request back onto the free list */
+ /*
+ * We don't expect a response from a DISC, so move the
+ * request back onto the free list
+ */
list_move(&pos->queue, &prv->free_reqs);
prv->nr_free_count++;
- prv->closed=2;
+ prv->closed = 2;
} else {
list_move(&pos->queue, &prv->sent_reqs);
}
@@ -296,69 +339,73 @@ void tdnbd_writer_cb(event_id_t eb, char mode, void *data)
disable_write_queue(prv);
- if(prv->closed==2) {
- tdnbd_disable(prv,EIO);
- }
+ if (prv->closed == 2)
+ tdnbd_disable(prv, EIO);
return;
}
-int enable_write_queue(struct tdnbd_data *prv)
+static int
+enable_write_queue(struct tdnbd_data *prv)
{
- if(prv->writer_event_id >= 0)
+ if (prv->writer_event_id >= 0)
return 0;
prv->writer_event_id =
tapdisk_server_register_event(SCHEDULER_POLL_WRITE_FD,
- prv->socket,
- 0,
- tdnbd_writer_cb,
- prv);
-
+ prv->socket,
+ 0,
+ tdnbd_writer_cb,
+ prv);
+
return prv->writer_event_id;
}
-void disable_write_queue(struct tdnbd_data *prv)
+static void
+disable_write_queue(struct tdnbd_data *prv)
{
- if(prv->writer_event_id < 0)
+ if (prv->writer_event_id < 0)
return;
tapdisk_server_unregister_event(prv->writer_event_id);
prv->writer_event_id = -1;
}
-int tdnbd_queue_request(struct tdnbd_data *prv, int type, uint64_t offset,
- char *buffer, uint32_t length, td_request_t treq, int fake)
+static int
+tdnbd_queue_request(struct tdnbd_data *prv, int type, uint64_t offset,
+ char *buffer, uint32_t length, td_request_t treq, int fake)
{
- if(prv->nr_free_count==0)
+ if (prv->nr_free_count == 0)
return -EBUSY;
- if(prv->closed==3) {
- td_complete_request(treq, -ETIMEDOUT);
+ if (prv->closed == 3) {
+ td_complete_request(treq, -ETIMEDOUT);
return -ETIMEDOUT;
}
- struct td_nbd_request *req=list_entry(prv->free_reqs.next, struct td_nbd_request, queue);
+ struct td_nbd_request *req = list_entry(prv->free_reqs.next,
+ struct td_nbd_request, queue);
/* fill in the request */
- req->treq=treq;
- int id=global_id++;
+ req->treq = treq;
+ int id = global_id++;
snprintf(req->nreq.handle, 8, "td%05x", id % 0xffff);
/* No response from a disconnect, so no need for a timeout */
- if(type != NBD_CMD_DISC) {
- req->timeout_event=tapdisk_server_register_event(SCHEDULER_POLL_TIMEOUT,
- -1, /* dummy */
- NBD_TIMEOUT,
- tdnbd_timeout_cb,
- prv);
+ if (type != NBD_CMD_DISC) {
+ req->timeout_event = tapdisk_server_register_event(
+ SCHEDULER_POLL_TIMEOUT,
+ -1, /* dummy */
+ NBD_TIMEOUT,
+ tdnbd_timeout_cb,
+ prv);
} else {
req->timeout_event = -1;
}
- INFO("request: %s timeout %d",req->nreq.handle, req->timeout_event);
+ INFO("request: %s timeout %d", req->nreq.handle, req->timeout_event);
req->nreq.magic = htonl(NBD_REQUEST_MAGIC);
req->nreq.type = htonl(type);
@@ -370,132 +417,132 @@ int tdnbd_queue_request(struct tdnbd_data *prv, int type, uint64_t offset,
req->body.buffer = buffer;
req->body.len = length;
req->body.so_far = 0;
- req->fake=fake;
+ req->fake = fake;
list_move_tail(&req->queue, &prv->pending_reqs);
prv->nr_free_count--;
- if(prv->writer_event_id < 0) {
+ if (prv->writer_event_id < 0)
enable_write_queue(prv);
- }
+
return 0;
}
/* NBD Reader callback */
-void tdnbd_reader_cb(event_id_t eb, char mode, void *data)
+static void
+tdnbd_reader_cb(event_id_t eb, char mode, void *data)
{
char handle[9];
- int do_disable=0;
+ int do_disable = 0;
/* Check to see if we're in the middle of reading a response already */
-
- struct tdnbd_data *prv=data;
-
+ struct tdnbd_data *prv = data;
int rc = tdnbd_read_some(prv->socket, &prv->cur_reply_qio);
- if(rc<0) {
- ERROR("Error reading reply header: %d",rc);
+ if (rc < 0) {
+ ERROR("Error reading reply header: %d", rc);
tdnbd_disable(prv, EIO);
return;
}
- if(rc>0) {
+ if (rc > 0)
return; /* need more data */
- }
/* Got a header. */
-
- if(prv->current_reply.error != 0) {
- ERROR("Error in reply: %d",prv->current_reply.error);
+ if (prv->current_reply.error != 0) {
+ ERROR("Error in reply: %d", prv->current_reply.error);
tdnbd_disable(prv, EIO);
return;
}
- /* Have we found the request yet? */
-
- if(prv->current_reply_request==NULL) {
+ /* Have we found the request yet? */
+ if (prv->curr_reply_req == NULL) {
struct td_nbd_request *pos, *q;
list_for_each_entry_safe(pos, q, &prv->sent_reqs, queue) {
- if(memcmp(pos->nreq.handle, prv->current_reply.handle, 8)==0) {
- prv->current_reply_request=pos;
+ if (memcmp(pos->nreq.handle, prv->current_reply.handle,
+ 8) == 0) {
+ prv->curr_reply_req = pos;
break;
}
}
- if(prv->current_reply_request==NULL) {
- memcpy(handle,prv->current_reply.handle,8);
- handle[8]=0;
+ if (prv->curr_reply_req == NULL) {
+ memcpy(handle, prv->current_reply.handle, 8);
+ handle[8] = 0;
- ERROR("Couldn't find request corresponding to reply (reply handle='%s')",handle);
+ ERROR("Couldn't find request corresponding to reply "
+ "(reply handle='%s')", handle);
tdnbd_disable(prv, EIO);
return;
}
}
- switch(ntohl(prv->current_reply_request->nreq.type)) {
+ switch(ntohl(prv->curr_reply_req->nreq.type)) {
case NBD_CMD_READ:
- rc = tdnbd_read_some(prv->socket, &prv->current_reply_request->body);
+ rc = tdnbd_read_some(prv->socket,
+ &prv->curr_reply_req->body);
- if(rc<0) {
- ERROR("Error reading body of request: %d",rc);
+ if (rc < 0) {
+ ERROR("Error reading body of request: %d", rc);
tdnbd_disable(prv, EIO);
return;
}
-
- if(rc>0) {
+
+ if (rc > 0)
return; /* need more data */
- }
- td_complete_request(prv->current_reply_request->treq, 0);
+ td_complete_request(prv->curr_reply_req->treq, 0);
break;
case NBD_CMD_WRITE:
- td_complete_request(prv->current_reply_request->treq, 0);
+ td_complete_request(prv->curr_reply_req->treq, 0);
break;
-
default:
- ERROR("Unhandled request response: %d",ntohl(prv->current_reply_request->nreq.type));
- do_disable=1;
+ ERROR("Unhandled request response: %d",
+ ntohl(prv->curr_reply_req->nreq.type));
+ do_disable = 1;
return;
}
/* remove the state */
-
- list_move(&prv->current_reply_request->queue, &prv->free_reqs);
+ list_move(&prv->curr_reply_req->queue, &prv->free_reqs);
prv->nr_free_count++;
- prv->cur_reply_qio.so_far=0;
- if(prv->current_reply_request->timeout_event >= 0) {
- tapdisk_server_unregister_event(prv->current_reply_request->timeout_event);
+ prv->cur_reply_qio.so_far = 0;
+ if (prv->curr_reply_req->timeout_event >= 0) {
+ tapdisk_server_unregister_event(
+ prv->curr_reply_req->timeout_event);
}
- prv->current_reply_request=NULL;
+ prv->curr_reply_req = NULL;
- /* Nb, do this here otherwise we cancel the request that has just been moved */
-
- if(do_disable)
+ /*
+ * NB: do this here otherwise we cancel the request that has just been
+ * moved
+ */
+ if (do_disable)
tdnbd_disable(prv, EIO);
-
-
}
-int tdnbd_wait_read(int fd)
+static int
+tdnbd_wait_read(int fd)
{
struct timeval select_tv;
fd_set socks;
int rc;
FD_ZERO(&socks);
FD_SET(fd, &socks);
- select_tv.tv_sec=10;
- select_tv.tv_usec=0;
- rc = select(fd+1, &socks, NULL, NULL, &select_tv);
+ select_tv.tv_sec = 10;
+ select_tv.tv_usec = 0;
+ rc = select(fd + 1, &socks, NULL, NULL, &select_tv);
return rc;
}
-int tdnbd_nbd_negotiate(struct tdnbd_data *prv, td_driver_t *driver)
+static int
+tdnbd_nbd_negotiate(struct tdnbd_data *prv, td_driver_t *driver)
{
#define RECV_BUFFER_SIZE 256
int rc;
@@ -506,119 +553,119 @@ int tdnbd_nbd_negotiate(struct tdnbd_data *prv, td_driver_t *driver)
int padbytes = 124;
int sock = prv->socket;
-
- /* NBD negotiation protocol:
+ /*
+ * NBD negotiation protocol:
*
* Server sends 'NBDMAGIC'
* then it sends 0x00420281861253L
* then it sends a 64 bit bigendian size
* then it sends a 32 bit bigendian flags
* then it sends 124 bytes of nothing
- *
*/
-
- /* We need to limit the time we spend in this function
- as we're still using blocking IO at this point */
-
- if(tdnbd_wait_read(sock) <= 0) {
- ERROR("Timeout in nbd_negotiate");
- close(sock);
- return -1;
+ /*
+ * We need to limit the time we spend in this function as we're still
+ * using blocking IO at this point
+ */
+ if (tdnbd_wait_read(sock) <= 0) {
+ ERROR("Timeout in nbd_negotiate");
+ close(sock);
+ return -1;
}
rc = recv(sock, buffer, 8, 0);
- if(rc<8) {
- ERROR("Short read in negotiation(1) (%d)\n",rc);
- close(sock);
- return -1;
+ if (rc < 8) {
+ ERROR("Short read in negotiation(1) (%d)\n", rc);
+ close(sock);
+ return -1;
}
- if(memcmp(buffer, "NBDMAGIC", 8) != 0) {
- buffer[8]=0;
- ERROR("Error in NBD negotiation: got '%s'",buffer);
- close(sock);
- return -1;
+ if (memcmp(buffer, "NBDMAGIC", 8) != 0) {
+ buffer[8] = 0;
+ ERROR("Error in NBD negotiation: got '%s'", buffer);
+ close(sock);
+ return -1;
}
- if(tdnbd_wait_read(sock) <= 0) {
- ERROR("Timeout in nbd_negotiate");
- close(sock);
- return -1;
+ if (tdnbd_wait_read(sock) <= 0) {
+ ERROR("Timeout in nbd_negotiate");
+ close(sock);
+ return -1;
}
rc = recv(sock, &magic, sizeof(magic), 0);
- if(rc<8) {
- ERROR("Short read in negotiation(2) (%d)\n",rc);
+ if (rc < 8) {
+ ERROR("Short read in negotiation(2) (%d)\n", rc);
- return -1;
+ return -1;
}
- if(ntohll(magic) != NBD_NEGOTIATION_MAGIC) {
- ERROR("Not enough magic in negotiation(2) (%"PRIu64")\n",ntohll(magic));
- close(sock);
- return -1;
+ if (ntohll(magic) != NBD_NEGOTIATION_MAGIC) {
+ ERROR("Not enough magic in negotiation(2) (%"PRIu64")\n",
+ ntohll(magic));
+ close(sock);
+ return -1;
}
- if(tdnbd_wait_read(sock) <= 0) {
- ERROR("Timeout in nbd_negotiate");
- close(sock);
- return -1;
+ if (tdnbd_wait_read(sock) <= 0) {
+ ERROR("Timeout in nbd_negotiate");
+ close(sock);
+ return -1;
}
rc = recv(sock, &size, sizeof(size), 0);
- if(rc<sizeof(size)) {
- ERROR("Short read in negotiation(3) (%d)\n",rc);
- close(sock);
- return -1;
+ if (rc < sizeof(size)) {
+ ERROR("Short read in negotiation(3) (%d)\n", rc);
+ close(sock);
+ return -1;
}
-
+
INFO("Got size: %"PRIu64"", ntohll(size));
driver->info.size = ntohll(size) >> SECTOR_SHIFT;
driver->info.sector_size = DEFAULT_SECTOR_SIZE;
driver->info.info = 0;
- if(tdnbd_wait_read(sock) <= 0) {
- ERROR("Timeout in nbd_negotiate");
- close(sock);
- return -1;
+ if (tdnbd_wait_read(sock) <= 0) {
+ ERROR("Timeout in nbd_negotiate");
+ close(sock);
+ return -1;
}
rc = recv(sock, &flags, sizeof(flags), 0);
- if(rc<sizeof(flags)) {
- ERROR("Short read in negotiation(4) (%d)\n",rc);
- close(sock);
- return -1;
+ if (rc < sizeof(flags)) {
+ ERROR("Short read in negotiation(4) (%d)\n", rc);
+ close(sock);
+ return -1;
}
INFO("Got flags: %"PRIu32"", ntohl(flags));
- while(padbytes>0) {
- if(tdnbd_wait_read(sock) <= 0) {
- ERROR("Timeout in nbd_negotiate");
- close(sock);
- return -1;
- }
+ while (padbytes > 0) {
+ if (tdnbd_wait_read(sock) <= 0) {
+ ERROR("Timeout in nbd_negotiate");
+ close(sock);
+ return -1;
+ }
- rc = recv(sock, buffer, padbytes, 0);
- if(rc<0) {
- ERROR("Bad read in negotiation(5) (%d)\n",rc);
- close(sock);
- return -1;
- }
- padbytes -= rc;
+ rc = recv(sock, buffer, padbytes, 0);
+ if (rc < 0) {
+ ERROR("Bad read in negotiation(5) (%d)\n", rc);
+ close(sock);
+ return -1;
+ }
+ padbytes -= rc;
}
-
+
INFO("Successfully connected to NBD server");
fcntl(sock, F_SETFL, O_NONBLOCK);
return 0;
}
-
-int tdnbd_connect_import_session(struct tdnbd_data *prv, td_driver_t* driver)
+static int
+tdnbd_connect_import_session(struct tdnbd_data *prv, td_driver_t* driver)
{
int sock;
int opt = 1;
@@ -630,13 +677,15 @@ int tdnbd_connect_import_session(struct tdnbd_data *prv, td_driver_t* driver)
return -1;
}
- rc = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&opt, sizeof(opt));
+ rc = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&opt,
+ sizeof(opt));
if (rc < 0) {
ERROR("Could not set TCP_NODELAY: %s\n", strerror(errno));
return -1;
}
- prv->remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *));
+ prv->remote = (struct sockaddr_in *)malloc(
+ sizeof(struct sockaddr_in *));
if (!prv->remote) {
ERROR("struct sockaddr_in malloc failure\n");
close(sock);
@@ -659,8 +708,9 @@ int tdnbd_connect_import_session(struct tdnbd_data *prv, td_driver_t* driver)
return -1;
}
prv->remote->sin_port = htons(prv->port);
-
- if (connect(sock, (struct sockaddr *)prv->remote, sizeof(struct sockaddr)) < 0) {
+
+ if (connect(sock, (struct sockaddr *)prv->remote,
+ sizeof(struct sockaddr)) < 0) {
ERROR("Could not connect to peer: %s\n", strerror(errno));
close(sock);
return -1;
@@ -675,7 +725,8 @@ int tdnbd_connect_import_session(struct tdnbd_data *prv, td_driver_t* driver)
static int tdnbd_close(td_driver_t*);
-static int tdnbd_open(td_driver_t* driver, const char* name, td_flag_t flags)
+static int
+tdnbd_open(td_driver_t* driver, const char* name, td_flag_t flags)
{
struct tdnbd_data *prv;
char peer_ip[256];
@@ -690,14 +741,14 @@ static int tdnbd_open(td_driver_t* driver, const char* name, td_flag_t flags)
memset(prv, 0, sizeof(struct tdnbd_data));
INFO("Opening nbd export to %s (flags=%x)\n", name, flags);
-
- prv->writer_event_id=-1;
+
+ prv->writer_event_id = -1;
INIT_LIST_HEAD(&prv->sent_reqs);
INIT_LIST_HEAD(&prv->pending_reqs);
INIT_LIST_HEAD(&prv->free_reqs);
- for(i=0; i<MAX_NBD_REQS; i++) {
+ for (i = 0; i < MAX_NBD_REQS; i++) {
INIT_LIST_HEAD(&prv->requests[i].queue);
- prv->requests[i].timeout_event=-1;
+ prv->requests[i].timeout_event = -1;
list_add(&prv->requests[i].queue, &prv->free_reqs);
}
prv->nr_free_count = MAX_NBD_REQS;
@@ -711,58 +762,57 @@ static int tdnbd_open(td_driver_t* driver, const char* name, td_flag_t flags)
return -1;
}
strcpy(prv->peer_ip, peer_ip);
- prv->port=port;
- prv->name=NULL;
+ prv->port = port;
+ prv->name = NULL;
INFO("Export peer=%s port=%d\n", prv->peer_ip, prv->port);
- if (tdnbd_connect_import_session(prv, driver) < 0) {
+ if (tdnbd_connect_import_session(prv, driver) < 0)
return -1;
- }
} else {
prv->socket = tdnbd_retreive_passed_fd(name);
- if(prv->socket < 0) {
- ERROR("Couldn't find fd named: %s",name);
+ if (prv->socket < 0) {
+ ERROR("Couldn't find fd named: %s", name);
return -1;
}
INFO("Found passed fd. Connecting...");
prv->remote = NULL;
prv->peer_ip = NULL;
prv->name = strdup(name);
prv->port = -1;
- if(tdnbd_nbd_negotiate(prv, driver) < 0) {
+ if (tdnbd_nbd_negotiate(prv, driver) < 0) {
ERROR("Failed to negotiate");
return -1;
}
}
prv->reader_event_id =
tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
- prv->socket, 0,
- tdnbd_reader_cb,
- (void *)prv);
+ prv->socket, 0,
+ tdnbd_reader_cb,
+ (void *)prv);
prv->flags = flags;
prv->closed = 0;
- if(flags & TD_OPEN_SECONDARY) {
- INFO("Opening in secondary mode: Read requests will be forwarded");
- }
+ if (flags & TD_OPEN_SECONDARY)
+ INFO("Opening in secondary mode: Read requests will be "
+ "forwarded");
return 0;
}
-static int tdnbd_close(td_driver_t* driver)
+static int
+tdnbd_close(td_driver_t* driver)
{
struct tdnbd_data *prv = (struct tdnbd_data *)driver->data;
td_request_t treq;
bzero(&treq, sizeof(treq));
-
- if(prv->closed == 3) {
+ if (prv->closed == 3) {
INFO("NBD close: already decided that the connection is dead.");
- if(prv->socket >= 0)
+ if (prv->socket >= 0)
close(prv->socket);
prv->socket = -1;
return 0;
@@ -777,7 +827,7 @@ static int tdnbd_close(td_driver_t* driver)
fcntl(prv->socket, F_SETFL, fcntl(prv->socket, F_GETFL) & ~O_NONBLOCK);
INFO("Writing disconnection request");
- tdnbd_writer_cb(0,0,prv);
+ tdnbd_writer_cb(0, 0, prv);
INFO("Written");
@@ -790,58 +840,61 @@ static int tdnbd_close(td_driver_t* driver)
tdnbd_stash_passed_fd(prv->socket, prv->name, 0);
free(prv->name);
} else {
- if(prv->socket >= 0)
+ if (prv->socket >= 0)
close(prv->socket);
prv->socket = -1;
}
return 0;
}
-static void tdnbd_queue_read(td_driver_t* driver, td_request_t treq)
+static void
+tdnbd_queue_read(td_driver_t* driver, td_request_t treq)
{
struct tdnbd_data *prv = (struct tdnbd_data *)driver->data;
int size = treq.secs * driver->info.sector_size;
uint64_t offset = treq.sec * (uint64_t)driver->info.sector_size;
- if(prv->flags & TD_OPEN_SECONDARY) {
+ if (prv->flags & TD_OPEN_SECONDARY)
td_forward_request(treq);
- } else {
- tdnbd_queue_request(prv, NBD_CMD_READ, offset, treq.buf, size, treq, 0);
- }
+ else
+ tdnbd_queue_request(prv, NBD_CMD_READ, offset, treq.buf, size,
+ treq, 0);
}
-static void tdnbd_queue_write(td_driver_t* driver, td_request_t treq)
+static void
+tdnbd_queue_write(td_driver_t* driver, td_request_t treq)
{
struct tdnbd_data *prv = (struct tdnbd_data *)driver->data;
int size = treq.secs * driver->info.sector_size;
uint64_t offset = treq.sec * (uint64_t)driver->info.sector_size;
-
- //memcpy(img + offset, treq.buf, size);
- tdnbd_queue_request(prv, NBD_CMD_WRITE, offset, treq.buf, size, treq, 0);
+ tdnbd_queue_request(prv, NBD_CMD_WRITE,
+ offset, treq.buf, size, treq, 0);
}
-static int tdnbd_get_parent_id(td_driver_t* driver, td_disk_id_t* id)
+static int
+tdnbd_get_parent_id(td_driver_t* driver, td_disk_id_t* id)
{
return TD_NO_PARENT;
}
-static int tdnbd_validate_parent(td_driver_t *driver,
- td_driver_t *parent, td_flag_t flags)
+static int
+tdnbd_validate_parent(td_driver_t *driver,
+ td_driver_t *parent, td_flag_t flags)
{
return -EINVAL;
}
struct tap_disk tapdisk_nbd = {
- .disk_type = "tapdisk_nbd",
- .private_data_size = sizeof(struct tdnbd_data),
- .flags = 0,
- .td_open = tdnbd_open,
- .td_close = tdnbd_close,
- .td_queue_read = tdnbd_queue_read,
- .td_queue_write = tdnbd_queue_write,
- .td_get_parent_id = tdnbd_get_parent_id,
- .td_validate_parent = tdnbd_validate_parent,
+ .disk_type = "tapdisk_nbd",
+ .private_data_size = sizeof(struct tdnbd_data),
+ .flags = 0,
+ .td_open = tdnbd_open,
+ .td_close = tdnbd_close,
+ .td_queue_read = tdnbd_queue_read,
+ .td_queue_write = tdnbd_queue_write,
+ .td_get_parent_id = tdnbd_get_parent_id,
+ .td_validate_parent = tdnbd_validate_parent,
};
View
1 drivers/block-vhd.c
@@ -313,6 +313,7 @@ vhd_free(struct vhd_state *s)
if (_vhd_master != s || !_vhd_zeros)
return;
+ free(s->padbm_buf);
munmap(_vhd_zeros, _vhd_zsize);
_vhd_zsize = 0;
_vhd_zeros = NULL;
View
30 drivers/tapdisk-control.c
@@ -763,11 +763,11 @@ tapdisk_control_open_image(struct tapdisk_ctl_conn *conn,
goto fail_close;
}
- /* For now, lets do this automatically on all 'open' calls
- In the future, we'll probably want a separate call to
- start the NBD server */
+ /*
+ * For now, let's do this automatically on all 'open' calls In the
+ * future, we'll probably want a separate call to start the NBD server
+ */
err = tapdisk_vbd_start_nbdserver(vbd);
-
if (err) {
EPRINTF("failed to start nbdserver: %d\n",err);
goto fail_close;
@@ -818,9 +818,8 @@ tapdisk_control_close_image(struct tapdisk_ctl_conn *conn,
goto out;
}
- if(vbd->nbdserver) {
- tapdisk_nbdserver_pause(vbd->nbdserver);
- }
+ if (vbd->nbdserver)
+ tapdisk_nbdserver_pause(vbd->nbdserver);
do {
err = tapdisk_blktap_remove_device(vbd->tap);
@@ -846,16 +845,18 @@ tapdisk_control_close_image(struct tapdisk_ctl_conn *conn,
if (err)
goto out;
- if(vbd->nbdserver) {
- tapdisk_nbdserver_free(vbd->nbdserver);
- vbd->nbdserver = NULL;
+ if (vbd->nbdserver) {
+ tapdisk_nbdserver_free(vbd->nbdserver);
+ vbd->nbdserver = NULL;
}
tapdisk_vbd_close_vdi(vbd);
- /* NB. vbd->name free should probably belong into close_vdi,
- but the current blktap1 reopen-stuff likely depends on a
- lifetime extended until shutdown. */
+ /*
+ * NB: vbd->name free should probably belong into close_vdi, but the
+ * current blktap1 reopen-stuff likely depends on a lifetime extended
+ * until shutdown
+ */
free(vbd->name);
vbd->name = NULL;
@@ -920,7 +921,8 @@ tapdisk_control_resume_vbd(struct tapdisk_ctl_conn *conn,
response.type = TAPDISK_MESSAGE_RESUME_RSP;
- INFO("Resuming. flags=0x%08x secondary=%p\n", request->u.params.flags, request->u.params.secondary);
+ INFO("Resuming: flags=0x%08x secondary=%p\n",
+ request->u.params.flags, request->u.params.secondary);
vbd = tapdisk_server_get_vbd(request->cookie);
if (!vbd) {
View
15 drivers/tapdisk-disktype.c
@@ -127,16 +127,10 @@ static const disk_info_t valve_disk = {
DISK_TYPE_FILTER,
};
-static const disk_info_t export_disk = {
- "export",
- "export to a remote VDI",
- 0,
-};
-
static const disk_info_t nbd_disk = {
- "nbd",
- "export to a NBD server",
- 0,
+ "nbd",
+ "export to a NBD server",
+ 0,
};
const disk_info_t *tapdisk_disk_types[] = {
@@ -155,7 +149,6 @@ const disk_info_t *tapdisk_disk_types[] = {
[DISK_TYPE_VALVE] = &valve_disk,
[DISK_TYPE_LLPCACHE] = &llpcache_disk,
[DISK_TYPE_LLECACHE] = &llecache_disk,
- [DISK_TYPE_EXPORT] = &export_disk,
[DISK_TYPE_NBD] = &nbd_disk,
0,
};
@@ -180,7 +173,6 @@ extern struct tap_disk tapdisk_lcache;
extern struct tap_disk tapdisk_llpcache;
extern struct tap_disk tapdisk_llecache;
extern struct tap_disk tapdisk_valve;
-extern struct tap_disk tapdisk_export;
extern struct tap_disk tapdisk_nbd;
const struct tap_disk *tapdisk_disk_drivers[] = {
@@ -204,7 +196,6 @@ const struct tap_disk *tapdisk_disk_drivers[] = {
[DISK_TYPE_LLPCACHE] = &tapdisk_llpcache,
[DISK_TYPE_LLECACHE] = &tapdisk_llecache,
[DISK_TYPE_VALVE] = &tapdisk_valve,
- [DISK_TYPE_EXPORT] = &tapdisk_export,
[DISK_TYPE_NBD] = &tapdisk_nbd,
0,
};
View
3 drivers/tapdisk-disktype.h
@@ -44,8 +44,7 @@
#define DISK_TYPE_LLECACHE 12
#define DISK_TYPE_LLPCACHE 13
#define DISK_TYPE_VALVE 14
-#define DISK_TYPE_EXPORT 15
-#define DISK_TYPE_NBD 16
+#define DISK_TYPE_NBD 15
#define DISK_TYPE_NAME_MAX 32
View
366 drivers/tapdisk-fdreceiver.c
@@ -1,3 +1,32 @@
+/*
+ * Copyright (c) 2012, Citrix Systems, 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 <stdlib.h>
#include <unistd.h>
@@ -21,208 +50,213 @@
#define INFO(_f, _a...) tlog_syslog(TLOG_INFO, "nbd: " _f, ##_a)
#define ERROR(_f, _a...) tlog_syslog(TLOG_WARN, "nbd: " _f, ##_a)
-
-void
+static void
td_fdreceiver_recv_fd(event_id_t id, char mode, void *data)
{
- struct td_fdreceiver *fdreceiver=data;
- int ret, cv_flags=0, *fdp, fd=-1;
- long numbytes;
- char iobuf[UNIX_BUFFER_SIZE];
- char buf[CMSG_SPACE(sizeof(fd))];
- struct sockaddr_un unix_socket_name;
-
- struct msghdr msg;
- struct iovec vec;
- struct cmsghdr *cmsg;
-
- numbytes = UNIX_BUFFER_SIZE;
-
- bzero(iobuf,numbytes);
-
- msg.msg_name=&unix_socket_name;
- msg.msg_namelen=sizeof(unix_socket_name);
- vec.iov_base=iobuf;
- vec.iov_len=numbytes;
- msg.msg_iov=&vec;
-
- msg.msg_iovlen=1;
-
- msg.msg_control = buf;
- msg.msg_controllen = sizeof(buf);
-
- ret=recvmsg(fdreceiver->client_fd, &msg, cv_flags);
-
- if(ret == -1) {
- ERROR("Failed to receive the message: %d",ret);
- return;
- }
-
- if(ret>0 && msg.msg_controllen>0) {
- cmsg = CMSG_FIRSTHDR(&msg);
- if(cmsg->cmsg_level == SOL_SOCKET && (cmsg->cmsg_type == SCM_RIGHTS)) {
- fdp = (int*)CMSG_DATA(cmsg);
- fd = *fdp;
+ struct td_fdreceiver *fdreceiver = data;
+ int ret, cv_flags = 0, *fdp, fd = -1;
+ long numbytes;
+ char iobuf[UNIX_BUFFER_SIZE];
+ char buf[CMSG_SPACE(sizeof(fd))];
+ struct sockaddr_un unix_socket_name;
+
+ struct msghdr msg;
+ struct iovec vec;
+ struct cmsghdr *cmsg;
+
+ numbytes = UNIX_BUFFER_SIZE;
+
+ bzero(iobuf, numbytes);
+
+ msg.msg_name = &unix_socket_name;
+ msg.msg_namelen = sizeof(unix_socket_name);
+ vec.iov_base = iobuf;
+ vec.iov_len = numbytes;
+ msg.msg_iov = &vec;
+
+ msg.msg_iovlen = 1;
+
+ msg.msg_control = buf;
+ msg.msg_controllen = sizeof(buf);
+
+ ret = recvmsg(fdreceiver->client_fd, &msg, cv_flags);
+
+ if (ret == -1) {
+ ERROR("Failed to receive the message: %d", ret);
+ return;
+ }
+
+ if (ret > 0 && msg.msg_controllen > 0) {
+ cmsg = CMSG_FIRSTHDR(&msg);
+ if (cmsg->cmsg_level == SOL_SOCKET &&
+ (cmsg->cmsg_type == SCM_RIGHTS)) {
+ fdp = (int*)CMSG_DATA(cmsg);
+ fd = *fdp;
+ } else {
+ ERROR("Failed to recieve a file descriptor");
+ }
} else {
- ERROR("Failed to recieve a file descriptor");
+ fd = -1;
}
- } else {
- fd=-1;
- }
-
- if(ret<numbytes)
- numbytes = ret;
-
- INFO("Recieved fd with message: %s",iobuf);
-
- /* We're done with this connection, it was only transiently used to
- connect the client */
- close(fdreceiver->client_fd);
- fdreceiver->client_fd = -1;
-
- tapdisk_server_unregister_event(fdreceiver->client_event_id);
- fdreceiver->client_event_id = -1;
-
- /* It is the responsibility of this callback function to arrange
- that the fd is eventually closed */
- fdreceiver->callback(fd, iobuf, fdreceiver->callback_data);
+
+ if (ret < numbytes)
+ numbytes = ret;
+
+ INFO("Recieved fd with message: %s", iobuf);
+
+ /*
+ * We're done with this connection, it was only transiently used to
+ * connect the client
+ */
+ close(fdreceiver->client_fd);
+ fdreceiver->client_fd = -1;
+
+ tapdisk_server_unregister_event(fdreceiver->client_event_id);
+ fdreceiver->client_event_id = -1;
+
+ /*
+ * It is the responsibility of this callback function to arrange that
+ * the fd is eventually closed
+ */
+ fdreceiver->callback(fd, iobuf, fdreceiver->callback_data);
}
-void
+static void
td_fdreceiver_accept_fd(event_id_t id, char mode, void *data)
{
- struct sockaddr_storage their_addr;
- socklen_t sin_size = sizeof(their_addr);
- struct td_fdreceiver *fdreceiver=data;
- int new_fd;
+ struct sockaddr_storage their_addr;
+ socklen_t sin_size = sizeof(their_addr);
+ struct td_fdreceiver *fdreceiver = data;
+ int new_fd;
- INFO("Unix domain socket is ready to accept");
+ INFO("Unix domain socket is ready to accept");
- new_fd = accept(fdreceiver->fd, (struct sockaddr *)&their_addr, &sin_size);
+ new_fd = accept(fdreceiver->fd,
+ (struct sockaddr *)&their_addr, &sin_size);
- if(fdreceiver->client_fd != -1) {
- ERROR("td_fdreceiver_accept_fd: an only cope with one connection at once to the unix domain socket!");
+ if (fdreceiver->client_fd != -1) {
+ ERROR("td_fdreceiver_accept_fd: can only cope with one connec"
+ "tion at once to the unix domain socket!");
close(new_fd);
return;
- }
+ }
- fdreceiver->client_fd = new_fd;
+ fdreceiver->client_fd = new_fd;
- fdreceiver->client_event_id =
- tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
- fdreceiver->client_fd, 0,
- td_fdreceiver_recv_fd,
- fdreceiver);
+ fdreceiver->client_event_id =
+ tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
+ fdreceiver->client_fd, 0,
+ td_fdreceiver_recv_fd,
+ fdreceiver);
- if(fdreceiver->client_event_id < 0) {
- ERROR("td_fdreceiver_accept_fd: failed to register event (errno=%d)",errno);
+ if (fdreceiver->client_event_id < 0) {
+ ERROR("td_fdreceiver_accept_fd: failed to register event "
+ "(errno=%d)", errno);
close(new_fd);
fdreceiver->client_fd = -1;
- }
+ }
}
-void td_fdreceiver_stop(struct td_fdreceiver *fdreceiver)
+void
+td_fdreceiver_stop(struct td_fdreceiver *fdreceiver)
{
- if(fdreceiver->client_fd >= 0) {
+ if (fdreceiver->client_fd >= 0)
close(fdreceiver->client_fd);
- }
-
- if(fdreceiver->client_event_id >= 0) {
+
+ if (fdreceiver->client_event_id >= 0)
tapdisk_server_unregister_event(fdreceiver->client_event_id);
- }
-
- if(fdreceiver->fd >= 0) {
+
+ if (fdreceiver->fd >= 0)
close(fdreceiver->fd);
- }
-
- if(fdreceiver->fd_event_id >= 0) {
+
+ if (fdreceiver->fd_event_id >= 0)
tapdisk_server_unregister_event(fdreceiver->fd_event_id);
- }
-
- if(fdreceiver->path != NULL) {
+
+ if (fdreceiver->path != NULL) {
unlink(fdreceiver->path);
free(fdreceiver->path);
- }
+ }
+ free(fdreceiver);
}
-
struct td_fdreceiver *
td_fdreceiver_start(char *path, fd_cb_t callback, void *data)
{
- unsigned int s = -1;
- struct sockaddr_un local;
- int len;
- int err;
- struct td_fdreceiver *fdreceiver;
-
- fdreceiver=malloc(sizeof(struct td_fdreceiver));
- if(!fdreceiver) {
- ERROR("td_fdreceiver_start: error allocating memory for fdreceiver (path=%s)",path);
- goto error;
- }
-
- fdreceiver->path=strdup(path);
- fdreceiver->fd=-1;
- fdreceiver->fd_event_id=-1;
- fdreceiver->client_fd=-1;
- fdreceiver->client_event_id=-1;
- fdreceiver->callback=callback;
- fdreceiver->callback_data=data;
-
- snprintf(local.sun_path, sizeof(local.sun_path), "%s", path);
- local.sun_family = AF_UNIX;
-
- /* N.b. here we unlink anything that was there before - be very
- careful with the paths you pass to this function! */
- unlink(local.sun_path);
- len = strlen(local.sun_path) + sizeof(local.sun_family);
-
- s = socket(AF_UNIX, SOCK_STREAM, 0);
-
- if(s<0) {
- ERROR("td_fdreceiver_start: error creating socket (path=%s)",path);
- goto error;
- }
-
- err = bind(s, (struct sockaddr *)&local, len);
-
- if(err<0) {
- ERROR("td_fdreceiver_start: error binding (path=%s)",path);
- goto error;
- }
-
- err = listen(s, 5);
- if(err < 0) {
- ERROR("td_fdreceiver_start: error listening (path=%s)",path);
- goto error;
- }
-
- fdreceiver->fd = s;
-
- fdreceiver->fd_event_id =
- tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
- fdreceiver->fd, 0,
- td_fdreceiver_accept_fd,
- fdreceiver);
-
- if(fdreceiver->fd_event_id < 0) {
- ERROR("td_fdreceiver_start: error registering event (path=%s)",path);
- goto error;
- }
-
- INFO("Set up local unix domain socket on path '%s'",path);
-
- return fdreceiver;
+ unsigned int s = -1;
+ struct sockaddr_un local;
+ int len;
+ int err;
+ struct td_fdreceiver *fdreceiver;
+
+ fdreceiver = malloc(sizeof(struct td_fdreceiver));
+ if (!fdreceiver) {
+ ERROR("td_fdreceiver_start: error allocating memory for "
+ "fdreceiver (path=%s)", path);
+ goto error;
+ }
+
+ fdreceiver->path = strdup(path);
+ fdreceiver->fd = -1;
+ fdreceiver->fd_event_id = -1;
+ fdreceiver->client_fd = -1;
+ fdreceiver->client_event_id = -1;
+ fdreceiver->callback = callback;
+ fdreceiver->callback_data = data;
+
+ snprintf(local.sun_path, sizeof(local.sun_path), "%s", path);
+ local.sun_family = AF_UNIX;
+
+ /*
+ * NB: here we unlink anything that was there before - be very careful
+ * with the paths you pass to this function!
+ */
+ unlink(local.sun_path);
+ len = strlen(local.sun_path) + sizeof(local.sun_family);
+
+ s = socket(AF_UNIX, SOCK_STREAM, 0);
+
+ if (s < 0) {
+ ERROR("td_fdreceiver_start: error creating socket "
+ "(path=%s)", path);
+ goto error;
+ }
+
+ err = bind(s, (struct sockaddr *)&local, len);
+ if (err < 0) {
+ ERROR("td_fdreceiver_start: error binding (path=%s)", path);
+ goto error;
+ }
+
+ err = listen(s, 5);
+ if (err < 0) {
+ ERROR("td_fdreceiver_start: error listening (path=%s)", path);
+ goto error;
+ }
+
+ fdreceiver->fd = s;
+
+ fdreceiver->fd_event_id =
+ tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
+ fdreceiver->fd, 0,
+ td_fdreceiver_accept_fd,
+ fdreceiver);
+
+ if (fdreceiver->fd_event_id < 0) {
+ ERROR("td_fdreceiver_start: error registering event "
+ "(path=%s)", path);
+ goto error;
+ }
+
+ INFO("Set up local unix domain socket on path '%s'", path);
+
+ return fdreceiver;
error:
- if(fdreceiver) {
free(fdreceiver);
- }
- if(s>=0)
- close(s);
+ if (s >= 0)
+ close(s);
- return NULL;
+ return NULL;
}
-
View
43 drivers/tapdisk-fdreceiver.h
@@ -1,3 +1,32 @@
+/*
+ * Copyright (c) 2012, Citrix Systems, 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.
+ */
+
/* Unix domain socket fd receiver */
typedef void (*fd_cb_t) (int fd, char *msg, void *data);
@@ -6,14 +35,14 @@ struct td_fdreceiver *td_fdreceiver_start(char *path, fd_cb_t, void *data);
void td_fdreceiver_stop(struct td_fdreceiver *);
struct td_fdreceiver {
- char *path;
+ char *path;
- int fd;
- int fd_event_id;
+ int fd;
+ int fd_event_id;
- int client_fd;
- int client_event_id;
+ int client_fd;
+ int client_event_id;
- fd_cb_t callback;
- void *callback_data;
+ fd_cb_t callback;
+ void *callback_data;
};
View
13 drivers/tapdisk-nbd.h
@@ -43,12 +43,13 @@ enum {
#define NBD_CMD_FLAG_FUA (1<<16)
/* values for flags field */
-#define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
-#define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
-#define NBD_FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */
-#define NBD_FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */
-#define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm - rotational media */
-#define NBD_FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */
+#define NBD_FLAG_HAS_FLAGS (1 << 0) /* Flags are there */
+#define NBD_FLAG_READ_ONLY (1 << 1) /* Device is read-only */
+#define NBD_FLAG_SEND_FLUSH (1 << 2) /* Send FLUSH */
+#define NBD_FLAG_SEND_FUA (1 << 3) /* Send FUA (Force Unit Access) */
+#define NBD_FLAG_ROTATIONAL (1 << 4) /* Use elevator algorithm -
+ rotational media */
+#define NBD_FLAG_SEND_TRIM (1 << 5) /* Send TRIM (discard) */
#define nbd_cmd(req) ((req)->cmd[0])
View
474 drivers/tapdisk-nbdserver.c
@@ -1,3 +1,32 @@
+/*
+ * Copyright (c) 2012, Citrix Systems, 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 <stdlib.h>
#include <unistd.h>
@@ -27,7 +56,7 @@
#define NBD_SERVER_NUM_REQS TAPDISK_DATA_REQUESTS
-#define TAPDISK_NBDSERVER_LISTENING_SOCK_PATH "/var/run/blktap-control/nbdserver"
+#define TAPDISK_NBDSERVER_LISTEN_SOCK_PATH "/var/run/blktap-control/nbdserver"
#define TAPDISK_NBDSERVER_MAX_PATH_LEN 256
/*
@@ -43,16 +72,12 @@ struct td_nbdserver_req {
struct td_iovec iov;
};
-void
-tapdisk_nbdserver_disable_client(td_nbdserver_client_t *client);
-void
-tapdisk_nbdserver_clientcb(event_id_t id, char mode, void *data);
-int
-tapdisk_nbdserver_setup_listening_socket(td_nbdserver_t *server);
-int
-tapdisk_nbdserver_unpause(td_nbdserver_t *server);
+static void tapdisk_nbdserver_disable_client(td_nbdserver_client_t *client);
+static void tapdisk_nbdserver_clientcb(event_id_t id, char mode, void *data);
+int tapdisk_nbdserver_setup_listening_socket(td_nbdserver_t *server);
+int tapdisk_nbdserver_unpause(td_nbdserver_t *server);
-td_nbdserver_req_t *
+static td_nbdserver_req_t *
tapdisk_nbdserver_alloc_request(td_nbdserver_client_t *client)
{
td_nbdserver_req_t *req = NULL;
@@ -63,24 +88,26 @@ tapdisk_nbdserver_alloc_request(td_nbdserver_client_t *client)
return req;
}
-void
-tapdisk_nbdserver_free_request(td_nbdserver_client_t *client, td_nbdserver_req_t *req)
+static void
+tapdisk_nbdserver_free_request(td_nbdserver_client_t *client,
+ td_nbdserver_req_t *req)
{
- if(client->n_reqs_free >= client->n_reqs) {
- ERROR("Error, trying to free a client, but the free list is full! leaking!");
+ if (client->n_reqs_free >= client->n_reqs) {
+ ERROR("Error, trying to free a client, but the free list "
+ "is full! leaking!");
return;
}
client->reqs_free[client->n_reqs_free++] = req;
}
-void
+static void
tapdisk_nbdserver_reqs_free(td_nbdserver_client_t *client)
{
if (client->reqs) {
free(client->reqs);
client->reqs = NULL;
}
-
+
if (client->iovecs) {
free(client->iovecs);
client->iovecs = NULL;
@@ -92,7 +119,7 @@ tapdisk_nbdserver_reqs_free(td_nbdserver_client_t *client)
}
}
-int
+static int
tapdisk_nbdserver_reqs_init(td_nbdserver_client_t *client, int n_reqs)
{
int i, err;
@@ -120,7 +147,7 @@ tapdisk_nbdserver_reqs_init(td_nbdserver_client_t *client, int n_reqs)
client->n_reqs_free = 0;
for (i = 0; i < n_reqs; i++) {
- client->reqs[i].vreq.iov=&client->iovecs[i];
+ client->reqs[i].vreq.iov = &client->iovecs[i];
tapdisk_nbdserver_free_request(client, &cli