From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:38169) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T60o3-0005Lr-86 for qemu-devel@nongnu.org; Mon, 27 Aug 2012 11:00:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T60nu-0005bP-La for qemu-devel@nongnu.org; Mon, 27 Aug 2012 11:00:47 -0400 Received: from mail-ey0-f173.google.com ([209.85.215.173]:39158) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T60nu-0005bG-EB for qemu-devel@nongnu.org; Mon, 27 Aug 2012 11:00:38 -0400 Received: by eaac13 with SMTP id c13so1273715eaa.4 for ; Mon, 27 Aug 2012 08:00:37 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Mon, 27 Aug 2012 17:00:15 +0200 Message-Id: <1346079626-16386-3-git-send-email-pbonzini@redhat.com> In-Reply-To: <1346079626-16386-1-git-send-email-pbonzini@redhat.com> References: <1346079626-16386-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [RFC PATCH 02/13] nbd: pass NBDClient to nbd_send_negotiate List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@gmail.com We will need the NBDClient in nbd_send_negotiate to store the export requested by the client. Signed-off-by: Paolo Bonzini --- nbd.c | 76 ++++++++++++++++++++++++++++++++++++------------------------------- 1 file modificato, 41 inserzioni(+), 35 rimozioni(-) diff --git a/nbd.c b/nbd.c index 8201b7a..c1edbeb 100644 --- a/nbd.c +++ b/nbd.c @@ -78,6 +78,39 @@ #define NBD_OPT_EXPORT_NAME (1 << 0) +/* Definitions for opaque data types */ + +typedef struct NBDRequest NBDRequest; + +struct NBDRequest { + QSIMPLEQ_ENTRY(NBDRequest) entry; + NBDClient *client; + uint8_t *data; +}; + +struct NBDExport { + BlockDriverState *bs; + off_t dev_offset; + off_t size; + uint32_t nbdflags; + QSIMPLEQ_HEAD(, NBDRequest) requests; +}; + +struct NBDClient { + int refcount; + void (*close)(NBDClient *client); + + NBDExport *exp; + int sock; + + Coroutine *recv_coroutine; + + CoMutex send_lock; + Coroutine *send_coroutine; + + int nb_requests; +}; + /* That's all folks */ ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read) @@ -209,8 +242,9 @@ int unix_socket_outgoing(const char *path) Request (type == 2) */ -static int nbd_send_negotiate(int csock, off_t size, uint32_t flags) +static int nbd_send_negotiate(NBDClient *client) { + int csock = client->sock; char buf[8 + 8 + 8 + 128]; int rc; @@ -228,9 +262,9 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags) TRACE("Beginning negotiation."); memcpy(buf, "NBDMAGIC", 8); cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC); - cpu_to_be64w((uint64_t*)(buf + 16), size); + cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size); cpu_to_be32w((uint32_t*)(buf + 24), - flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | + client->exp->nbdflags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA); memset(buf + 28, 0, 124); @@ -615,35 +649,6 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply) typedef struct NBDRequest NBDRequest; -struct NBDRequest { - QSIMPLEQ_ENTRY(NBDRequest) entry; - NBDClient *client; - uint8_t *data; -}; - -struct NBDExport { - BlockDriverState *bs; - off_t dev_offset; - off_t size; - uint32_t nbdflags; - QSIMPLEQ_HEAD(, NBDRequest) requests; -}; - -struct NBDClient { - int refcount; - void (*close)(NBDClient *client); - - NBDExport *exp; - int sock; - - Coroutine *recv_coroutine; - - CoMutex send_lock; - Coroutine *send_coroutine; - - int nb_requests; -}; - static void nbd_client_get(NBDClient *client) { client->refcount++; @@ -977,13 +982,14 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock, void (*close)(NBDClient *)) { NBDClient *client; - if (nbd_send_negotiate(csock, exp->size, exp->nbdflags) < 0) { - return NULL; - } client = g_malloc0(sizeof(NBDClient)); client->refcount = 1; client->exp = exp; client->sock = csock; + if (nbd_send_negotiate(client) < 0) { + g_free(client); + return NULL; + } client->close = close; qemu_co_mutex_init(&client->send_lock); qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client); -- 1.7.11.2