Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Coding style cleanup for NBD code to match the rest of blktap code.

Linux coding style is used for this repository.
  • Loading branch information...
commit 27ecad564a45dba904e05f6fbbf78c6d42800fcb 1 parent 745dce7
@andreil andreil authored
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
658 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,81 @@ 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 __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 +258,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 +291,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,31 +332,32 @@ 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);
@@ -328,37 +365,40 @@ void disable_write_queue(struct tdnbd_data *prv)
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,118 +410,117 @@ 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;
-
- /* Nb, do this here otherwise we cancel the request that has just been moved */
+ prv->curr_reply_req = NULL;
- 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;
@@ -489,13 +528,14 @@ int tdnbd_wait_read(int fd)
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,110 +546,110 @@ 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);
@@ -617,8 +657,8 @@ int tdnbd_nbd_negotiate(struct tdnbd_data *prv, td_driver_t *driver)
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 +670,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 +701,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 +718,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 +734,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,17 +755,16 @@ 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...");
@@ -729,7 +772,7 @@ static int tdnbd_open(td_driver_t* driver, const char* name, td_flag_t flags)
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;
}
@@ -737,32 +780,32 @@ static int tdnbd_open(td_driver_t* driver, const char* name, td_flag_t flags)
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 +820,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,7 +833,7 @@ 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;
}
@@ -798,50 +841,53 @@ static int tdnbd_close(td_driver_t* driver)
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
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
6 drivers/tapdisk-disktype.c
@@ -128,9 +128,9 @@ static const disk_info_t valve_disk = {
};
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[] = {
View
365 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,212 @@
#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);
- }
+ }
}
-
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, &client->reqs[i]);
}
@@ -131,170 +158,173 @@ tapdisk_nbdserver_reqs_init(td_nbdserver_client_t *client, int n_reqs)
return err;
}
-td_nbdserver_client_t *
+static td_nbdserver_client_t *
tapdisk_nbdserver_alloc_client(td_nbdserver_t *server)
{
- td_nbdserver_client_t *client=NULL;
+ td_nbdserver_client_t *client = NULL;
int err;
INFO("Alloc client");
- client=malloc(sizeof(td_nbdserver_client_t));
- if(!client) {
- ERROR("Couldn't allocate client structure: %s",strerror(errno));
+ client = malloc(sizeof(td_nbdserver_client_t));
+ if (!client) {
+ ERROR("Couldn't allocate client structure: %s",
+ strerror(errno));
goto fail;
}
bzero(client, sizeof(td_nbdserver_client_t));
err = tapdisk_nbdserver_reqs_init(client, NBD_SERVER_NUM_REQS);
- if(err<0) {
- ERROR("Couldn't allocate client reqs: %d",err);
+ if (err < 0) {
+ ERROR("Couldn't allocate client reqs: %d", err);
goto fail;
}
- client->client_fd=-1;
- client->client_event_id=-1;
- client->server=server;
+ client->client_fd = -1;
+ client->client_event_id = -1;
+ client->server = server;
INIT_LIST_HEAD(&client->clientlist);
list_add(&client->clientlist, &server->clients);
- client->paused=0;
+ client->paused = 0;
return client;
fail:
- if(client) {
+ if (client) {
free(client);
- client=NULL;
+ client = NULL;
}
return client;
}
-void
+static void
tapdisk_nbdserver_free_client(td_nbdserver_client_t *client)
{
- INFO("Free client");
+ INFO("Free client");
- if(!client) {
+ if (!client) {
ERROR("Attempt to free NULL pointer!");
return;
}
- if(client->client_event_id >= 0) {
+ if (client->client_event_id >= 0)
tapdisk_nbdserver_disable_client(client);
- }
list_del(&client->clientlist);
tapdisk_nbdserver_reqs_free(client);
free(client);
}
-int
+static int
tapdisk_nbdserver_enable_client(td_nbdserver_client_t *client)
{
- INFO("Enable client");
+ INFO("Enable client");
- if(client->client_event_id >= 0) {
+ if (client->client_event_id >= 0) {
ERROR("Attempting to enable an already-enabled client");
return -1;
}
- if(client->client_fd < 0) {
+ if (client->client_fd < 0) {
ERROR("Attempting to register events on a closed client");
return -1;
}
- client->client_event_id =
- tapdisk_server_register_event(SCHEDULER_POLL_READ_FD,
- client->client_fd, 0,
- tapdisk_nbdserver_clientcb,
- client);
+ client->client_event_id = tapdisk_server_register_event(
+ SCHEDULER_POLL_READ_FD,
+ client->client_fd, 0,
+ tapdisk_nbdserver_clientcb,
+ client);
- if(client->client_event_id < 0) {
- ERROR("Error registering events on client: %d",client->client_event_id);
+ if (client->client_event_id < 0) {
+ ERROR("Error registering events on client: %d",
+ client->client_event_id);
return client->client_event_id;
}
return client->client_event_id;
}
-void
+static void
tapdisk_nbdserver_disable_client(td_nbdserver_client_t *client)
{
- INFO("Disable client");
+ INFO("Disable client");
- if(client->client_event_id < 0) {
+ if (client->client_event_id < 0) {
ERROR("Attempting to disable an already-disabled client");
return;
}
-
+
tapdisk_server_unregister_event(client->client_event_id);
- client->client_event_id=-1;
+ client->client_event_id = -1;
}
-void *get_in_addr(struct sockaddr *sa)
+static void
+*get_in_addr(struct sockaddr *sa)
{
- if (sa->sa_family == AF_INET) {
- return &(((struct sockaddr_in*)sa)->sin_addr);
- }
+ if (sa->sa_family == AF_INET)
+ return &(((struct sockaddr_in*)sa)->sin_addr);
- return &(((struct sockaddr_in6*)sa)->sin6_addr);
+ return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
-
-void
+static void
__tapdisk_nbdserver_request_cb(td_vbd_request_t *vreq, int error,
- void *token, int final)
+ void *token, int final)
{
- td_nbdserver_client_t *client = token;
- td_nbdserver_req_t *req = containerof(vreq, td_nbdserver_req_t, vreq);
- struct nbd_reply reply;
- int tosend=0;
- int sent=0;
- int len=0;
-
- reply.magic = htonl(NBD_REPLY_MAGIC);
- reply.error = htonl(error);
- memcpy(reply.handle, req->id, sizeof(reply.handle));
-
- if(client->client_fd < 0) {
+ td_nbdserver_client_t *client = token;
+ td_nbdserver_req_t *req = containerof(vreq, td_nbdserver_req_t, vreq);
+ struct nbd_reply reply;
+ int tosend = 0;
+ int sent = 0;
+ int len = 0;
+
+ reply.magic = htonl(NBD_REPLY_MAGIC);
+ reply.error = htonl(error);
+ memcpy(reply.handle, req->id, sizeof(reply.handle));
+
+ if (client->client_fd < 0) {
ERROR("Finishing request for client that has disappeared");
goto finish;
- }
+ }
- send(client->client_fd, &reply, sizeof(reply), 0);
+ send(client->client_fd, &reply, sizeof(reply), 0);
- switch(vreq->op) {
- case TD_OP_READ:
- tosend=len=vreq->iov->secs << SECTOR_SHIFT;
- while(tosend>0) {
- sent = send(client->client_fd, vreq->iov->base+(len-tosend), tosend, 0);
- if(sent <= 0) {
- ERROR("Short send or error in callback: %d",sent);
+ switch(vreq->op) {
+ case TD_OP_READ:
+ tosend = len = vreq->iov->secs << SECTOR_SHIFT;
+ while (tosend > 0) {
+ sent = send(client->client_fd,
+ vreq->iov->base + (len - tosend),
+ tosend, 0);
+ if (sent <= 0) {
+ ERROR("Short send or error in "
+ "callback: %d", sent);
goto finish;
}
-
+
tosend -= sent;
}
break;
- default:
+ default:
break;
- }
-
+ }
+
finish:
- free(vreq->iov->base);
- tapdisk_nbdserver_free_request(client,req);
+ free(vreq->iov->base);
+ tapdisk_nbdserver_free_request(client, req);
}
-void
-tapdisk_nbdserver_newclient_fd(td_nbdserver_t *server, int new_fd);
-void
+static void tapdisk_nbdserver_newclient_fd(td_nbdserver_t *server, int new_fd);
+
+static void
tapdisk_nbdserver_clientcb(event_id_t id, char mode, void *data)
{
- td_nbdserver_client_t *client=data;
- td_nbdserver_t *server=client->server;
+ td_nbdserver_client_t *client = data;
+ td_nbdserver_t *server = client->server;
int rc;
int len;
int hdrlen;
@@ -304,37 +334,38 @@ tapdisk_nbdserver_clientcb(event_id_t id, char mode, void *data)
td_vbd_request_t *vreq;
struct nbd_request request;
- td_nbdserver_req_t *req=tapdisk_nbdserver_alloc_request(client);
+ td_nbdserver_req_t *req = tapdisk_nbdserver_alloc_request(client);
- if(req==NULL) {
+ if (req == NULL) {
ERROR("Couldn't allocate request in clientcb - killing client");
tapdisk_nbdserver_free_client(client);
return;
}
- vreq=&req->vreq;
+ vreq = &req->vreq;
memset(req, 0, sizeof(td_nbdserver_req_t));
/* Read the request the client has sent */
hdrlen = sizeof(struct nbd_request);
-
+
n = 0;
ptr = (char *) &request;
- while(n<hdrlen) {
- rc=recv(fd, ptr+n, hdrlen - n, 0);
- if(rc==0) {
- INFO("Client closed connection");
- goto fail;
- }
- if(rc < 0) {
- ERROR("Bad return in nbdserver_clientcb. Closing connection");
- goto fail;
- }
- n += rc;
+ while (n < hdrlen) {
+ rc = recv(fd, ptr + n, hdrlen - n, 0);
+ if (rc == 0) {
+ INFO("Client closed connection");
+ goto fail;
+ }
+ if (rc < 0) {
+ ERROR("Bad return in nbdserver_clientcb. Closing "
+ "connection");
+ goto fail;
+ }
+ n += rc;
}
-
- if(request.magic != htonl(NBD_REQUEST_MAGIC)) {
+
+ if (request.magic != htonl(NBD_REQUEST_MAGIC)) {
ERROR("Not enough magic");
goto fail;
}
@@ -342,75 +373,77 @@ tapdisk_nbdserver_clientcb(event_id_t id, char mode, void *data)
request.from = ntohll(request.from);
request.type = ntohl(request.type);
len = ntohl(request.len);
- if(((len & 0x1ff) != 0) || ((request.from & 0x1ff) != 0)) {
- ERROR("Non sector-aligned request (%"PRIu64",%d)",request.from,len);
-
+ if (((len & 0x1ff) != 0) || ((request.from & 0x1ff) != 0)) {
+ ERROR("Non sector-aligned request (%"PRIu64", %d)",
+ request.from, len);
}
bzero(req->id, sizeof(req->id));
memcpy(req->id, request.handle, sizeof(request.handle));
-
- rc=posix_memalign(&req->iov.base, 512, len);
- if(rc<0) {
- ERROR("posix_memalign failed (%d)",rc);
+
+ rc = posix_memalign(&req->iov.base, 512, len);
+ if (rc < 0) {
+ ERROR("posix_memalign failed (%d)", rc);
goto fail;
}
- vreq->sec=request.from >> SECTOR_SHIFT;
- vreq->iovcnt=1;
- vreq->iov=&req->iov;
+
+ vreq->sec = request.from >> SECTOR_SHIFT;
+ vreq->iovcnt = 1;
+ vreq->iov = &req->iov;
vreq->iov->secs = len >> SECTOR_SHIFT;
vreq->token = client;
vreq->cb = __tapdisk_nbdserver_request_cb;
vreq->name = req->id;
vreq->vbd = server->vbd;
-
+
switch(request.type) {
case NBD_CMD_READ:
- vreq->op=TD_OP_READ;
+ vreq->op = TD_OP_READ;
break;
case NBD_CMD_WRITE:
- vreq->op=TD_OP_WRITE;
-
- n=0;
- while(n<len) {
- rc = recv(fd, vreq->iov->base+n, (len-n), 0);
- if(rc <= 0) {
- ERROR("Short send or error in callback: %d",rc);
+ vreq->op = TD_OP_WRITE;
+
+ n = 0;
+ while (n < len) {
+ rc = recv(fd, vreq->iov->base + n, (len - n), 0);
+ if (rc <= 0) {
+ ERROR("Short send or error in "
+ "callback: %d", rc);
goto fail;
}
-
+
n += rc;
};
break;
case NBD_CMD_DISC:
- INFO("Received close message. Sending reconnect header");
- tapdisk_nbdserver_free_client(client);
- INFO("About to send initial connection message");
- tapdisk_nbdserver_newclient_fd(server, fd);
- INFO("Sent");
- return;
+ INFO("Received close message. Sending reconnect "
+ "header");
+ tapdisk_nbdserver_free_client(client);
+ INFO("About to send initial connection message");
+ tapdisk_nbdserver_newclient_fd(server, fd);
+ INFO("Sent");
+ return;
default:
- ERROR("Unsupported operation: 0x%x",request.type);
- goto fail;
+ ERROR("Unsupported operation: 0x%x", request.type);
+ goto fail;
}
rc = tapdisk_vbd_queue_request(server->vbd, vreq);
if (rc) {
- ERROR("tapdisk_vbd_queue_request failed: %d",rc);
+ ERROR("tapdisk_vbd_queue_request failed: %d", rc);
goto fail;
}
return;
fail:
-
tapdisk_nbdserver_free_client(client);
return;
}
-void
+static void
tapdisk_nbdserver_newclient_fd(td_nbdserver_t *server, int new_fd)
{
char buffer[256];
@@ -424,28 +457,27 @@ tapdisk_nbdserver_newclient_fd(td_nbdserver_t *server, int new_fd)
memcpy(buffer, "NBDMAGIC", 8);
tmp64 = htonll(NBD_NEGOTIATION_MAGIC);
- memcpy(buffer+8, &tmp64, sizeof(tmp64));
+ memcpy(buffer + 8, &tmp64, sizeof(tmp64));
tmp64 = htonll(server->info.size * server->info.sector_size);
- memcpy(buffer+16, &tmp64, sizeof(tmp64));
+ memcpy(buffer + 16, &tmp64, sizeof(tmp64));
tmp32 = htonl(0);
- memcpy(buffer+24, &tmp32, sizeof(tmp32));
- bzero(buffer+28, 124);
-
- rc=send(new_fd, buffer, 152, 0);
+ memcpy(buffer + 24, &tmp32, sizeof(tmp32));
+ bzero(buffer + 28, 124);
+
+ rc = send(new_fd, buffer, 152, 0);
- if(rc<152) {
+ if (rc < 152) {
close(new_fd);
INFO("Short write in negotiation!");
}
INFO("About to alloc client");
- td_nbdserver_client_t *client=tapdisk_nbdserver_alloc_client(server);
- INFO("Got an allocated client at %p",client);
+ td_nbdserver_client_t *client = tapdisk_nbdserver_alloc_client(server);
+ INFO("Got an allocated client at %p", client);
client->client_fd = new_fd;
INFO("About to enable client");
-
- if(tapdisk_nbdserver_enable_client(client) < 0) {
+ if (tapdisk_nbdserver_enable_client(client) < 0) {
ERROR("Error enabling client");
tapdisk_nbdserver_free_client(client);
close(new_fd);
@@ -453,40 +485,40 @@ tapdisk_nbdserver_newclient_fd(td_nbdserver_t *server, int new_fd)
}
}
-void